xenapi_driver.c 58.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 * xenapi_driver.c: Xen API driver.
 * Copyright (C) 2009, 2010 Citrix Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Sharadha Prabhakar <sharadha.prabhakar@citrix.com>
 */

#include <config.h>

#include <limits.h>
#include <stdint.h>
#include <string.h>
#include <libxml/uri.h>
#include <curl/curl.h>
#include <xen/api/xen_all.h>
#include "internal.h"
#include "domain_conf.h"
#include "virterror_internal.h"
#include "datatypes.h"
34
#include "authhelper.h"
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
#include "util.h"
#include "uuid.h"
#include "memory.h"
#include "buf.h"
#include "xenapi_driver.h"
#include "xenapi_driver_private.h"
#include "xenapi_utils.h"


/*
 * getCapsObject
 *
 * Build the capabilities of the hypervisor
 * Return virCapsPtr on success or NULL on failure
 */
static virCapsPtr
getCapsObject (void)
{
    virCapsPtr caps = virCapabilitiesNew("x86_64", 0, 0);
    if (!caps) {
        virReportOOMError();
        return NULL;
    }
    virCapsGuestPtr guest1 = virCapabilitiesAddGuest(caps, "hvm", "x86_64", 0, "", "", 0, NULL);
    if (!guest1)
        goto error_cleanup;
    virCapsGuestDomainPtr domain1 = virCapabilitiesAddGuestDomain(guest1, "xen", "", "", 0, NULL);
    if (!domain1)
        goto error_cleanup;
    virCapsGuestPtr guest2 = virCapabilitiesAddGuest(caps, "xen", "x86_64", 0, "", "", 0, NULL);
    if (!guest2)
        goto error_cleanup;
    virCapsGuestDomainPtr domain2 = virCapabilitiesAddGuestDomain(guest2, "xen", "", "", 0, NULL);
    if (!domain2)
        goto error_cleanup;

    return caps;

  error_cleanup:
    virCapabilitiesFree(caps);
    return NULL;
}

/*
 * XenapiOpen
 *
 * Authenticates and creates a session with the server
 * Return VIR_DRV_OPEN_SUCCESS on success, else VIR_DRV_OPEN_ERROR
 */
static virDrvOpenStatus
xenapiOpen (virConnectPtr conn, virConnectAuthPtr auth, int flags ATTRIBUTE_UNUSED)
{
87 88 89
    char *username = NULL;
    char *password = NULL;
    struct _xenapiPrivate *privP = NULL;
90

91 92
    if (conn->uri == NULL || conn->uri->scheme == NULL ||
        STRCASENEQ(conn->uri->scheme, "XenAPI")) {
93 94
        return VIR_DRV_OPEN_DECLINED;
    }
95

96
    if (conn->uri->server == NULL) {
97
        xenapiSessionErrorHandler(conn, VIR_ERR_AUTH_FAILED,
98
                                  _("Server name not in URI"));
99 100 101 102 103
        goto error;
    }

    if (auth == NULL) {
        xenapiSessionErrorHandler(conn, VIR_ERR_AUTH_FAILED,
104
                                  _("Authentication Credentials not found"));
105
        goto error;
106
    }
107 108 109 110 111 112 113 114

    if (conn->uri->user != NULL) {
        username = strdup(conn->uri->user);

        if (username == NULL) {
            virReportOOMError();
            goto error;
        }
115
    } else {
116 117 118 119
        username = virRequestUsername(auth, NULL, conn->uri->server);

        if (username == NULL) {
            xenapiSessionErrorHandler(conn, VIR_ERR_AUTH_FAILED,
120
                                      _("Username request failed"));
121 122
            goto error;
        }
123
    }
124 125 126 127 128

    password = virRequestPassword(auth, username, conn->uri->server);

    if (password == NULL) {
        xenapiSessionErrorHandler(conn, VIR_ERR_AUTH_FAILED,
129
                                  _("Password request failed"));
130
        goto error;
131
    }
132

133 134
    if (VIR_ALLOC(privP) < 0) {
        virReportOOMError();
135
        goto error;
136
    }
137

138 139
    if (virAsprintf(&privP->url, "https://%s", conn->uri->server) < 0) {
        virReportOOMError();
140
        goto error;
141
    }
142 143 144 145 146 147

    if (xenapiUtil_ParseQuery(conn, conn->uri, &privP->noVerify) < 0)
        goto error;

    if (!(privP->caps = getCapsObject())) {
        xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
148
                                  _("Capabilities not found"));
149 150 151
        goto error;
    }

152 153 154 155 156
    xmlInitParser();
    xmlKeepBlanksDefault(0);
    xen_init();
    curl_global_init(CURL_GLOBAL_ALL);

157 158
    privP->session = xen_session_login_with_password(call_func, privP, username,
                                                     password, xen_api_latest_version);
159

160
    if (privP->session != NULL && privP->session->ok) {
161
        conn->privateData = privP;
162 163 164 165

        VIR_FREE(username);
        VIR_FREE(password);

166
        return VIR_DRV_OPEN_SUCCESS;
167 168
    }

169
    xenapiSessionErrorHandler(conn, VIR_ERR_AUTH_FAILED, NULL);
170 171 172 173 174 175

  error:
    VIR_FREE(username);
    VIR_FREE(password);

    if (privP != NULL) {
176 177
        virCapabilitiesFree(privP->caps);

178 179 180
        if (privP->session != NULL)
            xenSessionFree(privP->session);

181
        VIR_FREE(privP->url);
182 183
        VIR_FREE(privP);
    }
184 185

    return VIR_DRV_OPEN_ERROR;
186 187 188 189 190 191 192 193 194 195 196
}

/*
 * xenapiClose:
 *
 * Returns 0 on successful session logout
 *
 */
static int
xenapiClose (virConnectPtr conn)
{
197 198 199 200 201 202 203 204 205 206 207 208 209 210
    struct _xenapiPrivate *priv = conn->privateData;

    virCapabilitiesFree(priv->caps);

    if (priv->session != NULL) {
        xen_session_logout(priv->session);
        xenSessionFree(priv->session);
    }

    VIR_FREE(priv->url);
    VIR_FREE(priv);

    conn->privateData = NULL;

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    return 0;
}

/*
 *
 * xenapiSupportsFeature
 *
 * Returns 0
 */
static int
xenapiSupportsFeature (virConnectPtr conn ATTRIBUTE_UNUSED, int feature)
{
    switch (feature) {
    case VIR_DRV_FEATURE_MIGRATION_V2:
    case VIR_DRV_FEATURE_MIGRATION_P2P:
    default:
        return 0;
    }
}

/*
 * xenapiType:
 *
 *
 * Returns name of the driver
 */
static const char *
xenapiType (virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return "XenAPI";
}


/*
 * xenapiGetVersion:
 *
 * Gets the version of XenAPI
 *
 */
static int
xenapiGetVersion (virConnectPtr conn, unsigned long *hvVer)
{
    xen_host host;
    xen_session *session = ((struct _xenapiPrivate *)(conn->privateData))->session;
    xen_string_string_map *result = NULL;
256
    int i, ret = -1;
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    char *version = NULL;
    if (!(xen_session_get_this_host(session, &host, session))) {
        xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR, NULL);
        return -1;
    }
    if (!(xen_host_get_software_version(session, &result, host))) {
        xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR, NULL);
        xen_host_free(host);
        return -1;
    }
    xen_host_free(host);
    if (result && result->size > 0) {
        for (i = 0; i < result->size; i++) {
            if (STREQ(result->contents[i].key, "xen")) {
                if (!(version = strdup(result->contents[i].val))) {
                    xen_string_string_map_free(result);
                    virReportOOMError();
                    return -1;
                }
                break;
            }
        }
        if (version) {
280
            if (virParseVersionString(version, hvVer) < 0)
281
                xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
282 283 284
                                          _("Couldn't parse version info"));
            else
                ret = 0;
285
            xen_string_string_map_free(result);
286 287
            VIR_FREE(version);
            return ret;
288
        }
