xen_xl.c 66.4 KB
Newer Older
1 2 3 4
/*
 * xen_xl.c: Xen XL parsing functions
 *
 * Copyright (c) 2015 SUSE LINUX Products GmbH, Nuernberg, Germany.
5
 * Copyright (C) 2014 David Kiarie Kahurani
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include <libxl.h>

#include "virconf.h"
#include "virerror.h"
28
#include "virlog.h"
29 30 31 32 33
#include "domain_conf.h"
#include "viralloc.h"
#include "virstring.h"
#include "virstoragefile.h"
#include "xen_xl.h"
J
Jim Fehlig 已提交
34
#include "libxl_capabilities.h"
35
#include "cpu/cpu.h"
36

37
#define VIR_FROM_THIS VIR_FROM_XENXL
38

39 40
VIR_LOG_INIT("xen.xen_xl");

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
/*
 * Xen provides a libxl utility library, with several useful functions,
 * specifically xlu_disk_parse for parsing xl disk config strings.
 * Although the libxlutil library is installed, until recently the
 * corresponding header file wasn't.  Use the header file if detected during
 * configure, otherwise provide extern declarations for any functions used.
 */
#ifdef HAVE_LIBXLUTIL_H
# include <libxlutil.h>
#else
typedef struct XLU_Config XLU_Config;

extern XLU_Config *xlu_cfg_init(FILE *report,
                                const char *report_filename);

extern void xlu_cfg_destroy(XLU_Config*);

extern int xlu_disk_parse(XLU_Config *cfg,
                          int nspecs,
                          const char *const *specs,
                          libxl_device_disk *disk);
#endif

64 65 66
static int xenParseCmdline(virConfPtr conf, char **r_cmdline)
{
    char *cmdline = NULL;
67 68 69
    VIR_AUTOFREE(char *) root = NULL;
    VIR_AUTOFREE(char *) extra = NULL;
    VIR_AUTOFREE(char *) buf = NULL;
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

    if (xenConfigGetString(conf, "cmdline", &buf, NULL) < 0)
        return -1;

    if (xenConfigGetString(conf, "root", &root, NULL) < 0)
        return -1;

    if (xenConfigGetString(conf, "extra", &extra, NULL) < 0)
        return -1;

    if (buf) {
        if (VIR_STRDUP(cmdline, buf) < 0)
            return -1;
        if (root || extra)
            VIR_WARN("ignoring root= and extra= in favour of cmdline=");
    } else {
        if (root && extra) {
            if (virAsprintf(&cmdline, "root=%s %s", root, extra) < 0)
                return -1;
        } else if (root) {
            if (virAsprintf(&cmdline, "root=%s", root) < 0)
                return -1;
        } else if (extra) {
            if (VIR_STRDUP(cmdline, extra) < 0)
                return -1;
        }
    }

    *r_cmdline = cmdline;
    return 0;
}

102
static int
103
xenParseXLOS(virConfPtr conf, virDomainDefPtr def, virCapsPtr caps)
104 105 106
{
    size_t i;

107
    if (def->os.type == VIR_DOMAIN_OSTYPE_HVM) {
108 109
        VIR_AUTOFREE(char *) bios = NULL;
        VIR_AUTOFREE(char *) boot = NULL;
110
        int val = 0;
111

J
Jim Fehlig 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
        if (xenConfigGetString(conf, "bios", &bios, NULL) < 0)
            return -1;

        if (bios && STREQ(bios, "ovmf")) {
            if (VIR_ALLOC(def->os.loader) < 0)
                return -1;

            def->os.loader->type = VIR_DOMAIN_LOADER_TYPE_PFLASH;
            def->os.loader->readonly = VIR_TRISTATE_BOOL_YES;

            if (VIR_STRDUP(def->os.loader->path,
                           LIBXL_FIRMWARE_DIR "/ovmf.bin") < 0)
                return -1;
        } else {
            for (i = 0; i < caps->nguests; i++) {
                if (caps->guests[i]->ostype == VIR_DOMAIN_OSTYPE_HVM &&
                    caps->guests[i]->arch.id == def->os.arch) {
                    if (VIR_ALLOC(def->os.loader) < 0 ||
                        VIR_STRDUP(def->os.loader->path,
                                   caps->guests[i]->arch.defaultInfo.loader) < 0)
                        return -1;
                }
134 135
            }
        }
136

137 138 139 140 141 142 143
#ifdef LIBXL_HAVE_BUILDINFO_KERNEL
        if (xenConfigCopyStringOpt(conf, "kernel", &def->os.kernel) < 0)
            return -1;

        if (xenConfigCopyStringOpt(conf, "ramdisk", &def->os.initrd) < 0)
            return -1;

144
        if (xenParseCmdline(conf, &def->os.cmdline) < 0)
145 146 147
            return -1;
#endif

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        if (xenConfigGetString(conf, "boot", &boot, "c") < 0)
            return -1;

        for (i = 0; i < VIR_DOMAIN_BOOT_LAST && boot[i]; i++) {
            switch (boot[i]) {
            case 'a':
                def->os.bootDevs[i] = VIR_DOMAIN_BOOT_FLOPPY;
                break;
            case 'd':
                def->os.bootDevs[i] = VIR_DOMAIN_BOOT_CDROM;
                break;
            case 'n':
                def->os.bootDevs[i] = VIR_DOMAIN_BOOT_NET;
                break;
            case 'c':
            default:
                def->os.bootDevs[i] = VIR_DOMAIN_BOOT_DISK;
                break;
            }
            def->os.nBootDevs++;
        }
169 170 171 172

        if (xenConfigGetBool(conf, "nestedhvm", &val, -1) < 0)
            return -1;

173 174
        if (val != -1) {
            const char *vtfeature = "vmx";
175 176 177 178 179 180 181 182

            if (caps && caps->host.cpu && ARCH_IS_X86(def->os.arch)) {
                if (virCPUCheckFeature(caps->host.arch, caps->host.cpu, "vmx"))
                    vtfeature = "vmx";
                else if (virCPUCheckFeature(caps->host.arch, caps->host.cpu, "svm"))
                    vtfeature = "svm";
            }

183
            if (!def->cpu) {
184 185 186 187 188 189
                virCPUDefPtr cpu;
                if (VIR_ALLOC(cpu) < 0)
                    return -1;

                cpu->mode = VIR_CPU_MODE_HOST_PASSTHROUGH;
                cpu->type = VIR_CPU_TYPE_GUEST;
190 191
                cpu->nfeatures = 0;
                cpu->nfeatures_max = 0;
192 193
                def->cpu = cpu;
            }
194 195 196 197 198 199 200

            if (val == 0) {
                if (virCPUDefAddFeature(def->cpu,
                                        vtfeature,
                                        VIR_CPU_FEATURE_DISABLE) < 0)
                    return -1;
            }
201
        }
202 203 204 205 206 207 208 209 210 211 212 213
    } else {
        if (xenConfigCopyStringOpt(conf, "bootloader", &def->os.bootloader) < 0)
            return -1;
        if (xenConfigCopyStringOpt(conf, "bootargs", &def->os.bootloaderArgs) < 0)
            return -1;

        if (xenConfigCopyStringOpt(conf, "kernel", &def->os.kernel) < 0)
            return -1;

        if (xenConfigCopyStringOpt(conf, "ramdisk", &def->os.initrd) < 0)
            return -1;

214
        if (xenParseCmdline(conf, &def->os.cmdline) < 0)
215 216 217 218 219 220
            return -1;
    }

    return 0;
}

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
/*
 * Translate CPU feature name from libvirt to libxl (from_libxl=false) or from
 * libxl to libvirt (from_libxl=true).
 */
const char *
xenTranslateCPUFeature(const char *feature_name, bool from_libxl)
{
    static const char *translation_table[][2] = {
        /* libvirt name, libxl name */
        { "cx16", "cmpxchg16" },
        { "cid", "cntxid" },
        { "ds_cpl", "dscpl" },
        { "pclmuldq", "pclmulqdq" },
        { "pni", "sse3" },
        { "ht", "htt" },
        { "pn", "psn" },
        { "clflush", "clfsh" },
        { "sep", "sysenter" },
        { "cx8", "cmpxchg8" },
        { "nodeid_msr", "nodeid" },
        { "cr8legacy", "altmovcr8" },
        { "lahf_lm", "lahfsahf" },
        { "cmp_legacy", "cmplegacy" },
        { "fxsr_opt", "ffxsr" },
        { "pdpe1gb", "page1gb" },
        { "spec-ctrl", "ibrsb" },
    };
    size_t i;

    for (i = 0; i < ARRAY_CARDINALITY(translation_table); i++)
        if (STREQ(translation_table[i][from_libxl], feature_name))
            return translation_table[i][!from_libxl];
    return feature_name;
}

