]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: Move openning VFIO device to virpci
authorPavel Hrdina <phrdina@redhat.com>
Sun, 15 Feb 2026 16:39:05 +0000 (17:39 +0100)
committerPavel Hrdina <phrdina@redhat.com>
Mon, 16 Feb 2026 14:50:39 +0000 (15:50 +0100)
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/libvirt_private.syms
src/qemu/qemu_process.c
src/util/virpci.c
src/util/virpci.h

index 576b1f65959e96c9774752230fa94dca68271941..4d29512e0b388400c29e9f1d35d9ff1cd1ea4490 100644 (file)
@@ -3175,6 +3175,7 @@ virPCIDeviceListNew;
 virPCIDeviceListSteal;
 virPCIDeviceListStealIndex;
 virPCIDeviceNew;
+virPCIDeviceOpenVfioFd;
 virPCIDeviceReattach;
 virPCIDeviceRebind;
 virPCIDeviceReset;
index d21abb9eb6205935b769ce360a77337d47beaad3..cc2a9c8abc645dae15f94eccd8f7a85903dfa78e 100644 (file)
@@ -7711,9 +7711,6 @@ qemuProcessOpenIommuFd(virDomainObj *vm)
 static int
 qemuProcessOpenVfioDeviceFd(virDomainHostdevDef *hostdev)
 {
-    g_autofree char *vfioPath = NULL;
-    int fd = -1;
-
     if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS ||
         hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -7721,25 +7718,7 @@ qemuProcessOpenVfioDeviceFd(virDomainHostdevDef *hostdev)
         return -1;
     }
 
-    if (virPCIDeviceGetVfioPath(&hostdev->source.subsys.u.pci.addr, &vfioPath) < 0)
-        return -1;
-
-    VIR_DEBUG("Opening VFIO device %s", vfioPath);
-
-    if ((fd = open(vfioPath, O_RDWR | O_CLOEXEC)) < 0) {
-        if (errno == ENOENT) {
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                           _("VFIO device %1$s not found - ensure device is bound to vfio-pci driver"),
-                           vfioPath);
-        } else {
-            virReportSystemError(errno,
-                                 _("cannot open VFIO device %1$s"), vfioPath);
-        }
-        return -1;
-    }
-
-    VIR_DEBUG("Opened VFIO device FD %d for %s", fd, vfioPath);
-    return fd;
+    return virPCIDeviceOpenVfioFd(&hostdev->source.subsys.u.pci.addr);
 }
 
 /**
index 2348a9800361f35ffa3800f740a8b93561b6b6b7..30feec6dae6314101fac4ce9c185fda5077876fc 100644 (file)
@@ -3359,3 +3359,31 @@ virPCIDeviceGetVfioPath(virPCIDeviceAddress *addr,
                    addrStr);
     return -1;
 }
+
+/**
+ * virPCIDeviceOpenVfioFd:
+ * @addr:
+ *
+ * Opens VFIO device and returns its FD.
+ *
+ * Returns: FD on success, -1 on failure
+ */
+int
+virPCIDeviceOpenVfioFd(virPCIDeviceAddress *addr)
+{
+    g_autofree char *vfioPath = NULL;
+    int fd = -1;
+
+    if (virPCIDeviceGetVfioPath(addr, &vfioPath) < 0)
+        return -1;
+
+    VIR_DEBUG("Opening VFIO device %s", vfioPath);
+
+    if ((fd = open(vfioPath, O_RDWR | O_CLOEXEC)) < 0) {
+        virReportSystemError(errno, _("cannot open VFIO device %1$s"), vfioPath);
+        return -1;
+    }
+
+    VIR_DEBUG("Opened VFIO device FD %d for %s", fd, vfioPath);
+    return fd;
+}
index 24ede1075532da96fcd9a7392d4460478893de70..78485672856b45b76e5e80c5792f8cfd3476b93f 100644 (file)
@@ -298,6 +298,8 @@ void virPCIDeviceAddressFree(virPCIDeviceAddress *address);
 
 int virPCIDeviceGetVfioPath(virPCIDeviceAddress *addr, char **vfioPath);
 
+int virPCIDeviceOpenVfioFd(virPCIDeviceAddress *addr);
+
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virPCIDevice, virPCIDeviceFree);
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virPCIDeviceAddress, virPCIDeviceAddressFree);
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virPCIEDeviceInfo, virPCIEDeviceInfoFree);