libxl_driver.c 139.9 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-2014 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"
48
#include "xen_xm.h"
49
#include "xen_sxpr.h"
50
#include "virtypedparam.h"
M
Martin Kletzander 已提交
51
#include "viruri.h"
52
#include "virstring.h"
53
#include "virsysinfo.h"
54
#include "viraccessapicheck.h"
55
#include "viratomic.h"
56
#include "virhostdev.h"
J
Jim Fehlig 已提交
57 58 59

#define VIR_FROM_THIS VIR_FROM_LIBXL

60 61
VIR_LOG_INIT("libxl.libxl_driver");

J
Jim Fehlig 已提交
62 63 64 65 66 67
#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

68
#define LIBXL_CONFIG_FORMAT_XM "xen-xm"
69
#define LIBXL_CONFIG_FORMAT_SEXPR "xen-sxpr"
70

71 72
#define HYPERVISOR_CAPABILITIES "/proc/xen/capabilities"

73 74 75
/* Number of Xen scheduler parameters */
#define XEN_SCHED_CREDIT_NPARAM   2

76

J
Jim Fehlig 已提交
77 78 79
static libxlDriverPrivatePtr libxl_driver = NULL;

/* Function declarations */
80 81
static int
libxlDomainManagedSaveLoad(virDomainObjPtr vm,
82 83
                           void *opaque);

J
Jim Fehlig 已提交
84
static int
85 86
libxlVmStart(libxlDriverPrivatePtr driver, virDomainObjPtr vm,
             bool start_paused, int restore_fd);
J
Jim Fehlig 已提交
87

J
Jim Fehlig 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

/* Function definitions */
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;
}

109 110
static int
libxlAutostartDomain(virDomainObjPtr vm,
111 112 113 114
                     void *opaque)
{
    libxlDriverPrivatePtr driver = opaque;
    virErrorPtr err;
115
    int ret = -1;
116

117
    virObjectLock(vm);
118 119 120
    virResetLastError();

    if (vm->autostart && !virDomainObjIsActive(vm) &&
121
        libxlVmStart(driver, vm, false, -1) < 0) {
122 123 124 125
        err = virGetLastError();
        VIR_ERROR(_("Failed to autostart VM '%s': %s"),
                  vm->def->name,
                  err ? err->message : _("unknown error"));
126
        goto cleanup;
127 128
    }

129 130
    ret = 0;
cleanup:
131
    virObjectUnlock(vm);
132
    return ret;
133 134
}

J
Jim Fehlig 已提交
135
/*
J
Jim Fehlig 已提交
136 137 138 139 140
 * Handle previously registered event notification from libxenlight.
 *
 * Note: Xen 4.3 removed the const from the event handler signature.
 * Detect which signature to use based on
 * LIBXL_HAVE_NONCONST_EVENT_OCCURS_EVENT_ARG.
J
Jim Fehlig 已提交
141
 */
J
Jim Fehlig 已提交
142 143 144 145 146 147 148

#ifdef LIBXL_HAVE_NONCONST_EVENT_OCCURS_EVENT_ARG
# define VIR_LIBXL_EVENT_CONST /* empty */
#else
# define VIR_LIBXL_EVENT_CONST const
#endif

149 150 151 152 153 154 155
struct libxlShutdownThreadInfo
{
    virDomainObjPtr vm;
    libxl_event *event;
};


156
static void
157
libxlDomainShutdownThread(void *opaque)
J
Jim Fehlig 已提交
158 159
{
    libxlDriverPrivatePtr driver = libxl_driver;
160 161 162 163 164
    struct libxlShutdownThreadInfo *shutdown_info = opaque;
    virDomainObjPtr vm = shutdown_info->vm;
    libxlDomainObjPrivatePtr priv = vm->privateData;
    libxl_event *ev = shutdown_info->event;
    libxl_ctx *ctx = priv->ctx;
165
    virObjectEventPtr dom_event = NULL;
166
    libxl_shutdown_reason xl_reason = ev->u.domain_shutdown.shutdown_reason;
167
    virDomainShutoffReason reason = VIR_DOMAIN_SHUTOFF_SHUTDOWN;
J
Jim Fehlig 已提交
168

169
    virObjectLock(vm);
J
Jiri Denemark 已提交
170

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    if (xl_reason == LIBXL_SHUTDOWN_REASON_POWEROFF) {
        dom_event = virDomainEventLifecycleNewFromObj(vm,
                                           VIR_DOMAIN_EVENT_STOPPED,
                                           VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
        switch ((enum virDomainLifecycleAction) vm->def->onPoweroff) {
        case VIR_DOMAIN_LIFECYCLE_DESTROY:
            reason = VIR_DOMAIN_SHUTOFF_SHUTDOWN;
            goto destroy;
        case VIR_DOMAIN_LIFECYCLE_RESTART:
        case VIR_DOMAIN_LIFECYCLE_RESTART_RENAME:
            goto restart;
        case VIR_DOMAIN_LIFECYCLE_PRESERVE:
        case VIR_DOMAIN_LIFECYCLE_LAST:
            goto cleanup;
        }
    } else if (xl_reason == LIBXL_SHUTDOWN_REASON_CRASH) {
        dom_event = virDomainEventLifecycleNewFromObj(vm,
188 189
                                           VIR_DOMAIN_EVENT_STOPPED,
                                           VIR_DOMAIN_EVENT_STOPPED_CRASHED);
190 191 192 193 194 195 196 197 198 199
        switch ((enum virDomainLifecycleCrashAction) vm->def->onCrash) {
        case VIR_DOMAIN_LIFECYCLE_CRASH_DESTROY:
            reason = VIR_DOMAIN_SHUTOFF_CRASHED;
            goto destroy;
        case VIR_DOMAIN_LIFECYCLE_CRASH_RESTART:
        case VIR_DOMAIN_LIFECYCLE_CRASH_RESTART_RENAME:
            goto restart;
        case VIR_DOMAIN_LIFECYCLE_CRASH_PRESERVE:
        case VIR_DOMAIN_LIFECYCLE_CRASH_LAST:
            goto cleanup;
200 201 202 203 204 205
        case VIR_DOMAIN_LIFECYCLE_CRASH_COREDUMP_DESTROY:
            libxlDomainAutoCoreDump(driver, vm);
            goto destroy;
        case VIR_DOMAIN_LIFECYCLE_CRASH_COREDUMP_RESTART:
            libxlDomainAutoCoreDump(driver, vm);
            goto restart;
206 207 208
        }
    } else if (xl_reason == LIBXL_SHUTDOWN_REASON_REBOOT) {
        dom_event = virDomainEventLifecycleNewFromObj(vm,
209 210
                                           VIR_DOMAIN_EVENT_STOPPED,
                                           VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
211 212 213 214 215 216 217 218 219 220 221 222 223 224
        switch ((enum virDomainLifecycleAction) vm->def->onReboot) {
        case VIR_DOMAIN_LIFECYCLE_DESTROY:
            reason = VIR_DOMAIN_SHUTOFF_SHUTDOWN;
            goto destroy;
        case VIR_DOMAIN_LIFECYCLE_RESTART:
        case VIR_DOMAIN_LIFECYCLE_RESTART_RENAME:
            goto restart;
        case VIR_DOMAIN_LIFECYCLE_PRESERVE:
        case VIR_DOMAIN_LIFECYCLE_LAST:
            goto cleanup;
        }
    } else {
        VIR_INFO("Unhandled shutdown_reason %d", xl_reason);
        goto cleanup;
J
Jim Fehlig 已提交
225 226
    }

227
destroy:
228 229 230 231
    if (dom_event) {
        libxlDomainEventQueue(driver, dom_event);
        dom_event = NULL;
    }
232
    libxl_domain_destroy(ctx, vm->def->id, NULL);
233
    if (libxlDomainCleanupJob(driver, vm, reason)) {
234 235 236 237 238 239 240 241
        if (!vm->persistent) {
            virDomainObjListRemove(driver->domains, vm);
            vm = NULL;
        }
    }
    goto cleanup;

restart:
242 243 244 245
    if (dom_event) {
        libxlDomainEventQueue(driver, dom_event);
        dom_event = NULL;
    }
246
    libxl_domain_destroy(ctx, vm->def->id, NULL);
247
    libxlDomainCleanupJob(driver, vm, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
248 249 250
    libxlVmStart(driver, vm, 0, -1);

cleanup:
J
Jim Fehlig 已提交
251
    if (vm)
252
        virObjectUnlock(vm);
253
    if (dom_event)
254
        libxlDomainEventQueue(driver, dom_event);
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    libxl_event_free(ctx, ev);
    VIR_FREE(shutdown_info);
}

static void
libxlEventHandler(void *data, VIR_LIBXL_EVENT_CONST libxl_event *event)
{
    virDomainObjPtr vm = data;
    libxlDomainObjPrivatePtr priv = vm->privateData;
    libxl_shutdown_reason xl_reason = event->u.domain_shutdown.shutdown_reason;
    struct libxlShutdownThreadInfo *shutdown_info;
    virThread thread;

    if (event->type != LIBXL_EVENT_TYPE_DOMAIN_SHUTDOWN) {
        VIR_INFO("Unhandled event type %d", event->type);
        goto error;
    }

    /*
     * Similar to the xl implementation, ignore SUSPEND.  Any actions needed
     * after calling libxl_domain_suspend() are handled by it's callers.
     */
    if (xl_reason == LIBXL_SHUTDOWN_REASON_SUSPEND)
        goto error;

    /*
     * Start a thread to handle shutdown.  We don't want to be tying up
     * libxl's event machinery by doing a potentially lengthy shutdown.
     */
    if (VIR_ALLOC(shutdown_info) < 0)
        goto error;

    shutdown_info->vm = data;
    shutdown_info->event = (libxl_event *)event;
    if (virThreadCreate(&thread, false, libxlDomainShutdownThread,
                        shutdown_info) < 0) {
        /*
         * Not much we can do on error here except log it.
         */
        VIR_ERROR(_("Failed to create thread to handle domain shutdown"));
        goto error;
    }

    /*
     * libxl_event freed in shutdown thread
     */
    return;

error:
J
Jim Fehlig 已提交
304 305
    /* Cast away any const */
    libxl_event_free(priv->ctx, (libxl_event *)event);
J
Jim Fehlig 已提交
306 307
}

308
const struct libxl_event_hooks ev_hooks = {
J
Jim Fehlig 已提交
309 310 311 312 313
    .event_occurs_mask = LIBXL_EVENTMASK_ALL,
    .event_occurs = libxlEventHandler,
    .disaster = NULL,
};

J
Jim Fehlig 已提交
314 315 316 317 318 319
/*
 * Start a domain through libxenlight.
 *
 * virDomainObjPtr should be locked on invocation
 */
static int
320 321
libxlVmStart(libxlDriverPrivatePtr driver, virDomainObjPtr vm,
             bool start_paused, int restore_fd)
J
Jim Fehlig 已提交
322 323
{
    libxl_domain_config d_config;
324
    virDomainDefPtr def = NULL;
325
    virObjectEventPtr event = NULL;
326
    libxlSavefileHeader hdr;
327
    int ret = -1;
J
Jim Fehlig 已提交
328 329
    uint32_t domid = 0;
    char *dom_xml = NULL;
330 331
    char *managed_save_path = NULL;
    int managed_save_fd = -1;
J
Jim Fehlig 已提交
332
    libxlDomainObjPrivatePtr priv = vm->privateData;
333
    libxlDriverConfigPtr cfg;
B
Bamvor Jian Zhang 已提交
334 335 336
#ifdef LIBXL_HAVE_DOMAIN_CREATE_RESTORE_PARAMS
    libxl_domain_restore_params params;
#endif
337
    virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;
J
Jim Fehlig 已提交
338

J
Jim Fehlig 已提交
339
    if (libxlDomainObjPrivateInitCtx(vm) < 0)
340 341 342 343
        return ret;

    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        return ret;
J
Jim Fehlig 已提交
344

345
    cfg = libxlDriverConfigGet(driver);
346 347 348
    /* If there is a managed saved state restore it instead of starting
     * from scratch. The old state is removed once the restoring succeeded. */
    if (restore_fd < 0) {
349 350
        managed_save_path = libxlDomainManagedSavePath(driver, vm);
        if (managed_save_path == NULL)
351
            goto endjob;
352

353
        if (virFileExists(managed_save_path)) {
354

355 356 357
            managed_save_fd = libxlDomainSaveImageOpen(driver, cfg,
                                                       managed_save_path,
                                                       &def, &hdr);
358
            if (managed_save_fd < 0)
359
                goto endjob;
360

361 362
            restore_fd = managed_save_fd;

363 364 365 366 367 368
            if (STRNEQ(vm->def->name, def->name) ||
                memcmp(vm->def->uuid, def->uuid, VIR_UUID_BUFLEN)) {
                char vm_uuidstr[VIR_UUID_STRING_BUFLEN];
                char def_uuidstr[VIR_UUID_STRING_BUFLEN];
                virUUIDFormat(vm->def->uuid, vm_uuidstr);
                virUUIDFormat(def->uuid, def_uuidstr);
369 370 371 372
                virReportError(VIR_ERR_OPERATION_FAILED,
                               _("cannot restore domain '%s' uuid %s from a file"
                                 " which belongs to domain '%s' uuid %s"),
                               vm->def->name, vm_uuidstr, def->name, def_uuidstr);
373
                goto endjob;
374 375
            }

376
            virDomainObjAssignDef(vm, def, true, NULL);
377 378
            def = NULL;

379
            if (unlink(managed_save_path) < 0)
380 381
                VIR_WARN("Failed to remove the managed state %s",
                         managed_save_path);
382

383
            vm->hasManagedSave = false;
384
        }
385
        VIR_FREE(managed_save_path);
386 387
    }

J
Jim Fehlig 已提交
388
    libxl_domain_config_init(&d_config);
J
Jim Fehlig 已提交
389

390
    if (libxlBuildDomainConfig(driver, vm, &d_config) < 0)
391
        goto endjob;
J
Jim Fehlig 已提交
392

393
    if (cfg->autoballoon && libxlDomainFreeMem(priv, &d_config) < 0) {
394 395 396
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("libxenlight failed to get free memory for domain '%s'"),
                       d_config.c_info.name);
397
        goto endjob;
M
Markus Groß 已提交
398
    }
J
Jim Fehlig 已提交
399

400 401 402 403
    if (virHostdevPrepareDomainDevices(hostdev_mgr, LIBXL_DRIVER_NAME,
                                       vm->def, VIR_HOSTDEV_SP_PCI) < 0)
        goto endjob;

404 405
    /* Unlock virDomainObj while creating the domain */
    virObjectUnlock(vm);
406
    if (restore_fd < 0) {
J
Jim Fehlig 已提交
407 408
        ret = libxl_domain_create_new(priv->ctx, &d_config,
                                      &domid, NULL, NULL);
409
    } else {
B
Bamvor Jian Zhang 已提交
410 411 412 413 414
#ifdef LIBXL_HAVE_DOMAIN_CREATE_RESTORE_PARAMS
        params.checkpointed_stream = 0;
        ret = libxl_domain_create_restore(priv->ctx, &d_config, &domid,
                                          restore_fd, &params, NULL, NULL);
#else
J
Jim Fehlig 已提交
415 416
        ret = libxl_domain_create_restore(priv->ctx, &d_config, &domid,
                                          restore_fd, NULL, NULL);
B
Bamvor Jian Zhang 已提交
417
#endif
418
    }
419
    virObjectLock(vm);
420

J
Jim Fehlig 已提交
421
    if (ret) {
422
        if (restore_fd < 0)
423 424 425
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("libxenlight failed to create new domain '%s'"),
                           d_config.c_info.name);
426
        else
427 428 429
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("libxenlight failed to restore domain '%s'"),
                           d_config.c_info.name);
430
        goto endjob;
J
Jim Fehlig 已提交
431 432
    }

433 434 435 436
    /*
     * The domain has been successfully created with libxl, so it should
     * be cleaned up if there are any subsequent failures.
     */
437
    vm->def->id = domid;
438
    if (libxlDomainEventsRegister(vm) < 0)
439
        goto cleanup_dom;
440

441
    if ((dom_xml = virDomainDefFormat(vm->def, 0)) == NULL)
442
        goto cleanup_dom;
J
Jim Fehlig 已提交
443

J
Jim Fehlig 已提交
444
    if (libxl_userdata_store(priv->ctx, domid, "libvirt-xml",
J
Jim Fehlig 已提交
445
                             (uint8_t *)dom_xml, strlen(dom_xml) + 1)) {
446 447
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("libxenlight failed to store userdata"));
448
        goto cleanup_dom;
J
Jim Fehlig 已提交
449 450
    }

451
    if (libxlDomainSetVcpuAffinities(driver, vm) < 0)
452
        goto cleanup_dom;
453

J
Jim Fehlig 已提交
454
    if (!start_paused) {
J
Jim Fehlig 已提交
455
        libxl_domain_unpause(priv->ctx, domid);
J
Jiri Denemark 已提交
456
        virDomainObjSetState(vm, VIR_DOMAIN_RUNNING, VIR_DOMAIN_RUNNING_BOOTED);
J
Jim Fehlig 已提交
457
    } else {
J
Jiri Denemark 已提交
458
        virDomainObjSetState(vm, VIR_DOMAIN_PAUSED, VIR_DOMAIN_PAUSED_USER);
J
Jim Fehlig 已提交
459 460
    }

461
    if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm) < 0)
462
        goto cleanup_dom;
J
Jim Fehlig 已提交
463

464
    if (virAtomicIntInc(&driver->nactive) == 1 && driver->inhibitCallback)
465 466
        driver->inhibitCallback(true, driver->inhibitOpaque);

467
    event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_STARTED,
468 469 470
                                     restore_fd < 0 ?
                                         VIR_DOMAIN_EVENT_STARTED_BOOTED :
                                         VIR_DOMAIN_EVENT_STARTED_RESTORED);