256 257 258
static int
xenParseXLCPUID(virConfPtr conf, virDomainDefPtr def)
{
259
    VIR_AUTOFREE(char *) cpuid_str = NULL;
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
    char **cpuid_pairs = NULL;
    char **name_and_value = NULL;
    size_t i;
    int ret = -1;
    int policy;

    if (xenConfigGetString(conf, "cpuid", &cpuid_str, NULL) < 0)
        return -1;

    if (!cpuid_str)
        return 0;

    if (!def->cpu) {
        if (VIR_ALLOC(def->cpu) < 0)
            goto cleanup;
        def->cpu->mode = VIR_CPU_MODE_HOST_PASSTHROUGH;
        def->cpu->type = VIR_CPU_TYPE_GUEST;
        def->cpu->nfeatures = 0;
        def->cpu->nfeatures_max = 0;
    }

    cpuid_pairs = virStringSplit(cpuid_str, ",", 0);
    if (!cpuid_pairs)
        goto cleanup;

    if (!cpuid_pairs[0]) {
        ret = 0;
        goto cleanup;
    }

    if (STRNEQ(cpuid_pairs[0], "host")) {
        virReportError(VIR_ERR_CONF_SYNTAX,
                       _("cpuid starting with %s is not supported, only libxl format is"),
                       cpuid_pairs[0]);
        goto cleanup;
    }

    for (i = 1; cpuid_pairs[i]; i++) {
        name_and_value = virStringSplit(cpuid_pairs[i], "=", 2);
        if (!name_and_value)
            goto cleanup;
        if (!name_and_value[0] || !name_and_value[1]) {
            virReportError(VIR_ERR_CONF_SYNTAX,
                           _("Invalid libxl cpuid key=value element: %s"),
                           cpuid_pairs[i]);
            goto cleanup;
        }
        if (STREQ(name_and_value[1], "1")) {
            policy = VIR_CPU_FEATURE_FORCE;
        } else if (STREQ(name_and_value[1], "0")) {
            policy = VIR_CPU_FEATURE_DISABLE;
        } else if (STREQ(name_and_value[1], "x")) {
            policy = VIR_CPU_FEATURE_OPTIONAL;
        } else if (STREQ(name_and_value[1], "k")) {
            policy = VIR_CPU_FEATURE_OPTIONAL;
        } else if (STREQ(name_and_value[1], "s")) {
            policy = VIR_CPU_FEATURE_OPTIONAL;
        } else {
            virReportError(VIR_ERR_CONF_SYNTAX,
                           _("Invalid libxl cpuid value: %s"),
                           cpuid_pairs[i]);
            goto cleanup;
        }

        if (virCPUDefAddFeature(def->cpu,
                                xenTranslateCPUFeature(name_and_value[0], true),
                                policy) < 0)
            goto cleanup;

        virStringListFree(name_and_value);
        name_and_value = NULL;
    }

    ret = 0;

 cleanup:
    virStringListFree(name_and_value);
    virStringListFree(cpuid_pairs);
    return ret;
}

341

342 343 344 345 346 347 348 349
static int
xenParseXLSpice(virConfPtr conf, virDomainDefPtr def)
{
    virDomainGraphicsDefPtr graphics = NULL;
    unsigned long port;
    char *listenAddr = NULL;
    int val;

350
    if (def->os.type == VIR_DOMAIN_OSTYPE_HVM) {
351 352 353 354 355 356 357 358 359 360
        if (xenConfigGetBool(conf, "spice", &val, 0) < 0)
            return -1;

        if (val) {
            if (VIR_ALLOC(graphics) < 0)
                return -1;

            graphics->type = VIR_DOMAIN_GRAPHICS_TYPE_SPICE;
            if (xenConfigCopyStringOpt(conf, "spicehost", &listenAddr) < 0)
                goto cleanup;
361
            if (virDomainGraphicsListenAppendAddress(graphics, listenAddr) < 0)
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
                goto cleanup;
            VIR_FREE(listenAddr);

            if (xenConfigGetULong(conf, "spicetls_port", &port, 0) < 0)
                goto cleanup;
            graphics->data.spice.tlsPort = (int)port;

            if (xenConfigGetULong(conf, "spiceport", &port, 0) < 0)
                goto cleanup;

            graphics->data.spice.port = (int)port;

            if (!graphics->data.spice.tlsPort &&
                !graphics->data.spice.port)
            graphics->data.spice.autoport = 1;

            if (xenConfigGetBool(conf, "spicedisable_ticketing", &val, 0) < 0)
                goto cleanup;
J
Jim Fehlig 已提交
380 381 382
            if (!val) {
                if (xenConfigCopyString(conf, "spicepasswd",
                                        &graphics->data.spice.auth.passwd) < 0)
383 384 385 386
                    goto cleanup;
            }

            if (xenConfigGetBool(conf, "spiceagent_mouse",
387
                                 &val, 0) < 0)
388 389
                goto cleanup;
            if (val) {
390 391 392 393 394
                graphics->data.spice.mousemode =
                    VIR_DOMAIN_GRAPHICS_SPICE_MOUSE_MODE_CLIENT;
            } else {
                graphics->data.spice.mousemode =
                    VIR_DOMAIN_GRAPHICS_SPICE_MOUSE_MODE_SERVER;
395 396
            }

397 398 399 400 401 402 403
            if (xenConfigGetBool(conf, "spice_clipboard_sharing", &val, 0) < 0)
                goto cleanup;
            if (val)
                graphics->data.spice.copypaste = VIR_TRISTATE_BOOL_YES;
            else
                graphics->data.spice.copypaste = VIR_TRISTATE_BOOL_NO;

404 405 406 407 408 409 410 411 412 413
            if (VIR_ALLOC_N(def->graphics, 1) < 0)
                goto cleanup;
            def->graphics[0] = graphics;
            def->ngraphics = 1;
        }
    }

    return 0;

 cleanup:
J
John Ferlan 已提交
414
    VIR_FREE(listenAddr);
415 416 417 418
    virDomainGraphicsDefFree(graphics);
    return -1;
}

419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
#ifdef LIBXL_HAVE_VNUMA
static int
xenParseXLVnuma(virConfPtr conf,
                virDomainDefPtr def)
{
    int ret = -1;
    char *tmp = NULL;
    char **token = NULL;
    size_t vcpus = 0;
    size_t nr_nodes = 0;
    size_t vnodeCnt = 0;
    virCPUDefPtr cpu = NULL;
    virConfValuePtr list;
    virConfValuePtr vnode;
    virDomainNumaPtr numa;

    numa = def->numa;
    if (numa == NULL)
        return -1;

    list = virConfGetValue(conf, "vnuma");
    if (!list || list->type != VIR_CONF_LIST)
        return 0;

    vnode = list->list;
    while (vnode && vnode->type == VIR_CONF_LIST) {
        vnode = vnode->next;
        nr_nodes++;
    }

    if (!virDomainNumaSetNodeCount(numa, nr_nodes))
        goto cleanup;

    if (VIR_ALLOC(cpu) < 0)
        goto cleanup;

    list = list->list;
    while (list) {
        int pnode = -1;
        virBitmapPtr cpumask = NULL;
        unsigned long long kbsize = 0;

        /* Is there a sublist (vnode)? */
        if (list && list->type == VIR_CONF_LIST) {
            vnode = list->list;

            while (vnode && vnode->type == VIR_CONF_STRING) {
                const char *data;
                const char *str = vnode->str;

                if (!str ||
                   !(data = strrchr(str, '='))) {
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("vnuma vnode invalid format '%s'"),
                                   str);
                    goto cleanup;
                }
                data++;

                if (*data) {
                    char vtoken[64];

                    if (STRPREFIX(str, "pnode")) {
                        unsigned int cellid;

484
                        if (virStrcpyStatic(vtoken, data) < 0) {
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
                            virReportError(VIR_ERR_INTERNAL_ERROR,
                                           _("vnuma vnode %zu pnode '%s' too long for destination"),
                                           vnodeCnt, data);
                            goto cleanup;
                        }

                        if ((virStrToLong_ui(vtoken, NULL, 10, &cellid) < 0) ||
                            (cellid >= nr_nodes)) {
                            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                           _("vnuma vnode %zu contains invalid pnode value '%s'"),
                                           vnodeCnt, data);
                            goto cleanup;
                        }
                        pnode = cellid;
                    } else if (STRPREFIX(str, "size")) {
500
                        if (virStrcpyStatic(vtoken, data) < 0) {
501 502 503 504 505 506 507 508 509 510 511 512
                            virReportError(VIR_ERR_INTERNAL_ERROR,
                                           _("vnuma vnode %zu size '%s' too long for destination"),
                                           vnodeCnt, data);
                            goto cleanup;
                        }

                        if (virStrToLong_ull(vtoken, NULL, 10, &kbsize) < 0)
                            goto cleanup;

                        virDomainNumaSetNodeMemorySize(numa, vnodeCnt, (kbsize * 1024));

                    } else if (STRPREFIX(str, "vcpus")) {
513
                        if (virStrcpyStatic(vtoken, data) < 0) {
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
                            virReportError(VIR_ERR_INTERNAL_ERROR,
                                           _("vnuma vnode %zu vcpus '%s' too long for destination"),
                                           vnodeCnt, data);
                            goto cleanup;
                        }

                        if ((virBitmapParse(vtoken, &cpumask, VIR_DOMAIN_CPUMASK_LEN) < 0) ||
                            (virDomainNumaSetNodeCpumask(numa, vnodeCnt, cpumask) == NULL))
                            goto cleanup;

                        vcpus += virBitmapCountBits(cpumask);

                    } else if (STRPREFIX(str, "vdistances")) {
                        size_t i, ndistances;
                        unsigned int value;

530
                        if (virStrcpyStatic(vtoken, data) < 0) {
531 532 533 534 535 536
                            virReportError(VIR_ERR_INTERNAL_ERROR,
                                           _("vnuma vnode %zu vdistances '%s' too long for destination"),
                                           vnodeCnt, data);
                            goto cleanup;
                        }

537
                        VIR_FREE(tmp);
538 539 540
                        if (VIR_STRDUP(tmp, vtoken) < 0)
                            goto cleanup;

541
                        virStringListFree(token);
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
                        if (!(token = virStringSplitCount(tmp, ",", 0, &ndistances)))
                            goto cleanup;

                        if (ndistances != nr_nodes) {
                            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                       _("vnuma pnode %d configured '%s' (count %zu) doesn't fit the number of specified vnodes %zu"),
                                       pnode, str, ndistances, nr_nodes);
                            goto cleanup;
                        }

                        if (virDomainNumaSetNodeDistanceCount(numa, vnodeCnt, ndistances) != ndistances)
                            goto cleanup;

                        for (i = 0; i < ndistances; i++) {
                            if ((virStrToLong_ui(token[i], NULL, 10, &value) < 0) ||
                                (virDomainNumaSetNodeDistance(numa, vnodeCnt, i, value) != value))
                                goto cleanup;
                        }

                    } else {
                        virReportError(VIR_ERR_CONF_SYNTAX,
                                       _("Invalid vnuma configuration for vnode %zu"),
                                       vnodeCnt);
                        goto cleanup;
                    }
                }
                vnode = vnode->next;
            }
        }

        if ((pnode < 0) ||
            (cpumask == NULL) ||
            (kbsize == 0)) {
            virReportError(VIR_ERR_CONF_SYNTAX,
                           _("Incomplete vnuma configuration for vnode %zu"),
                           vnodeCnt);
            goto cleanup;
        }

        list = list->next;
        vnodeCnt++;
    }

    if (def->maxvcpus == 0)
        def->maxvcpus = vcpus;

    if (def->maxvcpus < vcpus) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("vnuma configuration contains %zu vcpus, which is greater than %zu maxvcpus"),
                       vcpus, def->maxvcpus);
        goto cleanup;
    }

    cpu->type = VIR_CPU_TYPE_GUEST;
    def->cpu = cpu;

    ret = 0;

 cleanup:
    if (ret)
        VIR_FREE(cpu);
    virStringListFree(token);
    VIR_FREE(tmp);

    return ret;
}
#endif
609

