virNetDevBandwidthParseRate: Refactor parsing

Remove the unnecessary check for valid arguments and use
virXMLPropULongLong instead of hand-written property parsers.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2023-02-13 14:59:46 +01:00
parent ede5ee9eca
commit 036e677886

View File

@ -25,61 +25,46 @@
#define VIR_FROM_THIS VIR_FROM_NONE #define VIR_FROM_THIS VIR_FROM_NONE
static int static int
virNetDevBandwidthParseRate(xmlNodePtr node, virNetDevBandwidthRate *rate) virNetDevBandwidthParseRate(xmlNodePtr node,
virNetDevBandwidthRate *rate,
bool allowFloor)
{ {
g_autofree char *average = NULL; int rc_average;
g_autofree char *peak = NULL; int rc_peak;
g_autofree char *burst = NULL; int rc_burst;
g_autofree char *floor = NULL; int rc_floor;
if (!node || !rate) { if ((rc_average = virXMLPropULongLong(node, "average", 10, VIR_XML_PROP_NONE,
virReportError(VIR_ERR_INVALID_ARG, "%s", &rate->average)) < 0)
_("invalid argument supplied"));
return -1; return -1;
}
average = virXMLPropString(node, "average"); if ((rc_peak = virXMLPropULongLong(node, "peak", 10, VIR_XML_PROP_NONE,
peak = virXMLPropString(node, "peak"); &rate->peak)) < 0)
burst = virXMLPropString(node, "burst"); return -1;
floor = virXMLPropString(node, "floor");
if (average) { if ((rc_burst = virXMLPropULongLong(node, "burst", 10, VIR_XML_PROP_NONE,
if (virStrToLong_ullp(average, NULL, 10, &rate->average) < 0) { &rate->burst)) < 0)
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, return -1;
_("could not convert bandwidth average value '%1$s'"),
average); if ((rc_floor = virXMLPropULongLong(node, "floor", 10, VIR_XML_PROP_NONE,
return -1; &rate->floor)) < 0)
} return -1;
} else if (!floor) {
if (!rc_average && !rc_floor) {
virReportError(VIR_ERR_XML_DETAIL, "%s", virReportError(VIR_ERR_XML_DETAIL, "%s",
_("Missing mandatory average or floor attributes")); _("Missing mandatory average or floor attributes"));
return -1; return -1;
} }
if ((peak || burst) && !average) { if ((rc_peak || rc_burst) && !rc_average) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("'peak' and 'burst' require 'average' attribute")); _("'peak' and 'burst' require 'average' attribute"));
return -1; return -1;
} }
if (peak && virStrToLong_ullp(peak, NULL, 10, &rate->peak) < 0) { if (rc_floor && !allowFloor) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("could not convert bandwidth peak value '%1$s'"), _("floor attribute is not supported for this config"));
peak);
return -1;
}
if (burst && virStrToLong_ullp(burst, NULL, 10, &rate->burst) < 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("could not convert bandwidth burst value '%1$s'"),
burst);
return -1;
}
if (floor && virStrToLong_ullp(floor, NULL, 10, &rate->floor) < 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("could not convert bandwidth floor value '%1$s'"),
floor);
return -1; return -1;
} }
@ -157,32 +142,16 @@ virNetDevBandwidthParse(virNetDevBandwidth **bandwidth,
if (in) { if (in) {
def->in = g_new0(virNetDevBandwidthRate, 1); def->in = g_new0(virNetDevBandwidthRate, 1);
if (virNetDevBandwidthParseRate(in, def->in) < 0) { if (virNetDevBandwidthParseRate(in, def->in, allowFloor) < 0)
/* helper reported error for us */
return -1; return -1;
}
if (def->in->floor && !allowFloor) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("floor attribute is not supported for this config"));
return -1;
}
} }
if (out) { if (out) {
def->out = g_new0(virNetDevBandwidthRate, 1); def->out = g_new0(virNetDevBandwidthRate, 1);
if (virNetDevBandwidthParseRate(out, def->out) < 0) { /* floor is not allowed for <outbound> */
/* helper reported error for us */ if (virNetDevBandwidthParseRate(out, def->out, false) < 0)
return -1; return -1;
}
if (def->out->floor) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("'floor' attribute allowed "
"only in <inbound> element"));
return -1;
}
} }
if (def->in || def->out) if (def->in || def->out)