471 472
    if (event)
        libxlDomainEventQueue(driver, event);
473

474 475 476 477 478 479 480 481 482 483 484
    ret = 0;
    goto endjob;

cleanup_dom:
    libxl_domain_destroy(priv->ctx, domid, NULL);
    vm->def->id = -1;
    virDomainObjSetState(vm, VIR_DOMAIN_SHUTOFF, VIR_DOMAIN_SHUTOFF_FAILED);

endjob:
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;
J
Jim Fehlig 已提交
485

J
Jim Fehlig 已提交
486
    libxl_domain_config_dispose(&d_config);
J
Jim Fehlig 已提交
487
    VIR_FREE(dom_xml);
488
    VIR_FREE(managed_save_path);
489
    virDomainDefFree(def);
490
    VIR_FORCE_CLOSE(managed_save_fd);
491
    virObjectUnref(cfg);
492
    return ret;
J
Jim Fehlig 已提交
493 494 495 496 497 498 499
}


/*
 * Reconnect to running domains that were previously started/created
 * with libxenlight driver.
 */
500 501
static int
libxlReconnectDomain(virDomainObjPtr vm,
J
Jim Fehlig 已提交
502 503 504
                     void *opaque)
{
    libxlDriverPrivatePtr driver = opaque;
505
    libxlDomainObjPrivatePtr priv = vm->privateData;
J
Jim Fehlig 已提交
506 507 508 509
    int rc;
    libxl_dominfo d_info;
    int len;
    uint8_t *data = NULL;
510
    virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;
J
Jim Fehlig 已提交
511

512
    virObjectLock(vm);
J
Jim Fehlig 已提交
513

514
    libxlDomainObjPrivateInitCtx(vm);
J
Jim Fehlig 已提交
515
    /* Does domain still exist? */
516
    rc = libxl_domain_info(priv->ctx, &d_info, vm->def->id);
J
Jim Fehlig 已提交
517 518 519 520 521 522 523 524 525
    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? */
526
    if (libxl_userdata_retrieve(priv->ctx, vm->def->id,
J
Jim Fehlig 已提交
527 528 529 530 531 532 533
                                "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;
534 535 536 537 538 539

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

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

542
    if (virAtomicIntInc(&driver->nactive) == 1 && driver->inhibitCallback)
543 544
        driver->inhibitCallback(true, driver->inhibitOpaque);

545
    /* Re-register domain death et. al. events */
546
    libxlDomainEventsRegister(vm);
547
    virObjectUnlock(vm);
548
    return 0;
J
Jim Fehlig 已提交
549 550

out:
551
    libxlDomainCleanup(driver, vm, VIR_DOMAIN_SHUTOFF_UNKNOWN);
J
Jim Fehlig 已提交
552
    if (!vm->persistent)
553
        virDomainObjListRemoveLocked(driver->domains, vm);
J
Jim Fehlig 已提交
554
    else
555
        virObjectUnlock(vm);
556 557

    return -1;
J
Jim Fehlig 已提交
558 559 560 561 562
}

static void
libxlReconnectDomains(libxlDriverPrivatePtr driver)
{
563
    virDomainObjListForEach(driver->domains, libxlReconnectDomain, driver);
J
Jim Fehlig 已提交
564 565 566
}

static int
567
libxlStateCleanup(void)
J
Jim Fehlig 已提交
568 569 570 571
{
    if (!libxl_driver)
        return -1;

572
    virObjectUnref(libxl_driver->hostdevMgr);
573
    virObjectUnref(libxl_driver->config);
574
    virObjectUnref(libxl_driver->xmlopt);
575
    virObjectUnref(libxl_driver->domains);
576
    virObjectUnref(libxl_driver->reservedVNCPorts);
J
Jim Fehlig 已提交
577

578
    virObjectEventStateFree(libxl_driver->domainEventState);
579
    virSysinfoDefFree(libxl_driver->hostsysinfo);
580

J
Jim Fehlig 已提交
581 582 583 584 585 586
    virMutexDestroy(&libxl_driver->lock);
    VIR_FREE(libxl_driver);

    return 0;
}

587 588
static bool
libxlDriverShouldLoad(bool privileged)
589
{
590
    bool ret = false;
J
Jim Fehlig 已提交
591
    virCommandPtr cmd;
592
    int status;
593
    char *output = NULL;
J
Jim Fehlig 已提交
594

595
    /* Don't load if non-root */
J
Jim Fehlig 已提交
596
    if (!privileged) {
597
        VIR_INFO("Not running privileged, disabling libxenlight driver");
598
        return ret;
J
Jim Fehlig 已提交
599 600
    }

601 602 603 604 605
    if (!virFileExists(HYPERVISOR_CAPABILITIES)) {
        VIR_INFO("Disabling driver as " HYPERVISOR_CAPABILITIES
                 " does not exist");
        return false;
    }
606 607 608 609 610
    /*
     * 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.
     */
611
    status = virFileReadAll(HYPERVISOR_CAPABILITIES, 10, &output);
612 613 614 615 616
    if (status >= 0) {
        status = strncmp(output, "control_d", 9);
    }
    VIR_FREE(output);
    if (status) {
617 618 619 620 621 622 623
        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 */
J
Jim Fehlig 已提交
624 625
    cmd = virCommandNewArgList("/usr/sbin/xend", "status", NULL);
    if (virCommandRun(cmd, &status) == 0 && status == 0) {
626
        VIR_INFO("Legacy xen tool stack seems to be in use, disabling "
J
Jim Fehlig 已提交
627
                  "libxenlight driver.");
628 629
    } else {
        ret = true;
J
Jim Fehlig 已提交
630 631 632
    }
    virCommandFree(cmd);

633 634 635 636 637 638 639 640
    return ret;
}

static int
libxlStateInitialize(bool privileged,
                     virStateInhibitCallback callback ATTRIBUTE_UNUSED,
                     void *opaque ATTRIBUTE_UNUSED)
{
641
    libxlDriverConfigPtr cfg;
642 643 644 645 646
    char ebuf[1024];

    if (!libxlDriverShouldLoad(privileged))
        return 0;

J
Jim Fehlig 已提交
647 648 649 650
    if (VIR_ALLOC(libxl_driver) < 0)
        return -1;

    if (virMutexInit(&libxl_driver->lock) < 0) {
651 652
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("cannot initialize mutex"));
J
Jim Fehlig 已提交
653 654 655 656 657
        VIR_FREE(libxl_driver);
        return -1;
    }

    /* Allocate bitmap for vnc port reservation */
658
    if (!(libxl_driver->reservedVNCPorts =
J
Ján Tomko 已提交
659 660
          virPortAllocatorNew(_("VNC"),
                              LIBXL_VNC_PORT_MIN,
661 662
                              LIBXL_VNC_PORT_MAX)))
        goto error;
J
Jim Fehlig 已提交
663

664 665
    if (!(libxl_driver->domains = virDomainObjListNew()))
        goto error;
J
Jim Fehlig 已提交
666

667 668 669
    if (!(libxl_driver->hostdevMgr = virHostdevManagerGetDefault()))
        goto error;

670
    if (!(cfg = libxlDriverConfigNew()))
671
        goto error;
J
Jim Fehlig 已提交
672

673 674
    libxl_driver->config = cfg;
    if (virFileMakePath(cfg->logDir) < 0) {
675 676
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("failed to create log dir '%s': %s"),
677
                       cfg->logDir,
678
                       virStrerror(errno, ebuf, sizeof(ebuf)));
J
Jim Fehlig 已提交
679 680
        goto error;
    }
681
    if (virFileMakePath(cfg->stateDir) < 0) {
682 683
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("failed to create state dir '%s': %s"),
684
                       cfg->stateDir,
685
                       virStrerror(errno, ebuf, sizeof(ebuf)));
J
Jim Fehlig 已提交
686 687
        goto error;
    }
688
    if (virFileMakePath(cfg->libDir) < 0) {
689 690
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("failed to create lib dir '%s': %s"),
691
                       cfg->libDir,
692
                       virStrerror(errno, ebuf, sizeof(ebuf)));
J
Jim Fehlig 已提交
693 694
        goto error;
    }
695
    if (virFileMakePath(cfg->saveDir) < 0) {
696 697
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("failed to create save dir '%s': %s"),
698
                       cfg->saveDir,
699
                       virStrerror(errno, ebuf, sizeof(ebuf)));
J
Jim Fehlig 已提交
700 701
        goto error;
    }
702 703 704 705 706 707 708
    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 已提交
709

710
    /* read the host sysinfo */
711
    libxl_driver->hostsysinfo = virSysinfoRead();
712

713
    libxl_driver->domainEventState = virObjectEventStateNew();
E
Eric Blake 已提交
714
    if (!libxl_driver->domainEventState)
715 716
        goto error;

717
    if ((cfg->caps = libxlMakeCapabilities(cfg->ctx)) == NULL) {
718 719
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("cannot create capabilities for libxenlight"));
J
Jim Fehlig 已提交
720 721 722
        goto error;
    }

723
    if (!(libxl_driver->xmlopt = virDomainXMLOptionNew(&libxlDomainDefParserConfig,
724 725
                                                       &libxlDomainXMLPrivateDataCallbacks,
                                                       NULL)))
726
        goto error;
J
Jim Fehlig 已提交
727 728

    /* Load running domains first. */
729
    if (virDomainObjListLoadAllConfigs(libxl_driver->domains,
730 731
                                       cfg->stateDir,
                                       cfg->autostartDir,
732
                                       1,
733
                                       cfg->caps,
734 735
                                       libxl_driver->xmlopt,
                                       1 << VIR_DOMAIN_VIRT_XEN,
736
                                       NULL, NULL) < 0)
J
Jim Fehlig 已提交
737 738 739 740 741
        goto error;

    libxlReconnectDomains(libxl_driver);

    /* Then inactive persistent configs */
742
    if (virDomainObjListLoadAllConfigs(libxl_driver->domains,
743 744
                                       cfg->configDir,
                                       cfg->autostartDir,
745
                                       0,
746
                                       cfg->caps,
747 748
                                       libxl_driver->xmlopt,
                                       1 << VIR_DOMAIN_VIRT_XEN,
749
                                       NULL, NULL) < 0)
J
Jim Fehlig 已提交
750 751
        goto error;

752 753
    virDomainObjListForEach(libxl_driver->domains, libxlDomainManagedSaveLoad,
                            libxl_driver);
754

J
Jim Fehlig 已提交
755 756 757
    return 0;

error:
758
    libxlStateCleanup();
759
    return -1;
J
Jim Fehlig 已提交
760 761
}

762 763 764 765 766 767 768 769 770 771
static void
libxlStateAutoStart(void)
{
    if (!libxl_driver)
        return;

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

J
Jim Fehlig 已提交
772
static int
773
libxlStateReload(void)
J
Jim Fehlig 已提交
774
{
775 776
    libxlDriverConfigPtr cfg;

J
Jim Fehlig 已提交
777 778 779
    if (!libxl_driver)
        return 0;

780 781
    cfg = libxlDriverConfigGet(libxl_driver);

782
    virDomainObjListLoadAllConfigs(libxl_driver->domains,
783 784
                                   cfg->configDir,
                                   cfg->autostartDir,
785
                                   1,
786
                                   cfg->caps,
787 788
                                   libxl_driver->xmlopt,
                                   1 << VIR_DOMAIN_VIRT_XEN,
789 790
                                   NULL, libxl_driver);

791 792
    virDomainObjListForEach(libxl_driver->domains, libxlAutostartDomain,
                            libxl_driver);
793

794
    virObjectUnref(cfg);
J
Jim Fehlig 已提交
795 796 797 798 799
    return 0;
}


static virDrvOpenStatus
800 801 802
libxlConnectOpen(virConnectPtr conn,
                 virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                 unsigned int flags)
J
Jim Fehlig 已提交
803
{
E
Eric Blake 已提交
804 805
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

J
Jim Fehlig 已提交
806 807 808 809
    if (conn->uri == NULL) {
        if (libxl_driver == NULL)
            return VIR_DRV_OPEN_DECLINED;

810
        if (!(conn->uri = virURIParse("xen:///")))
J
Jim Fehlig 已提交
811 812 813 814 815 816 817 818 819 820 821 822
            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) {
823 824
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("libxenlight state driver is not active"));
J
Jim Fehlig 已提交
825 826 827 828 829 830 831 832
            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")) {
833 834 835
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unexpected Xen URI path '%s', try xen:///"),
                           NULLSTR(conn->uri->path));
J
Jim Fehlig 已提交
836 837 838 839
            return VIR_DRV_OPEN_ERROR;
        }
    }

840 841 842
    if (virConnectOpenEnsureACL(conn) < 0)
        return VIR_DRV_OPEN_ERROR;

J
Jim Fehlig 已提交
843 844 845 846 847 848
    conn->privateData = libxl_driver;

    return VIR_DRV_OPEN_SUCCESS;
};

static int
849
libxlConnectClose(virConnectPtr conn ATTRIBUTE_UNUSED)
J
Jim Fehlig 已提交
850 851 852 853 854 855
{
    conn->privateData = NULL;
    return 0;
}

static const char *
856
libxlConnectGetType(virConnectPtr conn)
J
Jim Fehlig 已提交
857
{
858 859 860
    if (virConnectGetTypeEnsureACL(conn) < 0)
        return NULL;

J
Jim Fehlig 已提交
861
    return "Xen";
J
Jim Fehlig 已提交
862 863 864
}

static int
865
libxlConnectGetVersion(virConnectPtr conn, unsigned long *version)
J
Jim Fehlig 已提交
866 867
{
    libxlDriverPrivatePtr driver = conn->privateData;
868
    libxlDriverConfigPtr cfg;
J
Jim Fehlig 已提交
869

870 871 872
    if (virConnectGetVersionEnsureACL(conn) < 0)
        return 0;

873 874 875
    cfg = libxlDriverConfigGet(driver);
    *version = cfg->version;
    virObjectUnref(cfg);
J
Jim Fehlig 已提交
876 877 878
    return 0;
}

879

880
static char *libxlConnectGetHostname(virConnectPtr conn)
881
{
882 883 884
    if (virConnectGetHostnameEnsureACL(conn) < 0)
        return NULL;

885 886 887
    return virGetHostname();
}

888 889 890 891 892 893 894 895
static char *
libxlConnectGetSysinfo(virConnectPtr conn, unsigned int flags)
{
    libxlDriverPrivatePtr driver = conn->privateData;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    virCheckFlags(0, NULL);

896 897 898
    if (virConnectGetSysinfoEnsureACL(conn) < 0)
        return NULL;

899 900 901 902 903 904 905 906 907 908 909 910 911 912
    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;
    if (virBufferError(&buf)) {
        virReportOOMError();
        return NULL;
    }
    return virBufferContentAndReset(&buf);
}
913

