xen_driver.c 66.9 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"
E
Eric Blake 已提交
52
#include "virfile.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, unsigned 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");
E
Eric Blake 已提交
354
        if (xenHypervisorOpen(conn, auth, flags) == VIR_DRV_OPEN_SUCCESS) {
355
            VIR_DEBUG("Activated hypervisor sub-driver");
356
            priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] = 1;
357
        }
358
    }
359

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

D
Daniel P. Berrange 已提交
393 394
    xenNumaInit(conn);

395
    if (!(priv->caps = xenHypervisorMakeCapabilities(conn))) {
396
        VIR_DEBUG("Failed to make capabilities");
397 398 399
        goto fail;
    }

400
#if WITH_XEN_INOTIFY
401
    if (xenHavePrivilege()) {
402
        VIR_DEBUG("Trying Xen inotify sub-driver");
E
Eric Blake 已提交
403
        if (xenInotifyOpen(conn, auth, flags) == VIR_DRV_OPEN_SUCCESS) {
404
            VIR_DEBUG("Activated Xen inotify sub-driver");
405 406
            priv->opened[XEN_UNIFIED_INOTIFY_OFFSET] = 1;
        }
407 408 409
    }
#endif

410
    return VIR_DRV_OPEN_SUCCESS;
411

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

425 426 427
#define GET_PRIVATE(conn) \
    xenUnifiedPrivatePtr priv = (xenUnifiedPrivatePtr) (conn)->privateData

428 429 430
static int
xenUnifiedClose (virConnectPtr conn)
{
431
    GET_PRIVATE(conn);
432
    int i;
433

434
    virCapabilitiesFree(priv->caps);
435 436
    virDomainEventCallbackListFree(priv->domainEventCallbacks);

437
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
438 439
        if (priv->opened[i])
            drivers[i]->xenClose(conn);
440

D
Daniel P. Berrange 已提交
441 442
    virMutexDestroy(&priv->lock);
    VIR_FREE(conn->privateData);
443

444
    return 0;
445 446
}

447 448 449 450 451 452 453 454 455 456 457

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


458 459 460
static const char *
xenUnifiedType (virConnectPtr conn)
{
461
    GET_PRIVATE(conn);
462
    int i;
463

464
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
465 466
        if (priv->opened[i])
            return "Xen";
467

468
    return NULL;
469 470
}

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

484
static int
485
xenUnifiedGetVersion (virConnectPtr conn, unsigned long *hvVer)
486
{
487
    GET_PRIVATE(conn);
488
    int i;
489

490 491
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
E
Eric Blake 已提交
492 493
            drivers[i]->xenVersion &&
            drivers[i]->xenVersion (conn, hvVer) == 0)
494
            return 0;
495

496
    return -1;
497 498
}

499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
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;
}

519
int
520 521
xenUnifiedGetMaxVcpus (virConnectPtr conn, const char *type)
{
522
    GET_PRIVATE(conn);
523

524
    if (type && STRCASENEQ (type, "Xen")) {
525
        xenUnifiedError(VIR_ERR_INVALID_ARG, __FUNCTION__);
526 527
        return -1;
    }
528

529 530 531
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET])
        return xenHypervisorGetMaxVcpus (conn, type);
    else {
532
        xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
533 534
        return -1;
    }
535 536 537 538 539
}

static int
xenUnifiedNodeGetInfo (virConnectPtr conn, virNodeInfoPtr info)
{
540
    GET_PRIVATE(conn);
541

542 543
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        return xenDaemonNodeGetInfo(conn, info);
544
    return -1;
545 546 547 548 549
}

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

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

D
Daniel P. Berrange 已提交
558
    return xml;
559 560 561 562 563
}

static int
xenUnifiedListDomains (virConnectPtr conn, int *ids, int maxids)
{
564
    GET_PRIVATE(conn);
565
    int ret;
566

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

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

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

585
    return -1;
586 587 588 589 590
}

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

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

612
    return -1;
613 614 615
}

static virDomainPtr
616 617
xenUnifiedDomainCreateXML (virConnectPtr conn,
                           const char *xmlDesc, unsigned int flags)
618
{
619
    GET_PRIVATE(conn);
620

621 622
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        return xenDaemonCreateXML(conn, xmlDesc, flags);
623
    return NULL;
624 625
}

626 627 628 629
/* 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.
 */
630 631 632
static virDomainPtr
xenUnifiedDomainLookupByID (virConnectPtr conn, int id)
{
633
    GET_PRIVATE(conn);
634
    virDomainPtr ret;
635

636 637 638 639 640
    /* Reset any connection-level errors in virterror first, in case
     * there is one hanging around from a previous call.
     */
    virConnResetLastError (conn);

641 642 643 644 645 646 647
    /* 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;
    }

648 649 650 651 652 653
    /* Try xend. */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
        ret = xenDaemonLookupByID (conn, id);
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }
654

655
    /* Not found. */
656
    xenUnifiedError(VIR_ERR_NO_DOMAIN, __FUNCTION__);
657
    return NULL;
658 659 660 661
}

static virDomainPtr
xenUnifiedDomainLookupByUUID (virConnectPtr conn,
662
                              const unsigned char *uuid)
663
{
664
    GET_PRIVATE(conn);
665
    virDomainPtr ret;
666

667 668 669 670 671
    /* Reset any connection-level errors in virterror first, in case
     * there is one hanging around from a previous call.
     */
    virConnResetLastError (conn);

672 673 674 675 676 677 678
    /* 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;
    }

679 680 681 682 683 684
    /* Try xend. */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
        ret = xenDaemonLookupByUUID (conn, uuid);
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }
685

686 687 688 689 690 691 692 693
    /* 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. */
