[libvirt] [RFC PATCH 02/28] util: move hostdev passthrough support functions to utility

Shivaprasad G Bhat sbhat at linux.vnet.ibm.com
Wed Mar 14 17:16:09 UTC 2018


There is some duplicity of code here. Move them to common place.

Signed-off-by: Shivaprasad G Bhat <sbhat at linux.vnet.ibm.com>
---
 src/libvirt_private.syms     |    2 +
 src/qemu/qemu_capabilities.c |    5 +--
 src/qemu/qemu_driver.c       |    5 +--
 src/qemu/qemu_hostdev.c      |   70 ++----------------------------------------
 src/qemu/qemu_hostdev.h      |    3 --
 src/util/virhostdev.c        |   63 ++++++++++++++++++++++++++++++++++++++
 src/util/virhostdev.h        |    3 ++
 tests/virhostdevtest.c       |   31 -------------------
 8 files changed, 75 insertions(+), 107 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index c67bce7389..e1bdaa127e 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1915,6 +1915,8 @@ virHostCPUStatsAssign;
 
 # util/virhostdev.h
 virHostdevFindUSBDevice;
+virHostdevHostSupportsPassthroughKVM;
+virHostdevHostSupportsPassthroughVFIO;
 virHostdevIsSCSIDevice;
 virHostdevManagerGetDefault;
 virHostdevPCINodeDeviceDetach;
diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 3eb5ed6d1a..943e92a3d3 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -42,7 +42,6 @@
 #include "virhostcpu.h"
 #include "qemu_monitor.h"
 #include "virstring.h"
-#include "qemu_hostdev.h"
 #include "qemu_domain.h"
 #define __QEMU_CAPSPRIV_H_ALLOW__
 #include "qemu_capspriv.h"
