libxl_driver.c 149.4 KB
Newer Older
1 2 3
/*
 * libxl_driver.c: core driver methods for managing libxenlight domains
 *
4
 * Copyright (C) 2006-2014 Red Hat, Inc.
5
 * Copyright (C) 2011-2015 SUSE LINUX Products GmbH, Nuernberg, Germany.
6
 * Copyright (C) 2011 Univention GmbH.
J
Jim Fehlig 已提交
7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
19
 * License along with this library.  If not, see
O
Osier Yang 已提交
20
 * <http://www.gnu.org/licenses/>.
M
Markus Groß 已提交
21 22 23 24 25
 *
 * Authors:
 *     Jim Fehlig <jfehlig@novell.com>
 *     Markus Groß <gross@univention.de>
 *     Daniel P. Berrange <berrange@redhat.com>
J
Jim Fehlig 已提交
26 27 28 29
 */

#include <config.h>

30
#include <math.h>
J
Jim Fehlig 已提交
31
#include <libxl.h>
32
#include <libxl_utils.h>
33
#include <fcntl.h>
34
#include <regex.h>
J
Jim Fehlig 已提交
35 36

#include "internal.h"
37
#include "virlog.h"
38
#include "virerror.h"
39
#include "virconf.h"
J
Jim Fehlig 已提交
40
#include "datatypes.h"
E
Eric Blake 已提交
41
#include "virfile.h"
42
#include "viralloc.h"
43
#include "viruuid.h"
44
#include "vircommand.h"
J
Jim Fehlig 已提交
45
#include "libxl_domain.h"
J
Jim Fehlig 已提交
46 47
#include "libxl_driver.h"
#include "libxl_conf.h"
J
Jim Fehlig 已提交
48
#include "libxl_migration.h"
49
#include "xen_xm.h"
50
#include "xen_sxpr.h"
51
#include "xen_xl.h"
52
#include "virtypedparam.h"
M
Martin Kletzander 已提交
53
#include "viruri.h"
54
#include "virstring.h"
55
#include "virsysinfo.h"
56
#include "viraccessapicheck.h"
57
#include "viratomic.h"
58
#include "virhostdev.h"
59
#include "network/bridge_driver.h"
J
Jim Fehlig 已提交
60 61 62

#define VIR_FROM_THIS VIR_FROM_LIBXL

63 64
VIR_LOG_INIT("libxl.libxl_driver");

J
Jim Fehlig 已提交
65 66 67 68 69 70
#define LIBXL_DOM_REQ_POWEROFF 0
#define LIBXL_DOM_REQ_REBOOT   1
#define LIBXL_DOM_REQ_SUSPEND  2
#define LIBXL_DOM_REQ_CRASH    3
#define LIBXL_DOM_REQ_HALT     4

71
#define LIBXL_CONFIG_FORMAT_XL "xen-xl"
72
#define LIBXL_CONFIG_FORMAT_XM "xen-xm"
73
#define LIBXL_CONFIG_FORMAT_SEXPR "xen-sxpr"
74

75 76
#define HYPERVISOR_CAPABILITIES "/proc/xen/capabilities"

77 78 79
/* Number of Xen scheduler parameters */
#define XEN_SCHED_CREDIT_NPARAM   2

80

81
static libxlDriverPrivatePtr libxl_driver;
J
Jim Fehlig 已提交
82

83 84 85 86 87 88 89 90 91
/* Object used to store info related to libxl event registrations */
typedef struct _libxlOSEventHookInfo libxlOSEventHookInfo;
typedef libxlOSEventHookInfo *libxlOSEventHookInfoPtr;
struct _libxlOSEventHookInfo {
    libxl_ctx *ctx;
    void *xl_priv;
    int id;
};

J
Jim Fehlig 已提交
92
/* Function declarations */
93 94
static int
libxlDomainManagedSaveLoad(virDomainObjPtr vm,
95 96
                           void *opaque);

J
Jim Fehlig 已提交
97 98

/* Function definitions */
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
static void
libxlOSEventHookInfoFree(void *obj)
{
    VIR_FREE(obj);
}

static void
libxlFDEventCallback(int watch ATTRIBUTE_UNUSED,
                     int fd,
                     int vir_events,
                     void *fd_info)
{
    libxlOSEventHookInfoPtr info = fd_info;
    int events = 0;

    if (vir_events & VIR_EVENT_HANDLE_READABLE)
        events |= POLLIN;
    if (vir_events & VIR_EVENT_HANDLE_WRITABLE)
        events |= POLLOUT;
    if (vir_events & VIR_EVENT_HANDLE_ERROR)
        events |= POLLERR;
    if (vir_events & VIR_EVENT_HANDLE_HANGUP)
        events |= POLLHUP;

    libxl_osevent_occurred_fd(info->ctx, info->xl_priv, fd, 0, events);
}

static int
libxlFDRegisterEventHook(void *priv,
                         int fd,
                         void **hndp,
                         short events,
                         void *xl_priv)
{
    int vir_events = VIR_EVENT_HANDLE_ERROR;
    libxlOSEventHookInfoPtr info;

    if (VIR_ALLOC(info) < 0)
        return -1;

    info->ctx = priv;
    info->xl_priv = xl_priv;

    if (events & POLLIN)
        vir_events |= VIR_EVENT_HANDLE_READABLE;
    if (events & POLLOUT)
        vir_events |= VIR_EVENT_HANDLE_WRITABLE;

    info->id = virEventAddHandle(fd, vir_events, libxlFDEventCallback,
                                 info, libxlOSEventHookInfoFree);
    if (info->id < 0) {
        VIR_FREE(info);
        return -1;
    }

    *hndp = info;

    return 0;
}

static int
libxlFDModifyEventHook(void *priv ATTRIBUTE_UNUSED,
                       int fd ATTRIBUTE_UNUSED,
                       void **hndp,
                       short events)
{
    libxlOSEventHookInfoPtr info = *hndp;
    int vir_events = VIR_EVENT_HANDLE_ERROR;

    if (events & POLLIN)
        vir_events |= VIR_EVENT_HANDLE_READABLE;
    if (events & POLLOUT)
        vir_events |= VIR_EVENT_HANDLE_WRITABLE;

    virEventUpdateHandle(info->id, vir_events);

    return 0;
}

static void
libxlFDDeregisterEventHook(void *priv ATTRIBUTE_UNUSED,
                           int fd ATTRIBUTE_UNUSED,
                           void *hnd)
{
    libxlOSEventHookInfoPtr info = hnd;

    virEventRemoveHandle(info->id);
}

static void
libxlTimerCallback(int timer ATTRIBUTE_UNUSED, void *timer_info)
{
    libxlOSEventHookInfoPtr info = timer_info;

    /*
     * libxl expects the event to be deregistered when calling
     * libxl_osevent_occurred_timeout, but we dont want the event info
     * destroyed.  Disable the timeout and only remove it after returning
     * from libxl.
     */
    virEventUpdateTimeout(info->id, -1);
    libxl_osevent_occurred_timeout(info->ctx, info->xl_priv);
    virEventRemoveTimeout(info->id);
}

static int
libxlTimeoutRegisterEventHook(void *priv,
                              void **hndp,
                              struct timeval abs_t,
                              void *xl_priv)
{
    libxlOSEventHookInfoPtr info;
    struct timeval now;
    struct timeval res;
    static struct timeval zero;
    int timeout;

    if (VIR_ALLOC(info) < 0)
        return -1;

    info->ctx = priv;
    info->xl_priv = xl_priv;

    gettimeofday(&now, NULL);
    timersub(&abs_t, &now, &res);
    /* Ensure timeout is not overflowed */
    if (timercmp(&res, &zero, <)) {
        timeout = 0;
    } else if (res.tv_sec > INT_MAX / 1000) {
        timeout = INT_MAX;
    } else {
        timeout = res.tv_sec * 1000 + (res.tv_usec + 999) / 1000;
    }
    info->id = virEventAddTimeout(timeout, libxlTimerCallback,
                                  info, libxlOSEventHookInfoFree);
    if (info->id < 0) {
        VIR_FREE(info);
        return -1;
    }

    *hndp = info;

    return 0;
}

/*
 * Note:  There are two changes wrt timeouts starting with xen-unstable
 * changeset 26469:
 *
 * 1. Timeout modify callbacks will only be invoked with an abs_t of {0,0},
 * i.e. make the timeout fire immediately.  Prior to this commit, timeout
 * modify callbacks were never invoked.
 *
 * 2. Timeout deregister hooks will no longer be called.
 */
static int
libxlTimeoutModifyEventHook(void *priv ATTRIBUTE_UNUSED,
                            void **hndp,
                            struct timeval abs_t ATTRIBUTE_UNUSED)
{
    libxlOSEventHookInfoPtr info = *hndp;

    /* Make the timeout fire */
    virEventUpdateTimeout(info->id, 0);

    return 0;
}

static void
libxlTimeoutDeregisterEventHook(void *priv ATTRIBUTE_UNUSED,
                                void *hnd)
{
    libxlOSEventHookInfoPtr info = hnd;

    virEventRemoveTimeout(info->id);
}

J
Jim Fehlig 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
static virDomainObjPtr
libxlDomObjFromDomain(virDomainPtr dom)
{
    virDomainObjPtr vm;
    libxlDriverPrivatePtr driver = dom->conn->privateData;
    char uuidstr[VIR_UUID_STRING_BUFLEN];

    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
    if (!vm) {
        virUUIDFormat(dom->uuid, uuidstr);
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching uuid '%s' (%s)"),
                       uuidstr, dom->name);
        return NULL;
    }

    return vm;
}

295 296
static int
libxlAutostartDomain(virDomainObjPtr vm,
297 298 299 300
                     void *opaque)
{
    libxlDriverPrivatePtr driver = opaque;
    virErrorPtr err;
301
    int ret = -1;
302

303
    virObjectLock(vm);
304 305
    virResetLastError();

306 307 308 309 310
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0) {
        virObjectUnlock(vm);
        return ret;
    }

311
    if (vm->autostart && !virDomainObjIsActive(vm) &&
312
        libxlDomainStart(driver, vm, false, -1) < 0) {
313 314 315 316
        err = virGetLastError();
        VIR_ERROR(_("Failed to autostart VM '%s': %s"),
                  vm->def->name,
                  err ? err->message : _("unknown error"));
317
        goto endjob;
318 319
    }

320
    ret = 0;
321 322 323 324 325

 endjob:
    if (libxlDomainObjEndJob(driver, vm))
        virObjectUnlock(vm);

326
    return ret;
327 328
}

J
Jim Fehlig 已提交
329 330 331 332
/*
 * Reconnect to running domains that were previously started/created
 * with libxenlight driver.
 */
333 334
static int
libxlReconnectDomain(virDomainObjPtr vm,
J
Jim Fehlig 已提交
335 336 337
                     void *opaque)
{
    libxlDriverPrivatePtr driver = opaque;
338
    libxlDomainObjPrivatePtr priv = vm->privateData;
J
Jim Fehlig 已提交
339
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
J
Jim Fehlig 已提交
340 341 342 343
    int rc;
    libxl_dominfo d_info;
    int len;
    uint8_t *data = NULL;
344
    virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;
J
Jim Fehlig 已提交
345

346
    virObjectLock(vm);
J
Jim Fehlig 已提交
347 348

    /* Does domain still exist? */
J
Jim Fehlig 已提交
349
    rc = libxl_domain_info(cfg->ctx, &d_info, vm->def->id);
J
Jim Fehlig 已提交
350 351 352 353 354 355 356 357 358
    if (rc == ERROR_INVAL) {
        goto out;
    } else if (rc != 0) {
        VIR_DEBUG("libxl_domain_info failed (code %d), ignoring domain %d",
                  rc, vm->def->id);
        goto out;
    }

    /* Is this a domain that was under libvirt control? */
J
Jim Fehlig 已提交
359
    if (libxl_userdata_retrieve(cfg->ctx, vm->def->id,
J
Jim Fehlig 已提交
360 361 362 363 364 365 366
                                "libvirt-xml", &data, &len)) {
        VIR_DEBUG("libxl_userdata_retrieve failed, ignoring domain %d", vm->def->id);
        goto out;
    }

    /* Update domid in case it changed (e.g. reboot) while we were gone? */
    vm->def->id = d_info.domid;
367 368 369 370 371 372

    /* Update hostdev state */
    if (virHostdevUpdateDomainActiveDevices(hostdev_mgr, LIBXL_DRIVER_NAME,
                                            vm->def, VIR_HOSTDEV_SP_PCI) < 0)
        goto out;

J
Jiri Denemark 已提交
373
    virDomainObjSetState(vm, VIR_DOMAIN_RUNNING, VIR_DOMAIN_RUNNING_UNKNOWN);
J
Jim Fehlig 已提交
374

375
    if (virAtomicIntInc(&driver->nactive) == 1 && driver->inhibitCallback)
376 377
        driver->inhibitCallback(true, driver->inhibitOpaque);

378
    /* Enable domain death events */
J
Jim Fehlig 已提交
379
    libxl_evenable_domain_death(cfg->ctx, vm->def->id, 0, &priv->deathW);
380

381
    virObjectUnlock(vm);
J
Jim Fehlig 已提交
382
    virObjectUnref(cfg);
383
    return 0;
J
Jim Fehlig 已提交
384

385
 out:
386
    libxlDomainCleanup(driver, vm, VIR_DOMAIN_SHUTOFF_UNKNOWN);
J
Jim Fehlig 已提交
387
    if (!vm->persistent)
388
        virDomainObjListRemoveLocked(driver->domains, vm);
J
Jim Fehlig 已提交
389
    else
390
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
391
    virObjectUnref(cfg);
392 393

    return -1;
J
Jim Fehlig 已提交
394 395 396 397 398
}

static void
libxlReconnectDomains(libxlDriverPrivatePtr driver)
{
399
    virDomainObjListForEach(driver->domains, libxlReconnectDomain, driver);
J
Jim Fehlig 已提交
400 401 402
}

static int
403
libxlStateCleanup(void)
J
Jim Fehlig 已提交
404 405 406 407
{
    if (!libxl_driver)
        return -1;

408
    virObjectUnref(libxl_driver->hostdevMgr);
409
    virObjectUnref(libxl_driver->config);
410
    virObjectUnref(libxl_driver->xmlopt);
411
    virObjectUnref(libxl_driver->domains);
412
    virObjectUnref(libxl_driver->reservedVNCPorts);
J
Jim Fehlig 已提交
413
    virObjectUnref(libxl_driver->migrationPorts);
J
Jim Fehlig 已提交
414

415
    virObjectEventStateFree(libxl_driver->domainEventState);
416
    virSysinfoDefFree(libxl_driver->hostsysinfo);
417

J
Jim Fehlig 已提交
418 419 420 421 422 423
    virMutexDestroy(&libxl_driver->lock);
    VIR_FREE(libxl_driver);

    return 0;
}

424 425
static bool
libxlDriverShouldLoad(bool privileged)
426
{
427 428
    bool ret = false;
    int status;
429
    char *output = NULL;
J
Jim Fehlig 已提交
430

431
    /* Don't load if non-root */
J
Jim Fehlig 已提交
432
    if (!privileged) {
433
        VIR_INFO("Not running privileged, disabling libxenlight driver");
434
        return ret;
J
Jim Fehlig 已提交
435 436
    }

437 438 439
    if (!virFileExists(HYPERVISOR_CAPABILITIES)) {
        VIR_INFO("Disabling driver as " HYPERVISOR_CAPABILITIES
                 " does not exist");
440
        return ret;
441
    }
442 443 444 445 446
    /*
     * Don't load if not running on a Xen control domain (dom0). It is not
     * sufficient to check for the file to exist as any guest can mount
     * xenfs to /proc/xen.
     */
447
    status = virFileReadAll(HYPERVISOR_CAPABILITIES, 10, &output);
448
    if (status >= 0)
449 450 451
        status = strncmp(output, "control_d", 9);
    VIR_FREE(output);
    if (status) {
452 453 454 455 456 457 458
        VIR_INFO("No Xen capabilities detected, probably not running "
                 "in a Xen Dom0.  Disabling libxenlight driver");

        return ret;
    }

    /* Don't load if legacy xen toolstack (xend) is in use */
459 460 461 462 463 464 465 466 467 468 469
    if (virFileExists("/usr/sbin/xend")) {
        virCommandPtr cmd;

        cmd = virCommandNewArgList("/usr/sbin/xend", "status", NULL);
        if (virCommandRun(cmd, NULL) == 0) {
            VIR_INFO("Legacy xen tool stack seems to be in use, disabling "
                     "libxenlight driver.");
        } else {
            ret = true;
        }
        virCommandFree(cmd);
470 471
    } else {
        ret = true;
J
Jim Fehlig 已提交
472 473
    }

474 475 476
    return ret;
}

477 478 479 480 481 482 483 484 485 486
/* Callbacks wrapping libvirt's event loop interface */
static const libxl_osevent_hooks libxl_osevent_callbacks = {
    .fd_register = libxlFDRegisterEventHook,
    .fd_modify = libxlFDModifyEventHook,
    .fd_deregister = libxlFDDeregisterEventHook,
    .timeout_register = libxlTimeoutRegisterEventHook,
    .timeout_modify = libxlTimeoutModifyEventHook,
    .timeout_deregister = libxlTimeoutDeregisterEventHook,
};

487 488 489 490 491 492 493 494
static const libxl_childproc_hooks libxl_child_hooks = {
#ifdef LIBXL_HAVE_SIGCHLD_OWNER_SELECTIVE_REAP
    .chldowner = libxl_sigchld_owner_libxl_always_selective_reap,
#else
    .chldowner = libxl_sigchld_owner_libxl,
#endif
};

495 496 497 498 499 500
const struct libxl_event_hooks ev_hooks = {
    .event_occurs_mask = LIBXL_EVENTMASK_ALL,
    .event_occurs = libxlDomainEventHandler,
    .disaster = NULL,
};

501 502 503 504 505
static int
libxlStateInitialize(bool privileged,
                     virStateInhibitCallback callback ATTRIBUTE_UNUSED,
                     void *opaque ATTRIBUTE_UNUSED)
{
506
    libxlDriverConfigPtr cfg;
507
    char *driverConf = NULL;
508 509 510 511 512
    char ebuf[1024];

    if (!libxlDriverShouldLoad(privileged))
        return 0;

J
Jim Fehlig 已提交
513 514 515 516
    if (VIR_ALLOC(libxl_driver) < 0)
        return -1;

    if (virMutexInit(&libxl_driver->lock) < 0) {
517 518
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("cannot initialize mutex"));
J
Jim Fehlig 已提交
519 520 521 522 523
        VIR_FREE(libxl_driver);
        return -1;
    }

    /* Allocate bitmap for vnc port reservation */
524
    if (!(libxl_driver->reservedVNCPorts =
J
Ján Tomko 已提交
525 526
          virPortAllocatorNew(_("VNC"),
                              LIBXL_VNC_PORT_MIN,
527 528
                              LIBXL_VNC_PORT_MAX,
                              0)))
529
        goto error;
J
Jim Fehlig 已提交
530

J
Jim Fehlig 已提交
531 532 533 534
    /* Allocate bitmap for migration port reservation */
    if (!(libxl_driver->migrationPorts =
          virPortAllocatorNew(_("migration"),
                              LIBXL_MIGRATION_PORT_MIN,
535
                              LIBXL_MIGRATION_PORT_MAX, 0)))
J
Jim Fehlig 已提交
536 537
        goto error;

538 539
    if (!(libxl_driver->domains = virDomainObjListNew()))
        goto error;
J
Jim Fehlig 已提交
540

541 542 543
    if (!(libxl_driver->hostdevMgr = virHostdevManagerGetDefault()))
        goto error;

544
    if (!(cfg = libxlDriverConfigNew()))
545
        goto error;
J
Jim Fehlig 已提交
546

547 548 549 550 551 552 553
    if (virAsprintf(&driverConf, "%s/libxl.conf", cfg->configBaseDir) < 0)
        goto error;

    if (libxlDriverConfigLoadFile(cfg, driverConf) < 0)
        goto error;
    VIR_FREE(driverConf);

554 555 556
    /* Register the callbacks providing access to libvirt's event loop */
    libxl_osevent_register_hooks(cfg->ctx, &libxl_osevent_callbacks, cfg->ctx);

557 558 559
    /* Setup child process handling.  See $xen-src/tools/libxl/libxl_event.h */
    libxl_childproc_setmode(cfg->ctx, &libxl_child_hooks, cfg->ctx);

560 561 562
    /* Register callback to handle domain events */
    libxl_event_register_callbacks(cfg->ctx, &ev_hooks, libxl_driver);

563 564
    libxl_driver->config = cfg;
    if (virFileMakePath(cfg->stateDir) < 0) {
565 566
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("failed to create state dir '%s': %s"),
567
                       cfg->stateDir,
568
                       virStrerror(errno, ebuf, sizeof(ebuf)));
J
Jim Fehlig 已提交
569 570
        goto error;
    }
571
    if (virFileMakePath(cfg->libDir) < 0) {
572 573
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("failed to create lib dir '%s': %s"),
574
                       cfg->libDir,
575
                       virStrerror(errno, ebuf, sizeof(ebuf)));
J
Jim Fehlig 已提交
576 577
        goto error;
    }
578
    if (virFileMakePath(cfg->saveDir) < 0) {
579 580
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("failed to create save dir '%s': %s"),
581
                       cfg->saveDir,
582
                       virStrerror(errno, ebuf, sizeof(ebuf)));
J
Jim Fehlig 已提交
583 584
        goto error;
    }
585 586 587 588 589 590 591
    if (virFileMakePath(cfg->autoDumpDir) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("failed to create dump dir '%s': %s"),
                       cfg->autoDumpDir,
                       virStrerror(errno, ebuf, sizeof(ebuf)));
        goto error;
    }
J
Jim Fehlig 已提交
592

593
    /* read the host sysinfo */
594
    libxl_driver->hostsysinfo = virSysinfoRead();
595

596
    libxl_driver->domainEventState = virObjectEventStateNew();
E
Eric Blake 已提交
597
    if (!libxl_driver->domainEventState)
598 599
        goto error;

600
    if ((cfg->caps = libxlMakeCapabilities(cfg->ctx)) == NULL) {
601 602
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("cannot create capabilities for libxenlight"));
J
Jim Fehlig 已提交
603 604 605
        goto error;
    }

606
    if (!(libxl_driver->xmlopt = libxlCreateXMLConf()))
607
        goto error;
J
Jim Fehlig 已提交
608 609

    /* Load running domains first. */
610
    if (virDomainObjListLoadAllConfigs(libxl_driver->domains,
611 612
                                       cfg->stateDir,
                                       cfg->autostartDir,
613
                                       1,
614
                                       cfg->caps,
615
                                       libxl_driver->xmlopt,
616
                                       NULL, NULL) < 0)
J
Jim Fehlig 已提交
617 618 619 620 621
        goto error;

    libxlReconnectDomains(libxl_driver);

    /* Then inactive persistent configs */
622
    if (virDomainObjListLoadAllConfigs(libxl_driver->domains,
623 624
                                       cfg->configDir,
                                       cfg->autostartDir,
625
                                       0,
626
                                       cfg->caps,
627
                                       libxl_driver->xmlopt,
628
                                       NULL, NULL) < 0)
J
Jim Fehlig 已提交
629 630
        goto error;

631 632
    virDomainObjListForEach(libxl_driver->domains, libxlDomainManagedSaveLoad,
                            libxl_driver);
633

J
Jim Fehlig 已提交
634 635
    return 0;

636
 error:
637
    VIR_FREE(driverConf);
638
    libxlStateCleanup();
639
    return -1;
J
Jim Fehlig 已提交
640 641
}

642 643 644 645 646 647 648 649 650 651
static void
libxlStateAutoStart(void)
{
    if (!libxl_driver)
        return;

    virDomainObjListForEach(libxl_driver->domains, libxlAutostartDomain,
                            libxl_driver);
}