J
Jim Fehlig 已提交
914
static int
915
libxlConnectGetMaxVcpus(virConnectPtr conn, const char *type ATTRIBUTE_UNUSED)
J
Jim Fehlig 已提交
916 917 918
{
    int ret;
    libxlDriverPrivatePtr driver = conn->privateData;
919
    libxlDriverConfigPtr cfg;
J
Jim Fehlig 已提交
920

921 922 923
    if (virConnectGetMaxVcpusEnsureACL(conn) < 0)
        return -1;

924 925
    cfg = libxlDriverConfigGet(driver);
    ret = libxl_get_max_cpus(cfg->ctx);
926 927 928 929 930
    /* 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)
931
        ret = -1;
J
Jim Fehlig 已提交
932

933
    virObjectUnref(cfg);
J
Jim Fehlig 已提交
934 935 936 937 938 939
    return ret;
}

static int
libxlNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info)
{
940 941 942
    if (virNodeGetInfoEnsureACL(conn) < 0)
        return -1;

943
    return libxlDriverNodeGetInfo(conn->privateData, info);
J
Jim Fehlig 已提交
944 945 946
}

static char *
947
libxlConnectGetCapabilities(virConnectPtr conn)
J
Jim Fehlig 已提交
948 949 950
{
    libxlDriverPrivatePtr driver = conn->privateData;
    char *xml;
951
    libxlDriverConfigPtr cfg;
J
Jim Fehlig 已提交
952

953 954 955
    if (virConnectGetCapabilitiesEnsureACL(conn) < 0)
        return NULL;

956 957
    cfg = libxlDriverConfigGet(driver);
    if ((xml = virCapabilitiesFormatXML(cfg->caps)) == NULL)
J
Jim Fehlig 已提交
958 959
        virReportOOMError();

960
    virObjectUnref(cfg);
J
Jim Fehlig 已提交
961 962 963 964
    return xml;
}

static int
965
libxlConnectListDomains(virConnectPtr conn, int *ids, int nids)
J
Jim Fehlig 已提交
966 967 968 969
{
    libxlDriverPrivatePtr driver = conn->privateData;
    int n;

970 971 972
    if (virConnectListDomainsEnsureACL(conn) < 0)
        return -1;

973 974
    n = virDomainObjListGetActiveIDs(driver->domains, ids, nids,
                                     virConnectListDomainsCheckACL, conn);
J
Jim Fehlig 已提交
975 976 977 978 979

    return n;
}

static int
980
libxlConnectNumOfDomains(virConnectPtr conn)
J
Jim Fehlig 已提交
981 982 983 984
{
    libxlDriverPrivatePtr driver = conn->privateData;
    int n;

985 986 987
    if (virConnectNumOfDomainsEnsureACL(conn) < 0)
        return -1;

988 989
    n = virDomainObjListNumOfDomains(driver->domains, true,
                                     virConnectNumOfDomainsCheckACL, conn);
J
Jim Fehlig 已提交
990 991 992 993 994 995 996 997 998 999 1000 1001

    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;
1002
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
J
Jim Fehlig 已提交
1003 1004 1005

    virCheckFlags(VIR_DOMAIN_START_PAUSED, NULL);

1006
    if (!(def = virDomainDefParseString(xml, cfg->caps, driver->xmlopt,
1007
                                        1 << VIR_DOMAIN_VIRT_XEN,
J
Jim Fehlig 已提交
1008 1009 1010
                                        VIR_DOMAIN_XML_INACTIVE)))
        goto cleanup;

1011 1012 1013
    if (virDomainCreateXMLEnsureACL(conn, def) < 0)
        goto cleanup;

1014
    if (!(vm = virDomainObjListAdd(driver->domains, def,
1015
                                   driver->xmlopt,
1016 1017
                                   VIR_DOMAIN_OBJ_LIST_ADD_CHECK_LIVE,
                                   NULL)))
J
Jim Fehlig 已提交
1018 1019 1020
        goto cleanup;
    def = NULL;

1021 1022
    if (libxlVmStart(driver, vm, (flags & VIR_DOMAIN_START_PAUSED) != 0,
                     -1) < 0) {
1023
        virDomainObjListRemove(driver->domains, vm);
J
Jim Fehlig 已提交
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
        vm = NULL;
        goto cleanup;
    }

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

cleanup:
    virDomainDefFree(def);
    if (vm)
1035
        virObjectUnlock(vm);
1036
    virObjectUnref(cfg);
J
Jim Fehlig 已提交
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
    return dom;
}

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

1047
    vm = virDomainObjListFindByID(driver->domains, id);
J
Jim Fehlig 已提交
1048
    if (!vm) {
1049
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
J
Jim Fehlig 已提交
1050 1051 1052
        goto cleanup;
    }

1053 1054 1055
    if (virDomainLookupByIDEnsureACL(conn, vm->def) < 0)
        goto cleanup;

J
Jim Fehlig 已提交
1056 1057 1058 1059 1060 1061
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom)
        dom->id = vm->def->id;

  cleanup:
    if (vm)
1062
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
    return dom;
}

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

1073
    vm = virDomainObjListFindByUUID(driver->domains, uuid);
J
Jim Fehlig 已提交
1074
    if (!vm) {
1075
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
J
Jim Fehlig 已提交
1076 1077 1078
        goto cleanup;
    }

1079 1080 1081
    if (virDomainLookupByUUIDEnsureACL(conn, vm->def) < 0)
        goto cleanup;

J
Jim Fehlig 已提交
1082 1083 1084 1085 1086 1087
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom)
        dom->id = vm->def->id;

  cleanup:
    if (vm)
1088
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
    return dom;
}

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

1099
    vm = virDomainObjListFindByName(driver->domains, name);
J
Jim Fehlig 已提交
1100
    if (!vm) {
1101
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
J
Jim Fehlig 已提交
1102 1103 1104
        goto cleanup;
    }

1105 1106 1107
    if (virDomainLookupByNameEnsureACL(conn, vm->def) < 0)
        goto cleanup;

J
Jim Fehlig 已提交
1108 1109 1110 1111 1112 1113
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom)
        dom->id = vm->def->id;

  cleanup:
    if (vm)
1114
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
1115 1116 1117
    return dom;
}

1118 1119 1120 1121
static int
libxlDomainSuspend(virDomainPtr dom)
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
1122
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
1123 1124
    virDomainObjPtr vm;
    libxlDomainObjPrivatePtr priv;
1125
    virObjectEventPtr event = NULL;
1126 1127
    int ret = -1;

J
Jim Fehlig 已提交
1128
    if (!(vm = libxlDomObjFromDomain(dom)))
1129
        goto cleanup;
1130 1131 1132 1133

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

1134 1135 1136
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

1137
    if (!virDomainObjIsActive(vm)) {
1138
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not running"));
1139
        goto endjob;
1140 1141 1142 1143
    }

    priv = vm->privateData;

J
Jiri Denemark 已提交
1144
    if (virDomainObjGetState(vm, NULL) != VIR_DOMAIN_PAUSED) {
J
Jim Fehlig 已提交
1145
        if (libxl_domain_pause(priv->ctx, dom->id) != 0) {
1146 1147 1148
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Failed to suspend domain '%d' with libxenlight"),
                           dom->id);
1149
            goto endjob;
1150 1151
        }

J
Jiri Denemark 已提交
1152
        virDomainObjSetState(vm, VIR_DOMAIN_PAUSED, VIR_DOMAIN_PAUSED_USER);
1153

1154
        event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_SUSPENDED,
1155 1156 1157
                                         VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
    }

1158
    if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm) < 0)
1159
        goto endjob;
1160 1161 1162

    ret = 0;

1163 1164 1165 1166
endjob:
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

1167 1168
cleanup:
    if (vm)
1169
        virObjectUnlock(vm);
1170
    if (event)
1171
        libxlDomainEventQueue(driver, event);
1172
    virObjectUnref(cfg);
1173 1174 1175 1176 1177 1178 1179 1180
    return ret;
}


static int
libxlDomainResume(virDomainPtr dom)
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
1181
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
1182 1183
    virDomainObjPtr vm;
    libxlDomainObjPrivatePtr priv;
1184
    virObjectEventPtr event = NULL;
1185 1186
    int ret = -1;

J
Jim Fehlig 已提交
1187
    if (!(vm = libxlDomObjFromDomain(dom)))
1188 1189
        goto cleanup;

1190 1191 1192
    if (virDomainResumeEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

1193 1194 1195
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

1196
    if (!virDomainObjIsActive(vm)) {
1197
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not running"));
1198
        goto endjob;
1199 1200 1201 1202
    }

    priv = vm->privateData;

J
Jiri Denemark 已提交
1203
    if (virDomainObjGetState(vm, NULL) == VIR_DOMAIN_PAUSED) {
J
Jim Fehlig 已提交
1204
        if (libxl_domain_unpause(priv->ctx, dom->id) != 0) {
1205 1206 1207
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Failed to resume domain '%d' with libxenlight"),
                           dom->id);
1208
            goto endjob;
1209 1210
        }

J
Jiri Denemark 已提交
1211 1212
        virDomainObjSetState(vm, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_UNPAUSED);
1213

1214
        event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_RESUMED,
1215 1216 1217
                                         VIR_DOMAIN_EVENT_RESUMED_UNPAUSED);
    }

1218
    if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm) < 0)
1219
        goto endjob;
1220 1221 1222

    ret = 0;

1223 1224 1225 1226
endjob:
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

1227 1228
cleanup:
    if (vm)
1229
        virObjectUnlock(vm);
1230
    if (event)
1231
        libxlDomainEventQueue(driver, event);
1232
    virObjectUnref(cfg);
1233 1234 1235
    return ret;
}

J
Jim Fehlig 已提交
1236
static int
1237
libxlDomainShutdownFlags(virDomainPtr dom, unsigned int flags)
J
Jim Fehlig 已提交
1238 1239 1240 1241 1242
{
    virDomainObjPtr vm;
    int ret = -1;
    libxlDomainObjPrivatePtr priv;

1243 1244
    virCheckFlags(0, -1);

J
Jim Fehlig 已提交
1245
    if (!(vm = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
1246 1247
        goto cleanup;

1248
    if (virDomainShutdownFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
1249 1250
        goto cleanup;

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

    priv = vm->privateData;
J
Jim Fehlig 已提交
1258
    if (libxl_domain_shutdown(priv->ctx, dom->id) != 0) {
1259 1260 1261
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to shutdown domain '%d' with libxenlight"),
                       dom->id);
J
Jim Fehlig 已提交
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
        goto cleanup;
    }

    /* vm is marked shutoff (or removed from domains list if not persistent)
     * in shutdown event handler.
     */
    ret = 0;

cleanup:
    if (vm)
1272
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
1273 1274 1275
    return ret;
}

1276 1277 1278 1279 1280 1281 1282
static int
libxlDomainShutdown(virDomainPtr dom)
{
    return libxlDomainShutdownFlags(dom, 0);
}


J
Jim Fehlig 已提交
1283
static int
E
Eric Blake 已提交
1284
libxlDomainReboot(virDomainPtr dom, unsigned int flags)
J
Jim Fehlig 已提交
1285 1286 1287 1288 1289
{
    virDomainObjPtr vm;
    int ret = -1;
    libxlDomainObjPrivatePtr priv;

E
Eric Blake 已提交
1290 1291
    virCheckFlags(0, -1);

J
Jim Fehlig 已提交
1292
    if (!(vm = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
1293 1294
        goto cleanup;

1295
    if (virDomainRebootEnsureACL(dom->conn, vm->def, flags) < 0)
1296 1297
        goto cleanup;

J
Jim Fehlig 已提交
1298
    if (!virDomainObjIsActive(vm)) {
1299 1300
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("Domain is not running"));
J
Jim Fehlig 已提交
1301 1302 1303 1304
        goto cleanup;
    }

    priv = vm->privateData;
J
Jim Fehlig 已提交
1305
    if (libxl_domain_reboot(priv->ctx, dom->id) != 0) {
1306 1307 1308
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to reboot domain '%d' with libxenlight"),
                       dom->id);
J
Jim Fehlig 已提交
1309 1310 1311 1312 1313 1314
        goto cleanup;
    }
    ret = 0;

cleanup:
    if (vm)
1315
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
1316 1317 1318 1319
    return ret;
}

static int
1320 1321
libxlDomainDestroyFlags(virDomainPtr dom,
                        unsigned int flags)
J
Jim Fehlig 已提交
1322 1323 1324 1325
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
1326
    virObjectEventPtr event = NULL;
J
Jim Fehlig 已提交
1327
    libxlDomainObjPrivatePtr priv;
J
Jim Fehlig 已提交
1328

1329 1330
    virCheckFlags(0, -1);

J
Jim Fehlig 已提交
1331
    if (!(vm = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
1332 1333
        goto cleanup;

1334 1335 1336
    if (virDomainDestroyFlagsEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

J
Jim Fehlig 已提交
1337
    if (!virDomainObjIsActive(vm)) {
1338 1339
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("Domain is not running"));
J
Jim Fehlig 已提交
1340 1341 1342
        goto cleanup;
    }

1343
    event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_STOPPED,
1344 1345
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);

J
Jim Fehlig 已提交
1346 1347
    priv = vm->privateData;
    if (libxl_domain_destroy(priv->ctx, vm->def->id, NULL) < 0) {
1348 1349
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to destroy domain '%d'"), dom->id);
J
Jim Fehlig 已提交
1350 1351 1352
        goto cleanup;
    }

1353
    if (libxlDomainCleanupJob(driver, vm, VIR_DOMAIN_SHUTOFF_DESTROYED)) {
1354 1355 1356 1357
        if (!vm->persistent) {
            virDomainObjListRemove(driver->domains, vm);
            vm = NULL;
        }
J
Jim Fehlig 已提交
1358 1359 1360 1361 1362 1363
    }

    ret = 0;

cleanup:
    if (vm)
1364
        virObjectUnlock(vm);
1365 1366
    if (event)
        libxlDomainEventQueue(driver, event);
J
Jim Fehlig 已提交
1367 1368 1369
    return ret;
}

1370 1371 1372 1373 1374 1375
static int
libxlDomainDestroy(virDomainPtr dom)
{
    return libxlDomainDestroyFlags(dom, 0);
}

1376 1377 1378 1379 1380 1381
static char *
libxlDomainGetOSType(virDomainPtr dom)
{
    virDomainObjPtr vm;
    char *type = NULL;

J
Jim Fehlig 已提交
1382
    if (!(vm = libxlDomObjFromDomain(dom)))
1383 1384
        goto cleanup;

1385 1386 1387 1388 1389
    if (virDomainGetOSTypeEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

    if (VIR_STRDUP(type, vm->def->os.type) < 0)
        goto cleanup;
1390 1391 1392

cleanup:
    if (vm)
1393
        virObjectUnlock(vm);
1394 1395 1396
    return type;
}

1397
static unsigned long long
1398 1399 1400
libxlDomainGetMaxMemory(virDomainPtr dom)
{
    virDomainObjPtr vm;
1401
    unsigned long long ret = 0;
1402

J
Jim Fehlig 已提交
1403
    if (!(vm = libxlDomObjFromDomain(dom)))
1404
        goto cleanup;
1405 1406 1407 1408

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

1409 1410 1411 1412
    ret = vm->def->mem.max_balloon;

cleanup:
    if (vm)
1413
        virObjectUnlock(vm);
1414 1415 1416 1417
    return ret;
}

static int
1418
libxlDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem,
1419 1420 1421
                          unsigned int flags)
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
1422
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
1423 1424
    libxlDomainObjPrivatePtr priv;
    virDomainObjPtr vm;
1425 1426
    virDomainDefPtr persistentDef = NULL;
    bool isActive;
1427 1428 1429
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_MEM_LIVE |
1430 1431
                  VIR_DOMAIN_MEM_CONFIG |
                  VIR_DOMAIN_MEM_MAXIMUM, -1);
1432

J
Jim Fehlig 已提交
1433
    if (!(vm = libxlDomObjFromDomain(dom)))
1434 1435
        goto cleanup;

1436 1437 1438
    if (virDomainSetMemoryFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
        goto cleanup;

1439 1440 1441
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
    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;
1455 1456
    }

1457
    if (!isActive && (flags & VIR_DOMAIN_MEM_LIVE)) {
1458 1459
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("cannot set memory on an inactive domain"));
1460
        goto endjob;
1461 1462 1463 1464
    }

    if (flags & VIR_DOMAIN_MEM_CONFIG) {
        if (!vm->persistent) {
1465 1466
            virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                           _("cannot change persistent config of a transient domain"));
1467
            goto endjob;
1468
        }
1469
        if (!(persistentDef = virDomainObjGetPersistentDef(cfg->caps,
1470
                                                           driver->xmlopt,
1471
                                                           vm)))
1472
            goto endjob;
1473 1474
    }

1475 1476
    if (flags & VIR_DOMAIN_MEM_MAXIMUM) {
        /* resize the maximum memory */
1477

1478 1479
        if (flags & VIR_DOMAIN_MEM_LIVE) {
            priv = vm->privateData;
J
Jim Fehlig 已提交
1480
            if (libxl_domain_setmaxmem(priv->ctx, dom->id, newmem) < 0) {
1481 1482 1483
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Failed to set maximum memory for domain '%d'"
                                 " with libxenlight"), dom->id);
1484
                goto endjob;
1485 1486 1487 1488 1489 1490 1491 1492 1493
            }
        }

        if (flags & VIR_DOMAIN_MEM_CONFIG) {
            /* Help clang 2.8 decipher the logic flow.  */
            sa_assert(persistentDef);
            persistentDef->mem.max_balloon = newmem;
            if (persistentDef->mem.cur_balloon > newmem)
                persistentDef->mem.cur_balloon = newmem;
1494
            ret = virDomainSaveConfig(cfg->configDir, persistentDef);
1495
            goto endjob;
1496 1497
        }

1498 1499
    } else {
        /* resize the current memory */
1500

1501
        if (newmem > vm->def->mem.max_balloon) {
1502 1503
            virReportError(VIR_ERR_INVALID_ARG, "%s",
                           _("cannot set memory higher than max memory"));
1504
            goto endjob;
1505 1506 1507
        }

        if (flags & VIR_DOMAIN_MEM_LIVE) {
1508 1509
            int res;

1510
            priv = vm->privateData;
1511 1512 1513 1514 1515 1516
            /* Unlock virDomainObj while ballooning memory */
            virObjectUnlock(vm);
            res = libxl_set_memory_target(priv->ctx, dom->id, newmem, 0,
                                          /* force */ 1);
            virObjectLock(vm);
            if (res < 0) {
1517 1518 1519
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Failed to set memory for domain '%d'"
                                 " with libxenlight"), dom->id);
1520
                goto endjob;
1521 1522 1523 1524 1525 1526
            }
        }

        if (flags & VIR_DOMAIN_MEM_CONFIG) {
            sa_assert(persistentDef);
            persistentDef->mem.cur_balloon = newmem;
1527
            ret = virDomainSaveConfig(cfg->configDir, persistentDef);
1528
            goto endjob;
1529
        }
1530 1531
    }

1532 1533
    ret = 0;

1534 1535 1536 1537
endjob:
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

1538 1539
cleanup:
    if (vm)
1540
        virObjectUnlock(vm);
1541
    virObjectUnref(cfg);
1542 1543 1544 1545 1546 1547 1548 1549 1550
    return ret;
}

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

1551 1552 1553 1554 1555 1556
static int
libxlDomainSetMaxMemory(virDomainPtr dom, unsigned long memory)
{
    return libxlDomainSetMemoryFlags(dom, memory, VIR_DOMAIN_MEM_MAXIMUM);
}

