xen_driver.c 72.9 KB
Newer Older
1
/*
2
 * xen_driver.c: Unified Xen driver.
3
 *
4
 * Copyright (C) 2007-2013 Red Hat, Inc.
5
 *
O
Osier Yang 已提交
6 7 8 9 10 11 12 13 14 15 16
 * 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
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <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 "virerror.h"
43
#include "virlog.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 "virxml.h"
57
#include "viralloc.h"
58
#include "node_device_conf.h"
59
#include "virpci.h"
60
#include "viruuid.h"
61
#include "fdstream.h"
E
Eric Blake 已提交
62
#include "virfile.h"
M
Martin Kletzander 已提交
63
#include "viruri.h"
64
#include "vircommand.h"
65
#include "virnodesuspend.h"
66
#include "nodeinfo.h"
67
#include "configmake.h"
68
#include "virstring.h"
69

70
#define VIR_FROM_THIS VIR_FROM_XEN
71
#define XEN_SAVE_DIR LOCALSTATEDIR "/lib/libvirt/xen/save"
72

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

82

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

91
static bool is_privileged = false;
J
John Levon 已提交
92

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

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

D
Daniel P. Berrange 已提交
111
    priv = conn->privateData;
112

D
Daniel P. Berrange 已提交
113 114
    priv->nbNodeCells = nodeInfo.nodes;
    priv->nbNodeCpus = nodeInfo.cpus;
115 116
}

D
Daniel P. Berrange 已提交
117

118 119 120 121 122 123 124 125 126 127 128 129 130 131
/**
 * 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 已提交
132
    int ncpus;
133
    int nb_vcpu;
134
    virBitmapPtr cpulist = NULL;
135 136 137 138 139 140
    unsigned char *cpumap = NULL;
    size_t cpumaplen;
    int nb = 0;
    int n, m;
    virVcpuInfoPtr cpuinfo = NULL;
    virNodeInfo nodeinfo;
D
Daniel P. Berrange 已提交
141
    xenUnifiedPrivatePtr priv;
142 143

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

D
Daniel P. Berrange 已提交
146 147 148
    priv = dom->conn->privateData;

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

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

    if ((ncpus = xenUnifiedDomainGetVcpus(dom, cpuinfo, nb_vcpu,
                                          cpumap, cpumaplen)) >= 0) {
173
        for (n = 0 ; n < ncpus ; n++) {
D
Daniel P. Berrange 已提交
174
            for (m = 0 ; m < priv->nbNodeCpus; m++) {
175 176 177
                bool used;
                ignore_value(virBitmapGetBit(cpulist, m, &used));
                if ((!used) &&
178
                    (VIR_CPU_USABLE(cpumap, cpumaplen, n, m))) {
179
                    ignore_value(virBitmapSetBit(cpulist, m));
180 181
                    nb++;
                    /* if all CPU are used just return NULL */
D
Daniel P. Berrange 已提交
182
                    if (nb == priv->nbNodeCpus)
183 184 185 186 187
                        goto done;

                }
            }
        }
188
        res = virBitmapFormat(cpulist);
189 190 191
    }

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

J
John Levon 已提交
198
static int
199
xenUnifiedStateInitialize(bool privileged,
200 201
                          virStateInhibitCallback callback ATTRIBUTE_UNUSED,
                          void *opaque ATTRIBUTE_UNUSED)
J
John Levon 已提交
202
{
203 204 205
    /* Don't allow driver to work in non-root libvirtd */
    if (privileged)
        is_privileged = true;
J
John Levon 已提交
206 207 208 209
    return 0;
}

static virStateDriver state_driver = {
210
    .name = "Xen",
211
    .stateInitialize = xenUnifiedStateInitialize,
J
John Levon 已提交
212 213
};

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

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

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

J
Jim Fehlig 已提交
243 244
#ifdef WITH_LIBXL
static int
245
xenUnifiedXendProbe(void)
J
Jim Fehlig 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259
{
    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

260

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
static int
xenDomainDeviceDefPostParse(virDomainDeviceDefPtr dev,
                            virDomainDefPtr def,
                            virCapsPtr caps ATTRIBUTE_UNUSED,
                            void *opaque ATTRIBUTE_UNUSED)
{
    if (dev->type == VIR_DOMAIN_DEVICE_CHR &&
        dev->data.chr->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE &&
        dev->data.chr->targetType == VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_NONE &&
        STRNEQ(def->os.type, "hvm"))
        dev->data.chr->targetType = VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_XEN;

    return 0;
}


277 278
virDomainDefParserConfig xenDomainDefParserConfig = {
    .macPrefix = { 0x00, 0x16, 0x3e },
279
    .devicesPostParseCallback = xenDomainDeviceDefPostParse,
280 281
};

282

283 284 285 286 287 288 289 290
virDomainXMLOptionPtr
xenDomainXMLConfInit(void)
{
    return virDomainXMLOptionNew(&xenDomainDefParserConfig,
                                 NULL, NULL);
}


291
static virDrvOpenStatus
292
xenUnifiedConnectOpen(virConnectPtr conn, virConnectAuthPtr auth, unsigned int flags)
293
{
294
    xenUnifiedPrivatePtr priv;
295
    char ebuf[1024];
296

J
John Levon 已提交
297 298 299 300
    /*
     * Only the libvirtd instance can open this driver.
     * Everything else falls back to the remote driver.
     */
301
    if (!is_privileged)
J
John Levon 已提交
302 303
        return VIR_DRV_OPEN_DECLINED;

304 305 306 307
    if (conn->uri == NULL) {
        if (!xenUnifiedProbe())
            return VIR_DRV_OPEN_DECLINED;

308
        if (!(conn->uri = virURIParse("xen:///")))
309
            return VIR_DRV_OPEN_ERROR;
310 311 312 313 314 315 316 317 318 319 320 321
    } 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, "/")) {
322 323 324
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("unexpected Xen URI path '%s', try xen:///"),
                               conn->uri->path);
325 326
                return VIR_DRV_OPEN_ERROR;
            }
327

328 329 330 331 332 333
            /* 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 {
334
            return VIR_DRV_OPEN_DECLINED;
335 336
        }
    }
337

J
Jim Fehlig 已提交
338 339 340 341 342 343 344
#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

345 346
    /* We now know the URI is definitely for this driver, so beyond
     * here, don't return DECLINED, always use ERROR */
347 348

    /* Allocate per-connection private data. */
349
    if (VIR_ALLOC(priv) < 0) {
350
        virReportOOMError();
351 352
        return VIR_DRV_OPEN_ERROR;
    }
D
Daniel P. Berrange 已提交
353
    if (virMutexInit(&priv->lock) < 0) {
354 355
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("cannot initialize mutex"));
D
Daniel P. Berrange 已提交
356 357 358
        VIR_FREE(priv);
        return VIR_DRV_OPEN_ERROR;
    }
359

360
    if (!(priv->domainEvents = virDomainEventStateNew())) {
D
Daniel P. Berrange 已提交
361 362
        virMutexDestroy(&priv->lock);
        VIR_FREE(priv);
363 364
        return VIR_DRV_OPEN_ERROR;
    }
D
Daniel P. Berrange 已提交
365 366
    conn->privateData = priv;

367 368 369 370 371
    priv->handle = -1;
    priv->xendConfigVersion = -1;
    priv->xshandle = NULL;


372 373 374 375 376 377
    /* Hypervisor required to succeed */
    VIR_DEBUG("Trying hypervisor sub-driver");
    if (xenHypervisorOpen(conn, auth, flags) < 0)
        goto error;
    VIR_DEBUG("Activated hypervisor sub-driver");
    priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] = 1;
378

379
    /* XenD is required to succeed */
380
    VIR_DEBUG("Trying XenD sub-driver");
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
    if (xenDaemonOpen(conn, auth, flags) < 0)
        goto error;
    VIR_DEBUG("Activated XenD sub-driver");
    priv->opened[XEN_UNIFIED_XEND_OFFSET] = 1;

    /* For old XenD, the XM driver is required to succeed */
    if (priv->xendConfigVersion <= XEND_CONFIG_VERSION_3_0_3) {
        VIR_DEBUG("Trying XM sub-driver");
        if (xenXMOpen(conn, auth, flags) < 0)
            goto error;
        VIR_DEBUG("Activated XM sub-driver");
        priv->opened[XEN_UNIFIED_XM_OFFSET] = 1;
    }

    VIR_DEBUG("Trying XS sub-driver");
    if (xenStoreOpen(conn, auth, flags) < 0)
        goto error;
    VIR_DEBUG("Activated XS sub-driver");
    priv->opened[XEN_UNIFIED_XS_OFFSET] = 1;
400

D
Daniel P. Berrange 已提交
401 402
    xenNumaInit(conn);

