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