libvirt.c 80.1 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 "config.h"
13
#include "libvirt/libvirt.h"
D
Daniel Veillard 已提交
14

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

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

D
Daniel Veillard 已提交
25
#include "internal.h"
26
#include "driver.h"
27

28
#include "xml.h"
29
#include "test.h"
30
#include "xen_unified.h"
31
#include "remote_internal.h"
32
#include "qemu_driver.h"
33

D
Daniel Veillard 已提交
34 35 36 37 38 39
/*
 * TODO:
 * - use lock to protect against concurrent accesses ?
 * - use reference counting to garantee coherent pointer state ?
 */

40
static virDriverPtr virDriverTab[MAX_DRIVERS];
41
static int virDriverTabCount = 0;
42
static virNetworkDriverPtr virNetworkDriverTab[MAX_DRIVERS];
43
static int virNetworkDriverTabCount = 0;
44 45
static virStateDriverPtr virStateDriverTab[MAX_DRIVERS];
static int virStateDriverTabCount = 0;
46 47
static int initialized = 0;

48 49 50 51 52 53 54 55 56 57 58 59 60
/* If configured with --enable-debug=yes then library calls
 * are printed to stderr for debugging.
 */
#ifdef ENABLE_DEBUG
#define DEBUG(fs,...)                                                   \
    fprintf (stderr, "libvirt: %s (" fs ")\n", __func__, __VA_ARGS__)
#define DEBUG0                                                          \
    fprintf (stderr, "libvirt: %s ()\n", __func__)
#else
#define DEBUG0
#define DEBUG(fs,...)
#endif /* !ENABLE_DEBUG */

61 62 63 64 65 66 67 68 69 70 71 72
/**
 * 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)
{
73
    DEBUG0;
74 75
    if (initialized)
        return(0);
76
    initialized = 1;
77

78 79 80
    if (!bindtextdomain(GETTEXT_PACKAGE, LOCALEBASEDIR))
        return (-1);

81
    /*
82 83
     * Note that the order is important: the first ones have a higher
     * priority when calling virConnectOpen.
84
     */
85
#ifdef WITH_TEST
86
    if (testRegister() == -1) return -1;
87
#endif
88
#ifdef WITH_QEMU 
89
    if (qemudRegister() == -1) return -1;
90
#endif
91 92
#ifdef WITH_XEN
    if (xenUnifiedRegister () == -1) return -1;
93
#endif
94 95 96
#ifdef WITH_OPENVZ
    if (openvzRegister() == -1) return -1;
#endif
97 98
#ifdef WITH_REMOTE
    if (remoteRegister () == -1) return -1;
99
#endif
D
Daniel P. Berrange 已提交
100

101 102 103 104 105
    return(0);
}



D
Daniel Veillard 已提交
106 107 108
/**
 * virLibConnError:
 * @conn: the connection if available
109
 * @error: the error number
D
Daniel Veillard 已提交
110 111 112 113 114
 * @info: extra information string
 *
 * Handle an error at the connection level
 */
static void
115 116
virLibConnError(virConnectPtr conn, virErrorNumber error, const char *info)
{
D
Daniel Veillard 已提交
117
    const char *errmsg;
118

D
Daniel Veillard 已提交
119 120 121 122
    if (error == VIR_ERR_OK)
        return;

    errmsg = __virErrorMsg(error, info);
123
    __virRaiseError(conn, NULL, NULL, VIR_FROM_NONE, error, VIR_ERR_ERROR,
D
Daniel Veillard 已提交
124 125 126 127
                    errmsg, info, NULL, 0, 0, errmsg, info);
}

/**
128
 * virLibConnWarning:
D
Daniel Veillard 已提交
129
 * @conn: the connection if available
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
 * @error: the error number
 * @info: extra information string
 *
 * Handle an error at the connection level
 */
static void
virLibConnWarning(virConnectPtr conn, virErrorNumber error, const char *info)
{
    const char *errmsg;

    if (error == VIR_ERR_OK)
        return;

    errmsg = __virErrorMsg(error, info);
    __virRaiseError(conn, NULL, NULL, VIR_FROM_NONE, error, VIR_ERR_WARNING,
                    errmsg, info, NULL, 0, 0, errmsg, info);
}

/**
 * virLibDomainError:
 * @domain: the domain if available
 * @error: the error number
D
Daniel Veillard 已提交
152 153 154 155 156
 * @info: extra information string
 *
 * Handle an error at the connection level
 */
static void
157 158 159
virLibDomainError(virDomainPtr domain, virErrorNumber error,
                  const char *info)
{
D
Daniel Veillard 已提交
160 161
    virConnectPtr conn = NULL;
    const char *errmsg;
162

D
Daniel Veillard 已提交
163 164 165 166 167 168 169
    if (error == VIR_ERR_OK)
        return;

    errmsg = __virErrorMsg(error, info);
    if (error != VIR_ERR_INVALID_DOMAIN) {
        conn = domain->conn;
    }
170
    __virRaiseError(conn, domain, NULL, VIR_FROM_DOM, error, VIR_ERR_ERROR,
D
Daniel Veillard 已提交
171 172 173
                    errmsg, info, NULL, 0, 0, errmsg, info);
}

174
/**
175 176 177 178
 * virLibNetworkError:
 * @conn: the connection if available
 * @error: the error noumber
 * @info: extra information string
179
 *
180
 * Handle an error at the connection level
181
 */
182 183 184 185 186 187 188 189 190 191 192 193 194 195
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;
    }
196
    __virRaiseError(conn, NULL, network, VIR_FROM_NET, error, VIR_ERR_ERROR,
197 198 199 200 201 202 203 204 205 206 207 208 209 210
                    errmsg, info, NULL, 0, 0, errmsg, info);
}

/**
 * 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)
{
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    if (virInitialize() < 0)
      return -1;

    if (driver == NULL) {
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return(-1);
    }

    if (virNetworkDriverTabCount >= MAX_DRIVERS) {
    	virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return(-1);
    }

    virNetworkDriverTab[virNetworkDriverTabCount] = driver;
    return virNetworkDriverTabCount++;
226 227 228 229 230 231 232 233 234 235 236 237 238
}

/**
 * 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)
{
239 240 241 242 243 244 245 246 247 248 249 250 251
    if (virInitialize() < 0)
      return -1;

    if (driver == NULL) {
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return(-1);
    }

    if (virDriverTabCount >= MAX_DRIVERS) {
    	virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return(-1);
    }

252 253 254 255 256 257 258
    if (driver->no < 0) {
    	virLibConnError
            (NULL, VIR_ERR_INVALID_ARG,
             "virRegisterDriver: tried to register an internal Xen driver");
        return -1;
    }

259 260
    virDriverTab[virDriverTabCount] = driver;
    return virDriverTabCount++;
261 262
}

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
/**
 * virRegisterStateDriver:
 * @driver: pointer to a driver block
 *
 * Register a virtualization driver
 *
 * Returns the driver priority or -1 in case of error.
 */
int
virRegisterStateDriver(virStateDriverPtr driver)
{
    if (virInitialize() < 0)
      return -1;

    if (driver == NULL) {
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return(-1);
    }

    if (virStateDriverTabCount >= MAX_DRIVERS) {
    	virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return(-1);
    }

    virStateDriverTab[virStateDriverTabCount] = driver;
    return virStateDriverTabCount++;
}

