qos-scripts update:
[openwrt/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 echo "Interface '$iface' not found or disabled." >&2
251 return 1
252 }
253 config_get upload "$iface" upload
254 config_get halfduplex "$iface" halfduplex
255 config_get download "$iface" download
256 config_get classgroup "$iface" classgroup
257 config_get_bool overhead "$iface" overhead 0
258
259 download="${download:-${halfduplex:+$upload}}"
260 enum_classes "$classgroup"
261 for dir in up${halfduplex} ${download:+down}; do
262 case "$dir" in
263 up)
264 [ "$overhead" = 1 ] && upload=$(($upload * 98 / 100 - (15 * 128 / $upload)))
265 dev="$device"
266 rate="$upload"
267 dl_mode=""
268 prefix="cls"
269 ;;
270 down)
271 add_insmod imq numdevs="$num_imq"
272 config_get imqdev "$iface" imqdev
273 [ "$overhead" = 1 ] && download=$(($download * 98 / 100 - (80 * 1024 / $download)))
274 dev="imq$imqdev"
275 rate="$download"
276 dl_mode=1
277 prefix="d_cls"
278 ;;
279 *) continue;;
280 esac
281 cstr=
282 for class in $classes; do
283 cls_var pktsize "$class" packetsize $dir 1500
284 cls_var pktdelay "$class" packetdelay $dir 0
285 cls_var maxrate "$class" limitrate $dir 100
286 cls_var prio "$class" priority $dir 1
287 cls_var avgrate "$class" avgrate $dir 0
288 config_get classnr "$class" classnr
289 append cstr "$classnr:$prio:$avgrate:$pktsize:$pktdelay:$maxrate" "$N"
290 done
291 append ${prefix}q "$(tcrules)" "$N"
292 export dev_${dir}="ifconfig $dev up txqueuelen 5 >&- 2>&-
293 tc qdisc del dev $dev root >&- 2>&-
294 tc qdisc add dev $dev root handle 1: hfsc default ${class_default}0
295 tc class add dev $dev parent 1: classid 1:1 hfsc sc rate ${rate}kbit ul rate ${rate}kbit"
296 done
297 add_insmod cls_fw
298 add_insmod sch_hfsc
299 add_insmod sch_sfq
300 add_insmod sch_red
301
302 cat <<EOF
303 ${INSMOD:+$INSMOD$N}${dev_up:+$dev_up
304 $clsq
305 }${imqdev:+$dev_down
306 $d_clsq
307 $d_clsl
308 $d_clsf
309 }
310 EOF
311 unset INSMOD clsq clsf clsl d_clsq d_clsl d_clsf dev_up dev_down
312 }
313
314 start_interfaces() {
315 local C="$1"
316 for iface in $INTERFACES; do
317 start_interface "$iface" "$C"
318 done
319 }
320
321 add_rules() {
322 local var="$1"
323 local rules="$2"
324 local prefix="$3"
325
326 for rule in $rules; do
327 unset iptrule
328 config_get target "$rule" target
329 config_get target "$target" classnr
330 config_get options "$rule" options
331 parse_matching_rule iptrule "$rule" "$options" "$prefix" "-j MARK --set-mark $target"
332 append "$var" "$iptrule" "$N"
333 done
334 }
335
336 start_cg() {
337 local cg="$1"
338 local iptrules
339 local pktrules
340 local sizerules
341 local download
342 enum_classes "$cg"
343 add_rules iptrules "$ctrules" "iptables -t mangle -A ${cg}_ct"
344 config_get classes "$cg" classes
345 for class in $classes; do
346 config_get mark "$class" classnr
347 config_get maxsize "$class" maxsize
348 [ -z "$maxsize" -o -z "$mark" ] || {
349 add_insmod ipt_length
350 append pktrules "iptables -t mangle -A ${cg} -m mark --mark $mark -m length --length $maxsize: -j MARK --set-mark 0" "$N"
351 }
352 done
353 add_rules pktrules "$rules" "iptables -t mangle -A ${cg}"
354 for iface in $INTERFACES; do
355 config_get classgroup "$iface" classgroup
356 config_get device "$iface" device
357 config_get imqdev "$iface" imqdev
358 config_get dl "$iface" download
359 config_get halfduplex "$iface" halfduplex
360 add_insmod ipt_IMQ
361 append up "iptables -t mangle -A OUTPUT -o $device -j ${cg}" "$N"
362 append up "iptables -t mangle -A FORWARD -o $device -j ${cg}" "$N"
363 [ -z "$dl" ] || {
364 [ -z "$halfduplex" ] || {
365 append down "iptables -t mangle -A POSTROUTING -o $device -j IMQ --todev $imqdev" "$N"
366 }
367 append down "iptables -t mangle -A PREROUTING -i $device -j ${cg}" "$N"
368 append down "iptables -t mangle -A POSTROUTING -o $device -j ${cg}" "$N"
369 append down "iptables -t mangle -A PREROUTING -i $device -j IMQ --todev $imqdev" "$N"
370 }
371 done
372 cat <<EOF
373 $INSMOD
374 iptables -t mangle -N ${cg} >&- 2>&-
375 iptables -t mangle -N ${cg}_ct >&- 2>&-
376 ${iptrules:+${iptrules}${N}iptables -t mangle -A ${cg}_ct -j CONNMARK --save-mark}
377 iptables -t mangle -A ${cg} -j CONNMARK --restore-mark
378 iptables -t mangle -A ${cg} -m mark --mark 0 -j ${cg}_ct
379 $pktrules
380 $up$N${down:+${down}$N}
381 EOF
382 unset INSMOD
383 }
384
385 start_firewall() {
386 add_insmod ipt_multiport
387 add_insmod ipt_CONNMARK
388 cat <<EOF
389 iptables -t mangle -F
390 iptables -t mangle -X
391 EOF
392 for group in $CG; do
393 start_cg $group
394 done
395 }
396
397 C="0"
398 INTERFACES=""
399 [ -e ./qos.conf ] && {
400 . ./qos.conf
401 config_cb
402 } || config_load qos
403
404 C="0"
405 for iface in $INTERFACES; do
406 export C="$(($C + 1))"
407 done
408
409 case "$1" in
410 all)
411 start_interfaces "$C"
412 start_firewall
413 ;;
414 interface)
415 start_interface "$2" "$C"
416 ;;
417 interfaces)
418 start_interfaces
419 ;;
420 firewall)
421 start_firewall
422 ;;
423 esac