2 [ -e /etc
/functions.sh
] && .
/etc
/functions.sh || . .
/functions.sh
3 [ -x /sbin
/modprobe
] && {
12 eval "export isset=\${insmod_$1}"
16 [ "$2" ] && append INSMOD
"$rmmod $1 >&- 2>&-" "$N"
17 append INSMOD
"$insmod $* >&- 2>&-" "$N"; export insmod_
$1=1
22 [ -e /etc
/config
/network
] && {
23 # only try to parse network config on openwrt
29 config_get
"$1" ifname
33 echo "Interface not found."
38 parse_matching_rule
() {
48 append
"$var" "$prefix" "$N"
49 for option
in $options; do
51 proto
) config_get value
"$section" proto
; proto
="${proto:-$value}";;
54 config_get
type "$section" TYPE
56 classify
) unset pkt
; append
"$var" "-m mark --mark 0";;
57 default
) pkt
=1; append
"$var" "-m mark --mark 0";;
60 append
"$var" "${proto:+-p $proto}"
61 for option
in $options; do
62 config_get value
"$section" "$option"
64 case "$pkt:$option" in
66 append
"$var" "-s $value"
69 append
"$var" "-d $value"
74 append
"$var" "-m layer7 --l7proto $value${pkt:+ --l7pkt}"
76 *:ports|
*:srcports|
*:dstports
)
77 value
="$(echo "$value" | sed -e 's,-,:,g')"
80 ""|tcp|udp
) append
"$var" "-m ${proto:-tcp -p tcp} -m multiport";;
81 *) unset "$var"; return 0;;
85 config_set
"$section" srcports
""
86 config_set
"$section" dstports
""
87 config_set
"$section" portrange
""
88 append
"$var" "--ports $value"
91 config_set
"$section" ports
""
92 config_set
"$section" dstports
""
93 config_set
"$section" portrange
""
94 append
"$var" "--sports $value"
97 config_set
"$section" ports
""
98 config_set
"$section" srcports
""
99 config_set
"$section" portrange
""
100 append
"$var" "--dports $value"
106 config_set
"$section" ports
""
107 config_set
"$section" srcports
""
108 config_set
"$section" dstports
""
109 value
="$(echo "$value" | sed -e 's,-,:,g')"
111 ""|tcp|udp
) append
"$var" "-m ${proto:-tcp -p tcp} --sport $value --dport $value";;
112 *) unset "$var"; return 0;;
117 value
="$(echo "$value" | sed -e 's,-,:,g')"
118 add_insmod ipt_connbytes
119 append
"$var" "-m connbytes --connbytes $value --connbytes-dir both --connbytes-mode bytes"
124 !*) append
"$var" "-m tos ! --tos $value";;
125 *) append
"$var" "-m tos --tos $value"
131 [ -z "${value%%[EBCA]*}" ] && dscp_option
="--dscp-class"
133 !*) append
"$var" "-m dscp ! $dscp_option $value";;
134 *) append
"$var" "-m dscp $dscp_option $value"
138 value
="$(echo "$value" | sed -e 's,-,:,g')"
139 if [ "$value" = "out" ]; then
140 append
"$var" "-o $device"
141 elif [ "$value" = "in" ]; then
142 append
"$var" "-i $device"
146 value
="$(echo "$value" | sed -e 's,-,:,g')"
147 add_insmod ipt_length
148 append
"$var" "-m length --length $value"
152 append
"$var" "-m limit --limit $value"
156 tcp
) append
"$var" "-m tcp --tcp-flags ALL $value";;
157 *) unset $var; return 0;;
161 config_get class
"${value##!}" classnr
162 [ -z "$class" ] && continue;
164 !*) append
"$var" "-m mark ! --mark $class";;
165 *) append
"$var" "-m mark --mark $class";;
170 config_get TOS
"$rule" 'TOS'
171 suffix
="-j TOS --set-tos "${TOS:-"Normal-Service"}
175 config_get DSCP
"$rule" 'DSCP'
176 [ -z "${DSCP%%[EBCA]*}" ] && set_value
="--set-dscp-class $DSCP" \
177 || set_value
="--set-dscp $DSCP"
178 suffix
="-j DSCP $set_value"
182 append
"$var" "$suffix"
183 case "$ports:$proto" in
184 1:) parse_matching_rule
"$var" "$section" "$options" "$prefix" "$suffix" "udp";;
196 config_set
"$2" "classgroup" "Default"
197 config_set
"$2" "upload" "128"
199 classify|default|reclassify
)
207 config_get TYPE
"$CONFIG_SECTION" TYPE
210 config_get_bool enabled
"$CONFIG_SECTION" enabled
1
211 [ 1 -eq "$enabled" ] ||
return 0
212 config_get classgroup
"$CONFIG_SECTION" classgroup
213 config_set
"$CONFIG_SECTION" ifbdev
"$C"
215 append INTERFACES
"$CONFIG_SECTION"
216 config_set
"$classgroup" enabled
1
217 config_get device
"$CONFIG_SECTION" device
218 [ -z "$device" ] && {
219 device
="$(find_ifname ${CONFIG_SECTION})"
220 config_set
"$CONFIG_SECTION" device
"${device:-eth0}"
223 classgroup
) append CG
"$CONFIG_SECTION";;
224 classify|default|reclassify
)
226 classify
) var
="ctrules";;
229 config_get target
"$CONFIG_SECTION" target
230 config_set
"$CONFIG_SECTION" options
"$options"
231 append
"$var" "$CONFIG_SECTION"
240 config_get classes
"$1" classes
241 config_get default
"$1" default
242 for class
in $classes; do
244 config_set
"${class}" classnr
$c
246 $default) class_default
=$c;;
249 class_default
="${class_default:-$c}"
259 config_get tmp1
"$class" "$name"
260 config_get tmp2
"${class}_${type}" "$name"
263 export ${varname}="${tmp:-$default}"
268 [ -e $dir/tcrules.
awk ] || dir
=.
271 -v linespeed
="$rate" \
278 config_get device
"$iface" device
279 config_get_bool enabled
"$iface" enabled
1
280 [ -z "$device" -o 1 -ne "$enabled" ] && {
283 config_get upload
"$iface" upload
284 config_get_bool halfduplex
"$iface" halfduplex
285 config_get download
"$iface" download
286 config_get classgroup
"$iface" classgroup
287 config_get_bool overhead
"$iface" overhead
0
289 download
="${download:-${halfduplex:+$upload}}"
290 enum_classes
"$classgroup"
291 for dir
in ${halfduplex:-up} ${download:+down}; do
294 [ "$overhead" = 1 ] && upload
=$
(($upload * 98 / 100 - (15 * 128 / $upload)))
301 [ "$(ls -d /proc/sys/net/ipv4/conf/ifb* 2>&- | wc -l)" -ne "$num_ifb" ] && add_insmod ifb numifbs
="$num_ifb"
302 config_get ifbdev
"$iface" ifbdev
303 [ "$overhead" = 1 ] && download
=$
(($download * 98 / 100 - (80 * 1024 / $download)))
312 for class
in $classes; do
313 cls_var pktsize
"$class" packetsize
$dir 1500
314 cls_var pktdelay
"$class" packetdelay
$dir 0
315 cls_var maxrate
"$class" limitrate
$dir 100
316 cls_var prio
"$class" priority
$dir 1
317 cls_var avgrate
"$class" avgrate
$dir 0
318 cls_var qdisc
"$class" qdisc
$dir ""
319 cls_var filter
"$class" filter
$dir ""
320 config_get classnr
"$class" classnr
321 append cstr
"$classnr:$prio:$avgrate:$pktsize:$pktdelay:$maxrate:$qdisc:$filter" "$N"
323 append
${prefix}q
"$(tcrules)" "$N"
324 export dev_
${dir}="ifconfig $dev up txqueuelen 5 >&- 2>&-
325 tc qdisc del dev $dev root >&- 2>&-
326 tc qdisc add dev $dev root handle 1: hfsc default ${class_default}0
327 tc class add dev $dev parent 1: classid 1:1 hfsc sc rate ${rate}kbit ul rate ${rate}kbit"
329 [ -n "$download" ] && {
332 add_insmod act_connmark
333 add_insmod act_mirred
334 add_insmod sch_ingress
336 if [ -n "$halfduplex" ]; then
337 export dev_up
="tc qdisc del dev $device root >&- 2>&-
338 tc qdisc add dev $device root handle 1: hfsc
339 tc filter add dev $device parent 1: protocol ip prio 10 u32 match u32 0 0 flowid 1:1 action mirred egress redirect dev ifb$ifbdev"
340 elif [ -n "$download" ]; then
341 append dev_
${dir} "tc qdisc del dev $device ingress >&- 2>&-
342 tc qdisc add dev $device ingress
343 tc filter add dev $device parent ffff: protocol ip prio 1 u32 match u32 0 0 flowid 1:1 action connmark action mirred egress redirect dev ifb$ifbdev" "$N"
351 ${INSMOD:+$INSMOD$N}${dev_up:+$dev_up
359 unset INSMOD clsq clsf clsl d_clsq d_clsl d_clsf dev_up dev_down
364 for iface
in $INTERFACES; do
365 start_interface
"$iface" "$C"
374 for rule
in $rules; do
376 config_get target
"$rule" target
377 config_get target
"$target" classnr
378 config_get options
"$rule" options
380 ## If we want to override the TOS field, let's clear the DSCP field first.
381 [ ! -z "$(echo $options | grep 'TOS')" ] && {
382 s_options
=${options%%TOS}
384 parse_matching_rule iptrule
"$rule" "$s_options" "$prefix" "-j DSCP --set-dscp 0"
385 append
"$var" "$iptrule" "$N"
389 parse_matching_rule iptrule
"$rule" "$options" "$prefix" "-j MARK --set-mark $target"
390 append
"$var" "$iptrule" "$N"
400 add_rules iptrules
"$ctrules" "iptables -t mangle -A qos_${cg}_ct"
401 config_get classes
"$cg" classes
402 for class
in $classes; do
403 config_get mark
"$class" classnr
404 config_get maxsize
"$class" maxsize
405 [ -z "$maxsize" -o -z "$mark" ] ||
{
406 add_insmod ipt_length
407 append pktrules
"iptables -t mangle -A qos_${cg} -m mark --mark $mark -m length --length $maxsize: -j MARK --set-mark 0" "$N"
410 add_rules pktrules
"$rules" "iptables -t mangle -A qos_${cg}"
411 for iface
in $INTERFACES; do
412 config_get classgroup
"$iface" classgroup
413 config_get device
"$iface" device
414 config_get ifbdev
"$iface" ifbdev
415 config_get upload
"$iface" upload
416 config_get download
"$iface" download
417 config_get halfduplex
"$iface" halfduplex
418 download
="${download:-${halfduplex:+$upload}}"
419 append up
"iptables -t mangle -A OUTPUT -o $device -j qos_${cg}" "$N"
420 append up
"iptables -t mangle -A FORWARD -o $device -j qos_${cg}" "$N"
424 iptables -t mangle -N qos_${cg} >&- 2>&-
425 iptables -t mangle -N qos_${cg}_ct >&- 2>&-
426 ${iptrules:+${iptrules}${N}iptables -t mangle -A qos_${cg}_ct -j CONNMARK --save-mark}
427 iptables -t mangle -A qos_${cg} -j CONNMARK --restore-mark
428 iptables -t mangle -A qos_${cg} -m mark --mark 0 -j qos_${cg}_ct
430 $up$N${down:+${down}$N}
436 add_insmod ipt_multiport
437 add_insmod ipt_CONNMARK
445 # Builds up a list of iptables commands to flush the qos_* chains,
446 # remove rules referring to them, then delete them
448 # Print rules in the mangle table, like iptables-save
449 iptables
-t mangle
-S |
450 # Find rules for the qos_* chains
451 grep '^-N qos_\|-j qos_' |
452 # Exclude rules in qos_* chains (inter-qos_* refs)
454 # Replace -N with -X and hold, with -F and print
456 # Print held lines at the end (note leading newline)
457 sed -e '/^-N/{s/^-N/-X/;H;s/^-X/-F/}' \
460 # Make into proper iptables calls
461 # Note: awkward in previous call due to hold space usage
462 sed -n -e 's/^./iptables -t mangle &/p'
467 [ -e .
/qos.conf
] && {
473 for iface
in $INTERFACES; do
474 export C
="$(($C + 1))"
479 start_interfaces
"$C"
483 start_interface
"$2" "$C"