int __virStateInitialize(void) {
    int i, ret = 0;

294 295 296
    if (virInitialize() < 0)
        return -1;

297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
    if (virInitialize() < 0)
        return -1;

    for (i = 0 ; i < virStateDriverTabCount ; i++) {
        if (virStateDriverTab[i]->initialize() < 0)
            ret = -1;
    }
    return ret;
}

int __virStateCleanup(void) {
    int i, ret = 0;

    for (i = 0 ; i < virStateDriverTabCount ; i++) {
        if (virStateDriverTab[i]->cleanup() < 0)
            ret = -1;
    }
    return ret;
}

int __virStateReload(void) {
    int i, ret = 0;

    for (i = 0 ; i < virStateDriverTabCount ; i++) {
        if (virStateDriverTab[i]->reload() < 0)
            ret = -1;
    }
    return ret;
}

int __virStateActive(void) {
    int i, ret = 0;

    for (i = 0 ; i < virStateDriverTabCount ; i++) {
        if (virStateDriverTab[i]->active())
            ret = 1;
    }
    return ret;
}



339 340 341
/**
 * virGetVersion:
 * @libVer: return value for the library version (OUT)
342
 * @type: the type of connection/driver looked at
343 344 345 346 347 348 349 350 351 352 353 354
 * @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
355 356 357
virGetVersion(unsigned long *libVer, const char *type,
              unsigned long *typeVer)
{
358
    int i;
359
    DEBUG("libVir=%p, type=%s, typeVer=%p", libVer, type, typeVer);
360

361
    if (!initialized)
362 363
        if (virInitialize() < 0)
	    return -1;
364

365
    if (libVer == NULL)
366
        return (-1);
367 368 369
    *libVer = LIBVIR_VERSION_NUMBER;

    if (typeVer != NULL) {
370 371
        if (type == NULL)
	    type = "Xen";
372
	for (i = 0;i < virDriverTabCount;i++) {
373
	    if ((virDriverTab[i] != NULL) &&
374
	        (!strcasecmp(virDriverTab[i]->name, type))) {
375 376 377 378
		*typeVer = virDriverTab[i]->ver;
		break;
	    }
	}
379
        if (i >= virDriverTabCount) {
380
            *typeVer = 0;
381
            virLibConnError(NULL, VIR_ERR_NO_SUPPORT, type);
382 383 384 385
            return (-1);
        }
    }
    return (0);
386 387
}

388 389
static virConnectPtr
do_open (const char *name, int flags)
390
{
391
    int i, res;
392
    virConnectPtr ret = NULL;
393

394 395 396 397 398 399 400 401
    /* Convert NULL or "" to xen:/// for back compat */
    if (!name || name[0] == '\0')
        name = "xen:///";

    /* Convert xen -> xen:/// for back compat */
    if (!strcasecmp(name, "xen"))
        name = "xen:///";

402
    if (!initialized)
403 404
        if (virInitialize() < 0)
	    return NULL;
405

406
    ret = virGetConnect();
407
    if (ret == NULL) {
408
        virLibConnError(NULL, VIR_ERR_NO_MEMORY, _("allocating connection"));
409 410 411
        goto failed;
    }

412 413 414 415
#ifdef ENABLE_DEBUG
    fprintf (stderr, "libvirt: do_open: proceeding with name=%s\n", name);
#endif

416
    for (i = 0; i < virDriverTabCount; i++) {
417 418 419 420
#ifdef ENABLE_DEBUG
        fprintf (stderr, "libvirt: do_open: trying driver %d (%s) ...\n",
                 i, virDriverTab[i]->name);
#endif
421
        res = virDriverTab[i]->open (ret, name, flags);
422 423 424 425 426 427 428
#ifdef ENABLE_DEBUG
        fprintf (stderr, "libvirt: do_open: driver %d returned %s\n",
                 i,
                 res == VIR_DRV_OPEN_SUCCESS ? "SUCCESS" :
                 (res == VIR_DRV_OPEN_DECLINED ? "DECLINED" :
                  (res == VIR_DRV_OPEN_ERROR ? "ERROR" : "unknown status")));
#endif
429 430 431 432 433
        if (res == VIR_DRV_OPEN_ERROR) goto failed;
        else if (res == VIR_DRV_OPEN_SUCCESS) {
            ret->driver = virDriverTab[i];
            break;
        }
434 435
    }

436
    if (!ret->driver) {
437 438
        /* If we reach here, then all drivers declined the connection. */
        virLibConnError (NULL, VIR_ERR_NO_CONNECT, name);
439
        goto failed;
440 441
    }

442 443
    for (i = 0; i < virNetworkDriverTabCount; i++) {
        res = virNetworkDriverTab[i]->open (ret, name, flags);
444 445 446 447 448
        if (res == VIR_DRV_OPEN_ERROR) {
	    virLibConnWarning (NULL, VIR_WAR_NO_NETWORK, 
	                       "Is the daemon running ?");
            break;
        } else if (res == VIR_DRV_OPEN_SUCCESS) {
449 450 451
            ret->networkDriver = virNetworkDriverTab[i];
            break;
        }
452
    }
453

454 455 456 457
    if (flags & VIR_DRV_OPEN_RO) {
        ret->flags = VIR_CONNECT_RO;
    }

458
    return ret;
459 460

failed:
461
    if (ret->driver) ret->driver->close (ret);
462
	virFreeConnect(ret);
463
    return (NULL);
464 465
}

466 467
/**
 * virConnectOpen:
468
 * @name: URI of the hypervisor
469 470 471 472 473
 *
 * This function should be called first to get a connection to the 
 * Hypervisor and xen store
 *
 * Returns a pointer to the hypervisor connection or NULL in case of error
474 475
 *
 * URIs are documented at http://libvirt.org/uri.html
476 477 478 479
 */
virConnectPtr
virConnectOpen (const char *name)
{
480
    DEBUG("name=%s", name);
481
    return do_open (name, 0);
482 483
}

484
/**
485
 * virConnectOpenReadOnly:
486
 * @name: URI of the hypervisor
487
 *
488 489 490
 * 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.
491 492
 *
 * Returns a pointer to the hypervisor connection or NULL in case of error
493 494
 *
 * URIs are documented at http://libvirt.org/uri.html
495
 */
496
virConnectPtr
497 498
virConnectOpenReadOnly(const char *name)
{
499
    DEBUG("name=%s", name);
500
    return do_open (name, VIR_DRV_OPEN_RO);
D
Daniel Veillard 已提交
501 502 503
}

/**
504
 * virConnectClose:
D
Daniel Veillard 已提交
505 506 507 508 509 510 511 512 513 514
 * @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
515 516
virConnectClose(virConnectPtr conn)
{
517 518
    DEBUG("conn=%p", conn);

K
Karel Zak 已提交
519
    if (!VIR_IS_CONNECT(conn))
520
        return (-1);
521 522 523

    if (conn->networkDriver)
        conn->networkDriver->close (conn);
524
    conn->driver->close (conn);
525

526 527
    if (virFreeConnect(conn) < 0)
        return (-1);
528
    return (0);
D
Daniel Veillard 已提交
529 530
}

531 532 533 534 535 536 537
/**
 * 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.
538 539 540
 *
 * See also:
 * http://www.redhat.com/archives/libvir-list/2007-February/msg00096.html
541 542
 */
const char *
543 544
virConnectGetType(virConnectPtr conn)
{
545
    const char *ret;
546
    DEBUG("conn=%p", conn);
547

D
Daniel Veillard 已提交
548
    if (!VIR_IS_CONNECT(conn)) {
549
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
550
        return (NULL);
D
Daniel Veillard 已提交
551
    }
552 553 554 555

    if (conn->driver->type) {
        ret = conn->driver->type (conn);
        if (ret) return ret;
556
    }
557
    return conn->driver->name;
558 559
}

