libvirt.c 78.7 KB
Newer Older
D
Daniel Veillard 已提交
1
/*
2
 * libvirt.c: Main interfaces for the libvirt library to handle virtualization
D
Daniel Veillard 已提交
3 4
 *           domains from a process running in domain 0
 *
5
 * Copyright (C) 2005,2006 Red Hat, Inc.
D
Daniel Veillard 已提交
6 7 8 9 10 11
 *
 * See COPYING.LIB for the License of this software
 *
 * Daniel Veillard <veillard@redhat.com>
 */

12
#include "libvirt/libvirt.h"
D
Daniel Veillard 已提交
13

14
#include <stdio.h>
15
#include <stdlib.h>
16
#include <string.h>
17 18 19
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
20

21 22 23
#include <libxml/parser.h>
#include <libxml/xpath.h>

24
#include <xs.h>
25

D
Daniel Veillard 已提交
26
#include "internal.h"
27
#include "driver.h"
28
#include "xen_internal.h"
29
#include "xend_internal.h"
30
#include "xs_internal.h"
31
#include "xm_internal.h"
32
#include "proxy_internal.h"
33
#include "xml.h"
34
#include "test.h"
D
Daniel P. Berrange 已提交
35
#include "qemu_internal.h"
36

D
Daniel Veillard 已提交
37 38 39 40
/*
 * TODO:
 * - use lock to protect against concurrent accesses ?
 * - use reference counting to garantee coherent pointer state ?
41
 * - memory wrappers for malloc/free ?
D
Daniel Veillard 已提交
42 43
 */

44
static virDriverPtr virDriverTab[MAX_DRIVERS];
45
static virNetworkDriverPtr virNetworkDriverTab[MAX_DRIVERS];
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
static int initialized = 0;

/**
 * virInitialize:
 *
 * Initialize the library. It's better to call this routine at startup
 * in multithreaded applications to avoid potential race when initializing
 * the library.
 *
 * Returns 0 in case of success, -1 in case of error
 */
int
virInitialize(void)
{
    int i;

    if (initialized)
        return(0);
64
    initialized = 1;
65

66 67 68
    if (!bindtextdomain(GETTEXT_PACKAGE, LOCALEBASEDIR))
        return (-1);

69 70 71
    /*
     * should not be needed but...
     */
72
    for (i = 0;i < MAX_DRIVERS;i++) {
73
         virDriverTab[i] = NULL;
74 75
         virNetworkDriverTab[i] = NULL;
    }
76 77 78 79 80

    /*
     * Note that the order is important the first ones have a higher priority
     */
    xenHypervisorRegister();
81
    xenProxyRegister();
82 83
    xenDaemonRegister();
    xenStoreRegister();
84
    xenXMRegister();
85
    testRegister();
D
Daniel P. Berrange 已提交
86 87
    qemuRegister();

88 89 90 91 92
    return(0);
}



D
Daniel Veillard 已提交
93 94 95 96 97 98 99 100 101
/**
 * virLibConnError:
 * @conn: the connection if available
 * @error: the error noumber
 * @info: extra information string
 *
 * Handle an error at the connection level
 */
static void
102 103
virLibConnError(virConnectPtr conn, virErrorNumber error, const char *info)
{
D
Daniel Veillard 已提交
104
    const char *errmsg;
105

D
Daniel Veillard 已提交
106 107 108 109
    if (error == VIR_ERR_OK)
        return;

    errmsg = __virErrorMsg(error, info);
110
    __virRaiseError(conn, NULL, NULL, VIR_FROM_NONE, error, VIR_ERR_ERROR,
D
Daniel Veillard 已提交
111 112 113 114 115 116 117 118 119 120 121 122
                    errmsg, info, NULL, 0, 0, errmsg, info);
}

/**
 * virLibConnError:
 * @conn: the connection if available
 * @error: the error noumber
 * @info: extra information string
 *
 * Handle an error at the connection level
 */
static void
123 124 125
virLibDomainError(virDomainPtr domain, virErrorNumber error,
                  const char *info)
{
D
Daniel Veillard 已提交
126 127
    virConnectPtr conn = NULL;
    const char *errmsg;
128

D
Daniel Veillard 已提交
129 130 131 132 133 134 135
    if (error == VIR_ERR_OK)
        return;

    errmsg = __virErrorMsg(error, info);
    if (error != VIR_ERR_INVALID_DOMAIN) {
        conn = domain->conn;
    }
136
    __virRaiseError(conn, domain, NULL, VIR_FROM_DOM, error, VIR_ERR_ERROR,
D
Daniel Veillard 已提交
137 138 139
                    errmsg, info, NULL, 0, 0, errmsg, info);
}

140
/**
141 142 143 144
 * virLibNetworkError:
 * @conn: the connection if available
 * @error: the error noumber
 * @info: extra information string
145
 *
146
 * Handle an error at the connection level
147
 */
148 149 150 151 152 153 154 155 156 157 158 159 160 161
static void
virLibNetworkError(virNetworkPtr network, virErrorNumber error,
                   const char *info)
{
    virConnectPtr conn = NULL;
    const char *errmsg;

    if (error == VIR_ERR_OK)
        return;

    errmsg = __virErrorMsg(error, info);
    if (error != VIR_ERR_INVALID_NETWORK) {
        conn = network->conn;
    }
162
    __virRaiseError(conn, NULL, network, VIR_FROM_NET, error, VIR_ERR_ERROR,
163 164 165 166 167
                    errmsg, info, NULL, 0, 0, errmsg, info);
}

static int
_virRegisterDriver(void *driver, int isNetwork)
168
{
169
    void **drivers;
170 171
    int i;

172
    if (!initialized)
173 174
        if (virInitialize() < 0)
	    return -1;
175

176 177 178 179
    if (driver == NULL) {
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return(-1);
    }
180
    drivers = isNetwork ? (void **) virNetworkDriverTab : (void **) virDriverTab;
181
    for (i = 0;i < MAX_DRIVERS;i++) {
182
        if (drivers[i] == driver)
183 184 185
	    return(i);
    }
    for (i = 0;i < MAX_DRIVERS;i++) {
186 187
        if (drivers[i] == NULL) {
	    drivers[i] = driver;
188 189 190 191 192 193 194
	    return(i);
	}
    }
    virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
    return(-1);
}

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
/**
 * virRegisterNetworkDriver:
 * @driver: pointer to a network driver block
 *
 * Register a network virtualization driver
 *
 * Returns the driver priority or -1 in case of error.
 */
int
virRegisterNetworkDriver(virNetworkDriverPtr driver)
{
    return _virRegisterDriver(driver, 1);
}

/**
 * virRegisterDriver:
 * @driver: pointer to a driver block
 *
 * Register a virtualization driver
 *
 * Returns the driver priority or -1 in case of error.
 */
int
virRegisterDriver(virDriverPtr driver)
{
    return _virRegisterDriver(driver, 0);
}

223 224 225
/**
 * virGetVersion:
 * @libVer: return value for the library version (OUT)
226
 * @type: the type of connection/driver looked at
227 228 229 230 231 232 233 234 235 236 237 238
 * @typeVer: return value for the version of the hypervisor (OUT)
 *
 * Provides two information back, @libVer is the version of the library
 * while @typeVer will be the version of the hypervisor type @type against
 * which the library was compiled. If @type is NULL, "Xen" is assumed, if
 * @type is unknown or not availble, an error code will be returned and 
 * @typeVer will be 0.
 *
 * Returns -1 in case of failure, 0 otherwise, and values for @libVer and
 *       @typeVer have the format major * 1,000,000 + minor * 1,000 + release.
 */
int
239 240 241
virGetVersion(unsigned long *libVer, const char *type,
              unsigned long *typeVer)
{
242 243
    int i;

244
    if (!initialized)
245 246
        if (virInitialize() < 0)
	    return -1;
247

248
    if (libVer == NULL)
249
        return (-1);
250 251 252
    *libVer = LIBVIR_VERSION_NUMBER;

    if (typeVer != NULL) {
253 254 255 256 257 258 259 260 261 262
        if (type == NULL)
	    type = "Xen";
	for (i = 0;i < MAX_DRIVERS;i++) {
	    if ((virDriverTab[i] != NULL) &&
	        (!strcmp(virDriverTab[i]->name, type))) {
		*typeVer = virDriverTab[i]->ver;
		break;
	    }
	}
        if (i >= MAX_DRIVERS) {
263
            *typeVer = 0;
264
            virLibConnError(NULL, VIR_ERR_NO_SUPPORT, type);
265 266 267 268
            return (-1);
        }
    }
    return (0);
269 270
}

D
Daniel Veillard 已提交
271
/**
272
 * virConnectOpen:
D
Daniel Veillard 已提交
273 274 275
 * @name: optional argument currently unused, pass NULL
 *
 * This function should be called first to get a connection to the 
276
 * Hypervisor and xen store
D
Daniel Veillard 已提交
277 278 279
 *
 * Returns a pointer to the hypervisor connection or NULL in case of error
 */