694
    xenUnifiedError(VIR_ERR_NO_DOMAIN, __FUNCTION__);
695
    return NULL;
696 697 698 699
}

static virDomainPtr
xenUnifiedDomainLookupByName (virConnectPtr conn,
700
                              const char *name)
701
{
702
    GET_PRIVATE(conn);
703
    virDomainPtr ret;
704

705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
    /* 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;
    }
730

731
    /* Not found. */
732
    xenUnifiedError(VIR_ERR_NO_DOMAIN, __FUNCTION__);
733
    return NULL;
734 735
}

736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754

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
755
xenUnifiedDomainIsPersistent(virDomainPtr dom)
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
{
    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) {
786
                        virReportOOMError();
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
                        goto done;
                    }
                    if (access(path, R_OK) == 0)
                        ret = 1;
                    else if (errno == ENOENT)
                        ret = 0;
                }
            }
        }
    }

done:
    if (currdom)
        virDomainFree(currdom);

    return ret;
}

805 806 807 808 809 810
static int
xenUnifiedDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

811 812 813
static int
xenUnifiedDomainSuspend (virDomainPtr dom)
{
814
    GET_PRIVATE(dom->conn);
815
    int i;
816

817 818 819
    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
820
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
821
        if (i != XEN_UNIFIED_HYPERVISOR_OFFSET &&
822
            priv->opened[i] &&
E
Eric Blake 已提交
823 824
            drivers[i]->xenDomainSuspend &&
            drivers[i]->xenDomainSuspend (dom) == 0)
825
            return 0;
826

827
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] &&
E
Eric Blake 已提交
828
        xenHypervisorPauseDomain(dom) == 0)
829
        return 0;
830

831
    return -1;
832 833 834 835 836
}

static int
xenUnifiedDomainResume (virDomainPtr dom)
{
837
    GET_PRIVATE(dom->conn);
838
    int i;
839

840 841 842
    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
843
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
844
        if (i != XEN_UNIFIED_HYPERVISOR_OFFSET &&
845
            priv->opened[i] &&
E
Eric Blake 已提交
846 847
            drivers[i]->xenDomainResume &&
            drivers[i]->xenDomainResume (dom) == 0)
848
            return 0;
849

850
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] &&
E
Eric Blake 已提交
851
        xenHypervisorResumeDomain(dom) == 0)
852
        return 0;
853

854
    return -1;
855 856 857 858 859
}

static int
xenUnifiedDomainShutdown (virDomainPtr dom)
{
860
    GET_PRIVATE(dom->conn);
861
    int i;
862

863 864
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
E
Eric Blake 已提交
865 866
            drivers[i]->xenDomainShutdown &&
            drivers[i]->xenDomainShutdown (dom) == 0)
867
            return 0;
868

869
    return -1;
870 871 872 873 874
}

static int
xenUnifiedDomainReboot (virDomainPtr dom, unsigned int flags)
{
875
    GET_PRIVATE(dom->conn);
876
    int i;
877

878 879
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
E
Eric Blake 已提交
880 881
            drivers[i]->xenDomainReboot &&
            drivers[i]->xenDomainReboot (dom, flags) == 0)
882
            return 0;
883

884
    return -1;
885 886
}

887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
static int
xenUnifiedDomainDestroyFlags(virDomainPtr dom,
                             unsigned int flags)
{
    GET_PRIVATE(dom->conn);
    int i;

    virCheckFlags(0, -1);

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

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

    return -1;
}

913 914 915 916 917 918
static int
xenUnifiedDomainDestroy(virDomainPtr dom)
{
    return xenUnifiedDomainDestroyFlags(dom, 0);
}

919 920 921
static char *
xenUnifiedDomainGetOSType (virDomainPtr dom)
{
922
    GET_PRIVATE(dom->conn);
923 924
    int i;
    char *ret;
925

926
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
927 928
        if (priv->opened[i] && drivers[i]->xenDomainGetOSType) {
            ret = drivers[i]->xenDomainGetOSType (dom);
929 930
            if (ret) return ret;
        }
931

932
    return NULL;
933 934 935 936 937
}

static unsigned long
xenUnifiedDomainGetMaxMemory (virDomainPtr dom)
{
938
    GET_PRIVATE(dom->conn);
939 940
    int i;
    unsigned long ret;
941

942
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
943 944
        if (priv->opened[i] && drivers[i]->xenDomainGetMaxMemory) {
            ret = drivers[i]->xenDomainGetMaxMemory (dom);
945 946
            if (ret != 0) return ret;
        }
947

948
    return 0;
949 950 951 952 953
}

static int
xenUnifiedDomainSetMaxMemory (virDomainPtr dom, unsigned long memory)
{
954
    GET_PRIVATE(dom->conn);
955
    int i;
956

957 958 959 960 961 962
    /* Prefer xend for setting max memory */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
        if (xenDaemonDomainSetMaxMemory (dom, memory) == 0)
            return 0;
    }

963
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
964 965
        if (i != XEN_UNIFIED_XEND_OFFSET &&
            priv->opened[i] &&
E
Eric Blake 已提交
966 967
            drivers[i]->xenDomainSetMaxMemory &&
            drivers[i]->xenDomainSetMaxMemory (dom, memory) == 0)
968
            return 0;
969

970
    return -1;
971 972 973 974 975
}

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

979 980
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
E
Eric Blake 已提交
981 982
            drivers[i]->xenDomainSetMemory &&
            drivers[i]->xenDomainSetMemory (dom, memory) == 0)
983
            return 0;
984

985
    return -1;
986 987 988 989 990
}

static int
xenUnifiedDomainGetInfo (virDomainPtr dom, virDomainInfoPtr info)
{
991
    GET_PRIVATE(dom->conn);
992
    int i;
993

994 995
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
E
Eric Blake 已提交
996 997
            drivers[i]->xenDomainGetInfo &&
            drivers[i]->xenDomainGetInfo (dom, info) == 0)
998
            return 0;
999

1000
    return -1;
1001 1002
}

1003 1004 1005 1006 1007 1008 1009 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
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;
}

1044
static int
1045 1046
xenUnifiedDomainSaveFlags(virDomainPtr dom, const char *to, const char *dxml,
                          unsigned int flags)
1047
{
1048
    GET_PRIVATE(dom->conn);
1049

1050 1051 1052 1053 1054 1055 1056
    virCheckFlags(0, -1);
    if (dxml) {
        xenUnifiedError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                        _("xml modification unsupported"));
        return -1;
    }

1057 1058
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        return xenDaemonDomainSave(dom, to);
1059
    return -1;
1060 1061 1062
}

static int
1063 1064 1065 1066 1067 1068 1069 1070
xenUnifiedDomainSave(virDomainPtr dom, const char *to)
{
    return xenUnifiedDomainSaveFlags(dom, to, NULL, 0);
}

static int
xenUnifiedDomainRestoreFlags(virConnectPtr conn, const char *from,
                             const char *dxml, unsigned int flags)
1071
{
1072
    GET_PRIVATE(conn);
1073

1074 1075 1076 1077 1078 1079 1080
    virCheckFlags(0, -1);
    if (dxml) {
        xenUnifiedError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                        _("xml modification unsupported"));
        return -1;
    }

1081 1082
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        return xenDaemonDomainRestore(conn, from);
1083
    return -1;
1084 1085
}

1086 1087 1088 1089 1090 1091
static int
xenUnifiedDomainRestore (virConnectPtr conn, const char *from)
{
    return xenUnifiedDomainRestoreFlags(conn, from, NULL, 0);
}

1092
static int
1093
xenUnifiedDomainCoreDump (virDomainPtr dom, const char *to, unsigned int flags)
1094
{
1095
    GET_PRIVATE(dom->conn);
1096

1097 1098
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        return xenDaemonDomainCoreDump(dom, to, flags);
1099
    return -1;
1100 1101 1102
}

static int
1103 1104
xenUnifiedDomainSetVcpusFlags (virDomainPtr dom, unsigned int nvcpus,
                               unsigned int flags)
1105
{
1106
    GET_PRIVATE(dom->conn);
1107
    int ret;
1108

1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
    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);
1125 1126 1127
        return -1;
    }

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

1144
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1145
    return -1;
1146 1147
}

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

1165 1166
static int
xenUnifiedDomainPinVcpu (virDomainPtr dom, unsigned int vcpu,
1167
                         unsigned char *cpumap, int maplen)
1168
{
1169
    GET_PRIVATE(dom->conn);
1170
    int i;
1171

1172 1173
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
E
Eric Blake 已提交
1174 1175
            drivers[i]->xenDomainPinVcpu &&
            drivers[i]->xenDomainPinVcpu (dom, vcpu, cpumap, maplen) == 0)
1176
            return 0;
1177

1178
    return -1;
1179 1180 1181 1182
}

static int
xenUnifiedDomainGetVcpus (virDomainPtr dom,
1183 1184
                          virVcpuInfoPtr info, int maxinfo,
                          unsigned char *cpumaps, int maplen)
1185
{
1186
    GET_PRIVATE(dom->conn);
1187
    int i, ret;
1188

1189
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1190 1191
        if (priv->opened[i] && drivers[i]->xenDomainGetVcpus) {
            ret = drivers[i]->xenDomainGetVcpus (dom, info, maxinfo, cpumaps, maplen);
1192 1193 1194
            if (ret > 0)
                return ret;
        }
1195
    return -1;
1196 1197 1198
}

static int
1199
xenUnifiedDomainGetVcpusFlags (virDomainPtr dom, unsigned int flags)
1200
{
1201
    GET_PRIVATE(dom->conn);
1202
    int ret;
1203

1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
    virCheckFlags(VIR_DOMAIN_VCPU_LIVE |
                  VIR_DOMAIN_VCPU_CONFIG |
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

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

1221
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1222
    return -1;
1223 1224
}

1225 1226 1227 1228 1229 1230 1231
static int
xenUnifiedDomainGetMaxVcpus (virDomainPtr dom)
{
    return xenUnifiedDomainGetVcpusFlags(dom, (VIR_DOMAIN_VCPU_LIVE |
                                               VIR_DOMAIN_VCPU_MAXIMUM));
}

1232
static char *
1233
xenUnifiedDomainGetXMLDesc(virDomainPtr dom, unsigned int flags)
1234
{
1235
    GET_PRIVATE(dom->conn);
1236

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

1252
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1253
    return NULL;
1254 1255
}

1256 1257 1258 1259 1260

static char *
xenUnifiedDomainXMLFromNative(virConnectPtr conn,
                              const char *format,
                              const char *config,
E
Eric Blake 已提交
1261
                              unsigned int flags)
1262 1263 1264 1265
{
    virDomainDefPtr def = NULL;
    char *ret = NULL;
    virConfPtr conf = NULL;
1266 1267 1268
    int id;
    char * tty;
    int vncport;
1269 1270
    GET_PRIVATE(conn);

E
Eric Blake 已提交
1271 1272
    virCheckFlags(0, NULL);

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

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

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

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

cleanup:
    virDomainDefFree(def);
1302 1303
    if (conf)
        virConfFree(conf);
1304 1305 1306 1307 1308 1309 1310 1311 1312
    return ret;
}


#define MAX_CONFIG_SIZE (1024 * 65)
static char *
xenUnifiedDomainXMLToNative(virConnectPtr conn,
                            const char *format,
                            const char *xmlData,
E
Eric Blake 已提交
1313
                            unsigned int flags)
1314 1315 1316 1317 1318 1319
{
    virDomainDefPtr def = NULL;
    char *ret = NULL;
    virConfPtr conf = NULL;
    GET_PRIVATE(conn);

E
Eric Blake 已提交
1320 1321
    virCheckFlags(0, NULL);

1322 1323
    if (STRNEQ(format, XEN_CONFIG_FORMAT_XM) &&
        STRNEQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
1324
        xenUnifiedError(VIR_ERR_INVALID_ARG,
1325 1326 1327 1328
                        _("unsupported config type %s"), format);
        goto cleanup;
    }

M
Matthias Bolte 已提交
1329 1330
    if (!(def = virDomainDefParseString(priv->caps, xmlData,
                                        1 << VIR_DOMAIN_VIRT_XEN, 0)))
1331 1332 1333 1334
        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
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);

E
Eric Blake 已提交
1372 1373
    virCheckFlags(XEN_MIGRATION_FLAGS, -1);

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

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

E
Eric Blake 已提交
1394 1395
    virCheckFlags(XEN_MIGRATION_FLAGS, -1);

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

1400
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1401 1402 1403 1404 1405 1406 1407 1408 1409
    return -1;
}

static virDomainPtr
xenUnifiedDomainMigrateFinish (virConnectPtr dconn,
                               const char *dname,
                               const char *cookie ATTRIBUTE_UNUSED,
                               int cookielen ATTRIBUTE_UNUSED,
                               const char *uri ATTRIBUTE_UNUSED,
1410
                               unsigned long flags)
1411
{
1412 1413 1414 1415
    virDomainPtr dom = NULL;
    char *domain_xml = NULL;
    virDomainPtr dom_new = NULL;

E
Eric Blake 已提交
1416 1417
    virCheckFlags(XEN_MIGRATION_FLAGS, NULL);

1418 1419 1420 1421 1422 1423
    dom = xenUnifiedDomainLookupByName (dconn, dname);
    if (! dom) {
        return NULL;
    }

    if (flags & VIR_MIGRATE_PERSIST_DEST) {
1424
        domain_xml = xenDaemonDomainGetXMLDesc(dom, 0, NULL);
1425
        if (! domain_xml) {
1426
            xenUnifiedError(VIR_ERR_MIGRATE_PERSIST_FAILED,
1427
                            "%s", _("failed to get XML representation of migrated domain"));
1428 1429 1430 1431 1432
            goto failure;
        }

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

1455 1456
static int
xenUnifiedListDefinedDomains (virConnectPtr conn, char **const names,
1457
                              int maxnames)
1458
{
1459
    GET_PRIVATE(conn);
1460 1461
    int i;
    int ret;
1462

1463
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1464 1465
        if (priv->opened[i] && drivers[i]->xenListDefinedDomains) {
            ret = drivers[i]->xenListDefinedDomains (conn, names, maxnames);
1466 1467
            if (ret >= 0) return ret;
        }
1468

1469
    return -1;
1470 1471 1472 1473 1474
}

static int
xenUnifiedNumOfDefinedDomains (virConnectPtr conn)
{
1475
    GET_PRIVATE(conn);
1476 1477
    int i;
    int ret;
1478

1479
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1480 1481
        if (priv->opened[i] && drivers[i]->xenNumOfDefinedDomains) {
            ret = drivers[i]->xenNumOfDefinedDomains (conn);
1482 1483
            if (ret >= 0) return ret;
        }
1484

1485
    return -1;
1486 1487 1488
}

static int
1489
xenUnifiedDomainCreateWithFlags (virDomainPtr dom, unsigned int flags)
1490
{
1491
    GET_PRIVATE(dom->conn);
1492
    int i;
1493

1494 1495
    virCheckFlags(0, -1);

1496
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1497 1498
        if (priv->opened[i] && drivers[i]->xenDomainCreate &&
            drivers[i]->xenDomainCreate (dom) == 0)
1499
            return 0;
1500

1501
    return -1;
1502 1503
}

1504 1505 1506 1507 1508 1509
static int
xenUnifiedDomainCreate (virDomainPtr dom)
{
    return xenUnifiedDomainCreateWithFlags(dom, 0);
}

1510 1511 1512
static virDomainPtr
xenUnifiedDomainDefineXML (virConnectPtr conn, const char *xml)
{
1513
    GET_PRIVATE(conn);
1514 1515
    int i;
    virDomainPtr ret;
1516

1517
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1518 1519
        if (priv->opened[i] && drivers[i]->xenDomainDefineXML) {
            ret = drivers[i]->xenDomainDefineXML (conn, xml);
1520 1521
            if (ret) return ret;
        }
1522

1523
    return NULL;
1524 1525 1526
}

static int
1527
xenUnifiedDomainUndefineFlags (virDomainPtr dom, unsigned int flags)
1528
{
1529
    GET_PRIVATE(dom->conn);
1530
    int i;
1531

1532
    virCheckFlags(0, -1);
1533
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1534 1535
        if (priv->opened[i] && drivers[i]->xenDomainUndefine &&
            drivers[i]->xenDomainUndefine (dom) == 0)
1536
            return 0;
1537

1538
    return -1;
1539 1540
}

1541 1542 1543 1544 1545
static int
xenUnifiedDomainUndefine (virDomainPtr dom) {
    return xenUnifiedDomainUndefineFlags(dom, 0);
}

1546
static int
1547
xenUnifiedDomainAttachDevice (virDomainPtr dom, const char *xml)
1548 1549 1550
{
    GET_PRIVATE(dom->conn);
    int i;
1551
    unsigned int flags = VIR_DOMAIN_DEVICE_MODIFY_LIVE;
1552

1553 1554 1555 1556 1557 1558 1559 1560
    /*
     * 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;
1561 1562

    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1563 1564
        if (priv->opened[i] && drivers[i]->xenDomainAttachDeviceFlags &&
            drivers[i]->xenDomainAttachDeviceFlags(dom, xml, flags) == 0)
1565 1566 1567 1568 1569 1570 1571 1572
            return 0;

    return -1;
}

static int
xenUnifiedDomainAttachDeviceFlags (virDomainPtr dom, const char *xml,
                                   unsigned int flags)
1573
{
1574
    GET_PRIVATE(dom->conn);
1575
    int i;
1576

1577
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1578 1579
        if (priv->opened[i] && drivers[i]->xenDomainAttachDeviceFlags &&
            drivers[i]->xenDomainAttachDeviceFlags(dom, xml, flags) == 0)
1580
            return 0;
1581

1582
    return -1;
1583 1584 1585
}

static int
1586
xenUnifiedDomainDetachDevice (virDomainPtr dom, const char *xml)
1587 1588 1589
{
    GET_PRIVATE(dom->conn);
    int i;
1590
    unsigned int flags = VIR_DOMAIN_DEVICE_MODIFY_LIVE;
1591

1592 1593 1594 1595 1596 1597 1598 1599
    /*
     * 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;
1600 1601

    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1602 1603
        if (priv->opened[i] && drivers[i]->xenDomainDetachDeviceFlags &&
            drivers[i]->xenDomainDetachDeviceFlags(dom, xml, flags) == 0)
1604 1605 1606 1607 1608 1609 1610 1611
            return 0;

    return -1;
}

static int
xenUnifiedDomainDetachDeviceFlags (virDomainPtr dom, const char *xml,
                                   unsigned int flags)
1612
{
1613
    GET_PRIVATE(dom->conn);
1614
    int i;
1615

1616
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1617 1618
        if (priv->opened[i] && drivers[i]->xenDomainDetachDeviceFlags &&
            drivers[i]->xenDomainDetachDeviceFlags(dom, xml, flags) == 0)
1619
            return 0;
1620

1621
    return -1;
1622 1623
}

1624 1625 1626 1627 1628 1629
static int
xenUnifiedDomainUpdateDeviceFlags (virDomainPtr dom, const char *xml,
                                   unsigned int flags)
{
    GET_PRIVATE(dom->conn);

1630 1631
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        return xenDaemonUpdateDeviceFlags(dom, xml, flags);
1632 1633 1634
    return -1;
}

1635 1636 1637 1638 1639
static int
xenUnifiedDomainGetAutostart (virDomainPtr dom, int *autostart)
{
    GET_PRIVATE(dom->conn);

1640 1641 1642 1643 1644 1645 1646
    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);
    }
1647

1648
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1649 1650 1651 1652 1653 1654 1655 1656
    return -1;
}

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

1657 1658 1659 1660 1661 1662 1663
    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);
    }
1664

1665
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1666 1667 1668
    return -1;
}

1669 1670 1671 1672 1673 1674 1675 1676
static char *
xenUnifiedDomainGetSchedulerType (virDomainPtr dom, int *nparams)
{
    GET_PRIVATE(dom->conn);
    int i;
    char *schedulertype;

    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; i++) {
E
Eric Blake 已提交
1677 1678
        if (priv->opened[i] && drivers[i]->xenDomainGetSchedulerType) {
            schedulertype = drivers[i]->xenDomainGetSchedulerType (dom, nparams);
1679 1680
            if (schedulertype != NULL)
                return(schedulertype);
1681 1682 1683 1684 1685 1686
        }
    }
    return(NULL);
}

static int
1687 1688 1689 1690
xenUnifiedDomainGetSchedulerParametersFlags(virDomainPtr dom,
                                            virTypedParameterPtr params,
                                            int *nparams,
                                            unsigned int flags)
1691 1692 1693 1694
{
    GET_PRIVATE(dom->conn);
    int i, ret;

1695 1696
    virCheckFlags(0, -1);

1697
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i) {
E
Eric Blake 已提交
1698 1699
        if (priv->opened[i] && drivers[i]->xenDomainGetSchedulerParameters) {
           ret = drivers[i]->xenDomainGetSchedulerParameters(dom, params, nparams);
1700 1701 1702
           if (ret == 0)
               return(0);
        }
1703 1704 1705 1706 1707
    }
    return(-1);
}

static int
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720
xenUnifiedDomainGetSchedulerParameters(virDomainPtr dom,
                                       virTypedParameterPtr params,
                                       int *nparams)
{
    return xenUnifiedDomainGetSchedulerParametersFlags(dom, params,
                                                       nparams, 0);
}

static int
xenUnifiedDomainSetSchedulerParametersFlags(virDomainPtr dom,
                                            virTypedParameterPtr params,
                                            int nparams,
                                            unsigned int flags)
1721 1722 1723 1724
{
    GET_PRIVATE(dom->conn);
    int i, ret;

1725 1726
    virCheckFlags(0, -1);

1727 1728
    /* do the hypervisor call last to get better error */
    for (i = XEN_UNIFIED_NR_DRIVERS - 1; i >= 0; i--) {
E
Eric Blake 已提交
1729 1730
        if (priv->opened[i] && drivers[i]->xenDomainSetSchedulerParameters) {
           ret = drivers[i]->xenDomainSetSchedulerParameters(dom, params, nparams);
1731 1732 1733
           if (ret == 0)
               return 0;
        }
1734 1735 1736 1737 1738
    }

    return(-1);
}

1739 1740 1741 1742 1743 1744 1745 1746 1747
static int
xenUnifiedDomainSetSchedulerParameters(virDomainPtr dom,
                                       virTypedParameterPtr params,
                                       int nparams)
{
    return xenUnifiedDomainSetSchedulerParametersFlags(dom, params,
                                                       nparams, 0);
}

1748 1749 1750 1751 1752 1753 1754 1755 1756
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);

1757
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
    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);

1770
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1771 1772 1773
    return -1;
}

R
Richard W.M. Jones 已提交
1774 1775 1776
static int
xenUnifiedDomainBlockPeek (virDomainPtr dom, const char *path,
                           unsigned long long offset, size_t size,
E
Eric Blake 已提交
1777
                           void *buffer, unsigned int flags)
R
Richard W.M. Jones 已提交
1778 1779 1780 1781
{
    int r;
    GET_PRIVATE (dom->conn);

E
Eric Blake 已提交
1782 1783
    virCheckFlags(0, -1);

R
Richard W.M. Jones 已提交
1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794
    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;
    }

1795
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
R
Richard W.M. Jones 已提交
1796 1797 1798
    return -1;
}

1799 1800 1801 1802 1803 1804 1805
static int
xenUnifiedNodeGetCellsFreeMemory (virConnectPtr conn, unsigned long long *freeMems,
                                  int startCell, int maxCells)
{
    GET_PRIVATE (conn);

    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET])
1806
        return xenHypervisorNodeGetCellsFreeMemory (conn, freeMems,
1807 1808
                                                    startCell, maxCells);

1809
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1810 1811 1812
    return -1;
}

1813 1814 1815 1816 1817 1818 1819 1820
static unsigned long long
xenUnifiedNodeGetFreeMemory (virConnectPtr conn)
{
    unsigned long long freeMem = 0;
    int ret;
    GET_PRIVATE (conn);

    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET]) {
1821
        ret = xenHypervisorNodeGetCellsFreeMemory (conn, &freeMem,
1822
                                                    -1, 1);
1823 1824 1825
        if (ret != 1)
            return (0);
        return(freeMem);
1826 1827
    }

