xen_unified.c 31.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * xen_unified.c: Unified Xen driver.
 *
 * Copyright (C) 2007 Red Hat, Inc.
 *
 * See COPYING.LIB for the License of this software
 *
 * Richard W.M. Jones <rjones@redhat.com>
 */

#ifdef WITH_XEN

/* Note:
 *
 * This driver provides a unified interface to the five
 * separate underlying Xen drivers (xen_internal, proxy_internal,
 * xend_internal, xs_internal and xm_internal).  Historically
 * the body of libvirt.c handled the five Xen drivers,
 * and contained Xen-specific code.
 */

#include <stdint.h>
#include <unistd.h>
24
#include <string.h>
25
#include <errno.h>
26 27
#include <sys/types.h>
#include <xen/dom0_ops.h>
28
#include <libxml/uri.h>
29 30 31 32 33 34 35 36 37 38 39 40

#include "internal.h"

#include "xen_unified.h"

#include "xen_internal.h"
#include "proxy_internal.h"
#include "xend_internal.h"
#include "xs_internal.h"
#include "xm_internal.h"

/* The five Xen drivers below us. */
41 42 43 44 45 46
static struct xenUnifiedDriver *drivers[XEN_UNIFIED_NR_DRIVERS] = {
    [XEN_UNIFIED_HYPERVISOR_OFFSET] = &xenHypervisorDriver,
    [XEN_UNIFIED_PROXY_OFFSET] = &xenProxyDriver,
    [XEN_UNIFIED_XEND_OFFSET] = &xenDaemonDriver,
    [XEN_UNIFIED_XS_OFFSET] = &xenStoreDriver,
    [XEN_UNIFIED_XM_OFFSET] = &xenXMDriver,
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
};

/**
 * xenUnifiedError:
 * @conn: the connection
 * @error: the error number
 * @info: extra information string
 *
 * Handle an error at the xend daemon interface
 */
static void
xenUnifiedError (virConnectPtr conn, virErrorNumber error, const char *info)
{
    const char *errmsg;

    errmsg = __virErrorMsg (error, info);
    __virRaiseError (conn, NULL, NULL, VIR_FROM_XEN, error, VIR_ERR_ERROR,
64
                     errmsg, info, NULL, 0, 0, errmsg, info);
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
}

/*----- 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.
 */

