xen_driver.c 59.8 KB
Newer Older
1
/*
2
 * xen_driver.c: Unified Xen driver.
3
 *
4
 * Copyright (C) 2007-2010 Red Hat, Inc.
5 6 7 8 9 10
 *
 * See COPYING.LIB for the License of this software
 *
 * Richard W.M. Jones <rjones@redhat.com>
 */

11
#include <config.h>
12

13 14 15 16 17 18 19 20 21 22 23
/* Note:
 *
 * This driver provides a unified interface to the five
 * separate underlying Xen drivers (xen_internal, proxy_internal,
 * 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>
24
#include <string.h>
25
#include <errno.h>
26 27
#include <sys/types.h>
#include <xen/dom0_ops.h>
28
#include <libxml/uri.h>
29

30
#include "virterror_internal.h"
31
#include "logging.h"
32
#include "datatypes.h"
33
#include "xen_driver.h"
34

35
#include "xen_hypervisor.h"
36 37 38 39
#include "proxy_internal.h"
#include "xend_internal.h"
#include "xs_internal.h"
#include "xm_internal.h"
40
#if WITH_XEN_INOTIFY
41
# include "xen_inotify.h"
42
#endif
43
#include "xml.h"
44
#include "util.h"
45
#include "memory.h"
46 47
#include "node_device_conf.h"
#include "pci.h"
48
#include "uuid.h"
49

50 51
#define VIR_FROM_THIS VIR_FROM_XEN

52 53
static int
xenUnifiedNodeGetInfo (virConnectPtr conn, virNodeInfoPtr info);
54 55 56 57 58 59
static int
xenUnifiedDomainGetMaxVcpus (virDomainPtr dom);
static int
xenUnifiedDomainGetVcpus (virDomainPtr dom,
                          virVcpuInfoPtr info, int maxinfo,
                          unsigned char *cpumaps, int maplen);
60

61
/* The five Xen drivers below us. */
D
Daniel P. Berrange 已提交
62
static struct xenUnifiedDriver const * const drivers[XEN_UNIFIED_NR_DRIVERS] = {
63 64 65 66 67
    [XEN_UNIFIED_HYPERVISOR_OFFSET] = &xenHypervisorDriver,
    [XEN_UNIFIED_PROXY_OFFSET] = &xenProxyDriver,
    [XEN_UNIFIED_XEND_OFFSET] = &xenDaemonDriver,
    [XEN_UNIFIED_XS_OFFSET] = &xenStoreDriver,
    [XEN_UNIFIED_XM_OFFSET] = &xenXMDriver,
68 69 70
#if WITH_XEN_INOTIFY
    [XEN_UNIFIED_INOTIFY_OFFSET] = &xenInotifyDriver,
#endif
71 72
};

73
#if defined WITH_LIBVIRTD || defined __sun
J
John Levon 已提交
74
static int inside_daemon;
75
#endif
J
John Levon 已提交
76

77 78 79
#define xenUnifiedError(code, ...)                                         \
        virReportErrorHelper(NULL, VIR_FROM_XEN, code, __FILE__,           \
                             __FUNCTION__, __LINE__, __VA_ARGS__)
80

81 82 83 84 85
/**
 * xenNumaInit:
 * @conn: pointer to the hypervisor connection
 *
 * Initializer for previous variables. We currently assume that
R
Richard W.M. Jones 已提交
86
 * the number of physical CPU and the number of NUMA cell is fixed
87 88 89 90 91
 * until reboot which might be false in future Xen implementations.
 */
static void
xenNumaInit(virConnectPtr conn) {
    virNodeInfo nodeInfo;
D
Daniel P. Berrange 已提交
92
    xenUnifiedPrivatePtr priv;
93 94 95 96 97 98
    int ret;

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

D
Daniel P. Berrange 已提交
99
    priv = conn->privateData;
100

D
Daniel P. Berrange 已提交
101 102
    priv->nbNodeCells = nodeInfo.nodes;
    priv->nbNodeCpus = nodeInfo.cpus;
103 104
}

D
Daniel P. Berrange 已提交
105

106 107 108 109 110 111 112 113 114 115 116 117 118 119
/**
 * 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 已提交
120
    int ncpus;
121 122 123 124 125 126 127 128
    int nb_vcpu;
    char *cpulist = NULL;
    unsigned char *cpumap = NULL;
    size_t cpumaplen;
    int nb = 0;
    int n, m;
    virVcpuInfoPtr cpuinfo = NULL;
    virNodeInfo nodeinfo;
D
Daniel P. Berrange 已提交
129
    xenUnifiedPrivatePtr priv;
130 131 132 133

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

D
Daniel P. Berrange 已提交
134 135 136
    priv = dom->conn->privateData;

    if (priv->nbNodeCpus <= 0)
137 138 139 140 141 142 143
        return(NULL);
    nb_vcpu = xenUnifiedDomainGetMaxVcpus(dom);
    if (nb_vcpu <= 0)
        return(NULL);
    if (xenUnifiedNodeGetInfo(dom->conn, &nodeinfo) < 0)
        return(NULL);

144
    if (VIR_ALLOC_N(cpulist, priv->nbNodeCpus) < 0) {
145
        virReportOOMError();
146
        goto done;
147 148
    }
    if (VIR_ALLOC_N(cpuinfo, nb_vcpu) < 0) {
149
        virReportOOMError();
150
        goto done;
151
    }
152
    cpumaplen = VIR_CPU_MAPLEN(VIR_NODEINFO_MAXCPUS(nodeinfo));
153
    if (xalloc_oversized(nb_vcpu, cpumaplen) ||
154
        VIR_ALLOC_N(cpumap, nb_vcpu * cpumaplen) < 0) {
155
        virReportOOMError();
156
        goto done;
157
    }
158 159 160

    if ((ncpus = xenUnifiedDomainGetVcpus(dom, cpuinfo, nb_vcpu,
                                          cpumap, cpumaplen)) >= 0) {
161
        for (n = 0 ; n < ncpus ; n++) {
D
Daniel P. Berrange 已提交
162
            for (m = 0 ; m < priv->nbNodeCpus; m++) {
163 164 165 166 167
                if ((cpulist[m] == 0) &&
                    (VIR_CPU_USABLE(cpumap, cpumaplen, n, m))) {
                    cpulist[m] = 1;
                    nb++;
                    /* if all CPU are used just return NULL */
D
Daniel P. Berrange 已提交
168
                    if (nb == priv->nbNodeCpus)
169 170 171 172 173
                        goto done;

                }
            }
        }
174
        res = virDomainCpuSetFormat(cpulist, priv->nbNodeCpus);
175 176 177
    }

done:
178 179 180
    VIR_FREE(cpulist);
    VIR_FREE(cpumap);
    VIR_FREE(cpuinfo);
181 182 183
    return(res);
}

J
John Levon 已提交
184 185 186
#ifdef WITH_LIBVIRTD

static int
187
xenInitialize (int privileged ATTRIBUTE_UNUSED)
J
John Levon 已提交
188 189 190 191 192 193
{
    inside_daemon = 1;
    return 0;
}

static virStateDriver state_driver = {
194
    .name = "Xen",
J
John Levon 已提交
195 196 197 198 199
    .initialize = xenInitialize,
};

#endif

200 201 202 203 204 205 206 207 208 209 210
/*----- 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.
 */

211
static int
212 213 214 215
xenUnifiedProbe (void)
{
#ifdef __linux__
    if (virFileExists("/proc/xen"))
216
        return 1;
217
#endif
218
#ifdef __sun
219 220 221
    FILE *fh;

    if (fh = fopen("/dev/xen/domcaps", "r")) {
222
        fclose(fh);
223
        return 1;
224 225
    }
#endif
226
    return 0;
227 228
}

