xen_driver.c 65.9 KB
Newer Older
1
/*
2
 * xen_driver.c: Unified Xen driver.
3
 *
4
 * Copyright (C) 2007-2012 Red Hat, Inc.
5
 *
O
Osier Yang 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library;  If not, see
 * <http://www.gnu.org/licenses/>.
19 20 21 22
 *
 * Richard W.M. Jones <rjones@redhat.com>
 */

23
#include <config.h>
24

25 26 27
/* Note:
 *
 * This driver provides a unified interface to the five
28
 * separate underlying Xen drivers (xen_internal,
29 30 31 32 33 34 35
 * 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>
36
#include <string.h>
37
#include <errno.h>
38
#include <sys/types.h>
39
#include <fcntl.h>
40 41
#include <xen/dom0_ops.h>

42
#include "virterror_internal.h"
43
#include "logging.h"
44
#include "datatypes.h"
45
#include "xen_driver.h"
46

47
#include "xen_sxpr.h"
48
#include "xen_xm.h"
49
#include "xen_hypervisor.h"
50 51 52
#include "xend_internal.h"
#include "xs_internal.h"
#include "xm_internal.h"
53
#if WITH_XEN_INOTIFY
54
# include "xen_inotify.h"
55
#endif
56
#include "xml.h"
57
#include "util.h"
58
#include "memory.h"
59 60
#include "node_device_conf.h"
#include "pci.h"
61
#include "uuid.h"
62
#include "fdstream.h"
E
Eric Blake 已提交
63
#include "virfile.h"
M
Martin Kletzander 已提交
64
#include "viruri.h"
J
Jim Fehlig 已提交
65
#include "command.h"
66
#include "virnodesuspend.h"
67
#include "nodeinfo.h"
68

69 70
#define VIR_FROM_THIS VIR_FROM_XEN

71 72
static int
xenUnifiedNodeGetInfo (virConnectPtr conn, virNodeInfoPtr info);
73 74 75 76 77 78
static int
xenUnifiedDomainGetMaxVcpus (virDomainPtr dom);
static int
xenUnifiedDomainGetVcpus (virDomainPtr dom,
                          virVcpuInfoPtr info, int maxinfo,
                          unsigned char *cpumaps, int maplen);
79

80

81
/* The five Xen drivers below us. */
D
Daniel P. Berrange 已提交
82
static struct xenUnifiedDriver const * const drivers[XEN_UNIFIED_NR_DRIVERS] = {
83 84 85 86
    [XEN_UNIFIED_HYPERVISOR_OFFSET] = &xenHypervisorDriver,
    [XEN_UNIFIED_XEND_OFFSET] = &xenDaemonDriver,
    [XEN_UNIFIED_XS_OFFSET] = &xenStoreDriver,
    [XEN_UNIFIED_XM_OFFSET] = &xenXMDriver,
87 88 89
#if WITH_XEN_INOTIFY
    [XEN_UNIFIED_INOTIFY_OFFSET] = &xenInotifyDriver,
#endif
90 91
};

92
#if defined WITH_LIBVIRTD || defined __sun
J
John Levon 已提交
93
static int inside_daemon;
94
#endif
J
John Levon 已提交
95

96 97 98 99 100
/**
 * xenNumaInit:
 * @conn: pointer to the hypervisor connection
 *
 * Initializer for previous variables. We currently assume that
R
Richard W.M. Jones 已提交
101
 * the number of physical CPU and the number of NUMA cell is fixed
102 103 104 105 106
 * until reboot which might be false in future Xen implementations.
 */
static void
xenNumaInit(virConnectPtr conn) {
    virNodeInfo nodeInfo;
D
Daniel P. Berrange 已提交
107
    xenUnifiedPrivatePtr priv;
108 109 110 111 112 113
    int ret;

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

D
Daniel P. Berrange 已提交
114
    priv = conn->privateData;
115

D
Daniel P. Berrange 已提交
116 117
    priv->nbNodeCells = nodeInfo.nodes;
    priv->nbNodeCpus = nodeInfo.cpus;
118 119
}

D
Daniel P. Berrange 已提交
120

121 122 123 124 125 126 127 128 129 130 131 132 133 134
/**
 * 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 已提交
135
    int ncpus;
136 137 138 139 140 141 142 143
    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 已提交
144
    xenUnifiedPrivatePtr priv;
145 146

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

D
Daniel P. Berrange 已提交
149 150 151
    priv = dom->conn->privateData;

    if (priv->nbNodeCpus <= 0)
152
        return NULL;
153 154
    nb_vcpu = xenUnifiedDomainGetMaxVcpus(dom);
    if (nb_vcpu <= 0)
155
        return NULL;
156
    if (xenUnifiedNodeGetInfo(dom->conn, &nodeinfo) < 0)
157
        return NULL;
158

159
    if (VIR_ALLOC_N(cpulist, priv->nbNodeCpus) < 0) {
160
        virReportOOMError();
161
        goto done;
162 163
    }
    if (VIR_ALLOC_N(cpuinfo, nb_vcpu) < 0) {
164
        virReportOOMError();
165
        goto done;
166
    }
167
    cpumaplen = VIR_CPU_MAPLEN(VIR_NODEINFO_MAXCPUS(nodeinfo));
168
    if (xalloc_oversized(nb_vcpu, cpumaplen) ||
169
        VIR_ALLOC_N(cpumap, nb_vcpu * cpumaplen) < 0) {
170
        virReportOOMError();
171
        goto done;
172
    }
173 174 175

    if ((ncpus = xenUnifiedDomainGetVcpus(dom, cpuinfo, nb_vcpu,
                                          cpumap, cpumaplen)) >= 0) {
176
        for (n = 0 ; n < ncpus ; n++) {
D
Daniel P. Berrange 已提交
177
            for (m = 0 ; m < priv->nbNodeCpus; m++) {
178 179 180 181 182
                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 已提交
183
                    if (nb == priv->nbNodeCpus)
184 185 186 187 188
                        goto done;

                }
            }
        }
189
        res = virDomainCpuSetFormat(cpulist, priv->nbNodeCpus);
190 191 192
    }

done:
193 194 195
    VIR_FREE(cpulist);
    VIR_FREE(cpumap);
    VIR_FREE(cpuinfo);
196
    return res;
197 198
}

J
John Levon 已提交
199 200 201
#ifdef WITH_LIBVIRTD

static int
202
xenInitialize (int privileged ATTRIBUTE_UNUSED)
J
John Levon 已提交
203 204 205 206 207 208
{
    inside_daemon = 1;
    return 0;
}

static virStateDriver state_driver = {
209
    .name = "Xen",
J
John Levon 已提交
210 211 212 213 214
    .initialize = xenInitialize,
};

#endif

215 216 217 218 219 220 221 222 223 224 225
/*----- 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.
 */