610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
#ifdef LIBXL_HAVE_BUILDINFO_GRANT_LIMITS
static int
xenParseXLGntLimits(virConfPtr conf, virDomainDefPtr def)
{
    unsigned long max_gntframes;
    int ctlr_idx;
    virDomainControllerDefPtr xenbus_ctlr;

    if (xenConfigGetULong(conf, "max_grant_frames", &max_gntframes, 0) < 0)
        return -1;

    if (max_gntframes <= 0)
        return 0;

    ctlr_idx = virDomainControllerFindByType(def, VIR_DOMAIN_CONTROLLER_TYPE_XENBUS);
    if (ctlr_idx == -1)
        xenbus_ctlr = virDomainDefAddController(def, VIR_DOMAIN_CONTROLLER_TYPE_XENBUS, -1, -1);
    else
        xenbus_ctlr = def->controllers[ctlr_idx];

    if (xenbus_ctlr == NULL)
        return -1;

    xenbus_ctlr->opts.xenbusopts.maxGrantFrames = max_gntframes;
    return 0;
}
#endif

638 639 640 641 642 643
static int
xenParseXLDiskSrc(virDomainDiskDefPtr disk, char *srcstr)
{
    char *tmpstr = NULL;
    int ret = -1;

644 645 646 647
    /* A NULL source is valid, e.g. an empty CDROM */
    if (srcstr == NULL)
        return 0;

648
    if (STRPREFIX(srcstr, "rbd:")) {
649 650
        if (!(tmpstr = virStringReplace(srcstr, "\\\\", "\\")))
            goto cleanup;
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667

        virDomainDiskSetType(disk, VIR_STORAGE_TYPE_NETWORK);
        disk->src->protocol = VIR_STORAGE_NET_PROTOCOL_RBD;
        ret = virStorageSourceParseRBDColonString(tmpstr, disk->src);
    } else {
        if (virDomainDiskSetSource(disk, srcstr) < 0)
            goto cleanup;

        ret = 0;
    }

 cleanup:
    VIR_FREE(tmpstr);
    return ret;
}


668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
/*
 * For details on xl disk config syntax, see
 * docs/misc/xl-disk-configuration.txt in the Xen sources.  The important
 * section of text is:
 *
 *   More formally, the string is a series of comma-separated keyword/value
 *   pairs, flags and positional parameters.  Parameters which are not bare
 *   keywords and which do not contain "=" symbols are assigned to the
 *   so-far-unspecified positional parameters, in the order below.  The
 *   positional parameters may also be specified explicitly by name.
 *
 *   Each parameter may be specified at most once, either as a positional
 *   parameter or a named parameter.  Default values apply if the parameter
 *   is not specified, or if it is specified with an empty value (whether
 *   positionally or explicitly).
 *
 *   Whitespace may appear before each parameter and will be ignored.
 *
 * The order of the positional parameters mentioned in the quoted text is:
 *
 *   target,format,vdev,access
 *
 * The following options must be specified by key=value:
 *
 *   devtype=<devtype>
 *   backendtype=<backend-type>
 *
 * The following options are currently not supported:
 *
 *   backend=<domain-name>
 *   script=<script>
 *   direct-io-safe
 *
 */
static int
xenParseXLDisk(virConfPtr conf, virDomainDefPtr def)
{
    int ret = -1;
    virConfValuePtr list = virConfGetValue(conf, "disk");
    XLU_Config *xluconf;
    libxl_device_disk *libxldisk;
    virDomainDiskDefPtr disk = NULL;

    if (VIR_ALLOC(libxldisk) < 0)
        return -1;

    if (!(xluconf = xlu_cfg_init(stderr, "command line")))
        goto cleanup;

    if (list && list->type == VIR_CONF_LIST) {
        list = list->list;
        while (list) {
            const char *disk_spec = list->str;

            if (list->type != VIR_CONF_STRING || list->str == NULL)
                goto skipdisk;

            libxl_device_disk_init(libxldisk);

            if (xlu_disk_parse(xluconf, 1, &disk_spec, libxldisk))
                goto fail;

730
            if (!(disk = virDomainDiskDefNew(NULL)))
731 732
                goto fail;

733
            if (xenParseXLDiskSrc(disk, libxldisk->pdev_path) < 0)
734 735
                goto fail;

736
            if (VIR_STRDUP(disk->dst, libxldisk->vdev) < 0)
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
                goto fail;

            disk->src->readonly = !libxldisk->readwrite;
            disk->removable = libxldisk->removable;

            if (libxldisk->is_cdrom) {
                if (virDomainDiskSetDriver(disk, "qemu") < 0)
                    goto fail;

                virDomainDiskSetType(disk, VIR_STORAGE_TYPE_FILE);
                disk->device = VIR_DOMAIN_DISK_DEVICE_CDROM;
                if (!disk->src->path || STREQ(disk->src->path, ""))
                    disk->src->format = VIR_STORAGE_FILE_NONE;
                else
                    disk->src->format = VIR_STORAGE_FILE_RAW;
            } else {
                switch (libxldisk->format) {
                case LIBXL_DISK_FORMAT_QCOW:
                    disk->src->format = VIR_STORAGE_FILE_QCOW;
                    break;

                case LIBXL_DISK_FORMAT_QCOW2:
                    disk->src->format = VIR_STORAGE_FILE_QCOW2;
                    break;

                case LIBXL_DISK_FORMAT_VHD:
                    disk->src->format = VIR_STORAGE_FILE_VHD;
                    break;

                case LIBXL_DISK_FORMAT_RAW:
                case LIBXL_DISK_FORMAT_UNKNOWN:
                    disk->src->format = VIR_STORAGE_FILE_RAW;
                    break;

                case LIBXL_DISK_FORMAT_EMPTY:
                    break;
773

774 775 776 777 778 779
#ifdef LIBXL_HAVE_QED
                case LIBXL_DISK_FORMAT_QED:
                    disk->src->format = VIR_STORAGE_FILE_QED;
                    break;
#endif

780 781 782 783 784
                default:
                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                   _("disk image format not supported: %s"),
                                   libxl_disk_format_to_string(libxldisk->format));
                    goto fail;
785 786 787 788 789 790 791
                }

                switch (libxldisk->backend) {
                case LIBXL_DISK_BACKEND_QDISK:
                case LIBXL_DISK_BACKEND_UNKNOWN:
                    if (virDomainDiskSetDriver(disk, "qemu") < 0)
                        goto fail;
792 793
                    if (virDomainDiskGetType(disk) == VIR_STORAGE_TYPE_NONE)
                        virDomainDiskSetType(disk, VIR_STORAGE_TYPE_FILE);
794 795 796 797 798 799 800 801 802 803 804 805 806
                    break;

                case LIBXL_DISK_BACKEND_TAP:
                    if (virDomainDiskSetDriver(disk, "tap") < 0)
                        goto fail;
                    virDomainDiskSetType(disk, VIR_STORAGE_TYPE_FILE);
                    break;

                case LIBXL_DISK_BACKEND_PHY:
                    if (virDomainDiskSetDriver(disk, "phy") < 0)
                        goto fail;
                    virDomainDiskSetType(disk, VIR_STORAGE_TYPE_BLOCK);
                    break;
807 808 809 810 811
                default:
                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                   _("disk backend not supported: %s"),
                                   libxl_disk_backend_to_string(libxldisk->backend));
                    goto fail;
812 813 814
                }
            }

815 816
            if (STRPREFIX(libxldisk->vdev, "xvd") ||
                def->os.type != VIR_DOMAIN_OSTYPE_HVM)
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
                disk->bus = VIR_DOMAIN_DISK_BUS_XEN;
            else if (STRPREFIX(libxldisk->vdev, "sd"))
                disk->bus = VIR_DOMAIN_DISK_BUS_SCSI;
            else
                disk->bus = VIR_DOMAIN_DISK_BUS_IDE;

            if (VIR_APPEND_ELEMENT(def->disks, def->ndisks, disk) < 0)
                goto fail;

            libxl_device_disk_dispose(libxldisk);

        skipdisk:
            list = list->next;
        }
    }
    ret = 0;

 cleanup:
    virDomainDiskDefFree(disk);
    xlu_cfg_destroy(xluconf);
    VIR_FREE(libxldisk);
    return ret;

 fail:
    libxl_device_disk_dispose(libxldisk);
    goto cleanup;
}