403
    if (!(priv->caps = xenHypervisorMakeCapabilities(conn))) {
404
        VIR_DEBUG("Failed to make capabilities");
405
        goto error;
406 407
    }

408
    if (!(priv->xmlopt = xenDomainXMLConfInit()))
409
        goto error;
410

411
#if WITH_XEN_INOTIFY
412 413 414 415 416
    VIR_DEBUG("Trying Xen inotify sub-driver");
    if (xenInotifyOpen(conn, auth, flags) < 0)
        goto error;
    VIR_DEBUG("Activated Xen inotify sub-driver");
    priv->opened[XEN_UNIFIED_INOTIFY_OFFSET] = 1;
417 418
#endif

E
Eric Blake 已提交
419
    if (!(priv->saveDir = strdup(XEN_SAVE_DIR))) {
420
        virReportOOMError();
421
        goto error;
422 423 424
    }

    if (virFileMakePath(priv->saveDir) < 0) {
425
        VIR_ERROR(_("Errored to create save dir '%s': %s"), priv->saveDir,
426
                  virStrerror(errno, ebuf, sizeof(ebuf)));
427
        goto error;
428 429
    }

430
    return VIR_DRV_OPEN_SUCCESS;
431

432
error:
433
    VIR_DEBUG("Failed to activate a mandatory sub-driver");
434 435 436 437 438 439 440 441 442 443 444 445
#if WITH_XEN_INOTIFY
    if (priv->opened[XEN_UNIFIED_INOTIFY_OFFSET])
        xenInotifyClose(conn);
#endif
    if (priv->opened[XEN_UNIFIED_XM_OFFSET])
        xenXMClose(conn);
    if (priv->opened[XEN_UNIFIED_XS_OFFSET])
        xenStoreClose(conn);
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        xenDaemonClose(conn);
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET])
        xenHypervisorClose(conn);
D
Daniel P. Berrange 已提交
446
    virMutexDestroy(&priv->lock);
447
    VIR_FREE(priv->saveDir);
448
    VIR_FREE(priv);
D
Daniel P. Berrange 已提交
449
    conn->privateData = NULL;
450
    return VIR_DRV_OPEN_ERROR;
451 452 453
}

static int
454
xenUnifiedConnectClose(virConnectPtr conn)
455
{
456
    xenUnifiedPrivatePtr priv = conn->privateData;
457

458
    virObjectUnref(priv->caps);
459
    virObjectUnref(priv->xmlopt);
460
    virDomainEventStateFree(priv->domainEvents);
461

462 463 464 465 466 467 468 469 470 471 472 473
#if WITH_XEN_INOTIFY
    if (priv->opened[XEN_UNIFIED_INOTIFY_OFFSET])
        xenInotifyClose(conn);
#endif
    if (priv->opened[XEN_UNIFIED_XM_OFFSET])
        xenXMClose(conn);
    if (priv->opened[XEN_UNIFIED_XS_OFFSET])
        xenStoreClose(conn);
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        xenDaemonClose(conn);
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET])
        xenHypervisorClose(conn);
474

475
    VIR_FREE(priv->saveDir);
D
Daniel P. Berrange 已提交
476 477
    virMutexDestroy(&priv->lock);
    VIR_FREE(conn->privateData);
478

479
    return 0;
480 481
}

482 483 484 485 486 487 488 489 490 491 492

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


493
static const char *
494
xenUnifiedConnectGetType(virConnectPtr conn)
495
{
496
    xenUnifiedPrivatePtr priv = conn->privateData;
497
    int i;
498

499
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
500 501
        if (priv->opened[i])
            return "Xen";
502

503
    return NULL;
504 505
}

506 507
/* Which features are supported by this driver? */
static int
508
xenUnifiedConnectSupportsFeature(virConnectPtr conn ATTRIBUTE_UNUSED, int feature)
509 510
{
    switch (feature) {
511 512 513 514 515
    case VIR_DRV_FEATURE_MIGRATION_V1:
    case VIR_DRV_FEATURE_MIGRATION_DIRECT:
        return 1;
    default:
        return 0;
516 517 518
    }
}

519
static int
520
xenUnifiedConnectGetVersion(virConnectPtr conn, unsigned long *hvVer)
521
{
522
    xenUnifiedPrivatePtr priv = conn->privateData;
523
    int i;
524

525 526
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
E
Eric Blake 已提交
527
            drivers[i]->xenVersion &&
528
            drivers[i]->xenVersion(conn, hvVer) == 0)
529
            return 0;
530

531
    return -1;
532 533
}

534 535 536 537 538 539 540

static char *xenUnifiedConnectGetHostname(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return virGetHostname();
}


541
static int
542
xenUnifiedConnectIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
543 544 545 546 547
{
    return 0;
}

static int
548
xenUnifiedConnectIsSecure(virConnectPtr conn)
549
{
550
    xenUnifiedPrivatePtr priv = conn->privateData;
551 552 553 554 555 556 557 558 559 560
    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;
}

561
static int
562
xenUnifiedConnectIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
563 564 565 566 567
{
    /* XenD reconnects for each request */
    return 1;
}

568
int
569
xenUnifiedConnectGetMaxVcpus(virConnectPtr conn, const char *type)
570
{
571
    xenUnifiedPrivatePtr priv = conn->privateData;
572

573
    if (type && STRCASENEQ(type, "Xen")) {
574
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
575 576
        return -1;
    }
577

578
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET])
579
        return xenHypervisorGetMaxVcpus(conn, type);
580
    else {
581
        virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
582 583
        return -1;
    }
584 585 586
}

static int
587
xenUnifiedNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info)
588
{
589
    xenUnifiedPrivatePtr priv = conn->privateData;
590

591 592
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        return xenDaemonNodeGetInfo(conn, info);
593
    return -1;
594 595 596
}

static char *
597
xenUnifiedConnectGetCapabilities(virConnectPtr conn)
598
{
D
Daniel P. Berrange 已提交
599 600
    xenUnifiedPrivatePtr priv = conn->privateData;
    char *xml;
601

D
Daniel P. Berrange 已提交
602
    if (!(xml = virCapabilitiesFormatXML(priv->caps))) {
603
        virReportOOMError();
D
Daniel P. Berrange 已提交
604 605
        return NULL;
    }
606

D
Daniel P. Berrange 已提交
607
    return xml;
608 609 610
}

static int
611
xenUnifiedConnectListDomains(virConnectPtr conn, int *ids, int maxids)
612
{
613
    xenUnifiedPrivatePtr priv = conn->privateData;
614
    int ret;
615

616 617
    /* Try xenstore. */
    if (priv->opened[XEN_UNIFIED_XS_OFFSET]) {
618
        ret = xenStoreListDomains(conn, ids, maxids);
619 620
        if (ret >= 0) return ret;
    }
621

622 623
    /* Try HV. */
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET]) {
624
        ret = xenHypervisorListDomains(conn, ids, maxids);
625 626 627 628 629
        if (ret >= 0) return ret;
    }

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

634
    return -1;
635 636 637
}

static int
638
xenUnifiedConnectNumOfDomains(virConnectPtr conn)
639
{
640
    xenUnifiedPrivatePtr priv = conn->privateData;
641
    int ret;
642

643 644
    /* Try xenstore. */
    if (priv->opened[XEN_UNIFIED_XS_OFFSET]) {
645
        ret = xenStoreNumOfDomains(conn);
646 647 648 649 650
        if (ret >= 0) return ret;
    }

    /* Try HV. */
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET]) {
651
        ret = xenHypervisorNumOfDomains(conn);
652 653 654 655 656
        if (ret >= 0) return ret;
    }

    /* Try xend. */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
657
        ret = xenDaemonNumOfDomains(conn);
658 659 660
        if (ret >= 0) return ret;
    }

661
    return -1;
662 663 664
}

static virDomainPtr
665 666
xenUnifiedDomainCreateXML(virConnectPtr conn,
                          const char *xmlDesc, unsigned int flags)
667
{
668
    xenUnifiedPrivatePtr priv = conn->privateData;
669

670 671
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        return xenDaemonCreateXML(conn, xmlDesc, flags);
672
    return NULL;
673 674
}

675 676 677 678
/* 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.
 */
679
static virDomainPtr
680
xenUnifiedDomainLookupByID(virConnectPtr conn, int id)
681
{
682
    xenUnifiedPrivatePtr priv = conn->privateData;
683
    virDomainPtr ret;
684

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

690 691
    /* Try hypervisor/xenstore combo. */
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET]) {
692
        ret = xenHypervisorLookupDomainByID(conn, id);
693 694 695 696
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }

697 698
    /* Try xend. */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
699
        ret = xenDaemonLookupByID(conn, id);
700 701 702
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }
703

704
    /* Not found. */
705
    virReportError(VIR_ERR_NO_DOMAIN, __FUNCTION__);