226
static int
227 228 229 230
xenUnifiedProbe (void)
{
#ifdef __linux__
    if (virFileExists("/proc/xen"))
231
        return 1;
232
#endif
233
#ifdef __sun
234
    int fd;
235

236 237
    if ((fd = open("/dev/xen/domcaps", O_RDONLY)) >= 0) {
        VIR_FORCE_CLOSE(fd);
238
        return 1;
239 240
    }
#endif
241
    return 0;
242 243
}

J
Jim Fehlig 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
#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

261 262


263
static virDrvOpenStatus
264
xenUnifiedOpen (virConnectPtr conn, virConnectAuthPtr auth, unsigned int flags)
265
{
266
    int i, ret = VIR_DRV_OPEN_DECLINED;
267
    xenUnifiedPrivatePtr priv;
268

J
John Levon 已提交
269 270 271 272 273 274 275 276 277
#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

278 279 280 281
    if (conn->uri == NULL) {
        if (!xenUnifiedProbe())
            return VIR_DRV_OPEN_DECLINED;

282
        if (!(conn->uri = virURIParse("xen:///")))
283
            return VIR_DRV_OPEN_ERROR;
284 285 286 287 288 289 290 291 292 293 294 295
    } 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, "/")) {
296 297 298
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("unexpected Xen URI path '%s', try xen:///"),
                               conn->uri->path);
299 300
                return VIR_DRV_OPEN_ERROR;
            }
301

302 303 304 305 306 307
            /* 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 {
308
            return VIR_DRV_OPEN_DECLINED;
309 310
        }
    }
311

J
Jim Fehlig 已提交
312 313 314 315 316 317 318
#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

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

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

334
    if (!(priv->domainEvents = virDomainEventStateNew())) {
D
Daniel P. Berrange 已提交
335 336
        virMutexDestroy(&priv->lock);
        VIR_FREE(priv);
337 338
        return VIR_DRV_OPEN_ERROR;
    }
D
Daniel P. Berrange 已提交
339 340
    conn->privateData = priv;

341 342 343 344 345
    priv->handle = -1;
    priv->xendConfigVersion = -1;
    priv->xshandle = NULL;


J
John Levon 已提交
346 347
    /* Hypervisor is only run with privilege & required to succeed */
    if (xenHavePrivilege()) {
348
        VIR_DEBUG("Trying hypervisor sub-driver");
E
Eric Blake 已提交
349
        if (xenHypervisorOpen(conn, auth, flags) == VIR_DRV_OPEN_SUCCESS) {
350
            VIR_DEBUG("Activated hypervisor sub-driver");
351
            priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] = 1;
352 353
        } else {
            goto fail;
354
        }
355
    }
356

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

D
Daniel P. Berrange 已提交
390 391
    xenNumaInit(conn);

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

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

407
    return VIR_DRV_OPEN_SUCCESS;
408

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

422 423 424
#define GET_PRIVATE(conn) \
    xenUnifiedPrivatePtr priv = (xenUnifiedPrivatePtr) (conn)->privateData

425 426 427
static int
xenUnifiedClose (virConnectPtr conn)
{
428
    GET_PRIVATE(conn);
429
    int i;
430

431
    virCapabilitiesFree(priv->caps);
432
    virDomainEventStateFree(priv->domainEvents);
433

434
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
435 436
        if (priv->opened[i])
            drivers[i]->xenClose(conn);
437

D
Daniel P. Berrange 已提交
438 439
    virMutexDestroy(&priv->lock);
    VIR_FREE(conn->privateData);
440

441
    return 0;
442 443
}

444 445 446 447 448 449 450 451 452 453 454

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


455 456 457
static const char *
xenUnifiedType (virConnectPtr conn)
{
458
    GET_PRIVATE(conn);
459
    int i;
460

461
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
462 463
        if (priv->opened[i])
            return "Xen";
464

465
    return NULL;
466 467
}

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

481
static int
482
xenUnifiedGetVersion (virConnectPtr conn, unsigned long *hvVer)
483
{
484
    GET_PRIVATE(conn);
485
    int i;
486

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

493
    return -1;
494 495
}

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

516 517 518 519 520 521 522
static int
xenUnifiedIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    /* XenD reconnects for each request */
    return 1;
}

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

