qos-scripts: fix interface resolving
[openwrt/staging/rmilecki.git] / package / network / config / qos-scripts / files / usr / lib / qos / generate.sh
1 #!/bin/sh
2 [ -e /lib/functions.sh ] && . /lib/functions.sh || . ./functions.sh
3 [ -x /sbin/modprobe ] && {
4 insmod="modprobe"
5 rmmod="$insmod -r"
6 } || {
7 insmod="insmod"
8 rmmod="rmmod"
9 }
10
11 add_insmod() {
12 eval "export isset=\${insmod_$1}"
13 case "$isset" in
14 1) ;;
15 *) {
16 [ "$2" ] && append INSMOD "$rmmod $1 >&- 2>&-" "$N"
17 append INSMOD "$insmod $* >&- 2>&-" "$N"; export insmod_$1=1
18 };;
19 esac
20 }
21
22 [ -e /etc/config/network ] && {
23 # only try to parse network config on openwrt
24
25 . /lib/functions/network.sh
26
27 find_ifname() {
28 local ifname
29 if network_get_device ifname "$1"; then
30 echo "$ifname"
31 else
32 echo "Device for interface $1 not found." >&2
33 exit 1
34 fi
35 }
36 } || {
37 find_ifname() {
38 echo "Interface not found." >&2
39 exit 1
40 }
41 }
42
43 parse_matching_rule() {
44 local var="$1"
45 local section="$2"
46 local options="$3"
47 local prefix="$4"
48 local suffix="$5"
49 local proto="$6"
50 local mport=""
51 local ports=""
52
53 append "$var" "$prefix" "$N"
54 for option in $options; do
55 case "$option" in
56 proto) config_get value "$section" proto; proto="${proto:-$value}";;
57 esac
58 done
59 config_get type "$section" TYPE
60 case "$type" in
61 classify) unset pkt; append "$var" "-m mark --mark 0/0x0f";;
62 default) pkt=1; append "$var" "-m mark --mark 0/0xf0";;
63 reclassify) pkt=1;;
64 esac
65 append "$var" "${proto:+-p $proto}"
66 for option in $options; do
67 config_get value "$section" "$option"
68
69 case "$pkt:$option" in
70 *:srchost)
71 append "$var" "-s $value"
72 ;;
73 *:dsthost)
74 append "$var" "-d $value"
75 ;;
76 *:ports|*:srcports|*:dstports)
77 value="$(echo "$value" | sed -e 's,-,:,g')"
78 lproto=${lproto:-tcp}
79 case "$proto" in
80 ""|tcp|udp) append "$var" "-m ${proto:-tcp -p tcp} -m multiport";;
81 *) unset "$var"; return 0;;
82 esac
83 case "$option" in
84 ports)
85 config_set "$section" srcports ""
86 config_set "$section" dstports ""
87 config_set "$section" portrange ""
88 append "$var" "--ports $value"
89 ;;
90 srcports)
91 config_set "$section" ports ""
92 config_set "$section" dstports ""
93 config_set "$section" portrange ""
94 append "$var" "--sports $value"
95 ;;
96 dstports)
97 config_set "$section" ports ""
98 config_set "$section" srcports ""
99 config_set "$section" portrange ""
100 append "$var" "--dports $value"
101 ;;
102 esac
103 ports=1
104 ;;
105 *:portrange)
106 config_set "$section" ports ""
107 config_set "$section" srcports ""
108 config_set "$section" dstports ""
109 value="$(echo "$value" | sed -e 's,-,:,g')"
110 case "$proto" in
111 ""|tcp|udp) append "$var" "-m ${proto:-tcp -p tcp} --sport $value --dport $value";;
112 *) unset "$var"; return 0;;
113 esac
114 ports=1
115 ;;
116 *:connbytes)
117 value="$(echo "$value" | sed -e 's,-,:,g')"
118 add_insmod xt_connbytes
119 append "$var" "-m connbytes --connbytes $value --connbytes-dir both --connbytes-mode bytes"
120 ;;
121 *:comment)
122 add_insmod xt_comment
123 append "$var" "-m comment --comment '$value'"
124 ;;
125 *:tos)
126 add_insmod xt_dscp
127 case "$value" in
128 !*) append "$var" "-m tos ! --tos $value";;
129 *) append "$var" "-m tos --tos $value"
130 esac
131 ;;
132 *:dscp)
133 add_insmod xt_dscp
134 dscp_option="--dscp"
135 [ -z "${value%%[EBCA]*}" ] && dscp_option="--dscp-class"
136 case "$value" in
137 !*) append "$var" "-m dscp ! $dscp_option $value";;
138 *) append "$var" "-m dscp $dscp_option $value"
139 esac
140 ;;
141 *:direction)
142 value="$(echo "$value" | sed -e 's,-,:,g')"
143 if [ "$value" = "out" ]; then
144 append "$var" "-o $device"
145 elif [ "$value" = "in" ]; then
146 append "$var" "-i $device"
147 fi
148 ;;
149 *:srciface)
150 append "$var" "-i $value"
151 ;;
152 1:pktsize)
153 value="$(echo "$value" | sed -e 's,-,:,g')"
154 add_insmod xt_length
155 append "$var" "-m length --length $value"
156 ;;
157 1:limit)
158 add_insmod xt_limit
159 append "$var" "-m limit --limit $value"
160 ;;
161 1:tcpflags)
162 case "$proto" in
163 tcp) append "$var" "-m tcp --tcp-flags ALL $value";;
164 *) unset $var; return 0;;
165 esac
166 ;;
167 1:mark)
168 config_get class "${value##!}" classnr
169 [ -z "$class" ] && continue;
170 case "$value" in
171 !*) append "$var" "-m mark ! --mark $class/0x0f";;
172 *) append "$var" "-m mark --mark $class/0x0f";;
173 esac
174 ;;
175 1:TOS)
176 add_insmod xt_DSCP
177 config_get TOS "$rule" 'TOS'
178 suffix="-j TOS --set-tos "${TOS:-"Normal-Service"}
179 ;;
180 1:DSCP)
181 add_insmod xt_DSCP
182 config_get DSCP "$rule" 'DSCP'
183 [ -z "${DSCP%%[EBCA]*}" ] && set_value="--set-dscp-class $DSCP" \
184 || set_value="--set-dscp $DSCP"
185 suffix="-j DSCP $set_value"
186 ;;
187 esac
188 done
189 append "$var" "$suffix"
190 case "$ports:$proto" in
191 1:) parse_matching_rule "$var" "$section" "$options" "$prefix" "$suffix" "udp";;
192 esac
193 }
194
195 config_cb() {
196 option_cb() {
197 return 0
198 }
199 case "$1" in
200 interface)
201 config_set "$2" "classgroup" "Default"
202 config_set "$2" "upload" "128"
203 ;;
204 classify|default|reclassify)
205 option_cb() {
206 append "CONFIG_${CONFIG_SECTION}_options" "$1"
207 }
208 ;;
209 esac
210 }
211
212 qos_parse_config() {
213 config_get TYPE "$1" TYPE
214 case "$TYPE" in
215 interface)
216 config_get_bool enabled "$1" enabled 1
217 [ 1 -eq "$enabled" ] && {
218 config_get classgroup "$1" classgroup
219 config_set "$1" ifbdev "$C"
220 C=$(($C+1))
221 append INTERFACES "$1"
222 config_set "$classgroup" enabled 1
223 config_get device "$1" device
224 [ -z "$device" ] && {
225 device="$(find_ifname $1)"
226 [ -z "$device" ] && exit 1
227 config_set "$1" device "$device"
228 }
229 }
230 ;;
231 classgroup) append CG "$1";;
232 classify|default|reclassify)
233 case "$TYPE" in
234 classify) var="ctrules";;
235 *) var="rules";;
236 esac
237 append "$var" "$1"
238 ;;
239 esac
240 }
241
242 enum_classes() {
243 local c="0"
244 config_get classes "$1" classes
245 config_get default "$1" default
246 for class in $classes; do
247 c="$(($c + 1))"
248 config_set "${class}" classnr $c
249 case "$class" in
250 $default) class_default=$c;;
251 esac
252 done
253 class_default="${class_default:-$c}"
254 }
255
256 cls_var() {
257 local varname="$1"
258 local class="$2"
259 local name="$3"
260 local type="$4"
261 local default="$5"
262 local tmp tmp1 tmp2
263 config_get tmp1 "$class" "$name"
264 config_get tmp2 "${class}_${type}" "$name"
265 tmp="${tmp2:-$tmp1}"
266 tmp="${tmp:-$tmp2}"
267 export ${varname}="${tmp:-$default}"
268 }
269
270 tcrules() {
271 _dir=/usr/lib/qos
272 [ -e $_dir/tcrules.awk ] || _dir=.
273 echo "$cstr" | awk \
274 -v device="$dev" \
275 -v linespeed="$rate" \
276 -v direction="$dir" \
277 -f $_dir/tcrules.awk
278 }
279
280 start_interface() {
281 local iface="$1"
282 local num_ifb="$2"
283 config_get device "$iface" device
284 config_get_bool enabled "$iface" enabled 1
285 [ -z "$device" -o 1 -ne "$enabled" ] && {
286 return 1
287 }
288 config_get upload "$iface" upload
289 config_get_bool halfduplex "$iface" halfduplex
290 config_get download "$iface" download
291 config_get classgroup "$iface" classgroup
292 config_get_bool overhead "$iface" overhead 0
293
294 download="${download:-${halfduplex:+$upload}}"
295 enum_classes "$classgroup"
296 for dir in ${halfduplex:-up} ${download:+down}; do
297 case "$dir" in
298 up)
299 [ "$overhead" = 1 ] && upload=$(($upload * 98 / 100 - (15 * 128 / $upload)))
300 dev="$device"
301 rate="$upload"
302 dl_mode=""
303 prefix="cls"
304 ;;
305 down)
306 [ "$(ls -d /proc/sys/net/ipv4/conf/ifb* 2>&- | wc -l)" -ne "$num_ifb" ] && add_insmod ifb numifbs="$num_ifb"
307 config_get ifbdev "$iface" ifbdev
308 [ "$overhead" = 1 ] && download=$(($download * 98 / 100 - (80 * 1024 / $download)))
309 dev="ifb$ifbdev"
310 rate="$download"
311 dl_mode=1
312 prefix="d_cls"
313 ;;
314 *) continue;;
315 esac
316 cstr=
317 for class in $classes; do
318 cls_var pktsize "$class" packetsize $dir 1500
319 cls_var pktdelay "$class" packetdelay $dir 0
320 cls_var maxrate "$class" limitrate $dir 100
321 cls_var prio "$class" priority $dir 1
322 cls_var avgrate "$class" avgrate $dir 0
323 cls_var qdisc "$class" qdisc $dir ""
324 cls_var filter "$class" filter $dir ""
325 config_get classnr "$class" classnr
326 append cstr "$classnr:$prio:$avgrate:$pktsize:$pktdelay:$maxrate:$qdisc:$filter" "$N"
327 done
328 append ${prefix}q "$(tcrules)" "$N"
329 export dev_${dir}="ifconfig $dev up >&- 2>&-
330 tc qdisc del dev $dev root >&- 2>&-
331 tc qdisc add dev $dev root handle 1: hfsc default ${class_default}0
332 tc class add dev $dev parent 1: classid 1:1 hfsc sc rate ${rate}kbit ul rate ${rate}kbit"
333 done
334 [ -n "$download" ] && {
335 add_insmod cls_u32
336 add_insmod em_u32
337 add_insmod act_connmark
338 add_insmod act_mirred
339 add_insmod sch_ingress
340 }
341 if [ -n "$halfduplex" ]; then
342 export dev_up="tc qdisc del dev $device root >&- 2>&-
343 tc qdisc add dev $device root handle 1: hfsc
344 tc filter add dev $device parent 1: prio 10 u32 match u32 0 0 flowid 1:1 action mirred egress redirect dev ifb$ifbdev"
345 elif [ -n "$download" ]; then
346 append dev_${dir} "tc qdisc del dev $device ingress >&- 2>&-
347 tc qdisc add dev $device ingress
348 tc filter add dev $device parent ffff: prio 1 u32 match u32 0 0 flowid 1:1 action connmark action mirred egress redirect dev ifb$ifbdev" "$N"
349 fi
350 add_insmod cls_fw
351 add_insmod sch_hfsc
352
353 cat <<EOF
354 ${INSMOD:+$INSMOD$N}${dev_up:+$dev_up
355 $clsq
356 }${ifbdev:+$dev_down
357 $d_clsq
358 $d_clsl
359 $d_clsf
360 }
361 EOF
362 unset INSMOD clsq clsf clsl d_clsq d_clsl d_clsf dev_up dev_down
363 }
364
365 start_interfaces() {
366 local C="$1"
367 for iface in $INTERFACES; do
368 start_interface "$iface" "$C"
369 done
370 }
371
372 add_rules() {
373 local var="$1"
374 local rules="$2"
375 local prefix="$3"
376
377 for rule in $rules; do
378 unset iptrule
379 config_get target "$rule" target
380 config_get target "$target" classnr
381 config_get options "$rule" options
382
383 ## If we want to override the TOS field, let's clear the DSCP field first.
384 [ ! -z "$(echo $options | grep 'TOS')" ] && {
385 s_options=${options%%TOS}
386 add_insmod xt_DSCP
387 parse_matching_rule iptrule "$rule" "$s_options" "$prefix" "-j DSCP --set-dscp 0"
388 append "$var" "$iptrule" "$N"
389 unset iptrule
390 }
391
392 target=$(($target | ($target << 4)))
393 parse_matching_rule iptrule "$rule" "$options" "$prefix" "-j MARK --set-mark $target/0xff"
394 append "$var" "$iptrule" "$N"
395 done
396 }
397
398 start_cg() {
399 local cg="$1"
400 local iptrules
401 local pktrules
402 local sizerules
403 enum_classes "$cg"
404 for command in $iptables; do
405 add_rules iptrules "$ctrules" "$command -w -t mangle -A qos_${cg}_ct"
406 done
407 config_get classes "$cg" classes
408 for class in $classes; do
409 config_get mark "$class" classnr
410 config_get maxsize "$class" maxsize
411 [ -z "$maxsize" -o -z "$mark" ] || {
412 add_insmod xt_length
413 for command in $iptables; do
414 append pktrules "$command -w -t mangle -A qos_${cg} -m mark --mark $mark/0x0f -m length --length $maxsize: -j MARK --set-mark 0/0xff" "$N"
415 done
416 }
417 done
418 for command in $iptables; do
419 add_rules pktrules "$rules" "$command -w -t mangle -A qos_${cg}"
420 done
421 for iface in $INTERFACES; do
422 config_get classgroup "$iface" classgroup
423 config_get device "$iface" device
424 config_get ifbdev "$iface" ifbdev
425 config_get upload "$iface" upload
426 config_get download "$iface" download
427 config_get halfduplex "$iface" halfduplex
428 download="${download:-${halfduplex:+$upload}}"
429 for command in $iptables; do
430 append up "$command -w -t mangle -A OUTPUT -o $device -j qos_${cg}" "$N"
431 append up "$command -w -t mangle -A FORWARD -o $device -j qos_${cg}" "$N"
432 done
433 done
434 cat <<EOF
435 $INSMOD
436 EOF
437
438 for command in $iptables; do
439 cat <<EOF
440 $command -w -t mangle -N qos_${cg}
441 $command -w -t mangle -N qos_${cg}_ct
442 EOF
443 done
444 cat <<EOF
445 ${iptrules:+${iptrules}${N}}
446 EOF
447 for command in $iptables; do
448 cat <<EOF
449 $command -w -t mangle -A qos_${cg}_ct -j CONNMARK --save-mark --mask 0xff
450 $command -w -t mangle -A qos_${cg} -j CONNMARK --restore-mark --mask 0x0f
451 $command -w -t mangle -A qos_${cg} -m mark --mark 0/0x0f -j qos_${cg}_ct
452 EOF
453 done
454 cat <<EOF
455 $pktrules
456 EOF
457 for command in $iptables; do
458 cat <<EOF
459 $command -w -t mangle -A qos_${cg} -j CONNMARK --save-mark --mask 0xff
460 EOF
461 done
462 cat <<EOF
463 $up$N${down:+${down}$N}
464 EOF
465 unset INSMOD
466 }
467
468 start_firewall() {
469 add_insmod xt_multiport
470 add_insmod xt_connmark
471 stop_firewall
472 for group in $CG; do
473 start_cg $group
474 done
475 }
476
477 stop_firewall() {
478 # Builds up a list of iptables commands to flush the qos_* chains,
479 # remove rules referring to them, then delete them
480
481 # Print rules in the mangle table, like iptables-save
482 for command in $iptables; do
483 $command -w -t mangle -S |
484 # Find rules for the qos_* chains
485 grep -E '(^-N qos_|-j qos_)' |
486 # Exclude rules in qos_* chains (inter-qos_* refs)
487 grep -v '^-A qos_' |
488 # Replace -N with -X and hold, with -F and print
489 # Replace -A with -D
490 # Print held lines at the end (note leading newline)
491 sed -e '/^-N/{s/^-N/-X/;H;s/^-X/-F/}' \
492 -e 's/^-A/-D/' \
493 -e '${p;g}' |
494 # Make into proper iptables calls
495 # Note: awkward in previous call due to hold space usage
496 sed -n -e "s/^./${command} -w -t mangle &/p"
497 done
498 }
499
500 C="0"
501 INTERFACES=""
502 [ -e ./qos.conf ] && {
503 . ./qos.conf
504 config_cb
505 } || {
506 config_load qos
507 config_foreach qos_parse_config
508 }
509
510 C="0"
511 for iface in $INTERFACES; do
512 export C="$(($C + 1))"
513 done
514
515 [ -x /usr/sbin/ip6tables ] && {
516 iptables="ip6tables iptables"
517 } || {
518 iptables="iptables"
519 }
520
521 case "$1" in
522 all)
523 start_interfaces "$C"
524 start_firewall
525 ;;
526 interface)
527 start_interface "$2" "$C"
528 ;;
529 interfaces)
530 start_interfaces
531 ;;
532 firewall)
533 case "$2" in
534 stop)
535 stop_firewall
536 ;;
537 start|"")
538 start_firewall
539 ;;
540 esac
541 ;;
542 esac