This new helper avoids more code repetition inside
lxcDomainSetBlkioParameters() and qemuDomainSetBlkioParameters().

Signed-off-by: Daniel Henrique Barboza <danielhb...@gmail.com>
---
 src/conf/domain_conf.c   | 41 ++++++++++++++++++++++++++++++++++++++++
 src/conf/domain_conf.h   |  4 ++++
 src/libvirt_private.syms |  1 +
 src/lxc/lxc_driver.c     | 32 +++----------------------------
 src/qemu/qemu_driver.c   | 32 +++----------------------------
 5 files changed, 52 insertions(+), 58 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index df8a71290d..fa1ef10899 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -1806,6 +1806,47 @@ virDomainParseBlkioDeviceStr(char *blkioDeviceStr, const 
char *type,
 }
 
 
+int
+virDomainParseMergePersistentDefBlkioParams(virDomainDefPtr persistentDef,
+                                            virTypedParameterPtr params,
+                                            int nparams)
+{
+    size_t i;
+    int ret = 0;
+
+    for (i = 0; i < nparams; i++) {
+        virTypedParameterPtr param = &params[i];
+
+        if (STREQ(param->field, VIR_DOMAIN_BLKIO_WEIGHT)) {
+            persistentDef->blkio.weight = param->value.ui;
+        } else if (STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_WEIGHT) ||
+                   STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_READ_IOPS) ||
+                   STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_WRITE_IOPS) ||
+                   STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_READ_BPS) ||
+                   STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_WRITE_BPS)) {
+            virBlkioDevicePtr devices = NULL;
+            size_t ndevices;
+
+            if (virDomainParseBlkioDeviceStr(param->value.s,
+                                             param->field,
+                                             &devices,
+                                             &ndevices) < 0) {
+                ret = -1;
+                continue;
+            }
+            if (virDomainMergeBlkioDevice(&persistentDef->blkio.devices,
+                                          &persistentDef->blkio.ndevices,
+                                          devices, ndevices, param->field) < 0)
+                ret = -1;
+            virBlkioDeviceArrayClear(devices, ndevices);
+            VIR_FREE(devices);
+        }
+    }
+
+    return ret;
+}
+
+
 /**
  * virDomainDefCheckUnsupportedMemoryHotplug:
  * @def: domain definition
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index e1daf26e07..c09bc31c12 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -2218,6 +2218,10 @@ int virDomainMergeBlkioDevice(virBlkioDevicePtr 
*dest_array,
 int virDomainParseBlkioDeviceStr(char *blkioDeviceStr, const char *type,
                                  virBlkioDevicePtr *dev, size_t *size);
 
+int virDomainParseMergePersistentDefBlkioParams(virDomainDefPtr persistentDef,
+                                                virTypedParameterPtr params,
+                                                int nparams);
+
 struct _virDomainResourceDef {
     char *partition;
 };
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 196c7bdb23..1422fbc3d9 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -565,6 +565,7 @@ virDomainOSTypeFromString;
 virDomainOSTypeToString;
 virDomainParseBlkioDeviceStr;
 virDomainParseMemory;
+virDomainParseMergePersistentDefBlkioParams;
 virDomainPausedReasonTypeFromString;
 virDomainPausedReasonTypeToString;
 virDomainPMSuspendedReasonTypeFromString;
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
index 9a585d0d07..1fabba40bd 100644
--- a/src/lxc/lxc_driver.c
+++ b/src/lxc/lxc_driver.c
@@ -2313,7 +2313,6 @@ lxcDomainSetBlkioParameters(virDomainPtr dom,
                             unsigned int flags)
 {
     virLXCDriverPtr driver = dom->conn->privateData;
-    size_t i;
     virDomainObjPtr vm = NULL;
     virDomainDefPtr def = NULL;
     virDomainDefPtr persistentDef = NULL;
@@ -2370,34 +2369,9 @@ lxcDomainSetBlkioParameters(virDomainPtr dom,
     if (ret < 0)
         goto endjob;
     if (persistentDef) {
-        for (i = 0; i < nparams; i++) {
-            virTypedParameterPtr param = &params[i];
-
-            if (STREQ(param->field, VIR_DOMAIN_BLKIO_WEIGHT)) {
-                persistentDef->blkio.weight = params[i].value.ui;
-            } else if (STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_WEIGHT) ||
-                       STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_READ_IOPS) 
||
-                       STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_WRITE_IOPS) 
||
-                       STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_READ_BPS) ||
-                       STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_WRITE_BPS)) 
{
-                virBlkioDevicePtr devices = NULL;
-                size_t ndevices;
-
-                if (virDomainParseBlkioDeviceStr(params[i].value.s,
-                                                 param->field,
-                                                 &devices,
-                                                 &ndevices) < 0) {
-                    ret = -1;
-                    continue;
-                }
-                if (virDomainMergeBlkioDevice(&persistentDef->blkio.devices,
-                                              &persistentDef->blkio.ndevices,
-                                              devices, ndevices, param->field) 
< 0)
-                    ret = -1;
-                virBlkioDeviceArrayClear(devices, ndevices);
-                VIR_FREE(devices);
-            }
-        }
+        ret = virDomainParseMergePersistentDefBlkioParams(persistentDef,
+                                                          params,
+                                                          nparams);
 
         if (virDomainDefSave(persistentDef, driver->xmlopt, cfg->configDir) < 
0)
             ret = -1;
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index da4e6d7a93..c154efbaf6 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -9317,7 +9317,6 @@ qemuDomainSetBlkioParameters(virDomainPtr dom,
                              unsigned int flags)
 {
     virQEMUDriverPtr driver = dom->conn->privateData;
-    size_t i;
     virDomainObjPtr vm = NULL;
     virDomainDefPtr def;
     virDomainDefPtr persistentDef;
@@ -9383,34 +9382,9 @@ qemuDomainSetBlkioParameters(virDomainPtr dom,
     if (ret < 0)
         goto endjob;
     if (persistentDef) {
-        for (i = 0; i < nparams; i++) {
-            virTypedParameterPtr param = &params[i];
-
-            if (STREQ(param->field, VIR_DOMAIN_BLKIO_WEIGHT)) {
-                persistentDef->blkio.weight = param->value.ui;
-            } else if (STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_WEIGHT) ||
-                       STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_READ_IOPS) 
||
-                       STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_WRITE_IOPS) 
||
-                       STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_READ_BPS) ||
-                       STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_WRITE_BPS)) 
{
-                virBlkioDevicePtr devices = NULL;
-                size_t ndevices;
-
-                if (virDomainParseBlkioDeviceStr(param->value.s,
-                                                 param->field,
-                                                 &devices,
-                                                 &ndevices) < 0) {
-                    ret = -1;
-                    continue;
-                }
-                if (virDomainMergeBlkioDevice(&persistentDef->blkio.devices,
-                                              &persistentDef->blkio.ndevices,
-                                              devices, ndevices, param->field) 
< 0)
-                    ret = -1;
-                virBlkioDeviceArrayClear(devices, ndevices);
-                VIR_FREE(devices);
-            }
-        }
+        ret = virDomainParseMergePersistentDefBlkioParams(persistentDef,
+                                                          params,
+                                                          nparams);
 
         if (virDomainDefSave(persistentDef, driver->xmlopt, cfg->configDir) < 
0)
             ret = -1;
-- 
2.24.1


Reply via email to