Re: [PATCH v2 02/11] qemu: support host usb device unplug

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

 





On 9/9/19 8:33 AM, Nikolay Shirokovskiy wrote:
Handle host usb device unplug in DEVICE_DELETED handle execution
path.

Signed-off-by: Nikolay Shirokovskiy <nshirokovskiy@xxxxxxxxxxxxx>
---

Reviewed-by: Daniel Henrique Barboza <danielhb413@xxxxxxxxx>


  src/qemu/qemu_hotplug.c | 38 +++++++++++++++++++++++++++++++-------
  1 file changed, 31 insertions(+), 7 deletions(-)

diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index 559254b234..b045735022 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -4366,7 +4366,8 @@ qemuDomainRemoveUSBHostDevice(virQEMUDriverPtr driver,
                                virDomainHostdevDefPtr hostdev)
  {
      qemuHostdevReAttachUSBDevices(driver, vm->def->name, &hostdev, 1);
-    qemuDomainReleaseDeviceAddress(vm, hostdev->info);
+    if (hostdev->deleteAction != VIR_DOMAIN_HOSTDEV_DELETE_ACTION_UNPLUG)
+        qemuDomainReleaseDeviceAddress(vm, hostdev->info);
  }
static void
@@ -4408,6 +4409,7 @@ qemuDomainRemoveHostDevice(virQEMUDriverPtr driver,
      char *drivealias = NULL;
      char *objAlias = NULL;
      bool is_vfio = false;
+    bool unplug = hostdev->deleteAction == VIR_DOMAIN_HOSTDEV_DELETE_ACTION_UNPLUG;
VIR_DEBUG("Removing host device %s from domain %p %s",
                hostdev->info->alias, vm, vm->def->name);
@@ -4454,16 +4456,24 @@ qemuDomainRemoveHostDevice(virQEMUDriverPtr driver,
          }
      }
- for (i = 0; i < vm->def->nhostdevs; i++) {
-        if (vm->def->hostdevs[i] == hostdev) {
-            virDomainHostdevRemove(vm->def, i);
-            break;
+    if (!unplug) {
+        for (i = 0; i < vm->def->nhostdevs; i++) {
+            if (vm->def->hostdevs[i] == hostdev) {
+                virDomainHostdevRemove(vm->def, i);
+                break;
+            }
          }
      }
virDomainAuditHostdev(vm, hostdev, "detach", true); - if (!is_vfio &&
+    /*
+     * In case of unplug the attempt to restore label will fail. But we don't
+     * need to restore the label! In case of separate mount namespace for the
+     * domain we remove device file later in this function. In case of global
+     * mount namespace the device file is deleted or being deleted by systemd.
+     */
+    if (!is_vfio && !unplug &&
          qemuSecurityRestoreHostdevLabel(driver, vm, hostdev) < 0)
          VIR_WARN("Failed to restore host device labelling");
@@ -4497,7 +4507,13 @@ qemuDomainRemoveHostDevice(virQEMUDriverPtr driver,
          break;
      }
- virDomainHostdevDefFree(hostdev);
+    if (unplug) {
+        virDomainHostdevSubsysUSBPtr usbsrc = &hostdev->source.subsys.u.usb;
+        usbsrc->bus = 0;
+        usbsrc->device = 0;
+    } else {
+        virDomainHostdevDefFree(hostdev);
+    }
if (net) {
          if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK) {
@@ -4512,6 +4528,8 @@ qemuDomainRemoveHostDevice(virQEMUDriverPtr driver,
          virDomainNetDefFree(net);
      }
+ hostdev->deleteAction = VIR_DOMAIN_HOSTDEV_DELETE_ACTION_NONE;
+
      ret = 0;
cleanup:
@@ -4981,6 +4999,7 @@ qemuDomainRemoveDevice(virQEMUDriverPtr driver,
      virDomainDeviceInfoPtr info;
      virObjectEventPtr event;
      VIR_AUTOFREE(char *) alias = NULL;
+    bool unplug;
/*
       * save the alias to use when sending a DEVICE_REMOVED event after
@@ -5021,8 +5040,13 @@ qemuDomainRemoveDevice(virQEMUDriverPtr driver,
              return -1;
          break;
      case VIR_DOMAIN_DEVICE_HOSTDEV:
+        unplug = dev->data.hostdev->deleteAction == VIR_DOMAIN_HOSTDEV_DELETE_ACTION_UNPLUG;
+
          if (qemuDomainRemoveHostDevice(driver, vm, dev->data.hostdev) < 0)
              return -1;
+
+        if (unplug)
+            return 0;
          break;
      case VIR_DOMAIN_DEVICE_RNG:
          if (qemuDomainRemoveRNGDevice(driver, vm, dev->data.rng) < 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