xen_driver.c 65.3 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
static bool is_privileged = false;
J
John Levon 已提交
84

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
static virDomainDefPtr xenGetDomainDefForID(virConnectPtr conn, int id)
{
    virDomainDefPtr ret;

    ret = xenHypervisorLookupDomainByID(conn, id);

    if (!ret && virGetLastError() == NULL)
        virReportError(VIR_ERR_NO_DOMAIN, __FUNCTION__);

    return ret;
}


static virDomainDefPtr xenGetDomainDefForName(virConnectPtr conn, const char *name)
{
    xenUnifiedPrivatePtr priv = conn->privateData;
    virDomainDefPtr ret;

    ret = xenDaemonLookupByName(conn, name);

    /* Try XM for inactive domains. */
    if (!ret &&
        priv->xendConfigVersion <= XEND_CONFIG_VERSION_3_0_3)
        ret = xenXMDomainLookupByName(conn, name);

    if (!ret && virGetLastError() == NULL)
        virReportError(VIR_ERR_NO_DOMAIN, __FUNCTION__);

    return ret;
}


static virDomainDefPtr xenGetDomainDefForUUID(virConnectPtr conn, const unsigned char *uuid)
{
    xenUnifiedPrivatePtr priv = conn->privateData;
    virDomainDefPtr ret;

    ret = xenHypervisorLookupDomainByUUID(conn, uuid);

    /* Try XM for inactive domains. */
    if (!ret) {
        if (priv->xendConfigVersion <= XEND_CONFIG_VERSION_3_0_3)
            ret = xenXMDomainLookupByUUID(conn, uuid);
        else
            ret = xenDaemonLookupByUUID(conn, uuid);
    }

    if (!ret && virGetLastError() == NULL)
        virReportError(VIR_ERR_NO_DOMAIN, __FUNCTION__);

    return ret;
}


139 140 141 142 143 144 145
static virDomainDefPtr xenGetDomainDefForDom(virDomainPtr dom)
{
    /* UUID lookup is more efficient than name lookup */
    return xenGetDomainDefForUUID(dom->conn, dom->uuid);
}


146 147 148 149 150
/**
 * xenNumaInit:
 * @conn: pointer to the hypervisor connection
 *
 * Initializer for previous variables. We currently assume that
R
Richard W.M. Jones 已提交
151
 * the number of physical CPU and the number of NUMA cell is fixed
152 153 154 155 156
 * until reboot which might be false in future Xen implementations.
 */
static void
xenNumaInit(virConnectPtr conn) {
    virNodeInfo nodeInfo;
D
Daniel P. Berrange 已提交
157
    xenUnifiedPrivatePtr priv;
158 159 160 161 162 163
    int ret;

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

D
Daniel P. Berrange 已提交
164
    priv = conn->privateData;
165

D
Daniel P. Berrange 已提交
166 167
    priv->nbNodeCells = nodeInfo.nodes;
    priv->nbNodeCpus = nodeInfo.cpus;
168 169
}

D
Daniel P. Berrange 已提交
170

171 172 173 174 175 176 177 178 179 180 181 182 183 184
/**
 * 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 已提交
185
    int ncpus;
186
    int nb_vcpu;
187
    virBitmapPtr cpulist = NULL;
188 189 190 191 192 193
    unsigned char *cpumap = NULL;
    size_t cpumaplen;
    int nb = 0;
    int n, m;
    virVcpuInfoPtr cpuinfo = NULL;
    virNodeInfo nodeinfo;
D
Daniel P. Berrange 已提交
194
    xenUnifiedPrivatePtr priv;
195 196

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

D
Daniel P. Berrange 已提交
199 200 201
    priv = dom->conn->privateData;

    if (priv->nbNodeCpus <= 0)
202
        return NULL;
203 204
    nb_vcpu = xenUnifiedDomainGetMaxVcpus(dom);
    if (nb_vcpu <= 0)
205
        return NULL;
206
    if (xenUnifiedNodeGetInfo(dom->conn, &nodeinfo) < 0)
207
        return NULL;
208

209
    if (!(cpulist = virBitmapNew(priv->nbNodeCpus))) {
210
        virReportOOMError();
211
        goto done;
212 213
    }
    if (VIR_ALLOC_N(cpuinfo, nb_vcpu) < 0) {
214
        virReportOOMError();
215
        goto done;
216
    }
217
    cpumaplen = VIR_CPU_MAPLEN(VIR_NODEINFO_MAXCPUS(nodeinfo));
218
    if (xalloc_oversized(nb_vcpu, cpumaplen) ||
219
        VIR_ALLOC_N(cpumap, nb_vcpu * cpumaplen) < 0) {
220
        virReportOOMError();
221
        goto done;
222
    }
223 224 225

    if ((ncpus = xenUnifiedDomainGetVcpus(dom, cpuinfo, nb_vcpu,
                                          cpumap, cpumaplen)) >= 0) {
226
        for (n = 0 ; n < ncpus ; n++) {
D
Daniel P. Berrange 已提交
227
            for (m = 0 ; m < priv->nbNodeCpus; m++) {
228 229 230
                bool used;
                ignore_value(virBitmapGetBit(cpulist, m, &used));
                if ((!used) &&
231
                    (VIR_CPU_USABLE(cpumap, cpumaplen, n, m))) {
232
                    ignore_value(virBitmapSetBit(cpulist, m));
233 234
                    nb++;
                    /* if all CPU are used just return NULL */
D
Daniel P. Berrange 已提交
235
                    if (nb == priv->nbNodeCpus)
236 237 238 239 240
                        goto done;

                }
            }
        }
241
        res = virBitmapFormat(cpulist);
242 243 244
    }

done:
245
    virBitmapFree(cpulist);
246 247
    VIR_FREE(cpumap);
    VIR_FREE(cpuinfo);
248
    return res;
249 250
}

J
John Levon 已提交
251
static int
252
xenUnifiedStateInitialize(bool privileged,
253 254
                          virStateInhibitCallback callback ATTRIBUTE_UNUSED,
                          void *opaque ATTRIBUTE_UNUSED)
J
John Levon 已提交
255
{
256 257 258
    /* Don't allow driver to work in non-root libvirtd */
    if (privileged)
        is_privileged = true;
J
John Levon 已提交
259 260 261 262
    return 0;
}

static virStateDriver state_driver = {
263
    .name = "Xen",
264
    .stateInitialize = xenUnifiedStateInitialize,
J
John Levon 已提交
265 266
};

267 268 269 270 271 272 273 274 275 276 277
/*----- 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.
 */

278
static int
279
xenUnifiedProbe(void)
280 281 282
{
#ifdef __linux__
    if (virFileExists("/proc/xen"))
283
        return 1;
284
#endif
285
#ifdef __sun
286
    int fd;
287

288 289
    if ((fd = open("/dev/xen/domcaps", O_RDONLY)) >= 0) {
        VIR_FORCE_CLOSE(fd);
290
        return 1;
291 292
    }
#endif
293
    return 0;
294 295
}

J
Jim Fehlig 已提交
296 297
#ifdef WITH_LIBXL
static int
298
xenUnifiedXendProbe(void)
J
Jim Fehlig 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312
{
    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

313

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
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;
}


330 331
virDomainDefParserConfig xenDomainDefParserConfig = {
    .macPrefix = { 0x00, 0x16, 0x3e },
332
    .devicesPostParseCallback = xenDomainDeviceDefPostParse,
333 334
};

335

336 337 338 339 340 341 342 343
virDomainXMLOptionPtr
xenDomainXMLConfInit(void)
{
    return virDomainXMLOptionNew(&xenDomainDefParserConfig,
                                 NULL, NULL);
}


