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";;
73 append
"$var" "-m layer7 --l7proto $value${pkt:+ --l7pkt}"
75 *:ports|
*:srcports|
*:dstports
)
76 value
="$(echo "$value" | sed -e 's,-,:,g')"
79 ""|tcp|udp
) append
"$var" "-m ${proto:-tcp -p tcp} -m multiport";;
80 *) unset "$var"; return 0;;
84 config_set
"$section" srcports
""
85 config_set
"$section" dstports
""
86 config_set
"$section" portrange
""
87 append
"$var" "--ports $value"
90 config_set
"$section" ports
""
91 config_set
"$section" dstports
""
92 config_set
"$section" portrange
""
93 append
"$var" "--sports $value"
96 config_set
"$section" ports
""
97 config_set
"$section" srcports
""
98 config_set
"$section" portrange
""
99 append
"$var" "--dports $value"
105 config_set
"$section" ports
""
106 config_set
"$section" srcports
""
107 config_set
"$section" dstports
""
108 value
="$(echo "$value" | sed -e 's,-,:,g')"
110 ""|tcp|udp
) append
"$var" "-m ${proto:-tcp -p tcp} --sport $value --dport $value";;
111 *) unset "$var"; return 0;;
116 value
="$(echo "$value" | sed -e 's,-,:,g')"
117 add_insmod ipt_connbytes
118 append
"$var" "-m connbytes --connbytes $value --connbytes-dir both --connbytes-mode bytes"
123 !*) append
"$var" "-m tos ! --tos $value";;
124 *) append
"$var" "-m tos --tos $value"
130 [ -z "${value%%[EBCA]*}" ] && dscp_option
="--dscp-class"
132 !*) append
"$var" "-m dscp ! $dscp_option $value";;
133 *) append
"$var" "-m dscp $dscp_option $value"
137 value
="$(echo "$value" | sed -e 's,-,:,g')"
138 if [ "$value" = "out" ]; then
139 append
"$var" "-o $device"
140 elif [ "$value" = "in" ]; then
141 append
"$var" "-i $device"
145 value
="$(echo "$value" | sed -e 's,-,:,g')"
146 add_insmod ipt_length
147 append
"$var" "-m length --length $value"
151 append
"$var" "-m limit --limit $value"
155 tcp
) append
"$var" "-m tcp --tcp-flags ALL $value";;
156 *) unset $var; return 0;;
160 config_get class
"${value##!}" classnr
161 [ -z "$class" ] && continue;
163 !*) append
"$var" "-m mark ! --mark $class";;
164 *) append
"$var" "-m mark --mark $class";;
169 config_get TOS
"$rule" 'TOS'
170 suffix
="-j TOS --set-tos "${TOS:-"Normal-Service"}
174 config_get DSCP
"$rule" 'DSCP'
175 [ -z "${DSCP%%[EBCA]*}" ] && set_value
="--set-dscp-class $DSCP" \
176 || set_value
="--set-dscp $DSCP"
177 suffix
="-j DSCP $set_value"
181 append
"$var" "$suffix"
182 case "$ports:$proto" in
183 1:) parse_matching_rule
"$var" "$section" "$options" "$prefix" "$suffix" "udp";;
195 config_set
"$2" "classgroup" "Default"
196 config_set
"$2" "upload" "128"
198 classify|default|reclassify
)
206 config_get TYPE
"$CONFIG_SECTION" TYPE
209 config_get_bool enabled
"$CONFIG_SECTION" enabled
1
210 [ 1 -eq "$enabled" ] ||
return 0
211 config_get classgroup
"$CONFIG_SECTION" classgroup
212 config_set
"$CONFIG_SECTION" imqdev
"$C"
214 append INTERFACES
"$CONFIG_SECTION"
215 config_set
"$classgroup" enabled
1
216 config_get device
"$CONFIG_SECTION" device
217 [ -z "$device" ] && {
218 device
="$(find_ifname ${CONFIG_SECTION})"
219 config_set
"$CONFIG_SECTION" device
"${device:-eth0}"
222 classgroup
) append CG
"$CONFIG_SECTION";;
223 classify|default|reclassify
)
225 classify
) var
="ctrules";;
228 config_get target
"$CONFIG_SECTION" target
229 config_set
"$CONFIG_SECTION" options
"$options"
230 append
"$var" "$CONFIG_SECTION"
239 config_get classes
"$1" classes
240 config_get default
"$1" default
241 for class
in $classes; do
243 config_set
"${class}" classnr
$c
245 $default) class_default
=$c;;
248 class_default
="${class_default:-$c}"
258 config_get tmp1
"$class" "$name"
259 config_get tmp2
"${class}_${type}" "$name"
262 export ${varname}="${tmp:-$default}"
267 [ -e $dir/tcrules.
awk ] || dir
=.
270 -v linespeed
="$rate" \
277 config_get device
"$iface" device
278 config_get_bool enabled
"$iface" enabled
1
279 [ -z "$device" -o 1 -ne "$enabled" ] && {
282 config_get upload
"$iface" upload
283 config_get halfduplex
"$iface" halfduplex
284 config_get download
"$iface" download
285 config_get classgroup
"$iface" classgroup
286 config_get_bool overhead
"$iface" overhead
0
288 download
="${download:-${halfduplex:+$upload}}"
289 enum_classes
"$classgroup"
290 for dir
in up
${halfduplex} ${download:+down}; do
293 [ "$overhead" = 1 ] && upload
=$
(($upload * 98 / 100 - (15 * 128 / $upload)))
300 add_insmod imq numdevs
="$num_imq"
301 config_get imqdev
"$iface" imqdev
302 [ "$overhead" = 1 ] && download
=$
(($download * 98 / 100 - (80 * 1024 / $download)))
311 for class
in $classes; do
312 cls_var pktsize
"$class" packetsize
$dir 1500
313 cls_var pktdelay
"$class" packetdelay
$dir 0
314 cls_var maxrate
"$class" limitrate
$dir 100
315 cls_var prio
"$class" priority
$dir 1
316 cls_var avgrate
"$class" avgrate
$dir 0
317 config_get classnr
"$class" classnr
318 append cstr
"$classnr:$prio:$avgrate:$pktsize:$pktdelay:$maxrate" "$N"
320 append
${prefix}q
"$(tcrules)" "$N"
321 export dev_
${dir}="ifconfig $dev up txqueuelen 5 >&- 2>&-
322 tc qdisc del dev $dev root >&- 2>&-
323 tc qdisc add dev $dev root handle 1: hfsc default ${class_default}0
324 tc class add dev $dev parent 1: classid 1:1 hfsc sc rate ${rate}kbit ul rate ${rate}kbit"
332 ${INSMOD:+$INSMOD$N}${dev_up:+$dev_up
340 unset INSMOD clsq clsf clsl d_clsq d_clsl d_clsf dev_up dev_down
345 for iface
in $INTERFACES; do
346 start_interface
"$iface" "$C"
355 for rule
in $rules; do
357 config_get target
"$rule" target
358 config_get target
"$target" classnr
359 config_get options
"$rule" options
361 ## If we want to override the TOS field, let's clear the DSCP field first.
362 [ ! -z "$(echo $options | grep 'TOS')" ] && {
363 s_options
=${options%%TOS}
365 parse_matching_rule iptrule
"$rule" "$s_options" "$prefix" "-j DSCP --set-dscp 0"
366 append
"$var" "$iptrule" "$N"
370 parse_matching_rule iptrule
"$rule" "$options" "$prefix" "-j MARK --set-mark $target"
371 append
"$var" "$iptrule" "$N"
382 add_rules iptrules
"$ctrules" "iptables -t mangle -A ${cg}_ct"
383 config_get classes
"$cg" classes
384 for class
in $classes; do
385 config_get mark
"$class" classnr
386 config_get maxsize
"$class" maxsize
387 [ -z "$maxsize" -o -z "$mark" ] ||
{
388 add_insmod ipt_length
389 append pktrules
"iptables -t mangle -A ${cg} -m mark --mark $mark -m length --length $maxsize: -j MARK --set-mark 0" "$N"
392 add_rules pktrules
"$rules" "iptables -t mangle -A ${cg}"
393 for iface
in $INTERFACES; do
394 config_get classgroup
"$iface" classgroup
395 config_get device
"$iface" device
396 config_get imqdev
"$iface" imqdev
397 config_get dl
"$iface" download
398 config_get halfduplex
"$iface" halfduplex
400 append up
"iptables -t mangle -A OUTPUT -o $device -j ${cg}" "$N"
401 append up
"iptables -t mangle -A FORWARD -o $device -j ${cg}" "$N"
403 [ -z "$halfduplex" ] ||
{
404 append down
"iptables -t mangle -A POSTROUTING -o $device -j IMQ --todev $imqdev" "$N"
406 append down
"iptables -t mangle -A PREROUTING -i $device -j ${cg}" "$N"
407 append down
"iptables -t mangle -A POSTROUTING -o $device -j ${cg}" "$N"
408 append down
"iptables -t mangle -A PREROUTING -i $device -j IMQ --todev $imqdev" "$N"
413 iptables -t mangle -N ${cg} >&- 2>&-
414 iptables -t mangle -N ${cg}_ct >&- 2>&-
415 ${iptrules:+${iptrules}${N}iptables -t mangle -A ${cg}_ct -j CONNMARK --save-mark}
416 iptables -t mangle -A ${cg} -j CONNMARK --restore-mark
417 iptables -t mangle -A ${cg} -m mark --mark 0 -j ${cg}_ct
419 $up$N${down:+${down}$N}
425 add_insmod ipt_multiport
426 add_insmod ipt_CONNMARK
428 iptables -t mangle -F
429 iptables -t mangle -X
438 [ -e .
/qos.conf
] && {
444 for iface
in $INTERFACES; do
445 export C
="$(($C + 1))"
450 start_interfaces
"$C"
454 start_interface
"$2" "$C"