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

4
# include "testutilsqemu.h"
P
Pavel Hrdina 已提交
5
# include "testutilshostcpus.h"
6
# 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
# define LIBVIRT_QEMU_CAPSPRIV_H_ALLOW
12
# include "qemu/qemu_capspriv.h"
13
# include "virstring.h"
14
# include "virfilecache.h"
J
Ján Tomko 已提交
15
# include "virutil.h"
16 17

# define VIR_FROM_THIS VIR_FROM_QEMU
18

19 20
virCPUDefPtr cpuDefault;
virCPUDefPtr cpuHaswell;
21
virCPUDefPtr cpuPower8;
22
virCPUDefPtr cpuPower9;
23

24

25 26 27 28 29 30 31 32 33 34 35
static const char *qemu_emulators[VIR_ARCH_LAST] = {
    [VIR_ARCH_I686] = "/usr/bin/qemu-system-i386",
    [VIR_ARCH_X86_64] = "/usr/bin/qemu-system-x86_64",
    [VIR_ARCH_AARCH64] = "/usr/bin/qemu-system-aarch64",
    [VIR_ARCH_ARMV7L] = "/usr/bin/qemu-system-arm",
    [VIR_ARCH_PPC64] = "/usr/bin/qemu-system-ppc64",
    [VIR_ARCH_PPC] = "/usr/bin/qemu-system-ppc",
    [VIR_ARCH_RISCV32] = "/usr/bin/qemu-system-riscv32",
    [VIR_ARCH_RISCV64] = "/usr/bin/qemu-system-riscv64",
    [VIR_ARCH_S390X] = "/usr/bin/qemu-system-s390x"
};
M
Mark McLoughlin 已提交
36

37 38 39 40
static const virArch arch_alias[VIR_ARCH_LAST] = {
    [VIR_ARCH_PPC64LE] = VIR_ARCH_PPC64,
    [VIR_ARCH_ARMV6L] = VIR_ARCH_ARMV7L,
};
M
Mark McLoughlin 已提交
41

42 43 44
static const char *const i386_machines[] = {
    "pc", "isapc", NULL
};
45 46 47
/**
 * Oldest supported qemu-1.5 supports machine types back to pc-0.10.
 */
48
static const char *const x86_64_machines[] = {
49
    "pc", "isapc", "q35",
50
    "pc-1.0", "pc-1.2",
51 52 53 54
    "pc-i440fx-1.4", "pc-i440fx-2.1", "pc-i440fx-2.3", "pc-i440fx-2.5",
    "pc-i440fx-2.6", "pc-i440fx-2.9", "pc-i440fx-2.12",
    "pc-q35-2.3", "pc-q35-2.4", "pc-q35-2.5", "pc-q35-2.7", "pc-q35-2.10",
    NULL
55 56
};
static const char *const aarch64_machines[] = {
57
    "virt", "virt-2.6", "versatilepb", NULL
58 59
};
static const char *const arm_machines[] = {
60
    "vexpress-a9", "vexpress-a15", "versatilepb", "virt", NULL
61 62 63 64 65 66 67 68 69 70 71 72 73 74
};
static const char *const ppc64_machines[] = {
    "pseries", NULL
};
static const char *const ppc_machines[] = {
    "g3beige", "mac99", "prep", "ppce500", NULL
};
static const char *const riscv32_machines[] = {
    "spike_v1.10", "spike_v1.9.1", "sifive_e", "virt", "sifive_u", NULL
};
static const char *const riscv64_machines[] = {
    "spike_v1.10", "spike_v1.9.1", "sifive_e", "virt", "sifive_u", NULL
};
static const char *const s390x_machines[] = {
75
    "s390-virtio", "s390-ccw-virtio", "s390-ccw", NULL
76
};
M
Mark McLoughlin 已提交
77