528
    if (type && STRCASENEQ (type, "Xen")) {
529
        virReportError(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
        virReportError(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

546 547
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        return xenDaemonNodeGetInfo(conn, info);
548
    return -1;
549 550 551 552 553
}

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

D
Daniel P. Berrange 已提交
557
    if (!(xml = virCapabilitiesFormatXML(priv->caps))) {
558
        virReportOOMError();
D
Daniel P. Berrange 已提交
559 560
        return NULL;
    }
561

D
Daniel P. Berrange 已提交
562
    return xml;
563 564 565 566 567
}

static int
xenUnifiedListDomains (virConnectPtr conn, int *ids, int maxids)
{
568
    GET_PRIVATE(conn);
569
    int ret;
570

571 572 573 574 575
    /* Try xenstore. */
    if (priv->opened[XEN_UNIFIED_XS_OFFSET]) {
        ret = xenStoreListDomains (conn, ids, maxids);
        if (ret >= 0) return ret;
    }
576

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

589
    return -1;
590 591 592 593 594
}

static int
xenUnifiedNumOfDomains (virConnectPtr conn)
{
595
    GET_PRIVATE(conn);
596
    int ret;
597

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

616
    return -1;
617 618 619
}

static virDomainPtr
620 621
xenUnifiedDomainCreateXML (virConnectPtr conn,
                           const char *xmlDesc, unsigned int flags)
622
{
623
    GET_PRIVATE(conn);
624

625 626
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        return xenDaemonCreateXML(conn, xmlDesc, flags);
627
    return NULL;
628 629
}

630 631 632 633
/* 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.
 */
634 635 636
static virDomainPtr
xenUnifiedDomainLookupByID (virConnectPtr conn, int id)
{
637
    GET_PRIVATE(conn);
638
    virDomainPtr ret;
639

640 641 642 643 644
    /* Reset any connection-level errors in virterror first, in case
     * there is one hanging around from a previous call.
     */
    virConnResetLastError (conn);

645 646 647 648 649 650 651
    /* 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;
    }

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

659
    /* Not found. */
660
    virReportError(VIR_ERR_NO_DOMAIN, __FUNCTION__);
661
    return NULL;
662 663 664 665
}

static virDomainPtr
xenUnifiedDomainLookupByUUID (virConnectPtr conn,
666
                              const unsigned char *uuid)
667
{
668
    GET_PRIVATE(conn);
669
    virDomainPtr ret;
670

671 672 673 674 675
    /* Reset any connection-level errors in virterror first, in case
     * there is one hanging around from a previous call.
     */
    virConnResetLastError (conn);

676 677 678 679 680 681 682
    /* 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;
    }

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

690 691 692 693 694 695 696 697
    /* 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. */
698
    virReportError(VIR_ERR_NO_DOMAIN, __FUNCTION__);
699
    return NULL;
700 701 702 703
}

static virDomainPtr
xenUnifiedDomainLookupByName (virConnectPtr conn,
704
                              const char *name)
705
{
706
    GET_PRIVATE(conn);
707
    virDomainPtr ret;
708

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

735
    /* Not found. */
736
    virReportError(VIR_ERR_NO_DOMAIN, __FUNCTION__);
737
    return NULL;
738 739
}

740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758

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
759
xenUnifiedDomainIsPersistent(virDomainPtr dom)
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 786 787 788 789
{
    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) {
790
                        virReportOOMError();
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
                        goto done;
                    }
                    if (access(path, R_OK) == 0)
                        ret = 1;
                    else if (errno == ENOENT)
                        ret = 0;
                }
            }
        }
    }

done:
    if (currdom)
        virDomainFree(currdom);

    return ret;
}

809 810 811 812 813 814
static int
xenUnifiedDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

815 816 817
static int
xenUnifiedDomainSuspend (virDomainPtr dom)
{
818
    GET_PRIVATE(dom->conn);
819
    int i;
820

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

831
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] &&
E
Eric Blake 已提交
832
        xenHypervisorPauseDomain(dom) == 0)
833
        return 0;
834

835
    return -1;
836 837 838 839 840
}

static int
xenUnifiedDomainResume (virDomainPtr dom)
{
841
    GET_PRIVATE(dom->conn);
842
    int i;
843

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

854
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] &&
E
Eric Blake 已提交
855
        xenHypervisorResumeDomain(dom) == 0)
856
        return 0;
857

858
    return -1;
859 860 861
}

static int
862 863
xenUnifiedDomainShutdownFlags(virDomainPtr dom,
                              unsigned int flags)
864
{
865
    GET_PRIVATE(dom->conn);
866
    int i;
867

868 869
    virCheckFlags(0, -1);

870 871
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
E
Eric Blake 已提交
872
            drivers[i]->xenDomainShutdown &&
873
            drivers[i]->xenDomainShutdown(dom) == 0)
874
            return 0;
875

876
    return -1;
877 878
}

879 880 881 882 883 884
static int
xenUnifiedDomainShutdown(virDomainPtr dom)
{
    return xenUnifiedDomainShutdownFlags(dom, 0);
}

885 886 887
static int
xenUnifiedDomainReboot (virDomainPtr dom, unsigned int flags)
{
888
    GET_PRIVATE(dom->conn);
889
    int i;
890

891 892
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
E
Eric Blake 已提交
893 894
            drivers[i]->xenDomainReboot &&
            drivers[i]->xenDomainReboot (dom, flags) == 0)
895
            return 0;
896

897
    return -1;
898 899
}

900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
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 已提交
915 916
            drivers[i]->xenDomainDestroyFlags &&
            drivers[i]->xenDomainDestroyFlags(dom, flags) == 0)
917 918 919
            return 0;

    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] &&
E
Eric Blake 已提交
920
        xenHypervisorDestroyDomainFlags(dom, flags) == 0)
921 922 923 924 925
        return 0;

    return -1;
}

926 927 928 929 930 931
static int
xenUnifiedDomainDestroy(virDomainPtr dom)
{
    return xenUnifiedDomainDestroyFlags(dom, 0);
}

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

939
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
940 941
        if (priv->opened[i] && drivers[i]->xenDomainGetOSType) {
            ret = drivers[i]->xenDomainGetOSType (dom);
942 943
            if (ret) return ret;
        }
944

945
    return NULL;
946 947
}

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

955
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
956 957
        if (priv->opened[i] && drivers[i]->xenDomainGetMaxMemory) {
            ret = drivers[i]->xenDomainGetMaxMemory (dom);
958 959
            if (ret != 0) return ret;
        }
960

961
    return 0;
962 963 964 965 966
}

static int
xenUnifiedDomainSetMaxMemory (virDomainPtr dom, unsigned long memory)
{
967
    GET_PRIVATE(dom->conn);
968
    int i;
969

970 971 972 973 974 975
    /* Prefer xend for setting max memory */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
        if (xenDaemonDomainSetMaxMemory (dom, memory) == 0)
            return 0;
    }

976
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
977 978
        if (i != XEN_UNIFIED_XEND_OFFSET &&
            priv->opened[i] &&
E
Eric Blake 已提交
979 980
            drivers[i]->xenDomainSetMaxMemory &&
            drivers[i]->xenDomainSetMaxMemory (dom, memory) == 0)
981
            return 0;
982

983
    return -1;
984 985 986 987 988
}

static int
xenUnifiedDomainSetMemory (virDomainPtr dom, unsigned long memory)
{
989
    GET_PRIVATE(dom->conn);
990
    int i;
991

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

998
    return -1;
999 1000 1001 1002 1003
}

static int
xenUnifiedDomainGetInfo (virDomainPtr dom, virDomainInfoPtr info)
{
1004
    GET_PRIVATE(dom->conn);
1005
    int i;
1006

1007 1008
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
E
Eric Blake 已提交
1009 1010
            drivers[i]->xenDomainGetInfo &&
            drivers[i]->xenDomainGetInfo (dom, info) == 0)
