xen_common.c 68.8 KB
Newer Older
1 2 3 4 5
/*
 * xen_common.c: Parsing and formatting functions for config common
 * between XM and XL
 *
 * Copyright (C) 2014 SUSE LINUX Products GmbH, Nuernberg, Germany.
6
 * Copyright (C) 2006-2007, 2009-2016 Red Hat, Inc.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * Copyright (C) 2011 Univention GmbH
 * Copyright (C) 2006 Daniel P. Berrange
 *
 * 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>

27 28
#include <regex.h>

29 30 31 32 33 34 35 36 37 38 39
#include "internal.h"
#include "virerror.h"
#include "virconf.h"
#include "viralloc.h"
#include "viruuid.h"
#include "count-one-bits.h"
#include "xenxs_private.h"
#include "domain_conf.h"
#include "virstring.h"
#include "xen_common.h"

J
Jim Fehlig 已提交
40
#define VIR_FROM_THIS VIR_FROM_XEN
41 42 43 44

/*
 * Convenience method to grab a long int from the config file object
 */
45
int
46 47 48 49 50 51 52 53 54 55 56 57 58
xenConfigGetBool(virConfPtr conf,
                 const char *name,
                 int *value,
                 int def)
{
    virConfValuePtr val;

    *value = 0;
    if (!(val = virConfGetValue(conf, name))) {
        *value = def;
        return 0;
    }

59
    if (val->type == VIR_CONF_ULLONG) {
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        *value = val->l ? 1 : 0;
    } else if (val->type == VIR_CONF_STRING) {
        *value = STREQ(val->str, "1") ? 1 : 0;
    } else {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("config value %s was malformed"), name);
        return -1;
    }
    return 0;
}


/*
 * Convenience method to grab a int from the config file object
 */
75
int
76 77 78 79 80 81 82 83 84 85 86 87 88
xenConfigGetULong(virConfPtr conf,
                  const char *name,
                  unsigned long *value,
                  unsigned long def)
{
    virConfValuePtr val;

    *value = 0;
    if (!(val = virConfGetValue(conf, name))) {
        *value = def;
        return 0;
    }

89
    if (val->type == VIR_CONF_ULLONG) {
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
        *value = val->l;
    } else if (val->type == VIR_CONF_STRING) {
        if (virStrToLong_ul(val->str, NULL, 10, value) < 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("config value %s was malformed"), name);
            return -1;
        }
    } else {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("config value %s was malformed"), name);
        return -1;
    }
    return 0;
}


/*
 * Convenience method to grab a int from the config file object
 */
static int
xenConfigGetULongLong(virConfPtr conf,
                      const char *name,
                      unsigned long long *value,
                      unsigned long long def)
{
    virConfValuePtr val;

    *value = 0;
    if (!(val = virConfGetValue(conf, name))) {
        *value = def;
        return 0;
    }

123
    if (val->type == VIR_CONF_ULLONG) {
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
        *value = val->l;
    } else if (val->type == VIR_CONF_STRING) {
        if (virStrToLong_ull(val->str, NULL, 10, value) < 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("config value %s was malformed"), name);
            return -1;
        }
    } else {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("config value %s was malformed"), name);
        return -1;
    }
    return 0;
}


static int
xenConfigCopyStringInternal(virConfPtr conf,
                            const char *name,
                            char **value,
                            int allowMissing)
{
146
    int rc;
147 148

    *value = NULL;
149
    if ((rc = virConfGetValueString(conf, name, value)) < 0)
150 151
        return -1;

152
    if (rc == 0) {
153 154 155 156 157 158 159
        if (allowMissing)
            return 0;
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("config value %s was missing"), name);
        return -1;
    }

160
    return 1;
161 162 163
}


164
int
165 166 167 168 169 170
xenConfigCopyString(virConfPtr conf, const char *name, char **value)
{
    return xenConfigCopyStringInternal(conf, name, value, 0);
}


171
int
172 173 174 175 176 177 178 179 180 181 182 183
xenConfigCopyStringOpt(virConfPtr conf, const char *name, char **value)
{
    return xenConfigCopyStringInternal(conf, name, value, 1);
}


/*
 * Convenience method to grab a string UUID from the config file object
 */
static int
xenConfigGetUUID(virConfPtr conf, const char *name, unsigned char *uuid)
{
184 185
    VIR_AUTOFREE(char *) string = NULL;
    int rc;
186 187 188 189 190 191 192

    if (!uuid || !name || !conf) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Arguments must be non null"));
        return -1;
    }

193 194 195 196 197

    if ((rc = virConfGetValueString(conf, name, &string)) < 0)
        return -1;

    if (rc == 0) {
198
        if (virUUIDGenerate(uuid) < 0) {
199 200 201 202 203 204 205 206
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("Failed to generate UUID"));
            return -1;
        } else {
            return 0;
        }
    }

207
    if (!string) {
208 209 210 211 212
        virReportError(VIR_ERR_CONF_SYNTAX,
                       _("%s can't be empty"), name);
        return -1;
    }

213
    if (virUUIDParse(string, uuid) < 0) {
214
        virReportError(VIR_ERR_CONF_SYNTAX,
215
                       _("%s not parseable"), string);
216 217 218 219 220 221 222 223 224 225 226 227 228
        return -1;
    }

    return 0;
}


/*
 * Convenience method to grab a string from the config file object
 */
int
xenConfigGetString(virConfPtr conf,
                   const char *name,
229
                   char **value,
230 231
                   const char *def)
{
232 233
    char *string = NULL;
    int rc;
234 235

    *value = NULL;
236
    if ((rc = virConfGetValueString(conf, name, &string)) < 0)
237
        return -1;
238 239 240 241 242 243

    if (rc == 0 || !string) {
        if (VIR_STRDUP(*value, def) < 0)
            return -1;
    } else {
        *value = string;
244
    }
245

246 247 248 249
    return 0;
}


250 251
int
xenConfigSetInt(virConfPtr conf, const char *setting, long long l)
252 253 254
{
    virConfValuePtr value = NULL;

255
    if ((long)l != l) {
256 257 258 259 260 261 262
        virReportError(VIR_ERR_OVERFLOW, _("failed to store %lld to %s"),
                       l, setting);
        return -1;
    }
    if (VIR_ALLOC(value) < 0)
        return -1;

263
    value->type = VIR_CONF_LLONG;
264 265 266 267 268 269 270
    value->next = NULL;
    value->l = l;

    return virConfSetValue(conf, setting, value);
}


271 272
int
xenConfigSetString(virConfPtr conf, const char *setting, const char *str)
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
{
    virConfValuePtr value = NULL;

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

    value->type = VIR_CONF_STRING;
    value->next = NULL;
    if (VIR_STRDUP(value->str, str) < 0) {
        VIR_FREE(value);
        return -1;
    }

    return virConfSetValue(conf, setting, value);
}


static int
xenParseMem(virConfPtr conf, virDomainDefPtr def)
{
293 294
    unsigned long long memory;

295 296 297 298
    if (xenConfigGetULongLong(conf, "memory", &def->mem.cur_balloon,
                                MIN_XEN_GUEST_SIZE * 2) < 0)
        return -1;

299
    if (xenConfigGetULongLong(conf, "maxmem", &memory,
300 301 302 303
                                def->mem.cur_balloon) < 0)
        return -1;

    def->mem.cur_balloon *= 1024;
304
    virDomainDefSetMemoryTotal(def, memory * 1024);
305 306 307 308 309 310

    return 0;
}


static int
311
xenParseTimeOffset(virConfPtr conf, virDomainDefPtr def)
312 313 314 315 316 317
{
    int vmlocaltime;

    if (xenConfigGetBool(conf, "localtime", &vmlocaltime, 0) < 0)
        return -1;

318
    if (def->os.type == VIR_DOMAIN_OSTYPE_HVM) {
319 320 321 322
        unsigned long rtc_timeoffset;
        def->clock.offset = VIR_DOMAIN_CLOCK_OFFSET_VARIABLE;
        if (xenConfigGetULong(conf, "rtc_timeoffset", &rtc_timeoffset, 0) < 0)
            return -1;
323

324 325 326 327
        def->clock.data.variable.adjustment = (int)rtc_timeoffset;
        def->clock.data.variable.basis = vmlocaltime ?
            VIR_DOMAIN_CLOCK_BASIS_LOCALTIME :
            VIR_DOMAIN_CLOCK_BASIS_UTC;
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
    } else {
        /* PV domains do not have an emulated RTC and the offset is fixed. */
        def->clock.offset = vmlocaltime ?
            VIR_DOMAIN_CLOCK_OFFSET_LOCALTIME :
            VIR_DOMAIN_CLOCK_OFFSET_UTC;
        def->clock.data.utc_reset = true;
    } /* !hvm */

    return 0;
}


static int
xenParseEventsActions(virConfPtr conf, virDomainDefPtr def)
{
343 344 345
    VIR_AUTOFREE(char *) on_poweroff = NULL;
    VIR_AUTOFREE(char *) on_reboot = NULL;
    VIR_AUTOFREE(char *) on_crash = NULL;
346

347
    if (xenConfigGetString(conf, "on_poweroff", &on_poweroff, "destroy") < 0)
348 349
        return -1;

350
    if ((def->onPoweroff = virDomainLifecycleActionTypeFromString(on_poweroff)) < 0) {
351
        virReportError(VIR_ERR_INTERNAL_ERROR,
352
                       _("unexpected value %s for on_poweroff"), on_poweroff);
353 354 355
        return -1;
    }

356
    if (xenConfigGetString(conf, "on_reboot", &on_reboot, "restart") < 0)
357 358
        return -1;

359
    if ((def->onReboot = virDomainLifecycleActionTypeFromString(on_reboot)) < 0) {
360
        virReportError(VIR_ERR_INTERNAL_ERROR,
361
                       _("unexpected value %s for on_reboot"), on_reboot);
362 363 364
        return -1;
    }

365
    if (xenConfigGetString(conf, "on_crash", &on_crash, "restart") < 0)
366 367
        return -1;

368
    if ((def->onCrash = virDomainLifecycleActionTypeFromString(on_crash)) < 0) {
369
        virReportError(VIR_ERR_INTERNAL_ERROR,
370
                       _("unexpected value %s for on_crash"), on_crash);
371 372 373 374 375 376 377
        return -1;
    }

    return 0;
}


