testutilsqemu.c 17.5 KB
Newer Older
1
#include <config.h>
A
Atsushi SAKAI 已提交
2
#ifdef WITH_QEMU
3
# include <stdlib.h>
4

5 6
# include "testutilsqemu.h"
# include "testutils.h"
7
# include "viralloc.h"
J
Jiri Denemark 已提交
8
# include "cpu_conf.h"
9
# include "qemu/qemu_driver.h"
10
# include "qemu/qemu_domain.h"
11 12
# define __QEMU_CAPSRIV_H_ALLOW__
# include "qemu/qemu_capspriv.h"
13 14 15
# include "virstring.h"

# define VIR_FROM_THIS VIR_FROM_QEMU
16

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
virCPUDefPtr cpuDefault;
virCPUDefPtr cpuHaswell;

static virCPUFeatureDef cpuDefaultFeatures[] = {
    { (char *) "lahf_lm",   -1 },
    { (char *) "xtpr",      -1 },
    { (char *) "cx16",      -1 },
    { (char *) "tm2",       -1 },
    { (char *) "est",       -1 },
    { (char *) "vmx",       -1 },
    { (char *) "ds_cpl",    -1 },
    { (char *) "pbe",       -1 },
    { (char *) "tm",        -1 },
    { (char *) "ht",        -1 },
    { (char *) "ss",        -1 },
    { (char *) "acpi",      -1 },
    { (char *) "ds",        -1 }
};
static virCPUDef cpuDefaultData = {
    VIR_CPU_TYPE_HOST,      /* type */
    0,                      /* mode */
    0,                      /* match */
    VIR_ARCH_X86_64,        /* arch */
    (char *) "core2duo",    /* model */
    NULL,                   /* vendor_id */
    0,                      /* fallback */
    (char *) "Intel",       /* vendor */
    1,                      /* sockets */
    2,                      /* cores */
    1,                      /* threads */
    ARRAY_CARDINALITY(cpuDefaultFeatures), /* nfeatures */
    ARRAY_CARDINALITY(cpuDefaultFeatures), /* nfeatures_max */
    cpuDefaultFeatures,     /* features */
};

static virCPUFeatureDef cpuHaswellFeatures[] = {
    { (char *) "lahf_lm",   -1 },
    { (char *) "invtsc",    -1 },
    { (char *) "abm",       -1 },
    { (char *) "pdpe1gb",   -1 },
    { (char *) "rdrand",    -1 },
    { (char *) "f16c",      -1 },
    { (char *) "osxsave",   -1 },
    { (char *) "pdcm",      -1 },
    { (char *) "xtpr",      -1 },
    { (char *) "tm2",       -1 },
    { (char *) "est",       -1 },
    { (char *) "smx",       -1 },
    { (char *) "vmx",       -1 },
    { (char *) "ds_cpl",    -1 },
    { (char *) "monitor",   -1 },
    { (char *) "dtes64",    -1 },
    { (char *) "pbe",       -1 },
    { (char *) "tm",        -1 },
    { (char *) "ht",        -1 },
    { (char *) "ss",        -1 },
    { (char *) "acpi",      -1 },
    { (char *) "ds",        -1 },
    { (char *) "vme",       -1 },
};
static virCPUDef cpuHaswellData = {
    VIR_CPU_TYPE_HOST,      /* type */
    0,                      /* mode */
    0,                      /* match */
    VIR_ARCH_X86_64,        /* arch */
    (char *) "Haswell",     /* model */
    NULL,                   /* vendor_id */
    0,                      /* fallback */
    (char *) "Intel",       /* vendor */
    1,                      /* sockets */
    2,                      /* cores */
    2,                      /* threads */
    ARRAY_CARDINALITY(cpuHaswellFeatures), /* nfeatures */
    ARRAY_CARDINALITY(cpuHaswellFeatures), /* nfeatures_max */
    cpuHaswellFeatures,     /* features */
};

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
static virCapsGuestMachinePtr *testQemuAllocMachines(int *nmachines)
{
    virCapsGuestMachinePtr *machines;
    static const char *const x86_machines[] = {
        "pc", "isapc"
    };

    machines = virCapabilitiesAllocMachines(x86_machines,
                                            ARRAY_CARDINALITY(x86_machines));
    if (machines == NULL)
        return NULL;

    *nmachines = ARRAY_CARDINALITY(x86_machines);

    return machines;
}

