xen_driver.c 58.4 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
};

J
John Levon 已提交
73 74
static int inside_daemon;

75
#define xenUnifiedError(conn, code, ...)                                   \
76
        virReportErrorHelper(conn, VIR_FROM_XEN, code, __FILE__,           \
77
                               __FUNCTION__, __LINE__, __VA_ARGS__)
78

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

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

D
Daniel P. Berrange 已提交
97
    priv = conn->privateData;
98

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

D
Daniel P. Berrange 已提交
103

104 105 106 107 108 109 110 111 112 113 114 115 116 117
/**
 * 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 已提交
118
    int ncpus;
119 120 121 122 123 124 125 126
    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 已提交
127
    xenUnifiedPrivatePtr priv;
128 129 130 131

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

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

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

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

    if ((ncpus = xenUnifiedDomainGetVcpus(dom, cpuinfo, nb_vcpu,
                                          cpumap, cpumaplen)) >= 0) {
159
        for (n = 0 ; n < ncpus ; n++) {
D
Daniel P. Berrange 已提交
160
            for (m = 0 ; m < priv->nbNodeCpus; m++) {
161 162 163 164 165
                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 已提交
166
                    if (nb == priv->nbNodeCpus)
167 168 169 170 171
                        goto done;

                }
            }
        }
172
        res = virDomainCpuSetFormat(cpulist, priv->nbNodeCpus);
173 174 175
    }

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

J
John Levon 已提交
182 183 184
#ifdef WITH_LIBVIRTD

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

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

#endif

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

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

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

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

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

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

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

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
            /* 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] != '/') {
                xenUnifiedError(NULL, VIR_ERR_INTERNAL_ERROR,
                                _("unexpected Xen URI path '%s', try ///var/lib/xen/xend-socket"),
                                NULLSTR(conn->uri->path));
                return VIR_DRV_OPEN_ERROR;
            }
        }
    }
289

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

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

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

314 315
    priv->domainEventCallbacks = cbList;

316 317 318 319 320 321
    priv->handle = -1;
    priv->xendConfigVersion = -1;
    priv->xshandle = NULL;
    priv->proxy = -1;


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

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

D
Daniel P. Berrange 已提交
381 382
    xenNumaInit(conn);

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

388
#if WITH_XEN_INOTIFY
389 390 391 392 393 394 395
    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;
        }
396 397 398
    }
#endif

399
    return VIR_DRV_OPEN_SUCCESS;
400

401 402
fail:
    ret = VIR_DRV_OPEN_ERROR;
403
#ifndef WITH_PROXY
404
clean:
405
#endif
406 407 408
    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 已提交
409
    virMutexDestroy(&priv->lock);
410
    VIR_FREE(priv);
D
Daniel P. Berrange 已提交
411
    conn->privateData = NULL;
D
Daniel Veillard 已提交
412
    return ret;
413 414
}

415 416 417
#define GET_PRIVATE(conn) \
    xenUnifiedPrivatePtr priv = (xenUnifiedPrivatePtr) (conn)->privateData

418 419 420
static int
xenUnifiedClose (virConnectPtr conn)
{
421
    GET_PRIVATE(conn);
422
    int i;
423

424
    virCapabilitiesFree(priv->caps);
425 426
    virDomainEventCallbackListFree(priv->domainEventCallbacks);

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

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

434
    return 0;
435 436
}

437 438 439 440 441 442 443 444 445 446 447

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


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

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

458
    return NULL;
459 460
}

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

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

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

486
    return -1;
487 488
}

489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
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;
}

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

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

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

static int
xenUnifiedNodeGetInfo (virConnectPtr conn, virNodeInfoPtr info)
{
530
    GET_PRIVATE(conn);
531
    int i;
532

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

539
    return -1;
540 541 542 543 544
}

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

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

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

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

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

568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
    /* 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;
    }
585
    return -1;
586 587 588 589 590
}

static int
xenUnifiedNumOfDomains (virConnectPtr conn)
{
591
    GET_PRIVATE(conn);
592
    int ret;
593

594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
    /* 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;
    }
617

618
    return -1;
619 620 621
}

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

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

635
    return NULL;
636 637
}

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

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

653 654 655 656 657 658 659
    /* 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;
    }

660 661 662 663 664 665 666 667 668 669 670 671 672
    /* 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;
    }
673

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

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

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

691 692 693 694 695 696 697
    /* 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;
    }

698 699 700 701 702 703 704 705 706 707 708 709 710
    /* 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;
    }
711

712 713 714 715 716 717 718 719 720
    /* 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. */
    xenUnifiedError (conn, VIR_ERR_NO_DOMAIN, __FUNCTION__);