J
Jim Fehlig 已提交
652
static int
653
libxlStateReload(void)
J
Jim Fehlig 已提交
654
{
655 656
    libxlDriverConfigPtr cfg;

J
Jim Fehlig 已提交
657 658 659
    if (!libxl_driver)
        return 0;

660 661
    cfg = libxlDriverConfigGet(libxl_driver);

662
    virDomainObjListLoadAllConfigs(libxl_driver->domains,
663 664
                                   cfg->configDir,
                                   cfg->autostartDir,
665
                                   1,
666
                                   cfg->caps,
667
                                   libxl_driver->xmlopt,
668 669
                                   NULL, libxl_driver);

670 671
    virDomainObjListForEach(libxl_driver->domains, libxlAutostartDomain,
                            libxl_driver);
672

673
    virObjectUnref(cfg);
J
Jim Fehlig 已提交
674 675 676 677 678
    return 0;
}


static virDrvOpenStatus
679 680 681
libxlConnectOpen(virConnectPtr conn,
                 virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                 unsigned int flags)
J
Jim Fehlig 已提交
682
{
E
Eric Blake 已提交
683 684
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

J
Jim Fehlig 已提交
685 686 687 688
    if (conn->uri == NULL) {
        if (libxl_driver == NULL)
            return VIR_DRV_OPEN_DECLINED;

689
        if (!(conn->uri = virURIParse("xen:///")))
J
Jim Fehlig 已提交
690 691 692 693 694 695 696 697 698 699 700 701
            return VIR_DRV_OPEN_ERROR;
    } else {
        /* Only xen scheme */
        if (conn->uri->scheme == NULL || STRNEQ(conn->uri->scheme, "xen"))
            return VIR_DRV_OPEN_DECLINED;

        /* If server name is given, its for remote driver */
        if (conn->uri->server != NULL)
            return VIR_DRV_OPEN_DECLINED;

        /* Error if xen or libxl scheme specified but driver not started. */
        if (libxl_driver == NULL) {
702 703
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("libxenlight state driver is not active"));
J
Jim Fehlig 已提交
704 705 706 707 708 709 710 711
            return VIR_DRV_OPEN_ERROR;
        }

        /* /session isn't supported in libxenlight */
        if (conn->uri->path &&
            STRNEQ(conn->uri->path, "") &&
            STRNEQ(conn->uri->path, "/") &&
            STRNEQ(conn->uri->path, "/system")) {
712 713 714
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unexpected Xen URI path '%s', try xen:///"),
                           NULLSTR(conn->uri->path));
J
Jim Fehlig 已提交
715 716 717 718
            return VIR_DRV_OPEN_ERROR;
        }
    }

719 720 721
    if (virConnectOpenEnsureACL(conn) < 0)
        return VIR_DRV_OPEN_ERROR;

J
Jim Fehlig 已提交
722 723 724 725 726 727
    conn->privateData = libxl_driver;

    return VIR_DRV_OPEN_SUCCESS;
};

static int
728
libxlConnectClose(virConnectPtr conn ATTRIBUTE_UNUSED)
J
Jim Fehlig 已提交
729 730 731 732 733 734
{
    conn->privateData = NULL;
    return 0;
}

static const char *
735
libxlConnectGetType(virConnectPtr conn)
J
Jim Fehlig 已提交
736
{
737 738 739
    if (virConnectGetTypeEnsureACL(conn) < 0)
        return NULL;

J
Jim Fehlig 已提交
740
    return "Xen";
J
Jim Fehlig 已提交
741 742 743
}

static int
744
libxlConnectGetVersion(virConnectPtr conn, unsigned long *version)
J
Jim Fehlig 已提交
745 746
{
    libxlDriverPrivatePtr driver = conn->privateData;
747
    libxlDriverConfigPtr cfg;
J
Jim Fehlig 已提交
748

749 750 751
    if (virConnectGetVersionEnsureACL(conn) < 0)
        return 0;

752 753 754
    cfg = libxlDriverConfigGet(driver);
    *version = cfg->version;
    virObjectUnref(cfg);
J
Jim Fehlig 已提交
755 756 757
    return 0;
}

758

759
static char *libxlConnectGetHostname(virConnectPtr conn)
760
{
761 762 763
    if (virConnectGetHostnameEnsureACL(conn) < 0)
        return NULL;

764 765 766
    return virGetHostname();
}

767 768 769 770 771 772 773 774
static char *
libxlConnectGetSysinfo(virConnectPtr conn, unsigned int flags)
{
    libxlDriverPrivatePtr driver = conn->privateData;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    virCheckFlags(0, NULL);

775 776 777
    if (virConnectGetSysinfoEnsureACL(conn) < 0)
        return NULL;

778 779 780 781 782 783 784 785
    if (!driver->hostsysinfo) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Host SMBIOS information is not available"));
        return NULL;
    }

    if (virSysinfoFormat(&buf, driver->hostsysinfo) < 0)
        return NULL;
786
    if (virBufferCheckError(&buf) < 0)
787 788 789
        return NULL;
    return virBufferContentAndReset(&buf);
}
790

J
Jim Fehlig 已提交
791
static int
792
libxlConnectGetMaxVcpus(virConnectPtr conn, const char *type ATTRIBUTE_UNUSED)
J
Jim Fehlig 已提交
793 794 795
{
    int ret;
    libxlDriverPrivatePtr driver = conn->privateData;
796
    libxlDriverConfigPtr cfg;
J
Jim Fehlig 已提交
797

798 799 800
    if (virConnectGetMaxVcpusEnsureACL(conn) < 0)
        return -1;

801 802
    cfg = libxlDriverConfigGet(driver);
    ret = libxl_get_max_cpus(cfg->ctx);
803 804 805 806 807
    /* On failure, libxl_get_max_cpus() will return ERROR_FAIL from Xen 4.4
     * onward, but it ever returning 0 is obviously wrong too (and it is
     * what happens, on failure, on Xen 4.3 and earlier). Therefore, a 'less
     * or equal' is the catchall we want. */
    if (ret <= 0)
808
        ret = -1;
J
Jim Fehlig 已提交
809

810
    virObjectUnref(cfg);
J
Jim Fehlig 已提交
811 812 813 814 815 816
    return ret;
}

static int
libxlNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info)
{
817 818 819
    if (virNodeGetInfoEnsureACL(conn) < 0)
        return -1;

820
    return libxlDriverNodeGetInfo(conn->privateData, info);
J
Jim Fehlig 已提交
821 822 823
}

static char *
824
libxlConnectGetCapabilities(virConnectPtr conn)
J
Jim Fehlig 已提交
825 826 827
{
    libxlDriverPrivatePtr driver = conn->privateData;
    char *xml;
828
    libxlDriverConfigPtr cfg;
J
Jim Fehlig 已提交
829

830 831 832
    if (virConnectGetCapabilitiesEnsureACL(conn) < 0)
        return NULL;

833
    cfg = libxlDriverConfigGet(driver);
834
    xml = virCapabilitiesFormatXML(cfg->caps);
J
Jim Fehlig 已提交
835

836
    virObjectUnref(cfg);
J
Jim Fehlig 已提交
837 838 839 840
    return xml;
}

static int
841
libxlConnectListDomains(virConnectPtr conn, int *ids, int nids)
J
Jim Fehlig 已提交
842 843 844 845
{
    libxlDriverPrivatePtr driver = conn->privateData;
    int n;

846 847 848
    if (virConnectListDomainsEnsureACL(conn) < 0)
        return -1;

849 850
    n = virDomainObjListGetActiveIDs(driver->domains, ids, nids,
                                     virConnectListDomainsCheckACL, conn);
J
Jim Fehlig 已提交
851 852 853 854 855

    return n;
}

static int
856
libxlConnectNumOfDomains(virConnectPtr conn)
J
Jim Fehlig 已提交
857 858 859 860
{
    libxlDriverPrivatePtr driver = conn->privateData;
    int n;

861 862 863
    if (virConnectNumOfDomainsEnsureACL(conn) < 0)
        return -1;

864 865
    n = virDomainObjListNumOfDomains(driver->domains, true,
                                     virConnectNumOfDomainsCheckACL, conn);
J
Jim Fehlig 已提交
866 867 868 869 870 871 872 873 874 875 876 877

    return n;
}

static virDomainPtr
libxlDomainCreateXML(virConnectPtr conn, const char *xml,
                     unsigned int flags)
{
    libxlDriverPrivatePtr driver = conn->privateData;
    virDomainDefPtr def;
    virDomainObjPtr vm = NULL;
    virDomainPtr dom = NULL;
878
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
879
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;
J
Jim Fehlig 已提交
880

881 882 883 884 885
    virCheckFlags(VIR_DOMAIN_START_PAUSED |
                  VIR_DOMAIN_START_VALIDATE, NULL);

    if (flags & VIR_DOMAIN_START_VALIDATE)
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE;
J
Jim Fehlig 已提交
886

887
    if (!(def = virDomainDefParseString(xml, cfg->caps, driver->xmlopt,
888
                                        parse_flags)))
J
Jim Fehlig 已提交
889 890
        goto cleanup;

891 892 893
    if (virDomainCreateXMLEnsureACL(conn, def) < 0)
        goto cleanup;

894
    if (!(vm = virDomainObjListAdd(driver->domains, def,
895
                                   driver->xmlopt,
896 897
                                   VIR_DOMAIN_OBJ_LIST_ADD_CHECK_LIVE,
                                   NULL)))
J
Jim Fehlig 已提交
898 899 900
        goto cleanup;
    def = NULL;

901 902 903 904 905 906 907 908
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0) {
        if (!vm->persistent) {
            virDomainObjListRemove(driver->domains, vm);
            vm = NULL;
        }
        goto cleanup;
    }

909
    if (libxlDomainStart(driver, vm, (flags & VIR_DOMAIN_START_PAUSED) != 0,
910
                     -1) < 0) {
911
        virDomainObjListRemove(driver->domains, vm);
912
        goto endjob;
J
Jim Fehlig 已提交
913 914 915 916 917 918
    }

    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom)
        dom->id = vm->def->id;

919 920 921 922
 endjob:
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

923
 cleanup:
J
Jim Fehlig 已提交
924 925
    virDomainDefFree(def);
    if (vm)
926
        virObjectUnlock(vm);
927
    virObjectUnref(cfg);
J
Jim Fehlig 已提交
928 929 930 931 932 933 934 935 936 937
    return dom;
}

static virDomainPtr
libxlDomainLookupByID(virConnectPtr conn, int id)
{
    libxlDriverPrivatePtr driver = conn->privateData;
    virDomainObjPtr vm;
    virDomainPtr dom = NULL;

938
    vm = virDomainObjListFindByID(driver->domains, id);
J
Jim Fehlig 已提交
939
    if (!vm) {
940
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
J
Jim Fehlig 已提交
941 942 943
        goto cleanup;
    }

944 945 946
    if (virDomainLookupByIDEnsureACL(conn, vm->def) < 0)
        goto cleanup;

J
Jim Fehlig 已提交
947 948 949 950
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom)
        dom->id = vm->def->id;

951
 cleanup:
J
Jim Fehlig 已提交
952
    if (vm)
953
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
954 955 956 957 958 959 960 961 962 963
    return dom;
}

static virDomainPtr
libxlDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
{
    libxlDriverPrivatePtr driver = conn->privateData;
    virDomainObjPtr vm;
    virDomainPtr dom = NULL;

964
    vm = virDomainObjListFindByUUID(driver->domains, uuid);
J
Jim Fehlig 已提交
965
    if (!vm) {
966
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
J
Jim Fehlig 已提交
967 968 969
        goto cleanup;
    }

970 971 972
    if (virDomainLookupByUUIDEnsureACL(conn, vm->def) < 0)
        goto cleanup;

J
Jim Fehlig 已提交
973 974 975 976
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom)
        dom->id = vm->def->id;

977
 cleanup:
J
Jim Fehlig 已提交
978
    if (vm)
979
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
980 981 982 983 984 985 986 987 988 989
    return dom;
}

static virDomainPtr
libxlDomainLookupByName(virConnectPtr conn, const char *name)
{
    libxlDriverPrivatePtr driver = conn->privateData;
    virDomainObjPtr vm;
    virDomainPtr dom = NULL;

990
    vm = virDomainObjListFindByName(driver->domains, name);
J
Jim Fehlig 已提交
991
    if (!vm) {
992
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
J
Jim Fehlig 已提交
993 994 995
        goto cleanup;
    }

996 997 998
    if (virDomainLookupByNameEnsureACL(conn, vm->def) < 0)
        goto cleanup;

J
Jim Fehlig 已提交
999 1000 1001 1002
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom)
        dom->id = vm->def->id;

1003
 cleanup:
1004
    virDomainObjEndAPI(&vm);
J
Jim Fehlig 已提交
1005 1006 1007
    return dom;
}

1008 1009 1010 1011
static int
libxlDomainSuspend(virDomainPtr dom)
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
1012
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
1013
    virDomainObjPtr vm;
1014
    virObjectEventPtr event = NULL;
1015 1016
    int ret = -1;

J
Jim Fehlig 已提交
1017
    if (!(vm = libxlDomObjFromDomain(dom)))
1018
        goto cleanup;
1019 1020 1021 1022

    if (virDomainSuspendEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

1023 1024 1025
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

1026
    if (!virDomainObjIsActive(vm)) {
1027
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not running"));
1028
        goto endjob;
1029 1030
    }

J
Jiri Denemark 已提交
1031
    if (virDomainObjGetState(vm, NULL) != VIR_DOMAIN_PAUSED) {
J
Jim Fehlig 已提交
1032
        if (libxl_domain_pause(cfg->ctx, vm->def->id) != 0) {
1033 1034
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Failed to suspend domain '%d' with libxenlight"),
1035
                           vm->def->id);
1036
            goto endjob;
1037 1038
        }

J
Jiri Denemark 已提交
1039
        virDomainObjSetState(vm, VIR_DOMAIN_PAUSED, VIR_DOMAIN_PAUSED_USER);
1040

1041
        event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_SUSPENDED,
1042 1043 1044
                                         VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
    }

1045
    if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm) < 0)
1046
        goto endjob;
1047 1048 1049

    ret = 0;

1050
 endjob:
1051 1052 1053
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

1054
 cleanup:
1055
    if (vm)
1056
        virObjectUnlock(vm);
1057
    if (event)
1058
        libxlDomainEventQueue(driver, event);
1059
    virObjectUnref(cfg);
1060 1061 1062 1063 1064 1065 1066 1067
    return ret;
}


static int
libxlDomainResume(virDomainPtr dom)
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
1068
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
1069
    virDomainObjPtr vm;
1070
    virObjectEventPtr event = NULL;
1071 1072
    int ret = -1;

J
Jim Fehlig 已提交
1073
    if (!(vm = libxlDomObjFromDomain(dom)))
1074 1075
        goto cleanup;

1076 1077 1078
    if (virDomainResumeEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

1079 1080 1081
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

1082
    if (!virDomainObjIsActive(vm)) {
1083
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not running"));
1084
        goto endjob;
1085 1086
    }

J
Jiri Denemark 已提交
1087
    if (virDomainObjGetState(vm, NULL) == VIR_DOMAIN_PAUSED) {
J
Jim Fehlig 已提交
1088
        if (libxl_domain_unpause(cfg->ctx, vm->def->id) != 0) {
1089 1090
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Failed to resume domain '%d' with libxenlight"),
1091
                           vm->def->id);
1092
            goto endjob;
1093 1094
        }

J
Jiri Denemark 已提交
1095 1096
        virDomainObjSetState(vm, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_UNPAUSED);
1097

1098
        event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_RESUMED,
1099 1100 1101
                                         VIR_DOMAIN_EVENT_RESUMED_UNPAUSED);
    }

1102
    if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm) < 0)
1103
        goto endjob;
1104 1105 1106

    ret = 0;

1107
 endjob:
1108 1109 1110
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

1111
 cleanup:
1112
    if (vm)
1113
        virObjectUnlock(vm);
1114
    if (event)
1115
        libxlDomainEventQueue(driver, event);
1116
    virObjectUnref(cfg);
1117 1118 1119
    return ret;
}

J
Jim Fehlig 已提交
1120
static int
1121
libxlDomainShutdownFlags(virDomainPtr dom, unsigned int flags)
J
Jim Fehlig 已提交
1122
{
J
Jim Fehlig 已提交
1123 1124
    libxlDriverPrivatePtr driver = dom->conn->privateData;
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
J
Jim Fehlig 已提交
1125 1126 1127
    virDomainObjPtr vm;
    int ret = -1;

1128 1129 1130 1131 1132
    virCheckFlags(VIR_DOMAIN_SHUTDOWN_ACPI_POWER_BTN |
                  VIR_DOMAIN_SHUTDOWN_PARAVIRT, -1);
    if (flags == 0)
        flags = VIR_DOMAIN_SHUTDOWN_PARAVIRT |
            VIR_DOMAIN_SHUTDOWN_ACPI_POWER_BTN;
1133

J
Jim Fehlig 已提交
1134
    if (!(vm = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
1135 1136
        goto cleanup;

1137
    if (virDomainShutdownFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
1138 1139
        goto cleanup;

J
Jim Fehlig 已提交
1140
    if (!virDomainObjIsActive(vm)) {
1141 1142
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("Domain is not running"));
J
Jim Fehlig 已提交
1143 1144 1145
        goto cleanup;
    }

1146
    if (flags & VIR_DOMAIN_SHUTDOWN_PARAVIRT) {
J
Jim Fehlig 已提交
1147
        ret = libxl_domain_shutdown(cfg->ctx, vm->def->id);
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
        if (ret == 0)
            goto cleanup;

        if (ret != ERROR_NOPARAVIRT) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Failed to shutdown domain '%d' with libxenlight"),
                           vm->def->id);
            ret = -1;
            goto cleanup;
        }
        ret = -1;
    }

    if (flags & VIR_DOMAIN_SHUTDOWN_ACPI_POWER_BTN) {
J
Jim Fehlig 已提交
1162
        ret = libxl_send_trigger(cfg->ctx, vm->def->id,
1163 1164 1165 1166
                                 LIBXL_TRIGGER_POWER, 0);
        if (ret == 0)
            goto cleanup;

1167 1168
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to shutdown domain '%d' with libxenlight"),
1169
                       vm->def->id);
1170
        ret = -1;
J
Jim Fehlig 已提交
1171 1172
    }

1173
 cleanup:
J
Jim Fehlig 已提交
1174
    if (vm)
1175
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
1176
    virObjectUnref(cfg);
J
Jim Fehlig 已提交
1177 1178 1179
    return ret;
}

1180 1181 1182 1183 1184 1185 1186
static int
libxlDomainShutdown(virDomainPtr dom)
{
    return libxlDomainShutdownFlags(dom, 0);
}


J
Jim Fehlig 已提交
1187
static int
E
Eric Blake 已提交
1188
libxlDomainReboot(virDomainPtr dom, unsigned int flags)
J
Jim Fehlig 已提交
1189
{
J
Jim Fehlig 已提交
1190 1191
    libxlDriverPrivatePtr driver = dom->conn->privateData;
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
J
Jim Fehlig 已提交
1192 1193 1194
    virDomainObjPtr vm;
    int ret = -1;

J
Jim Fehlig 已提交
1195 1196 1197
    virCheckFlags(VIR_DOMAIN_REBOOT_PARAVIRT, -1);
    if (flags == 0)
        flags = VIR_DOMAIN_REBOOT_PARAVIRT;
E
Eric Blake 已提交
1198

J
Jim Fehlig 已提交
1199
    if (!(vm = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
1200 1201
        goto cleanup;

1202
    if (virDomainRebootEnsureACL(dom->conn, vm->def, flags) < 0)
1203 1204
        goto cleanup;

J
Jim Fehlig 已提交
1205
    if (!virDomainObjIsActive(vm)) {
1206 1207
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("Domain is not running"));
J
Jim Fehlig 已提交
1208 1209 1210
        goto cleanup;
    }

J
Jim Fehlig 已提交
1211
    if (flags & VIR_DOMAIN_REBOOT_PARAVIRT) {
J
Jim Fehlig 已提交
1212
        ret = libxl_domain_reboot(cfg->ctx, vm->def->id);
J
Jim Fehlig 已提交
1213 1214 1215
        if (ret == 0)
            goto cleanup;

1216 1217
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to reboot domain '%d' with libxenlight"),
1218
                       vm->def->id);
J
Jim Fehlig 已提交
1219
        ret = -1;
J
Jim Fehlig 已提交
1220 1221
    }

1222
 cleanup:
J
Jim Fehlig 已提交
1223
    if (vm)
1224
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
1225
    virObjectUnref(cfg);
J
Jim Fehlig 已提交
1226 1227 1228 1229
    return ret;
}

static int
1230 1231
libxlDomainDestroyFlags(virDomainPtr dom,
                        unsigned int flags)
J
Jim Fehlig 已提交
1232 1233
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
J
Jim Fehlig 已提交
1234
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
J
Jim Fehlig 已提交
1235 1236
    virDomainObjPtr vm;
    int ret = -1;
1237
    virObjectEventPtr event = NULL;
J
Jim Fehlig 已提交
1238

1239 1240
    virCheckFlags(0, -1);

J
Jim Fehlig 已提交
1241
    if (!(vm = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
1242 1243
        goto cleanup;

1244 1245 1246
    if (virDomainDestroyFlagsEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

1247 1248 1249
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

J
Jim Fehlig 已提交
1250
    if (!virDomainObjIsActive(vm)) {
1251 1252
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("Domain is not running"));
1253
        goto endjob;
J
Jim Fehlig 已提交
1254 1255
    }

1256
    event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_STOPPED,
1257 1258
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);

1259
    if (libxlDomainDestroyInternal(driver, vm) < 0) {
1260
        virReportError(VIR_ERR_INTERNAL_ERROR,
1261
                       _("Failed to destroy domain '%d'"), vm->def->id);
1262
        goto endjob;
J
Jim Fehlig 已提交
1263 1264
    }

1265 1266 1267
    libxlDomainCleanup(driver, vm, VIR_DOMAIN_SHUTOFF_DESTROYED);
    if (!vm->persistent)
        virDomainObjListRemove(driver->domains, vm);
J
Jim Fehlig 已提交
1268 1269 1270

    ret = 0;

1271 1272 1273 1274
 endjob:
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

1275
 cleanup:
J
Jim Fehlig 已提交
1276
    if (vm)
1277
        virObjectUnlock(vm);
1278 1279
    if (event)
        libxlDomainEventQueue(driver, event);
J
Jim Fehlig 已提交
1280
    virObjectUnref(cfg);
J
Jim Fehlig 已提交
1281 1282 1283
    return ret;
}

1284 1285 1286 1287 1288 1289
static int
libxlDomainDestroy(virDomainPtr dom)
{
    return libxlDomainDestroyFlags(dom, 0);
}

1290 1291 1292 1293 1294 1295
static char *
libxlDomainGetOSType(virDomainPtr dom)
{
    virDomainObjPtr vm;
    char *type = NULL;

J
Jim Fehlig 已提交
1296
    if (!(vm = libxlDomObjFromDomain(dom)))
1297 1298
        goto cleanup;

1299 1300 1301
    if (virDomainGetOSTypeEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

1302
    if (VIR_STRDUP(type, virDomainOSTypeToString(vm->def->os.type)) < 0)
1303
        goto cleanup;
1304

1305
 cleanup:
1306
    if (vm)
1307
        virObjectUnlock(vm);
1308 1309 1310
    return type;
}

1311
static unsigned long long
1312 1313 1314
libxlDomainGetMaxMemory(virDomainPtr dom)
{
    virDomainObjPtr vm;
1315
    unsigned long long ret = 0;
1316

J
Jim Fehlig 已提交
1317
    if (!(vm = libxlDomObjFromDomain(dom)))
1318
        goto cleanup;
1319 1320 1321 1322

    if (virDomainGetMaxMemoryEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

1323
    ret = virDomainDefGetMemoryActual(vm->def);
1324

1325
 cleanup:
1326
    if (vm)
1327
        virObjectUnlock(vm);
1328 1329 1330 1331
    return ret;
}

static int
1332
libxlDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem,
1333 1334 1335
                          unsigned int flags)
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
1336
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
1337
    virDomainObjPtr vm;
1338 1339
    virDomainDefPtr persistentDef = NULL;
    bool isActive;
1340 1341 1342
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_MEM_LIVE |
1343 1344
                  VIR_DOMAIN_MEM_CONFIG |
                  VIR_DOMAIN_MEM_MAXIMUM, -1);