706
    return NULL;
707 708 709
}

static virDomainPtr
710 711
xenUnifiedDomainLookupByUUID(virConnectPtr conn,
                             const unsigned char *uuid)
712
{
713
    xenUnifiedPrivatePtr priv = conn->privateData;
714
    virDomainPtr ret;
715

716 717 718
    /* Reset any connection-level errors in virterror first, in case
     * there is one hanging around from a previous call.
     */
719
    virConnResetLastError(conn);
720

721 722
    /* Try hypervisor/xenstore combo. */
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET]) {
723
        ret = xenHypervisorLookupDomainByUUID(conn, uuid);
724 725 726 727
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }

728 729
    /* Try xend. */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
730
        ret = xenDaemonLookupByUUID(conn, uuid);
731 732 733
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }
734

735 736
    /* Try XM for inactive domains. */
    if (priv->opened[XEN_UNIFIED_XM_OFFSET]) {
737
        ret = xenXMDomainLookupByUUID(conn, uuid);
738 739 740 741 742
        if (ret || conn->err.code != VIR_ERR_OK)
            return ret;
    }

    /* Not found. */
743
    virReportError(VIR_ERR_NO_DOMAIN, __FUNCTION__);
744
    return NULL;
745 746 747
}

static virDomainPtr
748 749
xenUnifiedDomainLookupByName(virConnectPtr conn,
                             const char *name)
750
{
751
    xenUnifiedPrivatePtr priv = conn->privateData;
752
    virDomainPtr ret;
753

754 755 756
    /* Reset any connection-level errors in virterror first, in case
     * there is one hanging around from a previous call.
     */
757
    virConnResetLastError(conn);
758 759 760

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

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

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

780
    /* Not found. */
781
    virReportError(VIR_ERR_NO_DOMAIN, __FUNCTION__);
782
    return NULL;
783 784
}

785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803

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
804
xenUnifiedDomainIsPersistent(virDomainPtr dom)
805
{
806
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
    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) {
835
                        virReportOOMError();
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
                        goto done;
                    }
                    if (access(path, R_OK) == 0)
                        ret = 1;
                    else if (errno == ENOENT)
                        ret = 0;
                }
            }
        }
    }

done:
    if (currdom)
        virDomainFree(currdom);

    return ret;
}

854 855 856 857 858 859
static int
xenUnifiedDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

860
static int
861
xenUnifiedDomainSuspend(virDomainPtr dom)
862
{
863
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
864
    int i;
865

866 867 868
    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
869
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
870
        if (i != XEN_UNIFIED_HYPERVISOR_OFFSET &&
871
            priv->opened[i] &&
E
Eric Blake 已提交
872
            drivers[i]->xenDomainSuspend &&
873
            drivers[i]->xenDomainSuspend(dom) == 0)
874
            return 0;
875

876
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] &&
E
Eric Blake 已提交
877
        xenHypervisorPauseDomain(dom) == 0)
878
        return 0;
879

880
    return -1;
881 882 883
}

static int
884
xenUnifiedDomainResume(virDomainPtr dom)
885
{
886
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
887
    int i;
888

889 890 891
    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
892
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
893
        if (i != XEN_UNIFIED_HYPERVISOR_OFFSET &&
894
            priv->opened[i] &&
E
Eric Blake 已提交
895
            drivers[i]->xenDomainResume &&
896
            drivers[i]->xenDomainResume(dom) == 0)
897
            return 0;
898

899
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] &&
E
Eric Blake 已提交
900
        xenHypervisorResumeDomain(dom) == 0)
901
        return 0;
902

903
    return -1;
904 905 906
}

static int
907 908
xenUnifiedDomainShutdownFlags(virDomainPtr dom,
                              unsigned int flags)
909
{
910
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
911
    int i;
912

913 914
    virCheckFlags(0, -1);

915 916
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
E
Eric Blake 已提交
917
            drivers[i]->xenDomainShutdown &&
918
            drivers[i]->xenDomainShutdown(dom) == 0)
919
            return 0;
920

921
    return -1;
922 923
}

924 925 926 927 928 929
static int
xenUnifiedDomainShutdown(virDomainPtr dom)
{
    return xenUnifiedDomainShutdownFlags(dom, 0);
}

930
static int
931
xenUnifiedDomainReboot(virDomainPtr dom, unsigned int flags)
932
{
933
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
934
    int i;
935

936 937
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
E
Eric Blake 已提交
938
            drivers[i]->xenDomainReboot &&
939
            drivers[i]->xenDomainReboot(dom, flags) == 0)
940
            return 0;
941

942
    return -1;
943 944
}

945 946 947 948
static int
xenUnifiedDomainDestroyFlags(virDomainPtr dom,
                             unsigned int flags)
{
949
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
950 951 952 953 954 955 956 957 958 959
    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 已提交
960 961
            drivers[i]->xenDomainDestroyFlags &&
            drivers[i]->xenDomainDestroyFlags(dom, flags) == 0)
962 963 964
            return 0;

    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET] &&
E
Eric Blake 已提交
965
        xenHypervisorDestroyDomainFlags(dom, flags) == 0)
966 967 968 969 970
        return 0;

    return -1;
}

971 972 973 974 975 976
static int
xenUnifiedDomainDestroy(virDomainPtr dom)
{
    return xenUnifiedDomainDestroyFlags(dom, 0);
}

977
static char *
978
xenUnifiedDomainGetOSType(virDomainPtr dom)
979
{
980
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
981 982
    int i;
    char *ret;
983

984
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
985
        if (priv->opened[i] && drivers[i]->xenDomainGetOSType) {
986
            ret = drivers[i]->xenDomainGetOSType(dom);
987 988
            if (ret) return ret;
        }
989

990
    return NULL;
991 992
}

993
static unsigned long long
994
xenUnifiedDomainGetMaxMemory(virDomainPtr dom)
995
{
996
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
997
    int i;
998
    unsigned long long ret;
999

1000
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1001
        if (priv->opened[i] && drivers[i]->xenDomainGetMaxMemory) {
1002
            ret = drivers[i]->xenDomainGetMaxMemory(dom);
1003 1004
            if (ret != 0) return ret;
        }
1005

1006
    return 0;
1007 1008 1009
}

static int
1010
xenUnifiedDomainSetMaxMemory(virDomainPtr dom, unsigned long memory)
1011
{
1012
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1013
    int i;
1014

1015 1016
    /* Prefer xend for setting max memory */
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
1017
        if (xenDaemonDomainSetMaxMemory(dom, memory) == 0)
1018 1019 1020
            return 0;
    }

1021
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
1022 1023
        if (i != XEN_UNIFIED_XEND_OFFSET &&
            priv->opened[i] &&
E
Eric Blake 已提交
1024
            drivers[i]->xenDomainSetMaxMemory &&
1025
            drivers[i]->xenDomainSetMaxMemory(dom, memory) == 0)
1026
            return 0;
1027

1028
    return -1;
1029 1030 1031
}

static int
1032
xenUnifiedDomainSetMemory(virDomainPtr dom, unsigned long memory)
1033
{
1034
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1035
    int i;
1036

1037 1038
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
E
Eric Blake 已提交
1039
            drivers[i]->xenDomainSetMemory &&
1040
            drivers[i]->xenDomainSetMemory(dom, memory) == 0)
1041
            return 0;
1042

1043
    return -1;
1044 1045 1046
}

static int
1047
xenUnifiedDomainGetInfo(virDomainPtr dom, virDomainInfoPtr info)
1048
{
1049
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1050
    int i;
1051

1052 1053
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
E
Eric Blake 已提交
1054
            drivers[i]->xenDomainGetInfo &&
1055
            drivers[i]->xenDomainGetInfo(dom, info) == 0)
1056
            return 0;
1057

1058
    return -1;
1059 1060
}

1061 1062 1063 1064 1065 1066
static int
xenUnifiedDomainGetState(virDomainPtr dom,
                         int *state,
                         int *reason,
                         unsigned int flags)
{
1067
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
    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;
}

1102
static int
1103 1104
xenUnifiedDomainSaveFlags(virDomainPtr dom, const char *to, const char *dxml,
                          unsigned int flags)
1105
{
1106
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1107

1108 1109
    virCheckFlags(0, -1);
    if (dxml) {
1110 1111
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
1112 1113 1114
        return -1;
    }

1115 1116
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        return xenDaemonDomainSave(dom, to);
1117
    return -1;
1118 1119 1120
}

static int
1121 1122 1123 1124 1125
xenUnifiedDomainSave(virDomainPtr dom, const char *to)
{
    return xenUnifiedDomainSaveFlags(dom, to, NULL, 0);
}

