xenapi_driver.c 65.5 KB
Newer Older
1 2
/*
 * xenapi_driver.c: Xen API driver.
3
 * Copyright (C) 2011-2015 Red Hat, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16
 * 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
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21 22 23 24 25 26 27 28 29 30
 *
 * Author: Sharadha Prabhakar <sharadha.prabhakar@citrix.com>
 */

#include <config.h>

#include <limits.h>
#include <string.h>
#include <curl/curl.h>
#include <xen/api/xen_all.h>
#include "internal.h"
#include "domain_conf.h"
31
#include "virerror.h"
32
#include "datatypes.h"
33
#include "virauth.h"
34
#include "viruuid.h"
35
#include "viralloc.h"
36
#include "virbuffer.h"
M
Martin Kletzander 已提交
37
#include "viruri.h"
38 39 40
#include "xenapi_driver.h"
#include "xenapi_driver_private.h"
#include "xenapi_utils.h"
41
#include "virstring.h"
42

43 44
#define VIR_FROM_THIS VIR_FROM_XENAPI

45

46 47
static int
xenapiDomainDeviceDefPostParse(virDomainDeviceDefPtr dev,
48
                               const virDomainDef *def,
49 50
                               virCapsPtr caps ATTRIBUTE_UNUSED,
                               void *opaque ATTRIBUTE_UNUSED)
51
{
52 53 54
    if (dev->type == VIR_DOMAIN_DEVICE_CHR &&
        dev->data.chr->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE &&
        dev->data.chr->targetType == VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_NONE &&
55
        def->os.type != VIR_DOMAIN_OSTYPE_HVM)
56 57
        dev->data.chr->targetType = VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_XEN;

58 59 60 61 62 63 64 65 66 67
    /* forbid capabilities mode hostdev in this kind of hypervisor */
    if (dev->type == VIR_DOMAIN_DEVICE_HOSTDEV &&
        dev->data.hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_CAPABILITIES) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("hostdev mode 'capabilities' is not "
                         "supported in %s"),
                       virDomainVirtTypeToString(def->virtType));
        return -1;
    }

68 69 70
    if (virDomainDeviceDefCheckUnsupportedMemoryDevice(dev) < 0)
        return -1;

71
    return 0;
72 73 74
}


75
static int
76
xenapiDomainDefPostParse(virDomainDefPtr def,
77 78 79
                         virCapsPtr caps ATTRIBUTE_UNUSED,
                         void *opaque ATTRIBUTE_UNUSED)
{
80 81 82 83
    /* memory hotplug tunables are not supported by this driver */
    if (virDomainDefCheckUnsupportedMemoryHotplug(def) < 0)
        return -1;

84 85 86 87
    return 0;
}


88 89
virDomainDefParserConfig xenapiDomainDefParserConfig = {
    .devicesPostParseCallback = xenapiDomainDeviceDefPostParse,
90
    .domainPostParseCallback = xenapiDomainDefPostParse,
91 92 93
};


94 95 96 97 98 99 100
/*
 * getCapsObject
 *
 * Build the capabilities of the hypervisor
 * Return virCapsPtr on success or NULL on failure
 */
static virCapsPtr
101
getCapsObject(void)
102
{
103 104
    virCapsGuestPtr guest1, guest2;
    virCapsGuestDomainPtr domain1, domain2;
105
    virCapsPtr caps = virCapabilitiesNew(virArchFromHost(), false, false);
106

107
    if (!caps)
108
        return NULL;
109
    guest1 = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_X86_64, "", "", 0, NULL);
110 111
    if (!guest1)
        goto error_cleanup;
112
    domain1 = virCapabilitiesAddGuestDomain(guest1, VIR_DOMAIN_VIRT_XEN, "", "", 0, NULL);
113 114
    if (!domain1)
        goto error_cleanup;
115
    guest2 = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_XEN, VIR_ARCH_X86_64, "", "", 0, NULL);
116 117
    if (!guest2)
        goto error_cleanup;
118
    domain2 = virCapabilitiesAddGuestDomain(guest2, VIR_DOMAIN_VIRT_XEN, "", "", 0, NULL);
119 120 121 122 123
    if (!domain2)
        goto error_cleanup;

    return caps;

124
 error_cleanup:
125
    virObjectUnref(caps);
126 127 128 129
    return NULL;
}

/*
130
 * xenapiConnectOpen
131 132 133 134 135
 *
 * Authenticates and creates a session with the server
 * Return VIR_DRV_OPEN_SUCCESS on success, else VIR_DRV_OPEN_ERROR
 */
static virDrvOpenStatus
136 137
xenapiConnectOpen(virConnectPtr conn, virConnectAuthPtr auth,
                  unsigned int flags)
138
{
139 140 141
    char *username = NULL;
    char *password = NULL;
    struct _xenapiPrivate *privP = NULL;
142

E
Eric Blake 已提交
143 144
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

145 146
    if (conn->uri == NULL || conn->uri->scheme == NULL ||
        STRCASENEQ(conn->uri->scheme, "XenAPI")) {
147 148
        return VIR_DRV_OPEN_DECLINED;
    }
149

150
    if (conn->uri->server == NULL) {
151
        xenapiSessionErrorHandler(conn, VIR_ERR_INVALID_ARG,
152
                                  _("Server name not in URI"));
153 154 155 156 157
        goto error;
    }

    if (auth == NULL) {
        xenapiSessionErrorHandler(conn, VIR_ERR_AUTH_FAILED,
158
                                  _("Authentication Credentials not found"));
159
        goto error;
160
    }
161 162

    if (conn->uri->user != NULL) {
163
        if (VIR_STRDUP(username, conn->uri->user) < 0)
164
            goto error;
165
    } else {
166
        username = virAuthGetUsername(conn, auth, "xen", NULL, conn->uri->server);
167 168 169

        if (username == NULL) {
            xenapiSessionErrorHandler(conn, VIR_ERR_AUTH_FAILED,
170
                                      _("Username request failed"));
171 172
            goto error;
        }
173
    }
174

175
    password = virAuthGetPassword(conn, auth, "xen", username, conn->uri->server);
176 177 178

    if (password == NULL) {
        xenapiSessionErrorHandler(conn, VIR_ERR_AUTH_FAILED,
179
                                  _("Password request failed"));
180
        goto error;
181
    }
182

183
    if (VIR_ALLOC(privP) < 0)
184 185
        goto error;

186
    if (virAsprintf(&privP->url, "https://%s", conn->uri->server) < 0)
187 188 189 190 191 192 193
        goto error;

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

    if (!(privP->caps = getCapsObject())) {
        xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
194
                                  _("Capabilities not found"));
195 196 197
        goto error;
    }

198 199
    if (!(privP->xmlopt = virDomainXMLOptionNew(&xenapiDomainDefParserConfig,
                                                NULL, NULL))) {
200 201 202 203 204
        xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                  _("Failed to create XML conf object"));
        goto error;
    }

