1、 # tc qdisc change dev eth0 root netem delay 10ms reorder 10% 25%Linux网络流量控制工具Netem(可用于网络故障注入)第一篇:概念篇Netem是Linux 2.6及以上内核版本提供的一个网络模拟功能模块。该功能模块可以用来在性能良好的局域网中,模拟出复杂的互联网传输性能,诸如低带宽、传输延迟、丢包等等情况。使用(或以上) 版本内核的很多发行版Linux都开启了该内核功能,比如Fedora、Ubuntu、Redhat、OpenSuse、CentOS、Debian等等。 tc系统中的一个工具,全名为traffic control
2、(流量控制)。tc可以用来控制netem的工作模式,也就是说,如果想使用,需要至少两个条件,一个是内核中的功能被包含,另一个是要有。 特别注意:本文介绍的内容主要倾向于利用tc工具的发包控制,来实现网络故障部分的注入实现,对于网络流控的介绍相对较少,详细的内容大家可以参考如下网址进行深入了解:http:/www.linuxfoundation.org/collaborate/workgroups/networking /netem#Delay_distribution第二篇:原理篇TC用于Linux内核的流量控制,主要是通过在输出端口处建立一个队列来实现流量控制。 接收包从输入接口(Input
3、 Interface)进来后,经过流量限制(Ingress Policing)丢弃不符合规定的数据包,由输入多路分配器(Input De-Multiplexing)进行判断选择:如果接收包的目的是本主机,那么将该包送给上层处理;否则需要进行转发,将接收包交到转发块(Forwarding Block)处理。转发块同时也接收本主机上层(TCP、UDP等)产生的包。转发块通过查看路由表,决定所处理包的下一跳。然后,对包进行排列以便将它们传送到输出接口(Output Interface)。一般我们只能限制网卡发送的数据包,不能限制网卡接收的数据包,所以我们可以通过改变发送次序来控制传输速率。Linux
4、流量控制主要是在输出接口排列时进行处理和实现的。 第三篇:应用篇工具可完成如下功能:(故障模拟)模拟时延,丢包,重复包,乱序。1、模拟延迟传输# tc qdisc add dev eth0 root netem delay 100ms该命令将eth0网卡的传输设置为延迟100毫秒发送。更真实的情况下,延迟值不会这么精确,会有一定的波动,我们可以用下面的情况来模拟出带有波动性的延迟值:100ms 10ms网卡的传输设置为延迟100ms 10ms(90 110 ms之间的任意值)发送。还可以更进一步加强这种波动的随机性: 30%网卡的传输设置为,同时,大约有30%的包会延迟发送。2、模拟网络丢包
5、netemloss1%网卡的传输设置为随机丢掉1%的数据包。也可以设置丢包的成功率:的数据包,成功率为3、模拟包重复duplicate网卡的传输设置为随机产生的重复数据包 。4、模拟包损坏corrupt0.2% 0.2%的损坏的数据包 。 (内核版本需在2.6.16以上)5、模拟包乱序# tc qdisc change dev eth0 root delay reorder 25% 50%网卡的传输设置为:有25%的数据包(50%相关)会被立即发送,其他的延迟10秒。 新版本中,如下命令也会在一定程度上打乱发包的次序:其余更多的内容,请参考如下内容:1、2、netem官方说明: http:/w
6、ww.linuxfoundation.org/collaborate/workgroups/networking/netemLinux 中模拟延时和丢包的实现 分类: 网络与安全Linux 中使用 tc 进行流量管理。具体命令的使用请参考 tc 的 man 手册。本文简单记录一下使用 tc 模拟延时和丢包的命令。1. 延时(1)设置延时1.# tc qdisc add dev eth0 root netem delay 30ms上面的命令实现了给 eth0 口发包时添加 30ms 的延时。(2)显示延时的设置1.# tc qdisc show结果如下:1.rootKernel-Test tes
7、t# tc qdisc show2.qdisc pfifo_fast 0: dev eth1 root bands 3 priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 13.qdisc netem 8003: dev eth0 root limit 1000 delay 30.0ms(3)修改延时1.# tc qdisc change dev eth0 root netem delay 40ms(4)删除延时的配置1.# tc qdisc del dev eth0 root netem delay 40ms2. 丢包设置丢包命令如下:1.# tc qdisc add
8、 dev eth0 root netem loss 10%如果你是通过 SSH 登陆设备进行配置丢包的话,配置完这条命令就会感觉到机器访问时有点一卡一卡的。这说明,设备已经开始丢包了,可以通过具体抓包查看。其他诸如修改、查看、删除的命令,都可以参考上面延时的设置。最后一点需要注意的是,这两个命令应该都是控制的网卡出口的流量。如果想控制网卡入口的流量,应该是需要确认内核是否明确支持了,如果不支持的话,就需要修改内核配置文件,并重新编译内核。Linux下的网络仿真器 最近在工作中需要使用网络仿真器来设置网络中的速率,丢包,延迟. 在FreeBSD OS中可以使用DummyNet+Ipfw来进行设置
9、.但是在Linux下有什么软件来进行模拟呢?可供选择的有两种: 1.NistNet: 非常强大的工具,缺点是文档比较少. 2.NetEm: 简单实用.能够在网络环中“gives you todelay, reordering, loss, etc.”/www.linux-foundation.org/en/Net:Netem-netem provides Network Emulation functionality for testing protocols by emulating the properties of wide area networks. The current vers
10、ion emulates variable delay, loss, duplication and re-ordering. If you run a current 2.6 distribution, (Fedora, OpenSuse, Gentoo, Debian, Mandriva, Ubuntu), then netem is already enabled in the kernel and a current version of Net:iproute2 is included. The netem kernel component is enabled under: Net
11、working - Networking Options - QoS and/or fair queuing - Network emulatorNetem is controlled by the command line tool tc which is part of the iproute2 package of tools. The tc command uses shared libraries and data files in the /usr/lib/tc directory. Contents1 Examples 1.1 Emulating wide area networ
12、k delays 1.2 Delay distribution 1.3 Packet loss 1.3.1 Caveats 1.4 Packet duplication 1.5 Packet corruption 1.6 Packet re-ordering 1.6.1 Caveats 1.7 Rate control 1.8 Non FIFO queuing 1.9 Delaying only some traffic 2 FAQ 2.1 How come first ping takes longer?2.2 How come TCP is so slow over netem?2.3 H
13、ow can I use netem on incoming traffic?2.4 How to reorder packets based on jitter?2.5 How does the value of HZ impact Netem?3 Links 4 Contact Info ExamplesEmulating wide area network delaysThis is the simplest example, it just adds a fixed amount of delay to all packets going out of the local Ethern
14、et. # tc qdisc add dev eth0 root netem delay 100msNow a simple ping test to host on the local network should show an increase of 100 milliseconds. The delay is limited by the clock resolution of the kernel (HZ). On most 2.4 systems, the system clock runs at 100hz which allows delays in increments of
15、 10ms. On 2.6, the value is a configuration parameter from 1000 to 100 hz. Later examples just change parameters without reloading the qdisc Real wide area networks show variability so it is possible to add random variation. This causes the added delay to be 100ms 10ms. Network delay variation isnt
16、purely random, so to emulate that there is a correlation value as well. 10ms with the next random element depending 25% on the last one. This isnt true statistical correlation, but an approximation. Delay distributionTypically, the delay in a network is not uniform. It is more common to use a someth
17、ing like a normal distribution to describe the variation in delay. The netem discipline can take a table to specify a non-uniform distribution. The actual tables (normal, pareto, paretonormal) are generated as part of the iproute2 compilation and placed in /usr/lib/tc; so it is possible with some ef
18、fort to make your own distribution based on experimental data. Packet lossRandom packet loss is specified in the command in percent. The smallest possible non-zero value is:frac12 32 = 0.0000000232% # tc qdisc change dev eth0 root netem loss 0.1%This causes 1/10th of a percent (i.e 1 out of 1000) pa
19、ckets to be randomly dropped. An optional correlation may also be added. This causes the random number generator to be less random and can be used to emulate packet burst losses. # tc qdisc change dev eth0 root netem loss 0.3% 33.33%This will cause 0.3% of packets to be lost, and each successive pro
20、bability depends by about a third on the last one. Prob_n = Prob_n-1cdot;frac33.33100 + Rand()cdot;left(1-frac33.33100right)CaveatsWhen loss is used locally (not on a bridge or router), the loss is reported to the upper level protocols. This may cause TCP to resend and behave as if there was no loss
21、. When testing protocol reponse to loss it is best to use a netem on a bridge or bridge Packet duplicationPacket duplication is specified the same way as packet loss. # tc qdisc change dev eth0 root netem duplicate 1%Packet corruptionRandom noise can be emulated (in 2.6.16 or later) with the corrupt
22、 option. This introduces a single bit error at a random offset in the packet. # tc qdisc change dev eth0 root netem corrupt 0.1% Packet re-orderingThere are two different ways to specify reordering. The first method gap uses a fixed sequence and reorders every Nth packet. A simple usage of this is:T
23、his causes every 5th (10th, 15th, .) packet to go to be sent immediately and every other packet to be delayed by 10ms. This is predictable and useful for base protocol testing like reassembly. The second form reorder of re-ordering is more like real life. It causes a certain percentage of the packet
24、s to get mis-ordered. # tc qdisc change dev eth0 root netem delay 10ms reorder 25% 50%In this example, 25% of packets (with a correlation of 50%) will get sent immediately, others will be delayed by 10ms. Newer versions of netem will also re-order packets if the random delay values are out of order. The following will cause some reordering: # tc qdisc change dev eth0 root netem delay 100ms 75msIf the first packet gets a random delay of 100ms (100ms base - 0ms jitter) and the second packet is sent 1ms later and gets a delay of 50ms (100ms base - 50ms jitter); the second pack
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1