xen_driver.c 66.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_sxpr.h"
37
#include "xen_xm.h"
38
#include "xen_hypervisor.h"
39 40 41
#include "xend_internal.h"
#include "xs_internal.h"
#include "xm_internal.h"
42
#if WITH_XEN_INOTIFY
43
# include "xen_inotify.h"
44
#endif
45
#include "xml.h"
46
#include "util.h"
47
#include "memory.h"
48 49
#include "node_device_conf.h"
#include "pci.h"
50
#include "uuid.h"
51
#include "fdstream.h"
52
#include "files.h"
J
Jim Fehlig 已提交
53
#include "command.h"
54

55 56
#define VIR_FROM_THIS VIR_FROM_XEN

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

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

77
#if defined WITH_LIBVIRTD || defined __sun
J
John Levon 已提交
78
static int inside_daemon;
79
#endif
J
John Levon 已提交
80

81
#define xenUnifiedError(code, ...)                                         \
82
        virReportErrorHelper(VIR_FROM_XEN, code, __FILE__,                 \
83
                             __FUNCTION__, __LINE__, __VA_ARGS__)
84

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

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

D
Daniel P. Berrange 已提交
103
    priv = conn->privateData;
104

D
Daniel P. Berrange 已提交
105 106
    priv->nbNodeCells = nodeInfo.nodes;
    priv->nbNodeCpus = nodeInfo.cpus;
107 108
}

D
Daniel P. Berrange 已提交
109

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

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

D
Daniel P. Berrange 已提交
138 139 140
    priv = dom->conn->privateData;

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

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

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

                }
            }
        }
178
        res = virDomainCpuSetFormat(cpulist, priv->nbNodeCpus);
179 180 181
    }

done:
182 183 184
    VIR_FREE(cpulist);
    VIR_FREE(cpumap);
    VIR_FREE(cpuinfo);
185 186 187
    return(res);
}

J
John Levon 已提交
188 189 190
#ifdef WITH_LIBVIRTD

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

static virStateDriver state_driver = {
198
    .name = "Xen",
J
John Levon 已提交
199 200 201 202 203
    .initialize = xenInitialize,
};

#endif

204 205 206 207 208 209 210 211 212 213 214
/*----- 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.
 */

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

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

J
Jim Fehlig 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
#ifdef WITH_LIBXL
static int
xenUnifiedXendProbe (void)
{
    virCommandPtr cmd;
    int status;
    int ret = 0;

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

    return ret;
}
#endif

250
static virDrvOpenStatus
251
xenUnifiedOpen (virConnectPtr conn, virConnectAuthPtr auth, int flags)
252
{
253
    int i, ret = VIR_DRV_OPEN_DECLINED;
254
    xenUnifiedPrivatePtr priv;
255
    virDomainEventCallbackListPtr cbList;
256

J
John Levon 已提交
257 258 259 260 261 262 263 264 265
#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

266 267 268 269 270 271
    if (conn->uri == NULL) {
        if (!xenUnifiedProbe())
            return VIR_DRV_OPEN_DECLINED;

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

293 294 295 296 297 298 299 300 301 302 303 304
            /* 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] != '/') {
305
                xenUnifiedError(VIR_ERR_INTERNAL_ERROR,
306 307 308 309 310 311
                                _("unexpected Xen URI path '%s', try ///var/lib/xen/xend-socket"),
                                NULLSTR(conn->uri->path));
                return VIR_DRV_OPEN_ERROR;
            }
        }
    }
312

J
Jim Fehlig 已提交
313 314 315 316 317 318 319
#ifdef WITH_LIBXL
    /* Decline xen:// URI if xend is not running and libxenlight
     * driver is potentially available. */
    if (!xenUnifiedXendProbe())
        return VIR_DRV_OPEN_DECLINED;
#endif

320 321
    /* We now know the URI is definitely for this driver, so beyond
     * here, don't return DECLINED, always use ERROR */
322 323

    /* Allocate per-connection private data. */
324
    if (VIR_ALLOC(priv) < 0) {
325
        virReportOOMError();
326 327
        return VIR_DRV_OPEN_ERROR;
    }
D
Daniel P. Berrange 已提交
328
    if (virMutexInit(&priv->lock) < 0) {
329
        xenUnifiedError(VIR_ERR_INTERNAL_ERROR,
E
Eric Blake 已提交
330
                        "%s", _("cannot initialize mutex"));
D
Daniel P. Berrange 已提交
331 332 333
        VIR_FREE(priv);
        return VIR_DRV_OPEN_ERROR;
    }
334

335 336
    /* Allocate callback list */
    if (VIR_ALLOC(cbList) < 0) {
337
        virReportOOMError();
D
Daniel P. Berrange 已提交
338 339
        virMutexDestroy(&priv->lock);
        VIR_FREE(priv);
340 341
        return VIR_DRV_OPEN_ERROR;
    }
D
Daniel P. Berrange 已提交
342 343
    conn->privateData = priv;

344 345
    priv->domainEventCallbacks = cbList;

346 347 348 349 350
    priv->handle = -1;
    priv->xendConfigVersion = -1;
    priv->xshandle = NULL;


J
John Levon 已提交
351 352
    /* Hypervisor is only run with privilege & required to succeed */
    if (xenHavePrivilege()) {
353
        VIR_DEBUG("Trying hypervisor sub-driver");
354
        if (drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->open(conn, auth, flags) ==
355
            VIR_DRV_OPEN_SUCCESS) {
356
            VIR_DEBUG("Activated hypervisor sub-driver");
357
            priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] = 1;
358
        }
359
    }
360

361
    /* XenD is required to succeed if privileged */
362
    VIR_DEBUG("Trying XenD sub-driver");
363
    if (drivers[XEN_UNIFIED_XEND_OFFSET]->open(conn, auth, flags) ==
364
        VIR_DRV_OPEN_SUCCESS) {
365
        VIR_DEBUG("Activated XenD sub-driver");
366 367 368 369 370
        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) {
371
            VIR_DEBUG("Trying XM sub-driver");
372
            if (drivers[XEN_UNIFIED_XM_OFFSET]->open(conn, auth, flags) ==
373
                VIR_DRV_OPEN_SUCCESS) {
374
                VIR_DEBUG("Activated XM sub-driver");
375 376 377
                priv->opened[XEN_UNIFIED_XM_OFFSET] = 1;
            }
        }
378
        VIR_DEBUG("Trying XS sub-driver");
379
        if (drivers[XEN_UNIFIED_XS_OFFSET]->open(conn, auth, flags) ==
380
            VIR_DRV_OPEN_SUCCESS) {
381
            VIR_DEBUG("Activated XS sub-driver");
382 383
            priv->opened[XEN_UNIFIED_XS_OFFSET] = 1;
        } else {
J
John Levon 已提交
384 385
            if (xenHavePrivilege())
                goto fail; /* XS is mandatory when privileged */
386 387
        }
    } else {
J
John Levon 已提交
388 389
        if (xenHavePrivilege()) {
            goto fail; /* XenD is mandatory when privileged */
390
        } else {
391
            VIR_DEBUG("Handing off for remote driver");
392 393
            ret = VIR_DRV_OPEN_DECLINED; /* Let remote_driver try instead */
            goto clean;
394
        }
395 396
    }

