[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH 1/2] xenconfig: support parsing and formatting vif bandwidth



Both xm and xl config have long supported specifying vif rate
limiting, e.g.

vif = [ 'mac=00:16:3E:74:3d:76,bridge=br0,rate=10MB/s' ]

Add support for mapping rate to and from <bandwidth> in the xenconfig
parser and formatter. rate is mapped to the required 'average' attribute
of the <outbound> element, e.g.

  <interface type='bridge'>
    ...
    <bandwidth>
      <outbound average='10240'/>
    </bandwidth>
  </interface>

Also add a unit test to check the conversion logic.

Signed-off-by: Jim Fehlig <jfehlig@xxxxxxxx>
---

I used a bit of code from libxlu_vif.c to implement xenParseVifRate()
instead of using the libxlutil lib directly, since in theory rate limiting
applies to the old xen driver (no libxl) as well.

 src/xenconfig/xen_common.c           | 77 ++++++++++++++++++++++++++++++++++++
 tests/xlconfigdata/test-vif-rate.cfg | 26 ++++++++++++
 tests/xlconfigdata/test-vif-rate.xml | 57 ++++++++++++++++++++++++++
 tests/xlconfigtest.c                 |  1 +
 4 files changed, 161 insertions(+)

diff --git a/src/xenconfig/xen_common.c b/src/xenconfig/xen_common.c
index 54f5791..b9f0344 100644
--- a/src/xenconfig/xen_common.c
+++ b/src/xenconfig/xen_common.c
@@ -28,6 +28,8 @@
 
 #include <config.h>
 
+#include <regex.h>
+
 #include "internal.h"
 #include "virerror.h"
 #include "virconf.h"
@@ -803,6 +805,46 @@ xenParseCharDev(virConfPtr conf, virDomainDefPtr def)
 }
 
 
+static const char *vif_bytes_per_sec_re = "^[0-9]+[GMK]?[Bb]/s$";
+
+static int
+xenParseVifRate(char *rate, unsigned long long *kbytes_per_sec)
+{
+    regex_t rec;
+    char *suffix;
+    unsigned long long tmp;
+    int ret = -1;
+
+    regcomp(&rec, vif_bytes_per_sec_re, REG_EXTENDED|REG_NOSUB);
+    if (regexec(&rec, rate, 0, NULL, 0)) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Invalid rate '%s' specified"), rate);
+        goto cleanup;
+    }
+
+    if (virStrToLong_ull(rate, &suffix, 10, &tmp)) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Failed to parse rate '%s'"), rate);
+        goto cleanup;
+    }
+
+    if (*suffix == 'G')
+       tmp *= 1024 * 1024;
+    else if (*suffix == 'M')
+       tmp *= 1024;
+
+    if (*suffix == 'b' || *(suffix + 1) == 'b')
+       tmp /= 8;
+
+    *kbytes_per_sec = tmp;
+    ret = 0;
+
+ cleanup:
+    regfree(&rec);
+    return ret;
+}
+
+
 static int
 xenParseVif(virConfPtr conf, virDomainDefPtr def)
 {
@@ -819,6 +861,7 @@ xenParseVif(virConfPtr conf, virDomainDefPtr def)
             char mac[18];
             char bridge[50];
             char vifname[50];
+            char rate[50];
             char *key;
 
             bridge[0] = '\0';
@@ -827,6 +870,7 @@ xenParseVif(virConfPtr conf, virDomainDefPtr def)
             model[0] = '\0';
             type[0] = '\0';
             vifname[0] = '\0';
+            rate[0] = '\0';
 
             if ((list->type != VIR_CONF_STRING) || (list->str == NULL))
                 goto skipnic;
@@ -892,6 +936,13 @@ xenParseVif(virConfPtr conf, virDomainDefPtr def)
                                        _("IP %s too big for destination"), 
data);
                         goto skipnic;
                     }