289 290
        xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                  _("Couldn't get version info"));
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
    }
    return -1;
}


/*
 * xenapiGetHostname:
 *
 *
 * Returns the hostname on success, or NULL on failure
 */
static char *
xenapiGetHostname (virConnectPtr conn)
{
    char *result = NULL;
    xen_host host;
    xen_session *session = ((struct _xenapiPrivate *)(conn->privateData))->session;
    if (!(xen_session_get_this_host(session, &host, session))) {
        xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR, NULL);
        return NULL;
    }
    if (!(xen_host_get_hostname(session, &result, host)))
        xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR, NULL);
    xen_host_free(host);
    return result;
}


/*
 * xenapiGetMaxVcpus:
 *
 *
 * Returns a hardcoded value for Maximum VCPUS
 */
static int
xenapiGetMaxVcpus (virConnectPtr conn ATTRIBUTE_UNUSED, const char *type ATTRIBUTE_UNUSED)
{
    /* this is hardcoded for simplicity and set to a resonable value compared
       to the actual value */
    return 16;
}


/*
 * xenapiNodeGetInfo:
 *
 *
 * Returns Node details on success or else -1
 */
static int
xenapiNodeGetInfo (virConnectPtr conn, virNodeInfoPtr info)
{
    int64_t memory, mhz;
    xen_host_cpu_set *host_cpu_set;
    xen_host_cpu host_cpu;
    xen_host_metrics_set *xen_met_set;
    char *modelname;
    xen_session *session = ((struct _xenapiPrivate *)(conn->privateData))->session;
    info->nodes = 1;
    info->threads = 1;
    info->sockets = 1;

    if (xen_host_metrics_get_all(session, &xen_met_set)) {
        xen_host_metrics_get_memory_total(session, &memory, xen_met_set->contents[0]);
        info->memory = (unsigned long)(memory / 1024);
        xen_host_metrics_set_free(xen_met_set);
    } else {
358 359
        xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                  _("Unable to get host metric Information"));
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
        return -1;
    }
    if (xen_host_cpu_get_all(session, &host_cpu_set)) {
        host_cpu = host_cpu_set->contents[0];
        xen_host_cpu_get_modelname(session, &modelname, host_cpu);
        if (!virStrncpy(info->model, modelname, LIBVIRT_MODELNAME_LEN - 1, LIBVIRT_MODELNAME_LEN)) {
            virReportOOMError();
            xen_host_cpu_set_free(host_cpu_set);
            VIR_FREE(modelname);
            return -1;
        }
        xen_host_cpu_get_speed(session, &mhz, host_cpu);
        info->mhz = (unsigned long)mhz;
        info->cpus = host_cpu_set->size;
        info->cores = host_cpu_set->size;

        xen_host_cpu_set_free(host_cpu_set);
        VIR_FREE(modelname);
        return 0;
    }
380 381
    xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                              _("Unable to get Host CPU set"));
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
    return -1;
}

/*
 * xenapiGetCapabilities:
 *
 *
 * Returns capabilities as an XML string
 */
static char *
xenapiGetCapabilities (virConnectPtr conn)
{
    virCapsPtr caps = ((struct _xenapiPrivate *)(conn->privateData))->caps;
    if (caps) {
        char *xml = virCapabilitiesFormatXML(caps);
        if (!xml) goto cleanup;
        return xml;
    }
  cleanup:
401 402
    xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                              _("Capabilities not available"));
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
    return NULL;
}


/*
 * xenapiListDomains
 *
 * Collects the list of active domains, and store their ID in @maxids
 * Returns the number of domain found or -1 in case of error
 */
static int
xenapiListDomains (virConnectPtr conn, int *ids, int maxids)
{
    /* vm.list */
    xen_host host;
    xen_vm_set *result = NULL;
    int64_t t0;
    int i;
    xen_session *session = ((struct _xenapiPrivate *)(conn->privateData))->session;
    if (xen_session_get_this_host(session, &host, session)) {
        xen_host_get_resident_vms(session, &result, host);
        xen_host_free(host);
    } else
        xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR, NULL);
    if (result != NULL) {
        for (i = 0; (i < (result->size)) && (i < maxids); i++) {
            xen_vm_get_domid(session, &t0, result->contents[i]);
            if (t0 > (int64_t)INT_MAX || t0 < (int64_t)INT_MIN) {
431 432
                xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                          _("DomainID can't fit in 32 bits"));
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
                xen_vm_set_free(result);
                return -1;
            }
            ids[i] = (int)t0;
        }
        xen_vm_set_free(result);
        return i;
    }
    return -1;
}

/*
 * xenapiNumOfDomains
 *
 *
 * Returns the number of domains found or -1 in case of error
 */
static int
xenapiNumOfDomains (virConnectPtr conn)
{
    /* #(vm.list) */
    xen_vm_set *result = NULL;
    xen_host host = NULL;
    int numDomains = -1;
    xen_session *session = ((struct _xenapiPrivate *)(conn->privateData))->session;

    xen_session_get_this_host(session, &host, session);
    if (host != NULL) {
        xen_host_get_resident_vms(session, &result, host);
        if (result != NULL) {
            numDomains = result->size;
            xen_vm_set_free(result);
        }
        xen_host_free(host);
    }
    if (!session->ok)
        xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR, NULL);
    return numDomains;
}

/*
 * xenapiDomainCreateXML
 *
 * Launches a new domain based on the XML description
 * Returns the domain pointer or NULL in case of error
 */
static virDomainPtr
xenapiDomainCreateXML (virConnectPtr conn,
                       const char *xmlDesc,
482
                       unsigned int flags)
483 484 485 486 487 488 489 490 491
{
    xen_vm_record *record = NULL;
    xen_vm vm = NULL;
    virDomainPtr domP = NULL;
    xen_session *session = ((struct _xenapiPrivate *)(conn->privateData))->session;
    virCapsPtr caps = ((struct _xenapiPrivate *)(conn->privateData))->caps;
    if (!caps)
        return NULL;

492 493
    virCheckFlags(0, NULL);

494 495 496 497 498 499 500 501 502 503 504
    virDomainDefPtr defPtr = virDomainDefParseString(caps, xmlDesc, flags);
    createVMRecordFromXml(conn, defPtr, &record, &vm);
    virDomainDefFree(defPtr);
    if (record) {
        unsigned char raw_uuid[VIR_UUID_BUFLEN];
        virUUIDParse(record->uuid, raw_uuid);
        if (vm) {
            if (xen_vm_start(session, vm, false, false)) {
                domP = virGetDomain(conn, record->name_label, raw_uuid);
                if (!domP) {
                    xen_vm_record_free(record);
505 506
                    xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                              _("Domain Pointer is invalid"));
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
                    return domP;
                }
                domP->id = record->domid;
                xen_vm_free(vm);
            }
            else
                xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR, NULL);
        }
        xen_vm_record_free(record);
    }
    else
        xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR, NULL);
    return domP;
}