D
Daniel P. Berrange 已提交
397 398
    xenNumaInit(conn);

399
    if (!(priv->caps = xenHypervisorMakeCapabilities(conn))) {
400
        VIR_DEBUG("Failed to make capabilities");
401 402 403
        goto fail;
    }

404
#if WITH_XEN_INOTIFY
405
    if (xenHavePrivilege()) {
406
        VIR_DEBUG("Trying Xen inotify sub-driver");
407 408
        if (drivers[XEN_UNIFIED_INOTIFY_OFFSET]->open(conn, auth, flags) ==
            VIR_DRV_OPEN_SUCCESS) {
409
            VIR_DEBUG("Activated Xen inotify sub-driver");
410 411
            priv->opened[XEN_UNIFIED_INOTIFY_OFFSET] = 1;
        }
412 413 414
    }
#endif

415
    return VIR_DRV_OPEN_SUCCESS;
416

417 418 419
fail:
    ret = VIR_DRV_OPEN_ERROR;
clean:
420
    VIR_DEBUG("Failed to activate a mandatory sub-driver");
421 422
    for (i = 0 ; i < XEN_UNIFIED_NR_DRIVERS ; i++)
        if (priv->opened[i]) drivers[i]->close(conn);
D
Daniel P. Berrange 已提交
423
    virMutexDestroy(&priv->lock);
424
    VIR_FREE(priv);
D
Daniel P. Berrange 已提交
425
    conn->privateData = NULL;
D
Daniel Veillard 已提交
426
    return ret;
427 428
}

429 430 431
#define GET_PRIVATE(conn) \
    xenUnifiedPrivatePtr priv = (xenUnifiedPrivatePtr) (conn)->privateData

432 433 434
static int
xenUnifiedClose (virConnectPtr conn)
{
435
    GET_PRIVATE(conn);
436
    int i;
437

438
    virCapabilitiesFree(priv->caps);
439 440
    virDomainEventCallbackListFree(priv->domainEventCallbacks);

441 442
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->close)
443
            (void) drivers[i]->close (conn);
444

D
Daniel P. Berrange 已提交
445 446
    virMutexDestroy(&priv->lock);
    VIR_FREE(conn->privateData);
447

448
    return 0;
449 450
}

451 452 453 454 455 456 457 458 459 460 461

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


462 463 464
static const char *
xenUnifiedType (virConnectPtr conn)
{
465
    GET_PRIVATE(conn);
466
    int i;
467

468
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
469 470
        if (priv->opened[i])
            return "Xen";
471

472
    return NULL;
473 474
}

475 476 477 478 479
/* Which features are supported by this driver? */
static int
xenUnifiedSupportsFeature (virConnectPtr conn ATTRIBUTE_UNUSED, int feature)
{
    switch (feature) {
480 481 482 483 484
    case VIR_DRV_FEATURE_MIGRATION_V1:
    case VIR_DRV_FEATURE_MIGRATION_DIRECT:
        return 1;
    default:
        return 0;
485 486 487
    }
}

488
static int
489
xenUnifiedGetVersion (virConnectPtr conn, unsigned long *hvVer)
490
{
491
    GET_PRIVATE(conn);
492
    int i;
493

494 495 496
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->version &&
497 498
            drivers[i]->version (conn, hvVer) == 0)
            return 0;
499

500
    return -1;
501 502
}

503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
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;
}

523
int
524 525
xenUnifiedGetMaxVcpus (virConnectPtr conn, const char *type)
{
526
    GET_PRIVATE(conn);
527

528
    if (type && STRCASENEQ (type, "Xen")) {
529
        xenUnifiedError(VIR_ERR_INVALID_ARG, __FUNCTION__);
530 531
        return -1;
    }
532

533 534 535
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET])
        return xenHypervisorGetMaxVcpus (conn, type);
    else {
536
        xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
537 538
        return -1;
    }
539 540 541 542 543
}

static int
xenUnifiedNodeGetInfo (virConnectPtr conn, virNodeInfoPtr info)
{
544
    GET_PRIVATE(conn);
545
    int i;
546

547 548 549
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->nodeGetInfo &&
550 551
            drivers[i]->nodeGetInfo (conn, info) == 0)
            return 0;
552

553
    return -1;
554 555 556 557 558
}

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

D
Daniel P. Berrange 已提交
562
    if (!(xml = virCapabilitiesFormatXML(priv->caps))) {
563
        virReportOOMError();
D
Daniel P. Berrange 已提交
564 565
        return NULL;
    }
566

D
Daniel P. Berrange 已提交
567
    return xml;
568 569 570 571 572
}

static int
xenUnifiedListDomains (virConnectPtr conn, int *ids, int maxids)
{
573
    GET_PRIVATE(conn);
574
    int ret;
575

576 577 578 579 580
    /* Try xenstore. */
    if (priv->opened[XEN_UNIFIED_XS_OFFSET]) {
        ret = xenStoreListDomains (conn, ids, maxids);
        if (ret >= 0) return ret;
    }
581

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

594
    return -1;
595 596 597 598 599
}