845 846 847 848 849 850
static int
xenParseXLInputDevs(virConfPtr conf, virDomainDefPtr def)
{
    const char *str;
    virConfValuePtr val;

851
    if (def->os.type == VIR_DOMAIN_OSTYPE_HVM) {
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
        val = virConfGetValue(conf, "usbdevice");
        /* usbdevice can be defined as either a single string or a list */
        if (val && val->type == VIR_CONF_LIST) {
#ifdef LIBXL_HAVE_BUILDINFO_USBDEVICE_LIST
            val = val->list;
#else
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("multiple USB devices not supported"));
            return -1;
#endif
        }
        /* otherwise val->next is NULL, so can be handled by the same code */
        while (val) {
            if (val->type != VIR_CONF_STRING) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("config value %s was malformed"),
                               "usbdevice");
                return -1;
            }
            str = val->str;

            if (str &&
                    (STREQ(str, "tablet") ||
                     STREQ(str, "mouse") ||
                     STREQ(str, "keyboard"))) {
                virDomainInputDefPtr input;
                if (VIR_ALLOC(input) < 0)
                    return -1;

                input->bus = VIR_DOMAIN_INPUT_BUS_USB;
                if (STREQ(str, "mouse"))
                    input->type = VIR_DOMAIN_INPUT_TYPE_MOUSE;
                else if (STREQ(str, "tablet"))
                    input->type = VIR_DOMAIN_INPUT_TYPE_TABLET;
                else if (STREQ(str, "keyboard"))
                    input->type = VIR_DOMAIN_INPUT_TYPE_KBD;
                if (VIR_APPEND_ELEMENT(def->inputs, def->ninputs, input) < 0) {
                    virDomainInputDefFree(input);
                    return -1;
                }
            }
            val = val->next;
        }
    }
    return 0;
}
898

899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
static int
xenParseXLUSBController(virConfPtr conf, virDomainDefPtr def)
{
    virConfValuePtr list = virConfGetValue(conf, "usbctrl");
    virDomainControllerDefPtr controller = NULL;

    if (list && list->type == VIR_CONF_LIST) {
        list = list->list;
        while (list) {
            char type[8];
            char version[4];
            char ports[4];
            char *key;
            int usbctrl_version = 2; /* by default USB 2.0 */
            int usbctrl_ports = 8; /* by default 8 ports */
            int usbctrl_type = -1;

            type[0] = version[0] = ports[0] = '\0';

            if ((list->type != VIR_CONF_STRING) || (list->str == NULL))
                goto skipusbctrl;
            /* usbctrl=['type=pv,version=2,ports=8'] */
            key = list->str;
            while (key) {
                char *data;
                char *nextkey = strchr(key, ',');

                if (!(data = strchr(key, '=')))
                    goto skipusbctrl;
                data++;

                if (STRPREFIX(key, "type=")) {
931
                    int len = nextkey ? (nextkey - data) : strlen(data);
932
                    if (virStrncpy(type, data, len, sizeof(type)) < 0) {
933 934 935 936 937 938
                        virReportError(VIR_ERR_INTERNAL_ERROR,
                                       _("type %s invalid"),
                                       data);
                        goto skipusbctrl;
                    }
                } else if (STRPREFIX(key, "version=")) {
939
                    int len = nextkey ? (nextkey - data) : strlen(data);
940
                    if (virStrncpy(version, data, len, sizeof(version)) < 0) {
941 942 943 944 945 946 947 948
                        virReportError(VIR_ERR_INTERNAL_ERROR,
                                       _("version %s invalid"),
                                       data);
                        goto skipusbctrl;
                    }
                    if (virStrToLong_i(version, NULL, 16, &usbctrl_version) < 0)
                        goto skipusbctrl;
                } else if (STRPREFIX(key, "ports=")) {
949
                    int len = nextkey ? (nextkey - data) : strlen(data);
950
                    if (virStrncpy(ports, data, len, sizeof(ports)) < 0) {
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
                        virReportError(VIR_ERR_INTERNAL_ERROR,
                                       _("version %s invalid"),
                                       data);
                        goto skipusbctrl;
                    }
                    if (virStrToLong_i(ports, NULL, 16, &usbctrl_ports) < 0)
                        goto skipusbctrl;
                }

                while (nextkey && (nextkey[0] == ',' ||
                                   nextkey[0] == ' ' ||
                                   nextkey[0] == '\t'))
                    nextkey++;
                key = nextkey;
            }

            if (type[0] == '\0') {
                if (usbctrl_version == 1)
                    usbctrl_type = VIR_DOMAIN_CONTROLLER_MODEL_USB_QUSB1;
                else
                    usbctrl_type = VIR_DOMAIN_CONTROLLER_MODEL_USB_QUSB2;
            } else {
                if (STREQLEN(type, "qusb", 4)) {
                    if (usbctrl_version == 1)
                        usbctrl_type = VIR_DOMAIN_CONTROLLER_MODEL_USB_QUSB1;
                    else
                        usbctrl_type = VIR_DOMAIN_CONTROLLER_MODEL_USB_QUSB2;
                } else {
                    goto skipusbctrl;
                }
            }

            if (!(controller = virDomainControllerDefNew(VIR_DOMAIN_CONTROLLER_TYPE_USB)))
                return -1;

            controller->type = VIR_DOMAIN_CONTROLLER_TYPE_USB;
            controller->model = usbctrl_type;
            controller->opts.usbopts.ports = usbctrl_ports;

            if (VIR_APPEND_ELEMENT(def->controllers, def->ncontrollers, controller) < 0) {
                virDomainControllerDefFree(controller);
                return -1;
            }

        skipusbctrl:
            list = list->next;
        }
    }

    return 0;
}

1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
static int
xenParseXLUSB(virConfPtr conf, virDomainDefPtr def)
{
    virConfValuePtr list = virConfGetValue(conf, "usbdev");
    virDomainHostdevDefPtr hostdev = NULL;

    if (list && list->type == VIR_CONF_LIST) {
        list = list->list;
        while (list) {
            char bus[3];
            char device[3];
            char *key;
            int busNum;
            int devNum;

            bus[0] = device[0] = '\0';

            if ((list->type != VIR_CONF_STRING) || (list->str == NULL))
                goto skipusb;
            /* usbdev=['hostbus=1,hostaddr=3'] */
            key = list->str;
            while (key) {
                char *data;
                char *nextkey = strchr(key, ',');

                if (!(data = strchr(key, '=')))
                    goto skipusb;
                data++;

                if (STRPREFIX(key, "hostbus=")) {
1033
                    int len = nextkey ? (nextkey - data) : strlen(data);
1034
                    if (virStrncpy(bus, data, len, sizeof(bus)) < 0) {
1035 1036 1037 1038 1039 1040
                        virReportError(VIR_ERR_INTERNAL_ERROR,
                                       _("bus %s too big for destination"),
                                       data);
                        goto skipusb;
                    }
                } else if (STRPREFIX(key, "hostaddr=")) {
1041
                    int len = nextkey ? (nextkey - data) : strlen(data);
1042
                    if (virStrncpy(device, data, len, sizeof(device)) < 0) {
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
                        virReportError(VIR_ERR_INTERNAL_ERROR,
                                       _("device %s too big for destination"),
                                       data);
                        goto skipusb;
                    }
                }

                while (nextkey && (nextkey[0] == ',' ||
                                   nextkey[0] == ' ' ||
                                   nextkey[0] == '\t'))
                    nextkey++;
                key = nextkey;
            }

            if (virStrToLong_i(bus, NULL, 16, &busNum) < 0)
                goto skipusb;
            if (virStrToLong_i(device, NULL, 16, &devNum) < 0)
                goto skipusb;
J
John Ferlan 已提交
1061
            if (!(hostdev = virDomainHostdevDefNew()))
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
               return -1;

            hostdev->managed = false;
            hostdev->source.subsys.type = VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB;
            hostdev->source.subsys.u.usb.bus = busNum;
            hostdev->source.subsys.u.usb.device = devNum;

            if (VIR_APPEND_ELEMENT(def->hostdevs, def->nhostdevs, hostdev) < 0) {
                virDomainHostdevDefFree(hostdev);
                return -1;
            }

        skipusb:
            list = list->next;
        }
    }

    return 0;
}