D
Daniel Veillard 已提交
560
/**
561
 * virConnectGetVersion:
D
Daniel Veillard 已提交
562
 * @conn: pointer to the hypervisor connection
563
 * @hvVer: return value for the version of the running hypervisor (OUT)
D
Daniel Veillard 已提交
564
 *
565 566 567
 * 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 已提交
568
 *
569 570 571
 * 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 已提交
572
 */
573
int
574 575
virConnectGetVersion(virConnectPtr conn, unsigned long *hvVer)
{
576 577
    DEBUG("conn=%p, hvVer=%p", conn, hvVer);

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

D
Daniel Veillard 已提交
583 584
    if (hvVer == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
585
        return (-1);
D
Daniel Veillard 已提交
586
    }
587

588 589
    if (conn->driver->version)
        return conn->driver->version (conn, hvVer);
D
Daniel P. Berrange 已提交
590

591 592
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
593 594
}

595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
/**
 * virConnectGetHostname:
 * @conn: pointer to a hypervisor connection
 *
 * This returns the system hostname on which the hypervisor is
 * running (the result of the gethostname(2) system call).  If
 * we are connected to a remote system, then this returns the
 * hostname of the remote system.
 *
 * Returns the hostname which must be freed by the caller, or
 * NULL if there was an error.
 */
char *
virConnectGetHostname (virConnectPtr conn)
{
610 611
    DEBUG("conn=%p", conn);

612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return NULL;
    }

    if (conn->driver->getHostname)
        return conn->driver->getHostname (conn);

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return NULL;
}

/**
 * virConnectGetURI:
 * @conn: pointer to a hypervisor connection
 *
 * This returns the URI (name) of the hypervisor connection.
 * Normally this is the same as or similar to the string passed
 * to the virConnectOpen/virConnectOpenReadOnly call, but
 * the driver may make the URI canonical.  If name == NULL
 * was passed to virConnectOpen, then the driver will return
 * a non-NULL URI which can be used to connect to the same
 * hypervisor later.
 *
 * Returns the URI string which must be freed by the caller, or
 * NULL if there was an error.
 */
char *
virConnectGetURI (virConnectPtr conn)
{
642 643
    DEBUG("conn=%p", conn);

644 645 646 647 648 649 650 651 652 653 654 655
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return NULL;
    }

    if (conn->driver->getURI)
        return conn->driver->getURI (conn);

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return NULL;
}

656 657 658 659 660
/**
 * virConnectGetMaxVcpus:
 * @conn: pointer to the hypervisor connection
 * @type: value of the 'type' attribute in the <domain> element
 *
661
 * Provides the maximum number of virtual CPUs supported for a guest VM of a
662 663 664 665 666 667 668 669 670
 * 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)
{
671 672
    DEBUG("conn=%p, type=%s", conn, type);

673 674 675 676 677
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

678 679
    if (conn->driver->getMaxVcpus)
        return conn->driver->getMaxVcpus (conn, type);
680

681 682
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
683 684
}

685
/**
686
 * virConnectListDomains:
687 688 689 690 691 692 693 694 695
 * @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
696 697
virConnectListDomains(virConnectPtr conn, int *ids, int maxids)
{
698 699
    DEBUG("conn=%p, ids=%p, maxids=%d", conn, ids, maxids);

D
Daniel Veillard 已提交
700 701
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
702
        return (-1);
D
Daniel Veillard 已提交
703
    }
704

705
    if ((ids == NULL) || (maxids < 0)) {
D
Daniel Veillard 已提交
706
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
707
        return (-1);
D
Daniel Veillard 已提交
708
    }
709

710 711
    if (conn->driver->listDomains)
        return conn->driver->listDomains (conn, ids, maxids);
712

713 714
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
D
Daniel Veillard 已提交
715 716
}

K
 
Karel Zak 已提交
717 718 719 720
/**
 * virConnectNumOfDomains:
 * @conn: pointer to the hypervisor connection
 *
721 722
 * Provides the number of active domains.
 *
K
 
Karel Zak 已提交
723 724 725
 * Returns the number of domain found or -1 in case of error
 */
int
726 727
virConnectNumOfDomains(virConnectPtr conn)
{
728 729
    DEBUG("conn=%p", conn);

D
Daniel Veillard 已提交
730 731
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
732
        return (-1);
D
Daniel Veillard 已提交
733
    }
K
Karel Zak 已提交
734

735 736
    if (conn->driver->numOfDomains)
        return conn->driver->numOfDomains (conn);
737

738 739
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
K
 
Karel Zak 已提交
740 741
}

742 743 744 745
/**
 * virDomainGetConnect:
 * @dom: pointer to a domain
 *
746
 * Provides the connection pointer associated with a domain.  The
747 748 749 750 751 752 753 754
 * reference counter on the connection is not increased by this
 * call.
 *
 * Returns the virConnectPtr or NULL in case of failure.
 */
virConnectPtr
virDomainGetConnect (virDomainPtr dom)
{
755 756
    DEBUG("dom=%p", dom);

757 758 759 760 761 762 763
    if (!VIR_IS_DOMAIN (dom)) {
        virLibDomainError (dom, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return NULL;
    }
    return dom->conn;
}

D
Daniel Veillard 已提交
764
/**
765
 * virDomainCreateLinux:
D
Daniel Veillard 已提交
766
 * @conn: pointer to the hypervisor connection
767
 * @xmlDesc: an XML description of the domain
768
 * @flags: an optional set of virDomainFlags
D
Daniel Veillard 已提交
769
 *
770 771 772
 * 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 已提交
773 774 775
 * 
 * Returns a new domain object or NULL in case of failure
 */
776
virDomainPtr
777 778
virDomainCreateLinux(virConnectPtr conn, const char *xmlDesc,
                     unsigned int flags)
779
{
780 781
    DEBUG("conn=%p, xmlDesc=%s, flags=%d", conn, xmlDesc, flags);

D
Daniel Veillard 已提交
782 783
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
784
        return (NULL);
D
Daniel Veillard 已提交
785 786 787
    }
    if (xmlDesc == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
788
        return (NULL);
D
Daniel Veillard 已提交
789
    }
790 791 792 793
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (NULL);
    }
D
Daniel Veillard 已提交
794

795 796 797 798 799
    if (conn->driver->domainCreateLinux)
        return conn->driver->domainCreateLinux (conn, xmlDesc, flags);

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return NULL;
D
Daniel Veillard 已提交
800 801
}

802

803
/**
804
 * virDomainLookupByID:
805 806 807 808 809
 * @conn: pointer to the hypervisor connection
 * @id: the domain ID number
 *
 * Try to find a domain based on the hypervisor ID number
 *
810 811
 * Returns a new domain object or NULL in case of failure.  If the
 * domain cannot be found, then VIR_ERR_NO_DOMAIN error is raised.
812
 */
813
virDomainPtr
814 815
virDomainLookupByID(virConnectPtr conn, int id)
{
816 817
    DEBUG("conn=%p, id=%d", conn, id);

D
Daniel Veillard 已提交
818 819
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
820
        return (NULL);
D
Daniel Veillard 已提交
821 822 823
    }
    if (id < 0) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
824
        return (NULL);
D
Daniel Veillard 已提交
825
    }
826

827 828
    if (conn->driver->domainLookupByID)
        return conn->driver->domainLookupByID (conn, id);
829

830 831
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return NULL;
832 833 834 835 836
}

