Niels Horn's Blog

Random thoughts, tips & tricks about Slackware-Linux, Lego and Star Wars

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:

DEVICE=eth1,100Mbit,10Mbit
RATE=5000Kbit
WEIGHT=500Kbit
PRIO=5
RULE=192.168.1.110:873,192.168.1.0/24
BOUNDED=no
ISOLATED=no

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 192.168.1.110 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”.

Bookmark and Share

This entry was posted on Saturday, September 27th, 2008 at 0:42 and is filed under Linux, networking. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

4 Responses to “Traffic Shaping with cbq”

  1. Dmitry Says:

    Hi,
    very good and interesting article, thank you. I tried to do similar on SUSE 11.0, BUT I have neither of which did not work. Once enter the command cbq compile I get an error:

    “#:/etc/sysconfig/cbq # cbq compile
    find: warning: you have specified the -maxdepth option after a non-option argument (, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.

    find: warning: you have specified the -maxdepth option after a non-option argument (, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.

    /usr/sbin/tc qdisc del dev eth1 root
    /usr/sbin/tc qdisc add dev eth1 root handle 1 cbq bandwidth 100Mbit avpkt 3000 cell 8
    /usr/sbin/tc class change dev eth1 root cbq weight 10Mbit allot 1514

    /usr/sbin/tc class add dev eth1 parent 1: classid 1:2 cbq bandwidth 100Mbit rate 5Kbit weight 1Kbit prio 5 allot 1514 cell 8 maxburst 20 avpkt 3000 bounded
    /usr/sbin/tc qdisc add dev eth1 parent 1:2 handle 2 tbf rate 5Kbit buffer 10Kb/8 limit 15Kb mtu 1500
    /usr/sbin/tc filter add dev eth1 parent 1:0 protocol ip prio 100 u32 match ip dst 0.0.0.0 classid 1:2

  2. Niels Horn Says:

    Hi,

    Thanks for your positive comment!
    I have not used Suse for several years, so I don’t know if things are very different.
    Did you get the cbq script from the iproute documentation just like my example in the post?
    It might be a conflict between versions…

  3. Stanley Says:

    this warning is an error in cbq script.

    find: warning: you have specified the -maxdepth option after a non-option argument (, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.

    find: warning: you have specified the -maxdepth option after a non-option argument (, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.

    open your /sbin/cbq ,and vi the file, modify it and looks like below:

    577 ### Get a list of configured classes
    578 CLASSLIST=`find $1 -maxdepth 1 \( -type f -or -type l\ ) -name ‘cbq-*’ \
    579 -not -name ‘*~’ -printf “%f\n”| sort`
    580 [ -z "$CLASSLIST" ] &&
    581 cbq_failure “no configuration files found in $1!”
    582
    583 ### Gather all DEVICE fields from $1/cbq-*
    584 DEVFIELDS=`find $1 -maxdepth 1 \( -type f -or -type l \) -name ‘cbq-*’ \
    585 -not -name ‘*~’| xargs sed -n ’s/#.*//;
    586 s/[[:space:]]//g; /^DEVICE=[^,]*,[^,]*(,[^,]*)?/ \
    587 { s/.*=//; p; }’| sort -u`

  4. Niels Horn Says:

    @Stanley: Thanks for the help! Which version of cbq are you using? Mine is 0.7.3

Leave a Reply



XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

It may take some time for your comment to appear, it is not necessary to submit it again.