397dc2
From 0cee78aa69f5e3317b5e4853454a108e597228e5 Mon Sep 17 00:00:00 2001
397dc2
Message-Id: <0cee78aa69f5e3317b5e4853454a108e597228e5@dist-git>
397dc2
From: Michal Privoznik <mprivozn@redhat.com>
397dc2
Date: Wed, 7 Oct 2020 18:45:33 +0200
397dc2
Subject: [PATCH] conf: Move and rename virDomainParseScaledValue()
397dc2
MIME-Version: 1.0
397dc2
Content-Type: text/plain; charset=UTF-8
397dc2
Content-Transfer-Encoding: 8bit
397dc2
397dc2
There is nothing domain specific about the function, thus it
397dc2
should not have virDomain prefix. Also, the fact that it is a
397dc2
static function makes it impossible to use from other files.
397dc2
Move the function to virxml.c and drop the 'Domain' infix.
397dc2
397dc2
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
397dc2
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
397dc2
(cherry picked from commit 04bd77a19f8312493151ce377da40577b1470a0b)
397dc2
397dc2
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1749518
397dc2
397dc2
Conflicts:
397dc2
- src/conf/domain_conf.c: Some context mismatch, and some areas
397dc2
the original commit changes don't exist in this old libvirt yet
397dc2
or the calls are in other places because of refactors.
397dc2
397dc2
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
397dc2
Message-Id: <26a847deef5941fd90f892cf5fe1443cf3fc90ca.1602087923.git.mprivozn@redhat.com>
397dc2
Reviewed-by: Ján Tomko <jtomko@redhat.com>
397dc2
---
397dc2
 src/conf/domain_conf.c   | 135 ++++++++++-----------------------------
397dc2
 src/libvirt_private.syms |   1 +
397dc2
 src/util/virxml.c        |  72 +++++++++++++++++++++
397dc2
 src/util/virxml.h        |   8 +++
397dc2
 4 files changed, 114 insertions(+), 102 deletions(-)
397dc2
397dc2
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
397dc2
index 306926b64c..484f3b4352 100644
397dc2
--- a/src/conf/domain_conf.c
397dc2
+++ b/src/conf/domain_conf.c
397dc2
@@ -10644,75 +10644,6 @@ virDomainDiskDefParseXML(virDomainXMLOptionPtr xmlopt,
397dc2
     goto cleanup;
397dc2
 }
397dc2
 
397dc2
-/**
397dc2
- * virDomainParseScaledValue:
397dc2
- * @xpath: XPath to memory amount
397dc2
- * @units_xpath: XPath to units attribute
397dc2
- * @ctxt: XPath context
397dc2
- * @val: scaled value is stored here
397dc2
- * @scale: default scale for @val
397dc2
- * @max: maximal @val allowed
397dc2
- * @required: is the value required?
397dc2
- *
397dc2
- * Parse a value located at @xpath within @ctxt, and store the
397dc2
- * result into @val. The value is scaled by units located at
397dc2
- * @units_xpath (or the 'unit' attribute under @xpath if
397dc2
- * @units_xpath is NULL). If units are not present, the default
397dc2
- * @scale is used. If @required is set, then the value must
397dc2
- * exist; otherwise, the value is optional. The resulting value
397dc2
- * is in bytes.
397dc2
- *
397dc2
- * Returns 1 on success,
397dc2
- *         0 if the value was not present and !@required,
397dc2
- *         -1 on failure after issuing error.
397dc2
- */
397dc2
-static int
397dc2
-virDomainParseScaledValue(const char *xpath,
397dc2
-                          const char *units_xpath,
397dc2
-                          xmlXPathContextPtr ctxt,
397dc2
-                          unsigned long long *val,
397dc2
-                          unsigned long long scale,
397dc2
-                          unsigned long long max,
397dc2
-                          bool required)
397dc2
-{
397dc2
-    unsigned long long bytes;
397dc2
-    g_autofree char *xpath_full = NULL;
397dc2
-    g_autofree char *unit = NULL;
397dc2
-    g_autofree char *bytes_str = NULL;
397dc2
-
397dc2
-    *val = 0;
397dc2
-    xpath_full = g_strdup_printf("string(%s)", xpath);
397dc2
-
397dc2
-    bytes_str = virXPathString(xpath_full, ctxt);
397dc2
-    if (!bytes_str) {
397dc2
-        if (!required)
397dc2
-            return 0;
397dc2
-        virReportError(VIR_ERR_XML_ERROR,
397dc2
-                       _("missing element or attribute '%s'"),
397dc2
-                       xpath);
397dc2
-        return -1;
397dc2
-    }
397dc2
-    VIR_FREE(xpath_full);
397dc2
-
397dc2
-    if (virStrToLong_ullp(bytes_str, NULL, 10, &bytes) < 0) {
397dc2
-        virReportError(VIR_ERR_XML_ERROR,
397dc2
-                       _("Invalid value '%s' for element or attribute '%s'"),
397dc2
-                       bytes_str, xpath);
397dc2
-        return -1;
397dc2
-    }
397dc2
-
397dc2
-    if (units_xpath)
397dc2
-         xpath_full = g_strdup_printf("string(%s)", units_xpath);
397dc2
-    else
397dc2
-         xpath_full = g_strdup_printf("string(%s/@unit)", xpath);
397dc2
-    unit = virXPathString(xpath_full, ctxt);
397dc2
-
397dc2
-    if (virScaleInteger(&bytes, unit, scale, max) < 0)
397dc2
-        return -1;
397dc2
-
397dc2
-    *val = bytes;
397dc2
-    return 1;
397dc2
-}
397dc2
 