/*
 * xenapiDomainLookupByID
 *
 *
 * Returns a valid domain pointer of the domain with ID same as the one passed
 * or NULL in case of error
 */
static virDomainPtr
xenapiDomainLookupByID (virConnectPtr conn, int id)
{
    int i;
    int64_t domID;
    char *uuid;
    xen_host host;
    xen_vm_set *result;
    xen_vm_record *record;
    unsigned char raw_uuid[VIR_UUID_BUFLEN];
    virDomainPtr domP=NULL;
    xen_session *session = ((struct _xenapiPrivate *)(conn->privateData))->session;

    xen_session_get_this_host(session, &host, session);
    if (host != NULL && session->ok) {
        xen_host_get_resident_vms(session, &result, host);
        if (result != NULL ) {
            for (i = 0; i < result->size; i++) {
                xen_vm_get_domid(session, &domID, result->contents[i]);
                if (domID == id) {
                    xen_vm_get_record(session, &record, result->contents[i]);
                    xen_vm_get_uuid(session, &uuid, result->contents[i]);
                    virUUIDParse(uuid, raw_uuid);
                    domP = virGetDomain(conn, record->name_label, raw_uuid);
                    if (domP) {
                        int64_t domid = -1;
                        xen_vm_get_domid(session, &domid, result->contents[i]);
                        domP->id = domid;
                    } else {
558 559
                        xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                                  _("Domain Pointer not valid"));
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
                        domP = NULL;
                    }
                    xen_uuid_free(uuid);
                    xen_vm_record_free(record);
                    break;
                }
            }
            xen_vm_set_free(result);
        } else {
            xenapiSessionErrorHandler(conn, VIR_ERR_NO_DOMAIN, NULL);
        }
        xen_host_free(host);
    } else {
        xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR, NULL);
    }
    return domP;
}

/*
 * xenapiDomainLookupByUUID
 *
 * Returns the domain pointer of domain with matching UUID
 * or -1 in case of error
 */
static virDomainPtr
xenapiDomainLookupByUUID (virConnectPtr conn,
                          const unsigned char *uuid)
{
    /* vm.get_by_uuid */
    xen_vm vm;
    xen_vm_record *record;
    char uuidStr[VIR_UUID_STRING_BUFLEN];
    virDomainPtr domP = NULL;
    xen_session *session = ((struct _xenapiPrivate *)(conn->privateData))->session;
    virUUIDFormat(uuid,uuidStr);
    if (xen_vm_get_by_uuid(session, &vm, uuidStr)) {
        xen_vm_get_record(session, &record, vm);
        if (record != NULL) {
            domP = virGetDomain(conn, record->name_label, uuid);
            if (!domP) {
600 601
                xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                          _("Domain Pointer not valid"));
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
                domP = NULL;
            } else {
                domP->id = record->domid;
            }
            xen_vm_record_free(record);
        }
        else
            xenapiSessionErrorHandler(conn, VIR_ERR_NO_DOMAIN, NULL);
        xen_vm_free(vm);
    } else
        xenapiSessionErrorHandler(conn, VIR_ERR_NO_DOMAIN, NULL);
    return domP;
}

/*
 * xenapiDomainLookupByName
 *
 * Returns the domain pointer of domain with matching name
 * or -1 in case of error
 */
static virDomainPtr
xenapiDomainLookupByName (virConnectPtr conn,
                          const char *name)
{
    /* vm.get_by_name_label */
    xen_vm_set *vms = NULL;
    xen_vm vm;
    char *uuid = NULL;
    unsigned char raw_uuid[VIR_UUID_BUFLEN];
    virDomainPtr domP = NULL;
    xen_session *session = ((struct _xenapiPrivate *)(conn->privateData))->session;
    if (xen_vm_get_by_name_label(session, &vms, (char *)name) && vms->size > 0) {
        if (vms->size != 1) {
635 636
            xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
            xen_vm_set_free(vms);
            return NULL;
        }
        vm = vms->contents[0];
        xen_vm_get_uuid(session, &uuid, vm);
        if (uuid!=NULL) {
            virUUIDParse(uuid, raw_uuid);
            domP = virGetDomain(conn, name, raw_uuid);
            if (domP != NULL) {
                int64_t domid = -1;
                xen_vm_get_domid(session, &domid, vm);
                domP->id = domid;
                xen_uuid_free(uuid);
                xen_vm_set_free(vms);
                return domP;
            } else {
                xen_uuid_free(uuid);
                xen_vm_set_free(vms);
            if (!session->ok)
656 657
                xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                          _("Couldn't get the Domain Pointer"));
658 659 660 661 662
                return NULL;
            }
        }
    }
    if (vms) xen_vm_set_free(vms);
663
    xenapiSessionErrorHandler(conn, VIR_ERR_NO_DOMAIN, NULL);
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
    return NULL;
}

/*
 * xenapiDomainSuspend
 *
 * a VM is paused
 * Returns 0 on success or -1 in case of error
 */
static int
xenapiDomainSuspend (virDomainPtr dom)
{
    /* vm.pause() */
    xen_vm vm;
    xen_vm_set *vms=NULL;
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;
    if (xen_vm_get_by_name_label(session, &vms, dom->name) &&  vms->size > 0) {
        if (vms->size != 1) {
682 683
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
684 685 686 687 688 689 690 691 692 693 694 695 696 697
            xen_vm_set_free(vms);
            return -1;
        } else {
            vm = vms->contents[0];
            if (!xen_vm_pause(session, vm)) {
                xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR, NULL);
                xen_vm_set_free(vms);
                return -1;
            }
            xen_vm_set_free(vms);
            return 0;
        }
    }
    if (vms) xen_vm_set_free(vms);
698
    xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
    return -1;
}

/*
 * xenapiDomainResume
 *
 * Resumes a VM
 * Returns 0 on success or -1 in case of error
 */
static int
xenapiDomainResume (virDomainPtr dom)
{
    /* vm.unpause() */
    xen_vm vm;
    xen_vm_set *vms;
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;
    if (xen_vm_get_by_name_label(session, &vms, dom->name) && vms->size > 0) {
        if (vms->size != 1) {
717 718
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
719 720 721 722 723 724 725 726 727 728 729 730 731 732
            xen_vm_set_free(vms);
            return -1;
        } else {
            vm = vms->contents[0];
            if (!xen_vm_unpause(session, vm)) {
                xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR, NULL);
                xen_vm_set_free(vms);
                return -1;
            }
            xen_vm_set_free(vms);
            return 0;
        }
    }
    if (vms) xen_vm_set_free(vms);
733
    xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
    return -1;
}

/*
 * xenapiDomainShutdown
 *
 * shutsdown a VM
 * Returns 0 on success or -1 in case of error
 */
static int
xenapiDomainShutdown (virDomainPtr dom)
{
    /* vm.clean_shutdown */
    xen_vm vm;
    xen_vm_set *vms;
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;
    if (xen_vm_get_by_name_label(session, &vms, dom->name) && vms->size > 0) {
        if (vms->size != 1) {
752 753
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
754 755 756 757 758 759 760 761 762 763 764 765 766 767
            xen_vm_set_free(vms);
            return -1;
        } else {
            vm = vms->contents[0];
            if (!xen_vm_clean_shutdown(session, vm)) {
                xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR, NULL);
                xen_vm_set_free(vms);
                return -1;
            }
            xen_vm_set_free(vms);
            return 0;
        }
    }
    if (vms) xen_vm_set_free(vms);