1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
static char *
xenUnifiedDomainManagedSavePath(xenUnifiedPrivatePtr priv, virDomainPtr dom)
{
    char *ret;

    if (virAsprintf(&ret, "%s/%s.save", priv->saveDir, dom->name) < 0) {
        virReportOOMError();
        return NULL;
    }

    VIR_DEBUG("managed save image: %s", ret);
    return ret;
}

static int
xenUnifiedDomainManagedSave(virDomainPtr dom, unsigned int flags)
{
1143
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
    char *name;
    int ret = -1;

    virCheckFlags(0, -1);

    name = xenUnifiedDomainManagedSavePath(priv, dom);
    if (!name)
        goto cleanup;

    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        ret = xenDaemonDomainSave(dom, name);

cleanup:
    VIR_FREE(name);
    return ret;
}

static int
xenUnifiedDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
{
1164
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
    char *name;
    int ret = -1;

    virCheckFlags(0, -1);

    name = xenUnifiedDomainManagedSavePath(priv, dom);
    if (!name)
        return ret;

    ret = virFileExists(name);
    VIR_FREE(name);
    return ret;
}

static int
xenUnifiedDomainManagedSaveRemove(virDomainPtr dom, unsigned int flags)
{
1182
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
    char *name;
    int ret = -1;

    virCheckFlags(0, -1);

    name = xenUnifiedDomainManagedSavePath(priv, dom);
    if (!name)
        return ret;

    ret = unlink(name);
    VIR_FREE(name);
    return ret;
}

1197 1198 1199
static int
xenUnifiedDomainRestoreFlags(virConnectPtr conn, const char *from,
                             const char *dxml, unsigned int flags)
1200
{
1201
    xenUnifiedPrivatePtr priv = conn->privateData;
1202

1203 1204
    virCheckFlags(0, -1);
    if (dxml) {
1205 1206
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
1207 1208 1209
        return -1;
    }

1210 1211
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        return xenDaemonDomainRestore(conn, from);
1212
    return -1;
1213 1214
}

1215
static int
1216
xenUnifiedDomainRestore(virConnectPtr conn, const char *from)
1217 1218 1219 1220
{
    return xenUnifiedDomainRestoreFlags(conn, from, NULL, 0);
}

1221
static int
1222
xenUnifiedDomainCoreDump(virDomainPtr dom, const char *to, unsigned int flags)
1223
{
1224
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1225

1226 1227
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        return xenDaemonDomainCoreDump(dom, to, flags);
1228
    return -1;
1229 1230 1231
}

static int
1232 1233
xenUnifiedDomainSetVcpusFlags(virDomainPtr dom, unsigned int nvcpus,
                              unsigned int flags)
1234
{
1235
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1236
    int ret;
1237

1238 1239 1240 1241 1242 1243 1244 1245 1246
    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)) {
1247 1248
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid flag combination: (0x%x)"), flags);
1249 1250 1251
        return -1;
    }
    if (!nvcpus || (unsigned short) nvcpus != nvcpus) {
1252 1253
        virReportError(VIR_ERR_INVALID_ARG,
                       _("argument out of range: %d"), nvcpus);
1254 1255 1256
        return -1;
    }

1257 1258 1259
    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
    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);
1272

1273
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1274
    return -1;
1275 1276
}

1277
static int
1278
xenUnifiedDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus)
1279
{
1280 1281 1282 1283 1284 1285
    unsigned int flags = VIR_DOMAIN_VCPU_LIVE;

    /* 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) {
1286
        xenUnifiedPrivatePtr priv = dom->conn->privateData;
1287
        if (priv->xendConfigVersion >= XEND_CONFIG_VERSION_3_0_4)
1288
            flags |= VIR_DOMAIN_VCPU_CONFIG;
1289
        return xenUnifiedDomainSetVcpusFlags(dom, nvcpus, flags);
1290
    }
1291
    return -1;
1292 1293
}

1294
static int
1295 1296
xenUnifiedDomainPinVcpu(virDomainPtr dom, unsigned int vcpu,
                        unsigned char *cpumap, int maplen)
1297
{
1298
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1299
    int i;
1300

1301 1302
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
        if (priv->opened[i] &&
E
Eric Blake 已提交
1303
            drivers[i]->xenDomainPinVcpu &&
1304
            drivers[i]->xenDomainPinVcpu(dom, vcpu, cpumap, maplen) == 0)
1305
            return 0;
1306

1307
    return -1;
1308 1309 1310
}

static int
1311 1312 1313
xenUnifiedDomainGetVcpus(virDomainPtr dom,
                         virVcpuInfoPtr info, int maxinfo,
                         unsigned char *cpumaps, int maplen)
1314
{
1315
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1316
    int i, ret;
1317

1318
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1319
        if (priv->opened[i] && drivers[i]->xenDomainGetVcpus) {
1320
            ret = drivers[i]->xenDomainGetVcpus(dom, info, maxinfo, cpumaps, maplen);
1321 1322 1323
            if (ret > 0)
                return ret;
        }
1324
    return -1;
1325 1326 1327
}

static int
1328
xenUnifiedDomainGetVcpusFlags(virDomainPtr dom, unsigned int flags)
1329
{
1330
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1331
    int ret;
1332

1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
    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);
1349

1350
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1351
    return -1;
1352 1353
}

1354
static int
1355
xenUnifiedDomainGetMaxVcpus(virDomainPtr dom)
1356 1357 1358 1359 1360
{
    return xenUnifiedDomainGetVcpusFlags(dom, (VIR_DOMAIN_VCPU_LIVE |
                                               VIR_DOMAIN_VCPU_MAXIMUM));
}

1361
static char *
1362
xenUnifiedDomainGetXMLDesc(virDomainPtr dom, unsigned int flags)
1363
{
1364
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1365

1366
    if (dom->id == -1 && priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4) {
1367
        if (priv->opened[XEN_UNIFIED_XM_OFFSET])
1368
            return xenXMDomainGetXMLDesc(dom, flags);
1369 1370 1371
    } else {
        if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
            char *cpus, *res;
D
Daniel P. Berrange 已提交
1372
            xenUnifiedLock(priv);
1373
            cpus = xenDomainUsedCpus(dom);
D
Daniel P. Berrange 已提交
1374
            xenUnifiedUnlock(priv);
1375
            res = xenDaemonDomainGetXMLDesc(dom, flags, cpus);
1376
            VIR_FREE(cpus);
1377
            return res;
1378
        }
1379
    }
1380

1381
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1382
    return NULL;
1383 1384
}

1385 1386

static char *
1387 1388 1389 1390
xenUnifiedConnectDomainXMLFromNative(virConnectPtr conn,
                                     const char *format,
                                     const char *config,
                                     unsigned int flags)
1391 1392 1393 1394
{
    virDomainDefPtr def = NULL;
    char *ret = NULL;
    virConfPtr conf = NULL;
1395 1396 1397
    int id;
    char * tty;
    int vncport;
1398
    xenUnifiedPrivatePtr priv = conn->privateData;
1399

E
Eric Blake 已提交
1400 1401
    virCheckFlags(0, NULL);

1402 1403
    if (STRNEQ(format, XEN_CONFIG_FORMAT_XM) &&
        STRNEQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
1404 1405
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unsupported config type %s"), format);
1406 1407 1408 1409
        return NULL;
    }

    if (STREQ(format, XEN_CONFIG_FORMAT_XM)) {
1410
        conf = virConfReadMem(config, strlen(config), 0);
1411 1412 1413
        if (!conf)
            goto cleanup;

M
Markus Groß 已提交
1414
        def = xenParseXM(conf, priv->xendConfigVersion, priv->caps);
1415
    } else if (STREQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
1416 1417 1418 1419 1420
        id = xenGetDomIdFromSxprString(config, priv->xendConfigVersion);
        xenUnifiedLock(priv);
        tty = xenStoreDomainGetConsolePath(conn, id);
        vncport = xenStoreDomainGetVNCPort(conn, id);
        xenUnifiedUnlock(priv);
M
Markus Groß 已提交
1421
        def = xenParseSxprString(config, priv->xendConfigVersion, tty,
1422
                                       vncport);
1423 1424 1425 1426
    }
    if (!def)
        goto cleanup;

1427
    ret = virDomainDefFormat(def, 0);
1428 1429 1430

cleanup:
    virDomainDefFree(def);
1431 1432
    if (conf)
        virConfFree(conf);
1433 1434 1435 1436 1437 1438
    return ret;
}


#define MAX_CONFIG_SIZE (1024 * 65)
static char *
1439 1440 1441 1442
xenUnifiedConnectDomainXMLToNative(virConnectPtr conn,
                                   const char *format,
                                   const char *xmlData,
                                   unsigned int flags)
1443 1444 1445 1446
{
    virDomainDefPtr def = NULL;
    char *ret = NULL;
    virConfPtr conf = NULL;
1447
    xenUnifiedPrivatePtr priv = conn->privateData;
1448

E
Eric Blake 已提交
1449 1450
    virCheckFlags(0, NULL);

1451 1452
    if (STRNEQ(format, XEN_CONFIG_FORMAT_XM) &&
        STRNEQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
1453 1454
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unsupported config type %s"), format);
1455 1456 1457
        goto cleanup;
    }

1458 1459
    if (!(def = virDomainDefParseString(xmlData, priv->caps, priv->xmlopt,
                                        1 << VIR_DOMAIN_VIRT_XEN, 0)))
1460 1461 1462 1463
        goto cleanup;

    if (STREQ(format, XEN_CONFIG_FORMAT_XM)) {
        int len = MAX_CONFIG_SIZE;
M
Markus Groß 已提交
1464
        conf = xenFormatXM(conn, def, priv->xendConfigVersion);
1465 1466 1467 1468
        if (!conf)
            goto cleanup;

        if (VIR_ALLOC_N(ret, len) < 0) {
1469
            virReportOOMError();
1470 1471 1472 1473 1474 1475 1476 1477
            goto cleanup;
        }

        if (virConfWriteMem(ret, &len, conf) < 0) {
            VIR_FREE(ret);
            goto cleanup;
        }
    } else if (STREQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
M
Markus Groß 已提交
1478
        ret = xenFormatSxpr(conn, def, priv->xendConfigVersion);
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
    }

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


1489
static int
1490 1491 1492 1493 1494 1495 1496 1497
xenUnifiedDomainMigratePrepare(virConnectPtr dconn,
                               char **cookie,
                               int *cookielen,
                               const char *uri_in,
                               char **uri_out,
                               unsigned long flags,
                               const char *dname,
                               unsigned long resource)
1498
{
1499
    xenUnifiedPrivatePtr priv = dconn->privateData;
1500

E
Eric Blake 已提交
1501 1502
    virCheckFlags(XEN_MIGRATION_FLAGS, -1);

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

1508
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1509 1510 1511 1512
    return -1;
}

static int
1513 1514 1515 1516 1517 1518 1519
xenUnifiedDomainMigratePerform(virDomainPtr dom,
                               const char *cookie,
                               int cookielen,
                               const char *uri,
                               unsigned long flags,
                               const char *dname,
                               unsigned long resource)
1520
{
1521
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1522

E
Eric Blake 已提交
1523 1524
    virCheckFlags(XEN_MIGRATION_FLAGS, -1);

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

1529
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1530 1531 1532 1533
    return -1;
}

static virDomainPtr
1534 1535 1536 1537 1538 1539
xenUnifiedDomainMigrateFinish(virConnectPtr dconn,
                              const char *dname,
                              const char *cookie ATTRIBUTE_UNUSED,
                              int cookielen ATTRIBUTE_UNUSED,
                              const char *uri ATTRIBUTE_UNUSED,
                              unsigned long flags)
1540
{
1541 1542 1543 1544
    virDomainPtr dom = NULL;
    char *domain_xml = NULL;
    virDomainPtr dom_new = NULL;

E
Eric Blake 已提交
1545 1546
    virCheckFlags(XEN_MIGRATION_FLAGS, NULL);

1547
    dom = xenUnifiedDomainLookupByName(dconn, dname);
1548 1549 1550 1551 1552
    if (! dom) {
        return NULL;
    }

    if (flags & VIR_MIGRATE_PERSIST_DEST) {
1553
        domain_xml = xenDaemonDomainGetXMLDesc(dom, 0, NULL);
1554
        if (! domain_xml) {
1555 1556
            virReportError(VIR_ERR_MIGRATE_PERSIST_FAILED,
                           "%s", _("failed to get XML representation of migrated domain"));
1557 1558 1559
            goto failure;
        }

1560
        dom_new = xenDaemonDomainDefineXML(dconn, domain_xml);
1561
        if (! dom_new) {
1562 1563
            virReportError(VIR_ERR_MIGRATE_PERSIST_FAILED,
                           "%s", _("failed to define domain on destination host"));
1564 1565 1566 1567
            goto failure;
        }

        /* Free additional reference added by Define */
1568
        virDomainFree(dom_new);
1569 1570
    }