397dc2
 
397dc2
 /**
397dc2
@@ -10749,8 +10680,8 @@ virDomainParseMemory(const char *xpath,
397dc2
 
397dc2
     max = virMemoryMaxValue(capped);
397dc2
 
397dc2
-    if (virDomainParseScaledValue(xpath, units_xpath, ctxt,
397dc2
-                                  &bytes, 1024, max, required) < 0)
397dc2
+    if (virParseScaledValue(xpath, units_xpath, ctxt,
397dc2
+                            &bytes, 1024, max, required) < 0)
397dc2
         return -1;
397dc2
 
397dc2
     /* Yes, we really do use kibibytes for our internal sizing.  */
397dc2
@@ -10792,9 +10723,9 @@ virDomainParseMemoryLimit(const char *xpath,
397dc2
     int ret;
397dc2
     unsigned long long bytes;
397dc2
 
397dc2
-    ret = virDomainParseScaledValue(xpath, units_xpath, ctxt, &bytes, 1024,
397dc2
-                                    VIR_DOMAIN_MEMORY_PARAM_UNLIMITED << 10,
397dc2
-                                    false);
397dc2
+    ret = virParseScaledValue(xpath, units_xpath, ctxt, &bytes, 1024,
397dc2
+                              VIR_DOMAIN_MEMORY_PARAM_UNLIMITED << 10,
397dc2
+                              false);
397dc2
 
397dc2
     if (ret < 0)
397dc2
         return -1;
397dc2
@@ -11125,9 +11056,9 @@ virDomainControllerDefParseXML(virDomainXMLOptionPtr xmlopt,
397dc2
                                  "have an address"));
397dc2
                 goto error;
397dc2
             }
397dc2
-            if ((rc = virDomainParseScaledValue("./pcihole64", NULL,
397dc2
-                                                ctxt, &bytes, 1024,
397dc2
-                                                1024ULL * ULONG_MAX, false)) < 0)
397dc2
+            if ((rc = virParseScaledValue("./pcihole64", NULL,
397dc2
+                                          ctxt, &bytes, 1024,
397dc2
+                                          1024ULL * ULONG_MAX, false)) < 0)
397dc2
                 goto error;
397dc2
 
397dc2
             if (rc == 1)
397dc2
@@ -11349,14 +11280,14 @@ virDomainFSDefParseXML(virDomainXMLOptionPtr xmlopt,
397dc2
         }
397dc2
     }
397dc2
 
397dc2
-    if (virDomainParseScaledValue("./space_hard_limit[1]",
397dc2
-                                  NULL, ctxt, &def->space_hard_limit,
397dc2
-                                  1, ULLONG_MAX, false) < 0)
397dc2
+    if (virParseScaledValue("./space_hard_limit[1]",
397dc2
+                            NULL, ctxt, &def->space_hard_limit,
397dc2
+                            1, ULLONG_MAX, false) < 0)
397dc2
         goto error;
397dc2
 
397dc2
-    if (virDomainParseScaledValue("./space_soft_limit[1]",
397dc2
-                                  NULL, ctxt, &def->space_soft_limit,
397dc2
-                                  1, ULLONG_MAX, false) < 0)
397dc2
+    if (virParseScaledValue("./space_soft_limit[1]",
397dc2
+                            NULL, ctxt, &def->space_soft_limit,
397dc2
+                            1, ULLONG_MAX, false) < 0)
397dc2
         goto error;
397dc2
 
397dc2
     cur = node->children;