768
    xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
    return -1;
}

/*
 * xenapiDomainReboot
 *
 * Reboots a VM
 * Returns 0 on success or -1 in case of error
 */
static int
xenapiDomainReboot (virDomainPtr dom, unsigned int flags ATTRIBUTE_UNUSED)
{
    /* vm.clean_reboot */
    xen_vm vm;
    struct xen_vm_set *vms;
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;
    if (xen_vm_get_by_name_label(session, &vms, dom->name) && vms->size > 0) {
        if (vms->size != 1) {
787 788
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
789 790 791 792 793 794 795 796 797 798 799 800 801
            xen_vm_set_free(vms);
            return -1;
        }
        vm = vms->contents[0];
        if (!xen_vm_clean_reboot(session, vm)) {
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR, NULL);
            xen_vm_set_free(vms);
            return -1;
        }
        xen_vm_set_free(vms);
        return 0;
    }
    if (vms) xen_vm_set_free(vms);
802
    xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
    return -1;
}

/*
 * xenapiDomaindestroy
 *
 * A VM is hard shutdown
 * Returns 0 on success or -1 in case of error
 */
static int
xenapiDomainDestroy (virDomainPtr dom)
{
    /* vm.hard_shutdown */
    xen_vm vm;
    struct xen_vm_set *vms;
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;
    if (xen_vm_get_by_name_label(session, &vms, dom->name) && vms->size > 0) {
        if (vms->size != 1) {
821 822
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
823 824 825 826 827 828 829 830 831 832 833 834 835
            xen_vm_set_free(vms);
            return -1;
        }
        vm = vms->contents[0];
        if (!xen_vm_hard_shutdown(session, vm)) {
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR, NULL);
            xen_vm_set_free(vms);
            return -1;
        }
        xen_vm_set_free(vms);
        return 0;
    }
    if (vms) xen_vm_set_free(vms);
836
    xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
    return -1;
}

/*
 * xenapiDomainGetOSType
 *
 *
 * Returns OS version on success or NULL in case of error
 */
static char *
xenapiDomainGetOSType (virDomainPtr dom)
{
    xen_vm vm=NULL;
    xen_vm_set *vms;
    char *ostype = NULL;
    char *boot_policy=NULL;
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;

    if (xen_vm_get_by_name_label(session, &vms, dom->name) && vms->size > 0) {
        if (vms->size != 1) {
857 858
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
859 860 861 862 863 864 865 866 867 868 869 870
            xen_vm_set_free(vms);
            return NULL;
        }
        vm = vms->contents[0];
        if (!xen_vm_get_hvm_boot_policy(session, &boot_policy, vm)) {
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR, NULL);
            goto cleanup;
        }
        if (!(ostype = (STREQ(boot_policy,"BIOS order") ? strdup("hvm") : strdup("xen"))))
            virReportOOMError();
        VIR_FREE(boot_policy);
    } else
871
        xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891

  cleanup:
    if (vms) xen_vm_set_free(vms);
    return ostype;
}
/*
 * xenapiDomainGetMaxMemory
 *
 * Returns maximum static memory for VM on success
 * or 0 in case of error
 */
static unsigned long
xenapiDomainGetMaxMemory (virDomainPtr dom)
{
    int64_t mem_static_max = 0;
    xen_vm vm;
    xen_vm_set *vms;
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;
    if (xen_vm_get_by_name_label(session, &vms, dom->name) && vms->size > 0) {
        if (vms->size != 1) {
892 893
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
            xen_vm_set_free(vms);
            return 0;
        }
        vm = vms->contents[0];
        xen_vm_get_memory_static_max(session, &mem_static_max, vm);
        xen_vm_set_free(vms);
        return (unsigned long)(mem_static_max / 1024);
    } else {
        if (vms) xen_vm_set_free(vms);
        xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
        return 0;
    }
}

/*
 * xenapiDomainSetMaxMemory
 *
 * Sets maximum static memory for VM on success
 * Returns 0 on success or -1 in case of error
 */
static int
xenapiDomainSetMaxMemory (virDomainPtr dom, unsigned long memory)
{
    /* vm.set_memory_static_max */
    xen_vm vm;
    struct xen_vm_set *vms;
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;
    if (xen_vm_get_by_name_label(session, &vms, dom->name) && vms->size > 0) {
        if (vms->size != 1) {
923 924
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
            xen_vm_set_free(vms);
            return -1;
        }
        vm = vms->contents[0];
        if (!(xen_vm_set_memory_static_max(session, vm, memory * 1024))) {
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR, NULL);
            xen_vm_set_free(vms);
            return -1;
        }
        xen_vm_set_free(vms);
    } else {
        if (vms) xen_vm_set_free(vms);
        xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
        return -1;
    }
    return 0;
}

/*
 * xenapiDomainGetInfo:
 *
 * Fills a structure with domain information
 * Returns 0 on success or -1 in case of error
 */
static int
xenapiDomainGetInfo (virDomainPtr dom, virDomainInfoPtr info)
{
    int64_t maxmem = 0, memory = 0, vcpu = 0;
    xen_vm vm;
    xen_vm_record *record;
    xen_vm_set *vms;
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;
    info->cpuTime = 0; /* CPU time is not advertised */
    if (xen_vm_get_by_name_label(session, &vms, dom->name) && vms->size > 0) {
        if (vms->size != 1) {
960 961
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
            xen_vm_set_free(vms);
            return -1;
        }
        vm = vms->contents[0];
        xen_vm_get_memory_static_max(session, &maxmem, vm);
        info->maxMem = (maxmem / 1024);
        enum xen_vm_power_state state = XEN_VM_POWER_STATE_UNKNOWN;
        xen_vm_get_power_state(session, &state, vm);
        info->state = mapPowerState(state);
        xen_vm_get_record(session, &record, vm);
        if (record != NULL) {
            xen_vm_metrics_get_memory_actual(session, &memory, record->metrics->u.handle);
            info->memory = (memory / 1024);
            xen_vm_record_free(record);
        }
        xen_vm_get_vcpus_max(session, &vcpu, vm);
        info->nrVirtCpu = vcpu;
        xen_vm_set_free(vms);
        return 0;
    }
    if (vms) xen_vm_set_free(vms);
    xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
    return -1;
}


/*
 * xenapiDomainSetVcpus
 *
 * Sets the VCPUs on the domain
 * Return 0 on success or -1 in case of error
 */
static int
xenapiDomainSetVcpus (virDomainPtr dom, unsigned int nvcpus)
{

    /* vm.set_vcpus_max */
    xen_vm vm;
    xen_vm_set *vms;
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;
    if (xen_vm_get_by_name_label(session, &vms, dom->name) && vms->size > 0) {
        if (vms->size != 1) {
1004 1005
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
            xen_vm_set_free(vms);
            return -1;
        }
        vm = vms->contents[0];
        if (xen_vm_set_vcpus_number_live(session, vm, (int64_t)nvcpus)) {
            xen_vm_set_free(vms);
            return 0;
        }
    }
    if (vms) xen_vm_set_free(vms);
    xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
    return -1;
}

/*
 * xenapiDomainPinVcpu
 *
 * Dynamically change the real CPUs which can be allocated to a virtual CPU
 * Returns 0 on success or -1 in case of error
 */