229
static virDrvOpenStatus
230
xenUnifiedOpen (virConnectPtr conn, virConnectAuthPtr auth, int flags)
231
{
232
    int i, ret = VIR_DRV_OPEN_DECLINED;
233
    xenUnifiedPrivatePtr priv;
234
    virDomainEventCallbackListPtr cbList;
235

J
John Levon 已提交
236 237 238 239 240 241 242 243 244
#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

245 246 247 248 249 250
    if (conn->uri == NULL) {
        if (!xenUnifiedProbe())
            return VIR_DRV_OPEN_DECLINED;

        conn->uri = xmlParseURI("xen:///");
        if (!conn->uri) {
251
            virReportOOMError();
252 253
            return VIR_DRV_OPEN_ERROR;
        }
254 255 256 257 258 259 260 261 262 263 264 265
    } 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, "/")) {
266
                xenUnifiedError(VIR_ERR_INTERNAL_ERROR,
267 268 269 270
                                _("unexpected Xen URI path '%s', try xen:///"),
                                conn->uri->path);
                return VIR_DRV_OPEN_ERROR;
            }
271

272 273 274 275 276 277 278 279 280 281 282 283
            /* 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 {
            /* Special case URI for Xen driver only:
             *
             * Treat a plain path as a Xen UNIX socket path, and give
             * error unless path is absolute
             */
            if (!conn->uri->path || conn->uri->path[0] != '/') {
284
                xenUnifiedError(VIR_ERR_INTERNAL_ERROR,
285 286 287 288 289 290
                                _("unexpected Xen URI path '%s', try ///var/lib/xen/xend-socket"),
                                NULLSTR(conn->uri->path));
                return VIR_DRV_OPEN_ERROR;
            }
        }
    }
291

292 293
    /* We now know the URI is definitely for this driver, so beyond
     * here, don't return DECLINED, always use ERROR */
294 295

    /* Allocate per-connection private data. */
296
    if (VIR_ALLOC(priv) < 0) {
297
        virReportOOMError();
298 299
        return VIR_DRV_OPEN_ERROR;
    }
D
Daniel P. Berrange 已提交
300
    if (virMutexInit(&priv->lock) < 0) {
301
        xenUnifiedError(VIR_ERR_INTERNAL_ERROR,
E
Eric Blake 已提交
302
                        "%s", _("cannot initialize mutex"));
D
Daniel P. Berrange 已提交
303 304 305
        VIR_FREE(priv);
        return VIR_DRV_OPEN_ERROR;
    }
306

307 308
    /* Allocate callback list */
    if (VIR_ALLOC(cbList) < 0) {
309
        virReportOOMError();
D
Daniel P. Berrange 已提交
310 311
        virMutexDestroy(&priv->lock);
        VIR_FREE(priv);
312 313
        return VIR_DRV_OPEN_ERROR;
    }
D
Daniel P. Berrange 已提交
314 315
    conn->privateData = priv;

316 317
    priv->domainEventCallbacks = cbList;

318 319 320 321 322 323
    priv->handle = -1;
    priv->xendConfigVersion = -1;
    priv->xshandle = NULL;
    priv->proxy = -1;


J
John Levon 已提交
324 325
    /* Hypervisor is only run with privilege & required to succeed */
    if (xenHavePrivilege()) {
326
        DEBUG0("Trying hypervisor sub-driver");
327
        if (drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->open(conn, auth, flags) ==
328 329 330
            VIR_DRV_OPEN_SUCCESS) {
            DEBUG0("Activated hypervisor sub-driver");
            priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] = 1;
331
        }
332
    }
333

J
John Levon 已提交
334
    /* XenD is required to succeed if privileged.
335
     * If it fails as non-root, then the proxy driver may take over
336 337
     */
    DEBUG0("Trying XenD sub-driver");
338
    if (drivers[XEN_UNIFIED_XEND_OFFSET]->open(conn, auth, flags) ==
339 340 341 342 343 344 345 346
        VIR_DRV_OPEN_SUCCESS) {
        DEBUG0("Activated XenD sub-driver");
        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 */
        if (priv->xendConfigVersion <= 2) {
            DEBUG0("Trying XM sub-driver");
347
            if (drivers[XEN_UNIFIED_XM_OFFSET]->open(conn, auth, flags) ==
348 349 350 351 352 353
                VIR_DRV_OPEN_SUCCESS) {
                DEBUG0("Activated XM sub-driver");
                priv->opened[XEN_UNIFIED_XM_OFFSET] = 1;
            }
        }
        DEBUG0("Trying XS sub-driver");
354
        if (drivers[XEN_UNIFIED_XS_OFFSET]->open(conn, auth, flags) ==
355 356 357 358
            VIR_DRV_OPEN_SUCCESS) {
            DEBUG0("Activated XS sub-driver");
            priv->opened[XEN_UNIFIED_XS_OFFSET] = 1;
        } else {
J
John Levon 已提交
359 360
            if (xenHavePrivilege())
                goto fail; /* XS is mandatory when privileged */
361 362
        }
    } else {
J
John Levon 已提交
363 364
        if (xenHavePrivilege()) {
            goto fail; /* XenD is mandatory when privileged */
365 366 367
        } else {
#if WITH_PROXY
            DEBUG0("Trying proxy sub-driver");
368
            if (drivers[XEN_UNIFIED_PROXY_OFFSET]->open(conn, auth, flags) ==
369 370 371 372 373 374 375 376
                VIR_DRV_OPEN_SUCCESS) {
                DEBUG0("Activated proxy sub-driver");
                priv->opened[XEN_UNIFIED_PROXY_OFFSET] = 1;
            } else {
                goto fail; /* Proxy is mandatory if XenD failed */
            }
#else
            DEBUG0("Handing off for remote driver");
377 378
            ret = VIR_DRV_OPEN_DECLINED; /* Let remote_driver try instead */
            goto clean;
379
#endif
380
        }
381 382
    }

D
Daniel P. Berrange 已提交
383 384
    xenNumaInit(conn);

385
    if (!(priv->caps = xenHypervisorMakeCapabilities(conn))) {
386 387 388 389
        DEBUG0("Failed to make capabilities");
        goto fail;
    }

390
#if WITH_XEN_INOTIFY
391 392 393 394 395 396 397
    if (xenHavePrivilege()) {
        DEBUG0("Trying Xen inotify sub-driver");
        if (drivers[XEN_UNIFIED_INOTIFY_OFFSET]->open(conn, auth, flags) ==
            VIR_DRV_OPEN_SUCCESS) {
            DEBUG0("Activated Xen inotify sub-driver");
            priv->opened[XEN_UNIFIED_INOTIFY_OFFSET] = 1;
        }
398 399 400
    }
#endif

401
    return VIR_DRV_OPEN_SUCCESS;
402

403 404
fail:
    ret = VIR_DRV_OPEN_ERROR;
405
#ifndef WITH_PROXY
406
clean:
407
#endif
408 409 410
    DEBUG0("Failed to activate a mandatory sub-driver");
    for (i = 0 ; i < XEN_UNIFIED_NR_DRIVERS ; i++)
        if (priv->opened[i]) drivers[i]->close(conn);
D
Daniel P. Berrange 已提交
411
    virMutexDestroy(&priv->lock);
412
    VIR_FREE(priv);
D
Daniel P. Berrange 已提交
413
    conn->privateData = NULL;
D
Daniel Veillard 已提交
414
    return ret;
415 416
}

417 418 419
#define GET_PRIVATE(conn) \
    xenUnifiedPrivatePtr priv = (xenUnifiedPrivatePtr) (conn)->privateData

420 421 422
static int
xenUnifiedClose (virConnectPtr conn)
{
423
    GET_PRIVATE(conn);
424
    int i;
425

426
    virCapabilitiesFree(priv->caps);
427 428
    virDomainEventCallbackListFree(priv->domainEventCallbacks);

429 430
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->close)
431
            (void) drivers[i]->close (conn);
432

D
Daniel P. Berrange 已提交
433 434
    virMutexDestroy(&priv->lock);
    VIR_FREE(conn->privateData);
435

436
    return 0;
437 438
}

439 440 441 442 443 444 445 446 447 448 449

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


450 451 452
static const char *
xenUnifiedType (virConnectPtr conn)
{
453
    GET_PRIVATE(conn);
454
    int i;
455

456
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
457 458
        if (priv->opened[i])
            return "Xen";
459

460
    return NULL;
461 462
}

