do not fire up the qos-scripts hotplug part if the init script is disabled
[openwrt.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_load /var/state/network
21 config_get "$1" ifname
22 )}
23 } || {
24 find_ifname() {
25 echo "Interface not found."
26 exit 1
27 }
28 }
29
30 parse_matching_rule() {
31 local var="$1"
32 local section="$2"
33 local options="$3"
34 local prefix="$4"
35 local suffix="$5"
36 local proto="$6"
37 local mport=""
38 local ports=""
39
40 append "$var" "$prefix" "$N"
41 for option in $options; do
42 case "$option" in
43 proto) config_get value "$section" proto; proto="${proto:-$value}";;
44 esac
45 done
46 config_get type "$section" TYPE
47 case "$type" in
48 classify) unset pkt; append "$var" "-m mark --mark 0";;
49 default) pkt=1; append "$var" "-m mark --mark 0";;
50 reclassify) pkt=1;;
51 esac
52 append "$var" "${proto:+-p $proto}"
53 for option in $options; do
54 config_get value "$section" "$option"
55
56 case "$pkt:$option" in
57 *:srchost)
58 append "$var" "-s $value"
59 ;;
60 *:dsthost)
61 append "$var" "-d $value"
62 ;;
63 *:ipp2p)
64 add_insmod ipt_ipp2p
65 append "$var" "-m ipp2p"
66 case "$value" in
67 all) append "$var" "--edk --dc --kazaa --gnu --bit";;
68 *) append "$var" "--$value";;
69 esac
70 ;;
71 *:layer7)
72 add_insmod ipt_layer7
73 append "$var" "-m layer7 --l7proto $value${pkt:+ --l7pkt}"
74 ;;
75 *:ports|*:srcports|*:dstports)
76 value="$(echo "$value" | sed -e 's,-,:,g')"
77 lproto=${lproto:-tcp}
78 case "$proto" in
79 ""|tcp|udp) append "$var" "-m ${proto:-tcp -p tcp} -m multiport";;
80 *) unset "$var"; return 0;;
81 esac
82 case "$option" in
83 ports)
84 config_set "$section" srcports ""
85 config_set "$section" dstports ""
86 config_set "$section" portrange ""
87 append "$var" "--ports $value"
88 ;;
89 srcports)
90 config_set "$section" ports ""
91 config_set "$section" dstports ""
92 config_set "$section" portrange ""
93 append "$var" "--sports $value"
94 ;;
95 dstports)
96 config_set "$section" ports ""
97 config_set "$section" srcports ""
98 config_set "$section" portrange ""
99 append "$var" "--dports $value"
100 ;;
101 esac
102 ports=1
103 ;;
104 *:portrange)
105 config_set "$section" ports ""
106 config_set "$section" srcports ""
107 config_set "$section" dstports ""
108 value="$(echo "$value" | sed -e 's,-,:,g')"
109 case "$proto" in
110 ""|tcp|udp) append "$var" "-m ${proto:-tcp -p tcp} --sport $value --dport $value";;
111 *) unset "$var"; return 0;;
112 esac
113 ports=1
114 ;;
115 *:connbytes)
116 value="$(echo "$value" | sed -e 's,-,:,g')"
117 add_insmod ipt_connbytes
118 append "$var" "-m connbytes --connbytes $value --connbytes-dir both --connbytes-mode bytes"
119 ;;
120 *:tos)
121 add_insmod ipt_tos
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 ipt_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 ipt_length
147 append "$var" "-m length --length $value"
148 ;;
149 1:limit)
150 add_insmod ipt_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";;
164 *) append "$var" "-m mark --mark $class";;
165 esac
166 ;;
167 1:TOS)
168 add_insmod ipt_TOS
169 config_get TOS "$rule" 'TOS'
170 suffix="-j TOS --set-tos "${TOS:-"Normal-Service"}
171 ;;
172 1:DSCP)
173 add_insmod ipt_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" imqdev "$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:-eth0}"
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 -f $dir/tcrules.awk
272 }
273
274 start_interface() {
275 local iface="$1"
276 local num_imq="$2"
277 config_get device "$iface" device
278 config_get_bool enabled "$iface" enabled 1
279 [ -z "$device" -o 1 -ne "$enabled" ] && {
280 return 1
281 }
282 config_get upload "$iface" upload
283 config_get halfduplex "$iface" halfduplex
284 config_get download "$iface" download
285 config_get classgroup "$iface" classgroup
286 config_get_bool overhead "$iface" overhead 0
287
288 download="${download:-${halfduplex:+$upload}}"
289 enum_classes "$classgroup"
290 for dir in up${halfduplex} ${download:+down}; do
291 case "$dir" in
292 up)
293 [ "$overhead" = 1 ] && upload=$(($upload * 98 / 100 - (15 * 128 / $upload)))
294 dev="$device"
295 rate="$upload"
296 dl_mode=""
297 prefix="cls"
298 ;;
299 down)
300 add_insmod imq numdevs="$num_imq"
301 config_get imqdev "$iface" imqdev
302 [ "$overhead" = 1 ] && download=$(($download * 98 / 100 - (80 * 1024 / $download)))
303 dev="imq$imqdev"
304 rate="$download"
305 dl_mode=1
306 prefix="d_cls"
307 ;;
308 *) continue;;
309 esac
310 cstr=
311 for class in $classes; do
312 cls_var pktsize "$class" packetsize $dir 1500
313 cls_var pktdelay "$class" packetdelay $dir 0
314 cls_var maxrate "$class" limitrate $dir 100
315 cls_var prio "$class" priority $dir 1
316 cls_var avgrate "$class" avgrate $dir 0
317 config_get classnr "$class" classnr
318 append cstr "$classnr:$prio:$avgrate:$pktsize:$pktdelay:$maxrate" "$N"
319 done
320 append ${prefix}q "$(tcrules)" "$N"
321 export dev_${dir}="ifconfig $dev up txqueuelen 5 >&- 2>&-
322 tc qdisc del dev $dev root >&- 2>&-
323 tc qdisc add dev $dev root handle 1: hfsc default ${class_default}0
324 tc class add dev $dev parent 1: classid 1:1 hfsc sc rate ${rate}kbit ul rate ${rate}kbit"
325 done
326 add_insmod cls_fw
327 add_insmod sch_hfsc
328 add_insmod sch_sfq
329 add_insmod sch_red
330
331 cat <<EOF
332 ${INSMOD:+$INSMOD$N}${dev_up:+$dev_up
333 $clsq
334 }${imqdev:+$dev_down
335 $d_clsq
336 $d_clsl
337 $d_clsf
338 }
339 EOF
340 unset INSMOD clsq clsf clsl d_clsq d_clsl d_clsf dev_up dev_down
341 }
342
343 start_interfaces() {
344 local C="$1"
345 for iface in $INTERFACES; do
346 start_interface "$iface" "$C"
347 done
348 }
349
350 add_rules() {
351 local var="$1"
352 local rules="$2"
353 local prefix="$3"
354
355 for rule in $rules; do
356 unset iptrule
357 config_get target "$rule" target
358 config_get target "$target" classnr
359 config_get options "$rule" options
360
361 ## If we want to override the TOS field, let's clear the DSCP field first.
362 [ ! -z "$(echo $options | grep 'TOS')" ] && {
363 s_options=${options%%TOS}
364 add_insmod ipt_DSCP
365 parse_matching_rule iptrule "$rule" "$s_options" "$prefix" "-j DSCP --set-dscp 0"
366 append "$var" "$iptrule" "$N"
367 unset iptrule
368 }
369
370 parse_matching_rule iptrule "$rule" "$options" "$prefix" "-j MARK --set-mark $target"
371 append "$var" "$iptrule" "$N"
372 done
373 }
374
375 start_cg() {
376 local cg="$1"
377 local iptrules
378 local pktrules
379 local sizerules
380 local download
381 enum_classes "$cg"
382 add_rules iptrules "$ctrules" "iptables -t mangle -A ${cg}_ct"
383 config_get classes "$cg" classes
384 for class in $classes; do
385 config_get mark "$class" classnr
386 config_get maxsize "$class" maxsize
387 [ -z "$maxsize" -o -z "$mark" ] || {
388 add_insmod ipt_length
389 append pktrules "iptables -t mangle -A ${cg} -m mark --mark $mark -m length --length $maxsize: -j MARK --set-mark 0" "$N"
390 }
391 done
392 add_rules pktrules "$rules" "iptables -t mangle -A ${cg}"
393 for iface in $INTERFACES; do
394 config_get classgroup "$iface" classgroup
395 config_get device "$iface" device
396 config_get imqdev "$iface" imqdev
397 config_get dl "$iface" download
398 config_get halfduplex "$iface" halfduplex
399 add_insmod ipt_IMQ
400 append up "iptables -t mangle -A OUTPUT -o $device -j ${cg}" "$N"
401 append up "iptables -t mangle -A FORWARD -o $device -j ${cg}" "$N"
402 [ -z "$dl" ] || {
403 [ -z "$halfduplex" ] || {
404 append down "iptables -t mangle -A POSTROUTING -o $device -j IMQ --todev $imqdev" "$N"
405 }
406 append down "iptables -t mangle -A PREROUTING -i $device -j ${cg}" "$N"
407 append down "iptables -t mangle -A POSTROUTING -o $device -j ${cg}" "$N"
408 append down "iptables -t mangle -A PREROUTING -i $device -j IMQ --todev $imqdev" "$N"
409 }
410 done
411 cat <<EOF
412 $INSMOD
413 iptables -t mangle -N ${cg} >&- 2>&-
414 iptables -t mangle -N ${cg}_ct >&- 2>&-
415 ${iptrules:+${iptrules}${N}iptables -t mangle -A ${cg}_ct -j CONNMARK --save-mark}
416 iptables -t mangle -A ${cg} -j CONNMARK --restore-mark
417 iptables -t mangle -A ${cg} -m mark --mark 0 -j ${cg}_ct
418 $pktrules
419 $up$N${down:+${down}$N}
420 EOF
421 unset INSMOD
422 }
423
424 start_firewall() {
425 add_insmod ipt_multiport
426 add_insmod ipt_CONNMARK
427 cat <<EOF
428 iptables -t mangle -F
429 iptables -t mangle -X
430 EOF
431 for group in $CG; do
432 start_cg $group
433 done
434 }
435
436 C="0"
437 INTERFACES=""
438 [ -e ./qos.conf ] && {
439 . ./qos.conf
440 config_cb
441 } || config_load qos
442
443 C="0"
444 for iface in $INTERFACES; do
445 export C="$(($C + 1))"
446 done
447
448 case "$1" in
449 all)
450 start_interfaces "$C"
451 start_firewall
452 ;;
453 interface)
454 start_interface "$2" "$C"
455 ;;
456 interfaces)
457 start_interfaces
458 ;;
459 firewall)
460 start_firewall
461 ;;
462 esac
This page took 0.080226 seconds and 5 git commands to generate.