[PATCH 12/16] Refactor virBhyveProcessBuildBhyveCmd a bit

Ryan Moeller posted 16 patches 4 years, 9 months ago
There is a newer version of this series
[PATCH 12/16] Refactor virBhyveProcessBuildBhyveCmd a bit
Posted by Ryan Moeller 4 years, 9 months ago
Reduces the complexity by isolating loop bodies in separate functions.

Signed-off-by: Ryan Moeller <ryan@iXsystems.com>
---
 src/bhyve/bhyve_command.c | 115 ++++++++++++++++++++++----------------
 1 file changed, 67 insertions(+), 48 deletions(-)

diff --git a/src/bhyve/bhyve_command.c b/src/bhyve/bhyve_command.c
index 7e420165aa..bf1cfef3ab 100644
--- a/src/bhyve/bhyve_command.c
+++ b/src/bhyve/bhyve_command.c
@@ -44,9 +44,9 @@
 VIR_LOG_INIT("bhyve.bhyve_command");
 
 static int
-bhyveBuildNetArgStr(bhyveConnPtr driver,
-                    const virDomainDef *def,
+bhyveBuildNetArgStr(const virDomainDef *def,
                     virDomainNetDefPtr net,
+                    bhyveConnPtr driver,
                     virCommandPtr cmd,
                     bool dryRun)
 {
@@ -311,6 +311,61 @@ bhyveBuildVirtIODiskArgStr(const virDomainDef *def G_GNUC_UNUSED,
     return 0;
 }
 
+static int
+bhyveBuildDiskArgStr(const virDomainDef *def,
+                     virDomainDiskDefPtr disk,
+                     virCommandPtr cmd)
+{
+    switch (disk->bus) {
+    case VIR_DOMAIN_DISK_BUS_SATA:
+        /* Handled by bhyveBuildAHCIControllerArgStr() */
+        break;
+    case VIR_DOMAIN_DISK_BUS_VIRTIO:
+        if (bhyveBuildVirtIODiskArgStr(def, disk, cmd) < 0)
+            return -1;
+        break;
+    default:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("unsupported disk device"));
+        return -1;
+    }
+    return 0;
+}
+
+static int
+bhyveBuildControllerArgStr(const virDomainDef *def,
+                           virDomainControllerDefPtr controller,
+                           bhyveConnPtr driver,
+                           virCommandPtr cmd,
+                           unsigned *nusbcontrollers)
+{
+    switch (controller->type) {
+    case VIR_DOMAIN_CONTROLLER_TYPE_PCI:
+        if (controller->model != VIR_DOMAIN_CONTROLLER_MODEL_PCI_ROOT) {
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                           _("unsupported PCI controller model: "
+                             "only PCI root supported"));
+            return -1;
+        }
+        break;
+    case VIR_DOMAIN_CONTROLLER_TYPE_SATA:
+        if (bhyveBuildAHCIControllerArgStr(def, controller, driver, cmd) < 0)
+            return -1;
+        break;
+    case VIR_DOMAIN_CONTROLLER_TYPE_USB:
+        if (++*nusbcontrollers > 1) {
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                           _("only single USB controller is supported"));
+            return -1;
+        }
+
+        if (bhyveBuildUSBControllerArgStr(def, controller, cmd) < 0)
+            return -1;
+        break;
+    }
+    return 0;
+}
+
 static int
 bhyveBuildLPCArgStr(const virDomainDef *def G_GNUC_UNUSED,
                     virCommandPtr cmd)
@@ -432,8 +487,8 @@ bhyveBuildGraphicsArgStr(const virDomainDef *def,
 }
 
 virCommandPtr
-virBhyveProcessBuildBhyveCmd(bhyveConnPtr driver,
-                             virDomainDefPtr def, bool dryRun)
+virBhyveProcessBuildBhyveCmd(bhyveConnPtr driver, virDomainDefPtr def,
+                             bool dryRun)
 {
     /*
      * /usr/sbin/bhyve -c 2 -m 256 -AI -H -P \
@@ -443,11 +498,10 @@ virBhyveProcessBuildBhyveCmd(bhyveConnPtr driver,
      *            -S 31,uart,stdio \
      *            vm0
      */
-    size_t i;
-    int nusbcontrollers = 0;
-    unsigned int nvcpus = virDomainDefGetVcpus(def);
-
     virCommandPtr cmd = virCommandNew(BHYVE);
+    size_t i;
+    unsigned nusbcontrollers = 0;
+    unsigned nvcpus = virDomainDefGetVcpus(def);
 
     /* CPUs */
     virCommandAddArg(cmd, "-c");
@@ -551,52 +605,17 @@ virBhyveProcessBuildBhyveCmd(bhyveConnPtr driver,
 
     /* Devices */
     for (i = 0; i < def->ncontrollers; i++) {
-        virDomainControllerDefPtr controller = def->controllers[i];
-        switch (controller->type) {
-        case VIR_DOMAIN_CONTROLLER_TYPE_PCI:
-            if (controller->model != VIR_DOMAIN_CONTROLLER_MODEL_PCI_ROOT) {
-                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                               "%s", _("unsupported PCI controller model: only PCI root supported"));
-                goto error;
-            }
-            break;
-        case VIR_DOMAIN_CONTROLLER_TYPE_SATA:
-            if (bhyveBuildAHCIControllerArgStr(def, controller, driver, cmd) < 0)
-                goto error;
-            break;
-        case VIR_DOMAIN_CONTROLLER_TYPE_USB:
-            if (++nusbcontrollers > 1) {
-                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                               "%s", _("only single USB controller is supported"));
-                goto error;
-            }
-
-            if (bhyveBuildUSBControllerArgStr(def, controller, cmd) < 0)
-                goto error;
-            break;
-        }
+        if (bhyveBuildControllerArgStr(def, def->controllers[i], driver, cmd,
+                                       &nusbcontrollers) < 0)
+            goto error;
     }
     for (i = 0; i < def->nnets; i++) {
-        virDomainNetDefPtr net = def->nets[i];
-        if (bhyveBuildNetArgStr(driver, def, net, cmd, dryRun) < 0)
+        if (bhyveBuildNetArgStr(def, def->nets[i], driver, cmd, dryRun) < 0)
             goto error;
     }
     for (i = 0; i < def->ndisks; i++) {
-        virDomainDiskDefPtr disk = def->disks[i];
-
-        switch (disk->bus) {
-        case VIR_DOMAIN_DISK_BUS_SATA:
-            /* Handled by bhyveBuildAHCIControllerArgStr() */
-            break;
-        case VIR_DOMAIN_DISK_BUS_VIRTIO:
-            if (bhyveBuildVirtIODiskArgStr(def, disk, cmd) < 0)
-                goto error;
-            break;
-        default:
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                           _("unsupported disk device"));
+        if (bhyveBuildDiskArgStr(def, def->disks[i], cmd) < 0)
             goto error;
-        }
     }
 
     if (def->ngraphics && def->nvideos) {
-- 
2.24.1