397dc2
@@ -15205,8 +15136,8 @@ virDomainShmemDefParseXML(virDomainXMLOptionPtr xmlopt,
397dc2
         goto cleanup;
397dc2
     }
397dc2
 
397dc2
-    if (virDomainParseScaledValue("./size[1]", NULL, ctxt,
397dc2
-                                  &def->size, 1, ULLONG_MAX, false) < 0)
397dc2
+    if (virParseScaledValue("./size[1]", NULL, ctxt,
397dc2
+                            &def->size, 1, ULLONG_MAX, false) < 0)
397dc2
         goto cleanup;
397dc2
 
397dc2
     if ((server = virXPathNode("./server[1]", ctxt))) {
397dc2
@@ -19603,9 +19534,9 @@ virDomainCachetuneDefParseCache(xmlXPathContextPtr ctxt,
397dc2
         return -1;
397dc2
     }
397dc2
 
397dc2
-    if (virDomainParseScaledValue("./@size", "./@unit",
397dc2
-                                  ctxt, &size, 1024,
397dc2
-                                  ULLONG_MAX, true) < 0)
397dc2
+    if (virParseScaledValue("./@size", "./@unit",
397dc2
+                            ctxt, &size, 1024,
397dc2
+                            ULLONG_MAX, true) < 0)
397dc2
         return -1;
397dc2
 
397dc2
     if (virResctrlAllocSetCacheSize(alloc, level, type, cache, size) < 0)
397dc2
@@ -20712,13 +20643,13 @@ virDomainDefParseXML(xmlDocPtr xml,
397dc2
                 VIR_FREE(tmp);
397dc2
             }
397dc2
 
397dc2
-            if (virDomainParseScaledValue("./features/hpt/maxpagesize",
397dc2
-                                          NULL,
397dc2
-                                          ctxt,
397dc2
-                                          &def->hpt_maxpagesize,
397dc2
-                                          1024,
397dc2
-                                          ULLONG_MAX,
397dc2
-                                          false) < 0) {
397dc2
+            if (virParseScaledValue("./features/hpt/maxpagesize",
397dc2
+                                    NULL,
397dc2
+                                    ctxt,
397dc2
+                                    &def->hpt_maxpagesize,
397dc2
+                                    1024,
397dc2
+                                    ULLONG_MAX,
397dc2
+                                    false) < 0) {
397dc2
                 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
397dc2
                                "%s",
397dc2
                                _("Unable to parse HPT maxpagesize setting"));
397dc2
@@ -20944,13 +20875,13 @@ virDomainDefParseXML(xmlDocPtr xml,
397dc2
     }
397dc2
 
397dc2
     if (def->features[VIR_DOMAIN_FEATURE_SMM] == VIR_TRISTATE_SWITCH_ON) {
397dc2
-        int rv = virDomainParseScaledValue("string(./features/smm/tseg)",
397dc2
-                                           "string(./features/smm/tseg/@unit)",
397dc2
-                                           ctxt,
397dc2
-                                           &def->tseg_size,
397dc2
-                                           1024 * 1024, /* Defaults to mebibytes */
397dc2
-                                           ULLONG_MAX,
397dc2
-                                           false);
397dc2
+        int rv = virParseScaledValue("string(./features/smm/tseg)",
397dc2
+                                     "string(./features/smm/tseg/@unit)",
397dc2
+                                     ctxt,
397dc2
+                                     &def->tseg_size,
397dc2
+                                     1024 * 1024, /* Defaults to mebibytes */
397dc2
+                                     ULLONG_MAX,
397dc2
+                                     false);
397dc2
         if (rv < 0)
397dc2
             goto error;
397dc2
         def->tseg_specified = rv;
397dc2
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
397dc2
index 130828706a..acb25eb8c8 100644
397dc2
--- a/src/libvirt_private.syms
397dc2
+++ b/src/libvirt_private.syms
397dc2
@@ -3442,6 +3442,7 @@ virVsockSetGuestCid;
397dc2
 
397dc2
 
397dc2
 # util/virxml.h
397dc2
+virParseScaledValue;
397dc2
 virXMLCheckIllegalChars;
397dc2
 virXMLChildElementCount;
397dc2
 virXMLExtractNamespaceXML;
397dc2
diff --git a/src/util/virxml.c b/src/util/virxml.c
397dc2
index 0e66d1623b..bae2e6aca5 100644
397dc2
--- a/src/util/virxml.c
397dc2
+++ b/src/util/virxml.c
397dc2
@@ -33,6 +33,7 @@
397dc2
 #include "viralloc.h"
397dc2
 #include "virfile.h"
397dc2
 #include "virstring.h"
397dc2
+#include "virutil.h"
397dc2
 
397dc2
 #define VIR_FROM_THIS VIR_FROM_XML
397dc2
 
397dc2
@@ -1433,3 +1434,74 @@ virXMLNamespaceRegister(xmlXPathContextPtr ctxt,
397dc2
 
397dc2
     return 0;
397dc2
 }
397dc2
+
397dc2
+
397dc2
+/**
397dc2
+ * virParseScaledValue:
397dc2
+ * @xpath: XPath to memory amount
397dc2
+ * @units_xpath: XPath to units attribute
397dc2
+ * @ctxt: XPath context
397dc2
+ * @val: scaled value is stored here
397dc2
+ * @scale: default scale for @val
397dc2
+ * @max: maximal @val allowed
397dc2
+ * @required: is the value required?
397dc2
+ *
397dc2
+ * Parse a value located at @xpath within @ctxt, and store the
397dc2
+ * result into @val. The value is scaled by units located at
397dc2
+ * @units_xpath (or the 'unit' attribute under @xpath if
397dc2
+ * @units_xpath is NULL). If units are not present, the default
397dc2
+ * @scale is used. If @required is set, then the value must
397dc2
+ * exist; otherwise, the value is optional. The resulting value
397dc2
+ * is in bytes.
397dc2
+ *
397dc2
+ * Returns 1 on success,
397dc2
+ *         0 if the value was not present and !@required,
397dc2
+ *         -1 on failure after issuing error.
397dc2
+ */
397dc2
+int
397dc2
+virParseScaledValue(const char *xpath,
397dc2
+                    const char *units_xpath,
397dc2
+                    xmlXPathContextPtr ctxt,
397dc2
+                    unsigned long long *val,
397dc2
+                    unsigned long long scale,
397dc2
+                    unsigned long long max,
397dc2
+                    bool required)
397dc2
+{
397dc2
+    unsigned long long bytes;
397dc2
+    g_autofree char *xpath_full = NULL;
397dc2
+    g_autofree char *unit = NULL;
397dc2
+    g_autofree char *bytes_str = NULL;
397dc2
+
397dc2
+    *val = 0;
397dc2
+    xpath_full = g_strdup_printf("string(%s)", xpath);
397dc2
+
397dc2
+    bytes_str = virXPathString(xpath_full, ctxt);
397dc2
+    if (!bytes_str) {
397dc2
+        if (!required)
397dc2
+            return 0;
397dc2
+        virReportError(VIR_ERR_XML_ERROR,
397dc2
+                       _("missing element or attribute '%s'"),
397dc2
+                       xpath);
397dc2
+        return -1;
397dc2
+    }
397dc2
+    VIR_FREE(xpath_full);
397dc2
+
397dc2
+    if (virStrToLong_ullp(bytes_str, NULL, 10, &bytes) < 0) {
397dc2
+        virReportError(VIR_ERR_XML_ERROR,
397dc2
+                       _("Invalid value '%s' for element or attribute '%s'"),
397dc2
+                       bytes_str, xpath);
397dc2
+        return -1;
397dc2
+    }
397dc2
+
397dc2
+    if (units_xpath)
397dc2
+         xpath_full = g_strdup_printf("string(%s)", units_xpath);
397dc2
+    else
397dc2
+         xpath_full = g_strdup_printf("string(%s/@unit)", xpath);
397dc2
+    unit = virXPathString(xpath_full, ctxt);
397dc2
+
397dc2
+    if (virScaleInteger(&bytes, unit, scale, max) < 0)
397dc2
+        return -1;
397dc2
+
397dc2
+    *val = bytes;
397dc2
+    return 1;
397dc2
+}
397dc2
diff --git a/src/util/virxml.h b/src/util/virxml.h
397dc2
index 26ab9f9c2d..39b261687a 100644
397dc2
--- a/src/util/virxml.h
397dc2
+++ b/src/util/virxml.h
397dc2
@@ -269,3 +269,11 @@ virXMLNamespaceFormatNS(virBufferPtr buf,
397dc2
 int
397dc2
 virXMLNamespaceRegister(xmlXPathContextPtr ctxt,
397dc2
                         virXMLNamespace const *ns);
397dc2
+
397dc2
+int virParseScaledValue(const char *xpath,
397dc2
+                        const char *units_xpath,
397dc2
+                        xmlXPathContextPtr ctxt,
397dc2
+                        unsigned long long *val,
397dc2
+                        unsigned long long scale,
397dc2
+                        unsigned long long max,
397dc2
+                        bool required);
397dc2
-- 
397dc2
2.29.2
397dc2