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

#define VIR_FROM_THIS VIR_FROM_LIBXL

61 62
VIR_LOG_INIT("libxl.libxl_driver");

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

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

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

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

77

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

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

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

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

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

114
    virObjectLock(vm);
115 116 117
    virResetLastError();

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

126
    ret = 0;
127
 cleanup:
128
    virObjectUnlock(vm);
129
    return ret;
130 131
}

J
Jim Fehlig 已提交
132 133 134 135
/*
 * Reconnect to running domains that were previously started/created
 * with libxenlight driver.
 */
136 137
static int
libxlReconnectDomain(virDomainObjPtr vm,
J
Jim Fehlig 已提交
138 139 140
                     void *opaque)
{
    libxlDriverPrivatePtr driver = opaque;
141
    libxlDomainObjPrivatePtr priv = vm->privateData;
J
Jim Fehlig 已提交
142 143 144 145
    int rc;
    libxl_dominfo d_info;
    int len;
    uint8_t *data = NULL;
146
    virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;
J
Jim Fehlig 已提交
147

148
    virObjectLock(vm);
J
Jim Fehlig 已提交
149

150
    libxlDomainObjPrivateInitCtx(vm);
J
Jim Fehlig 已提交
151
    /* Does domain still exist? */
152
    rc = libxl_domain_info(priv->ctx, &d_info, vm->def->id);
J
Jim Fehlig 已提交
153 154 155 156 157 158 159 160 161
    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? */
162
    if (libxl_userdata_retrieve(priv->ctx, vm->def->id,
J
Jim Fehlig 已提交
163 164 165 166 167 168 169
                                "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;
170 171 172 173 174 175

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

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

178
    if (virAtomicIntInc(&driver->nactive) == 1 && driver->inhibitCallback)
179 180
        driver->inhibitCallback(true, driver->inhibitOpaque);

181
    /* Re-register domain death et. al. events */
182
    libxlDomainEventsRegister(driver, vm);
183
    virObjectUnlock(vm);
184
    return 0;
J
Jim Fehlig 已提交
185

186
 out:
187
    libxlDomainCleanup(driver, vm, VIR_DOMAIN_SHUTOFF_UNKNOWN);
J
Jim Fehlig 已提交
188
    if (!vm->persistent)
189
        virDomainObjListRemoveLocked(driver->domains, vm);
J
Jim Fehlig 已提交
190
    else
191
        virObjectUnlock(vm);
192 193

    return -1;
J
Jim Fehlig 已提交
194 195 196 197 198
}

static void
libxlReconnectDomains(libxlDriverPrivatePtr driver)
{
199
    virDomainObjListForEach(driver->domains, libxlReconnectDomain, driver);
J
Jim Fehlig 已提交
200 201 202
}

static int
203
libxlStateCleanup(void)
J
Jim Fehlig 已提交
204 205 206 207
{
    if (!libxl_driver)
        return -1;

208
    virObjectUnref(libxl_driver->hostdevMgr);
209
    virObjectUnref(libxl_driver->config);
210
    virObjectUnref(libxl_driver->xmlopt);
211
    virObjectUnref(libxl_driver->domains);
212
    virObjectUnref(libxl_driver->reservedVNCPorts);
J
Jim Fehlig 已提交
213
    virObjectUnref(libxl_driver->migrationPorts);
J
Jim Fehlig 已提交
214

215
    virObjectEventStateFree(libxl_driver->domainEventState);
216
    virSysinfoDefFree(libxl_driver->hostsysinfo);
217

J
Jim Fehlig 已提交
218 219 220 221 222 223
    virMutexDestroy(&libxl_driver->lock);
    VIR_FREE(libxl_driver);

    return 0;
}

224 225
static bool
libxlDriverShouldLoad(bool privileged)
226
{
227 228
    bool ret = false;
    int status;
229
    char *output = NULL;
J
Jim Fehlig 已提交
230

231
    /* Don't load if non-root */
J
Jim Fehlig 已提交
232
    if (!privileged) {
233
        VIR_INFO("Not running privileged, disabling libxenlight driver");
234
        return ret;
J
Jim Fehlig 已提交
235 236
    }

237 238 239
    if (!virFileExists(HYPERVISOR_CAPABILITIES)) {
        VIR_INFO("Disabling driver as " HYPERVISOR_CAPABILITIES
                 " does not exist");
240
        return ret;
241
    }
242 243 244 245 246
    /*
     * 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.
     */
247
    status = virFileReadAll(HYPERVISOR_CAPABILITIES, 10, &output);
248 249 250 251 252
    if (status >= 0) {
        status = strncmp(output, "control_d", 9);
    }
    VIR_FREE(output);
    if (status) {
253 254 255 256 257 258 259
        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 */
260 261 262 263 264 265 266 267 268 269 270
    if (virFileExists("/usr/sbin/xend")) {
        virCommandPtr cmd;

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

275 276 277 278 279 280 281 282
    return ret;
}

static int
libxlStateInitialize(bool privileged,
                     virStateInhibitCallback callback ATTRIBUTE_UNUSED,
                     void *opaque ATTRIBUTE_UNUSED)
{
283
    libxlDriverConfigPtr cfg;
284 285 286 287 288
    char ebuf[1024];

    if (!libxlDriverShouldLoad(privileged))
        return 0;

J
Jim Fehlig 已提交
289 290 291 292
    if (VIR_ALLOC(libxl_driver) < 0)
        return -1;

    if (virMutexInit(&libxl_driver->lock) < 0) {
293 294
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("cannot initialize mutex"));
J
Jim Fehlig 已提交
295 296 297 298 299
        VIR_FREE(libxl_driver);
        return -1;
    }

    /* Allocate bitmap for vnc port reservation */
300
    if (!(libxl_driver->reservedVNCPorts =
J
Ján Tomko 已提交
301 302
          virPortAllocatorNew(_("VNC"),
                              LIBXL_VNC_PORT_MIN,
303 304
                              LIBXL_VNC_PORT_MAX)))
        goto error;
J
Jim Fehlig 已提交
305

J
Jim Fehlig 已提交
306 307 308 309 310 311 312
    /* Allocate bitmap for migration port reservation */
    if (!(libxl_driver->migrationPorts =
          virPortAllocatorNew(_("migration"),
                              LIBXL_MIGRATION_PORT_MIN,
                              LIBXL_MIGRATION_PORT_MAX)))
        goto error;

313 314
    if (!(libxl_driver->domains = virDomainObjListNew()))
        goto error;
J
Jim Fehlig 已提交
315

316 317 318
    if (!(libxl_driver->hostdevMgr = virHostdevManagerGetDefault()))
        goto error;

319
    if (!(cfg = libxlDriverConfigNew()))
320
        goto error;
J
Jim Fehlig 已提交
321

322 323
    libxl_driver->config = cfg;
    if (virFileMakePath(cfg->stateDir) < 0) {
324 325
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("failed to create state dir '%s': %s"),
326
                       cfg->stateDir,
327
                       virStrerror(errno, ebuf, sizeof(ebuf)));
J
Jim Fehlig 已提交
328 329
        goto error;
    }
330
    if (virFileMakePath(cfg->libDir) < 0) {
331 332
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("failed to create lib dir '%s': %s"),
333
                       cfg->libDir,
334
                       virStrerror(errno, ebuf, sizeof(ebuf)));
J
Jim Fehlig 已提交
335 336
        goto error;
    }
337
    if (virFileMakePath(cfg->saveDir) < 0) {
338 339
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("failed to create save dir '%s': %s"),
340
                       cfg->saveDir,
341
                       virStrerror(errno, ebuf, sizeof(ebuf)));
J
Jim Fehlig 已提交
342 343
        goto error;
    }
344 345 346 347 348 349 350
    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 已提交
351

352
    /* read the host sysinfo */
353
    libxl_driver->hostsysinfo = virSysinfoRead();
354

355
    libxl_driver->domainEventState = virObjectEventStateNew();
E
Eric Blake 已提交
356
    if (!libxl_driver->domainEventState)
357 358
        goto error;

359
    if ((cfg->caps = libxlMakeCapabilities(cfg->ctx)) == NULL) {
360 361
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("cannot create capabilities for libxenlight"));
J
Jim Fehlig 已提交
362 363 364
        goto error;
    }

365
    if (!(libxl_driver->xmlopt = libxlCreateXMLConf()))
366
        goto error;
J
Jim Fehlig 已提交
367 368

    /* Load running domains first. */
369
    if (virDomainObjListLoadAllConfigs(libxl_driver->domains,
370 371
                                       cfg->stateDir,
                                       cfg->autostartDir,
372
                                       1,
373
                                       cfg->caps,
374 375
                                       libxl_driver->xmlopt,
                                       1 << VIR_DOMAIN_VIRT_XEN,
376
                                       NULL, NULL) < 0)
J
Jim Fehlig 已提交
377 378 379 380 381
        goto error;

    libxlReconnectDomains(libxl_driver);

    /* Then inactive persistent configs */
382
    if (virDomainObjListLoadAllConfigs(libxl_driver->domains,
383 384
                                       cfg->configDir,
                                       cfg->autostartDir,
385
                                       0,
386
                                       cfg->caps,
387 388
                                       libxl_driver->xmlopt,
                                       1 << VIR_DOMAIN_VIRT_XEN,
389
                                       NULL, NULL) < 0)
J
Jim Fehlig 已提交
390 391
        goto error;

392 393
    virDomainObjListForEach(libxl_driver->domains, libxlDomainManagedSaveLoad,
                            libxl_driver);
394

J
Jim Fehlig 已提交
395 396
    return 0;

397
 error:
398
    libxlStateCleanup();
399
    return -1;
J
Jim Fehlig 已提交
400 401
}

402 403 404 405 406 407 408 409 410 411
static void
libxlStateAutoStart(void)
{
    if (!libxl_driver)
        return;

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

J
Jim Fehlig 已提交
412
static int
413
libxlStateReload(void)
J
Jim Fehlig 已提交
414
{
415 416
    libxlDriverConfigPtr cfg;

J
Jim Fehlig 已提交
417 418 419
    if (!libxl_driver)
        return 0;

420 421
    cfg = libxlDriverConfigGet(libxl_driver);

422
    virDomainObjListLoadAllConfigs(libxl_driver->domains,
423 424
                                   cfg->configDir,
                                   cfg->autostartDir,
425
                                   1,
426
                                   cfg->caps,
427 428
                                   libxl_driver->xmlopt,
                                   1 << VIR_DOMAIN_VIRT_XEN,
429 430
                                   NULL, libxl_driver);

431 432
    virDomainObjListForEach(libxl_driver->domains, libxlAutostartDomain,
                            libxl_driver);
433

434
    virObjectUnref(cfg);
J
Jim Fehlig 已提交
435 436 437 438 439
    return 0;
}


static virDrvOpenStatus
440 441 442
libxlConnectOpen(virConnectPtr conn,
                 virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                 unsigned int flags)
J
Jim Fehlig 已提交
443
{
E
Eric Blake 已提交
444 445
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

J
Jim Fehlig 已提交
446 447 448 449
    if (conn->uri == NULL) {
        if (libxl_driver == NULL)
            return VIR_DRV_OPEN_DECLINED;

450
        if (!(conn->uri = virURIParse("xen:///")))
J
Jim Fehlig 已提交
451 452 453 454 455 456 457 458 459 460 461 462
            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) {
463 464
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("libxenlight state driver is not active"));
J
Jim Fehlig 已提交
465 466 467 468 469 470 471 472
            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")) {
473 474 475
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unexpected Xen URI path '%s', try xen:///"),
                           NULLSTR(conn->uri->path));
J
Jim Fehlig 已提交
476 477 478 479
            return VIR_DRV_OPEN_ERROR;
        }
    }

480 481 482
    if (virConnectOpenEnsureACL(conn) < 0)
        return VIR_DRV_OPEN_ERROR;

J
Jim Fehlig 已提交
483 484 485 486 487 488
    conn->privateData = libxl_driver;

    return VIR_DRV_OPEN_SUCCESS;
};

static int
489
libxlConnectClose(virConnectPtr conn ATTRIBUTE_UNUSED)
J
Jim Fehlig 已提交
490 491 492 493 494 495
{
    conn->privateData = NULL;
    return 0;
}

static const char *
496
libxlConnectGetType(virConnectPtr conn)
J
Jim Fehlig 已提交
497
{
498 499 500
    if (virConnectGetTypeEnsureACL(conn) < 0)
        return NULL;

J
Jim Fehlig 已提交
501
    return "Xen";
J
Jim Fehlig 已提交
502 503 504
}

static int
505
libxlConnectGetVersion(virConnectPtr conn, unsigned long *version)
J
Jim Fehlig 已提交
506 507
{
    libxlDriverPrivatePtr driver = conn->privateData;
508
    libxlDriverConfigPtr cfg;
J
Jim Fehlig 已提交
509

510 511 512
    if (virConnectGetVersionEnsureACL(conn) < 0)
        return 0;

513 514 515
    cfg = libxlDriverConfigGet(driver);
    *version = cfg->version;
    virObjectUnref(cfg);
J
Jim Fehlig 已提交
516 517 518
    return 0;
}

519

520
static char *libxlConnectGetHostname(virConnectPtr conn)
521
{
522 523 524
    if (virConnectGetHostnameEnsureACL(conn) < 0)
        return NULL;

525 526 527
    return virGetHostname();
}

528 529 530 531 532 533 534 535
static char *
libxlConnectGetSysinfo(virConnectPtr conn, unsigned int flags)
{
    libxlDriverPrivatePtr driver = conn->privateData;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    virCheckFlags(0, NULL);

536 537 538
    if (virConnectGetSysinfoEnsureACL(conn) < 0)
        return NULL;

539 540 541 542 543 544 545 546
    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;
547
    if (virBufferCheckError(&buf) < 0)
548 549 550
        return NULL;
    return virBufferContentAndReset(&buf);
}
551