1571
    VIR_FREE(domain_xml);
1572 1573 1574 1575 1576

    return dom;


failure:
1577
    virDomainFree(dom);
1578

1579
    VIR_FREE(domain_xml);
1580 1581

    return NULL;
1582 1583
}

1584
static int
1585 1586
xenUnifiedConnectListDefinedDomains(virConnectPtr conn, char **const names,
                                    int maxnames)
1587
{
1588
    xenUnifiedPrivatePtr priv = conn->privateData;
1589 1590
    int i;
    int ret;
1591

1592
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1593
        if (priv->opened[i] && drivers[i]->xenListDefinedDomains) {
1594
            ret = drivers[i]->xenListDefinedDomains(conn, names, maxnames);
1595 1596
            if (ret >= 0) return ret;
        }
1597

1598
    return -1;
1599 1600 1601
}

static int
1602
xenUnifiedConnectNumOfDefinedDomains(virConnectPtr conn)
1603
{
1604
    xenUnifiedPrivatePtr priv = conn->privateData;
1605 1606
    int i;
    int ret;
1607

1608
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1609
        if (priv->opened[i] && drivers[i]->xenNumOfDefinedDomains) {
1610
            ret = drivers[i]->xenNumOfDefinedDomains(conn);
1611 1612
            if (ret >= 0) return ret;
        }
1613

1614
    return -1;
1615 1616 1617
}

static int
1618
xenUnifiedDomainCreateWithFlags(virDomainPtr dom, unsigned int flags)
1619
{
1620
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1621
    int i;
1622 1623
    int ret = -1;
    char *name = NULL;
1624

1625 1626
    virCheckFlags(0, -1);

1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640
    name = xenUnifiedDomainManagedSavePath(priv, dom);
    if (!name)
        goto cleanup;

    if (virFileExists(name)) {
        if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
            ret = xenDaemonDomainRestore(dom->conn, name);
            if (ret == 0)
                unlink(name);
        }
        goto cleanup;
    }

    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i) {
E
Eric Blake 已提交
1641
        if (priv->opened[i] && drivers[i]->xenDomainCreate &&
1642 1643 1644 1645 1646
            drivers[i]->xenDomainCreate(dom) == 0) {
            ret = 0;
            goto cleanup;
        }
    }
1647

1648 1649 1650
cleanup:
    VIR_FREE(name);
    return ret;
1651 1652
}

1653
static int
1654
xenUnifiedDomainCreate(virDomainPtr dom)
1655 1656 1657 1658
{
    return xenUnifiedDomainCreateWithFlags(dom, 0);
}

1659
static virDomainPtr
1660
xenUnifiedDomainDefineXML(virConnectPtr conn, const char *xml)
1661
{
1662
    xenUnifiedPrivatePtr priv = conn->privateData;
1663 1664
    int i;
    virDomainPtr ret;
1665

1666
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1667
        if (priv->opened[i] && drivers[i]->xenDomainDefineXML) {
1668
            ret = drivers[i]->xenDomainDefineXML(conn, xml);
1669 1670
            if (ret) return ret;
        }
1671

1672
    return NULL;
1673 1674 1675
}

static int
1676
xenUnifiedDomainUndefineFlags(virDomainPtr dom, unsigned int flags)
1677
{
1678
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1679
    int i;
1680

1681
    virCheckFlags(0, -1);
1682
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1683
        if (priv->opened[i] && drivers[i]->xenDomainUndefine &&
1684
            drivers[i]->xenDomainUndefine(dom) == 0)
1685
            return 0;
1686

1687
    return -1;
1688 1689
}

1690
static int
1691
xenUnifiedDomainUndefine(virDomainPtr dom) {
1692 1693 1694
    return xenUnifiedDomainUndefineFlags(dom, 0);
}

