xen_driver.c 74.3 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 330 331 332
                            void *opaque ATTRIBUTE_UNUSED)
{
    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 &&
333
        def->os.type != VIR_DOMAIN_OSTYPE_HVM)
334 335
        dev->data.chr->targetType = VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_XEN;

336 337 338 339 340 341 342 343 344 345
    /* 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;
    }

346 347 348 349 350
    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:
351
            dev->data.video->vram = 16 * 1024;
352 353 354 355 356 357 358 359 360 361 362 363 364
        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;
        }
    }

365 366 367 368 369 370 371
    return 0;
}


static int
xenDomainDefPostParse(virDomainDefPtr def,
                      virCapsPtr caps ATTRIBUTE_UNUSED,
372
                      unsigned int parseFlags ATTRIBUTE_UNUSED,
373 374
                      void *opaque ATTRIBUTE_UNUSED)
{
375 376 377 378 379 380 381 382 383
    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 已提交
384 385 386 387
    /* add implicit input device */
    if (xenDomainDefAddImplicitInputDevice(def) <0)
        return -1;

388 389 390 391
    return 0;
}


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

398

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


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

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

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

425 426 427 428 429 430 431
#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

432
        if (!(conn->uri = virURIParse("xen:///")))
433
            return VIR_DRV_OPEN_ERROR;
434 435 436 437 438 439 440
    } 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;

441 442 443 444 445 446
#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
447 448 449 450 451

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

458 459 460 461 462 463
            /* 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 {
464
            return VIR_DRV_OPEN_DECLINED;
465 466
        }
    }
467

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

471 472 473
    if (virConnectOpenEnsureACL(conn) < 0)
        return VIR_DRV_OPEN_ERROR;

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

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

491 492 493 494
    priv->handle = -1;
    priv->xshandle = NULL;


495 496 497 498 499 500
    /* 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;
501

502
    /* XenD is required to succeed */
503
    VIR_DEBUG("Trying XenD sub-driver");
504 505 506 507 508 509 510 511 512 513
    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;
514

D
Daniel P. Berrange 已提交
515 516
    xenNumaInit(conn);

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

522
    if (!(priv->xmlopt = xenDomainXMLConfInit()))
523
        goto error;
524

525
#if WITH_XEN_INOTIFY
526 527 528 529 530
    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;
531 532
#endif

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

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

542
    return VIR_DRV_OPEN_SUCCESS;
543

544
 error:
545
    VIR_DEBUG("Failed to activate a mandatory sub-driver");
546 547 548 549 550 551 552 553 554 555 556 557
#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 已提交
558
    virMutexDestroy(&priv->lock);
559
    VIR_FREE(priv->saveDir);
560
    VIR_FREE(priv);
D
Daniel P. Berrange 已提交
561
    conn->privateData = NULL;
562
    return VIR_DRV_OPEN_ERROR;
563 564 565
}

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

570
    virObjectUnref(priv->caps);
571
    virObjectUnref(priv->xmlopt);
572
    virObjectEventStateFree(priv->domainEvents);
573

574 575 576 577 578 579 580 581 582 583 584 585
#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);
586

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

591
    return 0;
592 593
}

594 595 596 597 598 599 600 601 602 603 604

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


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

611
    return "Xen";
612 613
}

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

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

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

636
    return xenHypervisorGetVersion(conn, hvVer);
637 638
}

639

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

645 646 647
    return virGetHostname();
}

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

    virCheckFlags(0, NULL);

656 657 658
    if (virConnectGetSysinfoEnsureACL(conn) < 0)
        return NULL;

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

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

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

static int
679
xenUnifiedConnectIsSecure(virConnectPtr conn)
680
{
681
    xenUnifiedPrivatePtr priv = conn->privateData;
682 683 684 685 686 687 688 689 690 691
    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;
}

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

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

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

710
    return xenHypervisorGetMaxVcpus(conn, type);
711 712 713
}

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

719
    return xenDaemonNodeGetInfo(conn, info);
720 721 722
}

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

727 728 729
    if (virConnectGetCapabilitiesEnsureACL(conn) < 0)
        return NULL;

730
    return virCapabilitiesFormatXML(priv->caps);
731 732 733
}

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

739
    return xenStoreListDomains(conn, ids, maxids);
740 741 742
}

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

748
    return xenStoreNumOfDomains(conn);
749 750 751
}

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

761 762 763
    virCheckFlags(VIR_DOMAIN_START_VALIDATE, NULL);

    if (flags & VIR_DOMAIN_START_VALIDATE)
764
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA;
765

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

770 771 772
    if (virDomainCreateXMLEnsureACL(conn, def) < 0)
        goto cleanup;

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

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

780
 cleanup:
781 782
    virDomainDefFree(def);
    return ret;