J
Jim Fehlig 已提交
552
static int
553
libxlConnectGetMaxVcpus(virConnectPtr conn, const char *type ATTRIBUTE_UNUSED)
J
Jim Fehlig 已提交
554 555 556
{
    int ret;
    libxlDriverPrivatePtr driver = conn->privateData;
557
    libxlDriverConfigPtr cfg;
J
Jim Fehlig 已提交
558

559 560 561
    if (virConnectGetMaxVcpusEnsureACL(conn) < 0)
        return -1;

562 563
    cfg = libxlDriverConfigGet(driver);
    ret = libxl_get_max_cpus(cfg->ctx);
564 565 566 567 568
    /* 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)
569
        ret = -1;
J
Jim Fehlig 已提交
570

571
    virObjectUnref(cfg);
J
Jim Fehlig 已提交
572 573 574 575 576 577
    return ret;
}

static int
libxlNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info)
{
578 579 580
    if (virNodeGetInfoEnsureACL(conn) < 0)
        return -1;

581
    return libxlDriverNodeGetInfo(conn->privateData, info);
J
Jim Fehlig 已提交
582 583 584
}

static char *
585
libxlConnectGetCapabilities(virConnectPtr conn)
J
Jim Fehlig 已提交
586 587 588
{
    libxlDriverPrivatePtr driver = conn->privateData;
    char *xml;
589
    libxlDriverConfigPtr cfg;
J
Jim Fehlig 已提交
590

591 592 593
    if (virConnectGetCapabilitiesEnsureACL(conn) < 0)
        return NULL;

594
    cfg = libxlDriverConfigGet(driver);
595
    xml = virCapabilitiesFormatXML(cfg->caps);
J
Jim Fehlig 已提交
596

597
    virObjectUnref(cfg);
J
Jim Fehlig 已提交
598 599 600 601
    return xml;
}

static int
602
libxlConnectListDomains(virConnectPtr conn, int *ids, int nids)
J
Jim Fehlig 已提交
603 604 605 606
{
    libxlDriverPrivatePtr driver = conn->privateData;
    int n;

607 608 609
    if (virConnectListDomainsEnsureACL(conn) < 0)
        return -1;

610 611
    n = virDomainObjListGetActiveIDs(driver->domains, ids, nids,
                                     virConnectListDomainsCheckACL, conn);
J
Jim Fehlig 已提交
612 613 614 615 616

    return n;
}

static int
617
libxlConnectNumOfDomains(virConnectPtr conn)
J
Jim Fehlig 已提交
618 619 620 621
{
    libxlDriverPrivatePtr driver = conn->privateData;
    int n;

622 623 624
    if (virConnectNumOfDomainsEnsureACL(conn) < 0)
        return -1;

625 626
    n = virDomainObjListNumOfDomains(driver->domains, true,
                                     virConnectNumOfDomainsCheckACL, conn);
J
Jim Fehlig 已提交
627 628 629 630 631 632 633 634 635 636 637 638

    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;
639
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
J
Jim Fehlig 已提交
640 641 642

    virCheckFlags(VIR_DOMAIN_START_PAUSED, NULL);

643
    if (!(def = virDomainDefParseString(xml, cfg->caps, driver->xmlopt,
644
                                        1 << VIR_DOMAIN_VIRT_XEN,
J
Jim Fehlig 已提交
645 646 647
                                        VIR_DOMAIN_XML_INACTIVE)))
        goto cleanup;

648 649 650
    if (virDomainCreateXMLEnsureACL(conn, def) < 0)
        goto cleanup;

651
    if (!(vm = virDomainObjListAdd(driver->domains, def,
652
                                   driver->xmlopt,
653 654
                                   VIR_DOMAIN_OBJ_LIST_ADD_CHECK_LIVE,
                                   NULL)))
J
Jim Fehlig 已提交
655 656 657
        goto cleanup;
    def = NULL;

658
    if (libxlDomainStart(driver, vm, (flags & VIR_DOMAIN_START_PAUSED) != 0,
659
                     -1) < 0) {
660
        virDomainObjListRemove(driver->domains, vm);
J
Jim Fehlig 已提交
661 662 663 664 665 666 667 668
        vm = NULL;
        goto cleanup;
    }

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

669
 cleanup:
J
Jim Fehlig 已提交
670 671
    virDomainDefFree(def);
    if (vm)
672
        virObjectUnlock(vm);
673
    virObjectUnref(cfg);
J
Jim Fehlig 已提交
674 675 676 677 678 679 680 681 682 683
    return dom;
}

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

684
    vm = virDomainObjListFindByID(driver->domains, id);
J
Jim Fehlig 已提交
685
    if (!vm) {
686
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
J
Jim Fehlig 已提交
687 688 689
        goto cleanup;
    }

690 691 692
    if (virDomainLookupByIDEnsureACL(conn, vm->def) < 0)
        goto cleanup;

J
Jim Fehlig 已提交
693 694 695 696
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom)
        dom->id = vm->def->id;

697
 cleanup:
J
Jim Fehlig 已提交
698
    if (vm)
699
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
700 701 702 703 704 705 706 707 708 709
    return dom;
}

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

710
    vm = virDomainObjListFindByUUID(driver->domains, uuid);
J
Jim Fehlig 已提交
711
    if (!vm) {
712
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
J
Jim Fehlig 已提交
713 714 715
        goto cleanup;
    }

716 717 718
    if (virDomainLookupByUUIDEnsureACL(conn, vm->def) < 0)
        goto cleanup;

J
Jim Fehlig 已提交
719 720 721 722
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom)
        dom->id = vm->def->id;

723
 cleanup:
J
Jim Fehlig 已提交
724
    if (vm)
725
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
726 727 728 729 730 731 732 733 734 735
    return dom;
}

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

736
    vm = virDomainObjListFindByName(driver->domains, name);
J
Jim Fehlig 已提交
737
    if (!vm) {
738
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
J
Jim Fehlig 已提交
739 740 741
        goto cleanup;
    }

742 743 744
    if (virDomainLookupByNameEnsureACL(conn, vm->def) < 0)
        goto cleanup;

J
Jim Fehlig 已提交
745 746 747 748
    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom)
        dom->id = vm->def->id;

749
 cleanup:
J
Jim Fehlig 已提交
750
    if (vm)
751
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
752 753 754
    return dom;
}

755 756 757 758
static int
libxlDomainSuspend(virDomainPtr dom)
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
759
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
760 761
    virDomainObjPtr vm;
    libxlDomainObjPrivatePtr priv;
762
    virObjectEventPtr event = NULL;
763 764
    int ret = -1;

J
Jim Fehlig 已提交
765
    if (!(vm = libxlDomObjFromDomain(dom)))
766
        goto cleanup;
767 768 769 770

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

771 772 773
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

774
    if (!virDomainObjIsActive(vm)) {
775
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not running"));
776
        goto endjob;
777 778 779 780
    }

    priv = vm->privateData;

J
Jiri Denemark 已提交
781
    if (virDomainObjGetState(vm, NULL) != VIR_DOMAIN_PAUSED) {
782
        if (libxl_domain_pause(priv->ctx, vm->def->id) != 0) {
783 784
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Failed to suspend domain '%d' with libxenlight"),
785
                           vm->def->id);
786
            goto endjob;
787 788
        }

J
Jiri Denemark 已提交
789
        virDomainObjSetState(vm, VIR_DOMAIN_PAUSED, VIR_DOMAIN_PAUSED_USER);
790

791
        event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_SUSPENDED,
792 793 794
                                         VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
    }

795
    if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm) < 0)
796
        goto endjob;
797 798 799

    ret = 0;

800
 endjob:
801 802 803
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

804
 cleanup:
805
    if (vm)
806
        virObjectUnlock(vm);
807
    if (event)
808
        libxlDomainEventQueue(driver, event);
809
    virObjectUnref(cfg);
810 811 812 813 814 815 816 817
    return ret;
}


static int
libxlDomainResume(virDomainPtr dom)
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
818
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
819 820
    virDomainObjPtr vm;
    libxlDomainObjPrivatePtr priv;
821
    virObjectEventPtr event = NULL;
822 823
    int ret = -1;

J
Jim Fehlig 已提交
824
    if (!(vm = libxlDomObjFromDomain(dom)))
825 826
        goto cleanup;

827 828 829
    if (virDomainResumeEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

830 831 832
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

833
    if (!virDomainObjIsActive(vm)) {
834
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not running"));
835
        goto endjob;
836 837 838 839
    }

    priv = vm->privateData;

J
Jiri Denemark 已提交
840
    if (virDomainObjGetState(vm, NULL) == VIR_DOMAIN_PAUSED) {
841
        if (libxl_domain_unpause(priv->ctx, vm->def->id) != 0) {
842 843
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Failed to resume domain '%d' with libxenlight"),
844
                           vm->def->id);
845
            goto endjob;
846 847
        }

J
Jiri Denemark 已提交
848 849
        virDomainObjSetState(vm, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_UNPAUSED);
850

851
        event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_RESUMED,
852 853 854
                                         VIR_DOMAIN_EVENT_RESUMED_UNPAUSED);
    }

855
    if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm) < 0)
856
        goto endjob;
857 858 859

    ret = 0;

860
 endjob:
861 862 863
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

864
 cleanup:
865
    if (vm)
866
        virObjectUnlock(vm);
867
    if (event)
868
        libxlDomainEventQueue(driver, event);
869
    virObjectUnref(cfg);
870 871 872
    return ret;
}

J
Jim Fehlig 已提交
873
static int
874
libxlDomainShutdownFlags(virDomainPtr dom, unsigned int flags)
J
Jim Fehlig 已提交
875 876 877 878 879
{
    virDomainObjPtr vm;
    int ret = -1;
    libxlDomainObjPrivatePtr priv;

880 881 882 883 884
    virCheckFlags(VIR_DOMAIN_SHUTDOWN_ACPI_POWER_BTN |
                  VIR_DOMAIN_SHUTDOWN_PARAVIRT, -1);
    if (flags == 0)
        flags = VIR_DOMAIN_SHUTDOWN_PARAVIRT |
            VIR_DOMAIN_SHUTDOWN_ACPI_POWER_BTN;
885

J
Jim Fehlig 已提交
886
    if (!(vm = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
887 888
        goto cleanup;

889
    if (virDomainShutdownFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
890 891
        goto cleanup;

J
Jim Fehlig 已提交
892
    if (!virDomainObjIsActive(vm)) {
893 894
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("Domain is not running"));
J
Jim Fehlig 已提交
895 896 897 898
        goto cleanup;
    }

    priv = vm->privateData;
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
    if (flags & VIR_DOMAIN_SHUTDOWN_PARAVIRT) {
        ret = libxl_domain_shutdown(priv->ctx, vm->def->id);
        if (ret == 0)
            goto cleanup;

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

    if (flags & VIR_DOMAIN_SHUTDOWN_ACPI_POWER_BTN) {
        ret = libxl_send_trigger(priv->ctx, vm->def->id,
                                 LIBXL_TRIGGER_POWER, 0);
        if (ret == 0)
            goto cleanup;

920 921
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to shutdown domain '%d' with libxenlight"),
922
                       vm->def->id);
923
        ret = -1;
J
Jim Fehlig 已提交
924 925
    }

926
 cleanup:
J
Jim Fehlig 已提交
927
    if (vm)
928
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
929 930 931
    return ret;
}

932 933 934 935 936 937 938
static int
libxlDomainShutdown(virDomainPtr dom)
{
    return libxlDomainShutdownFlags(dom, 0);
}


J
Jim Fehlig 已提交
939
static int
E
Eric Blake 已提交
940
libxlDomainReboot(virDomainPtr dom, unsigned int flags)
J
Jim Fehlig 已提交
941 942 943 944 945
{
    virDomainObjPtr vm;
    int ret = -1;
    libxlDomainObjPrivatePtr priv;

J
Jim Fehlig 已提交
946 947 948
    virCheckFlags(VIR_DOMAIN_REBOOT_PARAVIRT, -1);
    if (flags == 0)
        flags = VIR_DOMAIN_REBOOT_PARAVIRT;
E
Eric Blake 已提交
949

J
Jim Fehlig 已提交
950
    if (!(vm = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
951 952
        goto cleanup;

953
    if (virDomainRebootEnsureACL(dom->conn, vm->def, flags) < 0)
954 955
        goto cleanup;

J
Jim Fehlig 已提交
956
    if (!virDomainObjIsActive(vm)) {
957 958
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("Domain is not running"));
J
Jim Fehlig 已提交
959 960 961 962
        goto cleanup;
    }

    priv = vm->privateData;
J
Jim Fehlig 已提交
963 964 965 966 967
    if (flags & VIR_DOMAIN_REBOOT_PARAVIRT) {
        ret = libxl_domain_reboot(priv->ctx, vm->def->id);
        if (ret == 0)
            goto cleanup;

968 969
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to reboot domain '%d' with libxenlight"),
970
                       vm->def->id);
J
Jim Fehlig 已提交
971
        ret = -1;
J
Jim Fehlig 已提交
972 973
    }

974
 cleanup:
J
Jim Fehlig 已提交
975
    if (vm)
976
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
977 978 979 980
    return ret;
}

static int
981 982
libxlDomainDestroyFlags(virDomainPtr dom,
                        unsigned int flags)
J
Jim Fehlig 已提交
983 984 985 986
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
987
    virObjectEventPtr event = NULL;
J
Jim Fehlig 已提交
988
    libxlDomainObjPrivatePtr priv;
J
Jim Fehlig 已提交
989

990 991
    virCheckFlags(0, -1);

J
Jim Fehlig 已提交
992
    if (!(vm = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
993 994
        goto cleanup;

995 996 997
    if (virDomainDestroyFlagsEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

J
Jim Fehlig 已提交
998
    if (!virDomainObjIsActive(vm)) {
999 1000
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("Domain is not running"));
J
Jim Fehlig 已提交
1001 1002 1003
        goto cleanup;
    }

1004
    event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_STOPPED,
1005 1006
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);

J
Jim Fehlig 已提交
1007 1008
    priv = vm->privateData;
    if (libxl_domain_destroy(priv->ctx, vm->def->id, NULL) < 0) {
1009
        virReportError(VIR_ERR_INTERNAL_ERROR,
1010
                       _("Failed to destroy domain '%d'"), vm->def->id);
J
Jim Fehlig 已提交
1011 1012 1013
        goto cleanup;
    }

1014
    if (libxlDomainCleanupJob(driver, vm, VIR_DOMAIN_SHUTOFF_DESTROYED)) {
1015 1016 1017 1018
        if (!vm->persistent) {
            virDomainObjListRemove(driver->domains, vm);
            vm = NULL;
        }
J
Jim Fehlig 已提交
1019 1020 1021 1022
    }

    ret = 0;

1023
 cleanup:
J
Jim Fehlig 已提交
1024
    if (vm)
1025
        virObjectUnlock(vm);
1026 1027
    if (event)
        libxlDomainEventQueue(driver, event);
J
Jim Fehlig 已提交
1028 1029 1030
    return ret;
}

1031 1032 1033 1034 1035 1036
static int
libxlDomainDestroy(virDomainPtr dom)
{
    return libxlDomainDestroyFlags(dom, 0);
}

1037 1038 1039 1040 1041 1042
static char *
libxlDomainGetOSType(virDomainPtr dom)
{
    virDomainObjPtr vm;
    char *type = NULL;

J
Jim Fehlig 已提交
1043
    if (!(vm = libxlDomObjFromDomain(dom)))
1044 1045
        goto cleanup;

1046 1047 1048 1049 1050
    if (virDomainGetOSTypeEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

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

1052
 cleanup:
1053
    if (vm)
1054
        virObjectUnlock(vm);
1055 1056 1057
    return type;
}

1058
static unsigned long long
1059 1060 1061
libxlDomainGetMaxMemory(virDomainPtr dom)
{
    virDomainObjPtr vm;
1062
    unsigned long long ret = 0;
1063

J
Jim Fehlig 已提交
1064
    if (!(vm = libxlDomObjFromDomain(dom)))
1065
        goto cleanup;
1066 1067 1068 1069

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

1070 1071
    ret = vm->def->mem.max_balloon;

1072
 cleanup:
1073
    if (vm)
1074
        virObjectUnlock(vm);
1075 1076 1077 1078
    return ret;
}

static int
1079
libxlDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem,
1080 1081 1082
                          unsigned int flags)
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
1083
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
1084 1085
    libxlDomainObjPrivatePtr priv;
    virDomainObjPtr vm;
1086 1087
    virDomainDefPtr persistentDef = NULL;
    bool isActive;
1088 1089 1090
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_MEM_LIVE |
1091 1092
                  VIR_DOMAIN_MEM_CONFIG |
                  VIR_DOMAIN_MEM_MAXIMUM, -1);
1093

J
Jim Fehlig 已提交
1094
    if (!(vm = libxlDomObjFromDomain(dom)))
1095 1096
        goto cleanup;

1097 1098 1099
    if (virDomainSetMemoryFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
        goto cleanup;

1100 1101 1102
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
    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;
1116 1117
    }

1118
    if (!isActive && (flags & VIR_DOMAIN_MEM_LIVE)) {
1119 1120
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("cannot set memory on an inactive domain"));
1121
        goto endjob;
1122 1123 1124 1125
    }

    if (flags & VIR_DOMAIN_MEM_CONFIG) {
        if (!vm->persistent) {
1126 1127
            virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                           _("cannot change persistent config of a transient domain"));
1128
            goto endjob;
1129
        }
1130
        if (!(persistentDef = virDomainObjGetPersistentDef(cfg->caps,
1131
                                                           driver->xmlopt,
1132
                                                           vm)))
1133
            goto endjob;
1134 1135
    }

1136 1137
    if (flags & VIR_DOMAIN_MEM_MAXIMUM) {
        /* resize the maximum memory */
1138

1139 1140
        if (flags & VIR_DOMAIN_MEM_LIVE) {
            priv = vm->privateData;
1141
            if (libxl_domain_setmaxmem(priv->ctx, vm->def->id, newmem) < 0) {
1142 1143
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Failed to set maximum memory for domain '%d'"
1144
                                 " with libxenlight"), vm->def->id);
1145
                goto endjob;
1146 1147 1148 1149 1150 1151 1152 1153 1154
            }
        }

        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;