static int
xenUnifiedNumOfDomains (virConnectPtr conn)
{
600
    GET_PRIVATE(conn);
601
    int ret;
602

603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
    /* 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;
    }

621
    return -1;
622 623 624
}

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

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

638
    return NULL;
639 640
}

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

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

656 657 658 659 660 661 662
    /* 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;
    }

663 664 665 666 667 668
    /* Try xend. */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
        ret = xenDaemonLookupByID (conn, id);
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }
669

670
    /* Not found. */
671
    xenUnifiedError(VIR_ERR_NO_DOMAIN, __FUNCTION__);
672
    return NULL;
673 674 675 676
}

static virDomainPtr
xenUnifiedDomainLookupByUUID (virConnectPtr conn,
677
                              const unsigned char *uuid)
678
{
679
    GET_PRIVATE(conn);
680
    virDomainPtr ret;
681

682 683 684 685 686
    /* Reset any connection-level errors in virterror first, in case
     * there is one hanging around from a previous call.
     */
    virConnResetLastError (conn);

687 688 689 690 691 692 693
    /* 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;
    }

694 695 696 697 698 699
    /* Try xend. */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
        ret = xenDaemonLookupByUUID (conn, uuid);
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }
700

701 702 703 704 705 706 707 708
    /* 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. */
709
    xenUnifiedError(VIR_ERR_NO_DOMAIN, __FUNCTION__);
710
    return NULL;
711 712 713 714
}

static virDomainPtr
xenUnifiedDomainLookupByName (virConnectPtr conn,
715
                              const char *name)
716
{
717
    GET_PRIVATE(conn);
718
    virDomainPtr ret;
719

720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
    /* 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;
    }
745

746
    /* Not found. */
747
    xenUnifiedError(VIR_ERR_NO_DOMAIN, __FUNCTION__);
748
    return NULL;
749 750
}

751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769

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
770
xenUnifiedDomainIsPersistent(virDomainPtr dom)
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
{
    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) {
801
                        virReportOOMError();
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
                        goto done;
                    }
                    if (access(path, R_OK) == 0)
                        ret = 1;
                    else if (errno == ENOENT)
                        ret = 0;
                }
            }
        }
    }

done:
    if (currdom)
        virDomainFree(currdom);

    return ret;
}

820 821 822 823 824 825
static int
xenUnifiedDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

826 827 828
static int
xenUnifiedDomainSuspend (virDomainPtr dom)
{
829
    GET_PRIVATE(dom->conn);
830
    int i;
831

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

842 843 844
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] &&
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainSuspend &&
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainSuspend (dom) == 0)
845
        return 0;
846

847
    return -1;
848 849 850 851 852
}

static int
xenUnifiedDomainResume (virDomainPtr dom)
{
853
    GET_PRIVATE(dom->conn);
854
    int i;
855

856 857 858
    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
859
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
860
        if (i != XEN_UNIFIED_HYPERVISOR_OFFSET &&
861
            priv->opened[i] &&
862 863 864
            drivers[i]->domainResume &&
            drivers[i]->domainResume (dom) == 0)
            return 0;
865

866 867 868
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] &&
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainResume &&
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainResume (dom) == 0)
869
        return 0;
870

871
    return -1;
872 873 874 875 876
}

static int
xenUnifiedDomainShutdown (virDomainPtr dom)
{
877
    GET_PRIVATE(dom->conn);
878
    int i;
879

880 881 882
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainShutdown &&
883 884
            drivers[i]->domainShutdown (dom) == 0)
            return 0;
885

886
    return -1;
887 888 889 890 891
}

static int
xenUnifiedDomainReboot (virDomainPtr dom, unsigned int flags)
{
892
    GET_PRIVATE(dom->conn);
893
    int i;
894

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

901
    return -1;
902 903 904 905 906
}

static int
xenUnifiedDomainDestroy (virDomainPtr dom)
{
907
    GET_PRIVATE(dom->conn);
908
    int i;
909

910 911 912
    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
913
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
914
        if (i != XEN_UNIFIED_HYPERVISOR_OFFSET &&
915
            priv->opened[i] &&
916 917 918
            drivers[i]->domainDestroy &&
            drivers[i]->domainDestroy (dom) == 0)
            return 0;
919

920
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] &&
921 922
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainDestroy &&
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainDestroy (dom) == 0)
923
        return 0;
924

925
    return -1;
926 927 928 929 930
}

static char *
xenUnifiedDomainGetOSType (virDomainPtr dom)
{
931
    GET_PRIVATE(dom->conn);
932 933
    int i;
    char *ret;
934

935 936
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainGetOSType) {
937 938 939
            ret = drivers[i]->domainGetOSType (dom);
            if (ret) return ret;
        }
940

941
    return NULL;
942 943 944 945 946
}

static unsigned long
xenUnifiedDomainGetMaxMemory (virDomainPtr dom)
{
947
    GET_PRIVATE(dom->conn);
948 949
    int i;
    unsigned long ret;
950

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

957
    return 0;
958 959 960 961 962
}

static int
xenUnifiedDomainSetMaxMemory (virDomainPtr dom, unsigned long memory)
{
963
    GET_PRIVATE(dom->conn);
964
    int i;
965

966 967 968 969 970 971
    /* Prefer xend for setting max memory */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
        if (xenDaemonDomainSetMaxMemory (dom, memory) == 0)
            return 0;
    }

972
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
973 974
        if (i != XEN_UNIFIED_XEND_OFFSET &&
            priv->opened[i] &&
975
            drivers[i]->domainSetMaxMemory &&
976 977
            drivers[i]->domainSetMaxMemory (dom, memory) == 0)
            return 0;
978

979
    return -1;
980 981 982 983 984
}

static int
xenUnifiedDomainSetMemory (virDomainPtr dom, unsigned long memory)
{
985
    GET_PRIVATE(dom->conn);
986
    int i;
987

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

994
    return -1;
995 996 997 998 999
}

static int
xenUnifiedDomainGetInfo (virDomainPtr dom, virDomainInfoPtr info)
{
1000
    GET_PRIVATE(dom->conn);
1001
    int i;
1002

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

1009
    return -1;
1010 1011
}

