#!/bin/sh
#set -x
tc qdisc del dev eth0 root
tc qdisc del dev eth1 root
tc qdisc add dev eth1 root handle 2:0 htb default 30
tc class add dev eth1 parent 2:0 classid 2:1 htb rate 4Mbit burst 15k
tc class add dev eth1 parent 2:1 classid 2:10 htb rate 4Mbit burst 15k
tc class add dev eth1 parent 2:1 classid 2:20 htb rate 2000kbit ceil 2Mbit burst 15k
tc class add dev eth1 parent 2:1 classid 2:30 htb rate 500kbit ceil 1000kbit burst 15k
tc qdisc add dev eth1 parent 2:10 handle 10: sfq perturb 10
tc qdisc add dev eth1 parent 2:20 handle 20: sfq perturb 10
tc qdisc add dev eth1 parent 2:30 handle 30: sfq perturb 10
U32_1="tc filter add dev eth1 protocol ip parent 2:0 prio 1 u32"
$U32_1 match ip src 59.108.65.50/32 flowid 2:10
U32_2="tc filter add dev eth1 protocol ip parent 2:0 prio 2 u32"
$U32_2 match ip dst 192.168.0.5/32 flowid 2:20
tc filter add dev eth1 protocol ip parent 2:0 prio 3 u32 match ip dst 192.168.0.0/24 flowid 2:30
iptables -A PREROUTING -t mangle -i eth1 -s 192.168.0.5/32 -j MARK --set-mark 1
#iptables -A PREROUTING -t mangle -i eth1 -s 192.168.0.0/24 -j MARK --set-mark 2
tc qdisc add dev eth0 root handle 1:0 htb default 30
tc class add dev eth0 parent 1:0 classid 1:1 htb rate 4Mbit burst 15k
tc class add dev eth0 parent 1:1 classid 1:10 htb rate 4Mbit burst 15k
tc class add dev eth0 parent 1:1 classid 1:20 htb rate 1000kbit ceil 2Mbit burst 15k
tc class add dev eth0 parent 1:1 classid 1:30 htb rate 400kbit ceil 400kbit burst 15k
tc qdisc add dev eth0 parent 1:10 handle 10: sfq perturb 10
tc qdisc add dev eth0 parent 1:20 handle 20: sfq perturb 10
tc qdisc add dev eth0 parent 1:30 handle 30: sfq perturb 10
U32="tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32"
tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 match ip dst 59.108.65.50/32 flowid 1:10
tc filter add dev eth0 parent 1:0 protocol ip prio 3 handle 1 fw classid 1:20
tc filter add dev eth0 parent 1:0 protocol ip prio 4 handle 2 fw classid 1:30
调试这个脚本中遇到很多问题。首先就是脚本报错。运行脚本,就出来很多提示,弄的我莫名其妙的。在google搜索这些错误提示,帮助也不大。
eth1为内网卡,可以限制下载流量
eth0为外网卡,可以限制上传流量
TC通过限制网卡发送数据包的速度来控制流量。原理可以参考《高级流量控制》