378 379
static virDomainHostdevDefPtr
xenParsePCI(char *entry)
380 381
{
    virDomainHostdevDefPtr hostdev = NULL;
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    char domain[5];
    char bus[3];
    char slot[3];
    char func[2];
    char *key, *nextkey;
    int domainID;
    int busID;
    int slotID;
    int funcID;

    domain[0] = bus[0] = slot[0] = func[0] = '\0';

    /* pci=['0000:00:1b.0','0000:00:13.0'] */
    if (!(key = entry))
        return NULL;
    if (!(nextkey = strchr(key, ':')))
        return NULL;
399
    if (virStrncpy(domain, key, (nextkey - key), sizeof(domain)) < 0) {
400 401 402 403
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Domain %s too big for destination"), key);
        return NULL;
    }
404

405 406 407
    key = nextkey + 1;
    if (!(nextkey = strchr(key, ':')))
        return NULL;
408
    if (virStrncpy(bus, key, (nextkey - key), sizeof(bus)) < 0) {
409 410 411 412
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Bus %s too big for destination"), key);
        return NULL;
    }
413

414 415 416
    key = nextkey + 1;
    if (!(nextkey = strchr(key, '.')))
        return NULL;
417
    if (virStrncpy(slot, key, (nextkey - key), sizeof(slot)) < 0) {
418 419 420 421
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Slot %s too big for destination"), key);
        return NULL;
    }
422

423 424 425
    key = nextkey + 1;
    if (strlen(key) != 1)
        return NULL;
426
    if (virStrncpy(func, key, 1, sizeof(func)) < 0) {
427 428 429 430
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Function %s too big for destination"), key);
        return NULL;
    }
431

432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
    if (virStrToLong_i(domain, NULL, 16, &domainID) < 0)
        return NULL;
    if (virStrToLong_i(bus, NULL, 16, &busID) < 0)
        return NULL;
    if (virStrToLong_i(slot, NULL, 16, &slotID) < 0)
        return NULL;
    if (virStrToLong_i(func, NULL, 16, &funcID) < 0)
        return NULL;

    if (!(hostdev = virDomainHostdevDefNew()))
       return NULL;

    hostdev->managed = false;
    hostdev->source.subsys.type = VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI;
    hostdev->source.subsys.u.pci.addr.domain = domainID;
    hostdev->source.subsys.u.pci.addr.bus = busID;
    hostdev->source.subsys.u.pci.addr.slot = slotID;
    hostdev->source.subsys.u.pci.addr.function = funcID;

    return hostdev;
}
453 454


455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
static int
xenHandleConfGetValueStringListErrors(int ret)
{
    if (ret < 0) {
        /* It means virConfGetValueStringList() didn't fail because the
         * cval->type switch fell through - since we're passing
         * @compatString == false - assumes failures for memory allocation
         * and VIR_CONF_LIST traversal failure should cause -1 to be
         * returned to the caller with the error message set. */
        if (virGetLastErrorCode() != VIR_ERR_INTERNAL_ERROR)
            return -1;

        /* If we did fall through the switch, then ignore and clear the
         * last error. */
        virResetLastError();
    }
    return 0;
}


475 476 477
static int
xenParsePCIList(virConfPtr conf, virDomainDefPtr def)
{
478
    VIR_AUTOSTRINGLIST pcis = NULL;
479
    char **entries = NULL;
480
    int rc;
481

482 483
    if ((rc = virConfGetValueStringList(conf, "pci", false, &pcis)) <= 0)
        return xenHandleConfGetValueStringListErrors(rc);
484

485
    for (entries = pcis; *entries; entries++) {
486
        char *entry = *entries;
487 488
        virDomainHostdevDefPtr hostdev;

489
        if (!(hostdev = xenParsePCI(entry)))
490 491 492 493 494
            return -1;

        if (VIR_APPEND_ELEMENT(def->hostdevs, def->nhostdevs, hostdev) < 0) {
            virDomainHostdevDefFree(hostdev);
            return -1;
495 496 497 498 499 500 501 502
        }
    }

    return 0;
}


static int
503 504 505
xenParseCPUFeatures(virConfPtr conf,
                    virDomainDefPtr def,
                    virDomainXMLOptionPtr xmlopt)
506 507
{
    unsigned long count = 0;
508 509
    VIR_AUTOFREE(char *) cpus = NULL;
    VIR_AUTOFREE(char *) tsc_mode = NULL;
510
    int val = 0;
511
    virDomainTimerDefPtr timer;
512

513
    if (xenConfigGetULong(conf, "vcpus", &count, 1) < 0)
514 515
        return -1;

516
    if (virDomainDefSetVcpusMax(def, count, xmlopt) < 0)
517 518
        return -1;

519
    if (virDomainDefSetVcpus(def, count) < 0)
520 521
        return -1;

522 523 524 525
    if (virConfGetValue(conf, "maxvcpus")) {
        if (xenConfigGetULong(conf, "maxvcpus", &count, 0) < 0)
            return -1;

526
        if (virDomainDefSetVcpusMax(def, count, xmlopt) < 0)
527 528
            return -1;
    }
529

530
    if (xenConfigGetString(conf, "cpus", &cpus, NULL) < 0)
531 532
        return -1;

533
    if (cpus && (virBitmapParse(cpus, &def->cpumask, 4096) < 0))
534 535
        return -1;

536
    if (xenConfigGetString(conf, "tsc_mode", &tsc_mode, NULL) < 0)
537 538
        return -1;

539
    if (tsc_mode) {
540 541 542 543 544 545 546 547 548
        if (VIR_EXPAND_N(def->clock.timers, def->clock.ntimers, 1) < 0 ||
            VIR_ALLOC(timer) < 0)
            return -1;

        timer->name = VIR_DOMAIN_TIMER_NAME_TSC;
        timer->present = 1;
        timer->tickpolicy = -1;
        timer->mode = VIR_DOMAIN_TIMER_MODE_AUTO;
        timer->track = -1;
549
        if (STREQ_NULLABLE(tsc_mode, "always_emulate"))
550
            timer->mode = VIR_DOMAIN_TIMER_MODE_EMULATE;
551
        else if (STREQ_NULLABLE(tsc_mode, "native"))
552
            timer->mode = VIR_DOMAIN_TIMER_MODE_NATIVE;
553
        else if (STREQ_NULLABLE(tsc_mode, "native_paravirt"))
554 555 556 557 558
            timer->mode = VIR_DOMAIN_TIMER_MODE_PARAVIRT;

        def->clock.timers[def->clock.ntimers - 1] = timer;
    }

559
    if (def->os.type == VIR_DOMAIN_OSTYPE_HVM) {
560
        if (xenConfigGetBool(conf, "pae", &val, 1) < 0)
561 562 563 564
            return -1;

        else if (val)
            def->features[VIR_DOMAIN_FEATURE_PAE] = VIR_TRISTATE_SWITCH_ON;
565
        if (xenConfigGetBool(conf, "acpi", &val, 1) < 0)
566 567 568 569
            return -1;

        else if (val)
            def->features[VIR_DOMAIN_FEATURE_ACPI] = VIR_TRISTATE_SWITCH_ON;
570
        if (xenConfigGetBool(conf, "apic", &val, 1) < 0)
571 572 573 574
            return -1;

        else if (val)
            def->features[VIR_DOMAIN_FEATURE_APIC] = VIR_TRISTATE_SWITCH_ON;
575
        if (xenConfigGetBool(conf, "hap", &val, 1) < 0)
576 577
            return -1;

578 579
        else if (!val)
            def->features[VIR_DOMAIN_FEATURE_HAP] = VIR_TRISTATE_SWITCH_OFF;
580 581 582 583 584 585 586 587 588
        if (xenConfigGetBool(conf, "viridian", &val, 0) < 0)
            return -1;

        else if (val)
            def->features[VIR_DOMAIN_FEATURE_VIRIDIAN] = VIR_TRISTATE_SWITCH_ON;

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

589
        if (val != -1) {
590
            if (VIR_EXPAND_N(def->clock.timers, def->clock.ntimers, 1) < 0 ||
591 592 593 594 595 596
                VIR_ALLOC(timer) < 0)
                return -1;

            timer->name = VIR_DOMAIN_TIMER_NAME_HPET;
            timer->present = val;
            timer->tickpolicy = -1;
597 598
            timer->mode = -1;
            timer->track = -1;
599

600
            def->clock.timers[def->clock.ntimers - 1] = timer;
601 602 603 604 605 606 607 608 609 610
        }
    }

    return 0;
}


#define MAX_VFB 1024

