[PATCH 3/4] qemu: Move <hostdev> SCSI path generation into qemuDomainPrepareHostdev()

Michal Privoznik mprivozn at redhat.com
Fri Apr 14 13:44:17 UTC 2023


When preparing a SCSI <hostdev/> with passthrough of a host SCSI
adapter (i.e. no protocol), a virStorageSource structure is
initialized and stored inside virDomainHostdevDef. But the source
structure is filled in many places, with almost the same code.

Firstly, qemuProcessPrepareHostHostdev() and
qemuConnectDomainXMLToNativePrepareHostHostdev() are the same.

Secondly, qemuDomainPrepareHostdev() allocates the src structure,
only to let qemuProcessPrepareHostHostdev() fill src->path later.

Well, src->path can be filled at the same place where the src
structure is allocated (qemuDomainPrepareHostdev()) which renders
the other two functions needless.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 src/qemu/qemu_domain.c   | 10 ++++++++++
 src/qemu/qemu_driver.c   | 32 +-------------------------------
 src/qemu/qemu_process.c  | 32 +-------------------------------
 tests/qemuxml2argvmock.c | 10 ++++++++++
 tests/qemuxml2argvtest.c | 22 ----------------------
 5 files changed, 22 insertions(+), 84 deletions(-)

diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 6de846f158..3925de4d49 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -11230,7 +11230,9 @@ qemuDomainPrepareHostdev(virDomainHostdevDef *hostdev,
 {
     if (virHostdevIsSCSIDevice(hostdev)) {
         virDomainHostdevSubsysSCSI *scsisrc = &hostdev->source.subsys.u.scsi;
+        virDomainHostdevSubsysSCSIHost *scsihostsrc = &scsisrc->u.host;
         virStorageSource *src = NULL;
+        g_autofree char *devstr = NULL;
 
         switch ((virDomainHostdevSCSIProtocolType) scsisrc->protocol) {
         case VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_NONE:
@@ -11238,7 +11240,15 @@ qemuDomainPrepareHostdev(virDomainHostdevDef *hostdev,
             scsisrc->u.host.src = virStorageSourceNew();
             src = scsisrc->u.host.src;
 
+            if (!(devstr = virSCSIDeviceGetSgName(NULL,
+                                                  scsihostsrc->adapter,
+                                                  scsihostsrc->bus,
+                                                  scsihostsrc->target,
+                                                  scsihostsrc->unit)))
+                return -1;
+
             src->type = VIR_STORAGE_TYPE_BLOCK;
+            src->path = g_strdup_printf("/dev/%s", devstr);
 
             break;
 
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 28e470e4a2..701656696b 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -6166,38 +6166,8 @@ static char
 
 
 static int
-qemuConnectDomainXMLToNativePrepareHostHostdev(virDomainHostdevDef *hostdev)
+qemuConnectDomainXMLToNativePrepareHostHostdev(virDomainHostdevDef *hostdev G_GNUC_UNUSED)
 {
-    if (virHostdevIsSCSIDevice(hostdev)) {
-        virDomainHostdevSubsysSCSI *scsisrc = &hostdev->source.subsys.u.scsi;
-
-        switch ((virDomainHostdevSCSIProtocolType) scsisrc->protocol) {
-        case VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_NONE: {
-            virDomainHostdevSubsysSCSIHost *scsihostsrc = &scsisrc->u.host;
-            virStorageSource *src = scsisrc->u.host.src;
-            g_autofree char *devstr = NULL;
-
-            if (!(devstr = virSCSIDeviceGetSgName(NULL,
-                                                  scsihostsrc->adapter,
-                                                  scsihostsrc->bus,
-                                                  scsihostsrc->target,
-                                                  scsihostsrc->unit)))
-                return -1;
-
-            src->path = g_strdup_printf("/dev/%s", devstr);
-            break;
-        }
-
-        case VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_ISCSI:
-            break;
-
-        case VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_LAST:
-        default:
-            virReportEnumRangeError(virDomainHostdevSCSIProtocolType, scsisrc->protocol);
-            return -1;
-        }
-    }
-
     return 0;
 }
 
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index b9e9a7d320..ec07f86d35 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -6510,38 +6510,8 @@ qemuProcessPrepareDomainHostdevs(virDomainObj *vm,
 
 
 int
-qemuProcessPrepareHostHostdev(virDomainHostdevDef *hostdev)
+qemuProcessPrepareHostHostdev(virDomainHostdevDef *hostdev G_GNUC_UNUSED)
 {
-    if (virHostdevIsSCSIDevice(hostdev)) {
-        virDomainHostdevSubsysSCSI *scsisrc = &hostdev->source.subsys.u.scsi;
-
-        switch ((virDomainHostdevSCSIProtocolType) scsisrc->protocol) {
-        case VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_NONE: {
-            virDomainHostdevSubsysSCSIHost *scsihostsrc = &scsisrc->u.host;
-            virStorageSource *src = scsisrc->u.host.src;
-            g_autofree char *devstr = NULL;
-
-            if (!(devstr = virSCSIDeviceGetSgName(NULL,
-                                                  scsihostsrc->adapter,
-                                                  scsihostsrc->bus,
-                                                  scsihostsrc->target,
-                                                  scsihostsrc->unit)))
-                return -1;
-
-            src->path = g_strdup_printf("/dev/%s", devstr);
-            break;
-        }
-
-        case VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_ISCSI:
-            break;
-
-        case VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_LAST:
-        default:
-            virReportEnumRangeError(virDomainHostdevSCSIProtocolType, scsisrc->protocol);
-            return -1;
-        }
-    }
-
     return 0;
 }
 
diff --git a/tests/qemuxml2argvmock.c b/tests/qemuxml2argvmock.c
index 863fdf0351..2b37b6c258 100644
--- a/tests/qemuxml2argvmock.c
+++ b/tests/qemuxml2argvmock.c
@@ -89,6 +89,16 @@ qemuHostdevHostSupportsPassthroughVFIO(void)
     return true;
 }
 
+char *
+virSCSIDeviceGetSgName(const char *sysfs_prefix G_GNUC_UNUSED,
+                       const char *adapter G_GNUC_UNUSED,
+                       unsigned int bus G_GNUC_UNUSED,
+                       unsigned int target G_GNUC_UNUSED,
+                       unsigned long long unit G_GNUC_UNUSED)
+{
+    return g_strdup_printf("sg0");
+}
+
 int
 virNetDevTapCreate(char **ifname,
                    const char *tunpath G_GNUC_UNUSED,
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
index 8333749eea..919e69e306 100644
--- a/tests/qemuxml2argvtest.c
+++ b/tests/qemuxml2argvtest.c
@@ -404,28 +404,6 @@ testCompareXMLToArgvCreateArgs(virQEMUDriver *drv,
             disk->src->hostcdrom = true;
     }
 
-    for (i = 0; i < vm->def->nhostdevs; i++) {
-        virDomainHostdevDef *hostdev = vm->def->hostdevs[i];
-
-        if (virHostdevIsSCSIDevice(hostdev)) {
-            virDomainHostdevSubsysSCSI *scsisrc = &hostdev->source.subsys.u.scsi;
-
-            switch ((virDomainHostdevSCSIProtocolType) scsisrc->protocol) {
-            case VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_NONE:
-                scsisrc->u.host.src->path = g_strdup("/dev/sg0");
-                break;
-
-            case VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_ISCSI:
-                break;
-
-            case VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_LAST:
-            default:
-                virReportEnumRangeError(virDomainHostdevSCSIProtocolType, scsisrc->protocol);
-                return NULL;
-            }
-        }
-    }
-
     if (vm->def->vsock) {
         virDomainVsockDef *vsock = vm->def->vsock;
         qemuDomainVsockPrivate *vsockPriv =
-- 
2.39.2



More information about the libvir-list mailing list