[libvirt] [PATCH 1/2] bhyve: helper function to probe hypervisor caps

Roman Bogorodskiy bogorodskiy at gmail.com
Mon Mar 20 16:31:58 UTC 2017


There are a number of functions in bhyve_capabilities.c that probe
hypervisor capabilities by executing the bhyve(1) binary with the
specific device arugment, checking error message (if any) and setting
proper capability bit. As those are extremely similar, move this logic
into a helper function and convert existing functions to use that.
---
 src/bhyve/bhyve_capabilities.c | 122 +++++++++++++++++------------------------
 1 file changed, 49 insertions(+), 73 deletions(-)

diff --git a/src/bhyve/bhyve_capabilities.c b/src/bhyve/bhyve_capabilities.c
index 4bf1d84fa..f72fdea41 100644
--- a/src/bhyve/bhyve_capabilities.c
+++ b/src/bhyve/bhyve_capabilities.c
@@ -193,121 +193,97 @@ virBhyveProbeGrubCaps(virBhyveGrubCapsFlags *caps)
 }
 
 static int
-bhyveProbeCapsRTC_UTC(unsigned int *caps, char *binary)
+bhyveProbeCapsDeviceHelper(unsigned int *caps,
+                           char *binary,
+                           const char *bus,
+                           const char *device,
+                           const char *errormsg,
+                           unsigned int flag)
 {
-    char *help;
+    char *error;
     virCommandPtr cmd = NULL;
-    int ret = 0, exit;
+    int ret = -1, exit;
 
     cmd = virCommandNew(binary);
-    virCommandAddArg(cmd, "-h");
-    virCommandSetErrorBuffer(cmd, &help);
-    if (virCommandRun(cmd, &exit) < 0) {
-        ret = -1;
-        goto out;
-    }
+    virCommandAddArgList(cmd, bus, device, NULL);
+    virCommandSetErrorBuffer(cmd, &error);
+    if (virCommandRun(cmd, &exit) < 0)
+        goto cleanup;
 
-    if (strstr(help, "-u:") != NULL)
-        *caps |= BHYVE_CAP_RTC_UTC;
+    if (strstr(error, errormsg) == NULL)
+        *caps |= flag;
 
- out:
-    VIR_FREE(help);
+    ret = 0;
+ cleanup:
+    VIR_FREE(error);
     virCommandFree(cmd);
     return ret;
 }
 
 static int
-bhyveProbeCapsAHCI32Slot(unsigned int *caps, char *binary)
+bhyveProbeCapsRTC_UTC(unsigned int *caps, char *binary)
 {
-    char *error;
+    char *help;
     virCommandPtr cmd = NULL;
     int ret = 0, exit;
 
     cmd = virCommandNew(binary);
-    virCommandAddArgList(cmd, "-s", "0,ahci", NULL);
-    virCommandSetErrorBuffer(cmd, &error);
+    virCommandAddArg(cmd, "-h");
+    virCommandSetErrorBuffer(cmd, &help);
     if (virCommandRun(cmd, &exit) < 0) {
         ret = -1;
         goto out;
     }
 
-    if (strstr(error, "pci slot 0:0: unknown device \"ahci\"") == NULL)
-        *caps |= BHYVE_CAP_AHCI32SLOT;
+    if (strstr(help, "-u:") != NULL)
+        *caps |= BHYVE_CAP_RTC_UTC;
 
  out:
-    VIR_FREE(error);
+    VIR_FREE(help);
     virCommandFree(cmd);
     return ret;
 }
 
 static int
-bhyveProbeCapsNetE1000(unsigned int *caps, char *binary)
+bhyveProbeCapsAHCI32Slot(unsigned int *caps, char *binary)
 {
-    char *error;
-    virCommandPtr cmd = NULL;
-    int ret = -1, exit;
-
-    cmd = virCommandNew(binary);
-    virCommandAddArgList(cmd, "-s", "0,e1000", NULL);
-    virCommandSetErrorBuffer(cmd, &error);
-    if (virCommandRun(cmd, &exit) < 0)
-        goto cleanup;
+    return bhyveProbeCapsDeviceHelper(caps, binary,
+                                      "-s",
+                                      "0,ahci",
+                                      "pci slot 0:0: unknown device \"ahci\"",
+                                      BHYVE_CAP_AHCI32SLOT);
+}
 
-    if (strstr(error, "pci slot 0:0: unknown device \"e1000\"") == NULL)
-        *caps |= BHYVE_CAP_NET_E1000;
 
-    ret = 0;
- cleanup:
-    VIR_FREE(error);
-    virCommandFree(cmd);
-    return ret;
+static int
+bhyveProbeCapsNetE1000(unsigned int *caps, char *binary)
+{
+    return bhyveProbeCapsDeviceHelper(caps, binary,
+                                      "-s",
+                                      "0,e1000",
+                                      "pci slot 0:0: unknown device \"e1000\"",
+                                      BHYVE_CAP_NET_E1000);
 }
 
 static int
 bhyveProbeCapsLPC_Bootrom(unsigned int *caps, char *binary)
 {
-    char *error;
-    virCommandPtr cmd = NULL;
-    int ret = -1, exit;
-
-    cmd = virCommandNew(binary);
-    virCommandAddArgList(cmd, "-l", "bootrom", NULL);
-    virCommandSetErrorBuffer(cmd, &error);
-    if (virCommandRun(cmd, &exit) < 0)
-        goto cleanup;
-
-    if (strstr(error, "bhyve: invalid lpc device configuration 'bootrom'") == NULL)
-        *caps |= BHYVE_CAP_LPC_BOOTROM;
-
-    ret = 0;
- cleanup:
-    VIR_FREE(error);
-    virCommandFree(cmd);
-    return ret;
+    return bhyveProbeCapsDeviceHelper(caps, binary,
+                                      "-l",
+                                      "bootrom",
+                                      "bhyve: invalid lpc device configuration 'bootrom'",
+                                      BHYVE_CAP_LPC_BOOTROM);
 }
 
 
 static int
 bhyveProbeCapsFramebuffer(unsigned int *caps, char *binary)
 {
-    char *error;
-    virCommandPtr cmd = NULL;
-    int ret = -1, exit;
-
-    cmd = virCommandNew(binary);
-    virCommandAddArgList(cmd, "-s", "0,fbuf", NULL);
-    virCommandSetErrorBuffer(cmd, &error);
-    if (virCommandRun(cmd, &exit) < 0)
-        goto cleanup;
-
-    if (strstr(error, "pci slot 0:0: unknown device \"fbuf\"") == NULL)
-        *caps |= BHYVE_CAP_FBUF;
-
-    ret = 0;
- cleanup:
-    VIR_FREE(error);
-    virCommandFree(cmd);
-    return ret;
+    return bhyveProbeCapsDeviceHelper(caps, binary,
+                                      "-s",
+                                      "0,fbuf",
+                                      "pci slot 0:0: unknown device \"fbuf\"",
+                                      BHYVE_CAP_FBUF);
 }
 
 int
-- 
2.11.0




More information about the libvir-list mailing list