1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
static int
xenUnifiedDomainGetState(virDomainPtr dom,
                         int *state,
                         int *reason,
                         unsigned int flags)
{
    GET_PRIVATE(dom->conn);
    int ret;

    virCheckFlags(0, -1);

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

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

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

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

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

    return -1;
}

1053 1054 1055
static int
xenUnifiedDomainSave (virDomainPtr dom, const char *to)
{
1056
    GET_PRIVATE(dom->conn);
1057
    int i;
1058

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

1065
    return -1;
1066 1067 1068 1069 1070
}

static int
xenUnifiedDomainRestore (virConnectPtr conn, const char *from)
{
1071
    GET_PRIVATE(conn);
1072
    int i;
1073

1074 1075 1076
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainRestore &&
1077 1078
            drivers[i]->domainRestore (conn, from) == 0)
            return 0;
1079

1080
    return -1;
1081 1082 1083 1084 1085
}

static int
xenUnifiedDomainCoreDump (virDomainPtr dom, const char *to, int flags)
{
1086
    GET_PRIVATE(dom->conn);
1087
    int i;
1088

1089 1090 1091
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainCoreDump &&
1092 1093
            drivers[i]->domainCoreDump (dom, to, flags) == 0)
            return 0;
1094

1095
    return -1;
1096 1097 1098
}

static int
1099 1100
xenUnifiedDomainSetVcpusFlags (virDomainPtr dom, unsigned int nvcpus,
                               unsigned int flags)
1101
{
1102
    GET_PRIVATE(dom->conn);
1103
    int ret;
1104

1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
    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);
1121 1122 1123
        return -1;
    }

1124 1125 1126
    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
    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);
1139

1140
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1141
    return -1;
1142 1143
}

1144 1145 1146
static int
xenUnifiedDomainSetVcpus (virDomainPtr dom, unsigned int nvcpus)
{
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
    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);
1159 1160
}

1161 1162
static int
xenUnifiedDomainPinVcpu (virDomainPtr dom, unsigned int vcpu,
1163
                         unsigned char *cpumap, int maplen)
1164
{
1165
    GET_PRIVATE(dom->conn);
1166
    int i;
1167

1168 1169 1170
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainPinVcpu &&
1171 1172
            drivers[i]->domainPinVcpu (dom, vcpu, cpumap, maplen) == 0)
            return 0;
1173

1174
    return -1;
1175 1176 1177 1178
}

static int
xenUnifiedDomainGetVcpus (virDomainPtr dom,
1179 1180
                          virVcpuInfoPtr info, int maxinfo,
                          unsigned char *cpumaps, int maplen)
1181
{
1182
    GET_PRIVATE(dom->conn);
1183
    int i, ret;
1184

1185 1186
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainGetVcpus) {
1187 1188 1189 1190
            ret = drivers[i]->domainGetVcpus (dom, info, maxinfo, cpumaps, maplen);
            if (ret > 0)
                return ret;
        }
1191
    return -1;
1192 1193 1194
}

static int
1195
xenUnifiedDomainGetVcpusFlags (virDomainPtr dom, unsigned int flags)
1196
{
1197
    GET_PRIVATE(dom->conn);
1198
    int ret;
1199

1200 1201 1202 1203 1204 1205 1206 1207
    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);
1208 1209 1210
        return -1;
    }

1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
    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);
1223

1224
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1225
    return -1;
1226 1227
}

1228 1229 1230 1231 1232 1233 1234
static int
xenUnifiedDomainGetMaxVcpus (virDomainPtr dom)
{
    return xenUnifiedDomainGetVcpusFlags(dom, (VIR_DOMAIN_VCPU_LIVE |
                                               VIR_DOMAIN_VCPU_MAXIMUM));
}

1235
static char *
1236
xenUnifiedDomainGetXMLDesc(virDomainPtr dom, int flags)
1237
{
1238
    GET_PRIVATE(dom->conn);
1239

1240 1241
    if (dom->id == -1 && priv->xendConfigVersion < 3 ) {
        if (priv->opened[XEN_UNIFIED_XM_OFFSET])
1242
            return xenXMDomainGetXMLDesc(dom, flags);
1243 1244 1245
    } else {
        if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
            char *cpus, *res;
D
Daniel P. Berrange 已提交
1246
            xenUnifiedLock(priv);
1247
            cpus = xenDomainUsedCpus(dom);
D
Daniel P. Berrange 已提交
1248
            xenUnifiedUnlock(priv);
1249
            res = xenDaemonDomainGetXMLDesc(dom, flags, cpus);
1250
            VIR_FREE(cpus);
1251
            return(res);
1252
        }
1253
    }
1254

1255
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1256
    return NULL;
1257 1258
}

1259 1260 1261 1262 1263 1264 1265 1266 1267 1268

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;
1269 1270 1271
    int id;
    char * tty;
    int vncport;
1272 1273 1274 1275
    GET_PRIVATE(conn);

    if (STRNEQ(format, XEN_CONFIG_FORMAT_XM) &&
        STRNEQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
1276
        xenUnifiedError(VIR_ERR_INVALID_ARG,
1277 1278 1279 1280 1281
                        _("unsupported config type %s"), format);
        return NULL;
    }

    if (STREQ(format, XEN_CONFIG_FORMAT_XM)) {
1282
        conf = virConfReadMem(config, strlen(config), 0);
1283 1284 1285
        if (!conf)
            goto cleanup;

M
Markus Groß 已提交
1286
        def = xenParseXM(conf, priv->xendConfigVersion, priv->caps);
1287
    } else if (STREQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
1288 1289 1290 1291 1292
        id = xenGetDomIdFromSxprString(config, priv->xendConfigVersion);
        xenUnifiedLock(priv);
        tty = xenStoreDomainGetConsolePath(conn, id);
        vncport = xenStoreDomainGetVNCPort(conn, id);
        xenUnifiedUnlock(priv);
M
Markus Groß 已提交
1293
        def = xenParseSxprString(config, priv->xendConfigVersion, tty,
1294
                                       vncport);
1295 1296 1297 1298
    }
    if (!def)
        goto cleanup;

1299
    ret = virDomainDefFormat(def, 0);