463 464 465 466 467
/* Which features are supported by this driver? */
static int
xenUnifiedSupportsFeature (virConnectPtr conn ATTRIBUTE_UNUSED, int feature)
{
    switch (feature) {
468 469 470 471 472
    case VIR_DRV_FEATURE_MIGRATION_V1:
    case VIR_DRV_FEATURE_MIGRATION_DIRECT:
        return 1;
    default:
        return 0;
473 474 475
    }
}

476
static int
477
xenUnifiedGetVersion (virConnectPtr conn, unsigned long *hvVer)
478
{
479
    GET_PRIVATE(conn);
480
    int i;
481

482 483 484
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->version &&
485 486
            drivers[i]->version (conn, hvVer) == 0)
            return 0;
487

488
    return -1;
489 490
}

491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
static int
xenUnifiedIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return 0;
}

static int
xenUnifiedIsSecure(virConnectPtr conn)
{
    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;
}

511 512 513
static int
xenUnifiedGetMaxVcpus (virConnectPtr conn, const char *type)
{
514
    GET_PRIVATE(conn);
515

516
    if (type && STRCASENEQ (type, "Xen")) {
517
        xenUnifiedError(VIR_ERR_INVALID_ARG, __FUNCTION__);
518 519
        return -1;
    }
520

521 522 523
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET])
        return xenHypervisorGetMaxVcpus (conn, type);
    else {
524
        xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
525 526
        return -1;
    }
527 528 529 530 531
}

static int
xenUnifiedNodeGetInfo (virConnectPtr conn, virNodeInfoPtr info)
{
532
    GET_PRIVATE(conn);
533
    int i;
534

535 536 537
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->nodeGetInfo &&
538 539
            drivers[i]->nodeGetInfo (conn, info) == 0)
            return 0;
540

541
    return -1;
542 543 544 545 546
}

static char *
xenUnifiedGetCapabilities (virConnectPtr conn)
{
D
Daniel P. Berrange 已提交
547 548
    xenUnifiedPrivatePtr priv = conn->privateData;
    char *xml;
549

D
Daniel P. Berrange 已提交
550
    if (!(xml = virCapabilitiesFormatXML(priv->caps))) {
551
        virReportOOMError();
D
Daniel P. Berrange 已提交
552 553
        return NULL;
    }
554

D
Daniel P. Berrange 已提交
555
    return xml;
556 557 558 559 560
}

static int
xenUnifiedListDomains (virConnectPtr conn, int *ids, int maxids)
{
561
    GET_PRIVATE(conn);
562
    int ret;
563

564 565 566 567 568
    /* Try xenstore. */
    if (priv->opened[XEN_UNIFIED_XS_OFFSET]) {
        ret = xenStoreListDomains (conn, ids, maxids);
        if (ret >= 0) return ret;
    }
569

570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
    /* Try HV. */
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET]) {
        ret = xenHypervisorListDomains (conn, ids, maxids);
        if (ret >= 0) return ret;
    }

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

    /* Try proxy. */
    if (priv->opened[XEN_UNIFIED_PROXY_OFFSET]) {
        ret = xenProxyListDomains (conn, ids, maxids);
        if (ret >= 0) return ret;
    }
587
    return -1;
588 589 590 591 592
}

static int
xenUnifiedNumOfDomains (virConnectPtr conn)
{
593
    GET_PRIVATE(conn);
594
    int ret;
595

596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
    /* Try xenstore. */
    if (priv->opened[XEN_UNIFIED_XS_OFFSET]) {
        ret = xenStoreNumOfDomains (conn);
        if (ret >= 0) return ret;
    }

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

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

    /* Try proxy. */
    if (priv->opened[XEN_UNIFIED_PROXY_OFFSET]) {
        ret = xenProxyNumOfDomains (conn);
        if (ret >= 0) return ret;
    }
619

620
    return -1;
621 622 623
}

static virDomainPtr
624 625
xenUnifiedDomainCreateXML (virConnectPtr conn,
                           const char *xmlDesc, unsigned int flags)
626
{
627
    GET_PRIVATE(conn);
628 629
    int i;
    virDomainPtr ret;
630

631
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
632 633
        if (priv->opened[i] && drivers[i]->domainCreateXML) {
            ret = drivers[i]->domainCreateXML (conn, xmlDesc, flags);
634 635
            if (ret) return ret;
        }
636

637
    return NULL;
638 639
}

640 641 642 643
/* 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.
 */
644 645 646
static virDomainPtr
xenUnifiedDomainLookupByID (virConnectPtr conn, int id)
{
647
    GET_PRIVATE(conn);
648
    virDomainPtr ret;
649

650 651 652 653 654
    /* Reset any connection-level errors in virterror first, in case
     * there is one hanging around from a previous call.
     */
    virConnResetLastError (conn);

655 656 657 658 659 660 661
    /* Try hypervisor/xenstore combo. */
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET]) {
        ret = xenHypervisorLookupDomainByID (conn, id);
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }

662 663 664 665 666 667 668 669 670 671 672 673 674
    /* Try proxy. */
    if (priv->opened[XEN_UNIFIED_PROXY_OFFSET]) {
        ret = xenProxyLookupByID (conn, id);
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }

    /* Try xend. */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
        ret = xenDaemonLookupByID (conn, id);
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }
675

676
    /* Not found. */
677
    xenUnifiedError(VIR_ERR_NO_DOMAIN, __FUNCTION__);
678
    return NULL;
679 680 681 682
}

static virDomainPtr
xenUnifiedDomainLookupByUUID (virConnectPtr conn,
683
                              const unsigned char *uuid)
684
{
685
    GET_PRIVATE(conn);
686
    virDomainPtr ret;
687

688 689 690 691 692
    /* Reset any connection-level errors in virterror first, in case
     * there is one hanging around from a previous call.
     */
    virConnResetLastError (conn);

693 694 695 696 697 698 699
    /* Try hypervisor/xenstore combo. */
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET]) {
        ret = xenHypervisorLookupDomainByUUID (conn, uuid);
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }

700 701 702 703 704 705 706 707 708 709 710 711 712
    /* Try proxy. */
    if (priv->opened[XEN_UNIFIED_PROXY_OFFSET]) {
        ret = xenProxyLookupByUUID (conn, uuid);
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }

    /* Try xend. */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
        ret = xenDaemonLookupByUUID (conn, uuid);
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }
713

714 715 716 717 718 719 720 721
    /* Try XM for inactive domains. */
    if (priv->opened[XEN_UNIFIED_XM_OFFSET]) {
        ret = xenXMDomainLookupByUUID (conn, uuid);
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }

    /* Not found. */
722
    xenUnifiedError(VIR_ERR_NO_DOMAIN, __FUNCTION__);
723
    return NULL;
724 725 726 727
}

static virDomainPtr
xenUnifiedDomainLookupByName (virConnectPtr conn,
728
                              const char *name)
729
{
730
    GET_PRIVATE(conn);
731
    virDomainPtr ret;
732

733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
    /* Reset any connection-level errors in virterror first, in case
     * there is one hanging around from a previous call.
     */
    virConnResetLastError (conn);

    /* Try proxy. */
    if (priv->opened[XEN_UNIFIED_PROXY_OFFSET]) {
        ret = xenProxyLookupByName (conn, name);
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }

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

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

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

766
    /* Not found. */
767
    xenUnifiedError(VIR_ERR_NO_DOMAIN, __FUNCTION__);
768
    return NULL;
769 770
}

771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820

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
xenUnifiedDomainisPersistent(virDomainPtr dom)
{
    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) {
821
                        virReportOOMError();
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
                        goto done;
                    }
                    if (access(path, R_OK) == 0)
                        ret = 1;
                    else if (errno == ENOENT)
                        ret = 0;
                }
            }
        }
    }

done:
    if (currdom)
        virDomainFree(currdom);

    return ret;
}

840 841 842
static int
xenUnifiedDomainSuspend (virDomainPtr dom)
{
843
    GET_PRIVATE(dom->conn);
844
    int i;
845

846 847 848
    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
849
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
850
        if (i != XEN_UNIFIED_HYPERVISOR_OFFSET &&
851
            priv->opened[i] &&
852 853 854
            drivers[i]->domainSuspend &&
            drivers[i]->domainSuspend (dom) == 0)
            return 0;
855

856 857 858
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] &&
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainSuspend &&
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainSuspend (dom) == 0)
859
        return 0;