721
    return NULL;
722 723 724 725
}

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

731 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
    /* 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;
    }
763

764 765
    /* Not found. */
    xenUnifiedError (conn, VIR_ERR_NO_DOMAIN, __FUNCTION__);
766
    return NULL;
767 768
}

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

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

done:
    if (currdom)
        virDomainFree(currdom);

    return ret;
}

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

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

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

859
    return -1;
860 861 862 863 864
}

static int
xenUnifiedDomainResume (virDomainPtr dom)
{
865
    GET_PRIVATE(dom->conn);
866
    int i;
867

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

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

883
    return -1;
884 885 886 887 888
}

static int
xenUnifiedDomainShutdown (virDomainPtr dom)
{
889
    GET_PRIVATE(dom->conn);
890
    int i;
891

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

898
    return -1;
899 900 901 902 903
}

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

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

913
    return -1;
914 915 916 917 918
}

static int
xenUnifiedDomainDestroy (virDomainPtr dom)
{
919
    GET_PRIVATE(dom->conn);
920
    int i;
921

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

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

937
    return -1;
938 939 940 941 942
}

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

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

953
    return NULL;
954 955 956 957 958
}

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

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

969
    return 0;
970 971 972 973 974
}

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

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

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

991
    return -1;
992 993 994 995 996
}

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

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

1006
    return -1;
1007 1008 1009 1010 1011
}

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

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

1021
    return -1;
1022 1023 1024 1025 1026
}

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

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

1036
    return -1;
1037 1038 1039 1040 1041
}

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

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

1051
    return -1;
1052 1053 1054 1055 1056
}

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

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

1066
    return -1;
1067 1068 1069 1070 1071
}

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

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

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

1090
    return -1;
1091 1092 1093 1094
}

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

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

1106
    return -1;
1107 1108 1109 1110
}

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

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

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

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

1138
    return -1;
1139 1140 1141 1142 1143
}

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

1146 1147 1148 1149 1150 1151
    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 已提交
1152
            xenUnifiedLock(priv);
1153
            cpus = xenDomainUsedCpus(dom);
D
Daniel P. Berrange 已提交
1154
            xenUnifiedUnlock(priv);
1155
            res = xenDaemonDomainDumpXML(dom, flags, cpus);
1156
            VIR_FREE(cpus);
1157
            return(res);
1158
        }
1159 1160
        if (priv->opened[XEN_UNIFIED_PROXY_OFFSET])
            return xenProxyDomainDumpXML(dom, flags);
1161
    }
1162

1163
    xenUnifiedError (dom->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1164
    return NULL;
1165 1166
}

1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186

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)) {
        xenUnifiedError(conn, VIR_ERR_INVALID_ARG,
                        _("unsupported config type %s"), format);
        return NULL;
    }

    if (STREQ(format, XEN_CONFIG_FORMAT_XM)) {
1187
        conf = virConfReadMem(config, strlen(config), 0);
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
        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;

1198
    ret = virDomainDefFormat(def, 0);
1199 1200 1201

cleanup:
    virDomainDefFree(def);
1202 1203
    if (conf)
        virConfFree(conf);
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
    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)) {
        xenUnifiedError(conn, VIR_ERR_INVALID_ARG,
                        _("unsupported config type %s"), format);
        goto cleanup;
    }