static int
xenUnifiedOpen (virConnectPtr conn, const char *name, int flags)
{
81 82
    int i, j;
    xenUnifiedPrivatePtr priv;
83
    xmlURIPtr uri;
84

85 86 87 88 89
    uri = xmlParseURI(name);
    if (uri == NULL) {
        return VIR_DRV_OPEN_DECLINED;
    }

90
    /* Refuse any scheme which isn't "xen://" or "http://". */
91 92
    if (uri->scheme &&
        strcasecmp(uri->scheme, "xen") != 0 &&
93 94 95 96 97 98 99 100 101 102 103
        strcasecmp(uri->scheme, "http") != 0) {
        xmlFreeURI(uri);
        return VIR_DRV_OPEN_DECLINED;
    }

    /* xmlParseURI will parse a naked string like "foo" as a URI with
     * a NULL scheme.  That's not useful for us because we want to only
     * allow full pathnames (eg. ///var/lib/xen/xend-socket).  Decline
     * anything else.
     */
    if (!uri->scheme && name[0] != '/') {
104
        xmlFreeURI(uri);
105
        return VIR_DRV_OPEN_DECLINED;
106 107 108
    }

    /* Refuse any xen:// URI with a server specified - allow remote to do it */
109
    if (uri->scheme && strcasecmp(uri->scheme, "xen") == 0 && uri->server) {
110 111 112
        xmlFreeURI(uri);
        return VIR_DRV_OPEN_DECLINED;
    }
113

114
    xmlFreeURI(uri);
115 116

    /* Allocate per-connection private data. */
117
    priv = calloc (1, sizeof *priv);
118
    if (!priv) {
119
        xenUnifiedError (NULL, VIR_ERR_NO_MEMORY, "allocating private data");
120 121 122 123
        return VIR_DRV_OPEN_ERROR;
    }
    conn->privateData = priv;

124 125 126 127 128 129 130
    priv->name = strdup (name);
    if (!priv->name) {
        xenUnifiedError (NULL, VIR_ERR_NO_MEMORY, "allocating priv->name");
        free (priv);
        return VIR_DRV_OPEN_ERROR;
    }

131 132 133 134 135 136 137 138
    priv->handle = -1;
    priv->xendConfigVersion = -1;
    priv->type = -1;
    priv->len = -1;
    priv->addr = NULL;
    priv->xshandle = NULL;
    priv->proxy = -1;

139 140
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i) {
        priv->opened[i] = 0;
141

142 143 144 145 146
        /* Only use XM driver for Xen <= 3.0.3 (ie xendConfigVersion <= 2) */
        if (drivers[i] == &xenXMDriver &&
            priv->xendConfigVersion > 2)
            continue;

147
        /* Ignore proxy for root */
148
        if (i == XEN_UNIFIED_PROXY_OFFSET && getuid() == 0)
149 150
            continue;

151 152 153 154 155 156 157 158 159 160 161
        if (drivers[i]->open) {
#ifdef ENABLE_DEBUG
            fprintf (stderr, "libvirt: xenUnifiedOpen: trying Xen sub-driver %d\n", i);
#endif
            if (drivers[i]->open (conn, name, flags) == VIR_DRV_OPEN_SUCCESS)
                priv->opened[i] = 1;
#ifdef ENABLE_DEBUG
            fprintf (stderr, "libvirt: xenUnifiedOpen: Xen sub-driver %d open %s\n",
                     i, priv->opened[i] ? "ok" : "failed");
#endif
        }
162 163 164

        /* If as root, then all drivers must succeed.
           If non-root, then only proxy must succeed */
165 166
        if (!priv->opened[i] &&
            (getuid() == 0 || i == XEN_UNIFIED_PROXY_OFFSET)) {
167
            for (j = 0; j < i; ++j)
168
                if (priv->opened[j]) drivers[j]->close (conn);
169 170 171 172 173
            free (priv->name);
            free (priv);
            /* The assumption is that one of the underlying drivers
             * has set virterror already.
             */
174 175
            return VIR_DRV_OPEN_ERROR;
        }
176 177
    }

178
    return VIR_DRV_OPEN_SUCCESS;
179 180
}

181 182 183
#define GET_PRIVATE(conn) \
    xenUnifiedPrivatePtr priv = (xenUnifiedPrivatePtr) (conn)->privateData

184 185 186
static int
xenUnifiedClose (virConnectPtr conn)
{
187
    GET_PRIVATE(conn);
188
    int i;
189

190 191
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->close)
192
            (void) drivers[i]->close (conn);
193

194
    free (priv->name);
195 196
    free (conn->privateData);
    conn->privateData = NULL;
197

198
    return 0;
199 200 201 202 203
}

static const char *
xenUnifiedType (virConnectPtr conn)
{
204
    GET_PRIVATE(conn);
205 206
    int i;
    const char *ret;
207

208 209
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->type) {
210 211 212
            ret = drivers[i]->type (conn);
            if (ret) return ret;
        }
213

214
    return NULL;
215 216
}

217 218 219 220 221 222 223 224 225 226
/* Which features are supported by this driver? */
static int
xenUnifiedSupportsFeature (virConnectPtr conn ATTRIBUTE_UNUSED, int feature)
{
    switch (feature) {
    case VIR_DRV_FEATURE_MIGRATION_V1: return 1;
    default: return 0;
    }
}

227 228 229
static int
xenUnifiedVersion (virConnectPtr conn, unsigned long *hvVer)
{
230
    GET_PRIVATE(conn);
231
    int i;
232

233 234 235
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->version &&
236 237
            drivers[i]->version (conn, hvVer) == 0)
            return 0;
238

239
    return -1;
240 241
}

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
/* NB: Even if connected to the proxy, we're still on the
 * same machine.
 */
static char *
xenUnifiedGetHostname (virConnectPtr conn)
{
    int r;
    char hostname [HOST_NAME_MAX+1], *str;

    r = gethostname (hostname, HOST_NAME_MAX+1);
    if (r == -1) {
        xenUnifiedError (conn, VIR_ERR_SYSTEM_ERROR, strerror (errno));
        return NULL;
    }
    str = strdup (hostname);
    if (str == NULL) {
        xenUnifiedError (conn, VIR_ERR_SYSTEM_ERROR, strerror (errno));
        return NULL;
    }
    return str;
}