M
Mark McLoughlin 已提交
111 112 113 114 115 116 117 118 119 120 121 122
/* Newer versions of qemu have versioned machine types to allow
 * compatibility with older releases.
 * The 'pc' machine type is an alias of the newest machine type.
 */
static virCapsGuestMachinePtr *testQemuAllocNewerMachines(int *nmachines)
{
    virCapsGuestMachinePtr *machines;
    char *canonical;
    static const char *const x86_machines[] = {
        "pc-0.11", "pc", "pc-0.10", "isapc"
    };

123
    if (VIR_STRDUP(canonical, x86_machines[0]) < 0)
M
Mark McLoughlin 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
        return NULL;

    machines = virCapabilitiesAllocMachines(x86_machines,
                                            ARRAY_CARDINALITY(x86_machines));
    if (machines == NULL) {
        VIR_FREE(canonical);
        return NULL;
    }

    machines[1]->canonical = canonical;

    *nmachines = ARRAY_CARDINALITY(x86_machines);

    return machines;
}

140

141 142 143 144 145 146 147 148 149 150
static int testQemuAddPPC64Guest(virCapsPtr caps)
{
    static const char *machine[] = { "pseries" };
    virCapsGuestMachinePtr *machines = NULL;
    virCapsGuestPtr guest;

    machines = virCapabilitiesAllocMachines(machine, 1);
    if (!machines)
        goto error;

151
    guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_PPC64,
152 153 154 155 156
                                    "/usr/bin/qemu-system-ppc64", NULL,
                                     1, machines);
    if (!guest)
        goto error;

157
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_QEMU, NULL, NULL, 0, NULL))
158 159 160 161
        goto error;

    return 0;

162
 error:
163 164 165 166 167
    /* No way to free a guest? */
    virCapabilitiesFreeMachines(machines, 1);
    return -1;
}

168 169 170 171 172 173 174 175 176 177
static int testQemuAddPPC64LEGuest(virCapsPtr caps)
{
    static const char *machine[] = { "pseries" };
    virCapsGuestMachinePtr *machines = NULL;
    virCapsGuestPtr guest;

    machines = virCapabilitiesAllocMachines(machine, 1);
    if (!machines)
        goto error;

178
    guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_PPC64LE,
179 180 181 182 183
                                    "/usr/bin/qemu-system-ppc64", NULL,
                                     1, machines);
    if (!guest)
        goto error;

184
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_QEMU, NULL, NULL, 0, NULL))
185 186 187 188 189 190 191 192 193 194
        goto error;

    return 0;

 error:
    /* No way to free a guest? */
    virCapabilitiesFreeMachines(machines, 1);
    return -1;
}

O
Olivia Yin 已提交
195 196 197 198 199
static int testQemuAddPPCGuest(virCapsPtr caps)
{
    static const char *machine[] = { "g3beige",
                                     "mac99",
                                     "prep",
200
                                     "ppce500" };
O
Olivia Yin 已提交
201 202 203 204 205 206 207
    virCapsGuestMachinePtr *machines = NULL;
    virCapsGuestPtr guest;

    machines = virCapabilitiesAllocMachines(machine, 1);
    if (!machines)
        goto error;

208
    guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_PPC,
O
Olivia Yin 已提交
209 210 211 212 213
                                    "/usr/bin/qemu-system-ppc", NULL,
                                     1, machines);
    if (!guest)
        goto error;

214
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_QEMU, NULL, NULL, 0, NULL))
O
Olivia Yin 已提交
215 216 217 218
        goto error;

    return 0;

219
 error:
O
Olivia Yin 已提交
220 221 222 223 224
    /* No way to free a guest? */
    virCapabilitiesFreeMachines(machines, 1);
    return -1;
}

225 226
static int testQemuAddS390Guest(virCapsPtr caps)
{
227 228
    static const char *s390_machines[] = { "s390-virtio",
                                           "s390-ccw-virtio" };
229 230 231 232 233 234 235 236
    virCapsGuestMachinePtr *machines = NULL;
    virCapsGuestPtr guest;

    machines = virCapabilitiesAllocMachines(s390_machines,
                                            ARRAY_CARDINALITY(s390_machines));
    if (!machines)
        goto error;

237
    guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_S390X,
238 239 240 241 242 243
                                    "/usr/bin/qemu-system-s390x", NULL,
                                    ARRAY_CARDINALITY(s390_machines),
                                    machines);
    if (!guest)
        goto error;

