[RFC PATCH 03/28] tests: pci: Mock the iommu groups and vfio

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The iommu group, /dev/vfio/<iommuGroupNumber> behaviours
of the host are mocked. This patch implments support for
multifunction/multiple devices per iommu groups and emulates
the /dev/vfio/<iommuGroupNumber> file correctly.

This code helps adding necessary testcases for pci-hotplug
code.

Signed-off-by: Shivaprasad G Bhat <sbhat@xxxxxxxxxxxxxxxxxx>
---
 tests/virpcimock.c                       |  178 +++++++++++++++++++++++++++---
 tests/virpcitestdata/0005-90-01.1.config |  Bin
 tests/virpcitestdata/0005-90-01.2.config |  Bin
 tests/virpcitestdata/0005-90-01.3.config |  Bin
 4 files changed, 162 insertions(+), 16 deletions(-)
 create mode 100644 tests/virpcitestdata/0005-90-01.3.config

diff --git a/tests/virpcimock.c b/tests/virpcimock.c
index 2a7e9216b2..22adc740b8 100644
--- a/tests/virpcimock.c
+++ b/tests/virpcimock.c
@@ -51,7 +51,13 @@ static DIR * (*real_opendir)(const char *name);
 char *fakerootdir;
 char *fakesysfspcidir;
 
+struct pciIommuGroup {
+    int iommu;
+    size_t nDevicesBoundToVFIO;        /* Indicates the devices in the group */
+};
+
 # define SYSFS_PCI_PREFIX "/sys/bus/pci/"
+# define SYSFS_KERNEL_PREFIX "/sys/kernel/"
 
 # define STDERR(...) \
     fprintf(stderr, "%s %zu: ", __FUNCTION__, (size_t) __LINE__); \
@@ -142,6 +148,9 @@ size_t nPCIDevices = 0;
 struct pciDriver **pciDrivers = NULL;
 size_t nPCIDrivers = 0;
 
+struct pciIommuGroup **pciIommuGroups = NULL;
+size_t npciIommuGroups = 0;
+
 struct fdCallback *callbacks = NULL;
 size_t nCallbacks = 0;
 
@@ -191,7 +200,7 @@ make_file(const char *path,
     VIR_FREE(filepath);
 }
 
-static void
+static void ATTRIBUTE_UNUSED
 make_symlink(const char *path,
           const char *name,
           const char *target)
@@ -255,6 +264,13 @@ getrealpath(char **newpath,
             errno = ENOMEM;
             return -1;
         }
+    } else if (STRPREFIX(path, SYSFS_KERNEL_PREFIX)) {
+        if (virAsprintfQuiet(newpath, "%s/%s",
+                             fakerootdir,
+                             path) < 0) {
+            errno = ENOMEM;
+            return -1;
+        }
     } else {
         if (VIR_STRDUP_QUIET(*newpath, path) < 0)
             return -1;
@@ -465,6 +481,101 @@ pci_device_autobind(struct pciDevice *dev)
     return pci_driver_bind(driver, dev);
 }
 
