[packages] /etc/functions.sh => /lib/functions.sh
[packages.git] / net / wshaper / files / wshaper.htb
1 #!/bin/sh
2 # Wonder Shaper
3 # please read the README before filling out these values 
4 #
5 # Set the following values to somewhat less than your actual download
6 # and uplink speed. In kilobits. Also set the device that is to be shaped.
7
8 # All config needs to be done in /etc/config/wshaper
9
10 . /lib/functions.sh
11 config_load wshaper
12 for s in downlink uplink network nopriohostdst nopriohostsrc noprioportdst noprioportsrc; do
13         config_get $s settings $s
14 done
15
16 device=$(uci_get_state network "$network" ifname "$network")
17 [ -z "$device" ] && logger -t wondershaper "Error: Could not find the device for network $network, aborting." && exit 1
18 [ -z "$downlink" ] && logger -t wondershaper "Error: Downlink speed not set, aborting." && exit 1
19 [ -z "$uplink" ] && logger -t wondershaper "Error: Uplink speed not set, aborting." && exit 1
20
21 MODULES='sch_ingress sch_sfq sch_htb cls_u32 act_police'
22 DOWNLINK="$downlink"
23 UPLINK="$uplink"
24 DEV="$device"
25
26 # low priority OUTGOING traffic - you can leave this blank if you want
27 # low priority source netmasks
28 NOPRIOHOSTSRC="$nopriohostsrc"
29
30 # low priority destination netmasks
31 NOPRIOHOSTDST="$nopriohostdst"
32
33 # low priority source ports
34 NOPRIOPORTSRC="$noprioportsrc"
35
36 # low priority destination ports
37 NOPRIOPORTDST="$noprioportdst"
38
39 if [ "$1" = "status" ]
40 then
41         tc -s qdisc ls dev $DEV
42         tc -s class ls dev $DEV
43         exit
44 fi
45
46
47 # clean existing down- and uplink qdiscs, hide errors
48 tc qdisc del dev $DEV root    2> /dev/null > /dev/null
49 tc qdisc del dev $DEV ingress 2> /dev/null > /dev/null
50
51 if [ "$1" = "stop" ] 
52 then 
53         for i in $MODULES ; do
54                 rmmod $i
55         done
56         exit
57 fi
58
59 for i in $MODULES ; do
60         insmod $i
61 done
62
63 ###### uplink
64
65 # install root HTB, point default traffic to 1:20:
66
67 tc qdisc add dev $DEV root handle 1: htb default 20
68
69 # shape everything at $UPLINK speed - this prevents huge queues in your
70 # DSL modem which destroy latency:
71
72 tc class add dev $DEV parent 1: classid 1:1 htb rate ${UPLINK}kbit burst 6k
73
74 # high prio class 1:10:
75
76 tc class add dev $DEV parent 1:1 classid 1:10 htb rate ${UPLINK}kbit \
77    burst 6k prio 1
78
79 # bulk & default class 1:20 - gets slightly less traffic, 
80 # and a lower priority:
81
82 tc class add dev $DEV parent 1:1 classid 1:20 htb rate $((9*$UPLINK/10))kbit \
83    burst 6k prio 2
84
85 tc class add dev $DEV parent 1:1 classid 1:30 htb rate $((8*$UPLINK/10))kbit \
86    burst 6k prio 2
87
88 # all get Stochastic Fairness:
89 tc qdisc add dev $DEV parent 1:10 handle 10: sfq perturb 10
90 tc qdisc add dev $DEV parent 1:20 handle 20: sfq perturb 10
91 tc qdisc add dev $DEV parent 1:30 handle 30: sfq perturb 10
92
93 # TOS Minimum Delay (ssh, NOT scp) in 1:10:
94
95 tc filter add dev $DEV parent 1:0 protocol ip prio 10 u32 \
96       match ip tos 0x10 0xff  flowid 1:10
97
98 # ICMP (ip protocol 1) in the interactive class 1:10 so we 
99 # can do measurements & impress our friends:
100 tc filter add dev $DEV parent 1:0 protocol ip prio 10 u32 \
101         match ip protocol 1 0xff flowid 1:10
102
103 # To speed up downloads while an upload is going on, put ACK packets in
104 # the interactive class:
105
106 tc filter add dev $DEV parent 1: protocol ip prio 10 u32 \
107    match ip protocol 6 0xff \
108    match u8 0x05 0x0f at 0 \
109    match u16 0x0000 0xffc0 at 2 \
110    match u8 0x10 0xff at 33 \
111    flowid 1:10
112
113 # rest is 'non-interactive' ie 'bulk' and ends up in 1:20
114
115 # some traffic however suffers a worse fate
116 for a in $NOPRIOPORTDST
117 do
118         tc filter add dev $DEV parent 1: protocol ip prio 14 u32 \
119            match ip dport $a 0xffff flowid 1:30
120 done
121
122 for a in $NOPRIOPORTSRC
123 do
124         tc filter add dev $DEV parent 1: protocol ip prio 15 u32 \
125            match ip sport $a 0xffff flowid 1:30
126 done
127
128 for a in $NOPRIOHOSTSRC
129 do
130         tc filter add dev $DEV parent 1: protocol ip prio 16 u32 \
131            match ip src $a flowid 1:30
132 done
133
134 for a in $NOPRIOHOSTDST
135 do
136         tc filter add dev $DEV parent 1: protocol ip prio 17 u32 \
137            match ip dst $a flowid 1:30
138 done
139
140 # rest is 'non-interactive' ie 'bulk' and ends up in 1:20
141
142 tc filter add dev $DEV parent 1: protocol ip prio 18 u32 \
143    match ip dst 0.0.0.0/0 flowid 1:20
144
145
146 ########## downlink #############
147 # slow downloads down to somewhat less than the real speed  to prevent 
148 # queuing at our ISP. Tune to see how high you can set it.
149 # ISPs tend to have *huge* queues to make sure big downloads are fast
150 #
151 # attach ingress policer:
152
153 tc qdisc add dev $DEV handle ffff: ingress
154
155 # filter *everything* to it (0.0.0.0/0), drop everything that's
156 # coming in too fast:
157
158 tc filter add dev $DEV parent ffff: protocol ip prio 50 u32 match ip src \
159    0.0.0.0/0 police rate ${DOWNLINK}kbit burst 10k drop flowid :1
160
161 logger -t wondershaper "Wondershaper was started on device $device."