280
virConnectPtr
281 282
virConnectOpen(const char *name)
{
283
    int i, res, for_xen = 0;
284
    virConnectPtr ret = NULL;
285

286
    if (!initialized)
287 288
        if (virInitialize() < 0)
	    return NULL;
289

290 291 292
    if (name == NULL) {
        name = "Xen";
	for_xen = 1;
293
    } else if (!strncasecmp(name, "xen", 3)) {
294 295 296
	for_xen = 1;
    }

297
    ret = virGetConnect();
298
    if (ret == NULL) {
299
        virLibConnError(NULL, VIR_ERR_NO_MEMORY, _("allocating connection"));
300 301 302
        goto failed;
    }

303 304
    for (i = 0;i < MAX_DRIVERS;i++) {
        if ((virDriverTab[i] != NULL) && (virDriverTab[i]->open != NULL)) {
305
	    res = virDriverTab[i]->open(ret, name, VIR_DRV_OPEN_QUIET);
306 307 308 309
	    /*
	     * For a default connect to Xen make sure we manage to contact
	     * all related drivers.
	     */
310
	    if ((res < 0) && (for_xen) &&
311 312
	        (!strncasecmp(virDriverTab[i]->name, "xen", 3)) &&
		(virDriverTab[i]->no != VIR_DRV_XEN_PROXY))
313 314 315 316 317 318
		goto failed;
	    if (res == 0)
	        ret->drivers[ret->nb_drivers++] = virDriverTab[i];
	}
    }

319 320 321 322 323 324 325 326
    for (i = 0;i < MAX_DRIVERS;i++) {
        if ((virNetworkDriverTab[i] != NULL) && (virNetworkDriverTab[i]->open != NULL) &&
	    (res = virNetworkDriverTab[i]->open(ret, name, VIR_DRV_OPEN_QUIET)) == 0) {
            ret->networkDrivers[ret->nb_network_drivers++] = virNetworkDriverTab[i];
        }
    }

    if (ret->nb_drivers == 0 || ret->nb_network_drivers == 0) {
327 328 329 330
	/* we failed to find an adequate driver */
	virLibConnError(NULL, VIR_ERR_NO_SUPPORT, name);
	goto failed;
    }
331

332
    return (ret);
333 334 335

failed:
    if (ret != NULL) {
336 337 338 339
	for (i = 0;i < ret->nb_drivers;i++) {
	    if ((ret->drivers[i] != NULL) && (ret->drivers[i]->close != NULL))
	        ret->drivers[i]->close(ret);
	}
340 341 342 343
	for (i = 0;i < ret->nb_network_drivers;i++) {
	    if ((ret->networkDrivers[i] != NULL) && (ret->networkDrivers[i]->close != NULL))
	        ret->networkDrivers[i]->close(ret);
	}
344
	virFreeConnect(ret);
345
    }
346
    return (NULL);
347 348 349
}

/**
350
 * virConnectOpenReadOnly:
351 352
 * @name: optional argument currently unused, pass NULL
 *
353 354 355
 * This function should be called first to get a restricted connection to the 
 * libbrary functionalities. The set of APIs usable are then restricted
 * on the available methods to control the domains.
356 357 358
 *
 * Returns a pointer to the hypervisor connection or NULL in case of error
 */
359
virConnectPtr
360 361
virConnectOpenReadOnly(const char *name)
{
362
    int i, res;
363
    virConnectPtr ret = NULL;
364

365
    if (!initialized)
366 367
        if (virInitialize() < 0)
	    return NULL;
368

369 370 371
    if (name == NULL)
        name = "Xen";

372
    ret = virGetConnect();
D
Daniel Veillard 已提交
373
    if (ret == NULL) {
374
        virLibConnError(NULL, VIR_ERR_NO_MEMORY, _("allocating connection"));
375
        goto failed;
D
Daniel Veillard 已提交
376
    }
377

378 379 380 381 382 383
    for (i = 0;i < MAX_DRIVERS;i++) {
        if ((virDriverTab[i] != NULL) && (virDriverTab[i]->open != NULL)) {
	    res = virDriverTab[i]->open(ret, name,
	                                VIR_DRV_OPEN_QUIET | VIR_DRV_OPEN_RO);
	    if (res == 0)
	        ret->drivers[ret->nb_drivers++] = virDriverTab[i];
384

385
	}
386 387 388 389
        if ((virNetworkDriverTab[i] != NULL) && (virNetworkDriverTab[i]->open != NULL) &&
	    (res = virNetworkDriverTab[i]->open(ret, name, VIR_DRV_OPEN_QUIET)) == 0) {
            ret->networkDrivers[ret->nb_network_drivers++] = virNetworkDriverTab[i];
        }
390 391 392 393
    }
    if (ret->nb_drivers == 0) {
	if (name == NULL)
	    virLibConnError(NULL, VIR_ERR_NO_CONNECT,
394
			    _("Xen Daemon or Xen Store"));
395 396 397 398
	else
	    /* we failed to find an adequate driver */
	    virLibConnError(NULL, VIR_ERR_NO_SUPPORT, name);
	goto failed;
D
Daniel Veillard 已提交
399
    }
400 401
    ret->flags = VIR_CONNECT_RO;

402
    return (ret);
403 404

failed:
405
    if (ret != NULL) {
406 407 408 409
	for (i = 0;i < ret->nb_drivers;i++) {
	    if ((ret->drivers[i] != NULL) && (ret->drivers[i]->close != NULL))
	        ret->drivers[i]->close(ret);
	}
410 411 412 413
	for (i = 0;i < ret->nb_network_drivers;i++) {
	    if ((ret->networkDrivers[i] != NULL) && (ret->networkDrivers[i]->close != NULL))
	        ret->networkDrivers[i]->close(ret);
	}
414
	virFreeConnect(ret);
415
    }
416
    return (NULL);
D
Daniel Veillard 已提交
417 418 419
}

/**
420
 * virConnectClose:
D
Daniel Veillard 已提交
421 422 423 424 425 426 427 428 429 430
 * @conn: pointer to the hypervisor connection
 *
 * This function closes the connection to the Hypervisor. This should
 * not be called if further interaction with the Hypervisor are needed
 * especially if there is running domain which need further monitoring by
 * the application.
 *
 * Returns 0 in case of success or -1 in case of error.
 */
int
431 432
virConnectClose(virConnectPtr conn)
{
433 434
    int i;

K
Karel Zak 已提交
435
    if (!VIR_IS_CONNECT(conn))
436
        return (-1);
437 438 439 440
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) && (conn->drivers[i]->close != NULL))
	    conn->drivers[i]->close(conn);
    }
441 442 443 444
    for (i = 0;i < conn->nb_network_drivers;i++) {
	if ((conn->networkDrivers[i] != NULL) && (conn->networkDrivers[i]->close != NULL))
	    conn->networkDrivers[i]->close(conn);
    }
445 446
    if (virFreeConnect(conn) < 0)
        return (-1);
447
    return (0);
D
Daniel Veillard 已提交
448 449
}

450 451 452 453 454 455 456 457 458
/**
 * virConnectGetType:
 * @conn: pointer to the hypervisor connection
 *
 * Get the name of the Hypervisor software used.
 *
 * Returns NULL in case of error, a static zero terminated string otherwise.
 */
const char *
459 460
virConnectGetType(virConnectPtr conn)
{
461
    int i;
462
    const char *ret;
463

D
Daniel Veillard 已提交
464
    if (!VIR_IS_CONNECT(conn)) {
465
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
466
        return (NULL);
D
Daniel Veillard 已提交
467
    }
468 469 470 471 472 473 474 475
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->type != NULL)) {
	    ret = conn->drivers[i]->type(conn);
	    if (ret != NULL)
	        return(ret);
	}
    }
476 477 478 479 480 481 482
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->name != NULL)) {
	    return(conn->drivers[i]->name);
	}
    }
    return(NULL);
483 484
}

D
Daniel Veillard 已提交
485
/**
486
 * virConnectGetVersion:
D
Daniel Veillard 已提交
487
 * @conn: pointer to the hypervisor connection
488
 * @hvVer: return value for the version of the running hypervisor (OUT)
D
Daniel Veillard 已提交
489
 *
490 491 492
 * Get the version level of the Hypervisor running. This may work only with 
 * hypervisor call, i.e. with priviledged access to the hypervisor, not
 * with a Read-Only connection.
D
Daniel Veillard 已提交
493
 *
494 495 496
 * Returns -1 in case of error, 0 otherwise. if the version can't be
 *    extracted by lack of capacities returns 0 and @hvVer is 0, otherwise
 *    @hvVer value is major * 1,000,000 + minor * 1,000 + release
D
Daniel Veillard 已提交
497
 */
498
int
499 500
virConnectGetVersion(virConnectPtr conn, unsigned long *hvVer)
{
501
    int ret, i;
502

D
Daniel Veillard 已提交
503 504
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
505
        return (-1);
D
Daniel Veillard 已提交
506
    }
507

D
Daniel Veillard 已提交
508 509
    if (hvVer == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
510
        return (-1);
D
Daniel Veillard 已提交
511
    }
512

513 514 515 516 517 518 519 520
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->version != NULL)) {
	    ret = conn->drivers[i]->version(conn, hvVer);
	    if (ret == 0)
	        return(0);
	}
    }
D
Daniel P. Berrange 已提交
521

522
    return (-1);
523 524
}

525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
/**
 * virConnectGetMaxVcpus:
 * @conn: pointer to the hypervisor connection
 * @type: value of the 'type' attribute in the <domain> element
 *
 * Returns the maximum number of virtual CPUs supported for a guest VM of a
 * specific type. The 'type' parameter here corresponds to the 'type'
 * attribute in the <domain> element of the XML.
 *
 * Returns the maximum of virtual CPU or -1 in case of error.
 */
int
virConnectGetMaxVcpus(virConnectPtr conn,
                      const char *type)
{
    int ret = -1;
    int i;

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

    if (type == NULL)
        type = "Xen";
    for (i = 0;i < MAX_DRIVERS;i++) {
        if ((virDriverTab[i] != NULL) &&
           (!strcmp(virDriverTab[i]->name, type))) {
            ret = conn->drivers[i]->getMaxVcpus(conn);
            if (ret >= 0)
                return(ret);
        }
    }

    return (-1);

}

563
/**
564
 * virConnectListDomains:
565 566 567 568 569 570 571 572 573
 * @conn: pointer to the hypervisor connection
 * @ids: array to collect the list of IDs of active domains
 * @maxids: size of @ids
 *
 * Collect the list of active domains, and store their ID in @maxids
 *
 * Returns the number of domain found or -1 in case of error
 */
int
574 575
virConnectListDomains(virConnectPtr conn, int *ids, int maxids)
{
576
    int ret = -1;
577
    int i;
578

D
Daniel Veillard 已提交
579 580
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
581
        return (-1);
D
Daniel Veillard 已提交
582
    }
583

D
Daniel Veillard 已提交
584 585
    if ((ids == NULL) || (maxids <= 0)) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
586
        return (-1);
D
Daniel Veillard 已提交
587
    }
588

