[libvirt] [PATCH v2 5/8] bandwidth: Create rate update function

Michal Privoznik mprivozn at redhat.com
Tue Dec 4 19:18:59 UTC 2012


This will be used whenever a NIC with guaranteed throughput is to
be plugged into a bridge. It will adjust the average throughput of
non guaranteed NICs (classid 1:2) to meet new requirements.
---
 src/util/virnetdevbandwidth.c |   49 +++++++++++++++++++++++++++++++++++++++++
 src/util/virnetdevbandwidth.h |    6 +++++
 2 files changed, 55 insertions(+), 0 deletions(-)

diff --git a/src/util/virnetdevbandwidth.c b/src/util/virnetdevbandwidth.c
index 6c4920b..5fa8b87 100644
--- a/src/util/virnetdevbandwidth.c
+++ b/src/util/virnetdevbandwidth.c
@@ -540,3 +540,52 @@ cleanup:
     virCommandFree(cmd);
     return ret;
 }
+
+/**
+ * virNetDevBandwidthUpdateRate:
+ * @ifname: interface name
+ * @classid: ID of class to update
+ * @new_rate: new rate
+ *
+ * This function updates the 'rate' attribute of HTB class.
+ * It can be used whenever a new interface is plugged to a
+ * bridge to adjust average throughput of non guaranteed
+ * NICs.
+ *
+ * Returns 0 on success, -1 otherwise.
+ */
+int
+virNetDevBandwidthUpdateRate(const char *ifname,
+                             const char *class_id,
+                             virNetDevBandwidthPtr bandwidth,
+                             unsigned long long new_rate)
+{
+    int ret = -1;
+    virCommandPtr cmd = NULL;
+    char *rate = NULL;
+    char *ceil = NULL;
+
+    if (virAsprintf(&rate, "%llukbps", new_rate) < 0 ||
+        virAsprintf(&ceil, "%llukbps", bandwidth->in->peak ?
+                    bandwidth->in->peak :
+                    bandwidth->in->average) < 0) {
+        virReportOOMError();
+        goto cleanup;
+    }
+
+    cmd = virCommandNew(TC);
+    virCommandAddArgList(cmd, "class", "change", "dev", ifname,
+                         "classid", class_id, "htb", "rate", rate,
+                         "ceil", ceil, NULL);
+
+    if (virCommandRun(cmd, NULL) < 0)
+        goto cleanup;
+
+    ret = 0;
+
+cleanup:
+    virCommandFree(cmd);
+    VIR_FREE(rate);
+    VIR_FREE(ceil);
+    return ret;
+}
diff --git a/src/util/virnetdevbandwidth.h b/src/util/virnetdevbandwidth.h
index 3d4072d..66f9e37 100644
--- a/src/util/virnetdevbandwidth.h
+++ b/src/util/virnetdevbandwidth.h
@@ -66,4 +66,10 @@ int virNetDevBandwidthUnplug(const char *brname,
                              unsigned int id)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
 
+int virNetDevBandwidthUpdateRate(const char *ifname,
+                                 const char *class_id,
+                                 virNetDevBandwidthPtr bandwidth,
+                                 unsigned long long new_rate)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
+    ATTRIBUTE_RETURN_CHECK;
 #endif /* __VIR_NETDEV_BANDWIDTH_H__ */
-- 
1.7.8.6




More information about the libvir-list mailing list