5 [ -f /sbin
/modprobe
] && insmod
="modprobe"
8 eval "export isset=\${insmod_$1}"
11 *) append INSMOD
"$insmod $* >&- 2>&-" "$N"; export insmod_
$1=1;;
18 config_get
"$1" ifname
21 parse_matching_rule
() {
31 append
"$var" "$prefix" "$N"
32 for option
in $options; do
34 proto
) config_get value
"$section" proto
; proto
="${proto:-$value}";;
37 config_get
type "$section" TYPE
39 classify
) unset pkt
; append
"$var" "-m mark --mark 0";;
40 default
) pkt
=1; append
"$var" "-m mark --mark 0";;
43 append
"$var" "${proto:+-p $proto}"
44 for option
in $options; do
45 config_get value
"$section" "$option"
47 case "$pkt:$option" in
49 append
"$var" "-s $value"
52 append
"$var" "-d $value"
56 append
"$var" "-m ipp2p"
58 all
) append
"$var" "--edk --dc --kazaa --gnu --bit";;
59 *) append
"$var" "--$value";;
64 append
"$var" "-m layer7 --l7proto $value${pkt:+ --l7pkt}"
66 *:ports|
*:srcports|
*:dstports
)
67 value
="$(echo "$value" | sed -e 's,-,:,g')"
70 ""|tcp|udp
) append
"$var" "-m ${proto:-tcp -p tcp} -m multiport";;
71 *) unset "$var"; return 0;;
75 config_set
"$section" srcports
""
76 config_set
"$section" dstports
""
77 config_set
"$section" portrange
""
78 append
"$var" "--ports $value"
81 config_set
"$section" ports
""
82 config_set
"$section" dstports
""
83 config_set
"$section" portrange
""
84 append
"$var" "--sports $value"
87 config_set
"$section" ports
""
88 config_set
"$section" srcports
""
89 config_set
"$section" portrange
""
90 append
"$var" "--dports $value"
96 config_set
"$section" ports
""
97 config_set
"$section" srcports
""
98 config_set
"$section" dstports
""
99 value
="$(echo "$value" | sed -e 's,-,:,g')"
101 ""|tcp|udp
) append
"$var" "-m ${proto:-tcp -p tcp} --sport $value --dport $value";;
102 *) unset "$var"; return 0;;
107 value
="$(echo "$value" | sed -e 's,-,:,g')"
108 add_insmod ipt_length
109 append
"$var" "-m length --length $value"
113 append
"$var" "-m limit --limit $value"
117 tcp
) append
"$var" "-m tcp --tcp-flags ALL $value";;
118 *) unset $var; return 0;;
122 config_get class
"${value##!}" classnr
123 [ -z "$class" ] && continue;
125 !*) append
"$var" "-m mark ! --mark $class";;
126 *) append
"$var" "-m mark --mark $class";;
130 append
"$var" "$suffix"
131 case "$ports:$proto" in
132 1:) parse_matching_rule
"$var" "$section" "$options" "$prefix" "$suffix" "udp";;
144 config_set
"$1" "classgroup" "Default"
145 config_set
"$1" "upload" "128"
147 classify|default|reclassify
)
155 config_get TYPE
"$CONFIG_SECTION" TYPE
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 ] ||
{
164 INTERFACES
="$INTERFACES $CONFIG_SECTION"
165 config_set
"$classgroup" enabled
1
167 config_get device
"$CONFIG_SECTION" device
168 [ -z "$device" ] && device
="$(find_ifname ${CONFIG_SECTION})"
169 config_set
"$CONFIG_SECTION" device
"${device:-eth0}"
171 classgroup
) append CG
"$CONFIG_SECTION";;
172 classify|default|reclassify
)
174 classify
) var
="ctrules";;
177 config_get target
"$CONFIG_SECTION" target
178 config_set
"$CONFIG_SECTION" options
"$options"
179 append
"$var" "$CONFIG_SECTION"
189 limit = int("$maxrate")
193 share = int("$share")
205 pdmax = int (dmax + (umax * 8 / limit))
207 if (share > 0) cdata = " rt"
209 if ((umax > 0) && (dmax > 0)) {
210 cdata = cdata " umax " umax "b dmax " pdmax "ms"
212 cdata = cdata " rate " m2 "kbit"
215 if ((m2 > 0) && (umax > 0) && (dmax > 0)) {
216 cdata = cdata " ls umax " umax "b dmax " pdmax "ms rate " share "kbit"
218 cdata = cdata " ls m1 " share "kbit d 500ms m2 " share "kbit"
222 print "tc class add dev $device parent 1:1 classid 1:${classnr}0 hfsc" cdata " ul rate " limit "kbit"
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
239 if ((m2 > 0) && (umax > 0)) qb -= int((1500 - umax) * qlen_pkt)
244 qr -= int((rate - m2) * qlen_avgr)
248 qt = qlen_base + qlen_dmax * (dmax / 1000)
251 ql = int(qr * qt / qb)
252 if (ql < qlen_min) ql = qlen_min
258 sfq_dthresh = 25 # use sfq for download if pktdelay set to this or lower
260 limit = int("$maxrate")
271 cqlen = ${dl_mode:+2 * }qlen(limit, m2, umax, dmax)
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
278 if (min < avpkt) min = avpkt
279 min = int(limit * 1024 / 8 * 0.1)
281 max = int(min + (dqb - min) * 0.25)
282 burst = int((2 * min + max) / (3 * avpkt))
284 print "red min " min " max " max " burst " burst " avpkt " avpkt " limit " dqb " probability 0.04 ecn"
292 config_get classes
"$1" classes
293 config_get default
"$1" default
294 for class
in $classes; do
296 config_set
"${class}" classnr
$c
298 $default) class_default
=$c;;
301 class_default
="${class_default:-$c}"
311 config_get tmp1
"$class" "$name"
312 config_get tmp2
"${class}_${type}" "$name"
315 export ${varname}="${tmp:-$default}"
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
329 download
="${download:-${halfduplex:+$upload}}"
330 enum_classes
"$classgroup"
331 for dir
in up
${halfduplex} ${download:+down}; do
334 upload
=$
(($upload * 98 / 100 - 10))
341 add_insmod imq numdevs
="$num_imq"
342 config_get imqdev
"$iface" imqdev
343 download
=$
(($download * 96 / 100 - 64))
351 for class
in $classes; do
352 cls_var umax
"$class" packetsize
$dir 1500
353 cls_var dmax
"$class" packetdelay
$dir 500
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))
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"
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"
377 ${INSMOD:+$INSMOD$N}${dev_up:+$dev_up
387 unset INSMOD clsq clsf clsl d_clsq d_clsl d_clsf dev_up dev_down
392 for iface
in $INTERFACES; do
393 start_interface
"$iface" "$C"
402 for rule
in $rules; do
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"
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"
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
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"
440 [ -z "$halfduplex" ] ||
{
441 append down
"iptables -t mangle -A POSTROUTING -o $device -j IMQ --todev $imqdev" "$N"
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"
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
455 $up$N${down:+${down}$N}
461 add_insmod ipt_multiport
462 add_insmod ipt_CONNMARK
464 iptables -t mangle -F
465 iptables -t mangle -X
477 for iface
in $INTERFACES; do
478 export C
="$(($C + 1))"
483 start_interfaces
"$C"
487 start_interface
"$2" "$C"