J
Jim Fehlig 已提交
1557 1558 1559 1560
static int
libxlDomainGetInfo(virDomainPtr dom, virDomainInfoPtr info)
{
    virDomainObjPtr vm;
1561
    libxl_dominfo d_info;
1562
    libxlDomainObjPrivatePtr priv;
J
Jim Fehlig 已提交
1563 1564
    int ret = -1;

J
Jim Fehlig 已提交
1565
    if (!(vm = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
1566 1567
        goto cleanup;

1568 1569 1570
    if (virDomainGetInfoEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

1571
    priv = vm->privateData;
1572 1573 1574
    if (!virDomainObjIsActive(vm)) {
        info->cpuTime = 0;
        info->memory = vm->def->mem.cur_balloon;
1575
        info->maxMem = vm->def->mem.max_balloon;
1576
    } else {
1577
        if (libxl_domain_info(priv->ctx, &d_info, dom->id) != 0) {
1578 1579
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("libxl_domain_info failed for domain '%d'"), dom->id);
1580 1581 1582 1583
            goto cleanup;
        }
        info->cpuTime = d_info.cpu_time;
        info->memory = d_info.current_memkb;
1584
        info->maxMem = d_info.max_memkb;
1585 1586
    }

J
Jiri Denemark 已提交
1587
    info->state = virDomainObjGetState(vm, NULL);
J
Jim Fehlig 已提交
1588 1589 1590 1591 1592
    info->nrVirtCpu = vm->def->vcpus;
    ret = 0;

  cleanup:
    if (vm)
1593
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
1594 1595 1596
    return ret;
}

1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
static int
libxlDomainGetState(virDomainPtr dom,
                    int *state,
                    int *reason,
                    unsigned int flags)
{
    virDomainObjPtr vm;
    int ret = -1;

    virCheckFlags(0, -1);

J
Jim Fehlig 已提交
1608
    if (!(vm = libxlDomObjFromDomain(dom)))
1609 1610
        goto cleanup;

1611 1612 1613
    if (virDomainGetStateEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

J
Jiri Denemark 已提交
1614
    *state = virDomainObjGetState(vm, reason);
1615 1616 1617 1618
    ret = 0;

  cleanup:
    if (vm)
1619
        virObjectUnlock(vm);
1620 1621 1622
    return ret;
}

1623 1624 1625
/*
 * virDomainObjPtr must be locked on invocation
 */
1626
static int
1627 1628
libxlDoDomainSave(libxlDriverPrivatePtr driver, virDomainObjPtr vm,
                  const char *to)
1629
{
1630
    libxlDomainObjPrivatePtr priv = vm->privateData;
1631
    libxlSavefileHeader hdr;
1632
    virObjectEventPtr event = NULL;
1633 1634
    char *xml = NULL;
    uint32_t xml_len;
1635
    int fd = -1;
1636 1637 1638
    int ret = -1;

    if (virDomainObjGetState(vm, NULL) == VIR_DOMAIN_PAUSED) {
1639 1640 1641
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Domain '%d' has to be running because libxenlight will"
                         " suspend it"), vm->def->id);
1642 1643 1644 1645
        goto cleanup;
    }

    if ((fd = virFileOpenAs(to, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR,
L
Laine Stump 已提交
1646
                            -1, -1, 0)) < 0) {
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
        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)) {
1662 1663
        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                       _("Failed to write save file header"));
1664 1665 1666 1667
        goto cleanup;
    }

    if (safewrite(fd, xml, xml_len) != xml_len) {
1668 1669
        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                       _("Failed to write xml description"));
1670 1671 1672
        goto cleanup;
    }

1673 1674 1675 1676 1677 1678
    /* Unlock virDomainObj while saving domain */
    virObjectUnlock(vm);
    ret = libxl_domain_suspend(priv->ctx, vm->def->id, fd, 0, NULL);
    virObjectLock(vm);

    if (ret != 0) {
1679 1680 1681
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to save domain '%d' with libxenlight"),
                       vm->def->id);
1682
        ret = -1;
1683 1684 1685
        goto cleanup;
    }

1686
    event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_STOPPED,
1687 1688
                                         VIR_DOMAIN_EVENT_STOPPED_SAVED);

J
Jim Fehlig 已提交
1689
    if (libxl_domain_destroy(priv->ctx, vm->def->id, NULL) < 0) {
1690 1691
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to destroy domain '%d'"), vm->def->id);
1692 1693 1694
        goto cleanup;
    }

1695
    libxlDomainCleanup(driver, vm, VIR_DOMAIN_SHUTOFF_SAVED);
1696
    vm->hasManagedSave = true;
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
    ret = 0;

cleanup:
    VIR_FREE(xml);
    if (VIR_CLOSE(fd) < 0)
        virReportSystemError(errno, "%s", _("cannot close file"));
    if (event)
        libxlDomainEventQueue(driver, event);
    return ret;
}

static int
1709 1710
libxlDomainSaveFlags(virDomainPtr dom, const char *to, const char *dxml,
                     unsigned int flags)
1711
{
1712 1713
    libxlDriverPrivatePtr driver = dom->conn->privateData;
    virDomainObjPtr vm;
1714
    int ret = -1;
1715
    bool remove_dom = false;
1716

1717 1718
    virCheckFlags(0, -1);
    if (dxml) {
1719 1720
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
1721 1722 1723
        return -1;
    }

J
Jim Fehlig 已提交
1724
    if (!(vm = libxlDomObjFromDomain(dom)))
1725 1726
        goto cleanup;

1727 1728 1729
    if (virDomainSaveFlagsEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

1730 1731 1732
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

1733
    if (!virDomainObjIsActive(vm)) {
1734
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not running"));
1735
        goto endjob;
1736 1737
    }

1738
    if (libxlDoDomainSave(driver, vm, to) < 0)
1739
        goto endjob;
1740

1741 1742
    if (!vm->persistent)
        remove_dom = true;
1743 1744

    ret = 0;
1745

1746 1747 1748 1749
endjob:
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

1750
cleanup:
1751 1752 1753 1754
    if (remove_dom && vm) {
        virDomainObjListRemove(driver->domains, vm);
        vm = NULL;
    }
1755
    if (vm)
1756
        virObjectUnlock(vm);
1757 1758
    return ret;
}
1759

1760
static int
1761 1762 1763 1764 1765 1766 1767 1768
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)
1769 1770
{
    libxlDriverPrivatePtr driver = conn->privateData;
1771
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
1772 1773 1774 1775 1776
    virDomainObjPtr vm = NULL;
    virDomainDefPtr def = NULL;
    libxlSavefileHeader hdr;
    int fd = -1;
    int ret = -1;
1777

1778
    virCheckFlags(VIR_DOMAIN_SAVE_PAUSED, -1);
1779
    if (dxml) {
1780 1781
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
1782 1783 1784
        return -1;
    }

1785
    fd = libxlDomainSaveImageOpen(driver, cfg, from, &def, &hdr);
1786
    if (fd < 0)
1787
        goto cleanup_unlock;
1788

1789
    if (virDomainRestoreFlagsEnsureACL(conn, def) < 0)
1790
        goto cleanup_unlock;
1791

1792
    if (!(vm = virDomainObjListAdd(driver->domains, def,
1793
                                   driver->xmlopt,
1794 1795 1796
                                   VIR_DOMAIN_OBJ_LIST_ADD_LIVE |
                                   VIR_DOMAIN_OBJ_LIST_ADD_CHECK_LIVE,
                                   NULL)))
1797
        goto cleanup_unlock;
1798 1799 1800

    def = NULL;

1801 1802
    ret = libxlVmStart(driver, vm, (flags & VIR_DOMAIN_SAVE_PAUSED) != 0, fd);
    if (ret < 0 && !vm->persistent) {
1803
        virDomainObjListRemove(driver->domains, vm);
1804 1805 1806 1807
        vm = NULL;
    }

cleanup:
1808 1809
    if (VIR_CLOSE(fd) < 0)
        virReportSystemError(errno, "%s", _("cannot close file"));
1810 1811
    virDomainDefFree(def);
    if (vm)
1812
        virObjectUnlock(vm);
1813
    virObjectUnref(cfg);
1814
    return ret;
1815 1816 1817 1818

cleanup_unlock:
    libxlDriverUnlock(driver);
    goto cleanup;
1819 1820
}

1821 1822 1823 1824 1825 1826
static int
libxlDomainRestore(virConnectPtr conn, const char *from)
{
    return libxlDomainRestoreFlags(conn, from, NULL, 0);
}

1827
static int
1828
libxlDomainCoreDump(virDomainPtr dom, const char *to, unsigned int flags)
1829 1830 1831 1832
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
    libxlDomainObjPrivatePtr priv;
    virDomainObjPtr vm;
1833
    virObjectEventPtr event = NULL;
1834
    bool remove_dom = false;
1835 1836 1837 1838 1839
    bool paused = false;
    int ret = -1;

    virCheckFlags(VIR_DUMP_LIVE | VIR_DUMP_CRASH, -1);

J
Jim Fehlig 已提交
1840
    if (!(vm = libxlDomObjFromDomain(dom)))
1841 1842
        goto cleanup;

1843 1844 1845
    if (virDomainCoreDumpEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

1846 1847 1848
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

1849
    if (!virDomainObjIsActive(vm)) {
1850
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not running"));
1851
        goto endjob;
1852 1853 1854 1855 1856 1857
    }

    priv = vm->privateData;

    if (!(flags & VIR_DUMP_LIVE) &&
        virDomainObjGetState(vm, NULL) == VIR_DOMAIN_RUNNING) {
J
Jim Fehlig 已提交
1858
        if (libxl_domain_pause(priv->ctx, dom->id) != 0) {
1859 1860 1861 1862
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Before dumping core, failed to suspend domain '%d'"
                             " with libxenlight"),
                           dom->id);
1863
            goto endjob;
1864 1865 1866 1867 1868
        }
        virDomainObjSetState(vm, VIR_DOMAIN_PAUSED, VIR_DOMAIN_PAUSED_DUMP);
        paused = true;
    }

1869 1870 1871 1872 1873
    /* Unlock virDomainObj while dumping core */
    virObjectUnlock(vm);
    ret = libxl_domain_core_dump(priv->ctx, dom->id, to, NULL);
    virObjectLock(vm);
    if (ret != 0) {
1874 1875 1876
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to dump core of domain '%d' with libxenlight"),
                       dom->id);
1877 1878
        ret = -1;
        goto unpause;
1879 1880 1881
    }

    if (flags & VIR_DUMP_CRASH) {
J
Jim Fehlig 已提交
1882
        if (libxl_domain_destroy(priv->ctx, dom->id, NULL) < 0) {
1883 1884
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Failed to destroy domain '%d'"), dom->id);
1885
            goto unpause;
1886 1887
        }

1888
        libxlDomainCleanup(driver, vm, VIR_DOMAIN_SHUTOFF_CRASHED);
1889
        event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_STOPPED,
1890
                                         VIR_DOMAIN_EVENT_STOPPED_CRASHED);
1891 1892
        if (!vm->persistent)
            remove_dom = true;
1893 1894 1895 1896
    }

    ret = 0;

1897 1898
unpause:
    if (virDomainObjIsActive(vm) && paused) {
J
Jim Fehlig 已提交
1899
        if (libxl_domain_unpause(priv->ctx, dom->id) != 0) {
1900 1901 1902
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("After dumping core, failed to resume domain '%d' with"
                             " libxenlight"), dom->id);
1903 1904 1905 1906 1907
        } else {
            virDomainObjSetState(vm, VIR_DOMAIN_RUNNING,
                                 VIR_DOMAIN_RUNNING_UNPAUSED);
        }
    }
1908 1909 1910 1911 1912

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

1913
cleanup:
1914 1915 1916 1917
    if (remove_dom && vm) {
        virDomainObjListRemove(driver->domains, vm);
        vm = NULL;
    }
1918
    if (vm)
1919
        virObjectUnlock(vm);
1920
    if (event)
1921 1922 1923 1924
        libxlDomainEventQueue(driver, event);
    return ret;
}

1925 1926 1927 1928 1929 1930 1931
static int
libxlDomainManagedSave(virDomainPtr dom, unsigned int flags)
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
    virDomainObjPtr vm = NULL;
    char *name = NULL;
    int ret = -1;
1932
    bool remove_dom = false;
1933 1934 1935

    virCheckFlags(0, -1);

J
Jim Fehlig 已提交
1936
    if (!(vm = libxlDomObjFromDomain(dom)))
1937 1938
        goto cleanup;

1939 1940 1941
    if (virDomainManagedSaveEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

1942 1943 1944
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

1945
    if (!virDomainObjIsActive(vm)) {
1946
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not running"));
1947
        goto endjob;
1948
    }
1949
    if (!vm->persistent) {
1950 1951
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("cannot do managed save for transient domain"));
1952
        goto endjob;
1953
    }
1954 1955 1956

    name = libxlDomainManagedSavePath(driver, vm);
    if (name == NULL)
1957
        goto endjob;
1958 1959 1960

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

1961
    if (libxlDoDomainSave(driver, vm, name) < 0)
1962
        goto endjob;
1963

1964 1965
    if (!vm->persistent)
        remove_dom = true;
1966 1967

    ret = 0;
1968

1969 1970 1971 1972
endjob:
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

1973
cleanup:
1974 1975 1976 1977
    if (remove_dom && vm) {
        virDomainObjListRemove(driver->domains, vm);
        vm = NULL;
    }
1978
    if (vm)
1979
        virObjectUnlock(vm);
1980 1981 1982 1983
    VIR_FREE(name);
    return ret;
}

1984 1985
static int
libxlDomainManagedSaveLoad(virDomainObjPtr vm,
1986 1987 1988 1989
                           void *opaque)
{
    libxlDriverPrivatePtr driver = opaque;
    char *name;
1990
    int ret = -1;
1991

1992
    virObjectLock(vm);
1993 1994 1995 1996 1997 1998

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

    vm->hasManagedSave = virFileExists(name);

1999
    ret = 0;
2000
cleanup:
2001
    virObjectUnlock(vm);
2002
    VIR_FREE(name);
2003
    return ret;
2004 2005
}

2006 2007 2008 2009 2010 2011 2012 2013
static int
libxlDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
{
    virDomainObjPtr vm = NULL;
    int ret = -1;

    virCheckFlags(0, -1);

J
Jim Fehlig 已提交
2014
    if (!(vm = libxlDomObjFromDomain(dom)))
2015 2016
        goto cleanup;

2017 2018 2019
    if (virDomainHasManagedSaveImageEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

2020
    ret = vm->hasManagedSave;
2021 2022 2023

cleanup:
    if (vm)
2024
        virObjectUnlock(vm);
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037
    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 已提交
2038
    if (!(vm = libxlDomObjFromDomain(dom)))
2039 2040
        goto cleanup;

2041 2042 2043
    if (virDomainManagedSaveRemoveEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

2044 2045 2046 2047 2048
    name = libxlDomainManagedSavePath(driver, vm);
    if (name == NULL)
        goto cleanup;

    ret = unlink(name);
2049
    vm->hasManagedSave = false;
2050 2051 2052 2053

cleanup:
    VIR_FREE(name);
    if (vm)
2054
        virObjectUnlock(vm);
2055 2056 2057
    return ret;
}

2058 2059 2060 2061 2062
static int
libxlDomainSetVcpusFlags(virDomainPtr dom, unsigned int nvcpus,
                         unsigned int flags)
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
2063
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
2064 2065 2066
    libxlDomainObjPrivatePtr priv;
    virDomainDefPtr def;
    virDomainObjPtr vm;
J
Jim Fehlig 已提交
2067
    libxl_bitmap map;
2068 2069
    uint8_t *bitmask = NULL;
    unsigned int maplen;
2070 2071
    size_t i;
    unsigned int pos;
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083
    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)) {
2084 2085
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid flag combination: (0x%x)"), flags);
2086 2087 2088 2089
        return -1;
    }

    if (!nvcpus) {
2090
        virReportError(VIR_ERR_INVALID_ARG, "%s", _("nvcpus is zero"));
2091 2092 2093
        return -1;
    }

J
Jim Fehlig 已提交
2094
    if (!(vm = libxlDomObjFromDomain(dom)))
2095 2096
        goto cleanup;

2097 2098 2099
    if (virDomainSetVcpusFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
        goto cleanup;

2100 2101 2102
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

2103
    if (!virDomainObjIsActive(vm) && (flags & VIR_DOMAIN_VCPU_LIVE)) {
2104 2105
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("cannot set vcpus on an inactive domain"));
2106
        goto endjob;
2107 2108 2109
    }

    if (!vm->persistent && (flags & VIR_DOMAIN_VCPU_CONFIG)) {
2110 2111
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("cannot change persistent config of a transient domain"));
2112
        goto endjob;
2113 2114
    }

2115
    if ((max = libxlConnectGetMaxVcpus(dom->conn, NULL)) < 0) {
2116 2117
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("could not determine max vcpus for the domain"));
2118
        goto endjob;
2119 2120 2121 2122 2123 2124 2125
    }

    if (!(flags & VIR_DOMAIN_VCPU_MAXIMUM) && vm->def->maxvcpus < max) {
        max = vm->def->maxvcpus;
    }

    if (nvcpus > max) {
2126 2127 2128
        virReportError(VIR_ERR_INVALID_ARG,
                       _("requested vcpus is greater than max allowable"
                         " vcpus for the domain: %d > %d"), nvcpus, max);
2129
        goto endjob;
2130 2131 2132 2133
    }

    priv = vm->privateData;

2134
    if (!(def = virDomainObjGetPersistentDef(cfg->caps, driver->xmlopt, vm)))
2135
        goto endjob;
2136

E
Eric Blake 已提交
2137
    maplen = VIR_CPU_MAPLEN(nvcpus);
2138
    if (VIR_ALLOC_N(bitmask, maplen) < 0)
2139
        goto endjob;
2140 2141

    for (i = 0; i < nvcpus; ++i) {
E
Eric Blake 已提交
2142
        pos = i / 8;
2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160
        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 已提交
2161
        if (libxl_set_vcpuonline(priv->ctx, dom->id, &map) != 0) {
2162 2163 2164
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Failed to set vcpus for domain '%d'"
                             " with libxenlight"), dom->id);
2165
            goto endjob;
2166 2167 2168 2169
        }
        break;

    case VIR_DOMAIN_VCPU_LIVE | VIR_DOMAIN_VCPU_CONFIG:
J
Jim Fehlig 已提交
2170
        if (libxl_set_vcpuonline(priv->ctx, dom->id, &map) != 0) {
2171 2172 2173
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Failed to set vcpus for domain '%d'"
                             " with libxenlight"), dom->id);
2174
            goto endjob;
2175 2176 2177 2178 2179 2180 2181 2182
        }
        def->vcpus = nvcpus;
        break;
    }

    ret = 0;

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

2185 2186 2187 2188
endjob:
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

2189 2190 2191
cleanup:
    VIR_FREE(bitmask);
     if (vm)
2192
        virObjectUnlock(vm);
2193
     virObjectUnref(cfg);
2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208
    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;
2209
    bool active;
2210 2211 2212 2213 2214

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

J
Jim Fehlig 已提交
2215
    if (!(vm = libxlDomObjFromDomain(dom)))
2216 2217
        goto cleanup;