1300 1301 1302

cleanup:
    virDomainDefFree(def);
1303 1304
    if (conf)
        virConfFree(conf);
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
    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)) {
1323
        xenUnifiedError(VIR_ERR_INVALID_ARG,
1324 1325 1326 1327
                        _("unsupported config type %s"), format);
        goto cleanup;
    }

1328
    if (!(def = virDomainDefParseString(priv->caps,
1329 1330 1331 1332 1333 1334
                                        xmlData,
                                        0)))
        goto cleanup;

    if (STREQ(format, XEN_CONFIG_FORMAT_XM)) {
        int len = MAX_CONFIG_SIZE;
M
Markus Groß 已提交
1335
        conf = xenFormatXM(conn, def, priv->xendConfigVersion);
1336 1337 1338 1339
        if (!conf)
            goto cleanup;

        if (VIR_ALLOC_N(ret, len) < 0) {
1340
            virReportOOMError();
1341 1342 1343 1344 1345 1346 1347 1348
            goto cleanup;
        }

        if (virConfWriteMem(ret, &len, conf) < 0) {
            VIR_FREE(ret);
            goto cleanup;
        }
    } else if (STREQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
M
Markus Groß 已提交
1349
        ret = xenFormatSxpr(conn, def, priv->xendConfigVersion);
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
    }

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


1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
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);

1377
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
    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);

1396
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1397 1398 1399 1400 1401 1402 1403 1404 1405
    return -1;
}

static virDomainPtr
xenUnifiedDomainMigrateFinish (virConnectPtr dconn,
                               const char *dname,
                               const char *cookie ATTRIBUTE_UNUSED,
                               int cookielen ATTRIBUTE_UNUSED,
                               const char *uri ATTRIBUTE_UNUSED,
1406
                               unsigned long flags)
1407
{
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
    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) {
1418
        domain_xml = xenDaemonDomainGetXMLDesc(dom, 0, NULL);
1419
        if (! domain_xml) {
1420
            xenUnifiedError(VIR_ERR_MIGRATE_PERSIST_FAILED,
1421
                            "%s", _("failed to get XML representation of migrated domain"));
1422 1423 1424 1425 1426
            goto failure;
        }

        dom_new = xenDaemonDomainDefineXML (dconn, domain_xml);
        if (! dom_new) {
1427
            xenUnifiedError(VIR_ERR_MIGRATE_PERSIST_FAILED,
1428
                            "%s", _("failed to define domain on destination host"));
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
            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;
1447 1448
}

1449 1450
static int
xenUnifiedListDefinedDomains (virConnectPtr conn, char **const names,
1451
                              int maxnames)
1452
{
1453
    GET_PRIVATE(conn);
1454 1455
    int i;
    int ret;
1456

1457 1458
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->listDefinedDomains) {
1459 1460 1461
            ret = drivers[i]->listDefinedDomains (conn, names, maxnames);
            if (ret >= 0) return ret;
        }
1462

1463
    return -1;
1464 1465 1466 1467 1468
}

static int
xenUnifiedNumOfDefinedDomains (virConnectPtr conn)
{
1469
    GET_PRIVATE(conn);
1470 1471
    int i;
    int ret;
1472

1473 1474
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->numOfDefinedDomains) {
1475 1476 1477
            ret = drivers[i]->numOfDefinedDomains (conn);
            if (ret >= 0) return ret;
        }
1478

1479
    return -1;
1480 1481 1482
}

static int
1483
xenUnifiedDomainCreateWithFlags (virDomainPtr dom, unsigned int flags)
1484
{
1485
    GET_PRIVATE(dom->conn);
1486
    int i;
1487

1488 1489
    virCheckFlags(0, -1);

1490 1491
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainCreate &&
1492 1493
            drivers[i]->domainCreate (dom) == 0)
            return 0;
1494

1495
    return -1;
1496 1497
}

1498 1499 1500 1501 1502 1503
static int
xenUnifiedDomainCreate (virDomainPtr dom)
{
    return xenUnifiedDomainCreateWithFlags(dom, 0);
}

1504 1505 1506
static virDomainPtr
xenUnifiedDomainDefineXML (virConnectPtr conn, const char *xml)
{
1507
    GET_PRIVATE(conn);
1508 1509
    int i;
    virDomainPtr ret;
1510

1511 1512
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainDefineXML) {
1513 1514 1515
            ret = drivers[i]->domainDefineXML (conn, xml);
            if (ret) return ret;
        }
1516

1517
    return NULL;
1518 1519 1520 1521 1522
}

static int
xenUnifiedDomainUndefine (virDomainPtr dom)
{
1523
    GET_PRIVATE(dom->conn);
1524
    int i;
1525

1526 1527
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainUndefine &&
1528 1529
            drivers[i]->domainUndefine (dom) == 0)
            return 0;
1530

1531
    return -1;
1532 1533 1534
}

static int
1535
xenUnifiedDomainAttachDevice (virDomainPtr dom, const char *xml)
1536 1537 1538
{
    GET_PRIVATE(dom->conn);
    int i;
1539
    unsigned int flags = VIR_DOMAIN_DEVICE_MODIFY_LIVE;
1540

1541 1542 1543 1544 1545 1546 1547 1548
    /*
     * 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;
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560

    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)
1561
{
1562
    GET_PRIVATE(dom->conn);
1563
    int i;
1564

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

1570
    return -1;
1571 1572 1573
}

static int
1574
xenUnifiedDomainDetachDevice (virDomainPtr dom, const char *xml)
1575 1576 1577
{
    GET_PRIVATE(dom->conn);
    int i;
1578
    unsigned int flags = VIR_DOMAIN_DEVICE_MODIFY_LIVE;
1579

1580 1581 1582 1583 1584 1585 1586 1587
    /*
     * 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;
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599

    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)
1600
{
1601
    GET_PRIVATE(dom->conn);
1602
    int i;
1603

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

1609
    return -1;
1610 1611
}

1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
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;
}

1627 1628 1629 1630 1631
static int
xenUnifiedDomainGetAutostart (virDomainPtr dom, int *autostart)
{
    GET_PRIVATE(dom->conn);

1632 1633 1634 1635 1636 1637 1638
    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);
    }
1639

1640
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1641 1642 1643 1644 1645 1646 1647 1648
    return -1;
}

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

1649 1650 1651 1652 1653 1654 1655
    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);
    }
1656

1657
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1658 1659 1660
    return -1;
}

1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
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);
1671 1672
            if (schedulertype != NULL)
                return(schedulertype);
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
        }
    }
    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);
1688 1689 1690
           if (ret == 0)
               return(0);
        }
1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
    }
    return(-1);
}

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

1702 1703
    /* do the hypervisor call last to get better error */
    for (i = XEN_UNIFIED_NR_DRIVERS - 1; i >= 0; i--) {
1704 1705
        if (priv->opened[i] && drivers[i]->domainSetSchedulerParameters) {
           ret = drivers[i]->domainSetSchedulerParameters(dom, params, nparams);
1706 1707 1708
           if (ret == 0)
               return 0;
        }
1709 1710 1711 1712 1713
    }

    return(-1);
}