205 206 207 208
    xmlInitParser();
    xmlKeepBlanksDefault(0);
    xen_init();

209 210
    privP->session = xen_session_login_with_password(call_func, privP, username,
                                                     password, xen_api_latest_version);
211

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    if (privP->session == NULL) {
        /* From inspection of xen_session_login_with_password in
         * libxenserver(Version 5.6.100-1), this appears not to be currently
         * possible. The only way for this to be NULL would be on malloc
         * failure, except that the code doesn't check for this and would
         * segfault before returning.
         *
         * We don't assume the reason here for a failure in an external library.
         */
        xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                  _("Failed to allocate xen session"));

        goto error;
    }

    if (privP->session->ok) {
228
        conn->privateData = privP;
229 230 231 232

        VIR_FREE(username);
        VIR_FREE(password);

233
        return VIR_DRV_OPEN_SUCCESS;
234 235
    }

236
    xenapiSessionErrorHandler(conn, VIR_ERR_AUTH_FAILED, NULL);
237

238
 error:
239 240 241 242
    VIR_FREE(username);
    VIR_FREE(password);

    if (privP != NULL) {
243
        virObjectUnref(privP->caps);
244
        virObjectUnref(privP->xmlopt);
245

246 247 248
        if (privP->session != NULL)
            xenSessionFree(privP->session);

249
        VIR_FREE(privP->url);
250 251
        VIR_FREE(privP);
    }
252 253

    return VIR_DRV_OPEN_ERROR;
254 255 256
}

/*
257
 * xenapiConnectClose:
258 259 260 261 262
 *
 * Returns 0 on successful session logout
 *
 */
static int
263
xenapiConnectClose(virConnectPtr conn)
264
{
265 266
    struct _xenapiPrivate *priv = conn->privateData;

267
    virObjectUnref(priv->caps);
268
    virObjectUnref(priv->xmlopt);
269 270 271

    if (priv->session != NULL) {
        xen_session_logout(priv->session);
272
        priv->session = NULL;
273 274 275 276 277 278 279
    }

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

    conn->privateData = NULL;

280 281 282 283 284
    return 0;
}

/*
 *
285
 * xenapiConnectSupportsFeature
286 287 288 289
 *
 * Returns 0
 */
static int
290
xenapiConnectSupportsFeature(virConnectPtr conn ATTRIBUTE_UNUSED, int feature)
291 292 293 294 295 296 297 298 299 300
{
    switch (feature) {
    case VIR_DRV_FEATURE_MIGRATION_V2:
    case VIR_DRV_FEATURE_MIGRATION_P2P:
    default:
        return 0;
    }
}

/*
301
 * xenapiConnectGetType:
302 303 304 305 306
 *
 *
 * Returns name of the driver
 */
static const char *
307
xenapiConnectGetType(virConnectPtr conn ATTRIBUTE_UNUSED)
308 309 310 311 312 313
{
    return "XenAPI";
}


/*
314
 * xenapiConnectGetVersion:
315 316 317 318 319
 *
 * Gets the version of XenAPI
 *
 */
static int
320
xenapiConnectGetVersion(virConnectPtr conn, unsigned long *hvVer)
321 322 323 324
{
    xen_host host;
    xen_session *session = ((struct _xenapiPrivate *)(conn->privateData))->session;
    xen_string_string_map *result = NULL;
325 326
    size_t i;
    int ret = -1;
327 328 329 330 331 332 333 334 335 336 337 338 339 340
    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")) {
341
                if (VIR_STRDUP(version, result->contents[i].val) < 0) {
342 343 344 345 346 347 348
                    xen_string_string_map_free(result);
                    return -1;
                }
                break;
            }
        }
        if (version) {
349
            if (virParseVersionString(version, hvVer, false) < 0)
350
                xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
351 352 353
                                          _("Couldn't parse version info"));
            else
                ret = 0;
354
            xen_string_string_map_free(result);
355 356
            VIR_FREE(version);
            return ret;
357
        }
358 359
        xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                  _("Couldn't get version info"));
360 361 362 363 364 365
    }
    return -1;
}


/*
366
 * xenapiConnectGetHostname:
367 368 369 370 371
 *
 *
 * Returns the hostname on success, or NULL on failure
 */
static char *
372
xenapiConnectGetHostname(virConnectPtr conn)
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
{
    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;
}


/*
389
 * xenapiConnectGetMaxVcpus:
390 391 392 393 394
 *
 *
 * Returns a hardcoded value for Maximum VCPUS
 */
static int
395
xenapiConnectGetMaxVcpus(virConnectPtr conn ATTRIBUTE_UNUSED, const char *type ATTRIBUTE_UNUSED)
396 397 398 399 400 401 402 403 404 405 406 407 408 409
{
    /* 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
410
xenapiNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info)
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
{
    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 {
427 428
        xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                  _("Unable to get host metric Information"));
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
        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;
    }
449 450
    xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                              _("Unable to get Host CPU set"));
451 452 453 454
    return -1;
}

/*
455
 * xenapiConnectGetCapabilities:
456 457 458 459 460
 *
 *
 * Returns capabilities as an XML string
 */
static char *
461
xenapiConnectGetCapabilities(virConnectPtr conn)
462
{
463

464
    virCapsPtr caps = ((struct _xenapiPrivate *)(conn->privateData))->caps;
465 466
    if (caps)
        return virCapabilitiesFormatXML(caps);
467

468 469
    xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                              _("Capabilities not available"));
470 471 472 473 474
    return NULL;
}


/*
475
 * xenapiConnectListDomains
476 477 478 479 480
 *
 * 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
481
xenapiConnectListDomains(virConnectPtr conn, int *ids, int maxids)
482 483 484 485 486
{
    /* vm.list */
    xen_host host;
    xen_vm_set *result = NULL;
    int64_t t0;
487
    size_t i;
488 489 490 491
    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);
492
    } else {
493
        xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR, NULL);
494
    }
495 496 497 498
    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) {
499 500
                xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                          _("DomainID can't fit in 32 bits"));
501 502 503 504 505 506 507 508 509 510 511 512
                xen_vm_set_free(result);
                return -1;
            }
            ids[i] = (int)t0;
        }
        xen_vm_set_free(result);
        return i;
    }
    return -1;
}

/*
513
 * xenapiConnectNumOfDomains
514 515 516 517 518
 *
 *
 * Returns the number of domains found or -1 in case of error
 */
