]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
bhyve: helper function to probe hypervisor caps
authorRoman Bogorodskiy <bogorodskiy@gmail.com>
Sun, 19 Mar 2017 13:40:52 +0000 (17:40 +0400)
committerRoman Bogorodskiy <bogorodskiy@gmail.com>
Sun, 26 Mar 2017 15:12:10 +0000 (19:12 +0400)
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

index 6f8be45c482d7a652dcca2aef76b0ab5024ac54b..6769f1888ae269515de4cd67e02c99950aaf050f 100644 (file)
@@ -217,121 +217,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