xen_driver.c 70.6 KB
Newer Older
1
/*
2
 * xen_driver.c: Unified Xen driver.
3
 *
4
 * Copyright (C) 2007-2013 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"
49
#include "xen_hypervisor.h"
50 51 52
#include "xend_internal.h"
#include "xs_internal.h"
#include "xm_internal.h"
53
#if WITH_XEN_INOTIFY
54
# include "xen_inotify.h"
55
#endif
56
#include "virxml.h"
57
#include "virutil.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

70
#define VIR_FROM_THIS VIR_FROM_XEN
71
#define XEN_SAVE_DIR LOCALSTATEDIR "/lib/libvirt/xen/save"
72

73
static int
74
xenUnifiedNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info);
75
static int
76
xenUnifiedDomainGetMaxVcpus(virDomainPtr dom);
77
static int
78 79 80
xenUnifiedDomainGetVcpus(virDomainPtr dom,
                         virVcpuInfoPtr info, int maxinfo,
                         unsigned char *cpumaps, int maplen);
81

82

83
/* The five Xen drivers below us. */
D
Daniel P. Berrange 已提交
84
static struct xenUnifiedDriver const * const drivers[XEN_UNIFIED_NR_DRIVERS] = {
85 86 87 88
    [XEN_UNIFIED_HYPERVISOR_OFFSET] = &xenHypervisorDriver,
    [XEN_UNIFIED_XEND_OFFSET] = &xenDaemonDriver,
    [XEN_UNIFIED_XS_OFFSET] = &xenStoreDriver,
    [XEN_UNIFIED_XM_OFFSET] = &xenXMDriver,
89 90 91
#if WITH_XEN_INOTIFY
    [XEN_UNIFIED_INOTIFY_OFFSET] = &xenInotifyDriver,
#endif
92 93
};

94
#if defined WITH_LIBVIRTD || defined __sun
95
static bool inside_daemon = false;
96
#endif
J
John Levon 已提交
97

98 99 100 101 102
/**
 * xenNumaInit:
 * @conn: pointer to the hypervisor connection
 *
 * Initializer for previous variables. We currently assume that
R
Richard W.M. Jones 已提交
103
 * the number of physical CPU and the number of NUMA cell is fixed
104 105 106 107 108
 * until reboot which might be false in future Xen implementations.
 */
static void
xenNumaInit(virConnectPtr conn) {
    virNodeInfo nodeInfo;
D
Daniel P. Berrange 已提交
109
    xenUnifiedPrivatePtr priv;
110 111 112 113 114 115
    int ret;

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

D
Daniel P. Berrange 已提交
116
    priv = conn->privateData;
117

D
Daniel P. Berrange 已提交
118 119
    priv->nbNodeCells = nodeInfo.nodes;
    priv->nbNodeCpus = nodeInfo.cpus;
120 121
}

D
Daniel P. Berrange 已提交
122

123 124 125 126 127 128 129 130 131 132 133 134 135 136
/**
 * xenDomainUsedCpus:
 * @dom: the domain
 *
 * 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 *
xenDomainUsedCpus(virDomainPtr dom)
{
    char *res = NULL;
D
Daniel P. Berrange 已提交
137
    int ncpus;
138
    int nb_vcpu;
139
    virBitmapPtr cpulist = NULL;
140 141 142 143 144 145
    unsigned char *cpumap = NULL;
    size_t cpumaplen;
    int nb = 0;
    int n, m;
    virVcpuInfoPtr cpuinfo = NULL;
    virNodeInfo nodeinfo;
D
Daniel P. Berrange 已提交
146
    xenUnifiedPrivatePtr priv;
147 148

    if (!VIR_IS_CONNECTED_DOMAIN(dom))
149
        return NULL;
150

D
Daniel P. Berrange 已提交
151 152 153
    priv = dom->conn->privateData;

    if (priv->nbNodeCpus <= 0)
154
        return NULL;
155 156
    nb_vcpu = xenUnifiedDomainGetMaxVcpus(dom);
    if (nb_vcpu <= 0)
157
        return NULL;
158
    if (xenUnifiedNodeGetInfo(dom->conn, &nodeinfo) < 0)
159
        return NULL;
160

161
    if (!(cpulist = virBitmapNew(priv->nbNodeCpus))) {
162
        virReportOOMError();
163
        goto done;
164 165
    }
    if (VIR_ALLOC_N(cpuinfo, nb_vcpu) < 0) {
166
        virReportOOMError();
167
        goto done;
168
    }
169
    cpumaplen = VIR_CPU_MAPLEN(VIR_NODEINFO_MAXCPUS(nodeinfo));
170
    if (xalloc_oversized(nb_vcpu, cpumaplen) ||
171
        VIR_ALLOC_N(cpumap, nb_vcpu * cpumaplen) < 0) {
172
        virReportOOMError();
173
        goto done;
174
    }
175 176 177

    if ((ncpus = xenUnifiedDomainGetVcpus(dom, cpuinfo, nb_vcpu,
                                          cpumap, cpumaplen)) >= 0) {
178
        for (n = 0 ; n < ncpus ; n++) {
D
Daniel P. Berrange 已提交
179
            for (m = 0 ; m < priv->nbNodeCpus; m++) {
180 181 182
                bool used;
                ignore_value(virBitmapGetBit(cpulist, m, &used));
                if ((!used) &&
183
                    (VIR_CPU_USABLE(cpumap, cpumaplen, n, m))) {
184
                    ignore_value(virBitmapSetBit(cpulist, m));
185 186
                    nb++;
                    /* if all CPU are used just return NULL */
D
Daniel P. Berrange 已提交
187
                    if (nb == priv->nbNodeCpus)
188 189 190 191 192
                        goto done;

                }
            }
        }
193
        res = virBitmapFormat(cpulist);
194 195 196
    }

done:
197
    virBitmapFree(cpulist);
198 199
    VIR_FREE(cpumap);
    VIR_FREE(cpuinfo);
200
    return res;
201 202
}

J
John Levon 已提交
203 204 205
#ifdef WITH_LIBVIRTD

static int
206 207 208
xenUnifiedStateInitialize(bool privileged ATTRIBUTE_UNUSED,
                          virStateInhibitCallback callback ATTRIBUTE_UNUSED,
                          void *opaque ATTRIBUTE_UNUSED)
J
John Levon 已提交
209
{
210
    inside_daemon = true;
J
John Levon 已提交
211 212 213 214
    return 0;
}

static virStateDriver state_driver = {
215
    .name = "Xen",
216
    .stateInitialize = xenUnifiedStateInitialize,
J
John Levon 已提交
217 218 219 220
};

#endif

221 222 223 224 225 226 227 228 229 230 231
/*----- 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.
 */

232
static int
233
xenUnifiedProbe(void)
234 235 236
{
#ifdef __linux__
    if (virFileExists("/proc/xen"))
237
        return 1;
238
#endif
239
#ifdef __sun
240
    int fd;
241

242 243
    if ((fd = open("/dev/xen/domcaps", O_RDONLY)) >= 0) {
        VIR_FORCE_CLOSE(fd);
244
        return 1;
245 246
    }
#endif
247
    return 0;
248 249
}

J
Jim Fehlig 已提交
250 251
#ifdef WITH_LIBXL
static int
252
xenUnifiedXendProbe(void)
J
Jim Fehlig 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266
{
    virCommandPtr cmd;
    int status;
    int ret = 0;

    cmd = virCommandNewArgList("/usr/sbin/xend", "status", NULL);
    if (virCommandRun(cmd, &status) == 0 && status == 0)
        ret = 1;
    virCommandFree(cmd);

    return ret;
}
#endif

267

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
static int
xenDomainDeviceDefPostParse(virDomainDeviceDefPtr dev,
                            virDomainDefPtr def,
                            virCapsPtr caps ATTRIBUTE_UNUSED,
                            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 &&
        STRNEQ(def->os.type, "hvm"))
        dev->data.chr->targetType = VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_XEN;

    return 0;
}


284 285
virDomainDefParserConfig xenDomainDefParserConfig = {
    .macPrefix = { 0x00, 0x16, 0x3e },
286
    .devicesPostParseCallback = xenDomainDeviceDefPostParse,
287 288
};

289

290 291 292 293 294 295 296 297
virDomainXMLOptionPtr
xenDomainXMLConfInit(void)
{
    return virDomainXMLOptionNew(&xenDomainDefParserConfig,
                                 NULL, NULL);
}


298
static virDrvOpenStatus
299
xenUnifiedConnectOpen(virConnectPtr conn, virConnectAuthPtr auth, unsigned int flags)
300
{
301
    int i, ret = VIR_DRV_OPEN_DECLINED;
302
    xenUnifiedPrivatePtr priv;
303
    char ebuf[1024];
304

J
John Levon 已提交
305 306 307 308 309 310 311 312 313
#ifdef __sun
    /*
     * Only the libvirtd instance can open this driver.
     * Everything else falls back to the remote driver.
     */
    if (!inside_daemon)
        return VIR_DRV_OPEN_DECLINED;
#endif

314 315 316 317
    if (conn->uri == NULL) {
        if (!xenUnifiedProbe())
            return VIR_DRV_OPEN_DECLINED;

318
        if (!(conn->uri = virURIParse("xen:///")))
319
            return VIR_DRV_OPEN_ERROR;
320 321 322 323 324 325 326 327 328 329 330 331
    } 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;


            /* Return an error if the path isn't '' or '/' */
            if (conn->uri->path &&
                STRNEQ(conn->uri->path, "") &&
                STRNEQ(conn->uri->path, "/")) {
332 333 334
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("unexpected Xen URI path '%s', try xen:///"),
                               conn->uri->path);
335 336
                return VIR_DRV_OPEN_ERROR;
            }
337

338 339 340 341 342 343
            /* 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 {
344
            return VIR_DRV_OPEN_DECLINED;
345 346
        }
    }
347

J
Jim Fehlig 已提交
348 349 350 351 352 353 354
#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

355 356
    /* We now know the URI is definitely for this driver, so beyond
     * here, don't return DECLINED, always use ERROR */
357 358

    /* Allocate per-connection private data. */
