Friday, September 26, 2008

Traffic Shaping with cbq

Since I am on the subject of networking in Linux, here is another post about some nice things we can do: shaping our traffic so that we can limit how much of our bandwidth we use for each type of traffic.

Just as a small reminder: I am a long-time Slackware user (since 1996) and I only test my configurations on this distribution. I have used other Linux 'flavors' in the past but know much less about them.
Most thing I will post here will work though on other systems, but don't shoot me if they do not.

I started using cbq for traffic shaping on my local network because of the following situation:
I use rsync to copy some files I cannot afford to loose from my desktop to my wife's and v.v. I use crontab to automatically do this at certain hours.
Rsync is a wonderful protocol that only copies files that have changed, saving time and bandwidth.
But sometimes many files are changed or added, and then the whole bandwith of my local network is used, slowing down other traffic.
At these times even browsing the internet can become very slow, just because I am backing up some folders of new digital pictures.

Rsync has its own '--bwlimit' option, but I wanted a better, more structured solution. And this solution is cbq.

Basically configuring up cbq is done in three steps:

1) Setting up cbq
cbq is actually a script that can be found in the documentation of iproute2 in Slackware. We have to copy it to /sbin and make it executable:

cp /usr/doc/iproute2-2.6.16-060323/examples/cbq.init-v0.7.3 /sbin/cbq
chmod +x /sbin/cbq

cbq expects its configuration files in /etc/sysconfig/cbq
If this directory does not exist, create it:

mkdir /etc/sysconfig/cbq

2) Creating the rules-file

cbq reads files in /etc/sysconfig/cbq with the following names:
cbq-nnnn.yyy where:
  • nnnn: is a hexadecimal number from 0002 to ffff
  • yyy: is the name of your network interface, like eth0, eth1, etc

In my case, the network interface for my local network is eth1, so I created "cbq-0002.eth1"
Here is the contents of my file:


Some explanations:
  • DEVICE: the interface you want to limit, with its real speed and its weight (1/10 of the max. speed)
  • RATE: the bandwith you want to offer for this particular application / port / address
  • WEIGHT: 1/10 of the RATE
  • PRIO: Priority setting. 5 is default
  • RULE: source,destination --> in my case is my desktop, 873 is the port rsync uses
  • BOUNDED: Default no, used if you have other filters
  • ISOLATED: 'no' means that the rate can be used by other traffic if not in use
3) Starting the bandwidth limiting
Use cbq compile to prepare the new filters or after you alter your cbq-nnnn.yyy files.
Then use cbq start to start your traffic-shaping!
To always start cbq, include it in your rc.local script.

You can monitor your results with iptraf or wireshark.

More information can be found using "man tc-cbq".

Labels: ,


Post a Comment

Subscribe to Post Comments [Atom]

<< Home