]> xenbits.xensource.com Git - libvirt.git/commitdiff
node_device: support binding other drivers with virNodeDeviceDetachFlags()
authorLaine Stump <laine@redhat.com>
Sun, 9 Jul 2023 04:37:45 +0000 (00:37 -0400)
committerLaine Stump <laine@redhat.com>
Fri, 25 Aug 2023 03:36:18 +0000 (23:36 -0400)
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>
src/hypervisor/domain_driver.c
src/hypervisor/domain_driver.h
src/libxl/libxl_driver.c
src/qemu/qemu_driver.c

index a70f75f3ae82538a16318abb4478b56338e9fcc9..d9469ad6f9688d5c6db3e495156399eab2a7ea4d 100644 (file)
@@ -462,6 +462,7 @@ virDomainDriverNodeDeviceReAttach(virNodeDevicePtr dev,
 int
 virDomainDriverNodeDeviceDetachFlags(virNodeDevicePtr dev,
                                      virHostdevManager *hostdevMgr,
+                                     virPCIStubDriver driverType,
                                      const char *driverName)
 {
     g_autoptr(virPCIDevice) pci = NULL;
@@ -471,8 +472,10 @@ virDomainDriverNodeDeviceDetachFlags(virNodeDevicePtr dev,
     g_autoptr(virConnect) nodeconn = 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;
+    }
 
     if (!(nodeconn = virGetConnectNodeDev()))
         return -1;
@@ -504,10 +507,8 @@ virDomainDriverNodeDeviceDetachFlags(virNodeDevicePtr dev,
     if (!pci)
         return -1;
 
-    if (STREQ(driverName, "vfio"))
-        virPCIDeviceSetStubDriverType(pci, VIR_PCI_STUB_DRIVER_VFIO);
-    else if (STREQ(driverName, "xen"))
-        virPCIDeviceSetStubDriverType(pci, VIR_PCI_STUB_DRIVER_XEN);
+    virPCIDeviceSetStubDriverType(pci, driverType);
+    virPCIDeviceSetStubDriverName(pci, driverName);
 
     return virHostdevPCINodeDeviceDetach(hostdevMgr, pci);
 }
index 4241c86932070c4e6c02eb2333b2aab4d7ecf34a..9942f58fda1fe6ec151be8d2d798a56d20eba557 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "node_device_conf.h"
 #include "virhostdev.h"
+#include "virpci.h"
 
 char *
 virDomainDriverGenerateRootHash(const char *drivername,
@@ -58,6 +59,7 @@ int virDomainDriverNodeDeviceReAttach(virNodeDevicePtr dev,
 
 int virDomainDriverNodeDeviceDetachFlags(virNodeDevicePtr dev,
                                          virHostdevManager *hostdevMgr,
+                                         virPCIStubDriver driverType,
                                          const char *driverName);
 
 int virDomainDriverAddIOThreadCheck(virDomainDef *def,
index 3d10f458508747e214833b5a6c5748d82b18ee94..079922dd32a7c278b32e389568cc7f16542669a7 100644 (file)
@@ -5876,7 +5876,8 @@ libxlNodeDeviceDetachFlags(virNodeDevicePtr dev,
 
     /* virNodeDeviceDetachFlagsEnsureACL() is being called by
      * virDomainDriverNodeDeviceDetachFlags() */
-    return virDomainDriverNodeDeviceDetachFlags(dev, hostdev_mgr, driverName);
+    return virDomainDriverNodeDeviceDetachFlags(dev, hostdev_mgr,
+                                                VIR_PCI_STUB_DRIVER_XEN, NULL);
 }
 
 static int
index 73fa499e40d09d3cdb9b4bb6e06e2e5a850bd6be..5128b6436426175a657e348b5b768500d3de9c0a 100644 (file)
@@ -70,7 +70,6 @@
 #include "domain_driver.h"
 #include "domain_postparse.h"
 #include "domain_validate.h"
-#include "virpci.h"
 #include "virpidfile.h"
 #include "virprocess.h"
 #include "libvirt_internal.h"
@@ -11407,24 +11406,28 @@ qemuNodeDeviceDetachFlags(virNodeDevicePtr dev,
 
     virCheckFlags(0, -1);
 
-    if (!driverName)
-        driverName = "vfio";
-
-    /* Only the 'vfio' driver is supported and a special error message for
-     * the previously supported 'kvm' driver is provided below. */
-    if (STRNEQ(driverName, "vfio") && STRNEQ(driverName, "kvm")) {
-        virReportError(VIR_ERR_INVALID_ARG,
-                       _("unknown driver name '%1$s'"), driverName);
-        return -1;
-    }
+    /* For historical reasons, if driverName is "vfio", that is the
+     * same as NULL, i.e. the default vfio driver for this device
+     */
+    if (STREQ_NULLABLE(driverName, "vfio"))
+        driverName = NULL;
 
-    if (STREQ(driverName, "kvm")) {
+    /* the "kvm" driver name was used a very long time ago to force
+     * "legacy KVM device assignment", which hasn't been supported in
+     * over 10 years.
+     */
+    if (STREQ_NULLABLE(driverName, "kvm")) {
         virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
-                       _("KVM device assignment is no longer "
+                       _("'legacy KVM' device assignment is no longer "
                          "supported on this system"));
         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()) {
         virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                        _("VFIO device assignment is currently not "
@@ -11434,7 +11437,9 @@ qemuNodeDeviceDetachFlags(virNodeDevicePtr dev,
 
     /* virNodeDeviceDetachFlagsEnsureACL() is being called by
      * virDomainDriverNodeDeviceDetachFlags() */
-    return virDomainDriverNodeDeviceDetachFlags(dev, hostdev_mgr, driverName);
+    return virDomainDriverNodeDeviceDetachFlags(dev, hostdev_mgr,
+                                                VIR_PCI_STUB_DRIVER_VFIO,
+                                                driverName);
 }
 
 static int