359
    if (VIR_ALLOC(priv) < 0) {
360
        virReportOOMError();
361 362
        return VIR_DRV_OPEN_ERROR;
    }
D
Daniel P. Berrange 已提交
363
    if (virMutexInit(&priv->lock) < 0) {
364 365
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("cannot initialize mutex"));
D
Daniel P. Berrange 已提交
366 367 368
        VIR_FREE(priv);
        return VIR_DRV_OPEN_ERROR;
    }
369

370
    if (!(priv->domainEvents = virDomainEventStateNew())) {
D
Daniel P. Berrange 已提交
371 372
        virMutexDestroy(&priv->lock);
        VIR_FREE(priv);
373 374
        return VIR_DRV_OPEN_ERROR;
    }
D
Daniel P. Berrange 已提交
375 376
    conn->privateData = priv;

377 378 379 380 381
    priv->handle = -1;
    priv->xendConfigVersion = -1;
    priv->xshandle = NULL;


J
John Levon 已提交
382 383
    /* Hypervisor is only run with privilege & required to succeed */
    if (xenHavePrivilege()) {
384
        VIR_DEBUG("Trying hypervisor sub-driver");
E
Eric Blake 已提交
385
        if (xenHypervisorOpen(conn, auth, flags) == VIR_DRV_OPEN_SUCCESS) {
386
            VIR_DEBUG("Activated hypervisor sub-driver");
387
            priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] = 1;
388 389
        } else {
            goto fail;
390
        }
391
    }
392

393
    /* XenD is required to succeed if privileged */
394
    VIR_DEBUG("Trying XenD sub-driver");
E
Eric Blake 已提交
395
    if (xenDaemonOpen(conn, auth, flags) == VIR_DRV_OPEN_SUCCESS) {
396
        VIR_DEBUG("Activated XenD sub-driver");
397 398 399 400
        priv->opened[XEN_UNIFIED_XEND_OFFSET] = 1;

        /* XenD is active, so try the xm & xs drivers too, both requird to
         * succeed if root, optional otherwise */
401
        if (priv->xendConfigVersion <= XEND_CONFIG_VERSION_3_0_3) {
402
            VIR_DEBUG("Trying XM sub-driver");
E
Eric Blake 已提交
403
            if (xenXMOpen(conn, auth, flags) == VIR_DRV_OPEN_SUCCESS) {
404
                VIR_DEBUG("Activated XM sub-driver");
405 406 407
                priv->opened[XEN_UNIFIED_XM_OFFSET] = 1;
            }
        }
408
        VIR_DEBUG("Trying XS sub-driver");
E
Eric Blake 已提交
409
        if (xenStoreOpen(conn, auth, flags) == VIR_DRV_OPEN_SUCCESS) {
410
            VIR_DEBUG("Activated XS sub-driver");
411 412
            priv->opened[XEN_UNIFIED_XS_OFFSET] = 1;
        } else {
J
John Levon 已提交
413 414
            if (xenHavePrivilege())
                goto fail; /* XS is mandatory when privileged */
415 416
        }
    } else {
J
John Levon 已提交
417 418
        if (xenHavePrivilege()) {
            goto fail; /* XenD is mandatory when privileged */
419
        } else {
420
            VIR_DEBUG("Handing off for remote driver");
421 422
            ret = VIR_DRV_OPEN_DECLINED; /* Let remote_driver try instead */
            goto clean;
423
        }
424 425
    }

D
Daniel P. Berrange 已提交
426 427
    xenNumaInit(conn);

428
    if (!(priv->caps = xenHypervisorMakeCapabilities(conn))) {
429
        VIR_DEBUG("Failed to make capabilities");
430 431 432
        goto fail;
    }

433
    if (!(priv->xmlopt = xenDomainXMLConfInit()))
434 435
        goto fail;

436
#if WITH_XEN_INOTIFY
437
    if (xenHavePrivilege()) {
438
        VIR_DEBUG("Trying Xen inotify sub-driver");
E
Eric Blake 已提交
439
        if (xenInotifyOpen(conn, auth, flags) == VIR_DRV_OPEN_SUCCESS) {
440
            VIR_DEBUG("Activated Xen inotify sub-driver");
441 442
            priv->opened[XEN_UNIFIED_INOTIFY_OFFSET] = 1;
        }
443 444 445
    }
#endif

446 447 448 449 450 451 452 453 454 455 456
    if (virAsprintf(&priv->saveDir, "%s", XEN_SAVE_DIR) == -1) {
        virReportOOMError();
        goto fail;
    }

    if (virFileMakePath(priv->saveDir) < 0) {
        VIR_ERROR(_("Failed to create save dir '%s': %s"), priv->saveDir,
                  virStrerror(errno, ebuf, sizeof(ebuf)));
        goto fail;
    }

457
    return VIR_DRV_OPEN_SUCCESS;
458

459 460 461
fail:
    ret = VIR_DRV_OPEN_ERROR;
clean:
462
    VIR_DEBUG("Failed to activate a mandatory sub-driver");
463
    for (i = 0 ; i < XEN_UNIFIED_NR_DRIVERS ; i++)
464 465
        if (priv->opened[i])
            drivers[i]->xenClose(conn);
D
Daniel P. Berrange 已提交
466
    virMutexDestroy(&priv->lock);
467
    VIR_FREE(priv->saveDir);
468
    VIR_FREE(priv);
D
Daniel P. Berrange 已提交
469
    conn->privateData = NULL;
D
Daniel Veillard 已提交
470
    return ret;
471 472
}

473 474 475
#define GET_PRIVATE(conn) \
    xenUnifiedPrivatePtr priv = (xenUnifiedPrivatePtr) (conn)->privateData

476
static int
477
xenUnifiedConnectClose(virConnectPtr conn)
478
{
479
    GET_PRIVATE(conn);
480
    int i;
481

482
    virObjectUnref(priv->caps);
483
    virObjectUnref(priv->xmlopt);
484
    virDomainEventStateFree(priv->domainEvents);
485

486
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
487 488
        if (priv->opened[i])
            drivers[i]->xenClose(conn);
489

490
    VIR_FREE(priv->saveDir);
D
Daniel P. Berrange 已提交
491 492
    virMutexDestroy(&priv->lock);
    VIR_FREE(conn->privateData);
493

494
    return 0;
495 496
}

497 498 499 500 501 502 503 504 505 506 507

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


508
static const char *
509
xenUnifiedConnectGetType(virConnectPtr conn)
510
{
511
    GET_PRIVATE(conn);
512
    int i;
513

514
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
515 516
        if (priv->opened[i])
            return "Xen";
517

518
    return NULL;
519 520
}

521 522
/* Which features are supported by this driver? */
static int
523
xenUnifiedConnectSupportsFeature(virConnectPtr conn ATTRIBUTE_UNUSED, int feature)
524 525
{
    switch (feature) {
526 527 528 529 530
    case VIR_DRV_FEATURE_MIGRATION_V1:
    case VIR_DRV_FEATURE_MIGRATION_DIRECT:
        return 1;
    default:
        return 0;
531 532 533
    }
}

534
static int
535
xenUnifiedConnectGetVersion(virConnectPtr conn, unsigned long *hvVer)
536
{
537
    GET_PRIVATE(conn);
538
    int i;
539

540 541
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
E
Eric Blake 已提交
542
            drivers[i]->xenVersion &&
543
            drivers[i]->xenVersion(conn, hvVer) == 0)
544
            return 0;
545

546
    return -1;
547 548
}

549
static int
550
xenUnifiedConnectIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
551 552 553 554 555
{
    return 0;
}

static int
556
xenUnifiedConnectIsSecure(virConnectPtr conn)
557 558 559 560 561 562 563 564 565 566 567 568
{
    GET_PRIVATE(conn);
    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;
}

569
static int
570
xenUnifiedConnectIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
571 572 573 574 575
{
    /* XenD reconnects for each request */
    return 1;
}

576
int
577
xenUnifiedConnectGetMaxVcpus(virConnectPtr conn, const char *type)
578
{
579
    GET_PRIVATE(conn);
580

581
    if (type && STRCASENEQ(type, "Xen")) {
582
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
583 584
        return -1;
    }
585

586
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET])
587
        return xenHypervisorGetMaxVcpus(conn, type);
588
    else {
589
        virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
590 591
        return -1;
    }
592 593 594
}

static int
595
xenUnifiedNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info)
596
{
597
    GET_PRIVATE(conn);
598

599 600
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        return xenDaemonNodeGetInfo(conn, info);
601
    return -1;
602 603 604
}

static char *
605
xenUnifiedConnectGetCapabilities(virConnectPtr conn)
606
{
D
Daniel P. Berrange 已提交
607 608
    xenUnifiedPrivatePtr priv = conn->privateData;
    char *xml;
609

D
Daniel P. Berrange 已提交
610
    if (!(xml = virCapabilitiesFormatXML(priv->caps))) {
611
        virReportOOMError();
D
Daniel P. Berrange 已提交
612 613
        return NULL;
    }
614

D
Daniel P. Berrange 已提交
615
    return xml;
616 617 618
}

static int
619
xenUnifiedConnectListDomains(virConnectPtr conn, int *ids, int maxids)
620
{
621
    GET_PRIVATE(conn);
622
    int ret;
623

624 625
    /* Try xenstore. */
    if (priv->opened[XEN_UNIFIED_XS_OFFSET]) {
626
        ret = xenStoreListDomains(conn, ids, maxids);
627 628
        if (ret >= 0) return ret;
    }
629

630 631
    /* Try HV. */
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET]) {
632
        ret = xenHypervisorListDomains(conn, ids, maxids);
633 634 635 636 637
        if (ret >= 0) return ret;
    }

    /* Try xend. */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
638
        ret = xenDaemonListDomains(conn, ids, maxids);
639 640 641
        if (ret >= 0) return ret;
    }

642
    return -1;
643 644 645
}