783 784 785
}

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

791 792
    if (!(def = xenGetDomainDefForID(conn, id)))
        goto cleanup;
793

794 795 796
    if (virDomainLookupByIDEnsureACL(conn, def) < 0)
        goto cleanup;

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

800 801
    ret->id = def->id;

802
 cleanup:
803
    virDomainDefFree(def);
804
    return ret;
805 806 807
}

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

814 815
    if (!(def = xenGetDomainDefForUUID(conn, uuid)))
        goto cleanup;
816

817 818 819
    if (virDomainLookupByUUIDEnsureACL(conn, def) < 0)
        goto cleanup;

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

823
    ret->id = def->id;
824

825
 cleanup:
826
    virDomainDefFree(def);
827
    return ret;
828 829 830
}

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

837 838
    if (!(def = xenGetDomainDefForName(conn, name)))
        goto cleanup;
839

840 841 842
    if (virDomainLookupByNameEnsureACL(conn, def) < 0)
        goto cleanup;

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

846
    ret->id = def->id;
847

848
 cleanup:
849
    virDomainDefFree(def);
850
    return ret;
851 852
}

853 854 855 856

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

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

863
    ret = def->id == -1 ? 0 : 1;
864

865
 cleanup:
866
    virDomainDefFree(def);
867 868 869 870
    return ret;
}

