xen_driver.c 62.2 KB
Newer Older
1
/*
2
 * xen_driver.c: Unified Xen driver.
3
 *
4
 * Copyright (C) 2007-2011 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
/* Note:
 *
 * This driver provides a unified interface to the five
16
 * separate underlying Xen drivers (xen_internal,
17 18 19 20 21 22 23
 * 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
#include <sys/types.h>
27
#include <fcntl.h>
28
#include <xen/dom0_ops.h>
29
#include <libxml/uri.h>
30

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

36
#include "xen_hypervisor.h"
37 38 39
#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
#include "fdstream.h"
50
#include "files.h"
51

52 53
#define VIR_FROM_THIS VIR_FROM_XEN

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

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

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

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

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

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

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

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

D
Daniel P. Berrange 已提交
106

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

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

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

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

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

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

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

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

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

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

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

#endif

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

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

222 223
    if ((fd = open("/dev/xen/domcaps", O_RDONLY)) >= 0) {
        VIR_FORCE_CLOSE(fd);
224
        return 1;
225 226
    }
#endif
227
    return 0;
228 229
}

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

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

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

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

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

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

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

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

317 318
    priv->domainEventCallbacks = cbList;

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


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

334
    /* XenD is required to succeed if privileged */
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
        } else {
            DEBUG0("Handing off for remote driver");
365 366
            ret = VIR_DRV_OPEN_DECLINED; /* Let remote_driver try instead */
            goto clean;
367
        }
368 369
    }

D
Daniel P. Berrange 已提交
370 371
    xenNumaInit(conn);

372
    if (!(priv->caps = xenHypervisorMakeCapabilities(conn))) {
373 374 375 376
        DEBUG0("Failed to make capabilities");
        goto fail;
    }

377
#if WITH_XEN_INOTIFY
378 379 380 381 382 383 384
    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;
        }
385 386 387
    }
#endif

388
    return VIR_DRV_OPEN_SUCCESS;
389

390 391 392
fail:
    ret = VIR_DRV_OPEN_ERROR;
clean:
393 394 395
    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 已提交
396
    virMutexDestroy(&priv->lock);
397
    VIR_FREE(priv);
D
Daniel P. Berrange 已提交
398
    conn->privateData = NULL;
D
Daniel Veillard 已提交
399
    return ret;
400 401
}

402 403 404
#define GET_PRIVATE(conn) \
    xenUnifiedPrivatePtr priv = (xenUnifiedPrivatePtr) (conn)->privateData

405 406 407
static int
xenUnifiedClose (virConnectPtr conn)
{
408
    GET_PRIVATE(conn);
409
    int i;
410

411
    virCapabilitiesFree(priv->caps);
412 413
    virDomainEventCallbackListFree(priv->domainEventCallbacks);

414 415
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->close)
416
            (void) drivers[i]->close (conn);
417

D
Daniel P. Berrange 已提交
418 419
    virMutexDestroy(&priv->lock);
    VIR_FREE(conn->privateData);
420

421
    return 0;
422 423
}

424 425 426 427 428 429 430 431 432 433 434

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


435 436 437
static const char *
xenUnifiedType (virConnectPtr conn)
{
438
    GET_PRIVATE(conn);
439
    int i;
440

441
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
442 443
        if (priv->opened[i])
            return "Xen";
444

445
    return NULL;
446 447
}

448 449 450 451 452
/* Which features are supported by this driver? */
static int
xenUnifiedSupportsFeature (virConnectPtr conn ATTRIBUTE_UNUSED, int feature)
{
    switch (feature) {
453 454 455 456 457
    case VIR_DRV_FEATURE_MIGRATION_V1:
    case VIR_DRV_FEATURE_MIGRATION_DIRECT:
        return 1;
    default:
        return 0;
458 459 460
    }
}

461
static int
462
xenUnifiedGetVersion (virConnectPtr conn, unsigned long *hvVer)
463
{
464
    GET_PRIVATE(conn);
465
    int i;
466

467 468 469
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->version &&
470 471
            drivers[i]->version (conn, hvVer) == 0)
            return 0;
472

473
    return -1;
474 475
}

476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
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;
}

496
int
497 498
xenUnifiedGetMaxVcpus (virConnectPtr conn, const char *type)
{
499
    GET_PRIVATE(conn);
500

501
    if (type && STRCASENEQ (type, "Xen")) {
502
        xenUnifiedError(VIR_ERR_INVALID_ARG, __FUNCTION__);
503 504
        return -1;
    }
505

506 507 508
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET])
        return xenHypervisorGetMaxVcpus (conn, type);
    else {
509
        xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
510 511
        return -1;
    }
512 513 514 515 516
}

static int
xenUnifiedNodeGetInfo (virConnectPtr conn, virNodeInfoPtr info)
{
517
    GET_PRIVATE(conn);
518
    int i;
519

520 521 522
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->nodeGetInfo &&
523 524
            drivers[i]->nodeGetInfo (conn, info) == 0)
            return 0;
525

526
    return -1;
527 528 529 530 531
}

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

D
Daniel P. Berrange 已提交
535
    if (!(xml = virCapabilitiesFormatXML(priv->caps))) {
536
        virReportOOMError();
D
Daniel P. Berrange 已提交
537 538
        return NULL;
    }
