libvirt.c 97.2 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
#include <assert.h>
22

23 24
#include <libxml/parser.h>
#include <libxml/xpath.h>
25
#include <libxml/uri.h>
26 27
#include "getpass.h"

28
#ifdef HAVE_WINSOCK2_H
29 30
#include <winsock2.h>
#endif
31

D
Daniel Veillard 已提交
32
#include "internal.h"
33
#include "driver.h"
34

35
#include "uuid.h"
36
#include "test.h"
37
#include "xen_unified.h"
38
#include "remote_internal.h"
39
#include "qemu_driver.h"
40 41 42
#ifdef WITH_OPENVZ
#include "openvz_driver.h"
#endif
43

D
Daniel Veillard 已提交
44 45 46 47 48 49
/*
 * TODO:
 * - use lock to protect against concurrent accesses ?
 * - use reference counting to garantee coherent pointer state ?
 */

50
static virDriverPtr virDriverTab[MAX_DRIVERS];
51
static int virDriverTabCount = 0;
52
static virNetworkDriverPtr virNetworkDriverTab[MAX_DRIVERS];
53
static int virNetworkDriverTabCount = 0;
54 55
static virStateDriverPtr virStateDriverTab[MAX_DRIVERS];
static int virStateDriverTabCount = 0;
56 57
static int initialized = 0;

58 59 60
#define DEBUG(fmt,...) VIR_DEBUG(__FILE__, fmt, __VA_ARGS__)
#define DEBUG0(msg) VIR_DEBUG(__FILE__, "%s", msg)

61
#ifdef ENABLE_DEBUG
62 63
int debugFlag = 0;
#endif
64

65 66 67 68 69 70 71 72
static int virConnectAuthCallbackDefault(virConnectCredentialPtr cred,
                                         unsigned int ncred,
                                         void *cbdata ATTRIBUTE_UNUSED) {
    int i;

    for (i = 0 ; i < ncred ; i++) {
        char buf[1024];
        char *bufptr = buf;
73
        size_t len;
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

        if (printf("%s:", cred[i].prompt) < 0)
            return -1;
        if (fflush(stdout) != 0)
            return -1;

        switch (cred[i].type) {
        case VIR_CRED_USERNAME:
        case VIR_CRED_AUTHNAME:
        case VIR_CRED_ECHOPROMPT:
        case VIR_CRED_REALM:
            if (!fgets(buf, sizeof(buf), stdin)) {
                if (feof(stdin)) { /* Treat EOF as "" */
                    buf[0] = '\0';
                    break;
                }
                return -1;
            }
92 93 94
            len = strlen(buf);
            if (len != 0 && buf[len-1] == '\n')
                buf[len-1] = '\0';
95 96 97 98 99 100 101 102
            break;

        case VIR_CRED_PASSPHRASE:
        case VIR_CRED_NOECHOPROMPT:
            bufptr = getpass("");
            if (!bufptr)
                return -1;
            break;
103 104 105

        default:
            return -1;
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        }

        if (STREQ(bufptr, "") && cred[i].defresult)
            cred[i].result = strdup(cred[i].defresult);
        else
            cred[i].result = strdup(bufptr);
        if (!cred[i].result)
            return -1;
        cred[i].resultlen = strlen(cred[i].result);
    }

    return 0;
}

/* Don't typically want VIR_CRED_USERNAME. It enables you to authenticate
 * as one user, and act as another. It just results in annoying
 * prompts for the username twice & is very rarely what you want
 */
static int virConnectCredTypeDefault[] = {
    VIR_CRED_AUTHNAME,
    VIR_CRED_ECHOPROMPT,
    VIR_CRED_REALM,
    VIR_CRED_PASSPHRASE,
    VIR_CRED_NOECHOPROMPT,
};

static virConnectAuth virConnectAuthDefault = {
    virConnectCredTypeDefault,
    sizeof(virConnectCredTypeDefault)/sizeof(int),
    virConnectAuthCallbackDefault,
    NULL,
};

/*
 * virConnectAuthPtrDefault
 *
 * A default implementation of the authentication callbacks. This
 * implementation is suitable for command line based tools. It will
 * prompt for username, passwords, realm and one time keys as needed.
 * It will print on STDOUT, and read from STDIN. If this is not
 * suitable for the application's needs an alternative implementation
 * should be provided.
 */
virConnectAuthPtr virConnectAuthPtrDefault = &virConnectAuthDefault;

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
#if HAVE_WINSOCK2_H
static int
winsock_init (void)
{
    WORD winsock_version, err;
    WSADATA winsock_data;

    /* http://msdn2.microsoft.com/en-us/library/ms742213.aspx */
    winsock_version = MAKEWORD (2, 2);
    err = WSAStartup (winsock_version, &winsock_data);
    if (err != 0)
        return -1;
}
#endif

166 167 168 169 170 171 172 173 174 175 176 177
/**
 * 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)
{
178 179 180
#ifdef ENABLE_DEBUG
    char *debugEnv;
#endif
181 182
    if (initialized)
        return(0);
183
    initialized = 1;
184

185 186 187 188 189 190 191 192
#ifdef ENABLE_DEBUG
    debugEnv = getenv("LIBVIRT_DEBUG");
    if (debugEnv && *debugEnv && *debugEnv != '0')
        debugFlag = 1;
#endif

    DEBUG0("register drivers");

193 194 195 196
#if HAVE_WINSOCK2_H
    if (winsock_init () == -1) return -1;
#endif

197 198 199
    if (!bindtextdomain(GETTEXT_PACKAGE, LOCALEBASEDIR))
        return (-1);

200
    /*
201 202
     * Note that the order is important: the first ones have a higher
     * priority when calling virConnectOpen.
203
     */
204
#ifdef WITH_TEST
205
    if (testRegister() == -1) return -1;
206
#endif
207
#ifdef WITH_QEMU 
208
    if (qemudRegister() == -1) return -1;
209
#endif
210 211
#ifdef WITH_XEN
    if (xenUnifiedRegister () == -1) return -1;
212
#endif
213 214 215
#ifdef WITH_OPENVZ
    if (openvzRegister() == -1) return -1;
#endif
216 217
#ifdef WITH_REMOTE
    if (remoteRegister () == -1) return -1;
218
#endif
D
Daniel P. Berrange 已提交
219

220 221 222 223 224
    return(0);
}



D
Daniel Veillard 已提交
225 226 227
/**
 * virLibConnError:
 * @conn: the connection if available
228
 * @error: the error number
D
Daniel Veillard 已提交
229 230 231 232 233
 * @info: extra information string
 *
 * Handle an error at the connection level
 */
static void
234 235
virLibConnError(virConnectPtr conn, virErrorNumber error, const char *info)
{
D
Daniel Veillard 已提交
236
    const char *errmsg;
237

D
Daniel Veillard 已提交
238 239 240 241
    if (error == VIR_ERR_OK)
        return;

    errmsg = __virErrorMsg(error, info);
242
    __virRaiseError(conn, NULL, NULL, VIR_FROM_NONE, error, VIR_ERR_ERROR,
D
Daniel Veillard 已提交
243 244 245 246
                    errmsg, info, NULL, 0, 0, errmsg, info);
}

/**
247
 * virLibConnWarning:
D
Daniel Veillard 已提交
248
 * @conn: the connection if available
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
 * @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 已提交
271 272 273 274 275
 * @info: extra information string
 *
 * Handle an error at the connection level
 */
static void
276 277 278
virLibDomainError(virDomainPtr domain, virErrorNumber error,
                  const char *info)
{
D
Daniel Veillard 已提交
279 280
    virConnectPtr conn = NULL;
    const char *errmsg;
281

D
Daniel Veillard 已提交
282 283 284 285 286 287 288
    if (error == VIR_ERR_OK)
        return;

    errmsg = __virErrorMsg(error, info);
    if (error != VIR_ERR_INVALID_DOMAIN) {
        conn = domain->conn;
    }
289
    __virRaiseError(conn, domain, NULL, VIR_FROM_DOM, error, VIR_ERR_ERROR,
D
Daniel Veillard 已提交
290 291 292
                    errmsg, info, NULL, 0, 0, errmsg, info);
}

293
/**
294 295 296 297
 * virLibNetworkError:
 * @conn: the connection if available
 * @error: the error noumber
 * @info: extra information string
298
 *
299
 * Handle an error at the connection level
300
 */
301 302 303 304 305 306 307 308 309 310 311 312 313 314
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;
    }
315
    __virRaiseError(conn, NULL, network, VIR_FROM_NET, error, VIR_ERR_ERROR,
316 317 318 319 320 321 322 323 324 325 326 327 328 329
                    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)
{
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
    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++;
345 346 347 348 349 350 351 352 353 354 355 356 357
}