78 79
static const char *const *qemu_machines[VIR_ARCH_LAST] = {
    [VIR_ARCH_I686] = i386_machines,
80
    [VIR_ARCH_X86_64] = x86_64_machines,
81 82 83 84 85 86 87 88
    [VIR_ARCH_AARCH64] = aarch64_machines,
    [VIR_ARCH_ARMV7L] = arm_machines,
    [VIR_ARCH_PPC64] = ppc64_machines,
    [VIR_ARCH_PPC] = ppc_machines,
    [VIR_ARCH_RISCV32] = riscv32_machines,
    [VIR_ARCH_RISCV64] = riscv64_machines,
    [VIR_ARCH_S390X] = s390x_machines,
};
M
Mark McLoughlin 已提交
89

90 91
static const char *const *kvm_machines[VIR_ARCH_LAST] = {
    [VIR_ARCH_I686] = i386_machines,
92
    [VIR_ARCH_X86_64] = x86_64_machines,
93 94 95 96 97 98 99 100
    [VIR_ARCH_AARCH64] = aarch64_machines,
    [VIR_ARCH_ARMV7L] = arm_machines,
    [VIR_ARCH_PPC64] = ppc64_machines,
    [VIR_ARCH_PPC] = ppc_machines,
    [VIR_ARCH_RISCV32] = riscv32_machines,
    [VIR_ARCH_RISCV64] = riscv64_machines,
    [VIR_ARCH_S390X] = s390x_machines,
};
101

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

char *
virFindFileInPath(const char *file)
{
    if (g_str_has_prefix(file, "qemu-system") ||
        g_str_equal(file, "qemu-kvm")) {
        return g_strdup_printf("/usr/bin/%s", file);
    }

    /* Nothing in tests should be relying on real files
     * in host OS, so we return NULL to try to force
     * an error in such a case
     */
    return NULL;
}

118 119 120 121 122 123 124 125 126 127 128 129

virCapsHostNUMAPtr
virCapabilitiesHostNUMANewHost(void)
{
    /*
     * Build a NUMA topology with cell_id (NUMA node id
     * being 3(0 + 3),4(1 + 3), 5 and 6
     */
    return virTestCapsBuildNUMATopology(3);
}


130
static int
131 132
testQemuAddGuest(virCapsPtr caps,
                 virArch arch)
133
{
134
    size_t nmachines;
135 136
    virCapsGuestMachinePtr *machines = NULL;
    virCapsGuestPtr guest;
137
    virArch emu_arch = arch;
138

139 140
    if (arch_alias[arch] != VIR_ARCH_NONE)
        emu_arch = arch_alias[arch];
141

142 143
    if (qemu_emulators[emu_arch] == NULL)
        return 0;
144

145 146 147 148
    nmachines = g_strv_length((gchar **)qemu_machines[emu_arch]);
    machines = virCapabilitiesAllocMachines(qemu_machines[emu_arch],
                                            nmachines);
    if (machines == NULL)
149 150 151 152
        goto error;

    if (!(guest = virCapabilitiesAddGuest(caps,
                                          VIR_DOMAIN_OSTYPE_HVM,
153 154
                                          arch,
                                          qemu_emulators[emu_arch],
155 156 157 158 159 160
                                          NULL,
                                          nmachines,
                                          machines)))
        goto error;

    machines = NULL;
161 162 163 164 165
    nmachines = 0;

    if (arch == VIR_ARCH_I686 ||
        arch == VIR_ARCH_X86_64)
        virCapabilitiesAddGuestFeature(guest, VIR_CAPS_GUEST_FEATURE_TYPE_CPUSELECTION);
166 167 168 169 170 171 172 173 174

    if (!virCapabilitiesAddGuestDomain(guest,
                                       VIR_DOMAIN_VIRT_QEMU,
                                       NULL,
                                       NULL,
                                       0,
                                       NULL))
        goto error;

175 176 177 178
    nmachines = g_strv_length((char **)kvm_machines[emu_arch]);
    machines = virCapabilitiesAllocMachines(kvm_machines[emu_arch],
                                            nmachines);
    if (machines == NULL)
179 180 181 182
        goto error;

    if (!virCapabilitiesAddGuestDomain(guest,
                                       VIR_DOMAIN_VIRT_KVM,
183
                                       qemu_emulators[emu_arch],
184 185 186 187 188
                                       NULL,
                                       nmachines,
                                       machines))
        goto error;

L
Lubomir Rintel 已提交
189 190 191 192 193 194 195
    return 0;

 error:
    virCapabilitiesFreeMachines(machines, nmachines);
    return -1;
}