+                } else if (STRPREFIX(key, "rate=")) {
+                    int len = nextkey ? (nextkey - data) : sizeof(rate) - 1;
+                    if (virStrncpy(rate, data, len, sizeof(rate)) == NULL) {
+                        virReportError(VIR_ERR_INTERNAL_ERROR,
+                                       _("rate %s too big for destination"), 
data);
+                        goto skipnic;
+                    }
                 }
 
                 while (nextkey && (nextkey[0] == ',' ||
@@ -942,6 +993,29 @@ xenParseVif(virConfPtr conf, virDomainDefPtr def)
                 VIR_STRDUP(net->ifname, vifname) < 0)
                 goto cleanup;
 
+            if (rate[0]) {
+                char *p;
+                virNetDevBandwidthPtr bandwidth;
+                unsigned long long kbytes_per_sec;
+
+                p = strchr(rate, '@');
+                if (p != NULL)
+                    *p = 0;
+
+                if (xenParseVifRate(rate, &kbytes_per_sec) < 0)
+                    goto cleanup;
+
+                if (VIR_ALLOC(bandwidth) < 0)
+                    goto cleanup;
+                if (VIR_ALLOC(bandwidth->out) < 0) {
+                    VIR_FREE(bandwidth);
+                    goto cleanup;
+                }
+
+                bandwidth->out->average = kbytes_per_sec;
+                net->bandwidth = bandwidth;
+            }
+
             if (VIR_APPEND_ELEMENT(def->nets, def->nnets, net) < 0)
                 goto cleanup;
 
@@ -1184,6 +1258,9 @@ xenFormatNet(virConnectPtr conn,
         virBufferAsprintf(&buf, ",vifname=%s",
                           net->ifname);
 
+    if (net->bandwidth && net->bandwidth->out && net->bandwidth->out->average)
+        virBufferAsprintf(&buf, ",rate=%lluKB/s", 
net->bandwidth->out->average);
+
     if (virBufferCheckError(&buf) < 0)
         goto cleanup;
 
diff --git a/tests/xlconfigdata/test-vif-rate.cfg 
b/tests/xlconfigdata/test-vif-rate.cfg
new file mode 100644
index 0000000..c5484dc
--- /dev/null
+++ b/tests/xlconfigdata/test-vif-rate.cfg
@@ -0,0 +1,26 @@
+name = "XenGuest2"
+uuid = "c7a5fdb2-cdaf-9455-926a-d65c16db1809"
+maxmem = 579
+memory = 394
+vcpus = 1
+pae = 1
+acpi = 1
+apic = 1
+hap = 0
+viridian = 0
+rtc_timeoffset = 0
+localtime = 0
+on_poweroff = "destroy"
+on_reboot = "restart"
+on_crash = "restart"
+device_model = "/usr/lib/xen/bin/qemu-dm"
+sdl = 0
+vnc = 1
+vncunused = 1
+vnclisten = "127.0.0.1"
+vif = [ 
"mac=00:16:3e:66:92:9c,bridge=xenbr1,script=vif-bridge,model=e1000,rate=10240KB/s"
 ]
+parallel = "none"
+serial = "none"
+builder = "hvm"
+boot = "d"
+disk = [ "/dev/HostVG/XenGuest2,raw,hda,w,backendtype=phy", 
"/var/lib/libvirt/images/XenGuest2-home,qcow2,hdb,w,backendtype=qdisk", 
"/root/boot.iso,raw,hdc,r,backendtype=qdisk,devtype=cdrom" ]
diff --git a/tests/xlconfigdata/test-vif-rate.xml 
b/tests/xlconfigdata/test-vif-rate.xml
new file mode 100644
index 0000000..29f0f79
--- /dev/null
+++ b/tests/xlconfigdata/test-vif-rate.xml
@@ -0,0 +1,57 @@
+<domain type='xen'>
+  <name>XenGuest2</name>
+  <uuid>c7a5fdb2-cdaf-9455-926a-d65c16db1809</uuid>
+  <memory unit='KiB'>592896</memory>
+  <currentMemory unit='KiB'>403456</currentMemory>
+  <vcpu placement='static'>1</vcpu>
+  <os>
+    <type arch='x86_64' machine='xenfv'>hvm</type>
+    <loader type='rom'>/usr/lib/xen/boot/hvmloader</loader>
+    <boot dev='cdrom'/>
+  </os>
+  <features>
+    <acpi/>
+    <apic/>
+    <pae/>
+  </features>
+  <clock offset='variable' adjustment='0' basis='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>restart</on_crash>
+  <devices>
+    <emulator>/usr/lib/xen/bin/qemu-dm</emulator>
+    <disk type='block' device='disk'>
+      <driver name='phy' type='raw'/>
+      <source dev='/dev/HostVG/XenGuest2'/>
+      <target dev='hda' bus='ide'/>
+      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
+    </disk>
+    <disk type='file' device='disk'>
+      <driver name='qemu' type='qcow2'/>
+      <source file='/var/lib/libvirt/images/XenGuest2-home'/>
+      <target dev='hdb' bus='ide'/>
+      <address type='drive' controller='0' bus='0' target='0' unit='1'/>
+    </disk>
+    <disk type='file' device='cdrom'>
+      <driver name='qemu' type='raw'/>
+      <source file='/root/boot.iso'/>
+      <target dev='hdc' bus='ide'/>
+      <readonly/>
+      <address type='drive' controller='0' bus='1' target='0' unit='0'/>
+    </disk>
+    <interface type='bridge'>
+      <mac address='00:16:3e:66:92:9c'/>
+      <source bridge='xenbr1'/>
+      <bandwidth>
+        <outbound average='10240'/>
+      </bandwidth>
+      <script path='vif-bridge'/>
+      <model type='e1000'/>
+    </interface>
+    <input type='mouse' bus='ps2'/>
+    <input type='keyboard' bus='ps2'/>
+    <graphics type='vnc' port='-1' autoport='yes' listen='127.0.0.1'>
+      <listen type='address' address='127.0.0.1'/>
+    </graphics>
+  </devices>
+</domain>
diff --git a/tests/xlconfigtest.c b/tests/xlconfigtest.c
index 1db11e7..e997009 100644
--- a/tests/xlconfigtest.c
+++ b/tests/xlconfigtest.c
@@ -196,6 +196,7 @@ mymain(void)
     DO_TEST("new-disk");
     DO_TEST("spice");
     DO_TEST("spice-features");
+    DO_TEST("vif-rate");
 
 #ifdef LIBXL_HAVE_BUILDINFO_USBDEVICE_LIST
     DO_TEST("fullvirt-multiusb");
-- 
2.1.4


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.