539

D
Daniel P. Berrange 已提交
540
    return xml;
541 542 543 544 545
}

static int
xenUnifiedListDomains (virConnectPtr conn, int *ids, int maxids)
{
546
    GET_PRIVATE(conn);
547
    int ret;
548

549 550 551 552 553
    /* Try xenstore. */
    if (priv->opened[XEN_UNIFIED_XS_OFFSET]) {
        ret = xenStoreListDomains (conn, ids, maxids);
        if (ret >= 0) return ret;
    }
554

555 556 557 558 559 560 561 562 563 564 565 566
    /* 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;
    }

567
    return -1;
568 569 570 571 572
}

static int
xenUnifiedNumOfDomains (virConnectPtr conn)
{
573
    GET_PRIVATE(conn);
574
    int ret;
575

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
    /* 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;
    }

594
    return -1;
595 596 597
}

static virDomainPtr
598 599
xenUnifiedDomainCreateXML (virConnectPtr conn,
                           const char *xmlDesc, unsigned int flags)
600
{
601
    GET_PRIVATE(conn);
602 603
    int i;
    virDomainPtr ret;
604

605
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
606 607
        if (priv->opened[i] && drivers[i]->domainCreateXML) {
            ret = drivers[i]->domainCreateXML (conn, xmlDesc, flags);
608 609
            if (ret) return ret;
        }
610

611
    return NULL;
612 613
}

614 615 616 617
/* 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.
 */
618 619 620
static virDomainPtr
xenUnifiedDomainLookupByID (virConnectPtr conn, int id)
{
621
    GET_PRIVATE(conn);
622
    virDomainPtr ret;
623

624 625 626 627 628
    /* Reset any connection-level errors in virterror first, in case
     * there is one hanging around from a previous call.
     */
    virConnResetLastError (conn);

629 630 631 632 633 634 635
    /* 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;
    }

636 637 638 639 640 641
    /* Try xend. */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
        ret = xenDaemonLookupByID (conn, id);
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }
642

643
    /* Not found. */
644
    xenUnifiedError(VIR_ERR_NO_DOMAIN, __FUNCTION__);
645
    return NULL;
646 647 648 649
}

static virDomainPtr
xenUnifiedDomainLookupByUUID (virConnectPtr conn,
650
                              const unsigned char *uuid)
651
{
652
    GET_PRIVATE(conn);
653
    virDomainPtr ret;
654

655 656 657 658 659
    /* Reset any connection-level errors in virterror first, in case
     * there is one hanging around from a previous call.
     */
    virConnResetLastError (conn);

660 661 662 663 664 665 666
    /* 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;
    }

667 668 669 670 671 672
    /* Try xend. */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
        ret = xenDaemonLookupByUUID (conn, uuid);
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }
673

674 675 676 677 678 679 680 681
    /* 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. */
682
    xenUnifiedError(VIR_ERR_NO_DOMAIN, __FUNCTION__);
683
    return NULL;
684 685 686 687
}

static virDomainPtr
xenUnifiedDomainLookupByName (virConnectPtr conn,
688
                              const char *name)
689
{
690
    GET_PRIVATE(conn);
691
    virDomainPtr ret;
692

693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
    /* Reset any connection-level errors in virterror first, in case
     * there is one hanging around from a previous call.
     */
    virConnResetLastError (conn);

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

719
    /* Not found. */
720
    xenUnifiedError(VIR_ERR_NO_DOMAIN, __FUNCTION__);
721
    return NULL;
722 723
}

724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742

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
743
xenUnifiedDomainIsPersistent(virDomainPtr dom)
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
{
    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) {
774
                        virReportOOMError();
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
                        goto done;
                    }
                    if (access(path, R_OK) == 0)
                        ret = 1;
                    else if (errno == ENOENT)
                        ret = 0;
                }
            }
        }
    }

done:
    if (currdom)
        virDomainFree(currdom);

    return ret;
}

793 794 795 796 797 798
static int
xenUnifiedDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

799 800 801
static int
xenUnifiedDomainSuspend (virDomainPtr dom)
{
802
    GET_PRIVATE(dom->conn);
803
    int i;
804

805 806 807
    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
808
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
809
        if (i != XEN_UNIFIED_HYPERVISOR_OFFSET &&
810
            priv->opened[i] &&
811 812 813
            drivers[i]->domainSuspend &&
            drivers[i]->domainSuspend (dom) == 0)
            return 0;
814

815 816 817
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] &&
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainSuspend &&
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainSuspend (dom) == 0)
818
        return 0;
819

820
    return -1;
821 822 823 824 825
}

static int
xenUnifiedDomainResume (virDomainPtr dom)
{
826
    GET_PRIVATE(dom->conn);
827
    int i;
828

829 830 831
    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
832
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
833
        if (i != XEN_UNIFIED_HYPERVISOR_OFFSET &&
834
            priv->opened[i] &&
835 836 837
            drivers[i]->domainResume &&
            drivers[i]->domainResume (dom) == 0)
            return 0;
838

839 840 841
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] &&
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainResume &&
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainResume (dom) == 0)
842
        return 0;
843