860

861
    return -1;
862 863 864 865 866
}

static int
xenUnifiedDomainResume (virDomainPtr dom)
{
867
    GET_PRIVATE(dom->conn);
868
    int i;
869

870 871 872
    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
873
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
874
        if (i != XEN_UNIFIED_HYPERVISOR_OFFSET &&
875
            priv->opened[i] &&
876 877 878
            drivers[i]->domainResume &&
            drivers[i]->domainResume (dom) == 0)
            return 0;
879

880 881 882
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] &&
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainResume &&
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainResume (dom) == 0)
883
        return 0;
884

885
    return -1;
886 887 888 889 890
}

static int
xenUnifiedDomainShutdown (virDomainPtr dom)
{
891
    GET_PRIVATE(dom->conn);
892
    int i;
893

894 895 896
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainShutdown &&
897 898
            drivers[i]->domainShutdown (dom) == 0)
            return 0;
899

900
    return -1;
901 902 903 904 905
}

static int
xenUnifiedDomainReboot (virDomainPtr dom, unsigned int flags)
{
906
    GET_PRIVATE(dom->conn);
907
    int i;
908

909 910 911
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainReboot &&
912 913
            drivers[i]->domainReboot (dom, flags) == 0)
            return 0;
914

915
    return -1;
916 917 918 919 920
}

static int
xenUnifiedDomainDestroy (virDomainPtr dom)
{
921
    GET_PRIVATE(dom->conn);
922
    int i;
923

924 925 926
    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
927
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
928
        if (i != XEN_UNIFIED_HYPERVISOR_OFFSET &&
929
            priv->opened[i] &&
930 931 932
            drivers[i]->domainDestroy &&
            drivers[i]->domainDestroy (dom) == 0)
            return 0;
933

934
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] &&
935 936
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainDestroy &&
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainDestroy (dom) == 0)
937
        return 0;
938

939
    return -1;
940 941 942 943 944
}

static char *
xenUnifiedDomainGetOSType (virDomainPtr dom)
{
945
    GET_PRIVATE(dom->conn);
946 947
    int i;
    char *ret;
948

949 950
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainGetOSType) {
951 952 953
            ret = drivers[i]->domainGetOSType (dom);
            if (ret) return ret;
        }
954

955
    return NULL;
956 957 958 959 960
}

static unsigned long
xenUnifiedDomainGetMaxMemory (virDomainPtr dom)
{
961
    GET_PRIVATE(dom->conn);
962 963
    int i;
    unsigned long ret;
964

965 966
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainGetMaxMemory) {
967 968 969
            ret = drivers[i]->domainGetMaxMemory (dom);
            if (ret != 0) return ret;
        }
970

971
    return 0;
972 973 974 975 976
}

static int
xenUnifiedDomainSetMaxMemory (virDomainPtr dom, unsigned long memory)
{
977
    GET_PRIVATE(dom->conn);
978
    int i;
979

980 981 982 983 984 985
    /* Prefer xend for setting max memory */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
        if (xenDaemonDomainSetMaxMemory (dom, memory) == 0)
            return 0;
    }

986
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
987 988
        if (i != XEN_UNIFIED_XEND_OFFSET &&
            priv->opened[i] &&
989
            drivers[i]->domainSetMaxMemory &&
990 991
            drivers[i]->domainSetMaxMemory (dom, memory) == 0)
            return 0;
992

993
    return -1;
994 995 996 997 998
}

static int
xenUnifiedDomainSetMemory (virDomainPtr dom, unsigned long memory)
{
999
    GET_PRIVATE(dom->conn);
1000
    int i;
1001

1002 1003 1004
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainSetMemory &&
1005 1006
            drivers[i]->domainSetMemory (dom, memory) == 0)
            return 0;
1007

1008
    return -1;
1009 1010 1011 1012 1013
}

static int
xenUnifiedDomainGetInfo (virDomainPtr dom, virDomainInfoPtr info)
{
1014
    GET_PRIVATE(dom->conn);
1015
    int i;
1016

1017 1018 1019
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainGetInfo &&
1020 1021
            drivers[i]->domainGetInfo (dom, info) == 0)
            return 0;
1022

1023
    return -1;
1024 1025 1026 1027 1028
}

static int
xenUnifiedDomainSave (virDomainPtr dom, const char *to)
{
1029
    GET_PRIVATE(dom->conn);
1030
    int i;
1031

1032 1033 1034
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainSave &&
1035 1036
            drivers[i]->domainSave (dom, to) == 0)
            return 0;
1037

1038
    return -1;
1039 1040 1041 1042 1043
}

static int
xenUnifiedDomainRestore (virConnectPtr conn, const char *from)
{
1044
    GET_PRIVATE(conn);
1045
    int i;
1046

1047 1048 1049
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainRestore &&
1050 1051
            drivers[i]->domainRestore (conn, from) == 0)
            return 0;
1052

1053
    return -1;
1054 1055 1056 1057 1058
}

static int
xenUnifiedDomainCoreDump (virDomainPtr dom, const char *to, int flags)
{
1059
    GET_PRIVATE(dom->conn);
1060
    int i;
1061

1062 1063 1064
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainCoreDump &&
1065 1066
            drivers[i]->domainCoreDump (dom, to, flags) == 0)
            return 0;
1067

1068
    return -1;
1069 1070 1071 1072 1073
}

static int
xenUnifiedDomainSetVcpus (virDomainPtr dom, unsigned int nvcpus)
{
1074
    GET_PRIVATE(dom->conn);
1075
    int i;
1076

1077 1078 1079
    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
1080
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
1081
        if (i != XEN_UNIFIED_HYPERVISOR_OFFSET &&
1082
            priv->opened[i] &&
1083 1084 1085
            drivers[i]->domainSetVcpus &&
            drivers[i]->domainSetVcpus (dom, nvcpus) == 0)
            return 0;
1086

1087 1088 1089
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] &&
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainSetVcpus &&
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainSetVcpus (dom, nvcpus) == 0)
1090
        return 0;
1091

1092
    return -1;
1093 1094 1095 1096
}

static int
xenUnifiedDomainPinVcpu (virDomainPtr dom, unsigned int vcpu,
1097
                         unsigned char *cpumap, int maplen)
1098
{
1099
    GET_PRIVATE(dom->conn);
1100
    int i;
1101

1102 1103 1104
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainPinVcpu &&
1105 1106
            drivers[i]->domainPinVcpu (dom, vcpu, cpumap, maplen) == 0)
            return 0;
1107

1108
    return -1;
1109 1110 1111 1112
}

static int
xenUnifiedDomainGetVcpus (virDomainPtr dom,
1113 1114
                          virVcpuInfoPtr info, int maxinfo,
                          unsigned char *cpumaps, int maplen)
1115
{
1116
    GET_PRIVATE(dom->conn);
1117
    int i, ret;
1118

1119 1120
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainGetVcpus) {
1121 1122 1123 1124
            ret = drivers[i]->domainGetVcpus (dom, info, maxinfo, cpumaps, maplen);
            if (ret > 0)
                return ret;
        }
1125
    return -1;
1126 1127 1128 1129 1130
}

static int
xenUnifiedDomainGetMaxVcpus (virDomainPtr dom)
{
1131
    GET_PRIVATE(dom->conn);
1132
    int i, ret;
1133

1134 1135
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainGetMaxVcpus) {
1136 1137 1138
            ret = drivers[i]->domainGetMaxVcpus (dom);
            if (ret != 0) return ret;
        }
1139

1140
    return -1;
1141 1142 1143 1144 1145
}