589 590 591 592 593 594 595 596 597 598
    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->listDomains != NULL)) {
	    ret = conn->drivers[i]->listDomains(conn, ids, maxids);
	    if (ret >= 0)
	        return(ret);
	}
    }

599
    return (-1);
D
Daniel Veillard 已提交
600 601
}

K
 
Karel Zak 已提交
602 603 604 605
/**
 * virConnectNumOfDomains:
 * @conn: pointer to the hypervisor connection
 *
606 607
 * Provides the number of active domains.
 *
K
 
Karel Zak 已提交
608 609 610
 * Returns the number of domain found or -1 in case of error
 */
int
611 612
virConnectNumOfDomains(virConnectPtr conn)
{
K
 
Karel Zak 已提交
613
    int ret = -1;
614
    int i;
K
 
Karel Zak 已提交
615

D
Daniel Veillard 已提交
616 617
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
618
        return (-1);
D
Daniel Veillard 已提交
619
    }
K
Karel Zak 已提交
620

621 622 623 624 625 626 627 628 629 630
    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->numOfDomains != NULL)) {
	    ret = conn->drivers[i]->numOfDomains(conn);
	    if (ret >= 0)
	        return(ret);
	}
    }

631
    return(-1);
K
 
Karel Zak 已提交
632 633
}

D
Daniel Veillard 已提交
634
/**
635
 * virDomainCreateLinux:
D
Daniel Veillard 已提交
636
 * @conn: pointer to the hypervisor connection
637
 * @xmlDesc: an XML description of the domain
638
 * @flags: an optional set of virDomainFlags
D
Daniel Veillard 已提交
639
 *
640 641 642
 * Launch a new Linux guest domain, based on an XML description similar
 * to the one returned by virDomainGetXMLDesc()
 * This function may requires priviledged access to the hypervisor.
D
Daniel Veillard 已提交
643 644 645
 * 
 * Returns a new domain object or NULL in case of failure
 */
646
virDomainPtr
647 648
virDomainCreateLinux(virConnectPtr conn, const char *xmlDesc,
                     unsigned int flags)
649
{
650 651
    virDomainPtr ret;
    int i;
K
Karel Zak 已提交
652

D
Daniel Veillard 已提交
653 654
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
655
        return (NULL);
D
Daniel Veillard 已提交
656 657 658
    }
    if (xmlDesc == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
659
        return (NULL);
D
Daniel Veillard 已提交
660
    }
661 662 663 664
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (NULL);
    }
D
Daniel Veillard 已提交
665

666 667 668 669 670 671 672
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainCreateLinux != NULL)) {
	    ret = conn->drivers[i]->domainCreateLinux(conn, xmlDesc, flags);
	    if (ret != NULL)
	        return(ret);
	}
673
    }
674
    return(NULL);
D
Daniel Veillard 已提交
675 676
}

677

678
/**
679
 * virDomainLookupByID:
680 681 682 683 684 685 686
 * @conn: pointer to the hypervisor connection
 * @id: the domain ID number
 *
 * Try to find a domain based on the hypervisor ID number
 *
 * Returns a new domain object or NULL in case of failure
 */
687
virDomainPtr
688 689
virDomainLookupByID(virConnectPtr conn, int id)
{
690
    virDomainPtr ret;
691
    int i;
692

D
Daniel Veillard 已提交
693 694
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
695
        return (NULL);
D
Daniel Veillard 已提交
696 697 698
    }
    if (id < 0) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
699
        return (NULL);
D
Daniel Veillard 已提交
700
    }
701

702 703 704 705 706 707 708 709 710 711
    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainLookupByID != NULL)) {
	    ret = conn->drivers[i]->domainLookupByID(conn, id);
	    if (ret)
	        return(ret);
	}
    }

712
    return (NULL);
713 714 715 716 717
}

/**
 * virDomainLookupByUUID:
 * @conn: pointer to the hypervisor connection
K
Karel Zak 已提交
718
 * @uuid: the raw UUID for the domain
719 720 721 722 723 724
 *
 * Try to lookup a domain on the given hypervisor based on its UUID.
 *
 * Returns a new domain object or NULL in case of failure
 */
virDomainPtr
725 726
virDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
{
727
    virDomainPtr ret;
728
    int i;
729

D
Daniel Veillard 已提交
730 731
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
732
        return (NULL);
D
Daniel Veillard 已提交
733 734 735
    }
    if (uuid == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
736
        return (NULL);
D
Daniel Veillard 已提交
737
    }
738 739 740 741 742 743 744 745 746 747 748

    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainLookupByUUID != NULL)) {
	    ret = conn->drivers[i]->domainLookupByUUID(conn, uuid);
	    if (ret)
	        return(ret);
	}
    }

749
    return (NULL);
750 751
}

K
Karel Zak 已提交
752 753 754 755 756 757 758 759 760 761 762 763
/**
 * virDomainLookupByUUIDString:
 * @conn: pointer to the hypervisor connection
 * @uuidstr: the string UUID for the domain
 *
 * Try to lookup a domain on the given hypervisor based on its UUID.
 *
 * Returns a new domain object or NULL in case of failure
 */
virDomainPtr
virDomainLookupByUUIDString(virConnectPtr conn, const char *uuidstr)
{
764 765
    int raw[VIR_UUID_BUFLEN], i;
    unsigned char uuid[VIR_UUID_BUFLEN];
K
Karel Zak 已提交
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
    int ret;

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
    if (uuidstr == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
	
    }
    /* XXX: sexpr_uuid() also supports 'xxxx-xxxx-xxxx-xxxx' format. 
     *      We needn't it here. Right? 
     */
    ret = sscanf(uuidstr,
                 "%02x%02x%02x%02x-"
                 "%02x%02x-"
                 "%02x%02x-"
                 "%02x%02x-"
                 "%02x%02x%02x%02x%02x%02x",
                 raw + 0, raw + 1, raw + 2, raw + 3,
                 raw + 4, raw + 5, raw + 6, raw + 7,
                 raw + 8, raw + 9, raw + 10, raw + 11,
                 raw + 12, raw + 13, raw + 14, raw + 15);
    
791
    if (ret!=VIR_UUID_BUFLEN) {
K
Karel Zak 已提交
792 793 794
	virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return (NULL);
    }
795
    for (i = 0; i < VIR_UUID_BUFLEN; i++)
K
Karel Zak 已提交
796 797 798 799 800
        uuid[i] = raw[i] & 0xFF;
    
    return virDomainLookupByUUID(conn, &uuid[0]);
}

801 802 803 804 805
/**
 * virDomainLookupByName:
 * @conn: pointer to the hypervisor connection
 * @name: name for the domain
 *
806
 * Try to lookup a domain on the given hypervisor based on its name.
807 808 809 810
 *
 * Returns a new domain object or NULL in case of failure
 */
virDomainPtr
811 812
virDomainLookupByName(virConnectPtr conn, const char *name)
{
813
    virDomainPtr ret = NULL;
814
    int i;
815

D
Daniel Veillard 已提交
816 817
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
818
        return (NULL);
D
Daniel Veillard 已提交
819 820 821
    }
    if (name == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
822
        return (NULL);
D
Daniel Veillard 已提交
823
    }
824

825 826 827 828 829 830 831 832 833
    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainLookupByName != NULL)) {
	    ret = conn->drivers[i]->domainLookupByName(conn, name);
	    if (ret)
	        return(ret);
	}
    }
834
    return (NULL);
835 836
}

D
Daniel Veillard 已提交
837
/**
838
 * virDomainDestroy:
D
Daniel Veillard 已提交
839 840 841 842
 * @domain: a domain object
 *
 * Destroy the domain object. The running instance is shutdown if not down
 * already and all resources used by it are given back to the hypervisor.
843 844
 * The data structure is freed and should not be used thereafter if the
 * call does not return an error.
845
 * This function may requires priviledged access
D
Daniel Veillard 已提交
846 847
 *
 * Returns 0 in case of success and -1 in case of failure.
848
 */
D
Daniel Veillard 已提交
849
int
850 851
virDomainDestroy(virDomainPtr domain)
{
852
    int i;
853
    virConnectPtr conn;
854

D
Daniel Veillard 已提交
855 856
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
857
        return (-1);
D
Daniel Veillard 已提交
858
    }
859

860
    conn = domain->conn;
861 862 863 864
    if (conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
865

866 867 868 869
    /*
     * Go though the driver registered entry points but use the 
     * XEN_HYPERVISOR directly only as a last mechanism
     */
870 871
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
872 873 874
	    (conn->drivers[i]->no != VIR_DRV_XEN_HYPERVISOR) &&
	    (conn->drivers[i]->domainDestroy != NULL)) {
	    if (conn->drivers[i]->domainDestroy(domain) == 0)
875
	        return (0);
876 877 878 879 880
	}
    }
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->no == VIR_DRV_XEN_HYPERVISOR) &&
881 882
	    (conn->drivers[i]->domainDestroy != NULL)) {
	    if (conn->drivers[i]->domainDestroy(domain) == 0)
883
	        return (0);
884
	}
885 886
    }

887
        virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
888
    return (-1);
889 890 891 892 893 894 895 896 897 898 899 900
}

/**
 * virDomainFree:
 * @domain: a domain object
 *
 * Free the domain object. The running instance is kept alive.
 * The data structure is freed and should not be used thereafter.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
901 902
virDomainFree(virDomainPtr domain)
{
D
Daniel Veillard 已提交
903 904
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
905
        return (-1);
D
Daniel Veillard 已提交
906
    }
907 908 909
    if (virFreeDomain(domain->conn, domain) < 0)
        return (-1);
    return(0);
D
Daniel Veillard 已提交
910 911 912
}

/**
913
 * virDomainSuspend:
D
Daniel Veillard 已提交
914 915 916 917
 * @domain: a domain object
 *
 * Suspends an active domain, the process is frozen without further access
 * to CPU resources and I/O but the memory used by the domain at the 
918
 * hypervisor level will stay allocated. Use virDomainResume() to reactivate
D
Daniel Veillard 已提交
919
 * the domain.
920
 * This function may requires priviledged access.
D
Daniel Veillard 已提交
921 922 923 924
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
925 926
virDomainSuspend(virDomainPtr domain)
{
927
    int i;
928
    virConnectPtr conn;
929

D
Daniel Veillard 已提交
930 931
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
932
        return (-1);
D
Daniel Veillard 已提交
933
    }
934 935 936 937
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
D
Daniel Veillard 已提交
938

939 940
    conn = domain->conn;

941 942 943 944 945 946 947 948 949
    /*
     * Go though the driver registered entry points but use the 
     * XEN_HYPERVISOR directly only as a last mechanism
     */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->no != VIR_DRV_XEN_HYPERVISOR) &&
	    (conn->drivers[i]->domainSuspend != NULL)) {
	    if (conn->drivers[i]->domainSuspend(domain) == 0)
950
	        return (0);
951 952
	}
    }