1011
            return 0;
1012

1013
    return -1;
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 1053 1054 1055 1056
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;
}

1057
static int
1058 1059
xenUnifiedDomainSaveFlags(virDomainPtr dom, const char *to, const char *dxml,
                          unsigned int flags)
1060
{
1061
    GET_PRIVATE(dom->conn);
1062

1063 1064
    virCheckFlags(0, -1);
    if (dxml) {
1065 1066
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
1067 1068 1069
        return -1;
    }

1070 1071
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        return xenDaemonDomainSave(dom, to);
1072
    return -1;
1073 1074 1075
}

static int
1076 1077 1078 1079 1080 1081 1082 1083
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)
1084
{
1085
    GET_PRIVATE(conn);
1086

1087 1088
    virCheckFlags(0, -1);
    if (dxml) {
1089 1090
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
1091 1092 1093
        return -1;
    }

1094 1095
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        return xenDaemonDomainRestore(conn, from);
1096
    return -1;
1097 1098
}

1099 1100 1101 1102 1103 1104
static int
xenUnifiedDomainRestore (virConnectPtr conn, const char *from)
{
    return xenUnifiedDomainRestoreFlags(conn, from, NULL, 0);
}

1105
static int
1106
xenUnifiedDomainCoreDump (virDomainPtr dom, const char *to, unsigned int flags)
1107
{
1108
    GET_PRIVATE(dom->conn);
1109

1110 1111
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        return xenDaemonDomainCoreDump(dom, to, flags);
1112
    return -1;
1113 1114 1115
}

static int
1116 1117
xenUnifiedDomainSetVcpusFlags (virDomainPtr dom, unsigned int nvcpus,
                               unsigned int flags)
1118
{
1119
    GET_PRIVATE(dom->conn);
1120
    int ret;
1121

1122 1123 1124 1125 1126 1127 1128 1129 1130
    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)) {
1131 1132
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid flag combination: (0x%x)"), flags);
1133 1134 1135
        return -1;
    }
    if (!nvcpus || (unsigned short) nvcpus != nvcpus) {
1136 1137
        virReportError(VIR_ERR_INVALID_ARG,
                       _("argument out of range: %d"), nvcpus);
1138 1139 1140
        return -1;
    }

1141 1142 1143
    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
    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);
1156

1157
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1158
    return -1;
1159 1160
}

1161 1162 1163
static int
xenUnifiedDomainSetVcpus (virDomainPtr dom, unsigned int nvcpus)
{
1164 1165 1166 1167 1168 1169 1170 1171
    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;
1172
        if (priv->xendConfigVersion >= XEND_CONFIG_VERSION_3_0_4)
1173 1174 1175
            flags |= VIR_DOMAIN_VCPU_CONFIG;
    }
    return xenUnifiedDomainSetVcpusFlags(dom, nvcpus, flags);
1176 1177
}

1178 1179
static int
xenUnifiedDomainPinVcpu (virDomainPtr dom, unsigned int vcpu,
1180
                         unsigned char *cpumap, int maplen)
1181
{
1182
    GET_PRIVATE(dom->conn);
1183
    int i;
1184

1185 1186
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
E
Eric Blake 已提交
1187 1188
            drivers[i]->xenDomainPinVcpu &&
            drivers[i]->xenDomainPinVcpu (dom, vcpu, cpumap, maplen) == 0)
1189
            return 0;
1190

1191
    return -1;
1192 1193 1194 1195
}

static int
xenUnifiedDomainGetVcpus (virDomainPtr dom,
1196 1197
                          virVcpuInfoPtr info, int maxinfo,
                          unsigned char *cpumaps, int maplen)
1198
{
1199
    GET_PRIVATE(dom->conn);
1200
    int i, ret;
1201

1202
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1203 1204
        if (priv->opened[i] && drivers[i]->xenDomainGetVcpus) {
            ret = drivers[i]->xenDomainGetVcpus (dom, info, maxinfo, cpumaps, maplen);
1205 1206 1207
            if (ret > 0)
                return ret;
        }
1208
    return -1;
1209 1210 1211
}

static int
1212
xenUnifiedDomainGetVcpusFlags (virDomainPtr dom, unsigned int flags)
1213
{
1214
    GET_PRIVATE(dom->conn);
1215
    int ret;
1216

1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
    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);
1233

1234
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1235
    return -1;
1236 1237
}

1238 1239 1240 1241 1242 1243 1244
static int
xenUnifiedDomainGetMaxVcpus (virDomainPtr dom)
{
    return xenUnifiedDomainGetVcpusFlags(dom, (VIR_DOMAIN_VCPU_LIVE |
                                               VIR_DOMAIN_VCPU_MAXIMUM));
}

1245
static char *
1246
xenUnifiedDomainGetXMLDesc(virDomainPtr dom, unsigned int flags)
1247
{
1248
    GET_PRIVATE(dom->conn);
1249

1250
    if (dom->id == -1 && priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4) {
1251
        if (priv->opened[XEN_UNIFIED_XM_OFFSET])
1252
            return xenXMDomainGetXMLDesc(dom, flags);
1253 1254 1255
    } else {
        if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
            char *cpus, *res;
D
Daniel P. Berrange 已提交
1256
            xenUnifiedLock(priv);
1257
            cpus = xenDomainUsedCpus(dom);
D
Daniel P. Berrange 已提交
1258
            xenUnifiedUnlock(priv);
1259
            res = xenDaemonDomainGetXMLDesc(dom, flags, cpus);
1260
            VIR_FREE(cpus);
1261
            return res;
1262
        }
1263
    }
1264

1265
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1266
    return NULL;
1267 1268
}

1269 1270 1271 1272 1273

static char *
xenUnifiedDomainXMLFromNative(virConnectPtr conn,
                              const char *format,
                              const char *config,
E
Eric Blake 已提交
1274
                              unsigned int flags)
