6, sh ./tc_bash.sh start //開啓限速如今下行200kbit 上行100kbit 函數
#!/bin/bash
#
# tc uses the following units when passed as a parameter.
# kbps: Kilobytes per second
# mbps: Megabytes per second
# kbit: Kilobits per second
# mbit: Megabits per second
# bps: Bytes per second
# Amounts of data can be specified in:
# kb or k: Kilobytes
# mb or m: Megabytes
# mbit: Megabits
# kbit: Kilobits
# To get the byte figure from bits, divide the number by 8 bit
#
#
# Name of the traffic control command.
TC=/system/bin/tc
# Name of the iptables command
IPTAB=/system/bin/iptables
# Name of ifconfig
IFCONFIG=/system/bin/ifconfig
# Name of awk
AWK=/sbin/awk
# The network interface we're planning on limiting bandwidth.
#IF=wlan0 # Interface
IF=lo # Interface
# Download limit (in mega bits)
DNLD=200kbit # DOWNLOAD Limit
# Upload limit (in mega bits)
UPLD=100kbit # UPLOAD Limit
IP=127.0.0.1
IP=$($IFCONFIG $IF | $AWK '{print $3}')
# IP address of the machine we are controlling
#IP=10.58.27.32 # Host IP
echo"$IP"
# Filter options for limiting the intended interface.
U32="$TC filter add dev $IF protocol ip parent 1:0 prio 1 u32"
start() {
# We'll use Hierarchical Token Bucket (HTB) to shape bandwidth.
# For detailed configuration options, please consult Linux man
# page.
$TC qdisc add dev $IF root handle 1: htb default 30
$TC class add dev $IF parent 1: classid 1:1 htb rate $DNLD ceil $DNLD
$TC class add dev $IF parent 1: classid 1:2 htb rate $UPLD ceil $UPLD
$U32 match ip dst $IP/32flowid 1:1
$U32 match ip src $IP/32flowid 1:2
# The first line creates the root qdisc, and the next two lines
# create two child qdisc that are to be used to shape download
# and upload bandwidth.
#
# The 4th and 5th line creates the filter to match the interface.
# The 'dst' IP address is used to limit download speed, and the
# 'src' IP address is used to limit upload speed.
}
start_limit_dw() {
#刪除原來的tc規則隊列
#$TC qdisc del dev $IF root
#添加tc規則隊列
$TC qdisc add dev $IF root handle 10: htb default 256
#生成根類
$TC class add dev $IF parent 10: classid 10:1 htb rate 500kbit ceil 500kbit
#支類列表用於限制速度
#這裏的rate指的是保證帶寬,ceil是最大帶寬。
$TC class add dev $IF parent 10:1 classid 10:10 htb rate $DNLD ceil $DNLD prio 1
#添加支類規則隊列
#採用sfq僞隨機隊列,而且10秒重置一次散列函數。
#android not support sfq
#$TC qdisc add dev $IF parent 10:10 handle 101: sfq perturb 10
#創建網絡包過濾器,設置fw。android not support fw
#$TC filter add dev $IF parent 10: protocol ip prio 10 handle 1 fw classid 10:10
#在iptables裏面設定mark值,與上面的handle值對應。
$IPTAB -t mangle -A POSTROUTING -d $IP -j MARK --set-mark 1
##刪除原來的tc規則隊列
#TC qdisc del dev eth0 root
#
##添加tc規則隊列
#TC qdisc add dev eth0 root handle 20: htb default 256
#
##生成根類
#TC class add dev eth0 parent 20: classid 20:1 htb rate 1mbit ceil 1mbit
#
##支類列表用於限制速度
#TC class add dev eth0 parent 20:1 classid 20:10 htb rate 40kbps ceil 40kbps prio 1
#
##添加支類規則隊列
#TC qdisc add dev eth0 parent 20:10 handle 201: sfq perturb 10
#
##創建網絡包過濾器
#TC filter add dev eth0 parent 20: protocol ip prio 100 handle 2 fw classid 20:10
#IPTAB -t mangle -A PREROUTING -s IP -j MARK --set-mark 2
}
stop() {
# Stop the bandwidth shaping.
$TC qdisc del dev $IF root
}
restart() {
# Self-explanatory.
stop
sleep1
start
}
show() {
# Display status of traffic control status.
echo"tc -s qdisc ls dev ${IF}"
$TC -s qdisclsdev $IF
echo"tc -s class ls dev ${IF}"
$TC -s classlsdev $IF
echo"tc -s filter ls dev ${IF}"
$TC -s filterlsdev $IF
}
case"$1"in
start)
echo-n"Starting bandwidth shaping: "
start
echo"done"
;;
stop)
echo-n"Stopping bandwidth shaping: "
stop
echo"done"
;;
restart)
echo-n"Restarting bandwidth shaping: "
restart
echo"done"
;;
show)
echo"Bandwidth shaping status for $IF:"
show
echo""
;;
start_limit_dw)
echo"start ext shaping $IF:"
start_limit_dw
echo"done"
;;
*)
pwd=$(pwd)
echo"Usage: tc_bash.sh {start|start_limit_dw|stop|restart|show}"
;;
esac
exit0