static int
519
xenapiConnectNumOfDomains(virConnectPtr conn)
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
{
    /* #(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
548 549 550
xenapiDomainCreateXML(virConnectPtr conn,
                      const char *xmlDesc,
                      unsigned int flags)
551
{
552
    struct _xenapiPrivate *priv = conn->privateData;
553 554 555
    xen_vm_record *record = NULL;
    xen_vm vm = NULL;
    virDomainPtr domP = NULL;
556 557
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;

558
    if (!priv->caps)
559 560
        return NULL;

561 562 563 564
    virCheckFlags(VIR_DOMAIN_START_VALIDATE, NULL);

    if (flags & VIR_DOMAIN_START_VALIDATE)
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE;
565

566 567
    virDomainDefPtr defPtr = virDomainDefParseString(xmlDesc,
                                                     priv->caps, priv->xmlopt,
568
                                                     parse_flags);
569 570
    if (!defPtr)
        return NULL;
571 572 573 574
    createVMRecordFromXml(conn, defPtr, &record, &vm);
    virDomainDefFree(defPtr);
    if (record) {
        unsigned char raw_uuid[VIR_UUID_BUFLEN];
575
        ignore_value(virUUIDParse(record->uuid, raw_uuid));
576
        if (vm) {
577
            if (xen_vm_start(priv->session, vm, false, false)) {
578 579 580
                domP = virGetDomain(conn, record->name_label, raw_uuid);
                if (!domP) {
                    xen_vm_record_free(record);
581 582
                    xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                              _("Domain Pointer is invalid"));
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
                    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
606
xenapiDomainLookupByID(virConnectPtr conn, int id)
607
{
608
    size_t i;
609 610 611 612 613 614
    int64_t domID;
    char *uuid;
    xen_host host;
    xen_vm_set *result;
    xen_vm_record *record;
    unsigned char raw_uuid[VIR_UUID_BUFLEN];
615
    virDomainPtr domP = NULL;
616 617 618 619 620
    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);
621
        if (result != NULL) {
622 623 624 625 626
            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]);
627
                    ignore_value(virUUIDParse(uuid, raw_uuid));
628 629 630 631 632 633
                    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 {
634 635
                        xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                                  _("Domain Pointer not valid"));
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
                        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
661 662
xenapiDomainLookupByUUID(virConnectPtr conn,
                         const unsigned char *uuid)
663 664 665 666 667 668 669
{
    /* 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;
E
Eric Blake 已提交
670
    virUUIDFormat(uuid, uuidStr);
671 672 673 674 675
    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) {
676 677
                xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                          _("Domain Pointer not valid"));
678 679 680 681 682
                domP = NULL;
            } else {
                domP->id = record->domid;
            }
            xen_vm_record_free(record);
683
        } else {
684
            xenapiSessionErrorHandler(conn, VIR_ERR_NO_DOMAIN, NULL);
685
        }
686
        xen_vm_free(vm);
687
    } else {
688
        xenapiSessionErrorHandler(conn, VIR_ERR_NO_DOMAIN, NULL);
689
    }
690 691 692 693 694 695 696 697 698 699
    return domP;
}

/*
 * xenapiDomainLookupByName
 *
 * Returns the domain pointer of domain with matching name
 * or -1 in case of error
 */
static virDomainPtr
700 701
xenapiDomainLookupByName(virConnectPtr conn,
                         const char *name)
702 703 704 705 706 707 708 709 710 711
{
    /* 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) {
712 713
            xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
714 715 716 717 718
            xen_vm_set_free(vms);
            return NULL;
        }
        vm = vms->contents[0];
        xen_vm_get_uuid(session, &uuid, vm);
719
        if (uuid != NULL) {
720
            ignore_value(virUUIDParse(uuid, raw_uuid));
721 722 723 724 725 726 727 728 729 730 731 732
            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)
733 734
                xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                          _("Couldn't get the Domain Pointer"));
735 736 737 738
                return NULL;
            }
        }
    }
739 740
    if (vms)
        xen_vm_set_free(vms);
741
    xenapiSessionErrorHandler(conn, VIR_ERR_NO_DOMAIN, NULL);
742 743 744 745 746 747 748 749 750 751
    return NULL;
}

/*
 * xenapiDomainSuspend
 *
 * a VM is paused
 * Returns 0 on success or -1 in case of error
 */
static int
752
xenapiDomainSuspend(virDomainPtr dom)
753 754 755
{
    /* vm.pause() */
    xen_vm vm;
756
    xen_vm_set *vms = NULL;
757 758 759
    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) {
760 761
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
762 763 764 765 766 767 768 769 770 771 772 773 774
            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;
        }
    }
775 776
    if (vms)
        xen_vm_set_free(vms);
777
    xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
778 779 780 781 782 783 784 785 786 787
    return -1;
}

/*
 * xenapiDomainResume
 *
 * Resumes a VM
 * Returns 0 on success or -1 in case of error
 */
static int
788
xenapiDomainResume(virDomainPtr dom)
789 790 791 792 793 794 795
{
    /* 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) {
796 797
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
798 799 800 801 802 803 804 805 806 807 808 809 810
            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;
        }
    }
811 812
    if (vms)
        xen_vm_set_free(vms);
813
    xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
814 815 816 817 818 819 820 821 822 823
    return -1;
}

/*
 * xenapiDomainShutdown
 *
 * shutsdown a VM
 * Returns 0 on success or -1 in case of error
 */
static int
824
xenapiDomainShutdownFlags(virDomainPtr dom, unsigned int flags)
825 826 827 828 829
{
    /* vm.clean_shutdown */
    xen_vm vm;
    xen_vm_set *vms;
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;
830 831 832

    virCheckFlags(0, -1);

833 834
    if (xen_vm_get_by_name_label(session, &vms, dom->name) && vms->size > 0) {
        if (vms->size != 1) {
835 836
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
837 838 839 840 841 842 843 844 845 846 847 848 849
            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;
        }
    }
850 851
    if (vms)
        xen_vm_set_free(vms);
852
    xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
853 854 855
    return -1;
}

856 857 858 859 860 861
static int
xenapiDomainShutdown(virDomainPtr dom)
{
    return xenapiDomainShutdownFlags(dom, 0);
}

862 863 864 865 866 867 868
/*
 * xenapiDomainReboot
 *
 * Reboots a VM
 * Returns 0 on success or -1 in case of error
 */
static int
869
xenapiDomainReboot(virDomainPtr dom, unsigned int flags)
870 871 872 873 874
{
    /* vm.clean_reboot */
    xen_vm vm;
    struct xen_vm_set *vms;
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;
E
Eric Blake 已提交
875 876 877

    virCheckFlags(0, -1);

878 879
    if (xen_vm_get_by_name_label(session, &vms, dom->name) && vms->size > 0) {
        if (vms->size != 1) {
880 881
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
882 883 884 885 886 887 888 889 890 891 892 893
            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;
    }
894 895
    if (vms)
        xen_vm_set_free(vms);
896
    xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
897 898 899 900
    return -1;
}

/*
901 902 903 904 905 906
 * xenapiDomainDestroyFlags:
 * @dom: domain object
 * @flags: an OR'ed set of virDomainDestroyFlagsValues
 *
 * Calling this function with no flags set (equal to zero)
 * is equivalent to calling xenapiDomainDestroy.
907 908 909 910 911
 *
 * A VM is hard shutdown
 * Returns 0 on success or -1 in case of error
 */
static int
912 913
xenapiDomainDestroyFlags(virDomainPtr dom,
                         unsigned int flags)
914 915 916 917 918
{
    /* vm.hard_shutdown */
    xen_vm vm;
    struct xen_vm_set *vms;
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;
919 920 921

    virCheckFlags(0, -1);

922 923
    if (xen_vm_get_by_name_label(session, &vms, dom->name) && vms->size > 0) {
        if (vms->size != 1) {
924 925
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
926 927 928 929 930 931 932 933 934 935
            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);
936
        dom->id = -1;
937 938
        return 0;
    }
939 940
    if (vms)
        xen_vm_set_free(vms);
941
    xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
942 943 944
    return -1;
}

945 946 947 948 949 950 951 952 953 954 955
/*
 * xenapiDomainDestroy:
 * @dom: domain object
 *
 * See xenapiDomainDestroyFlags
 */
static int
xenapiDomainDestroy(virDomainPtr dom)
{
    return xenapiDomainDestroyFlags(dom, 0);
}
956 957 958 959 960 961 962
/*
 * xenapiDomainGetOSType
 *
 *
 * Returns OS version on success or NULL in case of error
 */
static char *
963
xenapiDomainGetOSType(virDomainPtr dom)
964
{
965
    xen_vm vm = NULL;
966 967
    xen_vm_set *vms;
    char *ostype = NULL;
968
    char *boot_policy = NULL;
969 970 971 972
    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) {
973 974
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
975 976 977 978 979 980 981 982
            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;
        }
983 984
        ignore_value(VIR_STRDUP(ostype,
                                STREQ(boot_policy, "BIOS order") ? "hvm" : "xen"));
985
        VIR_FREE(boot_policy);
986
    } else {
987
        xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
988
    }
989

990
 cleanup:
991 992
    if (vms)
        xen_vm_set_free(vms);
993 994 995 996 997 998 999 1000
    return ostype;
}
/*
 * xenapiDomainGetMaxMemory
 *
 * Returns maximum static memory for VM on success
 * or 0 in case of error
 */