static int
xenapiDomainPinVcpu (virDomainPtr dom, unsigned int vcpu ATTRIBUTE_UNUSED,
                     unsigned char *cpumap, int maplen)
{
    char *value = NULL;
    xen_vm vm;
    xen_vm_set *vms;
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;
    if (xen_vm_get_by_name_label(session, &vms, dom->name) && vms->size > 0) {
        if (vms->size != 1) {
1036 1037
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
            xen_vm_set_free(vms);
            return -1;
        }
        vm = vms->contents[0];
        if ((value = mapDomainPinVcpu(cpumap, maplen))) {
            xen_vm_remove_from_vcpus_params(session, vm, (char *)"mask");
            if (xen_vm_add_to_vcpus_params(session, vm, (char *)"mask", value)) {
                xen_vm_set_free(vms);
                VIR_FREE(value);
                return 0;
            }
            VIR_FREE(value);
        } else {
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR, NULL);
            xen_vm_set_free(vms);
            return -1;
        }
    }
    if (vms) xen_vm_set_free(vms);
    xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR, NULL);
    return -1;
}

/*
 * xenapiDomainGetVcpus
 *
 * Gets Vcpu information
 * Return number of structures filled on success or -1 in case of error
 */
static int
xenapiDomainGetVcpus (virDomainPtr dom,
                      virVcpuInfoPtr info, int maxinfo,
                      unsigned char *cpumaps, int maplen)
{

    xen_vm_set *vms = NULL;
    xen_vm vm = NULL;
    xen_string_string_map *vcpu_params = NULL;
    int nvcpus = 0, cpus = 0, i;
    virDomainInfo domInfo;
    virNodeInfo nodeInfo;
    virVcpuInfoPtr ifptr;
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;
    char *mask = NULL;
    if (cpumaps != NULL && maplen < 1)
        return -1;
    if (xenapiDomainGetInfo(dom, &domInfo) == 0) {
        nvcpus = domInfo.nrVirtCpu;
    } else {
1087 1088
        xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                  _("Couldn't fetch Domain Information"));
1089 1090 1091 1092 1093
        return -1;
    }
    if (xenapiNodeGetInfo(dom->conn, &nodeInfo) == 0)
        cpus = nodeInfo.cpus;
    else {
1094 1095
        xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                  _("Couldn't fetch Node Information"));
1096 1097 1098 1099 1100 1101 1102 1103 1104
        return -1;
    }
    if (nvcpus > maxinfo)
        nvcpus = maxinfo;
    if (cpumaps != NULL)
        memset(cpumaps, 0, maxinfo * maplen);
    if (!xen_vm_get_by_name_label(session, &vms, dom->name))
        return -1;
    if (vms->size != 1) {
1105 1106
        xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                  _("Domain name is not unique"));
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
        xen_vm_set_free(vms);
        return -1;
    }
    vm = vms->contents[0];
    if (!xen_vm_get_vcpus_params(session, &vcpu_params, vm)) {
        xen_vm_set_free(vms);
        xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR, NULL);
        return -1;
    }
    for (i = 0; i < vcpu_params->size; i++) {
        if (STREQ(vcpu_params->contents[i].key, "mask")) {
            if (!(mask = strdup(vcpu_params->contents[i].val))){
                 xen_vm_set_free(vms);
                 xen_string_string_map_free(vcpu_params);
                 virReportOOMError();
                 return -1;
            }
            break;
         }
    }
    xen_string_string_map_free(vcpu_params);
    for (i = 0, ifptr = info; i < nvcpus; i++, ifptr++) {
        ifptr->number = i;
        ifptr->state = VIR_VCPU_RUNNING;
        ifptr->cpuTime = 0;
        ifptr->cpu = 0;
        if (mask != NULL)
            getCpuBitMapfromString(mask, VIR_GET_CPUMAP(cpumaps, maplen, i), maplen);
    }
    VIR_FREE(mask);
    xen_vm_set_free(vms);
    return i;
}

/*
 * xenapiDomainGetMaxVcpus
 *
 *
 * Returns maximum number of Vcpus on success or -1 in case of error
 */
static int
xenapiDomainGetMaxVcpus (virDomainPtr dom)
{
    xen_vm vm;
    xen_vm_set *vms;
    int64_t maxvcpu = 0;
    enum xen_vm_power_state state;
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;
    if (xen_vm_get_by_name_label(session, &vms, dom->name) && vms->size > 0) {
        if (vms->size != 1) {
1157 1158
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
            xen_vm_set_free(vms);
            return -1;
        }
        vm = vms->contents[0];
        xen_vm_get_power_state(session, &state, vm);
        if (state == XEN_VM_POWER_STATE_RUNNING) {
            xen_vm_get_vcpus_max(session, &maxvcpu, vm);
        } else {
            maxvcpu = xenapiGetMaxVcpus(dom->conn, NULL);
        }
        xen_vm_set_free(vms);
        return (int)maxvcpu;
    }
    if (vms) xen_vm_set_free(vms);
    xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR, NULL);
    return -1;
}

/*
 * xenapiDomainDumpXML
 *
 *
 * Returns XML string of the domain configuration on success or -1 in case of error
 */
