[libvirt] [PATCH v3 2/4] qemu: hostdev: Refactor code

Filip Alac posted 4 patches 6 years, 11 months ago
There is a newer version of this series
[libvirt] [PATCH v3 2/4] qemu: hostdev: Refactor code
Posted by Filip Alac 6 years, 11 months ago
Make qemuHostdevHostSupportsPassthroughVFIO use
virHostHasIOMMU.

---
 src/libvirt_private.syms |  2 ++
 src/qemu/qemu_hostdev.c  | 29 ++++-------------------------
 2 files changed, 6 insertions(+), 25 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 6001635916..99a14ab460 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -58,6 +58,7 @@ virCapabilitiesFreeMachines;
 virCapabilitiesFreeNUMAInfo;
 virCapabilitiesGetCpusForNodemask;
 virCapabilitiesGetNodeInfo;
+virCapabilitiesHostInitIOMMU;
 virCapabilitiesHostSecModelAddBaseLabel;
 virCapabilitiesInitCaches;
 virCapabilitiesInitNUMA;
@@ -3073,6 +3074,7 @@ virGetUserName;
 virGetUserRuntimeDirectory;
 virGetUserShell;
 virHexToBin;
+virHostHasIOMMU;
 virIndexToDiskName;
 virIsDevMapperDevice;
 virIsSUID;
diff --git a/src/qemu/qemu_hostdev.c b/src/qemu/qemu_hostdev.c
index 955b5df1a3..25e2dcf868 100644
--- a/src/qemu/qemu_hostdev.c
+++ b/src/qemu/qemu_hostdev.c
@@ -23,7 +23,6 @@
 
 #include <config.h>
 
-#include <dirent.h>
 #include <fcntl.h>
 #include <sys/ioctl.h>
 #include <errno.h>
@@ -124,33 +123,13 @@ qemuHostdevUpdateActiveDomainDevices(virQEMUDriverPtr driver,
 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 */
+    if (!virHostHasIOMMU())
+        return false;
 
-    /* condition 2 - /dev/vfio/vfio exists */
     if (!virFileExists("/dev/vfio/vfio"))
-        goto cleanup;
-
-    ret = true;
+        return false;
 
- cleanup:
-    VIR_DIR_CLOSE(iommuDir);
-    return ret;
+    return true;
 }
 
 
-- 
2.17.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v3 2/4] qemu: hostdev: Refactor code
Posted by Michal Privoznik 6 years, 11 months ago
On 05/31/2018 02:30 PM, Filip Alac wrote:
> Make qemuHostdevHostSupportsPassthroughVFIO use
> virHostHasIOMMU.
> 
> ---
>  src/libvirt_private.syms |  2 ++
>  src/qemu/qemu_hostdev.c  | 29 ++++-------------------------
>  2 files changed, 6 insertions(+), 25 deletions(-)
> 
> diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
> index 6001635916..99a14ab460 100644
> --- a/src/libvirt_private.syms
> +++ b/src/libvirt_private.syms
> @@ -58,6 +58,7 @@ virCapabilitiesFreeMachines;
>  virCapabilitiesFreeNUMAInfo;
>  virCapabilitiesGetCpusForNodemask;
>  virCapabilitiesGetNodeInfo;
> +virCapabilitiesHostInitIOMMU;
>  virCapabilitiesHostSecModelAddBaseLabel;
>  virCapabilitiesInitCaches;
>  virCapabilitiesInitNUMA;

This does not belong here.

> @@ -3073,6 +3074,7 @@ virGetUserName;
>  virGetUserRuntimeDirectory;
>  virGetUserShell;
>  virHexToBin;
> +virHostHasIOMMU;
>  virIndexToDiskName;
>  virIsDevMapperDevice;
>  virIsSUID;

As I say in review to previous patch, this should go into 1/4.

> diff --git a/src/qemu/qemu_hostdev.c b/src/qemu/qemu_hostdev.c
> index 955b5df1a3..25e2dcf868 100644
> --- a/src/qemu/qemu_hostdev.c
> +++ b/src/qemu/qemu_hostdev.c
> @@ -23,7 +23,6 @@
>  
>  #include <config.h>
>  
> -#include <dirent.h>
>  #include <fcntl.h>
>  #include <sys/ioctl.h>
>  #include <errno.h>
> @@ -124,33 +123,13 @@ qemuHostdevUpdateActiveDomainDevices(virQEMUDriverPtr driver,
>  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 */
> +    if (!virHostHasIOMMU())
> +        return false;
>  
> -    /* condition 2 - /dev/vfio/vfio exists */
>      if (!virFileExists("/dev/vfio/vfio"))
> -        goto cleanup;
> -
> -    ret = true;
> +        return false;
>  
> - cleanup:
> -    VIR_DIR_CLOSE(iommuDir);
> -    return ret;
> +    return true;
>  }
>  
>  
> 

This should be merged into 1/4. What seems to be correct patch
ordering/content:

1) move out parts of qemuHostdevHostSupportsPassthroughVFIO() into a
separate function (be it virHostHasIOMMU()), expose it in
libvirt_private.syms and corresponding header file.

2) Introduce caps->iommu (which is basically patch 3/4) witch all
changes you need (libvirt_private.syms change for
virCapabilitiesHostInitIOMMU(), adding bool to struct _virCapsHost, etc.)

3) document the feature in news.xml.

Michal

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