利用tc工具做限速
2014-02-17 by Brant Xiong前言
有关tc的介绍,可以参见tc
tc的使用
tc封装的脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #!/bin/bash
#a simple example for "tc"
#set the param
PORT=$2
DELAY=$3
SPEED=$4
FLOW=$5
NETWORK_CARD=eth1
set -x
start () {
/sbin/tc qdisc add dev ${NETWORK_CARD} root handle 1: htb default 25
/sbin/tc class add dev ${NETWORK_CARD} parent 1: classid 1:1 htb rate ${SPEED}kbps
/sbin/tc qdisc add dev ${NETWORK_CARD} parent 1:1 netem delay ${DELAY}ms ${FLOW}ms
/sbin/tc filter add dev ${NETWORK_CARD} parent 1: protocol ip prio 1 u32 match ip sport ${PORT} 0xffff flowid 1:1
}
stop () {
/sbin/tc qdisc dele dev ${NETWORK_CARD} root
}
case "$1" in
start)
start
;;
stop)
stop
;;
*)
echo "Usage: `basename $0` {start|stop} port delay(ms) speed(KB) flow(ms)"
esac
|
使用方法
3g 限制http: sh /root/net_limit.sh start 8900 200 355 0
3g 限制Spdy: sh /root/net_limit.sh start 8901 200 355 0
慢wifi限制http: sh /root/net_limit.sh start 8900 80 2048 0
慢wifi限制spdy: sh /root/net_limit.sh start 8901 80 2048 0
快wifi限制http: sh /root/net_limit.sh start 8900 40 14748 0
快wifi限制spdy: sh /root/net_limit.sh start 8901 40 14748 0
查看当前限制
tc -s qdisc ls dev eth1