1001
static unsigned long long
1002
xenapiDomainGetMaxMemory(virDomainPtr dom)
1003 1004 1005 1006 1007 1008 1009
{
    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) {
1010 1011
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
1012 1013 1014 1015 1016 1017
            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);
1018
        return mem_static_max / 1024;
1019
    } else {
1020 1021
        if (vms)
            xen_vm_set_free(vms);
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
        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
1034
xenapiDomainSetMaxMemory(virDomainPtr dom, unsigned long memory)
1035 1036 1037 1038 1039 1040 1041
{
    /* 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) {
1042 1043
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
            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 {
1055 1056
        if (vms)
            xen_vm_set_free(vms);
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
        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
1070
xenapiDomainGetInfo(virDomainPtr dom, virDomainInfoPtr info)
1071 1072 1073 1074 1075 1076 1077 1078 1079
{
    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) {
1080 1081
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
1082 1083 1084 1085 1086 1087
            xen_vm_set_free(vms);
            return -1;
        }
        vm = vms->contents[0];
        xen_vm_get_memory_static_max(session, &maxmem, vm);
        info->maxMem = (maxmem / 1024);
1088
        enum xen_vm_power_state state = XEN_VM_POWER_STATE_UNDEFINED;
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
        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;
    }
1102 1103
    if (vms)
        xen_vm_set_free(vms);
1104 1105 1106 1107
    xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
    return -1;
}

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
/*
 * xenapiDomainGetState:
 *
 * Retrieves domain status and its reason.
 *
 * Returns 0 on success or -1 in case of error
 */
static int
xenapiDomainGetState(virDomainPtr dom,
                     int *state,
                     int *reason,
                     unsigned int flags)
{
    struct _xenapiPrivate *priv = dom->conn->privateData;
    enum xen_vm_power_state powerState = XEN_VM_POWER_STATE_UNDEFINED;
    xen_vm_set *vms = NULL;
    xen_vm vm;
    int ret = -1;

    virCheckFlags(0, -1);

    if (!xen_vm_get_by_name_label(priv->session, &vms, dom->name) ||
        vms->size == 0) {
        xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
        goto cleanup;
    }

    if (vms->size != 1) {
        xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                  _("Domain name is not unique"));
        goto cleanup;
    }

    vm = vms->contents[0];
    xen_vm_get_power_state(priv->session, &powerState, vm);

    *state = mapPowerState(powerState);
    if (reason)
        *reason = 0;

    ret = 0;

1150
 cleanup:
1151 1152 1153 1154 1155
    if (vms)
        xen_vm_set_free(vms);
    return ret;
}

1156 1157

/*
1158
 * xenapiDomainSetVcpusFlags
1159 1160 1161 1162 1163
 *
 * Sets the VCPUs on the domain
 * Return 0 on success or -1 in case of error
 */
static int
1164 1165
xenapiDomainSetVcpusFlags(virDomainPtr dom, unsigned int nvcpus,
                          unsigned int flags)
1166 1167 1168 1169 1170
{
    /* vm.set_vcpus_max */
    xen_vm vm;
    xen_vm_set *vms;
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;
1171 1172

    if (flags != VIR_DOMAIN_VCPU_LIVE) {
1173 1174
        virReportError(VIR_ERR_INVALID_ARG, _("unsupported flags: (0x%x)"),
                       flags);
1175 1176 1177
        return -1;
    }

1178 1179
    if (xen_vm_get_by_name_label(session, &vms, dom->name) && vms->size > 0) {
        if (vms->size != 1) {
1180 1181
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
1182 1183 1184 1185 1186 1187 1188 1189 1190
            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;
        }
    }
1191 1192
    if (vms)
        xen_vm_set_free(vms);
1193 1194 1195 1196
    xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
    return -1;
}

1197 1198 1199 1200 1201 1202 1203
/*
 * xenapiDomainSetVcpus
 *
 * Sets the VCPUs on the domain
 * Return 0 on success or -1 in case of error
 */
static int
1204
xenapiDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus)
1205 1206 1207 1208
{
    return xenapiDomainSetVcpusFlags(dom, nvcpus, VIR_DOMAIN_VCPU_LIVE);
}