1714 1715 1716 1717 1718 1719 1720 1721 1722
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);

1723
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
    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);

1736
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1737 1738 1739
    return -1;
}

R
Richard W.M. Jones 已提交
1740 1741 1742
static int
xenUnifiedDomainBlockPeek (virDomainPtr dom, const char *path,
                           unsigned long long offset, size_t size,
1743
                           void *buffer, unsigned int flags ATTRIBUTE_UNUSED)
R
Richard W.M. Jones 已提交
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
{
    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;
    }

1759
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
R
Richard W.M. Jones 已提交
1760 1761 1762
    return -1;
}

1763 1764 1765 1766 1767 1768 1769
static int
xenUnifiedNodeGetCellsFreeMemory (virConnectPtr conn, unsigned long long *freeMems,
                                  int startCell, int maxCells)
{
    GET_PRIVATE (conn);

    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET])
1770
        return xenHypervisorNodeGetCellsFreeMemory (conn, freeMems,
1771 1772
                                                    startCell, maxCells);

1773
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1774 1775 1776
    return -1;
}

1777 1778 1779 1780 1781 1782 1783 1784
static unsigned long long
xenUnifiedNodeGetFreeMemory (virConnectPtr conn)
{
    unsigned long long freeMem = 0;
    int ret;
    GET_PRIVATE (conn);

    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET]) {
1785
        ret = xenHypervisorNodeGetCellsFreeMemory (conn, &freeMem,
1786
                                                    -1, 1);
1787 1788 1789
        if (ret != 1)
            return (0);
        return(freeMem);
1790 1791
    }

1792
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1793 1794 1795
    return(0);
}

1796

1797
static int
1798 1799 1800 1801
xenUnifiedDomainEventRegister(virConnectPtr conn,
                              virConnectDomainEventCallback callback,
                              void *opaque,
                              virFreeCallback freefunc)
1802
{
D
Daniel P. Berrange 已提交
1803 1804
    GET_PRIVATE (conn);

1805
    int ret;
D
Daniel P. Berrange 已提交
1806
    xenUnifiedLock(priv);
1807

1808
    if (priv->xsWatch == -1) {
1809
        xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
D
Daniel P. Berrange 已提交
1810
        xenUnifiedUnlock(priv);
1811 1812 1813
        return -1;
    }

1814 1815 1816
    ret = virDomainEventCallbackListAdd(conn, priv->domainEventCallbacks,
                                        callback, opaque, freefunc);

D
Daniel P. Berrange 已提交
1817
    xenUnifiedUnlock(priv);
1818
    return (ret);
1819 1820
}

1821

1822
static int
1823 1824
xenUnifiedDomainEventDeregister(virConnectPtr conn,
                                virConnectDomainEventCallback callback)
1825 1826 1827
{
    int ret;
    GET_PRIVATE (conn);
D
Daniel P. Berrange 已提交
1828 1829
    xenUnifiedLock(priv);

1830
    if (priv->xsWatch == -1) {
1831
        xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
D
Daniel P. Berrange 已提交
1832
        xenUnifiedUnlock(priv);
1833 1834 1835
        return -1;
    }

D
Daniel P. Berrange 已提交
1836 1837 1838 1839 1840 1841
    if (priv->domainEventDispatching)
        ret = virDomainEventCallbackListMarkDelete(conn, priv->domainEventCallbacks,
                                                   callback);
    else
        ret = virDomainEventCallbackListRemove(conn, priv->domainEventCallbacks,
                                               callback);
1842

D
Daniel P. Berrange 已提交
1843
    xenUnifiedUnlock(priv);
1844 1845 1846
    return ret;
}

1847

1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
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) {
1862
        xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
        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) {
1884
        xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
        xenUnifiedUnlock(priv);
        return -1;
    }

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

    xenUnifiedUnlock(priv);
    return ret;
}


1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916
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;

1917
    def = virNodeDeviceDefParseString(xml, EXISTING_DEVICE);