953 954
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
955
	    (conn->drivers[i]->no == VIR_DRV_XEN_HYPERVISOR) &&
956 957
	    (conn->drivers[i]->domainSuspend != NULL)) {
	    if (conn->drivers[i]->domainSuspend(domain) == 0)
958
	        return (0);
959 960 961 962
	}
    }

        virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
963
    return (-1);
D
Daniel Veillard 已提交
964 965 966
}

/**
967
 * virDomainResume:
D
Daniel Veillard 已提交
968 969 970
 * @domain: a domain object
 *
 * Resume an suspended domain, the process is restarted from the state where
971
 * it was frozen by calling virSuspendDomain().
972
 * This function may requires priviledged access
D
Daniel Veillard 已提交
973 974 975 976
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
977 978
virDomainResume(virDomainPtr domain)
{
979
    int i;
980
    virConnectPtr conn;
981

D
Daniel Veillard 已提交
982 983
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
984
        return (-1);
D
Daniel Veillard 已提交
985
    }
986 987 988 989
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
D
Daniel Veillard 已提交
990

991 992
    conn = domain->conn;

993 994 995 996 997 998 999 1000 1001 1002 1003 1004
    /*
     * Go though the driver registered entry points but use the 
     * XEN_HYPERVISOR directly only as a last mechanism
     */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->no != VIR_DRV_XEN_HYPERVISOR) &&
	    (conn->drivers[i]->domainResume != NULL)) {
	    if (conn->drivers[i]->domainResume(domain) == 0)
	        return(0);
	}
    }
1005 1006
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
1007
	    (conn->drivers[i]->no == VIR_DRV_XEN_HYPERVISOR) &&
1008 1009
	    (conn->drivers[i]->domainResume != NULL)) {
	    if (conn->drivers[i]->domainResume(domain) == 0)
1010
	        return(0);
1011 1012 1013
	}
    }

1014 1015
    virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
    return (-1);
D
Daniel Veillard 已提交
1016 1017
}

1018 1019 1020 1021 1022 1023
/**
 * virDomainSave:
 * @domain: a domain object
 * @to: path for the output file
 *
 * This method will suspend a domain and save its memory contents to
1024 1025 1026
 * a file on disk. After the call, if successful, the domain is not
 * listed as running anymore (this may be a problem).
 * Use virDomainRestore() to restore a domain after saving.
1027 1028 1029 1030
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1031 1032
virDomainSave(virDomainPtr domain, const char *to)
{
1033
    int ret, i;
1034
    char filepath[4096];
1035
    virConnectPtr conn;
1036

D
Daniel Veillard 已提交
1037 1038
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1039
        return (-1);
D
Daniel Veillard 已提交
1040
    }
1041 1042 1043 1044
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
1045
    conn = domain->conn;
D
Daniel Veillard 已提交
1046 1047
    if (to == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1048
        return (-1);
D
Daniel Veillard 已提交
1049
    }
1050

1051 1052 1053 1054 1055
    /*
     * We must absolutize the file path as the save is done out of process
     * TODO: check for URI when libxml2 is linked in.
     */
    if (to[0] != '/') {
1056
        unsigned int len, t;
1057

1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
        t = strlen(to);
        if (getcwd(filepath, sizeof(filepath) - (t + 3)) == NULL)
            return (-1);
        len = strlen(filepath);
        /* that should be covered by getcwd() semantic, but be 100% sure */
        if (len > sizeof(filepath) - (t + 3))
            return (-1);
        filepath[len] = '/';
        strcpy(&filepath[len + 1], to);
        to = &filepath[0];
1068 1069 1070

    }

1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainSave != NULL)) {
	    ret = conn->drivers[i]->domainSave(domain, to);
	    if (ret == 0)
	        return(0);
	}
    }
    virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
    return (-1);
1082 1083 1084 1085
}

/**
 * virDomainRestore:
1086
 * @conn: pointer to the hypervisor connection
1087 1088 1089 1090 1091 1092 1093
 * @from: path to the 
 *
 * This method will restore a domain saved to disk by virDomainSave().
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1094 1095
virDomainRestore(virConnectPtr conn, const char *from)
{
1096
    int ret, i;
1097
    char filepath[4096];
1098

D
Daniel Veillard 已提交
1099 1100
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
1101
        return (-1);
D
Daniel Veillard 已提交
1102
    }
1103 1104 1105 1106
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
D
Daniel Veillard 已提交
1107 1108
    if (from == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1109
        return (-1);
D
Daniel Veillard 已提交
1110 1111
    }

1112 1113 1114 1115 1116
    /*
     * We must absolutize the file path as the restore is done out of process
     * TODO: check for URI when libxml2 is linked in.
     */
    if (from[0] != '/') {
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
        unsigned int len, t;

        t = strlen(from);
        if (getcwd(filepath, sizeof(filepath) - (t + 3)) == NULL)
            return (-1);
        len = strlen(filepath);
        /* that should be covered by getcwd() semantic, but be 100% sure */
        if (len > sizeof(filepath) - (t + 3))
            return (-1);
        filepath[len] = '/';
        strcpy(&filepath[len + 1], from);
        from = &filepath[0];
    }

1131 1132 1133 1134 1135 1136 1137 1138 1139
    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainSave != NULL)) {
	    ret = conn->drivers[i]->domainRestore(conn, from);
	    if (ret == 0)
	        return(0);
	}
    }
D
Daniel Veillard 已提交
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
    virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
    return (-1);
}

/**
 * virDomainCoreDump:
 * @domain: a domain object
 * @to: path for the core file
 * @flags: extra flags, currently unused
 *
 * This method will dump the core of a domain on a given file for analysis.
 * Note that for remote Xen Daemon the file path will be interpreted in
 * the remote host.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
virDomainCoreDump(virDomainPtr domain, const char *to, int flags)
{
    int ret, i;
    char filepath[4096];
    virConnectPtr conn;

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
    conn = domain->conn;
    if (to == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }

    /*
     * We must absolutize the file path as the save is done out of process
     * TODO: check for URI when libxml2 is linked in.
     */
    if (to[0] != '/') {
        unsigned int len, t;

        t = strlen(to);
        if (getcwd(filepath, sizeof(filepath) - (t + 3)) == NULL)
            return (-1);
        len = strlen(filepath);
        /* that should be covered by getcwd() semantic, but be 100% sure */
        if (len > sizeof(filepath) - (t + 3))
            return (-1);
        filepath[len] = '/';
        strcpy(&filepath[len + 1], to);
        to = &filepath[0];

    }

    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainCoreDump != NULL)) {
	    ret = conn->drivers[i]->domainCoreDump(domain, to, flags);
	    if (ret == 0)
	        return(0);
	}
    }
1206 1207
    virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
    return (-1);
1208 1209
}

1210 1211 1212 1213 1214
/**
 * virDomainShutdown:
 * @domain: a domain object
 *
 * Shutdown a domain, the domain object is still usable there after but
1215 1216
 * the domain OS is being stopped. Note that the guest OS may ignore the
 * request.
1217 1218 1219 1220 1221 1222 1223
 *
 * TODO: should we add an option for reboot, knowing it may not be doable
 *       in the general case ?
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1224 1225
virDomainShutdown(virDomainPtr domain)
{
1226 1227
    int ret = -1, i;
    virConnectPtr conn;
1228

D
Daniel Veillard 已提交
1229 1230
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1231
        return (-1);
D
Daniel Veillard 已提交
1232
    }
1233 1234 1235 1236
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
1237

1238 1239 1240 1241 1242 1243 1244 1245 1246
    conn = domain->conn;

    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainShutdown != NULL)) {
	    if (conn->drivers[i]->domainShutdown(domain) == 0)
	        ret = 0;
	}
1247
    }
1248

1249 1250 1251
    if (ret != 0) {
        virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
        return (ret);
1252
    }
1253

1254
    return (ret);
1255 1256
}

1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
/**
 * virDomainReboot:
 * @domain: a domain object
 * @flags: extra flags for the reboot operation, not used yet
 *
 * Reboot a domain, the domain object is still usable there after but
 * the domain OS is being stopped for a restart.
 * Note that the guest OS may ignore the request.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
virDomainReboot(virDomainPtr domain, unsigned int flags)
{
1271 1272
    int ret = -1, i;
    virConnectPtr conn;
1273 1274 1275 1276 1277

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
1278 1279 1280 1281
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
1282

1283 1284 1285 1286 1287 1288 1289 1290 1291
    conn = domain->conn;

    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainReboot != NULL)) {
	    if (conn->drivers[i]->domainReboot(domain, flags) == 0)
	        ret = 0;
	}
1292 1293
    }

1294 1295 1296
    if (ret != 0) {
        virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
        return (ret);
1297
    }
1298

1299 1300 1301
    return (ret);
}

1302
/**
1303
 * virDomainGetName:
1304 1305 1306 1307 1308 1309 1310 1311
 * @domain: a domain object
 *
 * Get the public name for that domain
 *
 * Returns a pointer to the name or NULL, the string need not be deallocated
 * its lifetime will be the same as the domain object.
 */
const char *
1312 1313
virDomainGetName(virDomainPtr domain)
{
D
Daniel Veillard 已提交
1314 1315
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1316
        return (NULL);
D
Daniel Veillard 已提交
1317
    }