196

197 198
virCapsPtr testQemuCapsInit(void)
{
199
    virCapsPtr caps;
200
    size_t i;
201

202
    if (!(caps = virCapabilitiesNew(VIR_ARCH_X86_64, false, false)))
203 204
        return NULL;

205 206 207 208 209 210
    /* 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;

211 212
    caps->host.secModels[0].model = g_strdup("none");
    caps->host.secModels[0].doi = g_strdup("0");
213

214
    if (!(caps->host.numa = virCapabilitiesHostNUMANewHost()))
215
        goto cleanup;
216

217 218 219 220
    for (i = 0; i < VIR_ARCH_LAST; i++) {
        if (testQemuAddGuest(caps, i) < 0)
            goto cleanup;
    }
221

222
    if (virTestGetDebug()) {
223 224 225 226 227 228
        char *caps_str;

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

229
        VIR_TEST_DEBUG("QEMU driver capabilities:\n%s", caps_str);
230 231 232 233

        VIR_FREE(caps_str);
    }

234 235
    return caps;

236
 cleanup:
237
    caps->host.cpu = NULL;
238
    virObjectUnref(caps);
239 240
    return NULL;
}
241 242


243
void
244
qemuTestSetHostArch(virQEMUDriverPtr driver,
245 246 247 248
                    virArch arch)
{
    if (arch == VIR_ARCH_NONE)
        arch = VIR_ARCH_X86_64;
249 250 251 252

    virTestHostArch = arch;
    driver->hostarch = virArchFromHost();
    driver->caps->host.arch = virArchFromHost();
253
    qemuTestSetHostCPU(driver, arch, NULL);
254 255 256 257
}


void
258 259
qemuTestSetHostCPU(virQEMUDriverPtr driver,
                   virArch arch,
260 261 262 263 264
                   virCPUDefPtr cpu)
{
    if (!cpu) {
        if (ARCH_IS_X86(arch))
            cpu = cpuDefault;
265 266
        else if (ARCH_IS_PPC64(arch))
            cpu = cpuPower8;
267 268
    }

269
    g_unsetenv("VIR_TEST_MOCK_FAKE_HOST_CPU");
P
Pavel Hrdina 已提交
270 271
    if (cpu) {
        if (cpu->model)
272
            g_setenv("VIR_TEST_MOCK_FAKE_HOST_CPU", cpu->model, TRUE);
P
Pavel Hrdina 已提交
273
    }
274 275 276 277 278 279 280 281 282 283
    if (driver) {
        if (cpu)
            driver->caps->host.arch = cpu->arch;
        driver->caps->host.cpu = cpu;

        virCPUDefFree(driver->hostcpu);
        if (cpu)
            virCPUDefRef(cpu);
        driver->hostcpu = cpu;
    }
284 285 286
}


287
virQEMUCapsPtr
288 289
qemuTestParseCapabilitiesArch(virArch arch,
                              const char *capsFile)
290 291
{
    virQEMUCapsPtr qemuCaps = NULL;
292 293
    g_autofree char *binary = g_strdup_printf("/usr/bin/qemu-system-%s",
                                              virArchToString(arch));
294

295
    if (!(qemuCaps = virQEMUCapsNewBinary(binary)) ||
296
        virQEMUCapsLoadCache(arch, qemuCaps, capsFile) < 0)
297 298
        goto error;

299
    virQEMUCapsSetInvalidation(qemuCaps, false);
300 301 302 303 304 305
    return qemuCaps;

 error:
    virObjectUnref(qemuCaps);
    return NULL;
}
306

307

308 309
void qemuTestDriverFree(virQEMUDriver *driver)
{
310
    virMutexDestroy(&driver->lock);
311 312 313 314
    if (driver->config) {
        virFileDeleteTree(driver->config->stateDir);
        virFileDeleteTree(driver->config->configDir);
    }
315
    virObjectUnref(driver->qemuCapsCache);
316 317 318
    virObjectUnref(driver->xmlopt);
    virObjectUnref(driver->caps);
    virObjectUnref(driver->config);
319
    virObjectUnref(driver->securityManager);
320 321
}

322
int qemuTestCapsCacheInsert(virFileCachePtr cache,
323 324
                            virQEMUCapsPtr caps)
{
325 326 327 328 329 330 331 332 333 334 335
    size_t i, j;

    for (i = 0; i < G_N_ELEMENTS(qemu_emulators); i++) {
        virQEMUCapsPtr tmpCaps;
        if (qemu_emulators[i] == NULL)
            continue;
        if (caps) {
            tmpCaps = virQEMUCapsNewCopy(caps);
        } else {
            tmpCaps = virQEMUCapsNew();
        }
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359

        if (!virQEMUCapsHasMachines(tmpCaps)) {
            virQEMUCapsSetArch(tmpCaps, i);
            for (j = 0; qemu_machines[i][j] != NULL; j++) {
                virQEMUCapsAddMachine(tmpCaps,
                                      VIR_DOMAIN_VIRT_QEMU,
                                      qemu_machines[i][j],
                                      NULL,
                                      NULL,
                                      0,
                                      false,
                                      false);
            }
            for (j = 0; kvm_machines[i][j] != NULL; j++) {
                virQEMUCapsAddMachine(tmpCaps,
                                      VIR_DOMAIN_VIRT_KVM,
                                      kvm_machines[i][j],
                                      NULL,
                                      NULL,
                                      0,
                                      false,
                                      false);
                virQEMUCapsSet(tmpCaps, QEMU_CAPS_KVM);
            }
360
        }
361

362
        if (virFileCacheInsertData(cache, qemu_emulators[i], tmpCaps) < 0) {
363 364 365 366
            virObjectUnref(tmpCaps);
            return -1;
        }
    }
367

368
    return 0;
369 370
}

371

372 373 374
# define STATEDIRTEMPLATE abs_builddir "/qemustatedir-XXXXXX"
# define CONFIGDIRTEMPLATE abs_builddir "/qemuconfigdir-XXXXXX"

375 376
int qemuTestDriverInit(virQEMUDriver *driver)
{
377
    virSecurityManagerPtr mgr = NULL;
378 379
    char statedir[] = STATEDIRTEMPLATE;
    char configdir[] = CONFIGDIRTEMPLATE;
380

381 382
    memset(driver, 0, sizeof(*driver));

383 384 385 386 387 388
    if (!(cpuDefault = virCPUDefCopy(&cpuDefaultData)) ||
        !(cpuHaswell = virCPUDefCopy(&cpuHaswellData)) ||
        !(cpuPower8 = virCPUDefCopy(&cpuPower8Data)) ||
        !(cpuPower9 = virCPUDefCopy(&cpuPower9Data)))
        return -1;

389 390 391
    if (virMutexInit(&driver->lock) < 0)
        return -1;

392
    driver->hostarch = virArchFromHost();
393
    driver->config = virQEMUDriverConfigNew(false, NULL);
394
    if (!driver->config)
395
        goto error;
396

397 398 399 400 401
    /* Do this early so that qemuTestDriverFree() doesn't see (unlink) the real
     * dirs. */
    VIR_FREE(driver->config->stateDir);
    VIR_FREE(driver->config->configDir);

402 403 404
    /* Overwrite some default paths so it's consistent for tests. */
    VIR_FREE(driver->config->libDir);
    VIR_FREE(driver->config->channelTargetDir);
405 406
    driver->config->libDir = g_strdup("/tmp/lib");
    driver->config->channelTargetDir = g_strdup("/tmp/channel");
407

J
Ján Tomko 已提交
408
    if (!g_mkdtemp(statedir)) {
409
        fprintf(stderr, "Cannot create fake stateDir");
410 411 412
        goto error;
    }

413
    driver->config->stateDir = g_strdup(statedir);
414

J
Ján Tomko 已提交
415
    if (!g_mkdtemp(configdir)) {
416
        fprintf(stderr, "Cannot create fake configDir");
417 418 419
        goto error;
    }

420
    driver->config->configDir = g_strdup(configdir);
421

422 423 424 425
    driver->caps = testQemuCapsInit();
    if (!driver->caps)
        goto error;

P
Pavel Fedin 已提交
426 427
    /* Using /dev/null for libDir and cacheDir automatically produces errors
     * upon attempt to use any of them */
428
    driver->qemuCapsCache = virQEMUCapsCacheNew("/dev/null", "/dev/null", 0, 0);
P
Pavel Fedin 已提交
429 430 431
    if (!driver->qemuCapsCache)
        goto error;

432
    driver->xmlopt = virQEMUDriverCreateXMLConf(driver, "none");
433 434 435
    if (!driver->xmlopt)
        goto error;

436
    if (qemuTestCapsCacheInsert(driver->qemuCapsCache, NULL) < 0)
P
Pavel Fedin 已提交
437 438
        goto error;

439
    if (!(mgr = virSecurityManagerNew("none", "qemu",
440 441 442 443 444
                                      VIR_SECURITY_MANAGER_PRIVILEGED)))
        goto error;
    if (!(driver->securityManager = virSecurityManagerNewStack(mgr)))
        goto error;

445 446
    qemuTestSetHostCPU(driver, driver->hostarch, NULL);

447 448 449
    return 0;

 error:
450
    virObjectUnref(mgr);
451 452 453
    qemuTestDriverFree(driver);
    return -1;
}
454