1155
            ret = virDomainSaveConfig(cfg->configDir, persistentDef);
1156
            goto endjob;
1157 1158
        }

1159 1160
    } else {
        /* resize the current memory */
1161

1162
        if (newmem > vm->def->mem.max_balloon) {
1163 1164
            virReportError(VIR_ERR_INVALID_ARG, "%s",
                           _("cannot set memory higher than max memory"));
1165
            goto endjob;
1166 1167 1168
        }

        if (flags & VIR_DOMAIN_MEM_LIVE) {
1169 1170
            int res;

1171
            priv = vm->privateData;
1172 1173
            /* Unlock virDomainObj while ballooning memory */
            virObjectUnlock(vm);
1174
            res = libxl_set_memory_target(priv->ctx, vm->def->id, newmem, 0,
1175 1176 1177
                                          /* force */ 1);
            virObjectLock(vm);
            if (res < 0) {
1178 1179
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Failed to set memory for domain '%d'"
1180
                                 " with libxenlight"), vm->def->id);
1181
                goto endjob;
1182 1183 1184 1185 1186 1187
            }
        }

        if (flags & VIR_DOMAIN_MEM_CONFIG) {
            sa_assert(persistentDef);
            persistentDef->mem.cur_balloon = newmem;
1188
            ret = virDomainSaveConfig(cfg->configDir, persistentDef);
1189
            goto endjob;
1190
        }
1191 1192
    }

1193 1194
    ret = 0;

1195
 endjob:
1196 1197 1198
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

1199
 cleanup:
1200
    if (vm)
1201
        virObjectUnlock(vm);
1202
    virObjectUnref(cfg);
1203 1204 1205 1206 1207 1208 1209 1210 1211
    return ret;
}

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

1212 1213 1214 1215 1216 1217
static int
libxlDomainSetMaxMemory(virDomainPtr dom, unsigned long memory)
{
    return libxlDomainSetMemoryFlags(dom, memory, VIR_DOMAIN_MEM_MAXIMUM);
}

J
Jim Fehlig 已提交
1218 1219 1220 1221
static int
libxlDomainGetInfo(virDomainPtr dom, virDomainInfoPtr info)
{
    virDomainObjPtr vm;
1222
    libxl_dominfo d_info;
1223
    libxlDomainObjPrivatePtr priv;
J
Jim Fehlig 已提交
1224 1225
    int ret = -1;

J
Jim Fehlig 已提交
1226
    if (!(vm = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
1227 1228
        goto cleanup;

1229 1230 1231
    if (virDomainGetInfoEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

1232
    priv = vm->privateData;
1233 1234 1235
    if (!virDomainObjIsActive(vm)) {
        info->cpuTime = 0;
        info->memory = vm->def->mem.cur_balloon;
1236
        info->maxMem = vm->def->mem.max_balloon;
1237
    } else {
1238
        if (libxl_domain_info(priv->ctx, &d_info, vm->def->id) != 0) {
1239
            virReportError(VIR_ERR_INTERNAL_ERROR,
1240 1241
                           _("libxl_domain_info failed for domain '%d'"),
                           vm->def->id);
1242 1243 1244 1245
            goto cleanup;
        }
        info->cpuTime = d_info.cpu_time;
        info->memory = d_info.current_memkb;
1246
        info->maxMem = d_info.max_memkb;
1247 1248
    }

J
Jiri Denemark 已提交
1249
    info->state = virDomainObjGetState(vm, NULL);
J
Jim Fehlig 已提交
1250 1251 1252
    info->nrVirtCpu = vm->def->vcpus;
    ret = 0;

1253
 cleanup:
J
Jim Fehlig 已提交
1254
    if (vm)
1255
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
1256 1257 1258
    return ret;
}

1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
static int
libxlDomainGetState(virDomainPtr dom,
                    int *state,
                    int *reason,
                    unsigned int flags)
{
    virDomainObjPtr vm;
    int ret = -1;

    virCheckFlags(0, -1);

J
Jim Fehlig 已提交
1270
    if (!(vm = libxlDomObjFromDomain(dom)))
1271 1272
        goto cleanup;

1273 1274 1275
    if (virDomainGetStateEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

J
Jiri Denemark 已提交
1276
    *state = virDomainObjGetState(vm, reason);
1277 1278
    ret = 0;

1279
 cleanup:
1280
    if (vm)
1281
        virObjectUnlock(vm);
1282 1283 1284
    return ret;
}

1285 1286 1287
/*
 * virDomainObjPtr must be locked on invocation
 */
1288
static int
1289 1290
libxlDoDomainSave(libxlDriverPrivatePtr driver, virDomainObjPtr vm,
                  const char *to)
1291
{
1292
    libxlDomainObjPrivatePtr priv = vm->privateData;
1293
    libxlSavefileHeader hdr;
1294
    virObjectEventPtr event = NULL;
1295 1296
    char *xml = NULL;
    uint32_t xml_len;
1297
    int fd = -1;
1298 1299 1300
    int ret = -1;

    if (virDomainObjGetState(vm, NULL) == VIR_DOMAIN_PAUSED) {
1301 1302 1303
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Domain '%d' has to be running because libxenlight will"
                         " suspend it"), vm->def->id);
1304 1305 1306 1307
        goto cleanup;
    }

    if ((fd = virFileOpenAs(to, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR,
L
Laine Stump 已提交
1308
                            -1, -1, 0)) < 0) {
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
        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)) {
1324 1325
        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                       _("Failed to write save file header"));
1326 1327 1328 1329
        goto cleanup;
    }

    if (safewrite(fd, xml, xml_len) != xml_len) {
1330 1331
        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                       _("Failed to write xml description"));
1332 1333 1334
        goto cleanup;
    }

1335 1336 1337 1338 1339 1340
    /* Unlock virDomainObj while saving domain */
    virObjectUnlock(vm);
    ret = libxl_domain_suspend(priv->ctx, vm->def->id, fd, 0, NULL);
    virObjectLock(vm);

    if (ret != 0) {
1341 1342 1343
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to save domain '%d' with libxenlight"),
                       vm->def->id);
1344
        ret = -1;
1345 1346 1347
        goto cleanup;
    }

1348
    event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_STOPPED,
1349 1350
                                         VIR_DOMAIN_EVENT_STOPPED_SAVED);

J
Jim Fehlig 已提交
1351
    if (libxl_domain_destroy(priv->ctx, vm->def->id, NULL) < 0) {
1352 1353
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to destroy domain '%d'"), vm->def->id);
1354 1355 1356
        goto cleanup;
    }

1357
    libxlDomainCleanup(driver, vm, VIR_DOMAIN_SHUTOFF_SAVED);
1358
    vm->hasManagedSave = true;
1359 1360
    ret = 0;

1361
 cleanup:
1362 1363 1364 1365 1366 1367 1368 1369 1370
    VIR_FREE(xml);
    if (VIR_CLOSE(fd) < 0)
        virReportSystemError(errno, "%s", _("cannot close file"));
    if (event)
        libxlDomainEventQueue(driver, event);
    return ret;
}

static int
1371 1372
libxlDomainSaveFlags(virDomainPtr dom, const char *to, const char *dxml,
                     unsigned int flags)
1373
{
1374 1375
    libxlDriverPrivatePtr driver = dom->conn->privateData;
    virDomainObjPtr vm;
1376
    int ret = -1;
1377
    bool remove_dom = false;
1378

1379 1380 1381 1382 1383
#ifdef LIBXL_HAVE_NO_SUSPEND_RESUME
    virReportUnsupportedError();
    return -1;
#endif

1384 1385
    virCheckFlags(0, -1);
    if (dxml) {
1386 1387
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
1388 1389 1390
        return -1;
    }

J
Jim Fehlig 已提交
1391
    if (!(vm = libxlDomObjFromDomain(dom)))
1392 1393
        goto cleanup;

1394 1395 1396
    if (virDomainSaveFlagsEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

1397 1398 1399
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

1400
    if (!virDomainObjIsActive(vm)) {
1401
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not running"));
1402
        goto endjob;
1403 1404
    }

1405
    if (libxlDoDomainSave(driver, vm, to) < 0)
1406
        goto endjob;
1407

1408 1409
    if (!vm->persistent)
        remove_dom = true;
1410 1411

    ret = 0;
1412

1413
 endjob:
1414 1415 1416
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

1417
 cleanup:
1418 1419 1420 1421
    if (remove_dom && vm) {
        virDomainObjListRemove(driver->domains, vm);
        vm = NULL;
    }
1422
    if (vm)
1423
        virObjectUnlock(vm);
1424 1425
    return ret;
}
1426

1427
static int
1428 1429 1430 1431 1432 1433 1434 1435
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)
1436 1437
{
    libxlDriverPrivatePtr driver = conn->privateData;
1438
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
1439 1440 1441 1442 1443
    virDomainObjPtr vm = NULL;
    virDomainDefPtr def = NULL;
    libxlSavefileHeader hdr;
    int fd = -1;
    int ret = -1;
1444

1445 1446 1447 1448 1449
#ifdef LIBXL_HAVE_NO_SUSPEND_RESUME
    virReportUnsupportedError();
    return -1;
#endif

1450
    virCheckFlags(VIR_DOMAIN_SAVE_PAUSED, -1);
1451
    if (dxml) {
1452 1453
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
1454 1455 1456
        return -1;
    }

1457
    fd = libxlDomainSaveImageOpen(driver, cfg, from, &def, &hdr);
1458
    if (fd < 0)
1459
        goto cleanup_unlock;
1460

1461
    if (virDomainRestoreFlagsEnsureACL(conn, def) < 0)
1462
        goto cleanup_unlock;
1463

1464
    if (!(vm = virDomainObjListAdd(driver->domains, def,
1465
                                   driver->xmlopt,
1466 1467 1468
                                   VIR_DOMAIN_OBJ_LIST_ADD_LIVE |
                                   VIR_DOMAIN_OBJ_LIST_ADD_CHECK_LIVE,
                                   NULL)))
1469
        goto cleanup_unlock;
1470 1471 1472

    def = NULL;

1473
    ret = libxlDomainStart(driver, vm, (flags & VIR_DOMAIN_SAVE_PAUSED) != 0, fd);
1474
    if (ret < 0 && !vm->persistent) {
1475
        virDomainObjListRemove(driver->domains, vm);
1476 1477 1478
        vm = NULL;
    }

1479
 cleanup:
1480 1481
    if (VIR_CLOSE(fd) < 0)
        virReportSystemError(errno, "%s", _("cannot close file"));
1482 1483
    virDomainDefFree(def);
    if (vm)
1484
        virObjectUnlock(vm);
1485
    virObjectUnref(cfg);
1486
    return ret;
1487

1488
 cleanup_unlock:
1489 1490
    libxlDriverUnlock(driver);
    goto cleanup;
1491 1492
}

1493 1494 1495 1496 1497 1498
static int
libxlDomainRestore(virConnectPtr conn, const char *from)
{
    return libxlDomainRestoreFlags(conn, from, NULL, 0);
}

1499
static int
1500
libxlDomainCoreDump(virDomainPtr dom, const char *to, unsigned int flags)
1501 1502 1503 1504
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
    libxlDomainObjPrivatePtr priv;
    virDomainObjPtr vm;
1505
    virObjectEventPtr event = NULL;
1506
    bool remove_dom = false;
1507 1508 1509 1510 1511
    bool paused = false;
    int ret = -1;

    virCheckFlags(VIR_DUMP_LIVE | VIR_DUMP_CRASH, -1);

J
Jim Fehlig 已提交
1512
    if (!(vm = libxlDomObjFromDomain(dom)))
1513 1514
        goto cleanup;

1515 1516 1517
    if (virDomainCoreDumpEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

1518 1519 1520
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

1521
    if (!virDomainObjIsActive(vm)) {
1522
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not running"));
1523
        goto endjob;
1524 1525 1526 1527 1528 1529
    }

    priv = vm->privateData;

    if (!(flags & VIR_DUMP_LIVE) &&
        virDomainObjGetState(vm, NULL) == VIR_DOMAIN_RUNNING) {
1530
        if (libxl_domain_pause(priv->ctx, vm->def->id) != 0) {
1531 1532 1533
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Before dumping core, failed to suspend domain '%d'"
                             " with libxenlight"),
1534
                           vm->def->id);
1535
            goto endjob;
1536 1537 1538 1539 1540
        }
        virDomainObjSetState(vm, VIR_DOMAIN_PAUSED, VIR_DOMAIN_PAUSED_DUMP);
        paused = true;
    }

1541 1542
    /* Unlock virDomainObj while dumping core */
    virObjectUnlock(vm);
1543
    ret = libxl_domain_core_dump(priv->ctx, vm->def->id, to, NULL);
1544 1545
    virObjectLock(vm);
    if (ret != 0) {
1546 1547
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to dump core of domain '%d' with libxenlight"),
1548
                       vm->def->id);
1549 1550
        ret = -1;
        goto unpause;
1551 1552 1553
    }

    if (flags & VIR_DUMP_CRASH) {
1554
        if (libxl_domain_destroy(priv->ctx, vm->def->id, NULL) < 0) {
1555
            virReportError(VIR_ERR_INTERNAL_ERROR,
1556
                           _("Failed to destroy domain '%d'"), vm->def->id);
1557
            goto unpause;
1558 1559
        }

1560
        libxlDomainCleanup(driver, vm, VIR_DOMAIN_SHUTOFF_CRASHED);
1561
        event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_STOPPED,
1562
                                         VIR_DOMAIN_EVENT_STOPPED_CRASHED);
1563 1564
        if (!vm->persistent)
            remove_dom = true;
1565 1566 1567 1568
    }

    ret = 0;

1569
 unpause:
1570
    if (virDomainObjIsActive(vm) && paused) {
1571
        if (libxl_domain_unpause(priv->ctx, vm->def->id) != 0) {
1572 1573
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("After dumping core, failed to resume domain '%d' with"
1574
                             " libxenlight"), vm->def->id);
1575 1576 1577 1578 1579
        } else {
            virDomainObjSetState(vm, VIR_DOMAIN_RUNNING,
                                 VIR_DOMAIN_RUNNING_UNPAUSED);
        }
    }
1580

1581
 endjob:
1582 1583 1584
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

1585
 cleanup:
1586 1587 1588 1589
    if (remove_dom && vm) {
        virDomainObjListRemove(driver->domains, vm);
        vm = NULL;
    }
1590
    if (vm)
1591
        virObjectUnlock(vm);
1592
    if (event)
1593 1594 1595 1596
        libxlDomainEventQueue(driver, event);
    return ret;
}

1597 1598 1599 1600 1601 1602 1603
static int
libxlDomainManagedSave(virDomainPtr dom, unsigned int flags)
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
    virDomainObjPtr vm = NULL;
    char *name = NULL;
    int ret = -1;
1604
    bool remove_dom = false;
1605 1606 1607

    virCheckFlags(0, -1);

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

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

