render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
6d3351
From 7bdac52dd6979f76f9a75a404a76e79f7da11a06 Mon Sep 17 00:00:00 2001
6d3351
Message-Id: <7bdac52dd6979f76f9a75a404a76e79f7da11a06@dist-git>
6d3351
From: Jiri Denemark <jdenemar@redhat.com>
6d3351
Date: Tue, 23 May 2017 09:29:36 +0200
6d3351
Subject: [PATCH] conf: Refactor virCPUDefParseXML
6d3351
6d3351
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
6d3351
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
6d3351
(cherry picked from commit 702013f3b3ad8bd28c326058e2dd9ea8afbd1e61)
6d3351
6d3351
https://bugzilla.redhat.com/show_bug.cgi?id=1441662
6d3351
6d3351
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
6d3351
---
6d3351
 src/conf/cpu_conf.c    | 109 +++++++++++++++++++++++++++----------------------
6d3351
 src/conf/cpu_conf.h    |   9 ++--
6d3351
 src/conf/domain_conf.c |  12 +-----
6d3351
 src/cpu/cpu.c          |   5 +--
6d3351
 tests/cputest.c        |   5 +--
6d3351
 5 files changed, 72 insertions(+), 68 deletions(-)
6d3351
6d3351
diff --git a/src/conf/cpu_conf.c b/src/conf/cpu_conf.c
6d3351
index ffb2e83d67..da40e9ba97 100644
6d3351
--- a/src/conf/cpu_conf.c
6d3351
+++ b/src/conf/cpu_conf.c
6d3351
@@ -245,12 +245,25 @@ virCPUDefCopy(const virCPUDef *cpu)
6d3351
 }
6d3351
 
6d3351
 