static int
646
xenUnifiedConnectNumOfDomains(virConnectPtr conn)
647
{
648
    GET_PRIVATE(conn);
649
    int ret;
650

651 652
    /* Try xenstore. */
    if (priv->opened[XEN_UNIFIED_XS_OFFSET]) {
653
        ret = xenStoreNumOfDomains(conn);
654 655 656 657 658
        if (ret >= 0) return ret;
    }

    /* Try HV. */
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET]) {
659
        ret = xenHypervisorNumOfDomains(conn);
660 661 662 663 664
        if (ret >= 0) return ret;
    }

    /* Try xend. */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
665
        ret = xenDaemonNumOfDomains(conn);
666 667 668
        if (ret >= 0) return ret;
    }

669
    return -1;
670 671 672
}

static virDomainPtr
673 674
xenUnifiedDomainCreateXML(virConnectPtr conn,
                          const char *xmlDesc, unsigned int flags)
675
{
676
    GET_PRIVATE(conn);
677

678 679
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        return xenDaemonCreateXML(conn, xmlDesc, flags);
680
    return NULL;
681 682
}

683 684 685 686
/* Assumption made in underlying drivers:
 * If the domain is "not found" and there is no other error, then
 * the Lookup* functions return a NULL but do not set virterror.
 */
687
static virDomainPtr
688
xenUnifiedDomainLookupByID(virConnectPtr conn, int id)
689
{
690
    GET_PRIVATE(conn);
691
    virDomainPtr ret;
692

693 694 695
    /* Reset any connection-level errors in virterror first, in case
     * there is one hanging around from a previous call.
     */
696
    virConnResetLastError(conn);
697

698 699
    /* Try hypervisor/xenstore combo. */
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET]) {
700
        ret = xenHypervisorLookupDomainByID(conn, id);
701 702 703 704
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }

705 706
    /* Try xend. */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
707
        ret = xenDaemonLookupByID(conn, id);
708 709 710
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }
711

712
    /* Not found. */
713
    virReportError(VIR_ERR_NO_DOMAIN, __FUNCTION__);
714
    return NULL;
715 716 717
}

static virDomainPtr
718 719
xenUnifiedDomainLookupByUUID(virConnectPtr conn,
                             const unsigned char *uuid)
720
{
721
    GET_PRIVATE(conn);
722
    virDomainPtr ret;
723

724 725 726
    /* Reset any connection-level errors in virterror first, in case
     * there is one hanging around from a previous call.
     */
727
    virConnResetLastError(conn);
728

729 730
    /* Try hypervisor/xenstore combo. */
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET]) {
731
        ret = xenHypervisorLookupDomainByUUID(conn, uuid);
732 733 734 735
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }

736 737
    /* Try xend. */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
738
        ret = xenDaemonLookupByUUID(conn, uuid);
739 740 741
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }
742

743 744
    /* Try XM for inactive domains. */
    if (priv->opened[XEN_UNIFIED_XM_OFFSET]) {
745
        ret = xenXMDomainLookupByUUID(conn, uuid);
746 747 748 749 750
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }

    /* Not found. */
751
    virReportError(VIR_ERR_NO_DOMAIN, __FUNCTION__);
752
    return NULL;
753 754 755
}

static virDomainPtr
756 757
xenUnifiedDomainLookupByName(virConnectPtr conn,
                             const char *name)
758
{
759
    GET_PRIVATE(conn);
760
    virDomainPtr ret;
761

762 763 764
    /* Reset any connection-level errors in virterror first, in case
     * there is one hanging around from a previous call.
     */
765
    virConnResetLastError(conn);
766 767 768

    /* Try xend. */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
769
        ret = xenDaemonLookupByName(conn, name);
770 771 772 773 774 775
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }

    /* Try xenstore for inactive domains. */
    if (priv->opened[XEN_UNIFIED_XS_OFFSET]) {
776
        ret = xenStoreLookupByName(conn, name);
777 778 779 780 781 782
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }

    /* Try XM for inactive domains. */
    if (priv->opened[XEN_UNIFIED_XM_OFFSET]) {
783
        ret = xenXMDomainLookupByName(conn, name);
784 785 786
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }
787

788
    /* Not found. */
789
    virReportError(VIR_ERR_NO_DOMAIN, __FUNCTION__);
790
    return NULL;
791 792
}

793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811

static int
xenUnifiedDomainIsActive(virDomainPtr dom)
{
    virDomainPtr currdom;
    int ret = -1;

    /* ID field in dom may be outdated, so re-lookup */
    currdom = xenUnifiedDomainLookupByUUID(dom->conn, dom->uuid);

    if (currdom) {
        ret = currdom->id == -1 ? 0 : 1;
        virDomainFree(currdom);
    }

    return ret;
}

static int
812
xenUnifiedDomainIsPersistent(virDomainPtr dom)
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
{
    GET_PRIVATE(dom->conn);
    virDomainPtr currdom = NULL;
    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 */
        currdom = xenXMDomainLookupByUUID(dom->conn, dom->uuid);
        if (currdom)
            ret = 1;
        else
            ret = 0;
    } else {
        /* New Xen with inactive domain management */
        if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
            currdom = xenDaemonLookupByUUID(dom->conn, dom->uuid);
            if (currdom) {
                if (currdom->id == -1) {
                    /* 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);
                    if (virAsprintf(&path, "%s/%s", XEND_DOMAINS_DIR, uuidstr) < 0) {
843
                        virReportOOMError();
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
                        goto done;
                    }
                    if (access(path, R_OK) == 0)
                        ret = 1;
                    else if (errno == ENOENT)
                        ret = 0;
                }
            }
        }
    }

done:
    if (currdom)
        virDomainFree(currdom);

    return ret;
}

862 863 864 865 866 867
static int
xenUnifiedDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

868
static int
869
xenUnifiedDomainSuspend(virDomainPtr dom)
870
{
871
    GET_PRIVATE(dom->conn);
872
    int i;
873

874 875 876
    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
877
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
878
        if (i != XEN_UNIFIED_HYPERVISOR_OFFSET &&
879
            priv->opened[i] &&
E
Eric Blake 已提交
880
            drivers[i]->xenDomainSuspend &&
881
            drivers[i]->xenDomainSuspend(dom) == 0)
882
            return 0;
883

884
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] &&
E
Eric Blake 已提交
885
        xenHypervisorPauseDomain(dom) == 0)
886
        return 0;
887

888
    return -1;
889 890 891
}

static int
892
xenUnifiedDomainResume(virDomainPtr dom)
893
{
894
    GET_PRIVATE(dom->conn);
895
    int i;
896

897 898 899
    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
900
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
901
        if (i != XEN_UNIFIED_HYPERVISOR_OFFSET &&
902
            priv->opened[i] &&
E
Eric Blake 已提交
903
            drivers[i]->xenDomainResume &&
904
            drivers[i]->xenDomainResume(dom) == 0)
905
            return 0;
906

907
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] &&
E
Eric Blake 已提交
908
        xenHypervisorResumeDomain(dom) == 0)
909
        return 0;
910

911
    return -1;
912 913 914
}

static int
915 916
xenUnifiedDomainShutdownFlags(virDomainPtr dom,
                              unsigned int flags)
917
{
918
    GET_PRIVATE(dom->conn);
919
    int i;
920

921 922
    virCheckFlags(0, -1);

923 924
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
E
Eric Blake 已提交
925
            drivers[i]->xenDomainShutdown &&
926
            drivers[i]->xenDomainShutdown(dom) == 0)
927
            return 0;
928

929
    return -1;
930 931
}

932 933 934 935 936 937
static int
xenUnifiedDomainShutdown(virDomainPtr dom)
{
    return xenUnifiedDomainShutdownFlags(dom, 0);
}

938
static int
939
xenUnifiedDomainReboot(virDomainPtr dom, unsigned int flags)
940
{
941
    GET_PRIVATE(dom->conn);
942
    int i;
943

944 945
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
E
Eric Blake 已提交
946
            drivers[i]->xenDomainReboot &&
947
            drivers[i]->xenDomainReboot(dom, flags) == 0)
948
            return 0;
949

950
    return -1;
951 952
}

953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
static int
xenUnifiedDomainDestroyFlags(virDomainPtr dom,
                             unsigned int flags)
{
    GET_PRIVATE(dom->conn);
    int i;

    virCheckFlags(0, -1);

    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (i != XEN_UNIFIED_HYPERVISOR_OFFSET &&
            priv->opened[i] &&
E
Eric Blake 已提交
968 969
            drivers[i]->xenDomainDestroyFlags &&
            drivers[i]->xenDomainDestroyFlags(dom, flags) == 0)
970 971 972
            return 0;

    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] &&
E
Eric Blake 已提交
973
        xenHypervisorDestroyDomainFlags(dom, flags) == 0)
974 975 976 977 978
        return 0;

    return -1;
}

979 980 981 982 983 984
static int
xenUnifiedDomainDestroy(virDomainPtr dom)
{
    return xenUnifiedDomainDestroyFlags(dom, 0);
}

985
static char *
986
xenUnifiedDomainGetOSType(virDomainPtr dom)
987
{
988
    GET_PRIVATE(dom->conn);
989 990
    int i;
    char *ret;
991

992
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
993
        if (priv->opened[i] && drivers[i]->xenDomainGetOSType) {
994
            ret = drivers[i]->xenDomainGetOSType(dom);
995 996
            if (ret) return ret;
        }
997

998
    return NULL;
999 1000
}

1001
static unsigned long long
1002
xenUnifiedDomainGetMaxMemory(virDomainPtr dom)
1003
{
1004
    GET_PRIVATE(dom->conn);
1005
    int i;
1006
    unsigned long long ret;
1007

1008
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1009
        if (priv->opened[i] && drivers[i]->xenDomainGetMaxMemory) {
1010
            ret = drivers[i]->xenDomainGetMaxMemory(dom);
1011 1012
            if (ret != 0) return ret;
        }
1013

1014
    return 0;
1015 1016 1017
}

static int
1018
xenUnifiedDomainSetMaxMemory(virDomainPtr dom, unsigned long memory)
1019
{
1020
    GET_PRIVATE(dom->conn);
1021
    int i;
1022

1023 1024
    /* Prefer xend for setting max memory */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
1025
        if (xenDaemonDomainSetMaxMemory(dom, memory) == 0)
1026 1027 1028
            return 0;
    }

1029
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
1030 1031
        if (i != XEN_UNIFIED_XEND_OFFSET &&
            priv->opened[i] &&
E
Eric Blake 已提交
1032
            drivers[i]->xenDomainSetMaxMemory &&
1033
            drivers[i]->xenDomainSetMaxMemory(dom, memory) == 0)
1034
            return 0;
1035

1036
    return -1;
1037 1038 1039
}

static int
1040
xenUnifiedDomainSetMemory(virDomainPtr dom, unsigned long memory)
1041
{
1042
    GET_PRIVATE(dom->conn);
1043
    int i;
1044

1045 1046
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
E
Eric Blake 已提交
1047
            drivers[i]->xenDomainSetMemory &&
1048
            drivers[i]->xenDomainSetMemory(dom, memory) == 0)
1049
            return 0;
1050

1051
    return -1;
1052 1053 1054
}