244
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_QEMU, NULL, NULL, 0, NULL))
245 246 247 248
        goto error;

    return 0;

249
 error:
250 251 252 253
    virCapabilitiesFreeMachines(machines, ARRAY_CARDINALITY(s390_machines));
    return -1;
}

254 255 256 257 258 259 260 261 262 263 264 265 266
static int testQemuAddArmGuest(virCapsPtr caps)
{
    static const char *machines[] = { "vexpress-a9",
                                      "vexpress-a15",
                                      "versatilepb" };
    virCapsGuestMachinePtr *capsmachines = NULL;
    virCapsGuestPtr guest;

    capsmachines = virCapabilitiesAllocMachines(machines,
                                                ARRAY_CARDINALITY(machines));
    if (!capsmachines)
        goto error;

267
    guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_ARMV7L,
268 269 270 271 272 273
                                    "/usr/bin/qemu-system-arm", NULL,
                                    ARRAY_CARDINALITY(machines),
                                    capsmachines);
    if (!guest)
        goto error;

274
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_QEMU, NULL, NULL, 0, NULL))
275 276 277 278
        goto error;

    return 0;

279
 error:
280 281 282 283
    virCapabilitiesFreeMachines(capsmachines, ARRAY_CARDINALITY(machines));
    return -1;
}

284 285 286 287 288 289 290 291 292 293 294
static int testQemuAddAARCH64Guest(virCapsPtr caps)
{
    static const char *machines[] = { "virt"};
    virCapsGuestMachinePtr *capsmachines = NULL;
    virCapsGuestPtr guest;

    capsmachines = virCapabilitiesAllocMachines(machines,
                                                ARRAY_CARDINALITY(machines));
    if (!capsmachines)
        goto error;

295
    guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_AARCH64,
296 297 298 299 300 301
                                    "/usr/bin/qemu-system-aarch64", NULL,
                                    ARRAY_CARDINALITY(machines),
                                    capsmachines);
    if (!guest)
        goto error;

302
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_QEMU, NULL, NULL, 0, NULL))
303 304 305 306
        goto error;

    return 0;

307
 error:
308 309 310
    virCapabilitiesFreeMachines(capsmachines, ARRAY_CARDINALITY(machines));
    return -1;
}
311

312 313
virCapsPtr testQemuCapsInit(void)
{
314 315
    virCapsPtr caps;
    virCapsGuestPtr guest;
316 317
    virCapsGuestMachinePtr *machines = NULL;
    int nmachines = 0;
318 319 320 321
    static const char *const xen_machines[] = {
        "xenner"
    };

322
    if (!(caps = virCapabilitiesNew(VIR_ARCH_X86_64, false, false)))
323 324
        return NULL;

325 326 327 328 329 330 331 332 333 334
    /* Add dummy 'none' security_driver. This is equal to setting
     * security_driver = "none" in qemu.conf. */
    if (VIR_ALLOC_N(caps->host.secModels, 1) < 0)
        goto cleanup;
    caps->host.nsecModels = 1;

    if (VIR_STRDUP(caps->host.secModels[0].model, "none") < 0 ||
        VIR_STRDUP(caps->host.secModels[0].doi, "0") < 0)
        goto cleanup;

335 336 337 338 339 340
    if (!(cpuDefault = virCPUDefCopy(&cpuDefaultData)) ||
        !(cpuHaswell = virCPUDefCopy(&cpuHaswellData)))
        goto cleanup;

    caps->host.cpu = cpuDefault;

341 342
    caps->host.nnumaCell_max = 4;

343
    if ((machines = testQemuAllocMachines(&nmachines)) == NULL)
344 345
        goto cleanup;

346
    if ((guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_I686,
347
                                         "/usr/bin/qemu", NULL,
348
                                         nmachines, machines)) == NULL ||
349
        !virCapabilitiesAddGuestFeature(guest, "cpuselection", true, false))
350
        goto cleanup;
351 352
    machines = NULL;