static char *
xenUnifiedDomainDumpXML (virDomainPtr dom, int flags)
{
1146
    GET_PRIVATE(dom->conn);
1147

1148 1149 1150 1151 1152 1153
    if (dom->id == -1 && priv->xendConfigVersion < 3 ) {
        if (priv->opened[XEN_UNIFIED_XM_OFFSET])
            return xenXMDomainDumpXML(dom, flags);
    } else {
        if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
            char *cpus, *res;
D
Daniel P. Berrange 已提交
1154
            xenUnifiedLock(priv);
1155
            cpus = xenDomainUsedCpus(dom);
D
Daniel P. Berrange 已提交
1156
            xenUnifiedUnlock(priv);
1157
            res = xenDaemonDomainDumpXML(dom, flags, cpus);
1158
            VIR_FREE(cpus);
1159
            return(res);
1160
        }
1161 1162
        if (priv->opened[XEN_UNIFIED_PROXY_OFFSET])
            return xenProxyDomainDumpXML(dom, flags);
1163
    }
1164

1165
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1166
    return NULL;
1167 1168
}

1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182

static char *
xenUnifiedDomainXMLFromNative(virConnectPtr conn,
                              const char *format,
                              const char *config,
                              unsigned int flags ATTRIBUTE_UNUSED)
{
    virDomainDefPtr def = NULL;
    char *ret = NULL;
    virConfPtr conf = NULL;
    GET_PRIVATE(conn);

    if (STRNEQ(format, XEN_CONFIG_FORMAT_XM) &&
        STRNEQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
1183
        xenUnifiedError(VIR_ERR_INVALID_ARG,
1184 1185 1186 1187 1188
                        _("unsupported config type %s"), format);
        return NULL;
    }

    if (STREQ(format, XEN_CONFIG_FORMAT_XM)) {
1189
        conf = virConfReadMem(config, strlen(config), 0);
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
        if (!conf)
            goto cleanup;

        def = xenXMDomainConfigParse(conn, conf);
    } else if (STREQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
        def = xenDaemonParseSxprString(conn, config, priv->xendConfigVersion);
    }
    if (!def)
        goto cleanup;

1200
    ret = virDomainDefFormat(def, 0);
1201 1202 1203

cleanup:
    virDomainDefFree(def);
1204 1205
    if (conf)
        virConfFree(conf);
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
    return ret;
}


#define MAX_CONFIG_SIZE (1024 * 65)
static char *
xenUnifiedDomainXMLToNative(virConnectPtr conn,
                            const char *format,
                            const char *xmlData,
                            unsigned int flags ATTRIBUTE_UNUSED)
{
    virDomainDefPtr def = NULL;
    char *ret = NULL;
    virConfPtr conf = NULL;
    GET_PRIVATE(conn);

    if (STRNEQ(format, XEN_CONFIG_FORMAT_XM) &&
        STRNEQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
1224
        xenUnifiedError(VIR_ERR_INVALID_ARG,
1225 1226 1227 1228
                        _("unsupported config type %s"), format);
        goto cleanup;
    }

1229
    if (!(def = virDomainDefParseString(priv->caps,
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
                                        xmlData,
                                        0)))
        goto cleanup;

    if (STREQ(format, XEN_CONFIG_FORMAT_XM)) {
        int len = MAX_CONFIG_SIZE;
        conf = xenXMDomainConfigFormat(conn, def);
        if (!conf)
            goto cleanup;

        if (VIR_ALLOC_N(ret, len) < 0) {
1241
            virReportOOMError();
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
            goto cleanup;
        }

        if (virConfWriteMem(ret, &len, conf) < 0) {
            VIR_FREE(ret);
            goto cleanup;
        }
    } else if (STREQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
        ret = xenDaemonFormatSxpr(conn, def, priv->xendConfigVersion);
    }

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


1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
static int
xenUnifiedDomainMigratePrepare (virConnectPtr dconn,
                                char **cookie,
                                int *cookielen,
                                const char *uri_in,
                                char **uri_out,
                                unsigned long flags,
                                const char *dname,
                                unsigned long resource)
{
    GET_PRIVATE(dconn);

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

1278
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
    return -1;
}

static int
xenUnifiedDomainMigratePerform (virDomainPtr dom,
                                const char *cookie,
                                int cookielen,
                                const char *uri,
                                unsigned long flags,
                                const char *dname,
                                unsigned long resource)
{
    GET_PRIVATE(dom->conn);

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

1297
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1298 1299 1300 1301 1302 1303 1304 1305 1306
    return -1;
}

static virDomainPtr
xenUnifiedDomainMigrateFinish (virConnectPtr dconn,
                               const char *dname,
                               const char *cookie ATTRIBUTE_UNUSED,
                               int cookielen ATTRIBUTE_UNUSED,
                               const char *uri ATTRIBUTE_UNUSED,
1307
                               unsigned long flags)
1308
{
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
    virDomainPtr dom = NULL;
    char *domain_xml = NULL;
    virDomainPtr dom_new = NULL;

    dom = xenUnifiedDomainLookupByName (dconn, dname);
    if (! dom) {
        return NULL;
    }

    if (flags & VIR_MIGRATE_PERSIST_DEST) {
        domain_xml = xenDaemonDomainDumpXML (dom, 0, NULL);
        if (! domain_xml) {
1321
            xenUnifiedError(VIR_ERR_MIGRATE_PERSIST_FAILED,
1322
                            "%s", _("failed to get XML representation of migrated domain"));
1323 1324 1325 1326 1327
            goto failure;
        }

        dom_new = xenDaemonDomainDefineXML (dconn, domain_xml);
        if (! dom_new) {
1328
            xenUnifiedError(VIR_ERR_MIGRATE_PERSIST_FAILED,
1329
                            "%s", _("failed to define domain on destination host"));
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
            goto failure;
        }

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

    VIR_FREE (domain_xml);

    return dom;


failure:
    virDomainFree (dom);

    VIR_FREE (domain_xml);

    return NULL;
1348 1349
}

1350 1351
static int
xenUnifiedListDefinedDomains (virConnectPtr conn, char **const names,
1352
                              int maxnames)
1353
{
1354
    GET_PRIVATE(conn);
1355 1356
    int i;
    int ret;
1357

1358 1359
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->listDefinedDomains) {
1360 1361 1362
            ret = drivers[i]->listDefinedDomains (conn, names, maxnames);
            if (ret >= 0) return ret;
        }
1363

1364
    return -1;
1365 1366 1367 1368 1369
}

static int
xenUnifiedNumOfDefinedDomains (virConnectPtr conn)
{
1370
    GET_PRIVATE(conn);
1371 1372
    int i;
    int ret;
1373

1374 1375
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->numOfDefinedDomains) {
1376 1377 1378
            ret = drivers[i]->numOfDefinedDomains (conn);
            if (ret >= 0) return ret;
        }
1379

1380
    return -1;
1381 1382 1383
}

static int
1384
xenUnifiedDomainCreateWithFlags (virDomainPtr dom, unsigned int flags)
1385
{
1386
    GET_PRIVATE(dom->conn);
1387
    int i;
1388

1389 1390
    virCheckFlags(0, -1);

1391 1392
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainCreate &&
1393 1394
            drivers[i]->domainCreate (dom) == 0)
            return 0;
1395

1396
    return -1;
1397 1398
}

1399 1400 1401 1402 1403 1404
static int
xenUnifiedDomainCreate (virDomainPtr dom)
{
    return xenUnifiedDomainCreateWithFlags(dom, 0);
}

1405 1406 1407
static virDomainPtr
xenUnifiedDomainDefineXML (virConnectPtr conn, const char *xml)
{
1408
    GET_PRIVATE(conn);
1409 1410
    int i;
    virDomainPtr ret;
1411

1412 1413
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainDefineXML) {
1414 1415 1416
            ret = drivers[i]->domainDefineXML (conn, xml);
            if (ret) return ret;
        }
1417

1418
    return NULL;
1419 1420 1421 1422 1423
}

static int
xenUnifiedDomainUndefine (virDomainPtr dom)
{
1424
    GET_PRIVATE(dom->conn);
1425
    int i;
1426

1427 1428
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainUndefine &&
1429 1430
            drivers[i]->domainUndefine (dom) == 0)
            return 0;
1431

1432
    return -1;
1433 1434 1435
}

