xen_driver.c 74.2 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 "configmake.h"
69
#include "virstring.h"
70
#include "viraccessapicheck.h"
71

72
#define VIR_FROM_THIS VIR_FROM_XEN
73 74 75

VIR_LOG_INIT("xen.xen_driver");

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

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

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

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

94

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

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

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

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

    return ret;
}


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


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

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

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

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

D
Daniel P. Berrange 已提交
173

174 175 176
/**
 * xenDomainUsedCpus:
 * @dom: the domain
177
 * @def: the domain definition
178 179 180 181 182 183 184 185
 *
 * 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 *
186
xenDomainUsedCpus(virDomainPtr dom, virDomainDefPtr def)
187 188
{
    char *res = NULL;
D
Daniel P. Berrange 已提交
189
    int ncpus;
190
    int nb_vcpu;
191
    virBitmapPtr cpulist = NULL;
192 193 194 195 196 197
    unsigned char *cpumap = NULL;
    size_t cpumaplen;
    int nb = 0;
    int n, m;
    virVcpuInfoPtr cpuinfo = NULL;
    virNodeInfo nodeinfo;
D
Daniel P. Berrange 已提交
198
    xenUnifiedPrivatePtr priv;
199

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

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

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

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

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

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

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

    return 0;
}

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

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

273 274 275 276 277 278 279 280 281 282 283
/*----- 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.
 */

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

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

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

308 309 310 311 312 313 314 315
    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 已提交
316 317 318 319 320

    return ret;
}
#endif

321

322 323
static int
xenDomainDeviceDefPostParse(virDomainDeviceDefPtr dev,
324
                            const virDomainDef *def,
325
                            virCapsPtr caps ATTRIBUTE_UNUSED,
326
                            unsigned int parseFlags ATTRIBUTE_UNUSED,
327 328 329 330 331
                            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 &&
332
        def->os.type != VIR_DOMAIN_OSTYPE_HVM)
333 334
        dev->data.chr->targetType = VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_XEN;

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

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

364 365 366 367 368 369 370
    return 0;
}


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

387 388 389 390
    return 0;
}


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

397

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


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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

541
    return VIR_DRV_OPEN_SUCCESS;
542

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

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

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

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

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

590
    return 0;
591 592
}

593 594 595 596 597 598 599 600 601 602 603

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


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

610
    return "Xen";
611 612
}

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

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

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

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

638

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

644 645 646
    return virGetHostname();
}

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

    virCheckFlags(0, NULL);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

747
    return xenStoreNumOfDomains(conn);
748 749 750
}

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

760 761 762
    virCheckFlags(VIR_DOMAIN_START_VALIDATE, NULL);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

852 853 854 855

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

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

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

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

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

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

    return ret;
}

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

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

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

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

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

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

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

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

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

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

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

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

965 966
    virCheckFlags(0, -1);

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

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

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

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

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

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

992
    virCheckFlags(0, -1);
993

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

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

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

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

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

1014 1015
    virCheckFlags(0, -1);

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

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

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

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

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

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

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

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

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

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

1057

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    virCheckFlags(0, -1);

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

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

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

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

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

1178
    virCheckFlags(0, -1);
1179

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

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

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

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

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

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

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

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

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

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

    virCheckFlags(0, -1);

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

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

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

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

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

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

    virCheckFlags(0, -1);

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

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

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

    ret = virFileExists(name);
1265

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

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

    virCheckFlags(0, -1);

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

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

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

    ret = unlink(name);
1292

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

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

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

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

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

    virCheckFlags(0, -1);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    return ret;
1433 1434 1435
}

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

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

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

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

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

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

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

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

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

    ret = xenUnifiedDomainGetVcpusFlagsInternal(dom, def, flags);

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

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

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

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

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

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

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

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

1537 1538

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1746
    return xenDaemonNumOfDefinedDomains(conn);
1747 1748 1749
}

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

1757 1758
    virCheckFlags(0, -1);

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

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

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

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

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

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

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

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

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

1800 1801 1802
    virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2038 2039
    virCheckFlags(0, -1);

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

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

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

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

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

2074 2075
    virCheckFlags(0, -1);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2188

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

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

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

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

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

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

2217

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

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

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

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

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

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

2245

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

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

2260 2261 2262
    xenUnifiedLock(priv);

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

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

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

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

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

2287 2288 2289
    xenUnifiedLock(priv);

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

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

    xenUnifiedUnlock(priv);
    return ret;
}


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

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

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

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

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

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

2360 2361
    virCheckFlags(0, -1);

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

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

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

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

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

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

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

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

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

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

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

    return ret;
}

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

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

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

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

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

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

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

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

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

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

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


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

    virCheckFlags(0, -1);

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

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

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

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

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

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

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

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

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

2567 2568 2569 2570 2571
    return nodeGetMemoryParameters(params, nparams, flags);
}


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

2580 2581 2582
    return nodeSetMemoryParameters(params, nparams, flags);
}

2583 2584

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

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


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

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

2696 2697 2698 2699 2700

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

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

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

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

    if (list == NULL)
        return;

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

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

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

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

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

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

2810

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

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

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

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