conf: Refactor virCPUDefParseXML

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
This commit is contained in:
Jiri Denemark 2017-05-23 09:29:36 +02:00
parent 25ec7f6fe9
commit 702013f3b3
5 changed files with 72 additions and 68 deletions

View File

@ -245,12 +245,25 @@ virCPUDefCopy(const virCPUDef *cpu)
} }
virCPUDefPtr /*
virCPUDefParseXML(xmlNodePtr node, * Parses CPU definition XML from a node pointed to by @xpath. If @xpath is
xmlXPathContextPtr ctxt, * NULL, the current node of @ctxt is used (i.e., it is a shortcut to ".").
virCPUType type) *
* Missing <cpu> element in the XML document is not considered an error unless
* @xpath is NULL in which case the function expects it was provided with a
* valid <cpu> element already. In other words, the function returns success
* and sets @cpu to NULL if @xpath is not NULL and the node pointed to by
* @xpath is not found.
*
* Returns 0 on success, -1 on error.
*/
int
virCPUDefParseXML(xmlXPathContextPtr ctxt,
const char *xpath,
virCPUType type,
virCPUDefPtr *cpu)
{ {
virCPUDefPtr def; virCPUDefPtr def = NULL;
xmlNodePtr *nodes = NULL; xmlNodePtr *nodes = NULL;
xmlNodePtr oldnode = ctxt->node; xmlNodePtr oldnode = ctxt->node;
int n; int n;
@ -258,15 +271,23 @@ virCPUDefParseXML(xmlNodePtr node,
char *cpuMode; char *cpuMode;
char *fallback = NULL; char *fallback = NULL;
char *vendor_id = NULL; char *vendor_id = NULL;
int ret = -1;
if (!xmlStrEqual(node->name, BAD_CAST "cpu")) { *cpu = NULL;
if (xpath && !(ctxt->node = virXPathNode(xpath, ctxt))) {
ret = 0;
goto cleanup;
}
if (!xmlStrEqual(ctxt->node->name, BAD_CAST "cpu")) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("XML does not contain expected 'cpu' element")); _("XML does not contain expected 'cpu' element"));
return NULL; goto cleanup;
} }
if (VIR_ALLOC(def) < 0) if (VIR_ALLOC(def) < 0)
return NULL; goto cleanup;
if (type == VIR_CPU_TYPE_AUTO) { if (type == VIR_CPU_TYPE_AUTO) {
if (virXPathBoolean("boolean(./arch)", ctxt)) { if (virXPathBoolean("boolean(./arch)", ctxt)) {
@ -274,7 +295,7 @@ virCPUDefParseXML(xmlNodePtr node,
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("'arch' element cannot be used inside 'cpu'" _("'arch' element cannot be used inside 'cpu'"
" element with 'match' attribute'")); " element with 'match' attribute'"));
goto error; goto cleanup;
} }
def->type = VIR_CPU_TYPE_HOST; def->type = VIR_CPU_TYPE_HOST;
} else { } else {
@ -284,12 +305,12 @@ virCPUDefParseXML(xmlNodePtr node,
def->type = type; def->type = type;
} }
if ((cpuMode = virXMLPropString(node, "mode"))) { if ((cpuMode = virXMLPropString(ctxt->node, "mode"))) {
if (def->type == VIR_CPU_TYPE_HOST) { if (def->type == VIR_CPU_TYPE_HOST) {
VIR_FREE(cpuMode); VIR_FREE(cpuMode);
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("Attribute mode is only allowed for guest CPU")); _("Attribute mode is only allowed for guest CPU"));
goto error; goto cleanup;
} else { } else {
def->mode = virCPUModeTypeFromString(cpuMode); def->mode = virCPUModeTypeFromString(cpuMode);
@ -298,7 +319,7 @@ virCPUDefParseXML(xmlNodePtr node,
_("Invalid mode attribute '%s'"), _("Invalid mode attribute '%s'"),
cpuMode); cpuMode);
VIR_FREE(cpuMode); VIR_FREE(cpuMode);
goto error; goto cleanup;
} }
VIR_FREE(cpuMode); VIR_FREE(cpuMode);
} }
@ -310,7 +331,7 @@ virCPUDefParseXML(xmlNodePtr node,
} }
if (def->type == VIR_CPU_TYPE_GUEST) { if (def->type == VIR_CPU_TYPE_GUEST) {
char *match = virXMLPropString(node, "match"); char *match = virXMLPropString(ctxt->node, "match");
char *check; char *check;
if (!match) { if (!match) {
@ -326,11 +347,11 @@ virCPUDefParseXML(xmlNodePtr node,
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("Invalid match attribute for CPU " _("Invalid match attribute for CPU "
"specification")); "specification"));
goto error; goto cleanup;
} }
} }
if ((check = virXMLPropString(node, "check"))) { if ((check = virXMLPropString(ctxt->node, "check"))) {
int value = virCPUCheckTypeFromString(check); int value = virCPUCheckTypeFromString(check);
VIR_FREE(check); VIR_FREE(check);
@ -338,7 +359,7 @@ virCPUDefParseXML(xmlNodePtr node,
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("Invalid check attribute for CPU " _("Invalid check attribute for CPU "
"specification")); "specification"));
goto error; goto cleanup;
} }
def->check = value; def->check = value;
} }
@ -349,13 +370,13 @@ virCPUDefParseXML(xmlNodePtr node,
if (!arch) { if (!arch) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("Missing CPU architecture")); _("Missing CPU architecture"));
goto error; goto cleanup;
} }
if ((def->arch = virArchFromString(arch)) == VIR_ARCH_NONE) { if ((def->arch = virArchFromString(arch)) == VIR_ARCH_NONE) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("Unknown architecture %s"), arch); _("Unknown architecture %s"), arch);
VIR_FREE(arch); VIR_FREE(arch);
goto error; goto cleanup;
} }
VIR_FREE(arch); VIR_FREE(arch);
} }
@ -364,7 +385,7 @@ virCPUDefParseXML(xmlNodePtr node,
def->type == VIR_CPU_TYPE_HOST) { def->type == VIR_CPU_TYPE_HOST) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("Missing CPU model name")); _("Missing CPU model name"));
goto error; goto cleanup;
} }
if (def->type == VIR_CPU_TYPE_GUEST && if (def->type == VIR_CPU_TYPE_GUEST &&
@ -374,7 +395,7 @@ virCPUDefParseXML(xmlNodePtr node,
if ((def->fallback = virCPUFallbackTypeFromString(fallback)) < 0) { if ((def->fallback = virCPUFallbackTypeFromString(fallback)) < 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("Invalid fallback attribute")); _("Invalid fallback attribute"));
goto error; goto cleanup;
} }
} }
@ -384,14 +405,14 @@ virCPUDefParseXML(xmlNodePtr node,
virReportError(VIR_ERR_XML_ERROR, virReportError(VIR_ERR_XML_ERROR,
_("vendor_id must be exactly %d characters long"), _("vendor_id must be exactly %d characters long"),
VIR_CPU_VENDOR_ID_LENGTH); VIR_CPU_VENDOR_ID_LENGTH);
goto error; goto cleanup;
} }
/* ensure that the string can be passed to qemu*/ /* ensure that the string can be passed to qemu*/
if (strchr(vendor_id, ',')) { if (strchr(vendor_id, ',')) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("vendor id is invalid")); _("vendor id is invalid"));
goto error; goto cleanup;
} }
def->vendor_id = vendor_id; def->vendor_id = vendor_id;
@ -403,61 +424,54 @@ virCPUDefParseXML(xmlNodePtr node,
if (def->vendor && !def->model) { if (def->vendor && !def->model) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("CPU vendor specified without CPU model")); _("CPU vendor specified without CPU model"));
goto error; goto cleanup;
} }
if (virXPathNode("./topology[1]", ctxt)) { if (virXPathNode("./topology[1]", ctxt)) {
int ret;
unsigned long ul; unsigned long ul;
ret = virXPathULong("string(./topology[1]/@sockets)", if (virXPathULong("string(./topology[1]/@sockets)", ctxt, &ul) < 0) {
ctxt, &ul);
if (ret < 0) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("Missing 'sockets' attribute in CPU topology")); _("Missing 'sockets' attribute in CPU topology"));
goto error; goto cleanup;
} }
def->sockets = (unsigned int) ul; def->sockets = (unsigned int) ul;
ret = virXPathULong("string(./topology[1]/@cores)", if (virXPathULong("string(./topology[1]/@cores)", ctxt, &ul) < 0) {
ctxt, &ul);
if (ret < 0) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("Missing 'cores' attribute in CPU topology")); _("Missing 'cores' attribute in CPU topology"));
goto error; goto cleanup;
} }
def->cores = (unsigned int) ul; def->cores = (unsigned int) ul;
ret = virXPathULong("string(./topology[1]/@threads)", if (virXPathULong("string(./topology[1]/@threads)", ctxt, &ul) < 0) {
ctxt, &ul);
if (ret < 0) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("Missing 'threads' attribute in CPU topology")); _("Missing 'threads' attribute in CPU topology"));
goto error; goto cleanup;
} }
def->threads = (unsigned int) ul; def->threads = (unsigned int) ul;
if (!def->sockets || !def->cores || !def->threads) { if (!def->sockets || !def->cores || !def->threads) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("Invalid CPU topology")); _("Invalid CPU topology"));
goto error; goto cleanup;
} }
} }
if ((n = virXPathNodeSet("./feature", ctxt, &nodes)) < 0) if ((n = virXPathNodeSet("./feature", ctxt, &nodes)) < 0)
goto error; goto cleanup;
if (n > 0) { if (n > 0) {
if (!def->model && def->mode == VIR_CPU_MODE_CUSTOM) { if (!def->model && def->mode == VIR_CPU_MODE_CUSTOM) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("Non-empty feature list specified without " _("Non-empty feature list specified without "
"CPU model")); "CPU model"));
goto error; goto cleanup;
} }
if (VIR_RESIZE_N(def->features, def->nfeatures_max, if (VIR_RESIZE_N(def->features, def->nfeatures_max,
def->nfeatures, n) < 0) def->nfeatures, n) < 0)
goto error; goto cleanup;
def->nfeatures = n; def->nfeatures = n;
} }
@ -480,7 +494,7 @@ virCPUDefParseXML(xmlNodePtr node,
if (policy < 0) { if (policy < 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("Invalid CPU feature policy")); _("Invalid CPU feature policy"));
goto error; goto cleanup;
} }
} else { } else {
policy = -1; policy = -1;
@ -490,7 +504,7 @@ virCPUDefParseXML(xmlNodePtr node,
VIR_FREE(name); VIR_FREE(name);
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("Invalid CPU feature name")); _("Invalid CPU feature name"));
goto error; goto cleanup;
} }
for (j = 0; j < i; j++) { for (j = 0; j < i; j++) {
@ -499,7 +513,7 @@ virCPUDefParseXML(xmlNodePtr node,
_("CPU feature '%s' specified more than once"), _("CPU feature '%s' specified more than once"),
name); name);
VIR_FREE(name); VIR_FREE(name);
goto error; goto cleanup;
} }
} }
@ -542,17 +556,16 @@ virCPUDefParseXML(xmlNodePtr node,
def->cache->mode = mode; def->cache->mode = mode;
} }
VIR_STEAL_PTR(*cpu, def);
ret = 0;
cleanup: cleanup:
ctxt->node = oldnode; ctxt->node = oldnode;
VIR_FREE(fallback); VIR_FREE(fallback);
VIR_FREE(vendor_id); VIR_FREE(vendor_id);
VIR_FREE(nodes); VIR_FREE(nodes);
return def;
error:
virCPUDefFree(def); virCPUDefFree(def);
def = NULL; return ret;
goto cleanup;
} }