1614 1615 1616
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

1617
    if (!virDomainObjIsActive(vm)) {
1618
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not running"));
1619
        goto endjob;
1620
    }
1621
    if (!vm->persistent) {
1622 1623
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("cannot do managed save for transient domain"));
1624
        goto endjob;
1625
    }
1626 1627 1628

    name = libxlDomainManagedSavePath(driver, vm);
    if (name == NULL)
1629
        goto endjob;
1630 1631 1632

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

1633
    if (libxlDoDomainSave(driver, vm, name) < 0)
1634
        goto endjob;
1635

1636 1637
    if (!vm->persistent)
        remove_dom = true;
1638 1639

    ret = 0;
1640

1641
 endjob:
1642 1643 1644
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

1645
 cleanup:
1646 1647 1648 1649
    if (remove_dom && vm) {
        virDomainObjListRemove(driver->domains, vm);
        vm = NULL;
    }
1650
    if (vm)
1651
        virObjectUnlock(vm);
1652 1653 1654 1655
    VIR_FREE(name);
    return ret;
}

1656 1657
static int
libxlDomainManagedSaveLoad(virDomainObjPtr vm,
1658 1659 1660 1661
                           void *opaque)
{
    libxlDriverPrivatePtr driver = opaque;
    char *name;
1662
    int ret = -1;
1663

1664
    virObjectLock(vm);
1665 1666 1667 1668 1669 1670

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

    vm->hasManagedSave = virFileExists(name);

1671
    ret = 0;
1672
 cleanup:
1673
    virObjectUnlock(vm);
1674
    VIR_FREE(name);
1675
    return ret;
1676 1677
}

1678 1679 1680 1681 1682 1683 1684 1685
static int
libxlDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
{
    virDomainObjPtr vm = NULL;
    int ret = -1;

    virCheckFlags(0, -1);

J
Jim Fehlig 已提交
1686
    if (!(vm = libxlDomObjFromDomain(dom)))
1687 1688
        goto cleanup;

1689 1690 1691
    if (virDomainHasManagedSaveImageEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

1692
    ret = vm->hasManagedSave;
1693

1694
 cleanup:
1695
    if (vm)
1696
        virObjectUnlock(vm);
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
    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 已提交
1710
    if (!(vm = libxlDomObjFromDomain(dom)))
1711 1712
        goto cleanup;

1713 1714 1715
    if (virDomainManagedSaveRemoveEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

1716 1717 1718 1719 1720
    name = libxlDomainManagedSavePath(driver, vm);
    if (name == NULL)
        goto cleanup;

    ret = unlink(name);
1721
    vm->hasManagedSave = false;
1722

1723
 cleanup:
1724 1725
    VIR_FREE(name);
    if (vm)
1726
        virObjectUnlock(vm);
1727 1728 1729
    return ret;
}

1730 1731 1732 1733 1734
static int
libxlDomainSetVcpusFlags(virDomainPtr dom, unsigned int nvcpus,
                         unsigned int flags)
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
1735
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
1736 1737 1738
    libxlDomainObjPrivatePtr priv;
    virDomainDefPtr def;
    virDomainObjPtr vm;
J
Jim Fehlig 已提交
1739
    libxl_bitmap map;
1740 1741
    uint8_t *bitmask = NULL;
    unsigned int maplen;
1742 1743
    size_t i;
    unsigned int pos;
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755
    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)) {
1756 1757
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid flag combination: (0x%x)"), flags);
1758 1759 1760 1761
        return -1;
    }

    if (!nvcpus) {
1762
        virReportError(VIR_ERR_INVALID_ARG, "%s", _("nvcpus is zero"));
1763 1764 1765
        return -1;
    }

J
Jim Fehlig 已提交
1766
    if (!(vm = libxlDomObjFromDomain(dom)))
1767 1768
        goto cleanup;

1769 1770 1771
    if (virDomainSetVcpusFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
        goto cleanup;

1772 1773 1774
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

1775
    if (!virDomainObjIsActive(vm) && (flags & VIR_DOMAIN_VCPU_LIVE)) {
1776 1777
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("cannot set vcpus on an inactive domain"));
1778
        goto endjob;
1779 1780 1781
    }

    if (!vm->persistent && (flags & VIR_DOMAIN_VCPU_CONFIG)) {
1782 1783
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("cannot change persistent config of a transient domain"));
1784
        goto endjob;
1785 1786
    }

1787
    if ((max = libxlConnectGetMaxVcpus(dom->conn, NULL)) < 0) {
1788 1789
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("could not determine max vcpus for the domain"));
1790
        goto endjob;
1791 1792 1793 1794 1795 1796 1797
    }

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

    if (nvcpus > max) {
1798 1799 1800
        virReportError(VIR_ERR_INVALID_ARG,
                       _("requested vcpus is greater than max allowable"
                         " vcpus for the domain: %d > %d"), nvcpus, max);
1801
        goto endjob;
1802 1803 1804 1805
    }

    priv = vm->privateData;

1806
    if (!(def = virDomainObjGetPersistentDef(cfg->caps, driver->xmlopt, vm)))
1807
        goto endjob;
1808

E
Eric Blake 已提交
1809
    maplen = VIR_CPU_MAPLEN(nvcpus);
1810
    if (VIR_ALLOC_N(bitmask, maplen) < 0)
1811
        goto endjob;
1812 1813

    for (i = 0; i < nvcpus; ++i) {
E
Eric Blake 已提交
1814
        pos = i / 8;
1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
        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:
1833
        if (libxl_set_vcpuonline(priv->ctx, vm->def->id, &map) != 0) {
1834 1835
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Failed to set vcpus for domain '%d'"
1836
                             " with libxenlight"), vm->def->id);
1837
            goto endjob;
1838 1839 1840 1841
        }
        break;

    case VIR_DOMAIN_VCPU_LIVE | VIR_DOMAIN_VCPU_CONFIG:
1842
        if (libxl_set_vcpuonline(priv->ctx, vm->def->id, &map) != 0) {
1843 1844
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Failed to set vcpus for domain '%d'"
1845
                             " with libxenlight"), vm->def->id);
1846
            goto endjob;
1847 1848 1849 1850 1851 1852 1853 1854
        }
        def->vcpus = nvcpus;
        break;
    }

    ret = 0;

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

1857
 endjob:
1858 1859 1860
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

1861
 cleanup:
1862 1863
    VIR_FREE(bitmask);
     if (vm)
1864
        virObjectUnlock(vm);
1865
     virObjectUnref(cfg);
1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880
    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;
1881
    bool active;
1882 1883 1884 1885 1886

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

J
Jim Fehlig 已提交
1887
    if (!(vm = libxlDomObjFromDomain(dom)))
1888 1889
        goto cleanup;

1890
    if (virDomainGetVcpusFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
1891 1892
        goto cleanup;

1893 1894 1895 1896 1897 1898 1899 1900 1901
    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)) {
1902 1903
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid flag combination: (0x%x)"), flags);
1904 1905 1906
        return -1;
    }

1907
    if (flags & VIR_DOMAIN_VCPU_LIVE) {
1908
        if (!active) {
1909 1910
            virReportError(VIR_ERR_OPERATION_INVALID,
                           "%s", _("Domain is not running"));
1911 1912 1913 1914
            goto cleanup;
        }
        def = vm->def;
    } else {
1915
        if (!vm->persistent) {
1916 1917
            virReportError(VIR_ERR_OPERATION_INVALID,
                           "%s", _("domain is transient"));
1918 1919
            goto cleanup;
        }
1920 1921 1922 1923 1924
        def = vm->newDef ? vm->newDef : vm->def;
    }

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

1925
 cleanup:
1926
    if (vm)
1927
        virObjectUnlock(vm);
1928 1929 1930 1931
    return ret;
}

static int
1932 1933 1934
libxlDomainPinVcpuFlags(virDomainPtr dom, unsigned int vcpu,
                        unsigned char *cpumap, int maplen,
                        unsigned int flags)
1935 1936
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
1937
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
1938
    virDomainDefPtr targetDef = NULL;
1939
    virBitmapPtr pcpumap = NULL;
1940 1941
    virDomainObjPtr vm;
    int ret = -1;
1942 1943 1944

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);
1945

J
Jim Fehlig 已提交
1946
    if (!(vm = libxlDomObjFromDomain(dom)))
1947 1948
        goto cleanup;

1949
    if (virDomainPinVcpuFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
1950 1951
        goto cleanup;

1952 1953 1954
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

1955
    if ((flags & VIR_DOMAIN_AFFECT_LIVE) && !virDomainObjIsActive(vm)) {
1956
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
1957
                       _("domain is inactive"));
1958
        goto endjob;
1959 1960
    }

1961 1962
    if (virDomainLiveConfigHelperMethod(cfg->caps, driver->xmlopt, vm,
                                        &flags, &targetDef) < 0)
1963
        goto endjob;
1964 1965 1966 1967 1968 1969 1970 1971

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

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

1972 1973
    pcpumap = virBitmapNewData(cpumap, maplen);
    if (!pcpumap)
1974
        goto endjob;
1975

1976 1977 1978 1979 1980
    if (flags & VIR_DOMAIN_AFFECT_LIVE) {
        libxl_bitmap map = { .size = maplen, .map = cpumap };
        libxlDomainObjPrivatePtr priv;

        priv = vm->privateData;
1981
        if (libxl_set_vcpuaffinity(priv->ctx, vm->def->id, vcpu, &map) != 0) {
1982 1983 1984
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Failed to pin vcpu '%d' with libxenlight"),
                           vcpu);
1985
            goto endjob;
1986
        }
1987
    }
1988

1989 1990
    /* full bitmap means reset the settings (if any). */
    if (virBitmapIsAllSet(pcpumap)) {
1991
        virDomainVcpuPinDel(targetDef, vcpu);
1992
        goto done;
1993 1994
    }

1995 1996
    if (!targetDef->cputune.vcpupin) {
        if (VIR_ALLOC(targetDef->cputune.vcpupin) < 0)
1997
            goto endjob;
1998
        targetDef->cputune.nvcpupin = 0;
H
Hu Tao 已提交
1999
    }
2000 2001
    if (virDomainVcpuPinAdd(&targetDef->cputune.vcpupin,
                            &targetDef->cputune.nvcpupin,
H
Hu Tao 已提交
2002 2003 2004
                            cpumap,
                            maplen,
                            vcpu) < 0) {
2005 2006
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("failed to update or add vcpupin xml"));
2007
        goto endjob;
2008 2009
    }

2010
 done:
2011 2012
    ret = 0;

2013 2014 2015 2016 2017 2018
    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);
    }

2019
 endjob:
2020 2021 2022
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

2023
 cleanup:
2024
    if (vm)
2025
        virObjectUnlock(vm);
2026
    virBitmapFree(pcpumap);
2027
    virObjectUnref(cfg);
2028 2029 2030
    return ret;
}

2031 2032 2033 2034 2035 2036 2037 2038
static int
libxlDomainPinVcpu(virDomainPtr dom, unsigned int vcpu, unsigned char *cpumap,
                   int maplen)
{
    return libxlDomainPinVcpuFlags(dom, vcpu, cpumap, maplen,
                                   VIR_DOMAIN_AFFECT_LIVE);
}

2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
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;

2110
 cleanup:
2111 2112 2113 2114 2115
    if (vm)
        virObjectUnlock(vm);
    virObjectUnref(cfg);
    return ret;
}
2116 2117 2118 2119 2120 2121 2122 2123 2124 2125

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;
2126
    size_t i;
2127 2128
    unsigned char *cpumap;

J
Jim Fehlig 已提交
2129
    if (!(vm = libxlDomObjFromDomain(dom)))
2130 2131
        goto cleanup;

2132 2133 2134
    if (virDomainGetVcpusEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

2135
    if (!virDomainObjIsActive(vm)) {
2136
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not running"));
2137 2138 2139 2140
        goto cleanup;
    }

    priv = vm->privateData;
2141
    if ((vcpuinfo = libxl_list_vcpu(priv->ctx, vm->def->id, &maxcpu,
2142
                                    &hostcpus)) == NULL) {
2143 2144
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to list vcpus for domain '%d' with libxenlight"),
2145
                       vm->def->id);
2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167
        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 已提交
2168
        libxl_vcpuinfo_dispose(&vcpuinfo[i]);
2169 2170 2171 2172 2173
    }
    VIR_FREE(vcpuinfo);

    ret = maxinfo;

2174
 cleanup:
2175
    if (vm)
2176
        virObjectUnlock(vm);
2177 2178 2179
    return ret;
}

