xen_driver.c 74.4 KB
Newer Older
1
/*
2
 * xen_driver.c: Unified Xen driver.
3
 *
4
 * Copyright (C) 2007-2015 Red Hat, Inc.
5
 *
O
Osier Yang 已提交
6 7 8 9 10 11 12 13 14 15 16
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21 22
 *
 * Richard W.M. Jones <rjones@redhat.com>
 */

23
#include <config.h>
24

25 26 27
/* Note:
 *
 * This driver provides a unified interface to the five
28
 * separate underlying Xen drivers (xen_internal,
29 30 31 32 33 34 35
 * xend_internal, xs_internal and xm_internal).  Historically
 * the body of libvirt.c handled the five Xen drivers,
 * and contained Xen-specific code.
 */

#include <stdint.h>
#include <unistd.h>
36
#include <string.h>
37
#include <errno.h>
38
#include <sys/types.h>
39
#include <fcntl.h>
40 41
#include <xen/dom0_ops.h>

42
#include "virerror.h"
43
#include "virlog.h"
44
#include "datatypes.h"
45
#include "xen_driver.h"
46

47
#include "xen_sxpr.h"
48
#include "xen_xm.h"
P
Pavel Hrdina 已提交
49
#include "xen_common.h"
50
#include "xen_hypervisor.h"
51 52 53
#include "xend_internal.h"
#include "xs_internal.h"
#include "xm_internal.h"
54
#if WITH_XEN_INOTIFY
55
# include "xen_inotify.h"
56
#endif
57
#include "virxml.h"
58
#include "viralloc.h"
59
#include "node_device_conf.h"
60
#include "virpci.h"
61
#include "viruuid.h"
62
#include "fdstream.h"
E
Eric Blake 已提交
63
#include "virfile.h"
M
Martin Kletzander 已提交
64
#include "viruri.h"
65
#include "vircommand.h"
66
#include "virnodesuspend.h"
67
#include "nodeinfo.h"
68
#include "virhostmem.h"
69
#include "configmake.h"
70
#include "virstring.h"
71
#include "viraccessapicheck.h"
72

73
#define VIR_FROM_THIS VIR_FROM_XEN
74 75 76

VIR_LOG_INIT("xen.xen_driver");

77
#define XEN_SAVE_DIR LOCALSTATEDIR "/lib/libvirt/xen/save"
78

79
static int
80
xenUnifiedNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info);
81

82
static int
83 84 85 86
xenUnifiedDomainGetVcpusFlagsInternal(virDomainPtr dom,
                                      virDomainDefPtr def,
                                      unsigned int flags);

87
static int
88 89 90 91 92 93
xenUnifiedDomainGetVcpusInternal(virDomainPtr dom,
                                 virDomainDefPtr def,
                                 virVcpuInfoPtr info,
                                 int maxinfo,
                                 unsigned char *cpumaps,
                                 int maplen);
94

95

96 97
static bool is_privileged;
static virSysinfoDefPtr hostsysinfo;
J
John Levon 已提交
98

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
static virDomainDefPtr xenGetDomainDefForID(virConnectPtr conn, int id)
{
    virDomainDefPtr ret;

    ret = xenHypervisorLookupDomainByID(conn, id);

    if (!ret && virGetLastError() == NULL)
        virReportError(VIR_ERR_NO_DOMAIN, __FUNCTION__);

    return ret;
}


static virDomainDefPtr xenGetDomainDefForName(virConnectPtr conn, const char *name)
{
    virDomainDefPtr ret;

    ret = xenDaemonLookupByName(conn, name);

    if (!ret && virGetLastError() == NULL)
        virReportError(VIR_ERR_NO_DOMAIN, __FUNCTION__);

    return ret;
}


static virDomainDefPtr xenGetDomainDefForUUID(virConnectPtr conn, const unsigned char *uuid)
{
    virDomainDefPtr ret;

    ret = xenHypervisorLookupDomainByUUID(conn, uuid);

131 132 133
    /* Try xend for inactive domains. */
    if (!ret)
        ret = xenDaemonLookupByUUID(conn, uuid);
134 135 136 137 138 139 140 141

    if (!ret && virGetLastError() == NULL)
        virReportError(VIR_ERR_NO_DOMAIN, __FUNCTION__);

    return ret;
}


142 143 144 145 146 147 148
static virDomainDefPtr xenGetDomainDefForDom(virDomainPtr dom)
{
    /* UUID lookup is more efficient than name lookup */
    return xenGetDomainDefForUUID(dom->conn, dom->uuid);
}


149 150 151 152 153
/**
 * xenNumaInit:
 * @conn: pointer to the hypervisor connection
 *
 * Initializer for previous variables. We currently assume that
R
Richard W.M. Jones 已提交
154
 * the number of physical CPU and the number of NUMA cell is fixed
155 156 157
 * until reboot which might be false in future Xen implementations.
 */
static void
158 159
xenNumaInit(virConnectPtr conn)
{
160
    virNodeInfo nodeInfo;
D
Daniel P. Berrange 已提交
161
    xenUnifiedPrivatePtr priv;
162 163 164 165 166 167
    int ret;

    ret = xenUnifiedNodeGetInfo(conn, &nodeInfo);
    if (ret < 0)
        return;

D
Daniel P. Berrange 已提交
168
    priv = conn->privateData;
169

D
Daniel P. Berrange 已提交
170 171
    priv->nbNodeCells = nodeInfo.nodes;
    priv->nbNodeCpus = nodeInfo.cpus;
172 173
}

D
Daniel P. Berrange 已提交
174

175 176 177
/**
 * xenDomainUsedCpus:
 * @dom: the domain
178
 * @def: the domain definition
179 180 181 182 183 184 185 186
 *
 * Analyze which set of CPUs are used by the domain and
 * return a string providing the ranges.
 *
 * Returns the string which needs to be freed by the caller or
 *         NULL if the domain uses all CPU or in case of error.
 */
char *
187
xenDomainUsedCpus(virDomainPtr dom, virDomainDefPtr def)
188 189
{
    char *res = NULL;
D
Daniel P. Berrange 已提交
190
    int ncpus;
191
    int nb_vcpu;
192
    virBitmapPtr cpulist = NULL;
193 194 195 196 197 198
    unsigned char *cpumap = NULL;
    size_t cpumaplen;
    int nb = 0;
    int n, m;
    virVcpuInfoPtr cpuinfo = NULL;
    virNodeInfo nodeinfo;
D
Daniel P. Berrange 已提交
199
    xenUnifiedPrivatePtr priv;
200

D
Daniel P. Berrange 已提交
201 202 203
    priv = dom->conn->privateData;

    if (priv->nbNodeCpus <= 0)
204
        return NULL;
205 206 207
    nb_vcpu = xenUnifiedDomainGetVcpusFlagsInternal(dom, def,
                                                    (VIR_DOMAIN_VCPU_LIVE |
                                                     VIR_DOMAIN_VCPU_MAXIMUM));
208
    if (nb_vcpu <= 0)
209
        return NULL;
210
    if (xenUnifiedNodeGetInfo(dom->conn, &nodeinfo) < 0)
211
        return NULL;
212

213
    if (!(cpulist = virBitmapNew(priv->nbNodeCpus)))
214
        goto done;
215
    if (VIR_ALLOC_N(cpuinfo, nb_vcpu) < 0)
216 217
        goto done;
    cpumaplen = VIR_CPU_MAPLEN(VIR_NODEINFO_MAXCPUS(nodeinfo));
218
    if (xalloc_oversized(nb_vcpu, cpumaplen) ||
219
        VIR_ALLOC_N(cpumap, nb_vcpu * cpumaplen) < 0)
220 221
        goto done;

222 223
    if ((ncpus = xenUnifiedDomainGetVcpusInternal(dom, def, cpuinfo, nb_vcpu,
                                                  cpumap, cpumaplen)) >= 0) {
224 225
        for (n = 0; n < ncpus; n++) {
            for (m = 0; m < priv->nbNodeCpus; m++) {
J
Ján Tomko 已提交
226
                if (!virBitmapIsBitSet(cpulist, m) &&
227
                    (VIR_CPU_USABLE(cpumap, cpumaplen, n, m))) {
228
                    ignore_value(virBitmapSetBit(cpulist, m));
229 230
                    nb++;
                    /* if all CPU are used just return NULL */
D
Daniel P. Berrange 已提交
231
                    if (nb == priv->nbNodeCpus)
232 233 234 235 236
                        goto done;

                }
            }
        }
237
        res = virBitmapFormat(cpulist);
238 239
    }

240
 done:
241
    virBitmapFree(cpulist);
242 243
    VIR_FREE(cpumap);
    VIR_FREE(cpuinfo);
244
    return res;
245 246
}

J
John Levon 已提交
247
static int
248
xenUnifiedStateInitialize(bool privileged,
249 250
                          virStateInhibitCallback callback ATTRIBUTE_UNUSED,
                          void *opaque ATTRIBUTE_UNUSED)
J
John Levon 已提交
251
{
252
    /* Don't allow driver to work in non-root libvirtd */
J
Jim Fehlig 已提交
253
    if (privileged) {
254
        is_privileged = true;
J
Jim Fehlig 已提交
255 256 257 258 259 260 261 262 263 264
        hostsysinfo = virSysinfoRead();
    }

    return 0;
}

static int
xenUnifiedStateCleanup(void)
{
    virSysinfoDefFree(hostsysinfo);
J
John Levon 已提交
265 266 267 268
    return 0;
}

static virStateDriver state_driver = {
269
    .name = "Xen",
270
    .stateInitialize = xenUnifiedStateInitialize,
J
Jim Fehlig 已提交
271
    .stateCleanup = xenUnifiedStateCleanup,
J
John Levon 已提交
272 273
};

274 275 276 277 278 279 280 281 282 283 284
/*----- Dispatch functions. -----*/

/* These dispatch functions follow the model used historically
 * by libvirt.c -- trying each low-level Xen driver in turn
 * until one succeeds.  However since we know what low-level
 * drivers can perform which functions, it is probably better
 * in future to optimise these dispatch functions to just call
 * the single function (or small number of appropriate functions)
 * in the low level drivers directly.
 */

285
static int
286
xenUnifiedProbe(void)
287 288 289
{
#ifdef __linux__
    if (virFileExists("/proc/xen"))
290
        return 1;
291
#endif
292
#ifdef __sun
293
    int fd;
294

295 296
    if ((fd = open("/dev/xen/domcaps", O_RDONLY)) >= 0) {
        VIR_FORCE_CLOSE(fd);
297
        return 1;
298 299
    }
#endif
300
    return 0;
301 302
}

J
Jim Fehlig 已提交
303
#ifdef WITH_LIBXL
304
static bool
305
xenUnifiedXendProbe(void)
J
Jim Fehlig 已提交
306
{
307
    bool ret = false;
J
Jim Fehlig 已提交
308

309 310 311 312 313 314 315 316
    if (virFileExists("/usr/sbin/xend")) {
        virCommandPtr cmd;

        cmd = virCommandNewArgList("/usr/sbin/xend", "status", NULL);
        if (virCommandRun(cmd, NULL) == 0)
            ret = true;
        virCommandFree(cmd);
    }
J
Jim Fehlig 已提交
317 318 319 320 321

    return ret;
}
#endif

322

323 324
static int
xenDomainDeviceDefPostParse(virDomainDeviceDefPtr dev,
325
                            const virDomainDef *def,
326
                            virCapsPtr caps ATTRIBUTE_UNUSED,
327
                            unsigned int parseFlags ATTRIBUTE_UNUSED,
328 329
                            void *opaque ATTRIBUTE_UNUSED,
                            void *parseOpaque ATTRIBUTE_UNUSED)
330 331 332 333
{
    if (dev->type == VIR_DOMAIN_DEVICE_CHR &&
        dev->data.chr->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE &&
        dev->data.chr->targetType == VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_NONE &&
334
        def->os.type != VIR_DOMAIN_OSTYPE_HVM)
335 336
        dev->data.chr->targetType = VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_XEN;

337 338 339 340 341 342 343 344 345 346
    /* forbid capabilities mode hostdev in this kind of hypervisor */
    if (dev->type == VIR_DOMAIN_DEVICE_HOSTDEV &&
        dev->data.hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_CAPABILITIES) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("hostdev mode 'capabilities' is not "
                         "supported in %s"),
                       virDomainVirtTypeToString(def->virtType));
        return -1;
    }

