node_device: support binding other drivers with virNodeDeviceDetachFlags()

In the past, the only allowable values for the "driver" field of
virNodeDeviceDetachFlags() were "kvm" or "vfio" for the QEMU driver,
and "xen" for the libxl driver. Then "kvm" was deprecated and removed,
so the driver name became essentially irrelevant (because it is always
called via a particular hypervisor driver, and so the "xen" or "vfio"
can be (and almost always is) implied.

With the advent of VFIO variant drivers, the ability to explicitly
specify a driver name once again becomes useful - it can be used to
name the exact VFIO driver that we want bound to the device in place
of vfio-pci, so this patch allows those other names to be passed down
the call chain, where the code in virpci.c can make use of them.

The names "vfio", "kvm", and "xen" retain their special meaning, though:

  1) because there may be some application or configuration that still
     calls virNodeDeviceDetachFlags() with driverName="vfio", this
     single value is substituted with the synonym of NULL, which means
     "bind the default driver for this device and hypervisor". This
     will currently result in the vfio-pci driver being bound to the
     device.

  2) in the case of the libxl driver, "xen" means to use the standard
     driver used in the case of Xen ("pciback").

  3) "kvm" as a driver name always results in an error, as legacy KVM
     device assignment was removed from the kernel around 10 years ago.

Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Laine Stump 2023-07-09 00:37:45 -04:00
parent 928296b044
commit 24beaffec3
4 changed files with 29 additions and 20 deletions

View File

@ -462,6 +462,7 @@ virDomainDriverNodeDeviceReAttach(virNodeDevicePtr dev,
int int
virDomainDriverNodeDeviceDetachFlags(virNodeDevicePtr dev, virDomainDriverNodeDeviceDetachFlags(virNodeDevicePtr dev,
virHostdevManager *hostdevMgr, virHostdevManager *hostdevMgr,
virPCIStubDriver driverType,
const char *driverName) const char *driverName)
{ {
g_autoptr(virPCIDevice) pci = NULL; g_autoptr(virPCIDevice) pci = NULL;
@ -471,8 +472,10 @@ virDomainDriverNodeDeviceDetachFlags(virNodeDevicePtr dev,
g_autoptr(virConnect) nodeconn = NULL; g_autoptr(virConnect) nodeconn = NULL;
g_autoptr(virNodeDevice) nodedev = NULL; g_autoptr(virNodeDevice) nodedev = NULL;
if (!driverName) if (driverType == VIR_PCI_STUB_DRIVER_NONE) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("driver type not set"));
return -1; return -1;
}
if (!(nodeconn = virGetConnectNodeDev())) if (!(nodeconn = virGetConnectNodeDev()))
return -1; return -1;
@ -504,10 +507,8 @@ virDomainDriverNodeDeviceDetachFlags(virNodeDevicePtr dev,
if (!pci) if (!pci)
return -1; return -1;
if (STREQ(driverName, "vfio")) virPCIDeviceSetStubDriverType(pci, driverType);
virPCIDeviceSetStubDriverType(pci, VIR_PCI_STUB_DRIVER_VFIO); virPCIDeviceSetStubDriverName(pci, driverName);
else if (STREQ(driverName, "xen"))
virPCIDeviceSetStubDriverType(pci, VIR_PCI_STUB_DRIVER_XEN);
return virHostdevPCINodeDeviceDetach(hostdevMgr, pci); return virHostdevPCINodeDeviceDetach(hostdevMgr, pci);
} }

View File

@ -22,6 +22,7 @@
#include "node_device_conf.h" #include "node_device_conf.h"
#include "virhostdev.h" #include "virhostdev.h"
#include "virpci.h"
char * char *
virDomainDriverGenerateRootHash(const char *drivername, virDomainDriverGenerateRootHash(const char *drivername,
@ -58,6 +59,7 @@ int virDomainDriverNodeDeviceReAttach(virNodeDevicePtr dev,
int virDomainDriverNodeDeviceDetachFlags(virNodeDevicePtr dev, int virDomainDriverNodeDeviceDetachFlags(virNodeDevicePtr dev,
virHostdevManager *hostdevMgr, virHostdevManager *hostdevMgr,
virPCIStubDriver driverType,
const char *driverName); const char *driverName);
int virDomainDriverAddIOThreadCheck(virDomainDef *def, int virDomainDriverAddIOThreadCheck(virDomainDef *def,

View File

@ -5876,7 +5876,8 @@ libxlNodeDeviceDetachFlags(virNodeDevicePtr dev,
/* virNodeDeviceDetachFlagsEnsureACL() is being called by /* virNodeDeviceDetachFlagsEnsureACL() is being called by
* virDomainDriverNodeDeviceDetachFlags() */ * virDomainDriverNodeDeviceDetachFlags() */
return virDomainDriverNodeDeviceDetachFlags(dev, hostdev_mgr, driverName); return virDomainDriverNodeDeviceDetachFlags(dev, hostdev_mgr,
VIR_PCI_STUB_DRIVER_XEN, NULL);
} }
static int static int

View File

@ -70,7 +70,6 @@
#include "domain_driver.h" #include "domain_driver.h"
#include "domain_postparse.h" #include "domain_postparse.h"
#include "domain_validate.h" #include "domain_validate.h"
#include "virpci.h"
#include "virpidfile.h" #include "virpidfile.h"
#include "virprocess.h" #include "virprocess.h"
#include "libvirt_internal.h" #include "libvirt_internal.h"
@ -11407,24 +11406,28 @@ qemuNodeDeviceDetachFlags(virNodeDevicePtr dev,
virCheckFlags(0, -1); virCheckFlags(0, -1);
if (!driverName) /* For historical reasons, if driverName is "vfio", that is the
driverName = "vfio"; * same as NULL, i.e. the default vfio driver for this device
*/
if (STREQ_NULLABLE(driverName, "vfio"))
driverName = NULL;
/* Only the 'vfio' driver is supported and a special error message for /* the "kvm" driver name was used a very long time ago to force
* the previously supported 'kvm' driver is provided below. */ * "legacy KVM device assignment", which hasn't been supported in
if (STRNEQ(driverName, "vfio") && STRNEQ(driverName, "kvm")) { * over 10 years.
virReportError(VIR_ERR_INVALID_ARG, */
_("unknown driver name '%1$s'"), driverName); if (STREQ_NULLABLE(driverName, "kvm")) {
return -1;
}
if (STREQ(driverName, "kvm")) {
virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
_("KVM device assignment is no longer " _("'legacy KVM' device assignment is no longer "
"supported on this system")); "supported on this system"));
return -1; return -1;
} }
/* for any other driver, we can't know whether or not it is a VFIO
* driver until the device has been bound to it, so we will defer
* further validation until then.
*/
if (!qemuHostdevHostSupportsPassthroughVFIO()) { if (!qemuHostdevHostSupportsPassthroughVFIO()) {
virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
_("VFIO device assignment is currently not " _("VFIO device assignment is currently not "
@ -11434,7 +11437,9 @@ qemuNodeDeviceDetachFlags(virNodeDevicePtr dev,
/* virNodeDeviceDetachFlagsEnsureACL() is being called by /* virNodeDeviceDetachFlagsEnsureACL() is being called by
* virDomainDriverNodeDeviceDetachFlags() */ * virDomainDriverNodeDeviceDetachFlags() */
return virDomainDriverNodeDeviceDetachFlags(dev, hostdev_mgr, driverName); return virDomainDriverNodeDeviceDetachFlags(dev, hostdev_mgr,
VIR_PCI_STUB_DRIVER_VFIO,
driverName);
} }
static int static int