+static void
+pci_iommu_new(int num)
+{
+    char *iommupath, *kerneldir;
+    struct pciIommuGroup *iommuGroup;
+
+    if (VIR_ALLOC_QUIET(iommuGroup) < 0)
+        ABORT_OOM();
+
+    iommuGroup->iommu = num;
+    iommuGroup->nDevicesBoundToVFIO = 0; /* No device bound to vfio by default */
+
+    if (virAsprintfQuiet(&kerneldir, "%s%s",
+                         fakerootdir, SYSFS_KERNEL_PREFIX) < 0)
+        ABORT_OOM();
+
+    if (virAsprintfQuiet(&iommupath, "%s/iommu_groups/%d/devices", kerneldir, num) < 0)
+        ABORT_OOM();
+    VIR_FREE(kerneldir);
+
+    if (virFileMakePath(iommupath) < 0)
+        ABORT("Unable to create: %s", iommupath);
+    VIR_FREE(iommupath);
+
+    if (VIR_APPEND_ELEMENT_QUIET(pciIommuGroups, npciIommuGroups, iommuGroup) < 0)
+        ABORT_OOM();
+}
+
+static int
+pci_vfio_release_iommu(struct pciDevice *device)
+{
+    char *vfiopath = NULL;
+    int ret = -1;
+    size_t i = 0;
+
+    for (i = 0; i < npciIommuGroups; i++) {
+        if (pciIommuGroups[i]->iommu == device->iommuGroup)
+            break;
+    }
+
+    if (i != npciIommuGroups) {
+        if (pciIommuGroups[i]->nDevicesBoundToVFIO == 0) {
+            ret = 0;
+            goto cleanup;
+        }
+        pciIommuGroups[i]->nDevicesBoundToVFIO--;
+        if (!pciIommuGroups[i]->nDevicesBoundToVFIO) {
+            if (virAsprintfQuiet(&vfiopath, "%s/dev/vfio/%d",
+                         fakesysfspcidir, device->iommuGroup) < 0) {
+                errno = ENOMEM;
+                goto cleanup;
+            }
+            if (unlink(vfiopath) < 0)
+                goto cleanup;
+        }
+    }
+
+    ret = 0;
+ cleanup:
+    VIR_FREE(vfiopath);
+    return ret;
+}
+
+static int
+pci_vfio_lock_iommu(struct pciDevice *device)
+{
+    char *vfiopath = NULL;
+    int ret = -1;
+    size_t i = 0;
+    int fd = -1;
+
+    for (i = 0; i < npciIommuGroups; i++) {
+        if (pciIommuGroups[i]->iommu == device->iommuGroup)
+            break;
+    }
+
+    if (i != npciIommuGroups) {
+        if (!pciIommuGroups[i]->nDevicesBoundToVFIO) {
+            if (virAsprintfQuiet(&vfiopath, "%s/dev/vfio/%d",
+                         fakesysfspcidir, device->iommuGroup) < 0) {
+                errno = ENOMEM;
+                goto cleanup;
+            }
+            if ((fd = real_open(vfiopath, O_CREAT)) < 0)
+                goto cleanup;
+        }
+        pciIommuGroups[i]->nDevicesBoundToVFIO++;
+    }
+
+    ret = 0;
+ cleanup:
+    real_close(fd);
+    VIR_FREE(vfiopath);
+    return ret;
+}
 
 /*
  * PCI Driver functions
@@ -588,6 +699,10 @@ pci_driver_bind(struct pciDriver *driver,
     if (symlink(devpath, driverpath) < 0)
         goto cleanup;
 
+    if (STREQ(driver->name, "vfio-pci"))
+        if (pci_vfio_lock_iommu(dev) < 0)
+            goto cleanup;
+
     dev->driver = driver;
     ret = 0;
  cleanup:
@@ -622,6 +737,10 @@ pci_driver_unbind(struct pciDriver *driver,
         unlink(driverpath) < 0)
         goto cleanup;
 
+    if (STREQ(driver->name, "vfio-pci"))
+        if (pci_vfio_release_iommu(dev) < 0)
+            goto cleanup;
+
     dev->driver = NULL;
     ret = 0;
  cleanup:
@@ -819,6 +938,8 @@ init_syms(void)
 static void
 init_env(void)
 {
+    char *devvfio;
+
     if (fakerootdir && fakesysfspcidir)
         return;
 
@@ -833,6 +954,28 @@ init_env(void)
         ABORT("Unable to create: %s", fakesysfspcidir);
 
     make_file(fakesysfspcidir, "drivers_probe", NULL, -1);
+    if (virAsprintfQuiet(&devvfio, "%s/dev/vfio", fakesysfspcidir) < 0)
+        ABORT_OOM();
+
+    if (virFileMakePath(devvfio) < 0)
+        ABORT("Unable to create: %s", devvfio);
+
+    /* Create /dev/vfio/vfio file */
+    make_file(devvfio, "vfio", NULL, -1);
+    VIR_FREE(devvfio);
+
+    pci_iommu_new(0);
+    pci_iommu_new(1);
+    pci_iommu_new(2);
+    pci_iommu_new(3);
+    pci_iommu_new(4);
+    pci_iommu_new(5);
+    pci_iommu_new(6);
+    pci_iommu_new(7);
+    pci_iommu_new(8);
+    pci_iommu_new(9);
+    pci_iommu_new(10);
+    pci_iommu_new(11);
 
 # define MAKE_PCI_DRIVER(name, ...) \
     pci_driver_new(name, 0, __VA_ARGS__, -1, -1)
@@ -842,6 +985,7 @@ init_env(void)
     MAKE_PCI_DRIVER("pci-stub", -1, -1);
     pci_driver_new("vfio-pci", PCI_ACTION_BIND, -1, -1);
 