static int
871
xenUnifiedDomainIsPersistent(virDomainPtr dom)
872
{
873
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
874
    virDomainDefPtr def = NULL;
875 876 877 878 879
    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 */
880 881
        def = xenXMDomainLookupByUUID(dom->conn, dom->uuid);
        if (def)
882 883 884 885 886
            ret = 1;
        else
            ret = 0;
    } else {
        /* New Xen with inactive domain management */
887 888 889
        def = xenDaemonLookupByUUID(dom->conn, dom->uuid);
        if (def) {
            if (def->id == -1) {
890 891 892 893 894 895 896 897 898
                /* 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);
899
                if (virAsprintf(&path, "%s/%s", XEND_DOMAINS_DIR, uuidstr) < 0)
900
                    goto cleanup;
901 902 903 904
                if (access(path, R_OK) == 0)
                    ret = 1;
                else if (errno == ENOENT)
                    ret = 0;
905 906 907 908
            }
        }
    }

909
 cleanup:
910
    virDomainDefFree(def);
911 912 913 914

    return ret;
}

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

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

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

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

933 934
    ret = xenDaemonDomainSuspend(dom->conn, def);

935
 cleanup:
936 937
    virDomainDefFree(def);
    return ret;
938 939 940
}

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

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

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

952 953
    ret = xenDaemonDomainResume(dom->conn, def);

954
 cleanup:
955 956
    virDomainDefFree(def);
    return ret;
957 958 959
}

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

966 967
    virCheckFlags(0, -1);

968 969 970
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

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

974 975
    ret = xenDaemonDomainShutdown(dom->conn, def);

976
 cleanup:
977 978
    virDomainDefFree(def);
    return ret;
979 980
}

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

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

993
    virCheckFlags(0, -1);
994

995 996 997
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

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

1001 1002
    ret = xenDaemonDomainReboot(dom->conn, def);

1003
 cleanup:
1004 1005
    virDomainDefFree(def);
    return ret;
1006 1007
}

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

1015 1016
    virCheckFlags(0, -1);

1017 1018 1019
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

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

1023 1024
    ret = xenDaemonDomainDestroy(dom->conn, def);

1025
 cleanup:
1026 1027
    virDomainDefFree(def);
    return ret;
1028 1029
}

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

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

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

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

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

1053
 cleanup:
1054 1055
    virDomainDefFree(def);
    return ret;
1056 1057
}

1058

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

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

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

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

1076
 cleanup:
1077 1078
    virDomainDefFree(def);
    return ret;
1079 1080 1081
}

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

1087 1088 1089
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

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

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

1098
 cleanup:
1099 1100
    virDomainDefFree(def);
    return ret;
1101 1102 1103
}

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

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

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

1115
    ret = xenDaemonDomainSetMemory(dom->conn, def, memory);
1116

1117
 cleanup:
1118 1119
    virDomainDefFree(def);
    return ret;
1120 1121 1122
}

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

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

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

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

1139
 cleanup:
1140 1141
    virDomainDefFree(def);
    return ret;
1142 1143
}

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

1151 1152
    int ret = -1;
    virDomainDefPtr def;
1153 1154 1155

    virCheckFlags(0, -1);

1156 1157 1158
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

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

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

1167
 cleanup:
1168 1169
    virDomainDefFree(def);
    return ret;
1170 1171
}

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

1179
    virCheckFlags(0, -1);
1180

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

1187 1188 1189
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

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

1193 1194
    ret = xenDaemonDomainSave(dom->conn, def, to);

1195
 cleanup:
1196 1197
    virDomainDefFree(def);
    return ret;
1198 1199 1200
}

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

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

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

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

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

    virCheckFlags(0, -1);

1229 1230 1231
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

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

1235
    if (!(name = xenUnifiedDomainManagedSavePath(priv, def)))
1236 1237
        goto cleanup;

1238
    ret = xenDaemonDomainSave(dom->conn, def, name);
1239

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

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

    virCheckFlags(0, -1);

1256 1257 1258
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

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

1262 1263
    if (!(name = xenUnifiedDomainManagedSavePath(priv, def)))
        goto cleanup;
1264 1265

    ret = virFileExists(name);
1266

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

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

    virCheckFlags(0, -1);

1283 1284 1285
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

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

1289 1290
    if (!(name = xenUnifiedDomainManagedSavePath(priv, def)))
        goto cleanup;
1291 1292

    ret = unlink(name);
1293

1294
 cleanup:
1295 1296 1297 1298
    VIR_FREE(name);
    return ret;
}

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

1310
    return xenDaemonDomainRestore(conn, from);
1311 1312
}

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

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

    virCheckFlags(0, -1);

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

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

1333 1334
    ret = xenDaemonDomainCoreDump(dom->conn, def, to, flags);

1335
 cleanup:
1336 1337
    virDomainDefFree(def);
    return ret;
1338 1339 1340
}

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

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

1366 1367 1368
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

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

1372
    ret = xenDaemonDomainSetVcpusFlags(dom->conn, def, nvcpus, flags);
1373

1374
 cleanup:
1375 1376
    virDomainDefFree(def);
    return ret;
1377 1378
}

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

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

    return xenUnifiedDomainSetVcpusFlags(dom, nvcpus, flags);
1390 1391
}

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

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

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

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

1410
 cleanup:
1411 1412
    virDomainDefFree(def);
    return ret;
1413 1414 1415
}

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

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

    return ret;
1434 1435 1436
}

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

1444 1445 1446
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

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

1450 1451 1452
    ret = xenUnifiedDomainGetVcpusInternal(dom, def, info, maxinfo, cpumaps,
                                           maplen);

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

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

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

1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
   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;

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

    ret = xenUnifiedDomainGetVcpusFlagsInternal(dom, def, flags);

1495
 cleanup:
1496 1497
    virDomainDefFree(def);
    return ret;
1498 1499
}

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

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

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

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

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

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

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

1538 1539

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

E
Eric Blake 已提交
1553 1554
    virCheckFlags(0, NULL);

1555 1556 1557
    if (virConnectDomainXMLFromNativeEnsureACL(conn) < 0)
        return NULL;

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

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

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

1584
    ret = virDomainDefFormat(def, priv->caps, 0);
1585

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


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

E
Eric Blake 已提交
1606 1607
    virCheckFlags(0, NULL);

1608 1609 1610
    if (virConnectDomainXMLToNativeEnsureACL(conn) < 0)
        return NULL;

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

1618
    if (!(def = virDomainDefParseString(xmlData, priv->caps, priv->xmlopt,
1619
                                        VIR_DOMAIN_DEF_PARSE_INACTIVE)))
1620 1621 1622 1623
        goto cleanup;

    if (STREQ(format, XEN_CONFIG_FORMAT_XM)) {
        int len = MAX_CONFIG_SIZE;
1624
        conf = xenFormatXM(conn, def);
1625 1626 1627
        if (!conf)
            goto cleanup;

1628
        if (VIR_ALLOC_N(ret, len) < 0)
1629 1630 1631 1632 1633 1634 1635
            goto cleanup;

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

1639
 cleanup:
1640 1641 1642 1643 1644 1645 1646
    virDomainDefFree(def);
    if (conf)
        virConfFree(conf);
    return ret;
}


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

1659 1660 1661
    return xenDaemonDomainMigratePrepare(dconn, cookie, cookielen,
                                         uri_in, uri_out,
                                         flags, dname, resource);
1662 1663 1664
}

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

E
Eric Blake 已提交
1676 1677
    virCheckFlags(XEN_MIGRATION_FLAGS, -1);

1678 1679 1680
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1681 1682 1683
    if (virDomainMigratePerformEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1684 1685 1686 1687
    ret = xenDaemonDomainMigratePerform(dom->conn, def,
                                        cookie, cookielen, uri,
                                        flags, dname, resource);

1688
 cleanup:
1689 1690
    virDomainDefFree(def);
    return ret;
1691 1692 1693
}

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

E
Eric Blake 已提交
1705 1706
    virCheckFlags(XEN_MIGRATION_FLAGS, NULL);

1707 1708
    if (!(minidef = xenGetDomainDefForName(dconn, dname)))
        goto cleanup;
1709

1710 1711 1712
    if (virDomainMigrateFinishEnsureACL(dconn, minidef) < 0)
        goto cleanup;

1713
    if (flags & VIR_MIGRATE_PERSIST_DEST) {
1714 1715
        if (!(def = xenDaemonDomainGetXMLDesc(dconn, minidef, NULL)))
            goto cleanup;
1716

1717 1718
        if (xenDaemonDomainDefineXML(dconn, def) < 0)
            goto cleanup;
1719 1720
    }

1721 1722 1723
    ret = virGetDomain(dconn, minidef->name, minidef->uuid);
    if (ret)
        ret->id = minidef->id;
1724

1725
 cleanup:
1726 1727 1728
    virDomainDefFree(def);
    virDomainDefFree(minidef);
    return ret;
1729 1730
}

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

1738
    return xenDaemonListDefinedDomains(conn, names, maxnames);
1739 1740 1741
}

static int
1742
xenUnifiedConnectNumOfDefinedDomains(virConnectPtr conn)
1743
{
1744 1745 1746
    if (virConnectNumOfDefinedDomainsEnsureACL(conn) < 0)
        return -1;

1747
    return xenDaemonNumOfDefinedDomains(conn);
1748 1749 1750
}

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

1758 1759
    virCheckFlags(0, -1);

1760 1761 1762
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1763 1764 1765
    if (virDomainCreateWithFlagsEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1766
    if (!(name = xenUnifiedDomainManagedSavePath(priv, def)))
1767 1768 1769
        goto cleanup;

    if (virFileExists(name)) {
1770 1771 1772
        ret = xenDaemonDomainRestore(dom->conn, name);
        if (ret == 0)
            unlink(name);
1773 1774 1775
        goto cleanup;
    }

1776
    ret = xenDaemonDomainCreate(dom->conn, def);
1777 1778 1779

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

1781
 cleanup:
1782
    virDomainDefFree(def);
1783 1784
    VIR_FREE(name);
    return ret;
1785 1786
}

1787
static int
1788
xenUnifiedDomainCreate(virDomainPtr dom)
1789 1790 1791 1792
{
    return xenUnifiedDomainCreateWithFlags(dom, 0);
}

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

1801 1802 1803
    virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL);

    if (flags & VIR_DOMAIN_DEFINE_VALIDATE)
1804
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA;
1805

1806
    if (!(def = virDomainDefParseString(xml, priv->caps, priv->xmlopt,
1807
                                        parse_flags)))
1808 1809
        goto cleanup;

1810
    if (virDomainDefineXMLFlagsEnsureACL(conn, def) < 0)
1811 1812
        goto cleanup;

1813 1814 1815
    if (xenDaemonDomainDefineXML(conn, def) < 0)
        goto cleanup;
    ret = virGetDomain(conn, def->name, def->uuid);
1816 1817 1818 1819

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

1820
 cleanup:
1821 1822
    virDomainDefFree(def);
    return ret;
1823 1824
}

1825 1826 1827 1828 1829 1830
static virDomainPtr
xenUnifiedDomainDefineXML(virConnectPtr conn, const char *xml)
{
    return xenUnifiedDomainDefineXMLFlags(conn, xml, 0);
}

1831
static int
1832
xenUnifiedDomainUndefineFlags(virDomainPtr dom, unsigned int flags)
1833
{
1834 1835
    virDomainDefPtr def = NULL;
    int ret = -1;
1836

1837
    virCheckFlags(0, -1);
1838 1839 1840 1841

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

1842 1843 1844
    if (virDomainUndefineFlagsEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1845
    ret = xenDaemonDomainUndefine(dom->conn, def);
1846

1847
 cleanup:
1848 1849
    virDomainDefFree(def);
    return ret;
1850 1851
}

1852
static int
1853 1854
xenUnifiedDomainUndefine(virDomainPtr dom)
{
1855 1856 1857
    return xenUnifiedDomainUndefineFlags(dom, 0);
}

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

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

1871 1872 1873
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1874 1875 1876
    if (virDomainAttachDeviceEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1877
    ret = xenDaemonAttachDeviceFlags(dom->conn, def, xml, flags);
1878

1879
 cleanup:
1880 1881
    virDomainDefFree(def);
    return ret;
1882 1883 1884
}

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

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

1894 1895 1896
    if (virDomainAttachDeviceFlagsEnsureACL(dom->conn, def, flags) < 0)
        goto cleanup;

1897
    ret = xenDaemonAttachDeviceFlags(dom->conn, def, xml, flags);
1898

1899
 cleanup:
1900 1901
    virDomainDefFree(def);
    return ret;
1902 1903 1904
}

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

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

1917 1918 1919
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1920 1921 1922
    if (virDomainDetachDeviceEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1923
    ret = xenDaemonDetachDeviceFlags(dom->conn, def, xml, flags);
1924

1925
 cleanup:
1926 1927
    virDomainDefFree(def);
    return ret;
1928 1929 1930
}

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

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

1940 1941 1942
    if (virDomainDetachDeviceFlagsEnsureACL(dom->conn, def, flags) < 0)
        goto cleanup;

1943
    ret = xenDaemonDetachDeviceFlags(dom->conn, def, xml, flags);
1944

1945
 cleanup:
1946 1947
    virDomainDefFree(def);
    return ret;
1948 1949
}

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

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

1960 1961 1962
    if (virDomainUpdateDeviceFlagsEnsureACL(dom->conn, def, flags) < 0)
        goto cleanup;

1963 1964
    ret = xenDaemonUpdateDeviceFlags(dom->conn, def, xml, flags);

1965
 cleanup:
1966 1967
    virDomainDefFree(def);
    return ret;
1968 1969
}

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

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

1979 1980 1981
    if (virDomainGetAutostartEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1982
    ret = xenDaemonDomainGetAutostart(dom->conn, def, autostart);
1983

1984
 cleanup:
1985 1986
    virDomainDefFree(def);
    return ret;
1987 1988 1989
}

static int
1990
xenUnifiedDomainSetAutostart(virDomainPtr dom, int autostart)
1991
{
1992 1993 1994 1995 1996
    virDomainDefPtr def = NULL;
    int ret = -1;

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

1998 1999 2000
    if (virDomainSetAutostartEnsureACL(dom->conn, def) < 0)
        goto cleanup;

2001
    ret = xenDaemonDomainSetAutostart(dom->conn, def, autostart);
2002

2003
 cleanup:
2004 2005
    virDomainDefFree(def);
    return ret;
2006 2007
}

2008
static char *
2009
xenUnifiedDomainGetSchedulerType(virDomainPtr dom, int *nparams)
2010
{
2011 2012 2013 2014 2015
    virDomainDefPtr def = NULL;
    char *ret = NULL;

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

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

2020
    if (dom->id < 0)
2021
        ret = xenDaemonGetSchedulerType(dom->conn, nparams);
2022
    else
2023 2024
        ret = xenHypervisorGetSchedulerType(dom->conn, nparams);

2025
 cleanup:
2026 2027
    virDomainDefFree(def);
    return ret;
2028 2029 2030
}

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

2039 2040
    virCheckFlags(0, -1);

2041 2042 2043
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

2044 2045 2046
    if (virDomainGetSchedulerParametersFlagsEnsureACL(dom->conn, def) < 0)
        goto cleanup;

2047
    if (dom->id < 0)
2048
        ret = xenDaemonGetSchedulerParameters(dom->conn, def, params, nparams);
2049
    else
2050 2051
        ret = xenHypervisorGetSchedulerParameters(dom->conn, def, params, nparams);

2052
 cleanup:
2053 2054
    virDomainDefFree(def);
    return ret;
2055 2056 2057
}

static int
2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070
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)
2071
{
2072 2073
    virDomainDefPtr def = NULL;
    int ret = -1;
2074

2075 2076
    virCheckFlags(0, -1);

2077 2078 2079
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

2080 2081 2082
    if (virDomainSetSchedulerParametersFlagsEnsureACL(dom->conn, def, flags) < 0)
        goto cleanup;

2083
    if (dom->id < 0)
2084
        ret = xenDaemonSetSchedulerParameters(dom->conn, def, params, nparams);
2085
    else
2086 2087
        ret = xenHypervisorSetSchedulerParameters(dom->conn, def, params, nparams);

2088
 cleanup:
2089 2090
    virDomainDefFree(def);
    return ret;
2091 2092
}

2093 2094 2095 2096 2097 2098 2099 2100 2101
static int
xenUnifiedDomainSetSchedulerParameters(virDomainPtr dom,
                                       virTypedParameterPtr params,
                                       int nparams)
{
    return xenUnifiedDomainSetSchedulerParametersFlags(dom, params,
                                                       nparams, 0);
}

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

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

2112 2113 2114
    if (virDomainBlockStatsEnsureACL(dom->conn, def) < 0)
        goto cleanup;

2115 2116
    ret = xenHypervisorDomainBlockStats(dom->conn, def, path, stats);

2117
 cleanup:
2118 2119
    virDomainDefFree(def);
    return ret;
2120 2121 2122
}

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

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

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

2135 2136
    ret = xenHypervisorDomainInterfaceStats(def, path, stats);

2137
 cleanup:
2138 2139
    virDomainDefFree(def);
    return ret;
2140 2141
}

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

E
Eric Blake 已提交
2150 2151
    virCheckFlags(0, -1);

2152 2153 2154
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

2155 2156 2157
    if (virDomainBlockPeekEnsureACL(dom->conn, def) < 0)
        goto cleanup;

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

2160
 cleanup:
2161 2162
    virDomainDefFree(def);
    return ret;
R
Richard W.M. Jones 已提交
2163 2164
}

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

2172 2173
    return xenHypervisorNodeGetCellsFreeMemory(conn, freeMems,
                                               startCell, maxCells);
2174 2175
}

2176
static unsigned long long
2177
xenUnifiedNodeGetFreeMemory(virConnectPtr conn)
2178 2179 2180
{
    unsigned long long freeMem = 0;

2181 2182 2183
    if (virNodeGetFreeMemoryEnsureACL(conn) < 0)
        return 0;

2184 2185 2186
    if (xenHypervisorNodeGetCellsFreeMemory(conn, &freeMem, -1, 1) < 0)
        return 0;
    return freeMem;
2187 2188
}

2189

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

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

D
Daniel P. Berrange 已提交
2202
    xenUnifiedLock(priv);
2203

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

2210 2211 2212
    if (virDomainEventStateRegister(conn, priv->domainEvents,
                                    callback, opaque, freefunc) < 0)
        ret = -1;
2213

D
Daniel P. Berrange 已提交
2214
    xenUnifiedUnlock(priv);
2215
    return ret;
2216 2217
}

2218

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

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

D
Daniel P. Berrange 已提交
2229 2230
    xenUnifiedLock(priv);

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

2237 2238 2239 2240
    if (virDomainEventStateDeregister(conn,
                                      priv->domainEvents,
                                      callback) < 0)
        ret = -1;
2241

D
Daniel P. Berrange 已提交
2242
    xenUnifiedUnlock(priv);
2243 2244 2245
    return ret;
}

2246

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

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

2261 2262 2263
    xenUnifiedLock(priv);

    if (priv->xsWatch == -1) {
2264
        virReportUnsupportedError();
2265 2266 2267 2268
        xenUnifiedUnlock(priv);
        return -1;
    }

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

    xenUnifiedUnlock(priv);
2275
    return ret;
2276 2277 2278
}

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

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

2288 2289 2290
    xenUnifiedLock(priv);

    if (priv->xsWatch == -1) {
2291
        virReportUnsupportedError();
2292 2293 2294 2295
        xenUnifiedUnlock(priv);
        return -1;
    }

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

    xenUnifiedUnlock(priv);
    return ret;
}


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

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

2322
    def = virNodeDeviceDefParseString(xml, EXISTING_DEVICE, NULL);
2323 2324 2325 2326 2327
    if (!def)
        goto out;

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

    ret = 0;
2346
 out:
2347 2348 2349 2350 2351 2352
    virNodeDeviceDefFree(def);
    VIR_FREE(xml);
    return ret;
}

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

2361 2362
    virCheckFlags(0, -1);

2363
    if (xenUnifiedNodeDeviceGetPCIInfo(dev, &domain, &bus, &slot, &function) < 0)
2364 2365
        return -1;

2366
    pci = virPCIDeviceNew(domain, bus, slot, function);
2367 2368 2369
    if (!pci)
        return -1;

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

2378
    if (virPCIDeviceDetach(pci, NULL, NULL) < 0)
2379 2380 2381
        goto out;

    ret = 0;
2382
 out:
2383
    virPCIDeviceFree(pci);
2384 2385 2386
    return ret;
}

2387 2388 2389 2390 2391 2392
static int
xenUnifiedNodeDeviceDettach(virNodeDevicePtr dev)
{
    return xenUnifiedNodeDeviceDetachFlags(dev, NULL, 0);
}

2393
static int
2394
xenUnifiedNodeDeviceAssignedDomainId(virNodeDevicePtr dev)
2395 2396
{
    int numdomains;
2397 2398
    int ret = -1;
    size_t i;
2399 2400 2401 2402 2403 2404 2405 2406
    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 */
2407
    numdomains = xenUnifiedConnectNumOfDomains(conn);
2408
    if (numdomains < 0)
2409
        return ret;
2410
    if (numdomains > 0) {
2411
        if (VIR_ALLOC_N(ids, numdomains) < 0)
2412
            goto out;
2413
        if ((numdomains = xenUnifiedConnectListDomains(conn, &ids[0], numdomains)) < 0)
2414 2415 2416 2417
            goto out;
    }

    /* Get pci bdf */
2418
    if (xenUnifiedNodeDeviceGetPCIInfo(dev, &domain, &bus, &slot, &function) < 0)
2419 2420 2421
        goto out;

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

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

    VIR_FREE(xref);
    VIR_FREE(bdf);
2440
 out:
2441 2442 2443 2444 2445
    VIR_FREE(ids);

    return ret;
}

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

2454
    if (xenUnifiedNodeDeviceGetPCIInfo(dev, &domain, &bus, &slot, &function) < 0)
2455 2456
        return -1;

2457
    pci = virPCIDeviceNew(domain, bus, slot, function);
2458 2459 2460
    if (!pci)
        return -1;

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

2469
    if (virPCIDeviceReattach(pci, NULL, NULL) < 0)
2470 2471 2472
        goto out;

    ret = 0;
2473
 out:
2474
    virPCIDeviceFree(pci);
2475 2476 2477 2478
    return ret;
}

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

2485
    if (xenUnifiedNodeDeviceGetPCIInfo(dev, &domain, &bus, &slot, &function) < 0)
2486 2487
        return -1;

2488
    pci = virPCIDeviceNew(domain, bus, slot, function);
2489 2490 2491
    if (!pci)
        return -1;

2492
    if (virPCIDeviceReset(pci, NULL, NULL) < 0)
2493 2494 2495
        goto out;

    ret = 0;
2496
 out:
2497
    virPCIDeviceFree(pci);
2498 2499 2500 2501
    return ret;
}


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

    virCheckFlags(0, -1);

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

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

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