1227
    if (!(def = virDomainDefParseString(priv->caps,
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
                                        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) {
1239
            virReportOOMError();
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
            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;
}


1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
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);

    xenUnifiedError (dconn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    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);

    xenUnifiedError (dom->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
}

static virDomainPtr
xenUnifiedDomainMigrateFinish (virConnectPtr dconn,
                               const char *dname,
                               const char *cookie ATTRIBUTE_UNUSED,
                               int cookielen ATTRIBUTE_UNUSED,
                               const char *uri ATTRIBUTE_UNUSED,
1305
                               unsigned long flags)
1306
{
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
    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) {
            xenUnifiedError(dconn, VIR_ERR_MIGRATE_PERSIST_FAILED,
1320
                            "%s", _("failed to get XML representation of migrated domain"));
1321 1322 1323 1324 1325
            goto failure;
        }

        dom_new = xenDaemonDomainDefineXML (dconn, domain_xml);
        if (! dom_new) {
1326 1327
            xenUnifiedError(dconn, VIR_ERR_MIGRATE_PERSIST_FAILED,
                            "%s", _("failed to define domain on destination host"));
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
            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;
1346 1347
}

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

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

1362
    return -1;
1363 1364 1365 1366 1367
}

static int
xenUnifiedNumOfDefinedDomains (virConnectPtr conn)
{
1368
    GET_PRIVATE(conn);
1369 1370
    int i;
    int ret;
1371

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

1378
    return -1;
1379 1380 1381 1382 1383
}

static int
xenUnifiedDomainCreate (virDomainPtr dom)
{
1384
    GET_PRIVATE(dom->conn);
1385
    int i;
1386

1387 1388
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainCreate &&
1389 1390
            drivers[i]->domainCreate (dom) == 0)
            return 0;
1391

1392
    return -1;
1393 1394 1395 1396 1397
}

static virDomainPtr
xenUnifiedDomainDefineXML (virConnectPtr conn, const char *xml)
{
1398
    GET_PRIVATE(conn);
1399 1400
    int i;
    virDomainPtr ret;
1401

1402 1403
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainDefineXML) {
1404 1405 1406
            ret = drivers[i]->domainDefineXML (conn, xml);
            if (ret) return ret;
        }
1407

1408
    return NULL;
1409 1410 1411 1412 1413
}

static int
xenUnifiedDomainUndefine (virDomainPtr dom)
{
1414
    GET_PRIVATE(dom->conn);
1415
    int i;
1416

1417 1418
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainUndefine &&
1419 1420
            drivers[i]->domainUndefine (dom) == 0)
            return 0;
1421

1422
    return -1;
1423 1424 1425
}

static int
1426
xenUnifiedDomainAttachDevice (virDomainPtr dom, const char *xml)
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
{
    GET_PRIVATE(dom->conn);
    int i;
    unsigned int flags = VIR_DOMAIN_DEVICE_MODIFY_CONFIG;

    if (dom->id >= 0)
        flags |= VIR_DOMAIN_DEVICE_MODIFY_LIVE;

    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)
1446
{
1447
    GET_PRIVATE(dom->conn);
1448
    int i;
1449

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

1455
    return -1;
1456 1457 1458
}

static int
1459
xenUnifiedDomainDetachDevice (virDomainPtr dom, const char *xml)
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
{
    GET_PRIVATE(dom->conn);
    int i;
    unsigned int flags = VIR_DOMAIN_DEVICE_MODIFY_CONFIG;

    if (dom->id >= 0)
        flags |= VIR_DOMAIN_DEVICE_MODIFY_LIVE;

    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)
1479
{
1480
    GET_PRIVATE(dom->conn);
1481
    int i;
1482

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

1488
    return -1;
1489 1490
}

1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
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;
}

1506 1507 1508 1509 1510
static int
xenUnifiedDomainGetAutostart (virDomainPtr dom, int *autostart)
{
    GET_PRIVATE(dom->conn);

1511 1512 1513 1514 1515 1516 1517
    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);
    }
1518

1519
    xenUnifiedError (dom->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1520 1521 1522 1523 1524 1525 1526 1527
    return -1;
}

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

1528 1529 1530 1531 1532 1533 1534
    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);
    }
1535

1536
    xenUnifiedError (dom->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1537 1538 1539
    return -1;
}

