模擬延遲傳輸簡介
netem
netem 是 Linux 2.6 及以上內(nèi)核版本提供的一個網(wǎng)絡(luò)模擬功能模塊。該功能模塊可以用來在性能良好的局域網(wǎng)中,模擬出復(fù)雜的互聯(lián)網(wǎng)傳輸性能,諸如低帶寬、傳輸延遲、丟包等等情況唬复。使用 Linux 2.6 (或以上) 版本內(nèi)核的很多發(fā)行版 Linux 都開啟了該內(nèi)核功能,比如 Fedora龙巨、Ubuntu、Redhat揪阶、OpenSuse遣总、CentOS睬罗、Debian 等等。
tc
tc是Linux 系統(tǒng)中的一個工具,全名為 traffic control(流量控制)旭斥。tc 可以用來控制 netem 的工作模式,也就是說,如果想使用 netem ,需要至少兩個條件,一個是內(nèi)核中的 netem 功能被包含,另一個是要有 tc 容达。
查看網(wǎng)卡信息
常用網(wǎng)絡(luò)信息查看指令ifconfig
Linux機(jī)器為:
[root@localhost ~]# ifconfig
enp4s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.216.8.16 netmask 255.255.248.0 broadcast 10.216.15.255
inet6 fe80::2a19:67f:fd7b:f6f6 prefixlen 64 scopeid 0x20<link>
ether c8:d3:ff:ba:c8:6e txqueuelen 1000 (Ethernet)
RX packets 303591 bytes 24430143 (23.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2383 bytes 422970 (413.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Mac機(jī)器為:
jc@jc:~$ ifconfig
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether a4:5e:60:ed:37:3d
inet 10.242.23.215 netmask 0xffff0000 broadcast 10.242.255.255
media: autoselect
status: active
如上圖,輸入指令ifconfig
后輸出若干網(wǎng)絡(luò)信息琉预,如Linux的網(wǎng)卡信息為enp3s0
董饰, mac機(jī)器的網(wǎng)卡信息為en0
蒿褂。備注一般看下inet參數(shù)對應(yīng)的圆米,即為網(wǎng)卡型號。
模擬延遲傳輸
基本命令形式:
tc qdisc add dev DEV root netem delay 100ms
其中tc qdisc add
為固定命令格式
使用tc enp3s0網(wǎng)卡進(jìn)行延遲傳輸啄栓,配置命令如下:
[root@localhost ~]# tc qdisc show
qdisc noqueue 0: dev lo root refcnt 2
qdisc pfifo_fast 0: dev enp4s0 root refcnt 2 bands 3 priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
qdisc noqueue 0: dev virbr0 root refcnt 2
qdisc pfifo_fast 0: dev virbr0-nic root refcnt 2 bands 3 priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
qdisc noqueue 0: dev docker0 root refcnt 2
qdisc noqueue 0: dev br-4bc03776afc3 root refcnt 2
qdisc noqueue 0: dev br-69d033945dee root refcnt 2
qdisc noqueue 0: dev br-9440cbbd9b61 root refcnt 2
qdisc noqueue 0: dev br-a07abf9888d5 root refcnt 2
qdisc noqueue 0: dev veth1aa4b64 root refcnt 2
qdisc noqueue 0: dev veth0d4b251 root refcnt 2
qdisc noqueue 0: dev vethe73ce26 root refcnt 2
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# tc qdisc add dev enp4s0 root netem delay 100ms
[root@localhost ~]#
[root@localhost ~]# tc qdisc show
qdisc noqueue 0: dev lo root refcnt 2
qdisc netem 8003: dev enp4s0 root refcnt 2 limit 1000 delay 100.0ms
qdisc noqueue 0: dev virbr0 root refcnt 2
qdisc pfifo_fast 0: dev virbr0-nic root refcnt 2 bands 3 priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
qdisc noqueue 0: dev docker0 root refcnt 2
qdisc noqueue 0: dev br-4bc03776afc3 root refcnt 2
qdisc noqueue 0: dev br-69d033945dee root refcnt 2
qdisc noqueue 0: dev br-9440cbbd9b61 root refcnt 2
qdisc noqueue 0: dev br-a07abf9888d5 root refcnt 2
qdisc noqueue 0: dev veth1aa4b64 root refcnt 2
qdisc noqueue 0: dev veth0d4b251 root refcnt 2
qdisc noqueue 0: dev vethe73ce26 root refcnt 2
-
tc qdisc show
:查看現(xiàn)有網(wǎng)卡的所有配置 -
tc qdisc add dev enp4s0 root netem delay 100ms
:對網(wǎng)卡進(jìn)行設(shè)置娄帖,模擬100ms延遲 -
qdisc netem 8003: dev enp4s0 root refcnt 2 limit 1000 delay 100.0ms
:模擬延遲的配置結(jié)果
配置前后網(wǎng)絡(luò)延遲對比
Linux服務(wù)網(wǎng)絡(luò)情況如下:
#網(wǎng)絡(luò)延遲設(shè)置前
jc@jc:~$ ping 10.216.8.16
PING 10.216.8.16 (10.216.8.16): 56 data bytes
64 bytes from 10.216.8.16: icmp_seq=0 ttl=60 time=3.109 ms
64 bytes from 10.216.8.16: icmp_seq=1 ttl=60 time=4.086 ms
64 bytes from 10.216.8.16: icmp_seq=2 ttl=60 time=3.001 ms
64 bytes from 10.216.8.16: icmp_seq=3 ttl=60 time=4.364 ms
64 bytes from 10.216.8.16: icmp_seq=4 ttl=60 time=3.627 ms
64 bytes from 10.216.8.16: icmp_seq=5 ttl=60 time=3.662 ms
64 bytes from 10.216.8.16: icmp_seq=6 ttl=60 time=2.217 ms
^C
--- 10.216.8.16 ping statistics ---
7 packets transmitted, 7 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 2.217/3.438/4.364/0.671 ms
#網(wǎng)絡(luò)延遲設(shè)置后
jc@jc:~$ ping 10.216.8.16
PING 10.216.8.16 (10.216.8.16): 56 data bytes
64 bytes from 10.216.8.16: icmp_seq=0 ttl=60 time=103.129 ms
64 bytes from 10.216.8.16: icmp_seq=1 ttl=60 time=102.081 ms
64 bytes from 10.216.8.16: icmp_seq=2 ttl=60 time=102.759 ms
64 bytes from 10.216.8.16: icmp_seq=3 ttl=60 time=103.481 ms
64 bytes from 10.216.8.16: icmp_seq=4 ttl=60 time=103.044 ms
64 bytes from 10.216.8.16: icmp_seq=5 ttl=60 time=102.210 ms
64 bytes from 10.216.8.16: icmp_seq=6 ttl=60 time=102.389 ms
64 bytes from 10.216.8.16: icmp_seq=7 ttl=60 time=103.228 ms
^C
--- 10.216.8.16 ping statistics ---
8 packets transmitted, 8 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 102.081/102.790/103.481/0.481 ms
可通過ping命令發(fā)現(xiàn),網(wǎng)路有明顯的100ms延遲
刪除網(wǎng)絡(luò)配置
基本命令形式:
tc qdisc del dev DEV root netem delay 100ms
其中tc qdisc del
為固定命令格式
[root@localhost ~]# tc qdisc show
qdisc noqueue 0: dev lo root refcnt 2
qdisc netem 8003: dev enp4s0 root refcnt 2 limit 1000 delay 100.0ms
qdisc noqueue 0: dev virbr0 root refcnt 2
qdisc pfifo_fast 0: dev virbr0-nic root refcnt 2 bands 3 priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
qdisc noqueue 0: dev docker0 root refcnt 2
qdisc noqueue 0: dev br-4bc03776afc3 root refcnt 2
qdisc noqueue 0: dev br-69d033945dee root refcnt 2
qdisc noqueue 0: dev br-9440cbbd9b61 root refcnt 2
qdisc noqueue 0: dev br-a07abf9888d5 root refcnt 2
qdisc noqueue 0: dev veth1aa4b64 root refcnt 2
qdisc noqueue 0: dev veth0d4b251 root refcnt 2
qdisc noqueue 0: dev vethe73ce26 root refcnt 2
[root@localhost ~]# tc qdisc del dev enp4s0 root netem delay 100ms
[root@localhost ~]# tc qdisc show
qdisc noqueue 0: dev lo root refcnt 2
qdisc pfifo_fast 0: dev enp4s0 root refcnt 2 bands 3 priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
qdisc noqueue 0: dev virbr0 root refcnt 2
qdisc pfifo_fast 0: dev virbr0-nic root refcnt 2 bands 3 priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
qdisc noqueue 0: dev docker0 root refcnt 2
qdisc noqueue 0: dev br-4bc03776afc3 root refcnt 2
qdisc noqueue 0: dev br-69d033945dee root refcnt 2
qdisc noqueue 0: dev br-9440cbbd9b61 root refcnt 2
qdisc noqueue 0: dev br-a07abf9888d5 root refcnt 2
qdisc noqueue 0: dev veth1aa4b64 root refcnt 2
qdisc noqueue 0: dev veth0d4b251 root refcnt 2
qdisc noqueue 0: dev vethe73ce26 root refcnt 2
-
tc qdisc show
:查看現(xiàn)有網(wǎng)卡的所有配置 -
tc qdisc del dev enp4s0 root netem delay 100ms
: 刪除enp4s0網(wǎng)絡(luò)的100ms延遲配置
模擬丟包
基本命令形式:
tc qdisc add dev enp4s0 root netem loss 50%
丟包命令配置
#設(shè)置50%的丟包率
[root@localhost ~]# tc qdisc add dev enp4s0 root netem loss 50%
#查看50%的丟包率是否配置正確
[root@localhost ~]# tc qdisc show dev enp4s0
qdisc netem 8005: root refcnt 2 limit 1000 loss 50%
ping查看是否配置成功
#配置丟包前
jc@jc:~$ ping 10.216.8.16
PING 10.216.8.16 (10.216.8.16): 56 data bytes
64 bytes from 10.216.8.16: icmp_seq=0 ttl=60 time=1.680 ms
64 bytes from 10.216.8.16: icmp_seq=1 ttl=60 time=5.192 ms
64 bytes from 10.216.8.16: icmp_seq=2 ttl=60 time=3.715 ms
64 bytes from 10.216.8.16: icmp_seq=3 ttl=60 time=4.588 ms
64 bytes from 10.216.8.16: icmp_seq=4 ttl=60 time=2.856 ms
64 bytes from 10.216.8.16: icmp_seq=5 ttl=60 time=2.206 ms
64 bytes from 10.216.8.16: icmp_seq=6 ttl=60 time=4.099 ms
64 bytes from 10.216.8.16: icmp_seq=7 ttl=60 time=3.201 ms
^C
--- 10.216.8.16 ping statistics ---
8 packets transmitted, 8 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 1.680/3.442/5.192/1.113 ms
#配置丟包后
jc@jc:~$ ping 10.216.8.16
PING 10.216.8.16 (10.216.8.16): 56 data bytes
64 bytes from 10.216.8.16: icmp_seq=0 ttl=60 time=2.771 ms
64 bytes from 10.216.8.16: icmp_seq=1 ttl=60 time=5.864 ms
Request timeout for icmp_seq 2
64 bytes from 10.216.8.16: icmp_seq=3 ttl=60 time=2.954 ms
64 bytes from 10.216.8.16: icmp_seq=4 ttl=60 time=3.155 ms
Request timeout for icmp_seq 5
64 bytes from 10.216.8.16: icmp_seq=6 ttl=60 time=2.051 ms
64 bytes from 10.216.8.16: icmp_seq=7 ttl=60 time=3.847 ms
Request timeout for icmp_seq 8
64 bytes from 10.216.8.16: icmp_seq=9 ttl=60 time=2.982 ms
^C
--- 10.216.8.16 ping statistics ---
10 packets transmitted, 7 packets received, 30.0% packet loss
round-trip min/avg/max/stddev = 2.051/3.375/5.864/1.129 ms
發(fā)現(xiàn)配置丟包后昙楚,有30%的概率出現(xiàn)超時近速,即丟包,雖然與配置的50%有所差距,但的確達(dá)到了丟包超時的目的
模擬重復(fù)包
基本命令形式:
tc qdisc add dev enp4s0 root netem duplicate 50%
[root@localhost ~]# tc qdisc add dev enp4s0 root netem duplicate 20%
[root@localhost ~]# tc qdisc show dev enp4s0
qdisc netem 8006: root refcnt 2 limit 1000 duplicate 20%
-
tc qdisc add dev enp4s0 root netem duplicate 20%
:設(shè)置重復(fù)包率20%
設(shè)置20%重復(fù)包率削葱,ping對應(yīng)的服務(wù)如下:
jc@jc:~$ ping 10.216.8.16
PING 10.216.8.16 (10.216.8.16): 56 data bytes
64 bytes from 10.216.8.16: icmp_seq=0 ttl=60 time=2.860 ms
64 bytes from 10.216.8.16: icmp_seq=1 ttl=60 time=6.321 ms
64 bytes from 10.216.8.16: icmp_seq=1 ttl=60 time=6.341 ms (DUP!)
64 bytes from 10.216.8.16: icmp_seq=2 ttl=60 time=3.431 ms
64 bytes from 10.216.8.16: icmp_seq=2 ttl=60 time=3.446 ms (DUP!)
64 bytes from 10.216.8.16: icmp_seq=3 ttl=60 time=4.553 ms
64 bytes from 10.216.8.16: icmp_seq=3 ttl=60 time=4.572 ms (DUP!)
64 bytes from 10.216.8.16: icmp_seq=4 ttl=60 time=2.474 ms
^C
--- 10.216.8.16 ping statistics ---
5 packets transmitted, 5 packets received, +3 duplicates, 0.0% packet loss
round-trip min/avg/max/stddev = 2.474/4.250/6.341/1.381 ms
模擬包損壞
基本命令形式:
tc qdisc add dev enp4s0 root nemet corrupt 20%
如下代碼顯示奖亚,設(shè)置20%錯誤包率,設(shè)置成功確認(rèn)網(wǎng)關(guān)配置
[root@localhost ~]# tc qdisc add dev enp4s0 root netem corrupt 20%
[root@localhost ~]# tc qdisc show dev enp4s0
qdisc netem 8007: root refcnt 2 limit 1000 corrupt 20%
ping ip發(fā)現(xiàn)27%的請求超時析砸,說明模擬包損害有效
jc@jc:~$ ping 10.216.8.16
PING 10.216.8.16 (10.216.8.16): 56 data bytes
64 bytes from 10.216.8.16: icmp_seq=0 ttl=60 time=1.924 ms
Request timeout for icmp_seq 1
Request timeout for icmp_seq 2
64 bytes from 10.216.8.16: icmp_seq=3 ttl=60 time=2.925 ms
64 bytes from 10.216.8.16: icmp_seq=4 ttl=60 time=2.816 ms
64 bytes from 10.216.8.16: icmp_seq=5 ttl=60 time=3.075 ms
Request timeout for icmp_seq 6
64 bytes from 10.216.8.16: icmp_seq=7 ttl=60 time=2.878 ms
64 bytes from 10.216.8.16: icmp_seq=8 ttl=60 time=2.699 ms
64 bytes from 10.216.8.16: icmp_seq=9 ttl=60 time=2.969 ms
64 bytes from 10.216.8.16: icmp_seq=10 ttl=60 time=2.782 ms
^C
--- 10.216.8.16 ping statistics ---
11 packets transmitted, 8 packets received, 27.3% packet loss
round-trip min/avg/max/stddev = 1.924/2.759/3.075/0.334 ms
模擬包亂序
基本命令形式:
tc qdisc add dev enp4s0 root netem delay 100ms reorder 25% 50%
該命令將enp4s0網(wǎng)卡的傳輸設(shè)置為:有25%的數(shù)據(jù)包(50%相關(guān))會被立即發(fā)送昔字,其他的延遲10ms
命令設(shè)置如下:
[root@localhost ~]# tc qdisc add dev enp4s0 root netem delay 100ms reorder 25% 50%
[root@localhost ~]# tc qdisc show dev enp4s0
qdisc netem 8008: root refcnt 2 limit 1000 delay 100.0ms reorder 25% 50% gap 1
ping ip結(jié)果如下,發(fā)現(xiàn)4/16=25%的請求5ms內(nèi)立刻返回首繁,其余75%請求耗時100ms
jc@jc:~$ ping 10.216.8.16
PING 10.216.8.16 (10.216.8.16): 56 data bytes
64 bytes from 10.216.8.16: icmp_seq=0 ttl=60 time=104.439 ms
64 bytes from 10.216.8.16: icmp_seq=1 ttl=60 time=103.857 ms
64 bytes from 10.216.8.16: icmp_seq=2 ttl=60 time=103.842 ms
64 bytes from 10.216.8.16: icmp_seq=3 ttl=60 time=102.541 ms
64 bytes from 10.216.8.16: icmp_seq=4 ttl=60 time=103.499 ms
64 bytes from 10.216.8.16: icmp_seq=5 ttl=60 time=103.394 ms
64 bytes from 10.216.8.16: icmp_seq=6 ttl=60 time=103.950 ms
64 bytes from 10.216.8.16: icmp_seq=7 ttl=60 time=104.413 ms
64 bytes from 10.216.8.16: icmp_seq=8 ttl=60 time=103.290 ms
64 bytes from 10.216.8.16: icmp_seq=9 ttl=60 time=189.894 ms
64 bytes from 10.216.8.16: icmp_seq=10 ttl=60 time=103.297 ms
64 bytes from 10.216.8.16: icmp_seq=11 ttl=60 time=3.160 ms
64 bytes from 10.216.8.16: icmp_seq=12 ttl=60 time=2.708 ms
64 bytes from 10.216.8.16: icmp_seq=13 ttl=60 time=4.405 ms
64 bytes from 10.216.8.16: icmp_seq=14 ttl=60 time=103.261 ms
64 bytes from 10.216.8.16: icmp_seq=15 ttl=60 time=104.344 ms
64 bytes from 10.216.8.16: icmp_seq=16 ttl=60 time=2.856 ms
^C
--- 10.216.8.16 ping statistics ---
17 packets transmitted, 17 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 2.708/85.126/189.894/49.649 ms
指定IP延遲
Linux流量控制主要分為建立隊(duì)列作郭,建立分類和建立過濾器三個方面
- 1).針對網(wǎng)絡(luò)物理設(shè)備(如eth0,enp4s0)綁定一個隊(duì)列qdis
- 2).在該隊(duì)列上建立分類class
- 3).為每一個分類建立一個基于路由的過濾器filter
- 4).最后與過濾器想配合弦疮,建立特定的路由表
建立一個含4class的root隊(duì)列
為網(wǎng)卡enp4s0建立一個隊(duì)列夹攒,隊(duì)列名稱為root,句柄handle為1(這條qdisc下設(shè)4個class胁塞,handle id為1:咏尝。在沒有filter的情況下,tc從IP協(xié)議層收到的包會根據(jù)IP包頭的TOS(Type of Service)字段進(jìn)入第1~第3個class(與pfifo_fast規(guī)則相同)闲先,第4個class是沒用的状土。下一條命令給他加個tc規(guī)則)
add dev enp4s0 root handle 1
:創(chuàng)建一個隊(duì)列,隊(duì)列名為root伺糠, 句柄名為1
bands 4
:創(chuàng)建4個class分類
tc qdisc add dev enp4s0 root handle 1: prio bands 4
設(shè)置分類的操作策略
給root qdisc添加一個filter蒙谓,將匹配到的包做200ms的延遲處理。
parent 1:4
:為分類的表示训桶,分類策略的句柄hanle40
netem delay 200ms
:超時200ms
tc qdisc add dev enp4s0 parent 1:4 handle 40: netem delay 200ms
綁定過濾器filter
給root qdisc添加一個filter累驮,將發(fā)給指定IP的包都送到第4個class:
protocol ip
:表示該過濾器應(yīng)該檢查報(bào)文分組的協(xié)議字段
prio 4
:表示它們對報(bào)文處理的優(yōu)先級
u32 match
:u32選擇器(命令中u32后面的部分)來匹配不同的數(shù)據(jù)流
ip dst 10.242.23.215
:匹配ip
flowid 1:4
:把符合匹配規(guī)則的數(shù)據(jù)流分配到類別1:4進(jìn)行處理
tc filter add dev enp4s0 protocol ip parent 1:0 prio 4 u32 match ip dst 10.242.23.215 flowid 1:4
實(shí)踐
操作步驟
#設(shè)置隊(duì)列及分類
[root@localhost ~]# tc qdisc add dev enp4s0 root handle 1: prio bands 4
[root@localhost ~]# tc qdisc show dev enp4s0
qdisc prio 1: root refcnt 2 bands 4 priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
#分類4設(shè)置延時操作
[root@localhost ~]# tc qdisc add dev enp4s0 parent 1:4 handle 40: netem delay 200ms
[root@localhost ~]# tc qdisc show
qdisc noqueue 0: dev lo root refcnt 2
qdisc prio 1: dev enp4s0 root refcnt 2 bands 4 priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
qdisc netem 40: dev enp4s0 parent 1:4 limit 1000 delay 200ms
#設(shè)置過濾器,當(dāng)指定ip請求切換為分類4
[root@localhost ~]# tc filter add dev enp4s0 protocol ip parent 1:0 prio 4 u32 match ip dst 10.242.23.215 flowid 1:4
結(jié)果
ping 10.242.23.215 :網(wǎng)絡(luò)耗時為200ms+
ping 10.216.8.202 :網(wǎng)絡(luò)耗時1ms
結(jié)論ip耗時模擬正常
[root@localhost ~]# ping 10.242.23.215
PING 10.242.23.215 (10.242.23.215) 56(84) bytes of data.
64 bytes from 10.242.23.215: icmp_seq=1 ttl=60 time=207 ms
64 bytes from 10.242.23.215: icmp_seq=2 ttl=60 time=294 ms
64 bytes from 10.242.23.215: icmp_seq=3 ttl=60 time=213 ms
64 bytes from 10.242.23.215: icmp_seq=4 ttl=60 time=235 ms
64 bytes from 10.242.23.215: icmp_seq=5 ttl=60 time=257 ms
64 bytes from 10.242.23.215: icmp_seq=6 ttl=60 time=279 ms
^C
--- 10.242.23.215 ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5008ms
rtt min/avg/max/mdev = 207.490/248.075/294.789/32.445 ms
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# ping 10.216.8.202
PING 10.216.8.202 (10.216.8.202) 56(84) bytes of data.
64 bytes from 10.216.8.202: icmp_seq=1 ttl=64 time=0.873 ms
64 bytes from 10.216.8.202: icmp_seq=2 ttl=64 time=0.434 ms
64 bytes from 10.216.8.202: icmp_seq=3 ttl=64 time=0.472 ms
64 bytes from 10.216.8.202: icmp_seq=4 ttl=64 time=0.472 ms
^C
--- 10.216.8.202 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3001ms
rtt min/avg/max/mdev = 0.434/0.562/0.873/0.182 ms