/**
 * virDomainLookupByUUID:
 * @conn: pointer to the hypervisor connection
K
Karel Zak 已提交
837
 * @uuid: the raw UUID for the domain
838 839 840
 *
 * Try to lookup a domain on the given hypervisor based on its UUID.
 *
841 842
 * Returns a new domain object or NULL in case of failure.  If the
 * domain cannot be found, then VIR_ERR_NO_DOMAIN error is raised.
843 844
 */
virDomainPtr
845 846
virDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
{
847 848
    DEBUG("conn=%p, uuid=%s", conn, uuid);

D
Daniel Veillard 已提交
849 850
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
851
        return (NULL);
D
Daniel Veillard 已提交
852 853 854
    }
    if (uuid == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
855
        return (NULL);
D
Daniel Veillard 已提交
856
    }
857

858 859
    if (conn->driver->domainLookupByUUID)
        return conn->driver->domainLookupByUUID (conn, uuid);
860

861 862
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return NULL;
863 864
}

K
Karel Zak 已提交
865 866 867 868 869 870 871
/**
 * 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.
 *
872 873
 * Returns a new domain object or NULL in case of failure.  If the
 * domain cannot be found, then VIR_ERR_NO_DOMAIN error is raised.
K
Karel Zak 已提交
874 875 876 877
 */
virDomainPtr
virDomainLookupByUUIDString(virConnectPtr conn, const char *uuidstr)
{
878 879
    int raw[VIR_UUID_BUFLEN], i;
    unsigned char uuid[VIR_UUID_BUFLEN];
K
Karel Zak 已提交
880 881
    int ret;

882 883
    DEBUG("conn=%p, uuidstr=%s", conn, uuidstr);

K
Karel Zak 已提交
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
    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);
    
907
    if (ret!=VIR_UUID_BUFLEN) {
K
Karel Zak 已提交
908 909 910
	virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return (NULL);
    }
911
    for (i = 0; i < VIR_UUID_BUFLEN; i++)
K
Karel Zak 已提交
912 913 914 915 916
        uuid[i] = raw[i] & 0xFF;
    
    return virDomainLookupByUUID(conn, &uuid[0]);
}

917 918 919 920 921
/**
 * virDomainLookupByName:
 * @conn: pointer to the hypervisor connection
 * @name: name for the domain
 *
922
 * Try to lookup a domain on the given hypervisor based on its name.
923
 *
924 925
 * Returns a new domain object or NULL in case of failure.  If the
 * domain cannot be found, then VIR_ERR_NO_DOMAIN error is raised.
926 927
 */
virDomainPtr
928 929
virDomainLookupByName(virConnectPtr conn, const char *name)
{
930 931
    DEBUG("conn=%p, name=%s", conn, name);

D
Daniel Veillard 已提交
932 933
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
934
        return (NULL);
D
Daniel Veillard 已提交
935 936 937
    }
    if (name == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
938
        return (NULL);
D
Daniel Veillard 已提交
939
    }
940

941 942 943 944 945
    if (conn->driver->domainLookupByName)
        return conn->driver->domainLookupByName (conn, name);

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return NULL;
946 947
}

D
Daniel Veillard 已提交
948
/**
949
 * virDomainDestroy:
D
Daniel Veillard 已提交
950 951 952 953
 * @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.
954 955
 * The data structure is freed and should not be used thereafter if the
 * call does not return an error.
956
 * This function may requires priviledged access
D
Daniel Veillard 已提交
957 958
 *
 * Returns 0 in case of success and -1 in case of failure.
959
 */
D
Daniel Veillard 已提交
960
int
961 962
virDomainDestroy(virDomainPtr domain)
{
963
    virConnectPtr conn;
964

965 966
    DEBUG("domain=%p", domain);

D
Daniel Veillard 已提交
967 968
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
969
        return (-1);
D
Daniel Veillard 已提交
970
    }
971

972
    conn = domain->conn;
973 974 975 976
    if (conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
977

978 979
    if (conn->driver->domainDestroy)
        return conn->driver->domainDestroy (domain);
980

981 982
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
983 984 985 986 987 988 989 990 991 992 993 994
}