1345

J
Jim Fehlig 已提交
1346
    if (!(vm = libxlDomObjFromDomain(dom)))
1347 1348
        goto cleanup;

1349 1350 1351
    if (virDomainSetMemoryFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
        goto cleanup;

1352 1353 1354
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
    isActive = virDomainObjIsActive(vm);

    if (flags == VIR_DOMAIN_MEM_CURRENT) {
        if (isActive)
            flags = VIR_DOMAIN_MEM_LIVE;
        else
            flags = VIR_DOMAIN_MEM_CONFIG;
    }
    if (flags == VIR_DOMAIN_MEM_MAXIMUM) {
        if (isActive)
            flags = VIR_DOMAIN_MEM_LIVE | VIR_DOMAIN_MEM_MAXIMUM;
        else
            flags = VIR_DOMAIN_MEM_CONFIG | VIR_DOMAIN_MEM_MAXIMUM;
1368 1369
    }

1370
    if (!isActive && (flags & VIR_DOMAIN_MEM_LIVE)) {
1371 1372
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("cannot set memory on an inactive domain"));
1373
        goto endjob;
1374 1375 1376 1377
    }

    if (flags & VIR_DOMAIN_MEM_CONFIG) {
        if (!vm->persistent) {
1378 1379
            virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                           _("cannot change persistent config of a transient domain"));
1380
            goto endjob;
1381
        }
1382
        if (!(persistentDef = virDomainObjGetPersistentDef(cfg->caps,
1383
                                                           driver->xmlopt,
1384
                                                           vm)))
1385
            goto endjob;
1386 1387
    }

1388 1389
    if (flags & VIR_DOMAIN_MEM_MAXIMUM) {
        /* resize the maximum memory */
1390

1391
        if (flags & VIR_DOMAIN_MEM_LIVE) {
J
Jim Fehlig 已提交
1392
            if (libxl_domain_setmaxmem(cfg->ctx, vm->def->id, newmem) < 0) {
1393 1394
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Failed to set maximum memory for domain '%d'"
1395
                                 " with libxenlight"), vm->def->id);
1396
                goto endjob;
1397 1398 1399 1400 1401 1402
            }
        }

        if (flags & VIR_DOMAIN_MEM_CONFIG) {
            /* Help clang 2.8 decipher the logic flow.  */
            sa_assert(persistentDef);
1403
            virDomainDefSetMemoryInitial(persistentDef, newmem);
1404 1405
            if (persistentDef->mem.cur_balloon > newmem)
                persistentDef->mem.cur_balloon = newmem;
1406
            ret = virDomainSaveConfig(cfg->configDir, persistentDef);
1407
            goto endjob;
1408 1409
        }

1410 1411
    } else {
        /* resize the current memory */
1412

1413
        if (newmem > virDomainDefGetMemoryActual(vm->def)) {
1414 1415
            virReportError(VIR_ERR_INVALID_ARG, "%s",
                           _("cannot set memory higher than max memory"));
1416
            goto endjob;
1417 1418 1419
        }

        if (flags & VIR_DOMAIN_MEM_LIVE) {
1420 1421 1422 1423
            int res;

            /* Unlock virDomainObj while ballooning memory */
            virObjectUnlock(vm);
J
Jim Fehlig 已提交
1424
            res = libxl_set_memory_target(cfg->ctx, vm->def->id, newmem, 0,
1425 1426 1427
                                          /* force */ 1);
            virObjectLock(vm);
            if (res < 0) {
1428 1429
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Failed to set memory for domain '%d'"
1430
                                 " with libxenlight"), vm->def->id);
1431
                goto endjob;
1432 1433 1434 1435 1436 1437
            }
        }

        if (flags & VIR_DOMAIN_MEM_CONFIG) {
            sa_assert(persistentDef);
            persistentDef->mem.cur_balloon = newmem;
1438
            ret = virDomainSaveConfig(cfg->configDir, persistentDef);
1439
            goto endjob;
1440
        }
1441 1442
    }

1443 1444
    ret = 0;

1445
 endjob:
1446 1447 1448
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

1449
 cleanup:
1450
    if (vm)
1451
        virObjectUnlock(vm);
1452
    virObjectUnref(cfg);
1453 1454 1455 1456 1457 1458 1459 1460 1461
    return ret;
}

static int
libxlDomainSetMemory(virDomainPtr dom, unsigned long memory)
{
    return libxlDomainSetMemoryFlags(dom, memory, VIR_DOMAIN_MEM_LIVE);
}

1462 1463 1464 1465 1466 1467
static int
libxlDomainSetMaxMemory(virDomainPtr dom, unsigned long memory)
{
    return libxlDomainSetMemoryFlags(dom, memory, VIR_DOMAIN_MEM_MAXIMUM);
}

J
Jim Fehlig 已提交
1468 1469 1470
static int
libxlDomainGetInfo(virDomainPtr dom, virDomainInfoPtr info)
{
J
Jim Fehlig 已提交
1471 1472
    libxlDriverPrivatePtr driver = dom->conn->privateData;
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
J
Jim Fehlig 已提交
1473
    virDomainObjPtr vm;
1474
    libxl_dominfo d_info;
J
Jim Fehlig 已提交
1475 1476
    int ret = -1;

J
Jim Fehlig 已提交
1477
    if (!(vm = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
1478 1479
        goto cleanup;

1480 1481 1482
    if (virDomainGetInfoEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

1483 1484 1485
    if (!virDomainObjIsActive(vm)) {
        info->cpuTime = 0;
        info->memory = vm->def->mem.cur_balloon;
1486
        info->maxMem = virDomainDefGetMemoryActual(vm->def);
1487
    } else {
J
Jim Fehlig 已提交
1488
        if (libxl_domain_info(cfg->ctx, &d_info, vm->def->id) != 0) {
1489
            virReportError(VIR_ERR_INTERNAL_ERROR,
1490 1491
                           _("libxl_domain_info failed for domain '%d'"),
                           vm->def->id);
1492 1493 1494 1495
            goto cleanup;
        }
        info->cpuTime = d_info.cpu_time;
        info->memory = d_info.current_memkb;
1496
        info->maxMem = d_info.max_memkb;
1497 1498
    }

J
Jiri Denemark 已提交
1499
    info->state = virDomainObjGetState(vm, NULL);
J
Jim Fehlig 已提交
1500 1501 1502
    info->nrVirtCpu = vm->def->vcpus;
    ret = 0;

1503
 cleanup:
J
Jim Fehlig 已提交
1504
    if (vm)
1505
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
1506
    virObjectUnref(cfg);
J
Jim Fehlig 已提交
1507 1508 1509
    return ret;
}

1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
static int
libxlDomainGetState(virDomainPtr dom,
                    int *state,
                    int *reason,
                    unsigned int flags)
{
    virDomainObjPtr vm;
    int ret = -1;

    virCheckFlags(0, -1);

J
Jim Fehlig 已提交
1521
    if (!(vm = libxlDomObjFromDomain(dom)))
1522 1523
        goto cleanup;

1524 1525 1526
    if (virDomainGetStateEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

J
Jiri Denemark 已提交
1527
    *state = virDomainObjGetState(vm, reason);
1528 1529
    ret = 0;

1530
 cleanup:
1531
    if (vm)
1532
        virObjectUnlock(vm);
1533 1534 1535
    return ret;
}

1536 1537 1538
/*
 * virDomainObjPtr must be locked on invocation
 */
1539
static int
1540 1541
libxlDoDomainSave(libxlDriverPrivatePtr driver, virDomainObjPtr vm,
                  const char *to)
1542
{
J
Jim Fehlig 已提交
1543
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
1544
    libxlSavefileHeader hdr;
1545
    virObjectEventPtr event = NULL;
1546 1547
    char *xml = NULL;
    uint32_t xml_len;
1548
    int fd = -1;
1549 1550 1551
    int ret = -1;

    if (virDomainObjGetState(vm, NULL) == VIR_DOMAIN_PAUSED) {
1552 1553 1554
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Domain '%d' has to be running because libxenlight will"
                         " suspend it"), vm->def->id);
1555 1556 1557 1558
        goto cleanup;
    }

    if ((fd = virFileOpenAs(to, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR,
L
Laine Stump 已提交
1559
                            -1, -1, 0)) < 0) {
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
        virReportSystemError(-fd,
                             _("Failed to create domain save file '%s'"), to);
        goto cleanup;
    }

    if ((xml = virDomainDefFormat(vm->def, 0)) == NULL)
        goto cleanup;
    xml_len = strlen(xml) + 1;

    memset(&hdr, 0, sizeof(hdr));
    memcpy(hdr.magic, LIBXL_SAVE_MAGIC, sizeof(hdr.magic));
    hdr.version = LIBXL_SAVE_VERSION;
    hdr.xmlLen = xml_len;

    if (safewrite(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
1575 1576
        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                       _("Failed to write save file header"));
1577 1578 1579 1580
        goto cleanup;
    }

    if (safewrite(fd, xml, xml_len) != xml_len) {
1581 1582
        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                       _("Failed to write xml description"));
1583 1584 1585
        goto cleanup;
    }

1586 1587
    /* Unlock virDomainObj while saving domain */
    virObjectUnlock(vm);
J
Jim Fehlig 已提交
1588
    ret = libxl_domain_suspend(cfg->ctx, vm->def->id, fd, 0, NULL);
1589 1590 1591
    virObjectLock(vm);

    if (ret != 0) {
1592 1593 1594
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to save domain '%d' with libxenlight"),
                       vm->def->id);
1595
        ret = -1;
1596 1597 1598
        goto cleanup;
    }

1599
    event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_STOPPED,
1600 1601
                                         VIR_DOMAIN_EVENT_STOPPED_SAVED);

1602
    if (libxlDomainDestroyInternal(driver, vm) < 0) {
1603 1604
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to destroy domain '%d'"), vm->def->id);
1605 1606 1607
        goto cleanup;
    }

1608
    libxlDomainCleanup(driver, vm, VIR_DOMAIN_SHUTOFF_SAVED);
1609
    vm->hasManagedSave = true;
1610 1611
    ret = 0;

1612
 cleanup:
1613 1614 1615 1616 1617
    VIR_FREE(xml);
    if (VIR_CLOSE(fd) < 0)
        virReportSystemError(errno, "%s", _("cannot close file"));
    if (event)
        libxlDomainEventQueue(driver, event);
J
Jim Fehlig 已提交
1618
    virObjectUnref(cfg);
1619 1620 1621 1622
    return ret;
}

static int
1623 1624
libxlDomainSaveFlags(virDomainPtr dom, const char *to, const char *dxml,
                     unsigned int flags)
1625
{
1626 1627
    libxlDriverPrivatePtr driver = dom->conn->privateData;
    virDomainObjPtr vm;
1628
    int ret = -1;
1629
    bool remove_dom = false;
1630

1631 1632 1633 1634 1635
#ifdef LIBXL_HAVE_NO_SUSPEND_RESUME
    virReportUnsupportedError();
    return -1;
#endif

1636 1637
    virCheckFlags(0, -1);
    if (dxml) {
1638 1639
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
1640 1641 1642
        return -1;
    }

J
Jim Fehlig 已提交
1643
    if (!(vm = libxlDomObjFromDomain(dom)))
1644 1645
        goto cleanup;

1646 1647 1648
    if (virDomainSaveFlagsEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

1649 1650 1651
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

1652
    if (!virDomainObjIsActive(vm)) {
1653
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not running"));
1654
        goto endjob;
1655 1656
    }

1657
    if (libxlDoDomainSave(driver, vm, to) < 0)
1658
        goto endjob;
1659

1660 1661
    if (!vm->persistent)
        remove_dom = true;
1662 1663

    ret = 0;
1664

1665
 endjob:
1666 1667 1668
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

1669
 cleanup:
1670 1671 1672 1673
    if (remove_dom && vm) {
        virDomainObjListRemove(driver->domains, vm);
        vm = NULL;
    }
1674
    if (vm)
1675
        virObjectUnlock(vm);
1676 1677
    return ret;
}
1678

1679
static int
1680 1681 1682 1683 1684 1685 1686 1687
libxlDomainSave(virDomainPtr dom, const char *to)
{
    return libxlDomainSaveFlags(dom, to, NULL, 0);
}

static int
libxlDomainRestoreFlags(virConnectPtr conn, const char *from,
                        const char *dxml, unsigned int flags)
1688 1689
{
    libxlDriverPrivatePtr driver = conn->privateData;
1690
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
1691 1692 1693 1694 1695
    virDomainObjPtr vm = NULL;
    virDomainDefPtr def = NULL;
    libxlSavefileHeader hdr;
    int fd = -1;
    int ret = -1;
1696

1697 1698 1699 1700 1701
#ifdef LIBXL_HAVE_NO_SUSPEND_RESUME
    virReportUnsupportedError();
    return -1;
#endif

1702
    virCheckFlags(VIR_DOMAIN_SAVE_PAUSED, -1);
1703
    if (dxml) {
1704 1705
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
1706 1707 1708
        return -1;
    }

1709
    fd = libxlDomainSaveImageOpen(driver, cfg, from, &def, &hdr);
1710
    if (fd < 0)
1711
        goto cleanup;
1712

1713
    if (virDomainRestoreFlagsEnsureACL(conn, def) < 0)
1714
        goto cleanup;
1715

1716
    if (!(vm = virDomainObjListAdd(driver->domains, def,
1717
                                   driver->xmlopt,
1718 1719 1720
                                   VIR_DOMAIN_OBJ_LIST_ADD_LIVE |
                                   VIR_DOMAIN_OBJ_LIST_ADD_CHECK_LIVE,
                                   NULL)))
1721
        goto cleanup;
1722 1723 1724

    def = NULL;

1725 1726 1727 1728 1729 1730 1731 1732
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0) {
        if (!vm->persistent) {
            virDomainObjListRemove(driver->domains, vm);
            vm = NULL;
        }
        goto cleanup;
    }

1733
    ret = libxlDomainStart(driver, vm, (flags & VIR_DOMAIN_SAVE_PAUSED) != 0, fd);
1734
    if (ret < 0 && !vm->persistent)
1735
        virDomainObjListRemove(driver->domains, vm);
1736 1737

    if (!libxlDomainObjEndJob(driver, vm))
1738 1739
        vm = NULL;

1740
 cleanup:
1741 1742
    if (VIR_CLOSE(fd) < 0)
        virReportSystemError(errno, "%s", _("cannot close file"));
1743 1744
    virDomainDefFree(def);
    if (vm)
1745
        virObjectUnlock(vm);
1746
    virObjectUnref(cfg);
1747 1748 1749
    return ret;
}

1750 1751 1752 1753 1754 1755
static int
libxlDomainRestore(virConnectPtr conn, const char *from)
{
    return libxlDomainRestoreFlags(conn, from, NULL, 0);
}

1756
static int
1757
libxlDomainCoreDump(virDomainPtr dom, const char *to, unsigned int flags)
1758 1759
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
J
Jim Fehlig 已提交
1760
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
1761
    virDomainObjPtr vm;
1762
    virObjectEventPtr event = NULL;
1763
    bool remove_dom = false;
1764 1765 1766 1767 1768
    bool paused = false;
    int ret = -1;

    virCheckFlags(VIR_DUMP_LIVE | VIR_DUMP_CRASH, -1);

J
Jim Fehlig 已提交
1769
    if (!(vm = libxlDomObjFromDomain(dom)))
1770 1771
        goto cleanup;

1772 1773 1774
    if (virDomainCoreDumpEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

1775 1776 1777
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

1778
    if (!virDomainObjIsActive(vm)) {
1779
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not running"));
1780
        goto endjob;
1781 1782 1783 1784
    }

    if (!(flags & VIR_DUMP_LIVE) &&
        virDomainObjGetState(vm, NULL) == VIR_DOMAIN_RUNNING) {
J
Jim Fehlig 已提交
1785
        if (libxl_domain_pause(cfg->ctx, vm->def->id) != 0) {
1786 1787 1788
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Before dumping core, failed to suspend domain '%d'"
                             " with libxenlight"),
1789
                           vm->def->id);
1790
            goto endjob;
1791 1792 1793 1794 1795
        }
        virDomainObjSetState(vm, VIR_DOMAIN_PAUSED, VIR_DOMAIN_PAUSED_DUMP);
        paused = true;
    }

1796 1797
    /* Unlock virDomainObj while dumping core */
    virObjectUnlock(vm);
J
Jim Fehlig 已提交
1798
    ret = libxl_domain_core_dump(cfg->ctx, vm->def->id, to, NULL);
1799 1800
    virObjectLock(vm);
    if (ret != 0) {
1801 1802
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to dump core of domain '%d' with libxenlight"),
1803
                       vm->def->id);
1804 1805
        ret = -1;
        goto unpause;
1806 1807 1808
    }

    if (flags & VIR_DUMP_CRASH) {
1809
        if (libxlDomainDestroyInternal(driver, vm) < 0) {
1810
            virReportError(VIR_ERR_INTERNAL_ERROR,
1811
                           _("Failed to destroy domain '%d'"), vm->def->id);
1812
            goto unpause;
1813 1814
        }

1815
        libxlDomainCleanup(driver, vm, VIR_DOMAIN_SHUTOFF_CRASHED);
1816
        event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_STOPPED,
1817
                                         VIR_DOMAIN_EVENT_STOPPED_CRASHED);
1818 1819
        if (!vm->persistent)
            remove_dom = true;
1820 1821 1822 1823
    }

    ret = 0;

1824
 unpause:
1825
    if (virDomainObjIsActive(vm) && paused) {
J
Jim Fehlig 已提交
1826
        if (libxl_domain_unpause(cfg->ctx, vm->def->id) != 0) {
1827 1828
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("After dumping core, failed to resume domain '%d' with"
1829
                             " libxenlight"), vm->def->id);
1830 1831 1832 1833 1834
        } else {
            virDomainObjSetState(vm, VIR_DOMAIN_RUNNING,
                                 VIR_DOMAIN_RUNNING_UNPAUSED);
        }
    }
1835

1836
 endjob:
1837 1838 1839
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

1840
 cleanup:
1841 1842 1843 1844
    if (remove_dom && vm) {
        virDomainObjListRemove(driver->domains, vm);
        vm = NULL;
    }
1845
    if (vm)
1846
        virObjectUnlock(vm);
1847
    if (event)
1848
        libxlDomainEventQueue(driver, event);
J
Jim Fehlig 已提交
1849
    virObjectUnref(cfg);
1850 1851 1852
    return ret;
}

1853 1854 1855 1856 1857 1858 1859
static int
libxlDomainManagedSave(virDomainPtr dom, unsigned int flags)
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
    virDomainObjPtr vm = NULL;
    char *name = NULL;
    int ret = -1;
1860
    bool remove_dom = false;
1861 1862 1863

    virCheckFlags(0, -1);

J
Jim Fehlig 已提交
1864
    if (!(vm = libxlDomObjFromDomain(dom)))
1865 1866
        goto cleanup;

1867 1868 1869
    if (virDomainManagedSaveEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

1870 1871 1872
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

1873
    if (!virDomainObjIsActive(vm)) {
1874
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not running"));
1875
        goto endjob;
1876
    }
1877
    if (!vm->persistent) {
1878 1879
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("cannot do managed save for transient domain"));
1880
        goto endjob;
1881
    }
1882 1883 1884

    name = libxlDomainManagedSavePath(driver, vm);
    if (name == NULL)
1885
        goto endjob;
1886 1887 1888

    VIR_INFO("Saving state to %s", name);

1889
    if (libxlDoDomainSave(driver, vm, name) < 0)
1890
        goto endjob;
1891

1892 1893
    if (!vm->persistent)
        remove_dom = true;
1894 1895

    ret = 0;
1896

1897
 endjob:
1898 1899 1900
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

1901
 cleanup:
1902 1903 1904 1905
    if (remove_dom && vm) {
        virDomainObjListRemove(driver->domains, vm);
        vm = NULL;
    }
1906
    if (vm)
1907
        virObjectUnlock(vm);
1908 1909 1910 1911
    VIR_FREE(name);
    return ret;
}

1912 1913
static int
libxlDomainManagedSaveLoad(virDomainObjPtr vm,
1914 1915 1916 1917
                           void *opaque)
{
    libxlDriverPrivatePtr driver = opaque;
    char *name;
1918
    int ret = -1;
1919

1920
    virObjectLock(vm);
1921 1922 1923 1924 1925 1926

    if (!(name = libxlDomainManagedSavePath(driver, vm)))
        goto cleanup;

    vm->hasManagedSave = virFileExists(name);

1927
    ret = 0;
1928
 cleanup:
1929
    virObjectUnlock(vm);
1930
    VIR_FREE(name);
1931
    return ret;
1932 1933
}

1934 1935 1936 1937 1938 1939 1940 1941
static int
libxlDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
{
    virDomainObjPtr vm = NULL;
    int ret = -1;

    virCheckFlags(0, -1);

J
Jim Fehlig 已提交
1942
    if (!(vm = libxlDomObjFromDomain(dom)))
1943 1944
        goto cleanup;

1945 1946 1947
    if (virDomainHasManagedSaveImageEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

1948
    ret = vm->hasManagedSave;
1949

1950
 cleanup:
1951
    if (vm)
1952
        virObjectUnlock(vm);
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965
    return ret;
}

static int
libxlDomainManagedSaveRemove(virDomainPtr dom, unsigned int flags)
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
    virDomainObjPtr vm = NULL;
    int ret = -1;
    char *name = NULL;

    virCheckFlags(0, -1);

J
Jim Fehlig 已提交
1966
    if (!(vm = libxlDomObjFromDomain(dom)))
1967 1968
        goto cleanup;

1969 1970 1971
    if (virDomainManagedSaveRemoveEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

1972 1973 1974 1975 1976
    name = libxlDomainManagedSavePath(driver, vm);
    if (name == NULL)
        goto cleanup;

    ret = unlink(name);
1977
    vm->hasManagedSave = false;
1978

1979
 cleanup:
1980 1981
    VIR_FREE(name);
    if (vm)
1982
        virObjectUnlock(vm);
1983 1984 1985
    return ret;
}

1986 1987 1988 1989 1990
static int
libxlDomainSetVcpusFlags(virDomainPtr dom, unsigned int nvcpus,
                         unsigned int flags)
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
1991
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
1992 1993
    virDomainDefPtr def;
    virDomainObjPtr vm;
J
Jim Fehlig 已提交
1994
    libxl_bitmap map;
1995 1996
    uint8_t *bitmask = NULL;
    unsigned int maplen;
1997 1998
    size_t i;
    unsigned int pos;
1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
    int max;
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_VCPU_LIVE |
                  VIR_DOMAIN_VCPU_CONFIG |
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    /* At least one of LIVE or CONFIG must be set.  MAXIMUM cannot be
     * mixed with LIVE.  */
    if ((flags & (VIR_DOMAIN_VCPU_LIVE | VIR_DOMAIN_VCPU_CONFIG)) == 0 ||
        (flags & (VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_VCPU_LIVE)) ==
         (VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_VCPU_LIVE)) {
2011 2012
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid flag combination: (0x%x)"), flags);
2013 2014 2015 2016
        return -1;
    }

    if (!nvcpus) {
2017
        virReportError(VIR_ERR_INVALID_ARG, "%s", _("nvcpus is zero"));
2018 2019 2020
        return -1;
    }

J
Jim Fehlig 已提交
2021
    if (!(vm = libxlDomObjFromDomain(dom)))
2022 2023
        goto cleanup;

2024 2025 2026
    if (virDomainSetVcpusFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
        goto cleanup;

2027 2028 2029
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

2030
    if (!virDomainObjIsActive(vm) && (flags & VIR_DOMAIN_VCPU_LIVE)) {
2031 2032
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("cannot set vcpus on an inactive domain"));
2033
        goto endjob;
2034 2035 2036
    }

    if (!vm->persistent && (flags & VIR_DOMAIN_VCPU_CONFIG)) {
2037 2038
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("cannot change persistent config of a transient domain"));
2039
        goto endjob;
2040 2041
    }

