Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have some scripts in Linux to manage internet bandwidth, but I do not understand what they do.

What is the meaning of the following scripts:

tc qdisc add dev $IF root handle 1: htb default 256
tc class add dev $IF parent 1: classid 1:1 htb rate $max 
iptables -F
iptables -t nat -F
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
#iptables -t nat -A PREROUTING -i eth1 -p tcp --dport 80 -j DNAT --to
echo 1 > /proc/sys/net/ipv4/ip_forward


tc class add dev $IF parent 1:1 classid 1:$(($n)) htb rate "$1"kbps ceil "$2"kbps 
tc filter add dev $IF protocol ip parent 1:0 prio 2 u32 match ip dst "$line"/32 flowid 1:$(($n))

tc filter add dev $IF parent 1: protocol ip prio 1 u32 match ip dst "$line"/32 match ip protocol 6 0xff match u8 0x05 0x0f at 0 match u16 0x0000 0xffc0 at 2 match u8 0x10 0xff at 33 flowid 1:$(($n))


tc class add dev $IF parent 1:1 classid 1:256 htb rate 60kbps
share|improve this question
man tc and man iptables will help. – goldilocks Apr 8 '13 at 13:33
tc is traffic control. Did you read man tc ? – Hennes Apr 8 '13 at 13:33
tc is one of the most complicated and one of the most badly documented commands in Linux networking. It easily beats iptables ten times over in that regard. Reading the man page does not suffice. Start with lartc.org for some light reading, then Google for mailinglist archives and extra resources. Then experiment. – frostschutz Apr 8 '13 at 19:42

HTB is a non-conserving qdisc. It limits the output speed. If more data is enqueued to the interface than can be dequeued by the HTB with the given limits then packets are dropped.

The script is incomplete so it's not clear what it does.

The traffic enqueued to this interface has to be given to a certain class. 1:0 is the qdisc, 1:1, 1:2, ... 1:256 are the classes which belong to this qdisc. Each class can have it's own configuration.

tc filter determines which class is responsible for a certain packet. If none is found then the qdisc definition makes 1:256 the default class which is statically configured in your case. The other ones are configured "dynamically" (however, that's not part of the code you have shown).

There seems to be a list of target hosts (single IP addresses) which all have a guaranteed bandwidth. If a packet is targeted at one of these hosts then it is put into the class responsible for this host. There are more conditions than just the target IP but I don't understand the u8 and u16 filters without looking that up. That may filter for TCP vs. UDP, their ports, TCP flags, packet size and the like.

share|improve this answer
If you want to trust the comment, the voodoo filter is for ACKs ;) Seems superfluous as it still goes into the same class and the first filter should match them already...? (It's been some years since I used tc.) – frostschutz Apr 8 '13 at 19:44
@frostschutz Embarrassing, I neither noticed the first filter line nor the # ack... We don't know what happens to $n (quite funny this $(($n)) anyway) but the first line does not match first, of course, due to its prio 2. Whether it makes sense to define filters in reverse prio order is another question. If $n doesn't change meanwhile then this filter structure is braindead. – Hauke Laging Apr 8 '13 at 20:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.