static int
1055
xenUnifiedDomainGetInfo(virDomainPtr dom, virDomainInfoPtr info)
1056
{
1057
    GET_PRIVATE(dom->conn);
1058
    int i;
1059

1060 1061
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
E
Eric Blake 已提交
1062
            drivers[i]->xenDomainGetInfo &&
1063
            drivers[i]->xenDomainGetInfo(dom, info) == 0)
1064
            return 0;
1065

1066
    return -1;
1067 1068
}

1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
static int
xenUnifiedDomainGetState(virDomainPtr dom,
                         int *state,
                         int *reason,
                         unsigned int flags)
{
    GET_PRIVATE(dom->conn);
    int ret;

    virCheckFlags(0, -1);

    /* trying drivers in the same order as GetInfo for consistent results:
     * hypervisor, xend, xs, and xm */

    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET]) {
        ret = xenHypervisorGetDomainState(dom, state, reason, flags);
        if (ret >= 0)
            return ret;
    }

    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
        ret = xenDaemonDomainGetState(dom, state, reason, flags);
        if (ret >= 0)
            return ret;
    }

    if (priv->opened[XEN_UNIFIED_XS_OFFSET]) {
        ret = xenStoreDomainGetState(dom, state, reason, flags);
        if (ret >= 0)
            return ret;
    }

    if (priv->opened[XEN_UNIFIED_XM_OFFSET]) {
        ret = xenXMDomainGetState(dom, state, reason, flags);
        if (ret >= 0)
            return ret;
    }

    return -1;
}

1110
static int
1111 1112
xenUnifiedDomainSaveFlags(virDomainPtr dom, const char *to, const char *dxml,
                          unsigned int flags)
1113
{
1114
    GET_PRIVATE(dom->conn);
1115

1116 1117
    virCheckFlags(0, -1);
    if (dxml) {
1118 1119
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
1120 1121 1122
        return -1;
    }

1123 1124
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        return xenDaemonDomainSave(dom, to);
1125
    return -1;
1126 1127 1128
}

static int
1129 1130 1131 1132 1133
xenUnifiedDomainSave(virDomainPtr dom, const char *to)
{
    return xenUnifiedDomainSaveFlags(dom, to, NULL, 0);
}

1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
static char *
xenUnifiedDomainManagedSavePath(xenUnifiedPrivatePtr priv, virDomainPtr dom)
{
    char *ret;

    if (virAsprintf(&ret, "%s/%s.save", priv->saveDir, dom->name) < 0) {
        virReportOOMError();
        return NULL;
    }

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

static int
xenUnifiedDomainManagedSave(virDomainPtr dom, unsigned int flags)
{
    GET_PRIVATE(dom->conn);
    char *name;
    int ret = -1;

    virCheckFlags(0, -1);

    name = xenUnifiedDomainManagedSavePath(priv, dom);
    if (!name)
        goto cleanup;

    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        ret = xenDaemonDomainSave(dom, name);

cleanup:
    VIR_FREE(name);
    return ret;
}

static int
xenUnifiedDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
{
    GET_PRIVATE(dom->conn);
    char *name;
    int ret = -1;

    virCheckFlags(0, -1);

    name = xenUnifiedDomainManagedSavePath(priv, dom);
    if (!name)
        return ret;

    ret = virFileExists(name);
    VIR_FREE(name);
    return ret;
}

static int
xenUnifiedDomainManagedSaveRemove(virDomainPtr dom, unsigned int flags)
{
    GET_PRIVATE(dom->conn);
    char *name;
    int ret = -1;

    virCheckFlags(0, -1);

    name = xenUnifiedDomainManagedSavePath(priv, dom);
    if (!name)
        return ret;

    ret = unlink(name);
    VIR_FREE(name);
    return ret;
}

1205 1206 1207
static int
xenUnifiedDomainRestoreFlags(virConnectPtr conn, const char *from,
                             const char *dxml, unsigned int flags)
1208
{
1209
    GET_PRIVATE(conn);
1210

1211 1212
    virCheckFlags(0, -1);
    if (dxml) {
1213 1214
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
1215 1216 1217
        return -1;
    }

1218 1219
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        return xenDaemonDomainRestore(conn, from);
1220
    return -1;
1221 1222
}

1223
static int
1224
xenUnifiedDomainRestore(virConnectPtr conn, const char *from)
1225 1226 1227 1228
{
    return xenUnifiedDomainRestoreFlags(conn, from, NULL, 0);
}

1229
static int
1230
xenUnifiedDomainCoreDump(virDomainPtr dom, const char *to, unsigned int flags)
1231
{
1232
    GET_PRIVATE(dom->conn);
1233

1234 1235
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        return xenDaemonDomainCoreDump(dom, to, flags);
1236
    return -1;
1237 1238 1239
}

static int
1240 1241
xenUnifiedDomainSetVcpusFlags(virDomainPtr dom, unsigned int nvcpus,
                              unsigned int flags)
1242
{
1243
    GET_PRIVATE(dom->conn);
1244
    int ret;
1245

1246 1247 1248 1249 1250 1251 1252 1253 1254
    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)) {
1255 1256
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid flag combination: (0x%x)"), flags);
1257 1258 1259
        return -1;
    }
    if (!nvcpus || (unsigned short) nvcpus != nvcpus) {
1260 1261
        virReportError(VIR_ERR_INVALID_ARG,
                       _("argument out of range: %d"), nvcpus);
1262 1263 1264
        return -1;
    }

1265 1266 1267
    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
        ret = xenDaemonDomainSetVcpusFlags(dom, nvcpus, flags);
        if (ret != -2)
            return ret;
    }
    if (priv->opened[XEN_UNIFIED_XM_OFFSET]) {
        ret = xenXMDomainSetVcpusFlags(dom, nvcpus, flags);
        if (ret != -2)
            return ret;
    }
    if (flags == VIR_DOMAIN_VCPU_LIVE)
        return xenHypervisorSetVcpus(dom, nvcpus);
1280

1281
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1282
    return -1;
1283 1284
}

1285
static int
1286
xenUnifiedDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus)
1287
{
1288 1289 1290 1291 1292 1293
    unsigned int flags = VIR_DOMAIN_VCPU_LIVE;

    /* Per the documented API, it is hypervisor-dependent whether this
     * affects just _LIVE or _LIVE|_CONFIG; in xen's case, that
     * depends on xendConfigVersion.  */
    if (dom) {
1294
        GET_PRIVATE(dom->conn);
1295
        if (priv->xendConfigVersion >= XEND_CONFIG_VERSION_3_0_4)
1296
            flags |= VIR_DOMAIN_VCPU_CONFIG;
1297
        return xenUnifiedDomainSetVcpusFlags(dom, nvcpus, flags);
1298
    }
1299
    return -1;
1300 1301
}

1302
static int
1303 1304
xenUnifiedDomainPinVcpu(virDomainPtr dom, unsigned int vcpu,
                        unsigned char *cpumap, int maplen)
1305
{
1306
    GET_PRIVATE(dom->conn);
1307
    int i;
1308

1309 1310
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
E
Eric Blake 已提交
1311
            drivers[i]->xenDomainPinVcpu &&
1312
            drivers[i]->xenDomainPinVcpu(dom, vcpu, cpumap, maplen) == 0)
1313
            return 0;
1314

1315
    return -1;
1316 1317 1318
}

static int
1319 1320 1321
xenUnifiedDomainGetVcpus(virDomainPtr dom,
                         virVcpuInfoPtr info, int maxinfo,
                         unsigned char *cpumaps, int maplen)
1322
{
1323
    GET_PRIVATE(dom->conn);
1324
    int i, ret;
1325

1326
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1327
        if (priv->opened[i] && drivers[i]->xenDomainGetVcpus) {
1328
            ret = drivers[i]->xenDomainGetVcpus(dom, info, maxinfo, cpumaps, maplen);
1329 1330 1331
            if (ret > 0)
                return ret;
        }
1332
    return -1;
1333 1334 1335
}

static int
1336
xenUnifiedDomainGetVcpusFlags(virDomainPtr dom, unsigned int flags)
1337
{
1338
    GET_PRIVATE(dom->conn);
1339
    int ret;
1340

1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
    virCheckFlags(VIR_DOMAIN_VCPU_LIVE |
                  VIR_DOMAIN_VCPU_CONFIG |
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
        ret = xenDaemonDomainGetVcpusFlags(dom, flags);
        if (ret != -2)
            return ret;
    }
    if (priv->opened[XEN_UNIFIED_XM_OFFSET]) {
        ret = xenXMDomainGetVcpusFlags(dom, flags);
        if (ret != -2)
            return ret;
    }
    if (flags == (VIR_DOMAIN_VCPU_CONFIG | VIR_DOMAIN_VCPU_MAXIMUM))
        return xenHypervisorGetVcpuMax(dom);
1357

1358
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1359
    return -1;
1360 1361
}

1362
static int
1363
xenUnifiedDomainGetMaxVcpus(virDomainPtr dom)
1364 1365 1366 1367 1368
{
    return xenUnifiedDomainGetVcpusFlags(dom, (VIR_DOMAIN_VCPU_LIVE |
                                               VIR_DOMAIN_VCPU_MAXIMUM));
}

