get rid of a remaining reference to ipp2p
[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 *:layer7)
63 add_insmod ipt_layer7
64 add_insmod xt_layer7
65 append "$var" "-m layer7 --l7proto $value${pkt:+ --l7pkt}"
66 ;;
67 *:ports|*:srcports|*:dstports)
68 value="$(echo "$value" | sed -e 's,-,:,g')"
69 lproto=${lproto:-tcp}
70 case "$proto" in
71 ""|tcp|udp) append "$var" "-m ${proto:-tcp -p tcp} -m multiport";;
72 *) unset "$var"; return 0;;
73 esac
74 case "$option" in
75 ports)
76 config_set "$section" srcports ""
77 config_set "$section" dstports ""
78 config_set "$section" portrange ""
79 append "$var" "--ports $value"
80 ;;
81 srcports)
82 config_set "$section" ports ""
83 config_set "$section" dstports ""
84 config_set "$section" portrange ""
85 append "$var" "--sports $value"
86 ;;
87 dstports)
88 config_set "$section" ports ""
89 config_set "$section" srcports ""
90 config_set "$section" portrange ""
91 append "$var" "--dports $value"
92 ;;
93 esac
94 ports=1
95 ;;
96 *:portrange)
97 config_set "$section" ports ""
98 config_set "$section" srcports ""
99 config_set "$section" dstports ""
100 value="$(echo "$value" | sed -e 's,-,:,g')"
101 case "$proto" in
102 ""|tcp|udp) append "$var" "-m ${proto:-tcp -p tcp} --sport $value --dport $value";;
103 *) unset "$var"; return 0;;
104 esac
105 ports=1
106 ;;
107 *:connbytes)
108 value="$(echo "$value" | sed -e 's,-,:,g')"
109 add_insmod ipt_connbytes
110 append "$var" "-m connbytes --connbytes $value --connbytes-dir both --connbytes-mode bytes"
111 ;;
112 *:tos)
113 add_insmod ipt_tos
114 case "$value" in
115 !*) append "$var" "-m tos ! --tos $value";;
116 *) append "$var" "-m tos --tos $value"
117 esac
118 ;;
119 *:dscp)
120 add_insmod ipt_dscp
121 dscp_option="--dscp"
122 [ -z "${value%%[EBCA]*}" ] && dscp_option="--dscp-class"
123 case "$value" in
124 !*) append "$var" "-m dscp ! $dscp_option $value";;
125 *) append "$var" "-m dscp $dscp_option $value"
126 esac
127 ;;
128 *:direction)
129 value="$(echo "$value" | sed -e 's,-,:,g')"
130 if [ "$value" = "out" ]; then
131 append "$var" "-o $device"
132 elif [ "$value" = "in" ]; then
133 append "$var" "-i $device"
134 fi
135 ;;
136 1:pktsize)
137 value="$(echo "$value" | sed -e 's,-,:,g')"
138 add_insmod ipt_length
139 append "$var" "-m length --length $value"
140 ;;
141 1:limit)
142 add_insmod ipt_limit
143 append "$var" "-m limit --limit $value"
144 ;;
145 1:tcpflags)
146 case "$proto" in
147 tcp) append "$var" "-m tcp --tcp-flags ALL $value";;
148 *) unset $var; return 0;;
149 esac
150 ;;
151 1:mark)
152 config_get class "${value##!}" classnr
153 [ -z "$class" ] && continue;
154 case "$value" in
155 !*) append "$var" "-m mark ! --mark $class";;
156 *) append "$var" "-m mark --mark $class";;
157 esac
158 ;;
159 1:TOS)
160 add_insmod ipt_TOS
161 config_get TOS "$rule" 'TOS'
162 suffix="-j TOS --set-tos "${TOS:-"Normal-Service"}
163 ;;
164 1:DSCP)
165 add_insmod ipt_DSCP
166 config_get DSCP "$rule" 'DSCP'
167 [ -z "${DSCP%%[EBCA]*}" ] && set_value="--set-dscp-class $DSCP" \
168 || set_value="--set-dscp $DSCP"
169 suffix="-j DSCP $set_value"
170 ;;
171 esac
172 done
173 append "$var" "$suffix"
174 case "$ports:$proto" in
175 1:) parse_matching_rule "$var" "$section" "$options" "$prefix" "$suffix" "udp";;
176 esac
177 }
178
179 config_cb() {
180 option_cb() {
181 return 0
182 }
183
184 # Section start
185 case "$1" in
186 interface)
187 config_set "$2" "classgroup" "Default"
188 config_set "$2" "upload" "128"
189 ;;
190 classify|default|reclassify)
191 option_cb() {
192 append options "$1"
193 }
194 ;;
195 esac
196
197 # Section end
198 config_get TYPE "$CONFIG_SECTION" TYPE
199 case "$TYPE" in
200 interface)
201 config_get_bool enabled "$CONFIG_SECTION" enabled 1
202 [ 1 -eq "$enabled" ] || return 0
203 config_get classgroup "$CONFIG_SECTION" classgroup
204 config_set "$CONFIG_SECTION" imqdev "$C"
205 C=$(($C+1))
206 append INTERFACES "$CONFIG_SECTION"
207 config_set "$classgroup" enabled 1
208 config_get device "$CONFIG_SECTION" device
209 [ -z "$device" ] && {
210 device="$(find_ifname ${CONFIG_SECTION})"
211 config_set "$CONFIG_SECTION" device "${device:-eth0}"
212 }
213 ;;
214 classgroup) append CG "$CONFIG_SECTION";;
215 classify|default|reclassify)
216 case "$TYPE" in
217 classify) var="ctrules";;
218 *) var="rules";;
219 esac
220 config_get target "$CONFIG_SECTION" target
221 config_set "$CONFIG_SECTION" options "$options"
222 append "$var" "$CONFIG_SECTION"
223 unset options
224 ;;
225 esac
226 }
227
228
229 enum_classes() {
230 local c="0"
231 config_get classes "$1" classes
232 config_get default "$1" default
233 for class in $classes; do
234 c="$(($c + 1))"
235 config_set "${class}" classnr $c
236 case "$class" in
237 $default) class_default=$c;;
238 esac
239 done
240 class_default="${class_default:-$c}"
241 }
242
243 cls_var() {
244 local varname="$1"
245 local class="$2"
246 local name="$3"
247 local type="$4"
248 local default="$5"
249 local tmp tmp1 tmp2
250 config_get tmp1 "$class" "$name"
251 config_get tmp2 "${class}_${type}" "$name"
252 tmp="${tmp2:-$tmp1}"
253 tmp="${tmp:-$tmp2}"
254 export ${varname}="${tmp:-$default}"
255 }
256
257 tcrules() {
258 dir=/usr/lib/qos
259 [ -e $dir/tcrules.awk ] || dir=.
260 echo "$cstr" | awk \
261 -v device="$dev" \
262 -v linespeed="$rate" \
263 -f $dir/tcrules.awk
264 }
265
266 start_interface() {
267 local iface="$1"
268 local num_imq="$2"
269 config_get device "$iface" device
270 config_get_bool enabled "$iface" enabled 1
271 [ -z "$device" -o 1 -ne "$enabled" ] && {
272 return 1
273 }
274 config_get upload "$iface" upload
275 config_get halfduplex "$iface" halfduplex
276 config_get download "$iface" download
277 config_get classgroup "$iface" classgroup
278 config_get_bool overhead "$iface" overhead 0
279
280 download="${download:-${halfduplex:+$upload}}"
281 enum_classes "$classgroup"
282 for dir in up${halfduplex} ${download:+down}; do
283 case "$dir" in
284 up)
285 [ "$overhead" = 1 ] && upload=$(($upload * 98 / 100 - (15 * 128 / $upload)))
286 dev="$device"
287 rate="$upload"
288 dl_mode=""
289 prefix="cls"
290 ;;
291 down)
292 add_insmod imq numdevs="$num_imq"
293 config_get imqdev "$iface" imqdev
294 [ "$overhead" = 1 ] && download=$(($download * 98 / 100 - (80 * 1024 / $download)))
295 dev="imq$imqdev"
296 rate="$download"
297 dl_mode=1
298 prefix="d_cls"
299 ;;
300 *) continue;;
301 esac
302 cstr=
303 for class in $classes; do
304 cls_var pktsize "$class" packetsize $dir 1500
305 cls_var pktdelay "$class" packetdelay $dir 0
306 cls_var maxrate "$class" limitrate $dir 100
307 cls_var prio "$class" priority $dir 1
308 cls_var avgrate "$class" avgrate $dir 0
309 config_get classnr "$class" classnr
310 append cstr "$classnr:$prio:$avgrate:$pktsize:$pktdelay:$maxrate" "$N"
311 done
312 append ${prefix}q "$(tcrules)" "$N"
313 export dev_${dir}="ifconfig $dev up txqueuelen 5 >&- 2>&-
314 tc qdisc del dev $dev root >&- 2>&-
315 tc qdisc add dev $dev root handle 1: hfsc default ${class_default}0
316 tc class add dev $dev parent 1: classid 1:1 hfsc sc rate ${rate}kbit ul rate ${rate}kbit"
317 done
318 add_insmod cls_fw
319 add_insmod sch_hfsc
320 add_insmod sch_sfq
321 add_insmod sch_red
322
323 cat <<EOF
324 ${INSMOD:+$INSMOD$N}${dev_up:+$dev_up
325 $clsq
326 }${imqdev:+$dev_down
327 $d_clsq
328 $d_clsl
329 $d_clsf
330 }
331 EOF
332 unset INSMOD clsq clsf clsl d_clsq d_clsl d_clsf dev_up dev_down
333 }
334
335 start_interfaces() {
336 local C="$1"
337 for iface in $INTERFACES; do
338 start_interface "$iface" "$C"
339 done
340 }
341
342 add_rules() {
343 local var="$1"
344 local rules="$2"
345 local prefix="$3"
346
347 for rule in $rules; do
348 unset iptrule
349 config_get target "$rule" target
350 config_get target "$target" classnr
351 config_get options "$rule" options
352
353 ## If we want to override the TOS field, let's clear the DSCP field first.
354 [ ! -z "$(echo $options | grep 'TOS')" ] && {
355 s_options=${options%%TOS}
356 add_insmod ipt_DSCP
357 parse_matching_rule iptrule "$rule" "$s_options" "$prefix" "-j DSCP --set-dscp 0"
358 append "$var" "$iptrule" "$N"
359 unset iptrule
360 }
361
362 parse_matching_rule iptrule "$rule" "$options" "$prefix" "-j MARK --set-mark $target"
363 append "$var" "$iptrule" "$N"
364 done
365 }
366
367 start_cg() {
368 local cg="$1"
369 local iptrules
370 local pktrules
371 local sizerules
372 local download
373 enum_classes "$cg"
374 add_rules iptrules "$ctrules" "iptables -t mangle -A ${cg}_ct"
375 config_get classes "$cg" classes
376 for class in $classes; do
377 config_get mark "$class" classnr
378 config_get maxsize "$class" maxsize
379 [ -z "$maxsize" -o -z "$mark" ] || {
380 add_insmod ipt_length
381 append pktrules "iptables -t mangle -A ${cg} -m mark --mark $mark -m length --length $maxsize: -j MARK --set-mark 0" "$N"
382 }
383 done
384 add_rules pktrules "$rules" "iptables -t mangle -A ${cg}"
385 for iface in $INTERFACES; do
386 config_get classgroup "$iface" classgroup
387 config_get device "$iface" device
388 config_get imqdev "$iface" imqdev
389 config_get dl "$iface" download
390 config_get halfduplex "$iface" halfduplex
391 add_insmod ipt_IMQ
392 append up "iptables -t mangle -A OUTPUT -o $device -j ${cg}" "$N"
393 append up "iptables -t mangle -A FORWARD -o $device -j ${cg}" "$N"
394 [ -z "$dl" ] || {
395 [ -z "$halfduplex" ] || {
396 append down "iptables -t mangle -A POSTROUTING -o $device -j IMQ --todev $imqdev" "$N"
397 }
398 append down "iptables -t mangle -A PREROUTING -i $device -j ${cg}" "$N"
399 append down "iptables -t mangle -A POSTROUTING -o $device -j ${cg}" "$N"
400 append down "iptables -t mangle -A PREROUTING -i $device -j IMQ --todev $imqdev" "$N"
401 }
402 done
403 cat <<EOF
404 $INSMOD
405 iptables -t mangle -N ${cg} >&- 2>&-
406 iptables -t mangle -N ${cg}_ct >&- 2>&-
407 ${iptrules:+${iptrules}${N}iptables -t mangle -A ${cg}_ct -j CONNMARK --save-mark}
408 iptables -t mangle -A ${cg} -j CONNMARK --restore-mark
409 iptables -t mangle -A ${cg} -m mark --mark 0 -j ${cg}_ct
410 $pktrules
411 $up$N${down:+${down}$N}
412 EOF
413 unset INSMOD
414 }
415
416 start_firewall() {
417 add_insmod ipt_multiport
418 add_insmod ipt_CONNMARK
419 cat <<EOF
420 iptables -t mangle -F
421 iptables -t mangle -X
422 EOF
423 for group in $CG; do
424 start_cg $group
425 done
426 }
427
428 C="0"
429 INTERFACES=""
430 [ -e ./qos.conf ] && {
431 . ./qos.conf
432 config_cb
433 } || config_load qos
434
435 C="0"
436 for iface in $INTERFACES; do
437 export C="$(($C + 1))"
438 done
439
440 case "$1" in
441 all)
442 start_interfaces "$C"
443 start_firewall
444 ;;
445 interface)
446 start_interface "$2" "$C"
447 ;;
448 interfaces)
449 start_interfaces
450 ;;
451 firewall)
452 start_firewall
453 ;;
454 esac