[libvirt] [PATCH v4 4/7] bandwidth: Create format functions

Michal Privoznik mprivozn at redhat.com
Fri Jul 22 14:07:26 UTC 2011


---
 src/conf/domain_conf.c   |    3 ++
 src/conf/network_conf.c  |    3 ++
 src/libvirt_private.syms |    1 +
 src/util/network.c       |   72 ++++++++++++++++++++++++++++++++++++++++++++++
 src/util/network.h       |    3 ++
 5 files changed, 82 insertions(+), 0 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index e11ad98..072c970 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -8855,6 +8855,9 @@ virDomainNetDefFormat(virBufferPtr buf,
         virBufferAddLit(buf,   "      </tune>\n");
     }
 
+    if (virBandwidthDefFormat(buf, def->bandwidth, "      ") < 0)
+        return -1;
+
     if (virDomainDeviceInfoFormat(buf, &def->info, flags) < 0)
         return -1;
 
diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index 01c094c..1ef80dc 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -1329,6 +1329,9 @@ char *virNetworkDefFormat(const virNetworkDefPtr def)
     if (virNetworkDNSDefFormat(&buf, def->dns) < 0)
         goto error;
 
+    if (virBandwidthDefFormat(&buf, def->bandwidth, "  ") < 0)
+        goto error;
+
     for (ii = 0; ii < def->nips; ii++) {
         if (virNetworkIpDefFormat(&buf, &def->ips[ii]) < 0)
             goto error;
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 832c8a6..188d647 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -707,6 +707,7 @@ nlComm;
 
 
 # network.h
+virBandwidthDefFormat;
 virBandwidthDefFree;
 virBandwidthDefParseNode;
 virSocketAddrBroadcast;
diff --git a/src/util/network.c b/src/util/network.c
index 2ba6f76..5639219 100644
--- a/src/util/network.c
+++ b/src/util/network.c
@@ -1030,3 +1030,75 @@ virBandwidthDefFree(virBandwidthPtr def)
     VIR_FREE(def->out);
     VIR_FREE(def);
 }
+
+static int
+virBandwidthChildDefFormat(virBufferPtr buf,
+                           virRatePtr def,
+                           const char *elem_name)
+{
+    if (!buf || !def || !elem_name)
+        return -1;
+
+    if (def->average) {
+        virBufferAsprintf(buf, "<%s average='%llu'", elem_name, def->average);
+
+        if (def->peak)
+            virBufferAsprintf(buf, " peak='%llu'", def->peak);
+
+        if (def->burst)
+            virBufferAsprintf(buf, " burst='%llu'", def->burst);
+        virBufferAddLit(buf, "/>\n");
+    }
+
+    return 0;
+}
+
+/**
+ * virBandwidthDefFormat:
+ * @buf: Buffer to print to
+ * @def: Data source
+ * @indent: prepend all lines printed with this
+ *
+ * Formats bandwidth and prepend each line with @indent.
+ * Passing NULL to @indent is equivalent passing "".
+ *
+ * Returns 0 on success, else -1.
+ */
+int
+virBandwidthDefFormat(virBufferPtr buf,
+                      virBandwidthPtr def,
+                      const char *indent)
+{
+    int ret = -1;
+
+    if (!buf)
+        goto cleanup;
+
+    if (!def) {
+        ret = 0;
+        goto cleanup;
+    }
+
+    if (!indent)
+        indent = "";
+
+    virBufferAsprintf(buf, "%s<bandwidth>\n", indent);
+    if (def->in) {
+        virBufferAsprintf(buf, "%s  ", indent);
+        if (virBandwidthChildDefFormat(buf, def->in, "inbound") < 0)
+            goto cleanup;
+    }
+
+    if (def->out) {
+        virBufferAsprintf(buf, "%s  ", indent);
+        if (virBandwidthChildDefFormat(buf, def->out, "outbound") < 0)
+            goto cleanup;
+    }
+
+    virBufferAsprintf(buf, "%s</bandwidth>\n", indent);
+
+    ret = 0;
+
+cleanup:
+    return ret;
+}
diff --git a/src/util/network.h b/src/util/network.h
index 4d35388..d0181fd 100644
--- a/src/util/network.h
+++ b/src/util/network.h
@@ -152,4 +152,7 @@ virVirtualPortProfileFormat(virBufferPtr buf,
 
 virBandwidthPtr virBandwidthDefParseNode(xmlNodePtr node);
 void virBandwidthDefFree(virBandwidthPtr def);
+int virBandwidthDefFormat(virBufferPtr buf,
+                          virBandwidthPtr def,
+                          const char *indent);
 #endif /* __VIR_NETWORK_H__ */
-- 
1.7.5.rc3




More information about the libvir-list mailing list