1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
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);
1550 1551
            if (schedulertype != NULL)
                return(schedulertype);
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
        }
    }
    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);
1567 1568 1569
           if (ret == 0)
               return(0);
        }
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
    }
    return(-1);
}

static int
xenUnifiedDomainSetSchedulerParameters (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]->domainSetSchedulerParameters) {
           ret = drivers[i]->domainSetSchedulerParameters(dom, params, nparams);
1584 1585 1586
           if (ret == 0)
               return 0;
        }
1587 1588 1589 1590 1591
    }

    return(-1);
}

1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
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);

    xenUnifiedError (dom->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    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);

    xenUnifiedError (dom->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
}

R
Richard W.M. Jones 已提交
1618 1619 1620
static int
xenUnifiedDomainBlockPeek (virDomainPtr dom, const char *path,
                           unsigned long long offset, size_t size,
1621
                           void *buffer, unsigned int flags ATTRIBUTE_UNUSED)
R
Richard W.M. Jones 已提交
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640
{
    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;
    }

    xenUnifiedError (dom->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
}

1641 1642 1643 1644 1645 1646 1647
static int
xenUnifiedNodeGetCellsFreeMemory (virConnectPtr conn, unsigned long long *freeMems,
                                  int startCell, int maxCells)
{
    GET_PRIVATE (conn);

    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET])
1648
        return xenHypervisorNodeGetCellsFreeMemory (conn, freeMems,
1649 1650 1651 1652 1653 1654
                                                    startCell, maxCells);

    xenUnifiedError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
}

1655 1656 1657 1658 1659 1660 1661 1662
static unsigned long long
xenUnifiedNodeGetFreeMemory (virConnectPtr conn)
{
    unsigned long long freeMem = 0;
    int ret;
    GET_PRIVATE (conn);

    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET]) {
1663
        ret = xenHypervisorNodeGetCellsFreeMemory (conn, &freeMem,
1664
                                                    -1, 1);
1665 1666 1667
        if (ret != 1)
            return (0);
        return(freeMem);
1668 1669 1670 1671 1672 1673
    }

    xenUnifiedError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return(0);
}

1674

1675
static int
1676 1677 1678 1679
xenUnifiedDomainEventRegister(virConnectPtr conn,
                              virConnectDomainEventCallback callback,
                              void *opaque,
                              virFreeCallback freefunc)
1680
{
D
Daniel P. Berrange 已提交
1681 1682
    GET_PRIVATE (conn);

1683
    int ret;
D
Daniel P. Berrange 已提交
1684
    xenUnifiedLock(priv);
1685

1686 1687
    if (priv->xsWatch == -1) {
        xenUnifiedError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
D
Daniel P. Berrange 已提交
1688
        xenUnifiedUnlock(priv);
1689 1690 1691
        return -1;
    }

1692 1693 1694
    ret = virDomainEventCallbackListAdd(conn, priv->domainEventCallbacks,
                                        callback, opaque, freefunc);

D
Daniel P. Berrange 已提交
1695
    xenUnifiedUnlock(priv);
1696
    return (ret);
1697 1698
}

1699

1700
static int
1701 1702
xenUnifiedDomainEventDeregister(virConnectPtr conn,
                                virConnectDomainEventCallback callback)
1703 1704 1705
{
    int ret;
    GET_PRIVATE (conn);
D
Daniel P. Berrange 已提交
1706 1707
    xenUnifiedLock(priv);

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

D
Daniel P. Berrange 已提交
1714 1715 1716 1717 1718 1719
    if (priv->domainEventDispatching)
        ret = virDomainEventCallbackListMarkDelete(conn, priv->domainEventCallbacks,
                                                   callback);
    else
        ret = virDomainEventCallbackListRemove(conn, priv->domainEventCallbacks,
                                               callback);
1720

D
Daniel P. Berrange 已提交
1721
    xenUnifiedUnlock(priv);
1722 1723 1724
    return ret;
}

1725

1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778
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) {
        xenUnifiedError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
        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) {
        xenUnifiedError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
        xenUnifiedUnlock(priv);
        return -1;
    }

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

    xenUnifiedUnlock(priv);
    return ret;
}