347 348 349 350 351
    if (dev->type == VIR_DOMAIN_DEVICE_VIDEO && dev->data.video->vram == 0) {
        switch (dev->data.video->type) {
        case VIR_DOMAIN_VIDEO_TYPE_VGA:
        case VIR_DOMAIN_VIDEO_TYPE_CIRRUS:
        case VIR_DOMAIN_VIDEO_TYPE_VMVGA:
352
            dev->data.video->vram = 16 * 1024;
353 354 355 356 357 358 359 360 361 362 363 364 365
        break;

        case VIR_DOMAIN_VIDEO_TYPE_XEN:
            /* Original Xen PVFB hardcoded to 4 MB */
            dev->data.video->vram = 4 * 1024;
            break;

        case VIR_DOMAIN_VIDEO_TYPE_QXL:
            /* Use 64M as the minimal video video memory for qxl device */
            return 64 * 1024;
        }
    }

366 367 368 369 370 371 372
    return 0;
}


static int
xenDomainDefPostParse(virDomainDefPtr def,
                      virCapsPtr caps ATTRIBUTE_UNUSED,
373
                      unsigned int parseFlags ATTRIBUTE_UNUSED,
374 375
                      void *opaque ATTRIBUTE_UNUSED,
                      void *parseOpaque ATTRIBUTE_UNUSED)
376
{
377 378 379 380 381 382 383 384 385
    if (!def->memballoon) {
        virDomainMemballoonDefPtr memballoon;
        if (VIR_ALLOC(memballoon) < 0)
            return -1;

        memballoon->model = VIR_DOMAIN_MEMBALLOON_MODEL_XEN;
        def->memballoon = memballoon;
    }

P
Pavel Hrdina 已提交
386 387 388 389
    /* add implicit input device */
    if (xenDomainDefAddImplicitInputDevice(def) <0)
        return -1;

390 391 392 393
    return 0;
}


394 395
virDomainDefParserConfig xenDomainDefParserConfig = {
    .macPrefix = { 0x00, 0x16, 0x3e },
396
    .devicesPostParseCallback = xenDomainDeviceDefPostParse,
397
    .domainPostParseCallback = xenDomainDefPostParse,
398 399
};

400

401 402 403 404 405 406 407 408
virDomainXMLOptionPtr
xenDomainXMLConfInit(void)
{
    return virDomainXMLOptionNew(&xenDomainDefParserConfig,
                                 NULL, NULL);
}


409
static virDrvOpenStatus
410 411 412
xenUnifiedConnectOpen(virConnectPtr conn, virConnectAuthPtr auth,
                      virConfPtr conf ATTRIBUTE_UNUSED,
                      unsigned int flags)
413
{
414
    xenUnifiedPrivatePtr priv;
415

J
John Levon 已提交
416 417 418 419
    /*
     * Only the libvirtd instance can open this driver.
     * Everything else falls back to the remote driver.
     */
420
    if (!is_privileged)
J
John Levon 已提交
421 422
        return VIR_DRV_OPEN_DECLINED;

423 424 425 426
    if (conn->uri == NULL) {
        if (!xenUnifiedProbe())
            return VIR_DRV_OPEN_DECLINED;

427 428 429 430 431 432 433
#ifdef WITH_LIBXL
        /* Decline xen:// URI if xend is not running and libxenlight
         * driver is potentially available. */
        if (!xenUnifiedXendProbe())
            return VIR_DRV_OPEN_DECLINED;
#endif

434
        if (!(conn->uri = virURIParse("xen:///")))
435
            return VIR_DRV_OPEN_ERROR;
436 437 438 439 440 441 442
    } else {
        if (conn->uri->scheme) {
            /* Decline any scheme which isn't "xen://" or "http://". */
            if (STRCASENEQ(conn->uri->scheme, "xen") &&
                STRCASENEQ(conn->uri->scheme, "http"))
                return VIR_DRV_OPEN_DECLINED;

443 444 445 446 447 448
#ifdef WITH_LIBXL
            /* Decline xen:// URI if xend is not running and libxenlight
             * driver is potentially available. */
            if (!xenUnifiedXendProbe())
                return VIR_DRV_OPEN_DECLINED;
#endif
449 450 451 452 453

            /* Return an error if the path isn't '' or '/' */
            if (conn->uri->path &&
                STRNEQ(conn->uri->path, "") &&
                STRNEQ(conn->uri->path, "/")) {
454 455 456
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("unexpected Xen URI path '%s', try xen:///"),
                               conn->uri->path);
457 458
                return VIR_DRV_OPEN_ERROR;
            }
459

460 461 462 463 464 465
            /* Decline any xen:// URI with a server specified, allowing remote
             * driver to handle, but keep any http:/// URIs */
            if (STRCASEEQ(conn->uri->scheme, "xen") &&
                conn->uri->server)
                return VIR_DRV_OPEN_DECLINED;
        } else {
466
            return VIR_DRV_OPEN_DECLINED;
467 468
        }
    }
469

470 471
    /* We now know the URI is definitely for this driver, so beyond
     * here, don't return DECLINED, always use ERROR */
472

473 474 475
    if (virConnectOpenEnsureACL(conn) < 0)
        return VIR_DRV_OPEN_ERROR;

476
    /* Allocate per-connection private data. */
477
    if (VIR_ALLOC(priv) < 0)
478
        return VIR_DRV_OPEN_ERROR;
D
Daniel P. Berrange 已提交
479
    if (virMutexInit(&priv->lock) < 0) {
480 481
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("cannot initialize mutex"));
D
Daniel P. Berrange 已提交
482 483 484
        VIR_FREE(priv);
        return VIR_DRV_OPEN_ERROR;
    }
485

486
    if (!(priv->domainEvents = virObjectEventStateNew())) {
D
Daniel P. Berrange 已提交
487 488
        virMutexDestroy(&priv->lock);
        VIR_FREE(priv);
489 490
        return VIR_DRV_OPEN_ERROR;
    }
D
Daniel P. Berrange 已提交
491 492
    conn->privateData = priv;

493 494 495 496
    priv->handle = -1;
    priv->xshandle = NULL;


497 498 499 500 501 502
    /* Hypervisor required to succeed */
    VIR_DEBUG("Trying hypervisor sub-driver");
    if (xenHypervisorOpen(conn, auth, flags) < 0)
        goto error;
    VIR_DEBUG("Activated hypervisor sub-driver");
    priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] = 1;
503

504
    /* XenD is required to succeed */
505
    VIR_DEBUG("Trying XenD sub-driver");
506 507 508 509 510 511 512 513 514 515
    if (xenDaemonOpen(conn, auth, flags) < 0)
        goto error;
    VIR_DEBUG("Activated XenD sub-driver");
    priv->opened[XEN_UNIFIED_XEND_OFFSET] = 1;

    VIR_DEBUG("Trying XS sub-driver");
    if (xenStoreOpen(conn, auth, flags) < 0)
        goto error;
    VIR_DEBUG("Activated XS sub-driver");
    priv->opened[XEN_UNIFIED_XS_OFFSET] = 1;
516

D
Daniel P. Berrange 已提交
517 518
    xenNumaInit(conn);

519
    if (!(priv->caps = xenHypervisorMakeCapabilities(conn))) {
520
        VIR_DEBUG("Failed to make capabilities");
521
        goto error;
522 523
    }

524
    if (!(priv->xmlopt = xenDomainXMLConfInit()))
525
        goto error;
526

527
#if WITH_XEN_INOTIFY
528 529 530 531 532
    VIR_DEBUG("Trying Xen inotify sub-driver");
    if (xenInotifyOpen(conn, auth, flags) < 0)
        goto error;
    VIR_DEBUG("Activated Xen inotify sub-driver");
    priv->opened[XEN_UNIFIED_INOTIFY_OFFSET] = 1;
533 534
#endif

535
    if (VIR_STRDUP(priv->saveDir, XEN_SAVE_DIR) < 0)
536
        goto error;
537 538

    if (virFileMakePath(priv->saveDir) < 0) {
539 540
        virReportSystemError(errno, _("Errored to create save dir '%s'"),
                             priv->saveDir);
541
        goto error;
542 543
    }

544
    return VIR_DRV_OPEN_SUCCESS;
545

546
 error:
547
    VIR_DEBUG("Failed to activate a mandatory sub-driver");
548 549 550 551 552 553 554 555 556 557 558 559
#if WITH_XEN_INOTIFY
    if (priv->opened[XEN_UNIFIED_INOTIFY_OFFSET])
        xenInotifyClose(conn);
#endif
    if (priv->opened[XEN_UNIFIED_XM_OFFSET])
        xenXMClose(conn);
    if (priv->opened[XEN_UNIFIED_XS_OFFSET])
        xenStoreClose(conn);
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        xenDaemonClose(conn);
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET])
        xenHypervisorClose(conn);
D
Daniel P. Berrange 已提交
560
    virMutexDestroy(&priv->lock);
561
    VIR_FREE(priv->saveDir);
562
    VIR_FREE(priv);
D
Daniel P. Berrange 已提交
563
    conn->privateData = NULL;
564
    return VIR_DRV_OPEN_ERROR;
565 566 567
}

static int
568
xenUnifiedConnectClose(virConnectPtr conn)
569
{
570
    xenUnifiedPrivatePtr priv = conn->privateData;
571

572
    virObjectUnref(priv->caps);
573
    virObjectUnref(priv->xmlopt);
574
    virObjectUnref(priv->domainEvents);
575

576 577 578 579 580 581 582 583 584 585 586 587
#if WITH_XEN_INOTIFY
    if (priv->opened[XEN_UNIFIED_INOTIFY_OFFSET])
        xenInotifyClose(conn);
#endif
    if (priv->opened[XEN_UNIFIED_XM_OFFSET])
        xenXMClose(conn);
    if (priv->opened[XEN_UNIFIED_XS_OFFSET])
        xenStoreClose(conn);
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        xenDaemonClose(conn);
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET])
        xenHypervisorClose(conn);
588

589
    VIR_FREE(priv->saveDir);
D
Daniel P. Berrange 已提交
590 591
    virMutexDestroy(&priv->lock);
    VIR_FREE(conn->privateData);
592

593
    return 0;
594 595
}

596 597 598 599 600 601 602 603 604 605 606

#define HV_VERSION ((DOM0_INTERFACE_VERSION >> 24) * 1000000 +         \
                    ((DOM0_INTERFACE_VERSION >> 16) & 0xFF) * 1000 +   \
                    (DOM0_INTERFACE_VERSION & 0xFFFF))

unsigned long xenUnifiedVersion(void)
{
    return HV_VERSION;
}


607
static const char *
608
xenUnifiedConnectGetType(virConnectPtr conn)
609
{
610 611 612
    if (virConnectGetTypeEnsureACL(conn) < 0)
        return NULL;

613
    return "Xen";
614 615
}

616 617
/* Which features are supported by this driver? */
static int
618
xenUnifiedConnectSupportsFeature(virConnectPtr conn, int feature)
619
{
620 621 622
    if (virConnectSupportsFeatureEnsureACL(conn) < 0)
        return -1;

623
    switch (feature) {
624 625 626 627 628
    case VIR_DRV_FEATURE_MIGRATION_V1:
    case VIR_DRV_FEATURE_MIGRATION_DIRECT:
        return 1;
    default:
        return 0;
629 630 631
    }
}

632
static int
633
xenUnifiedConnectGetVersion(virConnectPtr conn, unsigned long *hvVer)
634
{
635 636 637
    if (virConnectGetVersionEnsureACL(conn) < 0)
        return -1;

638
    return xenHypervisorGetVersion(conn, hvVer);
639 640
}

641

642
static char *xenUnifiedConnectGetHostname(virConnectPtr conn)
643
{
644 645 646
    if (virConnectGetHostnameEnsureACL(conn) < 0)
        return NULL;

647 648 649
    return virGetHostname();
}

J
Jim Fehlig 已提交
650 651 652 653 654 655 656 657
static char *
xenUnifiedConnectGetSysinfo(virConnectPtr conn ATTRIBUTE_UNUSED,
                            unsigned int flags)
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    virCheckFlags(0, NULL);

658 659 660
    if (virConnectGetSysinfoEnsureACL(conn) < 0)
        return NULL;

J
Jim Fehlig 已提交
661 662 663 664 665 666 667 668
    if (!hostsysinfo) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Host SMBIOS information is not available"));
        return NULL;
    }

    if (virSysinfoFormat(&buf, hostsysinfo) < 0)
        return NULL;
669
    if (virBufferCheckError(&buf) < 0)
J
Jim Fehlig 已提交
670 671 672
        return NULL;
    return virBufferContentAndReset(&buf);
}
673