455 456 457 458 459 460 461 462
int
testQemuCapsSetGIC(virQEMUCapsPtr qemuCaps,
                   int gic)
{
    virGICCapability *gicCapabilities = NULL;
    size_t ngicCapabilities = 0;

    if (VIR_ALLOC_N(gicCapabilities, 2) < 0)
463
        return -1;
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483

# define IMPL_BOTH \
         VIR_GIC_IMPLEMENTATION_KERNEL|VIR_GIC_IMPLEMENTATION_EMULATED

    if (gic & GIC_V2) {
        gicCapabilities[ngicCapabilities].version = VIR_GIC_VERSION_2;
        gicCapabilities[ngicCapabilities].implementation = IMPL_BOTH;
        ngicCapabilities++;
    }
    if (gic & GIC_V3) {
        gicCapabilities[ngicCapabilities].version = VIR_GIC_VERSION_3;
        gicCapabilities[ngicCapabilities].implementation = IMPL_BOTH;
        ngicCapabilities++;
    }

# undef IMPL_BOTH

    virQEMUCapsSetGICCapabilities(qemuCaps,
                                  gicCapabilities, ngicCapabilities);

484
    return 0;
485 486
}

A
Atsushi SAKAI 已提交
487
#endif
488 489 490


char *
491
testQemuGetLatestCapsForArch(const char *arch,
492 493 494 495 496 497 498 499 500
                             const char *suffix)
{
    struct dirent *ent;
    DIR *dir = NULL;
    int rc;
    char *fullsuffix = NULL;
    char *tmp = NULL;
    unsigned long maxver = 0;
    unsigned long ver;
501
    g_autofree char *maxname = NULL;
502 503
    char *ret = NULL;

504
    fullsuffix = g_strdup_printf("%s.%s", arch, suffix);
505

506
    if (virDirOpen(&dir, TEST_QEMU_CAPS_PATH) < 0)
507 508
        goto cleanup;

509
    while ((rc = virDirRead(dir, &ent, TEST_QEMU_CAPS_PATH)) > 0) {
510 511
        VIR_FREE(tmp);

512
        tmp = g_strdup(STRSKIP(ent->d_name, "caps_"));
513

514
        if (!tmp)
515 516
            continue;

517
        if (!virStringStripSuffix(tmp, fullsuffix))
518 519 520
            continue;

        if (virParseVersionString(tmp, &ver, false) < 0) {
521
            VIR_TEST_DEBUG("skipping caps file '%s'", ent->d_name);
522 523 524 525
            continue;
        }

        if (ver > maxver) {
526 527
            g_free(maxname);
            maxname = g_strdup(ent->d_name);
528 529 530 531 532 533 534 535
            maxver = ver;
        }
    }

    if (rc < 0)
        goto cleanup;

    if (!maxname) {
536
        VIR_TEST_VERBOSE("failed to find capabilities for '%s' in '%s'",
537
                         arch, TEST_QEMU_CAPS_PATH);
538 539 540
        goto cleanup;
    }

541
    ret = g_strdup_printf("%s/%s", TEST_QEMU_CAPS_PATH, maxname);
542 543 544 545 546 547 548

 cleanup:
    VIR_FREE(tmp);
    VIR_FREE(fullsuffix);
    virDirClose(&dir);
    return ret;
}
549 550


