testutilsqemu.c 29.9 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"
15 16

# define VIR_FROM_THIS VIR_FROM_QEMU
17

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

23
typedef enum {
24 25 26 27 28 29
    TEST_UTILS_QEMU_BIN_I686,
    TEST_UTILS_QEMU_BIN_X86_64,
    TEST_UTILS_QEMU_BIN_AARCH64,
    TEST_UTILS_QEMU_BIN_ARM,
    TEST_UTILS_QEMU_BIN_PPC64,
    TEST_UTILS_QEMU_BIN_PPC,
L
Lubomir Rintel 已提交
30 31
    TEST_UTILS_QEMU_BIN_RISCV32,
    TEST_UTILS_QEMU_BIN_RISCV64,
32
    TEST_UTILS_QEMU_BIN_S390X
33 34 35
} QEMUBinType;

static const char *QEMUBinList[] = {
36
    "/usr/bin/qemu-system-i686",
37 38 39 40 41
    "/usr/bin/qemu-system-x86_64",
    "/usr/bin/qemu-system-aarch64",
    "/usr/bin/qemu-system-arm",
    "/usr/bin/qemu-system-ppc64",
    "/usr/bin/qemu-system-ppc",
L
Lubomir Rintel 已提交
42 43
    "/usr/bin/qemu-system-riscv32",
    "/usr/bin/qemu-system-riscv64",
44 45 46 47
    "/usr/bin/qemu-system-s390x"
};


48 49 50 51 52 53 54 55
static virCapsGuestMachinePtr *testQemuAllocMachines(int *nmachines)
{
    virCapsGuestMachinePtr *machines;
    static const char *const x86_machines[] = {
        "pc", "isapc"
    };

    machines = virCapabilitiesAllocMachines(x86_machines,
56
                                            G_N_ELEMENTS(x86_machines));
57 58 59
    if (machines == NULL)
        return NULL;

60
    *nmachines = G_N_ELEMENTS(x86_machines);
61 62 63 64

    return machines;
}

M
Mark McLoughlin 已提交
65 66 67 68 69 70 71 72 73 74 75 76
/* 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"
    };

77
    canonical = g_strdup(x86_machines[0]);
M
Mark McLoughlin 已提交
78 79

    machines = virCapabilitiesAllocMachines(x86_machines,
80
                                            G_N_ELEMENTS(x86_machines));
M
Mark McLoughlin 已提交
81 82 83 84 85 86 87
    if (machines == NULL) {
        VIR_FREE(canonical);
        return NULL;
    }

    machines[1]->canonical = canonical;

88
    *nmachines = G_N_ELEMENTS(x86_machines);
M
Mark McLoughlin 已提交
89 90 91 92

    return machines;
}

93

94 95 96 97 98 99 100 101 102 103 104 105 106
static int
testQemuAddI686Guest(virCapsPtr caps)
{
    int nmachines = 0;
    virCapsGuestMachinePtr *machines = NULL;
    virCapsGuestPtr guest;

    if (!(machines = testQemuAllocMachines(&nmachines)))
        goto error;

    if (!(guest = virCapabilitiesAddGuest(caps,
                                          VIR_DOMAIN_OSTYPE_HVM,
                                          VIR_ARCH_I686,
107
                                          QEMUBinList[TEST_UTILS_QEMU_BIN_I686],
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
                                          NULL,
                                          nmachines,
                                          machines)))
        goto error;

    if (!virCapabilitiesAddGuestFeature(guest, "cpuselection", true, false))
        goto error;

    machines = NULL;

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

    if (!(machines = testQemuAllocMachines(&nmachines)))
        goto error;

    if (!virCapabilitiesAddGuestDomain(guest,
                                       VIR_DOMAIN_VIRT_KVM,
131
                                       QEMUBinList[TEST_UTILS_QEMU_BIN_I686],
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
                                       NULL,
                                       nmachines,
                                       machines))
        goto error;

    return 0;

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


static int
testQemuAddX86_64Guest(virCapsPtr caps)
{
    int nmachines = 0;
    virCapsGuestMachinePtr *machines = NULL;
    virCapsGuestPtr guest;

    if (!(machines = testQemuAllocNewerMachines(&nmachines)))
        goto error;

    if (!(guest = virCapabilitiesAddGuest(caps,
                                          VIR_DOMAIN_OSTYPE_HVM,
                                          VIR_ARCH_X86_64,
158
                                          QEMUBinList[TEST_UTILS_QEMU_BIN_X86_64],
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
                                          NULL,
                                          nmachines,
                                          machines)))
        goto error;

    if (!virCapabilitiesAddGuestFeature(guest, "cpuselection", true, false))
        goto error;

    machines = NULL;

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

    if (!(machines = testQemuAllocMachines(&nmachines)))
        goto error;

    if (!virCapabilitiesAddGuestDomain(guest,
                                       VIR_DOMAIN_VIRT_KVM,
182
                                       QEMUBinList[TEST_UTILS_QEMU_BIN_X86_64],
183 184 185 186 187 188 189 190 191
                                       NULL,
                                       nmachines,
                                       machines))
        goto error;

    machines = NULL;

    if (!virCapabilitiesAddGuestDomain(guest,
                                       VIR_DOMAIN_VIRT_KVM,
192
                                       QEMUBinList[TEST_UTILS_QEMU_BIN_X86_64],
193 194 195 196 197 198 199 200 201 202 203 204 205
                                       NULL,
                                       0,
                                       NULL))
        goto error;

    return 0;

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


206 207 208 209 210 211 212 213 214 215
static int testQemuAddPPC64Guest(virCapsPtr caps)
{
    static const char *machine[] = { "pseries" };
    virCapsGuestMachinePtr *machines = NULL;
    virCapsGuestPtr guest;

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

216
    guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_PPC64,
217
                                    QEMUBinList[TEST_UTILS_QEMU_BIN_PPC64],
218
                                    NULL, 1, machines);
219 220 221
    if (!guest)
        goto error;

222
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_QEMU, NULL, NULL, 0, NULL))
223
        goto error;
224 225 226
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_KVM,
                                       NULL, NULL, 0, NULL))
        goto error;
227 228 229

    return 0;

230
 error:
231 232 233 234 235
    /* No way to free a guest? */
    virCapabilitiesFreeMachines(machines, 1);
    return -1;
}

236 237 238 239 240 241 242 243 244 245
static int testQemuAddPPC64LEGuest(virCapsPtr caps)
{
    static const char *machine[] = { "pseries" };
    virCapsGuestMachinePtr *machines = NULL;
    virCapsGuestPtr guest;

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

246
    guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_PPC64LE,
247
                                    QEMUBinList[TEST_UTILS_QEMU_BIN_PPC64],
248
                                    NULL, 1, machines);
249 250 251
    if (!guest)
        goto error;

252
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_QEMU, NULL, NULL, 0, NULL))
253
        goto error;
254 255 256
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_KVM,
                                       NULL, NULL, 0, NULL))
        goto error;
257 258 259 260 261 262 263 264 265

    return 0;

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

O
Olivia Yin 已提交
266 267 268 269 270
static int testQemuAddPPCGuest(virCapsPtr caps)
{
    static const char *machine[] = { "g3beige",
                                     "mac99",
                                     "prep",
271
                                     "ppce500" };
O
Olivia Yin 已提交
272 273 274 275 276 277 278
    virCapsGuestMachinePtr *machines = NULL;
    virCapsGuestPtr guest;

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

279
    guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_PPC,
280
                                    QEMUBinList[TEST_UTILS_QEMU_BIN_PPC],
281
                                    NULL, 1, machines);
O
Olivia Yin 已提交
282 283 284
    if (!guest)
        goto error;

285
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_QEMU, NULL, NULL, 0, NULL))
O
Olivia Yin 已提交
286
        goto error;
287 288 289
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_KVM,
                                       NULL, NULL, 0, NULL))
        goto error;
O
Olivia Yin 已提交
290 291 292

    return 0;

293
 error:
O
Olivia Yin 已提交
294 295 296 297 298
    /* No way to free a guest? */
    virCapabilitiesFreeMachines(machines, 1);
    return -1;
}

L
Lubomir Rintel 已提交
299 300 301 302 303 304 305
static int testQemuAddRISCV32Guest(virCapsPtr caps)
{
    static const char *names[] = { "spike_v1.10",
                                   "spike_v1.9.1",
                                   "sifive_e",
                                   "virt",
                                   "sifive_u" };
306
    static const int nmachines = G_N_ELEMENTS(names);
L
Lubomir Rintel 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
    virCapsGuestMachinePtr *machines = NULL;
    virCapsGuestPtr guest;

    machines = virCapabilitiesAllocMachines(names, nmachines);
    if (!machines)
        goto error;

    guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_RISCV32,
                                    QEMUBinList[TEST_UTILS_QEMU_BIN_RISCV32],
                                    NULL, nmachines, machines);
    if (!guest)
        goto error;

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

    return 0;

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

static int testQemuAddRISCV64Guest(virCapsPtr caps)
{
    static const char *names[] = { "spike_v1.10",
                                   "spike_v1.9.1",
                                   "sifive_e",
                                   "virt",
                                   "sifive_u" };
337
    static const int nmachines = G_N_ELEMENTS(names);
L
Lubomir Rintel 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
    virCapsGuestMachinePtr *machines = NULL;
    virCapsGuestPtr guest;

    machines = virCapabilitiesAllocMachines(names, nmachines);
    if (!machines)
        goto error;

    guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_RISCV64,
                                    QEMUBinList[TEST_UTILS_QEMU_BIN_RISCV64],
                                    NULL, nmachines, machines);
    if (!guest)
        goto error;

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

    return 0;

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

361 362
static int testQemuAddS390Guest(virCapsPtr caps)
{
363 364
    static const char *s390_machines[] = { "s390-virtio",
                                           "s390-ccw-virtio" };
365 366 367 368
    virCapsGuestMachinePtr *machines = NULL;
    virCapsGuestPtr guest;

    machines = virCapabilitiesAllocMachines(s390_machines,
369
                                            G_N_ELEMENTS(s390_machines));
370 371 372
    if (!machines)
        goto error;

373
    guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_S390X,
374
                                    QEMUBinList[TEST_UTILS_QEMU_BIN_S390X],
375
                                    NULL,
376
                                    G_N_ELEMENTS(s390_machines),
377 378 379 380
                                    machines);
    if (!guest)
        goto error;

381
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_QEMU, NULL, NULL, 0, NULL))
382
        goto error;
383 384 385
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_KVM,
                                       NULL, NULL, 0, NULL))
        goto error;
386 387 388

    return 0;

389
 error:
390
    virCapabilitiesFreeMachines(machines, G_N_ELEMENTS(s390_machines));
391 392 393
    return -1;
}

S
Stefan Schallenberg 已提交
394 395 396 397 398 399 400
static int testQemuAddArm6Guest(virCapsPtr caps)
{
    static const char *machines[] = { "versatilepb" };
    virCapsGuestMachinePtr *capsmachines = NULL;
    virCapsGuestPtr guest;

    capsmachines = virCapabilitiesAllocMachines(machines,
401
                                                G_N_ELEMENTS(machines));
S
Stefan Schallenberg 已提交
402 403 404 405 406 407
    if (!capsmachines)
        goto error;

    guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_ARMV6L,
                                    QEMUBinList[TEST_UTILS_QEMU_BIN_ARM],
                                    NULL,
408
                                    G_N_ELEMENTS(machines),
S
Stefan Schallenberg 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421
                                    capsmachines);
    if (!guest)
        goto error;

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

    return 0;

 error:
422
    virCapabilitiesFreeMachines(capsmachines, G_N_ELEMENTS(machines));
S
Stefan Schallenberg 已提交
423 424 425 426
    return -1;
}

static int testQemuAddArm7Guest(virCapsPtr caps)
427 428 429 430 431 432 433 434
{
    static const char *machines[] = { "vexpress-a9",
                                      "vexpress-a15",
                                      "versatilepb" };
    virCapsGuestMachinePtr *capsmachines = NULL;
    virCapsGuestPtr guest;

    capsmachines = virCapabilitiesAllocMachines(machines,
435
                                                G_N_ELEMENTS(machines));
436 437 438
    if (!capsmachines)
        goto error;

439
    guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_ARMV7L,
440
                                    QEMUBinList[TEST_UTILS_QEMU_BIN_ARM],
441
                                    NULL,
442
                                    G_N_ELEMENTS(machines),
443 444 445 446
                                    capsmachines);
    if (!guest)
        goto error;