/* The name is recorded (canonicalised) in xenUnifiedOpen. */
static char *
xenUnifiedGetURI (virConnectPtr conn)
{
    GET_PRIVATE(conn);
    char *str;

    str = strdup (priv->name);
    if (str == NULL) {
        xenUnifiedError (conn, VIR_ERR_SYSTEM_ERROR, strerror (errno));
        return NULL;
    }
    return str;
}

279 280 281
static int
xenUnifiedGetMaxVcpus (virConnectPtr conn, const char *type)
{
282
    GET_PRIVATE(conn);
283

284 285 286 287
    if (type && STRCASENEQ (type, "Xen")) {
        xenUnifiedError (conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return -1;
    }
288

289 290 291 292 293 294
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET])
        return xenHypervisorGetMaxVcpus (conn, type);
    else {
        xenUnifiedError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
        return -1;
    }
295 296 297 298 299
}

static int
xenUnifiedNodeGetInfo (virConnectPtr conn, virNodeInfoPtr info)
{
300
    GET_PRIVATE(conn);
301
    int i;
302

303 304 305
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->nodeGetInfo &&
306 307
            drivers[i]->nodeGetInfo (conn, info) == 0)
            return 0;
308

309
    return -1;
310 311 312 313 314
}

static char *
xenUnifiedGetCapabilities (virConnectPtr conn)
{
315
    GET_PRIVATE(conn);
316 317
    int i;
    char *ret;
318

319 320
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->getCapabilities) {
321 322 323
            ret = drivers[i]->getCapabilities (conn);
            if (ret) return ret;
        }
324

325
    return NULL;
326 327 328 329 330
}

static int
xenUnifiedListDomains (virConnectPtr conn, int *ids, int maxids)
{
331
    GET_PRIVATE(conn);
332
    int i, ret;
333

334 335
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->listDomains) {
336 337 338
            ret = drivers[i]->listDomains (conn, ids, maxids);
            if (ret >= 0) return ret;
        }
339

340
    return -1;
341 342 343 344 345
}

static int
xenUnifiedNumOfDomains (virConnectPtr conn)
{
346
    GET_PRIVATE(conn);
347
    int i, ret;
348

349 350
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->numOfDomains) {
351 352 353
            ret = drivers[i]->numOfDomains (conn);
            if (ret >= 0) return ret;
        }
354

355
    return -1;
356 357 358 359
}

static virDomainPtr
xenUnifiedDomainCreateLinux (virConnectPtr conn,
360
                             const char *xmlDesc, unsigned int flags)
361
{
362
    GET_PRIVATE(conn);
363 364
    int i;
    virDomainPtr ret;
365

366 367
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainCreateLinux) {
368 369 370
            ret = drivers[i]->domainCreateLinux (conn, xmlDesc, flags);
            if (ret) return ret;
        }
371

372
    return NULL;
373 374
}

375 376 377 378
/* 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.
 */
379 380 381
static virDomainPtr
xenUnifiedDomainLookupByID (virConnectPtr conn, int id)
{
382
    GET_PRIVATE(conn);
383
    virDomainPtr ret;
384

385 386 387 388 389
    /* Reset any connection-level errors in virterror first, in case
     * there is one hanging around from a previous call.
     */
    virConnResetLastError (conn);

390 391 392 393 394 395 396
    /* 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;
    }

397 398 399 400 401 402 403 404 405 406 407 408 409
    /* Try proxy. */
    if (priv->opened[XEN_UNIFIED_PROXY_OFFSET]) {
        ret = xenProxyLookupByID (conn, id);
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }

    /* Try xend. */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
        ret = xenDaemonLookupByID (conn, id);
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }
410

411 412
    /* Not found. */
    xenUnifiedError (conn, VIR_ERR_NO_DOMAIN, __FUNCTION__);
413
    return NULL;
414 415 416 417
}

static virDomainPtr
xenUnifiedDomainLookupByUUID (virConnectPtr conn,
418
                              const unsigned char *uuid)