2042
    if ((max = libxlConnectGetMaxVcpus(dom->conn, NULL)) < 0) {
2043 2044
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("could not determine max vcpus for the domain"));
2045
        goto endjob;
2046 2047
    }

2048
    if (!(flags & VIR_DOMAIN_VCPU_MAXIMUM) && vm->def->maxvcpus < max)
2049 2050 2051
        max = vm->def->maxvcpus;

    if (nvcpus > max) {
2052 2053 2054
        virReportError(VIR_ERR_INVALID_ARG,
                       _("requested vcpus is greater than max allowable"
                         " vcpus for the domain: %d > %d"), nvcpus, max);
2055
        goto endjob;
2056 2057
    }

2058
    if (!(def = virDomainObjGetPersistentDef(cfg->caps, driver->xmlopt, vm)))
2059
        goto endjob;
2060

E
Eric Blake 已提交
2061
    maplen = VIR_CPU_MAPLEN(nvcpus);
2062
    if (VIR_ALLOC_N(bitmask, maplen) < 0)
2063
        goto endjob;
2064 2065

    for (i = 0; i < nvcpus; ++i) {
E
Eric Blake 已提交
2066
        pos = i / 8;
2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084
        bitmask[pos] |= 1 << (i % 8);
    }

    map.size = maplen;
    map.map = bitmask;

    switch (flags) {
    case VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_VCPU_CONFIG:
        def->maxvcpus = nvcpus;
        if (nvcpus < def->vcpus)
            def->vcpus = nvcpus;
        break;

    case VIR_DOMAIN_VCPU_CONFIG:
        def->vcpus = nvcpus;
        break;

    case VIR_DOMAIN_VCPU_LIVE:
J
Jim Fehlig 已提交
2085
        if (libxl_set_vcpuonline(cfg->ctx, vm->def->id, &map) != 0) {
2086 2087
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Failed to set vcpus for domain '%d'"
2088
                             " with libxenlight"), vm->def->id);
2089
            goto endjob;
2090 2091 2092 2093
        }
        break;

    case VIR_DOMAIN_VCPU_LIVE | VIR_DOMAIN_VCPU_CONFIG:
J
Jim Fehlig 已提交
2094
        if (libxl_set_vcpuonline(cfg->ctx, vm->def->id, &map) != 0) {
2095 2096
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Failed to set vcpus for domain '%d'"
2097
                             " with libxenlight"), vm->def->id);
2098
            goto endjob;
2099 2100 2101 2102 2103 2104 2105 2106
        }
        def->vcpus = nvcpus;
        break;
    }

    ret = 0;

    if (flags & VIR_DOMAIN_VCPU_CONFIG)
2107
        ret = virDomainSaveConfig(cfg->configDir, def);
2108

2109
 endjob:
2110 2111 2112
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

2113
 cleanup:
2114 2115
    VIR_FREE(bitmask);
     if (vm)
2116
        virObjectUnlock(vm);
2117
     virObjectUnref(cfg);
2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132
    return ret;
}

static int
libxlDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus)
{
    return libxlDomainSetVcpusFlags(dom, nvcpus, VIR_DOMAIN_VCPU_LIVE);
}

static int
libxlDomainGetVcpusFlags(virDomainPtr dom, unsigned int flags)
{
    virDomainObjPtr vm;
    virDomainDefPtr def;
    int ret = -1;
2133
    bool active;
2134 2135 2136 2137 2138

    virCheckFlags(VIR_DOMAIN_VCPU_LIVE |
                  VIR_DOMAIN_VCPU_CONFIG |
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

J
Jim Fehlig 已提交
2139
    if (!(vm = libxlDomObjFromDomain(dom)))
2140 2141
        goto cleanup;

2142
    if (virDomainGetVcpusFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
2143 2144
        goto cleanup;

2145 2146 2147 2148 2149 2150 2151 2152 2153
    active = virDomainObjIsActive(vm);

    if ((flags & (VIR_DOMAIN_VCPU_LIVE | VIR_DOMAIN_VCPU_CONFIG)) == 0) {
        if (active)
            flags |= VIR_DOMAIN_VCPU_LIVE;
        else
            flags |= VIR_DOMAIN_VCPU_CONFIG;
    }
    if ((flags & VIR_DOMAIN_VCPU_LIVE) && (flags & VIR_DOMAIN_VCPU_CONFIG)) {
2154 2155
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid flag combination: (0x%x)"), flags);
2156 2157 2158
        return -1;
    }

2159
    if (flags & VIR_DOMAIN_VCPU_LIVE) {
2160
        if (!active) {
2161 2162
            virReportError(VIR_ERR_OPERATION_INVALID,
                           "%s", _("Domain is not running"));
2163 2164 2165 2166
            goto cleanup;
        }
        def = vm->def;
    } else {
2167
        if (!vm->persistent) {
2168 2169
            virReportError(VIR_ERR_OPERATION_INVALID,
                           "%s", _("domain is transient"));
2170 2171
            goto cleanup;
        }
2172 2173 2174 2175 2176
        def = vm->newDef ? vm->newDef : vm->def;
    }

    ret = (flags & VIR_DOMAIN_VCPU_MAXIMUM) ? def->maxvcpus : def->vcpus;

2177
 cleanup:
2178
    if (vm)
2179
        virObjectUnlock(vm);
2180 2181 2182 2183
    return ret;
}

static int
2184 2185 2186
libxlDomainPinVcpuFlags(virDomainPtr dom, unsigned int vcpu,
                        unsigned char *cpumap, int maplen,
                        unsigned int flags)
2187 2188
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
2189
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
2190
    virDomainDefPtr targetDef = NULL;
2191
    virBitmapPtr pcpumap = NULL;
2192 2193
    virDomainObjPtr vm;
    int ret = -1;
2194 2195 2196

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);
2197

J
Jim Fehlig 已提交
2198
    if (!(vm = libxlDomObjFromDomain(dom)))
2199 2200
        goto cleanup;

2201
    if (virDomainPinVcpuFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
2202 2203
        goto cleanup;

2204 2205 2206
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

2207
    if ((flags & VIR_DOMAIN_AFFECT_LIVE) && !virDomainObjIsActive(vm)) {
2208
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
2209
                       _("domain is inactive"));
2210
        goto endjob;
2211 2212
    }

2213 2214
    if (virDomainLiveConfigHelperMethod(cfg->caps, driver->xmlopt, vm,
                                        &flags, &targetDef) < 0)
2215
        goto endjob;
2216

2217
    if (flags & VIR_DOMAIN_AFFECT_LIVE)
2218 2219 2220 2221 2222
        targetDef = vm->def;

    /* Make sure coverity knows targetDef is valid at this point. */
    sa_assert(targetDef);

2223 2224
    pcpumap = virBitmapNewData(cpumap, maplen);
    if (!pcpumap)
2225
        goto endjob;
2226

2227 2228
    if (flags & VIR_DOMAIN_AFFECT_LIVE) {
        libxl_bitmap map = { .size = maplen, .map = cpumap };
J
Jim Fehlig 已提交
2229
        if (libxl_set_vcpuaffinity(cfg->ctx, vm->def->id, vcpu, &map) != 0) {
2230 2231 2232
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Failed to pin vcpu '%d' with libxenlight"),
                           vcpu);
2233
            goto endjob;
2234
        }
2235
    }
2236

2237 2238
    /* full bitmap means reset the settings (if any). */
    if (virBitmapIsAllSet(pcpumap)) {
2239 2240 2241
        virDomainPinDel(&targetDef->cputune.vcpupin,
                        &targetDef->cputune.nvcpupin,
                        vcpu);
2242
        goto done;
2243 2244
    }

2245 2246
    if (!targetDef->cputune.vcpupin) {
        if (VIR_ALLOC(targetDef->cputune.vcpupin) < 0)
2247
            goto endjob;
2248
        targetDef->cputune.nvcpupin = 0;
H
Hu Tao 已提交
2249
    }
2250 2251 2252 2253 2254
    if (virDomainPinAdd(&targetDef->cputune.vcpupin,
                        &targetDef->cputune.nvcpupin,
                        cpumap,
                        maplen,
                        vcpu) < 0) {
2255 2256
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("failed to update or add vcpupin xml"));
2257
        goto endjob;
2258 2259
    }

2260
 done:
2261 2262
    ret = 0;

2263 2264 2265 2266 2267 2268
    if (flags & VIR_DOMAIN_AFFECT_LIVE) {
        ret = virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm);
    } else if (flags & VIR_DOMAIN_AFFECT_CONFIG) {
        ret = virDomainSaveConfig(cfg->configDir, targetDef);
    }

2269
 endjob:
2270 2271 2272
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

2273
 cleanup:
2274
    if (vm)
2275
        virObjectUnlock(vm);
2276
    virBitmapFree(pcpumap);
2277
    virObjectUnref(cfg);
2278 2279 2280
    return ret;
}

2281 2282 2283 2284 2285 2286 2287 2288
static int
libxlDomainPinVcpu(virDomainPtr dom, unsigned int vcpu, unsigned char *cpumap,
                   int maplen)
{
    return libxlDomainPinVcpuFlags(dom, vcpu, cpumap, maplen,
                                   VIR_DOMAIN_AFFECT_LIVE);
}

2289 2290 2291 2292 2293 2294 2295 2296 2297
static int
libxlDomainGetVcpuPinInfo(virDomainPtr dom, int ncpumaps,
                          unsigned char *cpumaps, int maplen,
                          unsigned int flags)
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
    virDomainObjPtr vm = NULL;
    virDomainDefPtr targetDef = NULL;
2298
    virDomainPinDefPtr *vcpupin_list;
2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315
    virBitmapPtr cpumask = NULL;
    int maxcpu, hostcpus, vcpu, pcpu, n, ret = -1;
    unsigned char *cpumap;

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

    if (!(vm = libxlDomObjFromDomain(dom)))
        goto cleanup;

    if (virDomainGetVcpuPinInfoEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

    if (virDomainLiveConfigHelperMethod(cfg->caps, driver->xmlopt, vm,
                                        &flags, &targetDef) < 0)
        goto cleanup;

2316
    if (flags & VIR_DOMAIN_AFFECT_LIVE)
2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344
        targetDef = vm->def;

    /* Make sure coverity knows targetDef is valid at this point. */
    sa_assert(targetDef);

    /* Clamp to actual number of vcpus */
    if (ncpumaps > targetDef->vcpus)
        ncpumaps = targetDef->vcpus;

    if ((hostcpus = libxl_get_max_cpus(cfg->ctx)) < 0)
        goto cleanup;

    maxcpu = maplen * 8;
    if (maxcpu > hostcpus)
        maxcpu = hostcpus;

    /* initialize cpumaps */
    memset(cpumaps, 0xff, maplen * ncpumaps);
    if (maxcpu % 8) {
        for (vcpu = 0; vcpu < ncpumaps; vcpu++) {
            cpumap = VIR_GET_CPUMAP(cpumaps, maplen, vcpu);
            cpumap[maplen - 1] &= (1 << maxcpu % 8) - 1;
        }
    }

    /* if vcpupin setting exists, there may be unused pcpus */
    for (n = 0; n < targetDef->cputune.nvcpupin; n++) {
        vcpupin_list = targetDef->cputune.vcpupin;
2345
        vcpu = vcpupin_list[n]->id;
2346 2347 2348
        cpumask = vcpupin_list[n]->cpumask;
        cpumap = VIR_GET_CPUMAP(cpumaps, maplen, vcpu);
        for (pcpu = 0; pcpu < maxcpu; pcpu++) {
J
Ján Tomko 已提交
2349
            if (!virBitmapIsBitSet(cpumask, pcpu))
2350 2351 2352 2353 2354
                VIR_UNUSE_CPU(cpumap, pcpu);
        }
    }
    ret = ncpumaps;

2355
 cleanup:
2356 2357 2358 2359 2360
    if (vm)
        virObjectUnlock(vm);
    virObjectUnref(cfg);
    return ret;
}
2361 2362 2363 2364 2365

static int
libxlDomainGetVcpus(virDomainPtr dom, virVcpuInfoPtr info, int maxinfo,
                    unsigned char *cpumaps, int maplen)
{
J
Jim Fehlig 已提交
2366 2367
    libxlDriverPrivatePtr driver = dom->conn->privateData;
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
2368 2369 2370 2371
    virDomainObjPtr vm;
    int ret = -1;
    libxl_vcpuinfo *vcpuinfo;
    int maxcpu, hostcpus;
2372
    size_t i;
2373 2374
    unsigned char *cpumap;

J
Jim Fehlig 已提交
2375
    if (!(vm = libxlDomObjFromDomain(dom)))
2376 2377
        goto cleanup;

2378 2379 2380
    if (virDomainGetVcpusEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

2381
    if (!virDomainObjIsActive(vm)) {
2382
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not running"));
2383 2384 2385
        goto cleanup;
    }

J
Jim Fehlig 已提交
2386
    if ((vcpuinfo = libxl_list_vcpu(cfg->ctx, vm->def->id, &maxcpu,
2387
                                    &hostcpus)) == NULL) {
2388 2389
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to list vcpus for domain '%d' with libxenlight"),
2390
                       vm->def->id);
2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412
        goto cleanup;
    }

    if (cpumaps && maplen > 0)
        memset(cpumaps, 0, maplen * maxinfo);
    for (i = 0; i < maxcpu && i < maxinfo; ++i) {
        info[i].number = vcpuinfo[i].vcpuid;
        info[i].cpu = vcpuinfo[i].cpu;
        info[i].cpuTime = vcpuinfo[i].vcpu_time;
        if (vcpuinfo[i].running)
            info[i].state = VIR_VCPU_RUNNING;
        else if (vcpuinfo[i].blocked)
            info[i].state = VIR_VCPU_BLOCKED;
        else
            info[i].state = VIR_VCPU_OFFLINE;

        if (cpumaps && maplen > 0) {
            cpumap = VIR_GET_CPUMAP(cpumaps, maplen, i);
            memcpy(cpumap, vcpuinfo[i].cpumap.map,
                   MIN(maplen, vcpuinfo[i].cpumap.size));
        }

J
Jim Fehlig 已提交
2413
        libxl_vcpuinfo_dispose(&vcpuinfo[i]);
2414 2415 2416 2417 2418
    }
    VIR_FREE(vcpuinfo);

    ret = maxinfo;

2419
 cleanup:
2420
    if (vm)
2421
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
2422
    virObjectUnref(cfg);
2423 2424 2425
    return ret;
}

J
Jim Fehlig 已提交
2426
static char *
2427
libxlDomainGetXMLDesc(virDomainPtr dom, unsigned int flags)
J
Jim Fehlig 已提交
2428 2429 2430 2431
{
    virDomainObjPtr vm;
    char *ret = NULL;

2432 2433
    /* Flags checked by virDomainDefFormat */

J
Jim Fehlig 已提交
2434
    if (!(vm = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
2435 2436
        goto cleanup;

2437 2438 2439
    if (virDomainGetXMLDescEnsureACL(dom->conn, vm->def, flags) < 0)
        goto cleanup;

2440 2441
    ret = virDomainDefFormat(vm->def,
                              virDomainDefFormatConvertXMLFlags(flags));
J
Jim Fehlig 已提交
2442

2443
 cleanup:
J
Jim Fehlig 已提交
2444
    if (vm)
2445
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
2446 2447 2448
    return ret;
}

2449
static char *
2450 2451 2452
libxlConnectDomainXMLFromNative(virConnectPtr conn,
                                const char *nativeFormat,
                                const char *nativeConfig,
2453
                                unsigned int flags)
2454 2455
{
    libxlDriverPrivatePtr driver = conn->privateData;
2456
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
2457 2458 2459 2460
    virDomainDefPtr def = NULL;
    virConfPtr conf = NULL;
    char *xml = NULL;

E
Eric Blake 已提交
2461 2462
    virCheckFlags(0, NULL);

2463 2464 2465
    if (virConnectDomainXMLFromNativeEnsureACL(conn) < 0)
        goto cleanup;

2466 2467 2468 2469 2470
    if (STREQ(nativeFormat, LIBXL_CONFIG_FORMAT_XL)) {
        if (!(conf = virConfReadMem(nativeConfig, strlen(nativeConfig), 0)))
            goto cleanup;
        if (!(def = xenParseXL(conf,
                               cfg->caps,
2471
                               cfg->verInfo->xen_version_major)))
2472 2473
            goto cleanup;
    } else if (STREQ(nativeFormat, LIBXL_CONFIG_FORMAT_XM)) {
2474 2475 2476 2477 2478
        if (!(conf = virConfReadMem(nativeConfig, strlen(nativeConfig), 0)))
            goto cleanup;

        if (!(def = xenParseXM(conf,
                               cfg->verInfo->xen_version_major,
2479
                               cfg->caps)))
2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491
            goto cleanup;
    } else if (STREQ(nativeFormat, LIBXL_CONFIG_FORMAT_SEXPR)) {
        /* only support latest xend config format */
        if (!(def = xenParseSxprString(nativeConfig,
                                       XEND_CONFIG_VERSION_3_1_0,
                                       NULL,
                                       -1))) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("parsing sxpr config failed"));
            goto cleanup;
        }
    } else {
2492 2493
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unsupported config type %s"), nativeFormat);
2494 2495 2496
        goto cleanup;
    }

2497
    xml = virDomainDefFormat(def, VIR_DOMAIN_DEF_FORMAT_INACTIVE);
2498

2499
 cleanup:
2500 2501 2502
    virDomainDefFree(def);
    if (conf)
        virConfFree(conf);
2503
    virObjectUnref(cfg);
2504 2505 2506 2507 2508
    return xml;
}

#define MAX_CONFIG_SIZE (1024 * 65)
static char *
2509 2510 2511
libxlConnectDomainXMLToNative(virConnectPtr conn, const char * nativeFormat,
                              const char * domainXml,
                              unsigned int flags)
2512 2513
{
    libxlDriverPrivatePtr driver = conn->privateData;
2514
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
2515 2516 2517 2518 2519
    virDomainDefPtr def = NULL;
    virConfPtr conf = NULL;
    int len = MAX_CONFIG_SIZE;
    char *ret = NULL;

E
Eric Blake 已提交
2520 2521
    virCheckFlags(0, NULL);

2522 2523 2524
    if (virConnectDomainXMLToNativeEnsureACL(conn) < 0)
        goto cleanup;

2525
    if (!(def = virDomainDefParseString(domainXml,
2526
                                        cfg->caps, driver->xmlopt,
2527
                                        VIR_DOMAIN_DEF_PARSE_INACTIVE)))
2528 2529
        goto cleanup;

2530 2531 2532 2533 2534 2535 2536 2537 2538 2539
    if (STREQ(nativeFormat, LIBXL_CONFIG_FORMAT_XL)) {
        if (!(conf = xenFormatXL(def, conn, cfg->verInfo->xen_version_major)))
            goto cleanup;
    } else if (STREQ(nativeFormat, LIBXL_CONFIG_FORMAT_XM)) {
        if (!(conf = xenFormatXM(conn, def, cfg->verInfo->xen_version_major)))
            goto cleanup;
    } else {

        virReportError(VIR_ERR_INVALID_ARG,
                       _("unsupported config type %s"), nativeFormat);
2540
        goto cleanup;
2541
    }
2542

2543
    if (VIR_ALLOC_N(ret, len) < 0)
2544 2545 2546 2547 2548 2549 2550
        goto cleanup;

    if (virConfWriteMem(ret, &len, conf) < 0) {
        VIR_FREE(ret);
        goto cleanup;
    }

2551
 cleanup:
2552 2553 2554
    virDomainDefFree(def);
    if (conf)
        virConfFree(conf);
2555
    virObjectUnref(cfg);
2556 2557 2558
    return ret;
}

J
Jim Fehlig 已提交
2559
static int
2560 2561
libxlConnectListDefinedDomains(virConnectPtr conn,
                               char **const names, int nnames)
J
Jim Fehlig 已提交
2562 2563 2564 2565
{
    libxlDriverPrivatePtr driver = conn->privateData;
    int n;

2566 2567 2568
    if (virConnectListDefinedDomainsEnsureACL(conn) < 0)
        return -1;

2569 2570
    n = virDomainObjListGetInactiveNames(driver->domains, names, nnames,
                                         virConnectListDefinedDomainsCheckACL, conn);
J
Jim Fehlig 已提交
2571 2572 2573 2574
    return n;
}

static int
2575
libxlConnectNumOfDefinedDomains(virConnectPtr conn)
J
Jim Fehlig 已提交
2576 2577 2578 2579
{
    libxlDriverPrivatePtr driver = conn->privateData;
    int n;

2580 2581 2582
    if (virConnectNumOfDefinedDomainsEnsureACL(conn) < 0)
        return -1;

2583
    n = virDomainObjListNumOfDomains(driver->domains, false,
2584 2585
                                     virConnectNumOfDefinedDomainsCheckACL,
                                     conn);
J
Jim Fehlig 已提交
2586 2587 2588 2589 2590
    return n;
}

static int
libxlDomainCreateWithFlags(virDomainPtr dom,
E
Eric Blake 已提交
2591
                           unsigned int flags)
J
Jim Fehlig 已提交
2592 2593 2594 2595 2596 2597 2598
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_START_PAUSED, -1);

J
Jim Fehlig 已提交
2599
    if (!(vm = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
2600 2601
        goto cleanup;

2602 2603 2604
    if (virDomainCreateWithFlagsEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

2605 2606 2607
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

J
Jim Fehlig 已提交
2608
    if (virDomainObjIsActive(vm)) {
2609 2610
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("Domain is already running"));
2611
        goto endjob;
J
Jim Fehlig 已提交
2612 2613
    }

2614
    ret = libxlDomainStart(driver, vm, (flags & VIR_DOMAIN_START_PAUSED) != 0, -1);
2615
    if (ret < 0)
2616
        goto endjob;
2617
    dom->id = vm->def->id;
J
Jim Fehlig 已提交
2618

2619 2620 2621 2622
 endjob:
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

2623
 cleanup:
J
Jim Fehlig 已提交
2624
    if (vm)
2625
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
2626 2627 2628 2629 2630 2631 2632 2633 2634 2635
    return ret;
}

static int
libxlDomainCreate(virDomainPtr dom)
{
    return libxlDomainCreateWithFlags(dom, 0);
}

static virDomainPtr
2636
libxlDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags)
J
Jim Fehlig 已提交
2637 2638
{
    libxlDriverPrivatePtr driver = conn->privateData;
2639
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
J
Jim Fehlig 已提交
2640 2641 2642
    virDomainDefPtr def = NULL;
    virDomainObjPtr vm = NULL;
    virDomainPtr dom = NULL;
2643
    virObjectEventPtr event = NULL;
2644
    virDomainDefPtr oldDef = NULL;
2645
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;
J
Jim Fehlig 已提交
2646

2647 2648 2649 2650
    virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL);

    if (flags & VIR_DOMAIN_DEFINE_VALIDATE)
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE;
2651

2652
    if (!(def = virDomainDefParseString(xml, cfg->caps, driver->xmlopt,
2653
                                        parse_flags)))
2654
        goto cleanup;
J
Jim Fehlig 已提交
2655