1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
    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) {
1935
        xenUnifiedError(VIR_ERR_INVALID_ARG,
1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956
                        _("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;

1957
    pci = pciGetDevice(domain, bus, slot, function);
1958 1959 1960
    if (!pci)
        return -1;

1961
    if (pciDettachDevice(pci, NULL) < 0)
1962 1963 1964 1965
        goto out;

    ret = 0;
out:
1966
    pciFreeDevice(pci);
1967 1968 1969
    return ret;
}

1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027
static int
xenUnifiedNodeDeviceAssignedDomainId (virNodeDevicePtr dev)
{
    int numdomains;
    int ret = -1, i;
    int *ids = NULL;
    char *bdf = NULL;
    char *xref = NULL;
    unsigned int domain, bus, slot, function;
    virConnectPtr conn = dev->conn;
    xenUnifiedPrivatePtr priv = conn->privateData;

    /* Get active domains */
    numdomains = xenUnifiedNumOfDomains(conn);
    if (numdomains < 0) {
        return ret;
    }
    if (numdomains > 0){
        if (VIR_ALLOC_N(ids, numdomains) < 0) {
            virReportOOMError();
            goto out;
        }
        if ((numdomains = xenUnifiedListDomains(conn, &ids[0], numdomains)) < 0) {
            goto out;
        }
    }

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

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

    xenUnifiedLock(priv);
    /* Check if bdf is assigned to one of active domains */
    for (i = 0; i < numdomains; i++ ) {
        xref = xenStoreDomainGetPCIID(conn, ids[i], bdf);
        if (xref == NULL) {
            continue;
        } else {
            ret = ids[i];
            break;
        }
    }
    xenUnifiedUnlock(priv);

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

    return ret;
}

2028 2029 2030 2031 2032 2033
static int
xenUnifiedNodeDeviceReAttach (virNodeDevicePtr dev)
{
    pciDevice *pci;
    unsigned domain, bus, slot, function;
    int ret = -1;
2034
    int domid;
2035 2036 2037 2038

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

2039
    pci = pciGetDevice(domain, bus, slot, function);
2040 2041 2042
    if (!pci)
        return -1;

2043 2044 2045 2046 2047 2048 2049 2050
    /* Check if device is assigned to an active guest */
    if ((domid = xenUnifiedNodeDeviceAssignedDomainId(dev)) >= 0) {
        xenUnifiedError(VIR_ERR_INTERNAL_ERROR,
                        _("Device %s has been assigned to guest %d"),
                        dev->name, domid);
        goto out;
    }

2051
    if (pciReAttachDevice(pci, NULL) < 0)
2052 2053 2054 2055
        goto out;

    ret = 0;
out:
2056
    pciFreeDevice(pci);
2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069
    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;

2070
    pci = pciGetDevice(domain, bus, slot, function);
2071 2072 2073
    if (!pci)
        return -1;

2074
    if (pciResetDevice(pci, NULL, NULL) < 0)
2075 2076 2077 2078
        goto out;

    ret = 0;
out:
2079
    pciFreeDevice(pci);
2080 2081 2082 2083
    return ret;
}


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 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123
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;
    }

2124
    if (chr->source.type != VIR_DOMAIN_CHR_TYPE_PTY) {
2125 2126 2127 2128 2129
        xenUnifiedError(VIR_ERR_INTERNAL_ERROR,
                        _("character device %s is not using a PTY"), devname);
        goto cleanup;
    }

2130 2131
    if (virFDStreamOpenFile(st, chr->source.data.file.path,
                            0, 0, O_RDWR, false) < 0)
2132 2133 2134 2135 2136 2137 2138
        goto cleanup;

    ret = 0;
cleanup:
    virDomainDefFree(def);
    return ret;
}
E
Eric Blake 已提交
2139
/*----- Register with libvirt.c, and initialize Xen drivers. -----*/
2140 2141 2142