344
static virDrvOpenStatus
345
xenUnifiedConnectOpen(virConnectPtr conn, virConnectAuthPtr auth, unsigned int flags)
346
{
347
    xenUnifiedPrivatePtr priv;
348
    char ebuf[1024];
349

J
John Levon 已提交
350 351 352 353
    /*
     * Only the libvirtd instance can open this driver.
     * Everything else falls back to the remote driver.
     */
354
    if (!is_privileged)
J
John Levon 已提交
355 356
        return VIR_DRV_OPEN_DECLINED;

357 358 359 360
    if (conn->uri == NULL) {
        if (!xenUnifiedProbe())
            return VIR_DRV_OPEN_DECLINED;

361
        if (!(conn->uri = virURIParse("xen:///")))
362
            return VIR_DRV_OPEN_ERROR;
363 364 365 366 367 368 369 370 371 372 373 374
    } 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, "/")) {
375 376 377
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("unexpected Xen URI path '%s', try xen:///"),
                               conn->uri->path);
378 379
                return VIR_DRV_OPEN_ERROR;
            }
380

381 382 383 384 385 386
            /* 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 {
387
            return VIR_DRV_OPEN_DECLINED;
388 389
        }
    }
390

J
Jim Fehlig 已提交
391 392 393 394 395 396 397
#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

398 399
    /* We now know the URI is definitely for this driver, so beyond
     * here, don't return DECLINED, always use ERROR */
400 401

    /* Allocate per-connection private data. */
402
    if (VIR_ALLOC(priv) < 0) {
403
        virReportOOMError();
404 405
        return VIR_DRV_OPEN_ERROR;
    }
D
Daniel P. Berrange 已提交
406
    if (virMutexInit(&priv->lock) < 0) {
407 408
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("cannot initialize mutex"));
D
Daniel P. Berrange 已提交
409 410 411
        VIR_FREE(priv);
        return VIR_DRV_OPEN_ERROR;
    }
412

413
    if (!(priv->domainEvents = virDomainEventStateNew())) {
D
Daniel P. Berrange 已提交
414 415
        virMutexDestroy(&priv->lock);
        VIR_FREE(priv);
416 417
        return VIR_DRV_OPEN_ERROR;
    }
D
Daniel P. Berrange 已提交
418 419
    conn->privateData = priv;

420 421 422 423 424
    priv->handle = -1;
    priv->xendConfigVersion = -1;
    priv->xshandle = NULL;


425 426 427 428 429 430
    /* 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;
431

432
    /* XenD is required to succeed */
433
    VIR_DEBUG("Trying XenD sub-driver");
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
    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;
453

D
Daniel P. Berrange 已提交
454 455
    xenNumaInit(conn);

456
    if (!(priv->caps = xenHypervisorMakeCapabilities(conn))) {
457
        VIR_DEBUG("Failed to make capabilities");
458
        goto error;
459 460
    }

461
    if (!(priv->xmlopt = xenDomainXMLConfInit()))
462
        goto error;
463

464
#if WITH_XEN_INOTIFY
465 466 467 468 469
    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;
470 471
#endif

E
Eric Blake 已提交
472
    if (!(priv->saveDir = strdup(XEN_SAVE_DIR))) {
473
        virReportOOMError();
474
        goto error;
475 476 477
    }

    if (virFileMakePath(priv->saveDir) < 0) {
478
        VIR_ERROR(_("Errored to create save dir '%s': %s"), priv->saveDir,
479
                  virStrerror(errno, ebuf, sizeof(ebuf)));
480
        goto error;
481 482
    }

483
    return VIR_DRV_OPEN_SUCCESS;
484

485
error:
486
    VIR_DEBUG("Failed to activate a mandatory sub-driver");
487 488 489 490 491 492 493 494 495 496 497 498
#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 已提交
499
    virMutexDestroy(&priv->lock);
500
    VIR_FREE(priv->saveDir);
501
    VIR_FREE(priv);
D
Daniel P. Berrange 已提交
502
    conn->privateData = NULL;
503
    return VIR_DRV_OPEN_ERROR;
504 505 506
}

static int
507
xenUnifiedConnectClose(virConnectPtr conn)
508
{
509
    xenUnifiedPrivatePtr priv = conn->privateData;
510

511
    virObjectUnref(priv->caps);
512
    virObjectUnref(priv->xmlopt);
513
    virDomainEventStateFree(priv->domainEvents);
514

515 516 517 518 519 520 521 522 523 524 525 526
#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);
527

528
    VIR_FREE(priv->saveDir);
D
Daniel P. Berrange 已提交
529 530
    virMutexDestroy(&priv->lock);
    VIR_FREE(conn->privateData);
531

532
    return 0;
533 534
}

535 536 537 538 539 540 541 542 543 544 545

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


546
static const char *
547
xenUnifiedConnectGetType(virConnectPtr conn ATTRIBUTE_UNUSED)
548
{
549
    return "Xen";
550 551
}

552 553
/* Which features are supported by this driver? */
static int
554
xenUnifiedConnectSupportsFeature(virConnectPtr conn ATTRIBUTE_UNUSED, int feature)
555 556
{
    switch (feature) {
557 558 559 560 561
    case VIR_DRV_FEATURE_MIGRATION_V1:
    case VIR_DRV_FEATURE_MIGRATION_DIRECT:
        return 1;
    default:
        return 0;
562 563 564
    }
}

565
static int
566
xenUnifiedConnectGetVersion(virConnectPtr conn, unsigned long *hvVer)
567
{
568
    return xenHypervisorGetVersion(conn, hvVer);
569 570
}

571 572 573 574 575 576 577

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


578
static int
579
xenUnifiedConnectIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
580 581 582 583 584
{
    return 0;
}

static int
585
xenUnifiedConnectIsSecure(virConnectPtr conn)
586
{
587
    xenUnifiedPrivatePtr priv = conn->privateData;
588 589 590 591 592 593 594 595 596 597
    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;
}

598
static int
599
xenUnifiedConnectIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
600 601 602 603 604
{
    /* XenD reconnects for each request */
    return 1;
}

605
int
606
xenUnifiedConnectGetMaxVcpus(virConnectPtr conn, const char *type)
607
{
608
    if (type && STRCASENEQ(type, "Xen")) {
609
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
610 611
        return -1;
    }
612

613
    return xenHypervisorGetMaxVcpus(conn, type);
614 615 616
}

static int
617
xenUnifiedNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info)
618
{
619
    return xenDaemonNodeGetInfo(conn, info);
620 621 622
}

static char *
623
xenUnifiedConnectGetCapabilities(virConnectPtr conn)
624
{
D
Daniel P. Berrange 已提交
625 626
    xenUnifiedPrivatePtr priv = conn->privateData;
    char *xml;
627

D
Daniel P. Berrange 已提交
628
    if (!(xml = virCapabilitiesFormatXML(priv->caps))) {
629
        virReportOOMError();
D
Daniel P. Berrange 已提交
630 631
        return NULL;
    }
632

D
Daniel P. Berrange 已提交
633
    return xml;
634 635 636
}

static int
637
xenUnifiedConnectListDomains(virConnectPtr conn, int *ids, int maxids)
638
{
639
    return xenStoreListDomains(conn, ids, maxids);
640 641 642
}

static int
643
xenUnifiedConnectNumOfDomains(virConnectPtr conn)
644
{
645
    return xenStoreNumOfDomains(conn);
646 647 648
}

static virDomainPtr
649 650
xenUnifiedDomainCreateXML(virConnectPtr conn,
                          const char *xmlDesc, unsigned int flags)
651
{
652
    virCheckFlags(0, NULL);
653

654
    return xenDaemonCreateXML(conn, xmlDesc);
655 656 657
}

static virDomainPtr
658
xenUnifiedDomainLookupByID(virConnectPtr conn, int id)
659
{
660
    virDomainPtr ret = NULL;
661
    virDomainDefPtr def = NULL;
662

663 664
    if (!(def = xenGetDomainDefForID(conn, id)))
        goto cleanup;
665

666 667
    if (!(ret = virGetDomain(conn, def->name, def->uuid)))
        goto cleanup;
668

669 670 671 672
    ret->id = def->id;

cleanup:
    virDomainDefFree(def);
673
    return ret;
674 675 676
}

static virDomainPtr
677 678
xenUnifiedDomainLookupByUUID(virConnectPtr conn,
                             const unsigned char *uuid)