1275 1276 1277 1278
{
    virDomainDefPtr def = NULL;
    char *ret = NULL;
    virConfPtr conf = NULL;
1279 1280 1281
    int id;
    char * tty;
    int vncport;
1282 1283
    GET_PRIVATE(conn);

E
Eric Blake 已提交
1284 1285
    virCheckFlags(0, NULL);

1286 1287
    if (STRNEQ(format, XEN_CONFIG_FORMAT_XM) &&
        STRNEQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
1288 1289
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unsupported config type %s"), format);
1290 1291 1292 1293
        return NULL;
    }

    if (STREQ(format, XEN_CONFIG_FORMAT_XM)) {
1294
        conf = virConfReadMem(config, strlen(config), 0);
1295 1296 1297
        if (!conf)
            goto cleanup;

M
Markus Groß 已提交
1298
        def = xenParseXM(conf, priv->xendConfigVersion, priv->caps);
1299
    } else if (STREQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
1300 1301 1302 1303 1304
        id = xenGetDomIdFromSxprString(config, priv->xendConfigVersion);
        xenUnifiedLock(priv);
        tty = xenStoreDomainGetConsolePath(conn, id);
        vncport = xenStoreDomainGetVNCPort(conn, id);
        xenUnifiedUnlock(priv);
M
Markus Groß 已提交
1305
        def = xenParseSxprString(config, priv->xendConfigVersion, tty,
1306
                                       vncport);
1307 1308 1309 1310
    }
    if (!def)
        goto cleanup;

1311
    ret = virDomainDefFormat(def, 0);
1312 1313 1314

cleanup:
    virDomainDefFree(def);
1315 1316
    if (conf)
        virConfFree(conf);
1317 1318 1319 1320 1321 1322 1323 1324 1325
    return ret;
}


#define MAX_CONFIG_SIZE (1024 * 65)
static char *
xenUnifiedDomainXMLToNative(virConnectPtr conn,
                            const char *format,
                            const char *xmlData,
E
Eric Blake 已提交
1326
                            unsigned int flags)
1327 1328 1329 1330 1331 1332
{
    virDomainDefPtr def = NULL;
    char *ret = NULL;
    virConfPtr conf = NULL;
    GET_PRIVATE(conn);

E
Eric Blake 已提交
1333 1334
    virCheckFlags(0, NULL);

1335 1336
    if (STRNEQ(format, XEN_CONFIG_FORMAT_XM) &&
        STRNEQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
1337 1338
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unsupported config type %s"), format);
1339 1340 1341
        goto cleanup;
    }

M
Matthias Bolte 已提交
1342 1343
    if (!(def = virDomainDefParseString(priv->caps, xmlData,
                                        1 << VIR_DOMAIN_VIRT_XEN, 0)))
1344 1345 1346 1347
        goto cleanup;

    if (STREQ(format, XEN_CONFIG_FORMAT_XM)) {
        int len = MAX_CONFIG_SIZE;
M
Markus Groß 已提交
1348
        conf = xenFormatXM(conn, def, priv->xendConfigVersion);
1349 1350 1351 1352
        if (!conf)
            goto cleanup;

        if (VIR_ALLOC_N(ret, len) < 0) {
1353
            virReportOOMError();
1354 1355 1356 1357 1358 1359 1360 1361
            goto cleanup;
        }

        if (virConfWriteMem(ret, &len, conf) < 0) {
            VIR_FREE(ret);
            goto cleanup;
        }
    } else if (STREQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
M
Markus Groß 已提交
1362
        ret = xenFormatSxpr(conn, def, priv->xendConfigVersion);
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
    }

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


1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
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 已提交
1385 1386
    virCheckFlags(XEN_MIGRATION_FLAGS, -1);

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

1392
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
    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 已提交
1407 1408
    virCheckFlags(XEN_MIGRATION_FLAGS, -1);

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

1413
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1414 1415 1416 1417 1418 1419 1420 1421 1422
    return -1;
}

static virDomainPtr
xenUnifiedDomainMigrateFinish (virConnectPtr dconn,
                               const char *dname,
                               const char *cookie ATTRIBUTE_UNUSED,
                               int cookielen ATTRIBUTE_UNUSED,
                               const char *uri ATTRIBUTE_UNUSED,
1423
                               unsigned long flags)