/**
 * 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)
{
358 359 360 361 362 363 364 365 366 367 368 369 370
    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);
    }

371 372 373 374 375 376 377
    if (driver->no < 0) {
    	virLibConnError
            (NULL, VIR_ERR_INVALID_ARG,
             "virRegisterDriver: tried to register an internal Xen driver");
        return -1;
    }

378 379
    virDriverTab[virDriverTabCount] = driver;
    return virDriverTabCount++;
380 381
}

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
/**
 * 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;

413 414 415
    if (virInitialize() < 0)
        return -1;

416 417 418 419
    if (virInitialize() < 0)
        return -1;

    for (i = 0 ; i < virStateDriverTabCount ; i++) {
420 421
        if (virStateDriverTab[i]->initialize &&
            virStateDriverTab[i]->initialize() < 0)
422 423 424 425 426 427 428 429 430
            ret = -1;
    }
    return ret;
}

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

    for (i = 0 ; i < virStateDriverTabCount ; i++) {
431 432
        if (virStateDriverTab[i]->cleanup &&
            virStateDriverTab[i]->cleanup() < 0)
433 434 435 436 437 438 439 440 441
            ret = -1;
    }
    return ret;
}

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

    for (i = 0 ; i < virStateDriverTabCount ; i++) {
442 443
        if (virStateDriverTab[i]->reload &&
            virStateDriverTab[i]->reload() < 0)
444 445 446 447 448 449 450 451 452
            ret = -1;
    }
    return ret;
}

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

    for (i = 0 ; i < virStateDriverTabCount ; i++) {
453 454
        if (virStateDriverTab[i]->active &&
            virStateDriverTab[i]->active())
455 456 457 458 459 460 461
            ret = 1;
    }
    return ret;
}



462 463 464
/**
 * virGetVersion:
 * @libVer: return value for the library version (OUT)
465
 * @type: the type of connection/driver looked at
466 467 468 469 470 471 472 473 474 475 476 477
 * @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
478 479 480
virGetVersion(unsigned long *libVer, const char *type,
              unsigned long *typeVer)
{
481
    int i;
482
    DEBUG("libVir=%p, type=%s, typeVer=%p", libVer, type, typeVer);
483

484
    if (!initialized)
485 486
        if (virInitialize() < 0)
	    return -1;
487

488
    if (libVer == NULL)
489
        return (-1);
490 491 492
    *libVer = LIBVIR_VERSION_NUMBER;

    if (typeVer != NULL) {
493 494
        if (type == NULL)
	    type = "Xen";
495
	for (i = 0;i < virDriverTabCount;i++) {
496
	    if ((virDriverTab[i] != NULL) &&
497
	        (!strcasecmp(virDriverTab[i]->name, type))) {
498 499 500 501
		*typeVer = virDriverTab[i]->ver;
		break;
	    }
	}
502
        if (i >= virDriverTabCount) {
503
            *typeVer = 0;
504
            virLibConnError(NULL, VIR_ERR_NO_SUPPORT, type);
505 506 507 508
            return (-1);
        }
    }
    return (0);
509 510
}

511
static virConnectPtr
512 513 514
do_open (const char *name,
         virConnectAuthPtr auth,
         int flags)
515
{
516
    int i, res;
517
    virConnectPtr ret = NULL;
518
    xmlURIPtr uri;
519

520 521 522 523 524 525 526 527
    /* 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:///";

528 529 530 531 532 533
    /* Convert xen:// -> xen:/// because xmlParseURI cannot parse the
     * former.  This allows URIs such as xen://localhost to work.
     */
    if (STREQ (name, "xen://"))
        name = "xen:///";

534
    if (!initialized)
535 536
        if (virInitialize() < 0)
	    return NULL;
537

538
    ret = virGetConnect();
539
    if (ret == NULL) {
540
        virLibConnError(NULL, VIR_ERR_NO_MEMORY, _("allocating connection"));
541 542 543 544 545 546 547
        return NULL;
    }

    uri = xmlParseURI (name);
    if (!uri) {
        virLibConnError (ret, VIR_ERR_INVALID_ARG,
                         _("could not parse connection URI"));
548 549 550
        goto failed;
    }

551 552 553 554 555 556 557 558 559 560 561
    DEBUG("name \"%s\" to URI components:\n"
          "  scheme %s\n"
          "  opaque %s\n"
          "  authority %s\n"
          "  server %s\n"
          "  user %s\n"
          "  port %d\n"
          "  path %s\n",
          name,
          uri->scheme, uri->opaque, uri->authority, uri->server,
          uri->user, uri->port, uri->path);
562

563 564 565 566 567 568
    ret->name = strdup (name);
    if (!ret->name) {
        virLibConnError (ret, VIR_ERR_NO_MEMORY, "allocating conn->name");
        goto failed;
    }

569
    for (i = 0; i < virDriverTabCount; i++) {
570 571
        DEBUG("trying driver %d (%s) ...",
              i, virDriverTab[i]->name);
572
        res = virDriverTab[i]->open (ret, uri, auth, flags);
573 574 575 576 577
        DEBUG("driver %d %s returned %s",
              i, virDriverTab[i]->name,
              res == VIR_DRV_OPEN_SUCCESS ? "SUCCESS" :
              (res == VIR_DRV_OPEN_DECLINED ? "DECLINED" :
               (res == VIR_DRV_OPEN_ERROR ? "ERROR" : "unknown status")));
578 579 580 581 582
        if (res == VIR_DRV_OPEN_ERROR) goto failed;
        else if (res == VIR_DRV_OPEN_SUCCESS) {
            ret->driver = virDriverTab[i];
            break;
        }
583 584
    }

585
    if (!ret->driver) {
586 587
        /* If we reach here, then all drivers declined the connection. */
        virLibConnError (NULL, VIR_ERR_NO_CONNECT, name);
588
        goto failed;
589 590
    }

591
    for (i = 0; i < virNetworkDriverTabCount; i++) {
592
        res = virNetworkDriverTab[i]->open (ret, uri, auth, flags);
593 594 595 596 597
        DEBUG("network driver %d %s returned %s",
              i, virNetworkDriverTab[i]->name,
              res == VIR_DRV_OPEN_SUCCESS ? "SUCCESS" :
              (res == VIR_DRV_OPEN_DECLINED ? "DECLINED" :
               (res == VIR_DRV_OPEN_ERROR ? "ERROR" : "unknown status")));
598
        if (res == VIR_DRV_OPEN_ERROR) {
599 600 601 602
            if (STREQ(virNetworkDriverTab[i]->name, "remote")) {
                virLibConnWarning (NULL, VIR_WAR_NO_NETWORK, 
                                   "Is the daemon running ?");
            }
603 604
            break;
        } else if (res == VIR_DRV_OPEN_SUCCESS) {
605 606 607
            ret->networkDriver = virNetworkDriverTab[i];
            break;
        }
608
    }
609

610 611
    /* Cleansing flags */
    ret->flags = flags & VIR_CONNECT_RO;
612

613 614
    xmlFreeURI (uri);

615
    return ret;
616 617

failed:
618
    if (ret->driver) ret->driver->close (ret);
619
    if (uri) xmlFreeURI(uri);
620
	virUnrefConnect(ret);
621
    return NULL;
622 623
}

624 625
/**
 * virConnectOpen:
626
 * @name: URI of the hypervisor
627 628 629 630 631
 *
 * 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
632 633
 *
 * URIs are documented at http://libvirt.org/uri.html
634 635 636 637
 */
virConnectPtr
virConnectOpen (const char *name)
{
638
    DEBUG("name=%s", name);
639
    return do_open (name, NULL, 0);
640 641
}

642
/**
643
 * virConnectOpenReadOnly:
644
 * @name: URI of the hypervisor
645
 *
646 647 648
 * 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.
649 650
 *
 * Returns a pointer to the hypervisor connection or NULL in case of error
651 652
 *
 * URIs are documented at http://libvirt.org/uri.html
653
 */
654
virConnectPtr
655 656
virConnectOpenReadOnly(const char *name)
{
657
    DEBUG("name=%s", name);
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
    return do_open (name, NULL, VIR_CONNECT_RO);
}

/**
 * virConnectOpenAuth:
 * @name: URI of the hypervisor
 * @auth: Authenticate callback parameters
 * @flags: Open flags
 *
 * This function should be called first to get a connection to the 
 * Hypervisor. If neccessary, authentication will be performed fetching
 * credentials via the callback
 *
 * Returns a pointer to the hypervisor connection or NULL in case of error
 *
 * URIs are documented at http://libvirt.org/uri.html
 */
virConnectPtr
virConnectOpenAuth(const char *name,
                   virConnectAuthPtr auth,
                   int flags)
{
    DEBUG("name=%s", name);
    return do_open (name, auth, flags);
D
Daniel Veillard 已提交
682 683 684
}

/**
685
 * virConnectClose:
D
Daniel Veillard 已提交
686 687 688 689 690 691 692 693 694 695
 * @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
696 697
virConnectClose(virConnectPtr conn)
{
698 699
    DEBUG("conn=%p", conn);

K
Karel Zak 已提交
700
    if (!VIR_IS_CONNECT(conn))
701
        return (-1);
702 703 704

    if (conn->networkDriver)
        conn->networkDriver->close (conn);
705
    conn->driver->close (conn);
706

707
    if (virUnrefConnect(conn) < 0)
708
        return (-1);
709
    return (0);
D
Daniel Veillard 已提交
710 711
}

712 713 714 715 716 717 718 719 720 721 722 723 724 725
/* Not for public use.  This function is part of the internal
 * implementation of driver features in the remote case.
 */
int
__virDrvSupportsFeature (virConnectPtr conn, int feature)
{
    DEBUG("conn=%p, feature=%d", conn, feature);

    if (!VIR_IS_CONNECT(conn))
        return (-1);

    return VIR_DRV_SUPPORTS_FEATURE (conn->driver, conn, feature);
}

726 727 728 729 730 731 732
/**
 * 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.
733 734 735
 *
 * See also:
 * http://www.redhat.com/archives/libvir-list/2007-February/msg00096.html
736 737
 */
const char *
738 739
virConnectGetType(virConnectPtr conn)
{
740
    const char *ret;
741
    DEBUG("conn=%p", conn);
742

D
Daniel Veillard 已提交
743
    if (!VIR_IS_CONNECT(conn)) {
744
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
745
        return (NULL);
D
Daniel Veillard 已提交
746
    }
747 748 749 750

    if (conn->driver->type) {
        ret = conn->driver->type (conn);
        if (ret) return ret;
751
    }
752
    return conn->driver->name;
753 754
}