static int
611
xenParseVfb(virConfPtr conf, virDomainDefPtr def)
612 613 614
{
    int val;
    char *listenAddr = NULL;
615
    int hvm = def->os.type == VIR_DOMAIN_OSTYPE_HVM;
616 617
    virDomainGraphicsDefPtr graphics = NULL;

618
    if (hvm) {
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
        if (xenConfigGetBool(conf, "vnc", &val, 0) < 0)
            goto cleanup;
        if (val) {
            if (VIR_ALLOC(graphics) < 0)
                goto cleanup;
            graphics->type = VIR_DOMAIN_GRAPHICS_TYPE_VNC;
            if (xenConfigGetBool(conf, "vncunused", &val, 1) < 0)
                goto cleanup;
            graphics->data.vnc.autoport = val ? 1 : 0;
            if (!graphics->data.vnc.autoport) {
                unsigned long vncdisplay;
                if (xenConfigGetULong(conf, "vncdisplay", &vncdisplay, 0) < 0)
                    goto cleanup;
                graphics->data.vnc.port = (int)vncdisplay + 5900;
            }

            if (xenConfigCopyStringOpt(conf, "vnclisten", &listenAddr) < 0)
                goto cleanup;
637
            if (virDomainGraphicsListenAppendAddress(graphics, listenAddr) < 0)
638
                goto cleanup;
639
            VIR_FREE(listenAddr);
640

641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
            if (xenConfigCopyStringOpt(conf, "vncpasswd", &graphics->data.vnc.auth.passwd) < 0)
                goto cleanup;
            if (xenConfigCopyStringOpt(conf, "keymap", &graphics->data.vnc.keymap) < 0)
                goto cleanup;
            if (VIR_ALLOC_N(def->graphics, 1) < 0)
                goto cleanup;
            def->graphics[0] = graphics;
            def->ngraphics = 1;
            graphics = NULL;
        } else {
            if (xenConfigGetBool(conf, "sdl", &val, 0) < 0)
                goto cleanup;
            if (val) {
                if (VIR_ALLOC(graphics) < 0)
                    goto cleanup;
                graphics->type = VIR_DOMAIN_GRAPHICS_TYPE_SDL;
                if (xenConfigCopyStringOpt(conf, "display", &graphics->data.sdl.display) < 0)
                    goto cleanup;
                if (xenConfigCopyStringOpt(conf, "xauthority", &graphics->data.sdl.xauth) < 0)
                    goto cleanup;
                if (VIR_ALLOC_N(def->graphics, 1) < 0)
                    goto cleanup;
                def->graphics[0] = graphics;
                def->ngraphics = 1;
                graphics = NULL;
            }
        }
    }

    if (!hvm && def->graphics == NULL) { /* New PV guests use this format */
671
        VIR_AUTOSTRINGLIST vfbs = NULL;
672 673 674
        int rc;

        if ((rc = virConfGetValueStringList(conf, "vfb", false, &vfbs)) == 1) {
675 676 677
            char vfb[MAX_VFB];
            char *key = vfb;

678
            if (virStrcpyStatic(vfb, *vfbs) < 0) {
679 680
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("VFB %s too big for destination"),
681
                               *vfbs);
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
                goto cleanup;
            }

            if (VIR_ALLOC(graphics) < 0)
                goto cleanup;
            if (strstr(key, "type=sdl"))
                graphics->type = VIR_DOMAIN_GRAPHICS_TYPE_SDL;
            else
                graphics->type = VIR_DOMAIN_GRAPHICS_TYPE_VNC;
            while (key) {
                char *nextkey = strchr(key, ',');
                char *end = nextkey;
                if (nextkey) {
                    *end = '\0';
                    nextkey++;
                }

                if (!strchr(key, '='))
                    break;
                if (graphics->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
                    if (STRPREFIX(key, "vncunused=")) {
                        if (STREQ(key + 10, "1"))
                            graphics->data.vnc.autoport = true;
                    } else if (STRPREFIX(key, "vnclisten=")) {
706
                        if (VIR_STRDUP(listenAddr, key+10) < 0)
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
                            goto cleanup;
                    } else if (STRPREFIX(key, "vncpasswd=")) {
                        if (VIR_STRDUP(graphics->data.vnc.auth.passwd, key + 10) < 0)
                            goto cleanup;
                    } else if (STRPREFIX(key, "keymap=")) {
                        if (VIR_STRDUP(graphics->data.vnc.keymap, key + 7) < 0)
                            goto cleanup;
                    } else if (STRPREFIX(key, "vncdisplay=")) {
                        if (virStrToLong_i(key + 11, NULL, 10,
                                           &graphics->data.vnc.port) < 0) {
                            virReportError(VIR_ERR_INTERNAL_ERROR,
                                           _("invalid vncdisplay value '%s'"),
                                           key + 11);
                            goto cleanup;
                        }
                        graphics->data.vnc.port += 5900;
                    }
                } else {
                    if (STRPREFIX(key, "display=")) {
                        if (VIR_STRDUP(graphics->data.sdl.display, key + 8) < 0)
                            goto cleanup;
                    } else if (STRPREFIX(key, "xauthority=")) {
                        if (VIR_STRDUP(graphics->data.sdl.xauth, key + 11) < 0)
                            goto cleanup;
                    }
                }

                while (nextkey && (nextkey[0] == ',' ||
                                   nextkey[0] == ' ' ||
                                   nextkey[0] == '\t'))
                    nextkey++;
                key = nextkey;
            }
740 741 742 743 744 745
            if (graphics->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
                if (virDomainGraphicsListenAppendAddress(graphics,
                                                         listenAddr) < 0)
                    goto cleanup;
                VIR_FREE(listenAddr);
            }
746 747 748 749 750
            if (VIR_ALLOC_N(def->graphics, 1) < 0)
                goto cleanup;
            def->graphics[0] = graphics;
            def->ngraphics = 1;
            graphics = NULL;
751 752 753
        } else {
            if (xenHandleConfGetValueStringListErrors(rc) < 0)
                goto cleanup;
754 755 756 757 758 759 760 761 762 763 764 765 766
        }
    }

    return 0;

 cleanup:
    virDomainGraphicsDefFree(graphics);
    VIR_FREE(listenAddr);
    return -1;
}


static int
767
xenParseCharDev(virConfPtr conf, virDomainDefPtr def, const char *nativeFormat)
768
{
769
    VIR_AUTOSTRINGLIST serials = NULL;
770 771
    virDomainChrDefPtr chr = NULL;

772
    if (def->os.type == VIR_DOMAIN_OSTYPE_HVM) {
773
        VIR_AUTOFREE(char *) parallel = NULL;
774 775
        int rc;

776
        if (xenConfigGetString(conf, "parallel", &parallel, NULL) < 0)
777
            goto cleanup;
778 779
        if (parallel && STRNEQ(parallel, "none") &&
            !(chr = xenParseSxprChar(parallel, NULL)))
780 781
            goto cleanup;
        if (chr) {
782
            if (VIR_ALLOC_N(def->parallels, 1) < 0)
783 784 785 786 787 788 789 790 791 792
                goto cleanup;

            chr->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_PARALLEL;
            chr->target.port = 0;
            def->parallels[0] = chr;
            def->nparallels++;
            chr = NULL;
        }

        /* Try to get the list of values to support multiple serial ports */
793
        if ((rc = virConfGetValueStringList(conf, "serial", false, &serials)) == 1) {
794
            char **entries;
795 796
            int portnum = -1;

797 798 799 800 801 802
            if (STREQ(nativeFormat, XEN_CONFIG_FORMAT_XM)) {
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                               _("Multiple serial devices are not supported by xen-xm"));
                goto cleanup;
            }

803
            for (entries = serials; *entries; entries++) {
804
                char *port = *entries;
805 806

                portnum++;
807
                if (STREQ(port, "none"))
808 809 810 811 812 813
                    continue;

                if (!(chr = xenParseSxprChar(port, NULL)))
                    goto cleanup;
                chr->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL;
                chr->target.port = portnum;
814
                if (VIR_APPEND_ELEMENT(def->serials, def->nserials, chr) < 0)
815 816 817
                    goto cleanup;
            }
        } else {
818
            VIR_AUTOFREE(char *) serial = NULL;
819 820 821 822

            if (xenHandleConfGetValueStringListErrors(rc) < 0)
                goto cleanup;

823
            /* If domain is not using multiple serial ports we parse data old way */
824
            if (xenConfigGetString(conf, "serial", &serial, NULL) < 0)
825
                goto cleanup;
826 827
            if (serial && STRNEQ(serial, "none") &&
                !(chr = xenParseSxprChar(serial, NULL)))
828 829
                goto cleanup;
            if (chr) {
830
                if (VIR_ALLOC_N(def->serials, 1) < 0)
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
                    goto cleanup;
                chr->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL;
                chr->target.port = 0;
                def->serials[0] = chr;
                def->nserials++;
            }
        }
    } else {
        if (VIR_ALLOC_N(def->consoles, 1) < 0)
            goto cleanup;
        def->nconsoles = 1;
        if (!(def->consoles[0] = xenParseSxprChar("pty", NULL)))
            goto cleanup;
        def->consoles[0]->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE;
        def->consoles[0]->target.port = 0;
        def->consoles[0]->targetType = VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_XEN;
    }

    return 0;

 cleanup:
    virDomainChrDefFree(chr);
    return -1;
}


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 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
xenParseVifBridge(virDomainNetDefPtr net, char *bridge)
{
    char *vlanstr;
    unsigned int tag;

    if ((vlanstr = strchr(bridge, '.'))) {
        /* 'bridge' string contains a bridge name and single vlan tag */
        if (VIR_STRNDUP(net->data.bridge.brname, bridge, vlanstr - bridge) < 0)
            return -1;

        vlanstr++;
        if (virStrToLong_ui(vlanstr, NULL, 10, &tag) < 0)
            return -1;

        if (VIR_ALLOC_N(net->vlan.tag, 1) < 0)
            return -1;

        net->vlan.tag[0] = tag;
        net->vlan.nTags = 1;

        if (VIR_ALLOC(net->virtPortProfile) < 0)
            return -1;

        net->virtPortProfile->virtPortType = VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH;
        return 0;
    } else if ((vlanstr = strchr(bridge, ':'))) {
        /* 'bridge' string contains a bridge name and one or more vlan trunks */
        size_t i;
        size_t nvlans = 0;
        char **vlanstr_list = virStringSplit(bridge, ":", 0);

        if (!vlanstr_list)
            return -1;

        if (VIR_STRDUP(net->data.bridge.brname, vlanstr_list[0]) < 0) {
            virStringListFree(vlanstr_list);
            return -1;
        }

        for (i = 1; vlanstr_list[i]; i++)
            nvlans++;

        if (VIR_ALLOC_N(net->vlan.tag, nvlans) < 0) {
            virStringListFree(vlanstr_list);
            return -1;
        }

        for (i = 1; i <= nvlans; i++) {
            if (virStrToLong_ui(vlanstr_list[i], NULL, 10, &tag) < 0) {
                virStringListFree(vlanstr_list);
                return -1;
            }
            net->vlan.tag[i - 1] = tag;
        }
        net->vlan.nTags = nvlans;
        net->vlan.trunk = true;
        virStringListFree(vlanstr_list);

        if (VIR_ALLOC(net->virtPortProfile) < 0)
            return -1;

        net->virtPortProfile->virtPortType = VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH;
        return 0;
    } else {
        /* 'bridge' string only contains the bridge name */
        if (VIR_STRDUP(net->data.bridge.brname, bridge) < 0)
            return -1;
    }

    return 0;
}