/**
 * 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
995 996
virDomainFree(virDomainPtr domain)
{
997 998
    DEBUG("domain=%p", domain);

D
Daniel Veillard 已提交
999 1000
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1001
        return (-1);
D
Daniel Veillard 已提交
1002
    }
1003 1004 1005
    if (virFreeDomain(domain->conn, domain) < 0)
        return (-1);
    return(0);
D
Daniel Veillard 已提交
1006 1007 1008
}

/**
1009
 * virDomainSuspend:
D
Daniel Veillard 已提交
1010 1011 1012 1013
 * @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 
1014
 * hypervisor level will stay allocated. Use virDomainResume() to reactivate
D
Daniel Veillard 已提交
1015
 * the domain.
1016
 * This function may requires priviledged access.
D
Daniel Veillard 已提交
1017 1018 1019 1020
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1021 1022
virDomainSuspend(virDomainPtr domain)
{
1023
    virConnectPtr conn;
1024
    DEBUG("domain=%p", domain);
1025

D
Daniel Veillard 已提交
1026 1027
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1028
        return (-1);
D
Daniel Veillard 已提交
1029
    }
1030 1031 1032 1033
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
D
Daniel Veillard 已提交
1034

1035 1036
    conn = domain->conn;

1037 1038
    if (conn->driver->domainSuspend)
        return conn->driver->domainSuspend (domain);
1039

1040 1041
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
D
Daniel Veillard 已提交
1042 1043 1044
}

/**
1045
 * virDomainResume:
D
Daniel Veillard 已提交
1046 1047 1048
 * @domain: a domain object
 *
 * Resume an suspended domain, the process is restarted from the state where
1049
 * it was frozen by calling virSuspendDomain().
1050
 * This function may requires priviledged access
D
Daniel Veillard 已提交
1051 1052 1053 1054
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1055 1056
virDomainResume(virDomainPtr domain)
{
1057
    virConnectPtr conn;
1058
    DEBUG("domain=%p", domain);
1059

D
Daniel Veillard 已提交
1060 1061
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1062
        return (-1);
D
Daniel Veillard 已提交
1063
    }
1064 1065 1066 1067
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
D
Daniel Veillard 已提交
1068

1069 1070
    conn = domain->conn;

1071 1072
    if (conn->driver->domainResume)
        return conn->driver->domainResume (domain);
1073

1074 1075
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
D
Daniel Veillard 已提交
1076 1077
}

1078 1079 1080 1081 1082 1083
/**
 * virDomainSave:
 * @domain: a domain object
 * @to: path for the output file
 *
 * This method will suspend a domain and save its memory contents to
1084 1085 1086
 * 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.
1087 1088 1089 1090
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1091 1092
virDomainSave(virDomainPtr domain, const char *to)
{
1093
    char filepath[4096];
1094
    virConnectPtr conn;
1095
    DEBUG("domain=%p, to=%s", domain, to);
1096

D
Daniel Veillard 已提交
1097 1098
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1099
        return (-1);
D
Daniel Veillard 已提交
1100
    }
1101 1102 1103 1104
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
1105
    conn = domain->conn;
D
Daniel Veillard 已提交
1106 1107
    if (to == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1108
        return (-1);
D
Daniel Veillard 已提交
1109
    }
1110

1111 1112 1113 1114 1115
    /*
     * 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] != '/') {
1116
        unsigned int len, t;
1117

1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
        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];
1128 1129 1130

    }

1131 1132 1133
    if (conn->driver->domainSave)
        return conn->driver->domainSave (domain, to);

1134
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1135
    return -1;
1136 1137 1138 1139
}

/**
 * virDomainRestore:
1140
 * @conn: pointer to the hypervisor connection
1141 1142 1143 1144 1145 1146 1147
 * @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
1148 1149
virDomainRestore(virConnectPtr conn, const char *from)
{
1150
    char filepath[4096];
1151
    DEBUG("conn=%p, from=%s", conn, from);
1152

D
Daniel Veillard 已提交
1153 1154
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
1155
        return (-1);
D
Daniel Veillard 已提交
1156
    }
1157 1158 1159 1160
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
D
Daniel Veillard 已提交
1161 1162
    if (from == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1163
        return (-1);
D
Daniel Veillard 已提交
1164 1165
    }

1166 1167 1168 1169 1170
    /*
     * 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] != '/') {
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
        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];
    }

1185 1186 1187
    if (conn->driver->domainRestore)
        return conn->driver->domainRestore (conn, from);

1188
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1189
    return -1;
D
Daniel Veillard 已提交
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
}

/**
 * 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)
{
    char filepath[4096];
    virConnectPtr conn;
1209
    DEBUG("domain=%p, to=%s, flags=%d", domain, to, flags);
D
Daniel Veillard 已提交
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244

    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];

    }

1245 1246 1247
    if (conn->driver->domainCoreDump)
        return conn->driver->domainCoreDump (domain, to, flags);

1248
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1249
    return -1;
1250 1251
}

1252 1253 1254 1255 1256
/**
 * virDomainShutdown:
 * @domain: a domain object
 *
 * Shutdown a domain, the domain object is still usable there after but
1257 1258
 * the domain OS is being stopped. Note that the guest OS may ignore the
 * request.
1259 1260 1261 1262 1263 1264 1265
 *
 * 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
1266 1267
virDomainShutdown(virDomainPtr domain)
{
1268
    virConnectPtr conn;
1269
    DEBUG("domain=%p", domain);
1270

D
Daniel Veillard 已提交
1271 1272
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1273
        return (-1);
D
Daniel Veillard 已提交
1274
    }
1275 1276 1277 1278
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
1279

1280 1281
    conn = domain->conn;

1282 1283
    if (conn->driver->domainShutdown)
        return conn->driver->domainShutdown (domain);
1284

1285
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1286
    return -1;
1287 1288
}

1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
/**
 * 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)
{
1303
    virConnectPtr conn;
1304
    DEBUG("domain=%p, flags=%u", domain, flags);
1305 1306 1307 1308 1309

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
1310 1311 1312 1313
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
1314

1315 1316
    conn = domain->conn;

1317 1318
    if (conn->driver->domainReboot)
        return conn->driver->domainReboot (domain, flags);
1319

1320
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1321
    return -1;
1322 1323
}

1324
/**
1325
 * virDomainGetName:
1326 1327 1328 1329 1330 1331 1332 1333
 * @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 *
1334 1335
virDomainGetName(virDomainPtr domain)
{
1336 1337
    DEBUG("domain=%p", domain);

D
Daniel Veillard 已提交
1338 1339
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1340
        return (NULL);
D
Daniel Veillard 已提交
1341
    }
1342
    return (domain->name);
1343 1344
}

1345 1346 1347
/**
 * virDomainGetUUID:
 * @domain: a domain object
1348
 * @uuid: pointer to a VIR_UUID_BUFLEN bytes array
1349 1350 1351 1352 1353 1354
 *
 * Get the UUID for a domain
 *
 * Returns -1 in case of error, 0 in case of success
 */
int
1355 1356
virDomainGetUUID(virDomainPtr domain, unsigned char *uuid)
{
1357 1358
    DEBUG("domain=%p, uuid=%p", domain, uuid);

D
Daniel Veillard 已提交
1359 1360
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1361
        return (-1);
D
Daniel Veillard 已提交
1362 1363 1364
    }
    if (uuid == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1365
        return (-1);
D
Daniel Veillard 已提交
1366 1367
    }

1368
    if (domain->id == 0) {
1369
        memset(uuid, 0, VIR_UUID_BUFLEN);
D
Daniel Veillard 已提交
1370
    } else {
1371 1372 1373 1374 1375
#if 0
        /* Probably legacy code: It appears that we always fill in
         * the UUID when creating the virDomain structure, so this
         * shouldn't be necessary.
         */
1376 1377 1378 1379 1380 1381 1382 1383
        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))
1384
            xenDaemonDomainLookupByName_ids(domain->conn, domain->name,
1385
                                &domain->uuid[0]);
1386
#endif
1387
        memcpy(uuid, &domain->uuid[0], VIR_UUID_BUFLEN);
1388 1389
    }
    return (0);
1390 1391
}

K
Karel Zak 已提交
1392 1393 1394
/**
 * virDomainGetUUIDString:
 * @domain: a domain object
1395
 * @buf: pointer to a VIR_UUID_STRING_BUFLEN bytes array
K
Karel Zak 已提交
1396 1397 1398 1399 1400 1401 1402 1403 1404
 *
 * 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)
{
1405
    unsigned char uuid[VIR_UUID_BUFLEN];
1406
    DEBUG("domain=%p, buf=%p", domain, buf);
1407

K
Karel Zak 已提交
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
    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]))
1418
        return (-1);
K
Karel Zak 已提交
1419

1420
    snprintf(buf, VIR_UUID_STRING_BUFLEN,
K
Karel Zak 已提交
1421 1422 1423 1424 1425 1426 1427 1428
	"%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);
}

1429
/**
1430
 * virDomainGetID:
1431 1432 1433 1434 1435 1436 1437
 * @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
1438 1439
virDomainGetID(virDomainPtr domain)
{
1440 1441
    DEBUG("domain=%p", domain);

D
Daniel Veillard 已提交
1442 1443
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1444
        return ((unsigned int) -1);
D
Daniel Veillard 已提交
1445
    }
1446
    return (domain->id);
1447 1448
}

1449 1450 1451 1452 1453 1454
/**
 * virDomainGetOSType:
 * @domain: a domain object
 *
 * Get the type of domain operation system.
 *
1455 1456
 * Returns the new string or NULL in case of error, the string must be
 *         freed by the caller.
1457 1458
 */
char *
1459 1460
virDomainGetOSType(virDomainPtr domain)
{
1461
    virConnectPtr conn;
1462
    DEBUG("domain=%p", domain);
1463

D
Daniel Veillard 已提交
1464 1465
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1466
        return (NULL);
D
Daniel Veillard 已提交
1467
    }
1468

1469 1470 1471 1472
    conn = domain->conn;

    if (conn->driver->domainGetOSType)
        return conn->driver->domainGetOSType (domain);
1473

1474
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1475
    return NULL;
1476 1477
}

