Get rid of lots of duplicated code.
---
src/qemu/qemu_driver.c | 411 +++++++++----------------------------------------
1 file changed, 75 insertions(+), 336 deletions(-)
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index e8e1418..a9cfde2 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -9080,6 +9080,57 @@ qemuDomainSetBlkioParameters(virDomainPtr dom,
return ret;
}
+
+static int
+qemuDomainGetBlkioParametersAssignFromDef(virDomainDefPtr def,
+ virTypedParameterPtr params,
+ int *nparams,
+ int maxparams)
+{
+ virBuffer buf = VIR_BUFFER_INITIALIZER;
+ char *data = NULL;
+ size_t i;
+
+#define QEMU_BLKIO_ASSIGN(param, format, name) \
+ if (*nparams < maxparams) { \
+ for (i = 0; i < def->blkio.ndevices; i++) { \
+ if (!def->blkio.devices[i].param) \
+ continue; \
+ virBufferAsprintf(&buf, "%s," format ",",
\
+ def->blkio.devices[i].path, \
+ def->blkio.devices[i].param); \
+ } \
+ virBufferTrim(&buf, ",", -1);
\
+ if (virBufferCheckError(&buf) < 0) \
+ goto error; \
+ data = virBufferContentAndReset(&buf); \
+ if (virTypedParameterAssign(&(params[(*nparams)++]), name, \
+ VIR_TYPED_PARAM_STRING, data) < 0) \
+ goto error; \
+ VIR_FREE(data); \
+ }
+
+ /* blkiotune.device_weight */
+ QEMU_BLKIO_ASSIGN(weight, "%u", VIR_DOMAIN_BLKIO_DEVICE_WEIGHT);
+ /* blkiotune.device_read_iops */
+ QEMU_BLKIO_ASSIGN(riops, "%u", VIR_DOMAIN_BLKIO_DEVICE_READ_IOPS);
+ /* blkiotune.device_write_iops */
+ QEMU_BLKIO_ASSIGN(wiops, "%u", VIR_DOMAIN_BLKIO_DEVICE_WRITE_IOPS);
+ /* blkiotune.device_read_bps */
+ QEMU_BLKIO_ASSIGN(rbps, "%llu", VIR_DOMAIN_BLKIO_DEVICE_READ_BPS);
+ /* blkiotune.device_write_bps */
+ QEMU_BLKIO_ASSIGN(wbps, "%llu", VIR_DOMAIN_BLKIO_DEVICE_WRITE_BPS);
+
+#undef QEMU_BLKIO_ASSIGN
+
+ return 0;
+
+ error:
+ VIR_FREE(data);
+ virBufferFreeAndReset(&buf);
+ return -1;
+}
+
static int
qemuDomainGetBlkioParameters(virDomainPtr dom,
virTypedParameterPtr params,
@@ -9087,10 +9138,10 @@ qemuDomainGetBlkioParameters(virDomainPtr dom,
unsigned int flags)
{
virQEMUDriverPtr driver = dom->conn->privateData;
- size_t i, j;
virDomainObjPtr vm = NULL;
virDomainDefPtr def = NULL;
virDomainDefPtr persistentDef = NULL;
+ int maxparams = QEMU_NB_BLKIO_PARAM;
unsigned int val;
int ret = -1;
qemuDomainObjPrivatePtr priv;
@@ -9123,8 +9174,12 @@ qemuDomainGetBlkioParameters(virDomainPtr dom,
*nparams = QEMU_NB_BLKIO_PARAM;
ret = 0;
goto cleanup;
+ } else if (*nparams < maxparams) {
+ maxparams = *nparams;
}
+ *nparams = 0;
+
if (virDomainObjGetDefs(vm, flags, &def, &persistentDef) < 0)
goto cleanup;
@@ -9135,347 +9190,31 @@ qemuDomainGetBlkioParameters(virDomainPtr dom,
goto cleanup;
}
- for (i = 0; i < *nparams && i < QEMU_NB_BLKIO_PARAM; i++) {
- virTypedParameterPtr param = ¶ms[i];
- val = 0;
-
- switch (i) {
- case 0: /* fill blkio weight here */
- if (virCgroupGetBlkioWeight(priv->cgroup, &val) < 0)
- goto cleanup;
- if (virTypedParameterAssign(param, VIR_DOMAIN_BLKIO_WEIGHT,
- VIR_TYPED_PARAM_UINT, val) < 0)
- goto cleanup;
- break;
-
- case 1: /* blkiotune.device_weight */
- if (def->blkio.ndevices > 0) {
- virBuffer buf = VIR_BUFFER_INITIALIZER;
- bool comma = false;
-
- for (j = 0; j < def->blkio.ndevices; j++) {
- if (!def->blkio.devices[j].weight)
- continue;
- if (comma)
- virBufferAddChar(&buf, ',');
- else
- comma = true;
- virBufferAsprintf(&buf, "%s,%u",
- def->blkio.devices[j].path,
- def->blkio.devices[j].weight);
- }
- if (virBufferCheckError(&buf) < 0)
- goto cleanup;
- param->value.s = virBufferContentAndReset(&buf);
- }
- if (virTypedParameterAssign(param,
- VIR_DOMAIN_BLKIO_DEVICE_WEIGHT,
- VIR_TYPED_PARAM_STRING,
- param->value.s) < 0)
- goto cleanup;
- break;
-
- case 2: /* blkiotune.device_read_iops */
- if (def->blkio.ndevices > 0) {
- virBuffer buf = VIR_BUFFER_INITIALIZER;
- bool comma = false;
-
- for (j = 0; j < def->blkio.ndevices; j++) {
- if (!def->blkio.devices[j].riops)
- continue;
- if (comma)
- virBufferAddChar(&buf, ',');
- else
- comma = true;
- virBufferAsprintf(&buf, "%s,%u",
- def->blkio.devices[j].path,
- def->blkio.devices[j].riops);
- }
- if (virBufferCheckError(&buf) < 0)
- goto cleanup;
- param->value.s = virBufferContentAndReset(&buf);
- }
- if (virTypedParameterAssign(param,
- VIR_DOMAIN_BLKIO_DEVICE_READ_IOPS,
- VIR_TYPED_PARAM_STRING,
- param->value.s) < 0)
- goto cleanup;
- break;
-
- case 3: /* blkiotune.device_write_iops */
- if (def->blkio.ndevices > 0) {
- virBuffer buf = VIR_BUFFER_INITIALIZER;
- bool comma = false;
-
- for (j = 0; j < def->blkio.ndevices; j++) {
- if (!def->blkio.devices[j].wiops)
- continue;
- if (comma)
- virBufferAddChar(&buf, ',');
- else
- comma = true;
- virBufferAsprintf(&buf, "%s,%u",
- def->blkio.devices[j].path,
- def->blkio.devices[j].wiops);
- }
- if (virBufferCheckError(&buf) < 0)
- goto cleanup;
- param->value.s = virBufferContentAndReset(&buf);
- }
- if (virTypedParameterAssign(param,
- VIR_DOMAIN_BLKIO_DEVICE_WRITE_IOPS,
- VIR_TYPED_PARAM_STRING,
- param->value.s) < 0)
- goto cleanup;
- break;
-
- case 4: /* blkiotune.device_read_bps */
- if (def->blkio.ndevices > 0) {
- virBuffer buf = VIR_BUFFER_INITIALIZER;
- bool comma = false;
-
- for (j = 0; j < def->blkio.ndevices; j++) {
- if (!def->blkio.devices[j].rbps)
- continue;
- if (comma)
- virBufferAddChar(&buf, ',');
- else
- comma = true;
- virBufferAsprintf(&buf, "%s,%llu",
- def->blkio.devices[j].path,
- def->blkio.devices[j].rbps);
- }
- if (virBufferCheckError(&buf) < 0)
- goto cleanup;
- param->value.s = virBufferContentAndReset(&buf);
- }
- if (virTypedParameterAssign(param,
- VIR_DOMAIN_BLKIO_DEVICE_READ_BPS,
- VIR_TYPED_PARAM_STRING,
- param->value.s) < 0)
- goto cleanup;
- break;
+ /* fill blkio weight here */
+ if (virCgroupGetBlkioWeight(priv->cgroup, &val) < 0)
+ goto cleanup;
+ if (virTypedParameterAssign(&(params[(*nparams)++]),
+ VIR_DOMAIN_BLKIO_WEIGHT,
+ VIR_TYPED_PARAM_UINT, val) < 0)
+ goto cleanup;
- case 5: /* blkiotune.device_write_bps */
- if (def->blkio.ndevices > 0) {
- virBuffer buf = VIR_BUFFER_INITIALIZER;
- bool comma = false;
-
- for (j = 0; j < def->blkio.ndevices; j++) {
- if (!def->blkio.devices[j].wbps)
- continue;
- if (comma)
- virBufferAddChar(&buf, ',');
- else
- comma = true;
- virBufferAsprintf(&buf, "%s,%llu",
- def->blkio.devices[j].path,
- def->blkio.devices[j].wbps);
- }
- if (virBufferCheckError(&buf) < 0)
- goto cleanup;
- param->value.s = virBufferContentAndReset(&buf);
- }
- if (virTypedParameterAssign(param,
- VIR_DOMAIN_BLKIO_DEVICE_WRITE_BPS,
- VIR_TYPED_PARAM_STRING,
- param->value.s) < 0)
- goto cleanup;
- break;
+ if (qemuDomainGetBlkioParametersAssignFromDef(def, params, nparams,
+ maxparams) < 0)
+ goto cleanup;
- /* coverity[dead_error_begin] */
- default:
- break;
- /* should not hit here */
- }
- }
} else if (persistentDef) {
- for (i = 0; i < *nparams && i < QEMU_NB_BLKIO_PARAM; i++) {
- virTypedParameterPtr param = ¶ms[i];
- val = 0;
- param->value.ui = 0;
- param->type = VIR_TYPED_PARAM_UINT;
-
- switch (i) {
- case 0: /* fill blkio weight here */
- if (virStrcpyStatic(param->field, VIR_DOMAIN_BLKIO_WEIGHT) == NULL) {
- virReportError(VIR_ERR_INTERNAL_ERROR,
- _("Field name '%s' too long"),
- VIR_DOMAIN_BLKIO_WEIGHT);
- goto cleanup;
- }
- param->value.ui = persistentDef->blkio.weight;
- break;
-
- case 1: /* blkiotune.device_weight */
- if (persistentDef->blkio.ndevices > 0) {
- virBuffer buf = VIR_BUFFER_INITIALIZER;
- bool comma = false;
-
- for (j = 0; j < persistentDef->blkio.ndevices; j++) {
- if (!persistentDef->blkio.devices[j].weight)
- continue;
- if (comma)
- virBufferAddChar(&buf, ',');
- else
- comma = true;
- virBufferAsprintf(&buf, "%s,%u",
- persistentDef->blkio.devices[j].path,
- persistentDef->blkio.devices[j].weight);
- }
- if (virBufferCheckError(&buf) < 0)
- goto cleanup;
- param->value.s = virBufferContentAndReset(&buf);
- }
- if (!param->value.s && VIR_STRDUP(param->value.s,
"") < 0)
- goto cleanup;
- param->type = VIR_TYPED_PARAM_STRING;
- if (virStrcpyStatic(param->field,
- VIR_DOMAIN_BLKIO_DEVICE_WEIGHT) == NULL) {
- virReportError(VIR_ERR_INTERNAL_ERROR,
- _("Field name '%s' too long"),
- VIR_DOMAIN_BLKIO_DEVICE_WEIGHT);
- goto cleanup;
- }
- break;
-
- case 2: /* blkiotune.device_read_iops */
- if (persistentDef->blkio.ndevices > 0) {
- virBuffer buf = VIR_BUFFER_INITIALIZER;
- bool comma = false;
-
- for (j = 0; j < persistentDef->blkio.ndevices; j++) {
- if (!persistentDef->blkio.devices[j].riops)
- continue;
- if (comma)
- virBufferAddChar(&buf, ',');
- else
- comma = true;
- virBufferAsprintf(&buf, "%s,%u",
- persistentDef->blkio.devices[j].path,
- persistentDef->blkio.devices[j].riops);
- }
- if (virBufferCheckError(&buf) < 0)
- goto cleanup;
- param->value.s = virBufferContentAndReset(&buf);
- }
- if (!param->value.s && VIR_STRDUP(param->value.s,
"") < 0)
- goto cleanup;
- param->type = VIR_TYPED_PARAM_STRING;
- if (virStrcpyStatic(param->field,
- VIR_DOMAIN_BLKIO_DEVICE_READ_IOPS) == NULL) {
- virReportError(VIR_ERR_INTERNAL_ERROR,
- _("Field name '%s' too long"),
- VIR_DOMAIN_BLKIO_DEVICE_READ_IOPS);
- goto cleanup;
- }
- break;
- case 3: /* blkiotune.device_write_iops */
- if (persistentDef->blkio.ndevices > 0) {
- virBuffer buf = VIR_BUFFER_INITIALIZER;
- bool comma = false;
-
- for (j = 0; j < persistentDef->blkio.ndevices; j++) {
- if (!persistentDef->blkio.devices[j].wiops)
- continue;
- if (comma)
- virBufferAddChar(&buf, ',');
- else
- comma = true;
- virBufferAsprintf(&buf, "%s,%u",
- persistentDef->blkio.devices[j].path,
- persistentDef->blkio.devices[j].wiops);
- }
- if (virBufferCheckError(&buf) < 0)
- goto cleanup;
- param->value.s = virBufferContentAndReset(&buf);
- }
- if (!param->value.s && VIR_STRDUP(param->value.s,
"") < 0)
- goto cleanup;
- param->type = VIR_TYPED_PARAM_STRING;
- if (virStrcpyStatic(param->field,
- VIR_DOMAIN_BLKIO_DEVICE_WRITE_IOPS) == NULL) {
- virReportError(VIR_ERR_INTERNAL_ERROR,
- _("Field name '%s' too long"),
- VIR_DOMAIN_BLKIO_DEVICE_WRITE_IOPS);
- goto cleanup;
- }
- break;
- case 4: /* blkiotune.device_read_bps */
- if (persistentDef->blkio.ndevices > 0) {
- virBuffer buf = VIR_BUFFER_INITIALIZER;
- bool comma = false;
-
- for (j = 0; j < persistentDef->blkio.ndevices; j++) {
- if (!persistentDef->blkio.devices[j].rbps)
- continue;
- if (comma)
- virBufferAddChar(&buf, ',');
- else
- comma = true;
- virBufferAsprintf(&buf, "%s,%llu",
- persistentDef->blkio.devices[j].path,
- persistentDef->blkio.devices[j].rbps);
- }
- if (virBufferCheckError(&buf) < 0)
- goto cleanup;
- param->value.s = virBufferContentAndReset(&buf);
- }
- if (!param->value.s && VIR_STRDUP(param->value.s,
"") < 0)
- goto cleanup;
- param->type = VIR_TYPED_PARAM_STRING;
- if (virStrcpyStatic(param->field,
- VIR_DOMAIN_BLKIO_DEVICE_READ_BPS) == NULL) {
- virReportError(VIR_ERR_INTERNAL_ERROR,
- _("Field name '%s' too long"),
- VIR_DOMAIN_BLKIO_DEVICE_READ_BPS);
- goto cleanup;
- }
- break;
-
- case 5: /* blkiotune.device_write_bps */
- if (persistentDef->blkio.ndevices > 0) {
- virBuffer buf = VIR_BUFFER_INITIALIZER;
- bool comma = false;
-
- for (j = 0; j < persistentDef->blkio.ndevices; j++) {
- if (!persistentDef->blkio.devices[j].wbps)
- continue;
- if (comma)
- virBufferAddChar(&buf, ',');
- else
- comma = true;
- virBufferAsprintf(&buf, "%s,%llu",
- persistentDef->blkio.devices[j].path,
- persistentDef->blkio.devices[j].wbps);
- }
- if (virBufferCheckError(&buf) < 0)
- goto cleanup;
- param->value.s = virBufferContentAndReset(&buf);
- }
- if (!param->value.s && VIR_STRDUP(param->value.s,
"") < 0)
- goto cleanup;
- param->type = VIR_TYPED_PARAM_STRING;
- if (virStrcpyStatic(param->field,
- VIR_DOMAIN_BLKIO_DEVICE_WRITE_BPS) == NULL) {
- virReportError(VIR_ERR_INTERNAL_ERROR,
- _("Field name '%s' too long"),
- VIR_DOMAIN_BLKIO_DEVICE_WRITE_BPS);
- goto cleanup;
- }
- break;
-
+ /* fill blkio weight here */
+ if (virTypedParameterAssign(&(params[(*nparams)++]),
+ VIR_DOMAIN_BLKIO_WEIGHT,
+ VIR_TYPED_PARAM_UINT,
+ persistentDef->blkio.weight) < 0)
+ goto cleanup;
- /* coverity[dead_error_begin] */
- default:
- break;
- /* should not hit here */
- }
- }
+ if (qemuDomainGetBlkioParametersAssignFromDef(persistentDef, params,
+ nparams, maxparams) < 0)
+ goto cleanup;
}
- if (QEMU_NB_BLKIO_PARAM < *nparams)
- *nparams = QEMU_NB_BLKIO_PARAM;
ret = 0;
cleanup:
--
2.8.2