353
    if (virCapabilitiesAddGuestDomain(guest,
354
                                      VIR_DOMAIN_VIRT_QEMU,
355 356 357 358 359 360
                                      NULL,
                                      NULL,
                                      0,
                                      NULL) == NULL)
        goto cleanup;

361 362 363 364 365 366 367 368 369 370 371 372
    if ((machines = testQemuAllocMachines(&nmachines)) == NULL)
        goto cleanup;

    if (virCapabilitiesAddGuestDomain(guest,
                                      VIR_DOMAIN_VIRT_KVM,
                                      "/usr/bin/qemu-kvm",
                                      NULL,
                                      nmachines,
                                      machines) == NULL)
        goto cleanup;
    machines = NULL;

M
Mark McLoughlin 已提交
373
    if ((machines = testQemuAllocNewerMachines(&nmachines)) == NULL)
374 375
        goto cleanup;

376
    if ((guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_X86_64,
377
                                         "/usr/bin/qemu-system-x86_64", NULL,
378
                                         nmachines, machines)) == NULL ||
379
        !virCapabilitiesAddGuestFeature(guest, "cpuselection", true, false))
380
        goto cleanup;
381 382
    machines = NULL;

383
    if (virCapabilitiesAddGuestDomain(guest,
384
                                      VIR_DOMAIN_VIRT_QEMU,
385 386 387 388 389
                                      NULL,
                                      NULL,
                                      0,
                                      NULL) == NULL)
        goto cleanup;
390 391 392 393

    if ((machines = testQemuAllocMachines(&nmachines)) == NULL)
        goto cleanup;

394
    if (virCapabilitiesAddGuestDomain(guest,
395
                                      VIR_DOMAIN_VIRT_KVM,
396 397
                                      "/usr/bin/kvm",
                                      NULL,
398 399
                                      nmachines,
                                      machines) == NULL)
400
        goto cleanup;
401
    machines = NULL;
402

403
    nmachines = ARRAY_CARDINALITY(xen_machines);
404 405 406
    if ((machines = virCapabilitiesAllocMachines(xen_machines, nmachines)) == NULL)
        goto cleanup;

407
    if ((guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_XEN, VIR_ARCH_X86_64,
408
                                         "/usr/bin/xenner", NULL,
409
                                         nmachines, machines)) == NULL)
410
        goto cleanup;
411 412
    machines = NULL;

413
    if (virCapabilitiesAddGuestDomain(guest,
414
                                      VIR_DOMAIN_VIRT_KVM,
415 416 417 418 419 420
                                      "/usr/bin/kvm",
                                      NULL,
                                      0,
                                      NULL) == NULL)
        goto cleanup;

421 422 423
    if (testQemuAddPPC64Guest(caps))
        goto cleanup;

424 425 426
    if (testQemuAddPPC64LEGuest(caps))
        goto cleanup;

O
Olivia Yin 已提交
427 428 429
    if (testQemuAddPPCGuest(caps))
        goto cleanup;

430 431 432
    if (testQemuAddS390Guest(caps))
        goto cleanup;

433 434 435
    if (testQemuAddArmGuest(caps))
        goto cleanup;

436 437 438
    if (testQemuAddAARCH64Guest(caps))
        goto cleanup;

439
    if (virTestGetDebug()) {
440 441 442 443 444 445
        char *caps_str;

        caps_str = virCapabilitiesFormatXML(caps);
        if (!caps_str)
            goto cleanup;

446
        VIR_TEST_DEBUG("QEMU driver capabilities:\n%s", caps_str);
447 448 449 450

        VIR_FREE(caps_str);
    }

451 452
    return caps;

453
 cleanup:
454
    virCapabilitiesFreeMachines(machines, nmachines);
455 456 457 458
    if (caps->host.cpu != cpuDefault)
        virCPUDefFree(cpuDefault);
    if (caps->host.cpu != cpuHaswell)
        virCPUDefFree(cpuHaswell);
459
    virObjectUnref(caps);
460 461
    return NULL;
}
462 463 464


static char *
465 466
testSCSIDeviceGetSgName(const char *sysfs_prefix ATTRIBUTE_UNUSED,
                        const char *adapter ATTRIBUTE_UNUSED,
467 468
                        unsigned int bus ATTRIBUTE_UNUSED,
                        unsigned int target ATTRIBUTE_UNUSED,
469
                        unsigned long long unit ATTRIBUTE_UNUSED)