1369
static char *
1370
xenUnifiedDomainGetXMLDesc(virDomainPtr dom, unsigned int flags)
1371
{
1372
    GET_PRIVATE(dom->conn);
1373

1374
    if (dom->id == -1 && priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4) {
1375
        if (priv->opened[XEN_UNIFIED_XM_OFFSET])
1376
            return xenXMDomainGetXMLDesc(dom, flags);
1377 1378 1379
    } else {
        if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
            char *cpus, *res;
D
Daniel P. Berrange 已提交
1380
            xenUnifiedLock(priv);
1381
            cpus = xenDomainUsedCpus(dom);
D
Daniel P. Berrange 已提交
1382
            xenUnifiedUnlock(priv);
1383
            res = xenDaemonDomainGetXMLDesc(dom, flags, cpus);
1384
            VIR_FREE(cpus);
1385
            return res;
1386
        }
1387
    }
1388

1389
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1390
    return NULL;
1391 1392
}

1393 1394

static char *
1395 1396 1397 1398
xenUnifiedConnectDomainXMLFromNative(virConnectPtr conn,
                                     const char *format,
                                     const char *config,
                                     unsigned int flags)
1399 1400 1401 1402
{
    virDomainDefPtr def = NULL;
    char *ret = NULL;
    virConfPtr conf = NULL;
1403 1404 1405
    int id;
    char * tty;
    int vncport;
1406 1407
    GET_PRIVATE(conn);

E
Eric Blake 已提交
1408 1409
    virCheckFlags(0, NULL);

1410 1411
    if (STRNEQ(format, XEN_CONFIG_FORMAT_XM) &&
        STRNEQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
1412 1413
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unsupported config type %s"), format);
1414 1415 1416 1417
        return NULL;
    }

    if (STREQ(format, XEN_CONFIG_FORMAT_XM)) {
1418
        conf = virConfReadMem(config, strlen(config), 0);
1419 1420 1421
        if (!conf)
            goto cleanup;

M
Markus Groß 已提交
1422
        def = xenParseXM(conf, priv->xendConfigVersion, priv->caps);
1423
    } else if (STREQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
1424 1425 1426 1427 1428
        id = xenGetDomIdFromSxprString(config, priv->xendConfigVersion);
        xenUnifiedLock(priv);
        tty = xenStoreDomainGetConsolePath(conn, id);
        vncport = xenStoreDomainGetVNCPort(conn, id);
        xenUnifiedUnlock(priv);
M
Markus Groß 已提交
1429
        def = xenParseSxprString(config, priv->xendConfigVersion, tty,
1430
                                       vncport);
1431 1432 1433 1434
    }
    if (!def)
        goto cleanup;

1435
    ret = virDomainDefFormat(def, 0);
1436 1437 1438

cleanup:
    virDomainDefFree(def);
1439 1440
    if (conf)
        virConfFree(conf);
1441 1442 1443 1444 1445 1446
    return ret;
}


#define MAX_CONFIG_SIZE (1024 * 65)
static char *
1447 1448 1449 1450
xenUnifiedConnectDomainXMLToNative(virConnectPtr conn,
                                   const char *format,
                                   const char *xmlData,
                                   unsigned int flags)
1451 1452 1453 1454 1455 1456
{
    virDomainDefPtr def = NULL;
    char *ret = NULL;
    virConfPtr conf = NULL;
    GET_PRIVATE(conn);

E
Eric Blake 已提交
1457 1458
    virCheckFlags(0, NULL);

1459 1460
    if (STRNEQ(format, XEN_CONFIG_FORMAT_XM) &&
        STRNEQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
1461 1462
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unsupported config type %s"), format);
1463 1464 1465
        goto cleanup;
    }

1466 1467
    if (!(def = virDomainDefParseString(xmlData, priv->caps, priv->xmlopt,
                                        1 << VIR_DOMAIN_VIRT_XEN, 0)))
1468 1469 1470 1471
        goto cleanup;

    if (STREQ(format, XEN_CONFIG_FORMAT_XM)) {
        int len = MAX_CONFIG_SIZE;
M
Markus Groß 已提交
1472
        conf = xenFormatXM(conn, def, priv->xendConfigVersion);
1473 1474 1475 1476
        if (!conf)
            goto cleanup;

        if (VIR_ALLOC_N(ret, len) < 0) {
1477
            virReportOOMError();
1478 1479 1480 1481 1482 1483 1484 1485
            goto cleanup;
        }

        if (virConfWriteMem(ret, &len, conf) < 0) {
            VIR_FREE(ret);
            goto cleanup;
        }
    } else if (STREQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
M
Markus Groß 已提交
1486
        ret = xenFormatSxpr(conn, def, priv->xendConfigVersion);
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
    }

cleanup:
    virDomainDefFree(def);
    if (conf)
        virConfFree(conf);
    return ret;
}


1497
static int
1498 1499 1500 1501 1502 1503 1504 1505
xenUnifiedDomainMigratePrepare(virConnectPtr dconn,
                               char **cookie,
                               int *cookielen,
                               const char *uri_in,
                               char **uri_out,
                               unsigned long flags,
                               const char *dname,
                               unsigned long resource)
1506 1507 1508
{
    GET_PRIVATE(dconn);

E
Eric Blake 已提交
1509 1510
    virCheckFlags(XEN_MIGRATION_FLAGS, -1);

1511
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
1512 1513 1514
        return xenDaemonDomainMigratePrepare(dconn, cookie, cookielen,
                                             uri_in, uri_out,
                                             flags, dname, resource);
1515

1516
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1517 1518 1519 1520
    return -1;
}

static int
1521 1522 1523 1524 1525 1526 1527
xenUnifiedDomainMigratePerform(virDomainPtr dom,
                               const char *cookie,
                               int cookielen,
                               const char *uri,
                               unsigned long flags,
                               const char *dname,
                               unsigned long resource)
1528 1529 1530
{
    GET_PRIVATE(dom->conn);

E
Eric Blake 已提交
1531 1532
    virCheckFlags(XEN_MIGRATION_FLAGS, -1);

1533
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
1534 1535
        return xenDaemonDomainMigratePerform(dom, cookie, cookielen, uri,
                                             flags, dname, resource);
1536

1537
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1538 1539 1540 1541
    return -1;
}

static virDomainPtr
1542 1543 1544 1545 1546 1547
xenUnifiedDomainMigrateFinish(virConnectPtr dconn,
                              const char *dname,
                              const char *cookie ATTRIBUTE_UNUSED,
                              int cookielen ATTRIBUTE_UNUSED,
                              const char *uri ATTRIBUTE_UNUSED,
                              unsigned long flags)
1548
{
1549 1550 1551 1552
    virDomainPtr dom = NULL;
    char *domain_xml = NULL;
    virDomainPtr dom_new = NULL;

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

1555
    dom = xenUnifiedDomainLookupByName(dconn, dname);
1556 1557 1558 1559 1560
    if (! dom) {
        return NULL;
    }

    if (flags & VIR_MIGRATE_PERSIST_DEST) {
1561
        domain_xml = xenDaemonDomainGetXMLDesc(dom, 0, NULL);
1562
        if (! domain_xml) {
1563 1564
            virReportError(VIR_ERR_MIGRATE_PERSIST_FAILED,
                           "%s", _("failed to get XML representation of migrated domain"));
1565 1566 1567
            goto failure;
        }

1568
        dom_new = xenDaemonDomainDefineXML(dconn, domain_xml);
1569
        if (! dom_new) {
1570 1571
            virReportError(VIR_ERR_MIGRATE_PERSIST_FAILED,
                           "%s", _("failed to define domain on destination host"));
1572 1573 1574 1575
            goto failure;
        }

        /* Free additional reference added by Define */
1576
        virDomainFree(dom_new);
1577 1578
    }

1579
    VIR_FREE(domain_xml);
1580 1581 1582 1583 1584

    return dom;


failure:
1585
    virDomainFree(dom);
1586

1587
    VIR_FREE(domain_xml);
1588 1589

    return NULL;
1590 1591
}

1592
static int
1593 1594
xenUnifiedConnectListDefinedDomains(virConnectPtr conn, char **const names,
                                    int maxnames)
1595
{
1596
    GET_PRIVATE(conn);
1597 1598
    int i;
    int ret;
1599

1600
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1601
        if (priv->opened[i] && drivers[i]->xenListDefinedDomains) {
1602
            ret = drivers[i]->xenListDefinedDomains(conn, names, maxnames);
1603 1604
            if (ret >= 0) return ret;
        }
1605

1606
    return -1;
1607 1608 1609
}

static int
1610
xenUnifiedConnectNumOfDefinedDomains(virConnectPtr conn)
1611
{
1612
    GET_PRIVATE(conn);
1613 1614
    int i;
    int ret;
1615

1616
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1617
        if (priv->opened[i] && drivers[i]->xenNumOfDefinedDomains) {
1618
            ret = drivers[i]->xenNumOfDefinedDomains(conn);
1619 1620
            if (ret >= 0) return ret;
        }
1621

1622
    return -1;
1623 1624 1625
}

static int
1626
xenUnifiedDomainCreateWithFlags(virDomainPtr dom, unsigned int flags)
1627
{
1628
    GET_PRIVATE(dom->conn);
1629
    int i;
1630 1631
    int ret = -1;
    char *name = NULL;
1632

1633 1634
    virCheckFlags(0, -1);

1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
    name = xenUnifiedDomainManagedSavePath(priv, dom);
    if (!name)
        goto cleanup;

    if (virFileExists(name)) {
        if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
            ret = xenDaemonDomainRestore(dom->conn, name);
            if (ret == 0)
                unlink(name);
        }
        goto cleanup;
    }

    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i) {
E
Eric Blake 已提交
1649
        if (priv->opened[i] && drivers[i]->xenDomainCreate &&
1650 1651 1652 1653 1654
            drivers[i]->xenDomainCreate(dom) == 0) {
            ret = 0;
            goto cleanup;
        }
    }
1655

1656 1657 1658
cleanup:
    VIR_FREE(name);
    return ret;
1659 1660
}

1661
static int
1662
xenUnifiedDomainCreate(virDomainPtr dom)
1663 1664 1665 1666
{
    return xenUnifiedDomainCreateWithFlags(dom, 0);
}