419
{
420
    GET_PRIVATE(conn);
421
    virDomainPtr ret;
422

423 424 425 426 427
    /* Reset any connection-level errors in virterror first, in case
     * there is one hanging around from a previous call.
     */
    virConnResetLastError (conn);

428 429 430 431 432 433 434
    /* 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;
    }

435 436 437 438 439 440 441 442 443 444 445 446 447
    /* Try proxy. */
    if (priv->opened[XEN_UNIFIED_PROXY_OFFSET]) {
        ret = xenProxyLookupByUUID (conn, uuid);
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }

    /* Try xend. */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
        ret = xenDaemonLookupByUUID (conn, uuid);
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }
448

449 450 451 452 453 454 455 456 457
    /* Try XM for inactive domains. */
    if (priv->opened[XEN_UNIFIED_XM_OFFSET]) {
        ret = xenXMDomainLookupByUUID (conn, uuid);
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }

    /* Not found. */
    xenUnifiedError (conn, VIR_ERR_NO_DOMAIN, __FUNCTION__);
458
    return NULL;
459 460 461 462
}

static virDomainPtr
xenUnifiedDomainLookupByName (virConnectPtr conn,
463
                              const char *name)
464
{
465
    GET_PRIVATE(conn);
466
    virDomainPtr ret;
467

468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
    /* Reset any connection-level errors in virterror first, in case
     * there is one hanging around from a previous call.
     */
    virConnResetLastError (conn);

    /* Try proxy. */
    if (priv->opened[XEN_UNIFIED_PROXY_OFFSET]) {
        ret = xenProxyLookupByName (conn, name);
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }

    /* Try xend. */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
        ret = xenDaemonLookupByName (conn, name);
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }

    /* Try xenstore for inactive domains. */
    if (priv->opened[XEN_UNIFIED_XS_OFFSET]) {
        ret = xenStoreLookupByName (conn, name);
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }

    /* Try XM for inactive domains. */
    if (priv->opened[XEN_UNIFIED_XM_OFFSET]) {
        ret = xenXMDomainLookupByName (conn, name);
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }
500

501 502
    /* Not found. */
    xenUnifiedError (conn, VIR_ERR_NO_DOMAIN, __FUNCTION__);
503
    return NULL;
504 505 506 507 508
}

static int
xenUnifiedDomainSuspend (virDomainPtr dom)
{
509
    GET_PRIVATE(dom->conn);
510
    int i;
511

512 513 514
    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
515
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
516
        if (i != XEN_UNIFIED_HYPERVISOR_OFFSET &&
517
            priv->opened[i] &&
518 519 520
            drivers[i]->domainSuspend &&
            drivers[i]->domainSuspend (dom) == 0)
            return 0;
521

522 523 524
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] &&
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainSuspend &&
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainSuspend (dom) == 0)
525
        return 0;
526

527
    return -1;
528 529 530 531 532
}

static int
xenUnifiedDomainResume (virDomainPtr dom)
{
533
    GET_PRIVATE(dom->conn);
534
    int i;
535

536 537 538
    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
539
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
540
        if (i != XEN_UNIFIED_HYPERVISOR_OFFSET &&
541
            priv->opened[i] &&
542 543 544
            drivers[i]->domainResume &&
            drivers[i]->domainResume (dom) == 0)
            return 0;
545

546 547 548
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] &&
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainResume &&
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainResume (dom) == 0)
549
        return 0;
550

551
    return -1;
552 553 554 555 556
}

static int
xenUnifiedDomainShutdown (virDomainPtr dom)
{
557
    GET_PRIVATE(dom->conn);
558
    int i;
559

560 561 562
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainShutdown &&
563 564
            drivers[i]->domainShutdown (dom) == 0)
            return 0;
565

566
    return -1;
567 568 569 570 571
}

static int
xenUnifiedDomainReboot (virDomainPtr dom, unsigned int flags)
{
572
    GET_PRIVATE(dom->conn);
573
    int i;
574

575 576 577
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainReboot &&
578 579
            drivers[i]->domainReboot (dom, flags) == 0)
            return 0;
580

581
    return -1;
582 583 584 585 586
}