1318
    return (domain->name);
1319 1320
}

1321 1322 1323
/**
 * virDomainGetUUID:
 * @domain: a domain object
1324
 * @uuid: pointer to a VIR_UUID_BUFLEN bytes array
1325 1326 1327 1328 1329 1330
 *
 * Get the UUID for a domain
 *
 * Returns -1 in case of error, 0 in case of success
 */
int
1331 1332
virDomainGetUUID(virDomainPtr domain, unsigned char *uuid)
{
D
Daniel Veillard 已提交
1333 1334
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1335
        return (-1);
D
Daniel Veillard 已提交
1336 1337 1338
    }
    if (uuid == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1339
        return (-1);
D
Daniel Veillard 已提交
1340 1341
    }

1342
    if (domain->id == 0) {
1343
        memset(uuid, 0, VIR_UUID_BUFLEN);
D
Daniel Veillard 已提交
1344
    } else {
1345 1346 1347 1348 1349 1350 1351 1352
        if ((domain->uuid[0] == 0) && (domain->uuid[1] == 0) &&
            (domain->uuid[2] == 0) && (domain->uuid[3] == 0) &&
            (domain->uuid[4] == 0) && (domain->uuid[5] == 0) &&
            (domain->uuid[6] == 0) && (domain->uuid[7] == 0) &&
            (domain->uuid[8] == 0) && (domain->uuid[9] == 0) &&
            (domain->uuid[10] == 0) && (domain->uuid[11] == 0) &&
            (domain->uuid[12] == 0) && (domain->uuid[13] == 0) &&
            (domain->uuid[14] == 0) && (domain->uuid[15] == 0))
1353
            xenDaemonDomainLookupByName_ids(domain->conn, domain->name,
1354
                                &domain->uuid[0]);
1355
        memcpy(uuid, &domain->uuid[0], VIR_UUID_BUFLEN);
1356 1357
    }
    return (0);
1358 1359
}

K
Karel Zak 已提交
1360 1361 1362
/**
 * virDomainGetUUIDString:
 * @domain: a domain object
1363
 * @buf: pointer to a VIR_UUID_STRING_BUFLEN bytes array
K
Karel Zak 已提交
1364 1365 1366 1367 1368 1369 1370 1371 1372
 *
 * Get the UUID for a domain as string. For more information about 
 * UUID see RFC4122.
 *
 * Returns -1 in case of error, 0 in case of success
 */
int
virDomainGetUUIDString(virDomainPtr domain, char *buf)
{
1373
    unsigned char uuid[VIR_UUID_BUFLEN];
1374

K
Karel Zak 已提交
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
    if (buf == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
    
    if (virDomainGetUUID(domain, &uuid[0]))
	return (-1);

1387
    snprintf(buf, VIR_UUID_STRING_BUFLEN,
K
Karel Zak 已提交
1388 1389 1390 1391 1392 1393 1394 1395
	"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
                      uuid[0], uuid[1], uuid[2], uuid[3],
                      uuid[4], uuid[5], uuid[6], uuid[7],
                      uuid[8], uuid[9], uuid[10], uuid[11],
                      uuid[12], uuid[13], uuid[14], uuid[15]);
    return (0);
}

1396
/**
1397
 * virDomainGetID:
1398 1399 1400 1401 1402 1403 1404
 * @domain: a domain object
 *
 * Get the hypervisor ID number for the domain
 *
 * Returns the domain ID number or (unsigned int) -1 in case of error
 */
unsigned int
1405 1406
virDomainGetID(virDomainPtr domain)
{
D
Daniel Veillard 已提交
1407 1408
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1409
        return ((unsigned int) -1);
D
Daniel Veillard 已提交
1410
    }
1411
    return (domain->id);
1412 1413
}

1414 1415 1416 1417 1418 1419
/**
 * virDomainGetOSType:
 * @domain: a domain object
 *
 * Get the type of domain operation system.
 *
1420 1421
 * Returns the new string or NULL in case of error, the string must be
 *         freed by the caller.
1422 1423
 */
char *
1424 1425
virDomainGetOSType(virDomainPtr domain)
{
1426 1427
    char *str = NULL;
    int i;
1428

D
Daniel Veillard 已提交
1429 1430
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1431
        return (NULL);
D
Daniel Veillard 已提交
1432
    }
1433

1434 1435 1436 1437 1438 1439 1440
    for (i = 0;i < domain->conn->nb_drivers;i++) {
	if ((domain->conn->drivers[i] != NULL) &&
	    (domain->conn->drivers[i]->domainGetOSType != NULL)) {
	    str = domain->conn->drivers[i]->domainGetOSType(domain);
	    if (str != NULL)
	        break;
	}
1441
    }
1442

1443
    return (str);
1444 1445
}

1446
/**
1447
 * virDomainGetMaxMemory:
1448 1449 1450 1451 1452 1453 1454 1455 1456
 * @domain: a domain object or NULL
 * 
 * Retrieve the maximum amount of physical memory allocated to a
 * domain. If domain is NULL, then this get the amount of memory reserved
 * to Domain0 i.e. the domain where the application runs.
 *
 * Returns the memory size in kilobytes or 0 in case of error.
 */
unsigned long
1457 1458
virDomainGetMaxMemory(virDomainPtr domain)
{
1459
    unsigned long ret = 0;
1460 1461
    virConnectPtr conn;
    int i;
1462

D
Daniel Veillard 已提交
1463 1464
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1465
        return (0);
D
Daniel Veillard 已提交
1466
    }
1467

1468 1469
    conn = domain->conn;

1470
    /*
1471 1472
     * in that case instead of trying only though one method try all availble.
     * If needed that can be changed back if it's a performcance problem.
1473
     */
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainGetMaxMemory != NULL)) {
	    ret = conn->drivers[i]->domainGetMaxMemory(domain);
	    if (ret != 0)
	        return(ret);
	}
    }
    virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
    return (-1);
1484 1485
}

D
Daniel Veillard 已提交
1486
/**
1487
 * virDomainSetMaxMemory:
D
Daniel Veillard 已提交
1488 1489 1490 1491 1492 1493
 * @domain: a domain object or NULL
 * @memory: the memory size in kilobytes
 * 
 * Dynamically change the maximum amount of physical memory allocated to a
 * domain. If domain is NULL, then this change the amount of memory reserved
 * to Domain0 i.e. the domain where the application runs.
1494
 * This function requires priviledged access to the hypervisor.
D
Daniel Veillard 已提交
1495 1496 1497 1498
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1499 1500
virDomainSetMaxMemory(virDomainPtr domain, unsigned long memory)
{
1501 1502
    int ret = -1 , i;
    virConnectPtr conn;
1503

1504 1505 1506 1507 1508 1509
    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1510
        return (-1);
1511
    }
1512 1513 1514 1515 1516 1517 1518 1519
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
    if (memory < 4096) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
1520
    conn = domain->conn;
1521

1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
    /*
     * in that case instead of trying only though one method try all availble.
     * If needed that can be changed back if it's a performcance problem.
     */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainSetMaxMemory != NULL)) {
	    if (conn->drivers[i]->domainSetMaxMemory(domain, memory) == 0)
	        ret = 0;
	}
    }
    if (ret != 0) {
        virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
1535
        return (-1);
1536 1537 1538
    }
    return (ret);
}
1539

1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
/**
 * virDomainSetMemory:
 * @domain: a domain object or NULL
 * @memory: the memory size in kilobytes
 * 
 * Dynamically change the target amount of physical memory allocated to a
 * domain. If domain is NULL, then this change the amount of memory reserved
 * to Domain0 i.e. the domain where the application runs.
 * This function may requires priviledged access to the hypervisor.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
virDomainSetMemory(virDomainPtr domain, unsigned long memory)
{
    int ret = -1 , i;
    virConnectPtr conn;
1557

1558 1559 1560 1561 1562 1563 1564
    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
1565
    }
1566 1567 1568 1569 1570 1571
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
    if (memory < 4096) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1572
        return (-1);
1573 1574 1575
    }

    conn = domain->conn;
1576

1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
    /*
     * in that case instead of trying only though one method try all availble.
     * If needed that can be changed back if it's a performcance problem.
     */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainSetMemory != NULL)) {
	    if (conn->drivers[i]->domainSetMemory(domain, memory) == 0)
	        ret = 0;
	}
    }
    if (ret != 0) {
        virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
        return (-1);
    }
1592
    return (ret);
D
Daniel Veillard 已提交
1593 1594
}

1595 1596
/**
 * virDomainGetInfo:
1597
 * @domain: a domain object
1598 1599 1600
 * @info: pointer to a virDomainInfo structure allocated by the user
 * 
 * Extract information about a domain. Note that if the connection
1601
 * used to get the domain is limited only a partial set of the information
1602 1603 1604 1605 1606
 * can be extracted.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1607 1608
virDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
{
1609
    int i;
1610

D
Daniel Veillard 已提交
1611 1612
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1613
        return (-1);
D
Daniel Veillard 已提交
1614 1615 1616
    }
    if (info == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1617
        return (-1);
D
Daniel Veillard 已提交
1618
    }
1619

1620
    memset(info, 0, sizeof(virDomainInfo));
1621

1622 1623 1624 1625 1626 1627 1628 1629
    for (i = 0;i < domain->conn->nb_drivers;i++) {
	if ((domain->conn->drivers[i] != NULL) &&
	    (domain->conn->drivers[i]->domainGetInfo != NULL)) {
	    if (domain->conn->drivers[i]->domainGetInfo(domain, info) == 0)
	        return 0;
	}
    }

1630
    return (-1);
1631
}
1632

1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
/**
 * virDomainGetXMLDesc:
 * @domain: a domain object
 * @flags: and OR'ed set of extraction flags, not used yet
 *
 * Provide an XML description of the domain. The description may be reused
 * later to relaunch the domain with virDomainCreateLinux().
 *
 * Returns a 0 terminated UTF-8 encoded XML instance, or NULL in case of error.
 *         the caller must free() the returned value.
 */
