upgrade the ixp4xx ethernet driver to v0.3.1, split the Gateway 7001 support to two...
[openwrt.git] / package / qos-scripts / files / usr / lib / qos.sh
1 #!/bin/sh
2 . /etc/functions.sh
3
4 insmod="insmod"
5 [ -f /sbin/modprobe ] && insmod="modprobe"
6
7 add_insmod() {
8 eval "export isset=\${insmod_$1}"
9 case "$isset" in
10 1) ;;
11 *) append INSMOD "$insmod $* >&- 2>&-" "$N"; export insmod_$1=1;;
12 esac
13 }
14
15 find_ifname() {(
16 include /lib/network
17 scan_interfaces
18 config_get "$1" ifname
19 )}
20
21 parse_matching_rule() {
22 local var="$1"
23 local section="$2"
24 local options="$3"
25 local prefix="$4"
26 local suffix="$5"
27 local proto="$6"
28 local mport=""
29 local ports=""
30
31 append "$var" "$prefix" "$N"
32 for option in $options; do
33 case "$option" in
34 proto) config_get value "$section" proto; proto="${proto:-$value}";;
35 esac
36 done
37 config_get type "$section" TYPE
38 case "$type" in
39 classify) unset pkt; append "$var" "-m mark --mark 0";;
40 default) pkt=1; append "$var" "-m mark --mark 0";;
41 reclassify) pkt=1;;
42 esac
43 append "$var" "${proto:+-p $proto}"
44 for option in $options; do
45 config_get value "$section" "$option"
46
47 case "$pkt:$option" in
48 *:srchost)
49 append "$var" "-s $value"
50 ;;
51 *:dsthost)
52 append "$var" "-d $value"
53 ;;
54 *:ipp2p)
55 add_insmod ipt_ipp2p
56 append "$var" "-m ipp2p"
57 case "$value" in
58 all) append "$var" "--edk --dc --kazaa --gnu --bit";;
59 *) append "$var" "--$value";;
60 esac
61 ;;
62 *:layer7)
63 add_insmod ipt_layer7
64 append "$var" "-m layer7 --l7proto $value${pkt:+ --l7pkt}"
65 ;;
66 *:ports|*:srcports|*:dstports)
67 value="$(echo "$value" | sed -e 's,-,:,g')"
68 lproto=${lproto:-tcp}
69 case "$proto" in
70 ""|tcp|udp) append "$var" "-m ${proto:-tcp -p tcp} -m multiport";;
71 *) unset "$var"; return 0;;
72 esac
73 case "$option" in
74 ports)
75 config_set "$section" srcports ""
76 config_set "$section" dstports ""
77 config_set "$section" portrange ""
78 append "$var" "--ports $value"
79 ;;
80 srcports)
81 config_set "$section" ports ""
82 config_set "$section" dstports ""
83 config_set "$section" portrange ""
84 append "$var" "--sports $value"
85 ;;
86 dstports)
87 config_set "$section" ports ""
88 config_set "$section" srcports ""
89 config_set "$section" portrange ""
90 append "$var" "--dports $value"
91 ;;
92 esac
93 ports=1
94 ;;
95 *:portrange)
96 config_set "$section" ports ""
97 config_set "$section" srcports ""
98 config_set "$section" dstports ""
99 value="$(echo "$value" | sed -e 's,-,:,g')"
100 case "$proto" in
101 ""|tcp|udp) append "$var" "-m ${proto:-tcp -p tcp} --sport $value --dport $value";;
102 *) unset "$var"; return 0;;
103 esac
104 ports=1
105 ;;
106 1:pktsize)
107 value="$(echo "$value" | sed -e 's,-,:,g')"
108 add_insmod ipt_length
109 append "$var" "-m length --length $value"
110 ;;
111 1:limit)
112 add_insmod ipt_limit
113 append "$var" "-m limit --limit $value"
114 ;;
115 1:tcpflags)
116 case "$proto" in
117 tcp) append "$var" "-m tcp --tcp-flags ALL $value";;
118 *) unset $var; return 0;;
119 esac
120 ;;
121 1:mark)
122 config_get class "${value##!}" classnr
123 [ -z "$class" ] && continue;
124 case "$value" in
125 !*) append "$var" "-m mark ! --mark $class";;
126 *) append "$var" "-m mark --mark $class";;
127 esac
128 esac
129 done
130 append "$var" "$suffix"
131 case "$ports:$proto" in
132 1:) parse_matching_rule "$var" "$section" "$options" "$prefix" "$suffix" "udp";;
133 esac
134 }
135
136 config_cb() {
137 option_cb() {
138 return 0
139 }
140
141 # Section start
142 case "$1" in
143 interface)
144 config_set "$1" "classgroup" "Default"
145 config_set "$1" "upload" "128"
146 ;;
147 classify|default|reclassify)
148 option_cb() {
149 append options "$1"
150 }
151 ;;
152 esac
153
154 # Section end
155 config_get TYPE "$CONFIG_SECTION" TYPE
156 case "$TYPE" in
157 interface)
158 config_get enabled "$CONFIG_SECTION" enabled
159 config_get download "$CONFIG_SECTION" download
160 config_get classgroup "$CONFIG_SECTION" classgroup
161 config_set "$CONFIG_SECTION" imqdev "$C"
162 [ -z "$enabled" -o "$(($enabled))" -eq 0 ] || {
163 C=$(($C+1))
164 INTERFACES="$INTERFACES $CONFIG_SECTION"
165 config_set "$classgroup" enabled 1
166 }
167 config_get device "$CONFIG_SECTION" device
168 [ -z "$device" ] && device="$(find_ifname ${CONFIG_SECTION})"
169 config_set "$CONFIG_SECTION" device "${device:-eth0}"
170 ;;
171 classgroup) append CG "$CONFIG_SECTION";;
172 classify|default|reclassify)
173 case "$TYPE" in
174 classify) var="ctrules";;
175 *) var="rules";;
176 esac
177 config_get target "$CONFIG_SECTION" target
178 config_set "$CONFIG_SECTION" options "$options"
179 append "$var" "$CONFIG_SECTION"
180 unset options
181 ;;
182 esac
183 }
184
185 class_main_qdisc() {
186 local device="$1"
187 awk -f - <<EOF
188 BEGIN {
189 limit = int("$maxrate")
190 m2 = int("$m2")
191 dmax = int("$dmax")
192 umax = int("$umax")
193 share = int("$share")
194
195 if (!(m2 > 0)) {
196 dmax = 500
197 umax = 1500
198 m2 = 10
199 rt = 0
200 } else {
201 rt = 1
202 }
203
204 cdata = ""
205 pdmax = int (dmax + (umax * 8 / limit))
206 if (rt == 1) {
207 if (share > 0) cdata = " rt"
208 else cdata = " ls"
209 if ((umax > 0) && (dmax > 0)) {
210 cdata = cdata " umax " umax "b dmax " pdmax "ms"
211 }
212 cdata = cdata " rate " m2 "kbit"
213 }
214 if (share > 0) {
215 if ((m2 > 0) && (umax > 0) && (dmax > 0)) {
216 cdata = cdata " ls umax " umax "b dmax " pdmax "ms rate " share "kbit"
217 } else {
218 cdata = cdata " ls m1 " share "kbit d 500ms m2 " share "kbit"
219 }
220 }
221
222 print "tc class add dev $device parent 1:1 classid 1:${classnr}0 hfsc" cdata " ul rate " limit "kbit"
223 }
224 EOF
225 }
226
227 class_leaf_qdisc() {
228 local device="$1"
229 awk -f - <<EOF
230
231 function qlen(rate, m2, umax, dmax, qb, qr, qt, ql) {
232 qlen_min = 5 # minimum queue length
233 qlen_base = 1.7 # base value - queueing time in seconds
234 qlen_avgr = 0.7 # avgrate modifier
235 qlen_dmax = 0.0 # dmax modifier
236
237 # bits in a packet
238 qb = 1500
239 if ((m2 > 0) && (umax > 0)) qb -= int((1500 - umax) * qlen_pkt)
240 qb *= 8
241
242 # rate in bits/s
243 qr = rate
244 qr -= int((rate - m2) * qlen_avgr)
245 qr *= 1024
246
247 # queue time
248 qt = qlen_base + qlen_dmax * (dmax / 1000)
249
250 # queue length
251 ql = int(qr * qt / qb)
252 if (ql < qlen_min) ql = qlen_min
253
254 return ql
255 }
256
257 BEGIN {
258 sfq_dthresh = 25 # use sfq for download if pktdelay set to this or lower
259
260 limit = int("$maxrate")
261 m2 = int("$m2")
262 dmax = int("$dmax")
263 umax = int("$umax")
264
265 if (!(m2 > 0)) {
266 dmax = 500
267 umax = 1500
268 m2 = 10
269 }
270
271 cqlen = ${dl_mode:+2 * }qlen(limit, m2, umax, dmax)
272
273 printf "tc qdisc add dev $device parent 1:${classnr}0 handle ${classnr}00: "
274 if (("$dir" != "down") || ((dmax > 0) && (dmax <= sfq_dthresh))) {
275 print "sfq perturb 10 limit " cqlen
276 } else {
277 avpkt = 1200
278 if (min < avpkt) min = avpkt
279 min = int(limit * 1024 / 8 * 0.1)
280 dqb = cqlen * 1500
281 max = int(min + (dqb - min) * 0.25)
282 burst = int((2 * min + max) / (3 * avpkt))
283
284 print "red min " min " max " max " burst " burst " avpkt " avpkt " limit " dqb " probability 0.04 ecn"
285 }
286 }
287 EOF
288 }
289
290 enum_classes() {
291 local c="0"
292 config_get classes "$1" classes
293 config_get default "$1" default
294 for class in $classes; do
295 c="$(($c + 1))"
296 config_set "${class}" classnr $c
297 case "$class" in
298 $default) class_default=$c;;
299 esac
300 done
301 class_default="${class_default:-$c}"
302 }
303
304 cls_var() {
305 local varname="$1"
306 local class="$2"
307 local name="$3"
308 local type="$4"
309 local default="$5"
310 local tmp tmp1 tmp2
311 config_get tmp1 "$class" "$name"
312 config_get tmp2 "${class}_${type}" "$name"
313 tmp="${tmp2:-$tmp1}"
314 tmp="${tmp:-$tmp2}"
315 export ${varname}="${tmp:-$default}"
316 }
317
318 start_interface() {
319 local iface="$1"
320 local num_imq="$2"
321 config_get device "$iface" device
322 config_get enabled "$iface" enabled
323 [ -z "$device" -o -z "$enabled" ] && exit
324 config_get upload "$iface" upload
325 config_get halfduplex "$iface" halfduplex
326 config_get download "$iface" download
327 config_get classgroup "$iface" classgroup
328
329 download="${download:-${halfduplex:+$upload}}"
330 enum_classes "$classgroup"
331 for dir in up${halfduplex} ${download:+down}; do
332 case "$dir" in
333 up)
334 upload=$(($upload * 98 / 100 - 10))
335 dev="$device"
336 rate="$upload"
337 dl_mode=""
338 prefix="cls"
339 ;;
340 down)
341 add_insmod imq numdevs="$num_imq"
342 config_get imqdev "$iface" imqdev
343 download=$(($download * 96 / 100 - 64))
344 dev="imq$imqdev"
345 rate="$download"
346 dl_mode=1
347 prefix="d_cls"
348 ;;
349 *) continue;;
350 esac
351 for class in $classes; do
352 cls_var umax "$class" packetsize $dir 1500
353 cls_var dmax "$class" packetdelay $dir 500
354
355 cls_var maxrate "$class" limitrate $dir 100
356 cls_var share "$class" linksharing $dir 0
357 cls_var m2 "$class" avgrate $dir 0
358 maxrate=$(($maxrate * $rate / 100))
359 share=$(($share * $rate / 100))
360 m2=$(($m2 * $rate / 100))
361
362 config_get classnr "$class" classnr
363 append ${prefix}q "$(class_main_qdisc "$dev" "$iface")" "$N"
364 append ${prefix}l "$(class_leaf_qdisc "$dev" "$iface")" "$N"
365 append ${prefix}f "tc filter add dev $dev parent 1: prio $classnr protocol ip handle $classnr fw flowid 1:${classnr}0" "$N"
366 done
367 export dev_${dir}="ifconfig $dev up txqueuelen 5 >&- 2>&-
368 tc qdisc del dev $dev root >&- 2>&-
369 tc qdisc add dev $dev root handle 1: hfsc default ${class_default}0
370 tc class add dev $dev parent 1: classid 1:1 hfsc sc rate ${rate}kbit ul rate ${rate}kbit"
371 done
372 add_insmod cls_fw
373 add_insmod sch_hfsc
374 add_insmod sch_sfq
375 add_insmod sch_red
376 cat <<EOF
377 ${INSMOD:+$INSMOD$N}${dev_up:+$dev_up
378 $clsq
379 $clsl
380 $clsf
381 }${imqdev:+$dev_down
382 $d_clsq
383 $d_clsl
384 $d_clsf
385 }
386 EOF
387 unset INSMOD clsq clsf clsl d_clsq d_clsl d_clsf dev_up dev_down
388 }
389
390 start_interfaces() {
391 local C="$1"
392 for iface in $INTERFACES; do
393 start_interface "$iface" "$C"
394 done
395 }
396
397 add_rules() {
398 local var="$1"
399 local rules="$2"
400 local prefix="$3"
401
402 for rule in $rules; do
403 unset iptrule
404 config_get target "$rule" target
405 config_get target "$target" classnr
406 config_get options "$rule" options
407 parse_matching_rule iptrule "$rule" "$options" "$prefix" "-j MARK --set-mark $target"
408 append "$var" "$iptrule" "$N"
409 done
410 }
411
412 start_cg() {
413 local cg="$1"
414 local iptrules
415 local pktrules
416 local sizerules
417 local download
418 enum_classes "$cg"
419 add_rules iptrules "$ctrules" "iptables -t mangle -A ${cg}_ct"
420 config_get classes "$cg" classes
421 for class in $classes; do
422 config_get mark "$class" classnr
423 config_get maxsize "$class" maxsize
424 [ -z "$maxsize" -o -z "$mark" ] || {
425 add_insmod ipt_length
426 append pktrules "iptables -t mangle -A ${cg} -m mark --mark $mark -m length --length $maxsize: -j MARK --set-mark 0" "$N"
427 }
428 done
429 add_rules pktrules "$rules" "iptables -t mangle -A ${cg}"
430 for iface in $INTERFACES; do
431 config_get classgroup "$iface" classgroup
432 config_get device "$iface" device
433 config_get imqdev "$iface" imqdev
434 config_get dl "$iface" download
435 config_get halfduplex "$iface" halfduplex
436 add_insmod ipt_IMQ
437 append up "iptables -t mangle -A OUTPUT -o $device -j ${cg}" "$N"
438 append up "iptables -t mangle -A FORWARD -o $device -j ${cg}" "$N"
439 [ -z "$dl" ] || {
440 [ -z "$halfduplex" ] || {
441 append down "iptables -t mangle -A POSTROUTING -o $device -j IMQ --todev $imqdev" "$N"
442 }
443 append down "iptables -t mangle -A PREROUTING -i $device -j ${cg}" "$N"
444 append down "iptables -t mangle -A PREROUTING -i $device -j IMQ --todev $imqdev" "$N"
445 }
446 done
447 cat <<EOF
448 $INSMOD
449 iptables -t mangle -N ${cg} >&- 2>&-
450 iptables -t mangle -N ${cg}_ct >&- 2>&-
451 ${iptrules:+${iptrules}${N}iptables -t mangle -A ${cg}_ct -j CONNMARK --save-mark}
452 iptables -t mangle -A ${cg} -j CONNMARK --restore-mark
453 iptables -t mangle -A ${cg} -m mark --mark 0 -j ${cg}_ct
454 $pktrules
455 $up$N${down:+${down}$N}
456 EOF
457 unset INSMOD
458 }
459
460 start_firewall() {
461 add_insmod ipt_multiport
462 add_insmod ipt_CONNMARK
463 cat <<EOF
464 iptables -t mangle -F
465 iptables -t mangle -X
466 EOF
467 for group in $CG; do
468 start_cg $group
469 done
470 }
471
472 C="0"
473 INTERFACES=""
474 config_load qos
475
476 C="0"
477 for iface in $INTERFACES; do
478 export C="$(($C + 1))"
479 done
480
481 case "$1" in
482 all)
483 start_interfaces "$C"
484 start_firewall
485 ;;
486 interface)
487 start_interface "$2" "$C"
488 ;;
489 interfaces)
490 start_interfaces
491 ;;
492 firewall)
493 start_firewall
494 ;;
495 esac
This page took 0.072737 seconds and 5 git commands to generate.