1478
/**
1479
 * virDomainGetMaxMemory:
1480 1481 1482 1483 1484 1485 1486 1487 1488
 * @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
1489 1490
virDomainGetMaxMemory(virDomainPtr domain)
{
1491
    virConnectPtr conn;
1492
    DEBUG("domain=%p", domain);
1493

D
Daniel Veillard 已提交
1494 1495
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1496
        return (0);
D
Daniel Veillard 已提交
1497
    }
1498

1499 1500
    conn = domain->conn;

1501 1502 1503
    if (conn->driver->domainGetMaxMemory)
        return conn->driver->domainGetMaxMemory (domain);

1504
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1505
    return 0;
1506 1507
}

D
Daniel Veillard 已提交
1508
/**
1509
 * virDomainSetMaxMemory:
D
Daniel Veillard 已提交
1510 1511 1512 1513 1514 1515
 * @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.
1516
 * This function requires priviledged access to the hypervisor.
D
Daniel Veillard 已提交
1517 1518 1519 1520
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1521 1522
virDomainSetMaxMemory(virDomainPtr domain, unsigned long memory)
{
1523
    virConnectPtr conn;
1524
    DEBUG("domain=%p, memory=%lu", domain, memory);
1525

1526 1527 1528 1529 1530 1531
    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1532
        return (-1);
1533
    }
1534 1535 1536 1537 1538 1539 1540 1541
    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);
    }
1542
    conn = domain->conn;
1543

1544 1545 1546
    if (conn->driver->domainSetMaxMemory)
        return conn->driver->domainSetMaxMemory (domain, memory);

1547
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1548
    return -1;
1549
}
1550

1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
/**
 * 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)
{
    virConnectPtr conn;
1567
    DEBUG("domain=%p, memory=%lu", domain, memory);
1568

1569 1570 1571 1572 1573 1574 1575
    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
1576
    }
1577 1578 1579 1580 1581 1582
    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__);
1583
        return (-1);
1584 1585 1586
    }

    conn = domain->conn;
1587

1588 1589 1590
    if (conn->driver->domainSetMemory)
        return conn->driver->domainSetMemory (domain, memory);

1591
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1592
    return -1;
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
    virConnectPtr conn;
1610
    DEBUG("domain=%p, info=%p", domain, info);
1611

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

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

1623 1624 1625 1626
    conn = domain->conn;

    if (conn->driver->domainGetInfo)
        return conn->driver->domainGetInfo (domain, info);
1627

1628
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1629
    return -1;
1630
}
1631

1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
/**
 * 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 *
1644 1645
virDomainGetXMLDesc(virDomainPtr domain, int flags)
{
1646
    virConnectPtr conn;
1647
    DEBUG("domain=%p, flags=%d", domain, flags);
1648

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
    conn = domain->conn;

    if (conn->driver->domainDumpXML)
        return conn->driver->domainDumpXML (domain, flags);

1663
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1664
    return NULL;
1665
}
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676

/**
 * 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
1677 1678
virNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info)
{
1679 1680
    DEBUG("conn=%p, info=%p", conn, info);

1681 1682 1683 1684 1685 1686 1687 1688 1689
    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);
    }

1690 1691 1692
    if (conn->driver->nodeGetInfo)
        return conn->driver->nodeGetInfo (conn, info);

1693
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1694
    return -1;
1695
}
1696

1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
/**
 * virConnectGetCapabilities:
 * @conn: pointer to the hypervisor connection
 *
 * Provides capabilities of the hypervisor / driver.
 *
 * Returns NULL in case of error, or a pointer to an opaque
 * virCapabilities structure (virCapabilitiesPtr).
 * The client must free the returned string after use.
 */
char *
virConnectGetCapabilities (virConnectPtr conn)
{
1710 1711
    DEBUG("conn=%p", conn);

1712 1713 1714 1715 1716
    if (!VIR_IS_CONNECT (conn)) {
        virLibConnError (conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return NULL;
    }

1717 1718
    if (conn->driver->getCapabilities)
        return conn->driver->getCapabilities (conn);
1719

1720
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1721 1722 1723
    return NULL;
}

1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
/**
 * virDomainGetSchedulerType:
 * @domain: pointer to domain object
 * @nparams: number of scheduler parameters(return value)
 *
 * Get the scheduler type.
 *
 * Returns NULL in case of error. The caller must free the returned string.
 */
char *
virDomainGetSchedulerType(virDomainPtr domain, int *nparams)
{
    virConnectPtr conn;
    char *schedtype;
1738
    DEBUG("domain=%p, nparams=%p", domain, nparams);
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return NULL;
    }
    conn = domain->conn;

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

    if (conn->driver->domainGetSchedulerType){
        schedtype = conn->driver->domainGetSchedulerType (domain, nparams);
        return schedtype;
    }

1756
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779
    return NULL;
}


/**
 * virDomainGetSchedulerParameters:
 * @domain: pointer to domain object
 * @params: pointer to scheduler parameter object
 *          (return value)
 * @nparams: pointer to number of scheduler parameter
 *          (this value should be same than the returned value
 *           nparams of virDomainGetSchedulerType)
 *
 * Get the scheduler parameters, the @params array will be filled with the
 * values.
 *
 * Returns -1 in case of error, 0 in case of success.
 */
int
virDomainGetSchedulerParameters(virDomainPtr domain,
				virSchedParameterPtr params, int *nparams)
{
    virConnectPtr conn;
1780
    DEBUG("domain=%p, params=%p, nparams=%p", domain, params, nparams);
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795

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

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

    if (conn->driver->domainGetSchedulerParameters)
        return conn->driver->domainGetSchedulerParameters (domain, params, nparams);

1796
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
    return -1;
}

/**
 * virDomainSetSchedulerParameters:
 * @domain: pointer to domain object
 * @params: pointer to scheduler parameter objects
 * @nparams: number of scheduler parameter
 *          (this value should be same or less than the returned value
 *           nparams of virDomainGetSchedulerType)
 *
 * Change the scheduler parameters
 *
 * Returns -1 in case of error, 0 in case of success.
 */
int
virDomainSetSchedulerParameters(virDomainPtr domain, 
				virSchedParameterPtr params, int nparams)
{
    virConnectPtr conn;
1817
    DEBUG("domain=%p, params=%p, nparams=%d", domain, params, nparams);
1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832

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

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

    if (conn->driver->domainSetSchedulerParameters)
        return conn->driver->domainSetSchedulerParameters (domain, params, nparams);

1833
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1834 1835 1836 1837 1838
    return -1;
}



1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
/************************************************************************
 *									*
 *		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) {
1856 1857
    DEBUG("conn=%p, xml=%s", conn, xml);

1858 1859 1860 1861
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
1862 1863 1864 1865
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (NULL);
    }
1866 1867 1868 1869 1870
    if (xml == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
    }

1871 1872
    if (conn->driver->domainDefineXML)
        return conn->driver->domainDefineXML (conn, xml);
1873

1874
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1875
    return NULL;
1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887
}

/**
 * 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) {
1888
    virConnectPtr conn;
1889
    DEBUG("domain=%p", domain);
1890 1891 1892 1893 1894

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
1895 1896
    conn = domain->conn;
    if (conn->flags & VIR_CONNECT_RO) {
1897 1898 1899 1900
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }

1901 1902
    if (conn->driver->domainUndefine)
        return conn->driver->domainUndefine (domain);
1903

1904
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1905
    return -1;
1906 1907 1908 1909 1910 1911
}

/**
 * virConnectNumOfDefinedDomains:
 * @conn: pointer to the hypervisor connection
 *
1912
 * Provides the number of inactive domains.
1913 1914 1915 1916 1917 1918
 *
 * Returns the number of domain found or -1 in case of error
 */
int
virConnectNumOfDefinedDomains(virConnectPtr conn)
{
1919 1920
    DEBUG("conn=%p", conn);

1921 1922 1923 1924 1925
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

1926 1927
    if (conn->driver->numOfDefinedDomains)
        return conn->driver->numOfDefinedDomains (conn);
1928

1929
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1930
    return -1;
1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
}