@@ -5725,8 +5724,8 @@ static int
 virQEMUCapsFillDomainDeviceHostdevCaps(virQEMUCapsPtr qemuCaps,
                                        virDomainCapsDeviceHostdevPtr hostdev)
 {
-    bool supportsPassthroughKVM = qemuHostdevHostSupportsPassthroughLegacy();
-    bool supportsPassthroughVFIO = qemuHostdevHostSupportsPassthroughVFIO();
+    bool supportsPassthroughKVM = virHostdevHostSupportsPassthroughKVM();
+    bool supportsPassthroughVFIO = virHostdevHostSupportsPassthroughVFIO();
 
     hostdev->supported = true;
     /* VIR_DOMAIN_HOSTDEV_MODE_CAPABILITIES is for containers only */
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 8c872c1f08..0ade86d6a9 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -52,7 +52,6 @@
 #include "qemu_command.h"
 #include "qemu_parse_command.h"
 #include "qemu_cgroup.h"
-#include "qemu_hostdev.h"
 #include "qemu_hotplug.h"
 #include "qemu_monitor.h"
 #include "qemu_process.h"
@@ -12963,8 +12962,8 @@ qemuNodeDeviceDetachFlags(virNodeDevicePtr dev,
     int ret = -1;
     virNodeDeviceDefPtr def = NULL;
     char *xml = NULL;
-    bool legacy = qemuHostdevHostSupportsPassthroughLegacy();
-    bool vfio = qemuHostdevHostSupportsPassthroughVFIO();
+    bool legacy = virHostdevHostSupportsPassthroughKVM();
+    bool vfio = virHostdevHostSupportsPassthroughVFIO();
     virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;
 
     virCheckFlags(0, -1);
diff --git a/src/qemu/qemu_hostdev.c b/src/qemu/qemu_hostdev.c
index 73d26f4c63..c110cf7816 100644
--- a/src/qemu/qemu_hostdev.c
+++ b/src/qemu/qemu_hostdev.c
@@ -121,78 +121,14 @@ qemuHostdevUpdateActiveDomainDevices(virQEMUDriverPtr driver,
     return 0;
 }
 
-bool
-qemuHostdevHostSupportsPassthroughVFIO(void)
-{
-    DIR *iommuDir = NULL;
-    struct dirent *iommuGroup = NULL;
-    bool ret = false;
-    int direrr;
-
-    /* condition 1 - /sys/kernel/iommu_groups/ contains entries */
-    if (virDirOpenQuiet(&iommuDir, "/sys/kernel/iommu_groups/") < 0)
-        goto cleanup;
-
-    while ((direrr = virDirRead(iommuDir, &iommuGroup, NULL)) > 0) {
-        /* assume we found a group */
-        break;
-    }
-
-    if (direrr < 0 || !iommuGroup)
-        goto cleanup;
-    /* okay, iommu is on and recognizes groups */
-
-    /* condition 2 - /dev/vfio/vfio exists */
-    if (!virFileExists("/dev/vfio/vfio"))
-        goto cleanup;
-
-    ret = true;
-
- cleanup:
-    VIR_DIR_CLOSE(iommuDir);
-    return ret;
-}
-
-
-#if HAVE_LINUX_KVM_H
-# include <linux/kvm.h>
-bool
-qemuHostdevHostSupportsPassthroughLegacy(void)
-{
-    int kvmfd = -1;
-    bool ret = false;
-
-    if ((kvmfd = open("/dev/kvm", O_RDONLY)) < 0)
-        goto cleanup;
-
-# ifdef KVM_CAP_IOMMU
-    if ((ioctl(kvmfd, KVM_CHECK_EXTENSION, KVM_CAP_IOMMU)) <= 0)
-        goto cleanup;
-
-    ret = true;
-# endif
-
- cleanup:
-    VIR_FORCE_CLOSE(kvmfd);
-
-    return ret;
-}
-#else
-bool
-qemuHostdevHostSupportsPassthroughLegacy(void)
-{
-    return false;
-}
-#endif
-
 
 static bool
 qemuHostdevPreparePCIDevicesCheckSupport(virDomainHostdevDefPtr *hostdevs,
                                          size_t nhostdevs,
                                          virQEMUCapsPtr qemuCaps)
 {
-    bool supportsPassthroughKVM = qemuHostdevHostSupportsPassthroughLegacy();
-    bool supportsPassthroughVFIO = qemuHostdevHostSupportsPassthroughVFIO();
+    bool supportsPassthroughKVM = virHostdevHostSupportsPassthroughKVM();
+    bool supportsPassthroughVFIO = virHostdevHostSupportsPassthroughVFIO();
     size_t i;
 
     /* assign defaults for hostdev passthrough */
@@ -330,7 +266,7 @@ qemuHostdevPrepareMediatedDevices(virQEMUDriverPtr driver,
                                   int nhostdevs)
 {
     virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;
-    bool supportsVFIO = qemuHostdevHostSupportsPassthroughVFIO();
+    bool supportsVFIO = virHostdevHostSupportsPassthroughVFIO();
     size_t i;
 
     for (i = 0; i < nhostdevs; i++) {
diff --git a/src/qemu/qemu_hostdev.h b/src/qemu/qemu_hostdev.h
index 9a7c7f143c..bdc51f9d9e 100644
--- a/src/qemu/qemu_hostdev.h
+++ b/src/qemu/qemu_hostdev.h
@@ -27,9 +27,6 @@
 # include "qemu_conf.h"
 # include "domain_conf.h"
 
-bool qemuHostdevHostSupportsPassthroughLegacy(void);
-bool qemuHostdevHostSupportsPassthroughVFIO(void);
-
 int qemuHostdevUpdateActiveMediatedDevices(virQEMUDriverPtr driver,
                                            virDomainDefPtr def);
 int qemuHostdevUpdateActivePCIDevices(virQEMUDriverPtr driver,
diff --git a/src/util/virhostdev.c b/src/util/virhostdev.c
index a12224c58f..e0133cdeec 100644
--- a/src/util/virhostdev.c
+++ b/src/util/virhostdev.c
@@ -2299,3 +2299,66 @@ virHostdevUpdateActiveDomainDevices(virHostdevManagerPtr mgr,
 
     return 0;
 }
+
+bool
+virHostdevHostSupportsPassthroughVFIO(void)
+{
+    DIR *iommuDir = NULL;
+    struct dirent *iommuGroup = NULL;
+    bool ret = false;
+    int direrr;
+
+    /* condition 1 - /sys/kernel/iommu_groups/ contains entries */
+    if (virDirOpenQuiet(&iommuDir, "/sys/kernel/iommu_groups/") < 0)
+        goto cleanup;
+
+    while ((direrr = virDirRead(iommuDir, &iommuGroup, NULL)) > 0) {
+        /* assume we found a group */
+        break;
+    }
+
+    if (direrr < 0 || !iommuGroup)
+        goto cleanup;
+    /* okay, iommu is on and recognizes groups */
+
+    /* condition 2 - /dev/vfio/vfio exists */
+    if (!virFileExists("/dev/vfio/vfio"))
+        goto cleanup;
+
+    ret = true;
+
+ cleanup:
+    VIR_DIR_CLOSE(iommuDir);
+    return ret;
+}
+
+#if HAVE_LINUX_KVM_H
+# include <linux/kvm.h>
+bool
+virHostdevHostSupportsPassthroughKVM(void)
+{
+    int kvmfd = -1;
+    bool ret = false;
+
+    if ((kvmfd = open("/dev/kvm", O_RDONLY)) < 0)
+        goto cleanup;
+
+# ifdef KVM_CAP_IOMMU
+    if ((ioctl(kvmfd, KVM_CHECK_EXTENSION, KVM_CAP_IOMMU)) <= 0)
+        goto cleanup;
+
+    ret = true;
+# endif
+
+ cleanup:
+    VIR_FORCE_CLOSE(kvmfd);
+
+    return ret;
+}
+#else
+bool
+virHostdevHostSupportsPassthroughKVM(void)
+{
+    return false;
+}
+#endif
diff --git a/src/util/virhostdev.h b/src/util/virhostdev.h
index 54e1c66be3..d5efffbac2 100644
--- a/src/util/virhostdev.h
+++ b/src/util/virhostdev.h
@@ -203,4 +203,7 @@ int virHostdevPCINodeDeviceReset(virHostdevManagerPtr mgr,
                                  virPCIDevicePtr pci)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
 
+bool virHostdevHostSupportsPassthroughKVM(void);
+bool virHostdevHostSupportsPassthroughVFIO(void);
+
 #endif /* __VIR_HOSTDEV_H__ */
diff --git a/tests/virhostdevtest.c b/tests/virhostdevtest.c
index 5b03cb6aee..e4fee567a2 100644
--- a/tests/virhostdevtest.c
+++ b/tests/virhostdevtest.c
@@ -130,37 +130,6 @@ myInit(void)
     return -1;
 }
 
-# if HAVE_LINUX_KVM_H
-#  include <linux/kvm.h>
-static bool
-virHostdevHostSupportsPassthroughKVM(void)
-{
-    int kvmfd = -1;
-    bool ret = false;
-
-    if ((kvmfd = open("/dev/kvm", O_RDONLY)) < 0)
-        goto cleanup;
-
-#  ifdef KVM_CAP_IOMMU
-    if ((ioctl(kvmfd, KVM_CHECK_EXTENSION, KVM_CAP_IOMMU)) <= 0)
-        goto cleanup;
-
-    ret = true;
-#  endif
-
- cleanup:
-    VIR_FORCE_CLOSE(kvmfd);
-
-    return ret;
-}
-# else
-static bool
-virHostdevHostSupportsPassthroughKVM(void)
-{
-    return false;
-}
-# endif
-
 static int
 testVirHostdevPreparePCIHostdevs_unmanaged(void)
 {




More information about the libvir-list mailing list