1828
    xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1829 1830 1831
    return(0);
}

1832

1833
static int
1834 1835 1836 1837
xenUnifiedDomainEventRegister(virConnectPtr conn,
                              virConnectDomainEventCallback callback,
                              void *opaque,
                              virFreeCallback freefunc)
1838
{
D
Daniel P. Berrange 已提交
1839 1840
    GET_PRIVATE (conn);

1841
    int ret;
D
Daniel P. Berrange 已提交
1842
    xenUnifiedLock(priv);
1843

1844
    if (priv->xsWatch == -1) {
1845
        xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
D
Daniel P. Berrange 已提交
1846
        xenUnifiedUnlock(priv);
1847 1848 1849
        return -1;
    }

1850 1851 1852
    ret = virDomainEventCallbackListAdd(conn, priv->domainEventCallbacks,
                                        callback, opaque, freefunc);

D
Daniel P. Berrange 已提交
1853
    xenUnifiedUnlock(priv);
1854
    return (ret);
1855 1856
}

1857

1858
static int
1859 1860
xenUnifiedDomainEventDeregister(virConnectPtr conn,
                                virConnectDomainEventCallback callback)
1861 1862 1863
{
    int ret;
    GET_PRIVATE (conn);
D
Daniel P. Berrange 已提交
1864 1865
    xenUnifiedLock(priv);

1866
    if (priv->xsWatch == -1) {
1867
        xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
D
Daniel P. Berrange 已提交
1868
        xenUnifiedUnlock(priv);
1869 1870 1871
        return -1;
    }

D
Daniel P. Berrange 已提交
1872 1873 1874 1875 1876 1877
    if (priv->domainEventDispatching)
        ret = virDomainEventCallbackListMarkDelete(conn, priv->domainEventCallbacks,
                                                   callback);
    else
        ret = virDomainEventCallbackListRemove(conn, priv->domainEventCallbacks,
                                               callback);
1878

D
Daniel P. Berrange 已提交
1879
    xenUnifiedUnlock(priv);
1880 1881 1882
    return ret;
}

1883

1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897
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) {
1898
        xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
        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) {
1920
        xenUnifiedError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936
        xenUnifiedUnlock(priv);
        return -1;
    }

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

    xenUnifiedUnlock(priv);
    return ret;
}


1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
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;

1953
    def = virNodeDeviceDefParseString(xml, EXISTING_DEVICE);