static char *
xenapiDomainDumpXML (virDomainPtr dom, int flags ATTRIBUTE_UNUSED)
{
    xen_vm vm=NULL;
    xen_vm_set *vms;
    xen_string_string_map *result=NULL;
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;
    virDomainDefPtr defPtr = NULL;

    if (!xen_vm_get_by_name_label(session, &vms, dom->name)) return NULL;
    if (vms->size != 1) {
1194 1195
        xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                  _("Domain name is not unique"));
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
        xen_vm_set_free(vms);
        return NULL;
    }
    if (VIR_ALLOC(defPtr) < 0) {
        virReportOOMError();
        xen_vm_set_free(vms);
        return NULL;
    }
    vm = vms->contents[0];
    defPtr->virtType = VIR_DOMAIN_VIRT_XEN;
    defPtr->id = dom->id;
    memcpy(defPtr->uuid, dom->uuid, VIR_UUID_BUFLEN);
    if (!(defPtr->name = strdup(dom->name)))
        goto error_cleanup;
    char *boot_policy = NULL;
    xen_vm_get_hvm_boot_policy(session, &boot_policy, vm);
    if (STREQ(boot_policy,"BIOS order")) {
        if (!(defPtr->os.type = strdup("hvm"))) {
            VIR_FREE(boot_policy);
            goto error_cleanup;
        }
        xen_vm_get_hvm_boot_params(session, &result, vm);
        if (result != NULL) {
            int i;
            for (i = 0; i < result->size; i++) {
                if (STREQ(result->contents[i].key, "order")) {
                    int cnt = 0;
                    while(result->contents[i].val[cnt] != '\0') {
                        defPtr->os.bootDevs[cnt] = map2LibvirtBootOrder(result->contents[i].val[cnt]);
                        cnt++;
                    }
                    defPtr->os.nBootDevs = cnt;
                    break;
                }
            }
            xen_string_string_map_free(result);
        }
        VIR_FREE(boot_policy);
    } else {
        char *value = NULL;
        if (!(defPtr->os.type = strdup("xen"))) {
            VIR_FREE(boot_policy);
            goto error_cleanup;
        }
        if (!(defPtr->os.loader = strdup("pygrub"))) {
            VIR_FREE(boot_policy);
            goto error_cleanup;
        }
        xen_vm_get_pv_kernel(session, &value, vm);
        if (STRNEQ(value, "")) {
            if (!(defPtr->os.kernel = strdup(value))) {
                VIR_FREE(boot_policy);
                VIR_FREE(value);
                goto error_cleanup;
            }
            VIR_FREE(value);
        }
        xen_vm_get_pv_ramdisk(session, &value, vm);
        if (STRNEQ(value, "")) {
            if (!(defPtr->os.initrd = strdup(value))) {
                VIR_FREE(boot_policy);
                VIR_FREE(value);
                goto error_cleanup;
            }
            VIR_FREE(value);
        }
        xen_vm_get_pv_args(session, &value, vm);
        if (STRNEQ(value, "")) {
            if(!(defPtr->os.cmdline = strdup(value))) {
                VIR_FREE(boot_policy);
                VIR_FREE(value);
                goto error_cleanup;
            }
            VIR_FREE(value);
        }
        VIR_FREE(boot_policy);
        if (!(defPtr->os.bootloader = strdup("pygrub")))
            goto error_cleanup;
    }
    char *val = NULL;
    xen_vm_get_pv_bootloader_args(session, &val, vm);
    if (STRNEQ(val, "")) {
        if (!(defPtr->os.bootloaderArgs = strdup(val))) {
            VIR_FREE(val);
            goto error_cleanup;
        }
        VIR_FREE(val);
    }
    unsigned long memory=0;
    memory = xenapiDomainGetMaxMemory(dom);
    defPtr->maxmem = memory;
    int64_t dynamic_mem=0;
    if (xen_vm_get_memory_dynamic_max(session, &dynamic_mem, vm)) {
        defPtr->memory = (unsigned long) (dynamic_mem / 1024);
    } else {
        defPtr->memory = memory;
    }
    defPtr->vcpus = xenapiDomainGetMaxVcpus(dom);
    enum xen_on_normal_exit action;
    if (xen_vm_get_actions_after_shutdown(session, &action, vm)) {
        defPtr->onPoweroff = xenapiNormalExitEnum2virDomainLifecycle(action);
    }
    if (xen_vm_get_actions_after_reboot(session, &action, vm)) {
        defPtr->onReboot = xenapiNormalExitEnum2virDomainLifecycle(action);
    }
    enum xen_on_crash_behaviour crash;
    if (xen_vm_get_actions_after_crash(session, &crash, vm)) {
        defPtr->onCrash = xenapiCrashExitEnum2virDomainLifecycle(action);
    }
    xen_vm_get_platform(session, &result, vm);
    if (result != NULL) {
        int i;
        for(i = 0; i < result->size; i++) {
            if (STREQ(result->contents[i].val, "true")) {
                if (STREQ(result->contents[i].key, "acpi"))
                    defPtr->features = defPtr->features | (1<<VIR_DOMAIN_FEATURE_ACPI);
                else if (STREQ(result->contents[i].key, "apic"))
                    defPtr->features = defPtr->features | (1<<VIR_DOMAIN_FEATURE_APIC);
                else if (STREQ(result->contents[i].key, "pae"))
                    defPtr->features = defPtr->features | (1<<VIR_DOMAIN_FEATURE_PAE);
            }
        }
        xen_string_string_map_free(result);
    }
    struct xen_vif_set *vif_set = NULL;
    xen_vm_get_vifs(session, &vif_set, vm);
    if (vif_set) {
        int i;
        xen_vif vif;
        xen_vif_record *vif_rec = NULL;
        xen_network network;
        char *bridge = NULL;
        defPtr->nnets = vif_set->size;
        if (VIR_ALLOC_N(defPtr->nets, vif_set->size) < 0) {
            xen_vif_set_free(vif_set);
            goto error_cleanup;
        }
        for (i = 0; i < vif_set->size; i++) {
            if (VIR_ALLOC(defPtr->nets[i]) < 0) {
                xen_vif_set_free(vif_set);
                goto error_cleanup;
            }
            defPtr->nets[i]->type = VIR_DOMAIN_NET_TYPE_BRIDGE;
            vif = vif_set->contents[i];
            xen_vif_get_network(session, &network, vif);
            if (network != NULL) {
                xen_network_get_bridge(session, &bridge, network);
                if (bridge != NULL)
                    defPtr->nets[i]->data.bridge.brname = bridge;
                xen_network_free(network);
            }
            xen_vif_get_record(session, &vif_rec, vif);
            if (vif_rec != NULL) {
                if (virParseMacAddr((const char *)vif_rec->mac,defPtr->nets[i]->mac) < 0)
                    xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
1351
                                              _("Unable to parse given mac address"));
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
                xen_vif_record_free(vif_rec);
            }
        }
        xen_vif_set_free(vif_set);
    }
    if (vms) xen_vm_set_free(vms);
    char *xml = virDomainDefFormat(defPtr, 0);
    virDomainDefFree(defPtr);
    return xml;

  error_cleanup:
    virReportOOMError();
    xen_vm_set_free(vms);
    virDomainDefFree(defPtr);
    return NULL;

}

/*
 * xenapiListDefinedDomains
 *
 * list the defined but inactive domains, stores the pointers to the names in @names
 * Returns number of names provided in the array or -1 in case of error
 */
static int
xenapiListDefinedDomains (virConnectPtr conn, char **const names,
                          int maxnames)
{
    int i,j=0,doms;
    xen_vm_set *result;
    xen_vm_record *record;
    xen_session *session = ((struct _xenapiPrivate *)(conn->privateData))->session;
    xen_vm_get_all(session, &result);
    if (result != NULL) {
        for (i = 0; i < result->size && j < maxnames; i++) {
            xen_vm_get_record(session, &record, result->contents[i]);
            if (record != NULL) {
                if (record->is_a_template == 0) {
                    char *usenames = NULL;
                    if (!(usenames = strdup(record->name_label))) {
                        virReportOOMError();
                        xen_vm_record_free(record);
                        xen_vm_set_free(result);
                        while (--j >= 0) VIR_FREE(names[j]);
                        return -1;
                    }
                    names[j++] = usenames;
                }
                xen_vm_record_free(record);
            } else {
1402 1403
                xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                          _("Couldn't get VM record"));
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 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
                xen_vm_set_free(result);
                while (--j >= 0) VIR_FREE(names[j]);
                   return -1;
            }
        }
        doms = j;
        xen_vm_set_free(result);
        return doms;
    }
    xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR, NULL);
    return -1;
}

/*
 * xenapiNumOfDefinedDomains
 *
 * Provides the number of defined but inactive domains
 * Returns number of domains found on success or -1 in case of error
 */
static int
xenapiNumOfDefinedDomains (virConnectPtr conn)
{
    xen_vm_set *result;
    xen_vm_record *record;
    int DomNum = 0, i;
    xen_session *session = ((struct _xenapiPrivate *)(conn->privateData))->session;
    xen_vm_get_all(session, &result);
    if (result != NULL) {
        for (i = 0; i < result->size; i++) {
            xen_vm_get_record(session, &record, result->contents[i]);
            if (record == NULL && !session->ok) {
                xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR, NULL);
                xen_vm_set_free(result);
                return -1;
            }
            if (record->is_a_template == 0)
                DomNum++;
            xen_vm_record_free(record);
        }
        xen_vm_set_free(result);
        return DomNum;
    }
    xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR, NULL);
    return -1;
}

/*
1451
 * xenapiDomainCreateWithFlags
1452 1453 1454 1455 1456
 *
 * starts a VM
 * Return 0 on success or -1 in case of error
 */