1695
static int
1696
xenUnifiedDomainAttachDevice(virDomainPtr dom, const char *xml)
1697
{
1698
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1699
    int i;
1700
    unsigned int flags = VIR_DOMAIN_DEVICE_MODIFY_LIVE;
1701

1702 1703 1704 1705 1706 1707
    /*
     * 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] &&
1708
        priv->xendConfigVersion >= XEND_CONFIG_VERSION_3_0_4)
1709
        flags |= VIR_DOMAIN_DEVICE_MODIFY_CONFIG;
1710 1711

    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1712 1713
        if (priv->opened[i] && drivers[i]->xenDomainAttachDeviceFlags &&
            drivers[i]->xenDomainAttachDeviceFlags(dom, xml, flags) == 0)
1714 1715 1716 1717 1718 1719
            return 0;

    return -1;
}

static int
1720 1721
xenUnifiedDomainAttachDeviceFlags(virDomainPtr dom, const char *xml,
                                  unsigned int flags)
1722
{
1723
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1724
    int i;
1725

1726
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1727 1728
        if (priv->opened[i] && drivers[i]->xenDomainAttachDeviceFlags &&
            drivers[i]->xenDomainAttachDeviceFlags(dom, xml, flags) == 0)
1729
            return 0;
1730

1731
    return -1;
1732 1733 1734
}

static int
1735
xenUnifiedDomainDetachDevice(virDomainPtr dom, const char *xml)
1736
{
1737
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1738
    int i;
1739
    unsigned int flags = VIR_DOMAIN_DEVICE_MODIFY_LIVE;
1740

1741 1742 1743 1744 1745 1746
    /*
     * 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] &&
1747
        priv->xendConfigVersion >= XEND_CONFIG_VERSION_3_0_4)
1748
        flags |= VIR_DOMAIN_DEVICE_MODIFY_CONFIG;
1749 1750

    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1751 1752
        if (priv->opened[i] && drivers[i]->xenDomainDetachDeviceFlags &&
            drivers[i]->xenDomainDetachDeviceFlags(dom, xml, flags) == 0)
1753 1754 1755 1756 1757 1758
            return 0;

    return -1;
}

static int
1759 1760
xenUnifiedDomainDetachDeviceFlags(virDomainPtr dom, const char *xml,
                                  unsigned int flags)
1761
{
1762
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1763
    int i;
1764

1765
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
E
Eric Blake 已提交
1766 1767
        if (priv->opened[i] && drivers[i]->xenDomainDetachDeviceFlags &&
            drivers[i]->xenDomainDetachDeviceFlags(dom, xml, flags) == 0)
1768
            return 0;
1769

1770
    return -1;
1771 1772
}

1773
static int
1774 1775
xenUnifiedDomainUpdateDeviceFlags(virDomainPtr dom, const char *xml,
                                  unsigned int flags)
1776
{
1777
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1778

1779 1780
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
        return xenDaemonUpdateDeviceFlags(dom, xml, flags);
1781 1782 1783
    return -1;
}

1784
static int
1785
xenUnifiedDomainGetAutostart(virDomainPtr dom, int *autostart)
1786
{
1787
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1788

1789
    if (priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4) {
1790 1791 1792 1793 1794 1795
        if (priv->opened[XEN_UNIFIED_XM_OFFSET])
            return xenXMDomainGetAutostart(dom, autostart);
    } else {
        if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
            return xenDaemonDomainGetAutostart(dom, autostart);
    }
1796

1797
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1798 1799 1800 1801
    return -1;
}

static int
1802
xenUnifiedDomainSetAutostart(virDomainPtr dom, int autostart)
1803
{
1804
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1805

1806
    if (priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4) {
1807 1808 1809 1810 1811 1812
        if (priv->opened[XEN_UNIFIED_XM_OFFSET])
            return xenXMDomainSetAutostart(dom, autostart);
    } else {
        if (priv->opened[XEN_UNIFIED_XEND_OFFSET])
            return xenDaemonDomainSetAutostart(dom, autostart);
    }
1813

1814
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1815 1816 1817
    return -1;
}

1818
static char *
1819
xenUnifiedDomainGetSchedulerType(virDomainPtr dom, int *nparams)
1820
{
1821
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1822 1823 1824 1825
    int i;
    char *schedulertype;

    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; i++) {
E
Eric Blake 已提交
1826
        if (priv->opened[i] && drivers[i]->xenDomainGetSchedulerType) {
1827
            schedulertype = drivers[i]->xenDomainGetSchedulerType(dom, nparams);
1828
            if (schedulertype != NULL)
1829
                return schedulertype;
1830 1831
        }
    }
1832
    return NULL;
1833 1834 1835
}

static int
1836 1837 1838 1839
xenUnifiedDomainGetSchedulerParametersFlags(virDomainPtr dom,
                                            virTypedParameterPtr params,
                                            int *nparams,
                                            unsigned int flags)
1840
{
1841
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1842 1843
    int i, ret;

1844 1845
    virCheckFlags(0, -1);

1846
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i) {
E
Eric Blake 已提交
1847 1848
        if (priv->opened[i] && drivers[i]->xenDomainGetSchedulerParameters) {
           ret = drivers[i]->xenDomainGetSchedulerParameters(dom, params, nparams);
1849
           if (ret == 0)
1850
               return 0;
1851
        }
1852
    }
1853
    return -1;
1854 1855 1856
}

static int
1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869
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)
1870
{
1871
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1872 1873
    int i, ret;

1874 1875
    virCheckFlags(0, -1);

1876 1877
    /* do the hypervisor call last to get better error */
    for (i = XEN_UNIFIED_NR_DRIVERS - 1; i >= 0; i--) {
E
Eric Blake 已提交
1878 1879
        if (priv->opened[i] && drivers[i]->xenDomainSetSchedulerParameters) {
           ret = drivers[i]->xenDomainSetSchedulerParameters(dom, params, nparams);
1880 1881 1882
           if (ret == 0)
               return 0;
        }
1883 1884
    }

1885
    return -1;
1886 1887
}

1888 1889 1890 1891 1892 1893 1894 1895 1896
static int
xenUnifiedDomainSetSchedulerParameters(virDomainPtr dom,
                                       virTypedParameterPtr params,
                                       int nparams)
{
    return xenUnifiedDomainSetSchedulerParametersFlags(dom, params,
                                                       nparams, 0);
}

1897
static int
1898 1899
xenUnifiedDomainBlockStats(virDomainPtr dom, const char *path,
                           struct _virDomainBlockStats *stats)
1900
{
1901
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1902 1903

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

1906
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1907 1908 1909 1910
    return -1;
}

static int
1911 1912
xenUnifiedDomainInterfaceStats(virDomainPtr dom, const char *path,
                               struct _virDomainInterfaceStats *stats)
1913
{
1914
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1915 1916

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

1919
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1920 1921 1922
    return -1;
}

R
Richard W.M. Jones 已提交
1923
static int
1924 1925 1926
xenUnifiedDomainBlockPeek(virDomainPtr dom, const char *path,
                          unsigned long long offset, size_t size,
                          void *buffer, unsigned int flags)
R
Richard W.M. Jones 已提交
1927 1928
{
    int r;
1929
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
R
Richard W.M. Jones 已提交
1930

E
Eric Blake 已提交
1931 1932
    virCheckFlags(0, -1);

R
Richard W.M. Jones 已提交
1933
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
1934
        r = xenDaemonDomainBlockPeek(dom, path, offset, size, buffer);
R
Richard W.M. Jones 已提交
1935 1936 1937 1938 1939
        if (r != -2) return r;
        /* r == -2 means declined, so fall through to XM driver ... */
    }

    if (priv->opened[XEN_UNIFIED_XM_OFFSET]) {
1940
        if (xenXMDomainBlockPeek(dom, path, offset, size, buffer) == 0)
R
Richard W.M. Jones 已提交
1941 1942 1943
            return 0;
    }

1944
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
R
Richard W.M. Jones 已提交
1945 1946 1947
    return -1;
}

1948
static int
1949 1950
xenUnifiedNodeGetCellsFreeMemory(virConnectPtr conn, unsigned long long *freeMems,
                                 int startCell, int maxCells)
1951
{
1952
    xenUnifiedPrivatePtr priv = conn->privateData;
1953 1954

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

1958
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1959 1960 1961
    return -1;
}

1962
static unsigned long long
1963
xenUnifiedNodeGetFreeMemory(virConnectPtr conn)
1964 1965 1966
{
    unsigned long long freeMem = 0;
    int ret;
1967
    xenUnifiedPrivatePtr priv = conn->privateData;
1968 1969

    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET]) {
1970 1971
        ret = xenHypervisorNodeGetCellsFreeMemory(conn, &freeMem,
                                                  -1, 1);
1972
        if (ret != 1)
1973 1974
            return 0;
        return freeMem;
1975 1976
    }

1977
    virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1978
    return 0;
1979 1980
}

1981

1982
static int
1983 1984 1985 1986
xenUnifiedConnectDomainEventRegister(virConnectPtr conn,
                                     virConnectDomainEventCallback callback,
                                     void *opaque,
                                     virFreeCallback freefunc)
1987
{
1988
    xenUnifiedPrivatePtr priv = conn->privateData;
D
Daniel P. Berrange 已提交
1989

1990
    int ret;
D
Daniel P. Berrange 已提交
1991
    xenUnifiedLock(priv);
1992

1993
    if (priv->xsWatch == -1) {
1994
        virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
D
Daniel P. Berrange 已提交
1995
        xenUnifiedUnlock(priv);
1996 1997 1998
        return -1;
    }

1999 2000
    ret = virDomainEventStateRegister(conn, priv->domainEvents,
                                      callback, opaque, freefunc);
2001

D
Daniel P. Berrange 已提交
2002
    xenUnifiedUnlock(priv);
2003
    return ret;
2004 2005
}

