]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
bandwidth: Add parsing and free functions
authorMichal Privoznik <mprivozn@redhat.com>
Fri, 22 Jul 2011 14:07:25 +0000 (16:07 +0200)
committerDaniel Veillard <veillard@redhat.com>
Mon, 25 Jul 2011 05:49:33 +0000 (13:49 +0800)
These functions parse given XML node and return pointer to the
output. Unknown elements are silently ignored. Attributes must
be integer and must fit in unsigned long long.

Free function frees elements of virBandwidth structure.

cfg.mk
src/conf/domain_conf.c
src/conf/domain_conf.h
src/conf/network_conf.c
src/conf/network_conf.h
src/libvirt_private.syms
src/util/network.c
src/util/network.h

diff --git a/cfg.mk b/cfg.mk
index 977ea87a0b2b2c4b8e015ac356b87ca8f13a411d..2eb73e69dbd2039c315a8012d7cf2953056858c5 100644 (file)
--- a/cfg.mk
+++ b/cfg.mk
@@ -84,6 +84,7 @@ useless_free_options =                                \
   --name=qemuMigrationCookieFree                \
   --name=qemuMigrationCookieGraphicsFree        \
   --name=sexpr_free                            \
+  --name=virBandwidthDefFree                   \
   --name=virBitmapFree                          \
   --name=virCPUDefFree                         \
   --name=virCapabilitiesFree                   \
index 919a75a76299afe573674e10934dafd0d7dad6e7..e11ad987a6d56020ec2e2e6488ab6b4e83b39d65 100644 (file)
@@ -810,6 +810,8 @@ void virDomainNetDefFree(virDomainNetDefPtr def)
     VIR_FREE(def->filter);
     virNWFilterHashTableFree(def->filterparams);
 
+    virBandwidthDefFree(def->bandwidth);
+
     VIR_FREE(def);
 }
 
@@ -2823,6 +2825,9 @@ virDomainNetDefParseXML(virCapsPtr caps,
                        xmlStrEqual(cur->name, BAD_CAST "actual")) {
                 if (virDomainActualNetDefParseXML(cur, ctxt, &actual) < 0)
                     goto error;
+            } else if (xmlStrEqual(cur->name, BAD_CAST "bandwidth")) {
+                if (!(def->bandwidth = virBandwidthDefParseNode(cur)))
+                    goto error;
             }
         }
         cur = cur->next;
index 551946b79b798a29192e94ed7c73ea8093a48295..212f0c5f0f78a4049c6e15154fce16a60153e144 100644 (file)
@@ -425,6 +425,7 @@ struct _virDomainNetDef {
     virDomainDeviceInfo info;
     char *filter;
     virNWFilterHashTablePtr filterparams;
+    virBandwidthPtr bandwidth;
 };
 
 enum virDomainChrDeviceType {
index 860eea3e42c19aea87760cbb79bc2020577764c2..01c094cba26e2eafcee3def4d49bf95138c3deda 100644 (file)
@@ -167,6 +167,8 @@ void virNetworkDefFree(virNetworkDefPtr def)
 
     virNetworkDNSDefFree(def->dns);
 
+    virBandwidthDefFree(def->bandwidth);
+
     VIR_FREE(def);
 }
 
@@ -814,6 +816,7 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt)
     int nIps, nPortGroups, nForwardIfs;
     char *forwardDev = NULL;
     xmlNodePtr save = ctxt->node;
+    xmlNodePtr bandwidthNode = NULL;
 
     if (VIR_ALLOC(def) < 0) {
         virReportOOMError();
@@ -848,6 +851,10 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt)
     /* Parse network domain information */
     def->domain = virXPathString("string(./domain[1]/@name)", ctxt);
 
+    if ((bandwidthNode = virXPathNode("./bandwidth", ctxt)) != NULL &&
+        (def->bandwidth = virBandwidthDefParseNode(bandwidthNode)) == NULL)
+        goto error;
+
     /* Parse bridge information */
     def->bridge = virXPathString("string(./bridge[1]/@name)", ctxt);
     stp = virXPathString("string(./bridge[1]/@stp)", ctxt);
index 92cc03eee0d5479a01526f2966565e19d1aeef9d..6d0845e36c8e31b5e5bef73cb91631de4874f4ba 100644 (file)
@@ -154,6 +154,7 @@ struct _virNetworkDef {
 
     size_t nPortGroups;
     virPortGroupDefPtr portGroups;
+    virBandwidthPtr bandwidth;
 };
 
 typedef struct _virNetworkObj virNetworkObj;
index a3b78ad35310384b2b80e99a906a4c0f23ed8bc8..5d7faa08776005aac114fa77417a2579bc78a7c7 100644 (file)
@@ -710,6 +710,8 @@ nlComm;
 
 
 # network.h
+virBandwidthDefFree;
+virBandwidthDefParseNode;
 virSocketAddrBroadcast;
 virSocketAddrBroadcastByPrefix;
 virSocketAddrIsNetmask;