J
Jim Fehlig 已提交
2180
static char *
2181
libxlDomainGetXMLDesc(virDomainPtr dom, unsigned int flags)
J
Jim Fehlig 已提交
2182 2183 2184 2185
{
    virDomainObjPtr vm;
    char *ret = NULL;

2186 2187
    /* Flags checked by virDomainDefFormat */

J
Jim Fehlig 已提交
2188
    if (!(vm = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
2189 2190
        goto cleanup;

2191 2192 2193
    if (virDomainGetXMLDescEnsureACL(dom->conn, vm->def, flags) < 0)
        goto cleanup;

J
Jim Fehlig 已提交
2194 2195
    ret = virDomainDefFormat(vm->def, flags);

2196
 cleanup:
J
Jim Fehlig 已提交
2197
    if (vm)
2198
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
2199 2200 2201
    return ret;
}

2202
static char *
2203 2204 2205
libxlConnectDomainXMLFromNative(virConnectPtr conn,
                                const char *nativeFormat,
                                const char *nativeConfig,
2206
                                unsigned int flags)
2207 2208
{
    libxlDriverPrivatePtr driver = conn->privateData;
2209
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
2210 2211 2212 2213
    virDomainDefPtr def = NULL;
    virConfPtr conf = NULL;
    char *xml = NULL;

E
Eric Blake 已提交
2214 2215
    virCheckFlags(0, NULL);

2216 2217 2218
    if (virConnectDomainXMLFromNativeEnsureACL(conn) < 0)
        goto cleanup;

2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240
    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 {
2241 2242
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unsupported config type %s"), nativeFormat);
2243 2244 2245 2246 2247
        goto cleanup;
    }

    xml = virDomainDefFormat(def, VIR_DOMAIN_XML_INACTIVE);

2248
 cleanup:
2249 2250 2251
    virDomainDefFree(def);
    if (conf)
        virConfFree(conf);
2252
    virObjectUnref(cfg);
2253 2254 2255 2256 2257
    return xml;
}

#define MAX_CONFIG_SIZE (1024 * 65)
static char *
2258 2259 2260
libxlConnectDomainXMLToNative(virConnectPtr conn, const char * nativeFormat,
                              const char * domainXml,
                              unsigned int flags)
2261 2262
{
    libxlDriverPrivatePtr driver = conn->privateData;
2263
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
2264 2265 2266 2267 2268
    virDomainDefPtr def = NULL;
    virConfPtr conf = NULL;
    int len = MAX_CONFIG_SIZE;
    char *ret = NULL;

E
Eric Blake 已提交
2269 2270
    virCheckFlags(0, NULL);

2271 2272 2273
    if (virConnectDomainXMLToNativeEnsureACL(conn) < 0)
        goto cleanup;

2274
    if (STRNEQ(nativeFormat, LIBXL_CONFIG_FORMAT_XM)) {
2275 2276
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unsupported config type %s"), nativeFormat);
2277 2278 2279
        goto cleanup;
    }

2280
    if (!(def = virDomainDefParseString(domainXml,
2281
                                        cfg->caps, driver->xmlopt,
2282 2283
                                        1 << VIR_DOMAIN_VIRT_XEN,
                                        VIR_DOMAIN_XML_INACTIVE)))
2284 2285
        goto cleanup;

2286
    if (!(conf = xenFormatXM(conn, def, cfg->verInfo->xen_version_major)))
2287 2288
        goto cleanup;

2289
    if (VIR_ALLOC_N(ret, len) < 0)
2290 2291 2292 2293 2294 2295 2296
        goto cleanup;

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

2297
 cleanup:
2298 2299 2300
    virDomainDefFree(def);
    if (conf)
        virConfFree(conf);
2301
    virObjectUnref(cfg);
2302 2303 2304
    return ret;
}

J
Jim Fehlig 已提交
2305
static int
2306 2307
libxlConnectListDefinedDomains(virConnectPtr conn,
                               char **const names, int nnames)
J
Jim Fehlig 已提交
2308 2309 2310 2311
{
    libxlDriverPrivatePtr driver = conn->privateData;
    int n;

2312 2313 2314
    if (virConnectListDefinedDomainsEnsureACL(conn) < 0)
        return -1;

2315 2316
    n = virDomainObjListGetInactiveNames(driver->domains, names, nnames,
                                         virConnectListDefinedDomainsCheckACL, conn);
J
Jim Fehlig 已提交
2317 2318 2319 2320
    return n;
}

static int
2321
libxlConnectNumOfDefinedDomains(virConnectPtr conn)
J
Jim Fehlig 已提交
2322 2323 2324 2325
{
    libxlDriverPrivatePtr driver = conn->privateData;
    int n;

2326 2327 2328
    if (virConnectNumOfDefinedDomainsEnsureACL(conn) < 0)
        return -1;

2329
    n = virDomainObjListNumOfDomains(driver->domains, false,
2330 2331
                                     virConnectNumOfDefinedDomainsCheckACL,
                                     conn);
J
Jim Fehlig 已提交
2332 2333 2334 2335 2336
    return n;
}

static int
libxlDomainCreateWithFlags(virDomainPtr dom,
E
Eric Blake 已提交
2337
                           unsigned int flags)
J
Jim Fehlig 已提交
2338 2339 2340 2341 2342 2343 2344
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_START_PAUSED, -1);

J
Jim Fehlig 已提交
2345
    if (!(vm = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
2346 2347
        goto cleanup;

2348 2349 2350
    if (virDomainCreateWithFlagsEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

J
Jim Fehlig 已提交
2351
    if (virDomainObjIsActive(vm)) {
2352 2353
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("Domain is already running"));
J
Jim Fehlig 已提交
2354 2355 2356
        goto cleanup;
    }

2357
    ret = libxlDomainStart(driver, vm, (flags & VIR_DOMAIN_START_PAUSED) != 0, -1);
J
Jim Fehlig 已提交
2358

2359
 cleanup:
J
Jim Fehlig 已提交
2360
    if (vm)
2361
        virObjectUnlock(vm);
J
Jim Fehlig 已提交
2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374
    return ret;
}

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

static virDomainPtr
libxlDomainDefineXML(virConnectPtr conn, const char *xml)
{
    libxlDriverPrivatePtr driver = conn->privateData;
2375
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
J
Jim Fehlig 已提交
2376 2377 2378
    virDomainDefPtr def = NULL;
    virDomainObjPtr vm = NULL;
    virDomainPtr dom = NULL;
2379
    virObjectEventPtr event = NULL;
2380
    virDomainDefPtr oldDef = NULL;
J
Jim Fehlig 已提交
2381

2382
    /* Lock the driver until the virDomainObj is created and locked */
J
Jim Fehlig 已提交
2383
    libxlDriverLock(driver);
2384
    if (!(def = virDomainDefParseString(xml, cfg->caps, driver->xmlopt,
M
Matthias Bolte 已提交
2385
                                        1 << VIR_DOMAIN_VIRT_XEN,
J
Jim Fehlig 已提交
2386
                                        VIR_DOMAIN_XML_INACTIVE)))
2387
        goto cleanup_unlock;
J
Jim Fehlig 已提交
2388

2389
    if (virDomainDefineXMLEnsureACL(conn, def) < 0)
2390
        goto cleanup_unlock;
2391

2392
    if (!(vm = virDomainObjListAdd(driver->domains, def,
2393
                                   driver->xmlopt,
2394 2395
                                   0,
                                   &oldDef)))
2396 2397
        goto cleanup_unlock;

J
Jim Fehlig 已提交
2398 2399
    def = NULL;
    vm->persistent = 1;
2400
    libxlDriverUnlock(driver);
J
Jim Fehlig 已提交
2401

2402
    if (virDomainSaveConfig(cfg->configDir,
J
Jim Fehlig 已提交
2403
                            vm->newDef ? vm->newDef : vm->def) < 0) {
2404
        virDomainObjListRemove(driver->domains, vm);
J
Jim Fehlig 已提交
2405 2406 2407 2408 2409 2410 2411 2412
        vm = NULL;
        goto cleanup;
    }

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

2413
    event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_DEFINED,
2414
                                     !oldDef ?
2415 2416 2417
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED :
                                     VIR_DOMAIN_EVENT_DEFINED_UPDATED);

2418
 cleanup:
J
Jim Fehlig 已提交
2419
    virDomainDefFree(def);
2420
    virDomainDefFree(oldDef);
J
Jim Fehlig 已提交
2421
    if (vm)
2422
        virObjectUnlock(vm);
2423 2424
    if (event)
        libxlDomainEventQueue(driver, event);
2425
    virObjectUnref(cfg);
J
Jim Fehlig 已提交
2426
    return dom;
2427

2428
 cleanup_unlock:
2429 2430
    libxlDriverUnlock(driver);
    goto cleanup;
J
Jim Fehlig 已提交
2431 2432 2433
}

static int
2434 2435
libxlDomainUndefineFlags(virDomainPtr dom,
                         unsigned int flags)
J
Jim Fehlig 已提交
2436 2437
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
2438
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
J
Jim Fehlig 已提交
2439
    virDomainObjPtr vm;
2440
    virObjectEventPtr event = NULL;
2441
    char *name = NULL;
J
Jim Fehlig 已提交
2442 2443
    int ret = -1;

2444 2445
    virCheckFlags(VIR_DOMAIN_UNDEFINE_MANAGED_SAVE, -1);

J
Jim Fehlig 已提交
2446
    if (!(vm = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
2447 2448
        goto cleanup;

2449 2450 2451
    if (virDomainUndefineFlagsEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

J
Jim Fehlig 已提交
2452
    if (!vm->persistent) {
2453 2454
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("cannot undefine transient domain"));
J
Jim Fehlig 已提交
2455 2456 2457
        goto cleanup;
    }

2458 2459 2460 2461 2462 2463 2464
    name = libxlDomainManagedSavePath(driver, vm);
    if (name == NULL)
        goto cleanup;

    if (virFileExists(name)) {
        if (flags & VIR_DOMAIN_UNDEFINE_MANAGED_SAVE) {
            if (unlink(name) < 0) {
2465 2466
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("Failed to remove domain managed save image"));
2467 2468 2469
                goto cleanup;
            }
        } else {
2470 2471 2472
            virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                           _("Refusing to undefine while domain managed "
                             "save image exists"));
2473 2474 2475 2476
            goto cleanup;
        }
    }

2477
    if (virDomainDeleteConfig(cfg->configDir, cfg->autostartDir, vm) < 0)
J
Jim Fehlig 已提交
2478 2479
        goto cleanup;

2480
    event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_UNDEFINED,
2481 2482
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);

2483 2484 2485
    if (virDomainObjIsActive(vm)) {
        vm->persistent = 0;
    } else {
2486
        virDomainObjListRemove(driver->domains, vm);
2487 2488 2489
        vm = NULL;
    }

J
Jim Fehlig 已提交
2490 2491
    ret = 0;

2492
 cleanup:
2493
    VIR_FREE(name);
J
Jim Fehlig 已提交
2494
    if (vm)
2495
        virObjectUnlock(vm);
2496 2497
    if (event)
        libxlDomainEventQueue(driver, event);
2498
    virObjectUnref(cfg);
J
Jim Fehlig 已提交
2499 2500 2501
    return ret;
}

2502 2503 2504 2505 2506 2507
static int
libxlDomainUndefine(virDomainPtr dom)
{
    return libxlDomainUndefineFlags(dom, 0);
}

2508 2509 2510 2511 2512 2513
static int
libxlDomainChangeEjectableMedia(libxlDomainObjPrivatePtr priv,
                                virDomainObjPtr vm, virDomainDiskDefPtr disk)
{
    virDomainDiskDefPtr origdisk = NULL;
    libxl_device_disk x_disk;
2514
    size_t i;
2515 2516
    int ret = -1;

2517
    for (i = 0; i < vm->def->ndisks; i++) {
2518 2519 2520 2521 2522 2523 2524 2525
        if (vm->def->disks[i]->bus == disk->bus &&
            STREQ(vm->def->disks[i]->dst, disk->dst)) {
            origdisk = vm->def->disks[i];
            break;
        }
    }

    if (!origdisk) {
2526 2527 2528
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("No device with bus '%s' and target '%s'"),
                       virDomainDiskBusTypeToString(disk->bus), disk->dst);
2529 2530 2531 2532
        goto cleanup;
    }

    if (origdisk->device != VIR_DOMAIN_DISK_DEVICE_CDROM) {
2533 2534 2535
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Removable media not supported for %s device"),
                       virDomainDiskDeviceTypeToString(disk->device));
2536 2537 2538
        return -1;
    }

J
Jim Fehlig 已提交
2539
    if (libxlMakeDisk(disk, &x_disk) < 0)
2540 2541
        goto cleanup;

J
Jim Fehlig 已提交
2542
    if ((ret = libxl_cdrom_insert(priv->ctx, vm->def->id, &x_disk, NULL)) < 0) {
2543 2544 2545
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("libxenlight failed to change media for disk '%s'"),
                       disk->dst);
2546 2547 2548
        goto cleanup;
    }

2549 2550 2551
    if (virDomainDiskSetSource(origdisk, virDomainDiskGetSource(disk)) < 0)
        goto cleanup;
    virDomainDiskSetType(origdisk, virDomainDiskGetType(disk));
2552 2553 2554 2555 2556

    virDomainDiskDefFree(disk);

    ret = 0;

2557
 cleanup:
2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574
    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) {
2575
                if (virDomainDiskIndexByName(vm->def, l_disk->dst, true) >= 0) {
2576 2577
                    virReportError(VIR_ERR_OPERATION_FAILED,
                                   _("target %s already exists"), l_disk->dst);
2578 2579 2580
                    goto cleanup;
                }

2581
                if (!virDomainDiskGetSource(l_disk)) {
2582 2583
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   "%s", _("disk source path is missing"));
2584 2585 2586
                    goto cleanup;
                }

2587
                if (VIR_REALLOC_N(vm->def->disks, vm->def->ndisks+1) < 0)
2588 2589
                    goto cleanup;

J
Jim Fehlig 已提交
2590
                if (libxlMakeDisk(l_disk, &x_disk) < 0)
2591 2592
                    goto cleanup;

J
Jim Fehlig 已提交
2593 2594
                if ((ret = libxl_device_disk_add(priv->ctx, vm->def->id,
                                                &x_disk, NULL)) < 0) {
2595 2596 2597
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("libxenlight failed to attach disk '%s'"),
                                   l_disk->dst);
2598 2599 2600 2601 2602 2603
                    goto cleanup;
                }

                virDomainDiskInsertPreAlloced(vm->def, l_disk);

            } else {
2604 2605 2606
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                               _("disk bus '%s' cannot be hotplugged."),
                               virDomainDiskBusTypeToString(l_disk->bus));
2607 2608 2609
            }
            break;
        default:
2610 2611 2612
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("disk device type '%s' cannot be hotplugged"),
                           virDomainDiskDeviceTypeToString(l_disk->device));
2613 2614 2615
            break;
    }

2616
 cleanup:
2617 2618 2619
    return ret;
}

2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648
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)
C
Chunyan Liu 已提交
2649
        return -1;
2650

2651
    if (libxlMakePCI(hostdev, &pcidev) < 0)
C
Chunyan Liu 已提交
2652
        goto error;
2653 2654 2655 2656 2657 2658 2659 2660

    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);
C
Chunyan Liu 已提交
2661
        goto error;
2662 2663 2664 2665 2666
    }

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

2667
 error:
2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690
    virHostdevReAttachPCIDevices(hostdev_mgr, LIBXL_DRIVER_NAME,
                                 vm->def->name, &hostdev, 1, NULL);
    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)
C
Chunyan Liu 已提交
2691
            return -1;
2692 2693 2694 2695 2696 2697
        break;

    default:
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("hostdev subsys type '%s' not supported"),
                       virDomainHostdevSubsysTypeToString(hostdev->source.subsys.type));
C
Chunyan Liu 已提交
2698
        return -1;
2699 2700 2701 2702 2703
    }

    return 0;
}

2704 2705 2706 2707 2708 2709
static int
libxlDomainDetachDeviceDiskLive(libxlDomainObjPrivatePtr priv,
                                virDomainObjPtr vm, virDomainDeviceDefPtr dev)
{
    virDomainDiskDefPtr l_disk = NULL;
    libxl_device_disk x_disk;
2710
    int idx;
2711 2712 2713 2714 2715 2716
    int ret = -1;

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

2717 2718 2719
                if ((idx = virDomainDiskIndexByName(vm->def,
                                                    dev->data.disk->dst,
                                                    false)) < 0) {
2720 2721
                    virReportError(VIR_ERR_OPERATION_FAILED,
                                   _("disk %s not found"), dev->data.disk->dst);
2722 2723 2724
                    goto cleanup;
                }

2725
                l_disk = vm->def->disks[idx];
2726

J
Jim Fehlig 已提交
2727
                if (libxlMakeDisk(l_disk, &x_disk) < 0)
2728 2729
                    goto cleanup;

J
Jim Fehlig 已提交
2730 2731
                if ((ret = libxl_device_disk_remove(priv->ctx, vm->def->id,
                                                    &x_disk, NULL)) < 0) {
2732 2733 2734
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("libxenlight failed to detach disk '%s'"),
                                   l_disk->dst);
2735 2736 2737
                    goto cleanup;
                }

2738
                virDomainDiskRemove(vm->def, idx);
2739 2740 2741
                virDomainDiskDefFree(l_disk);

            } else {
2742 2743 2744
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                               _("disk bus '%s' cannot be hot unplugged."),
                               virDomainDiskBusTypeToString(dev->data.disk->bus));
2745 2746 2747
            }
            break;
        default:
2748 2749 2750
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("device type '%s' cannot hot unplugged"),
                           virDomainDiskDeviceTypeToString(dev->data.disk->device));
2751 2752 2753
            break;
    }

2754
 cleanup:
2755 2756 2757 2758
    return ret;
}

static int
2759 2760 2761
libxlDomainAttachDeviceLive(libxlDriverPrivatePtr driver,
                            libxlDomainObjPrivatePtr priv,
                            virDomainObjPtr vm,
2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772
                            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;

2773 2774 2775 2776 2777 2778
        case VIR_DOMAIN_DEVICE_HOSTDEV:
            ret = libxlDomainAttachHostDevice(driver, priv, vm, dev);
            if (!ret)
                dev->data.hostdev = NULL;
            break;

2779
        default:
2780 2781 2782
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("device type '%s' cannot be attached"),
                           virDomainDeviceTypeToString(dev->type));
2783 2784 2785 2786 2787 2788 2789 2790 2791 2792
            break;
    }

    return ret;
}

