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"
64 append
"$var" "-m ipp2p"
66 all
) append
"$var" "--edk --dc --kazaa --gnu --bit";;
67 *) append
"$var" "--$value";;
72 append
"$var" "-m layer7 --l7proto $value${pkt:+ --l7pkt}"
74 *:ports|
*:srcports|
*:dstports
)
75 value
="$(echo "$value" | sed -e 's,-,:,g')"
78 ""|tcp|udp
) append
"$var" "-m ${proto:-tcp -p tcp} -m multiport";;
79 *) unset "$var"; return 0;;
83 config_set
"$section" srcports
""
84 config_set
"$section" dstports
""
85 config_set
"$section" portrange
""
86 append
"$var" "--ports $value"
89 config_set
"$section" ports
""
90 config_set
"$section" dstports
""
91 config_set
"$section" portrange
""
92 append
"$var" "--sports $value"
95 config_set
"$section" ports
""
96 config_set
"$section" srcports
""
97 config_set
"$section" portrange
""
98 append
"$var" "--dports $value"
104 config_set
"$section" ports
""
105 config_set
"$section" srcports
""
106 config_set
"$section" dstports
""
107 value
="$(echo "$value" | sed -e 's,-,:,g')"
109 ""|tcp|udp
) append
"$var" "-m ${proto:-tcp -p tcp} --sport $value --dport $value";;
110 *) unset "$var"; return 0;;
115 value
="$(echo "$value" | sed -e 's,-,:,g')"
116 add_insmod ipt_length
117 append
"$var" "-m length --length $value"
121 append
"$var" "-m limit --limit $value"
125 tcp
) append
"$var" "-m tcp --tcp-flags ALL $value";;
126 *) unset $var; return 0;;
130 config_get class
"${value##!}" classnr
131 [ -z "$class" ] && continue;
133 !*) append
"$var" "-m mark ! --mark $class";;
134 *) append
"$var" "-m mark --mark $class";;
138 append
"$var" "$suffix"
139 case "$ports:$proto" in
140 1:) parse_matching_rule
"$var" "$section" "$options" "$prefix" "$suffix" "udp";;
152 config_set
"$2" "classgroup" "Default"
153 config_set
"$2" "upload" "128"
155 classify|default|reclassify
)
163 config_get TYPE
"$CONFIG_SECTION" TYPE
166 config_get_bool enabled
"$CONFIG_SECTION" enabled
1
167 [ 1 -eq "$enabled" ] ||
return 0
168 config_get classgroup
"$CONFIG_SECTION" classgroup
169 config_set
"$CONFIG_SECTION" imqdev
"$C"
171 append INTERFACES
"$CONFIG_SECTION"
172 config_set
"$classgroup" enabled
1
173 config_get device
"$CONFIG_SECTION" device
174 [ -z "$device" ] && {
175 device
="$(find_ifname ${CONFIG_SECTION})"
176 config_set
"$CONFIG_SECTION" device
"${device:-eth0}"
179 classgroup
) append CG
"$CONFIG_SECTION";;
180 classify|default|reclassify
)
182 classify
) var
="ctrules";;
185 config_get target
"$CONFIG_SECTION" target
186 config_set
"$CONFIG_SECTION" options
"$options"
187 append
"$var" "$CONFIG_SECTION"
196 config_get classes
"$1" classes
197 config_get default
"$1" default
198 for class
in $classes; do
200 config_set
"${class}" classnr
$c
202 $default) class_default
=$c;;
205 class_default
="${class_default:-$c}"
215 config_get tmp1
"$class" "$name"
216 config_get tmp2
"${class}_${type}" "$name"
219 export ${varname}="${tmp:-$default}"
224 [ -e $dir/tcrules.
awk ] || dir
=.
227 -v linespeed
="$rate" \
234 config_get device
"$iface" device
235 config_get_bool enabled
"$iface" enabled
1
236 [ -z "$device" -o 1 -ne "$enabled" ] && {
237 echo "Interface '$iface' not found or disabled."
240 config_get upload
"$iface" upload
241 config_get halfduplex
"$iface" halfduplex
242 config_get download
"$iface" download
243 config_get classgroup
"$iface" classgroup
244 config_get_bool overhead
"$iface" overhead
0
246 download
="${download:-${halfduplex:+$upload}}"
247 enum_classes
"$classgroup"
248 for dir
in up
${halfduplex} ${download:+down}; do
251 [ "$overhead" = 1 ] && upload
=$
(($upload * 98 / 100 - (32 * 128 / $upload)))
258 add_insmod imq numdevs
="$num_imq"
259 config_get imqdev
"$iface" imqdev
260 [ "$overhead" = 1 ] && download
=$
(($download * 98 / 100 - (80 * 1024 / $download)))
269 for class
in $classes; do
270 cls_var pktsize
"$class" packetsize
$dir 1500
271 cls_var pktdelay
"$class" packetdelay
$dir 0
272 cls_var maxrate
"$class" limitrate
$dir 100
273 cls_var prio
"$class" priority
$dir 1
274 cls_var avgrate
"$class" avgrate
$dir 0
275 config_get classnr
"$class" classnr
276 append cstr
"$classnr:$prio:$avgrate:$pktsize:$pktdelay:$maxrate" "$N"
278 append
${prefix}q
"$(tcrules)" "$N"
279 export dev_
${dir}="ifconfig $dev up txqueuelen 5 >&- 2>&-
280 tc qdisc del dev $dev root >&- 2>&-
281 tc qdisc add dev $dev root handle 1: hfsc default ${class_default}0
282 tc class add dev $dev parent 1: classid 1:1 hfsc sc rate ${rate}kbit ul rate ${rate}kbit"
290 ${INSMOD:+$INSMOD$N}${dev_up:+$dev_up
298 unset INSMOD clsq clsf clsl d_clsq d_clsl d_clsf dev_up dev_down
303 for iface
in $INTERFACES; do
304 start_interface
"$iface" "$C"
313 for rule
in $rules; do
315 config_get target
"$rule" target
316 config_get target
"$target" classnr
317 config_get options
"$rule" options
318 parse_matching_rule iptrule
"$rule" "$options" "$prefix" "-j MARK --set-mark $target"
319 append
"$var" "$iptrule" "$N"
330 add_rules iptrules
"$ctrules" "iptables -t mangle -A ${cg}_ct"
331 config_get classes
"$cg" classes
332 for class
in $classes; do
333 config_get mark
"$class" classnr
334 config_get maxsize
"$class" maxsize
335 [ -z "$maxsize" -o -z "$mark" ] ||
{
336 add_insmod ipt_length
337 append pktrules
"iptables -t mangle -A ${cg} -m mark --mark $mark -m length --length $maxsize: -j MARK --set-mark 0" "$N"
340 add_rules pktrules
"$rules" "iptables -t mangle -A ${cg}"
341 for iface
in $INTERFACES; do
342 config_get classgroup
"$iface" classgroup
343 config_get device
"$iface" device
344 config_get imqdev
"$iface" imqdev
345 config_get dl
"$iface" download
346 config_get halfduplex
"$iface" halfduplex
348 append up
"iptables -t mangle -A OUTPUT -o $device -j ${cg}" "$N"
349 append up
"iptables -t mangle -A FORWARD -o $device -j ${cg}" "$N"
351 [ -z "$halfduplex" ] ||
{
352 append down
"iptables -t mangle -A POSTROUTING -o $device -j IMQ --todev $imqdev" "$N"
354 append down
"iptables -t mangle -A PREROUTING -i $device -j ${cg}" "$N"
355 append down
"iptables -t mangle -A PREROUTING -i $device -j IMQ --todev $imqdev" "$N"
360 iptables -t mangle -N ${cg} >&- 2>&-
361 iptables -t mangle -N ${cg}_ct >&- 2>&-
362 ${iptrules:+${iptrules}${N}iptables -t mangle -A ${cg}_ct -j CONNMARK --save-mark}
363 iptables -t mangle -A ${cg} -j CONNMARK --restore-mark
364 iptables -t mangle -A ${cg} -m mark --mark 0 -j ${cg}_ct
366 $up$N${down:+${down}$N}
372 add_insmod ipt_multiport
373 add_insmod ipt_CONNMARK
375 iptables -t mangle -F
376 iptables -t mangle -X
385 [ -e .
/qos.conf
] && {
391 for iface
in $INTERFACES; do
392 export C
="$(($C + 1))"
397 start_interfaces
"$C"
401 start_interface
"$2" "$C"