qos-scripts update - fix enabling/disabling of interfaces, make the overhead calculat...
[openwrt/svn-archive/archive.git] / package / qos-scripts / files / usr / lib / qos / generate.sh
1 #!/bin/sh
2 [ -e /etc/functions.sh ] && . /etc/functions.sh || . ./functions.sh
3 [ -x /sbin/modprobe ] && insmod="modprobe" || insmod="insmod"
4
5 add_insmod() {
6 eval "export isset=\${insmod_$1}"
7 case "$isset" in
8 1) ;;
9 *) append INSMOD "$insmod $* >&- 2>&-" "$N"; export insmod_$1=1;;
10 esac
11 }
12
13 [ -e /etc/config/network ] && {
14 # only try to parse network config on openwrt
15
16 find_ifname() {(
17 reset_cb
18 include /lib/network
19 scan_interfaces
20 config_get "$1" ifname
21 )}
22 } || {
23 find_ifname() {
24 echo "Interface not found."
25 exit 1
26 }
27 }
28
29 parse_matching_rule() {
30 local var="$1"
31 local section="$2"
32 local options="$3"
33 local prefix="$4"
34 local suffix="$5"
35 local proto="$6"
36 local mport=""
37 local ports=""
38
39 append "$var" "$prefix" "$N"
40 for option in $options; do
41 case "$option" in
42 proto) config_get value "$section" proto; proto="${proto:-$value}";;
43 esac
44 done
45 config_get type "$section" TYPE
46 case "$type" in
47 classify) unset pkt; append "$var" "-m mark --mark 0";;
48 default) pkt=1; append "$var" "-m mark --mark 0";;
49 reclassify) pkt=1;;
50 esac
51 append "$var" "${proto:+-p $proto}"
52 for option in $options; do
53 config_get value "$section" "$option"
54
55 case "$pkt:$option" in
56 *:srchost)
57 append "$var" "-s $value"
58 ;;
59 *:dsthost)
60 append "$var" "-d $value"
61 ;;
62 *:ipp2p)
63 add_insmod ipt_ipp2p
64 append "$var" "-m ipp2p"
65 case "$value" in
66 all) append "$var" "--edk --dc --kazaa --gnu --bit";;
67 *) append "$var" "--$value";;
68 esac
69 ;;
70 *:layer7)
71 add_insmod ipt_layer7
72 append "$var" "-m layer7 --l7proto $value${pkt:+ --l7pkt}"
73 ;;
74 *:ports|*:srcports|*:dstports)
75 value="$(echo "$value" | sed -e 's,-,:,g')"
76 lproto=${lproto:-tcp}
77 case "$proto" in
78 ""|tcp|udp) append "$var" "-m ${proto:-tcp -p tcp} -m multiport";;
79 *) unset "$var"; return 0;;
80 esac
81 case "$option" in
82 ports)
83 config_set "$section" srcports ""
84 config_set "$section" dstports ""
85 config_set "$section" portrange ""
86 append "$var" "--ports $value"
87 ;;
88 srcports)
89 config_set "$section" ports ""
90 config_set "$section" dstports ""
91 config_set "$section" portrange ""
92 append "$var" "--sports $value"
93 ;;
94 dstports)
95 config_set "$section" ports ""
96 config_set "$section" srcports ""
97 config_set "$section" portrange ""
98 append "$var" "--dports $value"
99 ;;
100 esac
101 ports=1
102 ;;
103 *:portrange)
104 config_set "$section" ports ""
105 config_set "$section" srcports ""
106 config_set "$section" dstports ""
107 value="$(echo "$value" | sed -e 's,-,:,g')"
108 case "$proto" in
109 ""|tcp|udp) append "$var" "-m ${proto:-tcp -p tcp} --sport $value --dport $value";;
110 *) unset "$var"; return 0;;
111 esac
112 ports=1
113 ;;
114 1:pktsize)
115 value="$(echo "$value" | sed -e 's,-,:,g')"
116 add_insmod ipt_length
117 append "$var" "-m length --length $value"
118 ;;
119 1:limit)
120 add_insmod ipt_limit
121 append "$var" "-m limit --limit $value"
122 ;;
123 1:tcpflags)
124 case "$proto" in
125 tcp) append "$var" "-m tcp --tcp-flags ALL $value";;
126 *) unset $var; return 0;;
127 esac
128 ;;
129 1:mark)
130 config_get class "${value##!}" classnr
131 [ -z "$class" ] && continue;
132 case "$value" in
133 !*) append "$var" "-m mark ! --mark $class";;
134 *) append "$var" "-m mark --mark $class";;
135 esac
136 esac
137 done
138 append "$var" "$suffix"
139 case "$ports:$proto" in
140 1:) parse_matching_rule "$var" "$section" "$options" "$prefix" "$suffix" "udp";;
141 esac
142 }
143
144 config_cb() {
145 option_cb() {
146 return 0
147 }
148
149 # Section start
150 case "$1" in
151 interface)
152 config_set "$2" "classgroup" "Default"
153 config_set "$2" "upload" "128"
154 ;;
155 classify|default|reclassify)
156 option_cb() {
157 append options "$1"
158 }
159 ;;
160 esac
161
162 # Section end
163 config_get TYPE "$CONFIG_SECTION" TYPE
164 case "$TYPE" in
165 interface)
166 config_get_bool enabled "$CONFIG_SECTION" enabled 1
167 [ 1 -eq "$enabled" ] || return 0
168 config_get classgroup "$CONFIG_SECTION" classgroup
169 config_set "$CONFIG_SECTION" imqdev "$C"
170 C=$(($C+1))
171 append INTERFACES "$CONFIG_SECTION"
172 config_set "$classgroup" enabled 1
173 config_get device "$CONFIG_SECTION" device
174 [ -z "$device" ] && {
175 device="$(find_ifname ${CONFIG_SECTION})"
176 config_set "$CONFIG_SECTION" device "${device:-eth0}"
177 }
178 ;;
179 classgroup) append CG "$CONFIG_SECTION";;
180 classify|default|reclassify)
181 case "$TYPE" in
182 classify) var="ctrules";;
183 *) var="rules";;
184 esac
185 config_get target "$CONFIG_SECTION" target
186 config_set "$CONFIG_SECTION" options "$options"
187 append "$var" "$CONFIG_SECTION"
188 unset options
189 ;;
190 esac
191 }
192
193
194 enum_classes() {
195 local c="0"
196 config_get classes "$1" classes
197 config_get default "$1" default
198 for class in $classes; do
199 c="$(($c + 1))"
200 config_set "${class}" classnr $c
201 case "$class" in
202 $default) class_default=$c;;
203 esac
204 done
205 class_default="${class_default:-$c}"
206 }
207
208 cls_var() {
209 local varname="$1"
210 local class="$2"
211 local name="$3"
212 local type="$4"
213 local default="$5"
214 local tmp tmp1 tmp2
215 config_get tmp1 "$class" "$name"
216 config_get tmp2 "${class}_${type}" "$name"
217 tmp="${tmp2:-$tmp1}"
218 tmp="${tmp:-$tmp2}"
219 export ${varname}="${tmp:-$default}"
220 }
221
222 tcrules() {
223 dir=/usr/lib/qos
224 [ -e $dir/tcrules.awk ] || dir=.
225 echo "$cstr" | awk \
226 -v device="$dev" \
227 -v linespeed="$rate" \
228 -f $dir/tcrules.awk
229 }
230
231 start_interface() {
232 local iface="$1"
233 local num_imq="$2"
234 config_get device "$iface" device
235 config_get_bool enabled "$iface" enabled 1
236 [ -z "$device" -o 1 -ne "$enabled" ] && {
237 echo "Interface '$iface' not found or disabled."
238 return 1
239 }
240 config_get upload "$iface" upload
241 config_get halfduplex "$iface" halfduplex
242 config_get download "$iface" download
243 config_get classgroup "$iface" classgroup
244 config_get_bool overhead "$iface" overhead 0
245
246 download="${download:-${halfduplex:+$upload}}"
247 enum_classes "$classgroup"
248 for dir in up${halfduplex} ${download:+down}; do
249 case "$dir" in
250 up)
251 [ "$overhead" = 1 ] && upload=$(($upload * 98 / 100 - (32 * 128 / $upload)))
252 dev="$device"
253 rate="$upload"
254 dl_mode=""
255 prefix="cls"
256 ;;
257 down)
258 add_insmod imq numdevs="$num_imq"
259 config_get imqdev "$iface" imqdev
260 [ "$overhead" = 1 ] && download=$(($download * 98 / 100 - (80 * 1024 / $download)))
261 dev="imq$imqdev"
262 rate="$download"
263 dl_mode=1
264 prefix="d_cls"
265 ;;
266 *) continue;;
267 esac
268 cstr=
269 for class in $classes; do
270 cls_var pktsize "$class" packetsize $dir 1500
271 cls_var pktdelay "$class" packetdelay $dir 0
272 cls_var maxrate "$class" limitrate $dir 100
273 cls_var prio "$class" priority $dir 1
274 cls_var avgrate "$class" avgrate $dir 0
275 config_get classnr "$class" classnr
276 append cstr "$classnr:$prio:$avgrate:$pktsize:$pktdelay:$maxrate" "$N"
277 done
278 append ${prefix}q "$(tcrules)" "$N"
279 export dev_${dir}="ifconfig $dev up txqueuelen 5 >&- 2>&-
280 tc qdisc del dev $dev root >&- 2>&-
281 tc qdisc add dev $dev root handle 1: hfsc default ${class_default}0
282 tc class add dev $dev parent 1: classid 1:1 hfsc sc rate ${rate}kbit ul rate ${rate}kbit"
283 done
284 add_insmod cls_fw
285 add_insmod sch_hfsc
286 add_insmod sch_sfq
287 add_insmod sch_red
288
289 cat <<EOF
290 ${INSMOD:+$INSMOD$N}${dev_up:+$dev_up
291 $clsq
292 }${imqdev:+$dev_down
293 $d_clsq
294 $d_clsl
295 $d_clsf
296 }
297 EOF
298 unset INSMOD clsq clsf clsl d_clsq d_clsl d_clsf dev_up dev_down
299 }
300
301 start_interfaces() {
302 local C="$1"
303 for iface in $INTERFACES; do
304 start_interface "$iface" "$C"
305 done
306 }
307
308 add_rules() {
309 local var="$1"
310 local rules="$2"
311 local prefix="$3"
312
313 for rule in $rules; do
314 unset iptrule
315 config_get target "$rule" target
316 config_get target "$target" classnr
317 config_get options "$rule" options
318 parse_matching_rule iptrule "$rule" "$options" "$prefix" "-j MARK --set-mark $target"
319 append "$var" "$iptrule" "$N"
320 done
321 }
322
323 start_cg() {
324 local cg="$1"
325 local iptrules
326 local pktrules
327 local sizerules
328 local download
329 enum_classes "$cg"
330 add_rules iptrules "$ctrules" "iptables -t mangle -A ${cg}_ct"
331 config_get classes "$cg" classes
332 for class in $classes; do
333 config_get mark "$class" classnr
334 config_get maxsize "$class" maxsize
335 [ -z "$maxsize" -o -z "$mark" ] || {
336 add_insmod ipt_length
337 append pktrules "iptables -t mangle -A ${cg} -m mark --mark $mark -m length --length $maxsize: -j MARK --set-mark 0" "$N"
338 }
339 done
340 add_rules pktrules "$rules" "iptables -t mangle -A ${cg}"
341 for iface in $INTERFACES; do
342 config_get classgroup "$iface" classgroup
343 config_get device "$iface" device
344 config_get imqdev "$iface" imqdev
345 config_get dl "$iface" download
346 config_get halfduplex "$iface" halfduplex
347 add_insmod ipt_IMQ
348 append up "iptables -t mangle -A OUTPUT -o $device -j ${cg}" "$N"
349 append up "iptables -t mangle -A FORWARD -o $device -j ${cg}" "$N"
350 [ -z "$dl" ] || {
351 [ -z "$halfduplex" ] || {
352 append down "iptables -t mangle -A POSTROUTING -o $device -j IMQ --todev $imqdev" "$N"
353 }
354 append down "iptables -t mangle -A PREROUTING -i $device -j ${cg}" "$N"
355 append down "iptables -t mangle -A PREROUTING -i $device -j IMQ --todev $imqdev" "$N"
356 }
357 done
358 cat <<EOF
359 $INSMOD
360 iptables -t mangle -N ${cg} >&- 2>&-
361 iptables -t mangle -N ${cg}_ct >&- 2>&-
362 ${iptrules:+${iptrules}${N}iptables -t mangle -A ${cg}_ct -j CONNMARK --save-mark}
363 iptables -t mangle -A ${cg} -j CONNMARK --restore-mark
364 iptables -t mangle -A ${cg} -m mark --mark 0 -j ${cg}_ct
365 $pktrules
366 $up$N${down:+${down}$N}
367 EOF
368 unset INSMOD
369 }
370
371 start_firewall() {
372 add_insmod ipt_multiport
373 add_insmod ipt_CONNMARK
374 cat <<EOF
375 iptables -t mangle -F
376 iptables -t mangle -X
377 EOF
378 for group in $CG; do
379 start_cg $group
380 done
381 }
382
383 C="0"
384 INTERFACES=""
385 [ -e ./qos.conf ] && {
386 . ./qos.conf
387 config_cb
388 } || config_load qos
389
390 C="0"
391 for iface in $INTERFACES; do
392 export C="$(($C + 1))"
393 done
394
395 case "$1" in
396 all)
397 start_interfaces "$C"
398 start_firewall
399 ;;
400 interface)
401 start_interface "$2" "$C"
402 ;;
403 interfaces)
404 start_interfaces
405 ;;
406 firewall)
407 start_firewall
408 ;;
409 esac