844
    return -1;
845 846 847 848 849
}

static int
xenUnifiedDomainShutdown (virDomainPtr dom)
{
850
    GET_PRIVATE(dom->conn);
851
    int i;
852

853 854 855
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainShutdown &&
856 857
            drivers[i]->domainShutdown (dom) == 0)
            return 0;
858

859
    return -1;
860 861 862 863 864
}

static int
xenUnifiedDomainReboot (virDomainPtr dom, unsigned int flags)
{
865
    GET_PRIVATE(dom->conn);
866
    int i;
867

868 869 870
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainReboot &&
871 872
            drivers[i]->domainReboot (dom, flags) == 0)
            return 0;
873

874
    return -1;
875 876 877 878 879
}

static int
xenUnifiedDomainDestroy (virDomainPtr dom)
{
880
    GET_PRIVATE(dom->conn);
881
    int i;
882

883 884 885
    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
886
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
887
        if (i != XEN_UNIFIED_HYPERVISOR_OFFSET &&
888
            priv->opened[i] &&
889 890 891
            drivers[i]->domainDestroy &&
            drivers[i]->domainDestroy (dom) == 0)
            return 0;
892

893
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] &&
894 895
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainDestroy &&
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainDestroy (dom) == 0)
896
        return 0;
897

898
    return -1;
899 900 901 902 903
}

static char *
xenUnifiedDomainGetOSType (virDomainPtr dom)
{
904
    GET_PRIVATE(dom->conn);
905 906
    int i;
    char *ret;
907

908 909
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainGetOSType) {
910 911 912
            ret = drivers[i]->domainGetOSType (dom);
            if (ret) return ret;
        }
913

914
    return NULL;
915 916 917 918 919
}

static unsigned long
xenUnifiedDomainGetMaxMemory (virDomainPtr dom)
{
920
    GET_PRIVATE(dom->conn);
921 922
    int i;
    unsigned long ret;
923

924 925
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainGetMaxMemory) {
926 927 928
            ret = drivers[i]->domainGetMaxMemory (dom);
            if (ret != 0) return ret;
        }
929

930
    return 0;
931 932 933 934 935
}

static int
xenUnifiedDomainSetMaxMemory (virDomainPtr dom, unsigned long memory)
{
936
    GET_PRIVATE(dom->conn);
937
    int i;
938

939 940 941 942 943 944
    /* Prefer xend for setting max memory */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
        if (xenDaemonDomainSetMaxMemory (dom, memory) == 0)
            return 0;
    }

945
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
946 947
        if (i != XEN_UNIFIED_XEND_OFFSET &&
            priv->opened[i] &&
948
            drivers[i]->domainSetMaxMemory &&
949 950
            drivers[i]->domainSetMaxMemory (dom, memory) == 0)
            return 0;
951

952
    return -1;
953 954 955 956 957
}

static int
xenUnifiedDomainSetMemory (virDomainPtr dom, unsigned long memory)
{
958
    GET_PRIVATE(dom->conn);
959
    int i;
960

961 962 963
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainSetMemory &&
964 965
            drivers[i]->domainSetMemory (dom, memory) == 0)
            return 0;
966

967
    return -1;
968 969 970 971 972
}

static int
xenUnifiedDomainGetInfo (virDomainPtr dom, virDomainInfoPtr info)
{
973
    GET_PRIVATE(dom->conn);
974
    int i;
975

976 977 978
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainGetInfo &&
979 980
            drivers[i]->domainGetInfo (dom, info) == 0)
            return 0;
981

982
    return -1;
983 984 985 986 987
}

static int
xenUnifiedDomainSave (virDomainPtr dom, const char *to)
{
988
    GET_PRIVATE(dom->conn);
989
    int i;
990

991 992 993
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainSave &&
994 995
            drivers[i]->domainSave (dom, to) == 0)
            return 0;
996

997
    return -1;
998 999 1000 1001 1002
}

static int
xenUnifiedDomainRestore (virConnectPtr conn, const char *from)
{
1003
    GET_PRIVATE(conn);
1004
    int i;
1005

1006 1007 1008
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainRestore &&
1009 1010
            drivers[i]->domainRestore (conn, from) == 0)
            return 0;
1011

1012
    return -1;
1013 1014 1015 1016 1017
}

static int
xenUnifiedDomainCoreDump (virDomainPtr dom, const char *to, int flags)
{
1018
    GET_PRIVATE(dom->conn);
1019
    int i;
1020

1021 1022 1023
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainCoreDump &&
1024 1025
            drivers[i]->domainCoreDump (dom, to, flags) == 0)
            return 0;
1026

1027
    return -1;
1028 1029 1030
}

static int
1031 1032
xenUnifiedDomainSetVcpusFlags (virDomainPtr dom, unsigned int nvcpus,
                               unsigned int flags)
