From e3f778f53a39635ac34e498219c2d08fee52d0b0 Mon Sep 17 00:00:00 2001 Message-Id: From: Andrea Bolognani Date: Mon, 17 Jul 2017 12:09:02 +0200 Subject: [PATCH] tests: Mock IOMMU groups Later on we're going to need access to information about IOMMU groups for host devices. Implement the support in virpcimock, and start using that mock library in a few QEMU test cases. Signed-off-by: Andrea Bolognani Reviewed-by: Laine Stump (cherry picked from commit dd9b29dad8ec3d5372ee5e857ab0922fecb26718) Conflicts: * tests/qemumemlocktest.c, tests/qemuxml2argvtest.c: caused by commit 4ceac4bf2931fb70ac1af4f9568c31d41959e8ce, which changed the name of the macro formerly known as VIRT_TEST_MAIN_PRELOAD(), being missing; * tests/qemuxml2xmltest.c: in addition to the same conflict as above, we also have to reintroduce the use of qemuxml2xmlmock because commit 2a13a0a103340878b05a5ae0b9f454721f67dbb3, which removed it from the test suite, has not been backported. Bug: https://bugzilla.redhat.com/show_bug.cgi?id=1431193 Signed-off-by: Andrea Bolognani Signed-off-by: Jiri Denemark --- tests/qemumemlocktest.c | 21 ++++++++++++++++++++- tests/qemuxml2argvtest.c | 21 ++++++++++++++++++++- tests/qemuxml2xmltest.c | 22 +++++++++++++++++++++- tests/virpcimock.c | 43 +++++++++++++++++++++++++++++++++++++------ 4 files changed, 98 insertions(+), 9 deletions(-) diff --git a/tests/qemumemlocktest.c b/tests/qemumemlocktest.c index 48b0565d68..ea25cd9a66 100644 --- a/tests/qemumemlocktest.c +++ b/tests/qemumemlocktest.c @@ -56,11 +56,25 @@ testCompareMemLock(const void *data) return ret; } +# define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX" static int mymain(void) { int ret = 0; + char *fakerootdir; + + if (VIR_STRDUP_QUIET(fakerootdir, FAKEROOTDIRTEMPLATE) < 0) { + fprintf(stderr, "Out of memory\n"); + abort(); + } + + if (!mkdtemp(fakerootdir)) { + fprintf(stderr, "Cannot create fakerootdir"); + abort(); + } + + setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, 1); abs_top_srcdir = getenv("abs_top_srcdir"); if (!abs_top_srcdir) @@ -124,12 +138,17 @@ mymain(void) DO_TEST("pseries-hardlimit+locked+hostdev", 2147483648); DO_TEST("pseries-locked+hostdev", VIR_DOMAIN_MEMORY_PARAM_UNLIMITED); + if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL) + virFileDeleteTree(fakerootdir); + qemuTestDriverFree(&driver); + VIR_FREE(fakerootdir); return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } -VIRT_TEST_MAIN(mymain) +VIRT_TEST_MAIN_PRELOAD(mymain, + abs_builddir "/.libs/virpcimock.so") #else diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index d4afaef06e..2a0b0ce954 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -542,13 +542,27 @@ testCompareXMLToArgv(const void *data) return ret; } +# define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX" static int mymain(void) { int ret = 0; + char *fakerootdir; bool skipLegacyCPUs = false; + if (VIR_STRDUP_QUIET(fakerootdir, FAKEROOTDIRTEMPLATE) < 0) { + fprintf(stderr, "Out of memory\n"); + abort(); + } + + if (!mkdtemp(fakerootdir)) { + fprintf(stderr, "Cannot create fakerootdir"); + abort(); + } + + setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, 1); + abs_top_srcdir = getenv("abs_top_srcdir"); if (!abs_top_srcdir) abs_top_srcdir = abs_srcdir "/.."; @@ -2672,7 +2686,11 @@ mymain(void) DO_TEST_PARSE_ERROR("cpu-cache-passthrough3", QEMU_CAPS_KVM); DO_TEST_PARSE_ERROR("cpu-cache-passthrough-l3", QEMU_CAPS_KVM); + if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL) + virFileDeleteTree(fakerootdir); + qemuTestDriverFree(&driver); + VIR_FREE(fakerootdir); return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } @@ -2680,7 +2698,8 @@ mymain(void) VIRT_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/qemuxml2argvmock.so", abs_builddir "/.libs/virrandommock.so", - abs_builddir "/.libs/qemucpumock.so") + abs_builddir "/.libs/qemucpumock.so", + abs_builddir "/.libs/virpcimock.so") #else diff --git a/tests/qemuxml2xmltest.c b/tests/qemuxml2xmltest.c index d026bdfa68..95f053faa7 100644 --- a/tests/qemuxml2xmltest.c +++ b/tests/qemuxml2xmltest.c @@ -307,14 +307,28 @@ testInfoSet(struct testInfo *info, return -1; } +# define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX" static int mymain(void) { int ret = 0; + char *fakerootdir; struct testInfo info; virQEMUDriverConfigPtr cfg = NULL; + if (VIR_STRDUP_QUIET(fakerootdir, FAKEROOTDIRTEMPLATE) < 0) { + fprintf(stderr, "Out of memory\n"); + abort(); + } + + if (!mkdtemp(fakerootdir)) { + fprintf(stderr, "Cannot create fakerootdir"); + abort(); + } + + setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, 1); + memset(&info, 0, sizeof(info)); if (qemuTestDriverInit(&driver) < 0) @@ -1142,12 +1156,18 @@ mymain(void) DO_TEST("cpu-check-default-partial", NONE); DO_TEST("cpu-check-default-partial2", NONE); + if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL) + virFileDeleteTree(fakerootdir); + qemuTestDriverFree(&driver); + VIR_FREE(fakerootdir); return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } -VIRT_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/qemuxml2xmlmock.so") +VIRT_TEST_MAIN_PRELOAD(mymain, + abs_builddir "/.libs/qemuxml2xmlmock.so", + abs_builddir "/.libs/virpcimock.so") #else diff --git a/tests/virpcimock.c b/tests/virpcimock.c index e9408aa0ae..1112a7215d 100644 --- a/tests/virpcimock.c +++ b/tests/virpcimock.c @@ -127,6 +127,7 @@ struct pciDevice { int vendor; int device; int class; + int iommuGroup; struct pciDriver *driver; /* Driver attached. NULL if attached to no driver */ }; @@ -190,6 +191,22 @@ make_file(const char *path, VIR_FREE(filepath); } +static void +make_symlink(const char *path, + const char *name, + const char *target) +{ + char *filepath = NULL; + + if (virAsprintfQuiet(&filepath, "%s/%s", path, name) < 0) + ABORT_OOM(); + + if (symlink(target, filepath) < 0) + ABORT("Unable to create symlink filepath -> target"); + + VIR_FREE(filepath); +} + static int pci_read_file(const char *path, char *buf, @@ -322,7 +339,7 @@ pci_device_new_from_stub(const struct pciDevice *data) char *id; char *c; char *configSrc; - char tmp[32]; + char tmp[256]; struct stat sb; if (VIR_STRDUP_QUIET(id, data->id) < 0) @@ -386,6 +403,20 @@ pci_device_new_from_stub(const struct pciDevice *data) ABORT("@tmp overflow"); make_file(devpath, "class", tmp, -1); + if (snprintf(tmp, sizeof(tmp), + "%s/../../../kernel/iommu_groups/%d", + devpath, dev->iommuGroup) < 0) { + ABORT("@tmp overflow"); + } + if (virFileMakePath(tmp) < 0) + ABORT("Unable to create %s", tmp); + + if (snprintf(tmp, sizeof(tmp), + "../../../kernel/iommu_groups/%d", dev->iommuGroup) < 0) { + ABORT("@tmp overflow"); + } + make_symlink(devpath, "iommu_group", tmp); + if (pci_device_autobind(dev) < 0) ABORT("Unable to bind: %s", data->id); @@ -821,12 +852,12 @@ init_env(void) MAKE_PCI_DEVICE("0000:00:02.0", 0x8086, 0x0046); MAKE_PCI_DEVICE("0000:00:03.0", 0x8086, 0x0048); MAKE_PCI_DEVICE("0001:00:00.0", 0x1014, 0x03b9, .class = 0x060400); - MAKE_PCI_DEVICE("0001:01:00.0", 0x8086, 0x105e); - MAKE_PCI_DEVICE("0001:01:00.1", 0x8086, 0x105e); + MAKE_PCI_DEVICE("0001:01:00.0", 0x8086, 0x105e, .iommuGroup = 0); + MAKE_PCI_DEVICE("0001:01:00.1", 0x8086, 0x105e, .iommuGroup = 0); MAKE_PCI_DEVICE("0005:80:00.0", 0x10b5, 0x8112, .class = 0x060400); - MAKE_PCI_DEVICE("0005:90:01.0", 0x1033, 0x0035); - MAKE_PCI_DEVICE("0005:90:01.1", 0x1033, 0x0035); - MAKE_PCI_DEVICE("0005:90:01.2", 0x1033, 0x00e0); + MAKE_PCI_DEVICE("0005:90:01.0", 0x1033, 0x0035, .iommuGroup = 1); + MAKE_PCI_DEVICE("0005:90:01.1", 0x1033, 0x0035, .iommuGroup = 1); + MAKE_PCI_DEVICE("0005:90:01.2", 0x1033, 0x00e0, .iommuGroup = 1); MAKE_PCI_DEVICE("0000:0a:01.0", 0x8086, 0x0047); MAKE_PCI_DEVICE("0000:0a:02.0", 0x8286, 0x0048); MAKE_PCI_DEVICE("0000:0a:03.0", 0x8386, 0x0048); -- 2.13.3