1209 1210 1211 1212 1213 1214 1215
/*
 * 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
1216 1217
xenapiDomainPinVcpu(virDomainPtr dom, unsigned int vcpu ATTRIBUTE_UNUSED,
                    unsigned char *cpumap, int maplen)
1218 1219 1220 1221 1222 1223 1224
{
    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) {
1225 1226
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
            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;
        }
    }
1245 1246
    if (vms)
        xen_vm_set_free(vms);
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
    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
1258 1259 1260
xenapiDomainGetVcpus(virDomainPtr dom,
                     virVcpuInfoPtr info, int maxinfo,
                     unsigned char *cpumaps, int maplen)
1261 1262 1263 1264 1265
{

    xen_vm_set *vms = NULL;
    xen_vm vm = NULL;
    xen_string_string_map *vcpu_params = NULL;
1266 1267
    int nvcpus = 0;
    size_t i;
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
    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 {
1278 1279
        xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                  _("Couldn't fetch Domain Information"));
1280 1281
        return -1;
    }
E
Eric Blake 已提交
1282
    if (xenapiNodeGetInfo(dom->conn, &nodeInfo) != 0) {
1283 1284
        xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                  _("Couldn't fetch Node Information"));
1285 1286 1287 1288 1289 1290 1291 1292 1293
        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) {
1294 1295
        xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                  _("Domain name is not unique"));
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
        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")) {
1307
            if (VIR_STRDUP(mask, vcpu_params->contents[i].val) < 0) {
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
                 xen_vm_set_free(vms);
                 xen_string_string_map_free(vcpu_params);
                 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;
}

/*
1330
 * xenapiDomainGetVcpusFlags
1331 1332
 *
 *
1333
 * Returns Vcpus count on success or -1 in case of error
1334 1335
 */
static int
1336
xenapiDomainGetVcpusFlags(virDomainPtr dom, unsigned int flags)
1337 1338 1339 1340 1341 1342
{
    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;
1343 1344

    if (flags != (VIR_DOMAIN_VCPU_LIVE | VIR_DOMAIN_VCPU_MAXIMUM)) {
1345 1346
        virReportError(VIR_ERR_INVALID_ARG, _("unsupported flags: (0x%x)"),
                       flags);
1347 1348 1349
        return -1;
    }

1350 1351
    if (xen_vm_get_by_name_label(session, &vms, dom->name) && vms->size > 0) {
        if (vms->size != 1) {
1352 1353
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
1354 1355 1356 1357 1358 1359 1360 1361
            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 {
1362
            maxvcpu = xenapiConnectGetMaxVcpus(dom->conn, NULL);
1363 1364 1365 1366
        }
        xen_vm_set_free(vms);
        return (int)maxvcpu;
    }
1367 1368
    if (vms)
        xen_vm_set_free(vms);
1369 1370 1371 1372
    xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR, NULL);
    return -1;
}

1373 1374 1375 1376 1377 1378 1379
/*
 * xenapiDomainGetMaxVcpus
 *
 *
 * Returns maximum number of Vcpus on success or -1 in case of error
 */
static int
1380
xenapiDomainGetMaxVcpus(virDomainPtr dom)
1381 1382 1383 1384 1385
{
    return xenapiDomainGetVcpusFlags(dom, (VIR_DOMAIN_VCPU_LIVE |
                                           VIR_DOMAIN_VCPU_MAXIMUM));
}

1386
/*
1387
 * xenapiDomainGetXMLDesc
1388 1389 1390 1391 1392
 *
 *
 * Returns XML string of the domain configuration on success or -1 in case of error
 */
static char *
E
Eric Blake 已提交
1393
xenapiDomainGetXMLDesc(virDomainPtr dom, unsigned int flags)
1394
{
1395
    xen_vm vm = NULL;
1396
    xen_vm_set *vms;
1397
    xen_string_string_map *result = NULL;
1398 1399
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;
    virDomainDefPtr defPtr = NULL;
1400
    char *boot_policy = NULL;
1401 1402
    unsigned long memory = 0;
    int64_t dynamic_mem = 0;
1403 1404 1405
    char *val = NULL;
    struct xen_vif_set *vif_set = NULL;
    char *xml;
1406
    unsigned int vcpus;
1407

1408
    /* Flags checked by virDomainDefFormat */
E
Eric Blake 已提交
1409

1410 1411
    if (!xen_vm_get_by_name_label(session, &vms, dom->name))
        return NULL;
1412
    if (vms->size != 1) {
1413 1414
        xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                  _("Domain name is not unique"));
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
        xen_vm_set_free(vms);
        return NULL;
    }
    if (VIR_ALLOC(defPtr) < 0) {
        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);
1426 1427
    if (VIR_STRDUP(defPtr->name, dom->name) < 0)
        goto error;
1428
    xen_vm_get_hvm_boot_policy(session, &boot_policy, vm);