931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 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
static const char *vif_bytes_per_sec_re = "^[0-9]+[GMK]?[Bb]/s$";

static int
xenParseSxprVifRate(const char *rate, unsigned long long *kbytes_per_sec)
{
    char *trate = NULL;
    char *p;
    regex_t rec;
    int err;
    char *suffix;
    unsigned long long tmp;
    int ret = -1;

    if (VIR_STRDUP(trate, rate) < 0)
        return -1;

    p = strchr(trate, '@');
    if (p != NULL)
        *p = 0;

    err = regcomp(&rec, vif_bytes_per_sec_re, REG_EXTENDED|REG_NOSUB);
    if (err != 0) {
        char error[100];
        regerror(err, &rec, error, sizeof(error));
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to compile regular expression '%s': %s"),
                       vif_bytes_per_sec_re, error);
        goto cleanup;
    }

    if (regexec(&rec, trate, 0, NULL, 0)) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Invalid rate '%s' specified"), rate);
        goto cleanup;
    }

    if (virStrToLong_ull(rate, &suffix, 10, &tmp)) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to parse rate '%s'"), rate);
        goto cleanup;
    }

    if (*suffix == 'G')
       tmp *= 1024 * 1024;
    else if (*suffix == 'M')
       tmp *= 1024;

    if (*suffix == 'b' || *(suffix + 1) == 'b')
       tmp /= 8;

    *kbytes_per_sec = tmp;
    ret = 0;

 cleanup:
    regfree(&rec);
    VIR_FREE(trate);
    return ret;
}


991 992
static virDomainNetDefPtr
xenParseVif(char *entry, const char *vif_typename)
993 994
{
    virDomainNetDefPtr net = NULL;
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
    virDomainNetDefPtr ret = NULL;
    char *script = NULL;
    char model[10];
    char type[10];
    char ip[128];
    char mac[18];
    char bridge[50];
    char vifname[50];
    char rate[50];
    char *key;

    bridge[0] = '\0';
    mac[0] = '\0';
    ip[0] = '\0';
    model[0] = '\0';
    type[0] = '\0';
    vifname[0] = '\0';
    rate[0] = '\0';

    key = entry;
    while (key) {
        char *data;
        char *nextkey = strchr(key, ',');

        if (!(data = strchr(key, '=')))
            return NULL;
        data++;

        if (STRPREFIX(key, "mac=")) {
1024
            int len = nextkey ? (nextkey - data) : strlen(data);
1025
            if (virStrncpy(mac, data, len, sizeof(mac)) < 0) {
1026 1027 1028 1029 1030 1031
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("MAC address %s too big for destination"),
                               data);
                return NULL;
            }
        } else if (STRPREFIX(key, "bridge=")) {
1032
            int len = nextkey ? (nextkey - data) : strlen(data);
1033
            if (virStrncpy(bridge, data, len, sizeof(bridge)) < 0) {
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Bridge %s too big for destination"),
                               data);
                return NULL;
            }
        } else if (STRPREFIX(key, "script=")) {
            int len = nextkey ? (nextkey - data) : strlen(data);
            VIR_FREE(script);
            if (VIR_STRNDUP(script, data, len) < 0)
                return NULL;
        } else if (STRPREFIX(key, "model=")) {
1045
            int len = nextkey ? (nextkey - data) : strlen(data);
1046
            if (virStrncpy(model, data, len, sizeof(model)) < 0) {
1047 1048 1049 1050 1051 1052
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Model %s too big for destination"),
                               data);
                return NULL;
            }
        } else if (STRPREFIX(key, "type=")) {
1053
            int len = nextkey ? (nextkey - data) : strlen(data);
1054
            if (virStrncpy(type, data, len, sizeof(type)) < 0) {
1055 1056 1057 1058 1059 1060
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Type %s too big for destination"),
                               data);
                return NULL;
            }
        } else if (STRPREFIX(key, "vifname=")) {
1061
            int len = nextkey ? (nextkey - data) : strlen(data);
1062
            if (virStrncpy(vifname, data, len, sizeof(vifname)) < 0) {
1063 1064 1065 1066 1067 1068
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Vifname %s too big for destination"),
                               data);
                return NULL;
            }
        } else if (STRPREFIX(key, "ip=")) {
1069
            int len = nextkey ? (nextkey - data) : strlen(data);
1070
            if (virStrncpy(ip, data, len, sizeof(ip)) < 0) {
1071 1072 1073 1074 1075
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("IP %s too big for destination"), data);
                return NULL;
            }
        } else if (STRPREFIX(key, "rate=")) {
1076
            int len = nextkey ? (nextkey - data) : strlen(data);
1077
            if (virStrncpy(rate, data, len, sizeof(rate)) < 0) {
1078 1079 1080 1081 1082
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("rate %s too big for destination"), data);
                return NULL;
            }
        }
1083

1084 1085 1086 1087 1088 1089
        while (nextkey && (nextkey[0] == ',' ||
                           nextkey[0] == ' ' ||
                           nextkey[0] == '\t'))
            nextkey++;
        key = nextkey;
    }
1090

1091 1092
    if (VIR_ALLOC(net) < 0)
        goto cleanup;
1093

1094 1095 1096 1097 1098 1099 1100
    if (mac[0]) {
        if (virMacAddrParse(mac, &net->mac) < 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("malformed mac address '%s'"), mac);
            goto cleanup;
        }
    }
1101

1102 1103 1104 1105 1106 1107
    if (bridge[0] || STREQ_NULLABLE(script, "vif-bridge") ||
        STREQ_NULLABLE(script, "vif-vnic")) {
        net->type = VIR_DOMAIN_NET_TYPE_BRIDGE;
    } else {
        net->type = VIR_DOMAIN_NET_TYPE_ETHERNET;
    }
1108

1109 1110
    if (net->type == VIR_DOMAIN_NET_TYPE_BRIDGE && bridge[0]) {
        if (xenParseVifBridge(net, bridge) < 0)
1111 1112 1113 1114 1115
            goto cleanup;
    }
    if (ip[0]) {
        char **ip_list = virStringSplit(ip, " ", 0);
        size_t i;
1116

1117 1118
        if (!ip_list)
            goto cleanup;
1119

1120 1121 1122 1123
        for (i = 0; ip_list[i]; i++) {
            if (virDomainNetAppendIPAddress(net, ip_list[i], 0, 0) < 0) {
                virStringListFree(ip_list);
                goto cleanup;
1124
            }
1125 1126 1127
        }
        virStringListFree(ip_list);
    }
1128

1129 1130 1131
    if (script && script[0] &&
        VIR_STRDUP(net->script, script) < 0)
        goto cleanup;
1132

1133 1134 1135 1136 1137 1138 1139
    if (model[0]) {
        if (virDomainNetSetModelString(net, model) < 0)
            goto cleanup;
    } else {
        if (type[0] && STREQ(type, vif_typename))
            net->model = VIR_DOMAIN_NET_MODEL_NETFRONT;
    }
1140

1141 1142 1143
    if (vifname[0] &&
        VIR_STRDUP(net->ifname, vifname) < 0)
        goto cleanup;
1144

1145 1146 1147
    if (rate[0]) {
        virNetDevBandwidthPtr bandwidth;
        unsigned long long kbytes_per_sec;
1148

1149 1150
        if (xenParseSxprVifRate(rate, &kbytes_per_sec) < 0)
            goto cleanup;
1151

1152 1153
        if (VIR_ALLOC(bandwidth) < 0)
            goto cleanup;
1154

1155 1156 1157 1158
        if (VIR_ALLOC(bandwidth->out) < 0) {
            VIR_FREE(bandwidth);
            goto cleanup;
        }
1159

1160 1161 1162
        bandwidth->out->average = kbytes_per_sec;
        net->bandwidth = bandwidth;
    }
1163

1164
    VIR_STEAL_PTR(ret, net);
1165

1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
 cleanup:
    virDomainNetDefFree(net);
    VIR_FREE(script);
    return ret;
}


