在Linux上使用流量整形(tc),尝试将吞吐量限制在2个级别。
班级1:20继承自1:10,但交通限制不适用。使用最后一个班级的限制来限制流量。
class htb 1:1 root rate 1Gbit ceil 1Gbit burst 15125b cburst 1375b
class htb 1:10 parent 1:1 rate 4Mbit ceil 5Mbit burst 15Kb cburst 1600b
class htb 1:20 parent 1:10 leaf 200: prio 0 rate 9Mbit ceil 10Mbit burst 15Kb cburst 1600b
class htb 1:30 parent 1:10 leaf 300: prio 0 rate 9Mbit ceil 10Mbit burst 15Kb cburst 1600b
我使用以下过滤器对每个电台进行过滤:
filter parent 1: protocol all pref 5 u32
filter parent 1: protocol all pref 5 u32 fh 800: ht divisor 1
filter parent 1: protocol all pref 5 u32 fh 800::20 order 32 key ht 800 bkt 0 flowid 1:20
match 00000800/0000ffff at -4
match 5be42dc1/ffffffff at -12
match 00009cd3/0000ffff at -16
使用iperf TCP测试:
[ ID] Interval Transfer Bandwidth
[ 3] 0.0- 1.0 sec 1.25 MBytes 10.5 Mbits/sec
[ 3] 1.0- 2.0 sec 1.12 MBytes 9.44 Mbits/sec
[ 3] 2.0- 3.0 sec 1.00 MBytes 8.39 Mbits/sec
所有流量都通过了2个类:
class htb 1:10 parent 1:1 rate 4Mbit ceil 5Mbit linklayer ethernet burst 15Kb/1 mpu 0b overhead 0b cburst 1600b/1 mpu 0b overhead 0b level 6
Sent 31091090 bytes 20545 pkt (dropped 0, overlimits 0 requeues 0)
rate 0bit 0pps backlog 0b 0p requeues 0
lended: 0 borrowed: 0 giants: 0
tokens: -135041557 ctokens: -86778070
class htb 1:20 parent 1:10 leaf 200: prio 0 quantum 112500 rate 9Mbit ceil 10Mbit linklayer ethernet burst 15Kb/1 mpu 0b overhead 0b cburst 1600b/1 mpu 0b overhead 0b level 0
Sent 31091090 bytes 20545 pkt (dropped 17, overlimits 0 requeues 0)
rate 0bit 0pps backlog 0b 101p requeues 0
lended: 20545 borrowed: 0 giants: 0
tokens: -20701 ctokens: 1075
答案 0 :(得分:2)
如果您想限制每个IP的流量,那么您可以使用此方法(我使用它来限制使用openvz虚拟化的虚拟服务器带宽,但您可以根据您的要求对其进行自定义):
tc qdisc del dev venet0 root
tc qdisc add dev venet0 root handle 1: htb default 10
tc class add dev venet0 parent 1: classid 1:1 htb rate 1000mbit burst 15k
tc class add dev venet0 parent 1:1 classid 1:10 htb rate 50mbit ceil 50mbit burst 15k
tc qdisc add dev venet0 parent 1:10 handle 10: sfq perturb 10
interface=eth0
tc qdisc del dev $interface root
tc qdisc add dev $interface root handle 1: htb default 10
tc class add dev $interface parent 1: classid 1:1 htb rate 1000mbit burst 15k
tc class add dev $interface parent 1:1 classid 1:110 htb rate 50mbit ceil 50mbit burst 15k
tc qdisc add dev $interface parent 1:110 handle 110: sfq perturb 10
然后为每个ipv4地址:
tc filter add dev venet0 protocol ip parent 1:0 prio 1 u32 match ip dst $IPADDRESS flowid 1:10
tc filter add dev $interface protocol ip parent 1:0 prio 1 u32 match ip src $IPADDRESS flowid 1:110
对于每个ipv6:
tc filter add dev venet0 protocol ipv6 parent 1:0 prio 2 u32 match ip6 dst $ipv6/128 flowid 1:10
tc filter add dev $interface protocol ipv6 parent 1:0 prio 2 u32 match ip6 src $ipv6/128 flowid 1:110
DS48236.vpsnet.com:~$ tc qdisc show
qdisc htb 1: dev eth0 root refcnt 2 r2q 10 default 10 direct_packets_stat 2630716
qdisc sfq 110: dev eth0 parent 1:110 limit 127p quantum 1514b divisor 1024 perturb 10sec
qdisc sfq 120: dev eth0 parent 1:120 limit 127p quantum 1514b divisor 1024 perturb 10sec
qdisc sfq 130: dev eth0 parent 1:130 limit 127p quantum 1514b divisor 1024 perturb 10sec
qdisc sfq 140: dev eth0 parent 1:140 limit 127p quantum 1514b divisor 1024 perturb 10sec
qdisc sfq 150: dev eth0 parent 1:150 limit 127p quantum 1514b divisor 1024 perturb 10sec
qdisc sfq 160: dev eth0 parent 1:160 limit 127p quantum 1514b divisor 1024 perturb 10sec
qdisc sfq 170: dev eth0 parent 1:170 limit 127p quantum 1514b divisor 1024 perturb 10sec
qdisc sfq 180: dev eth0 parent 1:180 limit 127p quantum 1514b divisor 1024 perturb 10sec
qdisc sfq 190: dev eth0 parent 1:190 limit 127p quantum 1514b divisor 1024 perturb 10sec
qdisc sfq 1110: dev eth0 parent 1:1110 limit 127p quantum 1514b divisor 1024 perturb 10sec
qdisc sfq 1111: dev eth0 parent 1:1111 limit 127p quantum 1514b divisor 1024 perturb 10sec
qdisc sfq 1100: dev eth0 parent 1:1100 limit 127p quantum 1514b divisor 1024 perturb 10sec
qdisc htb 1: dev venet0 root refcnt 2 r2q 10 default 10 direct_packets_stat 0
qdisc sfq 10: dev venet0 parent 1:10 limit 127p quantum 1514b divisor 1024 perturb 10sec
qdisc sfq 20: dev venet0 parent 1:20 limit 127p quantum 1514b divisor 1024 perturb 10sec
qdisc sfq 30: dev venet0 parent 1:30 limit 127p quantum 1514b divisor 1024 perturb 10sec
qdisc sfq 40: dev venet0 parent 1:40 limit 127p quantum 1514b divisor 1024 perturb 10sec
qdisc sfq 50: dev venet0 parent 1:50 limit 127p quantum 1514b divisor 1024 perturb 10sec
qdisc sfq 60: dev venet0 parent 1:60 limit 127p quantum 1514b divisor 1024 perturb 10sec
qdisc sfq 70: dev venet0 parent 1:70 limit 127p quantum 1514b divisor 1024 perturb 10sec
qdisc sfq 80: dev venet0 parent 1:80 limit 127p quantum 1514b divisor 1024 perturb 10sec
qdisc sfq 90: dev venet0 parent 1:90 limit 127p quantum 1514b divisor 1024 perturb 10sec
qdisc sfq 110: dev venet0 parent 1:110 limit 127p quantum 1514b divisor 1024 perturb 10sec
qdisc sfq 111: dev venet0 parent 1:111 limit 127p quantum 1514b divisor 1024 perturb 10sec
qdisc sfq 100: dev venet0 parent 1:100 limit 127p quantum 1514b divisor 1024 perturb 10sec
DS48236.vpsnet.com:~$
如果要限制每个接口的流量,则需要修改:
对于每个ipv4地址:
tc filter add dev venet0 protocol ip parent 1:0 prio 1 u32 match ip dst $IPADDRESS flowid 1:10
tc filter add dev $interface protocol ip parent 1:0 prio 1 u32 match ip src $IPADDRESS flowid 1:110
并且不要为venet0
创建qdisc和类因此,使用一个界面,它将类似于:
interface=eth0
tc qdisc del dev $interface root
tc qdisc add dev $interface root handle 1: htb default 10
tc class add dev $interface parent 1: classid 1:1 htb rate 1000mbit burst 15k
tc class add dev $interface parent 1:1 classid 1:110 htb rate 50mbit ceil 50mbit burst 15k
tc qdisc add dev $interface parent 1:110 handle 110: sfq perturb 10
每个ip:
tc filter add dev $interface protocol ip parent 1:0 prio 1 u32 match ip dst $IPADDRESS flowid 1:110
tc filter add dev $interface protocol ip parent 1:0 prio 1 u32 match ip src $IPADDRESS flowid 1:110