Traffic shaping with TC in Linux

Traffic shaping is a method to control the rate at which packets are sent and Linux does a pretty good job in doing this. I am currently developing a shell-like interface for TC in python and the following script is the framework for the application. I will update this post as I have more experience with TC. Here is the script with descriptions for which I also used tc manual;

#tc qdisc add dev eth1 root handle 1:0 htb

#tc class add dev eth1 parent 1: classid 1:1 htb rate 2mbit ceil 2mbit

Classful qdiscs contain classes which allows us to set rate limits. Class 1:1 is the root class here

This is the leaf class 1:10 attached to the root class 1:1.
Shaping can be done in the leaf class not in the root class.
In this example of leaf classes, we allocate 512kbit,1mbit
and 512kbit of bandwidth to each class respectively.
rate: guaranteed (minimum bandwidth) which can be exceeded
ceil: burstable (maximum bandwidth) which cannot be exceeded

We use filters to classify the packets into an output queue and attached it to our classful htb class.
What handle means will be meaningful in the rest of the post

SFQ (Stochastic Fairness Queueing) is added to each of the HTB
queues and it offers equality for limiting traffic while keeping
the overhead low. I have tested a setup without SFQ and it gave
frustrating results (e.g link rates go up and down)

quantum: Amount of bytes a flow is allowed to
dequeue during a round of the round robin process. Defaults to
the MTU of the interface which is also the advised value and
the minimum value. This is what manual says but I have seen
that default value is assigned as 814. So maybe it is better
to set it to 1500

perturb: Interval in seconds for queue algorithm perturbation.
Defaults to 0, which means that no perturbation occurs. Do not
set too low for each perturbation may cause some packet
reordering. Advised value: 10

By using iptables we mark packets in order for TC to classify them. Those marking numbers are used in handle parameter of tc filter 

In this script we direct hosts having IP addresses 192.168.111.2,3,4 into different classes each of which has different rate. Important thing is that eth1 interface is the inner interface facing these hosts.  For example if host 192.168.111.2 is downloading a file from a remote host, eth1 becomes an egress interface for the packets coming from the remote host. If we want to limit what these hosts send another qdisc for eth0 (outside interface) and root,child classes must be added.

I still have time to complete my shell-like interface for TC. I don’t have much experience in the field with TC except some tests in several ISPs. I welcome any opinion, feedback.

References:

http://tldp.org/HOWTO/html_single/Traffic-Control-HOWTO

You have a feedback?