447
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_QEMU, NULL, NULL, 0, NULL))
448
        goto error;
449 450 451
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_KVM,
                                       NULL, NULL, 0, NULL))
        goto error;
452 453 454

    return 0;

455
 error:
456
    virCapabilitiesFreeMachines(capsmachines, G_N_ELEMENTS(machines));
457 458 459
    return -1;
}

460 461 462 463 464 465 466
static int testQemuAddAARCH64Guest(virCapsPtr caps)
{
    static const char *machines[] = { "virt"};
    virCapsGuestMachinePtr *capsmachines = NULL;
    virCapsGuestPtr guest;

    capsmachines = virCapabilitiesAllocMachines(machines,
467
                                                G_N_ELEMENTS(machines));
468 469 470
    if (!capsmachines)
        goto error;

471
    guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_AARCH64,
472
                                    QEMUBinList[TEST_UTILS_QEMU_BIN_AARCH64],
473
                                    NULL,
474
                                    G_N_ELEMENTS(machines),
475 476 477 478
                                    capsmachines);
    if (!guest)
        goto error;

479
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_QEMU, NULL, NULL, 0, NULL))
480
        goto error;
481 482 483
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_KVM,
                                       NULL, NULL, 0, NULL))
        goto error;
484 485 486

    return 0;

487
 error:
488
    virCapabilitiesFreeMachines(capsmachines, G_N_ELEMENTS(machines));
489 490
    return -1;
}
491

492 493
virCapsPtr testQemuCapsInit(void)
{
494 495
    virCapsPtr caps;

496
    if (!(caps = virCapabilitiesNew(VIR_ARCH_X86_64, false, false)))
497 498
        return NULL;

499 500 501 502 503 504
    /* 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;

505 506
    caps->host.secModels[0].model = g_strdup("none");
    caps->host.secModels[0].doi = g_strdup("0");
507

508
    if (!(cpuDefault = virCPUDefCopy(&cpuDefaultData)) ||
509
        !(cpuHaswell = virCPUDefCopy(&cpuHaswellData)) ||
510 511
        !(cpuPower8 = virCPUDefCopy(&cpuPower8Data)) ||
        !(cpuPower9 = virCPUDefCopy(&cpuPower9Data)))
512 513
        goto cleanup;

514
    qemuTestSetHostCPU(caps, NULL);
515

516 517 518 519 520 521
    /*
     * Build a NUMA topology with cell_id (NUMA node id
     * being 3(0 + 3),4(1 + 3), 5 and 6
     */
    if (virTestCapsBuildNUMATopology(caps, 3) < 0)
        goto cleanup;
522

523
    if (testQemuAddI686Guest(caps) < 0)
524 525
        goto cleanup;

526
    if (testQemuAddX86_64Guest(caps) < 0)
527 528
        goto cleanup;

529 530 531
    if (testQemuAddPPC64Guest(caps))
        goto cleanup;

532 533 534
    if (testQemuAddPPC64LEGuest(caps))
        goto cleanup;

O
Olivia Yin 已提交
535 536 537
    if (testQemuAddPPCGuest(caps))
        goto cleanup;

L
Lubomir Rintel 已提交
538 539 540 541 542 543
    if (testQemuAddRISCV32Guest(caps) < 0)
        goto cleanup;

    if (testQemuAddRISCV64Guest(caps) < 0)
        goto cleanup;

544 545 546
    if (testQemuAddS390Guest(caps))
        goto cleanup;

S
Stefan Schallenberg 已提交
547 548 549 550
    if (testQemuAddArm6Guest(caps))
        goto cleanup;

    if (testQemuAddArm7Guest(caps))
551 552
        goto cleanup;

553 554 555
    if (testQemuAddAARCH64Guest(caps))
        goto cleanup;

556
    if (virTestGetDebug()) {
557 558 559 560 561 562
        char *caps_str;

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

563
        VIR_TEST_DEBUG("QEMU driver capabilities:\n%s", caps_str);
564 565 566 567

        VIR_FREE(caps_str);
    }

568 569
    return caps;

570
 cleanup:
571 572 573
    caps->host.cpu = NULL;
    virCPUDefFree(cpuDefault);
    virCPUDefFree(cpuHaswell);
574
    virCPUDefFree(cpuPower8);
575
    virObjectUnref(caps);
576 577
    return NULL;
}
578 579