static int
xenParseVifList(virConfPtr conf, virDomainDefPtr def, const char *vif_typename)
{
    virConfValuePtr list = virConfGetValue(conf, "vif");

    if (!list || list->type != VIR_CONF_LIST)
        return 0;

    for (list = list->list; list; list = list->next) {
        virDomainNetDefPtr net = NULL;
        int rc;
1184

1185 1186 1187 1188 1189 1190 1191 1192
        if ((list->type != VIR_CONF_STRING) || (list->str == NULL))
            continue;

        if (!(net = xenParseVif(list->str, vif_typename)))
            return -1;

        rc = VIR_APPEND_ELEMENT(def->nets, def->nnets, net);
        if (rc < 0) {
1193
            virDomainNetDefFree(net);
1194
            return -1;
1195 1196 1197 1198 1199 1200 1201
        }
    }

    return 0;
}


1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
/**
 * xenParseSxprSound:
 * @def: the domain config
 * @str: comma separated list of sound models
 *
 * This parses out sound devices from the domain S-expression
 *
 * Returns 0 if successful or -1 if failed.
 */
static int
xenParseSxprSound(virDomainDefPtr def,
                  const char *str)
{
    if (STREQ(str, "all")) {
        size_t i;

        /*
         * Special compatibility code for Xen with a bogus
         * sound=all in config.
         *
         * NB deliberately, don't include all possible
         * sound models anymore, just the 2 that were
         * historically present in Xen's QEMU.
         *
         * ie just es1370 + sb16.
         *
         * Hence use of MODEL_ES1370 + 1, instead of MODEL_LAST
         */

        if (VIR_ALLOC_N(def->sounds,
                        VIR_DOMAIN_SOUND_MODEL_ES1370 + 1) < 0)
            return -1;


        for (i = 0; i < (VIR_DOMAIN_SOUND_MODEL_ES1370 + 1); i++) {
            virDomainSoundDefPtr sound;
            if (VIR_ALLOC(sound) < 0)
                return -1;
            sound->model = i;
            def->sounds[def->nsounds++] = sound;
        }
    } else {
        char model[10];
        const char *offset = str, *offset2;

        do {
            int len;
            virDomainSoundDefPtr sound;
            offset2 = strchr(offset, ',');
            if (offset2)
                len = (offset2 - offset);
            else
                len = strlen(offset);
            if (virStrncpy(model, offset, len, sizeof(model)) < 0) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Sound model %s too big for destination"),
                               offset);
                return -1;
            }

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

            if ((sound->model = virDomainSoundModelTypeFromString(model)) < 0) {
                VIR_FREE(sound);
                return -1;
            }

            if (VIR_APPEND_ELEMENT(def->sounds, def->nsounds, sound) < 0) {
                virDomainSoundDefFree(sound);
                return -1;
            }

            offset = offset2 ? offset2 + 1 : NULL;
        } while (offset);
    }

    return 0;
}


1283 1284 1285
static int
xenParseEmulatedDevices(virConfPtr conf, virDomainDefPtr def)
{
1286
    VIR_AUTOFREE(char *) str = NULL;
1287

1288
    if (def->os.type == VIR_DOMAIN_OSTYPE_HVM) {
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
        if (xenConfigGetString(conf, "soundhw", &str, NULL) < 0)
            return -1;

        if (str &&
            xenParseSxprSound(def, str) < 0)
            return -1;
    }

    return 0;
}


static int
xenParseGeneralMeta(virConfPtr conf, virDomainDefPtr def, virCapsPtr caps)
{
1304
    virCapsDomainDataPtr capsdata = NULL;
1305
    VIR_AUTOFREE(char *) str = NULL;
1306
    int ret = -1;
1307 1308

    if (xenConfigCopyString(conf, "name", &def->name) < 0)
1309
        goto out;
1310 1311

    if (xenConfigGetUUID(conf, "uuid", def->uuid) < 0)
1312
        goto out;
1313

1314 1315
    def->os.type = VIR_DOMAIN_OSTYPE_XEN;

1316 1317
    if (xenConfigGetString(conf, "type", &str, NULL) == 0 && str) {
        if (STREQ(str, "pv")) {
1318 1319 1320
            def->os.type = VIR_DOMAIN_OSTYPE_XEN;
        } else if (STREQ(str, "pvh")) {
            def->os.type = VIR_DOMAIN_OSTYPE_XENPVH;
1321
        } else if (STREQ(str, "hvm")) {
1322
            def->os.type = VIR_DOMAIN_OSTYPE_HVM;
1323 1324 1325 1326 1327 1328 1329
        } else {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("type %s is not supported"), str);
            return -1;
        }
    } else {
        if ((xenConfigGetString(conf, "builder", &str, "linux") == 0) &&
1330 1331 1332
            STREQ(str, "hvm")) {
            def->os.type = VIR_DOMAIN_OSTYPE_HVM;
        }
1333
    }
1334

1335 1336 1337
    if (!(capsdata = virCapabilitiesDomainDataLookup(caps, def->os.type,
            VIR_ARCH_NONE, def->virtType, NULL, NULL)))
        goto out;
1338

1339 1340 1341
    def->os.arch = capsdata->arch;
    if (VIR_STRDUP(def->os.machine, capsdata->machinetype) < 0)
        goto out;
1342

1343 1344 1345 1346
    ret = 0;
 out:
    VIR_FREE(capsdata);
    return ret;
1347 1348 1349 1350 1351 1352 1353 1354 1355
}


/*
 * A convenience function for parsing all config common to both XM and XL
 */
int
xenParseConfigCommon(virConfPtr conf,
                     virDomainDefPtr def,
1356
                     virCapsPtr caps,
1357 1358
                     const char *nativeFormat,
                     virDomainXMLOptionPtr xmlopt)
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
{
    if (xenParseGeneralMeta(conf, def, caps) < 0)
        return -1;

    if (xenParseMem(conf, def) < 0)
        return -1;

    if (xenParseEventsActions(conf, def) < 0)
        return -1;

1369
    if (xenParseCPUFeatures(conf, def, xmlopt) < 0)
1370 1371
        return -1;

1372
    if (xenParseTimeOffset(conf, def) < 0)
1373 1374 1375 1376 1377
        return -1;

    if (xenConfigCopyStringOpt(conf, "device_model", &def->emulator) < 0)
        return -1;

1378
    if (STREQ(nativeFormat, XEN_CONFIG_FORMAT_XL)) {
1379
        if (xenParseVifList(conf, def, "vif") < 0)
1380 1381
            return -1;
    } else if (STREQ(nativeFormat, XEN_CONFIG_FORMAT_XM)) {
1382
        if (xenParseVifList(conf, def, "netfront") < 0)
1383 1384 1385 1386
            return -1;
    } else {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unsupported config type %s"), nativeFormat);
1387
        return -1;
1388
    }
1389

1390
    if (xenParsePCIList(conf, def) < 0)
1391 1392 1393 1394 1395
        return -1;

    if (xenParseEmulatedDevices(conf, def) < 0)
        return -1;

1396
    if (xenParseVfb(conf, def) < 0)
1397 1398
        return -1;

1399
    if (xenParseCharDev(conf, def, nativeFormat) < 0)
1400 1401 1402 1403 1404 1405
        return -1;

    return 0;
}


1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 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 1478 1479 1480 1481 1482 1483 1484
/**
 * xenFormatSxprChr:
 * @def: the domain config
 * @buf: a buffer for the result S-expression
 *
 * Convert the character device part of the domain config into a S-expression
 * in buf.
 *
 * Returns 0 in case of success, -1 in case of error
 */
static int
xenFormatSxprChr(virDomainChrDefPtr def,
                 virBufferPtr buf)
{
    const char *type = virDomainChrTypeToString(def->source->type);

    if (!type) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("unexpected chr device type"));
        return -1;
    }

    switch (def->source->type) {
    case VIR_DOMAIN_CHR_TYPE_NULL:
    case VIR_DOMAIN_CHR_TYPE_STDIO:
    case VIR_DOMAIN_CHR_TYPE_VC:
    case VIR_DOMAIN_CHR_TYPE_PTY:
        virBufferAdd(buf, type, -1);
        break;

    case VIR_DOMAIN_CHR_TYPE_FILE:
    case VIR_DOMAIN_CHR_TYPE_PIPE:
        virBufferAsprintf(buf, "%s:", type);
        virBufferEscapeSexpr(buf, "%s", def->source->data.file.path);
        break;

    case VIR_DOMAIN_CHR_TYPE_DEV:
        virBufferEscapeSexpr(buf, "%s", def->source->data.file.path);
        break;

    case VIR_DOMAIN_CHR_TYPE_TCP:
        virBufferAsprintf(buf, "%s:%s:%s%s",
                          (def->source->data.tcp.protocol
                           == VIR_DOMAIN_CHR_TCP_PROTOCOL_RAW ?
                           "tcp" : "telnet"),
                          NULLSTR_EMPTY(def->source->data.tcp.host),
                          NULLSTR_EMPTY(def->source->data.tcp.service),
                          (def->source->data.tcp.listen ?
                           ",server,nowait" : ""));
        break;

    case VIR_DOMAIN_CHR_TYPE_UDP:
        virBufferAsprintf(buf, "%s:%s:%s@%s:%s", type,
                          NULLSTR_EMPTY(def->source->data.udp.connectHost),
                          NULLSTR_EMPTY(def->source->data.udp.connectService),
                          NULLSTR_EMPTY(def->source->data.udp.bindHost),
                          NULLSTR_EMPTY(def->source->data.udp.bindService));
        break;

    case VIR_DOMAIN_CHR_TYPE_UNIX:
        virBufferAsprintf(buf, "%s:", type);
        virBufferEscapeSexpr(buf, "%s", def->source->data.nix.path);
        if (def->source->data.nix.listen)
            virBufferAddLit(buf, ",server,nowait");
        break;

    default:
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("unsupported chr device type '%s'"), type);
        return -1;
    }

    if (virBufferCheckError(buf) < 0)
        return -1;

    return 0;
}


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
static int
xenFormatSerial(virConfValuePtr list, virDomainChrDefPtr serial)
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    virConfValuePtr val, tmp;
    int ret;

    if (serial) {
        ret = xenFormatSxprChr(serial, &buf);
        if (ret < 0)
            goto cleanup;
    } else {
        virBufferAddLit(&buf, "none");
    }
    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;

    return 0;

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