1667
static virDomainPtr
1668
xenUnifiedDomainDefineXML(virConnectPtr conn, const char *xml)
1669
{
1670
    GET_PRIVATE(conn);
1671 1672
    int i;
    virDomainPtr ret;
1673

1674
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1675
        if (priv->opened[i] && drivers[i]->xenDomainDefineXML) {
1676
            ret = drivers[i]->xenDomainDefineXML(conn, xml);
1677 1678
            if (ret) return ret;
        }
1679

1680
    return NULL;
1681 1682 1683
}

static int
1684
xenUnifiedDomainUndefineFlags(virDomainPtr dom, unsigned int flags)
1685
{
1686
    GET_PRIVATE(dom->conn);
1687
    int i;
1688

1689
    virCheckFlags(0, -1);
1690
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1691
        if (priv->opened[i] && drivers[i]->xenDomainUndefine &&
1692
            drivers[i]->xenDomainUndefine(dom) == 0)
1693
            return 0;
1694

1695
    return -1;
1696 1697
}

1698
static int
1699
xenUnifiedDomainUndefine(virDomainPtr dom) {
1700 1701 1702
    return xenUnifiedDomainUndefineFlags(dom, 0);
}

1703
static int
1704
xenUnifiedDomainAttachDevice(virDomainPtr dom, const char *xml)
1705 1706 1707
{
    GET_PRIVATE(dom->conn);
    int i;
1708
    unsigned int flags = VIR_DOMAIN_DEVICE_MODIFY_LIVE;
1709

1710 1711 1712 1713 1714 1715
    /*
     * HACK: xend with xendConfigVersion >= 3 does not support changing live
     * config without touching persistent config, we add the extra flag here
     * to make this API work
     */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET] &&
1716
        priv->xendConfigVersion >= XEND_CONFIG_VERSION_3_0_4)
1717
        flags |= VIR_DOMAIN_DEVICE_MODIFY_CONFIG;
1718 1719

    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1720 1721
        if (priv->opened[i] && drivers[i]->xenDomainAttachDeviceFlags &&
            drivers[i]->xenDomainAttachDeviceFlags(dom, xml, flags) == 0)
1722 1723 1724 1725 1726 1727
            return 0;

    return -1;
}

static int
1728 1729
xenUnifiedDomainAttachDeviceFlags(virDomainPtr dom, const char *xml,
                                  unsigned int flags)
1730
{
1731
    GET_PRIVATE(dom->conn);
1732
    int i;
1733

1734
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1735 1736
        if (priv->opened[i] && drivers[i]->xenDomainAttachDeviceFlags &&
            drivers[i]->xenDomainAttachDeviceFlags(dom, xml, flags) == 0)
1737
            return 0;
1738

1739
    return -1;
1740 1741 1742
}

static int
1743
xenUnifiedDomainDetachDevice(virDomainPtr dom, const char *xml)
1744 1745 1746
{
    GET_PRIVATE(dom->conn);
    int i;
1747
    unsigned int flags = VIR_DOMAIN_DEVICE_MODIFY_LIVE;
1748

1749 1750 1751 1752 1753 1754
    /*
     * HACK: xend with xendConfigVersion >= 3 does not support changing live
     * config without touching persistent config, we add the extra flag here
     * to make this API work
     */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET] &&
1755
        priv->xendConfigVersion >= XEND_CONFIG_VERSION_3_0_4)
1756
        flags |= VIR_DOMAIN_DEVICE_MODIFY_CONFIG;
1757 1758

    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1759 1760
        if (priv->opened[i] && drivers[i]->xenDomainDetachDeviceFlags &&
            drivers[i]->xenDomainDetachDeviceFlags(dom, xml, flags) == 0)
1761 1762 1763 1764 1765 1766
            return 0;

    return -1;
}

static int
1767 1768
xenUnifiedDomainDetachDeviceFlags(virDomainPtr dom, const char *xml,
                                  unsigned int flags)
1769
{
1770
    GET_PRIVATE(dom->conn);
1771
    int i;
1772

1773
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1774 1775
        if (priv->opened[i] && drivers[i]->xenDomainDetachDeviceFlags &&
            drivers[i]->xenDomainDetachDeviceFlags(dom, xml, flags) == 0)
1776
            return 0;
1777

1778
    return -1;
1779 1780
}

1781
static int
1782 1783
xenUnifiedDomainUpdateDeviceFlags(virDomainPtr dom, const char *xml,
                                  unsigned int flags)
1784 1785 1786
{
    GET_PRIVATE(dom->conn);

1787 1788
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        return xenDaemonUpdateDeviceFlags(dom, xml, flags);
1789 1790 1791
    return -1;
}

1792
static int
1793
xenUnifiedDomainGetAutostart(virDomainPtr dom, int *autostart)
1794 1795 1796
{
    GET_PRIVATE(dom->conn);

1797
    if (priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4) {
1798 1799 1800 1801 1802 1803
        if (priv->opened[XEN_UNIFIED_XM_OFFSET])
            return xenXMDomainGetAutostart(dom, autostart);
    } else {
        if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
            return xenDaemonDomainGetAutostart(dom, autostart);
    }
1804

1805
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1806 1807 1808 1809
    return -1;
}

static int
1810
xenUnifiedDomainSetAutostart(virDomainPtr dom, int autostart)
1811 1812 1813
{
    GET_PRIVATE(dom->conn);

1814
    if (priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4) {
1815 1816 1817 1818 1819 1820
        if (priv->opened[XEN_UNIFIED_XM_OFFSET])
            return xenXMDomainSetAutostart(dom, autostart);
    } else {
        if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
            return xenDaemonDomainSetAutostart(dom, autostart);
    }
1821

1822
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1823 1824 1825
    return -1;
}

1826
static char *
1827
xenUnifiedDomainGetSchedulerType(virDomainPtr dom, int *nparams)
1828 1829 1830 1831 1832 1833
{
    GET_PRIVATE(dom->conn);
    int i;
    char *schedulertype;

    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; i++) {
E
Eric Blake 已提交
1834
        if (priv->opened[i] && drivers[i]->xenDomainGetSchedulerType) {
1835
            schedulertype = drivers[i]->xenDomainGetSchedulerType(dom, nparams);
1836
            if (schedulertype != NULL)
1837
                return schedulertype;
1838 1839
        }
    }
1840
    return NULL;
1841 1842 1843
}

static int
1844 1845 1846 1847
xenUnifiedDomainGetSchedulerParametersFlags(virDomainPtr dom,
                                            virTypedParameterPtr params,
                                            int *nparams,
                                            unsigned int flags)
1848 1849 1850 1851
{
    GET_PRIVATE(dom->conn);
    int i, ret;

1852 1853
    virCheckFlags(0, -1);

1854
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i) {
E
Eric Blake 已提交
1855 1856
        if (priv->opened[i] && drivers[i]->xenDomainGetSchedulerParameters) {
           ret = drivers[i]->xenDomainGetSchedulerParameters(dom, params, nparams);
1857
           if (ret == 0)
1858
               return 0;
1859
        }
1860
    }
1861
    return -1;
1862 1863 1864
}

static int
1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877
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)
1878 1879 1880 1881
{
    GET_PRIVATE(dom->conn);
    int i, ret;

1882 1883
    virCheckFlags(0, -1);

1884 1885
    /* do the hypervisor call last to get better error */
    for (i = XEN_UNIFIED_NR_DRIVERS - 1; i >= 0; i--) {
E
Eric Blake 已提交
1886 1887
        if (priv->opened[i] && drivers[i]->xenDomainSetSchedulerParameters) {
           ret = drivers[i]->xenDomainSetSchedulerParameters(dom, params, nparams);
1888 1889 1890
           if (ret == 0)
               return 0;
        }
1891 1892
    }

1893
    return -1;
1894 1895
}

1896 1897 1898 1899 1900 1901 1902 1903 1904
static int
xenUnifiedDomainSetSchedulerParameters(virDomainPtr dom,
                                       virTypedParameterPtr params,
                                       int nparams)
{
    return xenUnifiedDomainSetSchedulerParametersFlags(dom, params,
                                                       nparams, 0);
}

1905
static int
1906 1907
xenUnifiedDomainBlockStats(virDomainPtr dom, const char *path,
                           struct _virDomainBlockStats *stats)
1908
{
1909
    GET_PRIVATE(dom->conn);
1910 1911

    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET])
1912
        return xenHypervisorDomainBlockStats(dom, path, stats);
1913

1914
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1915 1916 1917 1918
    return -1;
}

static int
1919 1920
xenUnifiedDomainInterfaceStats(virDomainPtr dom, const char *path,
                               struct _virDomainInterfaceStats *stats)
1921
{
1922
    GET_PRIVATE(dom->conn);
1923 1924

    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET])
1925
        return xenHypervisorDomainInterfaceStats(dom, path, stats);
1926

1927
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1928 1929 1930
    return -1;
}

R
Richard W.M. Jones 已提交
1931
static int
1932 1933 1934
xenUnifiedDomainBlockPeek(virDomainPtr dom, const char *path,
                          unsigned long long offset, size_t size,
                          void *buffer, unsigned int flags)
R
Richard W.M. Jones 已提交
1935 1936
{
    int r;
1937
    GET_PRIVATE(dom->conn);
R
Richard W.M. Jones 已提交
1938

E
Eric Blake 已提交
1939 1940
    virCheckFlags(0, -1);

R
Richard W.M. Jones 已提交
1941
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
1942
        r = xenDaemonDomainBlockPeek(dom, path, offset, size, buffer);
R
Richard W.M. Jones 已提交
1943 1944 1945 1946 1947
        if (r != -2) return r;
        /* r == -2 means declined, so fall through to XM driver ... */
    }

    if (priv->opened[XEN_UNIFIED_XM_OFFSET]) {
1948
        if (xenXMDomainBlockPeek(dom, path, offset, size, buffer) == 0)
R
Richard W.M. Jones 已提交
1949 1950 1951
            return 0;
    }

1952
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
R
Richard W.M. Jones 已提交
1953 1954 1955
    return -1;
}

1956
static int
1957 1958
xenUnifiedNodeGetCellsFreeMemory(virConnectPtr conn, unsigned long long *freeMems,
                                 int startCell, int maxCells)
