testutilsqemu.c 21.5 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

        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);
348
                virQEMUCapsSet(tmpCaps, QEMU_CAPS_TCG);
349 350 351 352 353 354 355 356 357 358 359 360
            }
            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);
            }
361
        }
362

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

369
    return 0;
370 371
}

372

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

448 449 450
    return 0;

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

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

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

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

485
    return 0;
486 487
}

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


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

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

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

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

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

515
        if (!tmp)
516 517
            continue;

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

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

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

    if (rc < 0)
        goto cleanup;

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

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

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


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

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

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

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

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

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

579 580
    VIR_TEST_VERBOSE("");

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

 error:
    virHashFree(capslatest);
    return NULL;
}


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

    if (!callback)
        return 0;

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

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

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

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

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

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

633 634 635
        /* 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 */
636 637 638
        archName[0] = '\0';
        archName++;

639 640 641 642 643 644
        /* 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.
         */
645
        if (callback(TEST_QEMU_CAPS_PATH, "caps", version,
646 647
                     archName, suffix + 1, opaque) < 0)
            fail = true;
648 649
    }

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

    ret = 0;

 cleanup:
    virDirClose(&dir);

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


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;
672
    g_autofree char *capsfile = NULL;
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 756
    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;

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

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

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

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

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

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