1033
{
1034
    GET_PRIVATE(dom->conn);
1035
    int ret;
1036

1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
    virCheckFlags(VIR_DOMAIN_VCPU_LIVE |
                  VIR_DOMAIN_VCPU_CONFIG |
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    /* At least one of LIVE or CONFIG must be set.  MAXIMUM cannot be
     * mixed with LIVE.  */
    if ((flags & (VIR_DOMAIN_VCPU_LIVE | VIR_DOMAIN_VCPU_CONFIG)) == 0 ||
        (flags & (VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_VCPU_LIVE)) ==
         (VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_VCPU_LIVE)) {
        xenUnifiedError(VIR_ERR_INVALID_ARG,
                        _("invalid flag combination: (0x%x)"), flags);
        return -1;
    }
    if (!nvcpus || (unsigned short) nvcpus != nvcpus) {
        xenUnifiedError(VIR_ERR_INVALID_ARG,
                        _("argument out of range: %d"), nvcpus);
1053 1054 1055
        return -1;
    }

1056 1057 1058
    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
        ret = xenDaemonDomainSetVcpusFlags(dom, nvcpus, flags);
        if (ret != -2)
            return ret;
    }
    if (priv->opened[XEN_UNIFIED_XM_OFFSET]) {
        ret = xenXMDomainSetVcpusFlags(dom, nvcpus, flags);
        if (ret != -2)
            return ret;
    }
    if (flags == VIR_DOMAIN_VCPU_LIVE)
        return xenHypervisorSetVcpus(dom, nvcpus);
1071

1072
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1073
    return -1;
1074 1075
}

1076 1077 1078
static int
xenUnifiedDomainSetVcpus (virDomainPtr dom, unsigned int nvcpus)
{
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
    unsigned int flags = VIR_DOMAIN_VCPU_LIVE;
    xenUnifiedPrivatePtr priv;

    /* Per the documented API, it is hypervisor-dependent whether this
     * affects just _LIVE or _LIVE|_CONFIG; in xen's case, that
     * depends on xendConfigVersion.  */
    if (dom) {
        priv = dom->conn->privateData;
        if (priv->xendConfigVersion >= 3)
            flags |= VIR_DOMAIN_VCPU_CONFIG;
    }
    return xenUnifiedDomainSetVcpusFlags(dom, nvcpus, flags);
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
}

static int
1127
xenUnifiedDomainGetVcpusFlags (virDomainPtr dom, unsigned int flags)
1128
{
1129
    GET_PRIVATE(dom->conn);
1130
    int ret;
1131

1132 1133 1134 1135 1136 1137 1138 1139
    virCheckFlags(VIR_DOMAIN_VCPU_LIVE |
                  VIR_DOMAIN_VCPU_CONFIG |
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    /* Exactly one of LIVE or CONFIG must be set.  */
    if (!(flags & VIR_DOMAIN_VCPU_LIVE) == !(flags & VIR_DOMAIN_VCPU_CONFIG)) {
        xenUnifiedError(VIR_ERR_INVALID_ARG,
                        _("invalid flag combination: (0x%x)"), flags);
1140 1141 1142
        return -1;
    }

1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
        ret = xenDaemonDomainGetVcpusFlags(dom, flags);
        if (ret != -2)
            return ret;
    }
    if (priv->opened[XEN_UNIFIED_XM_OFFSET]) {
        ret = xenXMDomainGetVcpusFlags(dom, flags);
        if (ret != -2)
            return ret;
    }
    if (flags == (VIR_DOMAIN_VCPU_CONFIG | VIR_DOMAIN_VCPU_MAXIMUM))
        return xenHypervisorGetVcpuMax(dom);
1155

1156
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1157
    return -1;
1158 1159
}

1160 1161 1162 1163 1164 1165 1166
static int
xenUnifiedDomainGetMaxVcpus (virDomainPtr dom)
{
    return xenUnifiedDomainGetVcpusFlags(dom, (VIR_DOMAIN_VCPU_LIVE |
                                               VIR_DOMAIN_VCPU_MAXIMUM));
}

1167 1168 1169
static char *
xenUnifiedDomainDumpXML (virDomainPtr dom, int flags)
{
1170
    GET_PRIVATE(dom->conn);
1171

1172 1173 1174 1175 1176 1177
    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 已提交
1178
            xenUnifiedLock(priv);
1179
            cpus = xenDomainUsedCpus(dom);
D
Daniel P. Berrange 已提交
1180
            xenUnifiedUnlock(priv);
1181
            res = xenDaemonDomainDumpXML(dom, flags, cpus);
1182
            VIR_FREE(cpus);
1183
            return(res);
1184
        }
1185
    }
1186

1187
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1188
    return NULL;
1189 1190
}

1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204

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)) {
1205
        xenUnifiedError(VIR_ERR_INVALID_ARG,
1206 1207 1208 1209 1210
                        _("unsupported config type %s"), format);
        return NULL;
    }

    if (STREQ(format, XEN_CONFIG_FORMAT_XM)) {
1211
        conf = virConfReadMem(config, strlen(config), 0);
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
        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;

1222
    ret = virDomainDefFormat(def, 0);
1223 1224 1225

cleanup:
    virDomainDefFree(def);
1226 1227
    if (conf)
        virConfFree(conf);
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
    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)) {
1246
        xenUnifiedError(VIR_ERR_INVALID_ARG,
1247 1248 1249 1250
                        _("unsupported config type %s"), format);
        goto cleanup;
    }

