domaincapstest.c 15.2 KB
Newer Older
M
Michal Privoznik 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright (C) Red Hat, Inc. 2014
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include "testutils.h"
#include "domain_capabilities.h"


#define VIR_FROM_THIS VIR_FROM_NONE

27 28
typedef int (*virDomainCapsFill)(virDomainCapsPtr domCaps,
                                 void *opaque);
M
Michal Privoznik 已提交
29 30 31 32

#define SET_ALL_BITS(x) \
    memset(&(x.values), 0xff, sizeof(x.values))

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
static int ATTRIBUTE_SENTINEL
fillStringValues(virDomainCapsStringValuesPtr values, ...)
{
    int ret = 0;
    va_list list;
    const char *str;

    va_start(list, values);
    while ((str = va_arg(list, const char *))) {
        if (VIR_REALLOC_N(values->values, values->nvalues + 1) < 0 ||
            VIR_STRDUP(values->values[values->nvalues], str) < 0) {
            ret = -1;
            break;
        }
        values->nvalues++;
    }
    va_end(list);

    return ret;
}

54
static int
J
Jiri Denemark 已提交
55
fillAllCaps(virDomainCapsPtr domCaps)
M
Michal Privoznik 已提交
56
{
57 58
    virDomainCapsOSPtr os = &domCaps->os;
    virDomainCapsLoaderPtr loader = &os->loader;
59
    virDomainCapsCPUPtr cpu = &domCaps->cpu;
M
Michal Privoznik 已提交
60
    virDomainCapsDeviceDiskPtr disk = &domCaps->disk;
61
    virDomainCapsDeviceGraphicsPtr graphics = &domCaps->graphics;
62
    virDomainCapsDeviceVideoPtr video = &domCaps->video;
M
Michal Privoznik 已提交
63
    virDomainCapsDeviceHostdevPtr hostdev = &domCaps->hostdev;
64
    virCPUDef host = {
65 66 67 68
        .type = VIR_CPU_TYPE_HOST,
        .arch = VIR_ARCH_X86_64,
        .model = (char *) "host",
        .vendor = (char *) "CPU Vendorrr",
69
    };
M
Michal Privoznik 已提交
70

71
    domCaps->maxvcpus = 255;
72
    os->supported = true;
73

74
    loader->supported = true;
75 76
    SET_ALL_BITS(loader->type);
    SET_ALL_BITS(loader->readonly);
77 78 79 80 81
    if (fillStringValues(&loader->values,
                         "/foo/bar",
                         "/tmp/my_path",
                         NULL) < 0)
        return -1;
82

83
    cpu->hostPassthrough = true;
84
    cpu->hostModel = virCPUDefCopy(&host);
85
    if (!(cpu->custom = virDomainCapsCPUModelsNew(3)) ||
J
Jiri Denemark 已提交
86
        virDomainCapsCPUModelsAdd(cpu->custom, "Model1", -1,
87
                                  VIR_DOMCAPS_CPU_USABLE_UNKNOWN, NULL) < 0 ||
J
Jiri Denemark 已提交
88
        virDomainCapsCPUModelsAdd(cpu->custom, "Model2", -1,
89
                                  VIR_DOMCAPS_CPU_USABLE_NO, NULL) < 0 ||
J
Jiri Denemark 已提交
90
        virDomainCapsCPUModelsAdd(cpu->custom, "Model3", -1,
91
                                  VIR_DOMCAPS_CPU_USABLE_YES, NULL) < 0)
92 93
        return -1;

94
    disk->supported = true;
M
Michal Privoznik 已提交
95 96 97
    SET_ALL_BITS(disk->diskDevice);
    SET_ALL_BITS(disk->bus);

98 99 100
    graphics->supported = true;
    SET_ALL_BITS(graphics->type);

101 102 103
    video->supported = true;
    SET_ALL_BITS(video->modelType);

104
    hostdev->supported = true;
M
Michal Privoznik 已提交
105 106 107 108 109
    SET_ALL_BITS(hostdev->mode);
    SET_ALL_BITS(hostdev->startupPolicy);
    SET_ALL_BITS(hostdev->subsysType);
    SET_ALL_BITS(hostdev->capsType);
    SET_ALL_BITS(hostdev->pciBackend);
110
    return 0;
M
Michal Privoznik 已提交
111 112
}

113

J
Jiri Denemark 已提交
114
#if WITH_QEMU
115
# include "testutilsqemu.h"
P
Pavel Hrdina 已提交
116
# include "testutilshostcpus.h"
117

