[PATCH v2 02/39] virhostdev: Introduce and use virHostdevIsVFIODevice

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

 



In some places we need to check if a hostdev has VFIO backend.
Because of how complicated virDomainHostdevDef structure is, the
check consists of three lines. Move them to a function and
replace all checks with the function call.

Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
---
 src/conf/domain_conf.c   |  5 +----
 src/libvirt_private.syms |  1 +
 src/qemu/qemu_cgroup.c   |  4 +---
 src/qemu/qemu_domain.c   | 12 +++---------
 src/qemu/qemu_hotplug.c  |  8 +-------
 src/util/virhostdev.c    | 15 +++++++++++++++
 src/util/virhostdev.h    |  3 +++
 7 files changed, 25 insertions(+), 23 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index c290baf953..adf8455579 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -31550,10 +31550,7 @@ virDomainDefHasVFIOHostdev(const virDomainDef *def)
     size_t i;
 
     for (i = 0; i < def->nhostdevs; i++) {
-        const virDomainHostdevDef *tmp = def->hostdevs[i];
-        if (tmp->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
-            tmp->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI &&
-            tmp->source.subsys.u.pci.backend == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO)
+        if (virHostdevIsVFIODevice(def->hostdevs[i]))
             return true;
     }
 
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 287e63bffa..ac37aea626 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2104,6 +2104,7 @@ virHostCPUStatsAssign;
 virHostdevFindUSBDevice;
 virHostdevIsMdevDevice;
 virHostdevIsSCSIDevice;
+virHostdevIsVFIODevice;
 virHostdevManagerGetDefault;
 virHostdevPCINodeDeviceDetach;
 virHostdevPCINodeDeviceReAttach;
diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c
index 740a1b33dc..318157dab0 100644
--- a/src/qemu/qemu_cgroup.c
+++ b/src/qemu/qemu_cgroup.c
@@ -413,9 +413,7 @@ qemuTeardownHostdevCgroup(virDomainObjPtr vm,
     if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES))
         return 0;
 
-    if (dev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
-        dev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI &&
-        dev->source.subsys.u.pci.backend == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO &&
+    if (virHostdevIsVFIODevice(dev) &&
         qemuDomainGetHostdevPath(vm->def, dev, true,
                                  &npaths, &path, NULL) < 0)
         goto cleanup;
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 2aa2164953..824bca89f4 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -11871,9 +11871,7 @@ getPPC64MemLockLimitBytes(virDomainDefPtr def)
     for (i = 0; i < def->nhostdevs; i++) {
         virDomainHostdevDefPtr dev = def->hostdevs[i];
 
-        if (dev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
-            dev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI &&
-            dev->source.subsys.u.pci.backend == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
+        if (virHostdevIsVFIODevice(dev)) {
             usesVFIO = true;
 
             pciAddr = &dev->source.subsys.u.pci.addr;
@@ -12025,12 +12023,8 @@ qemuDomainGetMemLockLimitBytes(virDomainDefPtr def)
      * Note that this may not be valid for all platforms.
      */
     for (i = 0; i < def->nhostdevs; i++) {
-        virDomainHostdevSubsysPtr subsys = &def->hostdevs[i]->source.subsys;
-
-        if (def->hostdevs[i]->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
-            (subsys->type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV ||
-             (subsys->type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI &&
-              subsys->u.pci.backend == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO))) {
+        if (virHostdevIsVFIODevice(def->hostdevs[i]) ||
+            virHostdevIsMdevDevice(def->hostdevs[i])) {
             memKB = virDomainDefGetMemoryTotal(def) + 1024 * 1024;
             goto done;
         }
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index e9285f0964..5f92c61aa9 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -4440,16 +4440,10 @@ qemuDomainRemoveHostDevice(virQEMUDriverPtr driver,
     qemuDomainObjPrivatePtr priv = vm->privateData;
     VIR_AUTOFREE(char *) drivealias = NULL;
     VIR_AUTOFREE(char *) objAlias = NULL;
-    bool is_vfio = false;
 
     VIR_DEBUG("Removing host device %s from domain %p %s",
               hostdev->info->alias, vm, vm->def->name);
 
-    if (hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI) {
-        int backend = hostdev->source.subsys.u.pci.backend;
-        is_vfio = backend == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO;
-    }
-
     if (hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI) {
         virDomainHostdevSubsysSCSIPtr scsisrc = &hostdev->source.subsys.u.scsi;
         virDomainHostdevSubsysSCSIiSCSIPtr iscsisrc = &scsisrc->u.iscsi;
@@ -4497,7 +4491,7 @@ qemuDomainRemoveHostDevice(virQEMUDriverPtr driver,
 
     virDomainAuditHostdev(vm, hostdev, "detach", true);
 
-    if (!is_vfio &&
+    if (!virHostdevIsVFIODevice(hostdev) &&
         qemuSecurityRestoreHostdevLabel(driver, vm, hostdev) < 0)
         VIR_WARN("Failed to restore host device labelling");
 
diff --git a/src/util/virhostdev.c b/src/util/virhostdev.c
index 90967b7c7a..85812423b5 100644
--- a/src/util/virhostdev.c
+++ b/src/util/virhostdev.c
@@ -390,6 +390,21 @@ virHostdevIsMdevDevice(const virDomainHostdevDef *hostdev)
 }
 
 
+/**
+ * virHostdevIsVFIODevice:
+ * @hostdev: host device to check
+ *
+ * Returns true if @hostdev is a PCI device with VFIO backend, false otherwise.
+ */
+bool
+virHostdevIsVFIODevice(const virDomainHostdevDef *hostdev)
+{
+    return hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
+        hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI &&
+        hostdev->source.subsys.u.pci.backend == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO;
+}
+
+
 static int
 virHostdevNetConfigVirtPortProfile(const char *linkdev, int vf,
                                    virNetDevVPortProfilePtr virtPort,
diff --git a/src/util/virhostdev.h b/src/util/virhostdev.h
index 8d7b8c3284..c7ef2055c1 100644
--- a/src/util/virhostdev.h
+++ b/src/util/virhostdev.h
@@ -190,6 +190,9 @@ virHostdevIsSCSIDevice(const virDomainHostdevDef *hostdev)
 bool
 virHostdevIsMdevDevice(const virDomainHostdevDef *hostdev)
     ATTRIBUTE_NONNULL(1);
+bool
+virHostdevIsVFIODevice(const virDomainHostdevDef *hostdev)
+    ATTRIBUTE_NONNULL(1);
 
 /* functions used by NodeDevDetach/Reattach/Reset */
 int virHostdevPCINodeDeviceDetach(virHostdevManagerPtr mgr,
-- 
2.21.0

--
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