679
{
680 681
    virDomainPtr ret = NULL;
    virDomainDefPtr def = NULL;
682

683 684
    if (!(def = xenGetDomainDefForUUID(conn, uuid)))
        goto cleanup;
685

686 687
    if (!(ret = virGetDomain(conn, def->name, def->uuid)))
        goto cleanup;
688

689
    ret->id = def->id;
690

691 692
cleanup:
    virDomainDefFree(def);
693
    return ret;
694 695 696
}

static virDomainPtr
697 698
xenUnifiedDomainLookupByName(virConnectPtr conn,
                             const char *name)
699
{
700 701
    virDomainPtr ret = NULL;
    virDomainDefPtr def = NULL;
702

703 704
    if (!(def = xenGetDomainDefForName(conn, name)))
        goto cleanup;
705

706 707
    if (!(ret = virGetDomain(conn, def->name, def->uuid)))
        goto cleanup;
708

709
    ret->id = def->id;
710

711 712
cleanup:
    virDomainDefFree(def);
713
    return ret;
714 715
}

716 717 718 719

static int
xenUnifiedDomainIsActive(virDomainPtr dom)
{
720
    virDomainDefPtr def;
721 722
    int ret = -1;

723 724
    if (!(def = xenGetDomainDefForUUID(dom->conn, dom->uuid)))
        goto cleanup;
725

726
    ret = def->id == -1 ? 0 : 1;
727

728 729
cleanup:
    virDomainDefFree(def);
730 731 732 733
    return ret;
}