2006

2007
static int
2008 2009
xenUnifiedConnectDomainEventDeregister(virConnectPtr conn,
                                       virConnectDomainEventCallback callback)
2010 2011
{
    int ret;
2012
    xenUnifiedPrivatePtr priv = conn->privateData;
D
Daniel P. Berrange 已提交
2013 2014
    xenUnifiedLock(priv);

2015
    if (priv->xsWatch == -1) {
2016
        virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
D
Daniel P. Berrange 已提交
2017
        xenUnifiedUnlock(priv);
2018 2019 2020
        return -1;
    }

2021 2022 2023
    ret = virDomainEventStateDeregister(conn,
                                        priv->domainEvents,
                                        callback);
2024

D
Daniel P. Berrange 已提交
2025
    xenUnifiedUnlock(priv);
2026 2027 2028
    return ret;
}

2029

2030
static int
2031 2032 2033 2034 2035 2036
xenUnifiedConnectDomainEventRegisterAny(virConnectPtr conn,
                                        virDomainPtr dom,
                                        int eventID,
                                        virConnectDomainEventGenericCallback callback,
                                        void *opaque,
                                        virFreeCallback freefunc)
2037
{
2038
    xenUnifiedPrivatePtr priv = conn->privateData;
2039 2040 2041 2042 2043

    int ret;
    xenUnifiedLock(priv);

    if (priv->xsWatch == -1) {
2044
        virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
2045 2046 2047 2048
        xenUnifiedUnlock(priv);
        return -1;
    }

2049 2050 2051
    if (virDomainEventStateRegisterID(conn, priv->domainEvents,
                                      dom, eventID,
                                      callback, opaque, freefunc, &ret) < 0)
2052
        ret = -1;
2053 2054

    xenUnifiedUnlock(priv);
2055
    return ret;
2056 2057 2058
}

static int
2059 2060
xenUnifiedConnectDomainEventDeregisterAny(virConnectPtr conn,
                                          int callbackID)
2061 2062
{
    int ret;
2063
    xenUnifiedPrivatePtr priv = conn->privateData;
2064 2065 2066
    xenUnifiedLock(priv);

    if (priv->xsWatch == -1) {
2067
        virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
2068 2069 2070 2071
        xenUnifiedUnlock(priv);
        return -1;
    }

2072 2073 2074
    ret = virDomainEventStateDeregisterID(conn,
                                          priv->domainEvents,
                                          callbackID);
2075 2076 2077 2078 2079 2080

    xenUnifiedUnlock(priv);
    return ret;
}


2081
static int
2082 2083 2084 2085 2086
xenUnifiedNodeDeviceGetPciInfo(virNodeDevicePtr dev,
                               unsigned *domain,
                               unsigned *bus,
                               unsigned *slot,
                               unsigned *function)
2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
{
    virNodeDeviceDefPtr def = NULL;
    virNodeDevCapsDefPtr cap;
    char *xml = NULL;
    int ret = -1;

    xml = virNodeDeviceGetXMLDesc(dev, 0);
    if (!xml)
        goto out;

2097
    def = virNodeDeviceDefParseString(xml, EXISTING_DEVICE, NULL);
2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114
    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) {
2115 2116
        virReportError(VIR_ERR_INVALID_ARG,
                       _("device %s is not a PCI device"), dev->name);
2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127
        goto out;
    }

    ret = 0;
out:
    virNodeDeviceDefFree(def);
    VIR_FREE(xml);
    return ret;
}

static int
2128 2129 2130
xenUnifiedNodeDeviceDetachFlags(virNodeDevicePtr dev,
                                const char *driverName,
                                unsigned int flags)
2131
{
2132
    virPCIDevicePtr pci;
2133 2134 2135
    unsigned domain, bus, slot, function;
    int ret = -1;

2136 2137
    virCheckFlags(0, -1);

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

2141
    pci = virPCIDeviceNew(domain, bus, slot, function);
2142 2143 2144
    if (!pci)
        return -1;

2145 2146 2147 2148 2149 2150 2151 2152 2153
    if (!driverName) {
        virPCIDeviceSetStubDriver(pci, "pciback");
    } else {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unknown driver name '%s'"), driverName);
        goto out;
    }

    if (virPCIDeviceDetach(pci, NULL, NULL, NULL) < 0)
2154 2155 2156 2157
        goto out;

    ret = 0;
out:
2158
    virPCIDeviceFree(pci);
2159 2160 2161
    return ret;
}

2162 2163 2164 2165 2166 2167
static int
xenUnifiedNodeDeviceDettach(virNodeDevicePtr dev)
{
    return xenUnifiedNodeDeviceDetachFlags(dev, NULL, 0);
}

2168
static int
2169
xenUnifiedNodeDeviceAssignedDomainId(virNodeDevicePtr dev)
2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180
{
    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 */
2181
    numdomains = xenUnifiedConnectNumOfDomains(conn);
2182 2183 2184 2185 2186 2187 2188 2189
    if (numdomains < 0) {
        return ret;
    }
    if (numdomains > 0){
        if (VIR_ALLOC_N(ids, numdomains) < 0) {
            virReportOOMError();
            goto out;
        }
2190
        if ((numdomains = xenUnifiedConnectListDomains(conn, &ids[0], numdomains)) < 0) {
2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206
            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 */
2207
    for (i = 0; i < numdomains; i++) {
2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225
        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;
}

2226
static int
2227
xenUnifiedNodeDeviceReAttach(virNodeDevicePtr dev)
2228
{
2229
    virPCIDevicePtr pci;
2230 2231
    unsigned domain, bus, slot, function;
    int ret = -1;
2232
    int domid;
2233 2234 2235 2236

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

2237
    pci = virPCIDeviceNew(domain, bus, slot, function);
2238 2239 2240
    if (!pci)
        return -1;

2241 2242
    /* Check if device is assigned to an active guest */
    if ((domid = xenUnifiedNodeDeviceAssignedDomainId(dev)) >= 0) {
2243 2244 2245
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Device %s has been assigned to guest %d"),
                       dev->name, domid);
2246 2247 2248
        goto out;
    }

2249
    if (virPCIDeviceReattach(pci, NULL, NULL) < 0)
2250 2251 2252 2253
        goto out;

    ret = 0;
out:
2254
    virPCIDeviceFree(pci);
2255 2256 2257 2258
    return ret;
}

static int
2259
xenUnifiedNodeDeviceReset(virNodeDevicePtr dev)
2260
{
2261
    virPCIDevicePtr pci;
2262 2263 2264 2265 2266 2267
    unsigned domain, bus, slot, function;
    int ret = -1;

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

2268
    pci = virPCIDeviceNew(domain, bus, slot, function);
2269 2270 2271
    if (!pci)
        return -1;

2272
    if (virPCIDeviceReset(pci, NULL, NULL) < 0)
2273 2274 2275 2276
        goto out;

    ret = 0;
out:
2277
    virPCIDeviceFree(pci);
2278 2279 2280 2281
    return ret;
}


2282 2283
static int
xenUnifiedDomainOpenConsole(virDomainPtr dom,
2284
                            const char *dev_name,
2285 2286 2287 2288 2289 2290 2291 2292 2293 2294
                            virStreamPtr st,
                            unsigned int flags)
{
    virDomainDefPtr def = NULL;
    int ret = -1;
    virDomainChrDefPtr chr = NULL;

    virCheckFlags(0, -1);

    if (dom->id == -1) {
2295 2296
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("domain is not running"));
2297 2298 2299
        goto cleanup;
    }

2300
    if (dev_name) {
2301
        /* XXX support device aliases in future */
2302 2303
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Named device aliases are not supported"));
2304 2305 2306 2307 2308 2309 2310
        goto cleanup;
    }

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

2311 2312
    if (def->nconsoles)
        chr = def->consoles[0];
2313 2314 2315 2316
    else if (def->nserials)
        chr = def->serials[0];

    if (!chr) {
2317 2318
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot find default console device"));
2319 2320 2321
        goto cleanup;
    }

2322
    if (chr->source.type != VIR_DOMAIN_CHR_TYPE_PTY) {
2323 2324
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("character device %s is not using a PTY"), dev_name);
2325 2326 2327
        goto cleanup;
    }

2328
    if (virFDStreamOpenFile(st, chr->source.data.file.path,
E
Eric Blake 已提交
2329
                            0, 0, O_RDWR) < 0)
2330 2331 2332 2333 2334 2335 2336
        goto cleanup;

    ret = 0;
cleanup:
    virDomainDefFree(def);
    return ret;
}
2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356

