linux下使用tc流量控制命令模拟网络延迟和丢包

如何使用tc模拟网络延迟和丢包

修改网络延时:  sudo tc qdisc add dev eth0 root netem delay 1000ms

查看流量管理:tc qdisc show

删除策略:sudo tc qdisc del dev eth0 root netem delay 1000ms

验证效果:ping 192.168.102.124 -c 20

 

修改丢包率:sudo tc qdisc add dev eth0 root netem loss 10%

删除策略:sudo tc qdisc del dev eth0 root netem loss 10%

 

-------------------------------------------------------------------------------------------------------------------------------------

配置网络超时

 

[root@dev-xx-xx ~]# tc qdisc del dev eth0 root netem delay 100ms
RTNETLINK answers: Invalid argument
[root@dev-xx-xx ~]# tc qdisc show
qdisc mq 0: dev eth0 root 
qdisc pfifo_fast 0: dev eth0 parent :1 bands 3 priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
qdisc pfifo_fast 0: dev eth0 parent :2 bands 3 priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
qdisc pfifo_fast 0: dev eth0 parent :3 bands 3 priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
qdisc pfifo_fast 0: dev eth0 parent :4 bands 3 priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
[root@dev-xx-xx ~]# tc qdisc add dev eth0 root netem delay 100ms
[root@dev-xx-xx ~]# ping 192.168.102.124
PING 192.168.102.124 (192.168.102.124) 56(84) bytes of data.
64 bytes from 192.168.102.124: icmp_seq=1 ttl=64 time=0.074 ms
64 bytes from 192.168.102.124: icmp_seq=2 ttl=64 time=0.066 ms
64 bytes from 192.168.102.124: icmp_seq=3 ttl=64 time=0.080 ms
64 bytes from 192.168.102.124: icmp_seq=4 ttl=64 time=0.043 ms
64 bytes from 192.168.102.124: icmp_seq=5 ttl=64 time=0.084 ms
64 bytes from 192.168.102.124: icmp_seq=6 ttl=64 time=0.094 ms
^C
--- 192.168.102.124 ping statistics ---
12 packets transmitted, 12 received, 0% packet loss, time 11131ms
rtt min/avg/max/mdev = 0.043/0.081/0.107/0.018 ms
[root@dev-xx-xx ~]# tc qdisc del dev eth0 root netem delay 100ms
[root@dev-xx-xx ~]# tc qdisc del dev eth0 root netem delay 100ms
RTNETLINK answers: Invalid argument

  

 

配置网络丢包率

[root@dev-xx-xx ~]# tc qdisc del dev eth0 root netem loss 10%
RTNETLINK answers: Invalid argument
[root@dev-xx-xx ~]# tc qdisc add dev eth0 root netem loss 10%
[root@dev-xx-xx ~]# tc qdisc show
qdisc netem 8005: dev eth0 root refcnt 5 limit 1000 loss 10%
[root@dev-xx-xx ~]# ping 192.168.102.124 -n 20
PING 20 (0.0.0.20) 56(124) bytes of data.
^C
--- 20 ping statistics ---
21 packets transmitted, 0 received, 100% packet loss, time 20650ms

[root@dev-xx-xx ~]# ping 192.168.102.124 -c 20
PING 192.168.102.124 (192.168.102.124) 56(84) bytes of data.
64 bytes from 192.168.102.124: icmp_seq=1 ttl=64 time=0.101 ms
64 bytes from 192.168.102.124: icmp_seq=2 ttl=64 time=0.062 ms
64 bytes from 192.168.102.124: icmp_seq=3 ttl=64 time=0.098 ms
64 bytes from 192.168.102.124: icmp_seq=4 ttl=64 time=0.098 ms
64 bytes from 192.168.102.124: icmp_seq=5 ttl=64 time=0.062 ms
64 bytes from 192.168.102.124: icmp_seq=6 ttl=64 time=0.088 ms
64 bytes from 192.168.102.124: icmp_seq=7 ttl=64 time=0.045 ms
64 bytes from 192.168.102.124: icmp_seq=8 ttl=64 time=0.070 ms
64 bytes from 192.168.102.124: icmp_seq=9 ttl=64 time=0.062 ms
64 bytes from 192.168.102.124: icmp_seq=10 ttl=64 time=0.066 ms
64 bytes from 192.168.102.124: icmp_seq=11 ttl=64 time=0.088 ms
64 bytes from 192.168.102.124: icmp_seq=12 ttl=64 time=0.070 ms
64 bytes from 192.168.102.124: icmp_seq=13 ttl=64 time=0.089 ms
64 bytes from 192.168.102.124: icmp_seq=14 ttl=64 time=0.087 ms
64 bytes from 192.168.102.124: icmp_seq=15 ttl=64 time=0.054 ms
64 bytes from 192.168.102.124: icmp_seq=16 ttl=64 time=0.085 ms
64 bytes from 192.168.102.124: icmp_seq=17 ttl=64 time=0.064 ms
64 bytes from 192.168.102.124: icmp_seq=18 ttl=64 time=0.124 ms
64 bytes from 192.168.102.124: icmp_seq=19 ttl=64 time=0.063 ms
64 bytes from 192.168.102.124: icmp_seq=20 ttl=64 time=0.108 ms

