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