2 [ -e /etc
/functions.sh
] && .
/etc
/functions.sh || . .
/functions.sh
3 [ -x /sbin
/modprobe
] && insmod
="modprobe" || insmod
="insmod"
6 eval "export isset=\${insmod_$1}"
9 *) append INSMOD
"$insmod $* >&- 2>&-" "$N"; export insmod_
$1=1;;
13 [ -e /etc
/config
/network
] && {
14 # only try to parse network config on openwrt
20 config_get
"$1" ifname
24 echo "Interface not found."
29 parse_matching_rule
() {
39 append
"$var" "$prefix" "$N"
40 for option
in $options; do
42 proto
) config_get value
"$section" proto
; proto
="${proto:-$value}";;
45 config_get
type "$section" TYPE
47 classify
) unset pkt
; append
"$var" "-m mark --mark 0";;
48 default
) pkt
=1; append
"$var" "-m mark --mark 0";;
51 append
"$var" "${proto:+-p $proto}"
52 for option
in $options; do
53 config_get value
"$section" "$option"
55 case "$pkt:$option" in
57 append
"$var" "-s $value"
60 append
"$var" "-d $value"
65 append
"$var" "-m layer7 --l7proto $value${pkt:+ --l7pkt}"
67 *:ports|
*:srcports|
*:dstports
)
68 value
="$(echo "$value" | sed -e 's,-,:,g')"
71 ""|tcp|udp
) append
"$var" "-m ${proto:-tcp -p tcp} -m multiport";;
72 *) unset "$var"; return 0;;
76 config_set
"$section" srcports
""
77 config_set
"$section" dstports
""
78 config_set
"$section" portrange
""
79 append
"$var" "--ports $value"
82 config_set
"$section" ports
""
83 config_set
"$section" dstports
""
84 config_set
"$section" portrange
""
85 append
"$var" "--sports $value"
88 config_set
"$section" ports
""
89 config_set
"$section" srcports
""
90 config_set
"$section" portrange
""
91 append
"$var" "--dports $value"
97 config_set
"$section" ports
""
98 config_set
"$section" srcports
""
99 config_set
"$section" dstports
""
100 value
="$(echo "$value" | sed -e 's,-,:,g')"
102 ""|tcp|udp
) append
"$var" "-m ${proto:-tcp -p tcp} --sport $value --dport $value";;
103 *) unset "$var"; return 0;;
108 value
="$(echo "$value" | sed -e 's,-,:,g')"
109 add_insmod ipt_connbytes
110 append
"$var" "-m connbytes --connbytes $value --connbytes-dir both --connbytes-mode bytes"
115 !*) append
"$var" "-m tos ! --tos $value";;
116 *) append
"$var" "-m tos --tos $value"
122 [ -z "${value%%[EBCA]*}" ] && dscp_option
="--dscp-class"
124 !*) append
"$var" "-m dscp ! $dscp_option $value";;
125 *) append
"$var" "-m dscp $dscp_option $value"
129 value
="$(echo "$value" | sed -e 's,-,:,g')"
130 if [ "$value" = "out" ]; then
131 append
"$var" "-o $device"
132 elif [ "$value" = "in" ]; then
133 append
"$var" "-i $device"
137 value
="$(echo "$value" | sed -e 's,-,:,g')"
138 add_insmod ipt_length
139 append
"$var" "-m length --length $value"
143 append
"$var" "-m limit --limit $value"
147 tcp
) append
"$var" "-m tcp --tcp-flags ALL $value";;
148 *) unset $var; return 0;;
152 config_get class
"${value##!}" classnr
153 [ -z "$class" ] && continue;
155 !*) append
"$var" "-m mark ! --mark $class";;
156 *) append
"$var" "-m mark --mark $class";;
161 config_get TOS
"$rule" 'TOS'
162 suffix
="-j TOS --set-tos "${TOS:-"Normal-Service"}
166 config_get DSCP
"$rule" 'DSCP'
167 [ -z "${DSCP%%[EBCA]*}" ] && set_value
="--set-dscp-class $DSCP" \
168 || set_value
="--set-dscp $DSCP"
169 suffix
="-j DSCP $set_value"
173 append
"$var" "$suffix"
174 case "$ports:$proto" in
175 1:) parse_matching_rule
"$var" "$section" "$options" "$prefix" "$suffix" "udp";;
187 config_set
"$2" "classgroup" "Default"
188 config_set
"$2" "upload" "128"
190 classify|default|reclassify
)
198 config_get TYPE
"$CONFIG_SECTION" TYPE
201 config_get_bool enabled
"$CONFIG_SECTION" enabled
1
202 [ 1 -eq "$enabled" ] ||
return 0
203 config_get classgroup
"$CONFIG_SECTION" classgroup
204 config_set
"$CONFIG_SECTION" imqdev
"$C"
206 append INTERFACES
"$CONFIG_SECTION"
207 config_set
"$classgroup" enabled
1
208 config_get device
"$CONFIG_SECTION" device
209 [ -z "$device" ] && {
210 device
="$(find_ifname ${CONFIG_SECTION})"
211 config_set
"$CONFIG_SECTION" device
"${device:-eth0}"
214 classgroup
) append CG
"$CONFIG_SECTION";;
215 classify|default|reclassify
)
217 classify
) var
="ctrules";;
220 config_get target
"$CONFIG_SECTION" target
221 config_set
"$CONFIG_SECTION" options
"$options"
222 append
"$var" "$CONFIG_SECTION"
231 config_get classes
"$1" classes
232 config_get default
"$1" default
233 for class
in $classes; do
235 config_set
"${class}" classnr
$c
237 $default) class_default
=$c;;
240 class_default
="${class_default:-$c}"
250 config_get tmp1
"$class" "$name"
251 config_get tmp2
"${class}_${type}" "$name"
254 export ${varname}="${tmp:-$default}"
259 [ -e $dir/tcrules.
awk ] || dir
=.
262 -v linespeed
="$rate" \
269 config_get device
"$iface" device
270 config_get_bool enabled
"$iface" enabled
1
271 [ -z "$device" -o 1 -ne "$enabled" ] && {
274 config_get upload
"$iface" upload
275 config_get halfduplex
"$iface" halfduplex
276 config_get download
"$iface" download
277 config_get classgroup
"$iface" classgroup
278 config_get_bool overhead
"$iface" overhead
0
280 download
="${download:-${halfduplex:+$upload}}"
281 enum_classes
"$classgroup"
282 for dir
in up
${halfduplex} ${download:+down}; do
285 [ "$overhead" = 1 ] && upload
=$
(($upload * 98 / 100 - (15 * 128 / $upload)))
292 add_insmod imq numdevs
="$num_imq"
293 config_get imqdev
"$iface" imqdev
294 [ "$overhead" = 1 ] && download
=$
(($download * 98 / 100 - (80 * 1024 / $download)))
303 for class
in $classes; do
304 cls_var pktsize
"$class" packetsize
$dir 1500
305 cls_var pktdelay
"$class" packetdelay
$dir 0
306 cls_var maxrate
"$class" limitrate
$dir 100
307 cls_var prio
"$class" priority
$dir 1
308 cls_var avgrate
"$class" avgrate
$dir 0
309 config_get classnr
"$class" classnr
310 append cstr
"$classnr:$prio:$avgrate:$pktsize:$pktdelay:$maxrate" "$N"
312 append
${prefix}q
"$(tcrules)" "$N"
313 export dev_
${dir}="ifconfig $dev up txqueuelen 5 >&- 2>&-
314 tc qdisc del dev $dev root >&- 2>&-
315 tc qdisc add dev $dev root handle 1: hfsc default ${class_default}0
316 tc class add dev $dev parent 1: classid 1:1 hfsc sc rate ${rate}kbit ul rate ${rate}kbit"
324 ${INSMOD:+$INSMOD$N}${dev_up:+$dev_up
332 unset INSMOD clsq clsf clsl d_clsq d_clsl d_clsf dev_up dev_down
337 for iface
in $INTERFACES; do
338 start_interface
"$iface" "$C"
347 for rule
in $rules; do
349 config_get target
"$rule" target
350 config_get target
"$target" classnr
351 config_get options
"$rule" options
353 ## If we want to override the TOS field, let's clear the DSCP field first.
354 [ ! -z "$(echo $options | grep 'TOS')" ] && {
355 s_options
=${options%%TOS}
357 parse_matching_rule iptrule
"$rule" "$s_options" "$prefix" "-j DSCP --set-dscp 0"
358 append
"$var" "$iptrule" "$N"
362 parse_matching_rule iptrule
"$rule" "$options" "$prefix" "-j MARK --set-mark $target"
363 append
"$var" "$iptrule" "$N"
374 add_rules iptrules
"$ctrules" "iptables -t mangle -A ${cg}_ct"
375 config_get classes
"$cg" classes
376 for class
in $classes; do
377 config_get mark
"$class" classnr
378 config_get maxsize
"$class" maxsize
379 [ -z "$maxsize" -o -z "$mark" ] ||
{
380 add_insmod ipt_length
381 append pktrules
"iptables -t mangle -A ${cg} -m mark --mark $mark -m length --length $maxsize: -j MARK --set-mark 0" "$N"
384 add_rules pktrules
"$rules" "iptables -t mangle -A ${cg}"
385 for iface
in $INTERFACES; do
386 config_get classgroup
"$iface" classgroup
387 config_get device
"$iface" device
388 config_get imqdev
"$iface" imqdev
389 config_get dl
"$iface" download
390 config_get halfduplex
"$iface" halfduplex
392 append up
"iptables -t mangle -A OUTPUT -o $device -j ${cg}" "$N"
393 append up
"iptables -t mangle -A FORWARD -o $device -j ${cg}" "$N"
395 [ -z "$halfduplex" ] ||
{
396 append down
"iptables -t mangle -A POSTROUTING -o $device -j IMQ --todev $imqdev" "$N"
398 append down
"iptables -t mangle -A PREROUTING -i $device -j ${cg}" "$N"
399 append down
"iptables -t mangle -A POSTROUTING -o $device -j ${cg}" "$N"
400 append down
"iptables -t mangle -A PREROUTING -i $device -j IMQ --todev $imqdev" "$N"
405 iptables -t mangle -N ${cg} >&- 2>&-
406 iptables -t mangle -N ${cg}_ct >&- 2>&-
407 ${iptrules:+${iptrules}${N}iptables -t mangle -A ${cg}_ct -j CONNMARK --save-mark}
408 iptables -t mangle -A ${cg} -j CONNMARK --restore-mark
409 iptables -t mangle -A ${cg} -m mark --mark 0 -j ${cg}_ct
411 $up$N${down:+${down}$N}
417 add_insmod ipt_multiport
418 add_insmod ipt_CONNMARK
420 iptables -t mangle -F
421 iptables -t mangle -X
430 [ -e .
/qos.conf
] && {
436 for iface
in $INTERFACES; do
437 export C
="$(($C + 1))"
442 start_interfaces
"$C"
446 start_interface
"$2" "$C"