char *
1645 1646
virDomainGetXMLDesc(virDomainPtr domain, int flags)
{
1647 1648
    int i;
    char *ret = NULL;
D
Daniel Veillard 已提交
1649 1650
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1651
        return (NULL);
D
Daniel Veillard 已提交
1652 1653 1654
    }
    if (flags != 0) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1655
        return (NULL);
D
Daniel Veillard 已提交
1656
    }
1657

1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
    for (i = 0;i < domain->conn->nb_drivers;i++) {
	if ((domain->conn->drivers[i] != NULL) &&
	    (domain->conn->drivers[i]->domainDumpXML != NULL)) {
            ret = domain->conn->drivers[i]->domainDumpXML(domain, flags);
	    if (ret)
	        break;
	}
    }
    if (!ret) {
        virLibConnError(domain->conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
        return (NULL);
    }
    return(ret);
1671
}
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709

/**
 * virNodeGetInfo:
 * @conn: pointer to the hypervisor connection
 * @info: pointer to a virNodeInfo structure allocated by the user
 * 
 * Extract hardware information about the node.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
virNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info) {
    int i;
    int ret = -1;

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }
    if (info == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }

    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->nodeGetInfo != NULL)) {
	    ret = conn->drivers[i]->nodeGetInfo(conn, info);
	    if (ret == 0)
	        break;
	}
    }
    if (ret != 0) {
        virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
        return (-1);
    }
    return(0);
}
1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728

/************************************************************************
 *									*
 *		Handling of defined but not running domains		*
 *									*
 ************************************************************************/

/**
 * virDomainDefineXML:
 * @conn: pointer to the hypervisor connection
 * @xml: the XML description for the domain, preferably in UTF-8
 *
 * define a domain, but does not start it
 *
 * Returns NULL in case of error, a pointer to the domain otherwise
 */
virDomainPtr
virDomainDefineXML(virConnectPtr conn, const char *xml) {
    virDomainPtr ret = NULL;
1729
    int i;
1730 1731 1732 1733 1734

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
1735 1736 1737 1738
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (NULL);
    }
1739 1740 1741 1742 1743
    if (xml == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
    }

1744 1745 1746 1747 1748 1749 1750 1751
    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainDefineXML != NULL)) {
            ret = conn->drivers[i]->domainDefineXML(conn, xml);
	    if (ret)
	        return(ret);
	}
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766
    }

    return(ret);
}

/**
 * virDomainUndefine:
 * @domain: pointer to a defined domain
 *
 * undefine a domain but does not stop it if it is running
 *
 * Returns 0 in case of success, -1 in case of error
 */
int
virDomainUndefine(virDomainPtr domain) {
1767 1768
    int ret, i;
    virConnectPtr conn;
1769 1770 1771 1772 1773

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
1774 1775
    conn = domain->conn;
    if (conn->flags & VIR_CONNECT_RO) {
1776 1777 1778 1779
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }

1780 1781 1782 1783 1784 1785 1786 1787 1788
    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainUndefine != NULL)) {
	    ret = conn->drivers[i]->domainUndefine(domain);
	    if (ret >= 0)
	        return(ret);
	}
    }
1789

1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822
    return(-1);
}

/**
 * virConnectNumOfDefinedDomains:
 * @conn: pointer to the hypervisor connection
 *
 * Provides the number of active domains.
 *
 * Returns the number of domain found or -1 in case of error
 */
int
virConnectNumOfDefinedDomains(virConnectPtr conn)
{
    int ret = -1;
    int i;

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->numOfDefinedDomains != NULL)) {
	    ret = conn->drivers[i]->numOfDefinedDomains(conn);
	    if (ret >= 0)
	        return(ret);
	}
    }

    return(-1);
1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835
}

/**
 * virConnectListDefinedDomains:
 * @conn: pointer to the hypervisor connection
 * @names: pointer to an array to store the names
 * @maxnames: size of the array
 *
 * list the defined domains, stores the pointers to the names in @names
 * 
 * Returns the number of names provided in the array or -1 in case of error
 */
int
1836
virConnectListDefinedDomains(virConnectPtr conn, char **const names,
1837
                             int maxnames) {
1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
    int ret = -1;
    int i;

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

    if ((names == NULL) || (maxnames <= 0)) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }

    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->listDefinedDomains != NULL)) {
	    ret = conn->drivers[i]->listDefinedDomains(conn, names, maxnames);
	    if (ret >= 0)
	        return(ret);
	}
    }

    return (-1);
1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
}

/**
 * virDomainCreate:
 * @domain: pointer to a defined domain
 *
 * launch a defined domain. If the call succeed the domain moves from the
 * defined to the running domains pools.
 *
 * Returns 0 in case of success, -1 in case of error
 */
int
virDomainCreate(virDomainPtr domain) {
1875 1876 1877 1878 1879 1880
    int i, ret = -1;
    virConnectPtr conn;
    if (domain == NULL) {
        TODO
	return (-1);
    }
1881 1882 1883 1884
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
1885 1886
    conn = domain->conn;
    if (conn->flags & VIR_CONNECT_RO) {
1887 1888 1889
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899

    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainCreate != NULL)) {
	    ret = conn->drivers[i]->domainCreate(domain);
	    if (ret == 0)
	        return(ret);
	}
    }
    return(ret);
1900 1901
}

1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964
/**
 * virDomainGetAutostart:
 * @domain: a domain object
 *
 * Return a boolean value indicating whether the domain
 * configured to be automatically started when the host
 * machine boots.
 *
 * Returns -1 in case of error, 0 in case of success
 */
int
virDomainGetAutostart(virDomainPtr domain,
                      int *autostart) {
    int i;

    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
    if (!autostart) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }

    for (i = 0;i < domain->conn->nb_drivers;i++) {
	if ((domain->conn->drivers[i] != NULL) &&
	    (domain->conn->drivers[i]->domainGetAutostart != NULL) &&
            (domain->conn->drivers[i]->domainGetAutostart(domain, autostart) == 0))
            return (0);
    }
    virLibConnError(domain->conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
    return (-1);
}

/**
 * virDomainSetAutostart:
 * @domain: a domain object
 *
 * Configure the domain to be automatically started
 * when the host machine boots.
 *
 * Returns -1 in case of error, 0 in case of success
 */
int
virDomainSetAutostart(virDomainPtr domain,
                      int autostart) {
    int i;

    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }

    for (i = 0;i < domain->conn->nb_drivers;i++) {
	if ((domain->conn->drivers[i] != NULL) &&
	    (domain->conn->drivers[i]->domainSetAutostart != NULL) &&
            (domain->conn->drivers[i]->domainSetAutostart(domain, autostart) == 0))
            return (0);
    }
    virLibConnError(domain->conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
    return (-1);
}

1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
/**
 * virDomainSetVcpus:
 * @domain: pointer to domain object, or NULL for Domain0
 * @nvcpus: the new number of virtual CPUs for this domain
 *
 * Dynamically change the number of virtual CPUs used by the domain.
 * Note that this call may fail if the underlying virtualization hypervisor
 * does not support it or if growing the number is arbitrary limited.
 * This function requires priviledged access to the hypervisor.
 *
 * Returns 0 in case of success, -1 in case of failure.
 */

int
virDomainSetVcpus(virDomainPtr domain, unsigned int nvcpus)
{
1981 1982 1983
    int i;
    virConnectPtr conn;

1984 1985 1986 1987 1988 1989 1990 1991
    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
1992 1993 1994 1995
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
1996

1997 1998 1999 2000
    if (nvcpus < 1) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
2001
    conn = domain->conn;
2002

2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025
    /*
     * Go though the driver registered entry points but use the 
     * XEN_HYPERVISOR directly only as a last mechanism
     */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->no != VIR_DRV_XEN_HYPERVISOR) &&
	    (conn->drivers[i]->domainSetVcpus != NULL)) {
	    if (conn->drivers[i]->domainSetVcpus(domain, nvcpus) == 0)
	        return(0);
	}
    }
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->no == VIR_DRV_XEN_HYPERVISOR) &&
	    (conn->drivers[i]->domainSetVcpus != NULL)) {
	    if (conn->drivers[i]->domainSetVcpus(domain, nvcpus) == 0)
	        return(0);
	}
    }

    virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
    return (-1);
2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049
}

/**
 * virDomainPinVcpu:
 * @domain: pointer to domain object, or NULL for Domain0
 * @vcpu: virtual CPU number
 * @cpumap: pointer to a bit map of real CPUs (in 8-bit bytes) (IN)
 * 	Each bit set to 1 means that corresponding CPU is usable.
 * 	Bytes are stored in little-endian order: CPU0-7, 8-15...
 * 	In each byte, lowest CPU number is least significant bit.
 * @maplen: number of bytes in cpumap, from 1 up to size of CPU map in
 *	underlying virtualization system (Xen...).
 *	If maplen < size, missing bytes are set to zero.
 *	If maplen > size, failure code is returned.
 * 
 * Dynamically change the real CPUs which can be allocated to a virtual CPU.
 * This function requires priviledged access to the hypervisor.
 *
 * Returns 0 in case of success, -1 in case of failure.
 */
int
virDomainPinVcpu(virDomainPtr domain, unsigned int vcpu,
                 unsigned char *cpumap, int maplen)
{
2050 2051 2052
    int i;
    virConnectPtr conn;

2053 2054 2055 2056 2057 2058 2059 2060
    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
2061 2062 2063 2064
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
2065

2066
    if ((vcpu > 32000) || (cpumap == NULL) || (maplen < 1)) {
2067 2068 2069
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084
    conn = domain->conn;

    /*
     * Go though the driver registered entry points
     */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainPinVcpu != NULL)) {
	    if (conn->drivers[i]->domainPinVcpu(domain, vcpu,
	                                        cpumap, maplen) == 0)
	        return(0);
	}
    }
    virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
    return (-1);
2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112
}