2656
    if (virDomainDefineXMLFlagsEnsureACL(conn, def) < 0)
2657
        goto cleanup;
2658

2659
    if (!(vm = virDomainObjListAdd(driver->domains, def,
2660
                                   driver->xmlopt,
2661 2662
                                   0,
                                   &oldDef)))
2663
        goto cleanup;
2664

J
Jim Fehlig 已提交
2665 2666 2667
    def = NULL;
    vm->persistent = 1;

2668
    if (virDomainSaveConfig(cfg->configDir,
J
Jim Fehlig 已提交
2669
                            vm->newDef ? vm->newDef : vm->def) < 0) {
2670
        virDomainObjListRemove(driver->domains, vm);
J
Jim Fehlig 已提交
2671 2672 2673 2674 2675 2676 2677 2678
        vm = NULL;
        goto cleanup;
    }

    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom)
        dom->id = vm->def->id;

2679
    event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_DEFINED,
2680
                                     !oldDef ?
2681 2682 2683
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED :
                                     VIR_DOMAIN_EVENT_DEFINED_UPDATED);

2684
 cleanup:
J
Jim Fehlig 已提交
2685
    virDomainDefFree(def);
2686
    virDomainDefFree(oldDef);
J
Jim Fehlig 已提交
2687
    if (vm)
2688
        virObjectUnlock(vm);
2689 2690
    if (event)
        libxlDomainEventQueue(driver, event);
2691
    virObjectUnref(cfg);
J
Jim Fehlig 已提交
2692 2693 2694
    return dom;
}

2695 2696 2697 2698 2699 2700
static virDomainPtr
libxlDomainDefineXML(virConnectPtr conn, const char *xml)
{
    return libxlDomainDefineXMLFlags(conn, xml, 0);
}

J
Jim Fehlig 已提交
2701
static int
2702 2703
libxlDomainUndefineFlags(virDomainPtr dom,
                         unsigned int flags)
J
Jim Fehlig 已提交
2704 2705
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
2706
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
J
Jim Fehlig 已提交
2707
    virDomainObjPtr vm;
2708
    virObjectEventPtr event = NULL;
2709
    char *name = NULL;
J
Jim Fehlig 已提交
2710 2711
    int ret = -1;

2712 2713
    virCheckFlags(VIR_DOMAIN_UNDEFINE_MANAGED_SAVE, -1);

J
Jim Fehlig 已提交
2714
    if (!(vm = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
2715 2716
        goto cleanup;

2717 2718 2719
    if (virDomainUndefineFlagsEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

J
Jim Fehlig 已提交
2720
    if (!vm->persistent) {
2721 2722
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("cannot undefine transient domain"));
J
Jim Fehlig 已提交
2723 2724 2725
        goto cleanup;
    }

2726 2727 2728 2729 2730 2731 2732
    name = libxlDomainManagedSavePath(driver, vm);
    if (name == NULL)
        goto cleanup;

    if (virFileExists(name)) {
        if (flags & VIR_DOMAIN_UNDEFINE_MANAGED_SAVE) {
            if (unlink(name) < 0) {
2733 2734
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("Failed to remove domain managed save image"));
2735 2736 2737
                goto cleanup;
            }
        } else {
2738 2739 2740
            virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                           _("Refusing to undefine while domain managed "
                             "save image exists"));
2741 2742 2743 2744
            goto cleanup;
        }
    }

2745
    if (virDomainDeleteConfig(cfg->configDir, cfg->autostartDir, vm) < 0)
J
Jim Fehlig 已提交
2746 2747
        goto cleanup;

2748
    event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_UNDEFINED,
2749 2750
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);

2751 2752 2753
    if (virDomainObjIsActive(vm)) {
        vm->persistent = 0;
    } else {
2754
        virDomainObjListRemove(driver->domains, vm);
2755 2756 2757
        vm = NULL;
    }

J
Jim Fehlig 已提交
2758 2759
    ret = 0;

2760
 cleanup:
2761
    VIR_FREE(name);
J
Jim Fehlig 已提交
2762
    if (vm)
2763
        virObjectUnlock(vm);
2764 2765
    if (event)
        libxlDomainEventQueue(driver, event);
2766
    virObjectUnref(cfg);
J
Jim Fehlig 已提交
2767 2768 2769
    return ret;
}

2770 2771 2772 2773 2774 2775
static int
libxlDomainUndefine(virDomainPtr dom)
{
    return libxlDomainUndefineFlags(dom, 0);
}

2776
static int
J
Jim Fehlig 已提交
2777
libxlDomainChangeEjectableMedia(virDomainObjPtr vm, virDomainDiskDefPtr disk)
2778
{
J
Jim Fehlig 已提交
2779
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(libxl_driver);
2780 2781
    virDomainDiskDefPtr origdisk = NULL;
    libxl_device_disk x_disk;
2782
    size_t i;
2783 2784
    int ret = -1;

2785
    for (i = 0; i < vm->def->ndisks; i++) {
2786 2787 2788 2789 2790 2791 2792 2793
        if (vm->def->disks[i]->bus == disk->bus &&
            STREQ(vm->def->disks[i]->dst, disk->dst)) {
            origdisk = vm->def->disks[i];
            break;
        }
    }

    if (!origdisk) {
2794 2795 2796
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("No device with bus '%s' and target '%s'"),
                       virDomainDiskBusTypeToString(disk->bus), disk->dst);
2797 2798 2799 2800
        goto cleanup;
    }

    if (origdisk->device != VIR_DOMAIN_DISK_DEVICE_CDROM) {
2801 2802 2803
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Removable media not supported for %s device"),
                       virDomainDiskDeviceTypeToString(disk->device));
2804 2805 2806
        return -1;
    }

J
Jim Fehlig 已提交
2807
    if (libxlMakeDisk(disk, &x_disk) < 0)
2808 2809
        goto cleanup;

J
Jim Fehlig 已提交
2810
    if ((ret = libxl_cdrom_insert(cfg->ctx, vm->def->id, &x_disk, NULL)) < 0) {
2811 2812 2813
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("libxenlight failed to change media for disk '%s'"),
                       disk->dst);
2814 2815 2816
        goto cleanup;
    }

2817 2818 2819
    if (virDomainDiskSetSource(origdisk, virDomainDiskGetSource(disk)) < 0)
        goto cleanup;
    virDomainDiskSetType(origdisk, virDomainDiskGetType(disk));
2820 2821 2822 2823 2824

    virDomainDiskDefFree(disk);

    ret = 0;

2825
 cleanup:
J
Jim Fehlig 已提交
2826
    virObjectUnref(cfg);
2827 2828 2829 2830
    return ret;
}

static int
J
Jim Fehlig 已提交
2831
libxlDomainAttachDeviceDiskLive(virDomainObjPtr vm, virDomainDeviceDefPtr dev)
2832
{
J
Jim Fehlig 已提交
2833
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(libxl_driver);
2834 2835 2836 2837 2838 2839
    virDomainDiskDefPtr l_disk = dev->data.disk;
    libxl_device_disk x_disk;
    int ret = -1;

    switch (l_disk->device)  {
        case VIR_DOMAIN_DISK_DEVICE_CDROM:
J
Jim Fehlig 已提交
2840
            ret = libxlDomainChangeEjectableMedia(vm, l_disk);
2841 2842 2843
            break;
        case VIR_DOMAIN_DISK_DEVICE_DISK:
            if (l_disk->bus == VIR_DOMAIN_DISK_BUS_XEN) {
2844
                if (virDomainDiskIndexByName(vm->def, l_disk->dst, true) >= 0) {
2845 2846
                    virReportError(VIR_ERR_OPERATION_FAILED,
                                   _("target %s already exists"), l_disk->dst);
2847 2848 2849
                    goto cleanup;
                }

2850
                if (!virDomainDiskGetSource(l_disk)) {
2851 2852
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   "%s", _("disk source path is missing"));
2853 2854 2855
                    goto cleanup;
                }

2856
                if (VIR_REALLOC_N(vm->def->disks, vm->def->ndisks+1) < 0)
2857 2858
                    goto cleanup;

J
Jim Fehlig 已提交
2859
                if (libxlMakeDisk(l_disk, &x_disk) < 0)
2860 2861
                    goto cleanup;

J
Jim Fehlig 已提交
2862
                if ((ret = libxl_device_disk_add(cfg->ctx, vm->def->id,
J
Jim Fehlig 已提交
2863
                                                &x_disk, NULL)) < 0) {
2864 2865 2866
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("libxenlight failed to attach disk '%s'"),
                                   l_disk->dst);
2867 2868 2869 2870 2871 2872
                    goto cleanup;
                }

                virDomainDiskInsertPreAlloced(vm->def, l_disk);

            } else {
2873 2874 2875
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                               _("disk bus '%s' cannot be hotplugged."),
                               virDomainDiskBusTypeToString(l_disk->bus));
2876 2877 2878
            }
            break;
        default:
2879 2880 2881
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("disk device type '%s' cannot be hotplugged"),
                           virDomainDiskDeviceTypeToString(l_disk->device));
2882 2883 2884
            break;
    }

2885
 cleanup:
J
Jim Fehlig 已提交
2886
    virObjectUnref(cfg);
2887 2888 2889
    return ret;
}

2890 2891 2892 2893 2894
static int
libxlDomainAttachHostPCIDevice(libxlDriverPrivatePtr driver,
                               virDomainObjPtr vm,
                               virDomainHostdevDefPtr hostdev)
{
J
Jim Fehlig 已提交
2895 2896
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
     libxl_device_pci pcidev;
2897 2898
    virDomainHostdevDefPtr found;
    virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;
2899
    virDomainHostdevSubsysPCIPtr pcisrc = &hostdev->source.subsys.u.pci;
J
Jim Fehlig 已提交
2900
    int ret = -1;
2901 2902

    if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI)
J
Jim Fehlig 已提交
2903
        goto cleanup;
2904 2905 2906 2907

    if (virDomainHostdevFind(vm->def, hostdev, &found) >= 0) {
        virReportError(VIR_ERR_OPERATION_FAILED,
                       _("target pci device %.4x:%.2x:%.2x.%.1x already exists"),
2908 2909
                       pcisrc->addr.domain, pcisrc->addr.bus,
                       pcisrc->addr.slot, pcisrc->addr.function);
J
Jim Fehlig 已提交
2910
        goto cleanup;
2911 2912 2913
    }

    if (VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs + 1) < 0)
J
Jim Fehlig 已提交
2914
        goto cleanup;
2915 2916 2917 2918

    if (virHostdevPreparePCIDevices(hostdev_mgr, LIBXL_DRIVER_NAME,
                                    vm->def->name, vm->def->uuid,
                                    &hostdev, 1, 0) < 0)
J
Jim Fehlig 已提交
2919
        goto cleanup;
2920

2921
    if (libxlMakePCI(hostdev, &pcidev) < 0)
C
Chunyan Liu 已提交
2922
        goto error;
2923

J
Jim Fehlig 已提交
2924
    if (libxl_device_pci_add(cfg->ctx, vm->def->id, &pcidev, 0) < 0) {
2925 2926
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("libxenlight failed to attach pci device %.4x:%.2x:%.2x.%.1x"),
2927 2928
                       pcisrc->addr.domain, pcisrc->addr.bus,
                       pcisrc->addr.slot, pcisrc->addr.function);
C
Chunyan Liu 已提交
2929
        goto error;
2930 2931 2932
    }

    vm->def->hostdevs[vm->def->nhostdevs++] = hostdev;
J
Jim Fehlig 已提交
2933 2934
    ret = 0;
    goto cleanup;
2935

2936
 error:
2937 2938
    virHostdevReAttachPCIDevices(hostdev_mgr, LIBXL_DRIVER_NAME,
                                 vm->def->name, &hostdev, 1, NULL);
J
Jim Fehlig 已提交
2939 2940 2941 2942

 cleanup:
    virObjectUnref(cfg);
    return ret;
2943 2944 2945 2946 2947
}

static int
libxlDomainAttachHostDevice(libxlDriverPrivatePtr driver,
                            virDomainObjPtr vm,
2948
                            virDomainHostdevDefPtr hostdev)
2949 2950 2951 2952 2953 2954 2955 2956 2957 2958
{
    if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("hostdev mode '%s' not supported"),
                       virDomainHostdevModeTypeToString(hostdev->mode));
        return -1;
    }

    switch (hostdev->source.subsys.type) {
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
J
Jim Fehlig 已提交
2959
        if (libxlDomainAttachHostPCIDevice(driver, vm, hostdev) < 0)
C
Chunyan Liu 已提交
2960
            return -1;
2961 2962 2963 2964 2965 2966
        break;

    default:
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("hostdev subsys type '%s' not supported"),
                       virDomainHostdevSubsysTypeToString(hostdev->source.subsys.type));
C
Chunyan Liu 已提交
2967
        return -1;
2968 2969 2970 2971 2972
    }

    return 0;
}

2973
static int
J
Jim Fehlig 已提交
2974
libxlDomainDetachDeviceDiskLive(virDomainObjPtr vm, virDomainDeviceDefPtr dev)
2975
{
J
Jim Fehlig 已提交
2976
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(libxl_driver);
2977 2978
    virDomainDiskDefPtr l_disk = NULL;
    libxl_device_disk x_disk;
2979
    int idx;
2980 2981 2982 2983 2984 2985
    int ret = -1;

    switch (dev->data.disk->device)  {
        case VIR_DOMAIN_DISK_DEVICE_DISK:
            if (dev->data.disk->bus == VIR_DOMAIN_DISK_BUS_XEN) {

2986 2987 2988
                if ((idx = virDomainDiskIndexByName(vm->def,
                                                    dev->data.disk->dst,
                                                    false)) < 0) {
2989 2990
                    virReportError(VIR_ERR_OPERATION_FAILED,
                                   _("disk %s not found"), dev->data.disk->dst);
2991 2992 2993
                    goto cleanup;
                }

2994
                l_disk = vm->def->disks[idx];
2995

J
Jim Fehlig 已提交
2996
                if (libxlMakeDisk(l_disk, &x_disk) < 0)
2997 2998
                    goto cleanup;

J
Jim Fehlig 已提交
2999
                if ((ret = libxl_device_disk_remove(cfg->ctx, vm->def->id,
J
Jim Fehlig 已提交
3000
                                                    &x_disk, NULL)) < 0) {
3001 3002 3003
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("libxenlight failed to detach disk '%s'"),
                                   l_disk->dst);
3004 3005 3006
                    goto cleanup;
                }

3007
                virDomainDiskRemove(vm->def, idx);
3008 3009 3010
                virDomainDiskDefFree(l_disk);

            } else {
3011 3012 3013
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                               _("disk bus '%s' cannot be hot unplugged."),
                               virDomainDiskBusTypeToString(dev->data.disk->bus));
3014 3015 3016
            }
            break;
        default:
3017 3018 3019
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("device type '%s' cannot hot unplugged"),
                           virDomainDiskDeviceTypeToString(dev->data.disk->device));
3020 3021 3022
            break;
    }

3023
 cleanup:
J
Jim Fehlig 已提交
3024
    virObjectUnref(cfg);
3025 3026 3027
    return ret;
}

3028 3029 3030 3031 3032
static int
libxlDomainAttachNetDevice(libxlDriverPrivatePtr driver,
                           virDomainObjPtr vm,
                           virDomainNetDefPtr net)
{
J
Jim Fehlig 已提交
3033
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
3034 3035 3036
    int actualType;
    libxl_device_nic nic;
    int ret = -1;
3037
    char mac[VIR_MAC_STRING_BUFLEN];
3038 3039 3040

    /* preallocate new slot for device */
    if (VIR_REALLOC_N(vm->def->nets, vm->def->nnets + 1) < 0)
J
Jim Fehlig 已提交
3041
        goto out;
3042 3043 3044 3045 3046 3047

    /* If appropriate, grab a physical device from the configured
     * network's pool of devices, or resolve bridge device name
     * to the one defined in the network definition.
     */
    if (networkAllocateActualDevice(vm->def, net) < 0)
J
Jim Fehlig 已提交
3048
        goto out;
3049 3050 3051

    actualType = virDomainNetGetActualType(net);

3052 3053 3054 3055 3056 3057 3058
    if (virDomainHasNet(vm->def, net)) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("network device with mac %s already exists"),
                       virMacAddrFormat(&net->mac, mac));
        return -1;
    }

3059 3060 3061 3062 3063 3064
    if (actualType == VIR_DOMAIN_NET_TYPE_HOSTDEV) {
        /* This is really a "smart hostdev", so it should be attached
         * as a hostdev (the hostdev code will reach over into the
         * netdev-specific code as appropriate), then also added to
         * the nets list (see out:) if successful.
         */
J
Jim Fehlig 已提交
3065
        ret = libxlDomainAttachHostDevice(driver, vm,
3066 3067 3068 3069 3070 3071 3072 3073
                                          virDomainNetGetActualHostdev(net));
        goto out;
    }

    libxl_device_nic_init(&nic);
    if (libxlMakeNic(vm->def, net, &nic) < 0)
        goto cleanup;

J
Jim Fehlig 已提交
3074
    if (libxl_device_nic_add(cfg->ctx, vm->def->id, &nic, 0)) {
3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("libxenlight failed to attach network device"));
        goto cleanup;
    }

    ret = 0;

 cleanup:
    libxl_device_nic_dispose(&nic);
 out:
    if (!ret)
        vm->def->nets[vm->def->nnets++] = net;
J
Jim Fehlig 已提交
3087
    virObjectUnref(cfg);
3088 3089 3090
    return ret;
}

3091
static int
3092 3093
libxlDomainAttachDeviceLive(libxlDriverPrivatePtr driver,
                            virDomainObjPtr vm,
3094 3095 3096 3097 3098 3099
                            virDomainDeviceDefPtr dev)
{
    int ret = -1;

    switch (dev->type) {
        case VIR_DOMAIN_DEVICE_DISK:
J
Jim Fehlig 已提交
3100
            ret = libxlDomainAttachDeviceDiskLive(vm, dev);
3101 3102 3103 3104
            if (!ret)
                dev->data.disk = NULL;
            break;

3105
        case VIR_DOMAIN_DEVICE_NET:
J
Jim Fehlig 已提交
3106
            ret = libxlDomainAttachNetDevice(driver, vm,
3107 3108 3109 3110 3111
                                             dev->data.net);
            if (!ret)
                dev->data.net = NULL;
            break;

3112
        case VIR_DOMAIN_DEVICE_HOSTDEV:
J
Jim Fehlig 已提交
3113
            ret = libxlDomainAttachHostDevice(driver, vm,
3114
                                              dev->data.hostdev);
3115 3116 3117 3118
            if (!ret)
                dev->data.hostdev = NULL;
            break;

3119
        default:
3120 3121 3122
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("device type '%s' cannot be attached"),
                           virDomainDeviceTypeToString(dev->type));
3123 3124 3125 3126 3127 3128 3129 3130 3131 3132
            break;
    }

    return ret;
}

static int
libxlDomainAttachDeviceConfig(virDomainDefPtr vmdef, virDomainDeviceDefPtr dev)
{
    virDomainDiskDefPtr disk;
3133
    virDomainNetDefPtr net;
3134 3135
    virDomainHostdevDefPtr hostdev;
    virDomainHostdevDefPtr found;
3136
    virDomainHostdevSubsysPCIPtr pcisrc;
3137
    char mac[VIR_MAC_STRING_BUFLEN];
3138 3139 3140 3141

    switch (dev->type) {
        case VIR_DOMAIN_DEVICE_DISK:
            disk = dev->data.disk;
3142
            if (virDomainDiskIndexByName(vmdef, disk->dst, true) >= 0) {
3143 3144
                virReportError(VIR_ERR_INVALID_ARG,
                               _("target %s already exists."), disk->dst);
3145 3146
                return -1;
            }
3147
            if (virDomainDiskInsert(vmdef, disk))
3148 3149 3150 3151
                return -1;
            /* vmdef has the pointer. Generic codes for vmdef will do all jobs */
            dev->data.disk = NULL;
            break;
3152 3153 3154

        case VIR_DOMAIN_DEVICE_NET:
            net = dev->data.net;
3155 3156 3157 3158 3159 3160
            if (virDomainHasNet(vmdef, net)) {
                virReportError(VIR_ERR_INVALID_ARG,
                               _("network device with mac %s already exists"),
                               virMacAddrFormat(&net->mac, mac));
                return -1;
            }
3161 3162 3163 3164 3165
            if (virDomainNetInsert(vmdef, net))
                return -1;
            dev->data.net = NULL;
            break;

3166 3167 3168 3169 3170 3171 3172
        case VIR_DOMAIN_DEVICE_HOSTDEV:
            hostdev = dev->data.hostdev;

            if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI)
                return -1;

            if (virDomainHostdevFind(vmdef, hostdev, &found) >= 0) {
3173
                pcisrc = &hostdev->source.subsys.u.pci;
3174 3175 3176
                virReportError(VIR_ERR_OPERATION_FAILED,
                               _("target pci device %.4x:%.2x:%.2x.%.1x\
                                  already exists"),
3177 3178
                               pcisrc->addr.domain, pcisrc->addr.bus,
                               pcisrc->addr.slot, pcisrc->addr.function);
3179 3180 3181
                return -1;
            }

3182 3183
            if (virDomainHostdevInsert(vmdef, hostdev) < 0)
                return -1;
3184
            break;
3185 3186

        default:
3187 3188
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("persistent attach of device is not supported"));
3189 3190 3191 3192 3193 3194
            return -1;
    }
    return 0;
}

static int
3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227
libxlComparePCIDevice(virDomainDefPtr def ATTRIBUTE_UNUSED,
                      virDomainDeviceDefPtr device ATTRIBUTE_UNUSED,
                      virDomainDeviceInfoPtr info1,
                      void *opaque)
{
    virDomainDeviceInfoPtr info2 = opaque;

    if (info1->type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI ||
        info2->type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI)
        return 0;

    if (info1->addr.pci.domain == info2->addr.pci.domain &&
        info1->addr.pci.bus == info2->addr.pci.bus &&
        info1->addr.pci.slot == info2->addr.pci.slot &&
        info1->addr.pci.function != info2->addr.pci.function)
        return -1;
    return 0;
}

static bool
libxlIsMultiFunctionDevice(virDomainDefPtr def,
                           virDomainDeviceInfoPtr dev)
{
    if (virDomainDeviceInfoIterate(def, libxlComparePCIDevice, dev) < 0)
        return true;
    return false;
}

static int
libxlDomainDetachHostPCIDevice(libxlDriverPrivatePtr driver,
                               virDomainObjPtr vm,
                               virDomainHostdevDefPtr hostdev)
{
J
Jim Fehlig 已提交
3228
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
3229
    virDomainHostdevSubsysPtr subsys = &hostdev->source.subsys;
3230
    virDomainHostdevSubsysPCIPtr pcisrc = &subsys->u.pci;
3231 3232 3233 3234
    libxl_device_pci pcidev;
    virDomainHostdevDefPtr detach;
    int idx;
    virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;
J
Jim Fehlig 已提交
3235
    int ret = -1;
3236 3237

    if (subsys->type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI)
J
Jim Fehlig 已提交
3238
        goto cleanup;
3239 3240 3241 3242 3243

    idx = virDomainHostdevFind(vm->def, hostdev, &detach);
    if (idx < 0) {
        virReportError(VIR_ERR_OPERATION_FAILED,
                       _("host pci device %.4x:%.2x:%.2x.%.1x not found"),
3244 3245
                       pcisrc->addr.domain, pcisrc->addr.bus,
                       pcisrc->addr.slot, pcisrc->addr.function);
J
Jim Fehlig 已提交
3246
        goto cleanup;
3247 3248 3249 3250 3251
    }

    if (libxlIsMultiFunctionDevice(vm->def, detach->info)) {
        virReportError(VIR_ERR_OPERATION_FAILED,
                       _("cannot hot unplug multifunction PCI device: %.4x:%.2x:%.2x.%.1x"),
3252 3253
                       pcisrc->addr.domain, pcisrc->addr.bus,
                       pcisrc->addr.slot, pcisrc->addr.function);
C
Chunyan Liu 已提交
3254
        goto error;
3255 3256 3257 3258 3259
    }


    libxl_device_pci_init(&pcidev);

3260
    if (libxlMakePCI(detach, &pcidev) < 0)
C
Chunyan Liu 已提交
3261
        goto error;
3262

J
Jim Fehlig 已提交
3263
    if (libxl_device_pci_remove(cfg->ctx, vm->def->id, &pcidev, 0) < 0) {
3264 3265 3266
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("libxenlight failed to detach pci device\
                          %.4x:%.2x:%.2x.%.1x"),
3267 3268
                       pcisrc->addr.domain, pcisrc->addr.bus,
                       pcisrc->addr.slot, pcisrc->addr.function);
C
Chunyan Liu 已提交
3269
        goto error;
3270 3271 3272 3273 3274 3275 3276 3277 3278
    }

    libxl_device_pci_dispose(&pcidev);

    virDomainHostdevRemove(vm->def, idx);

    virHostdevReAttachPCIDevices(hostdev_mgr, LIBXL_DRIVER_NAME,
                                 vm->def->name, &hostdev, 1, NULL);