1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
char *
xenMakeIPList(virNetDevIPInfoPtr guestIP)
{
    size_t i;
    char **address_array;
    char *ret = NULL;

    if (VIR_ALLOC_N(address_array, guestIP->nips + 1) < 0)
        return NULL;

    for (i = 0; i < guestIP->nips; i++) {
        address_array[i] = virSocketAddrFormat(&guestIP->ips[i]->address);
        if (!address_array[i])
            goto cleanup;
    }
    ret = virStringListJoin((const char**)address_array, " ");

 cleanup:
1540
    virStringListFree(address_array);
1541 1542
    return ret;
}
1543 1544 1545 1546 1547

static int
xenFormatNet(virConnectPtr conn,
             virConfValuePtr list,
             virDomainNetDefPtr net,
1548 1549
             int hvm,
             const char *vif_typename)
1550 1551 1552 1553 1554 1555 1556 1557 1558
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    virConfValuePtr val, tmp;
    char macaddr[VIR_MAC_STRING_BUFLEN];

    virBufferAsprintf(&buf, "mac=%s", virMacAddrFormat(&net->mac, macaddr));

    switch (net->type) {
    case VIR_DOMAIN_NET_TYPE_BRIDGE:
1559 1560 1561 1562 1563 1564
    {
        virNetDevVPortProfilePtr port_profile = virDomainNetGetActualVirtPortProfile(net);
        virNetDevVlanPtr virt_vlan = virDomainNetGetActualVlan(net);
        const char *script = net->script;
        size_t i;

1565
        virBufferAsprintf(&buf, ",bridge=%s", net->data.bridge.brname);
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
        if (port_profile &&
            port_profile->virtPortType == VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH) {
            if (!script)
                script = "vif-openvswitch";
            /*
             * libxl_device_nic->bridge supports an extended format for
             * specifying VLAN tags and trunks
             *
             * BRIDGE_NAME[.VLAN][:TRUNK:TRUNK]
             */
            if (virt_vlan && virt_vlan->nTags > 0) {
                if (virt_vlan->trunk) {
                    for (i = 0; i < virt_vlan->nTags; i++)
                        virBufferAsprintf(&buf, ":%d", virt_vlan->tag[i]);
                } else {
                    virBufferAsprintf(&buf, ".%d", virt_vlan->tag[0]);
                }
            }
        }

1586 1587
        if (net->guestIP.nips > 0) {
            char *ipStr = xenMakeIPList(&net->guestIP);
1588 1589 1590
            virBufferAsprintf(&buf, ",ip=%s", ipStr);
            VIR_FREE(ipStr);
        }
1591 1592 1593
        virBufferAsprintf(&buf, ",script=%s", script ? script : DEFAULT_VIF_SCRIPT);
    }
    break;
1594 1595 1596 1597

    case VIR_DOMAIN_NET_TYPE_ETHERNET:
        if (net->script)
            virBufferAsprintf(&buf, ",script=%s", net->script);
1598 1599
        if (net->guestIP.nips > 0) {
            char *ipStr = xenMakeIPList(&net->guestIP);
1600 1601 1602
            virBufferAsprintf(&buf, ",ip=%s", ipStr);
            VIR_FREE(ipStr);
        }
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
        break;

    case VIR_DOMAIN_NET_TYPE_NETWORK:
    {
        virNetworkPtr network = virNetworkLookupByName(conn, net->data.network.name);
        char *bridge;
        if (!network) {
            virReportError(VIR_ERR_NO_NETWORK, "%s",
                           net->data.network.name);
            return -1;
        }
        bridge = virNetworkGetBridgeName(network);
1615
        virObjectUnref(network);
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
        if (!bridge) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("network %s is not active"),
                           net->data.network.name);
            return -1;
        }

        virBufferAsprintf(&buf, ",bridge=%s", bridge);
        virBufferAsprintf(&buf, ",script=%s", DEFAULT_VIF_SCRIPT);
    }
    break;

1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
    case VIR_DOMAIN_NET_TYPE_VHOSTUSER:
    case VIR_DOMAIN_NET_TYPE_SERVER:
    case VIR_DOMAIN_NET_TYPE_CLIENT:
    case VIR_DOMAIN_NET_TYPE_MCAST:
    case VIR_DOMAIN_NET_TYPE_INTERNAL:
    case VIR_DOMAIN_NET_TYPE_DIRECT:
    case VIR_DOMAIN_NET_TYPE_HOSTDEV:
    case VIR_DOMAIN_NET_TYPE_UDP:
    case VIR_DOMAIN_NET_TYPE_USER:
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("Unsupported net type '%s'"),
                       virDomainNetTypeToString(net->type));
        goto cleanup;

    case VIR_DOMAIN_NET_TYPE_LAST:
1642
    default:
1643
        virReportEnumRangeError(virDomainNetType, net->type);
1644 1645 1646
        goto cleanup;
    }

1647 1648 1649 1650
    if (virDomainNetGetModelString(net)) {
        if (!hvm) {
            virBufferAsprintf(&buf, ",model=%s",
                              virDomainNetGetModelString(net));
1651
        } else {
1652
            if (net->model == VIR_DOMAIN_NET_MODEL_NETFRONT)
1653 1654 1655 1656
                virBufferAsprintf(&buf, ",type=%s", vif_typename);
            else
                virBufferAsprintf(&buf, ",model=%s",
                                  virDomainNetGetModelString(net));
1657 1658 1659 1660 1661 1662 1663
        }
    }

    if (net->ifname)
        virBufferAsprintf(&buf, ",vifname=%s",
                          net->ifname);

1664 1665 1666
    if (net->bandwidth && net->bandwidth->out && net->bandwidth->out->average)
        virBufferAsprintf(&buf, ",rate=%lluKB/s", net->bandwidth->out->average);

1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
    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;

    return 0;

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


static int
xenFormatPCI(virConfPtr conf, virDomainDefPtr def)
{
    virConfValuePtr pciVal = NULL;
    int hasPCI = 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_PCI)
            hasPCI = 1;

    if (!hasPCI)
        return 0;

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

    pciVal->type = VIR_CONF_LIST;
    pciVal->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_PCI) {
            virConfValuePtr val, tmp;
            char *buf;

            if (virAsprintf(&buf, "%04x:%02x:%02x.%x",
                            def->hostdevs[i]->source.subsys.u.pci.addr.domain,
                            def->hostdevs[i]->source.subsys.u.pci.addr.bus,
                            def->hostdevs[i]->source.subsys.u.pci.addr.slot,
                            def->hostdevs[i]->source.subsys.u.pci.addr.function) < 0)
                goto error;

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

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

    return 0;

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


static int
xenFormatGeneralMeta(virConfPtr conf, virDomainDefPtr def)
{
    char uuid[VIR_UUID_STRING_BUFLEN];

1762
    if (xenConfigSetString(conf, "name", def->name) < 0)
1763 1764 1765
        return -1;

    virUUIDFormat(def->uuid, uuid);
1766
    if (xenConfigSetString(conf, "uuid", uuid) < 0)
1767 1768 1769 1770 1771 1772 1773 1774 1775
        return -1;

    return 0;
}


static int
xenFormatMem(virConfPtr conf, virDomainDefPtr def)
{
1776
    if (xenConfigSetInt(conf, "maxmem",
1777
                        VIR_DIV_UP(virDomainDefGetMemoryTotal(def), 1024)) < 0)
1778 1779
        return -1;

1780 1781
    if (xenConfigSetInt(conf, "memory",
                        VIR_DIV_UP(def->mem.cur_balloon, 1024)) < 0)
1782 1783 1784 1785 1786 1787 1788
        return -1;

    return 0;
}


static int
1789
xenFormatTimeOffset(virConfPtr conf, virDomainDefPtr def)
1790 1791 1792
{
    int vmlocaltime;

1793 1794 1795 1796
    if (def->os.type == VIR_DOMAIN_OSTYPE_HVM) {
        /* >=3.1 HV: VARIABLE */
        int rtc_timeoffset;

1797
        switch (def->clock.offset) {
1798 1799 1800 1801
        case VIR_DOMAIN_CLOCK_OFFSET_VARIABLE:
            vmlocaltime = (int)def->clock.data.variable.basis;
            rtc_timeoffset = def->clock.data.variable.adjustment;
            break;
1802
        case VIR_DOMAIN_CLOCK_OFFSET_UTC:
1803 1804 1805 1806 1807
            if (def->clock.data.utc_reset) {
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                               _("unsupported clock adjustment='reset'"));
                return -1;
            }
1808
            vmlocaltime = 0;
1809
            rtc_timeoffset = 0;
1810 1811
            break;
        case VIR_DOMAIN_CLOCK_OFFSET_LOCALTIME:
1812 1813 1814 1815 1816
            if (def->clock.data.utc_reset) {
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                               _("unsupported clock adjustment='reset'"));
                return -1;
            }
1817
            vmlocaltime = 1;
1818
            rtc_timeoffset = 0;
1819 1820 1821 1822 1823 1824 1825
            break;
        default:
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("unsupported clock offset='%s'"),
                           virDomainClockOffsetTypeToString(def->clock.offset));
            return -1;
        }
