render / rpms / libvirt

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