/**
 * 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
1944
virConnectListDefinedDomains(virConnectPtr conn, char **const names,
1945
                             int maxnames) {
1946 1947
    DEBUG("conn=%p, names=%p, maxnames=%d", conn, names, maxnames);

1948 1949 1950 1951 1952
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

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

1958 1959
    if (conn->driver->listDefinedDomains)
        return conn->driver->listDefinedDomains (conn, names, maxnames);
1960

1961
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1962
    return -1;
1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975
}

/**
 * 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) {
1976
    virConnectPtr conn;
1977
    DEBUG("domain=%p", domain);
1978

1979 1980 1981 1982
    if (domain == NULL) {
        TODO
	return (-1);
    }
1983 1984 1985 1986
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
1987 1988
    conn = domain->conn;
    if (conn->flags & VIR_CONNECT_RO) {
1989 1990 1991
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
1992

1993 1994 1995
    if (conn->driver->domainCreate)
        return conn->driver->domainCreate (domain);

1996
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1997
    return -1;
1998 1999
}

2000 2001 2002
/**
 * virDomainGetAutostart:
 * @domain: a domain object
2003
 * @autostart: the value returned
2004
 *
2005
 * Provides a boolean value indicating whether the domain
2006 2007 2008 2009 2010 2011 2012
 * 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,
2013 2014 2015
                      int *autostart)
{
    virConnectPtr conn;
2016
    DEBUG("domain=%p, autostart=%p", domain, autostart);
2017 2018 2019 2020 2021 2022 2023 2024 2025 2026

    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);
    }

2027 2028 2029 2030 2031
    conn = domain->conn;

    if (conn->driver->domainGetAutostart)
        return conn->driver->domainGetAutostart (domain, autostart);

2032
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2033
    return -1;
2034 2035 2036 2037 2038
}

/**
 * virDomainSetAutostart:
 * @domain: a domain object
2039
 * @autostart: whether the domain should be automatically started 0 or 1
2040 2041 2042 2043 2044 2045 2046 2047
 *
 * 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,
2048 2049 2050
                      int autostart)
{
    virConnectPtr conn;
2051
    DEBUG("domain=%p, autostart=%d", domain, autostart);
2052 2053 2054 2055 2056 2057

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

2058 2059 2060 2061 2062
    conn = domain->conn;

    if (conn->driver->domainSetAutostart)
        return conn->driver->domainSetAutostart (domain, autostart);

2063
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2064
    return -1;
2065 2066
}

2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082
/**
 * 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)
{
2083
    virConnectPtr conn;
2084
    DEBUG("domain=%p, nvcpus=%u", domain, nvcpus);
2085

2086 2087 2088 2089 2090 2091 2092 2093
    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
2094 2095 2096 2097
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
2098

2099 2100 2101 2102
    if (nvcpus < 1) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
2103
    conn = domain->conn;
2104

2105 2106
    if (conn->driver->domainSetVcpus)
        return conn->driver->domainSetVcpus (domain, nvcpus);
2107

2108
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2109
    return -1;
2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133
}

/**
 * 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)
{
2134
    virConnectPtr conn;
2135
    DEBUG("domain=%p, vcpu=%u, cpumap=%p, maplen=%d", domain, vcpu, cpumap, maplen);
2136

2137 2138 2139 2140 2141 2142 2143 2144
    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
2145 2146 2147 2148
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
2149

2150
    if ((vcpu > 32000) || (cpumap == NULL) || (maplen < 1)) {
2151 2152 2153
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
2154

2155 2156
    conn = domain->conn;

2157 2158 2159
    if (conn->driver->domainPinVcpu)
        return conn->driver->domainPinVcpu (domain, vcpu, cpumap, maplen);

2160
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2161
    return -1;
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 2188
}

/**
 * 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)
{
2189
    virConnectPtr conn;
2190
    DEBUG("domain=%p, info=%p, maxinfo=%d, cpumaps=%p, maplen=%d", domain, info, maxinfo, cpumaps, maplen);
2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207

    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);
    }
2208

2209 2210
    conn = domain->conn;

2211 2212 2213 2214
    if (conn->driver->domainGetVcpus)
        return conn->driver->domainGetVcpus (domain, info, maxinfo,
                                             cpumaps, maplen);

2215
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2216
    return -1;
2217
}
2218

2219 2220 2221 2222
/**
 * virDomainGetMaxVcpus:
 * @domain: pointer to domain object
 * 
2223 2224 2225 2226 2227
 * Provides 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.
2228 2229 2230 2231
 *
 * Returns the maximum of virtual CPU or -1 in case of error.
 */
int
2232 2233
virDomainGetMaxVcpus(virDomainPtr domain)
{
2234
    virConnectPtr conn;
2235
    DEBUG("domain=%p", domain);
2236 2237 2238 2239 2240 2241 2242 2243

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

    conn = domain->conn;

2244 2245 2246
    if (conn->driver->domainGetMaxVcpus)
        return conn->driver->domainGetMaxVcpus (domain);

2247
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2248
    return -1;
2249 2250 2251
}


2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264
/**
 * 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)
{
    virConnectPtr conn;
2265
    DEBUG("domain=%p, xml=%s", domain, xml);
2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276

    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;

2277 2278 2279
    if (conn->driver->domainAttachDevice)
        return conn->driver->domainAttachDevice (domain, xml);

2280
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2281
    return -1;
2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296
}

/**
 * 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)
{
    virConnectPtr conn;
2297
    DEBUG("domain=%p, xml=%s", domain, xml);
2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308

    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;

2309 2310 2311
    if (conn->driver->domainDetachDevice)
        return conn->driver->domainDetachDevice (domain, xml);

2312
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2313
    return -1;
2314
}
2315

2316 2317 2318 2319
/**
 * virNetworkGetConnect:
 * @net: pointer to a network
 *
2320
 * Provides the connection pointer associated with a network.  The
2321 2322 2323 2324 2325 2326 2327 2328
 * reference counter on the connection is not increased by this
 * call.
 *
 * Returns the virConnectPtr or NULL in case of failure.
 */
virConnectPtr
virNetworkGetConnect (virNetworkPtr net)
{
2329 2330
    DEBUG("net=%p", net);

2331 2332 2333 2334 2335 2336 2337
    if (!VIR_IS_NETWORK (net)) {
        virLibNetworkError (net, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return NULL;
    }
    return net->conn;
}

2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348
/**
 * 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)
{
2349 2350
    DEBUG("conn=%p", conn);

2351 2352 2353 2354 2355
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

2356 2357
    if (conn->networkDriver && conn->networkDriver->numOfNetworks)
        return conn->networkDriver->numOfNetworks (conn);
2358

2359
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2360
    return -1;
2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373
}

/**
 * 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
2374
virConnectListNetworks(virConnectPtr conn, char **const names, int maxnames)
2375
{
2376 2377
    DEBUG("conn=%p, names=%p, maxnames=%d", conn, names, maxnames);

2378 2379 2380 2381 2382
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

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

2388 2389
    if (conn->networkDriver && conn->networkDriver->listNetworks)
        return conn->networkDriver->listNetworks (conn, names, maxnames);
2390

2391
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2392
    return -1;
2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405
}

/**
 * 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)
{
2406 2407
    DEBUG("conn=%p", conn);

2408 2409 2410 2411 2412
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

2413 2414
    if (conn->networkDriver && conn->networkDriver->numOfDefinedNetworks)
        return conn->networkDriver->numOfDefinedNetworks (conn);
2415

2416
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2417
    return -1;
2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430
}

/**
 * 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
2431
virConnectListDefinedNetworks(virConnectPtr conn, char **const names,
2432 2433
                              int maxnames)
{
2434 2435
    DEBUG("conn=%p, names=%p, maxnames=%d", conn, names, maxnames);

2436 2437 2438 2439 2440
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

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

2446 2447 2448
    if (conn->networkDriver && conn->networkDriver->listDefinedNetworks)
        return conn->networkDriver->listDefinedNetworks (conn,
                                                         names, maxnames);
2449

2450
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2451
    return -1;
2452 2453 2454 2455 2456 2457 2458 2459 2460
}

/**
 * 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.
 *
2461 2462
 * Returns a new network object or NULL in case of failure.  If the
 * network cannot be found, then VIR_ERR_NO_NETWORK error is raised.
2463 2464 2465 2466
 */
virNetworkPtr
virNetworkLookupByName(virConnectPtr conn, const char *name)
{
2467 2468
    DEBUG("conn=%p, name=%s", conn, name);

2469 2470 2471 2472 2473 2474 2475 2476 2477
    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);
    }