1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794
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;

1795
    def = virNodeDeviceDefParseString(xml, EXISTING_DEVICE);
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
    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) {
        xenUnifiedError(dev->conn, VIR_ERR_INVALID_ARG,
                        _("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;

1835
    pci = pciGetDevice(domain, bus, slot, function);
1836 1837 1838
    if (!pci)
        return -1;

1839
    if (pciDettachDevice(pci) < 0)
1840 1841 1842 1843
        goto out;

    ret = 0;
out:
1844
    pciFreeDevice(pci);
1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857
    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;

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

1862
    if (pciReAttachDevice(pci) < 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
xenUnifiedNodeDeviceReset (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 (pciResetDevice(pci, NULL) < 0)
1886 1887 1888 1889
        goto out;

    ret = 0;
out:
1890
    pciFreeDevice(pci);
1891 1892 1893 1894
    return ret;
}


1895 1896 1897 1898
/*----- Register with libvirt.c, and initialise Xen drivers. -----*/

/* The interface which we export upwards to libvirt.c. */
static virDriver xenUnifiedDriver = {
1899 1900 1901 1902 1903 1904 1905
    VIR_DRV_XEN_UNIFIED,
    "Xen",
    xenUnifiedOpen, /* open */
    xenUnifiedClose, /* close */
    xenUnifiedSupportsFeature, /* supports_feature */
    xenUnifiedType, /* type */
    xenUnifiedGetVersion, /* version */
1906
    NULL, /* libvirtVersion (impl. in libvirt.c) */
1907
    virGetHostname, /* getHostname */
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936
    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 */
    xenUnifiedDomainPinVcpu, /* domainPinVcpu */
    xenUnifiedDomainGetVcpus, /* domainGetVcpus */
    xenUnifiedDomainGetMaxVcpus, /* domainGetMaxVcpus */
    NULL, /* domainGetSecurityLabel */
    NULL, /* nodeGetSecurityModel */
    xenUnifiedDomainDumpXML, /* domainDumpXML */
1937 1938
    xenUnifiedDomainXMLFromNative, /* domainXMLFromNative */
    xenUnifiedDomainXMLToNative, /* domainXMLToNative */
1939 1940 1941 1942 1943 1944
    xenUnifiedListDefinedDomains, /* listDefinedDomains */
    xenUnifiedNumOfDefinedDomains, /* numOfDefinedDomains */
    xenUnifiedDomainCreate, /* domainCreate */
    xenUnifiedDomainDefineXML, /* domainDefineXML */
    xenUnifiedDomainUndefine, /* domainUndefine */
    xenUnifiedDomainAttachDevice, /* domainAttachDevice */
1945
    xenUnifiedDomainAttachDeviceFlags, /* domainAttachDeviceFlags */
1946
    xenUnifiedDomainDetachDevice, /* domainDetachDevice */
1947
    xenUnifiedDomainDetachDeviceFlags, /* domainDetachDeviceFlags */
1948
    xenUnifiedDomainUpdateDeviceFlags, /* domainUpdateDeviceFlags */
1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
    xenUnifiedDomainGetAutostart, /* domainGetAutostart */
    xenUnifiedDomainSetAutostart, /* domainSetAutostart */
    xenUnifiedDomainGetSchedulerType, /* domainGetSchedulerType */
    xenUnifiedDomainGetSchedulerParameters, /* domainGetSchedulerParameters */
    xenUnifiedDomainSetSchedulerParameters, /* domainSetSchedulerParameters */
    xenUnifiedDomainMigratePrepare, /* domainMigratePrepare */
    xenUnifiedDomainMigratePerform, /* domainMigratePerform */
    xenUnifiedDomainMigrateFinish, /* domainMigrateFinish */
    xenUnifiedDomainBlockStats, /* domainBlockStats */
    xenUnifiedDomainInterfaceStats, /* domainInterfaceStats */
1959
    NULL, /* domainMemoryStats */
1960 1961 1962 1963 1964 1965 1966 1967
    xenUnifiedDomainBlockPeek, /* domainBlockPeek */
    NULL, /* domainMemoryPeek */
    xenUnifiedNodeGetCellsFreeMemory, /* nodeGetCellsFreeMemory */
    xenUnifiedNodeGetFreeMemory, /* getFreeMemory */
    xenUnifiedDomainEventRegister, /* domainEventRegister */
    xenUnifiedDomainEventDeregister, /* domainEventDeregister */
    NULL, /* domainMigratePrepare2 */
    NULL, /* domainMigrateFinish2 */
1968 1969 1970
    xenUnifiedNodeDeviceDettach, /* nodeDeviceDettach */
    xenUnifiedNodeDeviceReAttach, /* nodeDeviceReAttach */
    xenUnifiedNodeDeviceReset, /* nodeDeviceReset */
C
Chris Lalancette 已提交
1971
    NULL, /* domainMigratePrepareTunnel */
1972 1973 1974 1975
    xenUnifiedIsEncrypted,
    xenUnifiedIsSecure,
    xenUnifiedDomainIsActive,
    xenUnifiedDomainisPersistent,
J
Jiri Denemark 已提交
1976
    NULL, /* cpuCompare */
1977
    NULL, /* cpuBaseline */
1978
    NULL, /* domainGetJobInfo */
1979
    NULL, /* domainAbortJob */
1980
    NULL, /* domainMigrateSetMaxDowntime */
1981 1982
    xenUnifiedDomainEventRegisterAny, /* domainEventRegisterAny */
    xenUnifiedDomainEventDeregisterAny, /* domainEventDeregisterAny */
1983 1984
};

1985
/**
1986
 * xenRegister:
1987 1988 1989 1990 1991
 *
 * Register xen related drivers
 *
 * Returns the driver priority or -1 in case of error.
 */
1992
int
1993
xenRegister (void)
1994
{
1995 1996
    /* Ignore failures here. */
    (void) xenHypervisorInit ();
1997

J
John Levon 已提交
1998 1999 2000 2001
#ifdef WITH_LIBVIRTD
    if (virRegisterStateDriver (&state_driver) == -1) return -1;
#endif

2002
    return virRegisterDriver (&xenUnifiedDriver);
2003 2004
}

2005 2006 2007 2008 2009 2010 2011 2012 2013
/**
 * xenUnifiedDomainInfoListFree:
 *
 * Free the Domain Info List
 */
void
xenUnifiedDomainInfoListFree(xenUnifiedDomainInfoListPtr list)
{
    int i;
J
John Levon 已提交
2014 2015 2016 2017

    if (list == NULL)
        return;

2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066
    for (i=0; i<list->count; i++) {
        VIR_FREE(list->doms[i]->name);
        VIR_FREE(list->doms[i]);
    }
    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:
2067
    virReportOOMError();
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 2110 2111 2112
    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 已提交
2113 2114 2115
static void
xenUnifiedDomainEventDispatchFunc(virConnectPtr conn,
                                  virDomainEventPtr event,
2116
                                  virConnectDomainEventGenericCallback cb,
D
Daniel P. Berrange 已提交
2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133
                                  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);
}

2134 2135
/**
 * xenUnifiedDomainEventDispatch:
D
Daniel P. Berrange 已提交
2136 2137
 * @priv: the connection to dispatch events on
 * @event: the event to dispatch
2138 2139 2140
 *
 * Dispatch domain events to registered callbacks
 *
D
Daniel P. Berrange 已提交
2141 2142
 * The caller must hold the lock in 'priv' before invoking
 *
2143 2144
 */
void xenUnifiedDomainEventDispatch (xenUnifiedPrivatePtr priv,
2145
                                    virDomainEventPtr event)
2146
{
D
Daniel P. Berrange 已提交
2147
    if (!priv)
2148
        return;
2149

D
Daniel P. Berrange 已提交
2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161
    priv->domainEventDispatching = 1;

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

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

2162
    virDomainEventFree(event);
D
Daniel P. Berrange 已提交
2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174

    priv->domainEventDispatching = 0;
}

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

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