static int
1436
xenUnifiedDomainAttachDevice (virDomainPtr dom, const char *xml)
1437 1438 1439
{
    GET_PRIVATE(dom->conn);
    int i;
1440
    unsigned int flags = VIR_DOMAIN_DEVICE_MODIFY_LIVE;
1441

1442 1443 1444 1445 1446 1447 1448 1449
    /*
     * 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] &&
        priv->xendConfigVersion >= 3)
        flags |= VIR_DOMAIN_DEVICE_MODIFY_CONFIG;
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461

    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainAttachDeviceFlags &&
            drivers[i]->domainAttachDeviceFlags(dom, xml, flags) == 0)
            return 0;

    return -1;
}

static int
xenUnifiedDomainAttachDeviceFlags (virDomainPtr dom, const char *xml,
                                   unsigned int flags)
1462
{
1463
    GET_PRIVATE(dom->conn);
1464
    int i;
1465

1466
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
1467 1468
        if (priv->opened[i] && drivers[i]->domainAttachDeviceFlags &&
            drivers[i]->domainAttachDeviceFlags(dom, xml, flags) == 0)
1469
            return 0;
1470

1471
    return -1;
1472 1473 1474
}

static int
1475
xenUnifiedDomainDetachDevice (virDomainPtr dom, const char *xml)
1476 1477 1478
{
    GET_PRIVATE(dom->conn);
    int i;
1479
    unsigned int flags = VIR_DOMAIN_DEVICE_MODIFY_LIVE;
1480

1481 1482 1483 1484 1485 1486 1487 1488
    /*
     * 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] &&
        priv->xendConfigVersion >= 3)
        flags |= VIR_DOMAIN_DEVICE_MODIFY_CONFIG;
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500

    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainDetachDeviceFlags &&
            drivers[i]->domainDetachDeviceFlags(dom, xml, flags) == 0)
            return 0;

    return -1;
}

static int
xenUnifiedDomainDetachDeviceFlags (virDomainPtr dom, const char *xml,
                                   unsigned int flags)
1501
{
1502
    GET_PRIVATE(dom->conn);
1503
    int i;
1504

1505
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
1506 1507
        if (priv->opened[i] && drivers[i]->domainDetachDeviceFlags &&
            drivers[i]->domainDetachDeviceFlags(dom, xml, flags) == 0)
1508
            return 0;
1509

1510
    return -1;
1511 1512
}

1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
static int
xenUnifiedDomainUpdateDeviceFlags (virDomainPtr dom, const char *xml,
                                   unsigned int flags)
{
    GET_PRIVATE(dom->conn);
    int i;

    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainUpdateDeviceFlags &&
            drivers[i]->domainUpdateDeviceFlags(dom, xml, flags) == 0)
            return 0;

    return -1;
}

1528 1529 1530 1531 1532
static int
xenUnifiedDomainGetAutostart (virDomainPtr dom, int *autostart)
{
    GET_PRIVATE(dom->conn);

1533 1534 1535 1536 1537 1538 1539
    if (priv->xendConfigVersion < 3) {
        if (priv->opened[XEN_UNIFIED_XM_OFFSET])
            return xenXMDomainGetAutostart(dom, autostart);
    } else {
        if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
            return xenDaemonDomainGetAutostart(dom, autostart);
    }
1540

1541
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1542 1543 1544 1545 1546 1547 1548 1549
    return -1;
}

static int
xenUnifiedDomainSetAutostart (virDomainPtr dom, int autostart)
{
    GET_PRIVATE(dom->conn);

1550 1551 1552 1553 1554 1555 1556
    if (priv->xendConfigVersion < 3) {
        if (priv->opened[XEN_UNIFIED_XM_OFFSET])
            return xenXMDomainSetAutostart(dom, autostart);
    } else {
        if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
            return xenDaemonDomainSetAutostart(dom, autostart);
    }
1557

1558
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1559 1560 1561
    return -1;
}

1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
static char *
xenUnifiedDomainGetSchedulerType (virDomainPtr dom, int *nparams)
{
    GET_PRIVATE(dom->conn);
    int i;
    char *schedulertype;

    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; i++) {
        if (priv->opened[i] && drivers[i]->domainGetSchedulerType) {
            schedulertype = drivers[i]->domainGetSchedulerType (dom, nparams);
1572 1573
            if (schedulertype != NULL)
                return(schedulertype);
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
        }
    }
    return(NULL);
}

static int
xenUnifiedDomainGetSchedulerParameters (virDomainPtr dom,
                    virSchedParameterPtr params, int *nparams)
{
    GET_PRIVATE(dom->conn);
    int i, ret;

    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i) {
        if (priv->opened[i] && drivers[i]->domainGetSchedulerParameters) {
           ret = drivers[i]->domainGetSchedulerParameters(dom, params, nparams);
1589 1590 1591
           if (ret == 0)
               return(0);
        }
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
    }
    return(-1);
}

static int
xenUnifiedDomainSetSchedulerParameters (virDomainPtr dom,
                    virSchedParameterPtr params, int nparams)
{
    GET_PRIVATE(dom->conn);
    int i, ret;

1603 1604
    /* do the hypervisor call last to get better error */
    for (i = XEN_UNIFIED_NR_DRIVERS - 1; i >= 0; i--) {
1605 1606
        if (priv->opened[i] && drivers[i]->domainSetSchedulerParameters) {
           ret = drivers[i]->domainSetSchedulerParameters(dom, params, nparams);
1607 1608 1609
           if (ret == 0)
               return 0;
        }
1610 1611 1612 1613 1614
    }

    return(-1);
}

1615 1616 1617 1618 1619 1620 1621 1622 1623
static int
xenUnifiedDomainBlockStats (virDomainPtr dom, const char *path,
                            struct _virDomainBlockStats *stats)
{
    GET_PRIVATE (dom->conn);

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

1624
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
    return -1;
}

static int
xenUnifiedDomainInterfaceStats (virDomainPtr dom, const char *path,
                                struct _virDomainInterfaceStats *stats)
{
    GET_PRIVATE (dom->conn);

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

1637
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1638 1639 1640
    return -1;
}

R
Richard W.M. Jones 已提交
1641 1642 1643
static int
xenUnifiedDomainBlockPeek (virDomainPtr dom, const char *path,
                           unsigned long long offset, size_t size,
1644
                           void *buffer, unsigned int flags ATTRIBUTE_UNUSED)
R
Richard W.M. Jones 已提交
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
{
    int r;
    GET_PRIVATE (dom->conn);

    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
        r = xenDaemonDomainBlockPeek (dom, path, offset, size, buffer);
        if (r != -2) return r;
        /* r == -2 means declined, so fall through to XM driver ... */
    }

    if (priv->opened[XEN_UNIFIED_XM_OFFSET]) {
        if (xenXMDomainBlockPeek (dom, path, offset, size, buffer) == 0)
            return 0;
    }

1660
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
R
Richard W.M. Jones 已提交
1661 1662 1663
    return -1;
}

1664 1665 1666 1667 1668 1669 1670
static int
xenUnifiedNodeGetCellsFreeMemory (virConnectPtr conn, unsigned long long *freeMems,
                                  int startCell, int maxCells)
{
    GET_PRIVATE (conn);

    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET])
1671
        return xenHypervisorNodeGetCellsFreeMemory (conn, freeMems,
1672 1673
                                                    startCell, maxCells);

1674
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1675 1676 1677
    return -1;
}

1678 1679 1680 1681 1682 1683 1684 1685
static unsigned long long
xenUnifiedNodeGetFreeMemory (virConnectPtr conn)
{
    unsigned long long freeMem = 0;
    int ret;
    GET_PRIVATE (conn);

    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET]) {
1686
        ret = xenHypervisorNodeGetCellsFreeMemory (conn, &freeMem,
1687
                                                    -1, 1);
1688 1689 1690
        if (ret != 1)
            return (0);
        return(freeMem);
1691 1692
    }

1693
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1694 1695 1696
    return(0);
}

1697

1698
static int
1699 1700 1701 1702
xenUnifiedDomainEventRegister(virConnectPtr conn,
                              virConnectDomainEventCallback callback,
                              void *opaque,
                              virFreeCallback freefunc)
