一铃将、什么是TC?
TC(traffic control) 是 Linux 中的流量控制工具食店。它是通過控制 netem 來實(shí)現(xiàn)的網(wǎng)絡(luò)場景模擬霎奢。該工具是直接對物理網(wǎng)卡生效的蠕蚜,如果是邏輯網(wǎng)卡扣囊,則該控制無效乎折。如果是用的虛擬機(jī),可視虛擬網(wǎng)卡為物理網(wǎng)卡侵歇。
二骂澄、什么是HTB?
HTB 是 Hierarchy Token Bucket
的縮寫。通過在實(shí)踐基礎(chǔ)上的改進(jìn)惕虑,它實(shí)現(xiàn)了一個(gè)豐富的連接共享類別體系坟冲。使用 HTB 可以很容易地保證每個(gè)類別的帶寬,雖然它也允許特定的類可以突破帶寬上限溃蔫,占用別的類的帶寬健提。HTB 可以通過 TBF(Token Bucket Filter) 實(shí)現(xiàn)帶寬限制,也能夠劃分類別的優(yōu)先級酒唉。
三矩桂、TC使用步驟
要對網(wǎng)卡進(jìn)行流量控制的配置,需要進(jìn)行如下的步驟:
- 為網(wǎng)卡配置一個(gè)隊(duì)列
- 在該隊(duì)列上建立分類
- 根據(jù)需要建立子隊(duì)列和子分類
- 為每個(gè)分類建立過濾器
- 建立與過濾器配合的路由表
四、基本操作
1侄榴、正常網(wǎng)絡(luò)
使用 iperf 產(chǎn)生流量
Server 端:
iperf - s
復(fù)制代碼
Client 端:
iperf -c 172.17.211.143 -p 5001 -i 2 -P 5
復(fù)制代碼
Server 端結(jié)果:
[ 12] 0.0-10.1 sec 95.2 MBytes 79.3 Mbits/sec
[ 10] 0.0-10.1 sec 158 MBytes 131 Mbits/sec
[ 6] 0.0-10.1 sec 116 MBytes 95.7 Mbits/sec
[ 9] 0.0-10.2 sec 143 MBytes 118 Mbits/sec
[ 4] 0.0-10.3 sec 183 MBytes 150 Mbits/sec
[ 7] 0.0-10.3 sec 117 MBytes 96.0 Mbits/sec
[ 11] 0.0-10.3 sec 156 MBytes 127 Mbits/sec
[ 14] 0.0-10.3 sec 138 MBytes 113 Mbits/sec
[ 8] 0.0-10.3 sec 136 MBytes 111 Mbits/sec
[ 5] 0.0-10.3 sec 162 MBytes 132 Mbits/sec
[SUM] 0.0-10.3 sec 1.37 GBytes 1.14 Gbits/sec
復(fù)制代碼
Client 端:
[ ID] Interval Transfer Bandwidth
[ 6] 0.0- 2.0 sec 88.9 MBytes 373 Mbits/sec
[ 4] 0.0- 2.0 sec 85.8 MBytes 360 Mbits/sec
[ 5] 0.0- 2.0 sec 68.2 MBytes 286 Mbits/sec
[ 7] 0.0- 2.0 sec 52.2 MBytes 219 Mbits/sec
[ 3] 0.0- 2.0 sec 92.5 MBytes 388 Mbits/sec
[SUM] 0.0- 2.0 sec 388 MBytes 1.63 Gbits/sec
[ 4] 2.0- 4.0 sec 62.4 MBytes 262 Mbits/sec
[ 6] 2.0- 4.0 sec 48.9 MBytes 205 Mbits/sec
[ 5] 2.0- 4.0 sec 27.4 MBytes 115 Mbits/sec
[ 3] 2.0- 4.0 sec 68.9 MBytes 289 Mbits/sec
[ 7] 2.0- 4.0 sec 44.6 MBytes 187 Mbits/sec
[SUM] 2.0- 4.0 sec 252 MBytes 1.06 Gbits/sec
[ 3] 4.0- 6.0 sec 45.5 MBytes 191 Mbits/sec
[ 5] 4.0- 6.0 sec 30.0 MBytes 126 Mbits/sec
[ 4] 4.0- 6.0 sec 54.8 MBytes 230 Mbits/sec
[ 6] 4.0- 6.0 sec 69.4 MBytes 291 Mbits/sec
[ 7] 4.0- 6.0 sec 53.1 MBytes 223 Mbits/sec
[SUM] 4.0- 6.0 sec 253 MBytes 1.06 Gbits/sec
[ 4] 6.0- 8.0 sec 40.4 MBytes 169 Mbits/sec
[ 6] 6.0- 8.0 sec 25.6 MBytes 107 Mbits/sec
[ 7] 6.0- 8.0 sec 76.1 MBytes 319 Mbits/sec
[ 3] 6.0- 8.0 sec 59.1 MBytes 248 Mbits/sec
[ 5] 6.0- 8.0 sec 38.2 MBytes 160 Mbits/sec
[SUM] 6.0- 8.0 sec 240 MBytes 1.00 Gbits/sec
[ 6] 8.0-10.0 sec 37.8 MBytes 158 Mbits/sec
[ 6] 0.0-10.0 sec 270 MBytes 227 Mbits/sec
[ 4] 8.0-10.0 sec 39.9 MBytes 167 Mbits/sec
[ 4] 0.0-10.1 sec 283 MBytes 234 Mbits/sec
[ 5] 8.0-10.0 sec 40.8 MBytes 171 Mbits/sec
[ 5] 0.0-10.1 sec 205 MBytes 169 Mbits/sec
[ 7] 8.0-10.0 sec 48.0 MBytes 201 Mbits/sec
[ 7] 0.0-10.1 sec 274 MBytes 227 Mbits/sec
[ 3] 8.0-10.0 sec 84.8 MBytes 355 Mbits/sec
[SUM] 8.0-10.0 sec 251 MBytes 1.05 Gbits/sec
[ 3] 0.0-10.2 sec 351 MBytes 289 Mbits/sec
[SUM] 0.0-10.2 sec 1.35 GBytes 1.14 Gbits/sec
復(fù)制代碼
做了多遍雹锣,都和上面的結(jié)果相似。5 個(gè)線程加在一起每秒大概 1Gbits 左右癞蚕。
2蕊爵、模擬網(wǎng)絡(luò)丟包
模擬命令:
tc qdisc add dev eth0 root netem loss 10%
復(fù)制代碼
丟包率與帶寬的關(guān)系: [圖片上傳失敗...(image-1ab65c-1658806816109)]
3、模擬網(wǎng)絡(luò)延遲
模擬命令:
tc qdisc add dev eth0 root netem delay 100ms
復(fù)制代碼
延遲與帶寬的關(guān)系: [圖片上傳失敗...(image-1cf3c6-1658806816109)]
4桦山、HTB 隊(duì)列分層限制
分類配置:
tc qdisc add dev eth0 root handle 1: htb default 2
tc class add dev eth0 parent 1: classid 1:1 htb rate 100Mbps ceil 100Mbps
tc class add dev eth0 parent 1:1 classid 1:2 htb rate 20Mbps ceil 20Mbps
tc class add dev eth0 parent 1:1 classid 1:3 htb rate 50Mbps ceil 50Mbps
tc class add dev eth0 parent 1:1 classid 1:4 htb rate 20Mbps ceil 20Mbps
tc filter add dev eth0 parent 1:0 protocol ip prio 100 route
tc filter add dev eth0 parent 1:0 protocol ip prio 100 route to 2 flowid 1:2
tc filter add dev eth0 parent 1:0 protocol ip prio 100 route to 3 flowid 1:3
tc filter add dev eth0 parent 1:0 protocol ip prio 100 route to 4 flowid 1:4
ip route add 172.17.211.144 dev eth0 via 172.17.211.143 realm 2
[root@7dgroup ~]# tc -s class ls dev eth0
class htb 1:1 root rate 800000Kbit ceil 800000Kbit burst 1600b cburst 1600b
Sent 1350897 bytes 6146 pkt (dropped 0, overlimits 0 requeues 0)
rate 0bit 0pps backlog 0b 0p requeues 0
lended: 0 borrowed: 0 giants: 0
tokens: 234 ctokens: 234
class htb 1:2 parent 1:1 prio 0 rate 160000Kbit ceil 160000Kbit burst 1600b cburst 1600b
Sent 1350897 bytes 6146 pkt (dropped 0, overlimits 0 requeues 0)
rate 0bit 0pps backlog 0b 0p requeues 0
lended: 5850 borrowed: 0 giants: 0
tokens: 1170 ctokens: 1170
class htb 1:3 parent 1:1 prio 0 rate 400000Kbit ceil 400000Kbit burst 1600b cburst 1600b
Sent 0 bytes 0 pkt (dropped 0, overlimits 0 requeues 0)
rate 0bit 0pps backlog 0b 0p requeues 0
lended: 0 borrowed: 0 giants: 0
tokens: 500 ctokens: 500
class htb 1:4 parent 1:1 prio 0 rate 160000Kbit ceil 160000Kbit burst 1600b cburst 1600b
Sent 0 bytes 0 pkt (dropped 0, overlimits 0 requeues 0)
rate 0bit 0pps backlog 0b 0p requeues 0
lended: 0 borrowed: 0 giants: 0
tokens: 1250 ctokens: 1250
復(fù)制代碼
5攒射、網(wǎng)絡(luò)限流效果
測試手段從 A 機(jī)往 B 機(jī)用 iperf 開 5 個(gè)線程發(fā)數(shù)據(jù)包。
不限制:
[ 5] 0.0-10.0 sec 168 MBytes 140 Mbits/sec
[ 7] 0.0-10.1 sec 75.2 MBytes 62.4 Mbits/sec
[ 8] 0.0-10.2 sec 210 MBytes 172 Mbits/sec
[ 4] 0.0-10.3 sec 92.8 MBytes 75.7 Mbits/sec
[ 6] 0.0-10.3 sec 158 MBytes 129 Mbits/sec
[SUM] 0.0-10.3 sec 704 MBytes 574 Mbits/sec
復(fù)制代碼
限 10 M
[ 8] 0.0-10.2 sec 16.6 MBytes 13.7 Mbits/sec
[ 4] 0.0-10.2 sec 16.4 MBytes 13.5 Mbits/sec
[ 5] 0.0-10.2 sec 14.5 MBytes 11.9 Mbits/sec
[ 6] 0.0-10.2 sec 25.8 MBytes 21.2 Mbits/sec
[ 7] 0.0-10.2 sec 19.8 MBytes 16.2 Mbits/sec
[SUM] 0.0-10.2 sec 93.0 MBytes 76.4 Mbits/sec
復(fù)制代碼
限 20 M
[ 5] 0.0-10.1 sec 55.6 MBytes 46.0 Mbits/sec
[ 7] 0.0-10.2 sec 28.9 MBytes 23.8 Mbits/sec
[ 9] 0.0-10.2 sec 26.1 MBytes 21.6 Mbits/sec
[ 4] 0.0-10.2 sec 45.0 MBytes 37.1 Mbits/sec
[ 6] 0.0-10.2 sec 29.5 MBytes 24.3 Mbits/sec
[SUM] 0.0-10.2 sec 185 MBytes 153 Mbits/sec
復(fù)制代碼
限 30 M:
[ 4] 0.0-10.2 sec 53.0 MBytes 43.7 Mbits/sec
[ 6] 0.0-10.2 sec 62.0 MBytes 51.1 Mbits/sec
[ 8] 0.0-10.2 sec 57.9 MBytes 47.7 Mbits/sec
[ 5] 0.0-10.2 sec 58.5 MBytes 48.2 Mbits/sec
[ 7] 0.0-10.2 sec 46.4 MBytes 38.2 Mbits/sec
[SUM] 0.0-10.2 sec 278 MBytes 229 Mbits/sec
復(fù)制代碼
限 40 M:
[ 5] 0.0-10.1 sec 76.6 MBytes 63.5 Mbits/sec
[ 9] 0.0-10.1 sec 76.9 MBytes 63.6 Mbits/sec
[ 6] 0.0-10.1 sec 72.4 MBytes 59.9 Mbits/sec
[ 7] 0.0-10.1 sec 70.6 MBytes 58.5 Mbits/sec
[ 4] 0.0-10.1 sec 72.9 MBytes 60.3 Mbits/sec
[SUM] 0.0-10.1 sec 369 MBytes 305 Mbits/sec
復(fù)制代碼
限 50 M:
[ 4] 0.0-10.1 sec 89.9 MBytes 74.5 Mbits/sec
[ 5] 0.0-10.1 sec 99.6 MBytes 82.5 Mbits/sec
[ 8] 0.0-10.1 sec 89.9 MBytes 74.3 Mbits/sec
[ 6] 0.0-10.1 sec 91.9 MBytes 76.0 Mbits/sec
[ 7] 0.0-10.2 sec 89.8 MBytes 74.1 Mbits/sec
[SUM] 0.0-10.2 sec 461 MBytes 381 Mbits/sec
復(fù)制代碼
限 60 M:
[ 4] 0.0-10.1 sec 107 MBytes 89.1 Mbits/sec
[ 7] 0.0-10.1 sec 121 MBytes 101 Mbits/sec
[ 9] 0.0-10.1 sec 108 MBytes 89.3 Mbits/sec
[ 5] 0.0-10.1 sec 107 MBytes 89.1 Mbits/sec
[ 6] 0.0-10.1 sec 107 MBytes 89.2 Mbits/sec
[SUM] 0.0-10.1 sec 550 MBytes 457 Mbits/sec
復(fù)制代碼
限 70 M:
[ 8] 0.0-10.1 sec 178 MBytes 148 Mbits/sec
[ 7] 0.0-10.1 sec 94.4 MBytes 78.5 Mbits/sec
[ 4] 0.0-10.1 sec 95.0 MBytes 78.9 Mbits/sec
[ 6] 0.0-10.1 sec 94.6 MBytes 78.6 Mbits/sec
[ 5] 0.0-10.1 sec 178 MBytes 148 Mbits/sec
[SUM] 0.0-10.1 sec 640 MBytes 531 Mbits/sec
復(fù)制代碼
限 80M:
[ 7] 0.0-10.0 sec 167 MBytes 140 Mbits/sec
[ 9] 0.0-10.1 sec 166 MBytes 137 Mbits/sec
[ 4] 0.0-10.2 sec 99.8 MBytes 82.4 Mbits/sec
[ 5] 0.0-10.2 sec 157 MBytes 129 Mbits/sec
[ 6] 0.0-10.2 sec 110 MBytes 90.2 Mbits/sec
[SUM] 0.0-10.2 sec 700 MBytes 574 Mbits/sec
復(fù)制代碼
限 90 M
[ 4] 0.0-10.0 sec 220 MBytes 184 Mbits/sec
[ 7] 0.0-10.2 sec 124 MBytes 102 Mbits/sec
[ 5] 0.0-10.2 sec 104 MBytes 85.2 Mbits/sec
[ 8] 0.0-10.2 sec 117 MBytes 96.2 Mbits/sec
[ 6] 0.0-10.2 sec 135 MBytes 111 Mbits/sec
[SUM] 0.0-10.2 sec 699 MBytes 573 Mbits/sec
復(fù)制代碼
限 100 M
[ 4] 0.0-10.1 sec 140 MBytes 116 Mbits/sec
[ 7] 0.0-10.1 sec 139 MBytes 116 Mbits/sec
[ 6] 0.0-10.1 sec 145 MBytes 121 Mbits/sec
[ 5] 0.0-10.1 sec 128 MBytes 106 Mbits/sec
[ 9] 0.0-10.1 sec 146 MBytes 121 Mbits/sec
[SUM] 0.0-10.1 sec 698 MBytes 579 Mbits/sec
復(fù)制代碼
五恒水、小結(jié)
Linux 下有很多種手段可以模擬網(wǎng)絡(luò)丟包会放、延遲、限流等情況钉凌,大家可以自行探索咧最。
作者:zuozewei
鏈接:https://juejin.cn/post/7013657819010318349
來源:稀土掘金
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán)御雕,非商業(yè)轉(zhuǎn)載請注明出處矢沿。