674
static int
675
xenUnifiedConnectIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
676 677 678 679 680
{
    return 0;
}

static int
681
xenUnifiedConnectIsSecure(virConnectPtr conn)
682
{
683
    xenUnifiedPrivatePtr priv = conn->privateData;
684 685 686 687 688 689 690 691 692 693
    int ret = 1;

    /* All drivers are secure, except for XenD over TCP */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET] &&
        priv->addrfamily != AF_UNIX)
        ret = 0;

    return ret;
}

694
static int
695
xenUnifiedConnectIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
696 697 698 699 700
{
    /* XenD reconnects for each request */
    return 1;
}

701
int
702
xenUnifiedConnectGetMaxVcpus(virConnectPtr conn, const char *type)
703
{
704 705 706
    if (virConnectGetMaxVcpusEnsureACL(conn) < 0)
        return -1;

707
    if (type && STRCASENEQ(type, "Xen")) {
708
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
709 710
        return -1;
    }
711

712
    return xenHypervisorGetMaxVcpus(conn, type);
713 714 715
}

static int
716
xenUnifiedNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info)
717
{
718 719 720
    if (virNodeGetInfoEnsureACL(conn) < 0)
        return -1;

721
    return xenDaemonNodeGetInfo(conn, info);
722 723 724
}

static char *
725
xenUnifiedConnectGetCapabilities(virConnectPtr conn)
726
{
D
Daniel P. Berrange 已提交
727
    xenUnifiedPrivatePtr priv = conn->privateData;
728

729 730 731
    if (virConnectGetCapabilitiesEnsureACL(conn) < 0)
        return NULL;

732
    return virCapabilitiesFormatXML(priv->caps);
733 734 735
}

static int
736
xenUnifiedConnectListDomains(virConnectPtr conn, int *ids, int maxids)
737
{
738 739 740
    if (virConnectListDomainsEnsureACL(conn) < 0)
        return -1;

741
    return xenStoreListDomains(conn, ids, maxids);
742 743 744
}

static int
745
xenUnifiedConnectNumOfDomains(virConnectPtr conn)
746
{
747 748 749
    if (virConnectNumOfDomainsEnsureACL(conn) < 0)
        return -1;

750
    return xenStoreNumOfDomains(conn);
751 752 753
}

static virDomainPtr
754
xenUnifiedDomainCreateXML(virConnectPtr conn,
755 756
                          const char *xml,
                          unsigned int flags)
757
{
758 759 760
    xenUnifiedPrivatePtr priv = conn->privateData;
    virDomainDefPtr def = NULL;
    virDomainPtr ret = NULL;
761
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;
762

763 764 765
    virCheckFlags(VIR_DOMAIN_START_VALIDATE, NULL);

    if (flags & VIR_DOMAIN_START_VALIDATE)
766
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA;
767

768
    if (!(def = virDomainDefParseString(xml, priv->caps, priv->xmlopt,
769
                                        NULL, parse_flags)))
770 771
        goto cleanup;

772 773 774
    if (virDomainCreateXMLEnsureACL(conn, def) < 0)
        goto cleanup;

775 776 777 778 779 780 781
    if (xenDaemonCreateXML(conn, def) < 0)
        goto cleanup;

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

782
 cleanup:
783 784
    virDomainDefFree(def);
    return ret;
785 786 787
}

static virDomainPtr
788
xenUnifiedDomainLookupByID(virConnectPtr conn, int id)
789
{
790
    virDomainPtr ret = NULL;
791
    virDomainDefPtr def = NULL;
792

793 794
    if (!(def = xenGetDomainDefForID(conn, id)))
        goto cleanup;
795

796 797 798
    if (virDomainLookupByIDEnsureACL(conn, def) < 0)
        goto cleanup;

799 800
    if (!(ret = virGetDomain(conn, def->name, def->uuid)))
        goto cleanup;
801

802 803
    ret->id = def->id;

804
 cleanup:
805
    virDomainDefFree(def);
806
    return ret;
807 808 809
}

static virDomainPtr
810 811
xenUnifiedDomainLookupByUUID(virConnectPtr conn,
                             const unsigned char *uuid)
812
{
813 814
    virDomainPtr ret = NULL;
    virDomainDefPtr def = NULL;
815

816 817
    if (!(def = xenGetDomainDefForUUID(conn, uuid)))
        goto cleanup;
818

819 820 821
    if (virDomainLookupByUUIDEnsureACL(conn, def) < 0)
        goto cleanup;

822 823
    if (!(ret = virGetDomain(conn, def->name, def->uuid)))
        goto cleanup;
824

825
    ret->id = def->id;
826

827
 cleanup:
828
    virDomainDefFree(def);
829
    return ret;
830 831 832
}

static virDomainPtr
833 834
xenUnifiedDomainLookupByName(virConnectPtr conn,
                             const char *name)
835
{
836 837
    virDomainPtr ret = NULL;
    virDomainDefPtr def = NULL;
838

839 840
    if (!(def = xenGetDomainDefForName(conn, name)))
        goto cleanup;
841

842 843 844
    if (virDomainLookupByNameEnsureACL(conn, def) < 0)
        goto cleanup;

845 846
    if (!(ret = virGetDomain(conn, def->name, def->uuid)))
        goto cleanup;
847

848
    ret->id = def->id;
849

850
 cleanup:
851
    virDomainDefFree(def);
852
    return ret;
853 854
}

855 856 857 858

static int
xenUnifiedDomainIsActive(virDomainPtr dom)
{
859
    virDomainDefPtr def;
860 861
    int ret = -1;

862 863
    if (!(def = xenGetDomainDefForUUID(dom->conn, dom->uuid)))
        goto cleanup;
864

865
    ret = def->id == -1 ? 0 : 1;
866

867
 cleanup:
868
    virDomainDefFree(def);
869 870 871 872
    return ret;
}

static int
873
xenUnifiedDomainIsPersistent(virDomainPtr dom)
874
{
875
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
876
    virDomainDefPtr def = NULL;
877 878 879 880 881
    int ret = -1;

    if (priv->opened[XEN_UNIFIED_XM_OFFSET]) {
        /* Old Xen, pre-inactive domain management.
         * If the XM driver can see the guest, it is definitely persistent */
882 883
        def = xenXMDomainLookupByUUID(dom->conn, dom->uuid);
        if (def)
884 885 886 887 888
            ret = 1;
        else
            ret = 0;
    } else {
        /* New Xen with inactive domain management */
889 890 891
        def = xenDaemonLookupByUUID(dom->conn, dom->uuid);
        if (def) {
            if (def->id == -1) {
892 893 894 895 896 897 898 899 900
                /* If its inactive, then trivially, it must be persistent */
                ret = 1;
            } else {
                char *path;
                char uuidstr[VIR_UUID_STRING_BUFLEN];

                /* If its running there's no official way to tell, so we
                 * go behind xend's back & look at the config dir */
                virUUIDFormat(dom->uuid, uuidstr);
901
                if (virAsprintf(&path, "%s/%s", XEND_DOMAINS_DIR, uuidstr) < 0)
902
                    goto cleanup;
903 904 905 906
                if (access(path, R_OK) == 0)
                    ret = 1;
                else if (errno == ENOENT)
                    ret = 0;
907 908 909 910
            }
        }
    }

911
 cleanup:
912
    virDomainDefFree(def);
913 914 915 916

    return ret;
}

917 918 919 920 921 922
static int
xenUnifiedDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

923
static int
924
xenUnifiedDomainSuspend(virDomainPtr dom)
925
{
926 927 928 929 930 931
    int ret = -1;
    virDomainDefPtr def;

    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

932 933 934
    if (virDomainSuspendEnsureACL(dom->conn, def) < 0)
        goto cleanup;

935 936
    ret = xenDaemonDomainSuspend(dom->conn, def);

937
 cleanup:
938 939
    virDomainDefFree(def);
    return ret;
940 941 942
}

static int
943
xenUnifiedDomainResume(virDomainPtr dom)
944
{
945 946 947 948 949 950
    int ret = -1;
    virDomainDefPtr def;

    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

951 952 953
    if (virDomainResumeEnsureACL(dom->conn, def) < 0)
        goto cleanup;

954 955
    ret = xenDaemonDomainResume(dom->conn, def);

956
 cleanup:
957 958
    virDomainDefFree(def);
    return ret;
959 960 961
}

static int
962 963
xenUnifiedDomainShutdownFlags(virDomainPtr dom,
                              unsigned int flags)
964
{
965 966 967
    int ret = -1;
    virDomainDefPtr def;

968 969
    virCheckFlags(0, -1);

970 971 972
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

973
    if (virDomainShutdownFlagsEnsureACL(dom->conn, def, flags) < 0)
974 975
        goto cleanup;

976 977
    ret = xenDaemonDomainShutdown(dom->conn, def);

978
 cleanup:
979 980
    virDomainDefFree(def);
    return ret;
981 982
}

983 984 985 986 987 988
static int
xenUnifiedDomainShutdown(virDomainPtr dom)
{
    return xenUnifiedDomainShutdownFlags(dom, 0);
}

989
static int
990
xenUnifiedDomainReboot(virDomainPtr dom, unsigned int flags)
991
{
992 993 994
    int ret = -1;
    virDomainDefPtr def;

995
    virCheckFlags(0, -1);
996

997 998 999
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1000
    if (virDomainRebootEnsureACL(dom->conn, def, flags) < 0)
1001 1002
        goto cleanup;

1003 1004
    ret = xenDaemonDomainReboot(dom->conn, def);

1005
 cleanup:
1006 1007
    virDomainDefFree(def);
    return ret;
1008 1009
}