2531 2532
    if (def->nconsoles)
        chr = def->consoles[0];
2533 2534 2535 2536
    else if (def->nserials)
        chr = def->serials[0];

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

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

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

    ret = 0;
2554
 cleanup:
2555 2556 2557
    virDomainDefFree(def);
    return ret;
}
2558 2559

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

2568
    return virHostMemGetParameters(params, nparams, flags);
2569 2570 2571 2572
}


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

2581
    return virHostMemSetParameters(params, nparams, flags);
2582 2583
}

2584 2585

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

2594 2595 2596 2597
    return nodeSuspendForDuration(target, duration, flags);
}


E
Eric Blake 已提交
2598
/*----- Register with libvirt.c, and initialize Xen drivers. -----*/
2599 2600

/* The interface which we export upwards to libvirt.c. */
2601
static virHypervisorDriver xenUnifiedHypervisorDriver = {
2602
    .name = "Xen",
2603 2604 2605 2606 2607
    .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 */
2608
    .connectGetHostname = xenUnifiedConnectGetHostname, /* 0.7.3 */
2609
    .connectGetSysinfo = xenUnifiedConnectGetSysinfo, /* 1.1.0 */
2610
    .connectGetMaxVcpus = xenUnifiedConnectGetMaxVcpus, /* 0.2.1 */
2611
    .nodeGetInfo = xenUnifiedNodeGetInfo, /* 0.1.0 */
2612 2613 2614
    .connectGetCapabilities = xenUnifiedConnectGetCapabilities, /* 0.2.1 */
    .connectListDomains = xenUnifiedConnectListDomains, /* 0.0.3 */
    .connectNumOfDomains = xenUnifiedConnectNumOfDomains, /* 0.0.3 */
2615 2616 2617 2618 2619 2620 2621
    .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 */
2622
    .domainShutdownFlags = xenUnifiedDomainShutdownFlags, /* 0.9.10 */
2623 2624
    .domainReboot = xenUnifiedDomainReboot, /* 0.1.0 */
    .domainDestroy = xenUnifiedDomainDestroy, /* 0.0.3 */
2625
    .domainDestroyFlags = xenUnifiedDomainDestroyFlags, /* 0.9.4 */
2626 2627 2628 2629 2630 2631 2632
    .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 */
2633
    .domainSaveFlags = xenUnifiedDomainSaveFlags, /* 0.9.4 */
2634 2635 2636
    .domainManagedSave = xenUnifiedDomainManagedSave, /* 1.0.1 */
    .domainHasManagedSaveImage = xenUnifiedDomainHasManagedSaveImage, /* 1.0.1 */
    .domainManagedSaveRemove = xenUnifiedDomainManagedSaveRemove, /* 1.0.1 */
2637
    .domainRestore = xenUnifiedDomainRestore, /* 0.0.3 */
2638
    .domainRestoreFlags = xenUnifiedDomainRestoreFlags, /* 0.9.4 */
2639 2640 2641 2642 2643 2644 2645 2646
    .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 */
2647 2648 2649 2650
    .connectDomainXMLFromNative = xenUnifiedConnectDomainXMLFromNative, /* 0.6.4 */
    .connectDomainXMLToNative = xenUnifiedConnectDomainXMLToNative, /* 0.6.4 */
    .connectListDefinedDomains = xenUnifiedConnectListDefinedDomains, /* 0.1.1 */
    .connectNumOfDefinedDomains = xenUnifiedConnectNumOfDefinedDomains, /* 0.1.5 */
2651 2652 2653
    .domainCreate = xenUnifiedDomainCreate, /* 0.1.1 */
    .domainCreateWithFlags = xenUnifiedDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = xenUnifiedDomainDefineXML, /* 0.1.1 */
2654
    .domainDefineXMLFlags = xenUnifiedDomainDefineXMLFlags, /* 1.2.12 */
2655
    .domainUndefine = xenUnifiedDomainUndefine, /* 0.1.1 */
2656
    .domainUndefineFlags = xenUnifiedDomainUndefineFlags, /* 0.9.4 */
2657 2658 2659 2660 2661 2662 2663 2664 2665
    .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 */
2666
    .domainGetSchedulerParametersFlags = xenUnifiedDomainGetSchedulerParametersFlags, /* 0.9.2 */
2667
    .domainSetSchedulerParameters = xenUnifiedDomainSetSchedulerParameters, /* 0.2.3 */
2668
    .domainSetSchedulerParametersFlags = xenUnifiedDomainSetSchedulerParametersFlags, /* 0.9.2 */
2669 2670 2671 2672 2673 2674 2675 2676
    .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 */
2677 2678
    .connectDomainEventRegister = xenUnifiedConnectDomainEventRegister, /* 0.5.0 */
    .connectDomainEventDeregister = xenUnifiedConnectDomainEventDeregister, /* 0.5.0 */
2679
    .nodeDeviceDettach = xenUnifiedNodeDeviceDettach, /* 0.6.1 */
2680
    .nodeDeviceDetachFlags = xenUnifiedNodeDeviceDetachFlags, /* 1.0.5 */
2681 2682
    .nodeDeviceReAttach = xenUnifiedNodeDeviceReAttach, /* 0.6.1 */
    .nodeDeviceReset = xenUnifiedNodeDeviceReset, /* 0.6.1 */
2683 2684
    .connectIsEncrypted = xenUnifiedConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = xenUnifiedConnectIsSecure, /* 0.7.3 */
2685 2686 2687
    .domainIsActive = xenUnifiedDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = xenUnifiedDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = xenUnifiedDomainIsUpdated, /* 0.8.6 */
2688 2689
    .connectDomainEventRegisterAny = xenUnifiedConnectDomainEventRegisterAny, /* 0.8.0 */
    .connectDomainEventDeregisterAny = xenUnifiedConnectDomainEventDeregisterAny, /* 0.8.0 */
2690
    .domainOpenConsole = xenUnifiedDomainOpenConsole, /* 0.8.6 */
2691
    .connectIsAlive = xenUnifiedConnectIsAlive, /* 0.9.8 */
2692
    .nodeSuspendForDuration = xenUnifiedNodeSuspendForDuration, /* 0.9.8 */
2693 2694
    .nodeGetMemoryParameters = xenUnifiedNodeGetMemoryParameters, /* 0.10.2 */
    .nodeSetMemoryParameters = xenUnifiedNodeSetMemoryParameters, /* 0.10.2 */
2695 2696
};