1424
{
1425 1426 1427 1428
    virDomainPtr dom = NULL;
    char *domain_xml = NULL;
    virDomainPtr dom_new = NULL;

E
Eric Blake 已提交
1429 1430
    virCheckFlags(XEN_MIGRATION_FLAGS, NULL);

1431 1432 1433 1434 1435 1436
    dom = xenUnifiedDomainLookupByName (dconn, dname);
    if (! dom) {
        return NULL;
    }

    if (flags & VIR_MIGRATE_PERSIST_DEST) {
1437
        domain_xml = xenDaemonDomainGetXMLDesc(dom, 0, NULL);
1438
        if (! domain_xml) {
1439 1440
            virReportError(VIR_ERR_MIGRATE_PERSIST_FAILED,
                           "%s", _("failed to get XML representation of migrated domain"));
1441 1442 1443 1444 1445
            goto failure;
        }

        dom_new = xenDaemonDomainDefineXML (dconn, domain_xml);
        if (! dom_new) {
1446 1447
            virReportError(VIR_ERR_MIGRATE_PERSIST_FAILED,
                           "%s", _("failed to define domain on destination host"));
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
            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;
1466 1467
}

1468 1469
static int
xenUnifiedListDefinedDomains (virConnectPtr conn, char **const names,
1470
                              int maxnames)
1471
{
1472
    GET_PRIVATE(conn);
1473 1474
    int i;
    int ret;
1475

1476
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1477 1478
        if (priv->opened[i] && drivers[i]->xenListDefinedDomains) {
            ret = drivers[i]->xenListDefinedDomains (conn, names, maxnames);
1479 1480
            if (ret >= 0) return ret;
        }
1481

1482
    return -1;
1483 1484 1485 1486 1487
}

static int
xenUnifiedNumOfDefinedDomains (virConnectPtr conn)
{
1488
    GET_PRIVATE(conn);
1489 1490
    int i;
    int ret;
1491

1492
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1493 1494
        if (priv->opened[i] && drivers[i]->xenNumOfDefinedDomains) {
            ret = drivers[i]->xenNumOfDefinedDomains (conn);
1495 1496
            if (ret >= 0) return ret;
        }
1497

1498
    return -1;
1499 1500 1501
}

static int
1502
xenUnifiedDomainCreateWithFlags (virDomainPtr dom, unsigned int flags)
1503
{
1504
    GET_PRIVATE(dom->conn);
1505
    int i;
1506

1507 1508
    virCheckFlags(0, -1);

1509
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1510 1511
        if (priv->opened[i] && drivers[i]->xenDomainCreate &&
            drivers[i]->xenDomainCreate (dom) == 0)
1512
            return 0;
1513

1514
    return -1;
1515 1516
}

1517 1518 1519 1520 1521 1522
static int
xenUnifiedDomainCreate (virDomainPtr dom)
{
    return xenUnifiedDomainCreateWithFlags(dom, 0);
}

1523 1524 1525
static virDomainPtr
xenUnifiedDomainDefineXML (virConnectPtr conn, const char *xml)
{
1526
    GET_PRIVATE(conn);
1527 1528
    int i;
    virDomainPtr ret;
1529

1530
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1531 1532
        if (priv->opened[i] && drivers[i]->xenDomainDefineXML) {
            ret = drivers[i]->xenDomainDefineXML (conn, xml);
1533 1534
            if (ret) return ret;
        }
1535

1536
    return NULL;
1537 1538 1539
}

static int
1540
xenUnifiedDomainUndefineFlags (virDomainPtr dom, unsigned int flags)
1541
{
1542
    GET_PRIVATE(dom->conn);
1543
    int i;
1544

1545
    virCheckFlags(0, -1);
1546
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1547 1548
        if (priv->opened[i] && drivers[i]->xenDomainUndefine &&
            drivers[i]->xenDomainUndefine (dom) == 0)
1549
            return 0;
1550

1551
    return -1;
1552 1553
}

1554 1555 1556 1557 1558
static int
xenUnifiedDomainUndefine (virDomainPtr dom) {
    return xenUnifiedDomainUndefineFlags(dom, 0);
}

1559
static int
1560
xenUnifiedDomainAttachDevice (virDomainPtr dom, const char *xml)
1561 1562 1563
{
    GET_PRIVATE(dom->conn);
    int i;
1564
    unsigned int flags = VIR_DOMAIN_DEVICE_MODIFY_LIVE;
1565

1566 1567 1568 1569 1570 1571
    /*
     * 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] &&
1572
        priv->xendConfigVersion >= XEND_CONFIG_VERSION_3_0_4)
1573
        flags |= VIR_DOMAIN_DEVICE_MODIFY_CONFIG;
1574 1575

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

    return -1;
}

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

1590
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1591 1592
        if (priv->opened[i] && drivers[i]->xenDomainAttachDeviceFlags &&
            drivers[i]->xenDomainAttachDeviceFlags(dom, xml, flags) == 0)
1593
            return 0;
1594

1595
    return -1;
1596 1597 1598
}

static int
1599
xenUnifiedDomainDetachDevice (virDomainPtr dom, const char *xml)
1600 1601 1602
{
    GET_PRIVATE(dom->conn);
    int i;
1603
    unsigned int flags = VIR_DOMAIN_DEVICE_MODIFY_LIVE;
1604

1605 1606 1607 1608 1609 1610
    /*
     * 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] &&
1611
        priv->xendConfigVersion >= XEND_CONFIG_VERSION_3_0_4)
1612
        flags |= VIR_DOMAIN_DEVICE_MODIFY_CONFIG;
1613 1614

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

    return -1;
}

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

1629
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1630 1631
        if (priv->opened[i] && drivers[i]->xenDomainDetachDeviceFlags &&
            drivers[i]->xenDomainDetachDeviceFlags(dom, xml, flags) == 0)
1632
            return 0;
1633

1634
    return -1;
1635 1636
}

1637 1638 1639 1640 1641 1642
static int
xenUnifiedDomainUpdateDeviceFlags (virDomainPtr dom, const char *xml,
                                   unsigned int flags)
{
    GET_PRIVATE(dom->conn);

1643 1644
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        return xenDaemonUpdateDeviceFlags(dom, xml, flags);
1645 1646 1647
    return -1;
}

1648 1649 1650 1651 1652
static int
xenUnifiedDomainGetAutostart (virDomainPtr dom, int *autostart)
{
    GET_PRIVATE(dom->conn);

1653
    if (priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4) {
1654 1655 1656 1657 1658 1659
        if (priv->opened[XEN_UNIFIED_XM_OFFSET])
            return xenXMDomainGetAutostart(dom, autostart);
    } else {
        if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
            return xenDaemonDomainGetAutostart(dom, autostart);
    }
1660

1661
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1662 1663 1664 1665 1666 1667 1668 1669
    return -1;
}

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

1670
    if (priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4) {
1671 1672 1673 1674 1675 1676
        if (priv->opened[XEN_UNIFIED_XM_OFFSET])
            return xenXMDomainSetAutostart(dom, autostart);
    } else {
        if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
            return xenDaemonDomainSetAutostart(dom, autostart);
    }
1677

1678
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1679 1680 1681
    return -1;
}

1682 1683 1684 1685 1686 1687 1688 1689
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 已提交
1690 1691
        if (priv->opened[i] && drivers[i]->xenDomainGetSchedulerType) {
            schedulertype = drivers[i]->xenDomainGetSchedulerType (dom, nparams);
1692
            if (schedulertype != NULL)
1693
                return schedulertype;
1694 1695
        }
    }
1696
    return NULL;
1697 1698 1699
}

static int
1700 1701 1702 1703
xenUnifiedDomainGetSchedulerParametersFlags(virDomainPtr dom,
                                            virTypedParameterPtr params,
                                            int *nparams,
                                            unsigned int flags)
1704 1705 1706 1707
{
    GET_PRIVATE(dom->conn);
    int i, ret;

1708 1709
    virCheckFlags(0, -1);

1710
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i) {
E
Eric Blake 已提交
1711 1712
        if (priv->opened[i] && drivers[i]->xenDomainGetSchedulerParameters) {
           ret = drivers[i]->xenDomainGetSchedulerParameters(dom, params, nparams);
1713
           if (ret == 0)
1714
               return 0;
1715
        }
1716
    }
1717
    return -1;
1718 1719 1720
}

static int
1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733
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)
1734 1735 1736 1737
{
    GET_PRIVATE(dom->conn);
    int i, ret;

1738 1739
    virCheckFlags(0, -1);

1740 1741
    /* do the hypervisor call last to get better error */
    for (i = XEN_UNIFIED_NR_DRIVERS - 1; i >= 0; i--) {
E
Eric Blake 已提交
1742 1743
        if (priv->opened[i] && drivers[i]->xenDomainSetSchedulerParameters) {
           ret = drivers[i]->xenDomainSetSchedulerParameters(dom, params, nparams);
1744 1745 1746
           if (ret == 0)
               return 0;
        }
1747 1748
    }

1749
    return -1;
1750 1751
}

1752 1753 1754 1755 1756 1757 1758 1759 1760
static int
xenUnifiedDomainSetSchedulerParameters(virDomainPtr dom,
                                       virTypedParameterPtr params,
                                       int nparams)
{
    return xenUnifiedDomainSetSchedulerParametersFlags(dom, params,
                                                       nparams, 0);
}

1761 1762 1763 1764 1765 1766 1767 1768 1769
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);

1770
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
    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);

