render / rpms / libvirt

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