static int
libxlDomainAttachDeviceConfig(virDomainDefPtr vmdef, virDomainDeviceDefPtr dev)
{
    virDomainDiskDefPtr disk;
2793 2794
    virDomainHostdevDefPtr hostdev;
    virDomainHostdevDefPtr found;
2795 2796 2797 2798

    switch (dev->type) {
        case VIR_DOMAIN_DEVICE_DISK:
            disk = dev->data.disk;
2799
            if (virDomainDiskIndexByName(vmdef, disk->dst, true) >= 0) {
2800 2801
                virReportError(VIR_ERR_INVALID_ARG,
                               _("target %s already exists."), disk->dst);
2802 2803
                return -1;
            }
2804
            if (virDomainDiskInsert(vmdef, disk))
2805 2806 2807 2808
                return -1;
            /* vmdef has the pointer. Generic codes for vmdef will do all jobs */
            dev->data.disk = NULL;
            break;
2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827
        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;
2828 2829

        default:
2830 2831
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("persistent attach of device is not supported"));
2832 2833 2834 2835 2836 2837
            return -1;
    }
    return 0;
}

static int
2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894
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);
C
Chunyan Liu 已提交
2895
        goto error;
2896 2897 2898 2899 2900
    }


    libxl_device_pci_init(&pcidev);

2901
    if (libxlMakePCI(detach, &pcidev) < 0)
C
Chunyan Liu 已提交
2902
        goto error;
2903 2904 2905 2906 2907 2908 2909

    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);
C
Chunyan Liu 已提交
2910
        goto error;
2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921
    }

    libxl_device_pci_dispose(&pcidev);

    virDomainHostdevRemove(vm->def, idx);

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

    return 0;

2922
 error:
2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959
    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,
2960 2961 2962 2963 2964 2965 2966 2967 2968
                            virDomainDeviceDefPtr dev)
{
    int ret = -1;

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

2969 2970 2971 2972
        case VIR_DOMAIN_DEVICE_HOSTDEV:
            ret = libxlDomainDetachHostDevice(driver, priv, vm, dev);
            break;

2973
        default:
2974 2975 2976
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("device type '%s' cannot be detached"),
                           virDomainDeviceTypeToString(dev->type));
2977 2978 2979 2980 2981 2982
            break;
    }

    return ret;
}

2983

2984 2985 2986
static int
libxlDomainDetachDeviceConfig(virDomainDefPtr vmdef, virDomainDeviceDefPtr dev)
{
2987
    virDomainDiskDefPtr disk, detach;
2988 2989 2990 2991 2992
    int ret = -1;

    switch (dev->type) {
        case VIR_DOMAIN_DEVICE_DISK:
            disk = dev->data.disk;
2993
            if (!(detach = virDomainDiskRemoveByName(vmdef, disk->dst))) {
2994 2995
                virReportError(VIR_ERR_INVALID_ARG,
                               _("no target device %s"), disk->dst);
2996 2997
                break;
            }
2998
            virDomainDiskDefFree(detach);
2999 3000 3001
            ret = 0;
            break;
        default:
3002 3003
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("persistent detach of device is not supported"));
3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026
            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:
3027 3028 3029
                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                   _("disk bus '%s' cannot be updated."),
                                   virDomainDiskBusTypeToString(disk->bus));
3030 3031 3032 3033
                    break;
            }
            break;
        default:
3034 3035 3036
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("device type '%s' cannot be updated"),
                           virDomainDeviceTypeToString(dev->type));
3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047
            break;
    }

    return ret;
}

static int
libxlDomainUpdateDeviceConfig(virDomainDefPtr vmdef, virDomainDeviceDefPtr dev)
{
    virDomainDiskDefPtr orig;
    virDomainDiskDefPtr disk;
3048
    int idx;
3049 3050 3051 3052 3053
    int ret = -1;

    switch (dev->type) {
        case VIR_DOMAIN_DEVICE_DISK:
            disk = dev->data.disk;
3054
            if ((idx = virDomainDiskIndexByName(vmdef, disk->dst, false)) < 0) {
3055 3056
                virReportError(VIR_ERR_INVALID_ARG,
                               _("target %s doesn't exist."), disk->dst);
3057 3058
                goto cleanup;
            }
3059
            orig = vmdef->disks[idx];
3060
            if (!(orig->device == VIR_DOMAIN_DISK_DEVICE_CDROM)) {
3061 3062
                virReportError(VIR_ERR_INVALID_ARG, "%s",
                               _("this disk doesn't support update"));
3063 3064 3065
                goto cleanup;
            }

3066 3067 3068 3069 3070 3071
            if (virDomainDiskSetSource(orig, virDomainDiskGetSource(disk)) < 0)
                goto cleanup;
            virDomainDiskSetType(orig, virDomainDiskGetType(disk));
            virDomainDiskSetFormat(orig, virDomainDiskGetFormat(disk));
            if (virDomainDiskSetDriver(orig, virDomainDiskGetDriver(disk)) < 0)
                goto cleanup;
3072 3073
            break;
        default:
3074 3075
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("persistent update of device is not supported"));
3076 3077 3078 3079 3080
            goto cleanup;
    }

    ret = 0;

3081
 cleanup:
3082 3083 3084 3085 3086
    return ret;
}


static int
3087 3088
libxlDomainAttachDeviceFlags(virDomainPtr dom, const char *xml,
                             unsigned int flags)
3089 3090
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
3091
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
3092 3093 3094 3095 3096 3097 3098 3099 3100
    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 已提交
3101
    if (!(vm = libxlDomObjFromDomain(dom)))
3102 3103
        goto cleanup;

3104 3105 3106
    if (virDomainAttachDeviceFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
        goto cleanup;

3107 3108 3109
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

3110 3111 3112 3113 3114 3115 3116 3117
    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) {
3118 3119
            virReportError(VIR_ERR_OPERATION_INVALID,
                           "%s", _("Domain is not running"));
3120
            goto endjob;
3121 3122 3123 3124
        }
    }

    if ((flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) && !vm->persistent) {
3125 3126
         virReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("cannot modify device on transient domain"));
3127
         goto endjob;
3128 3129 3130 3131 3132
    }

    priv = vm->privateData;

    if (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) {
3133
        if (!(dev = virDomainDeviceDefParse(xml, vm->def,
3134
                                            cfg->caps, driver->xmlopt,
3135
                                            VIR_DOMAIN_XML_INACTIVE)))
3136
            goto endjob;
3137 3138

        /* Make a copy for updated domain. */
3139
        if (!(vmdef = virDomainObjCopyPersistentDef(vm, cfg->caps,
3140
                                                    driver->xmlopt)))
3141
            goto endjob;
3142

3143
        if ((ret = libxlDomainAttachDeviceConfig(vmdef, dev)) < 0)
3144
            goto endjob;
3145
    } else {
3146
        ret = 0;
3147
    }
3148 3149 3150 3151

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

3157
        if ((ret = libxlDomainAttachDeviceLive(driver, priv, vm, dev)) < 0)
3158
            goto endjob;
3159

3160 3161 3162 3163
        /*
         * update domain status forcibly because the domain status may be
         * changed even if we attach the device failed.
         */
3164
        if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm) < 0)
3165 3166 3167 3168 3169
            ret = -1;
    }

    /* Finally, if no error until here, we can save config. */
    if (!ret && (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG)) {
3170
        ret = virDomainSaveConfig(cfg->configDir, vmdef);
3171
        if (!ret) {
3172
            virDomainObjAssignDef(vm, vmdef, false, NULL);
3173 3174 3175 3176
            vmdef = NULL;
        }
    }

3177
 endjob:
3178 3179 3180
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

3181
 cleanup:
3182 3183 3184
    virDomainDefFree(vmdef);
    virDomainDeviceDefFree(dev);
    if (vm)
3185
        virObjectUnlock(vm);
3186
    virObjectUnref(cfg);
3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200
    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)
{
3201
    libxlDriverPrivatePtr driver = dom->conn->privateData;
3202
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
3203 3204 3205 3206 3207 3208 3209 3210 3211
    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 已提交
3212
    if (!(vm = libxlDomObjFromDomain(dom)))
3213 3214
        goto cleanup;

3215 3216 3217
    if (virDomainDetachDeviceFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
        goto cleanup;

3218 3219 3220
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

3221 3222 3223 3224 3225 3226 3227 3228 3229 3230
    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"));
3231
            goto endjob;
3232 3233 3234 3235 3236 3237
        }
    }

    if ((flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) && !vm->persistent) {
         virReportError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("cannot modify device on transient domain"));
3238
         goto endjob;
3239 3240 3241 3242 3243 3244
    }

    priv = vm->privateData;

    if (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) {
        if (!(dev = virDomainDeviceDefParse(xml, vm->def,
3245
                                            cfg->caps, driver->xmlopt,
3246
                                            VIR_DOMAIN_XML_INACTIVE)))
3247
            goto endjob;
3248 3249

        /* Make a copy for updated domain. */
3250
        if (!(vmdef = virDomainObjCopyPersistentDef(vm, cfg->caps,
3251
                                                    driver->xmlopt)))
3252
            goto endjob;
3253 3254

        if ((ret = libxlDomainDetachDeviceConfig(vmdef, dev)) < 0)
3255
            goto endjob;
3256 3257 3258 3259 3260 3261 3262 3263
    } 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,
3264
                                            cfg->caps, driver->xmlopt,
3265
                                            VIR_DOMAIN_XML_INACTIVE)))
3266
            goto endjob;
3267

3268
        if ((ret = libxlDomainDetachDeviceLive(driver, priv, vm, dev)) < 0)
3269
            goto endjob;
3270 3271 3272 3273 3274

        /*
         * update domain status forcibly because the domain status may be
         * changed even if we attach the device failed.
         */
3275
        if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm) < 0)
3276 3277 3278 3279 3280
            ret = -1;
    }

    /* Finally, if no error until here, we can save config. */
    if (!ret && (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG)) {
3281
        ret = virDomainSaveConfig(cfg->configDir, vmdef);
3282 3283 3284 3285 3286 3287
        if (!ret) {
            virDomainObjAssignDef(vm, vmdef, false, NULL);
            vmdef = NULL;
        }
    }

3288
 endjob:
3289 3290 3291
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

3292
 cleanup:
3293 3294 3295 3296
    virDomainDefFree(vmdef);
    virDomainDeviceDefFree(dev);
    if (vm)
        virObjectUnlock(vm);
3297
    virObjectUnref(cfg);
3298
    return ret;
3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311
}

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)
{
3312
    libxlDriverPrivatePtr driver = dom->conn->privateData;
3313
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
3314 3315 3316 3317 3318 3319 3320 3321 3322
    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 已提交
3323
    if (!(vm = libxlDomObjFromDomain(dom)))
3324 3325
        goto cleanup;

3326 3327 3328
    if (virDomainUpdateDeviceFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
        goto cleanup;

3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352
    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,
3353
                                            cfg->caps, driver->xmlopt,
3354 3355 3356 3357
                                            VIR_DOMAIN_XML_INACTIVE)))
            goto cleanup;

        /* Make a copy for updated domain. */
3358
        if (!(vmdef = virDomainObjCopyPersistentDef(vm, cfg->caps,
3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371
                                                    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,
3372
                                            cfg->caps, driver->xmlopt,
3373 3374 3375 3376 3377 3378 3379 3380 3381 3382
                                            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.
         */
3383
        if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm) < 0)
3384 3385 3386 3387 3388
            ret = -1;
    }

    /* Finally, if no error until here, we can save config. */
    if (!ret && (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG)) {
3389
        ret = virDomainSaveConfig(cfg->configDir, vmdef);
3390 3391 3392 3393 3394 3395
        if (!ret) {
            virDomainObjAssignDef(vm, vmdef, false, NULL);
            vmdef = NULL;
        }
    }

3396
 cleanup:
3397 3398 3399 3400
    virDomainDefFree(vmdef);
    virDomainDeviceDefFree(dev);
    if (vm)
        virObjectUnlock(vm);
3401
    virObjectUnref(cfg);
3402
    return ret;
3403 3404
}

3405 3406 3407 3408 3409
static unsigned long long
libxlNodeGetFreeMemory(virConnectPtr conn)
{
    libxl_physinfo phy_info;
    libxlDriverPrivatePtr driver = conn->privateData;
3410 3411
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
    unsigned long long ret = 0;
3412

3413
    if (virNodeGetFreeMemoryEnsureACL(conn) < 0)
3414
        goto cleanup;
3415

3416
    if (libxl_get_physinfo(cfg->ctx, &phy_info)) {
3417 3418
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("libxl_get_physinfo_info failed"));
3419
        goto cleanup;
3420 3421
    }

3422 3423
    ret = phy_info.free_pages * cfg->verInfo->pagesize;

3424
 cleanup:
3425 3426
    virObjectUnref(cfg);
    return ret;
3427 3428
}

3429 3430 3431 3432 3433 3434 3435 3436 3437 3438
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;
3439
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
3440 3441

    if (virNodeGetCellsFreeMemoryEnsureACL(conn) < 0)
3442
        goto cleanup;
3443

3444
    numa_info = libxl_get_numainfo(cfg->ctx, &nr_nodes);
3445
    if (numa_info == NULL || nr_nodes == 0) {
3446 3447 3448
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("libxl_get_numainfo failed"));
        goto cleanup;
3449 3450 3451
    }

    /* Check/sanitize the cell range */
3452
    if (startCell >= nr_nodes) {
3453 3454
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("start cell %d out of range (0-%d)"),
3455
                       startCell, nr_nodes - 1);
3456 3457 3458
        goto cleanup;
    }
    lastCell = startCell + maxCells - 1;
3459 3460
    if (lastCell >= nr_nodes)
        lastCell = nr_nodes - 1;
3461 3462 3463 3464 3465 3466 3467

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

3469 3470
    ret = numCells;

3471
 cleanup:
3472
    libxl_numainfo_list_free(numa_info, nr_nodes);
3473
    virObjectUnref(cfg);
3474 3475 3476
    return ret;
}

3477
static int
3478
libxlConnectDomainEventRegister(virConnectPtr conn,
3479 3480
                                virConnectDomainEventCallback callback,
                                void *opaque,
3481
                                virFreeCallback freecb)
3482 3483 3484
{
    libxlDriverPrivatePtr driver = conn->privateData;

3485 3486 3487
    if (virConnectDomainEventRegisterEnsureACL(conn) < 0)
        return -1;

3488 3489 3490 3491
    if (virDomainEventStateRegister(conn,
                                    driver->domainEventState,
                                    callback, opaque, freecb) < 0)
        return -1;
3492

3493
    return 0;
3494 3495 3496 3497
}


static int
3498 3499
libxlConnectDomainEventDeregister(virConnectPtr conn,
                                  virConnectDomainEventCallback callback)
3500 3501 3502
{
    libxlDriverPrivatePtr driver = conn->privateData;

3503 3504 3505
    if (virConnectDomainEventDeregisterEnsureACL(conn) < 0)
        return -1;

3506 3507 3508 3509
    if (virDomainEventStateDeregister(conn,
                                      driver->domainEventState,
                                      callback) < 0)
        return -1;
3510

3511
    return 0;
3512 3513
}