--- 192.168.102.124 ping statistics ---
20 packets transmitted, 20 received, 0% packet loss, time 19000ms
rtt min/avg/max/mdev = 0.045/0.079/0.124/0.020 ms
[root@dev-xx-xx ~]# tc qdisc del dev eth0 root netem loss 10%
[root@dev-xx-xx ~]# ping 192.168.102.124 -c 20
PING 192.168.102.124 (192.168.102.124) 56(84) bytes of data.
64 bytes from 192.168.102.124: icmp_seq=1 ttl=64 time=0.041 ms
64 bytes from 192.168.102.124: icmp_seq=2 ttl=64 time=0.132 ms
64 bytes from 192.168.102.124: icmp_seq=3 ttl=64 time=0.344 ms
64 bytes from 192.168.102.124: icmp_seq=4 ttl=64 time=0.404 ms
64 bytes from 192.168.102.124: icmp_seq=5 ttl=64 time=0.086 ms
64 bytes from 192.168.102.124: icmp_seq=6 ttl=64 time=0.088 ms
64 bytes from 192.168.102.124: icmp_seq=7 ttl=64 time=0.063 ms
64 bytes from 192.168.102.124: icmp_seq=8 ttl=64 time=0.109 ms
64 bytes from 192.168.102.124: icmp_seq=9 ttl=64 time=0.064 ms
64 bytes from 192.168.102.124: icmp_seq=10 ttl=64 time=0.092 ms
64 bytes from 192.168.102.124: icmp_seq=11 ttl=64 time=0.044 ms
64 bytes from 192.168.102.124: icmp_seq=12 ttl=64 time=0.066 ms
64 bytes from 192.168.102.124: icmp_seq=13 ttl=64 time=0.094 ms
64 bytes from 192.168.102.124: icmp_seq=14 ttl=64 time=0.097 ms
64 bytes from 192.168.102.124: icmp_seq=15 ttl=64 time=0.108 ms
64 bytes from 192.168.102.124: icmp_seq=16 ttl=64 time=0.043 ms
64 bytes from 192.168.102.124: icmp_seq=17 ttl=64 time=0.093 ms
64 bytes from 192.168.102.124: icmp_seq=18 ttl=64 time=0.056 ms
64 bytes from 192.168.102.124: icmp_seq=19 ttl=64 time=0.093 ms
64 bytes from 192.168.102.124: icmp_seq=20 ttl=64 time=0.039 ms

--- 192.168.102.124 ping statistics ---
20 packets transmitted, 20 received, 0% packet loss, time 18999ms
rtt min/avg/max/mdev = 0.039/0.107/0.404/0.093 ms
[root@dev-xx-xx ~]# 

  

case
1)模拟延迟传输:
# tc qdisc add dev eth0 root netem delay 100ms
2)模拟延迟波动:
# tc qdisc add dev eth0 root netem delay 100ms 10ms
3)延迟波动随机性:
# tc qdisc add dev eth0 root netem delay 100ms 10ms 30%
4)模拟网络丢包:
# tc qdisc add dev eth0 root netem loss 1%
5)网络丢包成功率:
# tc qdisc add dev eth0 root netem loss 1% 30%
6)删除相关配置:
# tc qdisc del dev eth0 root netem delay 100ms
7)模拟包重复:
# tc qdisc add dev eth0 root netem duplicate 1%
8)模拟包损坏:
# tc qdisc add dev eth0 root netem corrupt 0.2%
9)查看网卡配置:
# tc qdisc show dev eth0
10)查看丢包率:
# tc -s qdisc show dev eth0

上一篇:棋盘覆盖问题


下一篇:npx eslint --init 报错