1783
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1784 1785 1786
    return -1;
}

R
Richard W.M. Jones 已提交
1787 1788 1789
static int
xenUnifiedDomainBlockPeek (virDomainPtr dom, const char *path,
                           unsigned long long offset, size_t size,
E
Eric Blake 已提交
1790
                           void *buffer, unsigned int flags)
R
Richard W.M. Jones 已提交
1791 1792 1793 1794
{
    int r;
    GET_PRIVATE (dom->conn);

E
Eric Blake 已提交
1795 1796
    virCheckFlags(0, -1);

R
Richard W.M. Jones 已提交
1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807
    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;
    }

1808
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
R
Richard W.M. Jones 已提交
1809 1810 1811
    return -1;
}

1812 1813 1814 1815 1816 1817 1818
static int
xenUnifiedNodeGetCellsFreeMemory (virConnectPtr conn, unsigned long long *freeMems,
                                  int startCell, int maxCells)
{
    GET_PRIVATE (conn);

    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET])
1819
        return xenHypervisorNodeGetCellsFreeMemory (conn, freeMems,
1820 1821
                                                    startCell, maxCells);

1822
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1823 1824 1825
    return -1;
}

1826 1827 1828 1829 1830 1831 1832 1833
static unsigned long long
xenUnifiedNodeGetFreeMemory (virConnectPtr conn)
{
    unsigned long long freeMem = 0;
    int ret;
    GET_PRIVATE (conn);

    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET]) {
1834
        ret = xenHypervisorNodeGetCellsFreeMemory (conn, &freeMem,
1835
                                                    -1, 1);
1836
        if (ret != 1)
1837 1838
            return 0;
        return freeMem;
1839 1840
    }

1841
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1842
    return 0;
1843 1844
}

1845

1846
static int
1847 1848 1849 1850
xenUnifiedDomainEventRegister(virConnectPtr conn,
                              virConnectDomainEventCallback callback,
                              void *opaque,
                              virFreeCallback freefunc)
1851
{
D
Daniel P. Berrange 已提交
1852 1853
    GET_PRIVATE (conn);

1854
    int ret;
D
Daniel P. Berrange 已提交
1855
    xenUnifiedLock(priv);
1856

1857
    if (priv->xsWatch == -1) {
1858
        virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
D
Daniel P. Berrange 已提交
1859
        xenUnifiedUnlock(priv);
1860 1861 1862
        return -1;
    }

1863 1864
    ret = virDomainEventStateRegister(conn, priv->domainEvents,
                                      callback, opaque, freefunc);
1865

D
Daniel P. Berrange 已提交
1866
    xenUnifiedUnlock(priv);
1867
    return ret;
1868 1869
}

1870

1871
static int
1872 1873
xenUnifiedDomainEventDeregister(virConnectPtr conn,
                                virConnectDomainEventCallback callback)
1874 1875 1876
{
    int ret;
    GET_PRIVATE (conn);
D
Daniel P. Berrange 已提交
1877 1878
    xenUnifiedLock(priv);

1879
    if (priv->xsWatch == -1) {
1880
        virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
D
Daniel P. Berrange 已提交
1881
        xenUnifiedUnlock(priv);
1882 1883 1884
        return -1;
    }

1885 1886 1887
    ret = virDomainEventStateDeregister(conn,
                                        priv->domainEvents,
                                        callback);
1888

D
Daniel P. Berrange 已提交
1889
    xenUnifiedUnlock(priv);
1890 1891 1892
    return ret;
}

1893

1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907
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) {
1908
        virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1909 1910 1911 1912
        xenUnifiedUnlock(priv);
        return -1;
    }

1913 1914 1915
    if (virDomainEventStateRegisterID(conn, priv->domainEvents,
                                      dom, eventID,
                                      callback, opaque, freefunc, &ret) < 0)
1916
        ret = -1;
1917 1918

    xenUnifiedUnlock(priv);
1919
    return ret;
1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930
}

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

    if (priv->xsWatch == -1) {
1931
        virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1932 1933 1934 1935
        xenUnifiedUnlock(priv);
        return -1;
    }

1936 1937 1938
    ret = virDomainEventStateDeregisterID(conn,
                                          priv->domainEvents,
                                          callbackID);
1939 1940 1941 1942 1943 1944

    xenUnifiedUnlock(priv);
    return ret;
}


1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
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;

1961
    def = virNodeDeviceDefParseString(xml, EXISTING_DEVICE, NULL);
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978
    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) {
1979 1980
        virReportError(VIR_ERR_INVALID_ARG,
                       _("device %s is not a PCI device"), dev->name);
1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
        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;

2001
    pci = pciGetDevice(domain, bus, slot, function);
2002 2003 2004
    if (!pci)
        return -1;

2005
    if (pciDettachDevice(pci, NULL, NULL) < 0)
2006 2007 2008 2009
        goto out;

    ret = 0;
out:
2010
    pciFreeDevice(pci);
2011 2012 2013
    return ret;
}

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 2064 2065 2066 2067 2068 2069 2070 2071
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;
}