1959
{
1960
    GET_PRIVATE(conn);
1961 1962

    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET])
1963 1964
        return xenHypervisorNodeGetCellsFreeMemory(conn, freeMems,
                                                   startCell, maxCells);
1965

1966
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1967 1968 1969
    return -1;
}

1970
static unsigned long long
1971
xenUnifiedNodeGetFreeMemory(virConnectPtr conn)
1972 1973 1974
{
    unsigned long long freeMem = 0;
    int ret;
1975
    GET_PRIVATE(conn);
1976 1977

    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET]) {
1978 1979
        ret = xenHypervisorNodeGetCellsFreeMemory(conn, &freeMem,
                                                  -1, 1);
1980
        if (ret != 1)
1981 1982
            return 0;
        return freeMem;
1983 1984
    }

1985
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1986
    return 0;
1987 1988
}

1989

1990
static int
1991 1992 1993 1994
xenUnifiedConnectDomainEventRegister(virConnectPtr conn,
                                     virConnectDomainEventCallback callback,
                                     void *opaque,
                                     virFreeCallback freefunc)
1995
{
1996
    GET_PRIVATE(conn);
D
Daniel P. Berrange 已提交
1997

1998
    int ret;
D
Daniel P. Berrange 已提交
1999
    xenUnifiedLock(priv);
2000

2001
    if (priv->xsWatch == -1) {
2002
        virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
D
Daniel P. Berrange 已提交
2003
        xenUnifiedUnlock(priv);
2004 2005 2006
        return -1;
    }

2007 2008
    ret = virDomainEventStateRegister(conn, priv->domainEvents,
                                      callback, opaque, freefunc);
2009

D
Daniel P. Berrange 已提交
2010
    xenUnifiedUnlock(priv);
2011
    return ret;
2012 2013
}

2014

2015
static int
2016 2017
xenUnifiedConnectDomainEventDeregister(virConnectPtr conn,
                                       virConnectDomainEventCallback callback)
2018 2019
{
    int ret;
2020
    GET_PRIVATE(conn);
D
Daniel P. Berrange 已提交
2021 2022
    xenUnifiedLock(priv);

2023
    if (priv->xsWatch == -1) {
2024
        virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
D
Daniel P. Berrange 已提交
2025
        xenUnifiedUnlock(priv);
2026 2027 2028
        return -1;
    }

2029 2030 2031
    ret = virDomainEventStateDeregister(conn,
                                        priv->domainEvents,
                                        callback);
2032

D
Daniel P. Berrange 已提交
2033
    xenUnifiedUnlock(priv);
2034 2035 2036
    return ret;
}

2037

2038
static int
2039 2040 2041 2042 2043 2044
xenUnifiedConnectDomainEventRegisterAny(virConnectPtr conn,
                                        virDomainPtr dom,
                                        int eventID,
                                        virConnectDomainEventGenericCallback callback,
                                        void *opaque,
                                        virFreeCallback freefunc)
2045
{
2046
    GET_PRIVATE(conn);
2047 2048 2049 2050 2051

    int ret;
    xenUnifiedLock(priv);

    if (priv->xsWatch == -1) {
2052
        virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
2053 2054 2055 2056
        xenUnifiedUnlock(priv);
        return -1;
    }

2057 2058 2059
    if (virDomainEventStateRegisterID(conn, priv->domainEvents,
                                      dom, eventID,
                                      callback, opaque, freefunc, &ret) < 0)
2060
        ret = -1;
2061 2062

    xenUnifiedUnlock(priv);
2063
    return ret;
2064 2065 2066
}

static int
2067 2068
xenUnifiedConnectDomainEventDeregisterAny(virConnectPtr conn,
                                          int callbackID)
2069 2070
{
    int ret;
2071
    GET_PRIVATE(conn);
2072 2073 2074
    xenUnifiedLock(priv);

    if (priv->xsWatch == -1) {
2075
        virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
2076 2077 2078 2079
        xenUnifiedUnlock(priv);
        return -1;
    }

2080 2081 2082
    ret = virDomainEventStateDeregisterID(conn,
                                          priv->domainEvents,
                                          callbackID);
2083 2084 2085 2086 2087 2088

    xenUnifiedUnlock(priv);
    return ret;
}


2089
static int
2090 2091 2092 2093 2094
xenUnifiedNodeDeviceGetPciInfo(virNodeDevicePtr dev,
                               unsigned *domain,
                               unsigned *bus,
                               unsigned *slot,
                               unsigned *function)
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104
{
    virNodeDeviceDefPtr def = NULL;
    virNodeDevCapsDefPtr cap;
    char *xml = NULL;
    int ret = -1;

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

2105
    def = virNodeDeviceDefParseString(xml, EXISTING_DEVICE, NULL);
2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122
    if (!def)
        goto out;

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

        cap = cap->next;
    }

    if (!cap) {
2123 2124
        virReportError(VIR_ERR_INVALID_ARG,
                       _("device %s is not a PCI device"), dev->name);
2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
        goto out;
    }

    ret = 0;
out:
    virNodeDeviceDefFree(def);
    VIR_FREE(xml);
    return ret;
}

static int
2136 2137 2138
xenUnifiedNodeDeviceDetachFlags(virNodeDevicePtr dev,
                                const char *driverName,
                                unsigned int flags)
2139
{
2140
    virPCIDevicePtr pci;
2141 2142 2143
    unsigned domain, bus, slot, function;
    int ret = -1;

2144 2145
    virCheckFlags(0, -1);

2146 2147 2148
    if (xenUnifiedNodeDeviceGetPciInfo(dev, &domain, &bus, &slot, &function) < 0)
        return -1;

2149
    pci = virPCIDeviceNew(domain, bus, slot, function);
2150 2151 2152
    if (!pci)
        return -1;

2153 2154 2155 2156 2157 2158 2159 2160 2161
    if (!driverName) {
        virPCIDeviceSetStubDriver(pci, "pciback");
    } else {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unknown driver name '%s'"), driverName);
        goto out;
    }

    if (virPCIDeviceDetach(pci, NULL, NULL, NULL) < 0)
2162 2163 2164 2165
        goto out;

    ret = 0;
out:
2166
    virPCIDeviceFree(pci);
2167 2168 2169
    return ret;
}

2170 2171 2172 2173 2174 2175
static int
xenUnifiedNodeDeviceDettach(virNodeDevicePtr dev)
{
    return xenUnifiedNodeDeviceDetachFlags(dev, NULL, 0);
}

2176
static int
2177
xenUnifiedNodeDeviceAssignedDomainId(virNodeDevicePtr dev)
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188
{
    int numdomains;
    int ret = -1, i;
    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 */
2189
    numdomains = xenUnifiedConnectNumOfDomains(conn);
2190 2191 2192 2193 2194 2195 2196 2197
    if (numdomains < 0) {
        return ret;
    }
    if (numdomains > 0){
        if (VIR_ALLOC_N(ids, numdomains) < 0) {
            virReportOOMError();
            goto out;
        }
2198
        if ((numdomains = xenUnifiedConnectListDomains(conn, &ids[0], numdomains)) < 0) {
2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214
            goto out;
        }
    }

    /* Get pci bdf */
    if (xenUnifiedNodeDeviceGetPciInfo(dev, &domain, &bus, &slot, &function) < 0)
        goto out;

    if (virAsprintf(&bdf, "%04x:%02x:%02x.%0x",
                    domain, bus, slot, function) < 0) {
        virReportOOMError();
        goto out;
    }

    xenUnifiedLock(priv);
    /* Check if bdf is assigned to one of active domains */
2215
    for (i = 0; i < numdomains; i++) {
2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233
        xref = xenStoreDomainGetPCIID(conn, ids[i], bdf);
        if (xref == NULL) {
            continue;
        } else {
            ret = ids[i];
            break;
        }
    }
    xenUnifiedUnlock(priv);

    VIR_FREE(xref);
    VIR_FREE(bdf);
out:
    VIR_FREE(ids);

    return ret;
}

2234
static int
2235
xenUnifiedNodeDeviceReAttach(virNodeDevicePtr dev)
2236
{
2237
    virPCIDevicePtr pci;
2238 2239
    unsigned domain, bus, slot, function;
    int ret = -1;
2240
    int domid;
2241 2242 2243 2244

    if (xenUnifiedNodeDeviceGetPciInfo(dev, &domain, &bus, &slot, &function) < 0)
        return -1;

2245
    pci = virPCIDeviceNew(domain, bus, slot, function);
2246 2247 2248
    if (!pci)
        return -1;

2249 2250
    /* Check if device is assigned to an active guest */
    if ((domid = xenUnifiedNodeDeviceAssignedDomainId(dev)) >= 0) {
2251 2252 2253
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Device %s has been assigned to guest %d"),
                       dev->name, domid);
2254 2255 2256
        goto out;
    }

2257
    if (virPCIDeviceReattach(pci, NULL, NULL) < 0)
2258 2259 2260 2261
        goto out;

    ret = 0;
out:
2262
    virPCIDeviceFree(pci);
2263 2264 2265 2266
    return ret;
}

static int
2267
xenUnifiedNodeDeviceReset(virNodeDevicePtr dev)
2268
{
2269
    virPCIDevicePtr pci;
2270 2271 2272 2273 2274 2275
    unsigned domain, bus, slot, function;
    int ret = -1;

    if (xenUnifiedNodeDeviceGetPciInfo(dev, &domain, &bus, &slot, &function) < 0)
        return -1;

2276
    pci = virPCIDeviceNew(domain, bus, slot, function);
2277 2278 2279
    if (!pci)
        return -1;

2280
    if (virPCIDeviceReset(pci, NULL, NULL) < 0)
2281 2282 2283 2284
        goto out;

    ret = 0;
out:
2285
    virPCIDeviceFree(pci);
2286 2287 2288 2289
    return ret;
}