2218
    if (virDomainGetVcpusFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
2219 2220
        goto cleanup;

2221 2222 2223 2224 2225 2226 2227 2228 2229
    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)) {
2230 2231
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid flag combination: (0x%x)"), flags);
2232 2233 2234
        return -1;
    }

2235
    if (flags & VIR_DOMAIN_VCPU_LIVE) {
2236
        if (!active) {
2237 2238
            virReportError(VIR_ERR_OPERATION_INVALID,
                           "%s", _("Domain is not running"));
2239 2240 2241 2242
            goto cleanup;
        }
        def = vm->def;
    } else {
2243
        if (!vm->persistent) {
2244 2245
            virReportError(VIR_ERR_OPERATION_INVALID,
                           "%s", _("domain is transient"));
2246 2247
            goto cleanup;
        }
2248 2249 2250 2251 2252 2253 2254
        def = vm->newDef ? vm->newDef : vm->def;
    }

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

cleanup:
    if (vm)
2255
        virObjectUnlock(vm);
2256 2257 2258 2259
    return ret;
}

static int
2260 2261 2262
libxlDomainPinVcpuFlags(virDomainPtr dom, unsigned int vcpu,
                        unsigned char *cpumap, int maplen,
                        unsigned int flags)
2263 2264
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
2265
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
2266
    virDomainDefPtr targetDef = NULL;
2267
    virBitmapPtr pcpumap = NULL;
2268 2269
    virDomainObjPtr vm;
    int ret = -1;
2270 2271 2272

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);
2273

J
Jim Fehlig 已提交
2274
    if (!(vm = libxlDomObjFromDomain(dom)))
2275 2276
        goto cleanup;

2277
    if (virDomainPinVcpuFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
2278 2279
        goto cleanup;

2280 2281 2282
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

2283
    if ((flags & VIR_DOMAIN_AFFECT_LIVE) && !virDomainObjIsActive(vm)) {
2284
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
2285
                       _("domain is inactive"));
2286
        goto endjob;
2287 2288
    }

2289 2290
    if (virDomainLiveConfigHelperMethod(cfg->caps, driver->xmlopt, vm,
                                        &flags, &targetDef) < 0)
2291
        goto endjob;
2292 2293 2294 2295 2296 2297 2298 2299

    if (flags & VIR_DOMAIN_AFFECT_LIVE) {
        targetDef = vm->def;
    }

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

2300 2301
    pcpumap = virBitmapNewData(cpumap, maplen);
    if (!pcpumap)
2302
        goto endjob;
2303

2304 2305 2306 2307 2308 2309 2310 2311 2312
    if (flags & VIR_DOMAIN_AFFECT_LIVE) {
        libxl_bitmap map = { .size = maplen, .map = cpumap };
        libxlDomainObjPrivatePtr priv;

        priv = vm->privateData;
        if (libxl_set_vcpuaffinity(priv->ctx, dom->id, vcpu, &map) != 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Failed to pin vcpu '%d' with libxenlight"),
                           vcpu);
2313
            goto endjob;
2314
        }
2315
    }
2316

2317 2318 2319 2320 2321 2322
    /* full bitmap means reset the settings (if any). */
    if (virBitmapIsAllSet(pcpumap)) {
        if (virDomainVcpuPinDel(targetDef, vcpu) < 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Failed to delete vcpupin xml for vcpu '%d'"),
                           vcpu);
2323
            goto endjob;
2324
        }
2325
        goto done;
2326 2327
    }

2328 2329
    if (!targetDef->cputune.vcpupin) {
        if (VIR_ALLOC(targetDef->cputune.vcpupin) < 0)
2330
            goto endjob;
2331
        targetDef->cputune.nvcpupin = 0;
H
Hu Tao 已提交
2332
    }
2333 2334
    if (virDomainVcpuPinAdd(&targetDef->cputune.vcpupin,
                            &targetDef->cputune.nvcpupin,
H
Hu Tao 已提交
2335 2336 2337
                            cpumap,
                            maplen,
                            vcpu) < 0) {
2338 2339
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("failed to update or add vcpupin xml"));
2340
        goto endjob;
2341 2342
    }

2343
done:
2344 2345
    ret = 0;

2346 2347 2348 2349 2350 2351
    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);
    }

2352 2353 2354 2355
endjob:
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

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

2364 2365 2366 2367 2368 2369 2370 2371
static int
libxlDomainPinVcpu(virDomainPtr dom, unsigned int vcpu, unsigned char *cpumap,
                   int maplen)
{
    return libxlDomainPinVcpuFlags(dom, vcpu, cpumap, maplen,
                                   VIR_DOMAIN_AFFECT_LIVE);
}

2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448
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;
    virDomainVcpuPinDefPtr *vcpupin_list;
    virBitmapPtr cpumask = NULL;
    int maxcpu, hostcpus, vcpu, pcpu, n, ret = -1;
    unsigned char *cpumap;
    bool pinned;

    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;

    if (flags & VIR_DOMAIN_AFFECT_LIVE) {
        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;

    /* we use cfg->ctx, as vm->privateData->ctx may be NULL if VM is down. */
    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;
        vcpu = vcpupin_list[n]->vcpuid;
        cpumask = vcpupin_list[n]->cpumask;
        cpumap = VIR_GET_CPUMAP(cpumaps, maplen, vcpu);
        for (pcpu = 0; pcpu < maxcpu; pcpu++) {
            if (virBitmapGetBit(cpumask, pcpu, &pinned) < 0)
                goto cleanup;
            if (!pinned)
                VIR_UNUSE_CPU(cpumap, pcpu);
        }
    }
    ret = ncpumaps;

cleanup:
    if (vm)
        virObjectUnlock(vm);
    virObjectUnref(cfg);
    return ret;
}
2449 2450 2451 2452 2453 2454 2455 2456 2457 2458

static int
libxlDomainGetVcpus(virDomainPtr dom, virVcpuInfoPtr info, int maxinfo,
                    unsigned char *cpumaps, int maplen)
{
    libxlDomainObjPrivatePtr priv;
    virDomainObjPtr vm;
    int ret = -1;
    libxl_vcpuinfo *vcpuinfo;
    int maxcpu, hostcpus;
2459
    size_t i;
2460 2461
    unsigned char *cpumap;

J
Jim Fehlig 已提交
2462
    if (!(vm = libxlDomObjFromDomain(dom)))
2463 2464
        goto cleanup;

2465 2466 2467
    if (virDomainGetVcpusEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

2468
    if (!virDomainObjIsActive(vm)) {
2469
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not running"));
2470 2471 2472 2473
        goto cleanup;
    }

    priv = vm->privateData;
J
Jim Fehlig 已提交
2474
    if ((vcpuinfo = libxl_list_vcpu(priv->ctx, dom->id, &maxcpu,
2475
                                    &hostcpus)) == NULL) {
2476 2477 2478
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to list vcpus for domain '%d' with libxenlight"),
                       dom->id);
2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500
        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 已提交
2501
        libxl_vcpuinfo_dispose(&vcpuinfo[i]);
2502 2503 2504 2505 2506 2507 2508
    }
    VIR_FREE(vcpuinfo);

    ret = maxinfo;

cleanup:
    if (vm)
2509
        virObjectUnlock(vm);
2510 2511 2512
    return ret;
}

J
Jim Fehlig 已提交
2513
static char *
2514
libxlDomainGetXMLDesc(virDomainPtr dom, unsigned int flags)
J
Jim Fehlig 已提交
2515 2516 2517 2518
{
    virDomainObjPtr vm;
    char *ret = NULL;

2519 2520
    /* Flags checked by virDomainDefFormat */

J
Jim Fehlig 已提交
2521
    if (!(vm = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
2522 2523
        goto cleanup;

2524 2525 2526
    if (virDomainGetXMLDescEnsureACL(dom->conn, vm->def, flags) < 0)
        goto cleanup;

J
Jim Fehlig 已提交
2527 2528 2529 2530
    ret = virDomainDefFormat(vm->def, flags);

  cleanup:
    if (vm)
2531
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
2532 2533 2534
    return ret;
}

2535
static char *
2536 2537 2538
libxlConnectDomainXMLFromNative(virConnectPtr conn,
                                const char *nativeFormat,
                                const char *nativeConfig,
2539
                                unsigned int flags)
2540 2541
{
    libxlDriverPrivatePtr driver = conn->privateData;
2542
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
2543 2544 2545 2546
    virDomainDefPtr def = NULL;
    virConfPtr conf = NULL;
    char *xml = NULL;

E
Eric Blake 已提交
2547 2548
    virCheckFlags(0, NULL);

2549 2550 2551
    if (virConnectDomainXMLFromNativeEnsureACL(conn) < 0)
        goto cleanup;

2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573
    if (STREQ(nativeFormat, LIBXL_CONFIG_FORMAT_XM)) {
        if (!(conf = virConfReadMem(nativeConfig, strlen(nativeConfig), 0)))
            goto cleanup;

        if (!(def = xenParseXM(conf,
                               cfg->verInfo->xen_version_major,
                               cfg->caps))) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("parsing xm config failed"));
            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 {
2574 2575
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unsupported config type %s"), nativeFormat);
2576 2577 2578 2579 2580 2581 2582 2583 2584
        goto cleanup;
    }

    xml = virDomainDefFormat(def, VIR_DOMAIN_XML_INACTIVE);

cleanup:
    virDomainDefFree(def);
    if (conf)
        virConfFree(conf);
2585
    virObjectUnref(cfg);
2586 2587 2588 2589 2590
    return xml;
}

#define MAX_CONFIG_SIZE (1024 * 65)
static char *
2591 2592 2593
libxlConnectDomainXMLToNative(virConnectPtr conn, const char * nativeFormat,
                              const char * domainXml,
                              unsigned int flags)
2594 2595
{
    libxlDriverPrivatePtr driver = conn->privateData;
2596
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
2597 2598 2599 2600 2601
    virDomainDefPtr def = NULL;
    virConfPtr conf = NULL;
    int len = MAX_CONFIG_SIZE;
    char *ret = NULL;

E
Eric Blake 已提交
2602 2603
    virCheckFlags(0, NULL);

2604 2605 2606
    if (virConnectDomainXMLToNativeEnsureACL(conn) < 0)
        goto cleanup;

2607
    if (STRNEQ(nativeFormat, LIBXL_CONFIG_FORMAT_XM)) {
2608 2609
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unsupported config type %s"), nativeFormat);
2610 2611 2612
        goto cleanup;
    }

2613
    if (!(def = virDomainDefParseString(domainXml,
2614
                                        cfg->caps, driver->xmlopt,
2615 2616
                                        1 << VIR_DOMAIN_VIRT_XEN,
                                        VIR_DOMAIN_XML_INACTIVE)))
2617 2618
        goto cleanup;

2619
    if (!(conf = xenFormatXM(conn, def, cfg->verInfo->xen_version_major)))
2620 2621
        goto cleanup;

2622
    if (VIR_ALLOC_N(ret, len) < 0)
2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633
        goto cleanup;

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

cleanup:
    virDomainDefFree(def);
    if (conf)
        virConfFree(conf);
2634
    virObjectUnref(cfg);
2635 2636 2637
    return ret;
}

J
Jim Fehlig 已提交
2638
static int
2639 2640
libxlConnectListDefinedDomains(virConnectPtr conn,
                               char **const names, int nnames)
J
Jim Fehlig 已提交
2641 2642 2643 2644
{
    libxlDriverPrivatePtr driver = conn->privateData;
    int n;

2645 2646 2647
    if (virConnectListDefinedDomainsEnsureACL(conn) < 0)
        return -1;

2648 2649
    n = virDomainObjListGetInactiveNames(driver->domains, names, nnames,
                                         virConnectListDefinedDomainsCheckACL, conn);
J
Jim Fehlig 已提交
2650 2651 2652 2653
    return n;
}

static int
2654
libxlConnectNumOfDefinedDomains(virConnectPtr conn)
J
Jim Fehlig 已提交
2655 2656 2657 2658
{
    libxlDriverPrivatePtr driver = conn->privateData;
    int n;

2659 2660 2661
    if (virConnectNumOfDefinedDomainsEnsureACL(conn) < 0)
        return -1;

2662
    n = virDomainObjListNumOfDomains(driver->domains, false,
2663 2664
                                     virConnectNumOfDefinedDomainsCheckACL,
                                     conn);
J
Jim Fehlig 已提交
2665 2666 2667 2668 2669
    return n;
}

static int
libxlDomainCreateWithFlags(virDomainPtr dom,
E
Eric Blake 已提交
2670
                           unsigned int flags)
J
Jim Fehlig 已提交
2671 2672 2673 2674 2675 2676 2677
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_START_PAUSED, -1);

J
Jim Fehlig 已提交
2678
    if (!(vm = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
2679 2680
        goto cleanup;

2681 2682 2683
    if (virDomainCreateWithFlagsEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

J
Jim Fehlig 已提交
2684
    if (virDomainObjIsActive(vm)) {
2685 2686
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("Domain is already running"));
J
Jim Fehlig 已提交
2687 2688 2689
        goto cleanup;
    }

2690
    ret = libxlVmStart(driver, vm, (flags & VIR_DOMAIN_START_PAUSED) != 0, -1);
J
Jim Fehlig 已提交
2691 2692 2693

cleanup:
    if (vm)
2694
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707
    return ret;
}

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

static virDomainPtr
libxlDomainDefineXML(virConnectPtr conn, const char *xml)
{
    libxlDriverPrivatePtr driver = conn->privateData;
2708
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
J
Jim Fehlig 已提交
2709 2710 2711
    virDomainDefPtr def = NULL;
    virDomainObjPtr vm = NULL;
    virDomainPtr dom = NULL;
2712
    virObjectEventPtr event = NULL;
2713
    virDomainDefPtr oldDef = NULL;
J
Jim Fehlig 已提交
2714

2715
    /* Lock the driver until the virDomainObj is created and locked */
J
Jim Fehlig 已提交
2716
    libxlDriverLock(driver);
2717
    if (!(def = virDomainDefParseString(xml, cfg->caps, driver->xmlopt,
M
Matthias Bolte 已提交
2718
                                        1 << VIR_DOMAIN_VIRT_XEN,
J
Jim Fehlig 已提交
2719
                                        VIR_DOMAIN_XML_INACTIVE)))
2720
        goto cleanup_unlock;
J
Jim Fehlig 已提交
2721

2722
    if (virDomainDefineXMLEnsureACL(conn, def) < 0)
2723
        goto cleanup_unlock;
2724

2725
    if (!(vm = virDomainObjListAdd(driver->domains, def,
2726
                                   driver->xmlopt,
2727 2728
                                   0,
                                   &oldDef)))
2729 2730
        goto cleanup_unlock;

J
Jim Fehlig 已提交
2731 2732
    def = NULL;
    vm->persistent = 1;
2733
    libxlDriverUnlock(driver);
J
Jim Fehlig 已提交
2734

2735
    if (virDomainSaveConfig(cfg->configDir,
J
Jim Fehlig 已提交
2736
                            vm->newDef ? vm->newDef : vm->def) < 0) {
2737
        virDomainObjListRemove(driver->domains, vm);
J
Jim Fehlig 已提交
2738 2739 2740 2741 2742 2743 2744 2745
        vm = NULL;
        goto cleanup;
    }

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

2746
    event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_DEFINED,
2747
                                     !oldDef ?
2748 2749 2750
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED :
                                     VIR_DOMAIN_EVENT_DEFINED_UPDATED);

J
Jim Fehlig 已提交
2751 2752
cleanup:
    virDomainDefFree(def);
2753
    virDomainDefFree(oldDef);
J
Jim Fehlig 已提交
2754
    if (vm)
2755
        virObjectUnlock(vm);
2756 2757
    if (event)
        libxlDomainEventQueue(driver, event);
2758
    virObjectUnref(cfg);
J
Jim Fehlig 已提交
2759
    return dom;
2760 2761 2762 2763

cleanup_unlock:
    libxlDriverUnlock(driver);
    goto cleanup;
J
Jim Fehlig 已提交
2764 2765 2766
}

static int
2767 2768
libxlDomainUndefineFlags(virDomainPtr dom,
                         unsigned int flags)
J
Jim Fehlig 已提交
2769 2770
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
2771
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
J
Jim Fehlig 已提交
2772
    virDomainObjPtr vm;
2773
    virObjectEventPtr event = NULL;
2774
    char *name = NULL;
J
Jim Fehlig 已提交
2775 2776
    int ret = -1;

2777 2778
    virCheckFlags(VIR_DOMAIN_UNDEFINE_MANAGED_SAVE, -1);

J
Jim Fehlig 已提交
2779
    if (!(vm = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
2780 2781
        goto cleanup;

2782 2783 2784
    if (virDomainUndefineFlagsEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

J
Jim Fehlig 已提交
2785
    if (!vm->persistent) {
2786 2787
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("cannot undefine transient domain"));
J
Jim Fehlig 已提交
2788 2789 2790
        goto cleanup;
    }

2791 2792 2793 2794 2795 2796 2797
    name = libxlDomainManagedSavePath(driver, vm);
    if (name == NULL)
        goto cleanup;

    if (virFileExists(name)) {
        if (flags & VIR_DOMAIN_UNDEFINE_MANAGED_SAVE) {
            if (unlink(name) < 0) {
2798 2799
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("Failed to remove domain managed save image"));
2800 2801 2802
                goto cleanup;
            }
        } else {
2803 2804 2805
            virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                           _("Refusing to undefine while domain managed "
                             "save image exists"));
2806 2807 2808 2809
            goto cleanup;
        }
    }

2810
    if (virDomainDeleteConfig(cfg->configDir, cfg->autostartDir, vm) < 0)
J
Jim Fehlig 已提交
2811 2812
        goto cleanup;

2813
    event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_UNDEFINED,
2814 2815
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);

2816 2817 2818
    if (virDomainObjIsActive(vm)) {
        vm->persistent = 0;
    } else {
2819
        virDomainObjListRemove(driver->domains, vm);
2820 2821 2822
        vm = NULL;
    }

