[PATCH v2 10/14] domain_driver.c: add virDomainDriverParseBlkioDeviceStr()

Daniel Henrique Barboza danielhb413 at gmail.com
Mon Feb 17 21:29:17 UTC 2020


lxcDomainParseBlkioDeviceStr() and qemuDomainParseBlkioDeviceStr()
are the same function. Avoid code repetition by putting the code
in a new helper.

Signed-off-by: Daniel Henrique Barboza <danielhb413 at gmail.com>
---
 po/POTFILES.in                 |   1 +
 src/hypervisor/domain_driver.c | 112 +++++++++++++++++++++++++++++
 src/hypervisor/domain_driver.h |   3 +
 src/libvirt_private.syms       |   1 +
 src/lxc/lxc_driver.c           | 122 +++----------------------------
 src/qemu/qemu_driver.c         | 126 +++------------------------------
 6 files changed, 133 insertions(+), 232 deletions(-)

diff --git a/po/POTFILES.in b/po/POTFILES.in
index dba0d3a12e..aa5c1fb6c7 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -78,6 +78,7 @@
 @SRCDIR@/src/hyperv/hyperv_driver.c
 @SRCDIR@/src/hyperv/hyperv_util.c
 @SRCDIR@/src/hyperv/hyperv_wmi.c
+ at SRCDIR@/src/hypervisor/domain_driver.c
 @SRCDIR@/src/interface/interface_backend_netcf.c
 @SRCDIR@/src/interface/interface_backend_udev.c
 @SRCDIR@/src/internal.h
diff --git a/src/hypervisor/domain_driver.c b/src/hypervisor/domain_driver.c
index c999458c25..bbfadb3d9b 100644
--- a/src/hypervisor/domain_driver.c
+++ b/src/hypervisor/domain_driver.c
@@ -22,6 +22,7 @@
 
 #include "domain_driver.h"
 #include "viralloc.h"
+#include "virstring.h"
 
 #define VIR_FROM_THIS VIR_FROM_DOMAIN
 
@@ -94,3 +95,114 @@ virDomainDriverMergeBlkioDevice(virBlkioDevicePtr *dest_array,
 
     return 0;
 }
+
+
+/* blkioDeviceStr in the form of /device/path,weight,/device/path,weight
+ * for example, /dev/disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0,800
+ */
+int
+virDomainDriverParseBlkioDeviceStr(char *blkioDeviceStr, const char *type,
+                                   virBlkioDevicePtr *dev, size_t *size)
+{
+    char *temp;
+    int ndevices = 0;
+    int nsep = 0;
+    size_t i;
+    virBlkioDevicePtr result = NULL;
+
+    *dev = NULL;
+    *size = 0;
+
+    if (STREQ(blkioDeviceStr, ""))
+        return 0;
+
+    temp = blkioDeviceStr;
+    while (temp) {
+        temp = strchr(temp, ',');
+        if (temp) {
+            temp++;
+            nsep++;
+        }
+    }
+
+    /* A valid string must have even number of fields, hence an odd
+     * number of commas.  */
+    if (!(nsep & 1))
+        goto parse_error;
+
+    ndevices = (nsep + 1) / 2;
+
+    if (VIR_ALLOC_N(result, ndevices) < 0)
+        return -1;
+
+    i = 0;
+    temp = blkioDeviceStr;
+    while (temp) {
+        char *p = temp;
+
+        /* device path */
+        p = strchr(p, ',');
+        if (!p)
+            goto parse_error;
+
+        result[i].path = g_strndup(temp, p - temp);
+
+        /* value */
+        temp = p + 1;
+
+        if (STREQ(type, VIR_DOMAIN_BLKIO_DEVICE_WEIGHT)) {
+            if (virStrToLong_uip(temp, &p, 10, &result[i].weight) < 0)
+                goto number_error;
+        } else if (STREQ(type, VIR_DOMAIN_BLKIO_DEVICE_READ_IOPS)) {
+            if (virStrToLong_uip(temp, &p, 10, &result[i].riops) < 0)
+                goto number_error;
+        } else if (STREQ(type, VIR_DOMAIN_BLKIO_DEVICE_WRITE_IOPS)) {
+            if (virStrToLong_uip(temp, &p, 10, &result[i].wiops) < 0)
+                goto number_error;
+        } else if (STREQ(type, VIR_DOMAIN_BLKIO_DEVICE_READ_BPS)) {
+            if (virStrToLong_ullp(temp, &p, 10, &result[i].rbps) < 0)
+                goto number_error;
+        } else if (STREQ(type, VIR_DOMAIN_BLKIO_DEVICE_WRITE_BPS)) {
+            if (virStrToLong_ullp(temp, &p, 10, &result[i].wbps) < 0)
+                goto number_error;
+        } else {
+            virReportError(VIR_ERR_INVALID_ARG,
+                          _("unknown parameter '%s'"), type);
+            goto cleanup;
+        }
+
+        i++;
+
+        if (*p == '\0')
+            break;
+        else if (*p != ',')
+            goto parse_error;
+        temp = p + 1;
+    }
+
+    if (!i)
+        VIR_FREE(result);
+
+    *dev = result;
+    *size = i;
+
+    return 0;
+
+ parse_error:
+    virReportError(VIR_ERR_INVALID_ARG,
+                   _("unable to parse blkio device '%s' '%s'"),
+                   type, blkioDeviceStr);
+    goto cleanup;
+
+ number_error:
+    virReportError(VIR_ERR_INVALID_ARG,
+                   _("invalid value '%s' for parameter '%s' of device '%s'"),
+                   temp, type, result[i].path);
+
+ cleanup:
+    if (result) {
+        virBlkioDeviceArrayClear(result, ndevices);
+        VIR_FREE(result);
+    }
+    return -1;
+}
diff --git a/src/hypervisor/domain_driver.h b/src/hypervisor/domain_driver.h
index 2fb4148dff..b78401ea42 100644
--- a/src/hypervisor/domain_driver.h
+++ b/src/hypervisor/domain_driver.h
@@ -27,3 +27,6 @@ int virDomainDriverMergeBlkioDevice(virBlkioDevicePtr *dest_array,
                                     virBlkioDevicePtr src_array,
                                     size_t src_size,
                                     const char *type);