1826 1827
        if (xenConfigSetInt(conf, "rtc_timeoffset", rtc_timeoffset) < 0)
            return -1;
1828
    } else {
1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
        /* PV: UTC and LOCALTIME */
        switch (def->clock.offset) {
        case VIR_DOMAIN_CLOCK_OFFSET_UTC:
            vmlocaltime = 0;
            break;
        case VIR_DOMAIN_CLOCK_OFFSET_LOCALTIME:
            vmlocaltime = 1;
            break;
        default:
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("unsupported clock offset='%s'"),
                           virDomainClockOffsetTypeToString(def->clock.offset));
            return -1;
        }
    } /* !hvm */
1844

1845
    if (xenConfigSetInt(conf, "localtime", vmlocaltime) < 0)
1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856
        return -1;

    return 0;
}


static int
xenFormatEventActions(virConfPtr conf, virDomainDefPtr def)
{
    const char *lifecycle = NULL;

1857
    if (!(lifecycle = virDomainLifecycleActionTypeToString(def->onPoweroff))) {
1858 1859 1860 1861
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected lifecycle action %d"), def->onPoweroff);
        return -1;
    }
1862
    if (xenConfigSetString(conf, "on_poweroff", lifecycle) < 0)
1863 1864 1865
        return -1;


1866
    if (!(lifecycle = virDomainLifecycleActionTypeToString(def->onReboot))) {
1867 1868 1869 1870
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected lifecycle action %d"), def->onReboot);
        return -1;
    }
1871
    if (xenConfigSetString(conf, "on_reboot", lifecycle) < 0)
1872 1873 1874
        return -1;


1875
    if (!(lifecycle = virDomainLifecycleActionTypeToString(def->onCrash))) {
1876 1877 1878 1879
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected lifecycle action %d"), def->onCrash);
        return -1;
    }
1880
    if (xenConfigSetString(conf, "on_crash", lifecycle) < 0)
1881 1882 1883 1884 1885 1886 1887
        return -1;

    return 0;
}


static int
1888 1889
xenFormatCharDev(virConfPtr conf, virDomainDefPtr def,
                 const char *nativeFormat)
1890 1891 1892
{
    size_t i;

1893
    if (def->os.type == VIR_DOMAIN_OSTYPE_HVM) {
1894 1895 1896 1897 1898 1899 1900 1901
        if (def->nparallels) {
            virBuffer buf = VIR_BUFFER_INITIALIZER;
            char *str;
            int ret;

            ret = xenFormatSxprChr(def->parallels[0], &buf);
            str = virBufferContentAndReset(&buf);
            if (ret == 0)
1902
                ret = xenConfigSetString(conf, "parallel", str);
1903 1904 1905 1906
            VIR_FREE(str);
            if (ret < 0)
                return -1;
        } else {
1907
            if (xenConfigSetString(conf, "parallel", "none") < 0)
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
                return -1;
        }

        if (def->nserials) {
            if ((def->nserials == 1) && (def->serials[0]->target.port == 0)) {
                virBuffer buf = VIR_BUFFER_INITIALIZER;
                char *str;
                int ret;

                ret = xenFormatSxprChr(def->serials[0], &buf);
                str = virBufferContentAndReset(&buf);
                if (ret == 0)
1920
                    ret = xenConfigSetString(conf, "serial", str);
1921 1922 1923 1924 1925 1926 1927 1928
                VIR_FREE(str);
                if (ret < 0)
                    return -1;
            } else {
                size_t j = 0;
                int maxport = -1, port;
                virConfValuePtr serialVal = NULL;

1929 1930 1931 1932 1933 1934
                if (STREQ(nativeFormat, XEN_CONFIG_FORMAT_XM)) {
                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                                   _("Multiple serial devices are not supported by xen-xm"));
                    return -1;
                }

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 1968 1969 1970
                if (VIR_ALLOC(serialVal) < 0)
                    return -1;

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

                for (i = 0; i < def->nserials; i++)
                    if (def->serials[i]->target.port > maxport)
                        maxport = def->serials[i]->target.port;

                for (port = 0; port <= maxport; port++) {
                    virDomainChrDefPtr chr = NULL;

                    for (j = 0; j < def->nserials; j++) {
                        if (def->serials[j]->target.port == port) {
                            chr = def->serials[j];
                            break;
                        }
                    }

                    if (xenFormatSerial(serialVal, chr) < 0) {
                        VIR_FREE(serialVal);
                        return -1;
                    }
                }

                if (serialVal->list != NULL) {
                    int ret = virConfSetValue(conf, "serial", serialVal);

                    serialVal = NULL;
                    if (ret < 0)
                        return -1;
                }
                VIR_FREE(serialVal);
            }
        } else {
1971
            if (xenConfigSetString(conf, "serial", "none") < 0)
1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985
                return -1;
        }
    }

    return 0;
}


static int
xenFormatCPUAllocation(virConfPtr conf, virDomainDefPtr def)
{
    int ret = -1;
    char *cpus = NULL;

1986 1987
    if (virDomainDefGetVcpus(def) < virDomainDefGetVcpusMax(def) &&
        xenConfigSetInt(conf, "maxvcpus", virDomainDefGetVcpusMax(def)) < 0)
1988
        goto cleanup;
1989
    if (xenConfigSetInt(conf, "vcpus", virDomainDefGetVcpus(def)) < 0)
1990 1991 1992 1993 1994 1995 1996 1997
        goto cleanup;

    if ((def->cpumask != NULL) &&
        ((cpus = virBitmapFormat(def->cpumask)) == NULL)) {
        goto cleanup;
    }

    if (cpus &&
1998
        xenConfigSetString(conf, "cpus", cpus) < 0)
1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
        goto cleanup;

    ret = 0;

 cleanup:
    VIR_FREE(cpus);
    return ret;
}


static int
2010
xenFormatCPUFeatures(virConfPtr conf, virDomainDefPtr def)
2011 2012
{
    size_t i;
2013
    bool hvm = !!(def->os.type == VIR_DOMAIN_OSTYPE_HVM);
2014

2015
    if (hvm) {
2016 2017 2018
        if (xenConfigSetInt(conf, "pae",
                            (def->features[VIR_DOMAIN_FEATURE_PAE] ==
                            VIR_TRISTATE_SWITCH_ON) ? 1 : 0) < 0)
2019 2020
            return -1;

2021 2022 2023
        if (xenConfigSetInt(conf, "acpi",
                            (def->features[VIR_DOMAIN_FEATURE_ACPI] ==
                            VIR_TRISTATE_SWITCH_ON) ? 1 : 0) < 0)
2024 2025
            return -1;

2026 2027 2028
        if (xenConfigSetInt(conf, "apic",
                            (def->features[VIR_DOMAIN_FEATURE_APIC] ==
                            VIR_TRISTATE_SWITCH_ON) ? 1 : 0) < 0)
2029 2030
            return -1;

2031 2032 2033 2034
        if (def->features[VIR_DOMAIN_FEATURE_HAP] == VIR_TRISTATE_SWITCH_OFF) {
            if (xenConfigSetInt(conf, "hap", 0) < 0)
                return -1;
        }
2035

2036 2037 2038 2039
        if (xenConfigSetInt(conf, "viridian",
                            (def->features[VIR_DOMAIN_FEATURE_VIRIDIAN] ==
                             VIR_TRISTATE_SWITCH_ON) ? 1 : 0) < 0)
            return -1;
2040
    }
2041

2042
    for (i = 0; i < def->clock.ntimers; i++) {
2043
        switch ((virDomainTimerNameType)def->clock.timers[i]->name) {
2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074
        case VIR_DOMAIN_TIMER_NAME_TSC:
            switch (def->clock.timers[i]->mode) {
            case VIR_DOMAIN_TIMER_MODE_NATIVE:
                if (xenConfigSetString(conf, "tsc_mode", "native") < 0)
                    return -1;
                break;
            case VIR_DOMAIN_TIMER_MODE_PARAVIRT:
                if (xenConfigSetString(conf, "tsc_mode", "native_paravirt") < 0)
                    return -1;
                break;
            case VIR_DOMAIN_TIMER_MODE_EMULATE:
                if (xenConfigSetString(conf, "tsc_mode", "always_emulate") < 0)
                    return -1;
                break;
            default:
                if (xenConfigSetString(conf, "tsc_mode", "default") < 0)
                    return -1;
            }
            break;

        case VIR_DOMAIN_TIMER_NAME_HPET:
            if (hvm) {
                int enable_hpet = def->clock.timers[i]->present != 0;

                /* disable hpet if 'present' is 0, enable otherwise */
                if (xenConfigSetInt(conf, "hpet", enable_hpet) < 0)
                    return -1;
            } else {
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                               _("unsupported timer type (name) '%s'"),
                               virDomainTimerNameTypeToString(def->clock.timers[i]->name));
2075
                return -1;
2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090
            }
            break;

        case VIR_DOMAIN_TIMER_NAME_PLATFORM:
        case VIR_DOMAIN_TIMER_NAME_KVMCLOCK:
        case VIR_DOMAIN_TIMER_NAME_HYPERVCLOCK:
        case VIR_DOMAIN_TIMER_NAME_RTC:
        case VIR_DOMAIN_TIMER_NAME_PIT:
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("unsupported timer type (name) '%s'"),
                           virDomainTimerNameTypeToString(def->clock.timers[i]->name));
            return -1;

        case VIR_DOMAIN_TIMER_NAME_LAST:
            break;
2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101
        }
    }

    return 0;
}


static int
xenFormatEmulator(virConfPtr conf, virDomainDefPtr def)
{
    if (def->emulator &&
2102
        xenConfigSetString(conf, "device_model", def->emulator) < 0)
2103 2104 2105 2106 2107 2108 2109
        return -1;

    return 0;
}


