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