2072 2073 2074 2075 2076 2077
static int
xenUnifiedNodeDeviceReAttach (virNodeDevicePtr dev)
{
    pciDevice *pci;
    unsigned domain, bus, slot, function;
    int ret = -1;
2078
    int domid;
2079 2080 2081 2082

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

2083
    pci = pciGetDevice(domain, bus, slot, function);
2084 2085 2086
    if (!pci)
        return -1;

2087 2088
    /* Check if device is assigned to an active guest */
    if ((domid = xenUnifiedNodeDeviceAssignedDomainId(dev)) >= 0) {
2089 2090 2091
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Device %s has been assigned to guest %d"),
                       dev->name, domid);
2092 2093 2094
        goto out;
    }

2095
    if (pciReAttachDevice(pci, NULL, NULL) < 0)
2096 2097 2098 2099
        goto out;

    ret = 0;
out:
2100
    pciFreeDevice(pci);
2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113
    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;

2114
    pci = pciGetDevice(domain, bus, slot, function);
2115 2116 2117
    if (!pci)
        return -1;

2118
    if (pciResetDevice(pci, NULL, NULL) < 0)
2119 2120 2121 2122
        goto out;

    ret = 0;
out:
2123
    pciFreeDevice(pci);
2124 2125 2126 2127
    return ret;
}


2128 2129
static int
xenUnifiedDomainOpenConsole(virDomainPtr dom,
2130
                            const char *dev_name,
2131 2132 2133 2134 2135 2136 2137 2138 2139 2140
                            virStreamPtr st,
                            unsigned int flags)
{
    virDomainDefPtr def = NULL;
    int ret = -1;
    virDomainChrDefPtr chr = NULL;

    virCheckFlags(0, -1);

    if (dom->id == -1) {
2141 2142
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("domain is not running"));
2143 2144 2145
        goto cleanup;
    }

2146
    if (dev_name) {
2147
        /* XXX support device aliases in future */
2148 2149
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Named device aliases are not supported"));
2150 2151 2152 2153 2154 2155 2156
        goto cleanup;
    }

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

2157 2158
    if (def->nconsoles)
        chr = def->consoles[0];
2159 2160 2161 2162
    else if (def->nserials)
        chr = def->serials[0];

    if (!chr) {
2163 2164
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot find default console device"));
2165 2166 2167
        goto cleanup;
    }

2168
    if (chr->source.type != VIR_DOMAIN_CHR_TYPE_PTY) {
2169 2170
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("character device %s is not using a PTY"), dev_name);
2171 2172 2173
        goto cleanup;
    }

2174
    if (virFDStreamOpenFile(st, chr->source.data.file.path,
E
Eric Blake 已提交
2175
                            0, 0, O_RDWR) < 0)
2176 2177 2178 2179 2180 2181 2182
        goto cleanup;

    ret = 0;
cleanup:
    virDomainDefFree(def);
    return ret;
}
E
Eric Blake 已提交
2183
/*----- Register with libvirt.c, and initialize Xen drivers. -----*/
2184 2185 2186

/* The interface which we export upwards to libvirt.c. */
static virDriver xenUnifiedDriver = {
2187 2188
    .no = VIR_DRV_XEN_UNIFIED,
    .name = "Xen",
2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206
    .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 */
2207
    .domainShutdownFlags = xenUnifiedDomainShutdownFlags, /* 0.9.10 */
2208 2209
    .domainReboot = xenUnifiedDomainReboot, /* 0.1.0 */
    .domainDestroy = xenUnifiedDomainDestroy, /* 0.0.3 */
2210
    .domainDestroyFlags = xenUnifiedDomainDestroyFlags, /* 0.9.4 */
2211 2212 2213 2214 2215 2216 2217
    .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 */
2218
    .domainSaveFlags = xenUnifiedDomainSaveFlags, /* 0.9.4 */
2219
    .domainRestore = xenUnifiedDomainRestore, /* 0.0.3 */
2220
    .domainRestoreFlags = xenUnifiedDomainRestoreFlags, /* 0.9.4 */
2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236
    .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 */
2237
    .domainUndefineFlags = xenUnifiedDomainUndefineFlags, /* 0.9.4 */
2238 2239 2240 2241 2242 2243 2244 2245 2246
    .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 */
2247
    .domainGetSchedulerParametersFlags = xenUnifiedDomainGetSchedulerParametersFlags, /* 0.9.2 */
2248
    .domainSetSchedulerParameters = xenUnifiedDomainSetSchedulerParameters, /* 0.2.3 */
2249
    .domainSetSchedulerParametersFlags = xenUnifiedDomainSetSchedulerParametersFlags, /* 0.9.2 */
2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270
    .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 */
2271
    .isAlive = xenUnifiedIsAlive, /* 0.9.8 */
2272
    .nodeSuspendForDuration = nodeSuspendForDuration, /* 0.9.8 */
2273 2274
    .nodeGetMemoryParameters = nodeGetMemoryParameters, /* 0.10.2 */
    .nodeSetMemoryParameters = nodeSetMemoryParameters, /* 0.10.2 */
2275 2276
};

2277
/**
2278
 * xenRegister:
2279 2280 2281 2282 2283
 *
 * Register xen related drivers
 *
 * Returns the driver priority or -1 in case of error.
 */
2284
int
2285
xenRegister (void)
2286
{
J
John Levon 已提交
2287 2288 2289 2290
#ifdef WITH_LIBVIRTD
    if (virRegisterStateDriver (&state_driver) == -1) return -1;
#endif

2291
    return virRegisterDriver (&xenUnifiedDriver);
2292 2293
}

2294 2295 2296 2297 2298 2299 2300 2301 2302
/**
 * xenUnifiedDomainInfoListFree:
 *
 * Free the Domain Info List
 */
void
xenUnifiedDomainInfoListFree(xenUnifiedDomainInfoListPtr list)
{
    int i;
J
John Levon 已提交
2303 2304 2305 2306

    if (list == NULL)
        return;

2307 2308 2309 2310
    for (i=0; i<list->count; i++) {
        VIR_FREE(list->doms[i]->name);
        VIR_FREE(list->doms[i]);
    }
2311
    VIR_FREE(list->doms);
2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333
    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)) {
2334
            VIR_DEBUG("WARNING: dom already tracked");
2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356
            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:
2357
    virReportOOMError();
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 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402
    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;
}

2403

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

2420
    virDomainEventStateQueue(priv->domainEvents, event);
D
Daniel P. Berrange 已提交
2421 2422 2423 2424 2425 2426 2427 2428 2429 2430
}

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

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