2697 2698 2699 2700 2701

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

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

2714 2715
    return virRegisterConnectDriver(&xenUnifiedConnectDriver,
                                    true);
2716 2717
}

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

    if (list == NULL)
        return;

2731
    for (i = 0; i < list->count; i++) {
2732 2733 2734
        VIR_FREE(list->doms[i]->name);
        VIR_FREE(list->doms[i]);
    }
2735
    VIR_FREE(list->doms);
2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754
    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 */
2755
    for (n = 0; n < list->count; n++) {
2756 2757
        if (STREQ(list->doms[n]->name, name) &&
            !memcmp(list->doms[n]->uuid, uuid, VIR_UUID_BUFLEN)) {
2758
            VIR_DEBUG("WARNING: dom already tracked");
2759 2760 2761 2762 2763
            return -1;
        }
    }

    if (VIR_ALLOC(info) < 0)
2764
        goto error;
2765 2766
    if (VIR_STRDUP(info->name, name) < 0)
        goto error;
2767 2768 2769 2770 2771

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

    /* Make space on list */
2772
    if (VIR_APPEND_ELEMENT(list->doms, list->count, info) < 0)
2773
        goto error;
2774 2775

    return 0;
2776
 error:
2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794
    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)
{
2795
    size_t i;
2796
    for (i = 0; i < list->count; i++) {
2797
        if (list->doms[i]->id == id &&
2798 2799 2800 2801 2802 2803
            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]);

2804
            VIR_DELETE_ELEMENT(list->doms, i, list->count);
2805 2806 2807 2808 2809 2810
            return 0;
        }
    }
    return -1;
}

2811

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

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

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

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