1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
static int
xenParseXLChannel(virConfPtr conf, virDomainDefPtr def)
{
    virConfValuePtr list = virConfGetValue(conf, "channel");
    virDomainChrDefPtr channel = NULL;
    char *name = NULL;
    char *path = NULL;

    if (list && list->type == VIR_CONF_LIST) {
        list = list->list;
        while (list) {
            char type[10];
            char *key;

            if ((list->type != VIR_CONF_STRING) || (list->str == NULL))
                goto skipchannel;

            key = list->str;
            while (key) {
                char *data;
                char *nextkey = strchr(key, ',');

                if (!(data = strchr(key, '=')))
                    goto skipchannel;
                data++;

                if (STRPREFIX(key, "connection=")) {
1109
                    int len = nextkey ? (nextkey - data) : strlen(data);
1110
                    if (virStrncpy(type, data, len, sizeof(type)) < 0) {
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
                        virReportError(VIR_ERR_INTERNAL_ERROR,
                                       _("connection %s too big"), data);
                        goto skipchannel;
                    }
                } else if (STRPREFIX(key, "name=")) {
                    int len = nextkey ? (nextkey - data) : strlen(data);
                    VIR_FREE(name);
                    if (VIR_STRNDUP(name, data, len) < 0)
                        goto cleanup;
                } else if (STRPREFIX(key, "path=")) {
                    int len = nextkey ? (nextkey - data) : strlen(data);
                    VIR_FREE(path);
                    if (VIR_STRNDUP(path, data, len) < 0)
                        goto cleanup;
                }

                while (nextkey && (nextkey[0] == ',' ||
                                   nextkey[0] == ' ' ||
                                   nextkey[0] == '\t'))
                    nextkey++;
                key = nextkey;
            }

1134
            if (!(channel = virDomainChrDefNew(NULL)))
1135 1136 1137
                goto cleanup;

            if (STRPREFIX(type, "socket")) {
1138 1139
                channel->source->type = VIR_DOMAIN_CHR_TYPE_UNIX;
                channel->source->data.nix.listen = 1;
1140 1141
                channel->source->data.nix.path = path;
                path = NULL;
1142
            } else if (STRPREFIX(type, "pty")) {
1143
                channel->source->type = VIR_DOMAIN_CHR_TYPE_PTY;
1144 1145 1146 1147 1148 1149 1150 1151
                VIR_FREE(path);
            } else {
                goto cleanup;
            }

            channel->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_CHANNEL;
            channel->targetType = VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_XEN;
            channel->target.name = name;
1152
            name = NULL;
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170

            if (VIR_APPEND_ELEMENT(def->channels, def->nchannels, channel) < 0)
                goto cleanup;

        skipchannel:
            list = list->next;
        }
    }

    return 0;

 cleanup:
    virDomainChrDefFree(channel);
    VIR_FREE(path);
    VIR_FREE(name);
    return -1;
}

1171
virDomainDefPtr
1172 1173
xenParseXL(virConfPtr conf,
           virCapsPtr caps,
1174
           virDomainXMLOptionPtr xmlopt)
1175 1176 1177
{
    virDomainDefPtr def = NULL;

1178
    if (!(def = virDomainDefNew()))
1179 1180 1181 1182 1183
        return NULL;

    def->virtType = VIR_DOMAIN_VIRT_XEN;
    def->id = -1;

1184 1185
    if (xenParseConfigCommon(conf, def, caps, XEN_CONFIG_FORMAT_XL,
                             xmlopt) < 0)
1186 1187
        goto cleanup;

1188
    if (xenParseXLOS(conf, def, caps) < 0)
1189 1190
        goto cleanup;

1191 1192 1193 1194 1195
#ifdef LIBXL_HAVE_VNUMA
    if (xenParseXLVnuma(conf, def) < 0)
        goto cleanup;
#endif

1196 1197 1198 1199 1200
#ifdef LIBXL_HAVE_BUILDINFO_GRANT_LIMITS
    if (xenParseXLGntLimits(conf, def) < 0)
        goto cleanup;
#endif

1201 1202 1203
    if (xenParseXLCPUID(conf, def) < 0)
        goto cleanup;

1204 1205 1206 1207 1208 1209
    if (xenParseXLDisk(conf, def) < 0)
        goto cleanup;

    if (xenParseXLSpice(conf, def) < 0)
        goto cleanup;

1210 1211 1212
    if (xenParseXLInputDevs(conf, def) < 0)
        goto cleanup;

1213 1214 1215
    if (xenParseXLUSB(conf, def) < 0)
        goto cleanup;

1216 1217 1218
    if (xenParseXLUSBController(conf, def) < 0)
        goto cleanup;

1219 1220 1221
    if (xenParseXLChannel(conf, def) < 0)
        goto cleanup;

1222
    if (virDomainDefPostParse(def, caps, VIR_DOMAIN_DEF_PARSE_ABI_UPDATE,
1223
                              xmlopt, NULL) < 0)
1224 1225
        goto cleanup;

1226 1227 1228 1229 1230 1231 1232 1233
    return def;

 cleanup:
    virDomainDefFree(def);
    return NULL;
}


1234 1235 1236 1237 1238
static int
xenFormatXLOS(virConfPtr conf, virDomainDefPtr def)
{
    size_t i;

1239
    if (def->os.type == VIR_DOMAIN_OSTYPE_HVM) {
1240 1241 1242 1243
        char boot[VIR_DOMAIN_BOOT_LAST+1];
        if (xenConfigSetString(conf, "builder", "hvm") < 0)
            return -1;

J
Jim Fehlig 已提交
1244 1245 1246 1247 1248 1249
        if (def->os.loader &&
            def->os.loader->type == VIR_DOMAIN_LOADER_TYPE_PFLASH) {
            if (xenConfigSetString(conf, "bios", "ovmf") < 0)
                return -1;
        }

1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
#ifdef LIBXL_HAVE_BUILDINFO_KERNEL
        if (def->os.kernel &&
            xenConfigSetString(conf, "kernel", def->os.kernel) < 0)
            return -1;

        if (def->os.initrd &&
            xenConfigSetString(conf, "ramdisk", def->os.initrd) < 0)
            return -1;

        if (def->os.cmdline &&
1260
            xenConfigSetString(conf, "cmdline", def->os.cmdline) < 0)
1261 1262 1263
            return -1;
#endif

1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
        for (i = 0; i < def->os.nBootDevs; i++) {
            switch (def->os.bootDevs[i]) {
            case VIR_DOMAIN_BOOT_FLOPPY:
                boot[i] = 'a';
                break;
            case VIR_DOMAIN_BOOT_CDROM:
                boot[i] = 'd';
                break;
            case VIR_DOMAIN_BOOT_NET:
                boot[i] = 'n';
                break;
            case VIR_DOMAIN_BOOT_DISK:
            default:
                boot[i] = 'c';
                break;
            }
        }

        if (!def->os.nBootDevs) {
            boot[0] = 'c';
            boot[1] = '\0';
        } else {
            boot[def->os.nBootDevs] = '\0';
        }

        if (xenConfigSetString(conf, "boot", boot) < 0)
            return -1;

1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
        if (def->cpu &&
            def->cpu->mode == VIR_CPU_MODE_HOST_PASSTHROUGH) {
            bool hasHwVirt = true;

            if (def->cpu->nfeatures) {
                for (i = 0; i < def->cpu->nfeatures; i++) {

                    switch (def->cpu->features[i].policy) {
                        case VIR_CPU_FEATURE_DISABLE:
                        case VIR_CPU_FEATURE_FORBID:
                            if (STREQ(def->cpu->features[i].name, "vmx") ||
                                STREQ(def->cpu->features[i].name, "svm"))
                                hasHwVirt = false;
                            break;

                        case VIR_CPU_FEATURE_FORCE:
                        case VIR_CPU_FEATURE_REQUIRE:
                        case VIR_CPU_FEATURE_OPTIONAL:
                        case VIR_CPU_FEATURE_LAST:
                            break;
                    }
                }
            }

            if (xenConfigSetInt(conf, "nestedhvm", hasHwVirt) < 0)
                return -1;
        }

1320 1321
        /* XXX floppy disks */
    } else {
1322 1323 1324 1325 1326
        if (def->os.type == VIR_DOMAIN_OSTYPE_XENPVH) {
            if (xenConfigSetString(conf, "type", "pvh") < 0)
                return -1;
        }

1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
        if (def->os.bootloader &&
             xenConfigSetString(conf, "bootloader", def->os.bootloader) < 0)
            return -1;

         if (def->os.bootloaderArgs &&
             xenConfigSetString(conf, "bootargs", def->os.bootloaderArgs) < 0)
            return -1;

         if (def->os.kernel &&
             xenConfigSetString(conf, "kernel", def->os.kernel) < 0)
            return -1;

         if (def->os.initrd &&
             xenConfigSetString(conf, "ramdisk", def->os.initrd) < 0)
            return -1;

         if (def->os.cmdline &&
1344
             xenConfigSetString(conf, "cmdline", def->os.cmdline) < 0)
1345 1346 1347 1348 1349 1350
            return -1;
     } /* !hvm */

    return 0;
}

