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 min = int(limit * 1024 / 8 * 0.1)
280 max = int(min + (dqb - min) * 0.25)
281 burst = int((2 * min + max) / (3 * avpkt))
283 print "red min " min " max " max " burst " burst " avpkt " avpkt " limit " dqb " probability 0.04 ecn"
291 config_get classes
"$1" classes
292 config_get default
"$1" default
293 for class
in $classes; do
295 config_set
"${class}" classnr
$c
297 $default) class_default
=$c;;
300 class_default
="${class_default:-$c}"
310 config_get tmp1
"$class" "$name"
311 config_get tmp2
"${class}_${type}" "$name"
314 export ${varname}="${tmp:-$default}"
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
328 download
="${download:-${halfduplex:+$upload}}"
329 enum_classes
"$classgroup"
330 for dir
in up
${halfduplex} ${download:+down}; do
333 upload
=$
(($upload * 98 / 100 - 10))
340 add_insmod imq numdevs
="$num_imq"
341 config_get imqdev
"$iface" imqdev
342 download
=$
(($download * 96 / 100 - 64))
350 for class
in $classes; do
351 cls_var umax
"$class" packetsize
$dir 1500
352 cls_var dmax
"$class" packetdelay
$dir 500
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))
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"
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"
376 ${INSMOD:+$INSMOD$N}${dev_up:+$dev_up
386 unset INSMOD clsq clsf clsl d_clsq d_clsl d_clsf dev_up dev_down
391 for iface
in $INTERFACES; do
392 start_interface
"$iface" "$C"
401 for rule
in $rules; do
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"
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"
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
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"
439 [ -z "$halfduplex" ] ||
{
440 append down
"iptables -t mangle -A POSTROUTING -o $device -j IMQ --todev $imqdev" "$N"
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"
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
454 $up$N${down:+${down}$N}
460 add_insmod ipt_multiport
461 add_insmod ipt_CONNMARK
463 iptables -t mangle -F
464 iptables -t mangle -X
476 for iface
in $INTERFACES; do
477 export C
="$(($C + 1))"
482 start_interfaces
"$C"
486 start_interface
"$2" "$C"