[lxc-devel] [PATCH] traffic control setup

Farcasi Ana-Maria farcasia at gmail.com
Sat May 7 11:30:35 UTC 2011


Hello,

This commit adds traffic control support (download and upload limit rate)
for lxc containers. At the moment, the changes apply just for veth devices.
In order to do this, we have attached a new structure (lxc_traffic_control)
to struct lxc_netdev. This structure contains values for download and upload
rate limits and can be extended with other traffic control options.

In the configuration file the user has to complete the options for limiting
traffic and to attach a script that handles the limitation (in the upscript
option). We have attached here an example of script - lxc_upscript.sh. The
lines in the configuration file that deal with limitation are:
lxc.network.download.rate
lxc.network.upload.rate
lxc.network.up.script

We have also attached an example of configuration file - conf_example.conf.

The body of the patch is:

>From c4529b9c03cfdc11e11fa674bd240e2566c2bf05 Mon Sep 17 00:00:00 2001
From: Ana-Maria Farcasi <farcasia at gmail.com>
Date: Sat, 7 May 2011 03:52:47 -0700
Subject: [PATCH] traffic control setup


Signed-off-by: Ana-Maria Farcasi <farcasia at gmail.com>
Signed-off-by: Irina Presa <irina.presa at gmail.com>
---
 src/lxc/conf.c    |   27 +++++++++++++++++++++++++--
 src/lxc/conf.h    |    6 ++++++
 src/lxc/confile.c |   24 ++++++++++++++++++++++++
 3 files changed, 55 insertions(+), 2 deletions(-)

diff --git a/src/lxc/conf.c b/src/lxc/conf.c
index ae5b259..32445da 100644
--- a/src/lxc/conf.c
+++ b/src/lxc/conf.c
@@ -67,6 +67,7 @@ lxc_log_define(lxc_conf, lxc);
 #define MAXINDEXLEN 20
 #define MAXMTULEN   16
 #define MAXLINELEN  128
+#define MAXTCARGSIZE 80

 #ifndef MS_DIRSYNC
 #define MS_DIRSYNC  128
@@ -1360,7 +1361,15 @@ static int instanciate_veth(struct lxc_handler
*handler, struct lxc_netdev *netd
 {
     char veth1buf[IFNAMSIZ], *veth1;
     char veth2buf[IFNAMSIZ], *veth2;
-    int err;
+    int err, ret_tc;
+    char *tc;
+
+    tc = malloc(MAXTCARGSIZE);
+    if (!tc) {
+        ERROR("failed to allocate memory");
+        return -1;
+    }
+    memset(tc, 0, MAXTCARGSIZE);

     if (netdev->priv.veth_attr.pair)
         veth1 = netdev->priv.veth_attr.pair;
@@ -1416,9 +1425,21 @@ static int instanciate_veth(struct lxc_handler
*handler, struct lxc_netdev *netd
         goto out_delete;
     }

+    ret_tc = 0;
+
+    if (netdev->tc.download_rate) {
+        ret_tc += snprintf(tc + ret_tc, MAXTCARGSIZE, "%s %s ",
+                "downloadRate", netdev->tc.download_rate);
+    }
+
+    if (netdev->tc.upload_rate) {
+        ret_tc += snprintf(tc + ret_tc, MAXTCARGSIZE, "%s %s ",
+                "uploadRate", netdev->tc.upload_rate);
+    }
+
     if (netdev->upscript) {
         err = run_script(handler->name, "net", netdev->upscript, "up",
-                 "veth", veth1, (char*) NULL);
+                "veth", veth1, tc);
         if (err)
             goto out_delete;
     }
@@ -1426,6 +1447,8 @@ static int instanciate_veth(struct lxc_handler
*handler, struct lxc_netdev *netd
     DEBUG("instanciated veth '%s/%s', index is '%d'",
           veth1, veth2, netdev->ifindex);

+    free(tc);
+
     return 0;

 out_delete:
diff --git a/src/lxc/conf.h b/src/lxc/conf.h
index 8fd3dd8..bce418a 100644
--- a/src/lxc/conf.h
+++ b/src/lxc/conf.h
@@ -94,6 +94,11 @@ union netdev_p {
     struct ifla_macvlan macvlan_attr;
 };

+struct lxc_traffic_control {
+    char *upload_rate;
+    char *download_rate;
+};
+
 /*
  * Defines a structure to configure a network device
  * @link       : lxc.network.link, name of bridge or host iface to attach
if any
@@ -115,6 +120,7 @@ struct lxc_netdev {
     struct lxc_list ipv4;
     struct lxc_list ipv6;
     char *upscript;
+    struct lxc_traffic_control tc;
 };

 /*
diff --git a/src/lxc/confile.c b/src/lxc/confile.c
index 791f04f..6eeae6f 100644
--- a/src/lxc/confile.c
+++ b/src/lxc/confile.c
@@ -65,6 +65,8 @@ static int config_network_mtu(const char *, char *, struct
lxc_conf *);
 static int config_network_ipv4(const char *, char *, struct lxc_conf *);
 static int config_network_script(const char *, char *, struct lxc_conf *);
 static int config_network_ipv6(const char *, char *, struct lxc_conf *);
+static int config_network_rate_download(const char *, char *, struct
lxc_conf *);
+static int config_network_rate_upload(const char *, char *, struct lxc_conf
*);
 static int config_cap_drop(const char *, char *, struct lxc_conf *);
 static int config_console(const char *, char *, struct lxc_conf *);

@@ -95,6 +97,8 @@ static struct config config[] = {
     { "lxc.network.script.up",    config_network_script       },
     { "lxc.network.hwaddr",       config_network_hwaddr       },
     { "lxc.network.mtu",          config_network_mtu          },
+    { "lxc.network.upload.rate",  config_network_rate_upload  },
+    { "lxc.network.download.rate",config_network_rate_download},
     { "lxc.network.vlan.id",      config_network_vlan_id      },
     { "lxc.network.ipv4",         config_network_ipv4         },
     { "lxc.network.ipv6",         config_network_ipv6         },
@@ -480,6 +484,26 @@ static int config_network_ipv6(const char *key, char
*value,
     return 0;
 }

+static int config_network_rate_upload(const char *key, char *value,
+                        struct lxc_conf *lxc_conf){
+    struct lxc_netdev *netdev;
+
+    netdev = network_netdev(key, value, &lxc_conf->network);
+    netdev->tc.upload_rate = strdup(value);
+
+    return 0;
+}
+
+static int config_network_rate_download(const char *key, char *value,
+                        struct lxc_conf *lxc_conf){
+    struct lxc_netdev *netdev;
+
+    netdev = network_netdev(key, value, &lxc_conf->network);
+    netdev->tc.download_rate = strdup(value);
+
+    return 0;
+}
+
 static int config_network_script(const char *key, char *value,
                  struct lxc_conf *lxc_conf)
 {
-- 
1.7.1

Ana and Irina.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20110507/ad29d702/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: lxc_upscript.sh
Type: application/x-sh
Size: 741 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20110507/ad29d702/attachment.sh>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: conf_example.conf
Type: application/octet-stream
Size: 333 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20110507/ad29d702/attachment.obj>


More information about the lxc-devel mailing list