linux 下使用 tc 模拟网络延迟和丢包-使用 linux 模拟广域网延迟 - Emulating wide area network delays with Linux

tc 是linux 内置的命令;使用man pages 查看 我们看到,其功能为 show / manipulate traffic control settings,可对操作系统进行流量控制;

netem 与 tc:

netem 是 Linux 2.6 及以上内核版本提供的一个网络模拟功能模块。该功能模块可以用来在性能良好的局域网中,模拟出复杂的互联网传输性能,诸如低带宽、传输延迟、丢包等等情况。使用 Linux 2.6 (或以上) 版本内核的很多发行版 Linux 都开启了该内核功能,比如 Fedora、Ubuntu、Redhat、OpenSuse、CentOS、Debian 等等。 tc 是Linux 系统中的一个工具,全名为 traffic control(流量控制)。tc 可以用来控制 netem 的工作模式,也就是说,如果想使用 netem ,需要至少两个条件,一个是内核中的 netem 功能被包含,另一个是要有 tc 。

需要注意的是:本文介绍的流控只能控制发包动作,不能控制收包动作,同时,它直接对物理接口生效,如果控制了物理的 eth0,那么逻辑网卡(比如 eth0:1)也会受到影响,反之,如果您在逻辑网卡上做控制,该控制可能是无效的。(注:虚拟机中的多个网卡可以在虚拟机中视为多个物理网卡)。

DESCRIPTION
       NetEm  is  an  enhancement  of the Linux traffic control facilities that allow to add delay, packet loss,
       duplication and more other characteristics to packets outgoing from a selected network  interface.  NetEm
       is built using the existing Quality Of Service (QOS) and Differentiated Services (diffserv) facilities in
       the Linux kernel.

Linux下用tc控制网络延时和丢包率:

tc修改网络延时:

sudo tc qdisc add dev eth0 root netem delay 1000ms

删除策略:

sudo tc qdisc del dev eth0 root netem delay 1000ms

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

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

配置确认
sudo tc qdisc show dev enp2s0
NetEm (already enabled in the Linux kernel) provides Network Emulation functionality for testing protocols by emulating the properties of wide area networks. 

To simulate an additional latency of 80 ms, just type sudo tc qdisc add dev eth0 root netem delay 80ms 
It just adds a fixed amount of delay to all packets going out of the local Ethernet. 

To stop the additional latency, just type sudo tc qdisc change dev eth0 root netem delay 0ms 

Lines to add to the file /etc/rc.local before exit 0, to add 40ms of latency :

# Add +40ms latency tc qdisc add dev eth0 root netem delay 40ms

Note: If your network interface is not eth0, replace eth0 with the name of your network interface
NetEm相关

在调研 iperf 的时候,偶然看到。对项目很有帮助。

原文链接:https://iperf.fr/iperf-servers.php#netem 

扩展阅读:https://www.cnblogs.com/Dev0ps/p/8985778.html 

https://blog.csdn.net/huuinn/article/details/80970079 

https://wiki.linuxfoundation.org/networking/netem 

https://wiki.linuxfoundation.org/networking/netem#emulating_wide_area_network_delays (更多万维网模拟相关) 

tc 高级用法:

延迟有波动并成正态分布趋势:

tc qdisc change dev eth0 root netem delay 100ms 20ms distribution normal

猜你喜欢

转载自www.cnblogs.com/xuyaowen/p/netem.html