1251
    if (!(def = virDomainDefParseString(priv->caps,
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
                                        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) {
1263
            virReportOOMError();
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
            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;
}


1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
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);

1300
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
    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);

1319
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1320 1321 1322 1323 1324 1325 1326 1327 1328
    return -1;
}

static virDomainPtr
xenUnifiedDomainMigrateFinish (virConnectPtr dconn,
                               const char *dname,
                               const char *cookie ATTRIBUTE_UNUSED,
                               int cookielen ATTRIBUTE_UNUSED,
                               const char *uri ATTRIBUTE_UNUSED,
1329
                               unsigned long flags)
1330
{
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
    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) {
1343
            xenUnifiedError(VIR_ERR_MIGRATE_PERSIST_FAILED,
1344
                            "%s", _("failed to get XML representation of migrated domain"));
1345 1346 1347 1348 1349
            goto failure;
        }

        dom_new = xenDaemonDomainDefineXML (dconn, domain_xml);
        if (! dom_new) {
1350
            xenUnifiedError(VIR_ERR_MIGRATE_PERSIST_FAILED,
1351
                            "%s", _("failed to define domain on destination host"));
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
            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;
1370 1371
}

1372 1373
static int
xenUnifiedListDefinedDomains (virConnectPtr conn, char **const names,
1374
                              int maxnames)
1375
{
1376
    GET_PRIVATE(conn);
1377 1378
    int i;
    int ret;
1379

1380 1381
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->listDefinedDomains) {
1382 1383 1384
            ret = drivers[i]->listDefinedDomains (conn, names, maxnames);
            if (ret >= 0) return ret;
        }
1385

1386
    return -1;
1387 1388 1389 1390 1391
}

static int
xenUnifiedNumOfDefinedDomains (virConnectPtr conn)
{
1392
    GET_PRIVATE(conn);
1393 1394
    int i;
    int ret;
1395

1396 1397
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->numOfDefinedDomains) {
1398 1399 1400
            ret = drivers[i]->numOfDefinedDomains (conn);
            if (ret >= 0) return ret;
        }
1401

1402
    return -1;
1403 1404 1405
}

static int
1406
xenUnifiedDomainCreateWithFlags (virDomainPtr dom, unsigned int flags)
1407
{
1408
    GET_PRIVATE(dom->conn);
1409
    int i;
1410

1411 1412
    virCheckFlags(0, -1);

1413 1414
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainCreate &&
1415 1416
            drivers[i]->domainCreate (dom) == 0)
            return 0;
1417

1418
    return -1;
1419 1420
}

1421 1422 1423 1424 1425 1426
static int
xenUnifiedDomainCreate (virDomainPtr dom)
{
    return xenUnifiedDomainCreateWithFlags(dom, 0);
}

1427 1428 1429
static virDomainPtr
xenUnifiedDomainDefineXML (virConnectPtr conn, const char *xml)
{
1430
    GET_PRIVATE(conn);
1431 1432
    int i;
    virDomainPtr ret;
1433

1434 1435
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainDefineXML) {
1436 1437 1438
            ret = drivers[i]->domainDefineXML (conn, xml);
            if (ret) return ret;
        }
1439

1440
    return NULL;
1441 1442 1443 1444 1445
}

static int
xenUnifiedDomainUndefine (virDomainPtr dom)
{
1446
    GET_PRIVATE(dom->conn);
1447
    int i;
1448

1449 1450
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainUndefine &&
1451 1452
            drivers[i]->domainUndefine (dom) == 0)
            return 0;
1453

1454
    return -1;
1455 1456 1457
}

static int
1458
xenUnifiedDomainAttachDevice (virDomainPtr dom, const char *xml)
1459 1460 1461
{
    GET_PRIVATE(dom->conn);
    int i;
1462
    unsigned int flags = VIR_DOMAIN_DEVICE_MODIFY_LIVE;
1463

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

    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)
1484
{
1485
    GET_PRIVATE(dom->conn);
1486
    int i;
1487

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

1493
    return -1;
1494 1495 1496
}

static int
1497
xenUnifiedDomainDetachDevice (virDomainPtr dom, const char *xml)
1498 1499 1500
{
    GET_PRIVATE(dom->conn);
    int i;
1501
    unsigned int flags = VIR_DOMAIN_DEVICE_MODIFY_LIVE;
1502

1503 1504 1505 1506 1507 1508 1509 1510
    /*
     * 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;
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522

    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)
1523
{
1524
    GET_PRIVATE(dom->conn);
1525
    int i;
1526

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

1532
    return -1;
1533 1534
}

1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
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;
}

1550 1551 1552 1553 1554
static int
xenUnifiedDomainGetAutostart (virDomainPtr dom, int *autostart)
{
    GET_PRIVATE(dom->conn);

1555 1556 1557 1558 1559 1560 1561
    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);
    }
1562

1563
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1564 1565 1566 1567 1568 1569 1570 1571
    return -1;
}

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

1572 1573 1574 1575 1576 1577 1578
    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);
    }
1579

1580
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1581 1582 1583
    return -1;
}

1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
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);
1594 1595
            if (schedulertype != NULL)
                return(schedulertype);
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
        }
    }
    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);
1611 1612 1613
           if (ret == 0)
               return(0);
        }
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
    }
    return(-1);
}

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

1625 1626
    /* do the hypervisor call last to get better error */
    for (i = XEN_UNIFIED_NR_DRIVERS - 1; i >= 0; i--) {
1627 1628
        if (priv->opened[i] && drivers[i]->domainSetSchedulerParameters) {
           ret = drivers[i]->domainSetSchedulerParameters(dom, params, nparams);
1629 1630 1631
           if (ret == 0)
               return 0;
        }
1632 1633 1634 1635 1636
    }

    return(-1);
}

