allow the user to disable downlink shaping by removing the option download ... line
[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 min = int(limit * 1024 / 8 * 0.1)
279 dqb = cqlen * 1500
280 max = int(min + (dqb - min) * 0.25)
281 burst = int((2 * min + max) / (3 * avpkt))
282
283 print "red min " min " max " max " burst " burst " avpkt " avpkt " limit " dqb " probability 0.04 ecn"
284 }
285 }
286 EOF
287 }
288
289 enum_classes() {
290 local c="0"
291 config_get classes "$1" classes
292 config_get default "$1" default
293 for class in $classes; do
294 c="$(($c + 1))"
295 config_set "${class}" classnr $c
296 case "$class" in
297 $default) class_default=$c;;
298 esac
299 done
300 class_default="${class_default:-$c}"
301 }
302
303 cls_var() {
304 local varname="$1"
305 local class="$2"
306 local name="$3"
307 local type="$4"
308 local default="$5"
309 local tmp tmp1 tmp2
310 config_get tmp1 "$class" "$name"
311 config_get tmp2 "${class}_${type}" "$name"
312 tmp="${tmp2:-$tmp1}"
313 tmp="${tmp:-$tmp2}"
314 export ${varname}="${tmp:-$default}"
315 }
316
317 start_interface() {
318 local iface="$1"
319 local num_imq="$2"
320 config_get device "$iface" device
321 config_get enabled "$iface" enabled
322 [ -z "$device" -o -z "$enabled" ] && exit
323 config_get upload "$iface" upload
324 config_get halfduplex "$iface" halfduplex
325 config_get download "$iface" download
326 config_get classgroup "$iface" classgroup
327
328 download="${download:-${halfduplex:+$upload}}"
329 enum_classes "$classgroup"
330 for dir in up${halfduplex} ${download:+down}; do
331 case "$dir" in
332 up)
333 upload=$(($upload * 98 / 100 - 10))
334 dev="$device"
335 rate="$upload"
336 dl_mode=""
337 prefix="cls"
338 ;;
339 down)
340 add_insmod imq numdevs="$num_imq"
341 config_get imqdev "$iface" imqdev
342 download=$(($download * 96 / 100 - 64))
343 dev="imq$imqdev"
344 rate="$download"
345 dl_mode=1
346 prefix="d_cls"
347 ;;
348 *) continue;;
349 esac
350 for class in $classes; do
351 cls_var umax "$class" packetsize $dir 1500
352 cls_var dmax "$class" packetdelay $dir 500
353
354 cls_var maxrate "$class" limitrate $dir 100
355 cls_var share "$class" linksharing $dir 0
356 cls_var m2 "$class" avgrate $dir 0
357 maxrate=$(($maxrate * $rate / 100))
358 share=$(($share * $rate / 100))
359 m2=$(($m2 * $rate / 100))
360
361 config_get classnr "$class" classnr
362 append ${prefix}q "$(class_main_qdisc "$dev" "$iface")" "$N"
363 append ${prefix}l "$(class_leaf_qdisc "$dev" "$iface")" "$N"
364 append ${prefix}f "tc filter add dev $dev parent 1: prio $classnr protocol ip handle $classnr fw flowid 1:${classnr}0" "$N"
365 done
366 export dev_${dir}="ifconfig $dev up txqueuelen 5 >&- 2>&-
367 tc qdisc del dev $dev root >&- 2>&-
368 tc qdisc add dev $dev root handle 1: hfsc default ${class_default}0
369 tc class add dev $dev parent 1: classid 1:1 hfsc sc rate ${rate}kbit ul rate ${rate}kbit"
370 done
371 add_insmod cls_fw
372 add_insmod sch_hfsc
373 add_insmod sch_sfq
374 add_insmod sch_red
375 cat <<EOF
376 ${INSMOD:+$INSMOD$N}${dev_up:+$dev_up
377 $clsq
378 $clsl
379 $clsf
380 }${imqdev:+$dev_down
381 $d_clsq
382 $d_clsl
383 $d_clsf
384 }
385 EOF
386 unset INSMOD clsq clsf clsl d_clsq d_clsl d_clsf dev_up dev_down
387 }
388
389 start_interfaces() {
390 local C="$1"
391 for iface in $INTERFACES; do
392 start_interface "$iface" "$C"
393 done
394 }
395
396 add_rules() {
397 local var="$1"
398 local rules="$2"
399 local prefix="$3"
400
401 for rule in $rules; do
402 unset iptrule
403 config_get target "$rule" target
404 config_get target "$target" classnr
405 config_get options "$rule" options
406 parse_matching_rule iptrule "$rule" "$options" "$prefix" "-j MARK --set-mark $target"
407 append "$var" "$iptrule" "$N"
408 done
409 }
410
411 start_cg() {
412 local cg="$1"
413 local iptrules
414 local pktrules
415 local sizerules
416 local download
417 enum_classes "$cg"
418 add_rules iptrules "$ctrules" "iptables -t mangle -A ${cg}_ct"
419 config_get classes "$cg" classes
420 for class in $classes; do
421 config_get mark "$class" classnr
422 config_get maxsize "$class" maxsize
423 [ -z "$maxsize" -o -z "$mark" ] || {
424 add_insmod ipt_length
425 append pktrules "iptables -t mangle -A ${cg} -m mark --mark $mark -m length --length $maxsize: -j MARK --set-mark 0" "$N"
426 }
427 done
428 add_rules pktrules "$rules" "iptables -t mangle -A ${cg}"
429 for iface in $INTERFACES; do
430 config_get classgroup "$iface" classgroup
431 config_get device "$iface" device
432 config_get imqdev "$iface" imqdev
433 config_get dl "$iface" download
434 config_get halfduplex "$iface" halfduplex
435 add_insmod ipt_IMQ
436 append up "iptables -t mangle -A OUTPUT -o $device -j ${cg}" "$N"
437 append up "iptables -t mangle -A FORWARD -o $device -j ${cg}" "$N"
438 [ -z "$dl" ] || {
439 [ -z "$halfduplex" ] || {
440 append down "iptables -t mangle -A POSTROUTING -o $device -j IMQ --todev $imqdev" "$N"
441 }
442 append down "iptables -t mangle -A PREROUTING -i $device -j ${cg}" "$N"
443 append down "iptables -t mangle -A PREROUTING -i $device -j IMQ --todev $imqdev" "$N"
444 }
445 done
446 cat <<EOF
447 $INSMOD
448 iptables -t mangle -N ${cg} >&- 2>&-
449 iptables -t mangle -N ${cg}_ct >&- 2>&-
450 ${iptrules:+${iptrules}${N}iptables -t mangle -A ${cg}_ct -j CONNMARK --save-mark}
451 iptables -t mangle -A ${cg} -j CONNMARK --restore-mark
452 iptables -t mangle -A ${cg} -m mark --mark 0 -j ${cg}_ct
453 $pktrules
454 $up$N${down:+${down}$N}
455 EOF
456 unset INSMOD
457 }
458
459 start_firewall() {
460 add_insmod ipt_multiport
461 add_insmod ipt_CONNMARK
462 cat <<EOF
463 iptables -t mangle -F
464 iptables -t mangle -X
465 EOF
466 for group in $CG; do
467 start_cg $group
468 done
469 }
470
471 C="0"
472 INTERFACES=""
473 config_load qos
474
475 C="0"
476 for iface in $INTERFACES; do
477 export C="$(($C + 1))"
478 done
479
480 case "$1" in
481 all)
482 start_interfaces "$C"
483 start_firewall
484 ;;
485 interface)
486 start_interface "$2" "$C"
487 ;;
488 interfaces)
489 start_interfaces
490 ;;
491 firewall)
492 start_firewall
493 ;;
494 esac
This page took 0.113944 seconds and 5 git commands to generate.