util: Move openning VFIO device to virpci

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Pavel Hrdina committed 2026-02-16 15:50:39 +01:00
1 parent ff2848827f
commit 22c666097c
4 files changed
+32 -22

No files matched your search

+1
View File
@@ -3175,6 +3175,7 @@ virPCIDeviceListNew;
virPCIDeviceListSteal;
virPCIDeviceListStealIndex;
virPCIDeviceNew;
virPCIDeviceOpenVfioFd;
virPCIDeviceReattach;
virPCIDeviceRebind;
virPCIDeviceReset;
+1 -22
View 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);
}
/**
+28
View 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;
}
+2
View 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);