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

[libvirt] [PATCH 2/7] Split out QEMU code for building PCI/USB hostdev arg values



To allow for better code reuse from hotplug methods, the code for
generating PCI/USB hostdev arg values is split out into separate
methods

* qemu/qemu_conf.h, qemu/qemu_conf.c: Introduce new APis for
  qemuBuildPCIHostdevPCIDevStr, qemuBuildUSBHostdevUsbDevStr
  and qemuBuildUSBHostdevDevStr
---
 src/qemu/qemu_conf.c |  105 +++++++++++++++++++++++++++++++++----------------
 src/qemu/qemu_conf.h |   10 +++++
 2 files changed, 81 insertions(+), 34 deletions(-)

diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c
index e11ec35..85320c1 100644
--- a/src/qemu/qemu_conf.c
+++ b/src/qemu/qemu_conf.c
@@ -2508,6 +2508,65 @@ error:
 }
 
 
+char *
+qemuBuildPCIHostdevPCIDevStr(virDomainHostdevDefPtr dev)
+{
+    char *ret = NULL;
+
+    if (virAsprintf(&ret, "host=%.2x:%.2x.%.1x",
+                    dev->source.subsys.u.pci.bus,
+                    dev->source.subsys.u.pci.slot,
+                    dev->source.subsys.u.pci.function) < 0)
+        virReportOOMError(NULL);
+
+    return ret;
+}
+
+
+char *
+qemuBuildUSBHostdevDevStr(virDomainHostdevDefPtr dev)
+{
+    char *ret = NULL;
+
+    if (!dev->source.subsys.u.usb.bus &&
+        !dev->source.subsys.u.usb.device) {
+        qemudReportError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR, "%s",
+                         _("USB host device is missing bus/device information"));
+        return NULL;
+    }
+
+    if (virAsprintf(&ret, "usb-host,hostbus=%.3d,hostaddr=%.3d,id=%s",
+                    dev->source.subsys.u.usb.bus,
+                    dev->source.subsys.u.usb.device,
+                    dev->info.alias) < 0)
+        virReportOOMError(NULL);
+
+    return ret;
+}
+
+
+char *
+qemuBuildUSBHostdevUsbDevStr(virDomainHostdevDefPtr dev)
+{
+    char *ret = NULL;
+
+    if (!dev->source.subsys.u.usb.bus &&
+        !dev->source.subsys.u.usb.device) {
+        qemudReportError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR, "%s",
+                         _("USB host device is missing bus/device information"));
+        return NULL;
+    }
+
+    if (virAsprintf(&ret, "host:%.3d.%.3d",
+                    dev->source.subsys.u.usb.bus,
+                    dev->source.subsys.u.usb.device) < 0)
+        virReportOOMError(NULL);
+
+    return ret;
+}
+
+
+
 /* This function outputs a -chardev command line option which describes only the
  * host side of the character device */
 char *