1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
static int
xenFormatXLCPUID(virConfPtr conf, virDomainDefPtr def)
{
    char **cpuid_pairs = NULL;
    char *cpuid_string = NULL;
    size_t i, j;
    int ret = -1;

    if (!def->cpu)
        return 0;

    if (def->cpu->mode != VIR_CPU_MODE_HOST_PASSTHROUGH) {
        VIR_WARN("ignoring CPU mode '%s', only host-passthrough mode "
                 "is supported", virCPUModeTypeToString(def->cpu->mode));
        return 0;
    }

    /* "host" + all features + NULL */
    if (VIR_ALLOC_N(cpuid_pairs, def->cpu->nfeatures + 2) < 0)
        return -1;

    if (VIR_STRDUP(cpuid_pairs[0], "host") < 0)
        goto cleanup;

    j = 1;
    for (i = 0; i < def->cpu->nfeatures; i++) {
        const char *feature_name = xenTranslateCPUFeature(
                def->cpu->features[i].name,
                false);
        const char *policy = NULL;

        if (STREQ(feature_name, "vmx") || STREQ(feature_name, "svm"))
            /* ignore vmx/svm in cpuid option, translated into nestedhvm
             * elsewhere */
            continue;

        switch (def->cpu->features[i].policy) {
            case VIR_CPU_FEATURE_FORCE:
            case VIR_CPU_FEATURE_REQUIRE:
                policy = "1";
                break;
            case VIR_CPU_FEATURE_OPTIONAL:
                policy = "x";
                break;
            case VIR_CPU_FEATURE_DISABLE:
            case VIR_CPU_FEATURE_FORBID:
                policy = "0";
                break;
        }
        if (virAsprintf(&cpuid_pairs[j++], "%s=%s",
                        feature_name,
                        policy) < 0)
            goto cleanup;
    }
    cpuid_pairs[j] = NULL;

    if (j > 1) {
        cpuid_string = virStringListJoin((const char **)cpuid_pairs, ",");
        if (!cpuid_string)
            goto cleanup;

        if (xenConfigSetString(conf, "cpuid", cpuid_string) < 0)
            goto cleanup;
    }

    ret = 0;

 cleanup:
    virStringListFree(cpuid_pairs);
    VIR_FREE(cpuid_string);
    return ret;
}

1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
#ifdef LIBXL_HAVE_VNUMA
static int
xenFormatXLVnode(virConfValuePtr list,
                 virBufferPtr buf)
{
    int ret = -1;
    virConfValuePtr numaPnode, tmp;

    if (virBufferCheckError(buf) < 0)
        goto cleanup;

    if (VIR_ALLOC(numaPnode) < 0)
        goto cleanup;

    /* Place VNODE directive */
    numaPnode->type = VIR_CONF_STRING;
    numaPnode->str = virBufferContentAndReset(buf);

    tmp = list->list;
    while (tmp && tmp->next)
        tmp = tmp->next;
    if (tmp)
        tmp->next = numaPnode;
    else
        list->list = numaPnode;
    ret = 0;

 cleanup:
    virBufferFreeAndReset(buf);
    return ret;
}

static int
xenFormatXLVnuma(virConfValuePtr list,
                 virDomainNumaPtr numa,
                 size_t node,
                 size_t nr_nodes)
{
    int ret = -1;
    size_t i;

    virBuffer buf = VIR_BUFFER_INITIALIZER;
    virConfValuePtr numaVnode, tmp;

    size_t nodeSize = virDomainNumaGetNodeMemorySize(numa, node) / 1024;
    char *nodeVcpus = virBitmapFormat(virDomainNumaGetNodeCpumask(numa, node));

    if (VIR_ALLOC(numaVnode) < 0)
        goto cleanup;

    numaVnode->type = VIR_CONF_LIST;
    numaVnode->list = NULL;

    /* pnode */
J
Jim Fehlig 已提交
1478
    virBufferAsprintf(&buf, "pnode=%zu", node);
1479 1480 1481
    xenFormatXLVnode(numaVnode, &buf);

    /* size */
J
Jim Fehlig 已提交
1482
    virBufferAsprintf(&buf, "size=%zu", nodeSize);
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
    xenFormatXLVnode(numaVnode, &buf);

    /* vcpus */
    virBufferAsprintf(&buf, "vcpus=%s", nodeVcpus);
    xenFormatXLVnode(numaVnode, &buf);

    /* distances */
    virBufferAddLit(&buf, "vdistances=");
    for (i = 0; i < nr_nodes; i++) {
        virBufferAsprintf(&buf, "%zu",
            virDomainNumaGetNodeDistance(numa, node, i));
        if ((nr_nodes - i) > 1)
            virBufferAddLit(&buf, ",");
    }
    xenFormatXLVnode(numaVnode, &buf);

    tmp = list->list;
    while (tmp && tmp->next)
        tmp = tmp->next;
    if (tmp)
        tmp->next = numaVnode;
    else
        list->list = numaVnode;
    ret = 0;

 cleanup:
    VIR_FREE(nodeVcpus);
    return ret;
}

static int
xenFormatXLDomainVnuma(virConfPtr conf,
                       virDomainDefPtr def)
{
    virDomainNumaPtr numa = def->numa;
    virConfValuePtr vnumaVal;
    size_t i;
    size_t nr_nodes;

    if (numa == NULL)
        return -1;

    if (VIR_ALLOC(vnumaVal) < 0)
        return -1;

    vnumaVal->type = VIR_CONF_LIST;
    vnumaVal->list = NULL;

    nr_nodes = virDomainNumaGetNodeCount(numa);
    for (i = 0; i < nr_nodes; i++) {
        if (xenFormatXLVnuma(vnumaVal, numa, i, nr_nodes) < 0)
            goto cleanup;
    }

    if (vnumaVal->list != NULL) {
        int ret = virConfSetValue(conf, "vnuma", vnumaVal);
            vnumaVal = NULL;
            if (ret < 0)
                return -1;
    }
    VIR_FREE(vnumaVal);

    return 0;

 cleanup:
    virConfFreeValue(vnumaVal);
    return -1;
}
#endif
1552

1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
#ifdef LIBXL_HAVE_BUILDINFO_GRANT_LIMITS
static int
xenFormatXLGntLimits(virConfPtr conf, virDomainDefPtr def)
{
    size_t i;

    for (i = 0; i < def->ncontrollers; i++) {
        if (def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_XENBUS &&
            def->controllers[i]->opts.xenbusopts.maxGrantFrames > 0) {
            if (xenConfigSetInt(conf, "max_grant_frames",
                                def->controllers[i]->opts.xenbusopts.maxGrantFrames) < 0)
                return -1;
        }
    }
    return 0;
}
#endif

1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
static char *
xenFormatXLDiskSrcNet(virStorageSourcePtr src)
{
    char *ret = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    size_t i;

    switch ((virStorageNetProtocol) src->protocol) {
    case VIR_STORAGE_NET_PROTOCOL_NBD:
    case VIR_STORAGE_NET_PROTOCOL_HTTP:
    case VIR_STORAGE_NET_PROTOCOL_HTTPS:
    case VIR_STORAGE_NET_PROTOCOL_FTP:
    case VIR_STORAGE_NET_PROTOCOL_FTPS:
    case VIR_STORAGE_NET_PROTOCOL_TFTP:
    case VIR_STORAGE_NET_PROTOCOL_ISCSI:
    case VIR_STORAGE_NET_PROTOCOL_GLUSTER:
    case VIR_STORAGE_NET_PROTOCOL_SHEEPDOG:
1588
    case VIR_STORAGE_NET_PROTOCOL_SSH:
1589
    case VIR_STORAGE_NET_PROTOCOL_VXHS:
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
    case VIR_STORAGE_NET_PROTOCOL_LAST:
    case VIR_STORAGE_NET_PROTOCOL_NONE:
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("Unsupported network block protocol '%s'"),
                       virStorageNetProtocolTypeToString(src->protocol));
        goto cleanup;

    case VIR_STORAGE_NET_PROTOCOL_RBD:
        if (strchr(src->path, ':')) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("':' not allowed in RBD source volume name '%s'"),
                           src->path);
            goto cleanup;
        }

1605
        virBufferStrcat(&buf, "rbd:", src->volume, "/", src->path, NULL);
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622

        virBufferAddLit(&buf, ":auth_supported=none");

        if (src->nhosts > 0) {
            virBufferAddLit(&buf, ":mon_host=");
            for (i = 0; i < src->nhosts; i++) {
                if (i)
                    virBufferAddLit(&buf, "\\\\;");

                /* assume host containing : is ipv6 */
                if (strchr(src->hosts[i].name, ':'))
                    virBufferEscape(&buf, '\\', ":", "[%s]",
                                    src->hosts[i].name);
                else
                    virBufferAsprintf(&buf, "%s", src->hosts[i].name);

                if (src->hosts[i].port)
1623
                    virBufferAsprintf(&buf, "\\\\:%u", src->hosts[i].port);
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
            }
        }

        if (virBufferCheckError(&buf) < 0)
            goto cleanup;

        ret = virBufferContentAndReset(&buf);
        break;
    }

 cleanup:
    virBufferFreeAndReset(&buf);

    return ret;
}


static int
xenFormatXLDiskSrc(virStorageSourcePtr src, char **srcstr)
{
    int actualType = virStorageSourceGetActualType(src);

    *srcstr = NULL;

    if (virStorageSourceIsEmpty(src))
        return 0;

1651
    switch ((virStorageType)actualType) {
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
    case VIR_STORAGE_TYPE_BLOCK:
    case VIR_STORAGE_TYPE_FILE:
    case VIR_STORAGE_TYPE_DIR:
        if (VIR_STRDUP(*srcstr, src->path) < 0)
            return -1;
        break;

    case VIR_STORAGE_TYPE_NETWORK:
        if (!(*srcstr = xenFormatXLDiskSrcNet(src)))
            return -1;
        break;

    case VIR_STORAGE_TYPE_VOLUME:
    case VIR_STORAGE_TYPE_NONE:
    case VIR_STORAGE_TYPE_LAST:
        break;
    }

    return 0;
}