J
Jim Fehlig 已提交
3279
    ret = 0;
3280

3281
 error:
3282
    virDomainHostdevDefFree(detach);
J
Jim Fehlig 已提交
3283 3284 3285 3286

 cleanup:
    virObjectUnref(cfg);
    return ret;
3287 3288 3289 3290 3291
}

static int
libxlDomainDetachHostDevice(libxlDriverPrivatePtr driver,
                            virDomainObjPtr vm,
3292
                            virDomainHostdevDefPtr hostdev)
3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304
{
    virDomainHostdevSubsysPtr subsys = &hostdev->source.subsys;

    if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("hostdev mode '%s' not supported"),
                       virDomainHostdevModeTypeToString(hostdev->mode));
        return -1;
    }

    switch (subsys->type) {
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
J
Jim Fehlig 已提交
3305
            return libxlDomainDetachHostPCIDevice(driver, vm, hostdev);
3306 3307 3308 3309 3310 3311 3312 3313 3314 3315

        default:
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unexpected hostdev type %d"), subsys->type);
            break;
    }

    return -1;
}

3316 3317 3318 3319 3320
static int
libxlDomainDetachNetDevice(libxlDriverPrivatePtr driver,
                           virDomainObjPtr vm,
                           virDomainNetDefPtr net)
{
J
Jim Fehlig 已提交
3321
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
3322 3323 3324 3325 3326 3327 3328
    int detachidx;
    virDomainNetDefPtr detach = NULL;
    libxl_device_nic nic;
    char mac[VIR_MAC_STRING_BUFLEN];
    int ret = -1;

    if ((detachidx = virDomainNetFindIdx(vm->def, net)) < 0)
J
Jim Fehlig 已提交
3329
        goto out;
3330 3331 3332 3333 3334 3335 3336

    detach = vm->def->nets[detachidx];

    if (virDomainNetGetActualType(detach) == VIR_DOMAIN_NET_TYPE_HOSTDEV) {
        /* This is really a "smart hostdev", so it should be attached as a
         * hostdev, then also removed from nets list (see out:) if successful.
         */
J
Jim Fehlig 已提交
3337
        ret = libxlDomainDetachHostDevice(driver, vm,
3338 3339 3340 3341 3342
                                          virDomainNetGetActualHostdev(detach));
        goto out;
    }

    libxl_device_nic_init(&nic);
J
Jim Fehlig 已提交
3343
    if (libxl_mac_to_device_nic(cfg->ctx, vm->def->id,
3344 3345 3346
                                virMacAddrFormat(&detach->mac, mac), &nic))
        goto cleanup;

J
Jim Fehlig 已提交
3347
    if (libxl_device_nic_remove(cfg->ctx, vm->def->id, &nic, 0)) {
3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("libxenlight failed to detach network device"));
        goto cleanup;
    }

    ret = 0;

 cleanup:
    libxl_device_nic_dispose(&nic);
 out:
    if (!ret)
        virDomainNetRemove(vm->def, detachidx);
J
Jim Fehlig 已提交
3360
    virObjectUnref(cfg);
3361 3362 3363
    return ret;
}

3364 3365 3366
static int
libxlDomainDetachDeviceLive(libxlDriverPrivatePtr driver,
                            virDomainObjPtr vm,
3367 3368 3369 3370 3371 3372
                            virDomainDeviceDefPtr dev)
{
    int ret = -1;

    switch (dev->type) {
        case VIR_DOMAIN_DEVICE_DISK:
J
Jim Fehlig 已提交
3373
            ret = libxlDomainDetachDeviceDiskLive(vm, dev);
3374 3375
            break;

3376
        case VIR_DOMAIN_DEVICE_NET:
J
Jim Fehlig 已提交
3377
            ret = libxlDomainDetachNetDevice(driver, vm,
3378 3379 3380
                                             dev->data.net);
            break;

3381
        case VIR_DOMAIN_DEVICE_HOSTDEV:
J
Jim Fehlig 已提交
3382
            ret = libxlDomainDetachHostDevice(driver, vm,
3383
                                              dev->data.hostdev);
3384 3385
            break;

3386
        default:
3387 3388 3389
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("device type '%s' cannot be detached"),
                           virDomainDeviceTypeToString(dev->type));
3390 3391 3392 3393 3394 3395
            break;
    }

    return ret;
}

3396

3397 3398 3399
static int
libxlDomainDetachDeviceConfig(virDomainDefPtr vmdef, virDomainDeviceDefPtr dev)
{
3400
    virDomainDiskDefPtr disk, detach;
3401
    virDomainHostdevDefPtr hostdev, det_hostdev;
3402
    virDomainNetDefPtr net;
3403
    int idx;
3404 3405 3406 3407

    switch (dev->type) {
        case VIR_DOMAIN_DEVICE_DISK:
            disk = dev->data.disk;
3408
            if (!(detach = virDomainDiskRemoveByName(vmdef, disk->dst))) {
3409 3410
                virReportError(VIR_ERR_INVALID_ARG,
                               _("no target device %s"), disk->dst);
3411
                return -1;
3412
            }
3413
            virDomainDiskDefFree(detach);
3414
            break;
3415

3416 3417 3418 3419 3420 3421 3422 3423 3424
        case VIR_DOMAIN_DEVICE_NET:
            net = dev->data.net;
            if ((idx = virDomainNetFindIdx(vmdef, net)) < 0)
                return -1;

            /* this is guaranteed to succeed */
            virDomainNetDefFree(virDomainNetRemove(vmdef, idx));
            break;

3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436
        case VIR_DOMAIN_DEVICE_HOSTDEV: {
            hostdev = dev->data.hostdev;
            if ((idx = virDomainHostdevFind(vmdef, hostdev, &det_hostdev)) < 0) {
                virReportError(VIR_ERR_INVALID_ARG, "%s",
                               _("device not present in domain configuration"));
                return -1;
            }
            virDomainHostdevRemove(vmdef, idx);
            virDomainHostdevDefFree(det_hostdev);
            break;
        }

3437
        default:
3438 3439
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("persistent detach of device is not supported"));
3440
            return -1;
3441 3442
    }

3443
    return 0;
3444 3445 3446
}

static int
J
Jim Fehlig 已提交
3447
libxlDomainUpdateDeviceLive(virDomainObjPtr vm, virDomainDeviceDefPtr dev)
3448 3449 3450 3451 3452 3453 3454 3455 3456
{
    virDomainDiskDefPtr disk;
    int ret = -1;

    switch (dev->type) {
        case VIR_DOMAIN_DEVICE_DISK:
            disk = dev->data.disk;
            switch (disk->device) {
                case VIR_DOMAIN_DISK_DEVICE_CDROM:
J
Jim Fehlig 已提交
3457
                    ret = libxlDomainChangeEjectableMedia(vm, disk);
3458 3459 3460 3461
                    if (ret == 0)
                        dev->data.disk = NULL;
                    break;
                default:
3462 3463 3464
                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                   _("disk bus '%s' cannot be updated."),
                                   virDomainDiskBusTypeToString(disk->bus));
3465 3466 3467 3468
                    break;
            }
            break;
        default:
3469 3470 3471
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("device type '%s' cannot be updated"),
                           virDomainDeviceTypeToString(dev->type));
3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482
            break;
    }

    return ret;
}

static int
libxlDomainUpdateDeviceConfig(virDomainDefPtr vmdef, virDomainDeviceDefPtr dev)
{
    virDomainDiskDefPtr orig;
    virDomainDiskDefPtr disk;
3483
    int idx;
3484 3485 3486 3487 3488
    int ret = -1;

    switch (dev->type) {
        case VIR_DOMAIN_DEVICE_DISK:
            disk = dev->data.disk;
3489
            if ((idx = virDomainDiskIndexByName(vmdef, disk->dst, false)) < 0) {
3490 3491
                virReportError(VIR_ERR_INVALID_ARG,
                               _("target %s doesn't exist."), disk->dst);
3492 3493
                goto cleanup;
            }
3494
            orig = vmdef->disks[idx];
3495
            if (!(orig->device == VIR_DOMAIN_DISK_DEVICE_CDROM)) {
3496 3497
                virReportError(VIR_ERR_INVALID_ARG, "%s",
                               _("this disk doesn't support update"));
3498 3499 3500
                goto cleanup;
            }

3501 3502 3503 3504 3505 3506
            if (virDomainDiskSetSource(orig, virDomainDiskGetSource(disk)) < 0)
                goto cleanup;
            virDomainDiskSetType(orig, virDomainDiskGetType(disk));
            virDomainDiskSetFormat(orig, virDomainDiskGetFormat(disk));
            if (virDomainDiskSetDriver(orig, virDomainDiskGetDriver(disk)) < 0)
                goto cleanup;
3507 3508
            break;
        default:
3509 3510
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("persistent update of device is not supported"));
3511 3512 3513 3514 3515
            goto cleanup;
    }

    ret = 0;

3516
 cleanup:
3517 3518 3519 3520 3521
    return ret;
}


static int
3522 3523
libxlDomainAttachDeviceFlags(virDomainPtr dom, const char *xml,
                             unsigned int flags)
3524 3525
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
3526
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
3527 3528 3529 3530 3531 3532 3533 3534
    virDomainObjPtr vm = NULL;
    virDomainDefPtr vmdef = NULL;
    virDomainDeviceDefPtr dev = NULL;
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_DEVICE_MODIFY_LIVE |
                  VIR_DOMAIN_DEVICE_MODIFY_CONFIG, -1);

J
Jim Fehlig 已提交
3535
    if (!(vm = libxlDomObjFromDomain(dom)))
3536 3537
        goto cleanup;

3538 3539 3540
    if (virDomainAttachDeviceFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
        goto cleanup;

3541 3542 3543
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

3544 3545 3546 3547 3548 3549 3550 3551
    if (virDomainObjIsActive(vm)) {
        if (flags == VIR_DOMAIN_DEVICE_MODIFY_CURRENT)
            flags |= VIR_DOMAIN_DEVICE_MODIFY_LIVE;
    } else {
        if (flags == VIR_DOMAIN_DEVICE_MODIFY_CURRENT)
            flags |= VIR_DOMAIN_DEVICE_MODIFY_CONFIG;
        /* check consistency between flags and the vm state */
        if (flags & VIR_DOMAIN_DEVICE_MODIFY_LIVE) {
3552 3553
            virReportError(VIR_ERR_OPERATION_INVALID,
                           "%s", _("Domain is not running"));
3554
            goto endjob;
3555 3556 3557 3558
        }
    }

    if ((flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) && !vm->persistent) {
3559 3560
         virReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("cannot modify device on transient domain"));
3561
         goto endjob;
3562 3563 3564
    }

    if (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) {
3565
        if (!(dev = virDomainDeviceDefParse(xml, vm->def,
3566
                                            cfg->caps, driver->xmlopt,
3567
                                            VIR_DOMAIN_DEF_PARSE_INACTIVE)))
3568
            goto endjob;
3569 3570

        /* Make a copy for updated domain. */
3571
        if (!(vmdef = virDomainObjCopyPersistentDef(vm, cfg->caps,
3572
                                                    driver->xmlopt)))
3573
            goto endjob;
3574

3575
        if (libxlDomainAttachDeviceConfig(vmdef, dev) < 0)
3576
            goto endjob;
3577
    }
3578 3579 3580 3581

    if (flags & VIR_DOMAIN_DEVICE_MODIFY_LIVE) {
        /* If dev exists it was created to modify the domain config. Free it. */
        virDomainDeviceDefFree(dev);
3582
        if (!(dev = virDomainDeviceDefParse(xml, vm->def,
3583
                                            cfg->caps, driver->xmlopt,
3584
                                            VIR_DOMAIN_DEF_PARSE_INACTIVE)))
3585
            goto endjob;
3586

J
Jim Fehlig 已提交
3587
        if (libxlDomainAttachDeviceLive(driver, vm, dev) < 0)
3588
            goto endjob;
3589

3590 3591 3592 3593
        /*
         * update domain status forcibly because the domain status may be
         * changed even if we attach the device failed.
         */
3594
        if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm) < 0)
3595
            goto endjob;
3596 3597
    }

3598 3599
    ret = 0;

3600
    /* Finally, if no error until here, we can save config. */
3601
    if (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) {
3602
        ret = virDomainSaveConfig(cfg->configDir, vmdef);
3603
        if (!ret) {
3604
            virDomainObjAssignDef(vm, vmdef, false, NULL);
3605 3606 3607 3608
            vmdef = NULL;
        }
    }

3609
 endjob:
3610 3611 3612
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

3613
 cleanup:
3614 3615 3616
    virDomainDefFree(vmdef);
    virDomainDeviceDefFree(dev);
    if (vm)
3617
        virObjectUnlock(vm);
3618
    virObjectUnref(cfg);
3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632
    return ret;
}

static int
libxlDomainAttachDevice(virDomainPtr dom, const char *xml)
{
    return libxlDomainAttachDeviceFlags(dom, xml,
                                        VIR_DOMAIN_DEVICE_MODIFY_LIVE);
}

static int
libxlDomainDetachDeviceFlags(virDomainPtr dom, const char *xml,
                             unsigned int flags)
{
3633
    libxlDriverPrivatePtr driver = dom->conn->privateData;
3634
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
3635 3636 3637 3638 3639 3640 3641 3642
    virDomainObjPtr vm = NULL;
    virDomainDefPtr vmdef = NULL;
    virDomainDeviceDefPtr dev = NULL;
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_DEVICE_MODIFY_LIVE |
                  VIR_DOMAIN_DEVICE_MODIFY_CONFIG, -1);

J
Jim Fehlig 已提交
3643
    if (!(vm = libxlDomObjFromDomain(dom)))
3644 3645
        goto cleanup;

3646 3647 3648
    if (virDomainDetachDeviceFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
        goto cleanup;

3649 3650 3651
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

3652 3653 3654 3655 3656 3657 3658 3659 3660 3661
    if (virDomainObjIsActive(vm)) {
        if (flags == VIR_DOMAIN_DEVICE_MODIFY_CURRENT)
            flags |= VIR_DOMAIN_DEVICE_MODIFY_LIVE;
    } else {
        if (flags == VIR_DOMAIN_DEVICE_MODIFY_CURRENT)
            flags |= VIR_DOMAIN_DEVICE_MODIFY_CONFIG;
        /* check consistency between flags and the vm state */
        if (flags & VIR_DOMAIN_DEVICE_MODIFY_LIVE) {
            virReportError(VIR_ERR_OPERATION_INVALID,
                           "%s", _("Domain is not running"));
3662
            goto endjob;
3663 3664 3665 3666 3667 3668
        }
    }

    if ((flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) && !vm->persistent) {
         virReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("cannot modify device on transient domain"));
3669
         goto endjob;
3670 3671 3672 3673
    }

    if (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) {
        if (!(dev = virDomainDeviceDefParse(xml, vm->def,
3674
                                            cfg->caps, driver->xmlopt,
3675
                                            VIR_DOMAIN_DEF_PARSE_INACTIVE)))
3676
            goto endjob;
3677 3678

        /* Make a copy for updated domain. */
3679
        if (!(vmdef = virDomainObjCopyPersistentDef(vm, cfg->caps,
3680
                                                    driver->xmlopt)))
3681
            goto endjob;
3682

3683
        if (libxlDomainDetachDeviceConfig(vmdef, dev) < 0)
3684
            goto endjob;
3685 3686 3687 3688 3689 3690
    }

    if (flags & VIR_DOMAIN_DEVICE_MODIFY_LIVE) {
        /* If dev exists it was created to modify the domain config. Free it. */
        virDomainDeviceDefFree(dev);
        if (!(dev = virDomainDeviceDefParse(xml, vm->def,
3691
                                            cfg->caps, driver->xmlopt,
3692
                                            VIR_DOMAIN_DEF_PARSE_INACTIVE)))
3693
            goto endjob;
3694

J
Jim Fehlig 已提交
3695
        if (libxlDomainDetachDeviceLive(driver, vm, dev) < 0)
3696
            goto endjob;
3697 3698 3699 3700 3701

        /*
         * update domain status forcibly because the domain status may be
         * changed even if we attach the device failed.
         */
3702
        if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm) < 0)
3703
            goto endjob;
3704 3705
    }

3706 3707
    ret = 0;

3708
    /* Finally, if no error until here, we can save config. */
3709
    if (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) {
3710
        ret = virDomainSaveConfig(cfg->configDir, vmdef);
3711 3712 3713 3714 3715 3716
        if (!ret) {
            virDomainObjAssignDef(vm, vmdef, false, NULL);
            vmdef = NULL;
        }
    }

3717
 endjob:
3718 3719 3720
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

3721
 cleanup:
3722 3723 3724 3725
    virDomainDefFree(vmdef);
    virDomainDeviceDefFree(dev);
    if (vm)
        virObjectUnlock(vm);
3726
    virObjectUnref(cfg);
3727
    return ret;
3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740
}

static int
libxlDomainDetachDevice(virDomainPtr dom, const char *xml)
{
    return libxlDomainDetachDeviceFlags(dom, xml,
                                        VIR_DOMAIN_DEVICE_MODIFY_LIVE);
}

static int
libxlDomainUpdateDeviceFlags(virDomainPtr dom, const char *xml,
                             unsigned int flags)
{
3741
    libxlDriverPrivatePtr driver = dom->conn->privateData;
3742
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
3743 3744 3745 3746 3747 3748 3749 3750
    virDomainObjPtr vm = NULL;
    virDomainDefPtr vmdef = NULL;
    virDomainDeviceDefPtr dev = NULL;
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_DEVICE_MODIFY_LIVE |
                  VIR_DOMAIN_DEVICE_MODIFY_CONFIG, -1);

J
Jim Fehlig 已提交
3751
    if (!(vm = libxlDomObjFromDomain(dom)))
3752 3753
        goto cleanup;

3754 3755 3756
    if (virDomainUpdateDeviceFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
        goto cleanup;

3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778
    if (virDomainObjIsActive(vm)) {
        if (flags == VIR_DOMAIN_DEVICE_MODIFY_CURRENT)
            flags |= VIR_DOMAIN_DEVICE_MODIFY_LIVE;
    } else {
        if (flags == VIR_DOMAIN_DEVICE_MODIFY_CURRENT)
            flags |= VIR_DOMAIN_DEVICE_MODIFY_CONFIG;
        /* check consistency between flags and the vm state */
        if (flags & VIR_DOMAIN_DEVICE_MODIFY_LIVE) {
            virReportError(VIR_ERR_OPERATION_INVALID,
                           "%s", _("Domain is not running"));
            goto cleanup;
        }
    }

    if ((flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) && !vm->persistent) {
         virReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("cannot modify device on transient domain"));
         goto cleanup;
    }

    if (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) {
        if (!(dev = virDomainDeviceDefParse(xml, vm->def,
3779
                                            cfg->caps, driver->xmlopt,
3780
                                            VIR_DOMAIN_DEF_PARSE_INACTIVE)))
3781 3782 3783
            goto cleanup;

        /* Make a copy for updated domain. */
3784
        if (!(vmdef = virDomainObjCopyPersistentDef(vm, cfg->caps,
3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797
                                                    driver->xmlopt)))
            goto cleanup;

        if ((ret = libxlDomainUpdateDeviceConfig(vmdef, dev)) < 0)
            goto cleanup;
    } else {
        ret = 0;
    }

    if (flags & VIR_DOMAIN_DEVICE_MODIFY_LIVE) {
        /* If dev exists it was created to modify the domain config. Free it. */
        virDomainDeviceDefFree(dev);
        if (!(dev = virDomainDeviceDefParse(xml, vm->def,
3798
                                            cfg->caps, driver->xmlopt,
3799
                                            VIR_DOMAIN_DEF_PARSE_INACTIVE)))
3800 3801
            goto cleanup;

J
Jim Fehlig 已提交
3802
        if ((ret = libxlDomainUpdateDeviceLive(vm, dev)) < 0)
3803 3804 3805 3806 3807 3808
            goto cleanup;

        /*
         * update domain status forcibly because the domain status may be
         * changed even if we attach the device failed.
         */
3809
        if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm) < 0)
3810 3811 3812 3813 3814
            ret = -1;
    }

    /* Finally, if no error until here, we can save config. */
    if (!ret && (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG)) {
3815
        ret = virDomainSaveConfig(cfg->configDir, vmdef);
3816 3817 3818 3819 3820 3821
        if (!ret) {
            virDomainObjAssignDef(vm, vmdef, false, NULL);
            vmdef = NULL;
        }
    }

3822
 cleanup:
3823 3824 3825 3826
    virDomainDefFree(vmdef);
    virDomainDeviceDefFree(dev);
    if (vm)
        virObjectUnlock(vm);
3827
    virObjectUnref(cfg);
3828
    return ret;
3829 3830
}

3831 3832 3833 3834 3835
static unsigned long long
libxlNodeGetFreeMemory(virConnectPtr conn)
{
    libxl_physinfo phy_info;
    libxlDriverPrivatePtr driver = conn->privateData;
3836 3837
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
    unsigned long long ret = 0;
3838

3839
    if (virNodeGetFreeMemoryEnsureACL(conn) < 0)
3840
        goto cleanup;
3841

3842
    if (libxl_get_physinfo(cfg->ctx, &phy_info)) {
3843 3844
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("libxl_get_physinfo_info failed"));
3845
        goto cleanup;
3846 3847
    }

3848 3849
    ret = phy_info.free_pages * cfg->verInfo->pagesize;

3850
 cleanup:
3851 3852
    virObjectUnref(cfg);
    return ret;
3853 3854
}

3855 3856 3857 3858 3859 3860 3861 3862 3863 3864
static int
libxlNodeGetCellsFreeMemory(virConnectPtr conn,
                            unsigned long long *freeMems,
                            int startCell,
                            int maxCells)
{
    int n, lastCell, numCells;
    int ret = -1, nr_nodes = 0;
    libxl_numainfo *numa_info = NULL;
    libxlDriverPrivatePtr driver = conn->privateData;
3865
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
3866 3867

    if (virNodeGetCellsFreeMemoryEnsureACL(conn) < 0)
3868
        goto cleanup;
3869

3870
    numa_info = libxl_get_numainfo(cfg->ctx, &nr_nodes);
3871
    if (numa_info == NULL || nr_nodes == 0) {
3872 3873 3874
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("libxl_get_numainfo failed"));
        goto cleanup;
3875 3876 3877
    }

    /* Check/sanitize the cell range */
3878
    if (startCell >= nr_nodes) {
3879 3880
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("start cell %d out of range (0-%d)"),
3881
                       startCell, nr_nodes - 1);
3882 3883 3884
        goto cleanup;
    }
    lastCell = startCell + maxCells - 1;