@@ -3747,10 +3806,8 @@ int qemudBuildCommandLine(virConnectPtr conn,
 
     /* Add host passthrough hardware */
     for (i = 0 ; i < def->nhostdevs ; i++) {
-        int ret;
-        char* usbdev;
-        char* pcidev;
         virDomainHostdevDefPtr hostdev = def->hostdevs[i];
+        char *devstr;
 
         /* USB */
         if (hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
@@ -3758,33 +3815,15 @@ int qemudBuildCommandLine(virConnectPtr conn,
 
             if (qemuCmdFlags & QEMUD_CMD_FLAG_DEVICE) {
                 ADD_ARG_LIT("-device");
-                if (hostdev->source.subsys.u.usb.vendor) {
-                    ret = virAsprintf(&usbdev, "usb-host,vendor=%.4x,product=%.4x,id=%s",
-                                      hostdev->source.subsys.u.usb.vendor,
-                                      hostdev->source.subsys.u.usb.product,
-                                      hostdev->info.alias);
-                } else {
-                    ret = virAsprintf(&usbdev, "usb-host,hostbus=%.3d,hostaddr=%.3d,id=%s",
-                                      hostdev->source.subsys.u.usb.bus,
-                                      hostdev->source.subsys.u.usb.device,
-                                      hostdev->info.alias);
-                }
+                if (!(devstr = qemuBuildUSBHostdevDevStr(hostdev)))
+                    goto error;
+                ADD_ARG(devstr);
             } else {
                 ADD_ARG_LIT("-usbdevice");
-                if (hostdev->source.subsys.u.usb.vendor) {
-                    ret = virAsprintf(&usbdev, "host:%.4x:%.4x",
-                                      hostdev->source.subsys.u.usb.vendor,
-                                      hostdev->source.subsys.u.usb.product);
-                } else {
-                    ret = virAsprintf(&usbdev, "host:%.3d.%.3d",
-                                      hostdev->source.subsys.u.usb.bus,
-                                      hostdev->source.subsys.u.usb.device);
-                }
+                if (!(devstr = qemuBuildUSBHostdevUsbDevStr(hostdev)))
+                    goto error;
+                ADD_ARG(devstr);
             }
-            if (ret < 0)
-                goto error;
-
-            ADD_ARG(usbdev);
         }
 
         /* PCI */
@@ -3792,21 +3831,19 @@ int qemudBuildCommandLine(virConnectPtr conn,
             hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI) {
             if (qemuCmdFlags & QEMUD_CMD_FLAG_DEVICE) {
                 ADD_ARG_LIT("-device");
-                if (!(pcidev = qemuBuildPCIHostdevDevStr(hostdev)))
+                if (!(devstr = qemuBuildPCIHostdevDevStr(hostdev)))
                     goto error;
+                ADD_ARG(devstr);
             } else if (qemuCmdFlags & QEMUD_CMD_FLAG_PCIDEVICE) {
                 ADD_ARG_LIT("-pcidevice");
-                if (virAsprintf(&pcidev, "host=%.2x:%.2x.%.1x",
-                                hostdev->source.subsys.u.pci.bus,
-                                hostdev->source.subsys.u.pci.slot,
-                                hostdev->source.subsys.u.pci.function) < 0)
-                    goto no_memory;
+                if (!(devstr = qemuBuildPCIHostdevPCIDevStr(hostdev)))
+                    goto error;
+                ADD_ARG(devstr);
             } else {
                 qemudReportError(conn, NULL, NULL, VIR_ERR_NO_SUPPORT, "%s",
                                  _("PCI device assignment is not supported by this version of qemu"));
                 goto error;
             }
-            ADD_ARG(pcidev);
         }
     }
 
diff --git a/src/qemu/qemu_conf.h b/src/qemu/qemu_conf.h
index b6f128f..c3b196e 100644
--- a/src/qemu/qemu_conf.h
+++ b/src/qemu/qemu_conf.h
@@ -227,11 +227,21 @@ char * qemuBuildUSBInputDevStr(virDomainInputDefPtr dev);
 
 char * qemuBuildSoundDevStr(virDomainSoundDefPtr sound);
 
+/* Legacy, pre device support */
+char * qemuBuildPCIHostdevPCIDevStr(virDomainHostdevDefPtr dev);
+/* Current, best practice */
 char * qemuBuildPCIHostdevDevStr(virDomainHostdevDefPtr dev);
 
+/* Current, best practice */
 char * qemuBuildChrChardevStr(virDomainChrDefPtr dev);
+/* Legacy, pre device support */
 char * qemuBuildChrArgStr(virDomainChrDefPtr dev, const char *prefix);
 
+/* Legacy, pre device support */
+char * qemuBuildUSBHostdevUsbDevStr(virDomainHostdevDefPtr dev);
+/* Current, best practice */
+char * qemuBuildUSBHostdevDevStr(virDomainHostdevDefPtr dev);
+
 
 
 int         qemudNetworkIfaceConnect    (virConnectPtr conn,
-- 
1.6.5.2


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