E
Eric Blake 已提交
1429
    if (STREQ(boot_policy, "BIOS order")) {
1430
        defPtr->os.type = VIR_DOMAIN_OSTYPE_HVM;
1431 1432
        xen_vm_get_hvm_boot_params(session, &result, vm);
        if (result != NULL) {
1433
            size_t i;
1434 1435 1436
            for (i = 0; i < result->size; i++) {
                if (STREQ(result->contents[i].key, "order")) {
                    int cnt = 0;
1437
                    while (result->contents[i].val[cnt] != '\0') {
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
                        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;
1450
        defPtr->os.type = VIR_DOMAIN_OSTYPE_XEN;
1451 1452
        if (VIR_ALLOC(defPtr->os.loader) < 0 ||
            VIR_STRDUP(defPtr->os.loader->path, "pygrub") < 0) {
1453
            VIR_FREE(boot_policy);
1454
            goto error;
1455 1456 1457
        }
        xen_vm_get_pv_kernel(session, &value, vm);
        if (STRNEQ(value, "")) {
1458
            if (VIR_STRDUP(defPtr->os.kernel, value) < 0) {
1459 1460
                VIR_FREE(boot_policy);
                VIR_FREE(value);
1461
                goto error;
1462 1463 1464 1465 1466
            }
            VIR_FREE(value);
        }
        xen_vm_get_pv_ramdisk(session, &value, vm);
        if (STRNEQ(value, "")) {
1467
            if (VIR_STRDUP(defPtr->os.initrd, value) < 0) {
1468 1469
                VIR_FREE(boot_policy);
                VIR_FREE(value);
1470
                goto error;
1471 1472 1473 1474 1475
            }
            VIR_FREE(value);
        }
        xen_vm_get_pv_args(session, &value, vm);
        if (STRNEQ(value, "")) {
1476
            if (VIR_STRDUP(defPtr->os.cmdline, value) < 0) {
1477 1478
                VIR_FREE(boot_policy);
                VIR_FREE(value);
1479
                goto error;
1480 1481 1482 1483
            }
            VIR_FREE(value);
        }
        VIR_FREE(boot_policy);
1484 1485
        if (VIR_STRDUP(defPtr->os.bootloader, "pygrub") < 0)
            goto error;
1486 1487 1488
    }
    xen_vm_get_pv_bootloader_args(session, &val, vm);
    if (STRNEQ(val, "")) {
1489
        if (VIR_STRDUP(defPtr->os.bootloaderArgs, val) < 0) {
1490
            VIR_FREE(val);
1491
            goto error;
1492 1493 1494 1495
        }
        VIR_FREE(val);
    }
    memory = xenapiDomainGetMaxMemory(dom);
1496
    virDomainDefSetMemoryTotal(defPtr, memory);
1497
    if (xen_vm_get_memory_dynamic_max(session, &dynamic_mem, vm)) {
1498
        defPtr->mem.cur_balloon = (unsigned long) (dynamic_mem / 1024);
1499
    } else {
1500
        defPtr->mem.cur_balloon = memory;
1501
    }
1502 1503 1504 1505 1506 1507

    vcpus = xenapiDomainGetMaxVcpus(dom);

    defPtr->maxvcpus = vcpus;
    defPtr->vcpus = vcpus;

1508
    enum xen_on_normal_exit action;
1509
    if (xen_vm_get_actions_after_shutdown(session, &action, vm))
1510
        defPtr->onPoweroff = xenapiNormalExitEnum2virDomainLifecycle(action);
1511
    if (xen_vm_get_actions_after_reboot(session, &action, vm))
1512 1513
        defPtr->onReboot = xenapiNormalExitEnum2virDomainLifecycle(action);
    enum xen_on_crash_behaviour crash;
1514
    if (xen_vm_get_actions_after_crash(session, &crash, vm))
1515
        defPtr->onCrash = xenapiCrashExitEnum2virDomainLifecycle(crash);
1516 1517
    xen_vm_get_platform(session, &result, vm);
    if (result != NULL) {
1518
        size_t i;
1519
        for (i = 0; i < result->size; i++) {
1520 1521
            if (STREQ(result->contents[i].val, "true")) {
                if (STREQ(result->contents[i].key, "acpi"))
J
Ján Tomko 已提交
1522
                    defPtr->features[VIR_DOMAIN_FEATURE_ACPI] = VIR_TRISTATE_SWITCH_ON;
1523
                else if (STREQ(result->contents[i].key, "apic"))
J
Ján Tomko 已提交
1524
                    defPtr->features[VIR_DOMAIN_FEATURE_APIC] = VIR_TRISTATE_SWITCH_ON;
1525
                else if (STREQ(result->contents[i].key, "pae"))
J
Ján Tomko 已提交
1526
                    defPtr->features[VIR_DOMAIN_FEATURE_PAE] = VIR_TRISTATE_SWITCH_ON;
1527
                else if (STREQ(result->contents[i].key, "hap"))
J
Ján Tomko 已提交
1528
                    defPtr->features[VIR_DOMAIN_FEATURE_HAP] = VIR_TRISTATE_SWITCH_ON;
1529
                else if (STREQ(result->contents[i].key, "viridian"))
J
Ján Tomko 已提交
1530
                    defPtr->features[VIR_DOMAIN_FEATURE_VIRIDIAN] = VIR_TRISTATE_SWITCH_ON;
1531 1532 1533 1534 1535 1536
            }
        }
        xen_string_string_map_free(result);
    }
    xen_vm_get_vifs(session, &vif_set, vm);
    if (vif_set) {
1537
        size_t i;
1538 1539 1540 1541 1542 1543 1544
        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);
1545
            goto error;
1546 1547 1548 1549
        }
        for (i = 0; i < vif_set->size; i++) {
            if (VIR_ALLOC(defPtr->nets[i]) < 0) {
                xen_vif_set_free(vif_set);
1550
                goto error;
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
            }
            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) {
J
Jiri Denemark 已提交
1563 1564
                if (virMacAddrParse((const char *)vif_rec->mac,
                                    &defPtr->nets[i]->mac) < 0)
1565
                    xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
1566
                                              _("Unable to parse given mac address"));
1567 1568 1569 1570 1571
                xen_vif_record_free(vif_rec);
            }
        }
        xen_vif_set_free(vif_set);
    }
1572
    xen_vm_set_free(vms);
E
Eric Blake 已提交
1573
    xml = virDomainDefFormat(defPtr, flags);
1574 1575 1576
    virDomainDefFree(defPtr);
    return xml;

1577
 error:
1578 1579 1580 1581 1582 1583
    xen_vm_set_free(vms);
    virDomainDefFree(defPtr);
    return NULL;
}

/*
1584
 * xenapiConnectListDefinedDomains
1585 1586 1587 1588 1589
 *
 * 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
1590 1591
xenapiConnectListDefinedDomains(virConnectPtr conn, char **const names,
                                int maxnames)
1592
{
1593 1594
    size_t i, j;
    int doms;
1595 1596 1597 1598
    xen_vm_set *result;
    xen_vm_record *record;
    xen_session *session = ((struct _xenapiPrivate *)(conn->privateData))->session;
    xen_vm_get_all(session, &result);
1599
    memset(names, 0, sizeof(names[0])*maxnames);
1600
    if (result != NULL) {
1601
        for (i = 0, j = 0; i < result->size && j < maxnames; i++) {
1602 1603 1604 1605
            xen_vm_get_record(session, &record, result->contents[i]);
            if (record != NULL) {
                if (record->is_a_template == 0) {
                    char *usenames = NULL;
1606
                    if (VIR_STRDUP(usenames, record->name_label) < 0) {
1607 1608
                        xen_vm_record_free(record);
                        xen_vm_set_free(result);
1609 1610
                        for (i = 0; i < maxnames; i++)
                            VIR_FREE(names[j]);
1611 1612 1613 1614 1615 1616
                        return -1;
                    }
                    names[j++] = usenames;
                }
                xen_vm_record_free(record);
            } else {
1617 1618
                xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                          _("Couldn't get VM record"));
1619
                xen_vm_set_free(result);
1620 1621 1622
                for (i = 0; i < maxnames; i++)
                    VIR_FREE(names[j]);
                return -1;
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
            }
        }
        doms = j;
        xen_vm_set_free(result);
        return doms;
    }
    xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR, NULL);
    return -1;
}

/*
1634
 * xenapiConnectNumOfDefinedDomains
1635 1636 1637 1638 1639
 *
 * Provides the number of defined but inactive domains
 * Returns number of domains found on success or -1 in case of error
 */
static int
1640
xenapiConnectNumOfDefinedDomains(virConnectPtr conn)
1641 1642 1643
{
    xen_vm_set *result;
    xen_vm_record *record;
1644 1645
    int DomNum = 0;
    size_t i;
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
    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;
            }
1656 1657 1658 1659 1660
            if (record) {
                if (record->is_a_template == 0)
                    DomNum++;
                xen_vm_record_free(record);
            }
1661 1662 1663 1664 1665 1666 1667 1668 1669
        }
        xen_vm_set_free(result);
        return DomNum;
    }
    xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR, NULL);
    return -1;
}

/*
1670
 * xenapiDomainCreateWithFlags
1671 1672 1673 1674 1675
 *
 * starts a VM
 * Return 0 on success or -1 in case of error
 */