J
Jim Fehlig 已提交
2823 2824 2825
    ret = 0;

  cleanup:
2826
    VIR_FREE(name);
J
Jim Fehlig 已提交
2827
    if (vm)
2828
        virObjectUnlock(vm);
2829 2830
    if (event)
        libxlDomainEventQueue(driver, event);
2831
    virObjectUnref(cfg);
J
Jim Fehlig 已提交
2832 2833 2834
    return ret;
}

2835 2836 2837 2838 2839 2840
static int
libxlDomainUndefine(virDomainPtr dom)
{
    return libxlDomainUndefineFlags(dom, 0);
}

2841 2842 2843 2844 2845 2846
static int
libxlDomainChangeEjectableMedia(libxlDomainObjPrivatePtr priv,
                                virDomainObjPtr vm, virDomainDiskDefPtr disk)
{
    virDomainDiskDefPtr origdisk = NULL;
    libxl_device_disk x_disk;
2847
    size_t i;
2848 2849
    int ret = -1;

2850
    for (i = 0; i < vm->def->ndisks; i++) {
2851 2852 2853 2854 2855 2856 2857 2858
        if (vm->def->disks[i]->bus == disk->bus &&
            STREQ(vm->def->disks[i]->dst, disk->dst)) {
            origdisk = vm->def->disks[i];
            break;
        }
    }

    if (!origdisk) {
2859 2860 2861
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("No device with bus '%s' and target '%s'"),
                       virDomainDiskBusTypeToString(disk->bus), disk->dst);
2862 2863 2864 2865
        goto cleanup;
    }

    if (origdisk->device != VIR_DOMAIN_DISK_DEVICE_CDROM) {
2866 2867 2868
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Removable media not supported for %s device"),
                       virDomainDiskDeviceTypeToString(disk->device));
2869 2870 2871
        return -1;
    }

J
Jim Fehlig 已提交
2872
    if (libxlMakeDisk(disk, &x_disk) < 0)
2873 2874
        goto cleanup;

J
Jim Fehlig 已提交
2875
    if ((ret = libxl_cdrom_insert(priv->ctx, vm->def->id, &x_disk, NULL)) < 0) {
2876 2877 2878
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("libxenlight failed to change media for disk '%s'"),
                       disk->dst);
2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909
        goto cleanup;
    }

    VIR_FREE(origdisk->src);
    origdisk->src = disk->src;
    disk->src = NULL;
    origdisk->type = disk->type;


    virDomainDiskDefFree(disk);

    ret = 0;

cleanup:
    return ret;
}

static int
libxlDomainAttachDeviceDiskLive(libxlDomainObjPrivatePtr priv,
                                virDomainObjPtr vm, virDomainDeviceDefPtr dev)
{
    virDomainDiskDefPtr l_disk = dev->data.disk;
    libxl_device_disk x_disk;
    int ret = -1;

    switch (l_disk->device)  {
        case VIR_DOMAIN_DISK_DEVICE_CDROM:
            ret = libxlDomainChangeEjectableMedia(priv, vm, l_disk);
            break;
        case VIR_DOMAIN_DISK_DEVICE_DISK:
            if (l_disk->bus == VIR_DOMAIN_DISK_BUS_XEN) {
2910
                if (virDomainDiskIndexByName(vm->def, l_disk->dst, true) >= 0) {
2911 2912
                    virReportError(VIR_ERR_OPERATION_FAILED,
                                   _("target %s already exists"), l_disk->dst);
2913 2914 2915 2916
                    goto cleanup;
                }

                if (!l_disk->src) {
2917 2918
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   "%s", _("disk source path is missing"));
2919 2920 2921
                    goto cleanup;
                }

2922
                if (VIR_REALLOC_N(vm->def->disks, vm->def->ndisks+1) < 0)
2923 2924
                    goto cleanup;

J
Jim Fehlig 已提交
2925
                if (libxlMakeDisk(l_disk, &x_disk) < 0)
2926 2927
                    goto cleanup;

J
Jim Fehlig 已提交
2928 2929
                if ((ret = libxl_device_disk_add(priv->ctx, vm->def->id,
                                                &x_disk, NULL)) < 0) {
2930 2931 2932
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("libxenlight failed to attach disk '%s'"),
                                   l_disk->dst);
2933 2934 2935 2936 2937 2938
                    goto cleanup;
                }

                virDomainDiskInsertPreAlloced(vm->def, l_disk);

            } else {
2939 2940 2941
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                               _("disk bus '%s' cannot be hotplugged."),
                               virDomainDiskBusTypeToString(l_disk->bus));
2942 2943 2944
            }
            break;
        default:
2945 2946 2947
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("disk device type '%s' cannot be hotplugged"),
                           virDomainDiskDeviceTypeToString(l_disk->device));
2948 2949 2950 2951 2952 2953 2954
            break;
    }

cleanup:
    return ret;
}

2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043
static int
libxlDomainAttachHostPCIDevice(libxlDriverPrivatePtr driver,
                               libxlDomainObjPrivatePtr priv,
                               virDomainObjPtr vm,
                               virDomainHostdevDefPtr hostdev)
{
    libxl_device_pci pcidev;
    virDomainHostdevDefPtr found;
    virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;

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

    if (virDomainHostdevFind(vm->def, hostdev, &found) >= 0) {
        virReportError(VIR_ERR_OPERATION_FAILED,
                       _("target pci device %.4x:%.2x:%.2x.%.1x already exists"),
                       hostdev->source.subsys.u.pci.addr.domain,
                       hostdev->source.subsys.u.pci.addr.bus,
                       hostdev->source.subsys.u.pci.addr.slot,
                       hostdev->source.subsys.u.pci.addr.function);
        return -1;
    }

    if (VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs + 1) < 0)
        return -1;

    if (virHostdevPreparePCIDevices(hostdev_mgr, LIBXL_DRIVER_NAME,
                                    vm->def->name, vm->def->uuid,
                                    &hostdev, 1, 0) < 0)
        goto cleanup;

    if (libxlMakePci(hostdev, &pcidev) < 0)
        goto reattach_hostdev;

    if (libxl_device_pci_add(priv->ctx, vm->def->id, &pcidev, 0) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("libxenlight failed to attach pci device %.4x:%.2x:%.2x.%.1x"),
                       hostdev->source.subsys.u.pci.addr.domain,
                       hostdev->source.subsys.u.pci.addr.bus,
                       hostdev->source.subsys.u.pci.addr.slot,
                       hostdev->source.subsys.u.pci.addr.function);
        goto reattach_hostdev;
    }

    vm->def->hostdevs[vm->def->nhostdevs++] = hostdev;
    return 0;

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

cleanup:
    return -1;
}

static int
libxlDomainAttachHostDevice(libxlDriverPrivatePtr driver,
                            libxlDomainObjPrivatePtr priv,
                            virDomainObjPtr vm,
                            virDomainDeviceDefPtr dev)
{
    virDomainHostdevDefPtr hostdev = dev->data.hostdev;

    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:
        if (libxlDomainAttachHostPCIDevice(driver, priv, vm, hostdev) < 0)
            goto error;
        break;

    default:
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("hostdev subsys type '%s' not supported"),
                       virDomainHostdevSubsysTypeToString(hostdev->source.subsys.type));
        goto error;
    }

    return 0;

error:
    return -1;
}

3044 3045 3046 3047 3048 3049
static int
libxlDomainDetachDeviceDiskLive(libxlDomainObjPrivatePtr priv,
                                virDomainObjPtr vm, virDomainDeviceDefPtr dev)
{
    virDomainDiskDefPtr l_disk = NULL;
    libxl_device_disk x_disk;
3050
    int idx;
3051 3052 3053 3054 3055 3056
    int ret = -1;

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

3057 3058 3059
                if ((idx = virDomainDiskIndexByName(vm->def,
                                                    dev->data.disk->dst,
                                                    false)) < 0) {
3060 3061
                    virReportError(VIR_ERR_OPERATION_FAILED,
                                   _("disk %s not found"), dev->data.disk->dst);
3062 3063 3064
                    goto cleanup;
                }

3065
                l_disk = vm->def->disks[idx];
3066

J
Jim Fehlig 已提交
3067
                if (libxlMakeDisk(l_disk, &x_disk) < 0)
3068 3069
                    goto cleanup;

J
Jim Fehlig 已提交
3070 3071
                if ((ret = libxl_device_disk_remove(priv->ctx, vm->def->id,
                                                    &x_disk, NULL)) < 0) {
3072 3073 3074
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("libxenlight failed to detach disk '%s'"),
                                   l_disk->dst);
3075 3076 3077
                    goto cleanup;
                }

3078
                virDomainDiskRemove(vm->def, idx);
3079 3080 3081
                virDomainDiskDefFree(l_disk);

            } else {
3082 3083 3084
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                               _("disk bus '%s' cannot be hot unplugged."),
                               virDomainDiskBusTypeToString(dev->data.disk->bus));
3085 3086 3087
            }
            break;
        default:
3088 3089 3090
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("device type '%s' cannot hot unplugged"),
                           virDomainDiskDeviceTypeToString(dev->data.disk->device));
3091 3092 3093 3094 3095 3096 3097 3098
            break;
    }

cleanup:
    return ret;
}

static int
3099 3100 3101
libxlDomainAttachDeviceLive(libxlDriverPrivatePtr driver,
                            libxlDomainObjPrivatePtr priv,
                            virDomainObjPtr vm,
3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112
                            virDomainDeviceDefPtr dev)
{
    int ret = -1;

    switch (dev->type) {
        case VIR_DOMAIN_DEVICE_DISK:
            ret = libxlDomainAttachDeviceDiskLive(priv, vm, dev);
            if (!ret)
                dev->data.disk = NULL;
            break;

3113 3114 3115 3116 3117 3118
        case VIR_DOMAIN_DEVICE_HOSTDEV:
            ret = libxlDomainAttachHostDevice(driver, priv, vm, dev);
            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 3134
    virDomainHostdevDefPtr hostdev;
    virDomainHostdevDefPtr found;
3135 3136 3137 3138

    switch (dev->type) {
        case VIR_DOMAIN_DEVICE_DISK:
            disk = dev->data.disk;
3139
            if (virDomainDiskIndexByName(vmdef, disk->dst, true) >= 0) {
3140 3141
                virReportError(VIR_ERR_INVALID_ARG,
                               _("target %s already exists."), disk->dst);
3142 3143
                return -1;
            }
3144
            if (virDomainDiskInsert(vmdef, disk))
3145 3146 3147 3148
                return -1;
            /* vmdef has the pointer. Generic codes for vmdef will do all jobs */
            dev->data.disk = NULL;
            break;
3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167
        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) {
                virReportError(VIR_ERR_OPERATION_FAILED,
                               _("target pci device %.4x:%.2x:%.2x.%.1x\
                                  already exists"),
                               hostdev->source.subsys.u.pci.addr.domain,
                               hostdev->source.subsys.u.pci.addr.bus,
                               hostdev->source.subsys.u.pci.addr.slot,
                               hostdev->source.subsys.u.pci.addr.function);
                return -1;
            }

            virDomainHostdevInsert(vmdef, hostdev);
            break;
3168 3169

        default:
3170 3171
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("persistent attach of device is not supported"));
3172 3173 3174 3175 3176 3177
            return -1;
    }
    return 0;
}

static int
3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 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 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299
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,
                               libxlDomainObjPrivatePtr priv,
                               virDomainObjPtr vm,
                               virDomainHostdevDefPtr hostdev)
{
    virDomainHostdevSubsysPtr subsys = &hostdev->source.subsys;
    libxl_device_pci pcidev;
    virDomainHostdevDefPtr detach;
    int idx;
    virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;

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

    idx = virDomainHostdevFind(vm->def, hostdev, &detach);
    if (idx < 0) {
        virReportError(VIR_ERR_OPERATION_FAILED,
                       _("host pci device %.4x:%.2x:%.2x.%.1x not found"),
                       subsys->u.pci.addr.domain, subsys->u.pci.addr.bus,
                       subsys->u.pci.addr.slot, subsys->u.pci.addr.function);
        return -1;
    }

    if (libxlIsMultiFunctionDevice(vm->def, detach->info)) {
        virReportError(VIR_ERR_OPERATION_FAILED,
                       _("cannot hot unplug multifunction PCI device: %.4x:%.2x:%.2x.%.1x"),
                       subsys->u.pci.addr.domain, subsys->u.pci.addr.bus,
                       subsys->u.pci.addr.slot, subsys->u.pci.addr.function);
        goto cleanup;
    }


    libxl_device_pci_init(&pcidev);

    if (libxlMakePci(detach, &pcidev) < 0)
        goto cleanup;

    if (libxl_device_pci_remove(priv->ctx, vm->def->id, &pcidev, 0) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("libxenlight failed to detach pci device\
                          %.4x:%.2x:%.2x.%.1x"),
                       subsys->u.pci.addr.domain, subsys->u.pci.addr.bus,
                       subsys->u.pci.addr.slot, subsys->u.pci.addr.function);
        goto cleanup;
    }

    libxl_device_pci_dispose(&pcidev);

    virDomainHostdevRemove(vm->def, idx);

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

    return 0;

cleanup:
    virDomainHostdevDefFree(detach);
    return -1;
}

static int
libxlDomainDetachHostDevice(libxlDriverPrivatePtr driver,
                            libxlDomainObjPrivatePtr priv,
                            virDomainObjPtr vm,
                            virDomainDeviceDefPtr dev)
{
    virDomainHostdevDefPtr hostdev = dev->data.hostdev;
    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:
            return libxlDomainDetachHostPCIDevice(driver, priv, vm, hostdev);

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

    return -1;
}

static int
libxlDomainDetachDeviceLive(libxlDriverPrivatePtr driver,
                            libxlDomainObjPrivatePtr priv,
                            virDomainObjPtr vm,
3300 3301 3302 3303 3304 3305 3306 3307 3308
                            virDomainDeviceDefPtr dev)
{
    int ret = -1;

    switch (dev->type) {
        case VIR_DOMAIN_DEVICE_DISK:
            ret = libxlDomainDetachDeviceDiskLive(priv, vm, dev);
            break;

3309 3310 3311 3312
        case VIR_DOMAIN_DEVICE_HOSTDEV:
            ret = libxlDomainDetachHostDevice(driver, priv, vm, dev);
            break;

3313
        default:
3314 3315 3316
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("device type '%s' cannot be detached"),
                           virDomainDeviceTypeToString(dev->type));
3317 3318 3319 3320 3321 3322
            break;
    }

    return ret;
}

3323

3324 3325 3326
static int
libxlDomainDetachDeviceConfig(virDomainDefPtr vmdef, virDomainDeviceDefPtr dev)
{
3327
    virDomainDiskDefPtr disk, detach;
3328 3329 3330 3331 3332
    int ret = -1;

    switch (dev->type) {
        case VIR_DOMAIN_DEVICE_DISK:
            disk = dev->data.disk;
3333
            if (!(detach = virDomainDiskRemoveByName(vmdef, disk->dst))) {
3334 3335
                virReportError(VIR_ERR_INVALID_ARG,
                               _("no target device %s"), disk->dst);
3336 3337
                break;
            }
3338
            virDomainDiskDefFree(detach);
3339 3340 3341
            ret = 0;
            break;
        default:
3342 3343
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("persistent detach of device is not supported"));
3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366
            break;
    }

    return ret;
}

static int
libxlDomainUpdateDeviceLive(libxlDomainObjPrivatePtr priv,
                            virDomainObjPtr vm, virDomainDeviceDefPtr dev)
{
    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:
                    ret = libxlDomainChangeEjectableMedia(priv, vm, disk);
                    if (ret == 0)
                        dev->data.disk = NULL;
                    break;
                default:
3367 3368 3369
                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                   _("disk bus '%s' cannot be updated."),
                                   virDomainDiskBusTypeToString(disk->bus));
3370 3371 3372 3373
                    break;
            }
            break;
        default:
3374 3375 3376
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("device type '%s' cannot be updated"),
                           virDomainDeviceTypeToString(dev->type));
3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387
            break;
    }

    return ret;
}

static int
libxlDomainUpdateDeviceConfig(virDomainDefPtr vmdef, virDomainDeviceDefPtr dev)
{
    virDomainDiskDefPtr orig;
    virDomainDiskDefPtr disk;
3388
    int idx;
3389 3390 3391 3392 3393
    int ret = -1;

    switch (dev->type) {
        case VIR_DOMAIN_DEVICE_DISK:
            disk = dev->data.disk;
3394
            if ((idx = virDomainDiskIndexByName(vmdef, disk->dst, false)) < 0) {
3395 3396
                virReportError(VIR_ERR_INVALID_ARG,
                               _("target %s doesn't exist."), disk->dst);
3397 3398
                goto cleanup;
            }
3399
            orig = vmdef->disks[idx];
3400
            if (!(orig->device == VIR_DOMAIN_DISK_DEVICE_CDROM)) {
3401 3402
                virReportError(VIR_ERR_INVALID_ARG, "%s",
                               _("this disk doesn't support update"));
3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413
                goto cleanup;
            }

            VIR_FREE(orig->src);
            orig->src = disk->src;
            orig->type = disk->type;
            if (disk->driverName) {
                VIR_FREE(orig->driverName);
                orig->driverName = disk->driverName;
                disk->driverName = NULL;
            }
3414
            orig->format = disk->format;
3415 3416 3417
            disk->src = NULL;
            break;
        default:
3418 3419
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("persistent update of device is not supported"));
3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430
            goto cleanup;
    }

    ret = 0;

cleanup:
    return ret;
}


static int
3431 3432
libxlDomainAttachDeviceFlags(virDomainPtr dom, const char *xml,
                             unsigned int flags)
