[PATCHv3 09/10] Assign addresses on USB device hotplug

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

 



USB disks, redirected devices, host devices and serial devices
are supported.
---
 src/conf/domain_addr.c                             | 29 ++++++++++++++++++++++
 src/conf/domain_addr.h                             |  4 +++
 src/libvirt_private.syms                           |  1 +
 src/qemu/qemu_domain_address.c                     |  5 ++++
 src/qemu/qemu_hotplug.c                            | 27 ++++++++++++++++++++
 .../qemuhotplug-hotplug-base-live+disk-usb.xml     |  1 +
 6 files changed, 67 insertions(+)

diff --git a/src/conf/domain_addr.c b/src/conf/domain_addr.c
index 74bbbb3..a754474 100644
--- a/src/conf/domain_addr.c
+++ b/src/conf/domain_addr.c
@@ -1693,3 +1693,32 @@ virDomainUSBAddressEnsure(virDomainUSBAddressSetPtr addrs,
 
     return 0;
 }
+
+
+int
+virDomainUSBAddressRelease(virDomainUSBAddressSetPtr addrs,
+                           virDomainDeviceInfoPtr info)
+{
+    virDomainUSBAddressHubPtr targetHub = NULL;
+    char *portStr = NULL;
+    int targetPort;
+    int ret = -1;
+
+    if (info->type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_USB)
+        return 0;
+
+    portStr = virDomainUSBAddressPortFormat(info->addr.usb.port);
+    VIR_DEBUG("Releasing USB addr bus=%u port=%s", info->addr.usb.bus, portStr);
+
+    if (!(targetHub = virDomainUSBAddressFindPort(addrs, info, &targetPort,
+                                                  portStr)))
+        goto cleanup;
+
+    ignore_value(virBitmapClearBit(targetHub->ports, targetPort));
+
+    ret = 0;
+
+ cleanup:
+    VIR_FREE(portStr);
+    return ret;
+}
diff --git a/src/conf/domain_addr.h b/src/conf/domain_addr.h
index cd8501f..4d9cbe1 100644
--- a/src/conf/domain_addr.h
+++ b/src/conf/domain_addr.h
@@ -289,4 +289,8 @@ int
 virDomainUSBAddressEnsure(virDomainUSBAddressSetPtr addrs,
                           virDomainDeviceInfoPtr info)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+int
+virDomainUSBAddressRelease(virDomainUSBAddressSetPtr addrs,
+                           virDomainDeviceInfoPtr info)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
 #endif /* __DOMAIN_ADDR_H__ */
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index f8776d6..351cc4e 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -113,6 +113,7 @@ virDomainUSBAddressAssign;
 virDomainUSBAddressEnsure;
 virDomainUSBAddressPortFormat;
 virDomainUSBAddressPortFormatBuf;
+virDomainUSBAddressRelease;
 virDomainUSBAddressReserve;
 virDomainUSBAddressSetAddControllers;
 virDomainUSBAddressSetAddHub;
diff --git a/src/qemu/qemu_domain_address.c b/src/qemu/qemu_domain_address.c
index 2406edf..ced7853 100644
--- a/src/qemu/qemu_domain_address.c
+++ b/src/qemu/qemu_domain_address.c
@@ -1774,4 +1774,9 @@ qemuDomainReleaseDeviceAddress(virDomainObjPtr vm,
         virDomainVirtioSerialAddrRelease(priv->vioserialaddrs, info) < 0)
         VIR_WARN("Unable to release virtio-serial address on %s",
                  NULLSTR(devstr));