static int
2110
xenFormatVfb(virConfPtr conf, virDomainDefPtr def)
2111
{
2112
    int hvm = def->os.type == VIR_DOMAIN_OSTYPE_HVM ? 1 : 0;
2113

2114 2115
    if (def->ngraphics == 1 &&
        def->graphics[0]->type != VIR_DOMAIN_GRAPHICS_TYPE_SPICE) {
2116
        if (hvm) {
2117
            if (def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_SDL) {
2118
                if (xenConfigSetInt(conf, "sdl", 1) < 0)
2119 2120
                    return -1;

2121
                if (xenConfigSetInt(conf, "vnc", 0) < 0)
2122 2123 2124
                    return -1;

                if (def->graphics[0]->data.sdl.display &&
2125 2126
                    xenConfigSetString(conf, "display",
                                       def->graphics[0]->data.sdl.display) < 0)
2127 2128 2129
                    return -1;

                if (def->graphics[0]->data.sdl.xauth &&
2130 2131
                    xenConfigSetString(conf, "xauthority",
                                       def->graphics[0]->data.sdl.xauth) < 0)
2132 2133
                    return -1;
            } else {
2134
                virDomainGraphicsListenDefPtr glisten;
2135

2136
                if (xenConfigSetInt(conf, "sdl", 0) < 0)
2137 2138
                    return -1;

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

2142
                if (xenConfigSetInt(conf, "vncunused",
2143 2144 2145 2146
                              def->graphics[0]->data.vnc.autoport ? 1 : 0) < 0)
                    return -1;

                if (!def->graphics[0]->data.vnc.autoport &&
2147 2148
                    xenConfigSetInt(conf, "vncdisplay",
                                    def->graphics[0]->data.vnc.port - 5900) < 0)
2149 2150
                    return -1;

2151 2152 2153
                if ((glisten = virDomainGraphicsGetListen(def->graphics[0], 0)) &&
                    glisten->address &&
                    xenConfigSetString(conf, "vnclisten", glisten->address) < 0)
2154 2155 2156
                    return -1;

                if (def->graphics[0]->data.vnc.auth.passwd &&
2157 2158
                    xenConfigSetString(conf, "vncpasswd",
                                       def->graphics[0]->data.vnc.auth.passwd) < 0)
2159 2160 2161
                    return -1;

                if (def->graphics[0]->data.vnc.keymap &&
2162 2163
                    xenConfigSetString(conf, "keymap",
                                       def->graphics[0]->data.vnc.keymap) < 0)
2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179
                    return -1;
            }
        } else {
            virConfValuePtr vfb, disp;
            char *vfbstr = NULL;
            virBuffer buf = VIR_BUFFER_INITIALIZER;

            if (def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_SDL) {
                virBufferAddLit(&buf, "type=sdl");
                if (def->graphics[0]->data.sdl.display)
                    virBufferAsprintf(&buf, ",display=%s",
                                      def->graphics[0]->data.sdl.display);
                if (def->graphics[0]->data.sdl.xauth)
                    virBufferAsprintf(&buf, ",xauthority=%s",
                                      def->graphics[0]->data.sdl.xauth);
            } else {
2180
                virDomainGraphicsListenDefPtr glisten
2181
                    = virDomainGraphicsGetListen(def->graphics[0], 0);
2182 2183 2184 2185 2186 2187 2188

                virBufferAddLit(&buf, "type=vnc");
                virBufferAsprintf(&buf, ",vncunused=%d",
                                  def->graphics[0]->data.vnc.autoport ? 1 : 0);
                if (!def->graphics[0]->data.vnc.autoport)
                    virBufferAsprintf(&buf, ",vncdisplay=%d",
                                      def->graphics[0]->data.vnc.port - 5900);
2189 2190
                if (glisten && glisten->address)
                    virBufferAsprintf(&buf, ",vnclisten=%s", glisten->address);
2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230
                if (def->graphics[0]->data.vnc.auth.passwd)
                    virBufferAsprintf(&buf, ",vncpasswd=%s",
                                      def->graphics[0]->data.vnc.auth.passwd);
                if (def->graphics[0]->data.vnc.keymap)
                    virBufferAsprintf(&buf, ",keymap=%s",
                                      def->graphics[0]->data.vnc.keymap);
            }
            if (virBufferCheckError(&buf) < 0)
                return -1;

            vfbstr = virBufferContentAndReset(&buf);

            if (VIR_ALLOC(vfb) < 0) {
                VIR_FREE(vfbstr);
                return -1;
            }

            if (VIR_ALLOC(disp) < 0) {
                VIR_FREE(vfb);
                VIR_FREE(vfbstr);
                return -1;
            }

            vfb->type = VIR_CONF_LIST;
            vfb->list = disp;
            disp->type = VIR_CONF_STRING;
            disp->str = vfbstr;

            if (virConfSetValue(conf, "vfb", vfb) < 0)
                return -1;
        }
    }

    return 0;
}


static int
xenFormatSound(virConfPtr conf, virDomainDefPtr def)
{
2231 2232 2233 2234
    VIR_AUTOCLEAN(virBuffer) buf = VIR_BUFFER_INITIALIZER;
    const char * model;
    VIR_AUTOFREE(char *) str = NULL;
    size_t i;
2235

2236 2237 2238
    if (def->os.type != VIR_DOMAIN_OSTYPE_HVM ||
        !def->sounds)
        return 0;
2239

2240 2241 2242 2243 2244 2245
    for (i = 0; i < def->nsounds; i++) {
        if (!(model = virDomainSoundModelTypeToString(def->sounds[i]->model))) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unexpected sound model %d"),
                           def->sounds[i]->model);
            return -1;
2246
        }
2247 2248 2249
        if (i)
            virBufferAddChar(&buf, ',');
        virBufferEscapeSexpr(&buf, "%s", model);
2250 2251
    }

2252 2253
    if (virBufferCheckError(&buf) < 0)
        return -1;
2254

2255 2256 2257 2258
    str = virBufferContentAndReset(&buf);

    return xenConfigSetString(conf, "soundhw", str);
}
2259 2260 2261 2262 2263


static int
xenFormatVif(virConfPtr conf,
             virConnectPtr conn,
2264 2265
             virDomainDefPtr def,
             const char *vif_typename)
2266
{
2267 2268 2269
    virConfValuePtr netVal = NULL;
    size_t i;
    int hvm = def->os.type == VIR_DOMAIN_OSTYPE_HVM;
2270

2271
    if (VIR_ALLOC(netVal) < 0)
2272 2273 2274 2275 2276 2277
        goto cleanup;
    netVal->type = VIR_CONF_LIST;
    netVal->list = NULL;

    for (i = 0; i < def->nnets; i++) {
        if (xenFormatNet(conn, netVal, def->nets[i],
2278
                         hvm, vif_typename) < 0)
2279
            goto cleanup;
2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303
    }

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

    VIR_FREE(netVal);
    return 0;

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


/*
 * A convenience function for formatting all config common to both XM and XL
 */
int
xenFormatConfigCommon(virConfPtr conf,
                      virDomainDefPtr def,
2304 2305
                      virConnectPtr conn,
                      const char *nativeFormat)
2306 2307 2308 2309 2310 2311 2312 2313 2314 2315
{
    if (xenFormatGeneralMeta(conf, def) < 0)
        return -1;

    if (xenFormatMem(conf, def) < 0)
        return -1;

    if (xenFormatCPUAllocation(conf, def) < 0)
        return -1;

2316
    if (xenFormatCPUFeatures(conf, def) < 0)
2317 2318
        return -1;

2319
    if (xenFormatTimeOffset(conf, def) < 0)
2320 2321 2322 2323 2324 2325 2326 2327
        return -1;

    if (xenFormatEventActions(conf, def) < 0)
        return -1;

    if (xenFormatEmulator(conf, def) < 0)
        return -1;

2328
    if (xenFormatVfb(conf, def) < 0)
2329 2330
        return -1;

2331 2332 2333 2334 2335 2336 2337 2338 2339
    if (STREQ(nativeFormat, XEN_CONFIG_FORMAT_XL)) {
        if (xenFormatVif(conf, conn, def, "vif") < 0)
            return -1;
    } else if (STREQ(nativeFormat, XEN_CONFIG_FORMAT_XM)) {
        if (xenFormatVif(conf, conn, def, "netfront") < 0)
            return -1;
    } else {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unsupported config type %s"), nativeFormat);
2340
        return -1;
2341
    }
2342 2343 2344 2345

    if (xenFormatPCI(conf, def) < 0)
        return -1;

2346
    if (xenFormatCharDev(conf, def, nativeFormat) < 0)
2347 2348 2349 2350 2351 2352 2353
        return -1;

    if (xenFormatSound(conf, def) < 0)
        return -1;

    return 0;
}
P
Pavel Hrdina 已提交
2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375


int
xenDomainDefAddImplicitInputDevice(virDomainDefPtr def)
{
    virDomainInputBus implicitInputBus = VIR_DOMAIN_INPUT_BUS_XEN;

    if (def->os.type == VIR_DOMAIN_OSTYPE_HVM)
        implicitInputBus = VIR_DOMAIN_INPUT_BUS_PS2;

    if (virDomainDefMaybeAddInput(def,
                                  VIR_DOMAIN_INPUT_TYPE_MOUSE,
                                  implicitInputBus) < 0)
        return -1;

    if (virDomainDefMaybeAddInput(def,
                                  VIR_DOMAIN_INPUT_TYPE_KBD,
                                  implicitInputBus) < 0)
        return -1;

    return 0;
}