118 119 120 121 122 123
static int
fakeHostCPU(virCapsPtr caps,
            virArch arch)
{
    virCPUDefPtr cpu;

P
Pavel Hrdina 已提交
124
    if (!(cpu = testUtilsHostCpusGetDefForArch(arch))) {
125 126 127 128 129 130
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "cannot fake host CPU for arch %s",
                       virArchToString(arch));
        return -1;
    }

P
Pavel Hrdina 已提交
131
    qemuTestSetHostCPU(caps, cpu);
132 133 134 135

    return 0;
}

136
static int
137
fillQemuCaps(virDomainCapsPtr domCaps,
J
Jiri Denemark 已提交
138
             const char *name,
J
Jiri Denemark 已提交
139
             const char *arch,
140
             const char *machine,
J
Jiri Denemark 已提交
141
             virQEMUDriverConfigPtr cfg)
142
{
J
Jiri Denemark 已提交
143 144
    int ret = -1;
    char *path = NULL;
145
    virCapsPtr caps = NULL;
J
Jiri Denemark 已提交
146
    virQEMUCapsPtr qemuCaps = NULL;
147
    virDomainCapsLoaderPtr loader = &domCaps->os.loader;
148

149 150 151 152
    if (!(caps = virCapabilitiesNew(domCaps->arch, false, false)) ||
        fakeHostCPU(caps, domCaps->arch) < 0)
        goto cleanup;

153
    if (virAsprintf(&path, "%s/qemucapabilitiesdata/%s.%s.xml",
J
Jiri Denemark 已提交
154
                    abs_srcdir, name, arch) < 0 ||
155
        !(qemuCaps = qemuTestParseCapabilities(caps, path)))
J
Jiri Denemark 已提交
156 157
        goto cleanup;

158 159 160 161 162 163
    if (machine) {
        VIR_FREE(domCaps->machine);
        if (VIR_STRDUP(domCaps->machine,
                       virQEMUCapsGetCanonicalMachine(qemuCaps, machine)) < 0)
            goto cleanup;
    }
164 165 166

    if (!domCaps->machine &&
        VIR_STRDUP(domCaps->machine,
167
                   virQEMUCapsGetPreferredMachine(qemuCaps)) < 0)
168 169
        goto cleanup;

170
    if (virQEMUCapsFillDomainCaps(caps, domCaps, qemuCaps,
171
                                  cfg->firmwares,
172
                                  cfg->nfirmwares) < 0)
J
Jiri Denemark 已提交
173
        goto cleanup;
174 175 176 177 178 179 180 181 182 183

    /* The function above tries to query host's KVM & VFIO capabilities by
     * calling qemuHostdevHostSupportsPassthroughLegacy() and
     * qemuHostdevHostSupportsPassthroughVFIO() which, however, can't be
     * successfully mocked as they are not exposed as internal APIs. Therefore,
     * instead of mocking set the expected values here by hand. */
    VIR_DOMAIN_CAPS_ENUM_SET(domCaps->hostdev.pciBackend,
                             VIR_DOMAIN_HOSTDEV_PCI_BACKEND_DEFAULT,
                             VIR_DOMAIN_HOSTDEV_PCI_BACKEND_KVM,
                             VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO);
184

J
Jiri Denemark 已提交
185 186
    /* As of f05b6a918e28 we are expecting to see OVMF_CODE.fd file which
     * may not exists everywhere. */
187 188 189 190
    while (loader->values.nvalues)
        VIR_FREE(loader->values.values[--loader->values.nvalues]);

    if (fillStringValues(&loader->values,
191
                         "/usr/share/AAVMF/AAVMF_CODE.fd",
192
                         "/usr/share/AAVMF/AAVMF32_CODE.fd",
193 194
                         "/usr/share/OVMF/OVMF_CODE.fd",
                         NULL) < 0)
M
Michal Privoznik 已提交
195 196
        goto cleanup;

J
Jiri Denemark 已提交
197
    ret = 0;
M
Michal Privoznik 已提交
198
 cleanup:
199
    virObjectUnref(caps);
J
Jiri Denemark 已提交
200 201
    virObjectUnref(qemuCaps);
    VIR_FREE(path);
202
    return ret;
M
Michal Privoznik 已提交
203
}
J
Jiri Denemark 已提交
204 205 206
#endif /* WITH_QEMU */


207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
#ifdef WITH_LIBXL
# include "testutilsxen.h"

