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

23
#include <config.h>
24

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

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

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

47
#include "xen_sxpr.h"
48
#include "xen_xm.h"
P
Pavel Hrdina 已提交
49
#include "xen_common.h"
50
#include "xen_hypervisor.h"
51 52 53
#include "xend_internal.h"
#include "xs_internal.h"
#include "xm_internal.h"
54
#if WITH_XEN_INOTIFY
55
# include "xen_inotify.h"
56
#endif
57
#include "virxml.h"
58
#include "viralloc.h"
59
#include "node_device_conf.h"
60
#include "virpci.h"
61
#include "viruuid.h"
62
#include "virfdstream.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 "virhostmem.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
                            void *opaque ATTRIBUTE_UNUSED,
                            void *parseOpaque ATTRIBUTE_UNUSED)
329 330 331 332
{
    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,
                      void *parseOpaque ATTRIBUTE_UNUSED)
375
{
376 377 378 379 380 381 382 383 384
    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 已提交
385 386 387 388
    /* add implicit input device */
    if (xenDomainDefAddImplicitInputDevice(def) <0)
        return -1;

389 390 391 392
    return 0;
}


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

399

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


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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

543
    return VIR_DRV_OPEN_SUCCESS;
544

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

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

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

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

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

592
    return 0;
593 594
}

595 596 597 598 599 600 601 602 603 604 605

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


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

612
    return "Xen";
613 614
}

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

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

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

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

640

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

646 647 648
    return virGetHostname();
}

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

    virCheckFlags(0, NULL);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

749
    return xenStoreNumOfDomains(conn);
750 751 752
}

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

762 763 764
    virCheckFlags(VIR_DOMAIN_START_VALIDATE, NULL);

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

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

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

774 775 776
    if (xenDaemonCreateXML(conn, def) < 0)
        goto cleanup;

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

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
    ret = virGetDomain(conn, def->name, def->uuid, def->id);
797

798
 cleanup:
799
    virDomainDefFree(def);
800
    return ret;
801 802 803
}

static virDomainPtr
804 805
xenUnifiedDomainLookupByUUID(virConnectPtr conn,
                             const unsigned char *uuid)
806
{
807 808
    virDomainPtr ret = NULL;
    virDomainDefPtr def = NULL;
809

810 811
    if (!(def = xenGetDomainDefForUUID(conn, uuid)))
        goto cleanup;
812

813 814 815
    if (virDomainLookupByUUIDEnsureACL(conn, def) < 0)
        goto cleanup;

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

818
 cleanup:
819
    virDomainDefFree(def);
820
    return ret;
821 822 823
}

static virDomainPtr
824 825
xenUnifiedDomainLookupByName(virConnectPtr conn,
                             const char *name)
826
{
827 828
    virDomainPtr ret = NULL;
    virDomainDefPtr def = NULL;
829

830 831
    if (!(def = xenGetDomainDefForName(conn, name)))
        goto cleanup;
832

833 834 835
    if (virDomainLookupByNameEnsureACL(conn, def) < 0)
        goto cleanup;

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

838
 cleanup:
839
    virDomainDefFree(def);
840
    return ret;
841 842
}

843 844 845 846

static int
xenUnifiedDomainIsActive(virDomainPtr dom)
{
847
    virDomainDefPtr def;
848 849
    int ret = -1;

850 851
    if (!(def = xenGetDomainDefForUUID(dom->conn, dom->uuid)))
        goto cleanup;
852

853
    ret = def->id == -1 ? 0 : 1;
854

855
 cleanup:
856
    virDomainDefFree(def);
857 858 859 860
    return ret;
}

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

899
 cleanup:
900
    virDomainDefFree(def);
901 902 903 904

    return ret;
}

905 906 907 908 909 910
static int
xenUnifiedDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

911
static int
912
xenUnifiedDomainSuspend(virDomainPtr dom)
913
{
914 915 916 917 918 919
    int ret = -1;
    virDomainDefPtr def;

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

920 921 922
    if (virDomainSuspendEnsureACL(dom->conn, def) < 0)
        goto cleanup;

923 924
    ret = xenDaemonDomainSuspend(dom->conn, def);

925
 cleanup:
926 927
    virDomainDefFree(def);
    return ret;
928 929 930
}

static int
931
xenUnifiedDomainResume(virDomainPtr dom)
932
{
933 934 935 936 937 938
    int ret = -1;
    virDomainDefPtr def;

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

939 940 941
    if (virDomainResumeEnsureACL(dom->conn, def) < 0)
        goto cleanup;

942 943
    ret = xenDaemonDomainResume(dom->conn, def);

944
 cleanup:
945 946
    virDomainDefFree(def);
    return ret;
947 948 949
}

static int
950 951
xenUnifiedDomainShutdownFlags(virDomainPtr dom,
                              unsigned int flags)
952
{
953 954 955
    int ret = -1;
    virDomainDefPtr def;

956 957
    virCheckFlags(0, -1);

958 959 960
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

961
    if (virDomainShutdownFlagsEnsureACL(dom->conn, def, flags) < 0)
962 963
        goto cleanup;

964 965
    ret = xenDaemonDomainShutdown(dom->conn, def);

966
 cleanup:
967 968
    virDomainDefFree(def);
    return ret;
969 970
}

971 972 973 974 975 976
static int
xenUnifiedDomainShutdown(virDomainPtr dom)
{
    return xenUnifiedDomainShutdownFlags(dom, 0);
}

977
static int
978
xenUnifiedDomainReboot(virDomainPtr dom, unsigned int flags)
979
{
980 981 982
    int ret = -1;
    virDomainDefPtr def;

983
    virCheckFlags(0, -1);
984

985 986 987
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

988
    if (virDomainRebootEnsureACL(dom->conn, def, flags) < 0)
989 990
        goto cleanup;

991 992
    ret = xenDaemonDomainReboot(dom->conn, def);

993
 cleanup:
994 995
    virDomainDefFree(def);
    return ret;
996 997
}