static int
734
xenUnifiedDomainIsPersistent(virDomainPtr dom)
735
{
736
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
737
    virDomainDefPtr def = NULL;
738 739 740 741 742
    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 */
743 744
        def = xenXMDomainLookupByUUID(dom->conn, dom->uuid);
        if (def)
745 746 747 748 749
            ret = 1;
        else
            ret = 0;
    } else {
        /* New Xen with inactive domain management */
750 751 752
        def = xenDaemonLookupByUUID(dom->conn, dom->uuid);
        if (def) {
            if (def->id == -1) {
753 754 755 756 757 758 759 760 761 762 763
                /* 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) {
                    virReportOOMError();
764
                    goto cleanup;
765
                }
766 767 768 769
                if (access(path, R_OK) == 0)
                    ret = 1;
                else if (errno == ENOENT)
                    ret = 0;
770 771 772 773
            }
        }
    }

774 775
cleanup:
    virDomainDefFree(def);
776 777 778 779

    return ret;
}

780 781 782 783 784 785
static int
xenUnifiedDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

786
static int
787
xenUnifiedDomainSuspend(virDomainPtr dom)
788
{
789 790 791 792 793 794 795 796 797 798 799
    int ret = -1;
    virDomainDefPtr def;

    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

    ret = xenDaemonDomainSuspend(dom->conn, def);

cleanup:
    virDomainDefFree(def);
    return ret;
800 801 802
}

static int
803
xenUnifiedDomainResume(virDomainPtr dom)
804
{
805 806 807 808 809 810 811 812 813 814 815
    int ret = -1;
    virDomainDefPtr def;

    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

    ret = xenDaemonDomainResume(dom->conn, def);

cleanup:
    virDomainDefFree(def);
    return ret;
816 817 818
}

static int
819 820
xenUnifiedDomainShutdownFlags(virDomainPtr dom,
                              unsigned int flags)
821
{
822 823 824
    int ret = -1;
    virDomainDefPtr def;

825 826
    virCheckFlags(0, -1);

827 828 829 830 831 832 833 834
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

    ret = xenDaemonDomainShutdown(dom->conn, def);

cleanup:
    virDomainDefFree(def);
    return ret;
835 836
}

837 838 839 840 841 842
static int
xenUnifiedDomainShutdown(virDomainPtr dom)
{
    return xenUnifiedDomainShutdownFlags(dom, 0);
}

843
static int
844
xenUnifiedDomainReboot(virDomainPtr dom, unsigned int flags)
845
{
846 847 848
    int ret = -1;
    virDomainDefPtr def;

849
    virCheckFlags(0, -1);
850

851 852 853 854 855 856 857 858
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

    ret = xenDaemonDomainReboot(dom->conn, def);

cleanup:
    virDomainDefFree(def);
    return ret;
859 860
}

861 862 863 864
static int
xenUnifiedDomainDestroyFlags(virDomainPtr dom,
                             unsigned int flags)
{
865 866 867
    int ret = -1;
    virDomainDefPtr def;

868 869
    virCheckFlags(0, -1);

870 871 872 873 874 875 876 877
    if (!(def = xenGetDomainDefForDom(dom)))
        goto cleanup;

    ret = xenDaemonDomainDestroy(dom->conn, def);

cleanup:
    virDomainDefFree(def);
    return ret;
878 879
}

880 881 882 883 884 885
static int
xenUnifiedDomainDestroy(virDomainPtr dom)
{
    return xenUnifiedDomainDestroyFlags(dom, 0);
}

886
static char *
887
xenUnifiedDomainGetOSType(virDomainPtr dom)
888
{
889
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
890

891 892 893 894 895 896 897
    if (dom->id < 0) {
        if (priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Unable to query OS type for inactive domain"));
            return NULL;
        } else {
            return xenDaemonDomainGetOSType(dom);
898
        }
899 900 901
    } else {
        return xenHypervisorDomainGetOSType(dom);
    }
902 903
}

904

905
static unsigned long long
906
xenUnifiedDomainGetMaxMemory(virDomainPtr dom)
907
{
908
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
909

910 911 912 913 914 915 916 917
    if (dom->id < 0) {
        if (priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4)
            return xenXMDomainGetMaxMemory(dom);
        else
            return xenDaemonDomainGetMaxMemory(dom);
    } else {
        return xenHypervisorGetMaxMemory(dom);
    }
918 919 920
}

static int
921
xenUnifiedDomainSetMaxMemory(virDomainPtr dom, unsigned long memory)
922
{
923
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
924

925 926 927 928 929 930 931
    if (dom->id < 0) {
        if (priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4)
            return xenXMDomainSetMaxMemory(dom, memory);
        else
            return xenDaemonDomainSetMaxMemory(dom, memory);
    } else {
        return xenHypervisorSetMaxMemory(dom, memory);
932
    }
933 934 935
}

static int
936
xenUnifiedDomainSetMemory(virDomainPtr dom, unsigned long memory)
937
{
938
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
939

940 941 942 943
    if (dom->id < 0 && priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4)
        return xenXMDomainSetMemory(dom, memory);
    else
        return xenDaemonDomainSetMemory(dom, memory);
944 945 946
}

static int
947
xenUnifiedDomainGetInfo(virDomainPtr dom, virDomainInfoPtr info)
948
{
949
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
950

951 952 953 954 955 956 957 958
    if (dom->id < 0) {
        if (priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4)
            return xenXMDomainGetInfo(dom, info);
        else
            return xenDaemonDomainGetInfo(dom, info);
    } else {
        return xenHypervisorGetDomainInfo(dom, info);
    }
959 960
}

961 962 963 964 965 966
static int
xenUnifiedDomainGetState(virDomainPtr dom,
                         int *state,
                         int *reason,
                         unsigned int flags)
{
967
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
968 969 970

    virCheckFlags(0, -1);

971 972 973 974 975 976 977
    if (dom->id < 0) {
        if (priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4)
            return xenXMDomainGetState(dom, state, reason);
        else
            return xenDaemonDomainGetState(dom, state, reason);
    } else {
        return xenHypervisorGetDomainState(dom, state, reason);
978 979 980
    }
}

981
static int
982 983
xenUnifiedDomainSaveFlags(virDomainPtr dom, const char *to, const char *dxml,
                          unsigned int flags)
984
{
985 986
    virCheckFlags(0, -1);
    if (dxml) {
987 988
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
989 990 991
        return -1;
    }

992
    return xenDaemonDomainSave(dom, to);
993 994 995
}

static int
996 997 998 999 1000
xenUnifiedDomainSave(virDomainPtr dom, const char *to)
{
    return xenUnifiedDomainSaveFlags(dom, to, NULL, 0);
}

1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
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)
{
1018
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1019 1020 1021 1022 1023 1024 1025 1026 1027
    char *name;
    int ret = -1;

    virCheckFlags(0, -1);

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

1028
    ret = xenDaemonDomainSave(dom, name);
1029 1030 1031 1032 1033 1034 1035 1036 1037

cleanup:
    VIR_FREE(name);
    return ret;
}

static int
xenUnifiedDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
{
1038
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
    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)
{
1056
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
    char *name;
    int ret = -1;

    virCheckFlags(0, -1);

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

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

1071 1072 1073
static int
xenUnifiedDomainRestoreFlags(virConnectPtr conn, const char *from,
                             const char *dxml, unsigned int flags)
1074
{
1075 1076
    virCheckFlags(0, -1);
    if (dxml) {
1077 1078
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
1079 1080 1081
        return -1;
    }

1082
    return xenDaemonDomainRestore(conn, from);
1083 1084
}

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

1091
static int
1092
xenUnifiedDomainCoreDump(virDomainPtr dom, const char *to, unsigned int flags)
1093
{
1094
    return xenDaemonDomainCoreDump(dom, to, flags);
1095 1096 1097
}

static int
1098 1099
xenUnifiedDomainSetVcpusFlags(virDomainPtr dom, unsigned int nvcpus,
                              unsigned int flags)
1100
{
1101
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1102

1103 1104 1105 1106 1107 1108 1109 1110 1111
    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)) {
1112 1113
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid flag combination: (0x%x)"), flags);
1114 1115 1116
        return -1;
    }
    if (!nvcpus || (unsigned short) nvcpus != nvcpus) {
1117 1118
        virReportError(VIR_ERR_INVALID_ARG,
                       _("argument out of range: %d"), nvcpus);
1119 1120 1121
        return -1;
    }

1122 1123 1124
    /* Try non-hypervisor methods first, then hypervisor direct method
     * as a last resort.
     */
1125 1126 1127 1128
    if (dom->id < 0 && priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4)
        return xenXMDomainSetVcpusFlags(dom, nvcpus, flags);
    else
        return xenDaemonDomainSetVcpusFlags(dom, nvcpus, flags);
1129 1130
}

1131
static int
1132
xenUnifiedDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus)
1133
{
1134
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1135 1136 1137 1138 1139
    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.  */
1140 1141 1142 1143
    if (priv->xendConfigVersion >= XEND_CONFIG_VERSION_3_0_4)
        flags |= VIR_DOMAIN_VCPU_CONFIG;

    return xenUnifiedDomainSetVcpusFlags(dom, nvcpus, flags);
1144 1145
}

1146
static int
1147 1148
xenUnifiedDomainPinVcpu(virDomainPtr dom, unsigned int vcpu,
                        unsigned char *cpumap, int maplen)
1149
{
1150
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1151

1152 1153 1154 1155 1156 1157 1158 1159
    if (dom->id < 0) {
        if (priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4)
            return xenXMDomainPinVcpu(dom, vcpu, cpumap, maplen);
        else
            return xenDaemonDomainPinVcpu(dom, vcpu, cpumap, maplen);
    } else {
        return xenHypervisorPinVcpu(dom, vcpu, cpumap, maplen);
    }
1160 1161 1162
}

static int
1163 1164 1165
xenUnifiedDomainGetVcpus(virDomainPtr dom,
                         virVcpuInfoPtr info, int maxinfo,
                         unsigned char *cpumaps, int maplen)
1166
{
1167
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1168 1169 1170 1171 1172 1173 1174
    if (dom->id < 0) {
        if (priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Cannot get VCPUs of inactive domain"));
            return -1;
        } else {
            return xenDaemonDomainGetVcpus(dom, info, maxinfo, cpumaps, maplen);
1175
        }
1176 1177 1178
    } else {
        return xenHypervisorGetVcpus(dom, info, maxinfo, cpumaps, maplen);
    }
1179 1180 1181
}

static int
1182
xenUnifiedDomainGetVcpusFlags(virDomainPtr dom, unsigned int flags)
1183
{
1184
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1185

1186 1187 1188 1189
    virCheckFlags(VIR_DOMAIN_VCPU_LIVE |
                  VIR_DOMAIN_VCPU_CONFIG |
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
    if (dom->id < 0) {
        if (priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4)
            return xenXMDomainGetVcpusFlags(dom, flags);
        else
            return xenDaemonDomainGetVcpusFlags(dom, flags);
    } else {
        if (flags == (VIR_DOMAIN_VCPU_CONFIG | VIR_DOMAIN_VCPU_MAXIMUM))
            return xenHypervisorGetVcpuMax(dom);
        else
            return xenDaemonDomainGetVcpusFlags(dom, flags);
1200
    }
1201 1202
}

1203
static int
1204
xenUnifiedDomainGetMaxVcpus(virDomainPtr dom)
1205 1206 1207 1208 1209
{
    return xenUnifiedDomainGetVcpusFlags(dom, (VIR_DOMAIN_VCPU_LIVE |
                                               VIR_DOMAIN_VCPU_MAXIMUM));
}

1210
static char *
1211
xenUnifiedDomainGetXMLDesc(virDomainPtr dom, unsigned int flags)
1212
{
1213
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1214

1215 1216
    if (dom->id < 0 && priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4) {
        return xenXMDomainGetXMLDesc(dom, flags);
1217
    } else {
1218 1219 1220 1221 1222 1223 1224
        char *cpus, *res;
        xenUnifiedLock(priv);
        cpus = xenDomainUsedCpus(dom);
        xenUnifiedUnlock(priv);
        res = xenDaemonDomainGetXMLDesc(dom, flags, cpus);
        VIR_FREE(cpus);
        return res;
1225
    }
1226 1227
}

1228 1229

static char *
1230 1231 1232 1233
xenUnifiedConnectDomainXMLFromNative(virConnectPtr conn,
                                     const char *format,
                                     const char *config,
                                     unsigned int flags)
1234 1235 1236 1237
{
    virDomainDefPtr def = NULL;
    char *ret = NULL;
    virConfPtr conf = NULL;
1238 1239 1240
    int id;
    char * tty;
    int vncport;
1241
    xenUnifiedPrivatePtr priv = conn->privateData;
1242

E
Eric Blake 已提交
1243 1244
    virCheckFlags(0, NULL);

1245 1246
    if (STRNEQ(format, XEN_CONFIG_FORMAT_XM) &&
        STRNEQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
1247 1248
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unsupported config type %s"), format);
1249 1250 1251 1252
        return NULL;
    }

    if (STREQ(format, XEN_CONFIG_FORMAT_XM)) {
1253
        conf = virConfReadMem(config, strlen(config), 0);
1254 1255 1256
        if (!conf)
            goto cleanup;

M
Markus Groß 已提交
1257
        def = xenParseXM(conf, priv->xendConfigVersion, priv->caps);
1258
    } else if (STREQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
1259 1260 1261 1262 1263
        id = xenGetDomIdFromSxprString(config, priv->xendConfigVersion);
        xenUnifiedLock(priv);
        tty = xenStoreDomainGetConsolePath(conn, id);
        vncport = xenStoreDomainGetVNCPort(conn, id);
        xenUnifiedUnlock(priv);
M
Markus Groß 已提交
1264
        def = xenParseSxprString(config, priv->xendConfigVersion, tty,
1265
                                       vncport);
1266 1267 1268 1269
    }
    if (!def)
        goto cleanup;

1270
    ret = virDomainDefFormat(def, 0);
1271 1272 1273

cleanup:
    virDomainDefFree(def);
1274 1275
    if (conf)
        virConfFree(conf);
1276 1277 1278 1279 1280 1281
    return ret;
}


#define MAX_CONFIG_SIZE (1024 * 65)
static char *
1282 1283 1284 1285
xenUnifiedConnectDomainXMLToNative(virConnectPtr conn,
                                   const char *format,
                                   const char *xmlData,
                                   unsigned int flags)
1286 1287 1288 1289
{
    virDomainDefPtr def = NULL;
    char *ret = NULL;
    virConfPtr conf = NULL;
1290
    xenUnifiedPrivatePtr priv = conn->privateData;
1291

E
Eric Blake 已提交
1292 1293
    virCheckFlags(0, NULL);

1294 1295
    if (STRNEQ(format, XEN_CONFIG_FORMAT_XM) &&
        STRNEQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
1296 1297
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unsupported config type %s"), format);
1298 1299 1300
        goto cleanup;
    }

1301 1302
    if (!(def = virDomainDefParseString(xmlData, priv->caps, priv->xmlopt,
                                        1 << VIR_DOMAIN_VIRT_XEN, 0)))
1303 1304 1305 1306
        goto cleanup;

    if (STREQ(format, XEN_CONFIG_FORMAT_XM)) {
        int len = MAX_CONFIG_SIZE;
M
Markus Groß 已提交
1307
        conf = xenFormatXM(conn, def, priv->xendConfigVersion);
1308 1309 1310 1311
        if (!conf)
            goto cleanup;

        if (VIR_ALLOC_N(ret, len) < 0) {
1312
            virReportOOMError();
1313 1314 1315 1316 1317 1318 1319 1320
            goto cleanup;
        }

        if (virConfWriteMem(ret, &len, conf) < 0) {
            VIR_FREE(ret);
            goto cleanup;
        }
    } else if (STREQ(format, XEN_CONFIG_FORMAT_SEXPR)) {
M
Markus Groß 已提交
1321
        ret = xenFormatSxpr(conn, def, priv->xendConfigVersion);
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
    }

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


1332
static int
1333 1334 1335 1336 1337 1338 1339 1340
xenUnifiedDomainMigratePrepare(virConnectPtr dconn,
                               char **cookie,
                               int *cookielen,
                               const char *uri_in,
                               char **uri_out,
                               unsigned long flags,
                               const char *dname,
                               unsigned long resource)
1341
{
E
Eric Blake 已提交
1342 1343
    virCheckFlags(XEN_MIGRATION_FLAGS, -1);

1344 1345 1346
    return xenDaemonDomainMigratePrepare(dconn, cookie, cookielen,
                                         uri_in, uri_out,
                                         flags, dname, resource);
1347 1348 1349
}

static int
1350 1351 1352 1353 1354 1355 1356
xenUnifiedDomainMigratePerform(virDomainPtr dom,
                               const char *cookie,
                               int cookielen,
                               const char *uri,
                               unsigned long flags,
                               const char *dname,
                               unsigned long resource)
1357
{
E
Eric Blake 已提交
1358 1359
    virCheckFlags(XEN_MIGRATION_FLAGS, -1);

1360 1361
    return xenDaemonDomainMigratePerform(dom, cookie, cookielen, uri,
                                         flags, dname, resource);
1362 1363 1364
}

static virDomainPtr
1365 1366 1367 1368 1369 1370
xenUnifiedDomainMigrateFinish(virConnectPtr dconn,
                              const char *dname,
                              const char *cookie ATTRIBUTE_UNUSED,
                              int cookielen ATTRIBUTE_UNUSED,
                              const char *uri ATTRIBUTE_UNUSED,
                              unsigned long flags)
1371
{
1372 1373 1374 1375
    virDomainPtr dom = NULL;
    char *domain_xml = NULL;
    virDomainPtr dom_new = NULL;

E
Eric Blake 已提交
1376 1377
    virCheckFlags(XEN_MIGRATION_FLAGS, NULL);

1378
    if (!(dom = xenUnifiedDomainLookupByName(dconn, dname)))
1379 1380 1381
        return NULL;

    if (flags & VIR_MIGRATE_PERSIST_DEST) {
1382
        domain_xml = xenDaemonDomainGetXMLDesc(dom, 0, NULL);
1383
        if (! domain_xml) {
1384 1385
            virReportError(VIR_ERR_MIGRATE_PERSIST_FAILED,
                           "%s", _("failed to get XML representation of migrated domain"));
1386
            goto error;
1387 1388
        }

1389
        dom_new = xenDaemonDomainDefineXML(dconn, domain_xml);
1390
        if (! dom_new) {
1391 1392
            virReportError(VIR_ERR_MIGRATE_PERSIST_FAILED,
                           "%s", _("failed to define domain on destination host"));
1393
            goto error;
1394 1395 1396
        }

        /* Free additional reference added by Define */
1397
        virDomainFree(dom_new);
1398 1399
    }

1400
    VIR_FREE(domain_xml);
1401 1402 1403 1404

    return dom;


1405
error:
1406
    virDomainFree(dom);
1407

1408
    VIR_FREE(domain_xml);
1409 1410

    return NULL;
1411 1412
}

1413
static int
1414 1415
xenUnifiedConnectListDefinedDomains(virConnectPtr conn, char **const names,
                                    int maxnames)
1416
{
1417
    xenUnifiedPrivatePtr priv = conn->privateData;
1418

1419 1420 1421 1422 1423
    if (priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4) {
        return xenXMListDefinedDomains(conn, names, maxnames);
    } else {
        return xenDaemonListDefinedDomains(conn, names, maxnames);
    }
1424 1425 1426
}

static int
1427
xenUnifiedConnectNumOfDefinedDomains(virConnectPtr conn)
1428
{
1429
    xenUnifiedPrivatePtr priv = conn->privateData;
1430

1431 1432 1433 1434 1435
    if (priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4) {
        return xenXMNumOfDefinedDomains(conn);
    } else {
        return xenDaemonNumOfDefinedDomains(conn);
    }
1436 1437 1438
}

static int
1439
xenUnifiedDomainCreateWithFlags(virDomainPtr dom, unsigned int flags)
1440
{
1441
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1442 1443
    int ret = -1;
    char *name = NULL;
1444

1445 1446
    virCheckFlags(0, -1);

1447 1448 1449 1450 1451
    name = xenUnifiedDomainManagedSavePath(priv, dom);
    if (!name)
        goto cleanup;

    if (virFileExists(name)) {
1452 1453 1454
        ret = xenDaemonDomainRestore(dom->conn, name);
        if (ret == 0)
            unlink(name);
1455 1456 1457
        goto cleanup;
    }

1458 1459 1460 1461
    if (priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4)
        ret = xenXMDomainCreate(dom);
    else
        ret = xenDaemonDomainCreate(dom);
1462

1463 1464 1465
cleanup:
    VIR_FREE(name);
    return ret;
1466 1467
}

1468
static int
1469
xenUnifiedDomainCreate(virDomainPtr dom)
1470 1471 1472 1473
{
    return xenUnifiedDomainCreateWithFlags(dom, 0);
}

1474
static virDomainPtr
1475
xenUnifiedDomainDefineXML(virConnectPtr conn, const char *xml)
1476
{
1477
    xenUnifiedPrivatePtr priv = conn->privateData;
1478

1479 1480 1481 1482
    if (priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4)
        return xenXMDomainDefineXML(conn, xml);
    else
        return xenDaemonDomainDefineXML(conn, xml);
1483 1484 1485
}

static int
1486
xenUnifiedDomainUndefineFlags(virDomainPtr dom, unsigned int flags)
1487
{
1488
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1489

1490
    virCheckFlags(0, -1);
1491 1492 1493 1494
    if (priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4)
        return xenXMDomainUndefine(dom);
    else
        return xenDaemonDomainUndefine(dom);
1495 1496
}

1497
static int
1498
xenUnifiedDomainUndefine(virDomainPtr dom) {
1499 1500 1501
    return xenUnifiedDomainUndefineFlags(dom, 0);
}

1502
static int
1503
xenUnifiedDomainAttachDevice(virDomainPtr dom, const char *xml)
1504
{
1505
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1506
    unsigned int flags = VIR_DOMAIN_DEVICE_MODIFY_LIVE;
1507

1508 1509 1510 1511 1512
    /*
     * 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
     */
1513
    if (priv->xendConfigVersion >= XEND_CONFIG_VERSION_3_0_4)
1514
        flags |= VIR_DOMAIN_DEVICE_MODIFY_CONFIG;
1515

1516 1517 1518 1519
    if (dom->id < 0 && priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4)
        return xenXMDomainAttachDeviceFlags(dom, xml, flags);
    else
        return xenDaemonAttachDeviceFlags(dom, xml, flags);
1520 1521 1522 1523 1524

    return -1;
}

static int
1525 1526
xenUnifiedDomainAttachDeviceFlags(virDomainPtr dom, const char *xml,
                                  unsigned int flags)
1527
{
1528
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1529

1530 1531 1532 1533
    if (dom->id < 0 && priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4)
        return xenXMDomainAttachDeviceFlags(dom, xml, flags);
    else
        return xenDaemonAttachDeviceFlags(dom, xml, flags);
1534 1535 1536
}

static int
1537
xenUnifiedDomainDetachDevice(virDomainPtr dom, const char *xml)
1538
{
1539
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1540
    unsigned int flags = VIR_DOMAIN_DEVICE_MODIFY_LIVE;
1541

1542 1543 1544 1545 1546
    /*
     * 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
     */
1547
    if (priv->xendConfigVersion >= XEND_CONFIG_VERSION_3_0_4)
1548
        flags |= VIR_DOMAIN_DEVICE_MODIFY_CONFIG;
1549

1550 1551 1552 1553
    if (dom->id < 0 && priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4)
        return xenXMDomainDetachDeviceFlags(dom, xml, flags);
    else
        return xenDaemonDetachDeviceFlags(dom, xml, flags);
1554 1555 1556
}

static int
1557 1558
xenUnifiedDomainDetachDeviceFlags(virDomainPtr dom, const char *xml,
                                  unsigned int flags)
1559
{
1560
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1561

1562 1563 1564 1565
    if (dom->id < 0 && priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4)
        return xenXMDomainDetachDeviceFlags(dom, xml, flags);
    else
        return xenDaemonDetachDeviceFlags(dom, xml, flags);
1566 1567
}

1568
static int
1569 1570
xenUnifiedDomainUpdateDeviceFlags(virDomainPtr dom, const char *xml,
                                  unsigned int flags)
1571
{
1572
    return xenDaemonUpdateDeviceFlags(dom, xml, flags);
1573 1574
}

1575
static int
1576
xenUnifiedDomainGetAutostart(virDomainPtr dom, int *autostart)
1577
{
1578
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1579

1580 1581 1582 1583
    if (priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4)
        return xenXMDomainGetAutostart(dom, autostart);
    else
        return xenDaemonDomainGetAutostart(dom, autostart);
1584 1585 1586
}

static int
1587
xenUnifiedDomainSetAutostart(virDomainPtr dom, int autostart)
1588
{
1589
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1590

1591 1592 1593 1594
    if (priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4)
        return xenXMDomainSetAutostart(dom, autostart);
    else
        return xenDaemonDomainSetAutostart(dom, autostart);
1595 1596
}

1597
static char *
1598
xenUnifiedDomainGetSchedulerType(virDomainPtr dom, int *nparams)
1599
{
1600
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1601 1602 1603 1604 1605 1606

    if (dom->id < 0) {
        if (priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Cannot change scheduler parameters"));
            return NULL;
1607
        }
1608 1609 1610
        return xenDaemonGetSchedulerType(dom, nparams);
    } else {
        return xenHypervisorGetSchedulerType(dom, nparams);
1611 1612 1613 1614
    }
}

static int
1615 1616 1617 1618
xenUnifiedDomainGetSchedulerParametersFlags(virDomainPtr dom,
                                            virTypedParameterPtr params,
                                            int *nparams,
                                            unsigned int flags)
1619
{
1620
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1621

1622 1623
    virCheckFlags(0, -1);

1624 1625 1626 1627 1628
    if (dom->id < 0) {
        if (priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Cannot change scheduler parameters"));
            return -1;
1629
        }
1630 1631 1632
        return xenDaemonGetSchedulerParameters(dom, params, nparams);
    } else {
        return xenHypervisorGetSchedulerParameters(dom, params, nparams);
1633 1634 1635 1636
    }
}

static int
1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649
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)
1650
{
1651
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
1652

1653 1654
    virCheckFlags(0, -1);

1655 1656 1657 1658 1659
    if (dom->id < 0) {
        if (priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Cannot change scheduler parameters"));
            return -1;
1660
        }
1661 1662 1663
        return xenDaemonSetSchedulerParameters(dom, params, nparams);
    } else {
        return xenHypervisorSetSchedulerParameters(dom, params, nparams);
1664 1665 1666
    }
}

1667 1668 1669 1670 1671 1672 1673 1674 1675
static int
xenUnifiedDomainSetSchedulerParameters(virDomainPtr dom,
                                       virTypedParameterPtr params,
                                       int nparams)
{
    return xenUnifiedDomainSetSchedulerParametersFlags(dom, params,
                                                       nparams, 0);
}

1676
static int
1677 1678
xenUnifiedDomainBlockStats(virDomainPtr dom, const char *path,
                           struct _virDomainBlockStats *stats)
1679
{
1680
    return xenHypervisorDomainBlockStats(dom, path, stats);
1681 1682 1683
}

static int
1684 1685
xenUnifiedDomainInterfaceStats(virDomainPtr dom, const char *path,
                               struct _virDomainInterfaceStats *stats)
1686
{
1687
    return xenHypervisorDomainInterfaceStats(dom, path, stats);
1688 1689
}

R
Richard W.M. Jones 已提交
1690
static int
1691 1692 1693
xenUnifiedDomainBlockPeek(virDomainPtr dom, const char *path,
                          unsigned long long offset, size_t size,
                          void *buffer, unsigned int flags)
R
Richard W.M. Jones 已提交
1694
{
1695
    xenUnifiedPrivatePtr priv = dom->conn->privateData;
R
Richard W.M. Jones 已提交
1696

E
Eric Blake 已提交
1697 1698
    virCheckFlags(0, -1);

1699 1700 1701 1702
    if (dom->id < 0 && priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4)
        return xenXMDomainBlockPeek(dom, path, offset, size, buffer);
    else
        return xenDaemonDomainBlockPeek(dom, path, offset, size, buffer);
R
Richard W.M. Jones 已提交
1703 1704
}

1705
static int
1706 1707
xenUnifiedNodeGetCellsFreeMemory(virConnectPtr conn, unsigned long long *freeMems,
                                 int startCell, int maxCells)
1708
{
1709 1710
    return xenHypervisorNodeGetCellsFreeMemory(conn, freeMems,
                                               startCell, maxCells);
1711 1712
}

1713
static unsigned long long
1714
xenUnifiedNodeGetFreeMemory(virConnectPtr conn)
1715 1716 1717
{
    unsigned long long freeMem = 0;

1718 1719 1720
    if (xenHypervisorNodeGetCellsFreeMemory(conn, &freeMem, -1, 1) < 0)
        return 0;
    return freeMem;
1721 1722
}

1723

1724
static int
1725 1726 1727 1728
xenUnifiedConnectDomainEventRegister(virConnectPtr conn,
                                     virConnectDomainEventCallback callback,
                                     void *opaque,
                                     virFreeCallback freefunc)
1729
{
1730
    xenUnifiedPrivatePtr priv = conn->privateData;
D
Daniel P. Berrange 已提交
1731

1732
    int ret;
D
Daniel P. Berrange 已提交
1733
    xenUnifiedLock(priv);
1734

1735
    if (priv->xsWatch == -1) {
1736
        virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
D
Daniel P. Berrange 已提交
1737
        xenUnifiedUnlock(priv);
1738 1739 1740
        return -1;
    }

1741 1742
    ret = virDomainEventStateRegister(conn, priv->domainEvents,
                                      callback, opaque, freefunc);
1743

D
Daniel P. Berrange 已提交
1744
    xenUnifiedUnlock(priv);
1745
    return ret;
1746 1747
}

1748

1749
static int
1750 1751
xenUnifiedConnectDomainEventDeregister(virConnectPtr conn,
                                       virConnectDomainEventCallback callback)
1752 1753
{
    int ret;
1754
    xenUnifiedPrivatePtr priv = conn->privateData;
D
Daniel P. Berrange 已提交
1755 1756
    xenUnifiedLock(priv);

1757
    if (priv->xsWatch == -1) {
1758
        virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
D
Daniel P. Berrange 已提交
1759
        xenUnifiedUnlock(priv);
1760 1761 1762
        return -1;
    }

1763 1764 1765
    ret = virDomainEventStateDeregister(conn,
                                        priv->domainEvents,
                                        callback);
1766

D
Daniel P. Berrange 已提交
1767
    xenUnifiedUnlock(priv);
1768 1769 1770
    return ret;
}

1771

1772
static int
1773 1774 1775 1776 1777 1778
xenUnifiedConnectDomainEventRegisterAny(virConnectPtr conn,
                                        virDomainPtr dom,
                                        int eventID,
                                        virConnectDomainEventGenericCallback callback,
                                        void *opaque,
                                        virFreeCallback freefunc)
1779
{
1780
    xenUnifiedPrivatePtr priv = conn->privateData;
1781 1782 1783 1784 1785

    int ret;
    xenUnifiedLock(priv);

    if (priv->xsWatch == -1) {
1786
        virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1787 1788 1789 1790
        xenUnifiedUnlock(priv);
        return -1;
    }

1791 1792 1793
    if (virDomainEventStateRegisterID(conn, priv->domainEvents,
                                      dom, eventID,
                                      callback, opaque, freefunc, &ret) < 0)
1794
        ret = -1;
1795 1796

    xenUnifiedUnlock(priv);
1797
    return ret;
1798 1799 1800
}

static int
1801 1802
xenUnifiedConnectDomainEventDeregisterAny(virConnectPtr conn,
                                          int callbackID)
1803 1804
{
    int ret;
1805
    xenUnifiedPrivatePtr priv = conn->privateData;
1806 1807 1808
    xenUnifiedLock(priv);

    if (priv->xsWatch == -1) {
1809
        virReportError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
1810 1811 1812 1813
        xenUnifiedUnlock(priv);
        return -1;
    }

1814 1815 1816
    ret = virDomainEventStateDeregisterID(conn,
                                          priv->domainEvents,
                                          callbackID);
1817 1818 1819 1820 1821 1822

    xenUnifiedUnlock(priv);
    return ret;
}


1823
static int
1824 1825 1826 1827 1828
xenUnifiedNodeDeviceGetPciInfo(virNodeDevicePtr dev,
                               unsigned *domain,
                               unsigned *bus,
                               unsigned *slot,
                               unsigned *function)
1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
{
    virNodeDeviceDefPtr def = NULL;
    virNodeDevCapsDefPtr cap;
    char *xml = NULL;
    int ret = -1;

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

1839
    def = virNodeDeviceDefParseString(xml, EXISTING_DEVICE, NULL);
1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856
    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) {
1857 1858
        virReportError(VIR_ERR_INVALID_ARG,
                       _("device %s is not a PCI device"), dev->name);
1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869
        goto out;
    }

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

static int
1870 1871 1872
xenUnifiedNodeDeviceDetachFlags(virNodeDevicePtr dev,
                                const char *driverName,
                                unsigned int flags)
1873
{
1874
    virPCIDevicePtr pci;
1875 1876 1877
    unsigned domain, bus, slot, function;
    int ret = -1;

1878 1879
    virCheckFlags(0, -1);

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

1883
    pci = virPCIDeviceNew(domain, bus, slot, function);
1884 1885 1886
    if (!pci)
        return -1;

1887 1888 1889 1890 1891 1892 1893 1894 1895
    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)
1896 1897 1898 1899
        goto out;

    ret = 0;
out:
1900
    virPCIDeviceFree(pci);
1901 1902 1903
    return ret;
}

1904 1905 1906 1907 1908 1909
static int
xenUnifiedNodeDeviceDettach(virNodeDevicePtr dev)
{
    return xenUnifiedNodeDeviceDetachFlags(dev, NULL, 0);
}

1910
static int
1911
xenUnifiedNodeDeviceAssignedDomainId(virNodeDevicePtr dev)
1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922
{
    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 */
1923
    numdomains = xenUnifiedConnectNumOfDomains(conn);
1924 1925 1926 1927 1928 1929 1930 1931
    if (numdomains < 0) {
        return ret;
    }
    if (numdomains > 0){
        if (VIR_ALLOC_N(ids, numdomains) < 0) {
            virReportOOMError();
            goto out;
        }
1932
        if ((numdomains = xenUnifiedConnectListDomains(conn, &ids[0], numdomains)) < 0) {
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948
            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 */
1949
    for (i = 0; i < numdomains; i++) {
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
        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;
}

1968
static int
1969
xenUnifiedNodeDeviceReAttach(virNodeDevicePtr dev)
1970
{
1971
    virPCIDevicePtr pci;
1972 1973
    unsigned domain, bus, slot, function;
    int ret = -1;
1974
    int domid;
1975 1976 1977 1978

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

1979
    pci = virPCIDeviceNew(domain, bus, slot, function);
1980 1981 1982
    if (!pci)
        return -1;

1983 1984
    /* Check if device is assigned to an active guest */
    if ((domid = xenUnifiedNodeDeviceAssignedDomainId(dev)) >= 0) {
1985 1986 1987
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Device %s has been assigned to guest %d"),
                       dev->name, domid);
1988 1989 1990
        goto out;
    }

1991
    if (virPCIDeviceReattach(pci, NULL, NULL) < 0)
1992 1993 1994 1995
        goto out;

    ret = 0;
out:
1996
    virPCIDeviceFree(pci);
1997 1998 1999 2000
    return ret;
}

static int
2001
xenUnifiedNodeDeviceReset(virNodeDevicePtr dev)
2002
{
2003
    virPCIDevicePtr pci;
2004 2005 2006 2007 2008 2009
    unsigned domain, bus, slot, function;
    int ret = -1;

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

2010
    pci = virPCIDeviceNew(domain, bus, slot, function);
2011 2012 2013
    if (!pci)
        return -1;

2014
    if (virPCIDeviceReset(pci, NULL, NULL) < 0)
2015 2016 2017 2018
        goto out;

    ret = 0;
out:
2019
    virPCIDeviceFree(pci);
2020 2021 2022 2023
    return ret;
}


2024 2025
static int
xenUnifiedDomainOpenConsole(virDomainPtr dom,
2026
                            const char *dev_name,
2027 2028 2029 2030 2031 2032 2033 2034 2035 2036
                            virStreamPtr st,
                            unsigned int flags)
{
    virDomainDefPtr def = NULL;
    int ret = -1;
    virDomainChrDefPtr chr = NULL;

    virCheckFlags(0, -1);

    if (dom->id == -1) {
2037 2038
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("domain is not running"));
2039 2040 2041
        goto cleanup;
    }

2042
    if (dev_name) {
2043
        /* XXX support device aliases in future */
2044 2045
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Named device aliases are not supported"));
2046 2047 2048 2049 2050 2051 2052
        goto cleanup;
    }

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

2053 2054
    if (def->nconsoles)
        chr = def->consoles[0];
2055 2056 2057 2058
    else if (def->nserials)
        chr = def->serials[0];

    if (!chr) {
2059 2060
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot find default console device"));
2061 2062 2063
        goto cleanup;
    }

2064
    if (chr->source.type != VIR_DOMAIN_CHR_TYPE_PTY) {
2065 2066
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("character device %s is not using a PTY"), dev_name);
2067 2068 2069
        goto cleanup;
    }

2070
    if (virFDStreamOpenFile(st, chr->source.data.file.path,
E
Eric Blake 已提交
2071
                            0, 0, O_RDWR) < 0)
2072 2073 2074 2075 2076 2077 2078
        goto cleanup;

    ret = 0;
cleanup:
    virDomainDefFree(def);
    return ret;
}
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098

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

2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109

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 已提交
2110
/*----- Register with libvirt.c, and initialize Xen drivers. -----*/
2111 2112 2113

/* The interface which we export upwards to libvirt.c. */
static virDriver xenUnifiedDriver = {
2114 2115
    .no = VIR_DRV_XEN_UNIFIED,
    .name = "Xen",
2116 2117 2118 2119 2120
    .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 */
2121
    .connectGetHostname = xenUnifiedConnectGetHostname, /* 0.7.3 */
2122
    .connectGetMaxVcpus = xenUnifiedConnectGetMaxVcpus, /* 0.2.1 */
2123
    .nodeGetInfo = xenUnifiedNodeGetInfo, /* 0.1.0 */
2124 2125 2126
    .connectGetCapabilities = xenUnifiedConnectGetCapabilities, /* 0.2.1 */
    .connectListDomains = xenUnifiedConnectListDomains, /* 0.0.3 */
    .connectNumOfDomains = xenUnifiedConnectNumOfDomains, /* 0.0.3 */
2127 2128 2129 2130 2131 2132 2133
    .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 */
2134
    .domainShutdownFlags = xenUnifiedDomainShutdownFlags, /* 0.9.10 */
2135 2136
    .domainReboot = xenUnifiedDomainReboot, /* 0.1.0 */
    .domainDestroy = xenUnifiedDomainDestroy, /* 0.0.3 */
2137
    .domainDestroyFlags = xenUnifiedDomainDestroyFlags, /* 0.9.4 */
2138 2139 2140 2141 2142 2143 2144
    .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 */
2145
    .domainSaveFlags = xenUnifiedDomainSaveFlags, /* 0.9.4 */
2146 2147 2148
    .domainManagedSave = xenUnifiedDomainManagedSave, /* 1.0.1 */
    .domainHasManagedSaveImage = xenUnifiedDomainHasManagedSaveImage, /* 1.0.1 */
    .domainManagedSaveRemove = xenUnifiedDomainManagedSaveRemove, /* 1.0.1 */
2149
    .domainRestore = xenUnifiedDomainRestore, /* 0.0.3 */
2150
    .domainRestoreFlags = xenUnifiedDomainRestoreFlags, /* 0.9.4 */
2151 2152 2153 2154 2155 2156 2157 2158
    .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 */
2159 2160 2161 2162
    .connectDomainXMLFromNative = xenUnifiedConnectDomainXMLFromNative, /* 0.6.4 */
    .connectDomainXMLToNative = xenUnifiedConnectDomainXMLToNative, /* 0.6.4 */
    .connectListDefinedDomains = xenUnifiedConnectListDefinedDomains, /* 0.1.1 */
    .connectNumOfDefinedDomains = xenUnifiedConnectNumOfDefinedDomains, /* 0.1.5 */
2163 2164 2165 2166
    .domainCreate = xenUnifiedDomainCreate, /* 0.1.1 */
    .domainCreateWithFlags = xenUnifiedDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = xenUnifiedDomainDefineXML, /* 0.1.1 */
    .domainUndefine = xenUnifiedDomainUndefine, /* 0.1.1 */
2167
    .domainUndefineFlags = xenUnifiedDomainUndefineFlags, /* 0.9.4 */
2168 2169 2170 2171 2172 2173 2174 2175 2176
    .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 */
2177
    .domainGetSchedulerParametersFlags = xenUnifiedDomainGetSchedulerParametersFlags, /* 0.9.2 */
2178
    .domainSetSchedulerParameters = xenUnifiedDomainSetSchedulerParameters, /* 0.2.3 */
2179
    .domainSetSchedulerParametersFlags = xenUnifiedDomainSetSchedulerParametersFlags, /* 0.9.2 */
2180 2181 2182 2183 2184 2185 2186 2187
    .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 */
2188 2189
    .connectDomainEventRegister = xenUnifiedConnectDomainEventRegister, /* 0.5.0 */
    .connectDomainEventDeregister = xenUnifiedConnectDomainEventDeregister, /* 0.5.0 */
2190
    .nodeDeviceDettach = xenUnifiedNodeDeviceDettach, /* 0.6.1 */
2191
    .nodeDeviceDetachFlags = xenUnifiedNodeDeviceDetachFlags, /* 1.0.5 */
2192 2193
    .nodeDeviceReAttach = xenUnifiedNodeDeviceReAttach, /* 0.6.1 */
    .nodeDeviceReset = xenUnifiedNodeDeviceReset, /* 0.6.1 */
2194 2195
    .connectIsEncrypted = xenUnifiedConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = xenUnifiedConnectIsSecure, /* 0.7.3 */
2196 2197 2198
    .domainIsActive = xenUnifiedDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = xenUnifiedDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = xenUnifiedDomainIsUpdated, /* 0.8.6 */
2199 2200
    .connectDomainEventRegisterAny = xenUnifiedConnectDomainEventRegisterAny, /* 0.8.0 */
    .connectDomainEventDeregisterAny = xenUnifiedConnectDomainEventDeregisterAny, /* 0.8.0 */
2201
    .domainOpenConsole = xenUnifiedDomainOpenConsole, /* 0.8.6 */
2202
    .connectIsAlive = xenUnifiedConnectIsAlive, /* 0.9.8 */
2203
    .nodeSuspendForDuration = xenUnifiedNodeSuspendForDuration, /* 0.9.8 */
2204 2205
    .nodeGetMemoryParameters = xenUnifiedNodeGetMemoryParameters, /* 0.10.2 */
    .nodeSetMemoryParameters = xenUnifiedNodeSetMemoryParameters, /* 0.10.2 */
2206 2207
};

2208
/**
2209
 * xenRegister:
2210 2211 2212 2213 2214
 *
 * Register xen related drivers
 *
 * Returns the driver priority or -1 in case of error.
 */
2215
int
2216
xenRegister(void)
2217
{
2218
    if (virRegisterStateDriver(&state_driver) == -1) return -1;
J
John Levon 已提交
2219

2220
    return virRegisterDriver(&xenUnifiedDriver);
2221 2222
}

2223 2224 2225 2226 2227 2228 2229 2230 2231
/**
 * xenUnifiedDomainInfoListFree:
 *
 * Free the Domain Info List
 */
void
xenUnifiedDomainInfoListFree(xenUnifiedDomainInfoListPtr list)
{
    int i;
J
John Levon 已提交
2232 2233 2234 2235

    if (list == NULL)
        return;

2236 2237 2238 2239
    for (i=0; i<list->count; i++) {
        VIR_FREE(list->doms[i]->name);
        VIR_FREE(list->doms[i]);
    }
2240
    VIR_FREE(list->doms);
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262
    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)) {
2263
            VIR_DEBUG("WARNING: dom already tracked");
2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285
            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:
2286
    virReportOOMError();
2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306
    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++) {
2307
        if (list->doms[i]->id == id &&
2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331
            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;
}

2332

2333 2334
/**
 * xenUnifiedDomainEventDispatch:
D
Daniel P. Berrange 已提交
2335 2336
 * @priv: the connection to dispatch events on
 * @event: the event to dispatch
2337 2338 2339
 *
 * Dispatch domain events to registered callbacks
 *
D
Daniel P. Berrange 已提交
2340 2341
 * The caller must hold the lock in 'priv' before invoking
 *
2342
 */
2343
void xenUnifiedDomainEventDispatch(xenUnifiedPrivatePtr priv,
2344
                                    virDomainEventPtr event)
2345
{
D
Daniel P. Berrange 已提交
2346
    if (!priv)
2347
        return;
2348

2349
    virDomainEventStateQueue(priv->domainEvents, event);
D
Daniel P. Berrange 已提交
2350 2351 2352 2353 2354 2355 2356 2357 2358 2359
}

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

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