static int
1457
xenapiDomainCreateWithFlags (virDomainPtr dom, unsigned int flags)
1458 1459 1460 1461
{
    xen_vm_set *vms;
    xen_vm vm;
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;
1462 1463 1464

    virCheckFlags(0, -1);

1465 1466
    if (xen_vm_get_by_name_label(session, &vms, dom->name) && vms->size > 0) {
        if (vms->size != 1) {
1467 1468
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
            xen_vm_set_free(vms);
            return -1;
        }
        vm = vms->contents[0];
        if (!xen_vm_start(session, vm, false, false)) {
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR, NULL);
            xen_vm_set_free(vms);
            return -1;
        }
        xen_vm_set_free(vms);
    } else {
        if (vms) xen_vm_set_free(vms);
        xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
        return -1;
    }
    return 0;
}

1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
/*
 * xenapiDomainCreate
 *
 * starts a VM
 * Return 0 on success or -1 in case of error
 */
static int
xenapiDomainCreate (virDomainPtr dom)
{
    return xenapiDomainCreateWithFlags(dom, 0);
}

1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
/*
 * xenapiDomainDefineXML
 *
 * Defines a domain from the given XML but does not start it
 * Returns 0 on success or -1 in case of error
 */
static virDomainPtr
xenapiDomainDefineXML (virConnectPtr conn, const char *xml)
{
    xen_vm_record *record=NULL;
    xen_vm vm=NULL;
    virDomainPtr domP=NULL;
    xen_session *session = ((struct _xenapiPrivate *)(conn->privateData))->session;
    virCapsPtr caps = ((struct _xenapiPrivate *)(conn->privateData))->caps;
    if (!caps)
        return NULL;
    virDomainDefPtr defPtr = virDomainDefParseString(caps, xml, 0);
    if (!defPtr)
        return NULL;
    if (createVMRecordFromXml(conn, defPtr, &record, &vm) != 0) {
        if (!session->ok)
            xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR, NULL);
        else
1522 1523
            xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Couldn't get VM information from XML"));
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
        virDomainDefFree(defPtr);
        return NULL;
    }
    if (record != NULL) {
        unsigned char raw_uuid[VIR_UUID_BUFLEN];
        virUUIDParse(record->uuid, raw_uuid);
        domP = virGetDomain(conn, record->name_label, raw_uuid);
        if (!domP && !session->ok)
            xenapiSessionErrorHandler(conn, VIR_ERR_NO_DOMAIN, NULL);
        xen_vm_record_free(record);
    }
    else if (vm != NULL)
        xen_vm_free(vm);
    virDomainDefFree(defPtr);
    return domP;
}

/*
 * xenapiDomainUndefine
 *
 * destroys a domain
 * Return 0 on success or -1 in case of error
 */
static int
xenapiDomainUndefine (virDomainPtr dom)
{
    struct xen_vm_set *vms;
    xen_vm vm;
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;
    if (xen_vm_get_by_name_label(session, &vms, dom->name) && vms->size > 0) {
        if (vms->size != 1) {
1555 1556
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569
            xen_vm_set_free(vms);
            return -1;
        }
        vm = vms->contents[0];
        if (!xen_vm_destroy(session, vm)) {
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR, NULL);
            xen_vm_set_free(vms);
            return -1;
        }
        xen_vm_set_free(vms);
        return 0;
    }
    if (vms) xen_vm_set_free(vms);
1570
    xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
    return -1;
}

/*
 * xenapiDomainGetAutostart
 *
 * Provides a boolean value indicating whether the domain configured
 * to be automatically started when the host machine boots
 * Return 0 on success or -1 in case of error
 */
static int
xenapiDomainGetAutostart (virDomainPtr dom, int *autostart)
{
    int i,flag=0;
    xen_vm_set *vms;
    xen_vm vm;
    xen_string_string_map *result;
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;
    if (xen_vm_get_by_name_label(session, &vms, dom->name) && vms->size > 0) {
        if (vms->size != 1) {
1591 1592
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
            xen_vm_set_free(vms);
            return -1;
        }
        vm = vms->contents[0];
        if (!xen_vm_get_other_config(session, &result, vm)) {
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR, NULL);
            xen_vm_set_free(vms);
            return -1;
        }
        for (i = 0; i < result->size; i++) {
            if (STREQ(result->contents[i].key, "auto_poweron")) {
                flag = 1;
                if (STREQ(result->contents[i].val, "true"))
                    *autostart = 1;
                else
                    *autostart = 0;
                break;
            }
        }
        xen_vm_set_free(vms);
        xen_string_string_map_free(result);
        if (flag == 0) return -1;
        return 0;
    }
    if (vms) xen_vm_set_free(vms);
1618
    xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
    return -1;
}

/*
 * xenapiDomainSetAutostart
 *
 * Configure the domain to be automatically started when the host machine boots
 * Return 0 on success or -1 in case of error
 */
static int
xenapiDomainSetAutostart (virDomainPtr dom, int autostart)
{
    xen_vm_set *vms;
    xen_vm vm;
    char *value;
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;
    if (xen_vm_get_by_name_label(session, &vms, dom->name) && vms->size > 0) {
        if (vms->size != 1) {
1637 1638
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
            xen_vm_set_free(vms);
            return -1;
        }
        vm = vms->contents[0];
        xen_vm_remove_from_other_config(session, vm, (char *)"auto_poweron");
        if (autostart==1)
            value = (char *)"true";
        else
            value = (char *)"false";
        if (!xen_vm_add_to_other_config(session, vm, (char *)"auto_poweron", value)) {
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR, NULL);
            xen_vm_set_free(vms);
            return -1;
        }
        xen_vm_set_free(vms);
        return 0;
    }
    if (vms) xen_vm_set_free(vms);
1657
    xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685
    return -1;
}

static char *
xenapiDomainGetSchedulerType (virDomainPtr dom ATTRIBUTE_UNUSED, int *nparams)
{
    char *result = NULL;
    *nparams = 0;
    if (!(result = strdup("credit")))
        virReportOOMError();
    return result;
}

/*
 * xenapiNodeGetFreeMemory
 *
 * provides the free memory available on the Node
 * Returns memory size on success or 0 in case of error
 */
static unsigned long long
xenapiNodeGetFreeMemory (virConnectPtr conn)
{
    xen_host_metrics_set *xen_met_set;
    unsigned long long freeMem = 0;
    xen_session *session = ((struct _xenapiPrivate *)(conn->privateData))->session;
    xen_host_metrics_get_all(session, &xen_met_set);
    if (xen_met_set != NULL) {
        if (!xen_host_metrics_get_memory_free(session, (int64_t *)&freeMem, xen_met_set->contents[0])) {
1686 1687
            xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Couldn't get host metrics - memory information"));
1688 1689 1690 1691
            freeMem = 0;
        }
        xen_host_metrics_set_free(xen_met_set);
    } else {
1692 1693
        xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                  _("Couldn't get host metrics"));
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
    }
    return freeMem;
}

/*
 * xenapiNodeGetCellsFreeMemory
 *
 *
 * Returns the number of entries filled in freeMems, or -1 in case of error.
 */
static int
xenapiNodeGetCellsFreeMemory (virConnectPtr conn, unsigned long long *freeMems,
                              int startCell, int maxCells)
{
    if (maxCells > 1 && startCell > 0) {
1709
        xenapiSessionErrorHandler(conn, VIR_ERR_NO_SUPPORT, NULL);
1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
        return -1;
    } else {
        freeMems[0] = xenapiNodeGetFreeMemory(conn);
        return 1;
    }
}