1010 1011 1012 1013
static int
xenUnifiedDomainDestroyFlags(virDomainPtr dom,
                             unsigned int flags)
{
1014 1015 1016
    int ret = -1;
    virDomainDefPtr def;

1017 1018
    virCheckFlags(0, -1);

1019 1020 1021
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1022 1023 1024
    if (virDomainDestroyFlagsEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1025 1026
    ret = xenDaemonDomainDestroy(dom->conn, def);

1027
 cleanup:
1028 1029
    virDomainDefFree(def);
    return ret;
1030 1031
}

1032 1033 1034 1035 1036 1037
static int
xenUnifiedDomainDestroy(virDomainPtr dom)
{
    return xenUnifiedDomainDestroyFlags(dom, 0);
}

1038
static char *
1039
xenUnifiedDomainGetOSType(virDomainPtr dom)
1040
{
1041 1042 1043 1044 1045
    char *ret = NULL;
    virDomainDefPtr def;

    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;
1046

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

1050 1051 1052
    if (def->id < 0)
        ret = xenDaemonDomainGetOSType(dom->conn, def);
    else
1053 1054
        ret = xenHypervisorDomainGetOSType(dom->conn, def);

1055
 cleanup:
1056 1057
    virDomainDefFree(def);
    return ret;
1058 1059
}

1060

1061
static unsigned long long
1062
xenUnifiedDomainGetMaxMemory(virDomainPtr dom)
1063
{
1064 1065 1066 1067 1068
    unsigned long long ret = 0;
    virDomainDefPtr def;

    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;
1069

1070 1071 1072
    if (virDomainGetMaxMemoryEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1073 1074 1075
    if (def->id < 0)
        ret = xenDaemonDomainGetMaxMemory(dom->conn, def);
    else
1076 1077
        ret = xenHypervisorGetMaxMemory(dom->conn, def);

1078
 cleanup:
1079 1080
    virDomainDefFree(def);
    return ret;
1081 1082 1083
}

static int
1084
xenUnifiedDomainSetMaxMemory(virDomainPtr dom, unsigned long memory)
1085
{
1086 1087
    int ret = -1;
    virDomainDefPtr def;
1088

1089 1090 1091
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1092 1093 1094
    if (virDomainSetMaxMemoryEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1095 1096 1097
    if (def->id < 0)
        ret = xenDaemonDomainSetMaxMemory(dom->conn, def, memory);
    else
1098 1099
        ret = xenHypervisorSetMaxMemory(dom->conn, def, memory);

1100
 cleanup:
1101 1102
    virDomainDefFree(def);
    return ret;
1103 1104 1105
}

static int
1106
xenUnifiedDomainSetMemory(virDomainPtr dom, unsigned long memory)
1107
{
1108 1109 1110 1111 1112
    int ret = -1;
    virDomainDefPtr def;

    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;
1113

1114 1115 1116
    if (virDomainSetMemoryEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1117
    ret = xenDaemonDomainSetMemory(dom->conn, def, memory);
1118

1119
 cleanup:
1120 1121
    virDomainDefFree(def);
    return ret;
1122 1123 1124
}

static int
1125
xenUnifiedDomainGetInfo(virDomainPtr dom, virDomainInfoPtr info)
1126
{
1127 1128 1129 1130 1131
    int ret = -1;
    virDomainDefPtr def;

    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;
1132

1133 1134 1135
    if (virDomainGetInfoEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1136 1137 1138
    if (def->id < 0)
        ret = xenDaemonDomainGetInfo(dom->conn, def, info);
    else
1139 1140
        ret = xenHypervisorGetDomainInfo(dom->conn, def, info);

1141
 cleanup:
1142 1143
    virDomainDefFree(def);
    return ret;
1144 1145
}

1146 1147 1148 1149 1150 1151
static int
xenUnifiedDomainGetState(virDomainPtr dom,
                         int *state,
                         int *reason,
                         unsigned int flags)
{
1152

1153 1154
    int ret = -1;
    virDomainDefPtr def;
1155 1156 1157

    virCheckFlags(0, -1);

1158 1159 1160
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1161 1162 1163
    if (virDomainGetStateEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1164 1165 1166
    if (def->id < 0)
        ret = xenDaemonDomainGetState(dom->conn, def, state, reason);
    else
1167 1168
        ret = xenHypervisorGetDomainState(dom->conn, def, state, reason);

1169
 cleanup:
1170 1171
    virDomainDefFree(def);
    return ret;
1172 1173
}

1174
static int
1175 1176
xenUnifiedDomainSaveFlags(virDomainPtr dom, const char *to, const char *dxml,
                          unsigned int flags)
1177
{
1178 1179 1180
    int ret = -1;
    virDomainDefPtr def;

1181
    virCheckFlags(0, -1);
1182

1183
    if (dxml) {
1184 1185
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
1186 1187 1188
        return -1;
    }

1189 1190 1191
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1192 1193 1194
    if (virDomainSaveFlagsEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1195 1196
    ret = xenDaemonDomainSave(dom->conn, def, to);

1197
 cleanup:
1198 1199
    virDomainDefFree(def);
    return ret;
1200 1201 1202
}

static int
1203 1204 1205 1206 1207
xenUnifiedDomainSave(virDomainPtr dom, const char *to)
{
    return xenUnifiedDomainSaveFlags(dom, to, NULL, 0);
}

1208
static char *
1209 1210
xenUnifiedDomainManagedSavePath(xenUnifiedPrivatePtr priv,
                                virDomainDefPtr def)
1211 1212 1213
{
    char *ret;

1214
    if (virAsprintf(&ret, "%s/%s.save", priv->saveDir, def->name) < 0)
1215 1216 1217 1218 1219 1220 1221 1222 1223
        return NULL;

    VIR_DEBUG("managed save image: %s", ret);
    return ret;
}

static int
xenUnifiedDomainManagedSave(virDomainPtr dom, unsigned int flags)
{
1224
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1225 1226
    char *name = NULL;
    virDomainDefPtr def = NULL;
1227 1228 1229 1230
    int ret = -1;

    virCheckFlags(0, -1);

1231 1232 1233
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1234 1235 1236
    if (virDomainManagedSaveEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1237
    if (!(name = xenUnifiedDomainManagedSavePath(priv, def)))
1238 1239
        goto cleanup;

1240
    ret = xenDaemonDomainSave(dom->conn, def, name);
1241

1242
 cleanup:
1243
    VIR_FREE(name);
1244
    virDomainDefFree(def);
1245 1246 1247 1248 1249 1250
    return ret;
}

static int
xenUnifiedDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
{
1251
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1252 1253
    char *name = NULL;
    virDomainDefPtr def = NULL;
1254 1255 1256 1257
    int ret = -1;

    virCheckFlags(0, -1);

1258 1259 1260
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1261 1262 1263
    if (virDomainHasManagedSaveImageEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1264 1265
    if (!(name = xenUnifiedDomainManagedSavePath(priv, def)))
        goto cleanup;
1266 1267

    ret = virFileExists(name);
1268

1269
 cleanup:
1270
    VIR_FREE(name);
1271
    virDomainDefFree(def);
1272 1273 1274 1275 1276 1277
    return ret;
}

static int
xenUnifiedDomainManagedSaveRemove(virDomainPtr dom, unsigned int flags)
{
1278
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1279 1280
    char *name = NULL;
    virDomainDefPtr def = NULL;
1281 1282 1283 1284
    int ret = -1;

    virCheckFlags(0, -1);

1285 1286 1287
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1288 1289 1290
    if (virDomainManagedSaveRemoveEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1291 1292
    if (!(name = xenUnifiedDomainManagedSavePath(priv, def)))
        goto cleanup;
1293 1294

    ret = unlink(name);
1295

1296
 cleanup:
1297 1298 1299 1300
    VIR_FREE(name);
    return ret;
}

1301 1302 1303
static int
xenUnifiedDomainRestoreFlags(virConnectPtr conn, const char *from,
                             const char *dxml, unsigned int flags)
1304
{
1305 1306
    virCheckFlags(0, -1);
    if (dxml) {
1307 1308
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
1309 1310 1311
        return -1;
    }

1312
    return xenDaemonDomainRestore(conn, from);
1313 1314
}

1315
static int
1316
xenUnifiedDomainRestore(virConnectPtr conn, const char *from)
1317 1318 1319 1320
{
    return xenUnifiedDomainRestoreFlags(conn, from, NULL, 0);
}

1321
static int
1322
xenUnifiedDomainCoreDump(virDomainPtr dom, const char *to, unsigned int flags)
1323
{
1324 1325 1326 1327 1328 1329 1330 1331
    virDomainDefPtr def = NULL;
    int ret = -1;

    virCheckFlags(0, -1);

    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1332 1333 1334
    if (virDomainCoreDumpEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1335 1336
    ret = xenDaemonDomainCoreDump(dom->conn, def, to, flags);

1337
 cleanup:
1338 1339
    virDomainDefFree(def);
    return ret;
1340 1341 1342
}

static int
1343 1344
xenUnifiedDomainSetVcpusFlags(virDomainPtr dom, unsigned int nvcpus,
                              unsigned int flags)
1345
{
1346 1347
    virDomainDefPtr def = NULL;
    int ret = -1;
1348

1349 1350 1351 1352 1353 1354 1355 1356 1357
    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)) {
1358 1359
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid flag combination: (0x%x)"), flags);
1360 1361 1362
        return -1;
    }
    if (!nvcpus || (unsigned short) nvcpus != nvcpus) {
1363 1364
        virReportError(VIR_ERR_INVALID_ARG,
                       _("argument out of range: %d"), nvcpus);
1365 1366 1367
        return -1;
    }

1368 1369 1370
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1371 1372 1373
    if (virDomainSetVcpusFlagsEnsureACL(dom->conn, def, flags) < 0)
        goto cleanup;

1374
    ret = xenDaemonDomainSetVcpusFlags(dom->conn, def, nvcpus, flags);
1375

1376
 cleanup:
1377 1378
    virDomainDefFree(def);
    return ret;
1379 1380
}

1381
static int
1382
xenUnifiedDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus)
1383
{
1384
    unsigned int flags;
1385 1386

    /* Per the documented API, it is hypervisor-dependent whether this
1387 1388 1389
     * affects just _LIVE or _LIVE|_CONFIG; in xen's case, both are
     * affected. */
    flags = VIR_DOMAIN_VCPU_LIVE | VIR_DOMAIN_VCPU_CONFIG;
1390 1391

    return xenUnifiedDomainSetVcpusFlags(dom, nvcpus, flags);
1392 1393
}

1394
static int
1395 1396
xenUnifiedDomainPinVcpu(virDomainPtr dom, unsigned int vcpu,
                        unsigned char *cpumap, int maplen)
1397
{
1398 1399 1400 1401 1402
    virDomainDefPtr def = NULL;
    int ret = -1;

    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;
1403

1404 1405 1406
    if (virDomainPinVcpuEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1407 1408 1409
    if (dom->id < 0)
        ret = xenDaemonDomainPinVcpu(dom->conn, def, vcpu, cpumap, maplen);
    else
1410 1411
        ret = xenHypervisorPinVcpu(dom->conn, def, vcpu, cpumap, maplen);

1412
 cleanup:
1413 1414
    virDomainDefFree(def);
    return ret;
1415 1416 1417
}

static int
1418 1419 1420 1421 1422 1423
xenUnifiedDomainGetVcpusInternal(virDomainPtr dom,
                                 virDomainDefPtr def,
                                 virVcpuInfoPtr info,
                                 int maxinfo,
                                 unsigned char *cpumaps,
                                 int maplen)
1424
{
1425 1426
    int ret = -1;

1427
    if (dom->id < 0) {
1428 1429
        ret = xenDaemonDomainGetVcpus(dom->conn, def, info, maxinfo,
                                      cpumaps, maplen);
1430
    } else {
1431 1432
        ret = xenHypervisorGetVcpus(dom->conn, def, info, maxinfo, cpumaps,
                                    maplen);
1433
    }
1434 1435

    return ret;
1436 1437 1438
}

static int
1439 1440 1441
xenUnifiedDomainGetVcpus(virDomainPtr dom,
                         virVcpuInfoPtr info, int maxinfo,
                         unsigned char *cpumaps, int maplen)
1442
{
1443 1444
    virDomainDefPtr def = NULL;
    int ret = -1;
1445

1446 1447 1448
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1449
    if (virDomainGetVcpusEnsureACL(dom->conn, def) < 0)
1450 1451
        goto cleanup;

1452 1453 1454
    ret = xenUnifiedDomainGetVcpusInternal(dom, def, info, maxinfo, cpumaps,
                                           maplen);

1455
 cleanup:
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
    virDomainDefFree(def);
    return ret;
}

static int
xenUnifiedDomainGetVcpusFlagsInternal(virDomainPtr dom,
                                      virDomainDefPtr def,
                                      unsigned int flags)
{
    int ret = -1;

1467
    if (dom->id < 0) {
1468
        ret = xenDaemonDomainGetVcpusFlags(dom->conn, def, flags);
1469 1470
    } else {
        if (flags == (VIR_DOMAIN_VCPU_CONFIG | VIR_DOMAIN_VCPU_MAXIMUM))
1471
            ret = xenHypervisorGetVcpuMax(dom->conn, def);
1472
        else
1473
            ret = xenDaemonDomainGetVcpusFlags(dom->conn, def, flags);
1474
    }
1475

1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
   return ret;
}

static int
xenUnifiedDomainGetVcpusFlags(virDomainPtr dom, unsigned int flags)
{
    virDomainDefPtr def = NULL;
    int ret = -1;

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

    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1492
    if (virDomainGetVcpusFlagsEnsureACL(dom->conn, def, flags) < 0)
1493 1494 1495 1496
        goto cleanup;

    ret = xenUnifiedDomainGetVcpusFlagsInternal(dom, def, flags);

1497
 cleanup:
1498 1499
    virDomainDefFree(def);
    return ret;
1500 1501
}

1502
static int
1503
xenUnifiedDomainGetMaxVcpus(virDomainPtr dom)
1504 1505 1506 1507 1508
{
    return xenUnifiedDomainGetVcpusFlags(dom, (VIR_DOMAIN_VCPU_LIVE |
                                               VIR_DOMAIN_VCPU_MAXIMUM));
}

1509
static char *
1510
xenUnifiedDomainGetXMLDesc(virDomainPtr dom, unsigned int flags)
1511
{
1512
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1513 1514 1515
    virDomainDefPtr minidef = NULL;
    virDomainDefPtr def = NULL;
    char *ret = NULL;
1516
    char *cpus = NULL;
1517 1518 1519

    if (!(minidef = xenGetDomainDefForDom(dom)))
        goto cleanup;
1520

1521 1522 1523
    if (virDomainGetXMLDescEnsureACL(dom->conn, minidef, flags) < 0)
        goto cleanup;

1524 1525 1526 1527
    xenUnifiedLock(priv);
    cpus = xenDomainUsedCpus(dom, minidef);
    xenUnifiedUnlock(priv);
    def = xenDaemonDomainGetXMLDesc(dom->conn, minidef, cpus);
1528 1529

    if (def)
1530
        ret = virDomainDefFormat(def, priv->caps,
1531
                                 virDomainDefFormatConvertXMLFlags(flags));
1532

1533
 cleanup:
1534
    VIR_FREE(cpus);
1535 1536 1537
    virDomainDefFree(def);
    virDomainDefFree(minidef);
    return ret;
1538 1539
}

1540 1541

static char *
1542 1543 1544 1545
xenUnifiedConnectDomainXMLFromNative(virConnectPtr conn,
                                     const char *format,
                                     const char *config,
                                     unsigned int flags)
1546 1547 1548 1549
{
    virDomainDefPtr def = NULL;
    char *ret = NULL;
    virConfPtr conf = NULL;
1550 1551 1552
    int id;
    char * tty;
    int vncport;
1553
    xenUnifiedPrivatePtr priv = conn->privateData;
1554

E
Eric Blake 已提交
1555 1556
    virCheckFlags(0, NULL);

1557 1558 1559
    if (virConnectDomainXMLFromNativeEnsureACL(conn) < 0)
        return NULL;

1560 1561
    if (STRNEQ(format, XEN_CONFIG_FORMAT_XM) &&
        STRNEQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
1562 1563
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unsupported config type %s"), format);
1564 1565 1566 1567
        return NULL;
    }

    if (STREQ(format, XEN_CONFIG_FORMAT_XM)) {
1568
        conf = virConfReadMem(config, strlen(config), 0);
1569 1570 1571
        if (!conf)
            goto cleanup;

1572
        def = xenParseXM(conf, priv->caps, priv->xmlopt);
1573
    } else if (STREQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
1574
        if (xenGetDomIdFromSxprString(config, &id) < 0)
1575
            goto cleanup;
1576 1577 1578 1579
        xenUnifiedLock(priv);
        tty = xenStoreDomainGetConsolePath(conn, id);
        vncport = xenStoreDomainGetVNCPort(conn, id);
        xenUnifiedUnlock(priv);
1580
        def = xenParseSxprString(config, tty,
1581
                                 vncport, priv->caps, priv->xmlopt);
1582 1583 1584 1585
    }
    if (!def)
        goto cleanup;

1586
    ret = virDomainDefFormat(def, priv->caps, 0);
1587

1588
 cleanup:
1589
    virDomainDefFree(def);
1590 1591
    if (conf)
        virConfFree(conf);
1592 1593 1594 1595 1596 1597
    return ret;
}


#define MAX_CONFIG_SIZE (1024 * 65)
static char *
1598 1599 1600 1601
xenUnifiedConnectDomainXMLToNative(virConnectPtr conn,
                                   const char *format,
                                   const char *xmlData,
                                   unsigned int flags)
1602 1603 1604 1605
{
    virDomainDefPtr def = NULL;
    char *ret = NULL;
    virConfPtr conf = NULL;
1606
    xenUnifiedPrivatePtr priv = conn->privateData;
1607

E
Eric Blake 已提交
1608 1609
    virCheckFlags(0, NULL);

1610 1611 1612
    if (virConnectDomainXMLToNativeEnsureACL(conn) < 0)
        return NULL;

1613 1614
    if (STRNEQ(format, XEN_CONFIG_FORMAT_XM) &&
        STRNEQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
1615 1616
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unsupported config type %s"), format);
1617 1618 1619
        goto cleanup;
    }

1620
    if (!(def = virDomainDefParseString(xmlData, priv->caps, priv->xmlopt,
1621
                                        NULL,
1622
                                        VIR_DOMAIN_DEF_PARSE_INACTIVE)))
1623 1624 1625 1626
        goto cleanup;

    if (STREQ(format, XEN_CONFIG_FORMAT_XM)) {
        int len = MAX_CONFIG_SIZE;
1627
        conf = xenFormatXM(conn, def);
1628 1629 1630
        if (!conf)
            goto cleanup;

1631
        if (VIR_ALLOC_N(ret, len) < 0)
1632 1633 1634 1635 1636 1637 1638
            goto cleanup;

        if (virConfWriteMem(ret, &len, conf) < 0) {
            VIR_FREE(ret);
            goto cleanup;
        }
    } else if (STREQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
1639
        ret = xenFormatSxpr(conn, def);
1640 1641
    }

1642
 cleanup:
1643 1644 1645 1646 1647 1648 1649
    virDomainDefFree(def);
    if (conf)
        virConfFree(conf);
    return ret;
}


1650
static int
1651 1652 1653 1654 1655 1656 1657 1658
xenUnifiedDomainMigratePrepare(virConnectPtr dconn,
                               char **cookie,
                               int *cookielen,
                               const char *uri_in,
                               char **uri_out,
                               unsigned long flags,
                               const char *dname,
                               unsigned long resource)
1659
{
E
Eric Blake 已提交
1660 1661
    virCheckFlags(XEN_MIGRATION_FLAGS, -1);

1662 1663 1664
    return xenDaemonDomainMigratePrepare(dconn, cookie, cookielen,
                                         uri_in, uri_out,
                                         flags, dname, resource);
1665 1666 1667
}

static int
1668 1669 1670 1671 1672 1673 1674
xenUnifiedDomainMigratePerform(virDomainPtr dom,
                               const char *cookie,
                               int cookielen,
                               const char *uri,
                               unsigned long flags,
                               const char *dname,
                               unsigned long resource)
1675
{
1676 1677 1678
    virDomainDefPtr def = NULL;
    int ret = -1;

E
Eric Blake 已提交
1679 1680
    virCheckFlags(XEN_MIGRATION_FLAGS, -1);

1681 1682 1683
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1684 1685 1686
    if (virDomainMigratePerformEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1687 1688 1689 1690
    ret = xenDaemonDomainMigratePerform(dom->conn, def,
                                        cookie, cookielen, uri,
                                        flags, dname, resource);

1691
 cleanup:
1692 1693
    virDomainDefFree(def);
    return ret;
1694 1695 1696
}

static virDomainPtr
1697 1698 1699 1700 1701 1702
xenUnifiedDomainMigrateFinish(virConnectPtr dconn,
                              const char *dname,
                              const char *cookie ATTRIBUTE_UNUSED,
                              int cookielen ATTRIBUTE_UNUSED,
                              const char *uri ATTRIBUTE_UNUSED,
                              unsigned long flags)
1703
{
1704 1705 1706
    virDomainPtr ret = NULL;
    virDomainDefPtr minidef = NULL;
    virDomainDefPtr def = NULL;
1707

E
Eric Blake 已提交
1708 1709
    virCheckFlags(XEN_MIGRATION_FLAGS, NULL);

1710 1711
    if (!(minidef = xenGetDomainDefForName(dconn, dname)))
        goto cleanup;
1712

1713 1714 1715
    if (virDomainMigrateFinishEnsureACL(dconn, minidef) < 0)
        goto cleanup;

1716
    if (flags & VIR_MIGRATE_PERSIST_DEST) {
1717 1718
        if (!(def = xenDaemonDomainGetXMLDesc(dconn, minidef, NULL)))
            goto cleanup;
1719

1720 1721
        if (xenDaemonDomainDefineXML(dconn, def) < 0)
            goto cleanup;
1722 1723
    }

1724 1725 1726
    ret = virGetDomain(dconn, minidef->name, minidef->uuid);
    if (ret)
        ret->id = minidef->id;
1727

1728
 cleanup:
1729 1730 1731
    virDomainDefFree(def);
    virDomainDefFree(minidef);
    return ret;
1732 1733
}

1734
static int
1735 1736
xenUnifiedConnectListDefinedDomains(virConnectPtr conn, char **const names,
                                    int maxnames)
1737
{
1738 1739 1740
    if (virConnectListDefinedDomainsEnsureACL(conn) < 0)
        return -1;

1741
    return xenDaemonListDefinedDomains(conn, names, maxnames);
1742 1743 1744
}

static int
1745
xenUnifiedConnectNumOfDefinedDomains(virConnectPtr conn)
1746
{
1747 1748 1749
    if (virConnectNumOfDefinedDomainsEnsureACL(conn) < 0)
        return -1;

1750
    return xenDaemonNumOfDefinedDomains(conn);
1751 1752 1753
}

static int
1754
xenUnifiedDomainCreateWithFlags(virDomainPtr dom, unsigned int flags)
1755
{
1756
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1757
    int ret = -1;
1758
    virDomainDefPtr def = NULL;
1759
    char *name = NULL;
1760

1761 1762
    virCheckFlags(0, -1);

1763 1764 1765
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1766 1767 1768
    if (virDomainCreateWithFlagsEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1769
    if (!(name = xenUnifiedDomainManagedSavePath(priv, def)))
1770 1771 1772
        goto cleanup;

    if (virFileExists(name)) {
1773 1774 1775
        ret = xenDaemonDomainRestore(dom->conn, name);
        if (ret == 0)
            unlink(name);
1776 1777 1778
        goto cleanup;
    }

1779
    ret = xenDaemonDomainCreate(dom->conn, def);
1780 1781 1782

    if (ret >= 0)
        dom->id = def->id;
1783

1784
 cleanup:
1785
    virDomainDefFree(def);
1786 1787
    VIR_FREE(name);
    return ret;
1788 1789
}

1790
static int
1791
xenUnifiedDomainCreate(virDomainPtr dom)
1792 1793 1794 1795
{
    return xenUnifiedDomainCreateWithFlags(dom, 0);
}

1796
static virDomainPtr
1797
xenUnifiedDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags)
1798
{
1799
    xenUnifiedPrivatePtr priv = conn->privateData;
1800 1801
    virDomainDefPtr def = NULL;
    virDomainPtr ret = NULL;
1802
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;
1803

1804 1805 1806
    virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL);

    if (flags & VIR_DOMAIN_DEFINE_VALIDATE)
1807
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA;
1808

1809
    if (!(def = virDomainDefParseString(xml, priv->caps, priv->xmlopt,
1810
                                        NULL, parse_flags)))
1811 1812
        goto cleanup;

1813
    if (virDomainDefineXMLFlagsEnsureACL(conn, def) < 0)
1814 1815
        goto cleanup;

1816 1817 1818
    if (xenDaemonDomainDefineXML(conn, def) < 0)
        goto cleanup;
    ret = virGetDomain(conn, def->name, def->uuid);
1819 1820 1821 1822

    if (ret)
        ret->id = -1;

1823
 cleanup:
1824 1825
    virDomainDefFree(def);
    return ret;
1826 1827
}

1828 1829 1830 1831 1832 1833
static virDomainPtr
xenUnifiedDomainDefineXML(virConnectPtr conn, const char *xml)
{
    return xenUnifiedDomainDefineXMLFlags(conn, xml, 0);
}

1834
static int
1835
xenUnifiedDomainUndefineFlags(virDomainPtr dom, unsigned int flags)
1836
{
1837 1838
    virDomainDefPtr def = NULL;
    int ret = -1;
1839

1840
    virCheckFlags(0, -1);
1841 1842 1843 1844

    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1845 1846 1847
    if (virDomainUndefineFlagsEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1848
    ret = xenDaemonDomainUndefine(dom->conn, def);
1849

1850
 cleanup:
1851 1852
    virDomainDefFree(def);
    return ret;
1853 1854
}

1855
static int
1856 1857
xenUnifiedDomainUndefine(virDomainPtr dom)
{
1858 1859 1860
    return xenUnifiedDomainUndefineFlags(dom, 0);
}

1861
static int
1862
xenUnifiedDomainAttachDevice(virDomainPtr dom, const char *xml)
1863
{
1864
    unsigned int flags = VIR_DOMAIN_DEVICE_MODIFY_LIVE;
1865 1866
    virDomainDefPtr def = NULL;
    int ret = -1;
1867

1868
    /*
1869 1870
     * HACK: xend does not support changing live config without also touching
     * persistent config. We add the extra flag here to make this API work
1871
     */
1872
    flags |= VIR_DOMAIN_DEVICE_MODIFY_CONFIG;
1873

1874 1875 1876
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1877 1878 1879
    if (virDomainAttachDeviceEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1880
    ret = xenDaemonAttachDeviceFlags(dom->conn, def, xml, flags);
1881

1882
 cleanup:
1883 1884
    virDomainDefFree(def);
    return ret;
1885 1886 1887
}

static int
1888 1889
xenUnifiedDomainAttachDeviceFlags(virDomainPtr dom, const char *xml,
                                  unsigned int flags)
1890
{
1891 1892 1893 1894 1895
    virDomainDefPtr def = NULL;
    int ret = -1;

    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;
1896

1897 1898 1899
    if (virDomainAttachDeviceFlagsEnsureACL(dom->conn, def, flags) < 0)
        goto cleanup;

1900
    ret = xenDaemonAttachDeviceFlags(dom->conn, def, xml, flags);
1901

1902
 cleanup:
1903 1904
    virDomainDefFree(def);
    return ret;
1905 1906 1907
}

static int
1908
xenUnifiedDomainDetachDevice(virDomainPtr dom, const char *xml)
1909
{
1910
    unsigned int flags = VIR_DOMAIN_DEVICE_MODIFY_LIVE;
1911 1912
    virDomainDefPtr def = NULL;
    int ret = -1;
1913

1914
    /*
1915 1916
     * HACK: xend does not support changing live config without also touching
     * persistent config. We add the extra flag here to make this API work
1917
     */
1918
    flags |= VIR_DOMAIN_DEVICE_MODIFY_CONFIG;
1919

1920 1921 1922
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1923 1924 1925
    if (virDomainDetachDeviceEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1926
    ret = xenDaemonDetachDeviceFlags(dom->conn, def, xml, flags);
1927

1928
 cleanup:
1929 1930
    virDomainDefFree(def);
    return ret;
1931 1932 1933
}

static int
1934 1935
xenUnifiedDomainDetachDeviceFlags(virDomainPtr dom, const char *xml,
                                  unsigned int flags)
1936
{
1937 1938 1939 1940 1941
    virDomainDefPtr def = NULL;
    int ret = -1;

    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;
1942

1943 1944 1945
    if (virDomainDetachDeviceFlagsEnsureACL(dom->conn, def, flags) < 0)
        goto cleanup;

1946
    ret = xenDaemonDetachDeviceFlags(dom->conn, def, xml, flags);
1947

1948
 cleanup:
1949 1950
    virDomainDefFree(def);
    return ret;
1951 1952
}

1953
static int
1954 1955
xenUnifiedDomainUpdateDeviceFlags(virDomainPtr dom, const char *xml,
                                  unsigned int flags)
1956
{
1957 1958 1959 1960 1961 1962
    virDomainDefPtr def = NULL;
    int ret = -1;

    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1963 1964 1965
    if (virDomainUpdateDeviceFlagsEnsureACL(dom->conn, def, flags) < 0)
        goto cleanup;

1966 1967
    ret = xenDaemonUpdateDeviceFlags(dom->conn, def, xml, flags);

1968
 cleanup:
1969 1970
    virDomainDefFree(def);
    return ret;
1971 1972
}

1973
static int
1974
xenUnifiedDomainGetAutostart(virDomainPtr dom, int *autostart)
1975
{
1976 1977 1978 1979 1980
    virDomainDefPtr def = NULL;
    int ret = -1;

    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;
1981

1982 1983 1984
    if (virDomainGetAutostartEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1985
    ret = xenDaemonDomainGetAutostart(dom->conn, def, autostart);
1986

1987
 cleanup:
1988 1989
    virDomainDefFree(def);
    return ret;
1990 1991 1992
}

static int
1993
xenUnifiedDomainSetAutostart(virDomainPtr dom, int autostart)
1994
{
1995 1996 1997 1998 1999
    virDomainDefPtr def = NULL;
    int ret = -1;

    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;
2000

2001 2002 2003
    if (virDomainSetAutostartEnsureACL(dom->conn, def) < 0)
        goto cleanup;

2004
    ret = xenDaemonDomainSetAutostart(dom->conn, def, autostart);
2005

2006
 cleanup:
2007 2008
    virDomainDefFree(def);
    return ret;
2009 2010
}

2011
static char *
2012
xenUnifiedDomainGetSchedulerType(virDomainPtr dom, int *nparams)
2013
{
2014 2015 2016 2017 2018
    virDomainDefPtr def = NULL;
    char *ret = NULL;

    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;
2019

2020 2021 2022
    if (virDomainGetSchedulerTypeEnsureACL(dom->conn, def) < 0)
        goto cleanup;

2023
    if (dom->id < 0)
2024
        ret = xenDaemonGetSchedulerType(dom->conn, nparams);
2025
    else
2026 2027
        ret = xenHypervisorGetSchedulerType(dom->conn, nparams);

2028
 cleanup:
2029 2030
    virDomainDefFree(def);
    return ret;
2031 2032 2033
}

static int
2034 2035 2036 2037
xenUnifiedDomainGetSchedulerParametersFlags(virDomainPtr dom,
                                            virTypedParameterPtr params,
                                            int *nparams,
                                            unsigned int flags)
2038
{
2039 2040
    virDomainDefPtr def = NULL;
    int ret = -1;
2041

2042 2043
    virCheckFlags(0, -1);

2044 2045 2046
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

2047 2048 2049
    if (virDomainGetSchedulerParametersFlagsEnsureACL(dom->conn, def) < 0)
        goto cleanup;

2050
    if (dom->id < 0)
2051
        ret = xenDaemonGetSchedulerParameters(dom->conn, def, params, nparams);
2052
    else
2053 2054
        ret = xenHypervisorGetSchedulerParameters(dom->conn, def, params, nparams);

2055
 cleanup:
2056 2057
    virDomainDefFree(def);
    return ret;
2058 2059 2060
}

static int
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073
xenUnifiedDomainGetSchedulerParameters(virDomainPtr dom,
                                       virTypedParameterPtr params,
                                       int *nparams)
{
    return xenUnifiedDomainGetSchedulerParametersFlags(dom, params,
                                                       nparams, 0);
}

static int
xenUnifiedDomainSetSchedulerParametersFlags(virDomainPtr dom,
                                            virTypedParameterPtr params,
                                            int nparams,
                                            unsigned int flags)
2074
{
2075 2076
    virDomainDefPtr def = NULL;
    int ret = -1;
2077

2078 2079
    virCheckFlags(0, -1);

2080 2081 2082
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

2083 2084 2085
    if (virDomainSetSchedulerParametersFlagsEnsureACL(dom->conn, def, flags) < 0)
        goto cleanup;

2086
    if (dom->id < 0)
2087
        ret = xenDaemonSetSchedulerParameters(dom->conn, def, params, nparams);
2088
    else
2089 2090
        ret = xenHypervisorSetSchedulerParameters(dom->conn, def, params, nparams);

2091
 cleanup:
2092 2093
    virDomainDefFree(def);
    return ret;
2094 2095
}

2096 2097 2098 2099 2100 2101 2102 2103 2104
static int
xenUnifiedDomainSetSchedulerParameters(virDomainPtr dom,
                                       virTypedParameterPtr params,
                                       int nparams)
{
    return xenUnifiedDomainSetSchedulerParametersFlags(dom, params,
                                                       nparams, 0);
}

2105
static int
2106
xenUnifiedDomainBlockStats(virDomainPtr dom, const char *path,
2107
                           virDomainBlockStatsPtr stats)
2108
{
2109 2110 2111 2112 2113 2114
    virDomainDefPtr def = NULL;
    int ret = -1;

    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

2115 2116 2117
    if (virDomainBlockStatsEnsureACL(dom->conn, def) < 0)
        goto cleanup;

2118 2119
    ret = xenHypervisorDomainBlockStats(dom->conn, def, path, stats);

2120
 cleanup:
2121 2122
    virDomainDefFree(def);
    return ret;
2123 2124 2125
}

static int
2126
xenUnifiedDomainInterfaceStats(virDomainPtr dom, const char *path,
2127
                               virDomainInterfaceStatsPtr stats)
2128
{
2129 2130 2131 2132 2133 2134
    virDomainDefPtr def = NULL;
    int ret = -1;

    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

2135 2136 2137
    if (virDomainInterfaceStatsEnsureACL(dom->conn, def) < 0)
        goto cleanup;

2138 2139
    ret = xenHypervisorDomainInterfaceStats(def, path, stats);

2140
 cleanup:
2141 2142
    virDomainDefFree(def);
    return ret;
2143 2144
}

R
Richard W.M. Jones 已提交
2145
static int
2146 2147 2148
xenUnifiedDomainBlockPeek(virDomainPtr dom, const char *path,
                          unsigned long long offset, size_t size,
                          void *buffer, unsigned int flags)
R
Richard W.M. Jones 已提交
2149
{
2150 2151
    virDomainDefPtr def = NULL;
    int ret = -1;
R
Richard W.M. Jones 已提交
2152

E
Eric Blake 已提交
2153 2154
    virCheckFlags(0, -1);

2155 2156 2157
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

2158 2159 2160
    if (virDomainBlockPeekEnsureACL(dom->conn, def) < 0)
        goto cleanup;

2161
    ret = xenDaemonDomainBlockPeek(dom->conn, def, path, offset, size, buffer);
2162

2163
 cleanup:
2164 2165
    virDomainDefFree(def);
    return ret;
R
Richard W.M. Jones 已提交
2166 2167
}

2168
static int
2169 2170
xenUnifiedNodeGetCellsFreeMemory(virConnectPtr conn, unsigned long long *freeMems,
                                 int startCell, int maxCells)
2171
{
2172 2173 2174
    if (virNodeGetCellsFreeMemoryEnsureACL(conn) < 0)
        return 0;

2175 2176
    return xenHypervisorNodeGetCellsFreeMemory(conn, freeMems,
                                               startCell, maxCells);
2177 2178
}

2179
static unsigned long long
2180
xenUnifiedNodeGetFreeMemory(virConnectPtr conn)
2181 2182 2183
{
    unsigned long long freeMem = 0;

2184 2185 2186
    if (virNodeGetFreeMemoryEnsureACL(conn) < 0)
        return 0;

2187 2188 2189
    if (xenHypervisorNodeGetCellsFreeMemory(conn, &freeMem, -1, 1) < 0)
        return 0;
    return freeMem;
2190 2191
}

2192

2193
static int
2194 2195 2196 2197
xenUnifiedConnectDomainEventRegister(virConnectPtr conn,
                                     virConnectDomainEventCallback callback,
                                     void *opaque,
                                     virFreeCallback freefunc)
2198
{
2199
    xenUnifiedPrivatePtr priv = conn->privateData;
2200
    int ret = 0;
2201 2202 2203 2204

    if (virConnectDomainEventRegisterEnsureACL(conn) < 0)
        return -1;

D
Daniel P. Berrange 已提交
2205
    xenUnifiedLock(priv);
2206

2207
    if (priv->xsWatch == -1) {
2208
        virReportUnsupportedError();
D
Daniel P. Berrange 已提交
2209
        xenUnifiedUnlock(priv);
2210 2211 2212
        return -1;
    }

2213 2214 2215
    if (virDomainEventStateRegister(conn, priv->domainEvents,
                                    callback, opaque, freefunc) < 0)
        ret = -1;
2216

D
Daniel P. Berrange 已提交
2217
    xenUnifiedUnlock(priv);
2218
    return ret;
2219 2220
}

2221

2222
static int
2223 2224
xenUnifiedConnectDomainEventDeregister(virConnectPtr conn,
                                       virConnectDomainEventCallback callback)
2225
{
2226
    int ret = 0;
2227
    xenUnifiedPrivatePtr priv = conn->privateData;
2228 2229 2230 2231

    if (virConnectDomainEventDeregisterEnsureACL(conn) < 0)
        return -1;

D
Daniel P. Berrange 已提交
2232 2233
    xenUnifiedLock(priv);

2234
    if (priv->xsWatch == -1) {
2235
        virReportUnsupportedError();
D
Daniel P. Berrange 已提交
2236
        xenUnifiedUnlock(priv);
2237 2238 2239
        return -1;
    }

2240 2241 2242 2243
    if (virDomainEventStateDeregister(conn,
                                      priv->domainEvents,
                                      callback) < 0)
        ret = -1;
2244

D
Daniel P. Berrange 已提交
2245
    xenUnifiedUnlock(priv);
2246 2247 2248
    return ret;
}

2249

2250
static int
2251 2252 2253 2254 2255 2256
xenUnifiedConnectDomainEventRegisterAny(virConnectPtr conn,
                                        virDomainPtr dom,
                                        int eventID,
                                        virConnectDomainEventGenericCallback callback,
                                        void *opaque,
                                        virFreeCallback freefunc)
2257
{
2258
    xenUnifiedPrivatePtr priv = conn->privateData;
2259
    int ret;
2260 2261 2262 2263

    if (virConnectDomainEventRegisterAnyEnsureACL(conn) < 0)
        return -1;

2264 2265 2266
    xenUnifiedLock(priv);

    if (priv->xsWatch == -1) {
2267
        virReportUnsupportedError();
2268 2269 2270 2271
        xenUnifiedUnlock(priv);
        return -1;
    }

2272 2273 2274
    if (virDomainEventStateRegisterID(conn, priv->domainEvents,
                                      dom, eventID,
                                      callback, opaque, freefunc, &ret) < 0)
2275
        ret = -1;
2276 2277

    xenUnifiedUnlock(priv);
2278
    return ret;
2279 2280 2281
}

static int
2282 2283
xenUnifiedConnectDomainEventDeregisterAny(virConnectPtr conn,
                                          int callbackID)
2284
{
2285
    int ret = 0;
2286
    xenUnifiedPrivatePtr priv = conn->privateData;
2287 2288 2289 2290

    if (virConnectDomainEventDeregisterAnyEnsureACL(conn) < 0)
        return -1;

2291 2292 2293
    xenUnifiedLock(priv);

    if (priv->xsWatch == -1) {
2294
        virReportUnsupportedError();
2295 2296 2297 2298
        xenUnifiedUnlock(priv);
        return -1;
    }

2299 2300 2301 2302
    if (virObjectEventStateDeregisterID(conn,
                                        priv->domainEvents,
                                        callbackID) < 0)
        ret = -1;
2303 2304 2305 2306 2307 2308

    xenUnifiedUnlock(priv);
    return ret;
}


2309
static int
2310
xenUnifiedNodeDeviceGetPCIInfo(virNodeDevicePtr dev,
2311 2312 2313 2314
                               unsigned *domain,
                               unsigned *bus,
                               unsigned *slot,
                               unsigned *function)
2315 2316 2317 2318 2319 2320 2321 2322 2323 2324
{
    virNodeDeviceDefPtr def = NULL;
    virNodeDevCapsDefPtr cap;
    char *xml = NULL;
    int ret = -1;

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

2325
    def = virNodeDeviceDefParseString(xml, EXISTING_DEVICE, NULL);
2326 2327 2328 2329 2330
    if (!def)
        goto out;

    cap = def->caps;
    while (cap) {
2331
        if (cap->data.type == VIR_NODE_DEV_CAP_PCI_DEV) {
2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342
            *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) {
2343 2344
        virReportError(VIR_ERR_INVALID_ARG,
                       _("device %s is not a PCI device"), dev->name);
2345 2346 2347 2348
        goto out;
    }

    ret = 0;
2349
 out:
2350 2351 2352 2353 2354 2355
    virNodeDeviceDefFree(def);
    VIR_FREE(xml);
    return ret;
}

static int
2356 2357 2358
xenUnifiedNodeDeviceDetachFlags(virNodeDevicePtr dev,
                                const char *driverName,
                                unsigned int flags)
2359
{
2360
    virPCIDevicePtr pci;
2361 2362 2363
    unsigned domain, bus, slot, function;
    int ret = -1;

2364 2365
    virCheckFlags(0, -1);

2366
    if (xenUnifiedNodeDeviceGetPCIInfo(dev, &domain, &bus, &slot, &function) < 0)
2367 2368
        return -1;

2369
    pci = virPCIDeviceNew(domain, bus, slot, function);
2370 2371 2372
    if (!pci)
        return -1;

2373
    if (!driverName) {
2374
        virPCIDeviceSetStubDriver(pci, VIR_PCI_STUB_DRIVER_XEN);
2375 2376 2377 2378 2379 2380
    } else {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unknown driver name '%s'"), driverName);
        goto out;
    }

2381
    if (virPCIDeviceDetach(pci, NULL, NULL) < 0)
2382 2383 2384
        goto out;

    ret = 0;
2385
 out:
2386
    virPCIDeviceFree(pci);
2387 2388 2389
    return ret;
}

2390 2391 2392 2393 2394 2395
static int
xenUnifiedNodeDeviceDettach(virNodeDevicePtr dev)
{
    return xenUnifiedNodeDeviceDetachFlags(dev, NULL, 0);
}

2396
static int
2397
xenUnifiedNodeDeviceAssignedDomainId(virNodeDevicePtr dev)
2398 2399
{
    int numdomains;
2400 2401
    int ret = -1;
    size_t i;
2402 2403 2404 2405 2406 2407 2408 2409
    int *ids = NULL;
    char *bdf = NULL;
    char *xref = NULL;
    unsigned int domain, bus, slot, function;
    virConnectPtr conn = dev->conn;
    xenUnifiedPrivatePtr priv = conn->privateData;

    /* Get active domains */
2410
    numdomains = xenUnifiedConnectNumOfDomains(conn);
2411
    if (numdomains < 0)
2412
        return ret;
2413
    if (numdomains > 0) {
2414
        if (VIR_ALLOC_N(ids, numdomains) < 0)
2415
            goto out;
2416
        if ((numdomains = xenUnifiedConnectListDomains(conn, &ids[0], numdomains)) < 0)
2417 2418 2419 2420
            goto out;
    }

    /* Get pci bdf */
2421
    if (xenUnifiedNodeDeviceGetPCIInfo(dev, &domain, &bus, &slot, &function) < 0)
2422 2423 2424
        goto out;

    if (virAsprintf(&bdf, "%04x:%02x:%02x.%0x",
2425
                    domain, bus, slot, function) < 0)
2426 2427 2428 2429
        goto out;

    xenUnifiedLock(priv);
    /* Check if bdf is assigned to one of active domains */
2430
    for (i = 0; i < numdomains; i++) {
2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442
        xref = xenStoreDomainGetPCIID(conn, ids[i], bdf);
        if (xref == NULL) {
            continue;
        } else {
            ret = ids[i];
            break;
        }
    }
    xenUnifiedUnlock(priv);

    VIR_FREE(xref);
    VIR_FREE(bdf);
2443
 out:
2444 2445 2446 2447 2448
    VIR_FREE(ids);

    return ret;
}

2449
static int
2450
xenUnifiedNodeDeviceReAttach(virNodeDevicePtr dev)
2451
{
2452
    virPCIDevicePtr pci;
2453 2454
    unsigned domain, bus, slot, function;
    int ret = -1;
2455
    int domid;
2456

2457
    if (xenUnifiedNodeDeviceGetPCIInfo(dev, &domain, &bus, &slot, &function) < 0)
2458 2459
        return -1;

2460
    pci = virPCIDeviceNew(domain, bus, slot, function);
2461 2462 2463
    if (!pci)
        return -1;

2464 2465
    /* Check if device is assigned to an active guest */
    if ((domid = xenUnifiedNodeDeviceAssignedDomainId(dev)) >= 0) {
2466 2467 2468
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Device %s has been assigned to guest %d"),
                       dev->name, domid);
2469 2470 2471
        goto out;
    }

2472
    if (virPCIDeviceReattach(pci, NULL, NULL) < 0)
2473 2474 2475
        goto out;

    ret = 0;
2476
 out:
2477
    virPCIDeviceFree(pci);
2478 2479 2480 2481
    return ret;
}

static int
2482
xenUnifiedNodeDeviceReset(virNodeDevicePtr dev)
2483
{
2484
    virPCIDevicePtr pci;
2485 2486 2487
    unsigned domain, bus, slot, function;
    int ret = -1;

2488
    if (xenUnifiedNodeDeviceGetPCIInfo(dev, &domain, &bus, &slot, &function) < 0)
2489 2490
        return -1;

2491
    pci = virPCIDeviceNew(domain, bus, slot, function);
2492 2493 2494
    if (!pci)
        return -1;

2495
    if (virPCIDeviceReset(pci, NULL, NULL) < 0)
2496 2497 2498
        goto out;

    ret = 0;
2499
 out:
2500
    virPCIDeviceFree(pci);
2501 2502 2503 2504
    return ret;
}


2505 2506
static int
xenUnifiedDomainOpenConsole(virDomainPtr dom,
2507
                            const char *dev_name,
2508 2509 2510 2511 2512 2513 2514 2515 2516 2517
                            virStreamPtr st,
                            unsigned int flags)
{
    virDomainDefPtr def = NULL;
    int ret = -1;
    virDomainChrDefPtr chr = NULL;

    virCheckFlags(0, -1);

    if (dom->id == -1) {
2518 2519
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("domain is not running"));
2520 2521 2522
        goto cleanup;
    }

2523
    if (dev_name) {
2524
        /* XXX support device aliases in future */
2525 2526
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Named device aliases are not supported"));
2527 2528 2529 2530 2531 2532 2533
        goto cleanup;
    }

    def = xenDaemonDomainFetch(dom->conn, dom->id, dom->name, NULL);
    if (!def)
        goto cleanup;

2534 2535
    if (def->nconsoles)
        chr = def->consoles[0];
2536 2537 2538 2539
    else if (def->nserials)
        chr = def->serials[0];

    if (!chr) {
2540 2541
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot find default console device"));
2542 2543 2544
        goto cleanup;
    }

2545
    if (chr->source->type != VIR_DOMAIN_CHR_TYPE_PTY) {
2546
        virReportError(VIR_ERR_INTERNAL_ERROR,
2547 2548
                       _("character device %s is not using a PTY"),
                       dev_name ? dev_name : NULLSTR(chr->info.alias));
2549 2550 2551
        goto cleanup;
    }

2552
    if (virFDStreamOpenFile(st, chr->source->data.file.path,
E
Eric Blake 已提交
2553
                            0, 0, O_RDWR) < 0)
2554 2555 2556
        goto cleanup;

    ret = 0;
2557
 cleanup:
2558 2559 2560
    virDomainDefFree(def);
    return ret;
}
2561 2562

static int
2563
xenUnifiedNodeGetMemoryParameters(virConnectPtr conn,
2564 2565 2566 2567
                                  virTypedParameterPtr params,
                                  int *nparams,
                                  unsigned int flags)
{
2568 2569 2570
    if (virNodeGetMemoryParametersEnsureACL(conn) < 0)
        return -1;

2571
    return virHostMemGetParameters(params, nparams, flags);
2572 2573 2574 2575
}


static int
2576
xenUnifiedNodeSetMemoryParameters(virConnectPtr conn,
2577 2578 2579 2580
                                  virTypedParameterPtr params,
                                  int nparams,
                                  unsigned int flags)
{
2581 2582 2583
    if (virNodeSetMemoryParametersEnsureACL(conn) < 0)
        return -1;

2584
    return virHostMemSetParameters(params, nparams, flags);
2585 2586
}

2587 2588

static int
2589
xenUnifiedNodeSuspendForDuration(virConnectPtr conn,
2590 2591 2592 2593
                                 unsigned int target,
                                 unsigned long long duration,
                                 unsigned int flags)
{
2594 2595 2596
    if (virNodeSuspendForDurationEnsureACL(conn) < 0)
        return -1;

2597 2598 2599 2600
    return nodeSuspendForDuration(target, duration, flags);
}


E
Eric Blake 已提交
2601
/*----- Register with libvirt.c, and initialize Xen drivers. -----*/
2602 2603

/* The interface which we export upwards to libvirt.c. */
2604
static virHypervisorDriver xenUnifiedHypervisorDriver = {
2605
    .name = "Xen",
2606 2607 2608 2609 2610
    .connectOpen = xenUnifiedConnectOpen, /* 0.0.3 */
    .connectClose = xenUnifiedConnectClose, /* 0.0.3 */
    .connectSupportsFeature = xenUnifiedConnectSupportsFeature, /* 0.3.2 */
    .connectGetType = xenUnifiedConnectGetType, /* 0.0.3 */
    .connectGetVersion = xenUnifiedConnectGetVersion, /* 0.0.3 */
2611
    .connectGetHostname = xenUnifiedConnectGetHostname, /* 0.7.3 */
2612
    .connectGetSysinfo = xenUnifiedConnectGetSysinfo, /* 1.1.0 */
2613
    .connectGetMaxVcpus = xenUnifiedConnectGetMaxVcpus, /* 0.2.1 */
2614
    .nodeGetInfo = xenUnifiedNodeGetInfo, /* 0.1.0 */
2615 2616 2617
    .connectGetCapabilities = xenUnifiedConnectGetCapabilities, /* 0.2.1 */
    .connectListDomains = xenUnifiedConnectListDomains, /* 0.0.3 */
    .connectNumOfDomains = xenUnifiedConnectNumOfDomains, /* 0.0.3 */
2618 2619 2620 2621 2622 2623 2624
    .domainCreateXML = xenUnifiedDomainCreateXML, /* 0.0.3 */
    .domainLookupByID = xenUnifiedDomainLookupByID, /* 0.0.3 */
    .domainLookupByUUID = xenUnifiedDomainLookupByUUID, /* 0.0.5 */
    .domainLookupByName = xenUnifiedDomainLookupByName, /* 0.0.3 */
    .domainSuspend = xenUnifiedDomainSuspend, /* 0.0.3 */
    .domainResume = xenUnifiedDomainResume, /* 0.0.3 */
    .domainShutdown = xenUnifiedDomainShutdown, /* 0.0.3 */
2625
    .domainShutdownFlags = xenUnifiedDomainShutdownFlags, /* 0.9.10 */
2626 2627
    .domainReboot = xenUnifiedDomainReboot, /* 0.1.0 */
    .domainDestroy = xenUnifiedDomainDestroy, /* 0.0.3 */
2628
    .domainDestroyFlags = xenUnifiedDomainDestroyFlags, /* 0.9.4 */
2629 2630 2631 2632 2633 2634 2635
    .domainGetOSType = xenUnifiedDomainGetOSType, /* 0.0.3 */
    .domainGetMaxMemory = xenUnifiedDomainGetMaxMemory, /* 0.0.3 */
    .domainSetMaxMemory = xenUnifiedDomainSetMaxMemory, /* 0.0.3 */
    .domainSetMemory = xenUnifiedDomainSetMemory, /* 0.1.1 */
    .domainGetInfo = xenUnifiedDomainGetInfo, /* 0.0.3 */
    .domainGetState = xenUnifiedDomainGetState, /* 0.9.2 */
    .domainSave = xenUnifiedDomainSave, /* 0.0.3 */
2636
    .domainSaveFlags = xenUnifiedDomainSaveFlags, /* 0.9.4 */
2637 2638 2639
    .domainManagedSave = xenUnifiedDomainManagedSave, /* 1.0.1 */
    .domainHasManagedSaveImage = xenUnifiedDomainHasManagedSaveImage, /* 1.0.1 */
    .domainManagedSaveRemove = xenUnifiedDomainManagedSaveRemove, /* 1.0.1 */
2640
    .domainRestore = xenUnifiedDomainRestore, /* 0.0.3 */
2641
    .domainRestoreFlags = xenUnifiedDomainRestoreFlags, /* 0.9.4 */
2642 2643 2644 2645 2646 2647 2648 2649
    .domainCoreDump = xenUnifiedDomainCoreDump, /* 0.1.9 */
    .domainSetVcpus = xenUnifiedDomainSetVcpus, /* 0.1.4 */
    .domainSetVcpusFlags = xenUnifiedDomainSetVcpusFlags, /* 0.8.5 */
    .domainGetVcpusFlags = xenUnifiedDomainGetVcpusFlags, /* 0.8.5 */
    .domainPinVcpu = xenUnifiedDomainPinVcpu, /* 0.1.4 */
    .domainGetVcpus = xenUnifiedDomainGetVcpus, /* 0.1.4 */
    .domainGetMaxVcpus = xenUnifiedDomainGetMaxVcpus, /* 0.2.1 */
    .domainGetXMLDesc = xenUnifiedDomainGetXMLDesc, /* 0.0.3 */
2650 2651 2652 2653
    .connectDomainXMLFromNative = xenUnifiedConnectDomainXMLFromNative, /* 0.6.4 */
    .connectDomainXMLToNative = xenUnifiedConnectDomainXMLToNative, /* 0.6.4 */
    .connectListDefinedDomains = xenUnifiedConnectListDefinedDomains, /* 0.1.1 */
    .connectNumOfDefinedDomains = xenUnifiedConnectNumOfDefinedDomains, /* 0.1.5 */
2654 2655 2656
    .domainCreate = xenUnifiedDomainCreate, /* 0.1.1 */
    .domainCreateWithFlags = xenUnifiedDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = xenUnifiedDomainDefineXML, /* 0.1.1 */
2657
    .domainDefineXMLFlags = xenUnifiedDomainDefineXMLFlags, /* 1.2.12 */
2658
    .domainUndefine = xenUnifiedDomainUndefine, /* 0.1.1 */
2659
    .domainUndefineFlags = xenUnifiedDomainUndefineFlags, /* 0.9.4 */
2660 2661 2662 2663 2664 2665 2666 2667 2668
    .domainAttachDevice = xenUnifiedDomainAttachDevice, /* 0.1.9 */
    .domainAttachDeviceFlags = xenUnifiedDomainAttachDeviceFlags, /* 0.7.7 */
    .domainDetachDevice = xenUnifiedDomainDetachDevice, /* 0.1.9 */
    .domainDetachDeviceFlags = xenUnifiedDomainDetachDeviceFlags, /* 0.7.7 */
    .domainUpdateDeviceFlags = xenUnifiedDomainUpdateDeviceFlags, /* 0.8.0 */
    .domainGetAutostart = xenUnifiedDomainGetAutostart, /* 0.4.4 */
    .domainSetAutostart = xenUnifiedDomainSetAutostart, /* 0.4.4 */
    .domainGetSchedulerType = xenUnifiedDomainGetSchedulerType, /* 0.2.3 */
    .domainGetSchedulerParameters = xenUnifiedDomainGetSchedulerParameters, /* 0.2.3 */
2669
    .domainGetSchedulerParametersFlags = xenUnifiedDomainGetSchedulerParametersFlags, /* 0.9.2 */
2670
    .domainSetSchedulerParameters = xenUnifiedDomainSetSchedulerParameters, /* 0.2.3 */
2671
    .domainSetSchedulerParametersFlags = xenUnifiedDomainSetSchedulerParametersFlags, /* 0.9.2 */
2672 2673 2674 2675 2676 2677 2678 2679
    .domainMigratePrepare = xenUnifiedDomainMigratePrepare, /* 0.3.2 */
    .domainMigratePerform = xenUnifiedDomainMigratePerform, /* 0.3.2 */
    .domainMigrateFinish = xenUnifiedDomainMigrateFinish, /* 0.3.2 */
    .domainBlockStats = xenUnifiedDomainBlockStats, /* 0.3.2 */
    .domainInterfaceStats = xenUnifiedDomainInterfaceStats, /* 0.3.2 */
    .domainBlockPeek = xenUnifiedDomainBlockPeek, /* 0.4.4 */
    .nodeGetCellsFreeMemory = xenUnifiedNodeGetCellsFreeMemory, /* 0.3.3 */
    .nodeGetFreeMemory = xenUnifiedNodeGetFreeMemory, /* 0.3.3 */
2680 2681
    .connectDomainEventRegister = xenUnifiedConnectDomainEventRegister, /* 0.5.0 */
    .connectDomainEventDeregister = xenUnifiedConnectDomainEventDeregister, /* 0.5.0 */
2682
    .nodeDeviceDettach = xenUnifiedNodeDeviceDettach, /* 0.6.1 */
2683
    .nodeDeviceDetachFlags = xenUnifiedNodeDeviceDetachFlags, /* 1.0.5 */
2684 2685
    .nodeDeviceReAttach = xenUnifiedNodeDeviceReAttach, /* 0.6.1 */
    .nodeDeviceReset = xenUnifiedNodeDeviceReset, /* 0.6.1 */
2686 2687
    .connectIsEncrypted = xenUnifiedConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = xenUnifiedConnectIsSecure, /* 0.7.3 */
2688 2689 2690
    .domainIsActive = xenUnifiedDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = xenUnifiedDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = xenUnifiedDomainIsUpdated, /* 0.8.6 */
2691 2692
    .connectDomainEventRegisterAny = xenUnifiedConnectDomainEventRegisterAny, /* 0.8.0 */
    .connectDomainEventDeregisterAny = xenUnifiedConnectDomainEventDeregisterAny, /* 0.8.0 */
2693
    .domainOpenConsole = xenUnifiedDomainOpenConsole, /* 0.8.6 */
2694
    .connectIsAlive = xenUnifiedConnectIsAlive, /* 0.9.8 */
2695
    .nodeSuspendForDuration = xenUnifiedNodeSuspendForDuration, /* 0.9.8 */
2696 2697
    .nodeGetMemoryParameters = xenUnifiedNodeGetMemoryParameters, /* 0.10.2 */
    .nodeSetMemoryParameters = xenUnifiedNodeSetMemoryParameters, /* 0.10.2 */
2698 2699
};

2700 2701 2702 2703 2704

static virConnectDriver xenUnifiedConnectDriver = {
    .hypervisorDriver = &xenUnifiedHypervisorDriver,
};

2705
/**
2706
 * xenRegister:
2707 2708 2709 2710 2711
 *
 * Register xen related drivers
 *
 * Returns the driver priority or -1 in case of error.
 */
2712
int
2713
xenRegister(void)
2714
{
2715
    if (virRegisterStateDriver(&state_driver) == -1) return -1;
J
John Levon 已提交
2716

2717 2718
    return virRegisterConnectDriver(&xenUnifiedConnectDriver,
                                    true);
2719 2720
}

2721 2722 2723 2724 2725 2726 2727 2728
/**
 * xenUnifiedDomainInfoListFree:
 *
 * Free the Domain Info List
 */
void
xenUnifiedDomainInfoListFree(xenUnifiedDomainInfoListPtr list)
{
2729
    size_t i;
J
John Levon 已提交
2730 2731 2732 2733

    if (list == NULL)
        return;

2734
    for (i = 0; i < list->count; i++) {
2735 2736 2737
        VIR_FREE(list->doms[i]->name);
        VIR_FREE(list->doms[i]);
    }
2738
    VIR_FREE(list->doms);
2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757
    VIR_FREE(list);
}

/**
 * xenUnifiedAddDomainInfo:
 *
 * Add name and uuid to the domain info list
 *
 * Returns: 0 on success, -1 on failure
 */
int
xenUnifiedAddDomainInfo(xenUnifiedDomainInfoListPtr list,
                        int id, char *name,
                        unsigned char *uuid)
{
    xenUnifiedDomainInfoPtr info;
    int n;

    /* check if we already have this callback on our list */
2758
    for (n = 0; n < list->count; n++) {
2759 2760
        if (STREQ(list->doms[n]->name, name) &&
            !memcmp(list->doms[n]->uuid, uuid, VIR_UUID_BUFLEN)) {
2761
            VIR_DEBUG("WARNING: dom already tracked");
2762 2763 2764 2765 2766
            return -1;
        }
    }

    if (VIR_ALLOC(info) < 0)
2767
        goto error;
2768 2769
    if (VIR_STRDUP(info->name, name) < 0)
        goto error;
2770 2771 2772 2773 2774

    memcpy(info->uuid, uuid, VIR_UUID_BUFLEN);
    info->id = id;

    /* Make space on list */
2775
    if (VIR_APPEND_ELEMENT(list->doms, list->count, info) < 0)
2776
        goto error;
2777 2778

    return 0;
2779
 error:
2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797
    if (info)
        VIR_FREE(info->name);
    VIR_FREE(info);
    return -1;
}

/**
 * xenUnifiedRemoveDomainInfo:
 *
 * Removes name and uuid to the domain info list
 *
 * Returns: 0 on success, -1 on failure
 */
int
xenUnifiedRemoveDomainInfo(xenUnifiedDomainInfoListPtr list,
                           int id, char *name,
                           unsigned char *uuid)
{
2798
    size_t i;
2799
    for (i = 0; i < list->count; i++) {
2800
        if (list->doms[i]->id == id &&
2801 2802 2803 2804 2805 2806
            STREQ(list->doms[i]->name, name) &&
            !memcmp(list->doms[i]->uuid, uuid, VIR_UUID_BUFLEN)) {

            VIR_FREE(list->doms[i]->name);
            VIR_FREE(list->doms[i]);

2807
            VIR_DELETE_ELEMENT(list->doms, i, list->count);
2808 2809 2810 2811 2812 2813
            return 0;
        }
    }
    return -1;
}

2814

2815 2816
/**
 * xenUnifiedDomainEventDispatch:
D
Daniel P. Berrange 已提交
2817 2818
 * @priv: the connection to dispatch events on
 * @event: the event to dispatch
2819 2820 2821
 *
 * Dispatch domain events to registered callbacks
 *
D
Daniel P. Berrange 已提交
2822 2823
 * The caller must hold the lock in 'priv' before invoking
 *
2824
 */
2825
void xenUnifiedDomainEventDispatch(xenUnifiedPrivatePtr priv,
2826
                                    virObjectEventPtr event)
2827
{
D
Daniel P. Berrange 已提交
2828
    if (!priv)
2829
        return;
2830

2831
    virObjectEventStateQueue(priv->domainEvents, event);
D
Daniel P. Berrange 已提交
2832 2833 2834 2835 2836 2837 2838 2839 2840 2841
}

void xenUnifiedLock(xenUnifiedPrivatePtr priv)
{
    virMutexLock(&priv->lock);
}

void xenUnifiedUnlock(xenUnifiedPrivatePtr priv)
{
    virMutexUnlock(&priv->lock);
2842
}