D
Daniel Veillard 已提交
755
/**
756
 * virConnectGetVersion:
D
Daniel Veillard 已提交
757
 * @conn: pointer to the hypervisor connection
758
 * @hvVer: return value for the version of the running hypervisor (OUT)
D
Daniel Veillard 已提交
759
 *
760 761 762
 * 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 已提交
763
 *
764 765 766
 * 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 已提交
767
 */
768
int
769 770
virConnectGetVersion(virConnectPtr conn, unsigned long *hvVer)
{
771 772
    DEBUG("conn=%p, hvVer=%p", conn, hvVer);

D
Daniel Veillard 已提交
773
    if (!VIR_IS_CONNECT(conn)) {
774
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
775
        return (-1);
D
Daniel Veillard 已提交
776
    }
777

D
Daniel Veillard 已提交
778 779
    if (hvVer == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
780
        return (-1);
D
Daniel Veillard 已提交
781
    }
782

783 784
    if (conn->driver->version)
        return conn->driver->version (conn, hvVer);
D
Daniel P. Berrange 已提交
785

786 787
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
788 789
}

790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
/**
 * 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)
{
805 806
    DEBUG("conn=%p", conn);

807
    if (!VIR_IS_CONNECT(conn)) {
808
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
        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)
{
837 838
    char *name;

839 840
    DEBUG("conn=%p", conn);

841
    if (!VIR_IS_CONNECT(conn)) {
842
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
843 844 845
        return NULL;
    }

846 847 848
    /* Drivers may override getURI, but if they don't then
     * we provide a default implementation.
     */
849 850 851
    if (conn->driver->getURI)
        return conn->driver->getURI (conn);

852 853 854 855 856 857
    name = strdup (conn->name);
    if (!name) {
        virLibConnError (conn, VIR_ERR_NO_MEMORY, __FUNCTION__);
        return NULL;
    }
    return name;
858 859
}

860 861 862 863 864
/**
 * virConnectGetMaxVcpus:
 * @conn: pointer to the hypervisor connection
 * @type: value of the 'type' attribute in the <domain> element
 *
865
 * Provides the maximum number of virtual CPUs supported for a guest VM of a
866 867 868 869 870 871 872 873 874
 * 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)
{
875 876
    DEBUG("conn=%p, type=%s", conn, type);

877
    if (!VIR_IS_CONNECT(conn)) {
878
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
879 880 881
        return (-1);
    }

882 883
    if (conn->driver->getMaxVcpus)
        return conn->driver->getMaxVcpus (conn, type);
884

885 886
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
887 888
}

889
/**
890
 * virConnectListDomains:
891 892 893 894 895 896 897 898 899
 * @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
900 901
virConnectListDomains(virConnectPtr conn, int *ids, int maxids)
{
902 903
    DEBUG("conn=%p, ids=%p, maxids=%d", conn, ids, maxids);

D
Daniel Veillard 已提交
904
    if (!VIR_IS_CONNECT(conn)) {
905
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
906
        return (-1);
D
Daniel Veillard 已提交
907
    }
908

909
    if ((ids == NULL) || (maxids < 0)) {
D
Daniel Veillard 已提交
910
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
911
        return (-1);
D
Daniel Veillard 已提交
912
    }
913

914 915
    if (conn->driver->listDomains)
        return conn->driver->listDomains (conn, ids, maxids);
916

917 918
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
D
Daniel Veillard 已提交
919 920
}

K
 
Karel Zak 已提交
921 922 923 924
/**
 * virConnectNumOfDomains:
 * @conn: pointer to the hypervisor connection
 *
925 926
 * Provides the number of active domains.
 *
K
 
Karel Zak 已提交
927 928 929
 * Returns the number of domain found or -1 in case of error
 */
int
930 931
virConnectNumOfDomains(virConnectPtr conn)
{
932 933
    DEBUG("conn=%p", conn);

D
Daniel Veillard 已提交
934
    if (!VIR_IS_CONNECT(conn)) {
935
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
936
        return (-1);
D
Daniel Veillard 已提交
937
    }
K
Karel Zak 已提交
938

939 940
    if (conn->driver->numOfDomains)
        return conn->driver->numOfDomains (conn);
941

942 943
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
K
 
Karel Zak 已提交
944 945
}

946 947 948 949
/**
 * virDomainGetConnect:
 * @dom: pointer to a domain
 *
950
 * Provides the connection pointer associated with a domain.  The
951 952 953
 * reference counter on the connection is not increased by this
 * call.
 *
954 955 956 957
 * WARNING: When writing libvirt bindings in other languages, do
 * not use this function.  Instead, store the connection and
 * the domain object together.
 *
958 959 960 961 962
 * Returns the virConnectPtr or NULL in case of failure.
 */
virConnectPtr
virDomainGetConnect (virDomainPtr dom)
{
963 964
    DEBUG("dom=%p", dom);

965
    if (!VIR_IS_DOMAIN (dom)) {
966
        virLibDomainError (NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
967 968 969 970 971
        return NULL;
    }
    return dom->conn;
}

D
Daniel Veillard 已提交
972
/**
973
 * virDomainCreateLinux:
D
Daniel Veillard 已提交
974
 * @conn: pointer to the hypervisor connection
975
 * @xmlDesc: an XML description of the domain
976
 * @flags: an optional set of virDomainFlags
D
Daniel Veillard 已提交
977
 *
978 979 980
 * 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 已提交
981 982 983
 * 
 * Returns a new domain object or NULL in case of failure
 */
984
virDomainPtr
985 986
virDomainCreateLinux(virConnectPtr conn, const char *xmlDesc,
                     unsigned int flags)
987
{
988 989
    DEBUG("conn=%p, xmlDesc=%s, flags=%d", conn, xmlDesc, flags);

D
Daniel Veillard 已提交
990
    if (!VIR_IS_CONNECT(conn)) {
991
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
992
        return (NULL);
D
Daniel Veillard 已提交
993 994 995
    }
    if (xmlDesc == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
996
        return (NULL);
D
Daniel Veillard 已提交
997
    }
998 999 1000 1001
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (NULL);
    }
D
Daniel Veillard 已提交
1002

1003 1004 1005 1006 1007
    if (conn->driver->domainCreateLinux)
        return conn->driver->domainCreateLinux (conn, xmlDesc, flags);

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

1010

1011
/**
1012
 * virDomainLookupByID:
1013 1014 1015 1016 1017
 * @conn: pointer to the hypervisor connection
 * @id: the domain ID number
 *
 * Try to find a domain based on the hypervisor ID number
 *
1018 1019
 * 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.
1020
 */
1021
virDomainPtr
1022 1023
virDomainLookupByID(virConnectPtr conn, int id)
{
1024 1025
    DEBUG("conn=%p, id=%d", conn, id);

D
Daniel Veillard 已提交
1026
    if (!VIR_IS_CONNECT(conn)) {
1027
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1028
        return (NULL);
D
Daniel Veillard 已提交
1029 1030 1031
    }
    if (id < 0) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1032
        return (NULL);
D
Daniel Veillard 已提交
1033
    }
1034

1035 1036
    if (conn->driver->domainLookupByID)
        return conn->driver->domainLookupByID (conn, id);
1037

1038 1039
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return NULL;
1040 1041 1042 1043 1044
}

/**
 * virDomainLookupByUUID:
 * @conn: pointer to the hypervisor connection
K
Karel Zak 已提交
1045
 * @uuid: the raw UUID for the domain
1046 1047 1048
 *
 * Try to lookup a domain on the given hypervisor based on its UUID.
 *
1049 1050
 * 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.
1051 1052
 */
virDomainPtr
1053 1054
virDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
{
1055 1056
    DEBUG("conn=%p, uuid=%s", conn, uuid);

D
Daniel Veillard 已提交
1057
    if (!VIR_IS_CONNECT(conn)) {
1058
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1059
        return (NULL);
D
Daniel Veillard 已提交
1060 1061 1062
    }
    if (uuid == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1063
        return (NULL);
D
Daniel Veillard 已提交
1064
    }
1065

1066 1067
    if (conn->driver->domainLookupByUUID)
        return conn->driver->domainLookupByUUID (conn, uuid);
1068

1069 1070
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return NULL;
1071 1072
}

K
Karel Zak 已提交
1073 1074 1075 1076 1077 1078 1079
/**
 * 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.
 *
1080 1081
 * 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 已提交
1082 1083 1084 1085
 */
virDomainPtr
virDomainLookupByUUIDString(virConnectPtr conn, const char *uuidstr)
{
1086 1087
    int raw[VIR_UUID_BUFLEN], i;
    unsigned char uuid[VIR_UUID_BUFLEN];
K
Karel Zak 已提交
1088 1089
    int ret;

1090 1091
    DEBUG("conn=%p, uuidstr=%s", conn, uuidstr);

K
Karel Zak 已提交
1092
    if (!VIR_IS_CONNECT(conn)) {
1093
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
K
Karel Zak 已提交
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
        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);
    
1115
    if (ret!=VIR_UUID_BUFLEN) {
K
Karel Zak 已提交
1116 1117 1118
	virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return (NULL);
    }
1119
    for (i = 0; i < VIR_UUID_BUFLEN; i++)
K
Karel Zak 已提交
1120 1121 1122 1123 1124
        uuid[i] = raw[i] & 0xFF;
    
    return virDomainLookupByUUID(conn, &uuid[0]);
}

1125 1126 1127 1128 1129
/**
 * virDomainLookupByName:
 * @conn: pointer to the hypervisor connection
 * @name: name for the domain
 *
1130
 * Try to lookup a domain on the given hypervisor based on its name.
1131
 *
1132 1133
 * 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.
1134 1135
 */