1674 1675 1676 1677 1678 1679 1680
static int
xenFormatXLDisk(virConfValuePtr list, virDomainDiskDefPtr disk)
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    virConfValuePtr val, tmp;
    int format = virDomainDiskGetFormat(disk);
    const char *driver = virDomainDiskGetDriver(disk);
1681
    char *target = NULL;
1682
    int ret = -1;
1683 1684

    /* format */
1685
    virBufferAddLit(&buf, "format=");
1686 1687
    switch (format) {
        case VIR_STORAGE_FILE_RAW:
1688
            virBufferAddLit(&buf, "raw");
1689 1690
            break;
        case VIR_STORAGE_FILE_VHD:
1691
            virBufferAddLit(&buf, "xvhd");
1692 1693
            break;
        case VIR_STORAGE_FILE_QCOW:
1694
            virBufferAddLit(&buf, "qcow");
1695 1696
            break;
        case VIR_STORAGE_FILE_QCOW2:
1697
            virBufferAddLit(&buf, "qcow2");
1698
            break;
C
Cédric Bosdonnat 已提交
1699 1700 1701
        case VIR_STORAGE_FILE_QED:
            virBufferAddLit(&buf, "qed");
            break;
1702 1703
      /* set default */
        default:
1704
            virBufferAddLit(&buf, "raw");
1705 1706 1707
    }

    /* device */
1708
    virBufferAsprintf(&buf, ",vdev=%s", disk->dst);
1709

1710
    /* access */
1711
    virBufferAddLit(&buf, ",access=");
1712
    if (disk->src->readonly)
1713
        virBufferAddLit(&buf, "ro");
1714
    else if (disk->src->shared)
1715
        virBufferAddLit(&buf, "!");
1716
    else
1717
        virBufferAddLit(&buf, "rw");
1718 1719 1720 1721 1722 1723
    if (disk->transient) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("transient disks not supported yet"));
        goto cleanup;
    }

1724
    /* backendtype */
1725
    if (driver) {
1726
        virBufferAddLit(&buf, ",backendtype=");
1727
        if (STREQ(driver, "qemu") || STREQ(driver, "file"))
1728
            virBufferAddLit(&buf, "qdisk");
1729
        else if (STREQ(driver, "tap"))
1730
            virBufferAddLit(&buf, "tap");
1731
        else if (STREQ(driver, "phy"))
1732
            virBufferAddLit(&buf, "phy");
1733
    }
1734

1735
    /* devtype */
1736
    if (disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM)
1737
        virBufferAddLit(&buf, ",devtype=cdrom");
1738

1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
    /*
     * target
     * From $xensrc/docs/misc/xl-disk-configuration.txt:
     * When this parameter is specified by name, ie with the "target="
     * syntax in the configuration file, it consumes the whole rest of the
     * <diskspec> including trailing whitespaces.  Therefore in that case
     * it must come last.
     */
    if (xenFormatXLDiskSrc(disk->src, &target) < 0)
        goto cleanup;

1750
    if (target)
1751
        virBufferAsprintf(&buf, ",target=%s", target);
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767

    if (virBufferCheckError(&buf) < 0)
        goto cleanup;

    if (VIR_ALLOC(val) < 0)
        goto cleanup;

    val->type = VIR_CONF_STRING;
    val->str = virBufferContentAndReset(&buf);
    tmp = list->list;
    while (tmp && tmp->next)
        tmp = tmp->next;
    if (tmp)
        tmp->next = val;
    else
        list->list = val;
1768
    ret = 0;
1769 1770

 cleanup:
1771
    VIR_FREE(target);
1772
    virBufferFreeAndReset(&buf);
1773
    return ret;
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
}


static int
xenFormatXLDomainDisks(virConfPtr conf, virDomainDefPtr def)
{
    virConfValuePtr diskVal;
    size_t i;

    if (VIR_ALLOC(diskVal) < 0)
        return -1;

    diskVal->type = VIR_CONF_LIST;
    diskVal->list = NULL;

    for (i = 0; i < def->ndisks; i++) {
        if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY)
            continue;

        if (xenFormatXLDisk(diskVal, def->disks[i]) < 0)
            goto cleanup;
    }

1797 1798 1799 1800 1801 1802 1803
    if (diskVal->list != NULL) {
        int ret = virConfSetValue(conf, "disk", diskVal);
        diskVal = NULL;
        if (ret < 0)
            return -1;
    }
    VIR_FREE(diskVal);
1804

1805
    return 0;
1806 1807 1808

 cleanup:
    virConfFreeValue(diskVal);
1809
    return -1;
1810 1811 1812 1813 1814 1815
}


static int
xenFormatXLSpice(virConfPtr conf, virDomainDefPtr def)
{
1816
    virDomainGraphicsListenDefPtr glisten;
1817
    virDomainGraphicsDefPtr graphics;
1818

1819
    if (def->os.type == VIR_DOMAIN_OSTYPE_HVM && def->graphics) {
1820 1821 1822
        graphics = def->graphics[0];

        if (graphics->type == VIR_DOMAIN_GRAPHICS_TYPE_SPICE) {
1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
            /* set others to false but may not be necessary */
            if (xenConfigSetInt(conf, "sdl", 0) < 0)
                return -1;

            if (xenConfigSetInt(conf, "vnc", 0) < 0)
                return -1;

            if (xenConfigSetInt(conf, "spice", 1) < 0)
                return -1;

1833 1834 1835
            if ((glisten = virDomainGraphicsGetListen(graphics, 0)) &&
                glisten->address &&
                xenConfigSetString(conf, "spicehost", glisten->address) < 0)
1836 1837
                return -1;

1838
            if (xenConfigSetInt(conf, "spiceport",
1839
                                graphics->data.spice.port) < 0)
1840 1841 1842
                return -1;

            if (xenConfigSetInt(conf, "spicetls_port",
1843
                                graphics->data.spice.tlsPort) < 0)
1844 1845
                return -1;

1846
            if (graphics->data.spice.auth.passwd) {
J
Jim Fehlig 已提交
1847
                if (xenConfigSetInt(conf, "spicedisable_ticketing", 0) < 0)
1848 1849
                    return -1;

J
Jim Fehlig 已提交
1850 1851 1852 1853 1854
                if (xenConfigSetString(conf, "spicepasswd",
                                       graphics->data.spice.auth.passwd) < 0)
                    return -1;
            } else {
                if (xenConfigSetInt(conf, "spicedisable_ticketing", 1) < 0)
1855 1856 1857
                    return -1;
            }

1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873
            if (graphics->data.spice.mousemode) {
                switch (graphics->data.spice.mousemode) {
                case VIR_DOMAIN_GRAPHICS_SPICE_MOUSE_MODE_SERVER:
                    if (xenConfigSetInt(conf, "spiceagent_mouse", 0) < 0)
                        return -1;
                    break;
                case VIR_DOMAIN_GRAPHICS_SPICE_MOUSE_MODE_CLIENT:
                    if (xenConfigSetInt(conf, "spiceagent_mouse", 1) < 0)
                        return -1;
                    /*
                     * spicevdagent must be enabled if using client
                     * mode mouse
                     */
                    if (xenConfigSetInt(conf, "spicevdagent", 1) < 0)
                        return -1;
                    break;
1874
                case VIR_DOMAIN_GRAPHICS_SPICE_MOUSE_MODE_DEFAULT:
1875
                    break;
1876 1877 1878 1879 1880
                case VIR_DOMAIN_GRAPHICS_SPICE_MOUSE_MODE_LAST:
                default:
                    virReportEnumRangeError(virDomainGraphicsSpiceMouseMode,
                                            graphics->data.spice.mousemode);
                    return -1;
1881 1882
                }
            }
1883

1884 1885 1886 1887 1888 1889 1890 1891
            if (graphics->data.spice.copypaste == VIR_TRISTATE_BOOL_YES) {
                if (xenConfigSetInt(conf, "spice_clipboard_sharing", 1) < 0)
                    return -1;
                /*
                 * spicevdagent must be enabled if spice_clipboard_sharing
                 * is enabled
                 */
                if (xenConfigSetInt(conf, "spicevdagent", 1) < 0)
1892 1893 1894 1895 1896 1897 1898 1899
                    return -1;
            }
        }
    }

    return 0;
}