6d3351
-virCPUDefPtr
6d3351
-virCPUDefParseXML(xmlNodePtr node,
6d3351
-                  xmlXPathContextPtr ctxt,
6d3351
-                  virCPUType type)
6d3351
+/*
6d3351
+ * Parses CPU definition XML from a node pointed to by @xpath. If @xpath is
6d3351
+ * NULL, the current node of @ctxt is used (i.e., it is a shortcut to ".").
6d3351
+ *
6d3351
+ * Missing <cpu> element in the XML document is not considered an error unless
6d3351
+ * @xpath is NULL in which case the function expects it was provided with a
6d3351
+ * valid <cpu> element already. In other words, the function returns success
6d3351
+ * and sets @cpu to NULL if @xpath is not NULL and the node pointed to by
6d3351
+ * @xpath is not found.
6d3351
+ *
6d3351
+ * Returns 0 on success, -1 on error.
6d3351
+ */
6d3351
+int
6d3351
+virCPUDefParseXML(xmlXPathContextPtr ctxt,
6d3351
+                  const char *xpath,
6d3351
+                  virCPUType type,
6d3351
+                  virCPUDefPtr *cpu)
6d3351
 {
6d3351
-    virCPUDefPtr def;
6d3351
+    virCPUDefPtr def = NULL;
6d3351
     xmlNodePtr *nodes = NULL;
6d3351
     xmlNodePtr oldnode = ctxt->node;
6d3351
     int n;
6d3351
@@ -258,15 +271,23 @@ virCPUDefParseXML(xmlNodePtr node,
6d3351
     char *cpuMode;
6d3351
     char *fallback = NULL;
6d3351
     char *vendor_id = NULL;
6d3351
+    int ret = -1;
6d3351
 
6d3351
-    if (!xmlStrEqual(node->name, BAD_CAST "cpu")) {
6d3351
+    *cpu = NULL;
6d3351
+
6d3351
+    if (xpath && !(ctxt->node = virXPathNode(xpath, ctxt))) {
6d3351
+        ret = 0;
6d3351
+        goto cleanup;
6d3351
+    }
6d3351
+
6d3351
+    if (!xmlStrEqual(ctxt->node->name, BAD_CAST "cpu")) {
6d3351
         virReportError(VIR_ERR_XML_ERROR, "%s",
6d3351
                        _("XML does not contain expected 'cpu' element"));
6d3351
-        return NULL;
6d3351
+        goto cleanup;
6d3351
     }
6d3351
 
6d3351
     if (VIR_ALLOC(def) < 0)
6d3351
-        return NULL;
6d3351
+        goto cleanup;
6d3351
 
6d3351
     if (type == VIR_CPU_TYPE_AUTO) {
6d3351
         if (virXPathBoolean("boolean(./arch)", ctxt)) {
6d3351
@@ -274,7 +295,7 @@ virCPUDefParseXML(xmlNodePtr node,
6d3351
                 virReportError(VIR_ERR_XML_ERROR, "%s",
6d3351
                                _("'arch' element cannot be used inside 'cpu'"
6d3351
                                  " element with 'match' attribute'"));
6d3351
-                goto error;
6d3351
+                goto cleanup;
6d3351
             }
6d3351
             def->type = VIR_CPU_TYPE_HOST;
6d3351
         } else {
6d3351
@@ -284,12 +305,12 @@ virCPUDefParseXML(xmlNodePtr node,
6d3351
         def->type = type;
6d3351
     }
6d3351
 
6d3351
-    if ((cpuMode = virXMLPropString(node, "mode"))) {
6d3351
+    if ((cpuMode = virXMLPropString(ctxt->node, "mode"))) {
6d3351
         if (def->type == VIR_CPU_TYPE_HOST) {
6d3351
             VIR_FREE(cpuMode);
6d3351
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6d3351
                            _("Attribute mode is only allowed for guest CPU"));
6d3351
-            goto error;
6d3351
+            goto cleanup;
6d3351
         } else {
6d3351
             def->mode = virCPUModeTypeFromString(cpuMode);
6d3351
 
6d3351
@@ -298,7 +319,7 @@ virCPUDefParseXML(xmlNodePtr node,
6d3351
                                _("Invalid mode attribute '%s'"),
6d3351
                                cpuMode);
6d3351
                 VIR_FREE(cpuMode);
6d3351
-                goto error;
6d3351
+                goto cleanup;
6d3351
             }
6d3351
             VIR_FREE(cpuMode);
6d3351
         }
6d3351
@@ -310,7 +331,7 @@ virCPUDefParseXML(xmlNodePtr node,
6d3351
     }
6d3351
 
6d3351
     if (def->type == VIR_CPU_TYPE_GUEST) {
6d3351
-        char *match = virXMLPropString(node, "match");
6d3351
+        char *match = virXMLPropString(ctxt->node, "match");
6d3351
         char *check;
6d3351
 
6d3351
         if (!match) {
6d3351
@@ -326,11 +347,11 @@ virCPUDefParseXML(xmlNodePtr node,
6d3351
                 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6d3351
                                _("Invalid match attribute for CPU "
6d3351
                                  "specification"));
6d3351
-                goto error;
6d3351
+                goto cleanup;
6d3351
             }
6d3351
         }
6d3351
 
6d3351
-        if ((check = virXMLPropString(node, "check"))) {
6d3351
+        if ((check = virXMLPropString(ctxt->node, "check"))) {
6d3351
             int value = virCPUCheckTypeFromString(check);
6d3351
             VIR_FREE(check);
6d3351
 
6d3351
@@ -338,7 +359,7 @@ virCPUDefParseXML(xmlNodePtr node,
6d3351
                 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6d3351
                                _("Invalid check attribute for CPU "
6d3351
                                  "specification"));
6d3351
-                goto error;
6d3351
+                goto cleanup;
6d3351
             }
6d3351
             def->check = value;
6d3351
         }
6d3351
@@ -349,13 +370,13 @@ virCPUDefParseXML(xmlNodePtr node,
6d3351
         if (!arch) {
6d3351
             virReportError(VIR_ERR_XML_ERROR, "%s",
6d3351
                            _("Missing CPU architecture"));
6d3351
-            goto error;
6d3351
+            goto cleanup;
6d3351
         }
6d3351
         if ((def->arch = virArchFromString(arch)) == VIR_ARCH_NONE) {
6d3351
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
6d3351
                            _("Unknown architecture %s"), arch);
6d3351
             VIR_FREE(arch);
6d3351
-            goto error;
6d3351
+            goto cleanup;
6d3351
         }
6d3351
         VIR_FREE(arch);
6d3351
     }
6d3351
@@ -364,7 +385,7 @@ virCPUDefParseXML(xmlNodePtr node,
6d3351
         def->type == VIR_CPU_TYPE_HOST) {
6d3351
         virReportError(VIR_ERR_XML_ERROR, "%s",
6d3351
                         _("Missing CPU model name"));
6d3351
-        goto error;
6d3351
+        goto cleanup;
6d3351
     }
6d3351
 
6d3351
     if (def->type == VIR_CPU_TYPE_GUEST &&
6d3351
@@ -374,7 +395,7 @@ virCPUDefParseXML(xmlNodePtr node,
6d3351
             if ((def->fallback = virCPUFallbackTypeFromString(fallback)) < 0) {
6d3351
                 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6d3351
                                _("Invalid fallback attribute"));
6d3351
-                goto error;
6d3351
+                goto cleanup;
6d3351
             }
6d3351
         }
6d3351
 
6d3351
@@ -384,14 +405,14 @@ virCPUDefParseXML(xmlNodePtr node,
6d3351
                 virReportError(VIR_ERR_XML_ERROR,
6d3351
                                _("vendor_id must be exactly %d characters long"),
6d3351
                                VIR_CPU_VENDOR_ID_LENGTH);
6d3351
-                goto error;
6d3351
+                goto cleanup;
6d3351
             }
6d3351
 
6d3351
             /* ensure that the string can be passed to qemu*/
6d3351
             if (strchr(vendor_id, ',')) {
6d3351
                     virReportError(VIR_ERR_XML_ERROR, "%s",
6d3351
                                    _("vendor id is invalid"));
6d3351
-                    goto error;
6d3351
+                    goto cleanup;
6d3351
             }
6d3351
 
6d3351
             def->vendor_id = vendor_id;
6d3351
@@ -403,61 +424,54 @@ virCPUDefParseXML(xmlNodePtr node,
6d3351
     if (def->vendor && !def->model) {
6d3351
         virReportError(VIR_ERR_XML_ERROR, "%s",
6d3351
                        _("CPU vendor specified without CPU model"));
6d3351
-        goto error;
6d3351
+        goto cleanup;
6d3351
     }
6d3351
 
6d3351
     if (virXPathNode("./topology[1]", ctxt)) {
6d3351
-        int ret;
6d3351
         unsigned long ul;
6d3351
 
6d3351
-        ret = virXPathULong("string(./topology[1]/@sockets)",
6d3351
-                            ctxt, &ul);
6d3351
-        if (ret < 0) {
6d3351
+        if (virXPathULong("string(./topology[1]/@sockets)", ctxt, &ul) < 0) {
6d3351
             virReportError(VIR_ERR_XML_ERROR, "%s",
6d3351
                            _("Missing 'sockets' attribute in CPU topology"));
6d3351
-            goto error;
6d3351
+            goto cleanup;
6d3351
         }
6d3351
         def->sockets = (unsigned int) ul;
6d3351
 
6d3351
-        ret = virXPathULong("string(./topology[1]/@cores)",
6d3351
-                            ctxt, &ul);
6d3351
-        if (ret < 0) {
6d3351
+        if (virXPathULong("string(./topology[1]/@cores)", ctxt, &ul) < 0) {
6d3351
             virReportError(VIR_ERR_XML_ERROR, "%s",
6d3351
                            _("Missing 'cores' attribute in CPU topology"));
6d3351
-            goto error;
6d3351
+            goto cleanup;
6d3351
         }
6d3351
         def->cores = (unsigned int) ul;
6d3351
 
6d3351
-        ret = virXPathULong("string(./topology[1]/@threads)",
6d3351
-                            ctxt, &ul);
6d3351
-        if (ret < 0) {
6d3351
+        if (virXPathULong("string(./topology[1]/@threads)", ctxt, &ul) < 0) {
6d3351
             virReportError(VIR_ERR_XML_ERROR, "%s",
6d3351
                            _("Missing 'threads' attribute in CPU topology"));
6d3351
-            goto error;
6d3351
+            goto cleanup;
6d3351
         }
6d3351
         def->threads = (unsigned int) ul;
6d3351
 
6d3351
         if (!def->sockets || !def->cores || !def->threads) {
6d3351
             virReportError(VIR_ERR_XML_ERROR, "%s",
6d3351
                            _("Invalid CPU topology"));
6d3351
-            goto error;
6d3351
+            goto cleanup;
6d3351
         }
6d3351
     }
6d3351
 
6d3351
     if ((n = virXPathNodeSet("./feature", ctxt, &nodes)) < 0)
6d3351
-        goto error;
6d3351
+        goto cleanup;
6d3351
 
6d3351
     if (n > 0) {
6d3351
         if (!def->model && def->mode == VIR_CPU_MODE_CUSTOM) {
6d3351
             virReportError(VIR_ERR_XML_ERROR, "%s",
6d3351
                            _("Non-empty feature list specified without "
6d3351
                              "CPU model"));
6d3351
-            goto error;
6d3351
+            goto cleanup;
6d3351
         }
6d3351
 
6d3351
         if (VIR_RESIZE_N(def->features, def->nfeatures_max,
6d3351
                          def->nfeatures, n) < 0)
6d3351
-            goto error;
6d3351
+            goto cleanup;
6d3351
 
6d3351
         def->nfeatures = n;
6d3351
     }
6d3351
@@ -480,7 +494,7 @@ virCPUDefParseXML(xmlNodePtr node,
6d3351
             if (policy < 0) {
6d3351
                 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6d3351
                                _("Invalid CPU feature policy"));
6d3351
-                goto error;
6d3351
+                goto cleanup;
6d3351
             }
6d3351
         } else {
6d3351
             policy = -1;
6d3351
@@ -490,7 +504,7 @@ virCPUDefParseXML(xmlNodePtr node,
6d3351
             VIR_FREE(name);
6d3351
             virReportError(VIR_ERR_XML_ERROR, "%s",
6d3351
                            _("Invalid CPU feature name"));
6d3351
-            goto error;
6d3351
+            goto cleanup;
6d3351
         }
6d3351
 
6d3351
         for (j = 0; j < i; j++) {
6d3351
@@ -499,7 +513,7 @@ virCPUDefParseXML(xmlNodePtr node,
6d3351
                                _("CPU feature '%s' specified more than once"),
6d3351
                                name);
6d3351
                 VIR_FREE(name);
6d3351
-                goto error;
6d3351
+                goto cleanup;
6d3351
             }
6d3351
         }
6d3351
 
6d3351
@@ -542,17 +556,16 @@ virCPUDefParseXML(xmlNodePtr node,
6d3351
         def->cache->mode = mode;
6d3351
     }
6d3351
 
6d3351
+    VIR_STEAL_PTR(*cpu, def);
6d3351
+    ret = 0;
6d3351
+
6d3351
  cleanup:
6d3351
     ctxt->node = oldnode;
6d3351
     VIR_FREE(fallback);
6d3351
     VIR_FREE(vendor_id);
6d3351
     VIR_FREE(nodes);
6d3351
-    return def;
6d3351
-
6d3351
- error:
6d3351
     virCPUDefFree(def);
6d3351
-    def = NULL;
6d3351
-    goto cleanup;
6d3351
+    return ret;
6d3351
 }
6d3351
 
6d3351
 
6d3351
diff --git a/src/conf/cpu_conf.h b/src/conf/cpu_conf.h
6d3351
index b0d891552a..b44974f47e 100644
6d3351
--- a/src/conf/cpu_conf.h
6d3351
+++ b/src/conf/cpu_conf.h
6d3351
@@ -182,10 +182,11 @@ virCPUDefCopy(const virCPUDef *cpu);
6d3351
 virCPUDefPtr
6d3351
 virCPUDefCopyWithoutModel(const virCPUDef *cpu);
6d3351
 
6d3351
-virCPUDefPtr
6d3351
-virCPUDefParseXML(xmlNodePtr node,
6d3351
-                  xmlXPathContextPtr ctxt,
6d3351
-                  virCPUType mode);
6d3351
+int
6d3351
+virCPUDefParseXML(xmlXPathContextPtr ctxt,
6d3351
+                  const char *xpath,
6d3351
+                  virCPUType mode,
6d3351
+                  virCPUDefPtr *cpu);
6d3351
 
6d3351
 bool
6d3351
 virCPUDefIsEqual(virCPUDefPtr src,
6d3351
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
6d3351
index 2197330a22..395dcc0531 100644
6d3351
--- a/src/conf/domain_conf.c
6d3351
+++ b/src/conf/domain_conf.c
6d3351
@@ -17418,16 +17418,8 @@ virDomainDefParseXML(xmlDocPtr xml,
6d3351
     }
6d3351
     VIR_FREE(nodes);
6d3351
 
6d3351
-    /* analysis of cpu handling */
6d3351
-    if ((node = virXPathNode("./cpu[1]", ctxt)) != NULL) {
6d3351
-        xmlNodePtr oldnode = ctxt->node;
6d3351
-        ctxt->node = node;
6d3351
-        def->cpu = virCPUDefParseXML(node, ctxt, VIR_CPU_TYPE_GUEST);
6d3351
-        ctxt->node = oldnode;
6d3351
-
6d3351
-        if (def->cpu == NULL)
6d3351
-            goto error;
6d3351
-    }
6d3351
+    if (virCPUDefParseXML(ctxt, "./cpu[1]", VIR_CPU_TYPE_GUEST, &def->cpu) < 0)
6d3351
+        goto error;
6d3351
 
6d3351
     if (virDomainNumaDefCPUParseXML(def->numa, ctxt) < 0)
6d3351
         goto error;
6d3351
diff --git a/src/cpu/cpu.c b/src/cpu/cpu.c
6d3351
index 702b14dbb7..96160901e1 100644
6d3351
--- a/src/cpu/cpu.c
6d3351
+++ b/src/cpu/cpu.c
6d3351
@@ -130,7 +130,7 @@ virCPUCompareXML(virArch arch,
6d3351
     if (!(doc = virXMLParseStringCtxt(xml, _("(CPU_definition)"), &ctxt)))
6d3351
         goto cleanup;
6d3351
 
6d3351
-    if (!(cpu = virCPUDefParseXML(ctxt->node, ctxt, VIR_CPU_TYPE_AUTO)))
6d3351
+    if (virCPUDefParseXML(ctxt, NULL, VIR_CPU_TYPE_AUTO, &cpu) < 0)
6d3351
         goto cleanup;
6d3351
 
6d3351
     ret = virCPUCompare(arch, host, cpu, failIncompatible);
6d3351
@@ -562,8 +562,7 @@ cpuBaselineXML(const char **xmlCPUs,
6d3351
         if (!(doc = virXMLParseStringCtxt(xmlCPUs[i], _("(CPU_definition)"), &ctxt)))
6d3351
             goto error;
6d3351
 
6d3351
-        cpus[i] = virCPUDefParseXML(ctxt->node, ctxt, VIR_CPU_TYPE_HOST);
6d3351
-        if (cpus[i] == NULL)
6d3351
+        if (virCPUDefParseXML(ctxt, NULL, VIR_CPU_TYPE_HOST, &cpus[i]) < 0)
6d3351
             goto error;
6d3351
 
6d3351
         xmlXPathFreeContext(ctxt);
6d3351
diff --git a/tests/cputest.c b/tests/cputest.c
6d3351
index efa891dc18..97b34de9ed 100644
6d3351
--- a/tests/cputest.c
6d3351
+++ b/tests/cputest.c
6d3351
@@ -88,7 +88,7 @@ cpuTestLoadXML(virArch arch, const char *name)
6d3351
     if (!(doc = virXMLParseFileCtxt(xml, &ctxt)))
6d3351
         goto cleanup;
6d3351
 
6d3351
-    cpu = virCPUDefParseXML(ctxt->node, ctxt, VIR_CPU_TYPE_AUTO);
6d3351
+    virCPUDefParseXML(ctxt, NULL, VIR_CPU_TYPE_AUTO, &cpu);
6d3351
 
6d3351
  cleanup:
6d3351
     xmlXPathFreeContext(ctxt);
6d3351
@@ -126,8 +126,7 @@ cpuTestLoadMultiXML(virArch arch,
6d3351
 
6d3351
     for (i = 0; i < n; i++) {
6d3351
         ctxt->node = nodes[i];
6d3351
-        cpus[i] = virCPUDefParseXML(nodes[i], ctxt, VIR_CPU_TYPE_HOST);
6d3351
-        if (!cpus[i])
6d3351
+        if (virCPUDefParseXML(ctxt, NULL, VIR_CPU_TYPE_HOST, &cpus[i]) < 0)
6d3351
             goto cleanup_cpus;
6d3351
     }
6d3351
 
6d3351
-- 
6d3351
2.13.1
6d3351