C
Cole Robinson 已提交
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
virHashTablePtr
testQemuGetLatestCaps(void)
{
    const char *archs[] = {
        "aarch64",
        "ppc64",
        "riscv64",
        "s390x",
        "x86_64",
    };
    virHashTablePtr capslatest;
    size_t i;

    if (!(capslatest = virHashCreate(4, virHashValueFree)))
        goto error;

567
    VIR_TEST_VERBOSE("");
C
Cole Robinson 已提交
568

569
    for (i = 0; i < G_N_ELEMENTS(archs); ++i) {
570
        char *cap = testQemuGetLatestCapsForArch(archs[i], "xml");
C
Cole Robinson 已提交
571 572 573 574

        if (!cap || virHashAddEntry(capslatest, archs[i], cap) < 0)
            goto error;

575
        VIR_TEST_VERBOSE("latest caps for %s: %s", archs[i], cap);
C
Cole Robinson 已提交
576 577
    }

578 579
    VIR_TEST_VERBOSE("");

C
Cole Robinson 已提交
580 581 582 583 584 585 586 587
    return capslatest;

 error:
    virHashFree(capslatest);
    return NULL;
}


588
int
589
testQemuCapsIterate(const char *suffix,
590 591 592 593 594 595 596
                    testQemuCapsIterateCallback callback,
                    void *opaque)
{
    struct dirent *ent;
    DIR *dir = NULL;
    int rc;
    int ret = -1;
597
    bool fail = false;
598 599 600 601

    if (!callback)
        return 0;

602 603 604 605 606 607
    /* Validate suffix */
    if (!STRPREFIX(suffix, ".")) {
        VIR_TEST_VERBOSE("malformed suffix '%s'", suffix);
        goto cleanup;
    }

608
    if (virDirOpen(&dir, TEST_QEMU_CAPS_PATH) < 0)
609 610
        goto cleanup;

611
    while ((rc = virDirRead(dir, &ent, TEST_QEMU_CAPS_PATH)) > 0) {
612
        g_autofree char *tmp = g_strdup(ent->d_name);
613
        char *version = NULL;
614 615 616 617 618 619
        char *archName = NULL;

        /* Strip the trailing suffix, moving on if it's not present */
        if (!virStringStripSuffix(tmp, suffix))
            continue;

620 621 622 623 624 625
        /* Strip the leading prefix */
        if (!(version = STRSKIP(tmp, "caps_"))) {
            VIR_TEST_VERBOSE("malformed file name '%s'", ent->d_name);
            goto cleanup;
        }

626 627 628 629 630
        /* Find the last dot */
        if (!(archName = strrchr(tmp, '.'))) {
            VIR_TEST_VERBOSE("malformed file name '%s'", ent->d_name);
            goto cleanup;
        }
631

632 633 634
        /* The version number and the architecture name are separated by
         * a dot: overwriting that dot with \0 results in both being usable
         * as independent, null-terminated strings */
635 636 637
        archName[0] = '\0';
        archName++;

638 639 640 641 642 643
        /* Run the user-provided callback.
         *
         * We skip the dot that, as verified earlier, starts the suffix
         * to make it nicer to rebuild the original file name from inside
         * the callback.
         */
644
        if (callback(TEST_QEMU_CAPS_PATH, "caps", version,
645 646
                     archName, suffix + 1, opaque) < 0)
            fail = true;
647 648
    }

649
    if (rc < 0 || fail)
650 651 652 653 654 655 656 657 658
        goto cleanup;

    ret = 0;

 cleanup:
    virDirClose(&dir);

    return ret;
}
659 660 661 662 663 664 665 666 667 668 669 670