static int
fillXenCaps(virDomainCapsPtr domCaps)
{
    virFirmwarePtr *firmwares;
    int ret = -1;

    if (VIR_ALLOC_N(firmwares, 2) < 0)
        return ret;

    if (VIR_ALLOC(firmwares[0]) < 0 || VIR_ALLOC(firmwares[1]) < 0)
        goto cleanup;
    if (VIR_STRDUP(firmwares[0]->name, "/usr/lib/xen/boot/hvmloader") < 0 ||
        VIR_STRDUP(firmwares[1]->name, "/usr/lib/xen/boot/ovmf.bin") < 0)
        goto cleanup;

    if (libxlMakeDomainCapabilities(domCaps, firmwares, 2) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
    virFirmwareFreeList(firmwares, 2);
    return ret;
}
#endif /* WITH_LIBXL */

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
#ifdef WITH_BHYVE
# include "bhyve/bhyve_capabilities.h"

static int
fillBhyveCaps(virDomainCapsPtr domCaps, unsigned int *bhyve_caps)
{
    virDomainCapsStringValuesPtr firmwares = NULL;
    int ret = -1;

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

    if (fillStringValues(firmwares, "/foo/bar", "/foo/baz", NULL) < 0)
        goto cleanup;

    if (virBhyveDomainCapsFill(domCaps, *bhyve_caps, firmwares) < 0)
        goto cleanup;

    ret = 0;
 cleanup:
    VIR_FREE(firmwares);
    return ret;
}
#endif /* WITH_BHYVE */
260

J
Jiri Denemark 已提交
261 262 263 264
enum testCapsType {
    CAPS_NONE,
    CAPS_ALL,
    CAPS_QEMU,
265
    CAPS_LIBXL,
266
    CAPS_BHYVE,
J
Jiri Denemark 已提交
267
};
M
Michal Privoznik 已提交
268

J
Jiri Denemark 已提交
269 270 271
struct testData {
    const char *name;
    const char *emulator;
M
Michal Privoznik 已提交
272
    const char *machine;
J
Jiri Denemark 已提交
273
    const char *arch;
M
Michal Privoznik 已提交
274
    virDomainVirtType type;
J
Jiri Denemark 已提交
275 276 277
    enum testCapsType capsType;
    const char *capsName;
    void *capsOpaque;
M
Michal Privoznik 已提交
278 279 280 281 282
};

static int
test_virDomainCapsFormat(const void *opaque)
{
J
Jiri Denemark 已提交
283
    const struct testData *data = opaque;
M
Michal Privoznik 已提交
284 285 286 287 288
    virDomainCapsPtr domCaps = NULL;
    char *path = NULL;
    char *domCapsXML = NULL;
    int ret = -1;

289
    if (virAsprintf(&path, "%s/domaincapsschemadata/%s.xml",
J
Jiri Denemark 已提交
290
                    abs_srcdir, data->name) < 0)
M
Michal Privoznik 已提交
291 292
        goto cleanup;

J
Jiri Denemark 已提交
293 294
    if (!(domCaps = virDomainCapsNew(data->emulator, data->machine,
                                     virArchFromString(data->arch),
J
Jiri Denemark 已提交
295
                                     data->type)))
M
Michal Privoznik 已提交
296 297
        goto cleanup;

J
Jiri Denemark 已提交
298 299 300 301 302 303 304 305 306 307 308
    switch (data->capsType) {
    case CAPS_NONE:
        break;

    case CAPS_ALL:
        if (fillAllCaps(domCaps) < 0)
            goto cleanup;
        break;

    case CAPS_QEMU:
#if WITH_QEMU
309
        if (fillQemuCaps(domCaps, data->capsName, data->arch, data->machine,
310
                         data->capsOpaque) < 0)
J
Jiri Denemark 已提交
311
            goto cleanup;
312 313 314 315 316 317 318
#endif
        break;

    case CAPS_LIBXL:
#if WITH_LIBXL
        if (fillXenCaps(domCaps) < 0)
            goto cleanup;
319 320 321 322 323 324
#endif
        break;
    case CAPS_BHYVE:
#if WITH_BHYVE
        if (fillBhyveCaps(domCaps, data->capsOpaque) < 0)
            goto cleanup;
J
Jiri Denemark 已提交
325 326 327 328
#endif
        break;
    }

M
Michal Privoznik 已提交
329 330 331
    if (!(domCapsXML = virDomainCapsFormat(domCaps)))
        goto cleanup;

332
    if (virTestCompareToFile(domCapsXML, path) < 0)
M
Michal Privoznik 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
        goto cleanup;

    ret = 0;
 cleanup:
    VIR_FREE(domCapsXML);
    VIR_FREE(path);
    virObjectUnref(domCaps);
    return ret;
}

static int
mymain(void)
{
    int ret = 0;

348 349 350 351
#if WITH_BHYVE
    unsigned int bhyve_caps = 0;
#endif

J
Jiri Denemark 已提交
352
#if WITH_QEMU
353 354
    virQEMUDriverConfigPtr cfg = virQEMUDriverConfigNew(false);

355 356
    if (!cfg)
        return EXIT_FAILURE;
J
Jiri Denemark 已提交
357 358
#endif

359 360 361 362 363 364 365 366 367 368 369 370
#define DO_TEST(Name, Emulator, Machine, Arch, Type, CapsType) \
    do { \
        struct testData data = { \
            .name = Name, \
            .emulator = Emulator, \
            .machine = Machine, \
            .arch = Arch, \
            .type = Type, \
            .capsType = CapsType, \
        }; \
        if (virTestRun(Name, test_virDomainCapsFormat, &data) < 0) \
            ret = -1; \
J
Jiri Denemark 已提交
371
    } while (0)
372

373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
#define DO_TEST_QEMU(Name, CapsName, Emulator, Machine, Arch, Type) \
    do { \
        char *name = NULL; \
        if (virAsprintf(&name, "qemu_%s%s%s.%s", \
                        Name, \
                        Machine ? "-" : "", Machine ? Machine : "", \
                        Arch) < 0) { \
            ret = -1; \
            break; \
        } \
        struct testData data = { \
            .name = name, \
            .emulator = Emulator, \
            .machine = Machine, \
            .arch = Arch, \
            .type = Type, \
            .capsType = CAPS_QEMU, \
            .capsName = CapsName, \
            .capsOpaque = cfg, \
        }; \
        if (virTestRun(name, test_virDomainCapsFormat, &data) < 0) \
            ret = -1; \
        VIR_FREE(name); \
396 397
    } while (0)

398 399 400 401 402 403 404 405 406 407 408 409
#define DO_TEST_LIBXL(Name, Emulator, Machine, Arch, Type) \
    do { \
        struct testData data = { \
            .name = Name, \
            .emulator = Emulator, \
            .machine = Machine, \
            .arch = Arch, \
            .type = Type, \
            .capsType = CAPS_LIBXL, \
        }; \
        if (virTestRun(Name, test_virDomainCapsFormat, &data) < 0) \
            ret = -1; \
410 411
    } while (0)

J
Jiri Denemark 已提交
412
    DO_TEST("full", "/bin/emulatorbin", "my-machine-type",
J
Jiri Denemark 已提交
413
            "x86_64", VIR_DOMAIN_VIRT_KVM, CAPS_ALL);
J
Jiri Denemark 已提交
414

415
#define DO_TEST_BHYVE(Name, Emulator, BhyveCaps, Type) \
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
    do { \
        char *name = NULL; \
        if (virAsprintf(&name, "bhyve_%s.x86_64", Name) < 0) { \
             ret = -1; \
             break; \
        } \
        struct testData data = { \
            .name = name, \
            .emulator = Emulator, \
            .arch = "x86_64", \
            .type = Type, \
            .capsType = CAPS_BHYVE, \
            .capsOpaque = BhyveCaps, \
        }; \
        if (virTestRun(name, test_virDomainCapsFormat, &data) < 0) \
            ret = -1; \
        VIR_FREE(name); \
433 434
    } while (0)

J
Jiri Denemark 已提交
435 436
#if WITH_QEMU

437
    DO_TEST_QEMU("1.7.0", "caps_1.7.0",
438
                 "/usr/bin/qemu-system-x86_64", NULL,
439
                 "x86_64", VIR_DOMAIN_VIRT_KVM);
440

441
    DO_TEST_QEMU("2.6.0", "caps_2.6.0",
442
                 "/usr/bin/qemu-system-x86_64", NULL,
443
                 "x86_64", VIR_DOMAIN_VIRT_KVM);
444

445 446 447 448 449 450 451 452
    DO_TEST_QEMU("2.8.0", "caps_2.8.0",
                 "/usr/bin/qemu-system-x86_64", NULL,
                 "x86_64", VIR_DOMAIN_VIRT_KVM);

    DO_TEST_QEMU("2.8.0-tcg", "caps_2.8.0",
                 "/usr/bin/qemu-system-x86_64", NULL,
                 "x86_64", VIR_DOMAIN_VIRT_QEMU);

453 454 455 456
    DO_TEST_QEMU("2.9.0", "caps_2.9.0",
                 "/usr/bin/qemu-system-x86_64", NULL,
                 "x86_64", VIR_DOMAIN_VIRT_KVM);

457 458 459 460
    DO_TEST_QEMU("2.9.0", "caps_2.9.0",
                 "/usr/bin/qemu-system-x86_64", "q35",
                 "x86_64", VIR_DOMAIN_VIRT_KVM);

461 462 463 464
    DO_TEST_QEMU("2.9.0-tcg", "caps_2.9.0",
                 "/usr/bin/qemu-system-x86_64", NULL,
                 "x86_64", VIR_DOMAIN_VIRT_QEMU);

465 466 467 468
    DO_TEST_QEMU("2.12.0", "caps_2.12.0",
                 "/usr/bin/qemu-system-x86_64", NULL,
                 "x86_64", VIR_DOMAIN_VIRT_KVM);

469
    DO_TEST_QEMU("2.6.0", "caps_2.6.0",
470 471 472
                 "/usr/bin/qemu-system-aarch64", NULL,
                 "aarch64", VIR_DOMAIN_VIRT_KVM);

473
    DO_TEST_QEMU("2.6.0", "caps_2.6.0",
474 475 476
                 "/usr/bin/qemu-system-aarch64", "virt",
                 "aarch64", VIR_DOMAIN_VIRT_KVM);

477
    DO_TEST_QEMU("2.12.0", "caps_2.12.0",
478 479 480
                 "/usr/bin/qemu-system-aarch64", "virt",
                 "aarch64", VIR_DOMAIN_VIRT_KVM);

481 482 483 484
    DO_TEST_QEMU("2.6.0", "caps_2.6.0",
                 "/usr/bin/qemu-system-ppc64", NULL,
                 "ppc64", VIR_DOMAIN_VIRT_KVM);

485 486 487 488
    DO_TEST_QEMU("2.12.0", "caps_2.12.0",
                 "/usr/bin/qemu-system-ppc64", NULL,
                 "ppc64", VIR_DOMAIN_VIRT_KVM);

489 490 491 492 493 494 495 496
    DO_TEST_QEMU("2.7.0", "caps_2.7.0",
                 "/usr/bin/qemu-system-s390x", NULL,
                 "s390x", VIR_DOMAIN_VIRT_KVM);

    DO_TEST_QEMU("2.8.0", "caps_2.8.0",
                 "/usr/bin/qemu-system-s390x", NULL,
                 "s390x", VIR_DOMAIN_VIRT_KVM);

497 498 499 500
    DO_TEST_QEMU("2.12.0", "caps_2.12.0",
                 "/usr/bin/qemu-system-s390x", NULL,
                 "s390x", VIR_DOMAIN_VIRT_KVM);

501 502 503 504
    DO_TEST_QEMU("3.0.0", "caps_3.0.0",
                 "/usr/bin/qemu-system-s390x", NULL,
                 "s390x", VIR_DOMAIN_VIRT_KVM);

505 506 507
    DO_TEST_QEMU("4.0.0", "caps_4.0.0",
                 "/usr/bin/qemu-system-x86_64", NULL,
                 "x86_64", VIR_DOMAIN_VIRT_KVM);
P
Pavel Hrdina 已提交
508 509
    virObjectUnref(cfg);

510 511
#endif /* WITH_QEMU */

512 513
#if WITH_LIBXL

514
    DO_TEST_LIBXL("libxl-xenpv", "/usr/bin/qemu-system-x86_64",
515
                  "xenpv", "x86_64", VIR_DOMAIN_VIRT_XEN);
516
    DO_TEST_LIBXL("libxl-xenfv", "/usr/bin/qemu-system-x86_64",
517 518 519 520
                  "xenfv", "x86_64", VIR_DOMAIN_VIRT_XEN);

#endif /* WITH_LIBXL */

521 522 523 524 525 526 527 528 529 530
#if WITH_BHYVE
    DO_TEST_BHYVE("basic", "/usr/sbin/bhyve", &bhyve_caps, VIR_DOMAIN_VIRT_BHYVE);

    bhyve_caps |= BHYVE_CAP_LPC_BOOTROM;
    DO_TEST_BHYVE("uefi", "/usr/sbin/bhyve", &bhyve_caps, VIR_DOMAIN_VIRT_BHYVE);

    bhyve_caps |= BHYVE_CAP_FBUF;
    DO_TEST_BHYVE("fbuf", "/usr/sbin/bhyve", &bhyve_caps, VIR_DOMAIN_VIRT_BHYVE);
#endif /* WITH_BHYVE */

M
Michal Privoznik 已提交
531 532 533
    return ret;
}

534
#if WITH_QEMU
535
VIR_TEST_MAIN_PRELOAD(mymain,
536 537 538
                       abs_builddir "/.libs/domaincapsmock.so",
                       abs_builddir "/.libs/qemucpumock.so")
#else
539
VIR_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/domaincapsmock.so")
540
#endif