6, sh ./tc_bash.sh start //开启限速现在下行200kbit 上行100kbit
# 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
# Amounts of data can be specified in:
# To get the byte figure from bits, divide the number by 8 bit
# Name of the traffic control command.
# Name of the iptables command
IPTAB=/system/bin/iptables
IFCONFIG=/system/bin/ifconfig
# The network interface we're planning on limiting bandwidth.
# Download limit (in mega bits)
DNLD=200kbit # DOWNLOAD Limit
# Upload limit (in mega bits)
UPLD=100kbit # UPLOAD Limit
IP=$($IFCONFIG $IF | $AWK '{print $3}')
# IP address of the machine we are controlling
#IP=10.58.27.32 # Host IP
# Filter options for limiting the intended interface.
U32="$TC filter add dev $IF protocol ip parent 1:0 prio 1 u32"
# We'll use Hierarchical Token Bucket (HTB) to shape bandwidth.
# For detailed configuration options, please consult Linux man
$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
# 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.
#$TC qdisc del dev $IF root
$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秒重置一次散列函数。
#$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 qdisc del dev eth0 root
#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 the bandwidth shaping.
$TC qdisc del dev $IF root
# Display status of traffic control status.
echo"tc -s qdisc ls dev ${IF}"
echo"tc -s class ls dev ${IF}"
echo"tc -s filter ls dev ${IF}"
echo-n"Starting bandwidth shaping: "
echo-n"Stopping bandwidth shaping: "
echo-n"Restarting bandwidth shaping: "
echo"Bandwidth shaping status for $IF:"
echo"start ext shaping $IF:"
echo"Usage: tc_bash.sh {start|start_limit_dw|stop|restart|show}"