/**
 * virDomainGetVcpus:
 * @domain: pointer to domain object, or NULL for Domain0
 * @info: pointer to an array of virVcpuInfo structures (OUT)
 * @maxinfo: number of structures in info array
 * @cpumaps: pointer to an bit map of real CPUs for all vcpus of this
 *      domain (in 8-bit bytes) (OUT)
 *	If cpumaps is NULL, then no cupmap information is returned by the API.
 *	It's assumed there is <maxinfo> cpumap in cpumaps array.
 *	The memory allocated to cpumaps must be (maxinfo * maplen) bytes
 *	(ie: calloc(maxinfo, maplen)).
 *	One cpumap inside cpumaps has the format described in
 *      virDomainPinVcpu() API.
 * @maplen: number of bytes in one cpumap, from 1 up to size of CPU map in
 *	underlying virtualization system (Xen...).
 * 
 * Extract information about virtual CPUs of domain, store it in info array
 * and also in cpumaps if this pointer is'nt NULL.
 *
 * Returns the number of info filled in case of success, -1 in case of failure.
 */
int
virDomainGetVcpus(virDomainPtr domain, virVcpuInfoPtr info, int maxinfo,
		  unsigned char *cpumaps, int maplen)
{
    int ret;
2113 2114
    int i;
    virConnectPtr conn;
2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131

    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
    if ((info == NULL) || (maxinfo < 1)) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
    if (cpumaps != NULL && maplen < 1) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147
    conn = domain->conn;

    /*
     * Go though the driver registered entry points
     */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainGetVcpus != NULL)) {
	    ret = conn->drivers[i]->domainGetVcpus(domain, info, maxinfo,
	                                           cpumaps, maplen);
	    if (ret >= 0)
	        return(ret);
	}
    }
    virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
    return (-1);
2148
}
2149

2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187
/**
 * virDomainGetMaxVcpus:
 * @domain: pointer to domain object
 * 
 *  Returns the maximum number of virtual CPUs supported for
 *  the guest VM. If the guest is inactive, this is basically
 *  the same as virConnectGetMaxVcpus. If the guest is running
 *  this will reflect the maximum number of virtual CPUs the
 *  guest was booted with.
 *
 * Returns the maximum of virtual CPU or -1 in case of error.
 */
int
virDomainGetMaxVcpus(virDomainPtr domain) {
    int i;
    int ret = 0;
    virConnectPtr conn;

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }

    conn = domain->conn;

    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainGetMaxVcpus != NULL)) {
	    ret = conn->drivers[i]->domainGetMaxVcpus(domain);
	    if (ret != 0)
	        return(ret);
	}
    }
    virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
    return (-1);
}


2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268
/**
 * virDomainAttachDevice:
 * @domain: pointer to domain object
 * @xml: pointer to XML description of one device
 * 
 * Create a virtual device attachment to backend.
 *
 * Returns 0 in case of success, -1 in case of failure.
 */
int
virDomainAttachDevice(virDomainPtr domain, char *xml)
{
    int ret;
    int i;
    virConnectPtr conn;

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
    conn = domain->conn;

    /*
     * Go though the driver registered entry points
     */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainAttachDevice != NULL)) {
	    ret = conn->drivers[i]->domainAttachDevice(domain, xml);
	    if (ret >= 0)
	        return(ret);
	}
    }
    virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
    return (-1);
}

/**
 * virDomainDetachDevice:
 * @domain: pointer to domain object
 * @xml: pointer to XML description of one device
 * 
 * Destroy a virtual device attachment to backend.
 *
 * Returns 0 in case of success, -1 in case of failure.
 */
int
virDomainDetachDevice(virDomainPtr domain, char *xml)
{
    int ret;
    int i;
    virConnectPtr conn;

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
    conn = domain->conn;

    /*
     * Go though the driver registered entry points
     */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainDetachDevice != NULL)) {
	    ret = conn->drivers[i]->domainDetachDevice(domain, xml);
	    if (ret >= 0)
	        return(ret);
	}
    }
    virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
    return (-1);
}
2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312

/**
 * virConnectNumOfNetworks:
 * @conn: pointer to the hypervisor connection
 *
 * Provides the number of active networks.
 *
 * Returns the number of network found or -1 in case of error
 */
int
virConnectNumOfNetworks(virConnectPtr conn)
{
    int ret = -1;
    int i;

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_network_drivers;i++) {
	if ((conn->networkDrivers[i] != NULL) &&
	    (conn->networkDrivers[i]->numOfNetworks != NULL)) {
	    ret = conn->networkDrivers[i]->numOfNetworks(conn);
	    if (ret >= 0)
	        return(ret);
	}
    }

    return(-1);
}

/**
 * virConnectListNetworks:
 * @conn: pointer to the hypervisor connection
 * @names: array to collect the list of names of active networks
 * @maxnames: size of @names
 *
 * Collect the list of active networks, and store their names in @names
 *
 * Returns the number of networks found or -1 in case of error
 */
int
2313
virConnectListNetworks(virConnectPtr conn, char **const names, int maxnames)
2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383
{
    int ret = -1;
    int i;

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

    if ((names == NULL) || (maxnames <= 0)) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }

    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->networkDrivers[i] != NULL) &&
	    (conn->networkDrivers[i]->listNetworks != NULL)) {
	    ret = conn->networkDrivers[i]->listNetworks(conn, names, maxnames);
	    if (ret >= 0)
	        return(ret);
	}
    }

    return (-1);
}

/**
 * virConnectNumOfDefinedNetworks:
 * @conn: pointer to the hypervisor connection
 *
 * Provides the number of inactive networks.
 *
 * Returns the number of networks found or -1 in case of error
 */
int
virConnectNumOfDefinedNetworks(virConnectPtr conn)
{
    int ret = -1;
    int i;

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->networkDrivers[i] != NULL) &&
	    (conn->networkDrivers[i]->numOfDefinedNetworks != NULL)) {
	    ret = conn->networkDrivers[i]->numOfDefinedNetworks(conn);
	    if (ret >= 0)
	        return(ret);
	}
    }

    return(-1);
}

/**
 * virConnectListDefinedNetworks:
 * @conn: pointer to the hypervisor connection
 * @names: pointer to an array to store the names
 * @maxnames: size of the array
 *
 * list the inactive networks, stores the pointers to the names in @names
 *
 * Returns the number of names provided in the array or -1 in case of error
 */
int
2384
virConnectListDefinedNetworks(virConnectPtr conn, char **const names,
2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872
                              int maxnames) {
    int ret = -1;
    int i;

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

    if ((names == NULL) || (maxnames <= 0)) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }

    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->networkDrivers[i] != NULL) &&
	    (conn->networkDrivers[i]->listDefinedNetworks != NULL)) {
	    ret = conn->networkDrivers[i]->listDefinedNetworks(conn, names, maxnames);
	    if (ret >= 0)
	        return(ret);
	}
    }

    return (-1);
}

/**
 * virNetworkLookupByName:
 * @conn: pointer to the hypervisor connection
 * @name: name for the network
 *
 * Try to lookup a network on the given hypervisor based on its name.
 *
 * Returns a new network object or NULL in case of failure
 */
virNetworkPtr
virNetworkLookupByName(virConnectPtr conn, const char *name)
{
    virNetworkPtr ret = NULL;
    int i;

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
    if (name == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
    }

    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->networkDrivers[i] != NULL) &&
	    (conn->networkDrivers[i]->networkLookupByName != NULL)) {
	    ret = conn->networkDrivers[i]->networkLookupByName(conn, name);
	    if (ret)
	        return(ret);
	}
    }
    return (NULL);
}

/**
 * virNetworkLookupByUUID:
 * @conn: pointer to the hypervisor connection
 * @uuid: the raw UUID for the network
 *
 * Try to lookup a network on the given hypervisor based on its UUID.
 *
 * Returns a new network object or NULL in case of failure
 */
virNetworkPtr
virNetworkLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
{
    virNetworkPtr ret;
    int i;

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
    if (uuid == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
    }

    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->networkDrivers[i] != NULL) &&
	    (conn->networkDrivers[i]->networkLookupByUUID != NULL)) {
	    ret = conn->networkDrivers[i]->networkLookupByUUID(conn, uuid);
	    if (ret)
	        return(ret);
	}
    }

    return (NULL);
}

/**
 * virNetworkLookupByUUIDString:
 * @conn: pointer to the hypervisor connection
 * @uuidstr: the string UUID for the network
 *
 * Try to lookup a network on the given hypervisor based on its UUID.
 *
 * Returns a new network object or NULL in case of failure
 */
virNetworkPtr
virNetworkLookupByUUIDString(virConnectPtr conn, const char *uuidstr)
{
    int raw[VIR_UUID_BUFLEN], i;
    unsigned char uuid[VIR_UUID_BUFLEN];
    int ret;

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
    if (uuidstr == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
    }

    /* XXX: sexpr_uuid() also supports 'xxxx-xxxx-xxxx-xxxx' format.
     *      We needn't it here. Right?
     */
    ret = sscanf(uuidstr,
                 "%02x%02x%02x%02x-"
                 "%02x%02x-"
                 "%02x%02x-"
                 "%02x%02x-"
                 "%02x%02x%02x%02x%02x%02x",
                 raw + 0, raw + 1, raw + 2, raw + 3,
                 raw + 4, raw + 5, raw + 6, raw + 7,
                 raw + 8, raw + 9, raw + 10, raw + 11,
                 raw + 12, raw + 13, raw + 14, raw + 15);

    if (ret!=VIR_UUID_BUFLEN) {
	virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return (NULL);
    }
    for (i = 0; i < VIR_UUID_BUFLEN; i++)
        uuid[i] = raw[i] & 0xFF;

    return virNetworkLookupByUUID(conn, &uuid[0]);
}

/**
 * virNetworkCreateXML:
 * @conn: pointer to the hypervisor connection
 * @xmlDesc: an XML description of the network
 *
 * Create and start a new virtual network, based on an XML description
 * similar to the one returned by virNetworkGetXMLDesc()
 *
 * Returns a new network object or NULL in case of failure
 */