470 471 472 473 474 475 476 477 478 479 480 481
{
    char *sg = NULL;

    if (VIR_STRDUP(sg, "sg0") < 0)
        return NULL;

    return sg;
}

qemuBuildCommandLineCallbacks testCallbacks = {
    .qemuGetSCSIDeviceSgName = testSCSIDeviceGetSgName,
};
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530

virQEMUCapsPtr
qemuTestParseCapabilities(const char *capsFile)
{
    virQEMUCapsPtr qemuCaps = NULL;
    xmlDocPtr xml;
    xmlXPathContextPtr ctxt = NULL;
    ssize_t i, n;
    xmlNodePtr *nodes = NULL;

    if (!(xml = virXMLParseFileCtxt(capsFile, &ctxt)))
        goto error;

    if ((n = virXPathNodeSet("/qemuCaps/flag", ctxt, &nodes)) < 0) {
        fprintf(stderr, "failed to parse qemu capabilities flags");
        goto error;
    }

    if (n > 0) {
        if (!(qemuCaps = virQEMUCapsNew()))
            goto error;

        for (i = 0; i < n; i++) {
            char *str = virXMLPropString(nodes[i], "name");
            if (str) {
                int flag = virQEMUCapsTypeFromString(str);
                if (flag < 0) {
                    fprintf(stderr, "Unknown qemu capabilities flag %s", str);
                    VIR_FREE(str);
                    goto error;
                }
                VIR_FREE(str);
                virQEMUCapsSet(qemuCaps, flag);
            }
        }
    }

    VIR_FREE(nodes);
    xmlFreeDoc(xml);
    xmlXPathFreeContext(ctxt);
    return qemuCaps;

 error:
    VIR_FREE(nodes);
    virObjectUnref(qemuCaps);
    xmlFreeDoc(xml);
    xmlXPathFreeContext(ctxt);
    return NULL;
}
531 532 533

void qemuTestDriverFree(virQEMUDriver *driver)
{
534
    virQEMUCapsCacheFree(driver->qemuCapsCache);
535 536 537 538 539
    virObjectUnref(driver->xmlopt);
    virObjectUnref(driver->caps);
    virObjectUnref(driver->config);
}

540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
int qemuTestCapsCacheInsert(virQEMUCapsCachePtr cache, const char *binary,
                            virQEMUCapsPtr caps)
{
    int ret;

    if (caps) {
        /* Our caps were created artificially, so we don't want
         * virQEMUCapsCacheFree() to attempt to deallocate them */
        virObjectRef(caps);
    } else {
        caps = virQEMUCapsNew();
        if (!caps)
            return -ENOMEM;
    }

    /* We can have repeating names for our test data sets,
     * so make sure there's no old copy */
    virHashRemoveEntry(cache->binaries, binary);

    ret = virHashAddEntry(cache->binaries, binary, caps);
    if (ret < 0)
        virObjectUnref(caps);
P
Pavel Fedin 已提交
562 563
    else
        qemuTestCapsName = binary;
564 565 566 567

    return ret;
}

568 569 570 571 572 573 574 575 576 577
int qemuTestDriverInit(virQEMUDriver *driver)
{
    driver->config = virQEMUDriverConfigNew(false);
    if (!driver->config)
        return -1;

    driver->caps = testQemuCapsInit();
    if (!driver->caps)
        goto error;

P
Pavel Fedin 已提交
578 579 580 581 582 583
    /* Using /dev/null for libDir and cacheDir automatically produces errors
     * upon attempt to use any of them */
    driver->qemuCapsCache = virQEMUCapsCacheNew("/dev/null", "/dev/null", 0, 0);
    if (!driver->qemuCapsCache)
        goto error;

584 585 586 587
    driver->xmlopt = virQEMUDriverCreateXMLConf(driver);
    if (!driver->xmlopt)
        goto error;

P
Pavel Fedin 已提交
588 589 590
    if (qemuTestCapsCacheInsert(driver->qemuCapsCache, "empty", NULL) < 0)
        goto error;

591 592 593 594 595 596
    return 0;

 error:
    qemuTestDriverFree(driver);
    return -1;
}
597

A
Atsushi SAKAI 已提交
598
#endif