int
testQemuInfoSetArgs(struct testQemuInfo *info,
                    virHashTablePtr capslatest, ...)
{
    va_list argptr;
    testQemuInfoArgName argname;
    virQEMUCapsPtr qemuCaps = NULL;
    int gic = GIC_NONE;
    char *capsarch = NULL;
    char *capsver = NULL;
671
    g_autofree char *capsfile = NULL;
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
    int flag;
    int ret = -1;

    va_start(argptr, capslatest);
    argname = va_arg(argptr, testQemuInfoArgName);
    while (argname != ARG_END) {
        switch (argname) {
        case ARG_QEMU_CAPS:
            if (qemuCaps || !(qemuCaps = virQEMUCapsNew()))
                goto cleanup;

            while ((flag = va_arg(argptr, int)) < QEMU_CAPS_LAST)
                virQEMUCapsSet(qemuCaps, flag);

            /* Some tests are run with NONE capabilities, which is just
             * another name for QEMU_CAPS_LAST. If that is the case the
             * arguments look like this :
             *
             *   ARG_QEMU_CAPS, NONE, QEMU_CAPS_LAST, ARG_END
             *
             * Fetch one argument more and if it is QEMU_CAPS_LAST then
             * break from the switch() to force getting next argument
             * in the line. If it is not QEMU_CAPS_LAST then we've
             * fetched real ARG_* and we must process it.
             */
            if ((flag = va_arg(argptr, int)) != QEMU_CAPS_LAST) {
                argname = flag;
                continue;
            }

            break;

        case ARG_GIC:
            gic = va_arg(argptr, int);
            break;

        case ARG_MIGRATE_FROM:
            info->migrateFrom = va_arg(argptr, char *);
            break;

        case ARG_MIGRATE_FD:
            info->migrateFd = va_arg(argptr, int);
            break;

        case ARG_FLAGS:
            info->flags = va_arg(argptr, int);
            break;

        case ARG_PARSEFLAGS:
            info->parseFlags = va_arg(argptr, int);
            break;

        case ARG_CAPS_ARCH:
            capsarch = va_arg(argptr, char *);
            break;

        case ARG_CAPS_VER:
            capsver = va_arg(argptr, char *);
            break;

        case ARG_END:
        default:
            fprintf(stderr, "Unexpected test info argument");
            goto cleanup;
        }

        argname = va_arg(argptr, testQemuInfoArgName);
    }

    if (!!capsarch ^ !!capsver) {
        fprintf(stderr, "ARG_CAPS_ARCH and ARG_CAPS_VER "
                        "must be specified together.\n");
        goto cleanup;
    }

    if (qemuCaps && (capsarch || capsver)) {
        fprintf(stderr, "ARG_QEMU_CAPS can not be combined with ARG_CAPS_ARCH "
                        "or ARG_CAPS_VER\n");
        goto cleanup;
    }

    if (!qemuCaps && capsarch && capsver) {
        bool stripmachinealiases = false;

756 757
        info->arch = virArchFromString(capsarch);

758
        if (STREQ(capsver, "latest")) {
759
            capsfile = g_strdup(virHashLookup(capslatest, capsarch));
760
            stripmachinealiases = true;
761 762
        } else capsfile = g_strdup_printf("%s/caps_%s.%s.xml",
                                          TEST_QEMU_CAPS_PATH, capsver, capsarch);
763

764
        if (!(qemuCaps = qemuTestParseCapabilitiesArch(info->arch, capsfile)))
765 766 767 768 769 770 771 772 773 774 775
            goto cleanup;

        if (stripmachinealiases)
            virQEMUCapsStripMachineAliases(qemuCaps);
        info->flags |= FLAG_REAL_CAPS;
    }

    if (!qemuCaps) {
        fprintf(stderr, "No qemuCaps generated\n");
        goto cleanup;
    }
776
    info->qemuCaps = g_steal_pointer(&qemuCaps);
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797

    if (gic != GIC_NONE && testQemuCapsSetGIC(info->qemuCaps, gic) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
    virObjectUnref(qemuCaps);
    va_end(argptr);

    return ret;
}


void
testQemuInfoClear(struct testQemuInfo *info)
{
    VIR_FREE(info->infile);
    VIR_FREE(info->outfile);
    virObjectUnref(info->qemuCaps);
}