+    if (info->type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_USB &&
+        priv->usbaddrs &&
+        virDomainUSBAddressRelease(priv->usbaddrs, info) < 0)
+        VIR_WARN("Unable to release usb address on %s",
+                 NULLSTR(devstr));
 }
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index f695903..10b9e30 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -627,6 +627,13 @@ qemuDomainAttachUSBMassStorageDevice(virQEMUDriverPtr driver,
     char *devstr = NULL;
     virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
     const char *src = virDomainDiskGetSource(disk);
+    bool releaseaddr;
+
+    if (priv->usbaddrs) {
+        if (virDomainUSBAddressEnsure(priv->usbaddrs, &disk->info) < 0)
+            goto cleanup;
+        releaseaddr = true;
+    }
 
     if (qemuDomainPrepareDisk(driver, vm, disk, NULL, false) < 0)
         goto cleanup;
@@ -672,6 +679,8 @@ qemuDomainAttachUSBMassStorageDevice(virQEMUDriverPtr driver,
     virDomainDiskInsertPreAlloced(vm->def, disk);
 
  cleanup:
+    if (ret < 0 && releaseaddr)
+        virDomainUSBAddressRelease(priv->usbaddrs, &disk->info);
     VIR_FREE(devstr);
     VIR_FREE(drivestr);
     virObjectUnref(cfg);
@@ -1473,6 +1482,12 @@ qemuDomainAttachChrDeviceAssignAddr(qemuDomainObjPrivatePtr priv,
             return -1;
         return 1;
 
+    } else if (chr->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL &&
+               chr->targetType == VIR_DOMAIN_CHR_SERIAL_TARGET_TYPE_USB) {
+        if (virDomainUSBAddressEnsure(priv->usbaddrs, &chr->info) < 0)
+            return -1;
+        return 1;
+
     } else if (chr->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CHANNEL &&
                chr->targetType == VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_VIRTIO) {
         if (virDomainVirtioSerialAddrAutoAssign(NULL, priv->vioserialaddrs,
@@ -1791,11 +1806,18 @@ qemuDomainAttachHostUSBDevice(virQEMUDriverPtr driver,
 {
     qemuDomainObjPrivatePtr priv = vm->privateData;
     char *devstr = NULL;
+    bool releaseaddr = false;
     bool added = false;
     bool teardowncgroup = false;
     bool teardownlabel = false;
     int ret = -1;
 
+    if (priv->usbaddrs) {
+        if (virDomainUSBAddressEnsure(priv->usbaddrs, hostdev->info) < 0)
+            goto cleanup;
+        releaseaddr = true;
+    }
+
     if (qemuHostdevPrepareUSBDevices(driver, vm->def->name, &hostdev, 1, 0) < 0)
         goto cleanup;
 
@@ -1841,6 +1863,8 @@ qemuDomainAttachHostUSBDevice(virQEMUDriverPtr driver,
             VIR_WARN("Unable to restore host device labelling on hotplug fail");
         if (added)
             qemuHostdevReAttachUSBDevices(driver, vm->def->name, &hostdev, 1);
+        if (releaseaddr)
+            virDomainUSBAddressRelease(priv->usbaddrs, hostdev->info);
     }
     VIR_FREE(devstr);
     return ret;
@@ -2834,6 +2858,8 @@ qemuDomainRemoveDiskDevice(virQEMUDriverPtr driver,
     dev.type = VIR_DOMAIN_DEVICE_DISK;
     dev.data.disk = disk;
     ignore_value(qemuRemoveSharedDevice(driver, &dev, vm->def->name));
+    if (priv->usbaddrs)
+        virDomainUSBAddressRelease(priv->usbaddrs, &disk->info);
 
     virDomainDiskDefFree(disk);
     return 0;
@@ -2930,6 +2956,7 @@ qemuDomainRemoveUSBHostDevice(virQEMUDriverPtr driver,
                               virDomainHostdevDefPtr hostdev)
 {
     qemuHostdevReAttachUSBDevices(driver, vm->def->name, &hostdev, 1);
+    qemuDomainReleaseDeviceAddress(vm, hostdev->info, NULL);
 }
 
 static void
diff --git a/tests/qemuhotplugtestdata/qemuhotplug-hotplug-base-live+disk-usb.xml b/tests/qemuhotplugtestdata/qemuhotplug-hotplug-base-live+disk-usb.xml
index 41039a4..cd686e6 100644
--- a/tests/qemuhotplugtestdata/qemuhotplug-hotplug-base-live+disk-usb.xml
+++ b/tests/qemuhotplugtestdata/qemuhotplug-hotplug-base-live+disk-usb.xml
@@ -27,6 +27,7 @@
       <readonly/>
       <shareable/>
       <alias name='usb-disk16'/>
+      <address type='usb' bus='0' port='1'/>
     </disk>
     <controller type='usb' index='0'>
       <alias name='usb'/>
-- 
2.7.3

--
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]