3433 3434
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
3435
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
3436 3437 3438 3439 3440 3441 3442 3443 3444
    virDomainObjPtr vm = NULL;
    virDomainDefPtr vmdef = NULL;
    virDomainDeviceDefPtr dev = NULL;
    libxlDomainObjPrivatePtr priv;
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_DEVICE_MODIFY_LIVE |
                  VIR_DOMAIN_DEVICE_MODIFY_CONFIG, -1);

J
Jim Fehlig 已提交
3445
    if (!(vm = libxlDomObjFromDomain(dom)))
3446 3447
        goto cleanup;

3448 3449 3450
    if (virDomainAttachDeviceFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
        goto cleanup;

3451 3452 3453
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

3454 3455 3456 3457 3458 3459 3460 3461
    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) {
3462 3463
            virReportError(VIR_ERR_OPERATION_INVALID,
                           "%s", _("Domain is not running"));
3464
            goto endjob;
3465 3466 3467 3468
        }
    }

    if ((flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) && !vm->persistent) {
3469 3470
         virReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("cannot modify device on transient domain"));
3471
         goto endjob;
3472 3473 3474 3475 3476
    }

    priv = vm->privateData;

    if (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) {
3477
        if (!(dev = virDomainDeviceDefParse(xml, vm->def,
3478
                                            cfg->caps, driver->xmlopt,
3479
                                            VIR_DOMAIN_XML_INACTIVE)))
3480
            goto endjob;
3481 3482

        /* Make a copy for updated domain. */
3483
        if (!(vmdef = virDomainObjCopyPersistentDef(vm, cfg->caps,
3484
                                                    driver->xmlopt)))
3485
            goto endjob;
3486

3487
        if ((ret = libxlDomainAttachDeviceConfig(vmdef, dev)) < 0)
3488
            goto endjob;
3489
    } else {
3490
        ret = 0;
3491
    }
3492 3493 3494 3495

    if (flags & VIR_DOMAIN_DEVICE_MODIFY_LIVE) {
        /* If dev exists it was created to modify the domain config. Free it. */
        virDomainDeviceDefFree(dev);
3496
        if (!(dev = virDomainDeviceDefParse(xml, vm->def,
3497
                                            cfg->caps, driver->xmlopt,
3498
                                            VIR_DOMAIN_XML_INACTIVE)))
3499
            goto endjob;
3500

3501
        if ((ret = libxlDomainAttachDeviceLive(driver, priv, vm, dev)) < 0)
3502
            goto endjob;
3503

3504 3505 3506 3507
        /*
         * update domain status forcibly because the domain status may be
         * changed even if we attach the device failed.
         */
3508
        if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm) < 0)
3509 3510 3511 3512 3513
            ret = -1;
    }

    /* Finally, if no error until here, we can save config. */
    if (!ret && (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG)) {
3514
        ret = virDomainSaveConfig(cfg->configDir, vmdef);
3515
        if (!ret) {
3516
            virDomainObjAssignDef(vm, vmdef, false, NULL);
3517 3518 3519 3520
            vmdef = NULL;
        }
    }

3521 3522 3523 3524
endjob:
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

3525 3526 3527 3528
cleanup:
    virDomainDefFree(vmdef);
    virDomainDeviceDefFree(dev);
    if (vm)
3529
        virObjectUnlock(vm);
3530
    virObjectUnref(cfg);
3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544
    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)
{
3545
    libxlDriverPrivatePtr driver = dom->conn->privateData;
3546
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
3547 3548 3549 3550 3551 3552 3553 3554 3555
    virDomainObjPtr vm = NULL;
    virDomainDefPtr vmdef = NULL;
    virDomainDeviceDefPtr dev = NULL;
    libxlDomainObjPrivatePtr priv;
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_DEVICE_MODIFY_LIVE |
                  VIR_DOMAIN_DEVICE_MODIFY_CONFIG, -1);

J
Jim Fehlig 已提交
3556
    if (!(vm = libxlDomObjFromDomain(dom)))
3557 3558
        goto cleanup;

3559 3560 3561
    if (virDomainDetachDeviceFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
        goto cleanup;

3562 3563 3564
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

3565 3566 3567 3568 3569 3570 3571 3572 3573 3574
    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"));
3575
            goto endjob;
3576 3577 3578 3579 3580 3581
        }
    }

    if ((flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) && !vm->persistent) {
         virReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("cannot modify device on transient domain"));
3582
         goto endjob;
3583 3584 3585 3586 3587 3588
    }

    priv = vm->privateData;

    if (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) {
        if (!(dev = virDomainDeviceDefParse(xml, vm->def,
3589
                                            cfg->caps, driver->xmlopt,
3590
                                            VIR_DOMAIN_XML_INACTIVE)))
3591
            goto endjob;
3592 3593

        /* Make a copy for updated domain. */
3594
        if (!(vmdef = virDomainObjCopyPersistentDef(vm, cfg->caps,
3595
                                                    driver->xmlopt)))
3596
            goto endjob;
3597 3598

        if ((ret = libxlDomainDetachDeviceConfig(vmdef, dev)) < 0)
3599
            goto endjob;
3600 3601 3602 3603 3604 3605 3606 3607
    } 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,
3608
                                            cfg->caps, driver->xmlopt,
3609
                                            VIR_DOMAIN_XML_INACTIVE)))
3610
            goto endjob;
3611

3612
        if ((ret = libxlDomainDetachDeviceLive(driver, priv, vm, dev)) < 0)
3613
            goto endjob;
3614 3615 3616 3617 3618

        /*
         * update domain status forcibly because the domain status may be
         * changed even if we attach the device failed.
         */
3619
        if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm) < 0)
3620 3621 3622 3623 3624
            ret = -1;
    }

    /* Finally, if no error until here, we can save config. */
    if (!ret && (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG)) {
3625
        ret = virDomainSaveConfig(cfg->configDir, vmdef);
3626 3627 3628 3629 3630 3631
        if (!ret) {
            virDomainObjAssignDef(vm, vmdef, false, NULL);
            vmdef = NULL;
        }
    }

3632 3633 3634 3635
endjob:
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

3636 3637 3638 3639 3640
cleanup:
    virDomainDefFree(vmdef);
    virDomainDeviceDefFree(dev);
    if (vm)
        virObjectUnlock(vm);
3641
    virObjectUnref(cfg);
3642
    return ret;
3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655
}

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)
{
3656
    libxlDriverPrivatePtr driver = dom->conn->privateData;
3657
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
3658 3659 3660 3661 3662 3663 3664 3665 3666
    virDomainObjPtr vm = NULL;
    virDomainDefPtr vmdef = NULL;
    virDomainDeviceDefPtr dev = NULL;
    libxlDomainObjPrivatePtr priv;
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_DEVICE_MODIFY_LIVE |
                  VIR_DOMAIN_DEVICE_MODIFY_CONFIG, -1);

J
Jim Fehlig 已提交
3667
    if (!(vm = libxlDomObjFromDomain(dom)))
3668 3669
        goto cleanup;

3670 3671 3672
    if (virDomainUpdateDeviceFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
        goto cleanup;

3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696
    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;
    }

    priv = vm->privateData;

    if (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) {
        if (!(dev = virDomainDeviceDefParse(xml, vm->def,
3697
                                            cfg->caps, driver->xmlopt,
3698 3699 3700 3701
                                            VIR_DOMAIN_XML_INACTIVE)))
            goto cleanup;

        /* Make a copy for updated domain. */
3702
        if (!(vmdef = virDomainObjCopyPersistentDef(vm, cfg->caps,
3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715
                                                    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,
3716
                                            cfg->caps, driver->xmlopt,
3717 3718 3719 3720 3721 3722 3723 3724 3725 3726
                                            VIR_DOMAIN_XML_INACTIVE)))
            goto cleanup;

        if ((ret = libxlDomainUpdateDeviceLive(priv, vm, dev)) < 0)
            goto cleanup;

        /*
         * update domain status forcibly because the domain status may be
         * changed even if we attach the device failed.
         */
3727
        if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm) < 0)
3728 3729 3730 3731 3732
            ret = -1;
    }

    /* Finally, if no error until here, we can save config. */
    if (!ret && (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG)) {
3733
        ret = virDomainSaveConfig(cfg->configDir, vmdef);
3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744
        if (!ret) {
            virDomainObjAssignDef(vm, vmdef, false, NULL);
            vmdef = NULL;
        }
    }

cleanup:
    virDomainDefFree(vmdef);
    virDomainDeviceDefFree(dev);
    if (vm)
        virObjectUnlock(vm);
3745
    virObjectUnref(cfg);
3746
    return ret;
3747 3748
}

3749 3750 3751 3752 3753
static unsigned long long
libxlNodeGetFreeMemory(virConnectPtr conn)
{
    libxl_physinfo phy_info;
    libxlDriverPrivatePtr driver = conn->privateData;
3754 3755
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
    unsigned long long ret = 0;
3756

3757
    if (virNodeGetFreeMemoryEnsureACL(conn) < 0)
3758
        goto cleanup;
3759

3760
    if (libxl_get_physinfo(cfg->ctx, &phy_info)) {
3761 3762
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("libxl_get_physinfo_info failed"));
3763
        goto cleanup;
3764 3765
    }

3766 3767 3768 3769 3770
    ret = phy_info.free_pages * cfg->verInfo->pagesize;

cleanup:
    virObjectUnref(cfg);
    return ret;
3771 3772
}

3773 3774 3775 3776 3777 3778 3779 3780 3781 3782
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;
3783
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
3784 3785

    if (virNodeGetCellsFreeMemoryEnsureACL(conn) < 0)
3786
        goto cleanup;
3787

3788
    numa_info = libxl_get_numainfo(cfg->ctx, &nr_nodes);
3789
    if (numa_info == NULL || nr_nodes == 0) {
3790 3791 3792
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("libxl_get_numainfo failed"));
        goto cleanup;
3793 3794 3795
    }

    /* Check/sanitize the cell range */
3796
    if (startCell >= nr_nodes) {
3797 3798
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("start cell %d out of range (0-%d)"),
3799
                       startCell, nr_nodes - 1);
3800 3801 3802
        goto cleanup;
    }
    lastCell = startCell + maxCells - 1;
3803 3804
    if (lastCell >= nr_nodes)
        lastCell = nr_nodes - 1;
3805 3806 3807 3808 3809 3810 3811

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

3813 3814 3815 3816
    ret = numCells;

cleanup:
    libxl_numainfo_list_free(numa_info, nr_nodes);
3817
    virObjectUnref(cfg);
3818 3819 3820
    return ret;
}

3821
static int
3822
libxlConnectDomainEventRegister(virConnectPtr conn,
3823 3824
                                virConnectDomainEventCallback callback,
                                void *opaque,
3825
                                virFreeCallback freecb)
3826 3827 3828
{
    libxlDriverPrivatePtr driver = conn->privateData;

3829 3830 3831
    if (virConnectDomainEventRegisterEnsureACL(conn) < 0)
        return -1;

3832 3833 3834 3835
    if (virDomainEventStateRegister(conn,
                                    driver->domainEventState,
                                    callback, opaque, freecb) < 0)
        return -1;
3836

3837
    return 0;
3838 3839 3840 3841
}


static int
3842 3843
libxlConnectDomainEventDeregister(virConnectPtr conn,
                                  virConnectDomainEventCallback callback)
3844 3845 3846
{
    libxlDriverPrivatePtr driver = conn->privateData;

3847 3848 3849
    if (virConnectDomainEventDeregisterEnsureACL(conn) < 0)
        return -1;

3850 3851 3852 3853
    if (virDomainEventStateDeregister(conn,
                                      driver->domainEventState,
                                      callback) < 0)
        return -1;
3854

3855
    return 0;
3856 3857
}

3858 3859 3860 3861 3862 3863
static int
libxlDomainGetAutostart(virDomainPtr dom, int *autostart)
{
    virDomainObjPtr vm;
    int ret = -1;

J
Jim Fehlig 已提交
3864
    if (!(vm = libxlDomObjFromDomain(dom)))
3865 3866
        goto cleanup;

3867 3868 3869
    if (virDomainGetAutostartEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

3870 3871 3872 3873 3874
    *autostart = vm->autostart;
    ret = 0;

cleanup:
    if (vm)
3875
        virObjectUnlock(vm);
3876 3877 3878 3879 3880 3881 3882
    return ret;
}

static int
libxlDomainSetAutostart(virDomainPtr dom, int autostart)
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
3883
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
3884 3885 3886 3887
    virDomainObjPtr vm;
    char *configFile = NULL, *autostartLink = NULL;
    int ret = -1;

J
Jim Fehlig 已提交
3888
    if (!(vm = libxlDomObjFromDomain(dom)))
3889 3890
        goto cleanup;

3891 3892 3893
    if (virDomainSetAutostartEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

3894 3895 3896
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

3897
    if (!vm->persistent) {
3898 3899
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("cannot set autostart for transient domain"));
3900
        goto endjob;
3901 3902 3903 3904 3905
    }

    autostart = (autostart != 0);

    if (vm->autostart != autostart) {
3906
        if (!(configFile = virDomainConfigFile(cfg->configDir, vm->def->name)))
3907
            goto endjob;
3908
        if (!(autostartLink = virDomainConfigFile(cfg->autostartDir, vm->def->name)))
3909
            goto endjob;
3910 3911

        if (autostart) {
3912
            if (virFileMakePath(cfg->autostartDir) < 0) {
3913
                virReportSystemError(errno,
3914
                                     _("cannot create autostart directory %s"),
3915
                                     cfg->autostartDir);
3916
                goto endjob;
3917 3918 3919 3920 3921 3922
            }

            if (symlink(configFile, autostartLink) < 0) {
                virReportSystemError(errno,
                                     _("Failed to create symlink '%s to '%s'"),
                                     autostartLink, configFile);
3923
                goto endjob;
3924 3925 3926 3927 3928 3929
            }
        } else {
            if (unlink(autostartLink) < 0 && errno != ENOENT && errno != ENOTDIR) {
                virReportSystemError(errno,
                                     _("Failed to delete symlink '%s'"),
                                     autostartLink);
3930
                goto endjob;
3931 3932 3933 3934 3935 3936 3937
            }
        }

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

3938 3939 3940 3941
endjob:
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

3942 3943 3944 3945
cleanup:
    VIR_FREE(configFile);
    VIR_FREE(autostartLink);
    if (vm)
3946
        virObjectUnlock(vm);
3947
    virObjectUnref(cfg);
3948 3949 3950
    return ret;
}

3951 3952 3953 3954 3955 3956
static char *
libxlDomainGetSchedulerType(virDomainPtr dom, int *nparams)
{
    libxlDomainObjPrivatePtr priv;
    virDomainObjPtr vm;
    char * ret = NULL;
3957
    const char *name = NULL;
J
Jim Fehlig 已提交
3958
    libxl_scheduler sched_id;
3959

J
Jim Fehlig 已提交
3960
    if (!(vm = libxlDomObjFromDomain(dom)))
3961 3962
        goto cleanup;

3963 3964 3965
    if (virDomainGetSchedulerTypeEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

3966
    if (!virDomainObjIsActive(vm)) {
3967
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not running"));
3968 3969 3970 3971
        goto cleanup;
    }

    priv = vm->privateData;
J
Jim Fehlig 已提交
3972
    sched_id = libxl_get_scheduler(priv->ctx);
3973

3974 3975
    if (nparams)
        *nparams = 0;
3976
    switch (sched_id) {
J
Jim Fehlig 已提交
3977
    case LIBXL_SCHEDULER_SEDF:
3978
        name = "sedf";
3979
        break;
J
Jim Fehlig 已提交
3980
    case LIBXL_SCHEDULER_CREDIT:
3981
        name = "credit";
3982 3983
        if (nparams)
            *nparams = XEN_SCHED_CREDIT_NPARAM;
3984
        break;
J
Jim Fehlig 已提交
3985
    case LIBXL_SCHEDULER_CREDIT2:
3986
        name = "credit2";
3987
        break;
J
Jim Fehlig 已提交
3988
    case LIBXL_SCHEDULER_ARINC653:
3989
        name = "arinc653";
3990 3991
        break;
    default:
J
Jim Fehlig 已提交
3992 3993 3994
        virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("Failed to get scheduler id for domain '%d'"
                     " with libxenlight"), dom->id);
3995 3996 3997
        goto cleanup;
    }

3998
    ignore_value(VIR_STRDUP(ret, name));
3999 4000 4001

cleanup:
    if (vm)
4002
        virObjectUnlock(vm);
4003 4004 4005
    return ret;
}

4006
static int
4007 4008 4009 4010
libxlDomainGetSchedulerParametersFlags(virDomainPtr dom,
                                       virTypedParameterPtr params,
                                       int *nparams,
                                       unsigned int flags)
4011 4012 4013
{
    libxlDomainObjPrivatePtr priv;
    virDomainObjPtr vm;
J
Jim Fehlig 已提交
4014 4015
    libxl_domain_sched_params sc_info;
    libxl_scheduler sched_id;
4016 4017
    int ret = -1;

4018 4019 4020 4021
    virCheckFlags(VIR_TYPED_PARAM_STRING_OKAY, -1);

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

J
Jim Fehlig 已提交
4023
    if (!(vm = libxlDomObjFromDomain(dom)))
4024 4025
        goto cleanup;

4026 4027 4028
    if (virDomainGetSchedulerParametersFlagsEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

4029
    if (!virDomainObjIsActive(vm)) {
J
Jim Fehlig 已提交
4030 4031
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Domain is not running"));
4032 4033 4034 4035 4036
        goto cleanup;
    }

    priv = vm->privateData;

J
Jim Fehlig 已提交
4037
    sched_id = libxl_get_scheduler(priv->ctx);
4038

J
Jim Fehlig 已提交
4039
    if (sched_id != LIBXL_SCHEDULER_CREDIT) {
4040 4041
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Only 'credit' scheduler is supported"));
4042 4043 4044
        goto cleanup;
    }

J
Jim Fehlig 已提交
4045
    if (libxl_domain_sched_params_get(priv->ctx, dom->id, &sc_info) != 0) {
4046 4047 4048
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to get scheduler parameters for domain '%d'"
                         " with libxenlight"), dom->id);
4049 4050 4051
        goto cleanup;
    }

4052 4053
    if (virTypedParameterAssign(&params[0], VIR_DOMAIN_SCHEDULER_WEIGHT,
                                VIR_TYPED_PARAM_UINT, sc_info.weight) < 0)
4054 4055
        goto cleanup;

4056
    if (*nparams > 1) {
4057 4058
        if (virTypedParameterAssign(&params[0], VIR_DOMAIN_SCHEDULER_CAP,
                                    VIR_TYPED_PARAM_UINT, sc_info.cap) < 0)
4059
            goto cleanup;
4060 4061
    }

4062 4063
    if (*nparams > XEN_SCHED_CREDIT_NPARAM)
        *nparams = XEN_SCHED_CREDIT_NPARAM;
4064 4065 4066 4067
    ret = 0;

cleanup:
    if (vm)
4068
        virObjectUnlock(vm);
4069 4070 4071 4072
    return ret;
}