3885 3886
    if (lastCell >= nr_nodes)
        lastCell = nr_nodes - 1;
3887 3888 3889 3890 3891 3892 3893

    for (numCells = 0, n = startCell; n <= lastCell; n++) {
        if (numa_info[n].size == LIBXL_NUMAINFO_INVALID_ENTRY)
            freeMems[numCells++] = 0;
        else
            freeMems[numCells++] = numa_info[n].free;
    }
3894

3895 3896
    ret = numCells;

3897
 cleanup:
3898
    libxl_numainfo_list_free(numa_info, nr_nodes);
3899
    virObjectUnref(cfg);
3900 3901 3902
    return ret;
}

3903
static int
3904
libxlConnectDomainEventRegister(virConnectPtr conn,
3905 3906
                                virConnectDomainEventCallback callback,
                                void *opaque,
3907
                                virFreeCallback freecb)
3908 3909 3910
{
    libxlDriverPrivatePtr driver = conn->privateData;

3911 3912 3913
    if (virConnectDomainEventRegisterEnsureACL(conn) < 0)
        return -1;

3914 3915 3916 3917
    if (virDomainEventStateRegister(conn,
                                    driver->domainEventState,
                                    callback, opaque, freecb) < 0)
        return -1;
3918

3919
    return 0;
3920 3921 3922 3923
}


static int
3924 3925
libxlConnectDomainEventDeregister(virConnectPtr conn,
                                  virConnectDomainEventCallback callback)
3926 3927 3928
{
    libxlDriverPrivatePtr driver = conn->privateData;

3929 3930 3931
    if (virConnectDomainEventDeregisterEnsureACL(conn) < 0)
        return -1;

3932 3933 3934 3935
    if (virDomainEventStateDeregister(conn,
                                      driver->domainEventState,
                                      callback) < 0)
        return -1;
3936

3937
    return 0;
3938 3939
}

3940 3941 3942 3943 3944 3945
static int
libxlDomainGetAutostart(virDomainPtr dom, int *autostart)
{
    virDomainObjPtr vm;
    int ret = -1;

J
Jim Fehlig 已提交
3946
    if (!(vm = libxlDomObjFromDomain(dom)))
3947 3948
        goto cleanup;

3949 3950 3951
    if (virDomainGetAutostartEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

3952 3953 3954
    *autostart = vm->autostart;
    ret = 0;

3955
 cleanup:
3956
    if (vm)
3957
        virObjectUnlock(vm);
3958 3959 3960 3961 3962 3963 3964
    return ret;
}

static int
libxlDomainSetAutostart(virDomainPtr dom, int autostart)
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
3965
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
3966 3967 3968 3969
    virDomainObjPtr vm;
    char *configFile = NULL, *autostartLink = NULL;
    int ret = -1;

J
Jim Fehlig 已提交
3970
    if (!(vm = libxlDomObjFromDomain(dom)))
3971 3972
        goto cleanup;

3973 3974 3975
    if (virDomainSetAutostartEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

3976 3977 3978
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

3979
    if (!vm->persistent) {
3980 3981
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("cannot set autostart for transient domain"));
3982
        goto endjob;
3983 3984 3985 3986 3987
    }

    autostart = (autostart != 0);

    if (vm->autostart != autostart) {
3988
        if (!(configFile = virDomainConfigFile(cfg->configDir, vm->def->name)))
3989
            goto endjob;
3990
        if (!(autostartLink = virDomainConfigFile(cfg->autostartDir, vm->def->name)))
3991
            goto endjob;
3992 3993

        if (autostart) {
3994
            if (virFileMakePath(cfg->autostartDir) < 0) {
3995
                virReportSystemError(errno,
3996
                                     _("cannot create autostart directory %s"),
3997
                                     cfg->autostartDir);
3998
                goto endjob;
3999 4000 4001 4002 4003 4004
            }

            if (symlink(configFile, autostartLink) < 0) {
                virReportSystemError(errno,
                                     _("Failed to create symlink '%s to '%s'"),
                                     autostartLink, configFile);
4005
                goto endjob;
4006 4007 4008 4009 4010 4011
            }
        } else {
            if (unlink(autostartLink) < 0 && errno != ENOENT && errno != ENOTDIR) {
                virReportSystemError(errno,
                                     _("Failed to delete symlink '%s'"),
                                     autostartLink);
4012
                goto endjob;
4013 4014 4015 4016 4017 4018 4019
            }
        }

        vm->autostart = autostart;
    }
    ret = 0;

4020
 endjob:
4021 4022 4023
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

4024
 cleanup:
4025 4026 4027
    VIR_FREE(configFile);
    VIR_FREE(autostartLink);
    if (vm)
4028
        virObjectUnlock(vm);
4029
    virObjectUnref(cfg);
4030 4031 4032
    return ret;
}

4033 4034 4035
static char *
libxlDomainGetSchedulerType(virDomainPtr dom, int *nparams)
{
J
Jim Fehlig 已提交
4036 4037
    libxlDriverPrivatePtr driver = dom->conn->privateData;
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
4038 4039
    virDomainObjPtr vm;
    char * ret = NULL;
4040
    const char *name = NULL;
J
Jim Fehlig 已提交
4041
    libxl_scheduler sched_id;
4042

J
Jim Fehlig 已提交
4043
    if (!(vm = libxlDomObjFromDomain(dom)))
4044 4045
        goto cleanup;

4046 4047 4048
    if (virDomainGetSchedulerTypeEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

4049
    if (!virDomainObjIsActive(vm)) {
4050
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not running"));
4051 4052 4053
        goto cleanup;
    }

J
Jim Fehlig 已提交
4054
    sched_id = libxl_get_scheduler(cfg->ctx);
4055

4056 4057
    if (nparams)
        *nparams = 0;
4058
    switch (sched_id) {
J
Jim Fehlig 已提交
4059
    case LIBXL_SCHEDULER_SEDF:
4060
        name = "sedf";
4061
        break;
J
Jim Fehlig 已提交
4062
    case LIBXL_SCHEDULER_CREDIT:
4063
        name = "credit";
4064 4065
        if (nparams)
            *nparams = XEN_SCHED_CREDIT_NPARAM;
4066
        break;
J
Jim Fehlig 已提交
4067
    case LIBXL_SCHEDULER_CREDIT2:
4068
        name = "credit2";
4069
        break;
J
Jim Fehlig 已提交
4070
    case LIBXL_SCHEDULER_ARINC653:
4071
        name = "arinc653";
4072 4073
        break;
    default:
J
Jim Fehlig 已提交
4074 4075
        virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("Failed to get scheduler id for domain '%d'"
4076
                     " with libxenlight"), vm->def->id);
4077 4078 4079
        goto cleanup;
    }

4080
    ignore_value(VIR_STRDUP(ret, name));
4081

4082
 cleanup:
4083
    if (vm)
4084
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
4085
    virObjectUnref(cfg);
4086 4087 4088
    return ret;
}

4089
static int
4090 4091 4092 4093
libxlDomainGetSchedulerParametersFlags(virDomainPtr dom,
                                       virTypedParameterPtr params,
                                       int *nparams,
                                       unsigned int flags)
4094
{
J
Jim Fehlig 已提交
4095 4096
    libxlDriverPrivatePtr driver = dom->conn->privateData;
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
4097
    virDomainObjPtr vm;
J
Jim Fehlig 已提交
4098 4099
    libxl_domain_sched_params sc_info;
    libxl_scheduler sched_id;
4100 4101
    int ret = -1;

4102 4103 4104 4105
    virCheckFlags(VIR_TYPED_PARAM_STRING_OKAY, -1);

    /* We don't return strings, and thus trivially support this flag.  */
    flags &= ~VIR_TYPED_PARAM_STRING_OKAY;
4106

J
Jim Fehlig 已提交
4107
    if (!(vm = libxlDomObjFromDomain(dom)))
4108 4109
        goto cleanup;

4110 4111 4112
    if (virDomainGetSchedulerParametersFlagsEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

4113
    if (!virDomainObjIsActive(vm)) {
J
Jim Fehlig 已提交
4114 4115
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Domain is not running"));
4116 4117 4118
        goto cleanup;
    }

J
Jim Fehlig 已提交
4119
    sched_id = libxl_get_scheduler(cfg->ctx);
4120

J
Jim Fehlig 已提交
4121
    if (sched_id != LIBXL_SCHEDULER_CREDIT) {
4122 4123
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Only 'credit' scheduler is supported"));
4124 4125 4126
        goto cleanup;
    }

J
Jim Fehlig 已提交
4127
    if (libxl_domain_sched_params_get(cfg->ctx, vm->def->id, &sc_info) != 0) {
4128 4129
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to get scheduler parameters for domain '%d'"
4130
                         " with libxenlight"), vm->def->id);
4131 4132 4133
        goto cleanup;
    }

4134 4135
    if (virTypedParameterAssign(&params[0], VIR_DOMAIN_SCHEDULER_WEIGHT,
                                VIR_TYPED_PARAM_UINT, sc_info.weight) < 0)
4136 4137
        goto cleanup;

4138
    if (*nparams > 1) {
4139 4140
        if (virTypedParameterAssign(&params[0], VIR_DOMAIN_SCHEDULER_CAP,
                                    VIR_TYPED_PARAM_UINT, sc_info.cap) < 0)
4141
            goto cleanup;
4142 4143
    }

4144 4145
    if (*nparams > XEN_SCHED_CREDIT_NPARAM)
        *nparams = XEN_SCHED_CREDIT_NPARAM;
4146 4147
    ret = 0;

4148
 cleanup:
4149
    if (vm)
4150
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
4151
    virObjectUnref(cfg);
4152 4153 4154 4155
    return ret;
}

static int
4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166
libxlDomainGetSchedulerParameters(virDomainPtr dom, virTypedParameterPtr params,
                                  int *nparams)
{
    return libxlDomainGetSchedulerParametersFlags(dom, params, nparams, 0);
}

static int
libxlDomainSetSchedulerParametersFlags(virDomainPtr dom,
                                       virTypedParameterPtr params,
                                       int nparams,
                                       unsigned int flags)
4167
{
4168
    libxlDriverPrivatePtr driver = dom->conn->privateData;
J
Jim Fehlig 已提交
4169
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
4170
    virDomainObjPtr vm;
J
Jim Fehlig 已提交
4171
    libxl_domain_sched_params sc_info;
4172
    int sched_id;
4173
    size_t i;
4174 4175
    int ret = -1;

4176
    virCheckFlags(0, -1);
4177 4178 4179 4180 4181 4182
    if (virTypedParamsValidate(params, nparams,
                               VIR_DOMAIN_SCHEDULER_WEIGHT,
                               VIR_TYPED_PARAM_UINT,
                               VIR_DOMAIN_SCHEDULER_CAP,
                               VIR_TYPED_PARAM_UINT,
                               NULL) < 0)
4183
        return -1;
4184

J
Jim Fehlig 已提交
4185
    if (!(vm = libxlDomObjFromDomain(dom)))
4186 4187
        goto cleanup;

4188 4189 4190
    if (virDomainSetSchedulerParametersFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
        goto cleanup;

4191 4192 4193
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

4194
    if (!virDomainObjIsActive(vm)) {
4195
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not running"));
4196
        goto endjob;
4197 4198
    }

J
Jim Fehlig 已提交
4199
    sched_id = libxl_get_scheduler(cfg->ctx);
4200

J
Jim Fehlig 已提交
4201
    if (sched_id != LIBXL_SCHEDULER_CREDIT) {
4202 4203
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Only 'credit' scheduler is supported"));
4204
        goto endjob;
4205 4206
    }

J
Jim Fehlig 已提交
4207
    if (libxl_domain_sched_params_get(cfg->ctx, vm->def->id, &sc_info) != 0) {
4208 4209
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to get scheduler parameters for domain '%d'"
4210
                         " with libxenlight"), vm->def->id);
4211
        goto endjob;
4212 4213 4214
    }

    for (i = 0; i < nparams; ++i) {
4215
        virTypedParameterPtr param = &params[i];
4216

4217
        if (STREQ(param->field, VIR_DOMAIN_SCHEDULER_WEIGHT))
4218
            sc_info.weight = params[i].value.ui;
4219
        else if (STREQ(param->field, VIR_DOMAIN_SCHEDULER_CAP))
4220 4221 4222
            sc_info.cap = params[i].value.ui;
    }

J
Jim Fehlig 已提交
4223
    if (libxl_domain_sched_params_set(cfg->ctx, vm->def->id, &sc_info) != 0) {
4224 4225
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to set scheduler parameters for domain '%d'"
4226
                         " with libxenlight"), vm->def->id);
4227
        goto endjob;
4228 4229 4230 4231
    }

    ret = 0;

4232
 endjob:
4233 4234 4235
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

4236
 cleanup:
4237
    if (vm)
4238
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
4239
    virObjectUnref(cfg);
4240 4241 4242
    return ret;
}

B
Bamvor Jian Zhang 已提交
4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263

static int
libxlDomainOpenConsole(virDomainPtr dom,
                       const char *dev_name,
                       virStreamPtr st,
                       unsigned int flags)
{
    virDomainObjPtr vm = NULL;
    int ret = -1;
    virDomainChrDefPtr chr = NULL;
    libxlDomainObjPrivatePtr priv;

    virCheckFlags(VIR_DOMAIN_CONSOLE_FORCE, -1);

    if (dev_name) {
        /* XXX support device aliases in future */
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Named device aliases are not supported"));
        goto cleanup;
    }

J
Jim Fehlig 已提交
4264
    if (!(vm = libxlDomObjFromDomain(dom)))
B
Bamvor Jian Zhang 已提交
4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277
        goto cleanup;

    if (virDomainOpenConsoleEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

    if (!virDomainObjIsActive(vm)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("domain is not running"));
        goto cleanup;
    }

    priv = vm->privateData;

I
Ian Campbell 已提交
4278 4279
    if (vm->def->nconsoles)
        chr = vm->def->consoles[0];
B
Bamvor Jian Zhang 已提交
4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306

    if (!chr) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("cannot find character device %s"),
                       NULLSTR(dev_name));
        goto cleanup;
    }

    if (chr->source.type != VIR_DOMAIN_CHR_TYPE_PTY) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("character device %s is not using a PTY"),
                       NULLSTR(dev_name));
        goto cleanup;
    }

    /* handle mutually exclusive access to console devices */
    ret = virChrdevOpen(priv->devs,
                        &chr->source,
                        st,
                        (flags & VIR_DOMAIN_CONSOLE_FORCE) != 0);

    if (ret == 1) {
        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                       _("Active console session exists for this domain"));
        ret = -1;
    }

4307
 cleanup:
B
Bamvor Jian Zhang 已提交
4308 4309 4310 4311 4312
    if (vm)
        virObjectUnlock(vm);
    return ret;
}

4313 4314 4315 4316 4317 4318 4319
static int
libxlDomainSetSchedulerParameters(virDomainPtr dom, virTypedParameterPtr params,
                                  int nparams)
{
    return libxlDomainSetSchedulerParametersFlags(dom, params, nparams, 0);
}

4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332
/* NUMA node affinity information is available through libxl
 * starting from Xen 4.3. */
#ifdef LIBXL_HAVE_DOMAIN_NODEAFFINITY

/* Number of Xen NUMA parameters */
# define LIBXL_NUMA_NPARAM 2

static int
libxlDomainGetNumaParameters(virDomainPtr dom,
                             virTypedParameterPtr params,
                             int *nparams,
                             unsigned int flags)
{
J
Jim Fehlig 已提交
4333 4334
    libxlDriverPrivatePtr driver = dom->conn->privateData;
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350
    virDomainObjPtr vm;
    libxl_bitmap nodemap;
    virBitmapPtr nodes = NULL;
    char *nodeset = NULL;
    int rc, ret = -1;
    size_t i, j;

    /* In Xen 4.3, it is possible to query the NUMA node affinity of a domain
     * via libxl, but not to change it. We therefore only allow AFFECT_LIVE. */
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_TYPED_PARAM_STRING_OKAY, -1);

    /* We blindly return a string, and let libvirt.c and remote_driver.c do
     * the filtering on behalf of older clients that can't parse it. */
    flags &= ~VIR_TYPED_PARAM_STRING_OKAY;

4351 4352
    libxl_bitmap_init(&nodemap);

J
Jim Fehlig 已提交
4353
    if (!(vm = libxlDomObjFromDomain(dom)))
4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372
        goto cleanup;

    if (virDomainGetNumaParametersEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

    if (!virDomainObjIsActive(vm)) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Domain is not running"));
        goto cleanup;
    }

    if ((*nparams) == 0) {
        *nparams = LIBXL_NUMA_NPARAM;
        ret = 0;
        goto cleanup;
    }

    for (i = 0; i < LIBXL_NUMA_NPARAM && i < *nparams; i++) {
        virMemoryParameterPtr param = &params[i];
4373
        int numnodes;
4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391

        switch (i) {
        case 0:
            /* NUMA mode */

            /* Xen implements something that is really close to numactl's
             * 'interleave' policy (see `man 8 numactl' for details). */
            if (virTypedParameterAssign(param, VIR_DOMAIN_NUMA_MODE,
                                        VIR_TYPED_PARAM_INT,
                                        VIR_DOMAIN_NUMATUNE_MEM_INTERLEAVE) < 0)
                goto cleanup;

            break;

        case 1:
            /* Node affinity */

            /* Let's allocate both libxl and libvirt bitmaps */
J
Jim Fehlig 已提交
4392
            numnodes = libxl_get_max_nodes(cfg->ctx);
4393 4394 4395
            if (numnodes <= 0)
                goto cleanup;

J
Jim Fehlig 已提交
4396
            if (libxl_node_bitmap_alloc(cfg->ctx, &nodemap, 0)) {
4397 4398 4399
                virReportOOMError();
                goto cleanup;
            }
J
Ján Tomko 已提交
4400 4401
            if (!(nodes = virBitmapNew(numnodes)))
                goto cleanup;
4402

J
Jim Fehlig 已提交
4403
            rc = libxl_domain_get_nodeaffinity(cfg->ctx,
4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422
                                               vm->def->id,
                                               &nodemap);
            if (rc != 0) {
                virReportSystemError(-rc, "%s",
                                     _("unable to get numa affinity"));
                goto cleanup;
            }

            /* First, we convert libxl_bitmap into virBitmap. After that,
             * we format virBitmap as a string that can be returned. */
            virBitmapClearAll(nodes);
            libxl_for_each_set_bit(j, nodemap) {
                if (virBitmapSetBit(nodes, j)) {
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("Node %zu out of range"), j);
                    goto cleanup;
                }
            }

4423
            if (!(nodeset = virBitmapFormat(nodes)))
4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439
                goto cleanup;

            if (virTypedParameterAssign(param, VIR_DOMAIN_NUMA_NODESET,
                                        VIR_TYPED_PARAM_STRING, nodeset) < 0)
                goto cleanup;

            nodeset = NULL;

            break;
        }
    }

    if (*nparams > LIBXL_NUMA_NPARAM)
        *nparams = LIBXL_NUMA_NPARAM;
    ret = 0;

4440
 cleanup:
4441 4442 4443 4444 4445
    VIR_FREE(nodeset);
    virBitmapFree(nodes);
    libxl_bitmap_dispose(&nodemap);
    if (vm)
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
4446
    virObjectUnref(cfg);
4447 4448 4449 4450
    return ret;
}
#endif

