domain_driver.c: add virDomainDriverParseBlkioDeviceStr()

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@gmail.com>
Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Daniel Henrique Barboza 2020-02-17 16:29:17 -05:00 committed by Ján Tomko
parent 8595948bc8
commit 2450a04119
5 changed files with 132 additions and 232 deletions

View File

@ -22,6 +22,7 @@
#include "domain_driver.h" #include "domain_driver.h"
#include "viralloc.h" #include "viralloc.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_DOMAIN #define VIR_FROM_THIS VIR_FROM_DOMAIN
@ -94,3 +95,114 @@ virDomainDriverMergeBlkioDevice(virBlkioDevicePtr *dest_array,
return 0; 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;
}

View File

@ -27,3 +27,6 @@ int virDomainDriverMergeBlkioDevice(virBlkioDevicePtr *dest_array,
virBlkioDevicePtr src_array, virBlkioDevicePtr src_array,
size_t src_size, size_t src_size,
const char *type); const char *type);
int virDomainDriverParseBlkioDeviceStr(char *blkioDeviceStr, const char *type,
virBlkioDevicePtr *dev, size_t *size);

View File

@ -1397,6 +1397,7 @@ virDomainCgroupSetupMemtune;
# hypervisor/domain_cgroup.h # hypervisor/domain_cgroup.h
virDomainDriverMergeBlkioDevice; virDomainDriverMergeBlkioDevice;
virDomainDriverParseBlkioDeviceStr;
# libvirt_internal.h # libvirt_internal.h

View File

@ -2108,112 +2108,6 @@ lxcDomainGetSchedulerParameters(virDomainPtr domain,
return lxcDomainGetSchedulerParametersFlags(domain, params, nparams, 0); 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 static int
lxcDomainBlockStats(virDomainPtr dom, lxcDomainBlockStats(virDomainPtr dom,
@ -2486,7 +2380,7 @@ lxcDomainSetBlkioParameters(virDomainPtr dom,
virBlkioDevicePtr devices = NULL; virBlkioDevicePtr devices = NULL;
size_t j; size_t j;
if (lxcDomainParseBlkioDeviceStr(params[i].value.s, if (virDomainDriverParseBlkioDeviceStr(params[i].value.s,
param->field, param->field,
&devices, &devices,
&ndevices) < 0) { &ndevices) < 0) {
@ -2571,7 +2465,7 @@ lxcDomainSetBlkioParameters(virDomainPtr dom,
virBlkioDevicePtr devices = NULL; virBlkioDevicePtr devices = NULL;
size_t ndevices; size_t ndevices;
if (lxcDomainParseBlkioDeviceStr(params[i].value.s, if (virDomainDriverParseBlkioDeviceStr(params[i].value.s,
param->field, param->field,
&devices, &devices,
&ndevices) < 0) { &ndevices) < 0) {

View File

@ -9310,116 +9310,6 @@ static char *qemuDomainGetSchedulerType(virDomainPtr dom,
return ret; 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 static int
qemuDomainSetBlkioParameters(virDomainPtr dom, qemuDomainSetBlkioParameters(virDomainPtr dom,
@ -9502,7 +9392,7 @@ qemuDomainSetBlkioParameters(virDomainPtr dom,
virBlkioDevicePtr devices = NULL; virBlkioDevicePtr devices = NULL;
size_t j; size_t j;
if (qemuDomainParseBlkioDeviceStr(param->value.s, if (virDomainDriverParseBlkioDeviceStr(param->value.s,
param->field, param->field,
&devices, &devices,
&ndevices) < 0) { &ndevices) < 0) {
@ -9590,7 +9480,7 @@ qemuDomainSetBlkioParameters(virDomainPtr dom,
virBlkioDevicePtr devices = NULL; virBlkioDevicePtr devices = NULL;
size_t ndevices; size_t ndevices;
if (qemuDomainParseBlkioDeviceStr(param->value.s, if (virDomainDriverParseBlkioDeviceStr(param->value.s,
param->field, param->field,
&devices, &devices,
&ndevices) < 0) { &ndevices) < 0) {