998 999 1000 1001
static int
xenUnifiedDomainDestroyFlags(virDomainPtr dom,
                             unsigned int flags)
{
1002 1003 1004
    int ret = -1;
    virDomainDefPtr def;

1005 1006
    virCheckFlags(0, -1);

1007 1008 1009
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1010 1011 1012
    if (virDomainDestroyFlagsEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1013 1014
    ret = xenDaemonDomainDestroy(dom->conn, def);

1015
 cleanup:
1016 1017
    virDomainDefFree(def);
    return ret;
1018 1019
}

1020 1021 1022 1023 1024 1025
static int
xenUnifiedDomainDestroy(virDomainPtr dom)
{
    return xenUnifiedDomainDestroyFlags(dom, 0);
}

1026
static char *
1027
xenUnifiedDomainGetOSType(virDomainPtr dom)
1028
{
1029 1030 1031 1032 1033
    char *ret = NULL;
    virDomainDefPtr def;

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

1035 1036 1037
    if (virDomainGetOSTypeEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1038 1039 1040
    if (def->id < 0)
        ret = xenDaemonDomainGetOSType(dom->conn, def);
    else
1041 1042
        ret = xenHypervisorDomainGetOSType(dom->conn, def);

1043
 cleanup:
1044 1045
    virDomainDefFree(def);
    return ret;
1046 1047
}

1048

1049
static unsigned long long
1050
xenUnifiedDomainGetMaxMemory(virDomainPtr dom)
1051
{
1052 1053 1054 1055 1056
    unsigned long long ret = 0;
    virDomainDefPtr def;

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

1058 1059 1060
    if (virDomainGetMaxMemoryEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1061 1062 1063
    if (def->id < 0)
        ret = xenDaemonDomainGetMaxMemory(dom->conn, def);
    else
1064 1065
        ret = xenHypervisorGetMaxMemory(dom->conn, def);

1066
 cleanup:
1067 1068
    virDomainDefFree(def);
    return ret;
1069 1070 1071
}

static int
1072
xenUnifiedDomainSetMaxMemory(virDomainPtr dom, unsigned long memory)
1073
{
1074 1075
    int ret = -1;
    virDomainDefPtr def;
1076

1077 1078 1079
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1080 1081 1082
    if (virDomainSetMaxMemoryEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1083 1084 1085
    if (def->id < 0)
        ret = xenDaemonDomainSetMaxMemory(dom->conn, def, memory);
    else
1086 1087
        ret = xenHypervisorSetMaxMemory(dom->conn, def, memory);

1088
 cleanup:
1089 1090
    virDomainDefFree(def);
    return ret;
1091 1092 1093
}

static int
1094
xenUnifiedDomainSetMemory(virDomainPtr dom, unsigned long memory)
1095
{
1096 1097 1098 1099 1100
    int ret = -1;
    virDomainDefPtr def;

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

1102 1103 1104
    if (virDomainSetMemoryEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1105
    ret = xenDaemonDomainSetMemory(dom->conn, def, memory);
1106

1107
 cleanup:
1108 1109
    virDomainDefFree(def);
    return ret;
1110 1111 1112
}

static int
1113
xenUnifiedDomainGetInfo(virDomainPtr dom, virDomainInfoPtr info)
1114
{
1115 1116 1117 1118 1119
    int ret = -1;
    virDomainDefPtr def;

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

1121 1122 1123
    if (virDomainGetInfoEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1124 1125 1126
    if (def->id < 0)
        ret = xenDaemonDomainGetInfo(dom->conn, def, info);
    else
1127 1128
        ret = xenHypervisorGetDomainInfo(dom->conn, def, info);

1129
 cleanup:
1130 1131
    virDomainDefFree(def);
    return ret;
1132 1133
}

1134 1135 1136 1137 1138 1139
static int
xenUnifiedDomainGetState(virDomainPtr dom,
                         int *state,
                         int *reason,
                         unsigned int flags)
{
1140

1141 1142
    int ret = -1;
    virDomainDefPtr def;
1143 1144 1145

    virCheckFlags(0, -1);

1146 1147 1148
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1149 1150 1151
    if (virDomainGetStateEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1152 1153 1154
    if (def->id < 0)
        ret = xenDaemonDomainGetState(dom->conn, def, state, reason);
    else
1155 1156
        ret = xenHypervisorGetDomainState(dom->conn, def, state, reason);

1157
 cleanup:
1158 1159
    virDomainDefFree(def);
    return ret;
1160 1161
}

1162
static int
1163 1164
xenUnifiedDomainSaveFlags(virDomainPtr dom, const char *to, const char *dxml,
                          unsigned int flags)
1165
{
1166 1167 1168
    int ret = -1;
    virDomainDefPtr def;

1169
    virCheckFlags(0, -1);
1170

1171
    if (dxml) {
1172 1173
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
1174 1175 1176
        return -1;
    }

1177 1178 1179
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1180 1181 1182
    if (virDomainSaveFlagsEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1183 1184
    ret = xenDaemonDomainSave(dom->conn, def, to);

1185
 cleanup:
1186 1187
    virDomainDefFree(def);
    return ret;
1188 1189 1190
}

static int
1191 1192 1193 1194 1195
xenUnifiedDomainSave(virDomainPtr dom, const char *to)
{
    return xenUnifiedDomainSaveFlags(dom, to, NULL, 0);
}

1196
static char *
1197 1198
xenUnifiedDomainManagedSavePath(xenUnifiedPrivatePtr priv,
                                virDomainDefPtr def)
1199 1200 1201
{
    char *ret;

1202
    if (virAsprintf(&ret, "%s/%s.save", priv->saveDir, def->name) < 0)
1203 1204 1205 1206 1207 1208 1209 1210 1211
        return NULL;

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

static int
xenUnifiedDomainManagedSave(virDomainPtr dom, unsigned int flags)
{
1212
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1213 1214
    char *name = NULL;
    virDomainDefPtr def = NULL;
1215 1216 1217 1218
    int ret = -1;

    virCheckFlags(0, -1);

1219 1220 1221
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1222 1223 1224
    if (virDomainManagedSaveEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1225
    if (!(name = xenUnifiedDomainManagedSavePath(priv, def)))
1226 1227
        goto cleanup;

1228
    ret = xenDaemonDomainSave(dom->conn, def, name);
1229

1230
 cleanup:
1231
    VIR_FREE(name);
1232
    virDomainDefFree(def);
1233 1234 1235 1236 1237 1238
    return ret;
}

static int
xenUnifiedDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
{
1239
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1240 1241
    char *name = NULL;
    virDomainDefPtr def = NULL;
1242 1243 1244 1245
    int ret = -1;

    virCheckFlags(0, -1);

1246 1247 1248
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1249 1250 1251
    if (virDomainHasManagedSaveImageEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1252 1253
    if (!(name = xenUnifiedDomainManagedSavePath(priv, def)))
        goto cleanup;
1254 1255

    ret = virFileExists(name);
1256

1257
 cleanup:
1258
    VIR_FREE(name);
1259
    virDomainDefFree(def);
1260 1261 1262 1263 1264 1265
    return ret;
}

static int
xenUnifiedDomainManagedSaveRemove(virDomainPtr dom, unsigned int flags)
{
1266
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1267 1268
    char *name = NULL;
    virDomainDefPtr def = NULL;
1269 1270 1271 1272
    int ret = -1;

    virCheckFlags(0, -1);

1273 1274 1275
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1276 1277 1278
    if (virDomainManagedSaveRemoveEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1279 1280
    if (!(name = xenUnifiedDomainManagedSavePath(priv, def)))
        goto cleanup;
1281 1282

    ret = unlink(name);
1283

1284
 cleanup:
1285 1286 1287 1288
    VIR_FREE(name);
    return ret;
}

1289 1290 1291
static int
xenUnifiedDomainRestoreFlags(virConnectPtr conn, const char *from,
                             const char *dxml, unsigned int flags)
1292
{
1293 1294
    virCheckFlags(0, -1);
    if (dxml) {
1295 1296
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
1297 1298 1299
        return -1;
    }

1300
    return xenDaemonDomainRestore(conn, from);
1301 1302
}

1303
static int
1304
xenUnifiedDomainRestore(virConnectPtr conn, const char *from)
1305 1306 1307 1308
{
    return xenUnifiedDomainRestoreFlags(conn, from, NULL, 0);
}

1309
static int
1310
xenUnifiedDomainCoreDump(virDomainPtr dom, const char *to, unsigned int flags)
1311
{
1312 1313 1314 1315 1316 1317 1318 1319
    virDomainDefPtr def = NULL;
    int ret = -1;

    virCheckFlags(0, -1);

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

1320 1321 1322
    if (virDomainCoreDumpEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1323 1324
    ret = xenDaemonDomainCoreDump(dom->conn, def, to, flags);

1325
 cleanup:
1326 1327
    virDomainDefFree(def);
    return ret;
1328 1329 1330
}

static int
1331 1332
xenUnifiedDomainSetVcpusFlags(virDomainPtr dom, unsigned int nvcpus,
                              unsigned int flags)
1333
{
1334 1335
    virDomainDefPtr def = NULL;
    int ret = -1;
1336

1337 1338 1339 1340 1341 1342 1343 1344 1345
    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)) {
1346 1347
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid flag combination: (0x%x)"), flags);
1348 1349 1350
        return -1;
    }
    if (!nvcpus || (unsigned short) nvcpus != nvcpus) {
1351 1352
        virReportError(VIR_ERR_INVALID_ARG,
                       _("argument out of range: %d"), nvcpus);
1353 1354 1355
        return -1;
    }

1356 1357 1358
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1359 1360 1361
    if (virDomainSetVcpusFlagsEnsureACL(dom->conn, def, flags) < 0)
        goto cleanup;

1362
    ret = xenDaemonDomainSetVcpusFlags(dom->conn, def, nvcpus, flags);
1363

1364
 cleanup:
1365 1366
    virDomainDefFree(def);
    return ret;
1367 1368
}

1369
static int
1370
xenUnifiedDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus)
1371
{
1372
    unsigned int flags;
1373 1374

    /* Per the documented API, it is hypervisor-dependent whether this
1375 1376 1377
     * affects just _LIVE or _LIVE|_CONFIG; in xen's case, both are
     * affected. */
    flags = VIR_DOMAIN_VCPU_LIVE | VIR_DOMAIN_VCPU_CONFIG;
1378 1379

    return xenUnifiedDomainSetVcpusFlags(dom, nvcpus, flags);
1380 1381
}

1382
static int
1383 1384
xenUnifiedDomainPinVcpu(virDomainPtr dom, unsigned int vcpu,
                        unsigned char *cpumap, int maplen)
1385
{
1386 1387 1388 1389 1390
    virDomainDefPtr def = NULL;
    int ret = -1;

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

1392 1393 1394
    if (virDomainPinVcpuEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1395 1396 1397
    if (dom->id < 0)
        ret = xenDaemonDomainPinVcpu(dom->conn, def, vcpu, cpumap, maplen);
    else
1398 1399
        ret = xenHypervisorPinVcpu(dom->conn, def, vcpu, cpumap, maplen);

1400
 cleanup:
1401 1402
    virDomainDefFree(def);
    return ret;
1403 1404 1405
}

static int
1406 1407 1408 1409 1410 1411
xenUnifiedDomainGetVcpusInternal(virDomainPtr dom,
                                 virDomainDefPtr def,
                                 virVcpuInfoPtr info,
                                 int maxinfo,
                                 unsigned char *cpumaps,
                                 int maplen)
1412
{
1413 1414
    int ret = -1;

1415
    if (dom->id < 0) {
1416 1417
        ret = xenDaemonDomainGetVcpus(dom->conn, def, info, maxinfo,
                                      cpumaps, maplen);
1418
    } else {
1419 1420
        ret = xenHypervisorGetVcpus(dom->conn, def, info, maxinfo, cpumaps,
                                    maplen);
1421
    }
1422 1423

    return ret;
1424 1425 1426
}

static int
1427 1428 1429
xenUnifiedDomainGetVcpus(virDomainPtr dom,
                         virVcpuInfoPtr info, int maxinfo,
                         unsigned char *cpumaps, int maplen)
1430
{
1431 1432
    virDomainDefPtr def = NULL;
    int ret = -1;
1433

1434 1435 1436
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1437
    if (virDomainGetVcpusEnsureACL(dom->conn, def) < 0)
1438 1439
        goto cleanup;

1440 1441 1442
    ret = xenUnifiedDomainGetVcpusInternal(dom, def, info, maxinfo, cpumaps,
                                           maplen);

1443
 cleanup:
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
    virDomainDefFree(def);
    return ret;
}

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

1455
    if (dom->id < 0) {
1456
        ret = xenDaemonDomainGetVcpusFlags(dom->conn, def, flags);
1457 1458
    } else {
        if (flags == (VIR_DOMAIN_VCPU_CONFIG | VIR_DOMAIN_VCPU_MAXIMUM))
1459
            ret = xenHypervisorGetVcpuMax(dom->conn, def);
1460
        else
1461
            ret = xenDaemonDomainGetVcpusFlags(dom->conn, def, flags);
1462
    }
1463

1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
   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;

1480
    if (virDomainGetVcpusFlagsEnsureACL(dom->conn, def, flags) < 0)
1481 1482 1483 1484
        goto cleanup;

    ret = xenUnifiedDomainGetVcpusFlagsInternal(dom, def, flags);

1485
 cleanup:
1486 1487
    virDomainDefFree(def);
    return ret;
1488 1489
}

1490
static int
1491
xenUnifiedDomainGetMaxVcpus(virDomainPtr dom)
1492 1493 1494 1495 1496
{
    return xenUnifiedDomainGetVcpusFlags(dom, (VIR_DOMAIN_VCPU_LIVE |
                                               VIR_DOMAIN_VCPU_MAXIMUM));
}

1497
static char *
1498
xenUnifiedDomainGetXMLDesc(virDomainPtr dom, unsigned int flags)
1499
{
1500
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1501 1502 1503
    virDomainDefPtr minidef = NULL;
    virDomainDefPtr def = NULL;
    char *ret = NULL;
1504
    char *cpus = NULL;
1505 1506 1507

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

1509 1510 1511
    if (virDomainGetXMLDescEnsureACL(dom->conn, minidef, flags) < 0)
        goto cleanup;

1512 1513 1514 1515
    xenUnifiedLock(priv);
    cpus = xenDomainUsedCpus(dom, minidef);
    xenUnifiedUnlock(priv);
    def = xenDaemonDomainGetXMLDesc(dom->conn, minidef, cpus);
1516 1517

    if (def)
1518
        ret = virDomainDefFormat(def, priv->caps,
1519
                                 virDomainDefFormatConvertXMLFlags(flags));
1520

1521
 cleanup:
1522
    VIR_FREE(cpus);
1523 1524 1525
    virDomainDefFree(def);
    virDomainDefFree(minidef);
    return ret;
1526 1527
}

1528 1529

static char *
1530 1531 1532 1533
xenUnifiedConnectDomainXMLFromNative(virConnectPtr conn,
                                     const char *format,
                                     const char *config,
                                     unsigned int flags)
1534 1535 1536 1537
{
    virDomainDefPtr def = NULL;
    char *ret = NULL;
    virConfPtr conf = NULL;
1538 1539 1540
    int id;
    char * tty;
    int vncport;
1541
    xenUnifiedPrivatePtr priv = conn->privateData;
1542

E
Eric Blake 已提交
1543 1544
    virCheckFlags(0, NULL);

1545 1546 1547
    if (virConnectDomainXMLFromNativeEnsureACL(conn) < 0)
        return NULL;

1548 1549
    if (STRNEQ(format, XEN_CONFIG_FORMAT_XM) &&
        STRNEQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
1550 1551
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unsupported config type %s"), format);
1552 1553 1554 1555
        return NULL;
    }

    if (STREQ(format, XEN_CONFIG_FORMAT_XM)) {
J
Ján Tomko 已提交
1556
        conf = virConfReadString(config, 0);
1557 1558 1559
        if (!conf)
            goto cleanup;

1560
        def = xenParseXM(conf, priv->caps, priv->xmlopt);
1561
    } else if (STREQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
1562
        if (xenGetDomIdFromSxprString(config, &id) < 0)
1563
            goto cleanup;
1564 1565 1566 1567
        xenUnifiedLock(priv);
        tty = xenStoreDomainGetConsolePath(conn, id);
        vncport = xenStoreDomainGetVNCPort(conn, id);
        xenUnifiedUnlock(priv);
1568
        def = xenParseSxprString(config, tty,
1569
                                 vncport, priv->caps, priv->xmlopt);
1570 1571 1572 1573
    }
    if (!def)
        goto cleanup;

1574
    ret = virDomainDefFormat(def, priv->caps, 0);
1575

1576
 cleanup:
1577
    virDomainDefFree(def);
1578 1579
    if (conf)
        virConfFree(conf);
1580 1581 1582 1583 1584 1585
    return ret;
}


#define MAX_CONFIG_SIZE (1024 * 65)
static char *
1586 1587 1588 1589
xenUnifiedConnectDomainXMLToNative(virConnectPtr conn,
                                   const char *format,
                                   const char *xmlData,
                                   unsigned int flags)
1590 1591 1592 1593
{
    virDomainDefPtr def = NULL;
    char *ret = NULL;
    virConfPtr conf = NULL;
1594
    xenUnifiedPrivatePtr priv = conn->privateData;
1595

E
Eric Blake 已提交
1596 1597
    virCheckFlags(0, NULL);

1598 1599 1600
    if (virConnectDomainXMLToNativeEnsureACL(conn) < 0)
        return NULL;

1601 1602
    if (STRNEQ(format, XEN_CONFIG_FORMAT_XM) &&
        STRNEQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
1603 1604
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unsupported config type %s"), format);
1605 1606 1607
        goto cleanup;
    }

1608
    if (!(def = virDomainDefParseString(xmlData, priv->caps, priv->xmlopt,
1609
                                        NULL,
1610
                                        VIR_DOMAIN_DEF_PARSE_INACTIVE)))
1611 1612 1613 1614
        goto cleanup;

    if (STREQ(format, XEN_CONFIG_FORMAT_XM)) {
        int len = MAX_CONFIG_SIZE;
1615
        conf = xenFormatXM(conn, def);
1616 1617 1618
        if (!conf)
            goto cleanup;

1619
        if (VIR_ALLOC_N(ret, len) < 0)
1620 1621 1622 1623 1624 1625 1626
            goto cleanup;

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

1630
 cleanup:
1631 1632 1633 1634 1635 1636 1637
    virDomainDefFree(def);
    if (conf)
        virConfFree(conf);
    return ret;
}


1638
static int
1639 1640 1641 1642 1643 1644 1645 1646
xenUnifiedDomainMigratePrepare(virConnectPtr dconn,
                               char **cookie,
                               int *cookielen,
                               const char *uri_in,
                               char **uri_out,
                               unsigned long flags,
                               const char *dname,
                               unsigned long resource)
1647
{
E
Eric Blake 已提交
1648 1649
    virCheckFlags(XEN_MIGRATION_FLAGS, -1);

1650 1651 1652
    return xenDaemonDomainMigratePrepare(dconn, cookie, cookielen,
                                         uri_in, uri_out,
                                         flags, dname, resource);
1653 1654 1655
}

static int
1656 1657 1658 1659 1660 1661 1662
xenUnifiedDomainMigratePerform(virDomainPtr dom,
                               const char *cookie,
                               int cookielen,
                               const char *uri,
                               unsigned long flags,
                               const char *dname,
                               unsigned long resource)
1663
{
1664 1665 1666
    virDomainDefPtr def = NULL;
    int ret = -1;

E
Eric Blake 已提交
1667 1668
    virCheckFlags(XEN_MIGRATION_FLAGS, -1);

1669 1670 1671
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1672 1673 1674
    if (virDomainMigratePerformEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1675 1676 1677 1678
    ret = xenDaemonDomainMigratePerform(dom->conn, def,
                                        cookie, cookielen, uri,
                                        flags, dname, resource);

1679
 cleanup:
1680 1681
    virDomainDefFree(def);
    return ret;
1682 1683 1684
}

static virDomainPtr
1685 1686 1687 1688 1689 1690
xenUnifiedDomainMigrateFinish(virConnectPtr dconn,
                              const char *dname,
                              const char *cookie ATTRIBUTE_UNUSED,
                              int cookielen ATTRIBUTE_UNUSED,
                              const char *uri ATTRIBUTE_UNUSED,
                              unsigned long flags)
1691
{
1692 1693 1694
    virDomainPtr ret = NULL;
    virDomainDefPtr minidef = NULL;
    virDomainDefPtr def = NULL;
1695

E
Eric Blake 已提交
1696 1697
    virCheckFlags(XEN_MIGRATION_FLAGS, NULL);

1698 1699
    if (!(minidef = xenGetDomainDefForName(dconn, dname)))
        goto cleanup;
1700

1701 1702 1703
    if (virDomainMigrateFinishEnsureACL(dconn, minidef) < 0)
        goto cleanup;

1704
    if (flags & VIR_MIGRATE_PERSIST_DEST) {
1705 1706
        if (!(def = xenDaemonDomainGetXMLDesc(dconn, minidef, NULL)))
            goto cleanup;
1707

1708 1709
        if (xenDaemonDomainDefineXML(dconn, def) < 0)
            goto cleanup;
1710 1711
    }

1712
    ret = virGetDomain(dconn, minidef->name, minidef->uuid, minidef->id);
1713

1714
 cleanup:
1715 1716 1717
    virDomainDefFree(def);
    virDomainDefFree(minidef);
    return ret;
1718 1719
}

1720
static int
1721 1722
xenUnifiedConnectListDefinedDomains(virConnectPtr conn, char **const names,
                                    int maxnames)
1723
{
1724 1725 1726
    if (virConnectListDefinedDomainsEnsureACL(conn) < 0)
        return -1;

1727
    return xenDaemonListDefinedDomains(conn, names, maxnames);
1728 1729 1730
}

static int
1731
xenUnifiedConnectNumOfDefinedDomains(virConnectPtr conn)
1732
{
1733 1734 1735
    if (virConnectNumOfDefinedDomainsEnsureACL(conn) < 0)
        return -1;

1736
    return xenDaemonNumOfDefinedDomains(conn);
1737 1738 1739
}

static int
1740
xenUnifiedDomainCreateWithFlags(virDomainPtr dom, unsigned int flags)
1741
{
1742
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1743
    int ret = -1;
1744
    virDomainDefPtr def = NULL;
1745
    char *name = NULL;
1746

1747 1748
    virCheckFlags(0, -1);

1749 1750 1751
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1752 1753 1754
    if (virDomainCreateWithFlagsEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1755
    if (!(name = xenUnifiedDomainManagedSavePath(priv, def)))
1756 1757 1758
        goto cleanup;

    if (virFileExists(name)) {
1759 1760 1761
        ret = xenDaemonDomainRestore(dom->conn, name);
        if (ret == 0)
            unlink(name);
1762 1763 1764
        goto cleanup;
    }

1765
    ret = xenDaemonDomainCreate(dom->conn, def);
1766 1767 1768

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

1770
 cleanup:
1771
    virDomainDefFree(def);
1772 1773
    VIR_FREE(name);
    return ret;
1774 1775
}

1776
static int
1777
xenUnifiedDomainCreate(virDomainPtr dom)
1778 1779 1780 1781
{
    return xenUnifiedDomainCreateWithFlags(dom, 0);
}

1782
static virDomainPtr
1783
xenUnifiedDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags)
1784
{
1785
    xenUnifiedPrivatePtr priv = conn->privateData;
1786 1787
    virDomainDefPtr def = NULL;
    virDomainPtr ret = NULL;
1788
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;
1789

1790 1791 1792
    virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL);

    if (flags & VIR_DOMAIN_DEFINE_VALIDATE)
1793
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA;
1794

1795
    if (!(def = virDomainDefParseString(xml, priv->caps, priv->xmlopt,
1796
                                        NULL, parse_flags)))
1797 1798
        goto cleanup;

1799 1800 1801
    if (virXMLCheckIllegalChars("name", def->name, "\n") < 0)
        goto cleanup;

1802
    if (virDomainDefineXMLFlagsEnsureACL(conn, def) < 0)
1803 1804
        goto cleanup;

1805 1806
    if (xenDaemonDomainDefineXML(conn, def) < 0)
        goto cleanup;
1807
    ret = virGetDomain(conn, def->name, def->uuid, -1);
1808

1809
 cleanup:
1810 1811
    virDomainDefFree(def);
    return ret;
1812 1813
}

1814 1815 1816 1817 1818 1819
static virDomainPtr
xenUnifiedDomainDefineXML(virConnectPtr conn, const char *xml)
{
    return xenUnifiedDomainDefineXMLFlags(conn, xml, 0);
}

1820
static int
1821
xenUnifiedDomainUndefineFlags(virDomainPtr dom, unsigned int flags)
1822
{
1823 1824
    virDomainDefPtr def = NULL;
    int ret = -1;
1825

1826
    virCheckFlags(0, -1);
1827 1828 1829 1830

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

1831 1832 1833
    if (virDomainUndefineFlagsEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1834
    ret = xenDaemonDomainUndefine(dom->conn, def);
1835

1836
 cleanup:
1837 1838
    virDomainDefFree(def);
    return ret;
1839 1840
}

1841
static int
1842 1843
xenUnifiedDomainUndefine(virDomainPtr dom)
{
1844 1845 1846
    return xenUnifiedDomainUndefineFlags(dom, 0);
}

1847
static int
1848
xenUnifiedDomainAttachDevice(virDomainPtr dom, const char *xml)
1849
{
1850
    unsigned int flags = VIR_DOMAIN_DEVICE_MODIFY_LIVE;
1851 1852
    virDomainDefPtr def = NULL;
    int ret = -1;
1853

1854
    /*
1855 1856
     * HACK: xend does not support changing live config without also touching
     * persistent config. We add the extra flag here to make this API work
1857
     */
1858
    flags |= VIR_DOMAIN_DEVICE_MODIFY_CONFIG;
1859

1860 1861 1862
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1863 1864 1865
    if (virDomainAttachDeviceEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1866
    ret = xenDaemonAttachDeviceFlags(dom->conn, def, xml, flags);
1867

1868
 cleanup:
1869 1870
    virDomainDefFree(def);
    return ret;
1871 1872 1873
}

static int
1874 1875
xenUnifiedDomainAttachDeviceFlags(virDomainPtr dom, const char *xml,
                                  unsigned int flags)
1876
{
1877 1878 1879 1880 1881
    virDomainDefPtr def = NULL;
    int ret = -1;

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

1883 1884 1885
    if (virDomainAttachDeviceFlagsEnsureACL(dom->conn, def, flags) < 0)
        goto cleanup;

1886
    ret = xenDaemonAttachDeviceFlags(dom->conn, def, xml, flags);
1887

1888
 cleanup:
1889 1890
    virDomainDefFree(def);
    return ret;
1891 1892 1893
}

static int
1894
xenUnifiedDomainDetachDevice(virDomainPtr dom, const char *xml)
1895
{
1896
    unsigned int flags = VIR_DOMAIN_DEVICE_MODIFY_LIVE;
1897 1898
    virDomainDefPtr def = NULL;
    int ret = -1;
1899

1900
    /*
1901 1902
     * HACK: xend does not support changing live config without also touching
     * persistent config. We add the extra flag here to make this API work
1903
     */
1904
    flags |= VIR_DOMAIN_DEVICE_MODIFY_CONFIG;
1905

1906 1907 1908
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

1909 1910 1911
    if (virDomainDetachDeviceEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1912
    ret = xenDaemonDetachDeviceFlags(dom->conn, def, xml, flags);
1913

1914
 cleanup:
1915 1916
    virDomainDefFree(def);
    return ret;
1917 1918 1919
}

static int
1920 1921
xenUnifiedDomainDetachDeviceFlags(virDomainPtr dom, const char *xml,
                                  unsigned int flags)
1922
{
1923 1924 1925 1926 1927
    virDomainDefPtr def = NULL;
    int ret = -1;

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

1929 1930 1931
    if (virDomainDetachDeviceFlagsEnsureACL(dom->conn, def, flags) < 0)
        goto cleanup;

1932
    ret = xenDaemonDetachDeviceFlags(dom->conn, def, xml, flags);
1933

1934
 cleanup:
1935 1936
    virDomainDefFree(def);
    return ret;
1937 1938
}

1939
static int
1940 1941
xenUnifiedDomainUpdateDeviceFlags(virDomainPtr dom, const char *xml,
                                  unsigned int flags)
1942
{
1943 1944 1945 1946 1947 1948
    virDomainDefPtr def = NULL;
    int ret = -1;

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

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

1952 1953
    ret = xenDaemonUpdateDeviceFlags(dom->conn, def, xml, flags);

1954
 cleanup:
1955 1956
    virDomainDefFree(def);
    return ret;
1957 1958
}

1959
static int
1960
xenUnifiedDomainGetAutostart(virDomainPtr dom, int *autostart)
1961
{
1962 1963 1964 1965 1966
    virDomainDefPtr def = NULL;
    int ret = -1;

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

1968 1969 1970
    if (virDomainGetAutostartEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1971
    ret = xenDaemonDomainGetAutostart(dom->conn, def, autostart);
1972

1973
 cleanup:
1974 1975
    virDomainDefFree(def);
    return ret;
1976 1977 1978
}

static int
1979
xenUnifiedDomainSetAutostart(virDomainPtr dom, int autostart)
1980
{
1981 1982 1983 1984 1985
    virDomainDefPtr def = NULL;
    int ret = -1;

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

1987 1988 1989
    if (virDomainSetAutostartEnsureACL(dom->conn, def) < 0)
        goto cleanup;

1990
    ret = xenDaemonDomainSetAutostart(dom->conn, def, autostart);
1991

1992
 cleanup:
1993 1994
    virDomainDefFree(def);
    return ret;
1995 1996
}

1997
static char *
1998
xenUnifiedDomainGetSchedulerType(virDomainPtr dom, int *nparams)
1999
{
2000 2001 2002 2003 2004
    virDomainDefPtr def = NULL;
    char *ret = NULL;

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

2006 2007 2008
    if (virDomainGetSchedulerTypeEnsureACL(dom->conn, def) < 0)
        goto cleanup;

2009
    if (dom->id < 0)
2010
        ret = xenDaemonGetSchedulerType(dom->conn, nparams);
2011
    else
2012 2013
        ret = xenHypervisorGetSchedulerType(dom->conn, nparams);

2014
 cleanup:
2015 2016
    virDomainDefFree(def);
    return ret;
2017 2018 2019
}

static int
2020 2021 2022 2023
xenUnifiedDomainGetSchedulerParametersFlags(virDomainPtr dom,
                                            virTypedParameterPtr params,
                                            int *nparams,
                                            unsigned int flags)
2024
{
2025 2026
    virDomainDefPtr def = NULL;
    int ret = -1;
2027

2028 2029
    virCheckFlags(0, -1);

2030 2031 2032
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

2033 2034 2035
    if (virDomainGetSchedulerParametersFlagsEnsureACL(dom->conn, def) < 0)
        goto cleanup;

2036
    if (dom->id < 0)
2037
        ret = xenDaemonGetSchedulerParameters(dom->conn, def, params, nparams);
2038
    else
2039 2040
        ret = xenHypervisorGetSchedulerParameters(dom->conn, def, params, nparams);

2041
 cleanup:
2042 2043
    virDomainDefFree(def);
    return ret;
2044 2045 2046
}

static int
2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059
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)
2060
{
2061 2062
    virDomainDefPtr def = NULL;
    int ret = -1;
2063

2064 2065
    virCheckFlags(0, -1);

2066 2067 2068
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

2069 2070 2071
    if (virDomainSetSchedulerParametersFlagsEnsureACL(dom->conn, def, flags) < 0)
        goto cleanup;

2072
    if (dom->id < 0)
2073
        ret = xenDaemonSetSchedulerParameters(dom->conn, def, params, nparams);
2074
    else
2075 2076
        ret = xenHypervisorSetSchedulerParameters(dom->conn, def, params, nparams);

2077
 cleanup:
2078 2079
    virDomainDefFree(def);
    return ret;
2080 2081
}

2082 2083 2084 2085 2086 2087 2088 2089 2090
static int
xenUnifiedDomainSetSchedulerParameters(virDomainPtr dom,
                                       virTypedParameterPtr params,
                                       int nparams)
{
    return xenUnifiedDomainSetSchedulerParametersFlags(dom, params,
                                                       nparams, 0);
}

2091
static int
2092
xenUnifiedDomainBlockStats(virDomainPtr dom, const char *path,
2093
                           virDomainBlockStatsPtr stats)
2094
{
2095 2096 2097 2098 2099 2100
    virDomainDefPtr def = NULL;
    int ret = -1;

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

2101 2102 2103
    if (virDomainBlockStatsEnsureACL(dom->conn, def) < 0)
        goto cleanup;

2104 2105
    ret = xenHypervisorDomainBlockStats(dom->conn, def, path, stats);

2106
 cleanup:
2107 2108
    virDomainDefFree(def);
    return ret;
2109 2110 2111
}

static int
2112
xenUnifiedDomainInterfaceStats(virDomainPtr dom, const char *path,
2113
                               virDomainInterfaceStatsPtr stats)
2114
{
2115
    virDomainDefPtr def = NULL;
2116
    virDomainNetDefPtr net = NULL;
2117 2118 2119 2120 2121
    int ret = -1;

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

2122 2123 2124
    if (virDomainInterfaceStatsEnsureACL(dom->conn, def) < 0)
        goto cleanup;

2125 2126 2127 2128
    if (!(net = virDomainNetFind(def, path)))
        goto cleanup;

    ret = xenHypervisorDomainInterfaceStats(def, net->ifname, stats);
2129

2130
 cleanup:
2131 2132
    virDomainDefFree(def);
    return ret;
2133 2134
}

R
Richard W.M. Jones 已提交
2135
static int
2136 2137 2138
xenUnifiedDomainBlockPeek(virDomainPtr dom, const char *path,
                          unsigned long long offset, size_t size,
                          void *buffer, unsigned int flags)
R
Richard W.M. Jones 已提交
2139
{
2140 2141
    virDomainDefPtr def = NULL;
    int ret = -1;
R
Richard W.M. Jones 已提交
2142

E
Eric Blake 已提交
2143 2144
    virCheckFlags(0, -1);

2145 2146 2147
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

2148 2149 2150
    if (virDomainBlockPeekEnsureACL(dom->conn, def) < 0)
        goto cleanup;

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

2153
 cleanup:
2154 2155
    virDomainDefFree(def);
    return ret;
R
Richard W.M. Jones 已提交
2156 2157
}

2158
static int
2159 2160
xenUnifiedNodeGetCellsFreeMemory(virConnectPtr conn, unsigned long long *freeMems,
                                 int startCell, int maxCells)
2161
{
2162 2163 2164
    if (virNodeGetCellsFreeMemoryEnsureACL(conn) < 0)
        return 0;

2165 2166
    return xenHypervisorNodeGetCellsFreeMemory(conn, freeMems,
                                               startCell, maxCells);
2167 2168
}

2169
static unsigned long long
2170
xenUnifiedNodeGetFreeMemory(virConnectPtr conn)
2171 2172 2173
{
    unsigned long long freeMem = 0;

2174 2175 2176
    if (virNodeGetFreeMemoryEnsureACL(conn) < 0)
        return 0;

2177 2178 2179
    if (xenHypervisorNodeGetCellsFreeMemory(conn, &freeMem, -1, 1) < 0)
        return 0;
    return freeMem;
2180 2181
}

2182

2183
static int
2184 2185 2186 2187
xenUnifiedConnectDomainEventRegister(virConnectPtr conn,
                                     virConnectDomainEventCallback callback,
                                     void *opaque,
                                     virFreeCallback freefunc)
2188
{
2189
    xenUnifiedPrivatePtr priv = conn->privateData;
2190
    int ret = 0;
2191 2192 2193 2194

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

D
Daniel P. Berrange 已提交
2195
    xenUnifiedLock(priv);
2196

2197
    if (priv->xsWatch == -1) {
2198
        virReportUnsupportedError();
D
Daniel P. Berrange 已提交
2199
        xenUnifiedUnlock(priv);
2200 2201 2202
        return -1;
    }

2203 2204 2205
    if (virDomainEventStateRegister(conn, priv->domainEvents,
                                    callback, opaque, freefunc) < 0)
        ret = -1;
2206

D
Daniel P. Berrange 已提交
2207
    xenUnifiedUnlock(priv);
2208
    return ret;
2209 2210
}

2211

2212
static int
2213 2214
xenUnifiedConnectDomainEventDeregister(virConnectPtr conn,
                                       virConnectDomainEventCallback callback)
2215
{
2216
    int ret = 0;
2217
    xenUnifiedPrivatePtr priv = conn->privateData;
2218 2219 2220 2221

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

D
Daniel P. Berrange 已提交
2222 2223
    xenUnifiedLock(priv);

2224
    if (priv->xsWatch == -1) {
2225
        virReportUnsupportedError();
D
Daniel P. Berrange 已提交
2226
        xenUnifiedUnlock(priv);
2227 2228 2229
        return -1;
    }

2230 2231 2232 2233
    if (virDomainEventStateDeregister(conn,
                                      priv->domainEvents,
                                      callback) < 0)
        ret = -1;
2234

D
Daniel P. Berrange 已提交
2235
    xenUnifiedUnlock(priv);
2236 2237 2238
    return ret;
}

2239

2240
static int
2241 2242 2243 2244 2245 2246
xenUnifiedConnectDomainEventRegisterAny(virConnectPtr conn,
                                        virDomainPtr dom,
                                        int eventID,
                                        virConnectDomainEventGenericCallback callback,
                                        void *opaque,
                                        virFreeCallback freefunc)
2247
{
2248
    xenUnifiedPrivatePtr priv = conn->privateData;
2249
    int ret;
2250 2251 2252 2253

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

2254 2255 2256
    xenUnifiedLock(priv);

    if (priv->xsWatch == -1) {
2257
        virReportUnsupportedError();
2258 2259 2260 2261
        xenUnifiedUnlock(priv);
        return -1;
    }

2262 2263 2264
    if (virDomainEventStateRegisterID(conn, priv->domainEvents,
                                      dom, eventID,
                                      callback, opaque, freefunc, &ret) < 0)
2265
        ret = -1;
2266 2267

    xenUnifiedUnlock(priv);
2268
    return ret;
2269 2270 2271
}

static int
2272 2273
xenUnifiedConnectDomainEventDeregisterAny(virConnectPtr conn,
                                          int callbackID)
2274
{
2275
    int ret = 0;
2276
    xenUnifiedPrivatePtr priv = conn->privateData;
2277 2278 2279 2280

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

2281 2282 2283
    xenUnifiedLock(priv);

    if (priv->xsWatch == -1) {
2284
        virReportUnsupportedError();
2285 2286 2287 2288
        xenUnifiedUnlock(priv);
        return -1;
    }

2289 2290
    if (virObjectEventStateDeregisterID(conn,
                                        priv->domainEvents,
2291
                                        callbackID, true) < 0)
2292
        ret = -1;
2293 2294 2295 2296 2297 2298

    xenUnifiedUnlock(priv);
    return ret;
}


2299
static int
2300
xenUnifiedNodeDeviceGetPCIInfo(virNodeDevicePtr dev,
2301 2302 2303 2304
                               unsigned *domain,
                               unsigned *bus,
                               unsigned *slot,
                               unsigned *function)
2305 2306 2307 2308 2309 2310 2311 2312 2313 2314
{
    virNodeDeviceDefPtr def = NULL;
    virNodeDevCapsDefPtr cap;
    char *xml = NULL;
    int ret = -1;

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

2315
    def = virNodeDeviceDefParseString(xml, EXISTING_DEVICE, NULL);
2316 2317 2318 2319 2320
    if (!def)
        goto out;

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

    ret = 0;
2339
 out:
2340 2341 2342 2343 2344 2345
    virNodeDeviceDefFree(def);
    VIR_FREE(xml);
    return ret;
}

static int
2346 2347 2348
xenUnifiedNodeDeviceDetachFlags(virNodeDevicePtr dev,
                                const char *driverName,
                                unsigned int flags)
2349
{
2350
    virPCIDevicePtr pci;
2351 2352 2353
    unsigned domain, bus, slot, function;
    int ret = -1;

2354 2355
    virCheckFlags(0, -1);

2356
    if (xenUnifiedNodeDeviceGetPCIInfo(dev, &domain, &bus, &slot, &function) < 0)
2357 2358
        return -1;

2359
    pci = virPCIDeviceNew(domain, bus, slot, function);
2360 2361 2362
    if (!pci)
        return -1;

2363
    if (!driverName) {
2364
        virPCIDeviceSetStubDriver(pci, VIR_PCI_STUB_DRIVER_XEN);
2365 2366 2367 2368 2369 2370
    } else {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unknown driver name '%s'"), driverName);
        goto out;
    }

2371
    if (virPCIDeviceDetach(pci, NULL, NULL) < 0)
2372 2373 2374
        goto out;

    ret = 0;
2375
 out:
2376
    virPCIDeviceFree(pci);
2377 2378 2379
    return ret;
}

2380 2381 2382 2383 2384 2385
static int
xenUnifiedNodeDeviceDettach(virNodeDevicePtr dev)
{
    return xenUnifiedNodeDeviceDetachFlags(dev, NULL, 0);
}

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

    /* Get pci bdf */
2411
    if (xenUnifiedNodeDeviceGetPCIInfo(dev, &domain, &bus, &slot, &function) < 0)
2412 2413 2414
        goto out;

    if (virAsprintf(&bdf, "%04x:%02x:%02x.%0x",
2415
                    domain, bus, slot, function) < 0)
2416 2417 2418 2419
        goto out;

    xenUnifiedLock(priv);
    /* Check if bdf is assigned to one of active domains */
2420
    for (i = 0; i < numdomains; i++) {
2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432
        xref = xenStoreDomainGetPCIID(conn, ids[i], bdf);
        if (xref == NULL) {
            continue;
        } else {
            ret = ids[i];
            break;
        }
    }
    xenUnifiedUnlock(priv);

    VIR_FREE(xref);
    VIR_FREE(bdf);
2433
 out:
2434 2435 2436 2437 2438
    VIR_FREE(ids);

    return ret;
}

2439
static int
2440
xenUnifiedNodeDeviceReAttach(virNodeDevicePtr dev)
2441
{
2442
    virPCIDevicePtr pci;
2443 2444
    unsigned domain, bus, slot, function;
    int ret = -1;
2445
    int domid;
2446

2447
    if (xenUnifiedNodeDeviceGetPCIInfo(dev, &domain, &bus, &slot, &function) < 0)
2448 2449
        return -1;

2450
    pci = virPCIDeviceNew(domain, bus, slot, function);
2451 2452 2453
    if (!pci)
        return -1;

2454 2455
    /* Check if device is assigned to an active guest */
    if ((domid = xenUnifiedNodeDeviceAssignedDomainId(dev)) >= 0) {
2456 2457 2458
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Device %s has been assigned to guest %d"),
                       dev->name, domid);
2459 2460 2461
        goto out;
    }

2462
    if (virPCIDeviceReattach(pci, NULL, NULL) < 0)
2463 2464 2465
        goto out;

    ret = 0;
2466
 out:
2467
    virPCIDeviceFree(pci);
2468 2469 2470 2471
    return ret;
}

static int
2472
xenUnifiedNodeDeviceReset(virNodeDevicePtr dev)
2473
{
2474
    virPCIDevicePtr pci;
2475 2476 2477
    unsigned domain, bus, slot, function;
    int ret = -1;

2478
    if (xenUnifiedNodeDeviceGetPCIInfo(dev, &domain, &bus, &slot, &function) < 0)
2479 2480
        return -1;

2481
    pci = virPCIDeviceNew(domain, bus, slot, function);
2482 2483 2484
    if (!pci)
        return -1;

2485
    if (virPCIDeviceReset(pci, NULL, NULL) < 0)
2486 2487 2488
        goto out;

    ret = 0;
2489
 out:
2490
    virPCIDeviceFree(pci);
2491 2492 2493 2494
    return ret;
}


2495 2496
static int
xenUnifiedDomainOpenConsole(virDomainPtr dom,
2497
                            const char *dev_name,
2498 2499 2500 2501 2502 2503 2504 2505 2506 2507
                            virStreamPtr st,
                            unsigned int flags)
{
    virDomainDefPtr def = NULL;
    int ret = -1;
    virDomainChrDefPtr chr = NULL;

    virCheckFlags(0, -1);

    if (dom->id == -1) {
2508 2509
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("domain is not running"));
2510 2511 2512
        goto cleanup;
    }

2513
    if (dev_name) {
2514
        /* XXX support device aliases in future */
2515 2516
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Named device aliases are not supported"));
2517 2518 2519 2520 2521 2522 2523
        goto cleanup;
    }

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

2524 2525
    if (def->nconsoles)
        chr = def->consoles[0];
2526 2527 2528 2529
    else if (def->nserials)
        chr = def->serials[0];

    if (!chr) {
2530 2531
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot find default console device"));
2532 2533 2534
        goto cleanup;
    }

2535
    if (chr->source->type != VIR_DOMAIN_CHR_TYPE_PTY) {
2536
        virReportError(VIR_ERR_INTERNAL_ERROR,
2537 2538
                       _("character device %s is not using a PTY"),
                       dev_name ? dev_name : NULLSTR(chr->info.alias));
2539 2540 2541
        goto cleanup;
    }

2542
    if (virFDStreamOpenFile(st, chr->source->data.file.path,
E
Eric Blake 已提交
2543
                            0, 0, O_RDWR) < 0)
2544 2545 2546
        goto cleanup;

    ret = 0;
2547
 cleanup:
2548 2549 2550
    virDomainDefFree(def);
    return ret;
}
2551 2552

static int
2553
xenUnifiedNodeGetMemoryParameters(virConnectPtr conn,
2554 2555 2556 2557
                                  virTypedParameterPtr params,
                                  int *nparams,
                                  unsigned int flags)
{
2558 2559 2560
    if (virNodeGetMemoryParametersEnsureACL(conn) < 0)
        return -1;

2561
    return virHostMemGetParameters(params, nparams, flags);
2562 2563 2564 2565
}


static int
2566
xenUnifiedNodeSetMemoryParameters(virConnectPtr conn,
2567 2568 2569 2570
                                  virTypedParameterPtr params,
                                  int nparams,
                                  unsigned int flags)
{
2571 2572 2573
    if (virNodeSetMemoryParametersEnsureACL(conn) < 0)
        return -1;

2574
    return virHostMemSetParameters(params, nparams, flags);
2575 2576
}

2577 2578

static int
2579
xenUnifiedNodeSuspendForDuration(virConnectPtr conn,
2580 2581 2582 2583
                                 unsigned int target,
                                 unsigned long long duration,
                                 unsigned int flags)
{
2584 2585 2586
    if (virNodeSuspendForDurationEnsureACL(conn) < 0)
        return -1;

2587
    return virNodeSuspend(target, duration, flags);
2588 2589 2590
}


E
Eric Blake 已提交
2591
/*----- Register with libvirt.c, and initialize Xen drivers. -----*/
2592 2593

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

2690 2691 2692 2693 2694

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

2695
/**
2696
 * xenRegister:
2697 2698 2699 2700 2701
 *
 * Register xen related drivers
 *
 * Returns the driver priority or -1 in case of error.
 */
2702
int
2703
xenRegister(void)
2704
{
2705
    if (virRegisterStateDriver(&state_driver) == -1) return -1;
J
John Levon 已提交
2706

2707 2708
    return virRegisterConnectDriver(&xenUnifiedConnectDriver,
                                    true);
2709 2710
}

2711 2712 2713 2714 2715 2716 2717 2718
/**
 * xenUnifiedDomainInfoListFree:
 *
 * Free the Domain Info List
 */
void
xenUnifiedDomainInfoListFree(xenUnifiedDomainInfoListPtr list)
{
2719
    size_t i;
J
John Levon 已提交
2720 2721 2722 2723

    if (list == NULL)
        return;

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

    if (VIR_ALLOC(info) < 0)
2757
        goto error;
2758 2759
    if (VIR_STRDUP(info->name, name) < 0)
        goto error;
2760 2761 2762 2763 2764

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

    /* Make space on list */
2765
    if (VIR_APPEND_ELEMENT(list->doms, list->count, info) < 0)
2766
        goto error;
2767 2768

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

2797
            VIR_DELETE_ELEMENT(list->doms, i, list->count);
2798 2799 2800 2801 2802 2803
            return 0;
        }
    }
    return -1;
}

2804

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

2821
    virObjectEventStateQueue(priv->domainEvents, event);
D
Daniel P. Berrange 已提交
2822 2823 2824 2825 2826 2827 2828 2829 2830 2831
}

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

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