+
+int virDomainDriverParseBlkioDeviceStr(char *blkioDeviceStr, const char *type,
+                                       virBlkioDevicePtr *dev, size_t *size);
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index c6b9afe259..88f9ca16d1 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1396,6 +1396,7 @@ virDomainCgroupSetupMemtune;
 
 # hypervisor/domain_cgroup.h
 virDomainDriverMergeBlkioDevice;
+virDomainDriverParseBlkioDeviceStr;
 
 
 # libvirt_internal.h
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
index 08c744a69b..78a0174277 100644
--- a/src/lxc/lxc_driver.c
+++ b/src/lxc/lxc_driver.c
@@ -2108,112 +2108,6 @@ lxcDomainGetSchedulerParameters(virDomainPtr domain,
     return lxcDomainGetSchedulerParametersFlags(domain, params, nparams, 0);
 }
 
-static int
-lxcDomainParseBlkioDeviceStr(char *blkioDeviceStr, const char *type,
-                             virBlkioDevicePtr *dev, size_t *size)
-{
-    char *temp;
-    int ndevices = 0;
-    int nsep = 0;
-    size_t i;
-    virBlkioDevicePtr result = NULL;
-
-    *dev = NULL;
-    *size = 0;
-
-    if (STREQ(blkioDeviceStr, ""))
-        return 0;
-
-    temp = blkioDeviceStr;
-    while (temp) {
-        temp = strchr(temp, ',');
-        if (temp) {
-            temp++;
-            nsep++;
-        }
-    }
-
-    /* A valid string must have even number of fields, hence an odd
-     * number of commas.  */
-    if (!(nsep & 1))
-        goto parse_error;
-
-    ndevices = (nsep + 1) / 2;
-
-    if (VIR_ALLOC_N(result, ndevices) < 0)
-        return -1;
-
-    i = 0;
-    temp = blkioDeviceStr;
-    while (temp) {
-        char *p = temp;
-
-        /* device path */
-        p = strchr(p, ',');
-        if (!p)
-            goto parse_error;
-
-        result[i].path = g_strndup(temp, p - temp);
-
-        /* value */
-        temp = p + 1;
-
-        if (STREQ(type, VIR_DOMAIN_BLKIO_DEVICE_WEIGHT)) {
-            if (virStrToLong_uip(temp, &p, 10, &result[i].weight) < 0)
-                goto number_error;
-        } else if (STREQ(type, VIR_DOMAIN_BLKIO_DEVICE_READ_IOPS)) {
-            if (virStrToLong_uip(temp, &p, 10, &result[i].riops) < 0)
-                goto number_error;
-        } else if (STREQ(type, VIR_DOMAIN_BLKIO_DEVICE_WRITE_IOPS)) {
-            if (virStrToLong_uip(temp, &p, 10, &result[i].wiops) < 0)
-                goto number_error;
-        } else if (STREQ(type, VIR_DOMAIN_BLKIO_DEVICE_READ_BPS)) {
-            if (virStrToLong_ullp(temp, &p, 10, &result[i].rbps) < 0)
-                goto number_error;
-        } else if (STREQ(type, VIR_DOMAIN_BLKIO_DEVICE_WRITE_BPS)) {
-            if (virStrToLong_ullp(temp, &p, 10, &result[i].wbps) < 0)
-                goto number_error;
-        } else {
-            virReportError(VIR_ERR_INVALID_ARG,
-                           _("unknown parameter '%s'"), type);
-            goto cleanup;
-        }
-
-        i++;
-
-        if (*p == '\0')
-            break;
-        else if (*p != ',')
-            goto parse_error;
-        temp = p + 1;
-    }
-
-    if (!i)
-        VIR_FREE(result);
-
-    *dev = result;
-    *size = i;
-
-    return 0;
-
- parse_error:
-    virReportError(VIR_ERR_INVALID_ARG,
-                   _("unable to parse blkio device '%s' '%s'"),
-                   type, blkioDeviceStr);
-    goto cleanup;
-
- number_error:
-    virReportError(VIR_ERR_INVALID_ARG,
-                   _("invalid value '%s' for parameter '%s' of device '%s'"),
-                   temp, type, result[i].path);
-
- cleanup:
-    if (result) {
-        virBlkioDeviceArrayClear(result, ndevices);
-        VIR_FREE(result);
-    }
-    return -1;
-}
 
 static int
 lxcDomainBlockStats(virDomainPtr dom,
@@ -2486,10 +2380,10 @@ lxcDomainSetBlkioParameters(virDomainPtr dom,
                 virBlkioDevicePtr devices = NULL;
                 size_t j;
 
-                if (lxcDomainParseBlkioDeviceStr(params[i].value.s,
-                                                 param->field,
-                                                 &devices,
-                                                 &ndevices) < 0) {
+                if (virDomainDriverParseBlkioDeviceStr(params[i].value.s,
+                                                       param->field,
+                                                       &devices,
+                                                       &ndevices) < 0) {
                     ret = -1;
                     continue;
                 }
@@ -2571,10 +2465,10 @@ lxcDomainSetBlkioParameters(virDomainPtr dom,
                 virBlkioDevicePtr devices = NULL;
                 size_t ndevices;
 
-                if (lxcDomainParseBlkioDeviceStr(params[i].value.s,
-                                                 param->field,
-                                                 &devices,
-                                                 &ndevices) < 0) {
+                if (virDomainDriverParseBlkioDeviceStr(params[i].value.s,
+                                                       param->field,
+                                                       &devices,
+                                                       &ndevices) < 0) {
                     ret = -1;
                     continue;
                 }
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 3048e8a5c7..2205dd1194 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -9310,116 +9310,6 @@ static char *qemuDomainGetSchedulerType(virDomainPtr dom,
     return ret;
 }
 
-/* blkioDeviceStr in the form of /device/path,weight,/device/path,weight
- * for example, /dev/disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0,800
- */
-static int
-qemuDomainParseBlkioDeviceStr(char *blkioDeviceStr, const char *type,
-                              virBlkioDevicePtr *dev, size_t *size)
-{
-    char *temp;
-    int ndevices = 0;
-    int nsep = 0;
-    size_t i;
-    virBlkioDevicePtr result = NULL;
-
-    *dev = NULL;
-    *size = 0;
-
-    if (STREQ(blkioDeviceStr, ""))
-        return 0;
-
-    temp = blkioDeviceStr;
-    while (temp) {
-        temp = strchr(temp, ',');
-        if (temp) {
-            temp++;
-            nsep++;
-        }
-    }
-
-    /* A valid string must have even number of fields, hence an odd
-     * number of commas.  */
-    if (!(nsep & 1))
-        goto parse_error;
-
-    ndevices = (nsep + 1) / 2;
-
-    if (VIR_ALLOC_N(result, ndevices) < 0)
-        return -1;
-
-    i = 0;
-    temp = blkioDeviceStr;
-    while (temp) {
-        char *p = temp;
-
-        /* device path */
-        p = strchr(p, ',');
-        if (!p)
-            goto parse_error;
-
-        result[i].path = g_strndup(temp, p - temp);
-
-        /* value */
-        temp = p + 1;
-
-        if (STREQ(type, VIR_DOMAIN_BLKIO_DEVICE_WEIGHT)) {
-            if (virStrToLong_uip(temp, &p, 10, &result[i].weight) < 0)
-                goto number_error;
-        } else if (STREQ(type, VIR_DOMAIN_BLKIO_DEVICE_READ_IOPS)) {
-            if (virStrToLong_uip(temp, &p, 10, &result[i].riops) < 0)
-                goto number_error;
-        } else if (STREQ(type, VIR_DOMAIN_BLKIO_DEVICE_WRITE_IOPS)) {
-            if (virStrToLong_uip(temp, &p, 10, &result[i].wiops) < 0)
-                goto number_error;
-        } else if (STREQ(type, VIR_DOMAIN_BLKIO_DEVICE_READ_BPS)) {
-            if (virStrToLong_ullp(temp, &p, 10, &result[i].rbps) < 0)
-                goto number_error;
-        } else if (STREQ(type, VIR_DOMAIN_BLKIO_DEVICE_WRITE_BPS)) {
-            if (virStrToLong_ullp(temp, &p, 10, &result[i].wbps) < 0)
-                goto number_error;
-        } else {
-            virReportError(VIR_ERR_INVALID_ARG,
-                           _("unknown parameter '%s'"), type);
-            goto cleanup;
-        }
-
-        i++;
-
-        if (*p == '\0')
-            break;
-        else if (*p != ',')
-            goto parse_error;
-        temp = p + 1;
-    }
-
-    if (!i)
-        VIR_FREE(result);
-
-    *dev = result;
-    *size = i;
-
-    return 0;
-
- parse_error:
-    virReportError(VIR_ERR_INVALID_ARG,
-                   _("unable to parse blkio device '%s' '%s'"),
-                   type, blkioDeviceStr);
-    goto cleanup;
-
- number_error:
-    virReportError(VIR_ERR_INVALID_ARG,
-                   _("invalid value '%s' for parameter '%s' of device '%s'"),
-                   temp, type, result[i].path);
-
- cleanup:
-    if (result) {
-        virBlkioDeviceArrayClear(result, ndevices);
-        VIR_FREE(result);
-    }
-    return -1;
-}
-
 
 static int
 qemuDomainSetBlkioParameters(virDomainPtr dom,
@@ -9502,10 +9392,10 @@ qemuDomainSetBlkioParameters(virDomainPtr dom,
                 virBlkioDevicePtr devices = NULL;
                 size_t j;
 
-                if (qemuDomainParseBlkioDeviceStr(param->value.s,
-                                                  param->field,
-                                                  &devices,
-                                                  &ndevices) < 0) {
+                if (virDomainDriverParseBlkioDeviceStr(param->value.s,
+                                                       param->field,
+                                                       &devices,
+                                                       &ndevices) < 0) {
                     ret = -1;
                     continue;
                 }
@@ -9590,10 +9480,10 @@ qemuDomainSetBlkioParameters(virDomainPtr dom,
                 virBlkioDevicePtr devices = NULL;
                 size_t ndevices;
 
-                if (qemuDomainParseBlkioDeviceStr(param->value.s,
-                                                  param->field,
-                                                  &devices,
-                                                  &ndevices) < 0) {
+                if (virDomainDriverParseBlkioDeviceStr(param->value.s,
+                                                       param->field,
+                                                       &devices,
+                                                       &ndevices) < 0) {
                     ret = -1;
                     continue;
                 }
-- 
2.24.1





More information about the libvir-list mailing list