static int
1676
xenapiDomainCreateWithFlags(virDomainPtr dom, unsigned int flags)
1677 1678 1679 1680
{
    xen_vm_set *vms;
    xen_vm vm;
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;
1681
    int64_t domid = -1;
1682 1683 1684

    virCheckFlags(0, -1);

1685 1686
    if (xen_vm_get_by_name_label(session, &vms, dom->name) && vms->size > 0) {
        if (vms->size != 1) {
1687 1688
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
1689 1690 1691 1692 1693 1694 1695 1696 1697
            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;
        }
1698 1699 1700 1701

        xen_vm_get_domid(session, &domid, vm);
        dom->id = domid;

1702 1703
        xen_vm_set_free(vms);
    } else {
1704 1705
        if (vms)
            xen_vm_set_free(vms);
1706 1707 1708 1709 1710 1711
        xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
        return -1;
    }
    return 0;
}

1712 1713 1714 1715 1716 1717 1718
/*
 * xenapiDomainCreate
 *
 * starts a VM
 * Return 0 on success or -1 in case of error
 */
static int
1719
xenapiDomainCreate(virDomainPtr dom)
1720 1721 1722 1723
{
    return xenapiDomainCreateWithFlags(dom, 0);
}

1724 1725 1726 1727 1728 1729 1730
/*
 * 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
1731
xenapiDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags)
1732
{
1733
    struct _xenapiPrivate *priv = conn->privateData;
1734 1735 1736
    xen_vm_record *record = NULL;
    xen_vm vm = NULL;
    virDomainPtr domP = NULL;
1737 1738 1739
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;

    virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL);
1740

1741 1742
    if (flags & VIR_DOMAIN_DEFINE_VALIDATE)
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE;
1743

1744
    if (!priv->caps)
1745
        return NULL;
1746 1747
    virDomainDefPtr defPtr = virDomainDefParseString(xml,
                                                     priv->caps, priv->xmlopt,
1748
                                                     parse_flags);
1749 1750
    if (!defPtr)
        return NULL;
M
Matthias Bolte 已提交
1751

1752
    if (createVMRecordFromXml(conn, defPtr, &record, &vm) != 0) {
1753
        if (!priv->session->ok)
1754 1755
            xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR, NULL);
        else
1756 1757
            xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Couldn't get VM information from XML"));
1758 1759 1760 1761 1762
        virDomainDefFree(defPtr);
        return NULL;
    }
    if (record != NULL) {
        unsigned char raw_uuid[VIR_UUID_BUFLEN];
1763
        ignore_value(virUUIDParse(record->uuid, raw_uuid));
1764
        domP = virGetDomain(conn, record->name_label, raw_uuid);
1765
        if (!domP && !priv->session->ok)
1766 1767 1768 1769 1770 1771 1772 1773 1774
            xenapiSessionErrorHandler(conn, VIR_ERR_NO_DOMAIN, NULL);
        xen_vm_record_free(record);
    }
    else if (vm != NULL)
        xen_vm_free(vm);
    virDomainDefFree(defPtr);
    return domP;
}

1775 1776 1777 1778 1779 1780
static virDomainPtr
xenapiDomainDefineXML(virConnectPtr conn, const char *xml)
{
    return xenapiDomainDefineXMLFlags(conn, xml, 0);
}

1781
/*
1782
 * xenapiDomainUndefineFlags
1783 1784 1785 1786 1787
 *
 * destroys a domain
 * Return 0 on success or -1 in case of error
 */
static int
1788
xenapiDomainUndefineFlags(virDomainPtr dom, unsigned int flags)
1789 1790 1791 1792
{
    struct xen_vm_set *vms;
    xen_vm vm;
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;
1793 1794
    virCheckFlags(0, -1);

1795 1796
    if (xen_vm_get_by_name_label(session, &vms, dom->name) && vms->size > 0) {
        if (vms->size != 1) {
1797 1798
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
            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;
    }
1811 1812
    if (vms)
        xen_vm_set_free(vms);
1813
    xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
1814 1815 1816
    return -1;
}

1817 1818 1819 1820 1821 1822
static int
xenapiDomainUndefine(virDomainPtr dom)
{
    return xenapiDomainUndefineFlags(dom, 0);
}

1823 1824 1825 1826 1827 1828 1829 1830
/*
 * 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
1831
xenapiDomainGetAutostart(virDomainPtr dom, int *autostart)
1832
{
1833 1834
    size_t i;
    int flag = 0;
1835 1836 1837 1838 1839 1840
    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) {
1841 1842
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863
            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);
1864 1865
        if (flag == 0)
            return -1;
1866 1867
        return 0;
    }
1868 1869
    if (vms)
        xen_vm_set_free(vms);
1870
    xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880
    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
1881
xenapiDomainSetAutostart(virDomainPtr dom, int autostart)
1882 1883 1884 1885 1886 1887 1888
{
    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) {
1889 1890
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
1891 1892 1893 1894 1895
            xen_vm_set_free(vms);
            return -1;
        }
        vm = vms->contents[0];
        xen_vm_remove_from_other_config(session, vm, (char *)"auto_poweron");
1896
        if (autostart == 1)
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907
            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;
    }
1908 1909
    if (vms)
        xen_vm_set_free(vms);
1910
    xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
1911 1912 1913 1914
    return -1;
}

static char *
1915
xenapiDomainGetSchedulerType(virDomainPtr dom ATTRIBUTE_UNUSED, int *nparams)
1916 1917
{
    char *result = NULL;
1918 1919 1920

    if (nparams)
        *nparams = 0;
1921
    ignore_value(VIR_STRDUP(result, "credit"));
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
    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
1932
xenapiNodeGetFreeMemory(virConnectPtr conn)
1933 1934 1935 1936 1937 1938 1939
{
    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])) {
1940 1941
            xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Couldn't get host metrics - memory information"));
1942 1943 1944 1945
            freeMem = 0;
        }
        xen_host_metrics_set_free(xen_met_set);
    } else {
1946 1947
        xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                  _("Couldn't get host metrics"));
1948 1949 1950 1951
    }
    return freeMem;
}

1952 1953 1954 1955 1956 1957
static int
xenapiDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

1958 1959 1960 1961 1962 1963 1964
/*
 * xenapiNodeGetCellsFreeMemory
 *
 *
 * Returns the number of entries filled in freeMems, or -1 in case of error.
 */
static int
1965 1966
xenapiNodeGetCellsFreeMemory(virConnectPtr conn, unsigned long long *freeMems,
                             int startCell, int maxCells)
1967 1968
{
    if (maxCells > 1 && startCell > 0) {
1969
        xenapiSessionErrorHandler(conn, VIR_ERR_NO_SUPPORT, NULL);
1970 1971 1972 1973 1974 1975 1976
        return -1;
    } else {
        freeMems[0] = xenapiNodeGetFreeMemory(conn);
        return 1;
    }
}

1977
static int
1978
xenapiConnectIsAlive(virConnectPtr conn)
1979 1980 1981 1982 1983 1984 1985 1986 1987
{
    struct _xenapiPrivate *priv = conn->privateData;

    if (priv->session && priv->session->ok)
        return 1;
    else
        return 0;
}

1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011
static int
xenapiDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
{
    struct xen_vm_set *vms;
    xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;

    virCheckFlags(0, -1);

    if (xen_vm_get_by_name_label(session, &vms, dom->name) && vms->size > 0) {
        if (vms->size != 1) {
            xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                      _("Domain name is not unique"));
            xen_vm_set_free(vms);
            return -1;
        }
        xen_vm_set_free(vms);
        return 0;
    }
    if (vms)
        xen_vm_set_free(vms);
    xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
    return -1;
}