1637 1638 1639 1640 1641 1642 1643 1644 1645
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);

1646
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
    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);

1659
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1660 1661 1662
    return -1;
}

R
Richard W.M. Jones 已提交
1663 1664 1665
static int
xenUnifiedDomainBlockPeek (virDomainPtr dom, const char *path,
                           unsigned long long offset, size_t size,
1666
                           void *buffer, unsigned int flags ATTRIBUTE_UNUSED)
R
Richard W.M. Jones 已提交
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
{
    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;
    }

1682
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
R
Richard W.M. Jones 已提交
1683 1684 1685
    return -1;
}

1686 1687 1688 1689 1690 1691 1692
static int
xenUnifiedNodeGetCellsFreeMemory (virConnectPtr conn, unsigned long long *freeMems,
                                  int startCell, int maxCells)
{
    GET_PRIVATE (conn);

    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET])
1693
        return xenHypervisorNodeGetCellsFreeMemory (conn, freeMems,
1694 1695
                                                    startCell, maxCells);

1696
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1697 1698 1699
    return -1;
}

1700 1701 1702 1703 1704 1705 1706 1707
static unsigned long long
xenUnifiedNodeGetFreeMemory (virConnectPtr conn)
{
    unsigned long long freeMem = 0;
    int ret;
    GET_PRIVATE (conn);

    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET]) {
1708
        ret = xenHypervisorNodeGetCellsFreeMemory (conn, &freeMem,
1709
                                                    -1, 1);
1710 1711 1712
        if (ret != 1)
            return (0);
        return(freeMem);
1713 1714
    }

1715
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1716 1717 1718
    return(0);
}

1719

1720
static int
1721 1722 1723 1724
xenUnifiedDomainEventRegister(virConnectPtr conn,
                              virConnectDomainEventCallback callback,
                              void *opaque,
                              virFreeCallback freefunc)
1725
{
D
Daniel P. Berrange 已提交
1726 1727
    GET_PRIVATE (conn);

1728
    int ret;
D
Daniel P. Berrange 已提交
1729
    xenUnifiedLock(priv);
1730

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

1737 1738 1739
    ret = virDomainEventCallbackListAdd(conn, priv->domainEventCallbacks,
                                        callback, opaque, freefunc);

D
Daniel P. Berrange 已提交
1740
    xenUnifiedUnlock(priv);
1741
    return (ret);
1742 1743
}

1744

1745
static int
1746 1747
xenUnifiedDomainEventDeregister(virConnectPtr conn,
                                virConnectDomainEventCallback callback)
1748 1749 1750
{
    int ret;
    GET_PRIVATE (conn);
D
Daniel P. Berrange 已提交
1751 1752
    xenUnifiedLock(priv);

1753
    if (priv->xsWatch == -1) {
1754
        xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
D
Daniel P. Berrange 已提交
1755
        xenUnifiedUnlock(priv);
1756 1757 1758
        return -1;
    }

D
Daniel P. Berrange 已提交
1759 1760 1761 1762 1763 1764
    if (priv->domainEventDispatching)
        ret = virDomainEventCallbackListMarkDelete(conn, priv->domainEventCallbacks,
                                                   callback);
    else
        ret = virDomainEventCallbackListRemove(conn, priv->domainEventCallbacks,
                                               callback);
1765

D
Daniel P. Berrange 已提交
1766
    xenUnifiedUnlock(priv);
1767 1768 1769
    return ret;
}

1770

1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784
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) {
1785
        xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806
        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) {
1807
        xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
        xenUnifiedUnlock(priv);
        return -1;
    }

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

    xenUnifiedUnlock(priv);
    return ret;
}


1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839
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;

1840
    def = virNodeDeviceDefParseString(xml, EXISTING_DEVICE);