+
 # define MAKE_PCI_DEVICE(Id, Vendor, Device, ...) \
     do { \
         struct pciDevice dev = {.id = (char *)Id, .vendor = Vendor, \
@@ -849,20 +993,21 @@ init_env(void)
         pci_device_new_from_stub(&dev); \
     } while (0)
 
-    MAKE_PCI_DEVICE("0000:00:00.0", 0x8086, 0x0044);
-    MAKE_PCI_DEVICE("0000:00:01.0", 0x8086, 0x0044);
-    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, .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, .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);
+    MAKE_PCI_DEVICE("0000:00:00.0", 0x8086, 0x0044, .iommuGroup = 0);
+    MAKE_PCI_DEVICE("0000:00:01.0", 0x8086, 0x0044, .iommuGroup = 1);
+    MAKE_PCI_DEVICE("0000:00:02.0", 0x8086, 0x0046, .iommuGroup = 2);
+    MAKE_PCI_DEVICE("0000:00:03.0", 0x8086, 0x0048, .iommuGroup = 3);
+    MAKE_PCI_DEVICE("0001:00:00.0", 0x1014, 0x03b9, .class = 0x060400, .iommuGroup = 4);
+    MAKE_PCI_DEVICE("0001:01:00.0", 0x8086, 0x105e, .iommuGroup = 5);
+    MAKE_PCI_DEVICE("0001:01:00.1", 0x8086, 0x105e, .iommuGroup = 5);
+    MAKE_PCI_DEVICE("0005:80:00.0", 0x10b5, 0x8112, .class = 0x060400, .iommuGroup = 6);
+    MAKE_PCI_DEVICE("0005:90:01.0", 0x1033, 0x0035, .iommuGroup = 7);
+    MAKE_PCI_DEVICE("0005:90:01.1", 0x1033, 0x0035, .iommuGroup = 7);
+    MAKE_PCI_DEVICE("0005:90:01.2", 0x1033, 0x00e0, .iommuGroup = 7);
+    MAKE_PCI_DEVICE("0005:90:01.3", 0x1033, 0x00e0, .iommuGroup = 7);
+    MAKE_PCI_DEVICE("0000:0a:01.0", 0x8086, 0x0047, .iommuGroup = 8);
+    MAKE_PCI_DEVICE("0000:0a:02.0", 0x8286, 0x0048, .iommuGroup = 8);
+    MAKE_PCI_DEVICE("0000:0a:03.0", 0x8386, 0x0048, .iommuGroup = 8);
 }
 
 
@@ -1029,7 +1174,8 @@ opendir(const char *path)
 
     init_syms();
 
-    if (STRPREFIX(path, SYSFS_PCI_PREFIX) &&
+    if ((STRPREFIX(path, SYSFS_PCI_PREFIX) ||
+         STRPREFIX(path, SYSFS_KERNEL_PREFIX)) &&
         getrealpath(&newpath, path) < 0)
         return NULL;
 
diff --git a/tests/virpcitestdata/0005-90-01.1.config b/tests/virpcitestdata/0005-90-01.1.config
index beee76534041a7020c08ae9ac03d9a349c6ea12e..a60599bd342d3ebcdc7b8367ca36ad337f602fde 100644
GIT binary patch
delta 44
ycmZo*YG4vE7BFRCV-R3+7GUOK;Amg~f~JXq5)*X<85t+qEn;Cc-jFjfPzC^<7YL>R

delta 39
ucmZo*YG4vE7BFRCV-R3+7GUOK;9y{25MXGU7$`AON05<eqTQm22?_viD+cla

diff --git a/tests/virpcitestdata/0005-90-01.2.config b/tests/virpcitestdata/0005-90-01.2.config
index cfd72e4d7165bff2751ecbdc570ce7f1e0646226..a60599bd342d3ebcdc7b8367ca36ad337f602fde 100644
GIT binary patch
literal 256
zcmXpOFlAt45MXi^VCG@qXkY+>CJ=!Q7z5RUfCHEW5{!&mj0{Y5Fz#TaS&cX3;ByxM
DCDjDB

literal 256
zcmXpOc)-BMAi%_;z|6zo!oa|wz|aIFu>xbDS`csmlR$!5K#7rosSd`)Mk^@TV-u#E
T7_0Gy9FS#<5E~CbC<F-rZiWW}

diff --git a/tests/virpcitestdata/0005-90-01.3.config b/tests/virpcitestdata/0005-90-01.3.config
new file mode 100644
index 0000000000000000000000000000000000000000..a60599bd342d3ebcdc7b8367ca36ad337f602fde
GIT binary patch
literal 256
zcmXpOFlAt45MXi^VCG@qXkY+>CJ=!Q7z5RUfCHEW5{!&mj0{Y5Fz#TaS&cX3;ByxM
DCDjDB

literal 0
HcmV?d00001


--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux