TC命令格式:
加入
tc qdisc [ add | change | replace | link ] dev DEV [ parent qdisc-id | root ] [ handle qdisc-id ] qdisc [ qdisc specific parameters ]
tc class [ add | change | replace ] dev DEV parent qdisc-id [ classid class-id ] qdisc [ qdisc specific parameters ]
tc filter [ add | change | replace ] dev DEV [ parent qdisc-id | root ] protocol protocol prio priority filtertype [ filtertype specific parameters ] flowid flow-id
顯示
tc [-s | -d ] qdisc show [ dev DEV ]
tc [-s | -d ] class show dev DEV tc filter show dev DEV
查看TC的狀態
tc -s -d qdisc show dev eth0
tc -s -d class show dev eth0
刪除tc規則
tc qdisc del dev eth0 root
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
限制單個IP
1,添加規則
[root@ct-test ~]# tc qdisc add dev eth1 root handel 1: htb r2q 1
[root@ct-test ~]# tc class add dev eth1 parent 1: classid 1:1 htb rate 5mbit ceil 10mbit
[root@ct-test ~]# tc filter add dev eth1 parent 1: protocol ip prio 16 u32 match ip dst 192.168.3.153 flowid 1:1 #filter用來將用戶划入到具體的控制策略中(即不同的class中).
TC包括三個基本的構成塊: 隊列規定qdisc(queueing discipline )、類(class)和分類器(Classifiers)
就可以限制192.168.3.153的下載速度為5Mbit最高可以10Mbit
r2q,是指沒有default的root,使整個網路的帶寬沒有限制
2,查看添加的規則
[root@ct-test ~]# tc -s -d qdisc show dev eth1 #查看隊列(用來實現控制網路的收發速度)
qdisc htb 1: r2q 1 default 0 direct_packets_stat 48846 ver 3.17
Sent 28638574 bytes 48870 pkts (dropped 0, overlimits 0 requeues 0)
[root@ct-test ~]# tc -s -d class show dev eth1 #查看類(class用來表示控制策略)
class htb 1:1 root prio 0 quantum 200000 rate 5Mbit ceil 10Mbit burst 2224b/8 mpu 0b overhead 0b cburst 2848b/8 mpu 0b overhead 0b level 0
Sent 14679 bytes 52 pkts (dropped 0, overlimits 0 requeues 0)
rate 202bit
lended: 52 borrowed: 0 giants: 0
tokens: 3568 ctokens: 2296
[root@ct-test ~]#
3,刪除添加的規則
[root@ct-test ~]# tc qdisc del dev eth1 root
4,再次查看規則
[root@ct-test ~]# tc -s -d qdisc show dev eth1
qdisc pfifo_fast 0: bands 3 priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
Sent 326504 bytes 488 pkts (dropped 0, overlimits 0 requeues 0)
[root@ct-test ~]# tc -s -d class show dev eth1 #已刪除
[root@ct-test ~]#
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
限制網段,如果多個網段可以添加分類.tc很強大,這裡是小小巫
tc qdisc add dev eth0 root handle 1: htb r2q 1
tc class add dev eth0 parent 1: classid 1:1 htb rate 50mbit ceil 1000mbit
tc filter add dev eth0 parent 1: protocol ip prio 16 u32 match ip dst 192.168.3.0/24 flowid 1:1
就可以限制192.168.3.0 到255 的帶寬為3000k了,實際下載速度為200k左右.
這種情況下,這個網段所有機器共享這200k的帶寬.
還可以加入一個sfq(隨機公平隊列)
tc qdisc add dev eth0 root handle 1: htb r2q 1
tc class add dev eth0 parent 1: classid 1:1 htb rate 3000kbit burst 10k
tc qdisc add dev eth0 parent 1:1 handle 10: sfq perturb 10
tc filter add dev eth0 parent 1: protocol ip prio 16 u32 match ip dst 192.168.111.168 flowid 1:1
sfq,他可以防止一個段內的一個ip佔用整個帶寬.
下面是實例:
[root@ct-test ~]# tc qdisc add dev eth1 root handle 1: htb r2q 1
[root@ct-test ~]# tc class add dev eth1 parent 1: classid 1:1 htb rate 5mbit ceil 10mbit
[root@ct-test ~]# tc filter add dev eth1 parent 1: protocol ip prio 16 u32 match ip dst 192.168.3.0/24 flowid 1:1
[root@ct-test ~]# tc -s -d qdisc show dev eth1
qdisc htb 1: r2q 1 default 0 direct_packets_stat 6402 ver 3.17
Sent 7404865 bytes 9868 pkts (dropped 0, overlimits 497 requeues 0)
[root@ct-test ~]# tc -s -d class show dev eth1
class htb 1:1 root prio 0 quantum 200000 rate 5Mbit ceil 10Mbit burst 2224b/8 mpu 0b overhead 0b cburst 2848b/8 mpu 0b overhead 0b level 0
Sent 6466768 bytes 6845 pkts (dropped 0, overlimits 0 requeues 0)
rate 56721bit 72pps
lended: 6845 borrowed: 0 giants: 0
tokens: 3502 ctokens: 2263
本文出自 「要成長要收穫」 博客,請務必保留此出處http://s2t148.blog.51cto.com/3858027/782818