3514 3515 3516 3517 3518 3519
static int
libxlDomainGetAutostart(virDomainPtr dom, int *autostart)
{
    virDomainObjPtr vm;
    int ret = -1;

J
Jim Fehlig 已提交
3520
    if (!(vm = libxlDomObjFromDomain(dom)))
3521 3522
        goto cleanup;

3523 3524 3525
    if (virDomainGetAutostartEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

3526 3527 3528
    *autostart = vm->autostart;
    ret = 0;

3529
 cleanup:
3530
    if (vm)
3531
        virObjectUnlock(vm);
3532 3533 3534 3535 3536 3537 3538
    return ret;
}

static int
libxlDomainSetAutostart(virDomainPtr dom, int autostart)
{
    libxlDriverPrivatePtr driver = dom->conn->privateData;
3539
    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
3540 3541 3542 3543
    virDomainObjPtr vm;
    char *configFile = NULL, *autostartLink = NULL;
    int ret = -1;

J
Jim Fehlig 已提交
3544
    if (!(vm = libxlDomObjFromDomain(dom)))
3545 3546
        goto cleanup;

3547 3548 3549
    if (virDomainSetAutostartEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

3550 3551 3552
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

3553
    if (!vm->persistent) {
3554 3555
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("cannot set autostart for transient domain"));
3556
        goto endjob;
3557 3558 3559 3560 3561
    }

    autostart = (autostart != 0);

    if (vm->autostart != autostart) {
3562
        if (!(configFile = virDomainConfigFile(cfg->configDir, vm->def->name)))
3563
            goto endjob;
3564
        if (!(autostartLink = virDomainConfigFile(cfg->autostartDir, vm->def->name)))
3565
            goto endjob;
3566 3567

        if (autostart) {
3568
            if (virFileMakePath(cfg->autostartDir) < 0) {
3569
                virReportSystemError(errno,
3570
                                     _("cannot create autostart directory %s"),
3571
                                     cfg->autostartDir);
3572
                goto endjob;
3573 3574 3575 3576 3577 3578
            }

            if (symlink(configFile, autostartLink) < 0) {
                virReportSystemError(errno,
                                     _("Failed to create symlink '%s to '%s'"),
                                     autostartLink, configFile);
3579
                goto endjob;
3580 3581 3582 3583 3584 3585
            }
        } else {
            if (unlink(autostartLink) < 0 && errno != ENOENT && errno != ENOTDIR) {
                virReportSystemError(errno,
                                     _("Failed to delete symlink '%s'"),
                                     autostartLink);
3586
                goto endjob;
3587 3588 3589 3590 3591 3592 3593
            }
        }

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

3594
 endjob:
3595 3596 3597
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

3598
 cleanup:
3599 3600 3601
    VIR_FREE(configFile);
    VIR_FREE(autostartLink);
    if (vm)
3602
        virObjectUnlock(vm);
3603
    virObjectUnref(cfg);
3604 3605 3606
    return ret;
}

3607 3608 3609 3610 3611 3612
static char *
libxlDomainGetSchedulerType(virDomainPtr dom, int *nparams)
{
    libxlDomainObjPrivatePtr priv;
    virDomainObjPtr vm;
    char * ret = NULL;
3613
    const char *name = NULL;
J
Jim Fehlig 已提交
3614
    libxl_scheduler sched_id;
3615

J
Jim Fehlig 已提交
3616
    if (!(vm = libxlDomObjFromDomain(dom)))
3617 3618
        goto cleanup;

3619 3620 3621
    if (virDomainGetSchedulerTypeEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

3622
    if (!virDomainObjIsActive(vm)) {
3623
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not running"));
3624 3625 3626 3627
        goto cleanup;
    }

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

3630 3631
    if (nparams)
        *nparams = 0;
3632
    switch (sched_id) {
J
Jim Fehlig 已提交
3633
    case LIBXL_SCHEDULER_SEDF:
3634
        name = "sedf";
3635
        break;
J
Jim Fehlig 已提交
3636
    case LIBXL_SCHEDULER_CREDIT:
3637
        name = "credit";
3638 3639
        if (nparams)
            *nparams = XEN_SCHED_CREDIT_NPARAM;
3640
        break;
J
Jim Fehlig 已提交
3641
    case LIBXL_SCHEDULER_CREDIT2:
3642
        name = "credit2";
3643
        break;
J
Jim Fehlig 已提交
3644
    case LIBXL_SCHEDULER_ARINC653:
3645
        name = "arinc653";
3646 3647
        break;
    default:
J
Jim Fehlig 已提交
3648 3649
        virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("Failed to get scheduler id for domain '%d'"
3650
                     " with libxenlight"), vm->def->id);
3651 3652 3653
        goto cleanup;
    }

3654
    ignore_value(VIR_STRDUP(ret, name));
3655

3656
 cleanup:
3657
    if (vm)
3658
        virObjectUnlock(vm);
3659 3660 3661
    return ret;
}

3662
static int
3663 3664 3665 3666
libxlDomainGetSchedulerParametersFlags(virDomainPtr dom,
                                       virTypedParameterPtr params,
                                       int *nparams,
                                       unsigned int flags)
3667 3668 3669
{
    libxlDomainObjPrivatePtr priv;
    virDomainObjPtr vm;
J
Jim Fehlig 已提交
3670 3671
    libxl_domain_sched_params sc_info;
    libxl_scheduler sched_id;
3672 3673
    int ret = -1;

3674 3675 3676 3677
    virCheckFlags(VIR_TYPED_PARAM_STRING_OKAY, -1);

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

J
Jim Fehlig 已提交
3679
    if (!(vm = libxlDomObjFromDomain(dom)))
3680 3681
        goto cleanup;

3682 3683 3684
    if (virDomainGetSchedulerParametersFlagsEnsureACL(dom->conn, vm->def) < 0)
        goto cleanup;

3685
    if (!virDomainObjIsActive(vm)) {
J
Jim Fehlig 已提交
3686 3687
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Domain is not running"));
3688 3689 3690 3691 3692
        goto cleanup;
    }

    priv = vm->privateData;

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

J
Jim Fehlig 已提交
3695
    if (sched_id != LIBXL_SCHEDULER_CREDIT) {
3696 3697
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Only 'credit' scheduler is supported"));
3698 3699 3700
        goto cleanup;
    }

3701
    if (libxl_domain_sched_params_get(priv->ctx, vm->def->id, &sc_info) != 0) {
3702 3703
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to get scheduler parameters for domain '%d'"
3704
                         " with libxenlight"), vm->def->id);
3705 3706 3707
        goto cleanup;
    }

3708 3709
    if (virTypedParameterAssign(&params[0], VIR_DOMAIN_SCHEDULER_WEIGHT,
                                VIR_TYPED_PARAM_UINT, sc_info.weight) < 0)
3710 3711
        goto cleanup;

3712
    if (*nparams > 1) {
3713 3714
        if (virTypedParameterAssign(&params[0], VIR_DOMAIN_SCHEDULER_CAP,
                                    VIR_TYPED_PARAM_UINT, sc_info.cap) < 0)
3715
            goto cleanup;
3716 3717
    }

3718 3719
    if (*nparams > XEN_SCHED_CREDIT_NPARAM)
        *nparams = XEN_SCHED_CREDIT_NPARAM;
3720 3721
    ret = 0;

3722
 cleanup:
3723
    if (vm)
3724
        virObjectUnlock(vm);
3725 3726 3727 3728
    return ret;
}

static int
3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739
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)
3740
{
3741
    libxlDriverPrivatePtr driver = dom->conn->privateData;
3742 3743
    libxlDomainObjPrivatePtr priv;
    virDomainObjPtr vm;
J
Jim Fehlig 已提交
3744
    libxl_domain_sched_params sc_info;
3745
    int sched_id;
3746
    size_t i;
3747 3748
    int ret = -1;

3749
    virCheckFlags(0, -1);
3750 3751 3752 3753 3754 3755
    if (virTypedParamsValidate(params, nparams,
                               VIR_DOMAIN_SCHEDULER_WEIGHT,
                               VIR_TYPED_PARAM_UINT,
                               VIR_DOMAIN_SCHEDULER_CAP,
                               VIR_TYPED_PARAM_UINT,
                               NULL) < 0)
3756
        return -1;
3757

J
Jim Fehlig 已提交
3758
    if (!(vm = libxlDomObjFromDomain(dom)))
3759 3760
        goto cleanup;

3761 3762 3763
    if (virDomainSetSchedulerParametersFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
        goto cleanup;

3764 3765 3766
    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
        goto cleanup;

3767
    if (!virDomainObjIsActive(vm)) {
3768
        virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Domain is not running"));
3769
        goto endjob;
3770 3771 3772 3773
    }

    priv = vm->privateData;

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

J
Jim Fehlig 已提交
3776
    if (sched_id != LIBXL_SCHEDULER_CREDIT) {
3777 3778
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Only 'credit' scheduler is supported"));
3779
        goto endjob;
3780 3781
    }

3782
    if (libxl_domain_sched_params_get(priv->ctx, vm->def->id, &sc_info) != 0) {
3783 3784
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to get scheduler parameters for domain '%d'"
3785
                         " with libxenlight"), vm->def->id);
3786
        goto endjob;
3787 3788 3789
    }

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

3792
        if (STREQ(param->field, VIR_DOMAIN_SCHEDULER_WEIGHT))
3793
            sc_info.weight = params[i].value.ui;
3794
        else if (STREQ(param->field, VIR_DOMAIN_SCHEDULER_CAP))
3795 3796 3797
            sc_info.cap = params[i].value.ui;
    }

3798
    if (libxl_domain_sched_params_set(priv->ctx, vm->def->id, &sc_info) != 0) {
3799 3800
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to set scheduler parameters for domain '%d'"
3801
                         " with libxenlight"), vm->def->id);
3802
        goto endjob;
3803 3804 3805 3806
    }

    ret = 0;

3807
 endjob:
3808 3809 3810
    if (!libxlDomainObjEndJob(driver, vm))
        vm = NULL;

3811
 cleanup:
3812
    if (vm)
3813
        virObjectUnlock(vm);
3814 3815 3816
    return ret;
}

B
Bamvor Jian Zhang 已提交
3817 3818 3819 3820 3821 3822 3823 3824 3825

static int
libxlDomainOpenConsole(virDomainPtr dom,
                       const char *dev_name,
                       virStreamPtr st,
                       unsigned int flags)
{
    virDomainObjPtr vm = NULL;
    int ret = -1;
I
Ian Campbell 已提交
3826
    libxl_console_type console_type;
B
Bamvor Jian Zhang 已提交
3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839
    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 已提交
3840
    if (!(vm = libxlDomObjFromDomain(dom)))
B
Bamvor Jian Zhang 已提交
3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853
        goto cleanup;

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

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

    priv = vm->privateData;

I
Ian Campbell 已提交
3854 3855
    if (vm->def->nconsoles)
        chr = vm->def->consoles[0];
B
Bamvor Jian Zhang 已提交
3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870

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

I
Ian Campbell 已提交
3871 3872 3873 3874 3875 3876
    console_type =
        (chr->targetType == VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SERIAL ?
                            LIBXL_CONSOLE_TYPE_SERIAL : LIBXL_CONSOLE_TYPE_PV);

    ret = libxl_console_get_tty(priv->ctx, vm->def->id, chr->target.port,
                                console_type, &console);
B
Bamvor Jian Zhang 已提交
3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894
    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;
    }

3895
 cleanup:
B
Bamvor Jian Zhang 已提交
3896 3897 3898 3899 3900 3901
    VIR_FREE(console);
    if (vm)
        virObjectUnlock(vm);
    return ret;
}

3902 3903 3904 3905 3906 3907 3908
static int
libxlDomainSetSchedulerParameters(virDomainPtr dom, virTypedParameterPtr params,
                                  int nparams)
{
    return libxlDomainSetSchedulerParametersFlags(dom, params, nparams, 0);
}

3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938
/* 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;

3939 3940
    libxl_bitmap_init(&nodemap);

J
Jim Fehlig 已提交
3941
    if (!(vm = libxlDomObjFromDomain(dom)))
3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962
        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];
3963
        int numnodes;
3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981

        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 */
3982 3983 3984 3985
            numnodes = libxl_get_max_nodes(priv->ctx);
            if (numnodes <= 0)
                goto cleanup;

J
Ján Tomko 已提交
3986
            if (libxl_node_bitmap_alloc(priv->ctx, &nodemap, 0)) {
3987 3988 3989
                virReportOOMError();
                goto cleanup;
            }
J
Ján Tomko 已提交
3990 3991
            if (!(nodes = virBitmapNew(numnodes)))
                goto cleanup;
3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012

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

4013
            if (!(nodeset = virBitmapFormat(nodes)))
4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029
                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;

4030
 cleanup:
4031 4032 4033 4034 4035 4036 4037 4038 4039
    VIR_FREE(nodeset);
    virBitmapFree(nodes);
    libxl_bitmap_dispose(&nodemap);
    if (vm)
        virObjectUnlock(vm);
    return ret;
}
#endif

J
Jim Fehlig 已提交
4040 4041 4042 4043 4044 4045
static int
libxlDomainIsActive(virDomainPtr dom)
{
    virDomainObjPtr obj;
    int ret = -1;

J
Jim Fehlig 已提交
4046
    if (!(obj = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
4047
        goto cleanup;
4048 4049 4050 4051

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

J
Jim Fehlig 已提交
4052 4053
    ret = virDomainObjIsActive(obj);

4054
 cleanup:
J
Jim Fehlig 已提交
4055
    if (obj)
4056
        virObjectUnlock(obj);
J
Jim Fehlig 已提交
4057 4058 4059 4060 4061 4062 4063 4064 4065
    return ret;
}

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

J
Jim Fehlig 已提交
4066
    if (!(obj = libxlDomObjFromDomain(dom)))
J
Jim Fehlig 已提交
4067
        goto cleanup;
4068 4069 4070 4071

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

J
Jim Fehlig 已提交
4072 4073
    ret = obj->persistent;

4074
 cleanup:
J
Jim Fehlig 已提交
4075
    if (obj)
4076
        virObjectUnlock(obj);
J
Jim Fehlig 已提交
4077 4078 4079
    return ret;
}

4080 4081 4082 4083 4084 4085
static int
libxlDomainIsUpdated(virDomainPtr dom)
{
    virDomainObjPtr vm;
    int ret = -1;

J
Jim Fehlig 已提交
4086
    if (!(vm = libxlDomObjFromDomain(dom)))
4087
        goto cleanup;
4088 4089 4090 4091

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

4092 4093
    ret = vm->updated;

4094
 cleanup:
4095
    if (vm)
4096
        virObjectUnlock(vm);
4097 4098 4099
    return ret;
}

4100
static int
4101 4102 4103
libxlConnectDomainEventRegisterAny(virConnectPtr conn, virDomainPtr dom, int eventID,
                                   virConnectDomainEventGenericCallback callback,
                                   void *opaque, virFreeCallback freecb)
4104 4105 4106 4107
{
    libxlDriverPrivatePtr driver = conn->privateData;
    int ret;

4108 4109 4110
    if (virConnectDomainEventRegisterAnyEnsureACL(conn) < 0)
        return -1;

4111 4112 4113 4114
    if (virDomainEventStateRegisterID(conn,
                                      driver->domainEventState,
                                      dom, eventID, callback, opaque,
                                      freecb, &ret) < 0)
4115
        ret = -1;
4116 4117 4118 4119 4120 4121

    return ret;
}


static int
4122
libxlConnectDomainEventDeregisterAny(virConnectPtr conn, int callbackID)
4123 4124 4125
{
    libxlDriverPrivatePtr driver = conn->privateData;

4126 4127 4128
    if (virConnectDomainEventDeregisterAnyEnsureACL(conn) < 0)
        return -1;

4129 4130 4131 4132
    if (virObjectEventStateDeregisterID(conn,
                                        driver->domainEventState,
                                        callbackID) < 0)
        return -1;
4133

4134
    return 0;
4135 4136
}

J
Jim Fehlig 已提交
4137

4138
static int
4139
libxlConnectIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
4140 4141 4142 4143
{
    return 1;
}

4144
static int
4145 4146 4147
libxlConnectListAllDomains(virConnectPtr conn,
                           virDomainPtr **domains,
                           unsigned int flags)
4148 4149 4150 4151
{
    libxlDriverPrivatePtr driver = conn->privateData;
    int ret = -1;

O
Osier Yang 已提交
4152
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
4153

4154 4155 4156
    if (virConnectListAllDomainsEnsureACL(conn) < 0)
        return -1;

4157 4158
    ret = virDomainObjListExport(driver->domains, conn, domains,
                                 virConnectListAllDomainsCheckACL, flags);
4159 4160 4161 4162

    return ret;
}

4163 4164 4165 4166 4167 4168 4169 4170 4171
/* Which features are supported by this driver? */
static int
libxlConnectSupportsFeature(virConnectPtr conn, int feature)
{
    if (virConnectSupportsFeatureEnsureACL(conn) < 0)
        return -1;

    switch (feature) {
    case VIR_DRV_FEATURE_TYPED_PARAM_STRING:
J
Jim Fehlig 已提交
4172
    case VIR_DRV_FEATURE_MIGRATION_PARAMS:
4173 4174 4175 4176 4177
        return 1;
    default:
        return 0;
    }
}
4178

4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203
static int
libxlNodeDeviceGetPCIInfo(virNodeDeviceDefPtr def,
                          unsigned *domain,
                          unsigned *bus,
                          unsigned *slot,
                          unsigned *function)
{
    virNodeDevCapsDefPtr cap;

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

        cap = cap->next;
    }

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

C
Chunyan Liu 已提交
4207
    return 0;
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 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255
}

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;
4256
 cleanup:
4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298
    virPCIDeviceFree(pci);
    virNodeDeviceDefFree(def);
    VIR_FREE(xml);
    return ret;
}

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

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

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

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

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

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

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

    if (virHostdevPCINodeDeviceReAttach(hostdev_mgr, pci) < 0)
C
Chunyan Liu 已提交
4299
        goto cleanup;
4300 4301

    ret = 0;
C
Chunyan Liu 已提交
4302

4303
 cleanup:
C
Chunyan Liu 已提交
4304
    virPCIDeviceFree(pci);
4305 4306 4307 4308 4309 4310 4311 4312
    virNodeDeviceDefFree(def);
    VIR_FREE(xml);
    return ret;
}

static int
libxlNodeDeviceReset(virNodeDevicePtr dev)
{
C
Chunyan Liu 已提交
4313
    virPCIDevicePtr pci = NULL;
4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339
    unsigned domain = 0, bus = 0, slot = 0, function = 0;
    int ret = -1;
    virNodeDeviceDefPtr def = NULL;
    char *xml = NULL;
    libxlDriverPrivatePtr driver = dev->conn->privateData;
    virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;

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

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

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

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

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

    if (virHostdevPCINodeDeviceReset(hostdev_mgr, pci) < 0)
C
Chunyan Liu 已提交
4340
        goto cleanup;
4341 4342

    ret = 0;
C
Chunyan Liu 已提交
4343

4344
 cleanup:
C
Chunyan Liu 已提交
4345
    virPCIDeviceFree(pci);
4346 4347 4348 4349 4350
    virNodeDeviceDefFree(def);
    VIR_FREE(xml);
    return ret;
}

J
Jim Fehlig 已提交
4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361
static char *
libxlDomainMigrateBegin3Params(virDomainPtr domain,
                               virTypedParameterPtr params,
                               int nparams,
                               char **cookieout ATTRIBUTE_UNUSED,
                               int *cookieoutlen ATTRIBUTE_UNUSED,
                               unsigned int flags)
{
    const char *xmlin = NULL;
    virDomainObjPtr vm = NULL;

4362 4363 4364 4365 4366
#ifdef LIBXL_HAVE_NO_SUSPEND_RESUME
    virReportUnsupportedError();
    return NULL;
#endif

J
Jim Fehlig 已提交
4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410
    virCheckFlags(LIBXL_MIGRATION_FLAGS, NULL);
    if (virTypedParamsValidate(params, nparams, LIBXL_MIGRATION_PARAMETERS) < 0)
        return NULL;

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

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

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

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

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

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

4411 4412 4413 4414 4415
#ifdef LIBXL_HAVE_NO_SUSPEND_RESUME
    virReportUnsupportedError();
    return -1;
#endif

J
Jim Fehlig 已提交
4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437
    virCheckFlags(LIBXL_MIGRATION_FLAGS, -1);
    if (virTypedParamsValidate(params, nparams, LIBXL_MIGRATION_PARAMETERS) < 0)
        goto error;

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

        goto error;

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

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

4438
    if (libxlDomainMigrationPrepare(dconn, &def, uri_in, uri_out, flags) < 0)
J
Jim Fehlig 已提交
4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465
        goto error;

    return 0;

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

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

4466 4467 4468 4469 4470
#ifdef LIBXL_HAVE_NO_SUSPEND_RESUME
    virReportUnsupportedError();
    return -1;
#endif

J
Jim Fehlig 已提交
4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493
    virCheckFlags(LIBXL_MIGRATION_FLAGS, -1);
    if (virTypedParamsValidate(params, nparams, LIBXL_MIGRATION_PARAMETERS) < 0)
        goto cleanup;

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

        goto cleanup;

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

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

    if (libxlDomainMigrationPerform(driver, vm, dom_xml, dconnuri,
4494
                                    uri, dname, flags) < 0)
J
Jim Fehlig 已提交
4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519
        goto cleanup;

    ret = 0;

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

static virDomainPtr
libxlDomainMigrateFinish3Params(virConnectPtr dconn,
                                virTypedParameterPtr params,
                                int nparams,
                                const char *cookiein ATTRIBUTE_UNUSED,
                                int cookieinlen ATTRIBUTE_UNUSED,
                                char **cookieout ATTRIBUTE_UNUSED,
                                int *cookieoutlen ATTRIBUTE_UNUSED,
                                unsigned int flags,
                                int cancelled)
{
    libxlDriverPrivatePtr driver = dconn->privateData;
    virDomainObjPtr vm = NULL;
    const char *dname = NULL;

4520 4521 4522 4523 4524
#ifdef LIBXL_HAVE_NO_SUSPEND_RESUME
    virReportUnsupportedError();
    return NULL;
#endif

J
Jim Fehlig 已提交
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
    virCheckFlags(LIBXL_MIGRATION_FLAGS, NULL);
    if (virTypedParamsValidate(params, nparams, LIBXL_MIGRATION_PARAMETERS) < 0)
        return NULL;

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

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

    if (virDomainMigrateFinish3ParamsEnsureACL(dconn, vm->def) < 0) {
        virObjectUnlock(vm);
        return NULL;
    }

    if (!virDomainObjIsActive(vm)) {
        /* Migration failed if domain is inactive */
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("Migration failed. Domain is not running "
                               "on destination host"));
        virObjectUnlock(vm);
        return NULL;
    }

    return libxlDomainMigrationFinish(dconn, vm, flags, cancelled);
}

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

4573 4574 4575 4576 4577
#ifdef LIBXL_HAVE_NO_SUSPEND_RESUME
    virReportUnsupportedError();
    return -1;
#endif

J
Jim Fehlig 已提交
4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592
    virCheckFlags(LIBXL_MIGRATION_FLAGS, -1);
    if (virTypedParamsValidate(params, nparams, LIBXL_MIGRATION_PARAMETERS) < 0)
        return -1;

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

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

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

4593

J
Jim Fehlig 已提交
4594
static virDriver libxlDriver = {
4595
    .no = VIR_DRV_LIBXL,
4596
    .name = LIBXL_DRIVER_NAME,
4597 4598 4599 4600
    .connectOpen = libxlConnectOpen, /* 0.9.0 */
    .connectClose = libxlConnectClose, /* 0.9.0 */
    .connectGetType = libxlConnectGetType, /* 0.9.0 */
    .connectGetVersion = libxlConnectGetVersion, /* 0.9.0 */
4601
    .connectGetHostname = libxlConnectGetHostname, /* 0.9.0 */
4602
    .connectGetSysinfo = libxlConnectGetSysinfo, /* 1.1.0 */
4603
    .connectGetMaxVcpus = libxlConnectGetMaxVcpus, /* 0.9.0 */
4604
    .nodeGetInfo = libxlNodeGetInfo, /* 0.9.0 */
4605 4606 4607 4608
    .connectGetCapabilities = libxlConnectGetCapabilities, /* 0.9.0 */
    .connectListDomains = libxlConnectListDomains, /* 0.9.0 */
    .connectNumOfDomains = libxlConnectNumOfDomains, /* 0.9.0 */
    .connectListAllDomains = libxlConnectListAllDomains, /* 0.9.13 */
4609 4610 4611 4612 4613 4614 4615
    .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 */
4616
    .domainShutdownFlags = libxlDomainShutdownFlags, /* 0.9.10 */
4617 4618
    .domainReboot = libxlDomainReboot, /* 0.9.0 */
    .domainDestroy = libxlDomainDestroy, /* 0.9.0 */
4619
    .domainDestroyFlags = libxlDomainDestroyFlags, /* 0.9.4 */
4620 4621
    .domainGetOSType = libxlDomainGetOSType, /* 0.9.0 */
    .domainGetMaxMemory = libxlDomainGetMaxMemory, /* 0.9.0 */
4622
    .domainSetMaxMemory = libxlDomainSetMaxMemory, /* 0.9.2 */
4623 4624 4625 4626
    .domainSetMemory = libxlDomainSetMemory, /* 0.9.0 */
    .domainSetMemoryFlags = libxlDomainSetMemoryFlags, /* 0.9.0 */
    .domainGetInfo = libxlDomainGetInfo, /* 0.9.0 */
    .domainGetState = libxlDomainGetState, /* 0.9.2 */
4627
    .domainSave = libxlDomainSave, /* 0.9.2 */
4628
    .domainSaveFlags = libxlDomainSaveFlags, /* 0.9.4 */
4629
    .domainRestore = libxlDomainRestore, /* 0.9.2 */
4630
    .domainRestoreFlags = libxlDomainRestoreFlags, /* 0.9.4 */
4631
    .domainCoreDump = libxlDomainCoreDump, /* 0.9.2 */
4632 4633 4634 4635
    .domainSetVcpus = libxlDomainSetVcpus, /* 0.9.0 */
    .domainSetVcpusFlags = libxlDomainSetVcpusFlags, /* 0.9.0 */
    .domainGetVcpusFlags = libxlDomainGetVcpusFlags, /* 0.9.0 */
    .domainPinVcpu = libxlDomainPinVcpu, /* 0.9.0 */
4636
    .domainPinVcpuFlags = libxlDomainPinVcpuFlags, /* 1.2.1 */
4637
    .domainGetVcpus = libxlDomainGetVcpus, /* 0.9.0 */
4638
    .domainGetVcpuPinInfo = libxlDomainGetVcpuPinInfo, /* 1.2.1 */
4639
    .domainGetXMLDesc = libxlDomainGetXMLDesc, /* 0.9.0 */
4640 4641 4642 4643
    .connectDomainXMLFromNative = libxlConnectDomainXMLFromNative, /* 0.9.0 */
    .connectDomainXMLToNative = libxlConnectDomainXMLToNative, /* 0.9.0 */
    .connectListDefinedDomains = libxlConnectListDefinedDomains, /* 0.9.0 */
    .connectNumOfDefinedDomains = libxlConnectNumOfDefinedDomains, /* 0.9.0 */
4644 4645 4646 4647
    .domainCreate = libxlDomainCreate, /* 0.9.0 */
    .domainCreateWithFlags = libxlDomainCreateWithFlags, /* 0.9.0 */
    .domainDefineXML = libxlDomainDefineXML, /* 0.9.0 */
    .domainUndefine = libxlDomainUndefine, /* 0.9.0 */
4648
    .domainUndefineFlags = libxlDomainUndefineFlags, /* 0.9.4 */
4649 4650 4651 4652 4653
    .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 */
4654 4655 4656 4657
    .domainGetAutostart = libxlDomainGetAutostart, /* 0.9.0 */
    .domainSetAutostart = libxlDomainSetAutostart, /* 0.9.0 */
    .domainGetSchedulerType = libxlDomainGetSchedulerType, /* 0.9.0 */
    .domainGetSchedulerParameters = libxlDomainGetSchedulerParameters, /* 0.9.0 */
4658
    .domainGetSchedulerParametersFlags = libxlDomainGetSchedulerParametersFlags, /* 0.9.2 */
4659
    .domainSetSchedulerParameters = libxlDomainSetSchedulerParameters, /* 0.9.0 */
4660
    .domainSetSchedulerParametersFlags = libxlDomainSetSchedulerParametersFlags, /* 0.9.2 */
4661 4662 4663
#ifdef LIBXL_HAVE_DOMAIN_NODEAFFINITY
    .domainGetNumaParameters = libxlDomainGetNumaParameters, /* 1.1.1 */
#endif
4664
    .nodeGetFreeMemory = libxlNodeGetFreeMemory, /* 0.9.0 */
4665
    .nodeGetCellsFreeMemory = libxlNodeGetCellsFreeMemory, /* 1.1.1 */
4666 4667
    .connectDomainEventRegister = libxlConnectDomainEventRegister, /* 0.9.0 */
    .connectDomainEventDeregister = libxlConnectDomainEventDeregister, /* 0.9.0 */
4668 4669 4670
    .domainManagedSave = libxlDomainManagedSave, /* 0.9.2 */
    .domainHasManagedSaveImage = libxlDomainHasManagedSaveImage, /* 0.9.2 */
    .domainManagedSaveRemove = libxlDomainManagedSaveRemove, /* 0.9.2 */
B
Bamvor Jian Zhang 已提交
4671
    .domainOpenConsole = libxlDomainOpenConsole, /* 1.1.2 */
4672 4673 4674
    .domainIsActive = libxlDomainIsActive, /* 0.9.0 */
    .domainIsPersistent = libxlDomainIsPersistent, /* 0.9.0 */
    .domainIsUpdated = libxlDomainIsUpdated, /* 0.9.0 */
4675 4676 4677
    .connectDomainEventRegisterAny = libxlConnectDomainEventRegisterAny, /* 0.9.0 */
    .connectDomainEventDeregisterAny = libxlConnectDomainEventDeregisterAny, /* 0.9.0 */
    .connectIsAlive = libxlConnectIsAlive, /* 0.9.8 */
4678
    .connectSupportsFeature = libxlConnectSupportsFeature, /* 1.1.1 */
4679 4680 4681 4682
    .nodeDeviceDettach = libxlNodeDeviceDettach, /* 1.2.3 */
    .nodeDeviceDetachFlags = libxlNodeDeviceDetachFlags, /* 1.2.3 */
    .nodeDeviceReAttach = libxlNodeDeviceReAttach, /* 1.2.3 */
    .nodeDeviceReset = libxlNodeDeviceReset, /* 1.2.3 */
4683 4684 4685 4686 4687
    .domainMigrateBegin3Params = libxlDomainMigrateBegin3Params, /* 1.2.6 */
    .domainMigratePrepare3Params = libxlDomainMigratePrepare3Params, /* 1.2.6 */
    .domainMigratePerform3Params = libxlDomainMigratePerform3Params, /* 1.2.6 */
    .domainMigrateFinish3Params = libxlDomainMigrateFinish3Params, /* 1.2.6 */
    .domainMigrateConfirm3Params = libxlDomainMigrateConfirm3Params, /* 1.2.6 */
J
Jim Fehlig 已提交
4688 4689 4690 4691
};

static virStateDriver libxlStateDriver = {
    .name = "LIBXL",
4692
    .stateInitialize = libxlStateInitialize,
4693
    .stateAutoStart = libxlStateAutoStart,
4694 4695
    .stateCleanup = libxlStateCleanup,
    .stateReload = libxlStateReload,
J
Jim Fehlig 已提交
4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708
};


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

    return 0;
}