First steps for bandwidth management
I’ve been messing a little with QoS on Linux, I’m using my WRT54GS with OpenWRT’s release candidate 5 (whiterussian) which provides the needed command line tools (tc and iptables) and needed kernel modules too.
The thing starts defining a qdisc which provides the root point on the flow of traffic control, declaring the device where the queues are going to apply, the algorithm we want to use, etc:
tc qdisc add dev $UPDEV root handle 1: htb default 10
With this line I declare a queue discipline based on
HTB applied on $UPDEV which point on the tree is 1: (1:0). After a root we have to define a class:tc class add dev $UPDEV parent 1: classid 1:1 htb rate ${UPLINK}kbit ceil ${UPLINK}kbit burst 6k
This class is defined as child of the root (1:) node and has got id 1:1, uses HTB and can get all the bandwidth declared by ${UPLINK}. The burst statement is quite interesting cause it will help to hold packets when over-limits come.
Now it’s time to start classifying the packets:tc class add dev $UPDEV parent 1:1 classid 1:20 htb rate 100kbit ceil ${UPLINK}kbit prio 0
Here I reserved 100kbps from my upload bandwidth for the class 1:20 child of 1:1, it could borrow the whole up link if needed and takes priority 0 which is the highest one.
All we have to do now is to write a filter line which will define the handle used by iptables to mark the packets, a relation between a classid and a mark.
tc filter add dev ${UPDEV} parent 1: protocol ip prio 1 handle 1 fw classid 1:20
As can be read here I defined the filter which indicates that packets marked with 1 has to be flowed by the class id 1:20. Then the needed iptables rule:
iptables -t mangle -A PREROUTING -s ${IPADDR} -p tcp --sport ${PORTNUM} -j MARK --set-mark 0x1
Now watch the packets going through the class defined with the following command:
tc -s class show dev ${UPDEV}
So…is it working?