2478 2479 2480
    if (conn->networkDriver && conn->networkDriver->networkLookupByName)
        return conn->networkDriver->networkLookupByName (conn, name);

2481
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2482
    return NULL;
2483 2484 2485 2486 2487 2488 2489 2490 2491
}

/**
 * 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.
 *
2492 2493
 * Returns a new network object or NULL in case of failure.  If the
 * network cannot be found, then VIR_ERR_NO_NETWORK error is raised.
2494 2495 2496 2497
 */
virNetworkPtr
virNetworkLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
{
2498 2499
    DEBUG("conn=%p, uuid=%s", conn, uuid);

2500 2501 2502 2503 2504 2505 2506 2507 2508
    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);
    }

2509 2510
    if (conn->networkDriver && conn->networkDriver->networkLookupByUUID)
        return conn->networkDriver->networkLookupByUUID (conn, uuid);
2511

2512
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2513
    return NULL;
2514 2515 2516 2517 2518 2519 2520 2521 2522
}

/**
 * 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.
 *
2523 2524
 * Returns a new network object or NULL in case of failure.  If the
 * network cannot be found, then VIR_ERR_NO_NETWORK error is raised.
2525 2526 2527 2528 2529 2530 2531
 */
virNetworkPtr
virNetworkLookupByUUIDString(virConnectPtr conn, const char *uuidstr)
{
    int raw[VIR_UUID_BUFLEN], i;
    unsigned char uuid[VIR_UUID_BUFLEN];
    int ret;
2532
    DEBUG("conn=%p, uuidstr=%s", conn, uuidstr);
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

    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)
{
2580 2581
    DEBUG("conn=%p, xmlDesc=%s", conn, xmlDesc);

2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594
    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);
    }

2595 2596 2597
    if (conn->networkDriver && conn->networkDriver->networkCreateXML)
        return conn->networkDriver->networkCreateXML (conn, xmlDesc);

2598
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2599
    return NULL;
2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611
}

/**
 * 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
2612 2613
virNetworkDefineXML(virConnectPtr conn, const char *xml)
{
2614 2615
    DEBUG("conn=%p, xml=%s", conn, xml);

2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628
    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);
    }

2629 2630
    if (conn->networkDriver && conn->networkDriver->networkDefineXML)
        return conn->networkDriver->networkDefineXML (conn, xml);
2631

2632
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2633
    return NULL;
2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646
}

/**
 * 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) {
    virConnectPtr conn;
2647
    DEBUG("network=%p", network);
2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658

    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);
    }

2659 2660
    if (conn->networkDriver && conn->networkDriver->networkUndefine)
        return conn->networkDriver->networkUndefine (network);
2661

2662
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2663
    return -1;
2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675
}

/**
 * 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
2676 2677
virNetworkCreate(virNetworkPtr network)
{
2678
    virConnectPtr conn;
2679 2680
    DEBUG("network=%p", network);

2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694
    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);
    }

2695 2696 2697
    if (conn->networkDriver && conn->networkDriver->networkCreate)
        return conn->networkDriver->networkCreate (network);

2698
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2699
    return -1;
2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717
}

/**
 * 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)
{
    virConnectPtr conn;
2718
    DEBUG("network=%p", network);
2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730

    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);
    }

2731 2732
    if (conn->networkDriver && conn->networkDriver->networkDestroy)
        return conn->networkDriver->networkDestroy (network);
2733

2734
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2735
    return -1;
2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749
}

/**
 * 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)
{
2750 2751
    DEBUG("network=%p", network);

2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772
    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)
{
2773 2774
    DEBUG("network=%p", network);

2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793
    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)
{
2794 2795
    DEBUG("network=%p, uuid=%p", network, uuid);

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
    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];
2824
    DEBUG("network=%p, buf=%p", network, buf);
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

    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)
{
2861
    virConnectPtr conn;
2862
    DEBUG("network=%p, flags=%d", network, flags);
2863

2864 2865 2866 2867 2868 2869 2870 2871 2872
    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);
    }

2873 2874 2875 2876 2877
    conn = network->conn;

    if (conn->networkDriver && conn->networkDriver->networkDumpXML)
        return conn->networkDriver->networkDumpXML (network, flags);

2878
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2879
    return NULL;
2880
}
2881 2882 2883 2884 2885

/**
 * virNetworkGetBridgeName:
 * @network: a network object
 *
2886
 * Provides a bridge interface name to which a domain may connect
2887 2888 2889 2890 2891 2892 2893 2894
 * 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)
{
2895
    virConnectPtr conn;
2896
    DEBUG("network=%p", network);
2897

2898 2899 2900 2901 2902
    if (!VIR_IS_NETWORK(network)) {
        virLibNetworkError(network, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return (NULL);
    }

2903 2904 2905 2906 2907
    conn = network->conn;

    if (conn->networkDriver && conn->networkDriver->networkGetBridgeName)
        return conn->networkDriver->networkGetBridgeName (network);

2908
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2909
    return NULL;
2910
}
2911 2912 2913 2914

/**
 * virNetworkGetAutostart:
 * @network: a network object
2915
 * @autostart: the value returned
2916
 *
2917
 * Provides a boolean value indicating whether the network
2918 2919 2920 2921 2922 2923 2924
 * 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,
2925 2926 2927
                       int *autostart)
{
    virConnectPtr conn;
2928
    DEBUG("network=%p, autostart=%p", network, autostart);
2929 2930 2931 2932 2933 2934 2935 2936 2937 2938

    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);
    }

2939 2940 2941 2942 2943
    conn = network->conn;

    if (conn->networkDriver && conn->networkDriver->networkGetAutostart)
        return conn->networkDriver->networkGetAutostart (network, autostart);

2944
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2945
    return -1;
2946 2947 2948 2949 2950
}

/**
 * virNetworkSetAutostart:
 * @network: a network object
2951
 * @autostart: whether the network should be automatically started 0 or 1
2952 2953 2954 2955 2956 2957 2958 2959
 *
 * 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,
2960 2961 2962
                       int autostart)
{
    virConnectPtr conn;
2963
    DEBUG("network=%p, autostart=%d", network, autostart);
2964 2965 2966 2967 2968 2969

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

2970 2971 2972 2973 2974
    conn = network->conn;

    if (conn->networkDriver && conn->networkDriver->networkSetAutostart)
        return conn->networkDriver->networkSetAutostart (network, autostart);

2975
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2976
    return -1;
2977
}
2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991

/*
 * vim: set tabstop=4:
 * vim: set shiftwidth=4:
 * vim: set expandtab:
 */
/*
 * Local variables:
 *  indent-tabs-mode: nil
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  tab-width: 4
 * End:
 */