J
Jim Fehlig 已提交
4451 4452 4453 4454 4455 4456
static int
libxlDomainIsActive(virDomainPtr dom)
{
    virDomainObjPtr obj;
    int ret = -1;

J
Jim Fehlig 已提交
4457
    if (!(obj = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
4458
        goto cleanup;
4459 4460 4461 4462

    if (virDomainIsActiveEnsureACL(dom->conn, obj->def) < 0)
        goto cleanup;

J
Jim Fehlig 已提交
4463 4464
    ret = virDomainObjIsActive(obj);

4465
 cleanup:
J
Jim Fehlig 已提交
4466
    if (obj)
4467
        virObjectUnlock(obj);
J
Jim Fehlig 已提交
4468 4469 4470 4471 4472 4473 4474 4475 4476
    return ret;
}

static int
libxlDomainIsPersistent(virDomainPtr dom)
{
    virDomainObjPtr obj;
    int ret = -1;

J
Jim Fehlig 已提交
4477
    if (!(obj = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
4478
        goto cleanup;
4479 4480 4481 4482

    if (virDomainIsPersistentEnsureACL(dom->conn, obj->def) < 0)
        goto cleanup;

J
Jim Fehlig 已提交
4483 4484
    ret = obj->persistent;

4485
 cleanup:
J
Jim Fehlig 已提交
4486
    if (obj)
4487
        virObjectUnlock(obj);
J
Jim Fehlig 已提交
4488 4489 4490
    return ret;
}

4491 4492 4493 4494 4495 4496
static int
libxlDomainIsUpdated(virDomainPtr dom)
{
    virDomainObjPtr vm;
    int ret = -1;

J
Jim Fehlig 已提交
4497
    if (!(vm = libxlDomObjFromDomain(dom)))
4498
        goto cleanup;
4499 4500 4501 4502

    if (virDomainIsUpdatedEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

4503 4504
    ret = vm->updated;

4505
 cleanup:
4506
    if (vm)
4507
        virObjectUnlock(vm);
4508 4509 4510
    return ret;
}

4511
static int
4512 4513 4514
libxlConnectDomainEventRegisterAny(virConnectPtr conn, virDomainPtr dom, int eventID,
                                   virConnectDomainEventGenericCallback callback,
                                   void *opaque, virFreeCallback freecb)
4515 4516 4517 4518
{
    libxlDriverPrivatePtr driver = conn->privateData;
    int ret;

4519 4520 4521
    if (virConnectDomainEventRegisterAnyEnsureACL(conn) < 0)
        return -1;

4522 4523 4524 4525
    if (virDomainEventStateRegisterID(conn,
                                      driver->domainEventState,
                                      dom, eventID, callback, opaque,
                                      freecb, &ret) < 0)
4526
        ret = -1;
4527 4528 4529 4530 4531 4532

    return ret;
}


static int
4533
libxlConnectDomainEventDeregisterAny(virConnectPtr conn, int callbackID)
4534 4535 4536
{
    libxlDriverPrivatePtr driver = conn->privateData;

4537 4538 4539
    if (virConnectDomainEventDeregisterAnyEnsureACL(conn) < 0)
        return -1;

4540 4541 4542 4543
    if (virObjectEventStateDeregisterID(conn,
                                        driver->domainEventState,
                                        callbackID) < 0)
        return -1;
4544

4545
    return 0;
4546 4547
}

J
Jim Fehlig 已提交
4548

4549
static int
4550
libxlConnectIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
4551 4552 4553 4554
{
    return 1;
}

4555
static int
4556 4557 4558
libxlConnectListAllDomains(virConnectPtr conn,
                           virDomainPtr **domains,
                           unsigned int flags)
4559 4560 4561 4562
{
    libxlDriverPrivatePtr driver = conn->privateData;
    int ret = -1;

O
Osier Yang 已提交
4563
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
4564

4565 4566 4567
    if (virConnectListAllDomainsEnsureACL(conn) < 0)
        return -1;

4568 4569
    ret = virDomainObjListExport(driver->domains, conn, domains,
                                 virConnectListAllDomainsCheckACL, flags);
4570 4571 4572 4573

    return ret;
}

4574 4575 4576 4577 4578 4579 4580 4581 4582
/* Which features are supported by this driver? */
static int
libxlConnectSupportsFeature(virConnectPtr conn, int feature)
{
    if (virConnectSupportsFeatureEnsureACL(conn) < 0)
        return -1;

    switch (feature) {
    case VIR_DRV_FEATURE_TYPED_PARAM_STRING:
J
Jim Fehlig 已提交
4583
    case VIR_DRV_FEATURE_MIGRATION_PARAMS:
4584 4585 4586 4587 4588
        return 1;
    default:
        return 0;
    }
}
4589

4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614
static int
libxlNodeDeviceGetPCIInfo(virNodeDeviceDefPtr def,
                          unsigned *domain,
                          unsigned *bus,
                          unsigned *slot,
                          unsigned *function)
{
    virNodeDevCapsDefPtr cap;

    cap = def->caps;
    while (cap) {
        if (cap->type == VIR_NODE_DEV_CAP_PCI_DEV) {
            *domain   = cap->data.pci_dev.domain;
            *bus      = cap->data.pci_dev.bus;
            *slot     = cap->data.pci_dev.slot;
            *function = cap->data.pci_dev.function;
            break;
        }

        cap = cap->next;
    }

    if (!cap) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("device %s is not a PCI device"), def->name);
C
Chunyan Liu 已提交
4615
        return -1;
4616 4617
    }

C
Chunyan Liu 已提交
4618
    return 0;
4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666
}

static int
libxlNodeDeviceDetachFlags(virNodeDevicePtr dev,
                           const char *driverName,
                           unsigned int flags)
{
    virPCIDevicePtr pci = NULL;
    unsigned domain = 0, bus = 0, slot = 0, function = 0;
    int ret = -1;
    virNodeDeviceDefPtr def = NULL;
    char *xml = NULL;
    libxlDriverPrivatePtr driver = dev->conn->privateData;
    virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;

    virCheckFlags(0, -1);

    xml = virNodeDeviceGetXMLDesc(dev, 0);
    if (!xml)
        goto cleanup;

    def = virNodeDeviceDefParseString(xml, EXISTING_DEVICE, NULL);
    if (!def)
        goto cleanup;

    if (virNodeDeviceDetachFlagsEnsureACL(dev->conn, def) < 0)
        goto cleanup;

    if (libxlNodeDeviceGetPCIInfo(def, &domain, &bus, &slot, &function) < 0)
        goto cleanup;

    pci = virPCIDeviceNew(domain, bus, slot, function);
    if (!pci)
        goto cleanup;

    if (!driverName || STREQ(driverName, "xen")) {
        if (virPCIDeviceSetStubDriver(pci, "pciback") < 0)
            goto cleanup;
    } else {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unsupported driver name '%s'"), driverName);
        goto cleanup;
    }

    if (virHostdevPCINodeDeviceDetach(hostdev_mgr, pci) < 0)
        goto cleanup;

    ret = 0;
4667
 cleanup:
4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709
    virPCIDeviceFree(pci);
    virNodeDeviceDefFree(def);
    VIR_FREE(xml);
    return ret;
}

static int
libxlNodeDeviceDettach(virNodeDevicePtr dev)
{
    return libxlNodeDeviceDetachFlags(dev, NULL, 0);
}

static int
libxlNodeDeviceReAttach(virNodeDevicePtr dev)
{
    virPCIDevicePtr pci = NULL;
    unsigned domain = 0, bus = 0, slot = 0, function = 0;
    int ret = -1;
    virNodeDeviceDefPtr def = NULL;
    char *xml = NULL;
    libxlDriverPrivatePtr driver = dev->conn->privateData;
    virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;

    xml = virNodeDeviceGetXMLDesc(dev, 0);
    if (!xml)
        goto cleanup;

    def = virNodeDeviceDefParseString(xml, EXISTING_DEVICE, NULL);
    if (!def)
        goto cleanup;

    if (virNodeDeviceReAttachEnsureACL(dev->conn, def) < 0)
        goto cleanup;

    if (libxlNodeDeviceGetPCIInfo(def, &domain, &bus, &slot, &function) < 0)
        goto cleanup;

    pci = virPCIDeviceNew(domain, bus, slot, function);
    if (!pci)
        goto cleanup;

    if (virHostdevPCINodeDeviceReAttach(hostdev_mgr, pci) < 0)
C
Chunyan Liu 已提交
4710
        goto cleanup;
4711 4712

    ret = 0;
C
Chunyan Liu 已提交
4713

4714
 cleanup:
C
Chunyan Liu 已提交
4715
    virPCIDeviceFree(pci);
4716 4717 4718 4719 4720 4721 4722 4723
    virNodeDeviceDefFree(def);
    VIR_FREE(xml);
    return ret;
}

static int
libxlNodeDeviceReset(virNodeDevicePtr dev)
{
C
Chunyan Liu 已提交
4724
    virPCIDevicePtr pci = NULL;
4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750
    unsigned domain = 0, bus = 0, slot = 0, function = 0;
    int ret = -1;
    virNodeDeviceDefPtr def = NULL;
    char *xml = NULL;
    libxlDriverPrivatePtr driver = dev->conn->privateData;
    virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;

    xml = virNodeDeviceGetXMLDesc(dev, 0);
    if (!xml)
        goto cleanup;

    def = virNodeDeviceDefParseString(xml, EXISTING_DEVICE, NULL);
    if (!def)
        goto cleanup;

    if (virNodeDeviceResetEnsureACL(dev->conn, def) < 0)
        goto cleanup;

    if (libxlNodeDeviceGetPCIInfo(def, &domain, &bus, &slot, &function) < 0)
        goto cleanup;

    pci = virPCIDeviceNew(domain, bus, slot, function);
    if (!pci)
        goto cleanup;

    if (virHostdevPCINodeDeviceReset(hostdev_mgr, pci) < 0)
C
Chunyan Liu 已提交
4751
        goto cleanup;
4752 4753

    ret = 0;
C
Chunyan Liu 已提交
4754

4755
 cleanup:
C
Chunyan Liu 已提交
4756
    virPCIDeviceFree(pci);
4757 4758 4759 4760 4761
    virNodeDeviceDefFree(def);
    VIR_FREE(xml);
    return ret;
}

J
Jim Fehlig 已提交
4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772
static char *
libxlDomainMigrateBegin3Params(virDomainPtr domain,
                               virTypedParameterPtr params,
                               int nparams,
                               char **cookieout ATTRIBUTE_UNUSED,
                               int *cookieoutlen ATTRIBUTE_UNUSED,
                               unsigned int flags)
{
    const char *xmlin = NULL;
    virDomainObjPtr vm = NULL;

4773 4774 4775 4776 4777
#ifdef LIBXL_HAVE_NO_SUSPEND_RESUME
    virReportUnsupportedError();
    return NULL;
#endif

J
Jim Fehlig 已提交
4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821
    virCheckFlags(LIBXL_MIGRATION_FLAGS, NULL);
    if (virTypedParamsValidate(params, nparams, LIBXL_MIGRATION_PARAMETERS) < 0)
        return NULL;

    if (virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_DEST_XML,
                                &xmlin) < 0)
        return NULL;

    if (!(vm = libxlDomObjFromDomain(domain)))
        return NULL;

    if (virDomainMigrateBegin3ParamsEnsureACL(domain->conn, vm->def) < 0) {
        virObjectUnlock(vm);
        return NULL;
    }

    if (!virDomainObjIsActive(vm)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("domain is not running"));
        virObjectUnlock(vm);
        return NULL;
    }

    return libxlDomainMigrationBegin(domain->conn, vm, xmlin);
}

static int
libxlDomainMigratePrepare3Params(virConnectPtr dconn,
                                 virTypedParameterPtr params,
                                 int nparams,
                                 const char *cookiein ATTRIBUTE_UNUSED,
                                 int cookieinlen ATTRIBUTE_UNUSED,
                                 char **cookieout ATTRIBUTE_UNUSED,
                                 int *cookieoutlen ATTRIBUTE_UNUSED,
                                 char **uri_out,
                                 unsigned int flags)
{
    libxlDriverPrivatePtr driver = dconn->privateData;
    virDomainDefPtr def = NULL;
    const char *dom_xml = NULL;
    const char *dname = NULL;
    const char *uri_in = NULL;

4822 4823 4824 4825 4826
#ifdef LIBXL_HAVE_NO_SUSPEND_RESUME
    virReportUnsupportedError();
    return -1;
#endif

J
Jim Fehlig 已提交
4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848
    virCheckFlags(LIBXL_MIGRATION_FLAGS, -1);
    if (virTypedParamsValidate(params, nparams, LIBXL_MIGRATION_PARAMETERS) < 0)
        goto error;

    if (virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_DEST_XML,
                                &dom_xml) < 0 ||
        virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_DEST_NAME,
                                &dname) < 0 ||
        virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_URI,
                                &uri_in) < 0)

        goto error;

    if (!(def = libxlDomainMigrationPrepareDef(driver, dom_xml, dname)))
        goto error;

    if (virDomainMigratePrepare3ParamsEnsureACL(dconn, def) < 0)
        goto error;

4849
    if (libxlDomainMigrationPrepare(dconn, &def, uri_in, uri_out, flags) < 0)
J
Jim Fehlig 已提交
4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876
        goto error;

    return 0;

 error:
    virDomainDefFree(def);
    return -1;
}

static int
libxlDomainMigratePerform3Params(virDomainPtr dom,
                                 const char *dconnuri,
                                 virTypedParameterPtr params,
                                 int nparams,
                                 const char *cookiein ATTRIBUTE_UNUSED,
                                 int cookieinlen ATTRIBUTE_UNUSED,
                                 char **cookieout ATTRIBUTE_UNUSED,
                                 int *cookieoutlen ATTRIBUTE_UNUSED,
                                 unsigned int flags)
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
    virDomainObjPtr vm = NULL;
    const char *dom_xml = NULL;
    const char *dname = NULL;
    const char *uri = NULL;
    int ret = -1;

4877 4878 4879 4880 4881
#ifdef LIBXL_HAVE_NO_SUSPEND_RESUME
    virReportUnsupportedError();
    return -1;
#endif

J
Jim Fehlig 已提交
4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904
    virCheckFlags(LIBXL_MIGRATION_FLAGS, -1);
    if (virTypedParamsValidate(params, nparams, LIBXL_MIGRATION_PARAMETERS) < 0)
        goto cleanup;

    if (virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_DEST_XML,
                                &dom_xml) < 0 ||
        virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_DEST_NAME,
                                &dname) < 0 ||
        virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_URI,
                                &uri) < 0)

        goto cleanup;

    if (!(vm = libxlDomObjFromDomain(dom)))
        goto cleanup;

    if (virDomainMigratePerform3ParamsEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

    if (libxlDomainMigrationPerform(driver, vm, dom_xml, dconnuri,
4905
                                    uri, dname, flags) < 0)
J
Jim Fehlig 已提交
4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929
        goto cleanup;

    ret = 0;

 cleanup:
    if (vm)
        virObjectUnlock(vm);
    return ret;
}

static virDomainPtr
libxlDomainMigrateFinish3Params(virConnectPtr dconn,
                                virTypedParameterPtr params,
                                int nparams,
                                const char *cookiein ATTRIBUTE_UNUSED,
                                int cookieinlen ATTRIBUTE_UNUSED,
                                char **cookieout ATTRIBUTE_UNUSED,
                                int *cookieoutlen ATTRIBUTE_UNUSED,
                                unsigned int flags,
                                int cancelled)
{
    libxlDriverPrivatePtr driver = dconn->privateData;
    virDomainObjPtr vm = NULL;
    const char *dname = NULL;
4930
    virDomainPtr ret = NULL;
J
Jim Fehlig 已提交
4931

4932 4933 4934 4935 4936
#ifdef LIBXL_HAVE_NO_SUSPEND_RESUME
    virReportUnsupportedError();
    return NULL;
#endif

J
Jim Fehlig 已提交
4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956
    virCheckFlags(LIBXL_MIGRATION_FLAGS, NULL);
    if (virTypedParamsValidate(params, nparams, LIBXL_MIGRATION_PARAMETERS) < 0)
        return NULL;

    if (virTypedParamsGetString(params, nparams,
                                VIR_MIGRATE_PARAM_DEST_NAME,
                                &dname) < 0)
        return NULL;

    if (!dname ||
        !(vm = virDomainObjListFindByName(driver->domains, dname))) {
        /* Migration obviously failed if the domain doesn't exist */
        virReportError(VIR_ERR_OPERATION_FAILED,
                       _("Migration failed. No domain on destination host "
                         "with matching name '%s'"),
                       NULLSTR(dname));
        return NULL;
    }

    if (virDomainMigrateFinish3ParamsEnsureACL(dconn, vm->def) < 0) {
4957
        virDomainObjEndAPI(&vm);
J
Jim Fehlig 已提交
4958 4959 4960
        return NULL;
    }

4961
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0) {
4962
        virDomainObjEndAPI(&vm);
4963 4964 4965 4966 4967 4968 4969 4970
        return NULL;
    }

    ret = libxlDomainMigrationFinish(dconn, vm, flags, cancelled);

    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

4971
    virDomainObjEndAPI(&vm);
4972 4973

    return ret;
J
Jim Fehlig 已提交
4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987
}

static int
libxlDomainMigrateConfirm3Params(virDomainPtr domain,
                                 virTypedParameterPtr params,
                                 int nparams,
                                 const char *cookiein ATTRIBUTE_UNUSED,
                                 int cookieinlen ATTRIBUTE_UNUSED,
                                 unsigned int flags,
                                 int cancelled)
{
    libxlDriverPrivatePtr driver = domain->conn->privateData;
    virDomainObjPtr vm = NULL;

4988 4989 4990 4991 4992
#ifdef LIBXL_HAVE_NO_SUSPEND_RESUME
    virReportUnsupportedError();
    return -1;
#endif

J
Jim Fehlig 已提交
4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007
    virCheckFlags(LIBXL_MIGRATION_FLAGS, -1);
    if (virTypedParamsValidate(params, nparams, LIBXL_MIGRATION_PARAMETERS) < 0)
        return -1;

    if (!(vm = libxlDomObjFromDomain(domain)))
        return -1;

    if (virDomainMigrateConfirm3ParamsEnsureACL(domain->conn, vm->def) < 0) {
        virObjectUnlock(vm);
        return -1;
    }

    return libxlDomainMigrationConfirm(driver, vm, flags, cancelled);
}

5008

5009
static virHypervisorDriver libxlHypervisorDriver = {
5010
    .name = LIBXL_DRIVER_NAME,
5011 5012 5013 5014
    .connectOpen = libxlConnectOpen, /* 0.9.0 */
    .connectClose = libxlConnectClose, /* 0.9.0 */
    .connectGetType = libxlConnectGetType, /* 0.9.0 */
    .connectGetVersion = libxlConnectGetVersion, /* 0.9.0 */
5015
    .connectGetHostname = libxlConnectGetHostname, /* 0.9.0 */
5016
    .connectGetSysinfo = libxlConnectGetSysinfo, /* 1.1.0 */
5017
    .connectGetMaxVcpus = libxlConnectGetMaxVcpus, /* 0.9.0 */
5018
    .nodeGetInfo = libxlNodeGetInfo, /* 0.9.0 */
5019 5020 5021 5022
    .connectGetCapabilities = libxlConnectGetCapabilities, /* 0.9.0 */
    .connectListDomains = libxlConnectListDomains, /* 0.9.0 */
    .connectNumOfDomains = libxlConnectNumOfDomains, /* 0.9.0 */
    .connectListAllDomains = libxlConnectListAllDomains, /* 0.9.13 */
5023 5024 5025 5026 5027 5028 5029
    .domainCreateXML = libxlDomainCreateXML, /* 0.9.0 */
    .domainLookupByID = libxlDomainLookupByID, /* 0.9.0 */
    .domainLookupByUUID = libxlDomainLookupByUUID, /* 0.9.0 */
    .domainLookupByName = libxlDomainLookupByName, /* 0.9.0 */
    .domainSuspend = libxlDomainSuspend, /* 0.9.0 */
    .domainResume = libxlDomainResume, /* 0.9.0 */
    .domainShutdown = libxlDomainShutdown, /* 0.9.0 */
5030
    .domainShutdownFlags = libxlDomainShutdownFlags, /* 0.9.10 */
5031 5032
    .domainReboot = libxlDomainReboot, /* 0.9.0 */
    .domainDestroy = libxlDomainDestroy, /* 0.9.0 */
5033
    .domainDestroyFlags = libxlDomainDestroyFlags, /* 0.9.4 */
5034 5035
    .domainGetOSType = libxlDomainGetOSType, /* 0.9.0 */
    .domainGetMaxMemory = libxlDomainGetMaxMemory, /* 0.9.0 */
5036
    .domainSetMaxMemory = libxlDomainSetMaxMemory, /* 0.9.2 */
5037 5038 5039 5040
    .domainSetMemory = libxlDomainSetMemory, /* 0.9.0 */
    .domainSetMemoryFlags = libxlDomainSetMemoryFlags, /* 0.9.0 */
    .domainGetInfo = libxlDomainGetInfo, /* 0.9.0 */
    .domainGetState = libxlDomainGetState, /* 0.9.2 */
5041
    .domainSave = libxlDomainSave, /* 0.9.2 */
5042
    .domainSaveFlags = libxlDomainSaveFlags, /* 0.9.4 */
5043
    .domainRestore = libxlDomainRestore, /* 0.9.2 */
5044
    .domainRestoreFlags = libxlDomainRestoreFlags, /* 0.9.4 */
5045
    .domainCoreDump = libxlDomainCoreDump, /* 0.9.2 */
5046 5047 5048 5049
    .domainSetVcpus = libxlDomainSetVcpus, /* 0.9.0 */
    .domainSetVcpusFlags = libxlDomainSetVcpusFlags, /* 0.9.0 */
    .domainGetVcpusFlags = libxlDomainGetVcpusFlags, /* 0.9.0 */
    .domainPinVcpu = libxlDomainPinVcpu, /* 0.9.0 */
5050
    .domainPinVcpuFlags = libxlDomainPinVcpuFlags, /* 1.2.1 */
5051
    .domainGetVcpus = libxlDomainGetVcpus, /* 0.9.0 */
5052
    .domainGetVcpuPinInfo = libxlDomainGetVcpuPinInfo, /* 1.2.1 */
5053
    .domainGetXMLDesc = libxlDomainGetXMLDesc, /* 0.9.0 */
5054 5055 5056 5057
    .connectDomainXMLFromNative = libxlConnectDomainXMLFromNative, /* 0.9.0 */
    .connectDomainXMLToNative = libxlConnectDomainXMLToNative, /* 0.9.0 */
    .connectListDefinedDomains = libxlConnectListDefinedDomains, /* 0.9.0 */
    .connectNumOfDefinedDomains = libxlConnectNumOfDefinedDomains, /* 0.9.0 */
5058 5059 5060
    .domainCreate = libxlDomainCreate, /* 0.9.0 */
    .domainCreateWithFlags = libxlDomainCreateWithFlags, /* 0.9.0 */
    .domainDefineXML = libxlDomainDefineXML, /* 0.9.0 */
5061
    .domainDefineXMLFlags = libxlDomainDefineXMLFlags, /* 1.2.12 */
5062
    .domainUndefine = libxlDomainUndefine, /* 0.9.0 */
5063
    .domainUndefineFlags = libxlDomainUndefineFlags, /* 0.9.4 */
5064 5065 5066 5067 5068
    .domainAttachDevice = libxlDomainAttachDevice, /* 0.9.2 */
    .domainAttachDeviceFlags = libxlDomainAttachDeviceFlags, /* 0.9.2 */
    .domainDetachDevice = libxlDomainDetachDevice,    /* 0.9.2 */
    .domainDetachDeviceFlags = libxlDomainDetachDeviceFlags, /* 0.9.2 */
    .domainUpdateDeviceFlags = libxlDomainUpdateDeviceFlags, /* 0.9.2 */
5069 5070 5071 5072
    .domainGetAutostart = libxlDomainGetAutostart, /* 0.9.0 */
    .domainSetAutostart = libxlDomainSetAutostart, /* 0.9.0 */
    .domainGetSchedulerType = libxlDomainGetSchedulerType, /* 0.9.0 */
    .domainGetSchedulerParameters = libxlDomainGetSchedulerParameters, /* 0.9.0 */
5073
    .domainGetSchedulerParametersFlags = libxlDomainGetSchedulerParametersFlags, /* 0.9.2 */
5074
    .domainSetSchedulerParameters = libxlDomainSetSchedulerParameters, /* 0.9.0 */
5075
    .domainSetSchedulerParametersFlags = libxlDomainSetSchedulerParametersFlags, /* 0.9.2 */
5076 5077 5078
#ifdef LIBXL_HAVE_DOMAIN_NODEAFFINITY
    .domainGetNumaParameters = libxlDomainGetNumaParameters, /* 1.1.1 */
#endif
5079
    .nodeGetFreeMemory = libxlNodeGetFreeMemory, /* 0.9.0 */
5080
    .nodeGetCellsFreeMemory = libxlNodeGetCellsFreeMemory, /* 1.1.1 */
5081 5082
    .connectDomainEventRegister = libxlConnectDomainEventRegister, /* 0.9.0 */
    .connectDomainEventDeregister = libxlConnectDomainEventDeregister, /* 0.9.0 */
5083 5084 5085
    .domainManagedSave = libxlDomainManagedSave, /* 0.9.2 */
    .domainHasManagedSaveImage = libxlDomainHasManagedSaveImage, /* 0.9.2 */
    .domainManagedSaveRemove = libxlDomainManagedSaveRemove, /* 0.9.2 */
B
Bamvor Jian Zhang 已提交
5086
    .domainOpenConsole = libxlDomainOpenConsole, /* 1.1.2 */
5087 5088 5089
    .domainIsActive = libxlDomainIsActive, /* 0.9.0 */
    .domainIsPersistent = libxlDomainIsPersistent, /* 0.9.0 */
    .domainIsUpdated = libxlDomainIsUpdated, /* 0.9.0 */
5090 5091 5092
    .connectDomainEventRegisterAny = libxlConnectDomainEventRegisterAny, /* 0.9.0 */
    .connectDomainEventDeregisterAny = libxlConnectDomainEventDeregisterAny, /* 0.9.0 */
    .connectIsAlive = libxlConnectIsAlive, /* 0.9.8 */
5093
    .connectSupportsFeature = libxlConnectSupportsFeature, /* 1.1.1 */
5094 5095 5096 5097
    .nodeDeviceDettach = libxlNodeDeviceDettach, /* 1.2.3 */
    .nodeDeviceDetachFlags = libxlNodeDeviceDetachFlags, /* 1.2.3 */
    .nodeDeviceReAttach = libxlNodeDeviceReAttach, /* 1.2.3 */
    .nodeDeviceReset = libxlNodeDeviceReset, /* 1.2.3 */
5098 5099 5100 5101 5102
    .domainMigrateBegin3Params = libxlDomainMigrateBegin3Params, /* 1.2.6 */
    .domainMigratePrepare3Params = libxlDomainMigratePrepare3Params, /* 1.2.6 */
    .domainMigratePerform3Params = libxlDomainMigratePerform3Params, /* 1.2.6 */
    .domainMigrateFinish3Params = libxlDomainMigrateFinish3Params, /* 1.2.6 */
    .domainMigrateConfirm3Params = libxlDomainMigrateConfirm3Params, /* 1.2.6 */
J
Jim Fehlig 已提交
5103 5104
};

5105 5106 5107 5108
static virConnectDriver libxlConnectDriver = {
    .hypervisorDriver = &libxlHypervisorDriver,
};

J
Jim Fehlig 已提交
5109 5110
static virStateDriver libxlStateDriver = {
    .name = "LIBXL",
5111
    .stateInitialize = libxlStateInitialize,
5112
    .stateAutoStart = libxlStateAutoStart,
5113 5114
    .stateCleanup = libxlStateCleanup,
    .stateReload = libxlStateReload,
J
Jim Fehlig 已提交
5115 5116 5117 5118 5119 5120
};


int
libxlRegister(void)
{
5121 5122
    if (virRegisterConnectDriver(&libxlConnectDriver,
                                 true) < 0)
J
Jim Fehlig 已提交
5123 5124 5125 5126 5127 5128
        return -1;
    if (virRegisterStateDriver(&libxlStateDriver) < 0)
        return -1;

    return 0;
}