static int
xenUnifiedDomainDestroy (virDomainPtr dom)
{
587
    GET_PRIVATE(dom->conn);
588
    int i;
589

590 591 592
    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
593
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
594
        if (i != XEN_UNIFIED_HYPERVISOR_OFFSET &&
595
            priv->opened[i] &&
596 597 598
            drivers[i]->domainDestroy &&
            drivers[i]->domainDestroy (dom) == 0)
            return 0;
599

600
    if (priv->opened[i] &&
601 602
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainDestroy &&
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainDestroy (dom) == 0)
603
        return 0;
604

605
    return -1;
606 607 608 609 610
}

static char *
xenUnifiedDomainGetOSType (virDomainPtr dom)
{
611
    GET_PRIVATE(dom->conn);
612 613
    int i;
    char *ret;
614

615 616
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainGetOSType) {
617 618 619
            ret = drivers[i]->domainGetOSType (dom);
            if (ret) return ret;
        }
620

621
    return NULL;
622 623 624 625 626
}

static unsigned long
xenUnifiedDomainGetMaxMemory (virDomainPtr dom)
{
627
    GET_PRIVATE(dom->conn);
628 629
    int i;
    unsigned long ret;
630

631 632
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainGetMaxMemory) {
633 634 635
            ret = drivers[i]->domainGetMaxMemory (dom);
            if (ret != 0) return ret;
        }
636

637
    return 0;
638 639 640 641 642
}

static int
xenUnifiedDomainSetMaxMemory (virDomainPtr dom, unsigned long memory)
{
643
    GET_PRIVATE(dom->conn);
644
    int i;
645

646 647 648
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainSetMaxMemory &&
649 650
            drivers[i]->domainSetMaxMemory (dom, memory) == 0)
            return 0;
651

652
    return -1;
653 654 655 656 657
}

static int
xenUnifiedDomainSetMemory (virDomainPtr dom, unsigned long memory)
{
658
    GET_PRIVATE(dom->conn);
659
    int i;
660

661 662 663
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainSetMemory &&
664 665
            drivers[i]->domainSetMemory (dom, memory) == 0)
            return 0;
666

667
    return -1;
668 669 670 671 672
}

static int
xenUnifiedDomainGetInfo (virDomainPtr dom, virDomainInfoPtr info)
{
673
    GET_PRIVATE(dom->conn);
674
    int i;
675

676 677 678
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainGetInfo &&
679 680
            drivers[i]->domainGetInfo (dom, info) == 0)
            return 0;
681

682
    return -1;
683 684 685 686 687
}

static int
xenUnifiedDomainSave (virDomainPtr dom, const char *to)
{
688
    GET_PRIVATE(dom->conn);
689
    int i;
690

691 692 693
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainSave &&
694 695
            drivers[i]->domainSave (dom, to) == 0)
            return 0;
696

697
    return -1;
698 699 700 701 702
}

static int
xenUnifiedDomainRestore (virConnectPtr conn, const char *from)
{
703
    GET_PRIVATE(conn);
704
    int i;
705

706 707 708
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainRestore &&
709 710
            drivers[i]->domainRestore (conn, from) == 0)
            return 0;
711

712
    return -1;
713 714 715 716 717
}

static int
xenUnifiedDomainCoreDump (virDomainPtr dom, const char *to, int flags)
{
718
    GET_PRIVATE(dom->conn);
719
    int i;
720

721 722 723
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainCoreDump &&
724 725
            drivers[i]->domainCoreDump (dom, to, flags) == 0)
            return 0;
726

727
    return -1;
728 729 730 731 732
}

static int
xenUnifiedDomainSetVcpus (virDomainPtr dom, unsigned int nvcpus)
{
733
    GET_PRIVATE(dom->conn);
734
    int i;
735

736 737 738
    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
739
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
740
        if (i != XEN_UNIFIED_HYPERVISOR_OFFSET &&
741
            priv->opened[i] &&
742 743 744
            drivers[i]->domainSetVcpus &&
            drivers[i]->domainSetVcpus (dom, nvcpus) == 0)
            return 0;
745

746 747 748
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] &&
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainSetVcpus &&
        drivers[XEN_UNIFIED_HYPERVISOR_OFFSET]->domainSetVcpus (dom, nvcpus) == 0)
749
        return 0;
750

751
    return -1;
752 753 754 755
}