580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
void
qemuTestSetHostArch(virCapsPtr caps,
                    virArch arch)
{
    if (arch == VIR_ARCH_NONE)
        arch = VIR_ARCH_X86_64;
    caps->host.arch = arch;
    qemuTestSetHostCPU(caps, NULL);
}


void
qemuTestSetHostCPU(virCapsPtr caps,
                   virCPUDefPtr cpu)
{
    virArch arch = caps->host.arch;

    if (!cpu) {
        if (ARCH_IS_X86(arch))
            cpu = cpuDefault;
600 601
        else if (ARCH_IS_PPC64(arch))
            cpu = cpuPower8;
602 603
    }

604
    unsetenv("VIR_TEST_MOCK_FAKE_HOST_CPU");
P
Pavel Hrdina 已提交
605
    if (cpu) {
606
        caps->host.arch = cpu->arch;
P
Pavel Hrdina 已提交
607 608 609
        if (cpu->model)
            setenv("VIR_TEST_MOCK_FAKE_HOST_CPU", cpu->model, 1);
    }
610 611 612 613
    caps->host.cpu = cpu;
}


614
virQEMUCapsPtr
615 616
qemuTestParseCapabilitiesArch(virArch arch,
                              const char *capsFile)
617 618 619
{
    virQEMUCapsPtr qemuCaps = NULL;

620
    if (!(qemuCaps = virQEMUCapsNew()) ||
621
        virQEMUCapsLoadCache(arch, qemuCaps, capsFile) < 0)
622 623 624 625 626 627 628 629
        goto error;

    return qemuCaps;

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

631 632 633 634 635 636 637 638 639 640 641 642

virQEMUCapsPtr
qemuTestParseCapabilities(virCapsPtr caps,
                          const char *capsFile)
{
    if (!caps)
        return NULL;

    return qemuTestParseCapabilitiesArch(caps->host.arch, capsFile);
}


643 644
void qemuTestDriverFree(virQEMUDriver *driver)
{
645
    virMutexDestroy(&driver->lock);
646 647 648 649
    if (driver->config) {
        virFileDeleteTree(driver->config->stateDir);
        virFileDeleteTree(driver->config->configDir);
    }
650
    virObjectUnref(driver->qemuCapsCache);
651 652 653
    virObjectUnref(driver->xmlopt);
    virObjectUnref(driver->caps);
    virObjectUnref(driver->config);
654
    virObjectUnref(driver->securityManager);
655 656
}

657
int qemuTestCapsCacheInsert(virFileCachePtr cache,
658 659
                            virQEMUCapsPtr caps)
{
660 661
    size_t i;
    virQEMUCapsPtr tmpCaps;
662 663

    if (caps) {
664
        tmpCaps = caps;
665
    } else {
666
        if (!(tmpCaps = virQEMUCapsNew()))
667 668 669
            return -ENOMEM;
    }

670
    for (i = 0; i < G_N_ELEMENTS(QEMUBinList); i++) {
671
        virObjectRef(tmpCaps);
672
        if (virFileCacheInsertData(cache, QEMUBinList[i], tmpCaps) < 0) {
673 674 675 676
            virObjectUnref(tmpCaps);
            return -1;
        }
    }
677

678 679
    if (!caps)
        virObjectUnref(tmpCaps);
680

681
    return 0;
682 683
}

684

685 686 687
# define STATEDIRTEMPLATE abs_builddir "/qemustatedir-XXXXXX"
# define CONFIGDIRTEMPLATE abs_builddir "/qemuconfigdir-XXXXXX"

688 689
int qemuTestDriverInit(virQEMUDriver *driver)
{
690
    virSecurityManagerPtr mgr = NULL;
691 692
    char statedir[] = STATEDIRTEMPLATE;
    char configdir[] = CONFIGDIRTEMPLATE;
693

694 695
    memset(driver, 0, sizeof(*driver));

696 697 698
    if (virMutexInit(&driver->lock) < 0)
        return -1;

699 700
    driver->config = virQEMUDriverConfigNew(false);
    if (!driver->config)
701
        goto error;
702

703 704 705 706 707
    /* Do this early so that qemuTestDriverFree() doesn't see (unlink) the real
     * dirs. */
    VIR_FREE(driver->config->stateDir);
    VIR_FREE(driver->config->configDir);

708 709 710
    /* Overwrite some default paths so it's consistent for tests. */
    VIR_FREE(driver->config->libDir);
    VIR_FREE(driver->config->channelTargetDir);
711 712
    driver->config->libDir = g_strdup("/tmp/lib");
    driver->config->channelTargetDir = g_strdup("/tmp/channel");
713

714 715 716 717 718
    if (!mkdtemp(statedir)) {
        virFilePrintf(stderr, "Cannot create fake stateDir");
        goto error;
    }

719
    driver->config->stateDir = g_strdup(statedir);
720 721 722 723 724 725

    if (!mkdtemp(configdir)) {
        virFilePrintf(stderr, "Cannot create fake configDir");
        goto error;
    }

726
    driver->config->configDir = g_strdup(configdir);
727

728 729 730 731
    driver->caps = testQemuCapsInit();
    if (!driver->caps)
        goto error;

P
Pavel Fedin 已提交
732 733
    /* Using /dev/null for libDir and cacheDir automatically produces errors
     * upon attempt to use any of them */
734
    driver->qemuCapsCache = virQEMUCapsCacheNew("/dev/null", "/dev/null", 0, 0);
P
Pavel Fedin 已提交
735 736 737
    if (!driver->qemuCapsCache)
        goto error;

738 739 740 741
    driver->xmlopt = virQEMUDriverCreateXMLConf(driver);
    if (!driver->xmlopt)
        goto error;

742
    if (qemuTestCapsCacheInsert(driver->qemuCapsCache, NULL) < 0)
P
Pavel Fedin 已提交
743 744
        goto error;

745
    if (!(mgr = virSecurityManagerNew("none", "qemu",
746 747 748 749 750
                                      VIR_SECURITY_MANAGER_PRIVILEGED)))
        goto error;
    if (!(driver->securityManager = virSecurityManagerNewStack(mgr)))
        goto error;

751 752 753
    return 0;

 error:
754
    virObjectUnref(mgr);
755 756 757
    qemuTestDriverFree(driver);
    return -1;
}
758

759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
int
testQemuCapsSetGIC(virQEMUCapsPtr qemuCaps,
                   int gic)
{
    virGICCapability *gicCapabilities = NULL;
    size_t ngicCapabilities = 0;
    int ret = -1;

    if (VIR_ALLOC_N(gicCapabilities, 2) < 0)
        goto out;

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

    ret = 0;

 out:
    return ret;
}

A
Atsushi SAKAI 已提交
795
#endif
796 797 798


char *
799
testQemuGetLatestCapsForArch(const char *arch,
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
                             const char *suffix)
{
    struct dirent *ent;
    DIR *dir = NULL;
    int rc;
    char *fullsuffix = NULL;
    char *tmp = NULL;
    unsigned long maxver = 0;
    unsigned long ver;
    const char *maxname = NULL;
    char *ret = NULL;

    if (virAsprintf(&fullsuffix, "%s.%s", arch, suffix) < 0)
        goto cleanup;

815
    if (virDirOpen(&dir, TEST_QEMU_CAPS_PATH) < 0)
816 817
        goto cleanup;

818
    while ((rc = virDirRead(dir, &ent, TEST_QEMU_CAPS_PATH)) > 0) {
819 820
        VIR_FREE(tmp);

821
        tmp = g_strdup(STRSKIP(ent->d_name, "caps_"));
822

823
        if (!tmp)
824 825
            continue;

826
        if (!virStringStripSuffix(tmp, fullsuffix))
827 828 829
            continue;

        if (virParseVersionString(tmp, &ver, false) < 0) {
830
            VIR_TEST_DEBUG("skipping caps file '%s'", ent->d_name);
831 832 833 834 835 836 837 838 839 840 841 842 843
            continue;
        }

        if (ver > maxver) {
            maxname = ent->d_name;
            maxver = ver;
        }
    }

    if (rc < 0)
        goto cleanup;

    if (!maxname) {
844
        VIR_TEST_VERBOSE("failed to find capabilities for '%s' in '%s'",
845
                         arch, TEST_QEMU_CAPS_PATH);
846 847 848
        goto cleanup;
    }

849
    ignore_value(virAsprintf(&ret, "%s/%s", TEST_QEMU_CAPS_PATH, maxname));
850 851 852 853 854 855 856

 cleanup:
    VIR_FREE(tmp);
    VIR_FREE(fullsuffix);
    virDirClose(&dir);
    return ret;
}
857 858


C
Cole Robinson 已提交
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
virHashTablePtr
testQemuGetLatestCaps(void)
{
    const char *archs[] = {
        "aarch64",
        "ppc64",
        "riscv64",
        "s390x",
        "x86_64",
    };
    virHashTablePtr capslatest;
    size_t i;

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

875
    VIR_TEST_VERBOSE("");
C
Cole Robinson 已提交
876

877
    for (i = 0; i < G_N_ELEMENTS(archs); ++i) {
878
        char *cap = testQemuGetLatestCapsForArch(archs[i], "xml");
C
Cole Robinson 已提交
879 880 881 882

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

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

886 887
    VIR_TEST_VERBOSE("");

C
Cole Robinson 已提交
888 889 890 891 892 893 894 895
    return capslatest;

 error:
    virHashFree(capslatest);
    return NULL;
}


896
int
897
testQemuCapsIterate(const char *suffix,
898 899 900 901 902 903 904 905 906 907 908
                    testQemuCapsIterateCallback callback,
                    void *opaque)
{
    struct dirent *ent;
    DIR *dir = NULL;
    int rc;
    int ret = -1;

    if (!callback)
        return 0;

909 910 911 912 913 914
    /* Validate suffix */
    if (!STRPREFIX(suffix, ".")) {
        VIR_TEST_VERBOSE("malformed suffix '%s'", suffix);
        goto cleanup;
    }

915
    if (virDirOpen(&dir, TEST_QEMU_CAPS_PATH) < 0)
916 917
        goto cleanup;

918
    while ((rc = virDirRead(dir, &ent, TEST_QEMU_CAPS_PATH)) > 0) {
919
        g_autofree char *tmp = g_strdup(ent->d_name);
920
        char *version = NULL;
921 922 923 924 925 926
        char *archName = NULL;

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

927 928 929 930 931 932
        /* Strip the leading prefix */
        if (!(version = STRSKIP(tmp, "caps_"))) {
            VIR_TEST_VERBOSE("malformed file name '%s'", ent->d_name);
            goto cleanup;
        }

933 934 935 936 937
        /* Find the last dot */
        if (!(archName = strrchr(tmp, '.'))) {
            VIR_TEST_VERBOSE("malformed file name '%s'", ent->d_name);
            goto cleanup;
        }
938

939 940 941
        /* 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 */
942 943 944
        archName[0] = '\0';
        archName++;

945 946 947 948 949 950
        /* 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.
         */
951
        if (callback(TEST_QEMU_CAPS_PATH, "caps", version,
952
                     archName, suffix + 1, opaque) < 0) {
953
            goto cleanup;
954
        }
955 956 957 958 959 960 961 962 963 964 965 966
    }

    if (rc < 0)
        goto cleanup;

    ret = 0;

 cleanup:
    virDirClose(&dir);

    return ret;
}
967 968 969 970 971 972 973 974 975 976 977 978


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;
979
    g_autofree char *capsfile = NULL;
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 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 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
    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;

        if (STREQ(capsver, "latest")) {
1065
            capsfile = g_strdup(virHashLookup(capslatest, capsarch));
1066 1067
            stripmachinealiases = true;
        } else if (virAsprintf(&capsfile, "%s/caps_%s.%s.xml",
1068
                               TEST_QEMU_CAPS_PATH, capsver, capsarch) < 0) {
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
            goto cleanup;
        }

        if (!(qemuCaps = qemuTestParseCapabilitiesArch(virArchFromString(capsarch),
                                                       capsfile))) {
            goto cleanup;
        }

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

    if (!qemuCaps) {
        fprintf(stderr, "No qemuCaps generated\n");
        goto cleanup;
    }
1086
    info->qemuCaps = g_steal_pointer(&qemuCaps);
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107

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