View File

@ -182,10 +182,11 @@ virCPUDefCopy(const virCPUDef *cpu);
virCPUDefPtr virCPUDefPtr
virCPUDefCopyWithoutModel(const virCPUDef *cpu); virCPUDefCopyWithoutModel(const virCPUDef *cpu);
virCPUDefPtr int
virCPUDefParseXML(xmlNodePtr node, virCPUDefParseXML(xmlXPathContextPtr ctxt,
xmlXPathContextPtr ctxt, const char *xpath,
virCPUType mode); virCPUType mode,
virCPUDefPtr *cpu);
bool bool
virCPUDefIsEqual(virCPUDefPtr src, virCPUDefIsEqual(virCPUDefPtr src,

View File

@ -17444,16 +17444,8 @@ virDomainDefParseXML(xmlDocPtr xml,
} }
VIR_FREE(nodes); VIR_FREE(nodes);
/* analysis of cpu handling */ if (virCPUDefParseXML(ctxt, "./cpu[1]", VIR_CPU_TYPE_GUEST, &def->cpu) < 0)
if ((node = virXPathNode("./cpu[1]", ctxt)) != NULL) { goto error;
xmlNodePtr oldnode = ctxt->node;
ctxt->node = node;
def->cpu = virCPUDefParseXML(node, ctxt, VIR_CPU_TYPE_GUEST);
ctxt->node = oldnode;
if (def->cpu == NULL)
goto error;
}
if (virDomainNumaDefCPUParseXML(def->numa, ctxt) < 0) if (virDomainNumaDefCPUParseXML(def->numa, ctxt) < 0)
goto error; goto error;

View File

@ -130,7 +130,7 @@ virCPUCompareXML(virArch arch,
if (!(doc = virXMLParseStringCtxt(xml, _("(CPU_definition)"), &ctxt))) if (!(doc = virXMLParseStringCtxt(xml, _("(CPU_definition)"), &ctxt)))
goto cleanup; goto cleanup;
if (!(cpu = virCPUDefParseXML(ctxt->node, ctxt, VIR_CPU_TYPE_AUTO))) if (virCPUDefParseXML(ctxt, NULL, VIR_CPU_TYPE_AUTO, &cpu) < 0)
goto cleanup; goto cleanup;
ret = virCPUCompare(arch, host, cpu, failIncompatible); ret = virCPUCompare(arch, host, cpu, failIncompatible);
@ -562,8 +562,7 @@ cpuBaselineXML(const char **xmlCPUs,
if (!(doc = virXMLParseStringCtxt(xmlCPUs[i], _("(CPU_definition)"), &ctxt))) if (!(doc = virXMLParseStringCtxt(xmlCPUs[i], _("(CPU_definition)"), &ctxt)))
goto error; goto error;
cpus[i] = virCPUDefParseXML(ctxt->node, ctxt, VIR_CPU_TYPE_HOST); if (virCPUDefParseXML(ctxt, NULL, VIR_CPU_TYPE_HOST, &cpus[i]) < 0)
if (cpus[i] == NULL)
goto error; goto error;
xmlXPathFreeContext(ctxt); xmlXPathFreeContext(ctxt);

View File

@ -88,7 +88,7 @@ cpuTestLoadXML(virArch arch, const char *name)
if (!(doc = virXMLParseFileCtxt(xml, &ctxt))) if (!(doc = virXMLParseFileCtxt(xml, &ctxt)))
goto cleanup; goto cleanup;
cpu = virCPUDefParseXML(ctxt->node, ctxt, VIR_CPU_TYPE_AUTO); virCPUDefParseXML(ctxt, NULL, VIR_CPU_TYPE_AUTO, &cpu);
cleanup: cleanup:
xmlXPathFreeContext(ctxt); xmlXPathFreeContext(ctxt);
@ -126,8 +126,7 @@ cpuTestLoadMultiXML(virArch arch,
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
ctxt->node = nodes[i]; ctxt->node = nodes[i];
cpus[i] = virCPUDefParseXML(nodes[i], ctxt, VIR_CPU_TYPE_HOST); if (virCPUDefParseXML(ctxt, NULL, VIR_CPU_TYPE_HOST, &cpus[i]) < 0)
if (!cpus[i])
goto cleanup_cpus; goto cleanup_cpus;
} }