[libvirt] [PATCH v3 04/12] qemu: support usb hostdev plugging back

Nikolay Shirokovskiy nshirokovskiy at virtuozzo.com
Tue Sep 17 13:47:39 UTC 2019


We are going to use qemuDomainAttachHostUSBDevice when
host usb device is plugged back to node.

Signed-off-by: Nikolay Shirokovskiy <nshirokovskiy at virtuozzo.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413 at gmail.com>
---
 src/qemu/qemu_hotplug.c | 29 ++++++++++++++++++++++++-----
 1 file changed, 24 insertions(+), 5 deletions(-)

diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index 121e4b2821..780bfde002 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -2524,8 +2524,18 @@ qemuDomainAttachHostUSBDevice(virQEMUDriverPtr driver,
     bool teardownlabel = false;
     bool teardowndevice = false;
     int ret = -1;
+    bool replug = false;
+    size_t i;
+
+    for (i = 0; i < vm->def->nhostdevs; i++) {
+        if (vm->def->hostdevs[i] == hostdev) {
+            replug = true;
+            break;
+        }
+    }
 
-    if (virDomainUSBAddressEnsure(priv->usbaddrs, hostdev->info) < 0)
+    if (!replug &&
+        virDomainUSBAddressEnsure(priv->usbaddrs, hostdev->info) < 0)
         return -1;
 
     if (qemuHostdevPrepareUSBDevices(driver, vm->def->name, &hostdev, 1, 0) < 0)
@@ -2550,7 +2560,7 @@ qemuDomainAttachHostUSBDevice(virQEMUDriverPtr driver,
     if (!(devstr = qemuBuildUSBHostdevDevStr(vm->def, hostdev, priv->qemuCaps)))
         goto cleanup;
 
-    if (VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs+1) < 0)
+    if (!replug && VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs+1) < 0)
         goto cleanup;
 
     qemuDomainObjEnterMonitor(driver, vm);
@@ -2563,7 +2573,8 @@ qemuDomainAttachHostUSBDevice(virQEMUDriverPtr driver,
     if (ret < 0)
         goto cleanup;
 
-    vm->def->hostdevs[vm->def->nhostdevs++] = hostdev;
+    if (!replug)
+        vm->def->hostdevs[vm->def->nhostdevs++] = hostdev;
 
     ret = 0;
  cleanup:
@@ -2576,9 +2587,17 @@ qemuDomainAttachHostUSBDevice(virQEMUDriverPtr driver,
         if (teardowndevice &&
             qemuDomainNamespaceTeardownHostdev(vm, hostdev) < 0)
             VIR_WARN("Unable to remove host device from /dev");
-        if (added)
+        if (added) {
             qemuHostdevReAttachUSBDevices(driver, vm->def->name, &hostdev, 1);
-        virDomainUSBAddressRelease(priv->usbaddrs, hostdev->info);
+
+            if (replug) {
+                virDomainHostdevSubsysUSBPtr usbsrc = &hostdev->source.subsys.u.usb;
+                usbsrc->bus = 0;
+                usbsrc->device = 0;
+            }
+        }
+        if (!replug)
+            virDomainUSBAddressRelease(priv->usbaddrs, hostdev->info);
     }
     VIR_FREE(devstr);
     return ret;
-- 
2.23.0




More information about the libvir-list mailing list