markdown 使用TC命令模拟网络延迟和丢包
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 使用TC命令模拟网络延迟和丢包相关的知识,希望对你有一定的参考价值。
# 操作环境
```
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.4 LTS"
```
# 模拟网络延迟
## 命令参数
```
tc qdisc [ add | change | replace | link | delete ] dev DEV [ parent qdisc-id | root ] [ handle qdisc-id ] qdisc [ qdisc specific parameters ]
```
## 网络延迟增加100ms
```
sudo tc qdisc add dev enp2s0 root netem delay 100ms
```
`ps. enp2s0为本地接口名`
- 命令执行前,测试ping www.huuinn.com
```
$ ping www.huuinn.com
PING www.huuinn.com (112.124.119.168) 56(84) bytes of data.
64 bytes from 112.124.119.168: icmp_seq=1 ttl=53 time=11.0 ms
64 bytes from 112.124.119.168: icmp_seq=2 ttl=53 time=11.3 ms
64 bytes from 112.124.119.168: icmp_seq=3 ttl=53 time=12.3 ms
64 bytes from 112.124.119.168: icmp_seq=4 ttl=53 time=11.3 ms
^C
--- www.huuinn.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 11.026/11.518/12.361/0.514 ms
```
- 命令执行后,测试ping www.huuinn.com
```
$ ping www.huuinn.com
PING www.huuinn.com (112.124.119.168) 56(84) bytes of data.
64 bytes from 112.124.119.168: icmp_seq=1 ttl=53 time=111 ms
64 bytes from 112.124.119.168: icmp_seq=2 ttl=53 time=111 ms
64 bytes from 112.124.119.168: icmp_seq=3 ttl=53 time=112 ms
64 bytes from 112.124.119.168: icmp_seq=4 ttl=53 time=111 ms
^C
--- www.huuinn.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 111.515/112.083/112.892/0.647 ms
```
## 网络延迟增加200ms
```
sudo tc qdisc change dev enp2s0 root netem delay 200ms
```
- ping测试结果
```
$ ping www.huuinn.com
PING www.huuinn.com (112.124.119.168) 56(84) bytes of data.
64 bytes from 112.124.119.168: icmp_seq=1 ttl=53 time=212 ms
64 bytes from 112.124.119.168: icmp_seq=2 ttl=53 time=212 ms
64 bytes from 112.124.119.168: icmp_seq=3 ttl=53 time=213 ms
64 bytes from 112.124.119.168: icmp_seq=4 ttl=53 time=211 ms
^C
--- www.huuinn.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3002ms
rtt min/avg/max/mdev = 211.757/212.512/213.966/0.916 ms
```
## 配置确认
```
$ sudo tc qdisc show dev enp2s0
qdisc netem 8001: root refcnt 2 limit 1000 delay 200.0ms
```
## 配置删除
```
sudo tc qdisc del dev enp2s0 root
```
# 模拟网络包
```
sudo tc qdisc add dev enp2s0 root netem loss 50%
```
- 测试结果
```
$ ping www.huuinn.com -c 30
PING www.huuinn.com (112.124.119.168) 56(84) bytes of data.
64 bytes from 112.124.119.168: icmp_seq=1 ttl=53 time=11.6 ms
64 bytes from 112.124.119.168: icmp_seq=3 ttl=53 time=11.9 ms
64 bytes from 112.124.119.168: icmp_seq=4 ttl=53 time=11.2 ms
64 bytes from 112.124.119.168: icmp_seq=5 ttl=53 time=11.0 ms
64 bytes from 112.124.119.168: icmp_seq=6 ttl=53 time=13.2 ms
64 bytes from 112.124.119.168: icmp_seq=8 ttl=53 time=11.4 ms
64 bytes from 112.124.119.168: icmp_seq=12 ttl=53 time=11.3 ms
64 bytes from 112.124.119.168: icmp_seq=13 ttl=53 time=11.9 ms
64 bytes from 112.124.119.168: icmp_seq=15 ttl=53 time=12.0 ms
64 bytes from 112.124.119.168: icmp_seq=20 ttl=53 time=11.8 ms
64 bytes from 112.124.119.168: icmp_seq=21 ttl=53 time=12.4 ms
64 bytes from 112.124.119.168: icmp_seq=24 ttl=53 time=10.9 ms
64 bytes from 112.124.119.168: icmp_seq=26 ttl=53 time=11.4 ms
64 bytes from 112.124.119.168: icmp_seq=29 ttl=53 time=16.0 ms
--- www.huuinn.com ping statistics ---
30 packets transmitted, 14 received, 53% packet loss, time 29278ms
rtt min/avg/max/mdev = 10.947/12.043/16.020/1.242 ms
```
以上是关于markdown 使用TC命令模拟网络延迟和丢包的主要内容,如果未能解决你的问题,请参考以下文章
linux 下使用 tc 模拟网络延迟和丢包-使用 linux 模拟广域网延迟 - Emulating wide area network delays with Linux