1703
{
D
Daniel P. Berrange 已提交
1704 1705
    GET_PRIVATE (conn);

1706
    int ret;
D
Daniel P. Berrange 已提交
1707
    xenUnifiedLock(priv);
1708

1709
    if (priv->xsWatch == -1) {
1710
        xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
D
Daniel P. Berrange 已提交
1711
        xenUnifiedUnlock(priv);
1712 1713 1714
        return -1;
    }

1715 1716 1717
    ret = virDomainEventCallbackListAdd(conn, priv->domainEventCallbacks,
                                        callback, opaque, freefunc);

D
Daniel P. Berrange 已提交
1718
    xenUnifiedUnlock(priv);
1719
    return (ret);
1720 1721
}

1722

1723
static int
1724 1725
xenUnifiedDomainEventDeregister(virConnectPtr conn,
                                virConnectDomainEventCallback callback)
1726 1727 1728
{
    int ret;
    GET_PRIVATE (conn);
D
Daniel P. Berrange 已提交
1729 1730
    xenUnifiedLock(priv);

1731
    if (priv->xsWatch == -1) {
1732
        xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
D
Daniel P. Berrange 已提交
1733
        xenUnifiedUnlock(priv);
1734 1735 1736
        return -1;
    }

D
Daniel P. Berrange 已提交
1737 1738 1739 1740 1741 1742
    if (priv->domainEventDispatching)
        ret = virDomainEventCallbackListMarkDelete(conn, priv->domainEventCallbacks,
                                                   callback);
    else
        ret = virDomainEventCallbackListRemove(conn, priv->domainEventCallbacks,
                                               callback);
1743

D
Daniel P. Berrange 已提交
1744
    xenUnifiedUnlock(priv);
1745 1746 1747
    return ret;
}

1748

1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
static int
xenUnifiedDomainEventRegisterAny(virConnectPtr conn,
                                 virDomainPtr dom,
                                 int eventID,
                                 virConnectDomainEventGenericCallback callback,
                                 void *opaque,
                                 virFreeCallback freefunc)
{
    GET_PRIVATE (conn);

    int ret;
    xenUnifiedLock(priv);

    if (priv->xsWatch == -1) {
1763
        xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784
        xenUnifiedUnlock(priv);
        return -1;
    }

    ret = virDomainEventCallbackListAddID(conn, priv->domainEventCallbacks,
                                          dom, eventID,
                                          callback, opaque, freefunc);

    xenUnifiedUnlock(priv);
    return (ret);
}

static int
xenUnifiedDomainEventDeregisterAny(virConnectPtr conn,
                                   int callbackID)
{
    int ret;
    GET_PRIVATE (conn);
    xenUnifiedLock(priv);

    if (priv->xsWatch == -1) {
1785
        xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
        xenUnifiedUnlock(priv);
        return -1;
    }

    if (priv->domainEventDispatching)
        ret = virDomainEventCallbackListMarkDeleteID(conn, priv->domainEventCallbacks,
                                                     callbackID);
    else
        ret = virDomainEventCallbackListRemoveID(conn, priv->domainEventCallbacks,
                                                 callbackID);

    xenUnifiedUnlock(priv);
    return ret;
}


1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817
static int
xenUnifiedNodeDeviceGetPciInfo (virNodeDevicePtr dev,
                           unsigned *domain,
                           unsigned *bus,
                           unsigned *slot,
                           unsigned *function)
{
    virNodeDeviceDefPtr def = NULL;
    virNodeDevCapsDefPtr cap;
    char *xml = NULL;
    int ret = -1;

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

1818
    def = virNodeDeviceDefParseString(xml, EXISTING_DEVICE);
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835
    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) {
1836
        xenUnifiedError(VIR_ERR_INVALID_ARG,
1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857
                        _("device %s is not a PCI device"), dev->name);
        goto out;
    }

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

static int
xenUnifiedNodeDeviceDettach (virNodeDevicePtr dev)
{
    pciDevice *pci;
    unsigned domain, bus, slot, function;
    int ret = -1;

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

1858
    pci = pciGetDevice(domain, bus, slot, function);
1859 1860 1861
    if (!pci)
        return -1;

1862
    if (pciDettachDevice(pci, NULL) < 0)
1863 1864 1865 1866
        goto out;

    ret = 0;
out:
1867
    pciFreeDevice(pci);
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880
    return ret;
}

static int
xenUnifiedNodeDeviceReAttach (virNodeDevicePtr dev)
{
    pciDevice *pci;
    unsigned domain, bus, slot, function;
    int ret = -1;

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

1881
    pci = pciGetDevice(domain, bus, slot, function);
1882 1883 1884
    if (!pci)
        return -1;

1885
    if (pciReAttachDevice(pci, NULL) < 0)
1886 1887 1888 1889
        goto out;

    ret = 0;
out:
1890
    pciFreeDevice(pci);
1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
    return ret;
}

static int
xenUnifiedNodeDeviceReset (virNodeDevicePtr dev)
{
    pciDevice *pci;
    unsigned domain, bus, slot, function;
    int ret = -1;

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

1904
    pci = pciGetDevice(domain, bus, slot, function);
1905 1906 1907
    if (!pci)
        return -1;

1908
    if (pciResetDevice(pci, NULL, NULL) < 0)
1909 1910 1911 1912
        goto out;

    ret = 0;
out:
1913
    pciFreeDevice(pci);
1914 1915 1916 1917
    return ret;
}


E
Eric Blake 已提交
1918
/*----- Register with libvirt.c, and initialize Xen drivers. -----*/
1919 1920 1921