virNetworkPtr
virNetworkCreateXML(virConnectPtr conn, const char *xmlDesc)
{
    virNetworkPtr ret;
    int i;

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
    if (xmlDesc == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
    }
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (NULL);
    }

    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->networkDrivers[i] != NULL) &&
	    (conn->networkDrivers[i]->networkCreateXML != NULL)) {
	    ret = conn->networkDrivers[i]->networkCreateXML(conn, xmlDesc);
	    if (ret != NULL)
	        return(ret);
	}
    }
    return(NULL);
}

/**
 * virNetworkDefineXML:
 * @conn: pointer to the hypervisor connection
 * @xml: the XML description for the network, preferably in UTF-8
 *
 * Define a network, but does not create it
 *
 * Returns NULL in case of error, a pointer to the network otherwise
 */
virNetworkPtr
virNetworkDefineXML(virConnectPtr conn, const char *xml) {
    virNetworkPtr ret = NULL;
    int i;

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (NULL);
    }
    if (xml == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
    }

    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->networkDrivers[i] != NULL) &&
	    (conn->networkDrivers[i]->networkDefineXML != NULL)) {
            ret = conn->networkDrivers[i]->networkDefineXML(conn, xml);
	    if (ret)
	        return(ret);
	}
    }

    return(ret);
}

/**
 * virNetworkUndefine:
 * @network: pointer to a defined network
 *
 * Undefine a network but does not stop it if it is running
 *
 * Returns 0 in case of success, -1 in case of error
 */
int
virNetworkUndefine(virNetworkPtr network) {
    int ret, i;
    virConnectPtr conn;

    if (!VIR_IS_CONNECTED_NETWORK(network)) {
        virLibNetworkError(network, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return (-1);
    }
    conn = network->conn;
    if (conn->flags & VIR_CONNECT_RO) {
        virLibNetworkError(network, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }

    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->networkDrivers[i] != NULL) &&
	    (conn->networkDrivers[i]->networkUndefine != NULL)) {
	    ret = conn->networkDrivers[i]->networkUndefine(network);
	    if (ret >= 0)
	        return(ret);
	}
    }

    return(-1);
}

/**
 * virNetworkCreate:
 * @network: pointer to a defined network
 *
 * Create and start a defined network. If the call succeed the network
 * moves from the defined to the running networks pools.
 *
 * Returns 0 in case of success, -1 in case of error
 */
int
virNetworkCreate(virNetworkPtr network) {
    int i, ret = -1;
    virConnectPtr conn;
    if (network == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_NETWORK(network)) {
        virLibNetworkError(network, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return (-1);
    }
    conn = network->conn;
    if (conn->flags & VIR_CONNECT_RO) {
        virLibNetworkError(network, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }

    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->networkDrivers[i] != NULL) &&
	    (conn->networkDrivers[i]->networkCreate != NULL)) {
	    ret = conn->networkDrivers[i]->networkCreate(network);
	    if (ret == 0)
	        return(ret);
	}
    }
    return(ret);
}

/**
 * virNetworkDestroy:
 * @network: a network object
 *
 * Destroy the network object. The running instance is shutdown if not down
 * already and all resources used by it are given back to the hypervisor.
 * The data structure is freed and should not be used thereafter if the
 * call does not return an error.
 * This function may requires priviledged access
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
virNetworkDestroy(virNetworkPtr network)
{
    int i;
    virConnectPtr conn;

    if (!VIR_IS_CONNECTED_NETWORK(network)) {
        virLibNetworkError(network, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return (-1);
    }

    conn = network->conn;
    if (conn->flags & VIR_CONNECT_RO) {
        virLibNetworkError(network, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }

    /*
     * Go though the driver registered entry points
     */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->networkDrivers[i] != NULL) &&
	    (conn->networkDrivers[i]->networkDestroy != NULL)) {
	    if (conn->networkDrivers[i]->networkDestroy(network) == 0)
	        return (0);
	}
    }

    virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
    return (-1);
}

/**
 * virNetworkFree:
 * @network: a network object
 *
 * Free the network object. The running instance is kept alive.
 * The data structure is freed and should not be used thereafter.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
virNetworkFree(virNetworkPtr network)
{
    if (!VIR_IS_NETWORK(network)) {
        virLibNetworkError(network, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return (-1);
    }
    if (virFreeNetwork(network->conn, network) < 0)
        return (-1);
    return(0);
}

/**
 * virNetworkGetName:
 * @network: a network object
 *
 * Get the public name for that network
 *
 * Returns a pointer to the name or NULL, the string need not be deallocated
 * its lifetime will be the same as the network object.
 */
const char *
virNetworkGetName(virNetworkPtr network)
{
    if (!VIR_IS_NETWORK(network)) {
        virLibNetworkError(network, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return (NULL);
    }
    return (network->name);
}

/**
 * virNetworkGetUUID:
 * @network: a network object
 * @uuid: pointer to a VIR_UUID_BUFLEN bytes array
 *
 * Get the UUID for a network
 *
 * Returns -1 in case of error, 0 in case of success
 */
int
virNetworkGetUUID(virNetworkPtr network, unsigned char *uuid)
{
    if (!VIR_IS_NETWORK(network)) {
        virLibNetworkError(network, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return (-1);
    }
    if (uuid == NULL) {
        virLibNetworkError(network, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }

    memcpy(uuid, &network->uuid[0], VIR_UUID_BUFLEN);

    return (0);
}

/**
 * virNetworkGetUUIDString:
 * @network: a network object
 * @buf: pointer to a VIR_UUID_STRING_BUFLEN bytes array
 *
 * Get the UUID for a network as string. For more information about
 * UUID see RFC4122.
 *
 * Returns -1 in case of error, 0 in case of success
 */
int
virNetworkGetUUIDString(virNetworkPtr network, char *buf)
{
    unsigned char uuid[VIR_UUID_BUFLEN];

    if (!VIR_IS_NETWORK(network)) {
        virLibNetworkError(network, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return (-1);
    }
    if (buf == NULL) {
        virLibNetworkError(network, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }

    if (virNetworkGetUUID(network, &uuid[0]))
	return (-1);

    snprintf(buf, VIR_UUID_STRING_BUFLEN,
	"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
                      uuid[0], uuid[1], uuid[2], uuid[3],
                      uuid[4], uuid[5], uuid[6], uuid[7],
                      uuid[8], uuid[9], uuid[10], uuid[11],
                      uuid[12], uuid[13], uuid[14], uuid[15]);
    return (0);
}

/**
 * virNetworkGetXMLDesc:
 * @network: a network object
 * @flags: and OR'ed set of extraction flags, not used yet
 *
 * Provide an XML description of the network. The description may be reused
 * later to relaunch the network with virNetworkCreateXML().
 *
 * Returns a 0 terminated UTF-8 encoded XML instance, or NULL in case of error.
 *         the caller must free() the returned value.
 */
char *
virNetworkGetXMLDesc(virNetworkPtr network, int flags)
{
    int i;
    char *ret = NULL;
    if (!VIR_IS_NETWORK(network)) {
        virLibNetworkError(network, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return (NULL);
    }
    if (flags != 0) {
        virLibNetworkError(network, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
    }

    for (i = 0;i < network->conn->nb_network_drivers;i++) {
	if ((network->conn->networkDrivers[i] != NULL) &&
	    (network->conn->networkDrivers[i]->networkDumpXML != NULL)) {
            ret = network->conn->networkDrivers[i]->networkDumpXML(network, flags);
	    if (ret)
	        break;
	}
    }
    if (!ret) {
        virLibConnError(network->conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
        return (NULL);
    }
    return(ret);
}
2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907

/**
 * virNetworkGetBridgeName:
 * @network: a network object
 *
 * Return a bridge interface name to which a domain may connect
 * a network interface in order to join the network.
 *
 * Returns a 0 terminated interface name, or NULL in case of error.
 *         the caller must free() the returned value.
 */
char *
virNetworkGetBridgeName(virNetworkPtr network)
{
    int i;
    char *ret = NULL;
    if (!VIR_IS_NETWORK(network)) {
        virLibNetworkError(network, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return (NULL);
    }

    for (i = 0;i < network->conn->nb_network_drivers;i++) {
	if ((network->conn->networkDrivers[i] != NULL) &&
	    (network->conn->networkDrivers[i]->networkGetBridgeName != NULL)) {
            ret = network->conn->networkDrivers[i]->networkGetBridgeName(network);
	    if (ret)
	        break;
	}
    }
    if (!ret) {
        virLibConnError(network->conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
        return (NULL);
    }
    return(ret);
}
2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970

/**
 * virNetworkGetAutostart:
 * @network: a network object
 *
 * Return a boolean value indicating whether the network
 * configured to be automatically started when the host
 * machine boots.
 *
 * Returns -1 in case of error, 0 in case of success
 */
int
virNetworkGetAutostart(virNetworkPtr network,
                       int *autostart) {
    int i;

    if (!VIR_IS_NETWORK(network)) {
        virLibNetworkError(network, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return (-1);
    }
    if (!autostart) {
        virLibNetworkError(network, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }

    for (i = 0;i < network->conn->nb_network_drivers;i++) {
	if ((network->conn->networkDrivers[i] != NULL) &&
	    (network->conn->networkDrivers[i]->networkGetAutostart != NULL) &&
            (network->conn->networkDrivers[i]->networkGetAutostart(network, autostart) == 0))
            return (0);
    }
    virLibConnError(network->conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
    return (-1);
}

/**
 * virNetworkSetAutostart:
 * @network: a network object
 *
 * Configure the network to be automatically started
 * when the host machine boots.
 *
 * Returns -1 in case of error, 0 in case of success
 */
int
virNetworkSetAutostart(virNetworkPtr network,
                       int autostart) {
    int i;

    if (!VIR_IS_NETWORK(network)) {
        virLibNetworkError(network, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return (-1);
    }

    for (i = 0;i < network->conn->nb_network_drivers;i++) {
	if ((network->conn->networkDrivers[i] != NULL) &&
	    (network->conn->networkDrivers[i]->networkSetAutostart != NULL) &&
            (network->conn->networkDrivers[i]->networkSetAutostart(network, autostart) == 0))
            return (0);
    }
    virLibConnError(network->conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
    return (-1);
}