/* The interface which we export upwards to libvirt.c. */
static virDriver xenapiDriver = {
    VIR_DRV_XENAPI,
    "XenAPI",
    xenapiOpen, /* open */
    xenapiClose, /* close */
    xenapiSupportsFeature, /* supports_feature */
    xenapiType, /* type */
    xenapiGetVersion, /* version */
    NULL, /*getlibvirtVersion */
    xenapiGetHostname, /* getHostname */
    xenapiGetMaxVcpus, /* getMaxVcpus */
    xenapiNodeGetInfo, /* nodeGetInfo */
    xenapiGetCapabilities, /* getCapabilities */
    xenapiListDomains, /* listDomains */
    xenapiNumOfDomains, /* numOfDomains */
    xenapiDomainCreateXML, /* domainCreateXML */
    xenapiDomainLookupByID, /* domainLookupByID */
    xenapiDomainLookupByUUID, /* domainLookupByUUID */
    xenapiDomainLookupByName, /* domainLookupByName */
    xenapiDomainSuspend, /* domainSuspend */
    xenapiDomainResume, /* domainResume */
    xenapiDomainShutdown, /* domainShutdown */
    xenapiDomainReboot, /* domainReboot */
    xenapiDomainDestroy, /* domainDestroy */
    xenapiDomainGetOSType, /* domainGetOSType */
    xenapiDomainGetMaxMemory, /* domainGetMaxMemory */
    xenapiDomainSetMaxMemory, /* domainSetMaxMemory */
    NULL, /* domainSetMemory */
    xenapiDomainGetInfo, /* domainGetInfo */
    NULL, /* domainSave */
    NULL, /* domainRestore */
    NULL, /* domainCoreDump */
    xenapiDomainSetVcpus, /* domainSetVcpus */
    xenapiDomainPinVcpu, /* domainPinVcpu */
    xenapiDomainGetVcpus, /* domainGetVcpus */
    xenapiDomainGetMaxVcpus, /* domainGetMaxVcpus */
    NULL, /* domainGetSecurityLabel */
    NULL, /* nodeGetSecurityModel */
    xenapiDomainDumpXML, /* domainDumpXML */
    NULL, /* domainXmlFromNative */
    NULL, /* domainXmlToNative */
    xenapiListDefinedDomains, /* listDefinedDomains */
    xenapiNumOfDefinedDomains, /* numOfDefinedDomains */
    xenapiDomainCreate, /* domainCreate */
1762
    xenapiDomainCreateWithFlags, /* domainCreateWithFlags */
1763 1764 1765 1766 1767 1768
    xenapiDomainDefineXML, /* domainDefineXML */
    xenapiDomainUndefine, /* domainUndefine */
    NULL, /* domainAttachDevice */
    NULL, /* domainAttachDeviceFlags */
    NULL, /* domainDetachDevice */
    NULL, /* domainDetachDeviceFlags */
1769
    NULL, /* domainUpdateDeviceFlags */
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
    xenapiDomainGetAutostart, /* domainGetAutostart */
    xenapiDomainSetAutostart, /* domainSetAutostart */
    xenapiDomainGetSchedulerType, /* domainGetSchedulerType */
    NULL, /* domainGetSchedulerParameters */
    NULL, /* domainSetSchedulerParameters */
    NULL, /* domainMigratePrepare */
    NULL, /* domainMigratePerform */
    NULL, /* domainMigrateFinish */
    NULL, /* domainBlockStats */
    NULL, /* domainInterfaceStats */
    NULL, /* domainMemoryStats */
    NULL, /* domainBlockPeek */
    NULL, /* domainMemoryPeek */
1783
    NULL, /* domainGetBlockInfo */
1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
    xenapiNodeGetCellsFreeMemory, /* nodeGetCellsFreeMemory */
    xenapiNodeGetFreeMemory, /* getFreeMemory */
    NULL, /* domainEventRegister */
    NULL, /* domainEventDeregister */
    NULL, /* domainMigratePrepare2 */
    NULL, /* domainMigrateFinish2 */
    NULL, /* nodeDeviceDettach */
    NULL, /* nodeDeviceReAttach */
    NULL, /* nodeDeviceReset */
    NULL, /* domainMigratePrepareTunnel */
    NULL, /* isEncrypted */
    NULL, /* isSecure */
    NULL, /* domainIsActive */
    NULL, /* domainIsPersistent */
    NULL, /* cpuCompare */
    NULL, /* cpuBaseline */
    NULL, /* domainGetJobInfo */
    NULL, /* domainAbortJob */
1802
    NULL, /* domainMigrateSetMaxDowntime */
1803 1804
    NULL, /* domainEventRegisterAny */
    NULL, /* domainEventDeregisterAny */
1805 1806 1807
    NULL, /* domainManagedSave */
    NULL, /* domainHasManagedSaveImage */
    NULL, /* domainManagedSaveRemove */
C
Chris Lalancette 已提交
1808 1809 1810 1811 1812 1813 1814 1815 1816
    NULL, /* domainSnapshotCreateXML */
    NULL, /* domainSnapshotDumpXML */
    NULL, /* domainSnapshotNum */
    NULL, /* domainSnapshotListNames */
    NULL, /* domainSnapshotLookupByName */
    NULL, /* domainHasCurrentSnapshot */
    NULL, /* domainSnapshotCurrent */
    NULL, /* domainRevertToSnapshot */
    NULL, /* domainSnapshotDelete */
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839
};

/**
 * xenapiRegister:
 *
 *
 * Returns the driver priority or -1 in case of error.
 */
int
xenapiRegister (void)
{
    return virRegisterDriver (&xenapiDriver);
}

/*
 * write_func
 * used by curl to read data from the server
 */
size_t
write_func(void *ptr, size_t size, size_t nmemb, void *comms_)
{
    xen_comms *comms = comms_;
    size_t n = size * nmemb;
J
Jiri Denemark 已提交
1840 1841 1842 1843 1844
#ifdef PRINT_XML
    printf("\n\n---Result from server -----------------------\n");
    printf("%s\n",((char*) ptr));
    fflush(stdout);
#endif
1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857
    return (size_t) (comms->func(ptr, n, comms->handle) ? n : 0);
}

/*
 * call_func
 * sets curl options, used with xen_session_login_with_password
 */
int
call_func(const void *data, size_t len, void *user_handle,
          void *result_handle, xen_result_func result_func)
{
    //(void)user_handle;
    struct _xenapiPrivate *priv = (struct _xenapiPrivate *)user_handle;
J
Jiri Denemark 已提交
1858 1859 1860 1861 1862
#ifdef PRINT_XML
    printf("\n\n---Data to server: -----------------------\n");
    printf("%s\n",((char*) data));
    fflush(stdout);
#endif
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
    CURL *curl = curl_easy_init();
    if (!curl) {
      return -1;
    }
    xen_comms comms = {
     .func = result_func,
     .handle = result_handle
    };
    curl_easy_setopt(curl, CURLOPT_URL, priv->url);
    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
J
Jiri Denemark 已提交
1873 1874 1875
#ifdef CURLOPT_MUTE
    curl_easy_setopt(curl, CURLOPT_MUTE, 1);
#endif
1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_func);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &comms);
    curl_easy_setopt(curl, CURLOPT_POST, 1);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, len);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, priv->noVerify?0:1);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, priv->noVerify?0:2);
    CURLcode result = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    return result;
}