/* The interface which we export upwards to libvirt.c. */
static virDriver xenUnifiedDriver = {
1922 1923 1924 1925 1926 1927 1928
    VIR_DRV_XEN_UNIFIED,
    "Xen",
    xenUnifiedOpen, /* open */
    xenUnifiedClose, /* close */
    xenUnifiedSupportsFeature, /* supports_feature */
    xenUnifiedType, /* type */
    xenUnifiedGetVersion, /* version */
1929
    NULL, /* libvirtVersion (impl. in libvirt.c) */
1930
    virGetHostname, /* getHostname */
1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953
    xenUnifiedGetMaxVcpus, /* getMaxVcpus */
    xenUnifiedNodeGetInfo, /* nodeGetInfo */
    xenUnifiedGetCapabilities, /* getCapabilities */
    xenUnifiedListDomains, /* listDomains */
    xenUnifiedNumOfDomains, /* numOfDomains */
    xenUnifiedDomainCreateXML, /* domainCreateXML */
    xenUnifiedDomainLookupByID, /* domainLookupByID */
    xenUnifiedDomainLookupByUUID, /* domainLookupByUUID */
    xenUnifiedDomainLookupByName, /* domainLookupByName */
    xenUnifiedDomainSuspend, /* domainSuspend */
    xenUnifiedDomainResume, /* domainResume */
    xenUnifiedDomainShutdown, /* domainShutdown */
    xenUnifiedDomainReboot, /* domainReboot */
    xenUnifiedDomainDestroy, /* domainDestroy */
    xenUnifiedDomainGetOSType, /* domainGetOSType */
    xenUnifiedDomainGetMaxMemory, /* domainGetMaxMemory */
    xenUnifiedDomainSetMaxMemory, /* domainSetMaxMemory */
    xenUnifiedDomainSetMemory, /* domainSetMemory */
    xenUnifiedDomainGetInfo, /* domainGetInfo */
    xenUnifiedDomainSave, /* domainSave */
    xenUnifiedDomainRestore, /* domainRestore */
    xenUnifiedDomainCoreDump, /* domainCoreDump */
    xenUnifiedDomainSetVcpus, /* domainSetVcpus */
E
Eric Blake 已提交
1954 1955
    NULL, /* domainSetVcpusFlags */
    NULL, /* domainGetVcpusFlags */
1956 1957 1958 1959 1960 1961
    xenUnifiedDomainPinVcpu, /* domainPinVcpu */
    xenUnifiedDomainGetVcpus, /* domainGetVcpus */
    xenUnifiedDomainGetMaxVcpus, /* domainGetMaxVcpus */
    NULL, /* domainGetSecurityLabel */
    NULL, /* nodeGetSecurityModel */
    xenUnifiedDomainDumpXML, /* domainDumpXML */
1962 1963
    xenUnifiedDomainXMLFromNative, /* domainXMLFromNative */
    xenUnifiedDomainXMLToNative, /* domainXMLToNative */
1964 1965 1966
    xenUnifiedListDefinedDomains, /* listDefinedDomains */
    xenUnifiedNumOfDefinedDomains, /* numOfDefinedDomains */
    xenUnifiedDomainCreate, /* domainCreate */
1967
    xenUnifiedDomainCreateWithFlags, /* domainCreateWithFlags */
1968 1969 1970
    xenUnifiedDomainDefineXML, /* domainDefineXML */
    xenUnifiedDomainUndefine, /* domainUndefine */
    xenUnifiedDomainAttachDevice, /* domainAttachDevice */
1971
    xenUnifiedDomainAttachDeviceFlags, /* domainAttachDeviceFlags */
1972
    xenUnifiedDomainDetachDevice, /* domainDetachDevice */
1973
    xenUnifiedDomainDetachDeviceFlags, /* domainDetachDeviceFlags */
1974
    xenUnifiedDomainUpdateDeviceFlags, /* domainUpdateDeviceFlags */
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
    xenUnifiedDomainGetAutostart, /* domainGetAutostart */
    xenUnifiedDomainSetAutostart, /* domainSetAutostart */
    xenUnifiedDomainGetSchedulerType, /* domainGetSchedulerType */
    xenUnifiedDomainGetSchedulerParameters, /* domainGetSchedulerParameters */
    xenUnifiedDomainSetSchedulerParameters, /* domainSetSchedulerParameters */
    xenUnifiedDomainMigratePrepare, /* domainMigratePrepare */
    xenUnifiedDomainMigratePerform, /* domainMigratePerform */
    xenUnifiedDomainMigrateFinish, /* domainMigrateFinish */
    xenUnifiedDomainBlockStats, /* domainBlockStats */
    xenUnifiedDomainInterfaceStats, /* domainInterfaceStats */
1985
    NULL, /* domainMemoryStats */
1986 1987
    xenUnifiedDomainBlockPeek, /* domainBlockPeek */
    NULL, /* domainMemoryPeek */
1988
    NULL, /* domainGetBlockInfo */
1989 1990 1991 1992 1993 1994
    xenUnifiedNodeGetCellsFreeMemory, /* nodeGetCellsFreeMemory */
    xenUnifiedNodeGetFreeMemory, /* getFreeMemory */
    xenUnifiedDomainEventRegister, /* domainEventRegister */
    xenUnifiedDomainEventDeregister, /* domainEventDeregister */
    NULL, /* domainMigratePrepare2 */
    NULL, /* domainMigrateFinish2 */
1995 1996 1997
    xenUnifiedNodeDeviceDettach, /* nodeDeviceDettach */
    xenUnifiedNodeDeviceReAttach, /* nodeDeviceReAttach */
    xenUnifiedNodeDeviceReset, /* nodeDeviceReset */
C
Chris Lalancette 已提交
1998
    NULL, /* domainMigratePrepareTunnel */
1999 2000 2001 2002
    xenUnifiedIsEncrypted, /* isEncrypted */
    xenUnifiedIsSecure, /* isSecure */
    xenUnifiedDomainIsActive, /* domainIsActive */
    xenUnifiedDomainisPersistent, /* domainIsPersistent */
J
Jiri Denemark 已提交
2003
    NULL, /* cpuCompare */
2004
    NULL, /* cpuBaseline */
2005
    NULL, /* domainGetJobInfo */
2006
    NULL, /* domainAbortJob */
2007
    NULL, /* domainMigrateSetMaxDowntime */
2008 2009
    xenUnifiedDomainEventRegisterAny, /* domainEventRegisterAny */
    xenUnifiedDomainEventDeregisterAny, /* domainEventDeregisterAny */
2010 2011 2012
    NULL, /* domainManagedSave */
    NULL, /* domainHasManagedSaveImage */
    NULL, /* domainManagedSaveRemove */
C
Chris Lalancette 已提交
2013 2014 2015 2016 2017 2018 2019 2020 2021
    NULL, /* domainSnapshotCreateXML */
    NULL, /* domainSnapshotDumpXML */
    NULL, /* domainSnapshotNum */
    NULL, /* domainSnapshotListNames */
    NULL, /* domainSnapshotLookupByName */
    NULL, /* domainHasCurrentSnapshot */
    NULL, /* domainSnapshotCurrent */
    NULL, /* domainRevertToSnapshot */
    NULL, /* domainSnapshotDelete */
C
Chris Lalancette 已提交
2022
    NULL, /* qemuDomainMonitorCommand */
2023 2024
    NULL, /* domainSetMemoryParameters */
    NULL, /* domainGetMemoryParameters */
2025 2026
};

2027
/**
2028
 * xenRegister:
2029 2030 2031 2032 2033
 *
 * Register xen related drivers
 *
 * Returns the driver priority or -1 in case of error.
 */
2034
int
2035
xenRegister (void)
2036
{
2037 2038
    /* Ignore failures here. */
    (void) xenHypervisorInit ();
2039

J
John Levon 已提交
2040 2041 2042 2043
#ifdef WITH_LIBVIRTD
    if (virRegisterStateDriver (&state_driver) == -1) return -1;
#endif

2044
    return virRegisterDriver (&xenUnifiedDriver);
2045 2046
}

2047 2048 2049 2050 2051 2052 2053 2054 2055
/**
 * xenUnifiedDomainInfoListFree:
 *
 * Free the Domain Info List
 */
void
xenUnifiedDomainInfoListFree(xenUnifiedDomainInfoListPtr list)
{
    int i;
J
John Levon 已提交
2056 2057 2058 2059

    if (list == NULL)
        return;

2060 2061 2062 2063
    for (i=0; i<list->count; i++) {
        VIR_FREE(list->doms[i]->name);
        VIR_FREE(list->doms[i]);
    }
2064
    VIR_FREE(list->doms);
2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
    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)) {
            DEBUG0("WARNING: dom already tracked");
            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:
2110
    virReportOOMError();
2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155
    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++) {
        if( list->doms[i]->id == id &&
            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;
}

D
Daniel P. Berrange 已提交
2156 2157 2158
static void
xenUnifiedDomainEventDispatchFunc(virConnectPtr conn,
                                  virDomainEventPtr event,
2159
                                  virConnectDomainEventGenericCallback cb,
D
Daniel P. Berrange 已提交
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176
                                  void *cbopaque,
                                  void *opaque)
{
    xenUnifiedPrivatePtr priv = opaque;

    /*
     * Release the lock while the callback is running so that
     * we're re-entrant safe for callback work - the callback
     * may want to invoke other virt functions & we have already
     * protected the one piece of state we have - the callback
     * list
     */
    xenUnifiedUnlock(priv);
    virDomainEventDispatchDefaultFunc(conn, event, cb, cbopaque, NULL);
    xenUnifiedLock(priv);
}

2177 2178
/**
 * xenUnifiedDomainEventDispatch:
D
Daniel P. Berrange 已提交
2179 2180
 * @priv: the connection to dispatch events on
 * @event: the event to dispatch
2181 2182 2183
 *
 * Dispatch domain events to registered callbacks
 *
D
Daniel P. Berrange 已提交
2184 2185
 * The caller must hold the lock in 'priv' before invoking
 *
2186 2187
 */
void xenUnifiedDomainEventDispatch (xenUnifiedPrivatePtr priv,
2188
                                    virDomainEventPtr event)
2189
{
D
Daniel P. Berrange 已提交
2190
    if (!priv)
2191
        return;
2192

D
Daniel P. Berrange 已提交
2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
    priv->domainEventDispatching = 1;

    if (priv->domainEventCallbacks) {
        virDomainEventDispatch(event,
                               priv->domainEventCallbacks,
                               xenUnifiedDomainEventDispatchFunc,
                               priv);

        /* Purge any deleted callbacks */
        virDomainEventCallbackListPurgeMarked(priv->domainEventCallbacks);
    }

2205
    virDomainEventFree(event);
D
Daniel P. Berrange 已提交
2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217

    priv->domainEventDispatching = 0;
}

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

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