index 64f5645a913abbf317ac305ae93b2a9ae88c3fd2..2ba6f768fba622058d53656e63d22ac87303f9f5 100644 (file)
@@ -883,3 +883,150 @@ virVirtualPortProfileFormat(virBufferPtr buf,
 
     virBufferAsprintf(buf, "%s</virtualport>\n", indent);
 }
+
+static int
+virBandwidthParseChildDefNode(xmlNodePtr node, virRatePtr rate)
+{
+    int ret = -1;
+    char *average = NULL;
+    char *peak = NULL;
+    char *burst = NULL;
+
+    if (!node || !rate) {
+        virSocketError(VIR_ERR_INVALID_ARG, "%s",
+                       _("invalid argument supplied"));
+        return -1;
+    }
+
+    average = virXMLPropString(node, "average");
+    peak = virXMLPropString(node, "peak");
+    burst = virXMLPropString(node, "burst");
+
+    if (average) {
+        if (virStrToLong_ull(average, NULL, 10, &rate->average) < 0) {
+            virSocketError(VIR_ERR_CONFIG_UNSUPPORTED,
+                           _("could not convert %s"),
+                           average);
+            goto cleanup;
+        }
+    } else {
+        virSocketError(VIR_ERR_XML_DETAIL, "%s",
+                       _("Missing mandatory average attribute"));
+        goto cleanup;
+    }
+
+    if (peak && virStrToLong_ull(peak, NULL, 10, &rate->peak) < 0) {
+        virSocketError(VIR_ERR_CONFIG_UNSUPPORTED,
+                       _("could not convert %s"),
+                       peak);
+        goto cleanup;
+    }
+
+    if (burst && virStrToLong_ull(burst, NULL, 10, &rate->burst) < 0) {
+        virSocketError(VIR_ERR_CONFIG_UNSUPPORTED,
+                       _("could not convert %s"),
+                       burst);
+        goto cleanup;
+    }
+
+    ret = 0;
+
+cleanup:
+    VIR_FREE(average);
+    VIR_FREE(peak);
+    VIR_FREE(burst);
+
+    return ret;
+}
+
+/**
+ * virBandwidthDefParseNode:
+ * @node: XML node
+ *
+ * Parse bandwidth XML and return pointer to structure
+ *
+ * Returns !NULL on success, NULL on error.
+ */
+virBandwidthPtr
+virBandwidthDefParseNode(xmlNodePtr node)
+{
+    virBandwidthPtr def = NULL;
+    xmlNodePtr cur = node->children;
+    xmlNodePtr in = NULL, out = NULL;
+
+    if (VIR_ALLOC(def) < 0) {
+        virReportOOMError();
+        return NULL;
+    }
+
+    if (!node || !xmlStrEqual(node->name, BAD_CAST "bandwidth")) {
+        virSocketError(VIR_ERR_INVALID_ARG, "%s",
+                       _("invalid argument supplied"));
+        goto error;
+    }
+
+    while (cur) {
+        if (cur->type == XML_ELEMENT_NODE) {
+            if (xmlStrEqual(cur->name, BAD_CAST "inbound")) {
+                if (in) {
+                    virSocketError(VIR_ERR_XML_DETAIL, "%s",
+                                   _("Only one child <inbound> "
+                                     "element allowed"));
+                    goto error;
+                }
+                in = cur;
+            } else if (xmlStrEqual(cur->name, BAD_CAST "outbound")) {
+                if (out) {
+                    virSocketError(VIR_ERR_XML_DETAIL, "%s",
+                                   _("Only one child <outbound> "
+                                     "element allowed"));
+                    goto error;
+                }
+                out = cur;
+            }
+            /* Silently ignore unknown elements */
+        }
+        cur = cur->next;
+    }
+
+    if (in) {
+        if (VIR_ALLOC(def->in) < 0) {
+            virReportOOMError();
+            goto error;
+        }
+
+        if (virBandwidthParseChildDefNode(in, def->in) < 0) {
+            /* helper reported error for us */
+            goto error;
+        }
+    }
+
+    if (out) {
+        if (VIR_ALLOC(def->out) < 0) {
+            virReportOOMError();
+            goto error;
+        }
+
+        if (virBandwidthParseChildDefNode(out, def->out) < 0) {
+            /* helper reported error for us */
+            goto error;
+        }
+    }
+
+    return def;
+
+error:
+    virBandwidthDefFree(def);
+    return NULL;
+}
+
+void
+virBandwidthDefFree(virBandwidthPtr def)
+{
+    if (!def)
+        return;
+
+    VIR_FREE(def->in);
+    VIR_FREE(def->out);
+    VIR_FREE(def);
+}
index 3c090d83991301429a926243162782c3fad9cdf7..4d35388c617affcc3d8b844141487bc602a44332 100644 (file)
@@ -150,4 +150,6 @@ virVirtualPortProfileFormat(virBufferPtr buf,
                             virVirtualPortProfileParamsPtr virtPort,
                             const char *indent);
 
+virBandwidthPtr virBandwidthDefParseNode(xmlNodePtr node);
+void virBandwidthDefFree(virBandwidthPtr def);
 #endif /* __VIR_NETWORK_H__ */