static int
xenUnifiedDomainPinVcpu (virDomainPtr dom, unsigned int vcpu,
756
                         unsigned char *cpumap, int maplen)
757
{
758
    GET_PRIVATE(dom->conn);
759
    int i;
760

761 762 763
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
            drivers[i]->domainPinVcpu &&
764 765
            drivers[i]->domainPinVcpu (dom, vcpu, cpumap, maplen) == 0)
            return 0;
766

767
    return -1;
768 769 770 771
}

static int
xenUnifiedDomainGetVcpus (virDomainPtr dom,
772 773
                          virVcpuInfoPtr info, int maxinfo,
                          unsigned char *cpumaps, int maplen)
774
{
775
    GET_PRIVATE(dom->conn);
776
    int i, ret;
777

778 779
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainGetVcpus) {
780 781 782 783
            ret = drivers[i]->domainGetVcpus (dom, info, maxinfo, cpumaps, maplen);
            if (ret > 0)
                return ret;
        }
784
    return -1;
785 786 787 788 789
}

static int
xenUnifiedDomainGetMaxVcpus (virDomainPtr dom)
{
790
    GET_PRIVATE(dom->conn);
791
    int i, ret;
792

793 794
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainGetMaxVcpus) {
795 796 797
            ret = drivers[i]->domainGetMaxVcpus (dom);
            if (ret != 0) return ret;
        }
798

799
    return -1;
800 801 802 803 804
}

static char *
xenUnifiedDomainDumpXML (virDomainPtr dom, int flags)
{
805
    GET_PRIVATE(dom->conn);
806 807
    int i;
    char *ret;
808

809 810
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainDumpXML) {
811 812 813
            ret = drivers[i]->domainDumpXML (dom, flags);
            if (ret) return ret;
        }
814

815 816 817 818
    /* XXX May need to return an error here if sub-drivers didn't
     * set one.  We really should change these to direct calls to
     * the sub-drivers at a later date.
     */
819
    return NULL;
820 821
}

822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
static int
xenUnifiedDomainMigratePrepare (virConnectPtr dconn,
                                char **cookie,
                                int *cookielen,
                                const char *uri_in,
                                char **uri_out,
                                unsigned long flags,
                                const char *dname,
                                unsigned long resource)
{
    GET_PRIVATE(dconn);

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

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

static int
xenUnifiedDomainMigratePerform (virDomainPtr dom,
                                const char *cookie,
                                int cookielen,
                                const char *uri,
                                unsigned long flags,
                                const char *dname,
                                unsigned long resource)
{
    GET_PRIVATE(dom->conn);

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

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

static virDomainPtr
xenUnifiedDomainMigrateFinish (virConnectPtr dconn,
                               const char *dname,
                               const char *cookie ATTRIBUTE_UNUSED,
                               int cookielen ATTRIBUTE_UNUSED,
                               const char *uri ATTRIBUTE_UNUSED,
                               unsigned long flags ATTRIBUTE_UNUSED)
{
    return xenUnifiedDomainLookupByName (dconn, dname);
}

873 874
static int
xenUnifiedListDefinedDomains (virConnectPtr conn, char **const names,
875
                              int maxnames)
876
{
877
    GET_PRIVATE(conn);
878 879
    int i;
    int ret;
880

881 882
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->listDefinedDomains) {
883 884 885
            ret = drivers[i]->listDefinedDomains (conn, names, maxnames);
            if (ret >= 0) return ret;
        }
886

887
    return -1;
888 889 890 891 892
}

static int
xenUnifiedNumOfDefinedDomains (virConnectPtr conn)
{
893
    GET_PRIVATE(conn);
894 895
    int i;
    int ret;
896

897 898
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->numOfDefinedDomains) {
899 900 901
            ret = drivers[i]->numOfDefinedDomains (conn);
            if (ret >= 0) return ret;
        }
902

903
    return -1;
904 905 906 907 908
}

static int
xenUnifiedDomainCreate (virDomainPtr dom)
{
909
    GET_PRIVATE(dom->conn);
910
    int i;
911

912 913
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainCreate &&
914 915
            drivers[i]->domainCreate (dom) == 0)
            return 0;
916

917
    return -1;
918 919 920 921 922
}