virDomainPtr
1136 1137
virDomainLookupByName(virConnectPtr conn, const char *name)
{
1138 1139
    DEBUG("conn=%p, name=%s", conn, name);

D
Daniel Veillard 已提交
1140
    if (!VIR_IS_CONNECT(conn)) {
1141
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1142
        return (NULL);
D
Daniel Veillard 已提交
1143 1144 1145
    }
    if (name == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1146
        return (NULL);
D
Daniel Veillard 已提交
1147
    }
1148

1149 1150 1151 1152 1153
    if (conn->driver->domainLookupByName)
        return conn->driver->domainLookupByName (conn, name);

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

D
Daniel Veillard 已提交
1156
/**
1157
 * virDomainDestroy:
D
Daniel Veillard 已提交
1158 1159 1160 1161
 * @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.
1162 1163
 * The data structure is freed and should not be used thereafter if the
 * call does not return an error.
1164
 * This function may requires priviledged access
D
Daniel Veillard 已提交
1165 1166
 *
 * Returns 0 in case of success and -1 in case of failure.
1167
 */
D
Daniel Veillard 已提交
1168
int
1169 1170
virDomainDestroy(virDomainPtr domain)
{
1171
    virConnectPtr conn;
1172

1173 1174
    DEBUG("domain=%p", domain);

D
Daniel Veillard 已提交
1175
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
1176
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1177
        return (-1);
D
Daniel Veillard 已提交
1178
    }
1179

1180
    conn = domain->conn;
1181 1182 1183 1184
    if (conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
1185

1186 1187
    if (conn->driver->domainDestroy)
        return conn->driver->domainDestroy (domain);
1188

1189 1190
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
}

/**
 * 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
1203 1204
virDomainFree(virDomainPtr domain)
{
1205 1206
    DEBUG("domain=%p", domain);

D
Daniel Veillard 已提交
1207
    if (!VIR_IS_DOMAIN(domain)) {
1208
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1209
        return (-1);
D
Daniel Veillard 已提交
1210
    }
1211
    if (virUnrefDomain(domain) < 0)
1212 1213
        return (-1);
    return(0);
D
Daniel Veillard 已提交
1214 1215 1216
}

/**
1217
 * virDomainSuspend:
D
Daniel Veillard 已提交
1218 1219 1220 1221
 * @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 
1222
 * hypervisor level will stay allocated. Use virDomainResume() to reactivate
D
Daniel Veillard 已提交
1223
 * the domain.
1224
 * This function may requires priviledged access.
D
Daniel Veillard 已提交
1225 1226 1227 1228
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1229 1230
virDomainSuspend(virDomainPtr domain)
{
1231
    virConnectPtr conn;
1232
    DEBUG("domain=%p", domain);
1233

D
Daniel Veillard 已提交
1234
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
1235
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1236
        return (-1);
D
Daniel Veillard 已提交
1237
    }
1238 1239 1240 1241
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
D
Daniel Veillard 已提交
1242

1243 1244
    conn = domain->conn;

1245 1246
    if (conn->driver->domainSuspend)
        return conn->driver->domainSuspend (domain);
1247

1248 1249
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
D
Daniel Veillard 已提交
1250 1251 1252
}

/**
1253
 * virDomainResume:
D
Daniel Veillard 已提交
1254 1255 1256
 * @domain: a domain object
 *
 * Resume an suspended domain, the process is restarted from the state where
1257
 * it was frozen by calling virSuspendDomain().
1258
 * This function may requires priviledged access
D
Daniel Veillard 已提交
1259 1260 1261 1262
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1263 1264
virDomainResume(virDomainPtr domain)
{
1265
    virConnectPtr conn;
1266
    DEBUG("domain=%p", domain);
1267

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

1277 1278
    conn = domain->conn;

1279 1280
    if (conn->driver->domainResume)
        return conn->driver->domainResume (domain);
1281

1282 1283
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
D
Daniel Veillard 已提交
1284 1285
}

1286 1287 1288 1289 1290 1291
/**
 * virDomainSave:
 * @domain: a domain object
 * @to: path for the output file
 *
 * This method will suspend a domain and save its memory contents to
1292 1293 1294
 * 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.
1295 1296 1297 1298
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1299 1300
virDomainSave(virDomainPtr domain, const char *to)
{
1301
    char filepath[4096];
1302
    virConnectPtr conn;
1303
    DEBUG("domain=%p, to=%s", domain, to);
1304

D
Daniel Veillard 已提交
1305
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
1306
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1307
        return (-1);
D
Daniel Veillard 已提交
1308
    }
1309 1310 1311 1312
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
1313
    conn = domain->conn;
D
Daniel Veillard 已提交
1314 1315
    if (to == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1316
        return (-1);
D
Daniel Veillard 已提交
1317
    }
1318

1319 1320 1321 1322 1323
    /*
     * 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] != '/') {
1324
        unsigned int len, t;
1325

1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
        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];
1336 1337 1338

    }

1339 1340 1341
    if (conn->driver->domainSave)
        return conn->driver->domainSave (domain, to);

1342
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1343
    return -1;
1344 1345 1346 1347
}

/**
 * virDomainRestore:
1348
 * @conn: pointer to the hypervisor connection
1349 1350 1351 1352 1353 1354 1355
 * @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
1356 1357
virDomainRestore(virConnectPtr conn, const char *from)
{
1358
    char filepath[4096];
1359
    DEBUG("conn=%p, from=%s", conn, from);
1360

D
Daniel Veillard 已提交
1361
    if (!VIR_IS_CONNECT(conn)) {
1362
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1363
        return (-1);
D
Daniel Veillard 已提交
1364
    }
1365 1366 1367 1368
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
D
Daniel Veillard 已提交
1369 1370
    if (from == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1371
        return (-1);
D
Daniel Veillard 已提交
1372 1373
    }

1374 1375 1376 1377 1378
    /*
     * 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] != '/') {
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
        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];
    }

1393 1394 1395
    if (conn->driver->domainRestore)
        return conn->driver->domainRestore (conn, from);

1396
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1397
    return -1;
D
Daniel Veillard 已提交
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
}

/**
 * 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;
1417
    DEBUG("domain=%p, to=%s, flags=%d", domain, to, flags);
D
Daniel Veillard 已提交
1418 1419

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
1420
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
D
Daniel Veillard 已提交
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
        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];

    }

1453 1454 1455
    if (conn->driver->domainCoreDump)
        return conn->driver->domainCoreDump (domain, to, flags);

1456
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1457
    return -1;
1458 1459
}

1460 1461 1462 1463 1464
/**
 * virDomainShutdown:
 * @domain: a domain object
 *
 * Shutdown a domain, the domain object is still usable there after but
1465 1466
 * the domain OS is being stopped. Note that the guest OS may ignore the
 * request.
1467 1468 1469 1470 1471 1472 1473
 *
 * 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
1474 1475
virDomainShutdown(virDomainPtr domain)
{
1476
    virConnectPtr conn;
1477
    DEBUG("domain=%p", domain);
1478

D
Daniel Veillard 已提交
1479
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
1480
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1481
        return (-1);
D
Daniel Veillard 已提交
1482
    }
1483 1484 1485 1486
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
1487

1488 1489
    conn = domain->conn;

1490 1491
    if (conn->driver->domainShutdown)
        return conn->driver->domainShutdown (domain);
1492

1493
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1494
    return -1;
1495 1496
}

1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
/**
 * 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)
{
1511
    virConnectPtr conn;
1512
    DEBUG("domain=%p, flags=%u", domain, flags);
1513 1514

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
1515
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1516 1517
        return (-1);
    }
1518 1519 1520 1521
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
1522

1523 1524
    conn = domain->conn;

1525 1526
    if (conn->driver->domainReboot)
        return conn->driver->domainReboot (domain, flags);
1527

1528
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1529
    return -1;
1530 1531
}

1532
/**
1533
 * virDomainGetName:
1534 1535 1536 1537 1538 1539 1540 1541
 * @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 *
1542 1543
virDomainGetName(virDomainPtr domain)
{
1544 1545
    DEBUG("domain=%p", domain);

D
Daniel Veillard 已提交
1546
    if (!VIR_IS_DOMAIN(domain)) {
1547
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1548
        return (NULL);
D
Daniel Veillard 已提交
1549
    }
1550
    return (domain->name);
1551 1552
}

1553 1554 1555
/**
 * virDomainGetUUID:
 * @domain: a domain object
1556
 * @uuid: pointer to a VIR_UUID_BUFLEN bytes array
1557 1558 1559 1560 1561 1562
 *
 * Get the UUID for a domain
 *
 * Returns -1 in case of error, 0 in case of success
 */
int
1563 1564
virDomainGetUUID(virDomainPtr domain, unsigned char *uuid)
{
1565 1566
    DEBUG("domain=%p, uuid=%p", domain, uuid);

D
Daniel Veillard 已提交
1567
    if (!VIR_IS_DOMAIN(domain)) {
1568
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1569
        return (-1);
D
Daniel Veillard 已提交
1570 1571 1572
    }
    if (uuid == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1573
        return (-1);
D
Daniel Veillard 已提交
1574 1575
    }

1576
    if (domain->id == 0) {
1577
        memset(uuid, 0, VIR_UUID_BUFLEN);
D
Daniel Veillard 已提交
1578
    } else {
1579
        memcpy(uuid, &domain->uuid[0], VIR_UUID_BUFLEN);
1580 1581
    }
    return (0);
1582 1583
}

K
Karel Zak 已提交
1584 1585 1586
/**
 * virDomainGetUUIDString:
 * @domain: a domain object
1587
 * @buf: pointer to a VIR_UUID_STRING_BUFLEN bytes array
K
Karel Zak 已提交
1588 1589 1590 1591 1592 1593 1594 1595 1596
 *
 * 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)
{
1597
    unsigned char uuid[VIR_UUID_BUFLEN];
1598
    DEBUG("domain=%p, buf=%p", domain, buf);
1599

K
Karel Zak 已提交
1600
    if (!VIR_IS_DOMAIN(domain)) {
1601
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
K
Karel Zak 已提交
1602 1603 1604 1605 1606 1607 1608 1609
        return (-1);
    }
    if (buf == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
    
    if (virDomainGetUUID(domain, &uuid[0]))
1610
        return (-1);
K
Karel Zak 已提交
1611

1612
    virUUIDFormat(uuid, buf);
K
Karel Zak 已提交
1613 1614 1615
    return (0);
}

1616
/**
1617
 * virDomainGetID:
1618 1619 1620 1621 1622 1623 1624
 * @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
1625 1626
virDomainGetID(virDomainPtr domain)
{
1627 1628
    DEBUG("domain=%p", domain);

D
Daniel Veillard 已提交
1629
    if (!VIR_IS_DOMAIN(domain)) {
1630
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1631
        return ((unsigned int) -1);
D
Daniel Veillard 已提交
1632
    }
1633
    return (domain->id);
1634 1635
}

1636 1637 1638 1639 1640 1641
/**
 * virDomainGetOSType:
 * @domain: a domain object
 *
 * Get the type of domain operation system.
 *
1642 1643
 * Returns the new string or NULL in case of error, the string must be
 *         freed by the caller.
1644 1645
 */
char *
1646 1647
virDomainGetOSType(virDomainPtr domain)
{
1648
    virConnectPtr conn;
1649
    DEBUG("domain=%p", domain);
1650

D
Daniel Veillard 已提交
1651
    if (!VIR_IS_DOMAIN(domain)) {
1652
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1653
        return (NULL);
D
Daniel Veillard 已提交
1654
    }
1655

1656 1657 1658 1659
    conn = domain->conn;

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

1661
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1662
    return NULL;
1663 1664
}

1665
/**
1666
 * virDomainGetMaxMemory:
1667 1668 1669 1670 1671 1672 1673 1674 1675
 * @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
1676 1677
virDomainGetMaxMemory(virDomainPtr domain)
{
1678
    virConnectPtr conn;
1679
    DEBUG("domain=%p", domain);
1680

D
Daniel Veillard 已提交
1681
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
1682
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1683
        return (0);
D
Daniel Veillard 已提交
1684
    }
1685

1686 1687
    conn = domain->conn;

1688 1689 1690
    if (conn->driver->domainGetMaxMemory)
        return conn->driver->domainGetMaxMemory (domain);

1691
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1692
    return 0;
1693 1694
}

D
Daniel Veillard 已提交
1695
/**
1696
 * virDomainSetMaxMemory:
D
Daniel Veillard 已提交
1697 1698 1699 1700 1701 1702
 * @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.
1703
 * This function requires priviledged access to the hypervisor.
D
Daniel Veillard 已提交
1704 1705 1706 1707
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1708 1709
virDomainSetMaxMemory(virDomainPtr domain, unsigned long memory)
{
1710
    virConnectPtr conn;
1711
    DEBUG("domain=%p, memory=%lu", domain, memory);
1712

1713 1714 1715 1716 1717
    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
1718
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1719
        return (-1);
1720
    }
1721 1722 1723 1724 1725 1726 1727 1728
    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);
    }
1729
    conn = domain->conn;
1730

1731 1732 1733
    if (conn->driver->domainSetMaxMemory)
        return conn->driver->domainSetMaxMemory (domain, memory);

1734
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1735
    return -1;
1736
}
1737

1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753
/**
 * 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;
1754
    DEBUG("domain=%p, memory=%lu", domain, memory);
1755

1756 1757 1758 1759 1760
    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
1761
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1762
        return (-1);
1763
    }
1764 1765 1766 1767 1768 1769
    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__);
1770
        return (-1);
1771 1772 1773
    }

    conn = domain->conn;
1774

1775 1776 1777
    if (conn->driver->domainSetMemory)
        return conn->driver->domainSetMemory (domain, memory);

1778
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1779
    return -1;
D
Daniel Veillard 已提交
1780 1781
}

1782 1783
/**
 * virDomainGetInfo:
1784
 * @domain: a domain object
1785 1786 1787
 * @info: pointer to a virDomainInfo structure allocated by the user
 * 
 * Extract information about a domain. Note that if the connection
1788
 * used to get the domain is limited only a partial set of the information
1789 1790 1791 1792 1793
 * can be extracted.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1794 1795
virDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
{
1796
    virConnectPtr conn;
1797
    DEBUG("domain=%p, info=%p", domain, info);
1798

D
Daniel Veillard 已提交
1799
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
1800
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1801
        return (-1);
D
Daniel Veillard 已提交
1802 1803 1804
    }
    if (info == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1805
        return (-1);
D
Daniel Veillard 已提交
1806
    }
1807

1808
    memset(info, 0, sizeof(virDomainInfo));
1809

1810 1811 1812 1813
    conn = domain->conn;

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

1815
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1816
    return -1;
1817
}
1818

1819 1820 1821
/**
 * virDomainGetXMLDesc:
 * @domain: a domain object
1822
 * @flags: an OR'ed set of virDomainXMLFlags
1823 1824 1825 1826 1827 1828 1829 1830
 *
 * 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 *
1831 1832
virDomainGetXMLDesc(virDomainPtr domain, int flags)
{
1833
    virConnectPtr conn;
1834
    DEBUG("domain=%p, flags=%d", domain, flags);
1835

D
Daniel Veillard 已提交
1836
    if (!VIR_IS_DOMAIN(domain)) {
1837
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1838
        return (NULL);
D
Daniel Veillard 已提交
1839
    }
1840

1841 1842 1843 1844 1845
    conn = domain->conn;

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

1846
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1847
    return NULL;
1848
}
1849

1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914
/**
 * virDomainMigrate:
 * @domain: a domain object
 * @dconn: destination host (a connection object)
 * @flags: flags
 * @dname: (optional) rename domain to this at destination
 * @uri: (optional) dest hostname/URI as seen from the source host
 * @bandwidth: (optional) specify migration bandwidth limit in Mbps
 *
 * Migrate the domain object from its current host to the destination
 * host given by dconn (a connection to the destination host).
 *
 * Flags may be one of more of the following:
 *   VIR_MIGRATE_LIVE   Attempt a live migration.
 *
 * If a hypervisor supports renaming domains during migration,
 * then you may set the dname parameter to the new name (otherwise
 * it keeps the same name).  If this is not supported by the
 * hypervisor, dname must be NULL or else you will get an error.
 *
 * Since typically the two hypervisors connect directly to each
 * other in order to perform the migration, you may need to specify
 * a path from the source to the destination.  This is the purpose
 * of the uri parameter.  If uri is NULL, then libvirt will try to
 * find the best method.  Uri may specify the hostname or IP address
 * of the destination host as seen from the source.  Or uri may be
 * a URI giving transport, hostname, user, port, etc. in the usual
 * form.  Refer to driver documentation for the particular URIs
 * supported.
 *
 * The maximum bandwidth (in Mbps) that will be used to do migration
 * can be specified with the bandwidth parameter.  If set to 0,
 * libvirt will choose a suitable default.  Some hypervisors do
 * not support this feature and will return an error if bandwidth
 * is not 0.
 *
 * To see which features are supported by the current hypervisor,
 * see virConnectGetCapabilities, /capabilities/host/migration_features.
 *
 * There are many limitations on migration imposed by the underlying
 * technology - for example it may not be possible to migrate between
 * different processors even with the same architecture, or between
 * different types of hypervisor.
 *
 * Returns the new domain object if the migration was successful,
 *   or NULL in case of error.  Note that the new domain object
 *   exists in the scope of the destination connection (dconn).
 */
virDomainPtr
virDomainMigrate (virDomainPtr domain,
                  virConnectPtr dconn,
                  unsigned long flags,
                  const char *dname,
                  const char *uri,
                  unsigned long bandwidth)
{
    virConnectPtr conn;
    virDomainPtr ddomain = NULL;
    char *uri_out = NULL;
    char *cookie = NULL;
    int cookielen = 0, ret;
    DEBUG("domain=%p, dconn=%p, flags=%lu, dname=%s, uri=%s, bandwidth=%lu",
          domain, dconn, flags, dname, uri, bandwidth);

    if (!VIR_IS_DOMAIN (domain)) {
1915
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975
        return NULL;
    }
    conn = domain->conn;        /* Source connection. */
    if (!VIR_IS_CONNECT (dconn)) {
        virLibConnError (conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return NULL;
    }

    /* Check that migration is supported by both drivers. */
    if (!VIR_DRV_SUPPORTS_FEATURE (conn->driver, conn,
                                   VIR_DRV_FEATURE_MIGRATION_V1) ||
        !VIR_DRV_SUPPORTS_FEATURE (dconn->driver, dconn,
                                   VIR_DRV_FEATURE_MIGRATION_V1)) {
        virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
        return NULL;
    }

    /* Prepare the migration.
     *
     * The destination host may return a cookie, or leave cookie as
     * NULL.
     *
     * The destination host MUST set uri_out if uri_in is NULL.
     *
     * If uri_in is non-NULL, then the destination host may modify
     * the URI by setting uri_out.  If it does not wish to modify
     * the URI, it should leave uri_out as NULL.
     */
    ret = dconn->driver->domainMigratePrepare
        (dconn, &cookie, &cookielen, uri, &uri_out, flags, dname, bandwidth);
    if (ret == -1) goto done;
    if (uri == NULL && uri_out == NULL) {
        virLibConnError (conn, VIR_ERR_INTERNAL_ERROR,
                         "domainMigratePrepare did not set uri");
        goto done;
    }
    if (uri_out) uri = uri_out; /* Did domainMigratePrepare change URI? */

    assert (uri != NULL);

    /* Perform the migration.  The driver isn't supposed to return
     * until the migration is complete.
     */
    ret = conn->driver->domainMigratePerform
        (domain, cookie, cookielen, uri, flags, dname, bandwidth);

    if (ret == -1) goto done;

    /* Get the destination domain and return it or error.
     * 'domain' no longer actually exists at this point (or so we hope), but
     * we still use the object in memory in order to get the name.
     */
    dname = dname ? dname : domain->name;
    if (dconn->driver->domainMigrateFinish)
        ddomain = dconn->driver->domainMigrateFinish
            (dconn, dname, cookie, cookielen, uri, flags);
    else
        ddomain = virDomainLookupByName (dconn, dname);

 done:
1976 1977
    free (uri_out);
    free (cookie);
1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996
    return ddomain;
}

/* Not for public use.  This function is part of the internal
 * implementation of migration in the remote case.
 */
int
__virDomainMigratePrepare (virConnectPtr dconn,
                           char **cookie,
                           int *cookielen,
                           const char *uri_in,
                           char **uri_out,
                           unsigned long flags,
                           const char *dname,
                           unsigned long bandwidth)
{
    DEBUG("dconn=%p, cookie=%p, cookielen=%p, uri_in=%s, uri_out=%p, flags=%lu, dname=%s, bandwidth=%lu", dconn, cookie, cookielen, uri_in, uri_out, flags, dname, bandwidth);

    if (!VIR_IS_CONNECT (dconn)) {
1997
        virLibConnError (NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025
        return -1;
    }

    if (dconn->driver->domainMigratePrepare)
        return dconn->driver->domainMigratePrepare (dconn, cookie, cookielen,
                                                    uri_in, uri_out,
                                                    flags, dname, bandwidth);

    virLibConnError (dconn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
}

/* Not for public use.  This function is part of the internal
 * implementation of migration in the remote case.
 */
int
__virDomainMigratePerform (virDomainPtr domain,
                           const char *cookie,
                           int cookielen,
                           const char *uri,
                           unsigned long flags,
                           const char *dname,
                           unsigned long bandwidth)
{
    virConnectPtr conn;
    DEBUG("domain=%p, cookie=%p, cookielen=%d, uri=%s, flags=%lu, dname=%s, bandwidth=%lu", domain, cookie, cookielen, uri, flags, dname, bandwidth);

    if (!VIR_IS_DOMAIN (domain)) {
2026
        virLibDomainError (NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053
        return -1;
    }
    conn = domain->conn;

    if (conn->driver->domainMigratePerform)
        return conn->driver->domainMigratePerform (domain, cookie, cookielen,
                                                   uri,
                                                   flags, dname, bandwidth);

    virLibDomainError (domain, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
}

/* Not for public use.  This function is part of the internal
 * implementation of migration in the remote case.
 */
virDomainPtr
__virDomainMigrateFinish (virConnectPtr dconn,
                          const char *dname,
                          const char *cookie,
                          int cookielen,
                          const char *uri,
                          unsigned long flags)
{
    DEBUG("dconn=%p, dname=%s, cookie=%p, cookielen=%d, uri=%s, flags=%lu", dconn, dname, cookie, cookielen, uri, flags);

    if (!VIR_IS_CONNECT (dconn)) {
2054
        virLibConnError (NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
        return NULL;
    }

    if (dconn->driver->domainMigrateFinish)
        return dconn->driver->domainMigrateFinish (dconn, dname,
                                                   cookie, cookielen,
                                                   uri, flags);

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


2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
/**
 * 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
2078 2079
virNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info)
{
2080 2081
    DEBUG("conn=%p, info=%p", conn, info);

2082
    if (!VIR_IS_CONNECT(conn)) {
2083
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
2084 2085 2086 2087 2088 2089 2090
        return (-1);
    }
    if (info == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }

2091 2092 2093
    if (conn->driver->nodeGetInfo)
        return conn->driver->nodeGetInfo (conn, info);

2094
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2095
    return -1;
2096
}
2097

2098 2099 2100 2101 2102 2103
/**
 * virConnectGetCapabilities:
 * @conn: pointer to the hypervisor connection
 *
 * Provides capabilities of the hypervisor / driver.
 *
2104 2105
 * Returns NULL in case of error, or an XML string
 * defining the capabilities.
2106 2107 2108 2109 2110
 * The client must free the returned string after use.
 */
char *
virConnectGetCapabilities (virConnectPtr conn)
{
2111 2112
    DEBUG("conn=%p", conn);

2113
    if (!VIR_IS_CONNECT (conn)) {
2114
        virLibConnError (NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
2115 2116 2117
        return NULL;
    }

2118 2119
    if (conn->driver->getCapabilities)
        return conn->driver->getCapabilities (conn);
2120

2121
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2122 2123 2124
    return NULL;
}

2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
/**
 * virNodeGetFreeMemory:
 * @conn: pointer to the hypervisor connection
 * 
 * provides the free memory availble on the Node
 *
 * Returns the available free memory in kilobytes or 0 in case of error
 */
unsigned long long
virNodeGetFreeMemory(virConnectPtr conn)
{
2136 2137
    DEBUG("conn=%p", conn);

2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149
    if (!VIR_IS_CONNECT (conn)) {
        virLibConnError (NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return 0;
    }

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

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

2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163
/**
 * 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;
2164
    DEBUG("domain=%p, nparams=%p", domain, nparams);
2165 2166

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2167
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2168 2169 2170 2171 2172 2173 2174 2175 2176
        return NULL;
    }
    conn = domain->conn;

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

2177
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200
    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;
2201
    DEBUG("domain=%p, params=%p, nparams=%p", domain, params, nparams);
2202 2203

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2204
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2205 2206 2207 2208 2209 2210 2211
        return -1;
    }
    conn = domain->conn;

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

2212
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232
    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;
2233
    DEBUG("domain=%p, params=%p, nparams=%d", domain, params, nparams);
2234 2235

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2236
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2237 2238 2239 2240 2241 2242 2243
        return -1;
    }
    conn = domain->conn;

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

2244
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2245 2246 2247 2248
    return -1;
}


2249
/**
2250
 * virDomainBlockStats:
2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284
 * @dom: pointer to the domain object
 * @path: path to the block device
 * @stats: block device stats (returned)
 * @size: size of stats structure
 *
 * This function returns block device (disk) stats for block
 * devices attached to the domain.
 *
 * The path parameter is the name of the block device.  Get this
 * by calling virDomainGetXMLDesc and finding the <target dev='...'>
 * attribute within //domain/devices/disk.  (For example, "xvda").
 *
 * Domains may have more than one block device.  To get stats for
 * each you should make multiple calls to this function.
 *
 * Individual fields within the stats structure may be returned
 * as -1, which indicates that the hypervisor does not support
 * that particular statistic.
 *
 * Returns: 0 in case of success or -1 in case of failure.
 */
int
virDomainBlockStats (virDomainPtr dom, const char *path,
                     virDomainBlockStatsPtr stats, size_t size)
{
    virConnectPtr conn;
    struct _virDomainBlockStats stats2 = { -1, -1, -1, -1, -1 };
    DEBUG("domain=%p, path=%s, stats=%p, size=%zi", dom, path, stats, size);

    if (!stats || size > sizeof stats2) {
        virLibDomainError (dom, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return -1;
    }
    if (!VIR_IS_CONNECTED_DOMAIN (dom)) {
2285
        virLibDomainError (NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302
        return -1;
    }
    conn = dom->conn;

    if (conn->driver->domainBlockStats) {
        if (conn->driver->domainBlockStats (dom, path, &stats2) == -1)
            return -1;

        memcpy (stats, &stats2, size);
        return 0;
    }

    virLibDomainError (dom, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
}

/**
2303
 * virDomainInterfaceStats:
2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336
 * @dom: pointer to the domain object
 * @path: path to the interface
 * @stats: network interface stats (returned)
 * @size: size of stats structure
 *
 * This function returns network interface stats for interfaces
 * attached to the domain.
 *
 * The path parameter is the name of the network interface.
 *
 * Domains may have more than network interface.  To get stats for
 * each you should make multiple calls to this function.
 *
 * Individual fields within the stats structure may be returned
 * as -1, which indicates that the hypervisor does not support
 * that particular statistic.
 *
 * Returns: 0 in case of success or -1 in case of failure.
 */
int
virDomainInterfaceStats (virDomainPtr dom, const char *path,
                         virDomainInterfaceStatsPtr stats, size_t size)
{
    virConnectPtr conn;
    struct _virDomainInterfaceStats stats2 = { -1, -1, -1, -1,
                                               -1, -1, -1, -1 };
    DEBUG("domain=%p, path=%s, stats=%p, size=%zi", dom, path, stats, size);

    if (!stats || size > sizeof stats2) {
        virLibDomainError (dom, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return -1;
    }
    if (!VIR_IS_CONNECTED_DOMAIN (dom)) {
2337
        virLibDomainError (NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355
        return -1;
    }
    conn = dom->conn;

    if (conn->driver->domainInterfaceStats) {
        if (conn->driver->domainInterfaceStats (dom, path, &stats2) == -1)
            return -1;

        memcpy (stats, &stats2, size);
        return 0;
    }

    virLibDomainError (dom, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
}



2356

2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373
/************************************************************************
 *									*
 *		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) {
2374 2375
    DEBUG("conn=%p, xml=%s", conn, xml);

2376
    if (!VIR_IS_CONNECT(conn)) {
2377
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
2378 2379
        return (NULL);
    }
2380 2381 2382 2383
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (NULL);
    }
2384 2385 2386 2387 2388
    if (xml == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
    }

2389 2390
    if (conn->driver->domainDefineXML)
        return conn->driver->domainDefineXML (conn, xml);
2391

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

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

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2410
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2411 2412
        return (-1);
    }
2413 2414
    conn = domain->conn;
    if (conn->flags & VIR_CONNECT_RO) {
2415 2416 2417 2418
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }

2419 2420
    if (conn->driver->domainUndefine)
        return conn->driver->domainUndefine (domain);
2421

2422
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2423
    return -1;
2424 2425 2426 2427 2428 2429
}

/**
 * virConnectNumOfDefinedDomains:
 * @conn: pointer to the hypervisor connection
 *
2430
 * Provides the number of inactive domains.
2431 2432 2433 2434 2435 2436
 *
 * Returns the number of domain found or -1 in case of error
 */
int
virConnectNumOfDefinedDomains(virConnectPtr conn)
{
2437 2438
    DEBUG("conn=%p", conn);

2439
    if (!VIR_IS_CONNECT(conn)) {
2440
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
2441 2442 2443
        return (-1);
    }

2444 2445
    if (conn->driver->numOfDefinedDomains)
        return conn->driver->numOfDefinedDomains (conn);
2446

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

/**
 * 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
2462
virConnectListDefinedDomains(virConnectPtr conn, char **const names,
2463
                             int maxnames) {
2464 2465
    DEBUG("conn=%p, names=%p, maxnames=%d", conn, names, maxnames);

2466
    if (!VIR_IS_CONNECT(conn)) {
2467
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
2468 2469 2470
        return (-1);
    }

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

2476 2477
    if (conn->driver->listDefinedDomains)
        return conn->driver->listDefinedDomains (conn, names, maxnames);
2478

2479
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2480
    return -1;
2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493
}

/**
 * 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) {
2494
    virConnectPtr conn;
2495
    DEBUG("domain=%p", domain);
2496

2497 2498 2499 2500
    if (domain == NULL) {
        TODO
	return (-1);
    }
2501
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2502
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2503 2504
        return (-1);
    }
2505 2506
    conn = domain->conn;
    if (conn->flags & VIR_CONNECT_RO) {
2507 2508 2509
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
2510

2511 2512 2513
    if (conn->driver->domainCreate)
        return conn->driver->domainCreate (domain);

2514
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2515
    return -1;
2516 2517
}

2518 2519 2520
/**
 * virDomainGetAutostart:
 * @domain: a domain object
2521
 * @autostart: the value returned
2522
 *
2523
 * Provides a boolean value indicating whether the domain
2524 2525 2526 2527 2528 2529 2530
 * 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,
2531 2532 2533
                      int *autostart)
{
    virConnectPtr conn;
2534
    DEBUG("domain=%p, autostart=%p", domain, autostart);
2535 2536

    if (!VIR_IS_DOMAIN(domain)) {
2537
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2538 2539 2540 2541 2542 2543 2544
        return (-1);
    }
    if (!autostart) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }

2545 2546 2547 2548 2549
    conn = domain->conn;

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

2550
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2551
    return -1;
2552 2553 2554 2555 2556
}

/**
 * virDomainSetAutostart:
 * @domain: a domain object
2557
 * @autostart: whether the domain should be automatically started 0 or 1
2558 2559 2560 2561 2562 2563 2564 2565
 *
 * 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,
2566 2567 2568
                      int autostart)
{
    virConnectPtr conn;
2569
    DEBUG("domain=%p, autostart=%d", domain, autostart);
2570 2571

    if (!VIR_IS_DOMAIN(domain)) {
2572
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2573 2574 2575
        return (-1);
    }

2576 2577 2578 2579 2580
    conn = domain->conn;

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

2581
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2582
    return -1;
2583 2584
}

2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600
/**
 * 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)
{
2601
    virConnectPtr conn;
2602
    DEBUG("domain=%p, nvcpus=%u", domain, nvcpus);
2603

2604 2605 2606 2607 2608
    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2609
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2610 2611
        return (-1);
    }
2612 2613 2614 2615
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
2616

2617 2618 2619 2620
    if (nvcpus < 1) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
2621
    conn = domain->conn;
2622

2623 2624
    if (conn->driver->domainSetVcpus)
        return conn->driver->domainSetVcpus (domain, nvcpus);
2625

2626
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2627
    return -1;
2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651
}

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

2655 2656 2657 2658 2659
    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2660
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2661 2662
        return (-1);
    }
2663 2664 2665 2666
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
2667

2668
    if ((vcpu > 32000) || (cpumap == NULL) || (maplen < 1)) {
2669 2670 2671
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
2672

2673 2674
    conn = domain->conn;

2675 2676 2677
    if (conn->driver->domainPinVcpu)
        return conn->driver->domainPinVcpu (domain, vcpu, cpumap, maplen);

2678
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2679
    return -1;
2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706
}

/**
 * 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)
{
2707
    virConnectPtr conn;
2708
    DEBUG("domain=%p, info=%p, maxinfo=%d, cpumaps=%p, maplen=%d", domain, info, maxinfo, cpumaps, maplen);
2709 2710 2711 2712 2713 2714

    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2715
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2716 2717 2718 2719 2720 2721 2722 2723 2724 2725
        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);
    }
2726

2727 2728
    conn = domain->conn;

2729 2730 2731 2732
    if (conn->driver->domainGetVcpus)
        return conn->driver->domainGetVcpus (domain, info, maxinfo,
                                             cpumaps, maplen);

2733
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2734
    return -1;
2735
}
2736

2737 2738 2739 2740
/**
 * virDomainGetMaxVcpus:
 * @domain: pointer to domain object
 * 
2741 2742 2743 2744 2745
 * 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.
2746 2747 2748 2749
 *
 * Returns the maximum of virtual CPU or -1 in case of error.
 */
int
2750 2751
virDomainGetMaxVcpus(virDomainPtr domain)
{
2752
    virConnectPtr conn;
2753
    DEBUG("domain=%p", domain);
2754 2755

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2756
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2757 2758 2759 2760 2761
        return (-1);
    }

    conn = domain->conn;

2762 2763 2764
    if (conn->driver->domainGetMaxVcpus)
        return conn->driver->domainGetMaxVcpus (domain);

2765
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2766
    return -1;
2767 2768 2769
}


2770 2771 2772 2773 2774 2775 2776 2777 2778 2779
/**
 * 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
2780
virDomainAttachDevice(virDomainPtr domain, const char *xml)
2781 2782
{
    virConnectPtr conn;
2783
    DEBUG("domain=%p, xml=%s", domain, xml);
2784 2785

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2786
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2787 2788 2789 2790 2791 2792 2793 2794
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
    conn = domain->conn;

2795 2796 2797
    if (conn->driver->domainAttachDevice)
        return conn->driver->domainAttachDevice (domain, xml);

2798
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2799
    return -1;
2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811
}

/**
 * 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
2812
virDomainDetachDevice(virDomainPtr domain, const char *xml)
2813 2814
{
    virConnectPtr conn;
2815
    DEBUG("domain=%p, xml=%s", domain, xml);
2816 2817

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2818
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2819 2820 2821 2822 2823 2824 2825 2826
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
    conn = domain->conn;

2827 2828 2829
    if (conn->driver->domainDetachDevice)
        return conn->driver->domainDetachDevice (domain, xml);

2830
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2831
    return -1;
2832
}
2833

2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855
/**
 * virNodeGetCellsFreeMemory:
 * @conn: pointer to the hypervisor connection
 * @freeMems: pointer to the array of unsigned long long
 * @startCell: index of first cell to return freeMems info on.
 * @maxCells: Maximum number of cells for which freeMems information can
 *            be returned.
 *
 * This call returns the amount of free memory in one or more NUMA cells.
 * The @freeMems array must be allocated by the caller and will be filled
 * with the amount of free memory in kilobytes for each cell requested,
 * starting with startCell (in freeMems[0]), up to either
 * (startCell + maxCells), or the number of additional cells in the node,
 * whichever is smaller.
 *
 * Returns the number of entries filled in freeMems, or -1 in case of error.
 */

int
virNodeGetCellsFreeMemory(virConnectPtr conn, unsigned long long *freeMems,
                          int startCell, int maxCells)
{
2856 2857 2858
    DEBUG("conn=%p, freeMems=%p, startCell=%d, maxCells=%d",
          conn, freeMems, startCell, maxCells);

2859 2860 2861 2862 2863
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

D
Daniel Veillard 已提交
2864
    if ((freeMems == NULL) || (maxCells <= 0) || (startCell < 0)) {
2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }

    if (conn->driver->nodeGetCellsFreeMemory)
        return conn->driver->nodeGetCellsFreeMemory (conn, freeMems, startCell, maxCells);

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

2876 2877 2878 2879
/**
 * virNetworkGetConnect:
 * @net: pointer to a network
 *
2880
 * Provides the connection pointer associated with a network.  The
2881 2882 2883
 * reference counter on the connection is not increased by this
 * call.
 *
2884 2885 2886 2887
 * WARNING: When writing libvirt bindings in other languages, do
 * not use this function.  Instead, store the connection and
 * the network object together.
 *
2888 2889 2890 2891 2892
 * Returns the virConnectPtr or NULL in case of failure.
 */
virConnectPtr
virNetworkGetConnect (virNetworkPtr net)
{
2893 2894
    DEBUG("net=%p", net);

2895
    if (!VIR_IS_NETWORK (net)) {
2896
        virLibNetworkError (NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
2897 2898 2899 2900 2901
        return NULL;
    }
    return net->conn;
}

2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912
/**
 * 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)
{
2913 2914
    DEBUG("conn=%p", conn);

2915
    if (!VIR_IS_CONNECT(conn)) {
2916
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
2917 2918 2919
        return (-1);
    }

2920 2921
    if (conn->networkDriver && conn->networkDriver->numOfNetworks)
        return conn->networkDriver->numOfNetworks (conn);
2922

2923
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2924
    return -1;
2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937
}

/**
 * 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
2938
virConnectListNetworks(virConnectPtr conn, char **const names, int maxnames)
2939
{
2940 2941
    DEBUG("conn=%p, names=%p, maxnames=%d", conn, names, maxnames);

2942
    if (!VIR_IS_CONNECT(conn)) {
2943
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
2944 2945 2946
        return (-1);
    }

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

2952 2953
    if (conn->networkDriver && conn->networkDriver->listNetworks)
        return conn->networkDriver->listNetworks (conn, names, maxnames);
2954

2955
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2956
    return -1;
2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969
}

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

2972
    if (!VIR_IS_CONNECT(conn)) {
2973
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
2974 2975 2976
        return (-1);
    }

2977 2978
    if (conn->networkDriver && conn->networkDriver->numOfDefinedNetworks)
        return conn->networkDriver->numOfDefinedNetworks (conn);
2979

2980
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2981
    return -1;
2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994
}

/**
 * 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
2995
virConnectListDefinedNetworks(virConnectPtr conn, char **const names,
2996 2997
                              int maxnames)
{
2998 2999
    DEBUG("conn=%p, names=%p, maxnames=%d", conn, names, maxnames);

3000
    if (!VIR_IS_CONNECT(conn)) {
3001
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
3002 3003 3004
        return (-1);
    }

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

3010 3011 3012
    if (conn->networkDriver && conn->networkDriver->listDefinedNetworks)
        return conn->networkDriver->listDefinedNetworks (conn,
                                                         names, maxnames);
3013

3014
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3015
    return -1;
3016 3017 3018 3019 3020 3021 3022 3023 3024
}

/**
 * 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.
 *
3025 3026
 * 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.
3027 3028 3029 3030
 */
virNetworkPtr
virNetworkLookupByName(virConnectPtr conn, const char *name)
{
3031 3032
    DEBUG("conn=%p, name=%s", conn, name);

3033
    if (!VIR_IS_CONNECT(conn)) {
3034
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
3035 3036 3037 3038 3039 3040 3041
        return (NULL);
    }
    if (name == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
    }

3042 3043 3044
    if (conn->networkDriver && conn->networkDriver->networkLookupByName)
        return conn->networkDriver->networkLookupByName (conn, name);

3045
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3046
    return NULL;
3047 3048 3049 3050 3051 3052 3053 3054 3055
}

/**
 * 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.
 *
3056 3057
 * 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.
3058 3059 3060 3061
 */
virNetworkPtr
virNetworkLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
{
3062 3063
    DEBUG("conn=%p, uuid=%s", conn, uuid);

3064
    if (!VIR_IS_CONNECT(conn)) {
3065
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
3066 3067 3068 3069 3070 3071 3072
        return (NULL);
    }
    if (uuid == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
    }

3073 3074
    if (conn->networkDriver && conn->networkDriver->networkLookupByUUID)
        return conn->networkDriver->networkLookupByUUID (conn, uuid);
3075

3076
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3077
    return NULL;
3078 3079 3080 3081 3082 3083 3084 3085 3086
}

/**
 * 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.
 *
3087 3088
 * 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.
3089 3090 3091 3092 3093 3094 3095
 */
virNetworkPtr
virNetworkLookupByUUIDString(virConnectPtr conn, const char *uuidstr)
{
    int raw[VIR_UUID_BUFLEN], i;
    unsigned char uuid[VIR_UUID_BUFLEN];
    int ret;
3096
    DEBUG("conn=%p, uuidstr=%s", conn, uuidstr);
3097 3098

    if (!VIR_IS_CONNECT(conn)) {
3099
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143
        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)
{
3144 3145
    DEBUG("conn=%p, xmlDesc=%s", conn, xmlDesc);

3146
    if (!VIR_IS_CONNECT(conn)) {
3147
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158
        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);
    }

3159 3160 3161
    if (conn->networkDriver && conn->networkDriver->networkCreateXML)
        return conn->networkDriver->networkCreateXML (conn, xmlDesc);

3162
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3163
    return NULL;
3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175
}

/**
 * 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
3176 3177
virNetworkDefineXML(virConnectPtr conn, const char *xml)
{
3178 3179
    DEBUG("conn=%p, xml=%s", conn, xml);

3180
    if (!VIR_IS_CONNECT(conn)) {
3181
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192
        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);
    }

3193 3194
    if (conn->networkDriver && conn->networkDriver->networkDefineXML)
        return conn->networkDriver->networkDefineXML (conn, xml);
3195

3196
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3197
    return NULL;
3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210
}

/**
 * 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;
3211
    DEBUG("network=%p", network);
3212 3213

    if (!VIR_IS_CONNECTED_NETWORK(network)) {
3214
        virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
3215 3216 3217 3218 3219 3220 3221 3222
        return (-1);
    }
    conn = network->conn;
    if (conn->flags & VIR_CONNECT_RO) {
        virLibNetworkError(network, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }

3223 3224
    if (conn->networkDriver && conn->networkDriver->networkUndefine)
        return conn->networkDriver->networkUndefine (network);
3225

3226
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3227
    return -1;
3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239
}

/**
 * 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
3240 3241
virNetworkCreate(virNetworkPtr network)
{
3242
    virConnectPtr conn;
3243 3244
    DEBUG("network=%p", network);

3245 3246 3247 3248 3249
    if (network == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_NETWORK(network)) {
3250
        virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
3251 3252 3253 3254 3255 3256 3257 3258
        return (-1);
    }
    conn = network->conn;
    if (conn->flags & VIR_CONNECT_RO) {
        virLibNetworkError(network, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }

3259 3260 3261
    if (conn->networkDriver && conn->networkDriver->networkCreate)
        return conn->networkDriver->networkCreate (network);

3262
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3263
    return -1;
3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281
}

/**
 * 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;
3282
    DEBUG("network=%p", network);
3283 3284

    if (!VIR_IS_CONNECTED_NETWORK(network)) {
3285
        virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
3286 3287 3288 3289 3290 3291 3292 3293 3294
        return (-1);
    }

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

3295 3296
    if (conn->networkDriver && conn->networkDriver->networkDestroy)
        return conn->networkDriver->networkDestroy (network);
3297

3298
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3299
    return -1;
3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313
}

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

3316
    if (!VIR_IS_NETWORK(network)) {
3317
        virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
3318 3319
        return (-1);
    }
3320
    if (virUnrefNetwork(network) < 0)
3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336
        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)
{
3337 3338
    DEBUG("network=%p", network);

3339
    if (!VIR_IS_NETWORK(network)) {
3340
        virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357
        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)
{
3358 3359
    DEBUG("network=%p, uuid=%p", network, uuid);

3360
    if (!VIR_IS_NETWORK(network)) {
3361
        virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387
        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];
3388
    DEBUG("network=%p, buf=%p", network, buf);
3389 3390

    if (!VIR_IS_NETWORK(network)) {
3391
        virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
3392 3393 3394 3395 3396 3397 3398 3399
        return (-1);
    }
    if (buf == NULL) {
        virLibNetworkError(network, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }

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

3402
    virUUIDFormat(uuid, buf);
3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419
    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)
{
3420
    virConnectPtr conn;
3421
    DEBUG("network=%p, flags=%d", network, flags);
3422

3423
    if (!VIR_IS_NETWORK(network)) {
3424
        virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
3425 3426 3427 3428 3429 3430 3431
        return (NULL);
    }
    if (flags != 0) {
        virLibNetworkError(network, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
    }

3432 3433 3434 3435 3436
    conn = network->conn;

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

3437
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3438
    return NULL;
3439
}
3440 3441 3442 3443 3444

/**
 * virNetworkGetBridgeName:
 * @network: a network object
 *
3445
 * Provides a bridge interface name to which a domain may connect
3446 3447 3448 3449 3450 3451 3452 3453
 * 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)
{
3454
    virConnectPtr conn;
3455
    DEBUG("network=%p", network);
3456

3457
    if (!VIR_IS_NETWORK(network)) {
3458
        virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
3459 3460 3461
        return (NULL);
    }

3462 3463 3464 3465 3466
    conn = network->conn;

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

3467
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3468
    return NULL;
3469
}
3470 3471 3472 3473

/**
 * virNetworkGetAutostart:
 * @network: a network object
3474
 * @autostart: the value returned
3475
 *
3476
 * Provides a boolean value indicating whether the network
3477 3478 3479 3480 3481 3482 3483
 * 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,
3484 3485 3486
                       int *autostart)
{
    virConnectPtr conn;
3487
    DEBUG("network=%p, autostart=%p", network, autostart);
3488 3489

    if (!VIR_IS_NETWORK(network)) {
3490
        virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
3491 3492 3493 3494 3495 3496 3497
        return (-1);
    }
    if (!autostart) {
        virLibNetworkError(network, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }

3498 3499 3500 3501 3502
    conn = network->conn;

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

3503
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3504
    return -1;
3505 3506 3507 3508 3509
}

/**
 * virNetworkSetAutostart:
 * @network: a network object
3510
 * @autostart: whether the network should be automatically started 0 or 1
3511 3512 3513 3514 3515 3516 3517 3518
 *
 * 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,
3519 3520 3521
                       int autostart)
{
    virConnectPtr conn;
3522
    DEBUG("network=%p, autostart=%d", network, autostart);
3523 3524

    if (!VIR_IS_NETWORK(network)) {
3525
        virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
3526 3527 3528
        return (-1);
    }

3529 3530 3531 3532 3533
    conn = network->conn;

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

3534
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3535
    return -1;
3536
}
3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550

/*
 * 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:
 */