/* The interface which we export upwards to libvirt.c. */
static virDriver xenUnifiedDriver = {
2143 2144 2145 2146 2147 2148 2149
    VIR_DRV_XEN_UNIFIED,
    "Xen",
    xenUnifiedOpen, /* open */
    xenUnifiedClose, /* close */
    xenUnifiedSupportsFeature, /* supports_feature */
    xenUnifiedType, /* type */
    xenUnifiedGetVersion, /* version */
2150
    NULL, /* libvirtVersion (impl. in libvirt.c) */
2151
    virGetHostname, /* getHostname */
E
Eric Blake 已提交
2152
    NULL, /* getSysinfo */
2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170
    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 */
2171
    NULL, /*domainSetMemoryFlags */
2172 2173 2174 2175
    NULL, /* domainSetMemoryParameters */
    NULL, /* domainGetMemoryParameters */
    NULL, /* domainSetBlkioParameters */
    NULL, /* domainGetBlkioParameters */
2176
    xenUnifiedDomainGetInfo, /* domainGetInfo */
2177
    xenUnifiedDomainGetState, /* domainGetState */
2178 2179 2180
    xenUnifiedDomainSave, /* domainSave */
    xenUnifiedDomainRestore, /* domainRestore */
    xenUnifiedDomainCoreDump, /* domainCoreDump */
2181
    NULL, /* domainScreenshot */
2182
    xenUnifiedDomainSetVcpus, /* domainSetVcpus */
2183 2184
    xenUnifiedDomainSetVcpusFlags, /* domainSetVcpusFlags */
    xenUnifiedDomainGetVcpusFlags, /* domainGetVcpusFlags */
2185 2186 2187 2188 2189
    xenUnifiedDomainPinVcpu, /* domainPinVcpu */
    xenUnifiedDomainGetVcpus, /* domainGetVcpus */
    xenUnifiedDomainGetMaxVcpus, /* domainGetMaxVcpus */
    NULL, /* domainGetSecurityLabel */
    NULL, /* nodeGetSecurityModel */
2190
    xenUnifiedDomainGetXMLDesc, /* domainGetXMLDesc */
2191 2192
    xenUnifiedDomainXMLFromNative, /* domainXMLFromNative */
    xenUnifiedDomainXMLToNative, /* domainXMLToNative */
2193 2194 2195
    xenUnifiedListDefinedDomains, /* listDefinedDomains */
    xenUnifiedNumOfDefinedDomains, /* numOfDefinedDomains */
    xenUnifiedDomainCreate, /* domainCreate */
2196
    xenUnifiedDomainCreateWithFlags, /* domainCreateWithFlags */
2197 2198 2199
    xenUnifiedDomainDefineXML, /* domainDefineXML */
    xenUnifiedDomainUndefine, /* domainUndefine */
    xenUnifiedDomainAttachDevice, /* domainAttachDevice */
2200
    xenUnifiedDomainAttachDeviceFlags, /* domainAttachDeviceFlags */
2201
    xenUnifiedDomainDetachDevice, /* domainDetachDevice */
2202
    xenUnifiedDomainDetachDeviceFlags, /* domainDetachDeviceFlags */
2203
    xenUnifiedDomainUpdateDeviceFlags, /* domainUpdateDeviceFlags */
2204 2205 2206 2207 2208 2209 2210 2211 2212 2213
    xenUnifiedDomainGetAutostart, /* domainGetAutostart */
    xenUnifiedDomainSetAutostart, /* domainSetAutostart */
    xenUnifiedDomainGetSchedulerType, /* domainGetSchedulerType */
    xenUnifiedDomainGetSchedulerParameters, /* domainGetSchedulerParameters */
    xenUnifiedDomainSetSchedulerParameters, /* domainSetSchedulerParameters */
    xenUnifiedDomainMigratePrepare, /* domainMigratePrepare */
    xenUnifiedDomainMigratePerform, /* domainMigratePerform */
    xenUnifiedDomainMigrateFinish, /* domainMigrateFinish */
    xenUnifiedDomainBlockStats, /* domainBlockStats */
    xenUnifiedDomainInterfaceStats, /* domainInterfaceStats */
2214
    NULL, /* domainMemoryStats */
2215 2216
    xenUnifiedDomainBlockPeek, /* domainBlockPeek */
    NULL, /* domainMemoryPeek */
2217
    NULL, /* domainGetBlockInfo */
2218
    xenUnifiedNodeGetCellsFreeMemory, /* nodeGetCellsFreeMemory */
2219
    xenUnifiedNodeGetFreeMemory, /* nodeGetFreeMemory */
2220 2221 2222 2223
    xenUnifiedDomainEventRegister, /* domainEventRegister */
    xenUnifiedDomainEventDeregister, /* domainEventDeregister */
    NULL, /* domainMigratePrepare2 */
    NULL, /* domainMigrateFinish2 */
2224 2225 2226
    xenUnifiedNodeDeviceDettach, /* nodeDeviceDettach */
    xenUnifiedNodeDeviceReAttach, /* nodeDeviceReAttach */
    xenUnifiedNodeDeviceReset, /* nodeDeviceReset */
C
Chris Lalancette 已提交
2227
    NULL, /* domainMigratePrepareTunnel */
2228 2229 2230
    xenUnifiedIsEncrypted, /* isEncrypted */
    xenUnifiedIsSecure, /* isSecure */
    xenUnifiedDomainIsActive, /* domainIsActive */
2231 2232
    xenUnifiedDomainIsPersistent, /* domainIsPersistent */
    xenUnifiedDomainIsUpdated, /* domainIsUpdated */
J
Jiri Denemark 已提交
2233
    NULL, /* cpuCompare */
2234
    NULL, /* cpuBaseline */
2235
    NULL, /* domainGetJobInfo */
2236
    NULL, /* domainAbortJob */
2237
    NULL, /* domainMigrateSetMaxDowntime */
2238
    NULL, /* domainMigrateSetMaxSpeed */
2239 2240
    xenUnifiedDomainEventRegisterAny, /* domainEventRegisterAny */
    xenUnifiedDomainEventDeregisterAny, /* domainEventDeregisterAny */
2241 2242 2243
    NULL, /* domainManagedSave */
    NULL, /* domainHasManagedSaveImage */
    NULL, /* domainManagedSaveRemove */
C
Chris Lalancette 已提交
2244
    NULL, /* domainSnapshotCreateXML */
2245
    NULL, /* domainSnapshotGetXMLDesc */
C
Chris Lalancette 已提交
2246 2247 2248 2249 2250 2251 2252
    NULL, /* domainSnapshotNum */
    NULL, /* domainSnapshotListNames */
    NULL, /* domainSnapshotLookupByName */
    NULL, /* domainHasCurrentSnapshot */
    NULL, /* domainSnapshotCurrent */
    NULL, /* domainRevertToSnapshot */
    NULL, /* domainSnapshotDelete */
C
Chris Lalancette 已提交
2253
    NULL, /* qemuDomainMonitorCommand */
2254
    xenUnifiedDomainOpenConsole, /* domainOpenConsole */
2255
    NULL, /* domainInjectNMI */
2256 2257
};

2258
/**
2259
 * xenRegister:
2260 2261 2262 2263 2264
 *
 * Register xen related drivers
 *
 * Returns the driver priority or -1 in case of error.
 */
2265
int
2266
xenRegister (void)
2267
{
2268 2269
    /* Ignore failures here. */
    (void) xenHypervisorInit ();
2270

J
John Levon 已提交
2271 2272 2273 2274
#ifdef WITH_LIBVIRTD
    if (virRegisterStateDriver (&state_driver) == -1) return -1;
#endif

2275
    return virRegisterDriver (&xenUnifiedDriver);
2276 2277
}

2278 2279 2280 2281 2282 2283 2284 2285 2286
/**
 * xenUnifiedDomainInfoListFree:
 *
 * Free the Domain Info List
 */
void
xenUnifiedDomainInfoListFree(xenUnifiedDomainInfoListPtr list)
{
    int i;
J
John Levon 已提交
2287 2288 2289 2290

    if (list == NULL)
        return;

2291 2292 2293 2294
    for (i=0; i<list->count; i++) {
        VIR_FREE(list->doms[i]->name);
        VIR_FREE(list->doms[i]);
    }
2295
    VIR_FREE(list->doms);
2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317
    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)) {
2318
            VIR_DEBUG("WARNING: dom already tracked");
2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340
            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:
2341
    virReportOOMError();
2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386
    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 已提交
2387 2388 2389
static void
xenUnifiedDomainEventDispatchFunc(virConnectPtr conn,
                                  virDomainEventPtr event,
2390
                                  virConnectDomainEventGenericCallback cb,
D
Daniel P. Berrange 已提交
2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407
                                  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);
}

2408 2409
/**
 * xenUnifiedDomainEventDispatch:
D
Daniel P. Berrange 已提交
2410 2411
 * @priv: the connection to dispatch events on
 * @event: the event to dispatch
2412 2413 2414
 *
 * Dispatch domain events to registered callbacks
 *
D
Daniel P. Berrange 已提交
2415 2416
 * The caller must hold the lock in 'priv' before invoking
 *
2417 2418
 */
void xenUnifiedDomainEventDispatch (xenUnifiedPrivatePtr priv,
2419
                                    virDomainEventPtr event)
2420
{
D
Daniel P. Berrange 已提交
2421
    if (!priv)
2422
        return;
2423

D
Daniel P. Berrange 已提交
2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435
    priv->domainEventDispatching = 1;

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

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

2436
    virDomainEventFree(event);
D
Daniel P. Berrange 已提交
2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448

    priv->domainEventDispatching = 0;
}

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

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