2012
/* The interface which we export upwards to libvirt.c. */
2013
static virHypervisorDriver xenapiHypervisorDriver = {
2014
    .name = "XenAPI",
2015 2016 2017 2018 2019 2020 2021
    .connectOpen = xenapiConnectOpen, /* 0.8.0 */
    .connectClose = xenapiConnectClose, /* 0.8.0 */
    .connectSupportsFeature = xenapiConnectSupportsFeature, /* 0.8.0 */
    .connectGetType = xenapiConnectGetType, /* 0.8.0 */
    .connectGetVersion = xenapiConnectGetVersion, /* 0.8.0 */
    .connectGetHostname = xenapiConnectGetHostname, /* 0.8.0 */
    .connectGetMaxVcpus = xenapiConnectGetMaxVcpus, /* 0.8.0 */
2022
    .nodeGetInfo = xenapiNodeGetInfo, /* 0.8.0 */
2023 2024 2025
    .connectGetCapabilities = xenapiConnectGetCapabilities, /* 0.8.0 */
    .connectListDomains = xenapiConnectListDomains, /* 0.8.0 */
    .connectNumOfDomains = xenapiConnectNumOfDomains, /* 0.8.0 */
2026 2027 2028 2029 2030 2031 2032
    .domainCreateXML = xenapiDomainCreateXML, /* 0.8.0 */
    .domainLookupByID = xenapiDomainLookupByID, /* 0.8.0 */
    .domainLookupByUUID = xenapiDomainLookupByUUID, /* 0.8.0 */
    .domainLookupByName = xenapiDomainLookupByName, /* 0.8.0 */
    .domainSuspend = xenapiDomainSuspend, /* 0.8.0 */
    .domainResume = xenapiDomainResume, /* 0.8.0 */
    .domainShutdown = xenapiDomainShutdown, /* 0.8.0 */
2033
    .domainShutdownFlags = xenapiDomainShutdownFlags, /* 0.9.10 */
2034 2035
    .domainReboot = xenapiDomainReboot, /* 0.8.0 */
    .domainDestroy = xenapiDomainDestroy, /* 0.8.0 */
2036
    .domainDestroyFlags = xenapiDomainDestroyFlags, /* 0.9.4 */
2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048
    .domainGetOSType = xenapiDomainGetOSType, /* 0.8.0 */
    .domainGetMaxMemory = xenapiDomainGetMaxMemory, /* 0.8.0 */
    .domainSetMaxMemory = xenapiDomainSetMaxMemory, /* 0.8.0 */
    .domainGetInfo = xenapiDomainGetInfo, /* 0.8.0 */
    .domainGetState = xenapiDomainGetState, /* 0.9.2 */
    .domainSetVcpus = xenapiDomainSetVcpus, /* 0.8.0 */
    .domainSetVcpusFlags = xenapiDomainSetVcpusFlags, /* 0.8.5 */
    .domainGetVcpusFlags = xenapiDomainGetVcpusFlags, /* 0.8.5 */
    .domainPinVcpu = xenapiDomainPinVcpu, /* 0.8.0 */
    .domainGetVcpus = xenapiDomainGetVcpus, /* 0.8.0 */
    .domainGetMaxVcpus = xenapiDomainGetMaxVcpus, /* 0.8.0 */
    .domainGetXMLDesc = xenapiDomainGetXMLDesc, /* 0.8.0 */
2049 2050
    .connectListDefinedDomains = xenapiConnectListDefinedDomains, /* 0.8.0 */
    .connectNumOfDefinedDomains = xenapiConnectNumOfDefinedDomains, /* 0.8.0 */
2051 2052 2053
    .domainCreate = xenapiDomainCreate, /* 0.8.0 */
    .domainCreateWithFlags = xenapiDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = xenapiDomainDefineXML, /* 0.8.0 */
2054
    .domainDefineXMLFlags = xenapiDomainDefineXMLFlags, /* 1.2.12 */
2055
    .domainUndefine = xenapiDomainUndefine, /* 0.8.0 */
2056
    .domainUndefineFlags = xenapiDomainUndefineFlags, /* 0.9.5 */
2057 2058 2059 2060 2061 2062
    .domainGetAutostart = xenapiDomainGetAutostart, /* 0.8.0 */
    .domainSetAutostart = xenapiDomainSetAutostart, /* 0.8.0 */
    .domainGetSchedulerType = xenapiDomainGetSchedulerType, /* 0.8.0 */
    .nodeGetCellsFreeMemory = xenapiNodeGetCellsFreeMemory, /* 0.8.0 */
    .nodeGetFreeMemory = xenapiNodeGetFreeMemory, /* 0.8.0 */
    .domainIsUpdated = xenapiDomainIsUpdated, /* 0.8.6 */
2063
    .connectIsAlive = xenapiConnectIsAlive, /* 0.9.8 */
2064
    .domainHasManagedSaveImage = xenapiDomainHasManagedSaveImage, /* 1.2.13 */
2065 2066
};

2067 2068 2069 2070 2071

static virConnectDriver xenapiConnectDriver = {
    .hypervisorDriver = &xenapiHypervisorDriver,
};

2072 2073 2074 2075 2076 2077 2078
/**
 * xenapiRegister:
 *
 *
 * Returns the driver priority or -1 in case of error.
 */
int
2079
xenapiRegister(void)
2080
{
2081 2082
    return virRegisterConnectDriver(&xenapiConnectDriver,
                                    false);
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093
}

/*
 * 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 已提交
2094 2095
#ifdef PRINT_XML
    printf("\n\n---Result from server -----------------------\n");
E
Eric Blake 已提交
2096
    printf("%s\n", (char*) ptr);
J
Jiri Denemark 已提交
2097 2098
    fflush(stdout);
#endif
2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110
    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)
{
    struct _xenapiPrivate *priv = (struct _xenapiPrivate *)user_handle;
J
Jiri Denemark 已提交
2111 2112
#ifdef PRINT_XML
    printf("\n\n---Data to server: -----------------------\n");
E
Eric Blake 已提交
2113
    printf("%s\n", (char*) data);
J
Jiri Denemark 已提交
2114 2115
    fflush(stdout);
#endif
2116
    CURL *curl = curl_easy_init();
2117
    if (!curl)
2118 2119 2120 2121 2122 2123 2124
      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 已提交
2125 2126 2127
#ifdef CURLOPT_MUTE
    curl_easy_setopt(curl, CURLOPT_MUTE, 1);
#endif
2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138
    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;
}