static virDomainPtr
xenUnifiedDomainDefineXML (virConnectPtr conn, const char *xml)
{
923
    GET_PRIVATE(conn);
924 925
    int i;
    virDomainPtr ret;
926

927 928
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainDefineXML) {
929 930 931
            ret = drivers[i]->domainDefineXML (conn, xml);
            if (ret) return ret;
        }
932

933
    return NULL;
934 935 936 937 938
}

static int
xenUnifiedDomainUndefine (virDomainPtr dom)
{
939
    GET_PRIVATE(dom->conn);
940
    int i;
941

942 943
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainUndefine &&
944 945
            drivers[i]->domainUndefine (dom) == 0)
            return 0;
946

947
    return -1;
948 949 950 951 952
}

static int
xenUnifiedDomainAttachDevice (virDomainPtr dom, char *xml)
{
953
    GET_PRIVATE(dom->conn);
954
    int i;
955

956 957
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainAttachDevice &&
958 959
            drivers[i]->domainAttachDevice (dom, xml) == 0)
            return 0;
960

961
    return -1;
962 963 964 965 966
}

static int
xenUnifiedDomainDetachDevice (virDomainPtr dom, char *xml)
{
967
    GET_PRIVATE(dom->conn);
968
    int i;
969

970 971
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] && drivers[i]->domainDetachDevice &&
972 973
            drivers[i]->domainDetachDevice (dom, xml) == 0)
            return 0;
974

975
    return -1;
976 977
}

978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 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
static char *
xenUnifiedDomainGetSchedulerType (virDomainPtr dom, int *nparams)
{
    GET_PRIVATE(dom->conn);
    int i;
    char *schedulertype;

    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; i++) {
        if (priv->opened[i] && drivers[i]->domainGetSchedulerType) {
            schedulertype = drivers[i]->domainGetSchedulerType (dom, nparams);
	    if (schedulertype != NULL)
		return(schedulertype); 
        }
    }
    return(NULL);
}

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

    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i) {
        if (priv->opened[i] && drivers[i]->domainGetSchedulerParameters) {
           ret = drivers[i]->domainGetSchedulerParameters(dom, params, nparams);
	   if (ret == 0)
	       return(0);
	}
    }
    return(-1);
}

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

    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i) {
        if (priv->opened[i] && drivers[i]->domainSetSchedulerParameters) {
           ret = drivers[i]->domainSetSchedulerParameters(dom, params, nparams);
	   if (ret == 0)
	       return 0;
	}
    }

    return(-1);
}

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
static int
xenUnifiedDomainBlockStats (virDomainPtr dom, const char *path,
                            struct _virDomainBlockStats *stats)
{
    GET_PRIVATE (dom->conn);

    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET])
        return xenHypervisorDomainBlockStats (dom, path, stats);

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

static int
xenUnifiedDomainInterfaceStats (virDomainPtr dom, const char *path,
                                struct _virDomainInterfaceStats *stats)
{
    GET_PRIVATE (dom->conn);

    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET])
        return xenHypervisorDomainInterfaceStats (dom, path, stats);

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

1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
static int
xenUnifiedNodeGetCellsFreeMemory (virConnectPtr conn, unsigned long long *freeMems,
                                  int startCell, int maxCells)
{
    GET_PRIVATE (conn);

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

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

1070 1071
/*----- Register with libvirt.c, and initialise Xen drivers. -----*/

1072 1073 1074
#define VERSION ((DOM0_INTERFACE_VERSION >> 24) * 1000000 +         \
                 ((DOM0_INTERFACE_VERSION >> 16) & 0xFF) * 1000 +	\
                 (DOM0_INTERFACE_VERSION & 0xFFFF))
1075 1076 1077