1900 1901 1902 1903 1904 1905 1906
static int
xenFormatXLInputDevs(virConfPtr conf, virDomainDefPtr def)
{
    size_t i;
    const char *devtype;
    virConfValuePtr usbdevices = NULL, lastdev;

1907
    if (def->os.type == VIR_DOMAIN_OSTYPE_HVM) {
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
        if (VIR_ALLOC(usbdevices) < 0)
            goto error;

        usbdevices->type = VIR_CONF_LIST;
        usbdevices->list = NULL;
        lastdev = NULL;
        for (i = 0; i < def->ninputs; i++) {
            if (def->inputs[i]->bus == VIR_DOMAIN_INPUT_BUS_USB) {
                if (xenConfigSetInt(conf, "usb", 1) < 0)
                    goto error;

                switch (def->inputs[i]->type) {
                    case VIR_DOMAIN_INPUT_TYPE_MOUSE:
                        devtype = "mouse";
                        break;
                    case VIR_DOMAIN_INPUT_TYPE_TABLET:
                        devtype = "tablet";
                        break;
                    case VIR_DOMAIN_INPUT_TYPE_KBD:
                        devtype = "keyboard";
                        break;
                    default:
                        continue;
                }

                if (lastdev == NULL) {
                    if (VIR_ALLOC(lastdev) < 0)
                        goto error;
                    usbdevices->list = lastdev;
                } else {
                    if (VIR_ALLOC(lastdev->next) < 0)
                        goto error;
                    lastdev = lastdev->next;
                }
                lastdev->type = VIR_CONF_STRING;
                if (VIR_STRDUP(lastdev->str, devtype) < 0)
                    goto error;
            }
        }
        if (usbdevices->list != NULL) {
            if (usbdevices->list->next == NULL) {
                /* for compatibility with Xen <= 4.2, use old syntax when
                 * only one device present */
                if (xenConfigSetString(conf, "usbdevice", usbdevices->list->str) < 0)
                    goto error;
                virConfFreeValue(usbdevices);
            } else {
                virConfSetValue(conf, "usbdevice", usbdevices);
            }
        } else {
            VIR_FREE(usbdevices);
        }
    }

    return 0;
 error:
    virConfFreeValue(usbdevices);
    return -1;
}

1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047
static int
xenFormatXLUSBController(virConfPtr conf,
                         virDomainDefPtr def)
{
    virConfValuePtr usbctrlVal = NULL;
    int hasUSBCtrl = 0;
    size_t i;

    for (i = 0; i < def->ncontrollers; i++) {
        if (def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_USB) {
            hasUSBCtrl = 1;
            break;
        }
    }

    if (!hasUSBCtrl)
        return 0;

    if (VIR_ALLOC(usbctrlVal) < 0)
        return -1;

    usbctrlVal->type = VIR_CONF_LIST;
    usbctrlVal->list = NULL;

    for (i = 0; i < def->ncontrollers; i++) {
        if (def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_USB) {
            virConfValuePtr val, tmp;
            virBuffer buf = VIR_BUFFER_INITIALIZER;

            if (def->controllers[i]->model != -1) {
                switch (def->controllers[i]->model) {
                case VIR_DOMAIN_CONTROLLER_MODEL_USB_QUSB1:
                    virBufferAddLit(&buf, "type=qusb,version=1,");
                    break;

                case VIR_DOMAIN_CONTROLLER_MODEL_USB_QUSB2:
                    virBufferAddLit(&buf, "type=qusb,version=2,");
                    break;

                default:
                    goto error;
                }
            }

            if (def->controllers[i]->opts.usbopts.ports != -1)
                virBufferAsprintf(&buf, "ports=%x",
                                  def->controllers[i]->opts.usbopts.ports);

            if (VIR_ALLOC(val) < 0) {
                virBufferFreeAndReset(&buf);
                goto error;
            }
            val->type = VIR_CONF_STRING;
            val->str = virBufferContentAndReset(&buf);
            tmp = usbctrlVal->list;
            while (tmp && tmp->next)
                tmp = tmp->next;
            if (tmp)
                tmp->next = val;
            else
                usbctrlVal->list = val;
        }
    }

    if (usbctrlVal->list != NULL) {
        int ret = virConfSetValue(conf, "usbctrl", usbctrlVal);
        usbctrlVal = NULL;
        if (ret < 0)
            return -1;
    }
    VIR_FREE(usbctrlVal);

    return 0;

 error:
    virConfFreeValue(usbctrlVal);
    return -1;
}


2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113
static int
xenFormatXLUSB(virConfPtr conf,
               virDomainDefPtr def)
{
    virConfValuePtr usbVal = NULL;
    int hasUSB = 0;
    size_t i;

    for (i = 0; i < def->nhostdevs; i++) {
        if (def->hostdevs[i]->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
            def->hostdevs[i]->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) {
            hasUSB = 1;
            break;
        }
    }

    if (!hasUSB)
        return 0;

    if (VIR_ALLOC(usbVal) < 0)
        return -1;

    usbVal->type = VIR_CONF_LIST;
    usbVal->list = NULL;

    for (i = 0; i < def->nhostdevs; i++) {
        if (def->hostdevs[i]->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
            def->hostdevs[i]->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) {
            virConfValuePtr val, tmp;
            char *buf;

            if (virAsprintf(&buf, "hostbus=%x,hostaddr=%x",
                            def->hostdevs[i]->source.subsys.u.usb.bus,
                            def->hostdevs[i]->source.subsys.u.usb.device) < 0)
                goto error;

            if (VIR_ALLOC(val) < 0) {
                VIR_FREE(buf);
                goto error;
            }
            val->type = VIR_CONF_STRING;
            val->str = buf;
            tmp = usbVal->list;
            while (tmp && tmp->next)
                tmp = tmp->next;
            if (tmp)
                tmp->next = val;
            else
                usbVal->list = val;
        }
    }

    if (usbVal->list != NULL) {
        int ret = virConfSetValue(conf, "usbdev", usbVal);
        usbVal = NULL;
        if (ret < 0)
            return -1;
    }
    VIR_FREE(usbVal);

    return 0;

 error:
    virConfFreeValue(usbVal);
    return -1;
}
2114

2115 2116 2117 2118
static int
xenFormatXLChannel(virConfValuePtr list, virDomainChrDefPtr channel)
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
2119
    int sourceType = channel->source->type;
2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130
    virConfValuePtr val, tmp;

    /* connection */
    virBufferAddLit(&buf, "connection=");
    switch (sourceType) {
        case VIR_DOMAIN_CHR_TYPE_PTY:
            virBufferAddLit(&buf, "pty,");
            break;
        case VIR_DOMAIN_CHR_TYPE_UNIX:
            virBufferAddLit(&buf, "socket,");
            /* path */
2131
            if (channel->source->data.nix.path)
2132
                virBufferAsprintf(&buf, "path=%s,",
2133
                                  channel->source->data.nix.path);
2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197
            break;
        default:
            goto cleanup;
    }

    /* name */
    virBufferAsprintf(&buf, "name=%s", channel->target.name);

    if (VIR_ALLOC(val) < 0)
        goto cleanup;

    val->type = VIR_CONF_STRING;
    val->str = virBufferContentAndReset(&buf);
    tmp = list->list;
    while (tmp && tmp->next)
        tmp = tmp->next;
    if (tmp)
        tmp->next = val;
    else
        list->list = val;
    return 0;

 cleanup:
    virBufferFreeAndReset(&buf);
    return -1;
}

static int
xenFormatXLDomainChannels(virConfPtr conf, virDomainDefPtr def)
{
    virConfValuePtr channelVal = NULL;
    size_t i;

    if (VIR_ALLOC(channelVal) < 0)
        goto cleanup;

    channelVal->type = VIR_CONF_LIST;
    channelVal->list = NULL;

    for (i = 0; i < def->nchannels; i++) {
        virDomainChrDefPtr chr = def->channels[i];

        if (chr->targetType != VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_XEN)
            continue;

        if (xenFormatXLChannel(channelVal, def->channels[i]) < 0)
            goto cleanup;
    }

    if (channelVal->list != NULL) {
        int ret = virConfSetValue(conf, "channel", channelVal);
        channelVal = NULL;
        if (ret < 0)
            goto cleanup;
    }

    VIR_FREE(channelVal);
    return 0;

 cleanup:
    virConfFreeValue(channelVal);
    return -1;
}

2198
virConfPtr
2199
xenFormatXL(virDomainDefPtr def, virConnectPtr conn)
2200 2201 2202 2203 2204 2205
{
    virConfPtr conf = NULL;

    if (!(conf = virConfNew()))
        goto cleanup;

2206
    if (xenFormatConfigCommon(conf, def, conn, XEN_CONFIG_FORMAT_XL) < 0)
2207 2208
        goto cleanup;

2209 2210 2211
    if (xenFormatXLOS(conf, def) < 0)
        goto cleanup;

2212 2213 2214
    if (xenFormatXLCPUID(conf, def) < 0)
        goto cleanup;

2215 2216 2217 2218 2219
#ifdef LIBXL_HAVE_VNUMA
    if (xenFormatXLDomainVnuma(conf, def) < 0)
        goto cleanup;
#endif

2220 2221 2222 2223 2224
#ifdef LIBXL_HAVE_BUILDINFO_GRANT_LIMITS
    if (xenFormatXLGntLimits(conf, def) < 0)
        goto cleanup;
#endif

2225 2226 2227 2228 2229 2230
    if (xenFormatXLDomainDisks(conf, def) < 0)
        goto cleanup;

    if (xenFormatXLSpice(conf, def) < 0)
        goto cleanup;

2231 2232 2233
    if (xenFormatXLInputDevs(conf, def) < 0)
        goto cleanup;

2234 2235 2236
    if (xenFormatXLUSB(conf, def) < 0)
        goto cleanup;

2237 2238 2239
    if (xenFormatXLUSBController(conf, def) < 0)
        goto cleanup;

2240 2241 2242
    if (xenFormatXLDomainChannels(conf, def) < 0)
        goto cleanup;

2243 2244 2245 2246 2247 2248 2249
    return conf;

 cleanup:
    if (conf)
        virConfFree(conf);
    return NULL;
}