62e7283633e08ef826654e0168233da1a839ca22
[openwrt/staging/ansuel.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}="ip link add ${dev} type ifb >&- 2>&-
330 ip link set $dev up >&- 2>&-
331 tc qdisc del dev $dev root >&- 2>&-
332 tc qdisc add dev $dev root handle 1: hfsc default ${class_default}0
333 tc class add dev $dev parent 1: classid 1:1 hfsc sc rate ${rate}kbit ul rate ${rate}kbit"
334 done
335 [ -n "$download" ] && {
336 add_insmod cls_u32
337 add_insmod em_u32
338 add_insmod act_connmark
339 add_insmod act_mirred
340 add_insmod sch_ingress
341 }
342 if [ -n "$halfduplex" ]; then
343 export dev_up="tc qdisc del dev $device root >&- 2>&-
344 tc qdisc add dev $device root handle 1: hfsc
345 tc filter add dev $device parent 1: prio 10 u32 match u32 0 0 flowid 1:1 action mirred egress redirect dev ifb$ifbdev"
346 elif [ -n "$download" ]; then
347 append dev_${dir} "tc qdisc del dev $device ingress >&- 2>&-
348 tc qdisc add dev $device ingress
349 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"
350 fi
351 add_insmod cls_fw
352 add_insmod sch_hfsc
353
354 cat <<EOF
355 ${INSMOD:+$INSMOD$N}${dev_up:+$dev_up
356 $clsq
357 }${ifbdev:+$dev_down
358 $d_clsq
359 $d_clsl
360 $d_clsf
361 }
362 EOF
363 unset INSMOD clsq clsf clsl d_clsq d_clsl d_clsf dev_up dev_down
364 }
365
366 start_interfaces() {
367 local C="$1"
368 for iface in $INTERFACES; do
369 start_interface "$iface" "$C"
370 done
371 }
372
373 add_rules() {
374 local var="$1"
375 local rules="$2"
376 local prefix="$3"
377
378 for rule in $rules; do
379 unset iptrule
380 config_get target "$rule" target
381 config_get target "$target" classnr
382 config_get options "$rule" options
383
384 ## If we want to override the TOS field, let's clear the DSCP field first.
385 [ ! -z "$(echo $options | grep 'TOS')" ] && {
386 s_options=${options%%TOS}
387 add_insmod xt_DSCP
388 parse_matching_rule iptrule "$rule" "$s_options" "$prefix" "-j DSCP --set-dscp 0"
389 append "$var" "$iptrule" "$N"
390 unset iptrule
391 }
392
393 target=$(($target | ($target << 4)))
394 parse_matching_rule iptrule "$rule" "$options" "$prefix" "-j MARK --set-mark $target/0xff"
395 append "$var" "$iptrule" "$N"
396 done
397 }
398
399 start_cg() {
400 local cg="$1"
401 local iptrules
402 local pktrules
403 local sizerules
404 enum_classes "$cg"
405 for command in $iptables; do
406 add_rules iptrules "$ctrules" "$command -w -t mangle -A qos_${cg}_ct"
407 done
408 config_get classes "$cg" classes
409 for class in $classes; do
410 config_get mark "$class" classnr
411 config_get maxsize "$class" maxsize
412 [ -z "$maxsize" -o -z "$mark" ] || {
413 add_insmod xt_length
414 for command in $iptables; do
415 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"
416 done
417 }
418 done
419 for command in $iptables; do
420 add_rules pktrules "$rules" "$command -w -t mangle -A qos_${cg}"
421 done
422 for iface in $INTERFACES; do
423 config_get classgroup "$iface" classgroup
424 config_get device "$iface" device
425 config_get ifbdev "$iface" ifbdev
426 config_get upload "$iface" upload
427 config_get download "$iface" download
428 config_get halfduplex "$iface" halfduplex
429 download="${download:-${halfduplex:+$upload}}"
430 for command in $iptables; do
431 append up "$command -w -t mangle -A OUTPUT -o $device -j qos_${cg}" "$N"
432 append up "$command -w -t mangle -A FORWARD -o $device -j qos_${cg}" "$N"
433 done
434 done
435 cat <<EOF
436 $INSMOD
437 EOF
438
439 for command in $iptables; do
440 cat <<EOF
441 $command -w -t mangle -N qos_${cg}
442 $command -w -t mangle -N qos_${cg}_ct
443 EOF
444 done
445 cat <<EOF
446 ${iptrules:+${iptrules}${N}}
447 EOF
448 for command in $iptables; do
449 cat <<EOF
450 $command -w -t mangle -A qos_${cg}_ct -j CONNMARK --save-mark --mask 0xff
451 $command -w -t mangle -A qos_${cg} -j CONNMARK --restore-mark --mask 0x0f
452 $command -w -t mangle -A qos_${cg} -m mark --mark 0/0x0f -j qos_${cg}_ct
453 EOF
454 done
455 cat <<EOF
456 $pktrules
457 EOF
458 for command in $iptables; do
459 cat <<EOF
460 $command -w -t mangle -A qos_${cg} -j CONNMARK --save-mark --mask 0xff
461 EOF
462 done
463 cat <<EOF
464 $up$N${down:+${down}$N}
465 EOF
466 unset INSMOD
467 }
468
469 start_firewall() {
470 add_insmod xt_multiport
471 add_insmod xt_connmark
472 stop_firewall
473 for group in $CG; do
474 start_cg $group
475 done
476 }
477
478 stop_firewall() {
479 # Builds up a list of iptables commands to flush the qos_* chains,
480 # remove rules referring to them, then delete them
481
482 # Print rules in the mangle table, like iptables-save
483 for command in $iptables; do
484 $command -w -t mangle -S |
485 # Find rules for the qos_* chains
486 grep -E '(^-N qos_|-j qos_)' |
487 # Exclude rules in qos_* chains (inter-qos_* refs)
488 grep -v '^-A qos_' |
489 # Replace -N with -X and hold, with -F and print
490 # Replace -A with -D
491 # Print held lines at the end (note leading newline)
492 sed -e '/^-N/{s/^-N/-X/;H;s/^-X/-F/}' \
493 -e 's/^-A/-D/' \
494 -e '${p;g}' |
495 # Make into proper iptables calls
496 # Note: awkward in previous call due to hold space usage
497 sed -n -e "s/^./${command} -w -t mangle &/p"
498 done
499 }
500
501 C="0"
502 INTERFACES=""
503 [ -e ./qos.conf ] && {
504 . ./qos.conf
505 config_cb
506 } || {
507 config_load qos
508 config_foreach qos_parse_config
509 }
510
511 C="0"
512 for iface in $INTERFACES; do
513 export C="$(($C + 1))"
514 done
515
516 [ -x /usr/sbin/ip6tables ] && {
517 iptables="ip6tables iptables"
518 } || {
519 iptables="iptables"
520 }
521
522 case "$1" in
523 all)
524 start_interfaces "$C"
525 start_firewall
526 ;;
527 interface)
528 start_interface "$2" "$C"
529 ;;
530 interfaces)
531 start_interfaces
532 ;;
533 firewall)
534 case "$2" in
535 stop)
536 stop_firewall
537 ;;
538 start|"")
539 start_firewall
540 ;;
541 esac
542 ;;
543 esac