1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857
    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) {
1858
        xenUnifiedError(VIR_ERR_INVALID_ARG,
1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879
                        _("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;

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

1884
    if (pciDettachDevice(pci, NULL) < 0)
1885 1886 1887 1888
        goto out;

    ret = 0;
out:
1889
    pciFreeDevice(pci);
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902
    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;

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

1907
    if (pciReAttachDevice(pci, NULL) < 0)
1908 1909 1910 1911
        goto out;

    ret = 0;
out:
1912
    pciFreeDevice(pci);
1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925
    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;

1926
    pci = pciGetDevice(domain, bus, slot, function);
1927 1928 1929
    if (!pci)
        return -1;

1930
    if (pciResetDevice(pci, NULL, NULL) < 0)
1931 1932 1933 1934
        goto out;

    ret = 0;
out:
1935
    pciFreeDevice(pci);
1936 1937 1938 1939
    return ret;
}


1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
static int
xenUnifiedDomainOpenConsole(virDomainPtr dom,
                            const char *devname,
                            virStreamPtr st,
                            unsigned int flags)
{
    virDomainDefPtr def = NULL;
    int ret = -1;
    virDomainChrDefPtr chr = NULL;

    virCheckFlags(0, -1);

    if (dom->id == -1) {
        xenUnifiedError(VIR_ERR_OPERATION_INVALID,
                        "%s", _("domain is not running"));
        goto cleanup;
    }

    if (devname) {
        /* XXX support device aliases in future */
        xenUnifiedError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                        _("Named device aliases are not supported"));
        goto cleanup;
    }

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

    if (def->console)
        chr = def->console;
    else if (def->nserials)
        chr = def->serials[0];

    if (!chr) {
        xenUnifiedError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("cannot find default console device"));
        goto cleanup;
    }

1980
    if (chr->source.type != VIR_DOMAIN_CHR_TYPE_PTY) {
1981 1982 1983 1984 1985
        xenUnifiedError(VIR_ERR_INTERNAL_ERROR,
                        _("character device %s is not using a PTY"), devname);
        goto cleanup;
    }

1986
    if (virFDStreamOpenFile(st, chr->source.data.file.path, O_RDWR) < 0)
1987 1988 1989 1990 1991 1992 1993
        goto cleanup;

    ret = 0;
cleanup:
    virDomainDefFree(def);
    return ret;
}
E
Eric Blake 已提交
1994
/*----- Register with libvirt.c, and initialize Xen drivers. -----*/
1995 1996 1997

/* The interface which we export upwards to libvirt.c. */
static virDriver xenUnifiedDriver = {
1998 1999 2000 2001 2002 2003 2004
    VIR_DRV_XEN_UNIFIED,
    "Xen",
    xenUnifiedOpen, /* open */
    xenUnifiedClose, /* close */
    xenUnifiedSupportsFeature, /* supports_feature */
    xenUnifiedType, /* type */
    xenUnifiedGetVersion, /* version */
2005
    NULL, /* libvirtVersion (impl. in libvirt.c) */
2006
    virGetHostname, /* getHostname */
E
Eric Blake 已提交
2007
    NULL, /* getSysinfo */
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030
    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 */
2031 2032
    xenUnifiedDomainSetVcpusFlags, /* domainSetVcpusFlags */
    xenUnifiedDomainGetVcpusFlags, /* domainGetVcpusFlags */
2033 2034 2035 2036 2037 2038
    xenUnifiedDomainPinVcpu, /* domainPinVcpu */
    xenUnifiedDomainGetVcpus, /* domainGetVcpus */
    xenUnifiedDomainGetMaxVcpus, /* domainGetMaxVcpus */
    NULL, /* domainGetSecurityLabel */
    NULL, /* nodeGetSecurityModel */
    xenUnifiedDomainDumpXML, /* domainDumpXML */
2039 2040
    xenUnifiedDomainXMLFromNative, /* domainXMLFromNative */
    xenUnifiedDomainXMLToNative, /* domainXMLToNative */
2041 2042 2043
    xenUnifiedListDefinedDomains, /* listDefinedDomains */
    xenUnifiedNumOfDefinedDomains, /* numOfDefinedDomains */
    xenUnifiedDomainCreate, /* domainCreate */
2044
    xenUnifiedDomainCreateWithFlags, /* domainCreateWithFlags */
2045 2046 2047
    xenUnifiedDomainDefineXML, /* domainDefineXML */
    xenUnifiedDomainUndefine, /* domainUndefine */
    xenUnifiedDomainAttachDevice, /* domainAttachDevice */
2048
    xenUnifiedDomainAttachDeviceFlags, /* domainAttachDeviceFlags */
2049
    xenUnifiedDomainDetachDevice, /* domainDetachDevice */
2050
    xenUnifiedDomainDetachDeviceFlags, /* domainDetachDeviceFlags */
2051
    xenUnifiedDomainUpdateDeviceFlags, /* domainUpdateDeviceFlags */
2052 2053 2054 2055 2056 2057 2058 2059 2060 2061
    xenUnifiedDomainGetAutostart, /* domainGetAutostart */
    xenUnifiedDomainSetAutostart, /* domainSetAutostart */
    xenUnifiedDomainGetSchedulerType, /* domainGetSchedulerType */
    xenUnifiedDomainGetSchedulerParameters, /* domainGetSchedulerParameters */
    xenUnifiedDomainSetSchedulerParameters, /* domainSetSchedulerParameters */
    xenUnifiedDomainMigratePrepare, /* domainMigratePrepare */
    xenUnifiedDomainMigratePerform, /* domainMigratePerform */
    xenUnifiedDomainMigrateFinish, /* domainMigrateFinish */
    xenUnifiedDomainBlockStats, /* domainBlockStats */
    xenUnifiedDomainInterfaceStats, /* domainInterfaceStats */
2062
    NULL, /* domainMemoryStats */
2063 2064
    xenUnifiedDomainBlockPeek, /* domainBlockPeek */
    NULL, /* domainMemoryPeek */
2065
    NULL, /* domainGetBlockInfo */
2066 2067 2068 2069 2070 2071
    xenUnifiedNodeGetCellsFreeMemory, /* nodeGetCellsFreeMemory */
    xenUnifiedNodeGetFreeMemory, /* getFreeMemory */
    xenUnifiedDomainEventRegister, /* domainEventRegister */
    xenUnifiedDomainEventDeregister, /* domainEventDeregister */
    NULL, /* domainMigratePrepare2 */
    NULL, /* domainMigrateFinish2 */
2072 2073 2074
    xenUnifiedNodeDeviceDettach, /* nodeDeviceDettach */
    xenUnifiedNodeDeviceReAttach, /* nodeDeviceReAttach */
    xenUnifiedNodeDeviceReset, /* nodeDeviceReset */
C
Chris Lalancette 已提交
2075
    NULL, /* domainMigratePrepareTunnel */
2076 2077 2078
    xenUnifiedIsEncrypted, /* isEncrypted */
    xenUnifiedIsSecure, /* isSecure */
    xenUnifiedDomainIsActive, /* domainIsActive */
2079 2080
    xenUnifiedDomainIsPersistent, /* domainIsPersistent */
    xenUnifiedDomainIsUpdated, /* domainIsUpdated */
J
Jiri Denemark 已提交
2081
    NULL, /* cpuCompare */
2082
    NULL, /* cpuBaseline */
2083
    NULL, /* domainGetJobInfo */
2084
    NULL, /* domainAbortJob */
2085
    NULL, /* domainMigrateSetMaxDowntime */
2086 2087
    xenUnifiedDomainEventRegisterAny, /* domainEventRegisterAny */
    xenUnifiedDomainEventDeregisterAny, /* domainEventDeregisterAny */
2088 2089 2090
    NULL, /* domainManagedSave */
    NULL, /* domainHasManagedSaveImage */
    NULL, /* domainManagedSaveRemove */
C
Chris Lalancette 已提交
2091 2092 2093 2094 2095 2096 2097 2098 2099
    NULL, /* domainSnapshotCreateXML */
    NULL, /* domainSnapshotDumpXML */
    NULL, /* domainSnapshotNum */
    NULL, /* domainSnapshotListNames */
    NULL, /* domainSnapshotLookupByName */
    NULL, /* domainHasCurrentSnapshot */
    NULL, /* domainSnapshotCurrent */
    NULL, /* domainRevertToSnapshot */
    NULL, /* domainSnapshotDelete */
C
Chris Lalancette 已提交
2100
    NULL, /* qemuDomainMonitorCommand */
2101 2102
    NULL, /* domainSetMemoryParameters */
    NULL, /* domainGetMemoryParameters */
2103
    xenUnifiedDomainOpenConsole, /* domainOpenConsole */
2104 2105
};

