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