前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >linux 下使用 tc 模拟网络延迟和丢包-使用 linux 模拟广域网延迟 - Emulating wide area network delays with Linux

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

作者头像
xuyaowen
发布2020-12-30 11:20:59
4.4K0
发布2020-12-30 11:20:59
举报
文章被收录于专栏:XUYAOWEN的专栏

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)也会受到影响,反之,如果您在逻辑网卡上做控制,该控制可能是无效的。(注:虚拟机中的多个网卡可以在虚拟机中视为多个物理网卡)。

代码语言:javascript
复制
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控制网络延时和丢包率:

代码语言:javascript
复制
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

配置删除:
sudo tc qdisc del dev enp2s0 root
代码语言:javascript
复制
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

在调研 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 高级用法:

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

代码语言:javascript
复制
tc qdisc add dev eth0 root netem delay 100ms 20ms distribution normal
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-09-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档