2015年5月16日 星期六

Linux limiting download and upload traffic by using HTB

I use Ubuntu (14.04) 32 bit as an example

Kernel Customize Part

Get the kernel source by apt-get
apt-get source linux-image-$(uname -r)

Get the compile Dependence by
sudo apt-get build-dep linux-image-$(uname -r)

After download the source, use make menuconfig to enter the Kernel Configuration

Go to
Networking Support -> Networking options -> QoS and/or fair queueing

Enable: Incoming device classifiication(CONFIG_NET_CLS_IND)

Go to
Devices driver -> Network devices support
Enable: Intermediate Functional Block support (CONFIG_IFB)

Save and Make

QoS Part

tc (traffic control) is your best friend to config the QoS

# Handle both root and ingress traffic
tc qdisc add dev eth0 root handle 1: htb default 12
tc qdisc add dev eth0 handle ffff: ingress

# Mirred the traffic of eth0 ingress to ifb0 so that we can do traffic control
tc filter add dev eth0 parent ffff: protocol ip u32 match u32 0 0 action mirred egress redirect dev ifb0
tc qdisc add dev ifb0 root handle 1: htb default 12

# Some example of traffic controlling
tc class add dev eth0 parent 1: classid 1:10 htb rate 10mbit ceil 10mbit
tc class add dev ifb0 parent 1: classid 1:10 htb rate 10mbit ceil 10mbit

# Add filter to cature all traffic through the device
tc filter add dev eth0 parent 1: protocol ip u32 match u8 0 0 classid 1:10
tc filter add dev ifb0 parent 1: protocol ip u32 match u8 0 0 classid 1:10