2106
/**
2107
 * xenRegister:
2108 2109 2110 2111 2112
 *
 * Register xen related drivers
 *
 * Returns the driver priority or -1 in case of error.
 */
2113
int
2114
xenRegister (void)
2115
{
2116 2117
    /* Ignore failures here. */
    (void) xenHypervisorInit ();
2118

J
John Levon 已提交
2119 2120 2121 2122
#ifdef WITH_LIBVIRTD
    if (virRegisterStateDriver (&state_driver) == -1) return -1;
#endif

2123
    return virRegisterDriver (&xenUnifiedDriver);
2124 2125
}

2126 2127 2128 2129 2130 2131 2132 2133 2134
/**
 * xenUnifiedDomainInfoListFree:
 *
 * Free the Domain Info List
 */
void
xenUnifiedDomainInfoListFree(xenUnifiedDomainInfoListPtr list)
{
    int i;
J
John Levon 已提交
2135 2136 2137 2138

    if (list == NULL)
        return;

2139 2140 2141 2142
    for (i=0; i<list->count; i++) {
        VIR_FREE(list->doms[i]->name);
        VIR_FREE(list->doms[i]);
    }
2143
    VIR_FREE(list->doms);
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188
    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:
2189
    virReportOOMError();
2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234
    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 已提交
2235 2236 2237
static void
xenUnifiedDomainEventDispatchFunc(virConnectPtr conn,
                                  virDomainEventPtr event,
2238
                                  virConnectDomainEventGenericCallback cb,
D
Daniel P. Berrange 已提交
2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255
                                  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);
}

2256 2257
/**
 * xenUnifiedDomainEventDispatch:
D
Daniel P. Berrange 已提交
2258 2259
 * @priv: the connection to dispatch events on
 * @event: the event to dispatch
2260 2261 2262
 *
 * Dispatch domain events to registered callbacks
 *
D
Daniel P. Berrange 已提交
2263 2264
 * The caller must hold the lock in 'priv' before invoking
 *
2265 2266
 */
void xenUnifiedDomainEventDispatch (xenUnifiedPrivatePtr priv,
2267
                                    virDomainEventPtr event)
2268
{
D
Daniel P. Berrange 已提交
2269
    if (!priv)
2270
        return;
2271

D
Daniel P. Berrange 已提交
2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283
    priv->domainEventDispatching = 1;

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

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

2284
    virDomainEventFree(event);
D
Daniel P. Berrange 已提交
2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296

    priv->domainEventDispatching = 0;
}

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

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