static int
4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083
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)
4084
{
4085
    libxlDriverPrivatePtr driver = dom->conn->privateData;
4086 4087
    libxlDomainObjPrivatePtr priv;
    virDomainObjPtr vm;
J
Jim Fehlig 已提交
4088
    libxl_domain_sched_params sc_info;
4089
    int sched_id;
4090
    size_t i;
4091 4092
    int ret = -1;

4093
    virCheckFlags(0, -1);
4094 4095 4096 4097 4098 4099
    if (virTypedParamsValidate(params, nparams,
                               VIR_DOMAIN_SCHEDULER_WEIGHT,
                               VIR_TYPED_PARAM_UINT,
                               VIR_DOMAIN_SCHEDULER_CAP,
                               VIR_TYPED_PARAM_UINT,
                               NULL) < 0)
4100
        return -1;
4101

J
Jim Fehlig 已提交
4102
    if (!(vm = libxlDomObjFromDomain(dom)))
4103 4104
        goto cleanup;

4105 4106 4107
    if (virDomainSetSchedulerParametersFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
        goto cleanup;

4108 4109 4110
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

4111
    if (!virDomainObjIsActive(vm)) {
4112
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not running"));
4113
        goto endjob;
4114 4115 4116 4117
    }

    priv = vm->privateData;

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

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

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

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

4136
        if (STREQ(param->field, VIR_DOMAIN_SCHEDULER_WEIGHT))
4137
            sc_info.weight = params[i].value.ui;
4138
        else if (STREQ(param->field, VIR_DOMAIN_SCHEDULER_CAP))
4139 4140 4141
            sc_info.cap = params[i].value.ui;
    }

J
Jim Fehlig 已提交
4142
    if (libxl_domain_sched_params_set(priv->ctx, dom->id, &sc_info) != 0) {
4143 4144 4145
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to set scheduler parameters for domain '%d'"
                         " with libxenlight"), dom->id);
4146
        goto endjob;
4147 4148 4149 4150
    }

    ret = 0;

4151 4152 4153 4154
endjob:
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

4155 4156
cleanup:
    if (vm)
4157
        virObjectUnlock(vm);
4158 4159 4160
    return ret;
}

B
Bamvor Jian Zhang 已提交
4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182

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;
    char *console = NULL;

    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 已提交
4183
    if (!(vm = libxlDomObjFromDomain(dom)))
B
Bamvor Jian Zhang 已提交
4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239
        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;

    if (vm->def->nserials)
        chr = vm->def->serials[0];

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

    ret = libxl_primary_console_get_tty(priv->ctx, vm->def->id, &console);
    if (ret)
        goto cleanup;

    if (VIR_STRDUP(chr->source.data.file.path, console) < 0)
        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;
    }

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

4240 4241 4242 4243 4244 4245 4246
static int
libxlDomainSetSchedulerParameters(virDomainPtr dom, virTypedParameterPtr params,
                                  int nparams)
{
    return libxlDomainSetSchedulerParametersFlags(dom, params, nparams, 0);
}

4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276
/* 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)
{
    libxlDomainObjPrivatePtr priv;
    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;

4277 4278
    libxl_bitmap_init(&nodemap);

J
Jim Fehlig 已提交
4279
    if (!(vm = libxlDomObjFromDomain(dom)))
4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300
        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;
    }

    priv = vm->privateData;

    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];
4301
        int numnodes;
4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319

        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 */
4320 4321 4322 4323
            numnodes = libxl_get_max_nodes(priv->ctx);
            if (numnodes <= 0)
                goto cleanup;

4324
            if (libxl_node_bitmap_alloc(priv->ctx, &nodemap, 0) ||
4325
                !(nodes = virBitmapNew(numnodes))) {
4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377
                virReportOOMError();
                goto cleanup;
            }

            rc = libxl_domain_get_nodeaffinity(priv->ctx,
                                               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;
                }
            }

            nodeset = virBitmapFormat(nodes);
            if (!nodeset && VIR_STRDUP(nodeset, "") < 0)
                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;

cleanup:
    VIR_FREE(nodeset);
    virBitmapFree(nodes);
    libxl_bitmap_dispose(&nodemap);
    if (vm)
        virObjectUnlock(vm);
    return ret;
}
#endif

J
Jim Fehlig 已提交
4378 4379 4380 4381 4382 4383
static int
libxlDomainIsActive(virDomainPtr dom)
{
    virDomainObjPtr obj;
    int ret = -1;

J
Jim Fehlig 已提交
4384
    if (!(obj = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
4385
        goto cleanup;
4386 4387 4388 4389

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

J
Jim Fehlig 已提交
4390 4391 4392 4393
    ret = virDomainObjIsActive(obj);

  cleanup:
    if (obj)
4394
        virObjectUnlock(obj);
J
Jim Fehlig 已提交
4395 4396 4397 4398 4399 4400 4401 4402 4403
    return ret;
}

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

J
Jim Fehlig 已提交
4404
    if (!(obj = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
4405
        goto cleanup;
4406 4407 4408 4409

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

J
Jim Fehlig 已提交
4410 4411 4412 4413
    ret = obj->persistent;

  cleanup:
    if (obj)
4414
        virObjectUnlock(obj);
J
Jim Fehlig 已提交
4415 4416 4417
    return ret;
}

4418 4419 4420 4421 4422 4423
static int
libxlDomainIsUpdated(virDomainPtr dom)
{
    virDomainObjPtr vm;
    int ret = -1;

J
Jim Fehlig 已提交
4424
    if (!(vm = libxlDomObjFromDomain(dom)))
4425
        goto cleanup;
4426 4427 4428 4429

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

4430 4431 4432 4433
    ret = vm->updated;

cleanup:
    if (vm)
4434
        virObjectUnlock(vm);
4435 4436 4437
    return ret;
}

4438
static int
4439 4440 4441
libxlConnectDomainEventRegisterAny(virConnectPtr conn, virDomainPtr dom, int eventID,
                                   virConnectDomainEventGenericCallback callback,
                                   void *opaque, virFreeCallback freecb)
4442 4443 4444 4445
{
    libxlDriverPrivatePtr driver = conn->privateData;
    int ret;

4446 4447 4448
    if (virConnectDomainEventRegisterAnyEnsureACL(conn) < 0)
        return -1;

4449 4450 4451 4452
    if (virDomainEventStateRegisterID(conn,
                                      driver->domainEventState,
                                      dom, eventID, callback, opaque,
                                      freecb, &ret) < 0)
4453
        ret = -1;
4454 4455 4456 4457 4458 4459

    return ret;
}


static int
4460
libxlConnectDomainEventDeregisterAny(virConnectPtr conn, int callbackID)
4461 4462 4463
{
    libxlDriverPrivatePtr driver = conn->privateData;

4464 4465 4466
    if (virConnectDomainEventDeregisterAnyEnsureACL(conn) < 0)
        return -1;

4467 4468 4469 4470
    if (virObjectEventStateDeregisterID(conn,
                                        driver->domainEventState,
                                        callbackID) < 0)
        return -1;
4471

4472
    return 0;
4473 4474
}

J
Jim Fehlig 已提交
4475

4476
static int
4477
libxlConnectIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
4478 4479 4480 4481
{
    return 1;
}

4482
static int
4483 4484 4485
libxlConnectListAllDomains(virConnectPtr conn,
                           virDomainPtr **domains,
                           unsigned int flags)
4486 4487 4488 4489
{
    libxlDriverPrivatePtr driver = conn->privateData;
    int ret = -1;

O
Osier Yang 已提交
4490
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
4491

4492 4493 4494
    if (virConnectListAllDomainsEnsureACL(conn) < 0)
        return -1;

4495 4496
    ret = virDomainObjListExport(driver->domains, conn, domains,
                                 virConnectListAllDomainsCheckACL, flags);
4497 4498 4499 4500

    return ret;
}

4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514
/* 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:
        return 1;
    default:
        return 0;
    }
}
4515

4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 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 4615 4616 4617 4618 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 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690
static int
libxlNodeDeviceGetPCIInfo(virNodeDeviceDefPtr def,
                          unsigned *domain,
                          unsigned *bus,
                          unsigned *slot,
                          unsigned *function)
{
    virNodeDevCapsDefPtr cap;
    int ret = -1;

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

    ret = 0;
out:
    return ret;
}

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;
cleanup:
    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)
        goto out;

    ret = 0;
out:
    virPCIDeviceFree(pci);
cleanup:
    virNodeDeviceDefFree(def);
    VIR_FREE(xml);
    return ret;
}

static int
libxlNodeDeviceReset(virNodeDevicePtr dev)
{
    virPCIDevicePtr pci;
    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)
        goto out;

    ret = 0;
out:
    virPCIDeviceFree(pci);
cleanup:
    virNodeDeviceDefFree(def);
    VIR_FREE(xml);
    return ret;
}

4691

J
Jim Fehlig 已提交
4692
static virDriver libxlDriver = {
4693
    .no = VIR_DRV_LIBXL,
4694
    .name = LIBXL_DRIVER_NAME,
4695 4696 4697 4698
    .connectOpen = libxlConnectOpen, /* 0.9.0 */
    .connectClose = libxlConnectClose, /* 0.9.0 */
    .connectGetType = libxlConnectGetType, /* 0.9.0 */
    .connectGetVersion = libxlConnectGetVersion, /* 0.9.0 */
4699
    .connectGetHostname = libxlConnectGetHostname, /* 0.9.0 */
4700
    .connectGetSysinfo = libxlConnectGetSysinfo, /* 1.1.0 */
4701
    .connectGetMaxVcpus = libxlConnectGetMaxVcpus, /* 0.9.0 */
4702
    .nodeGetInfo = libxlNodeGetInfo, /* 0.9.0 */
4703 4704 4705 4706
    .connectGetCapabilities = libxlConnectGetCapabilities, /* 0.9.0 */
    .connectListDomains = libxlConnectListDomains, /* 0.9.0 */
    .connectNumOfDomains = libxlConnectNumOfDomains, /* 0.9.0 */
    .connectListAllDomains = libxlConnectListAllDomains, /* 0.9.13 */
4707 4708 4709 4710 4711 4712 4713
    .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 */
4714
    .domainShutdownFlags = libxlDomainShutdownFlags, /* 0.9.10 */
4715 4716
    .domainReboot = libxlDomainReboot, /* 0.9.0 */
    .domainDestroy = libxlDomainDestroy, /* 0.9.0 */
4717
    .domainDestroyFlags = libxlDomainDestroyFlags, /* 0.9.4 */
4718 4719
    .domainGetOSType = libxlDomainGetOSType, /* 0.9.0 */
    .domainGetMaxMemory = libxlDomainGetMaxMemory, /* 0.9.0 */
4720
    .domainSetMaxMemory = libxlDomainSetMaxMemory, /* 0.9.2 */
4721 4722 4723 4724
    .domainSetMemory = libxlDomainSetMemory, /* 0.9.0 */
    .domainSetMemoryFlags = libxlDomainSetMemoryFlags, /* 0.9.0 */
    .domainGetInfo = libxlDomainGetInfo, /* 0.9.0 */
    .domainGetState = libxlDomainGetState, /* 0.9.2 */
4725
    .domainSave = libxlDomainSave, /* 0.9.2 */
4726
    .domainSaveFlags = libxlDomainSaveFlags, /* 0.9.4 */
4727
    .domainRestore = libxlDomainRestore, /* 0.9.2 */
4728
    .domainRestoreFlags = libxlDomainRestoreFlags, /* 0.9.4 */
4729
    .domainCoreDump = libxlDomainCoreDump, /* 0.9.2 */
4730 4731 4732 4733
    .domainSetVcpus = libxlDomainSetVcpus, /* 0.9.0 */
    .domainSetVcpusFlags = libxlDomainSetVcpusFlags, /* 0.9.0 */
    .domainGetVcpusFlags = libxlDomainGetVcpusFlags, /* 0.9.0 */
    .domainPinVcpu = libxlDomainPinVcpu, /* 0.9.0 */
4734
    .domainPinVcpuFlags = libxlDomainPinVcpuFlags, /* 1.2.1 */
4735
    .domainGetVcpus = libxlDomainGetVcpus, /* 0.9.0 */
4736
    .domainGetVcpuPinInfo = libxlDomainGetVcpuPinInfo, /* 1.2.1 */
4737
    .domainGetXMLDesc = libxlDomainGetXMLDesc, /* 0.9.0 */
4738 4739 4740 4741
    .connectDomainXMLFromNative = libxlConnectDomainXMLFromNative, /* 0.9.0 */
    .connectDomainXMLToNative = libxlConnectDomainXMLToNative, /* 0.9.0 */
    .connectListDefinedDomains = libxlConnectListDefinedDomains, /* 0.9.0 */
    .connectNumOfDefinedDomains = libxlConnectNumOfDefinedDomains, /* 0.9.0 */
4742 4743 4744 4745
    .domainCreate = libxlDomainCreate, /* 0.9.0 */
    .domainCreateWithFlags = libxlDomainCreateWithFlags, /* 0.9.0 */
    .domainDefineXML = libxlDomainDefineXML, /* 0.9.0 */
    .domainUndefine = libxlDomainUndefine, /* 0.9.0 */
4746
    .domainUndefineFlags = libxlDomainUndefineFlags, /* 0.9.4 */
4747 4748 4749 4750 4751
    .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 */
4752 4753 4754 4755
    .domainGetAutostart = libxlDomainGetAutostart, /* 0.9.0 */
    .domainSetAutostart = libxlDomainSetAutostart, /* 0.9.0 */
    .domainGetSchedulerType = libxlDomainGetSchedulerType, /* 0.9.0 */
    .domainGetSchedulerParameters = libxlDomainGetSchedulerParameters, /* 0.9.0 */
4756
    .domainGetSchedulerParametersFlags = libxlDomainGetSchedulerParametersFlags, /* 0.9.2 */
4757
    .domainSetSchedulerParameters = libxlDomainSetSchedulerParameters, /* 0.9.0 */
4758
    .domainSetSchedulerParametersFlags = libxlDomainSetSchedulerParametersFlags, /* 0.9.2 */
4759 4760 4761
#ifdef LIBXL_HAVE_DOMAIN_NODEAFFINITY
    .domainGetNumaParameters = libxlDomainGetNumaParameters, /* 1.1.1 */
#endif
4762
    .nodeGetFreeMemory = libxlNodeGetFreeMemory, /* 0.9.0 */
4763
    .nodeGetCellsFreeMemory = libxlNodeGetCellsFreeMemory, /* 1.1.1 */
4764 4765
    .connectDomainEventRegister = libxlConnectDomainEventRegister, /* 0.9.0 */
    .connectDomainEventDeregister = libxlConnectDomainEventDeregister, /* 0.9.0 */
4766 4767 4768
    .domainManagedSave = libxlDomainManagedSave, /* 0.9.2 */
    .domainHasManagedSaveImage = libxlDomainHasManagedSaveImage, /* 0.9.2 */
    .domainManagedSaveRemove = libxlDomainManagedSaveRemove, /* 0.9.2 */
B
Bamvor Jian Zhang 已提交
4769
    .domainOpenConsole = libxlDomainOpenConsole, /* 1.1.2 */
4770 4771 4772
    .domainIsActive = libxlDomainIsActive, /* 0.9.0 */
    .domainIsPersistent = libxlDomainIsPersistent, /* 0.9.0 */
    .domainIsUpdated = libxlDomainIsUpdated, /* 0.9.0 */
4773 4774 4775
    .connectDomainEventRegisterAny = libxlConnectDomainEventRegisterAny, /* 0.9.0 */
    .connectDomainEventDeregisterAny = libxlConnectDomainEventDeregisterAny, /* 0.9.0 */
    .connectIsAlive = libxlConnectIsAlive, /* 0.9.8 */
4776
    .connectSupportsFeature = libxlConnectSupportsFeature, /* 1.1.1 */
4777 4778 4779 4780
    .nodeDeviceDettach = libxlNodeDeviceDettach, /* 1.2.3 */
    .nodeDeviceDetachFlags = libxlNodeDeviceDetachFlags, /* 1.2.3 */
    .nodeDeviceReAttach = libxlNodeDeviceReAttach, /* 1.2.3 */
    .nodeDeviceReset = libxlNodeDeviceReset, /* 1.2.3 */
J
Jim Fehlig 已提交
4781 4782 4783 4784
};

static virStateDriver libxlStateDriver = {
    .name = "LIBXL",
4785
    .stateInitialize = libxlStateInitialize,
4786
    .stateAutoStart = libxlStateAutoStart,
4787 4788
    .stateCleanup = libxlStateCleanup,
    .stateReload = libxlStateReload,
J
Jim Fehlig 已提交
4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801
};


int
libxlRegister(void)
{
    if (virRegisterDriver(&libxlDriver) < 0)
        return -1;
    if (virRegisterStateDriver(&libxlStateDriver) < 0)
        return -1;

    return 0;
}