2290 2291
static int
xenUnifiedDomainOpenConsole(virDomainPtr dom,
2292
                            const char *dev_name,
2293 2294 2295 2296 2297 2298 2299 2300 2301 2302
                            virStreamPtr st,
                            unsigned int flags)
{
    virDomainDefPtr def = NULL;
    int ret = -1;
    virDomainChrDefPtr chr = NULL;

    virCheckFlags(0, -1);

    if (dom->id == -1) {
2303 2304
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("domain is not running"));
2305 2306 2307
        goto cleanup;
    }

2308
    if (dev_name) {
2309
        /* XXX support device aliases in future */
2310 2311
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Named device aliases are not supported"));
2312 2313 2314 2315 2316 2317 2318
        goto cleanup;
    }

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

2319 2320
    if (def->nconsoles)
        chr = def->consoles[0];
2321 2322 2323 2324
    else if (def->nserials)
        chr = def->serials[0];

    if (!chr) {
2325 2326
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot find default console device"));
2327 2328 2329
        goto cleanup;
    }

2330
    if (chr->source.type != VIR_DOMAIN_CHR_TYPE_PTY) {
2331 2332
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("character device %s is not using a PTY"), dev_name);
2333 2334 2335
        goto cleanup;
    }

2336
    if (virFDStreamOpenFile(st, chr->source.data.file.path,
E
Eric Blake 已提交
2337
                            0, 0, O_RDWR) < 0)
2338 2339 2340 2341 2342 2343 2344
        goto cleanup;

    ret = 0;
cleanup:
    virDomainDefFree(def);
    return ret;
}
E
Eric Blake 已提交
2345
/*----- Register with libvirt.c, and initialize Xen drivers. -----*/
2346 2347 2348

/* The interface which we export upwards to libvirt.c. */
static virDriver xenUnifiedDriver = {
2349 2350
    .no = VIR_DRV_XEN_UNIFIED,
    .name = "Xen",
2351 2352 2353 2354 2355
    .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 */
2356
    .connectGetHostname = virGetHostname, /* 0.7.3 */
2357
    .connectGetMaxVcpus = xenUnifiedConnectGetMaxVcpus, /* 0.2.1 */
2358
    .nodeGetInfo = xenUnifiedNodeGetInfo, /* 0.1.0 */
2359 2360 2361
    .connectGetCapabilities = xenUnifiedConnectGetCapabilities, /* 0.2.1 */
    .connectListDomains = xenUnifiedConnectListDomains, /* 0.0.3 */
    .connectNumOfDomains = xenUnifiedConnectNumOfDomains, /* 0.0.3 */
2362 2363 2364 2365 2366 2367 2368
    .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 */
2369
    .domainShutdownFlags = xenUnifiedDomainShutdownFlags, /* 0.9.10 */
2370 2371
    .domainReboot = xenUnifiedDomainReboot, /* 0.1.0 */
    .domainDestroy = xenUnifiedDomainDestroy, /* 0.0.3 */
2372
    .domainDestroyFlags = xenUnifiedDomainDestroyFlags, /* 0.9.4 */
2373 2374 2375 2376 2377 2378 2379
    .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 */
2380
    .domainSaveFlags = xenUnifiedDomainSaveFlags, /* 0.9.4 */
2381 2382 2383
    .domainManagedSave = xenUnifiedDomainManagedSave, /* 1.0.1 */
    .domainHasManagedSaveImage = xenUnifiedDomainHasManagedSaveImage, /* 1.0.1 */
    .domainManagedSaveRemove = xenUnifiedDomainManagedSaveRemove, /* 1.0.1 */
2384
    .domainRestore = xenUnifiedDomainRestore, /* 0.0.3 */
2385
    .domainRestoreFlags = xenUnifiedDomainRestoreFlags, /* 0.9.4 */
2386 2387 2388 2389 2390 2391 2392 2393
    .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 */
2394 2395 2396 2397
    .connectDomainXMLFromNative = xenUnifiedConnectDomainXMLFromNative, /* 0.6.4 */
    .connectDomainXMLToNative = xenUnifiedConnectDomainXMLToNative, /* 0.6.4 */
    .connectListDefinedDomains = xenUnifiedConnectListDefinedDomains, /* 0.1.1 */
    .connectNumOfDefinedDomains = xenUnifiedConnectNumOfDefinedDomains, /* 0.1.5 */
2398 2399 2400 2401
    .domainCreate = xenUnifiedDomainCreate, /* 0.1.1 */
    .domainCreateWithFlags = xenUnifiedDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = xenUnifiedDomainDefineXML, /* 0.1.1 */
    .domainUndefine = xenUnifiedDomainUndefine, /* 0.1.1 */
2402
    .domainUndefineFlags = xenUnifiedDomainUndefineFlags, /* 0.9.4 */
2403 2404 2405 2406 2407 2408 2409 2410 2411
    .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 */
2412
    .domainGetSchedulerParametersFlags = xenUnifiedDomainGetSchedulerParametersFlags, /* 0.9.2 */
2413
    .domainSetSchedulerParameters = xenUnifiedDomainSetSchedulerParameters, /* 0.2.3 */
2414
    .domainSetSchedulerParametersFlags = xenUnifiedDomainSetSchedulerParametersFlags, /* 0.9.2 */
2415 2416 2417 2418 2419 2420 2421 2422
    .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 */
2423 2424
    .connectDomainEventRegister = xenUnifiedConnectDomainEventRegister, /* 0.5.0 */
    .connectDomainEventDeregister = xenUnifiedConnectDomainEventDeregister, /* 0.5.0 */
2425
    .nodeDeviceDettach = xenUnifiedNodeDeviceDettach, /* 0.6.1 */
2426
    .nodeDeviceDetachFlags = xenUnifiedNodeDeviceDetachFlags, /* 1.0.5 */
2427 2428
    .nodeDeviceReAttach = xenUnifiedNodeDeviceReAttach, /* 0.6.1 */
    .nodeDeviceReset = xenUnifiedNodeDeviceReset, /* 0.6.1 */
2429 2430
    .connectIsEncrypted = xenUnifiedConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = xenUnifiedConnectIsSecure, /* 0.7.3 */
2431 2432 2433
    .domainIsActive = xenUnifiedDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = xenUnifiedDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = xenUnifiedDomainIsUpdated, /* 0.8.6 */
2434 2435
    .connectDomainEventRegisterAny = xenUnifiedConnectDomainEventRegisterAny, /* 0.8.0 */
    .connectDomainEventDeregisterAny = xenUnifiedConnectDomainEventDeregisterAny, /* 0.8.0 */
2436
    .domainOpenConsole = xenUnifiedDomainOpenConsole, /* 0.8.6 */
2437
    .connectIsAlive = xenUnifiedConnectIsAlive, /* 0.9.8 */
2438
    .nodeSuspendForDuration = nodeSuspendForDuration, /* 0.9.8 */
2439 2440
    .nodeGetMemoryParameters = nodeGetMemoryParameters, /* 0.10.2 */
    .nodeSetMemoryParameters = nodeSetMemoryParameters, /* 0.10.2 */
2441 2442
};

2443
/**
2444
 * xenRegister:
2445 2446 2447 2448 2449
 *
 * Register xen related drivers
 *
 * Returns the driver priority or -1 in case of error.
 */
2450
int
2451
xenRegister(void)
2452
{
J
John Levon 已提交
2453
#ifdef WITH_LIBVIRTD
2454
    if (virRegisterStateDriver(&state_driver) == -1) return -1;
J
John Levon 已提交
2455 2456
#endif

2457
    return virRegisterDriver(&xenUnifiedDriver);
2458 2459
}

2460 2461 2462 2463 2464 2465 2466 2467 2468
/**
 * xenUnifiedDomainInfoListFree:
 *
 * Free the Domain Info List
 */
void
xenUnifiedDomainInfoListFree(xenUnifiedDomainInfoListPtr list)
{
    int i;
J
John Levon 已提交
2469 2470 2471 2472

    if (list == NULL)
        return;

2473 2474 2475 2476
    for (i=0; i<list->count; i++) {
        VIR_FREE(list->doms[i]->name);
        VIR_FREE(list->doms[i]);
    }
2477
    VIR_FREE(list->doms);
2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499
    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 */
    for (n=0; n < list->count; n++) {
        if (STREQ(list->doms[n]->name, name) &&
            !memcmp(list->doms[n]->uuid, uuid, VIR_UUID_BUFLEN)) {
2500
            VIR_DEBUG("WARNING: dom already tracked");
2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522
            return -1;
        }
    }

    if (VIR_ALLOC(info) < 0)
        goto memory_error;
    if (!(info->name = strdup(name)))
        goto memory_error;

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

    /* Make space on list */
    n = list->count;
    if (VIR_REALLOC_N(list->doms, n + 1) < 0) {
        goto memory_error;
    }

    list->doms[n] = info;
    list->count++;
    return 0;
memory_error:
2523
    virReportOOMError();
2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543
    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)
{
    int i;
    for (i = 0 ; i < list->count ; i++) {
2544
        if (list->doms[i]->id == id &&
2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568
            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]);

            if (i < (list->count - 1))
                memmove(list->doms + i,
                        list->doms + i + 1,
                        sizeof(*(list->doms)) *
                                (list->count - (i + 1)));

            if (VIR_REALLOC_N(list->doms,
                              list->count - 1) < 0) {
                ; /* Failure to reduce memory allocation isn't fatal */
            }
            list->count--;

            return 0;
        }
    }
    return -1;
}

2569

2570 2571
/**
 * xenUnifiedDomainEventDispatch:
D
Daniel P. Berrange 已提交
2572 2573
 * @priv: the connection to dispatch events on
 * @event: the event to dispatch
2574 2575 2576
 *
 * Dispatch domain events to registered callbacks
 *
D
Daniel P. Berrange 已提交
2577 2578
 * The caller must hold the lock in 'priv' before invoking
 *
2579
 */
2580
void xenUnifiedDomainEventDispatch(xenUnifiedPrivatePtr priv,
2581
                                    virDomainEventPtr event)
2582
{
D
Daniel P. Berrange 已提交
2583
    if (!priv)
2584
        return;
2585

2586
    virDomainEventStateQueue(priv->domainEvents, event);
D
Daniel P. Berrange 已提交
2587 2588 2589 2590 2591 2592 2593 2594 2595 2596
}

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

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