/* The interface which we export upwards to libvirt.c. */
static virDriver xenUnifiedDriver = {
1078
    .no = VIR_DRV_XEN_UNIFIED,
1079
    .name = "Xen",
1080 1081 1082
    .ver = VERSION,
    .open 			= xenUnifiedOpen,
    .close 			= xenUnifiedClose,
1083
    .supports_feature   = xenUnifiedSupportsFeature,
1084 1085
    .type 			= xenUnifiedType,
    .version 			= xenUnifiedVersion,
1086 1087
    .getHostname    = xenUnifiedGetHostname,
    .getURI         = xenUnifiedGetURI,
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
    .getMaxVcpus 			= xenUnifiedGetMaxVcpus,
    .nodeGetInfo 			= xenUnifiedNodeGetInfo,
    .getCapabilities 		= xenUnifiedGetCapabilities,
    .listDomains 			= xenUnifiedListDomains,
    .numOfDomains 		= xenUnifiedNumOfDomains,
    .domainCreateLinux 		= xenUnifiedDomainCreateLinux,
    .domainLookupByID 		= xenUnifiedDomainLookupByID,
    .domainLookupByUUID 		= xenUnifiedDomainLookupByUUID,
    .domainLookupByName 		= xenUnifiedDomainLookupByName,
    .domainSuspend 		= xenUnifiedDomainSuspend,
    .domainResume 		= xenUnifiedDomainResume,
    .domainShutdown 		= xenUnifiedDomainShutdown,
    .domainReboot 		= xenUnifiedDomainReboot,
    .domainDestroy 		= xenUnifiedDomainDestroy,
    .domainGetOSType 		= xenUnifiedDomainGetOSType,
    .domainGetMaxMemory 		= xenUnifiedDomainGetMaxMemory,
    .domainSetMaxMemory 		= xenUnifiedDomainSetMaxMemory,
    .domainSetMemory 		= xenUnifiedDomainSetMemory,
    .domainGetInfo 		= xenUnifiedDomainGetInfo,
    .domainSave 			= xenUnifiedDomainSave,
    .domainRestore 		= xenUnifiedDomainRestore,
    .domainCoreDump 		= xenUnifiedDomainCoreDump,
    .domainSetVcpus 		= xenUnifiedDomainSetVcpus,
    .domainPinVcpu 		= xenUnifiedDomainPinVcpu,
    .domainGetVcpus 		= xenUnifiedDomainGetVcpus,
    .domainGetMaxVcpus 		= xenUnifiedDomainGetMaxVcpus,
    .domainDumpXML 		= xenUnifiedDomainDumpXML,
    .listDefinedDomains 		= xenUnifiedListDefinedDomains,
    .numOfDefinedDomains 		= xenUnifiedNumOfDefinedDomains,
    .domainCreate 		= xenUnifiedDomainCreate,
    .domainDefineXML 		= xenUnifiedDomainDefineXML,
    .domainUndefine 		= xenUnifiedDomainUndefine,
    .domainAttachDevice 		= xenUnifiedDomainAttachDevice,
    .domainDetachDevice 		= xenUnifiedDomainDetachDevice,
1122 1123 1124
    .domainGetSchedulerType	= xenUnifiedDomainGetSchedulerType,
    .domainGetSchedulerParameters	= xenUnifiedDomainGetSchedulerParameters,
    .domainSetSchedulerParameters	= xenUnifiedDomainSetSchedulerParameters,
1125 1126 1127
    .domainMigratePrepare		= xenUnifiedDomainMigratePrepare,
    .domainMigratePerform		= xenUnifiedDomainMigratePerform,
    .domainMigrateFinish		= xenUnifiedDomainMigrateFinish,
1128 1129
    .domainBlockStats	= xenUnifiedDomainBlockStats,
    .domainInterfaceStats = xenUnifiedDomainInterfaceStats,
1130
    .nodeGetCellsFreeMemory = xenUnifiedNodeGetCellsFreeMemory,
1131 1132
};

1133 1134 1135 1136 1137 1138 1139
/**
 * xenUnifiedRegister:
 *
 * Register xen related drivers
 *
 * Returns the driver priority or -1 in case of error.
 */
1140 1141 1142
int
xenUnifiedRegister (void)
{
1143 1144 1145 1146 1147 1148
    /* Ignore failures here. */
    (void) xenHypervisorInit ();
    (void) xenProxyInit ();
    (void) xenDaemonInit ();
    (void) xenStoreInit ();
    (void) xenXMInit ();
1149

1150
    return virRegisterDriver (&xenUnifiedDriver);
1151 1152 1153
}

#endif /* WITH_XEN */
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167

/*
 * vim: set tabstop=4:
 * vim: set shiftwidth=4:
 * vim: set expandtab:
 */
/*
 * Local variables:
 *  indent-tabs-mode: nil
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  tab-width: 4
 * End:
 */