static int
xenUnifiedNodeGetMemoryParameters(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  virTypedParameterPtr params,
                                  int *nparams,
                                  unsigned int flags)
{
    return nodeGetMemoryParameters(params, nparams, flags);
}


static int
xenUnifiedNodeSetMemoryParameters(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  virTypedParameterPtr params,
                                  int nparams,
                                  unsigned int flags)
{
    return nodeSetMemoryParameters(params, nparams, flags);
}

2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367

static int
xenUnifiedNodeSuspendForDuration(virConnectPtr conn ATTRIBUTE_UNUSED,
                                 unsigned int target,
                                 unsigned long long duration,
                                 unsigned int flags)
{
    return nodeSuspendForDuration(target, duration, flags);
}


E
Eric Blake 已提交
2368
/*----- Register with libvirt.c, and initialize Xen drivers. -----*/
2369 2370 2371

/* The interface which we export upwards to libvirt.c. */
static virDriver xenUnifiedDriver = {
2372 2373
    .no = VIR_DRV_XEN_UNIFIED,
    .name = "Xen",
2374 2375 2376 2377 2378
    .connectOpen = xenUnifiedConnectOpen, /* 0.0.3 */
    .connectClose = xenUnifiedConnectClose, /* 0.0.3 */
    .connectSupportsFeature = xenUnifiedConnectSupportsFeature, /* 0.3.2 */
    .connectGetType = xenUnifiedConnectGetType, /* 0.0.3 */
    .connectGetVersion = xenUnifiedConnectGetVersion, /* 0.0.3 */
2379
    .connectGetHostname = xenUnifiedConnectGetHostname, /* 0.7.3 */
2380
    .connectGetMaxVcpus = xenUnifiedConnectGetMaxVcpus, /* 0.2.1 */
2381
    .nodeGetInfo = xenUnifiedNodeGetInfo, /* 0.1.0 */
2382 2383 2384
    .connectGetCapabilities = xenUnifiedConnectGetCapabilities, /* 0.2.1 */
    .connectListDomains = xenUnifiedConnectListDomains, /* 0.0.3 */
    .connectNumOfDomains = xenUnifiedConnectNumOfDomains, /* 0.0.3 */
2385 2386 2387 2388 2389 2390 2391
    .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 */
2392
    .domainShutdownFlags = xenUnifiedDomainShutdownFlags, /* 0.9.10 */
2393 2394
    .domainReboot = xenUnifiedDomainReboot, /* 0.1.0 */
    .domainDestroy = xenUnifiedDomainDestroy, /* 0.0.3 */
2395
    .domainDestroyFlags = xenUnifiedDomainDestroyFlags, /* 0.9.4 */
2396 2397 2398 2399 2400 2401 2402
    .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 */
2403
    .domainSaveFlags = xenUnifiedDomainSaveFlags, /* 0.9.4 */
2404 2405 2406
    .domainManagedSave = xenUnifiedDomainManagedSave, /* 1.0.1 */
    .domainHasManagedSaveImage = xenUnifiedDomainHasManagedSaveImage, /* 1.0.1 */
    .domainManagedSaveRemove = xenUnifiedDomainManagedSaveRemove, /* 1.0.1 */
2407
    .domainRestore = xenUnifiedDomainRestore, /* 0.0.3 */
2408
    .domainRestoreFlags = xenUnifiedDomainRestoreFlags, /* 0.9.4 */
2409 2410 2411 2412 2413 2414 2415 2416
    .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 */
2417 2418 2419 2420
    .connectDomainXMLFromNative = xenUnifiedConnectDomainXMLFromNative, /* 0.6.4 */
    .connectDomainXMLToNative = xenUnifiedConnectDomainXMLToNative, /* 0.6.4 */
    .connectListDefinedDomains = xenUnifiedConnectListDefinedDomains, /* 0.1.1 */
    .connectNumOfDefinedDomains = xenUnifiedConnectNumOfDefinedDomains, /* 0.1.5 */
2421 2422 2423 2424
    .domainCreate = xenUnifiedDomainCreate, /* 0.1.1 */
    .domainCreateWithFlags = xenUnifiedDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = xenUnifiedDomainDefineXML, /* 0.1.1 */
    .domainUndefine = xenUnifiedDomainUndefine, /* 0.1.1 */
2425
    .domainUndefineFlags = xenUnifiedDomainUndefineFlags, /* 0.9.4 */
2426 2427 2428 2429 2430 2431 2432 2433 2434
    .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 */
2435
    .domainGetSchedulerParametersFlags = xenUnifiedDomainGetSchedulerParametersFlags, /* 0.9.2 */
2436
    .domainSetSchedulerParameters = xenUnifiedDomainSetSchedulerParameters, /* 0.2.3 */
2437
    .domainSetSchedulerParametersFlags = xenUnifiedDomainSetSchedulerParametersFlags, /* 0.9.2 */
2438 2439 2440 2441 2442 2443 2444 2445
    .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 */
2446 2447
    .connectDomainEventRegister = xenUnifiedConnectDomainEventRegister, /* 0.5.0 */
    .connectDomainEventDeregister = xenUnifiedConnectDomainEventDeregister, /* 0.5.0 */
2448
    .nodeDeviceDettach = xenUnifiedNodeDeviceDettach, /* 0.6.1 */
2449
    .nodeDeviceDetachFlags = xenUnifiedNodeDeviceDetachFlags, /* 1.0.5 */
2450 2451
    .nodeDeviceReAttach = xenUnifiedNodeDeviceReAttach, /* 0.6.1 */
    .nodeDeviceReset = xenUnifiedNodeDeviceReset, /* 0.6.1 */
2452 2453
    .connectIsEncrypted = xenUnifiedConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = xenUnifiedConnectIsSecure, /* 0.7.3 */
2454 2455 2456
    .domainIsActive = xenUnifiedDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = xenUnifiedDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = xenUnifiedDomainIsUpdated, /* 0.8.6 */
2457 2458
    .connectDomainEventRegisterAny = xenUnifiedConnectDomainEventRegisterAny, /* 0.8.0 */
    .connectDomainEventDeregisterAny = xenUnifiedConnectDomainEventDeregisterAny, /* 0.8.0 */
2459
    .domainOpenConsole = xenUnifiedDomainOpenConsole, /* 0.8.6 */
2460
    .connectIsAlive = xenUnifiedConnectIsAlive, /* 0.9.8 */
2461
    .nodeSuspendForDuration = xenUnifiedNodeSuspendForDuration, /* 0.9.8 */
2462 2463
    .nodeGetMemoryParameters = xenUnifiedNodeGetMemoryParameters, /* 0.10.2 */
    .nodeSetMemoryParameters = xenUnifiedNodeSetMemoryParameters, /* 0.10.2 */
2464 2465
};

2466
/**
2467
 * xenRegister:
2468 2469 2470 2471 2472
 *
 * Register xen related drivers
 *
 * Returns the driver priority or -1 in case of error.
 */
2473
int
2474
xenRegister(void)
2475
{
2476
    if (virRegisterStateDriver(&state_driver) == -1) return -1;
J
John Levon 已提交
2477

2478
    return virRegisterDriver(&xenUnifiedDriver);
2479 2480
}

2481 2482 2483 2484 2485 2486 2487 2488 2489
/**
 * xenUnifiedDomainInfoListFree:
 *
 * Free the Domain Info List
 */
void
xenUnifiedDomainInfoListFree(xenUnifiedDomainInfoListPtr list)
{
    int i;
J
John Levon 已提交
2490 2491 2492 2493

    if (list == NULL)
        return;

2494 2495 2496 2497
    for (i=0; i<list->count; i++) {
        VIR_FREE(list->doms[i]->name);
        VIR_FREE(list->doms[i]);
    }
2498
    VIR_FREE(list->doms);
2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520
    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)) {
2521
            VIR_DEBUG("WARNING: dom already tracked");
2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543
            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:
2544
    virReportOOMError();
2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564
    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++) {
2565
        if (list->doms[i]->id == id &&
2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589
            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;
}

2590

2591 2592
/**
 * xenUnifiedDomainEventDispatch:
D
Daniel P. Berrange 已提交
2593 2594
 * @priv: the connection to dispatch events on
 * @event: the event to dispatch
2595 2596 2597
 *
 * Dispatch domain events to registered callbacks
 *
D
Daniel P. Berrange 已提交
2598 2599
 * The caller must hold the lock in 'priv' before invoking
 *
2600
 */
2601
void xenUnifiedDomainEventDispatch(xenUnifiedPrivatePtr priv,
2602
                                    virDomainEventPtr event)
2603
{
D
Daniel P. Berrange 已提交
2604
    if (!priv)
2605
        return;
2606

2607
    virDomainEventStateQueue(priv->domainEvents, event);
D
Daniel P. Berrange 已提交
2608 2609 2610 2611 2612 2613 2614 2615 2616 2617
}

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

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