1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
    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) {
1971
        xenUnifiedError(VIR_ERR_INVALID_ARG,
1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992
                        _("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;

1993
    pci = pciGetDevice(domain, bus, slot, function);
1994 1995 1996
    if (!pci)
        return -1;

1997
    if (pciDettachDevice(pci, NULL) < 0)
1998 1999 2000 2001
        goto out;

    ret = 0;
out:
2002
    pciFreeDevice(pci);
2003 2004 2005
    return ret;
}

2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
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;
}

2064 2065 2066 2067 2068 2069
static int
xenUnifiedNodeDeviceReAttach (virNodeDevicePtr dev)
{
    pciDevice *pci;
    unsigned domain, bus, slot, function;
    int ret = -1;
2070
    int domid;
2071 2072 2073 2074

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

2075
    pci = pciGetDevice(domain, bus, slot, function);
2076 2077 2078
    if (!pci)
        return -1;

2079 2080 2081 2082 2083 2084 2085 2086
    /* 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;
    }

2087
    if (pciReAttachDevice(pci, NULL) < 0)
2088 2089 2090 2091
        goto out;

    ret = 0;
out:
2092
    pciFreeDevice(pci);
2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105
    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;

2106
    pci = pciGetDevice(domain, bus, slot, function);
2107 2108 2109
    if (!pci)
        return -1;

2110
    if (pciResetDevice(pci, NULL, NULL) < 0)
2111 2112 2113 2114
        goto out;

    ret = 0;
out:
2115
    pciFreeDevice(pci);
2116 2117 2118 2119
    return ret;
}


2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159
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;
    }

2160
    if (chr->source.type != VIR_DOMAIN_CHR_TYPE_PTY) {
2161 2162 2163 2164 2165
        xenUnifiedError(VIR_ERR_INTERNAL_ERROR,
                        _("character device %s is not using a PTY"), devname);
        goto cleanup;
    }

2166 2167
    if (virFDStreamOpenFile(st, chr->source.data.file.path,
                            0, 0, O_RDWR, false) < 0)
2168 2169 2170 2171 2172 2173 2174
        goto cleanup;

    ret = 0;
cleanup:
    virDomainDefFree(def);
    return ret;
}
E
Eric Blake 已提交
2175
/*----- Register with libvirt.c, and initialize Xen drivers. -----*/
2176 2177 2178

/* The interface which we export upwards to libvirt.c. */
static virDriver xenUnifiedDriver = {
2179 2180
    .no = VIR_DRV_XEN_UNIFIED,
    .name = "Xen",
2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200
    .open = xenUnifiedOpen, /* 0.0.3 */
    .close = xenUnifiedClose, /* 0.0.3 */
    .supports_feature = xenUnifiedSupportsFeature, /* 0.3.2 */
    .type = xenUnifiedType, /* 0.0.3 */
    .version = xenUnifiedGetVersion, /* 0.0.3 */
    .getHostname = virGetHostname, /* 0.7.3 */
    .getMaxVcpus = xenUnifiedGetMaxVcpus, /* 0.2.1 */
    .nodeGetInfo = xenUnifiedNodeGetInfo, /* 0.1.0 */
    .getCapabilities = xenUnifiedGetCapabilities, /* 0.2.1 */
    .listDomains = xenUnifiedListDomains, /* 0.0.3 */
    .numOfDomains = xenUnifiedNumOfDomains, /* 0.0.3 */
    .domainCreateXML = xenUnifiedDomainCreateXML, /* 0.0.3 */
    .domainLookupByID = xenUnifiedDomainLookupByID, /* 0.0.3 */
    .domainLookupByUUID = xenUnifiedDomainLookupByUUID, /* 0.0.5 */
    .domainLookupByName = xenUnifiedDomainLookupByName, /* 0.0.3 */
    .domainSuspend = xenUnifiedDomainSuspend, /* 0.0.3 */
    .domainResume = xenUnifiedDomainResume, /* 0.0.3 */
    .domainShutdown = xenUnifiedDomainShutdown, /* 0.0.3 */
    .domainReboot = xenUnifiedDomainReboot, /* 0.1.0 */
    .domainDestroy = xenUnifiedDomainDestroy, /* 0.0.3 */
2201
    .domainDestroyFlags = xenUnifiedDomainDestroyFlags, /* 0.9.4 */
2202 2203 2204 2205 2206 2207 2208
    .domainGetOSType = xenUnifiedDomainGetOSType, /* 0.0.3 */
    .domainGetMaxMemory = xenUnifiedDomainGetMaxMemory, /* 0.0.3 */
    .domainSetMaxMemory = xenUnifiedDomainSetMaxMemory, /* 0.0.3 */
    .domainSetMemory = xenUnifiedDomainSetMemory, /* 0.1.1 */
    .domainGetInfo = xenUnifiedDomainGetInfo, /* 0.0.3 */
    .domainGetState = xenUnifiedDomainGetState, /* 0.9.2 */
    .domainSave = xenUnifiedDomainSave, /* 0.0.3 */
2209
    .domainSaveFlags = xenUnifiedDomainSaveFlags, /* 0.9.4 */
2210
    .domainRestore = xenUnifiedDomainRestore, /* 0.0.3 */
2211
    .domainRestoreFlags = xenUnifiedDomainRestoreFlags, /* 0.9.4 */
2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227
    .domainCoreDump = xenUnifiedDomainCoreDump, /* 0.1.9 */
    .domainSetVcpus = xenUnifiedDomainSetVcpus, /* 0.1.4 */
    .domainSetVcpusFlags = xenUnifiedDomainSetVcpusFlags, /* 0.8.5 */
    .domainGetVcpusFlags = xenUnifiedDomainGetVcpusFlags, /* 0.8.5 */
    .domainPinVcpu = xenUnifiedDomainPinVcpu, /* 0.1.4 */
    .domainGetVcpus = xenUnifiedDomainGetVcpus, /* 0.1.4 */
    .domainGetMaxVcpus = xenUnifiedDomainGetMaxVcpus, /* 0.2.1 */
    .domainGetXMLDesc = xenUnifiedDomainGetXMLDesc, /* 0.0.3 */
    .domainXMLFromNative = xenUnifiedDomainXMLFromNative, /* 0.6.4 */
    .domainXMLToNative = xenUnifiedDomainXMLToNative, /* 0.6.4 */
    .listDefinedDomains = xenUnifiedListDefinedDomains, /* 0.1.1 */
    .numOfDefinedDomains = xenUnifiedNumOfDefinedDomains, /* 0.1.5 */
    .domainCreate = xenUnifiedDomainCreate, /* 0.1.1 */
    .domainCreateWithFlags = xenUnifiedDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = xenUnifiedDomainDefineXML, /* 0.1.1 */
    .domainUndefine = xenUnifiedDomainUndefine, /* 0.1.1 */
2228
    .domainUndefineFlags = xenUnifiedDomainUndefineFlags, /* 0.9.4 */
2229 2230 2231 2232 2233 2234 2235 2236 2237
    .domainAttachDevice = xenUnifiedDomainAttachDevice, /* 0.1.9 */
    .domainAttachDeviceFlags = xenUnifiedDomainAttachDeviceFlags, /* 0.7.7 */
    .domainDetachDevice = xenUnifiedDomainDetachDevice, /* 0.1.9 */
    .domainDetachDeviceFlags = xenUnifiedDomainDetachDeviceFlags, /* 0.7.7 */
    .domainUpdateDeviceFlags = xenUnifiedDomainUpdateDeviceFlags, /* 0.8.0 */
    .domainGetAutostart = xenUnifiedDomainGetAutostart, /* 0.4.4 */
    .domainSetAutostart = xenUnifiedDomainSetAutostart, /* 0.4.4 */
    .domainGetSchedulerType = xenUnifiedDomainGetSchedulerType, /* 0.2.3 */
    .domainGetSchedulerParameters = xenUnifiedDomainGetSchedulerParameters, /* 0.2.3 */
2238
    .domainGetSchedulerParametersFlags = xenUnifiedDomainGetSchedulerParametersFlags, /* 0.9.2 */
2239
    .domainSetSchedulerParameters = xenUnifiedDomainSetSchedulerParameters, /* 0.2.3 */
2240
    .domainSetSchedulerParametersFlags = xenUnifiedDomainSetSchedulerParametersFlags, /* 0.9.2 */
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261
    .domainMigratePrepare = xenUnifiedDomainMigratePrepare, /* 0.3.2 */
    .domainMigratePerform = xenUnifiedDomainMigratePerform, /* 0.3.2 */
    .domainMigrateFinish = xenUnifiedDomainMigrateFinish, /* 0.3.2 */
    .domainBlockStats = xenUnifiedDomainBlockStats, /* 0.3.2 */
    .domainInterfaceStats = xenUnifiedDomainInterfaceStats, /* 0.3.2 */
    .domainBlockPeek = xenUnifiedDomainBlockPeek, /* 0.4.4 */
    .nodeGetCellsFreeMemory = xenUnifiedNodeGetCellsFreeMemory, /* 0.3.3 */
    .nodeGetFreeMemory = xenUnifiedNodeGetFreeMemory, /* 0.3.3 */
    .domainEventRegister = xenUnifiedDomainEventRegister, /* 0.5.0 */
    .domainEventDeregister = xenUnifiedDomainEventDeregister, /* 0.5.0 */
    .nodeDeviceDettach = xenUnifiedNodeDeviceDettach, /* 0.6.1 */
    .nodeDeviceReAttach = xenUnifiedNodeDeviceReAttach, /* 0.6.1 */
    .nodeDeviceReset = xenUnifiedNodeDeviceReset, /* 0.6.1 */
    .isEncrypted = xenUnifiedIsEncrypted, /* 0.7.3 */
    .isSecure = xenUnifiedIsSecure, /* 0.7.3 */
    .domainIsActive = xenUnifiedDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = xenUnifiedDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = xenUnifiedDomainIsUpdated, /* 0.8.6 */
    .domainEventRegisterAny = xenUnifiedDomainEventRegisterAny, /* 0.8.0 */
    .domainEventDeregisterAny = xenUnifiedDomainEventDeregisterAny, /* 0.8.0 */
    .domainOpenConsole = xenUnifiedDomainOpenConsole, /* 0.8.6 */
2262 2263
};

2264
/**
2265
 * xenRegister:
2266 2267 2268 2269 2270
 *
 * Register xen related drivers
 *
 * Returns the driver priority or -1 in case of error.
 */
2271
int
2272
xenRegister (void)
2273
{
2274 2275
    /* Ignore failures here. */
    (void) xenHypervisorInit ();
2276

J
John Levon 已提交
2277 2278 2279 2280
#ifdef WITH_LIBVIRTD
    if (virRegisterStateDriver (&state_driver) == -1) return -1;
#endif

2281
    return virRegisterDriver (&xenUnifiedDriver);
2282 2283
}

2284 2285 2286 2287 2288 2289 2290 2291 2292
/**
 * xenUnifiedDomainInfoListFree:
 *
 * Free the Domain Info List
 */
void
xenUnifiedDomainInfoListFree(xenUnifiedDomainInfoListPtr list)
{
    int i;
J
John Levon 已提交
2293 2294 2295 2296

    if (list == NULL)
        return;

2297 2298 2299 2300
    for (i=0; i<list->count; i++) {
        VIR_FREE(list->doms[i]->name);
        VIR_FREE(list->doms[i]);
    }
2301
    VIR_FREE(list->doms);
2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323
    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)) {
2324
            VIR_DEBUG("WARNING: dom already tracked");
2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346
            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:
2347
    virReportOOMError();
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 2387 2388 2389 2390 2391 2392
    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 已提交
2393 2394 2395
static void
xenUnifiedDomainEventDispatchFunc(virConnectPtr conn,
                                  virDomainEventPtr event,
2396
                                  virConnectDomainEventGenericCallback cb,
D
Daniel P. Berrange 已提交
2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413
                                  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);
}

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

D
Daniel P. Berrange 已提交
2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441
    priv->domainEventDispatching = 1;

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

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

2442
    virDomainEventFree(event);
D
Daniel P. Berrange 已提交
2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454

    priv->domainEventDispatching = 0;
}

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

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