How to Realize QoS in your Openwrt Router

I realize IP QoS(Quality of Service) in my Openwrt with iptables and tc(Traffic Control).

Firstly , we should installs the related software packages.

1
2
opkg update
opkg install tc iptables

If your firmware is trunk version, you have to install kmod-sched. this software has been installed in stable version. If openwrt lacks the software, “RTNETLINK answers: No such file or directory” will be present when you enter tc command. So:

1
opkg install kmod-sched

After the above steps, let’s edit Qos shell scripts.

1
2
mkdir /etc/qos
vi /etc/qos/start.sh

Details of the start.sh are as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/sh

IDEV="br-lan"
ODEV="tun0"

UP="800kbit"
DOWN="1200kbit"

UPLOAD="80kbit"
DOWNLOAD="120kbit"

MUPLOAD="160kbit"
MDOWNLOAD="160kbit"

INET="192.168.254."

IPS="240"
IPE="249"

tc qdisc del dev $ODEV root 2>/dev/null
tc qdisc del dev $IDEV root 2>/dev/null

tc qdisc add dev $ODEV root handle 10: htb default 256
tc qdisc add dev $IDEV root handle 10: htb default 256

tc class add dev $ODEV parent 10: classid 10:1 htb rate $UP ceil $UP
tc class add dev $IDEV parent 10: classid 10:1 htb rate $DOWN ceil $DOWN

i=$IPS;
while [ $i -le $IPE ]
do
tc class add dev $ODEV parent 10:1 classid 10:2$i htb rate $UPLOAD ceil $MUPLOAD prio 1
tc qdisc add dev $ODEV parent 10:2$i handle 100$i: pfifo
tc filter add dev $ODEV parent 10: protocol ip prio 100 handle 2$i fw classid 10:2$i
tc class add dev $IDEV parent 10:1 classid 10:2$i htb rate $DOWNLOAD ceil $MDOWNLOAD prio 1
tc qdisc add dev $IDEV parent 10:2$i handle 100$i: pfifo
tc filter add dev $IDEV parent 10: protocol ip prio 100 handle 2$i fw classid 10:2$i
iptables -t mangle -A PREROUTING -s $INET$i -j MARK --set-mark 2$i
iptables -t mangle -A PREROUTING -s $INET$i -j RETURN
iptables -t mangle -A POSTROUTING -d $INET$i -j MARK --set-mark 2$i
iptables -t mangle -A POSTROUTING -d $INET$i -j RETURN
i=`expr $i + 1`
done

Generate stop.sh.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/sh

INET="192.168.254."

IPS="240"
IPE="249"

tc qdisc del dev $ODEV root 2>/dev/null
tc qdisc del dev $IDEV root 2>/dev/null

p=$IPS;
while [ $p -le $IPE ]
do
iptables -t mangle -D PREROUTING -s $INET$p -j MARK --set-mark 2$p
iptables -t mangle -D PREROUTING -s $INET$p -j RETURN
iptables -t mangle -D POSTROUTING -d $INET$p -j MARK --set-mark 2$p
iptables -t mangle -D POSTROUTING -d $INET$p -j RETURN
p=`expr $p + 1`
done

1
2
chmod +x st*
./start.sh
0%