我想在Linux上模拟UDP和TCP的数据包延迟和丢失,以衡量应用程序的性能。有什么简单的方法吗?


当前回答

对于丢弃的包,我将简单地使用iptables和统计模块。

iptables -A INPUT -m statistic --mode random --probability 0.01 -j DROP

以上将以1%的概率丢弃一个传入数据包。小心,任何超过0.14的东西,你们中的大多数tcp连接很可能会完全失速。

使用-D撤消:

iptables -D INPUT -m statistic --mode random --probability 0.01 -j DROP 

看看man iptables,搜索“statistic”来获取更多信息。

其他回答

我自己还没有尝试过,但是这个页面有一个在Linux内置iptables IP过滤系统中运行的插件模块列表。其中一个模块称为“nth”,它允许您设置一个规则,该规则将丢弃可配置的数据包速率。至少这是个不错的开始。

netem利用已经内置在Linux和用户空间实用程序中的功能来模拟网络。这实际上就是马克的答案所指的,只不过名字不同罢了。

他们主页上的例子已经告诉了你如何实现你的要求:

Examples Emulating wide area network delays This is the simplest example, it just adds a fixed amount of delay to all packets going out of the local Ethernet. # tc qdisc add dev eth0 root netem delay 100ms Now 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 100 Hz which allows delays in increments of 10 ms. 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. # tc qdisc change dev eth0 root netem delay 100ms 10ms This causes the added delay to be 100 ± 10 ms. Network delay variation isn't purely random, so to emulate that there is a correlation value as well. # tc qdisc change dev eth0 root netem delay 100ms 10ms 25% This causes the added delay to be 100 ± 10 ms with the next random element depending 25% on the last one. This isn't true statistical correlation, but an approximation. Delay distribution Typically, the delay in a network is not uniform. It is more common to use a something like a normal distribution to describe the variation in delay. The netem discipline can take a table to specify a non-uniform distribution. # tc qdisc change dev eth0 root netem delay 100ms 20ms distribution normal 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 effort to make your own distribution based on experimental data. Packet loss Random packet loss is specified in the 'tc' command in percent. The smallest possible non-zero value is: 2−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) packets 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% 25% This will cause 0.3% of packets to be lost, and each successive probability depends by a quarter on the last one. Probn = 0.25 × Probn-1 + 0.75 × Random

注意,如果你对该接口没有规则,你应该使用tc qdisc add;如果你已经对该接口有规则,你应该使用tc qdisc change。尝试在没有规则的接口上使用tc qdisc change将给出错误RTNETLINK答案:no such file or directory。

Iptables(8)有一个统计匹配模块,可以用来匹配每n个数据包。要删除这个包,只需添加-j drop。

对于丢弃的包,我将简单地使用iptables和统计模块。

iptables -A INPUT -m statistic --mode random --probability 0.01 -j DROP

以上将以1%的概率丢弃一个传入数据包。小心,任何超过0.14的东西,你们中的大多数tcp连接很可能会完全失速。

使用-D撤消:

iptables -D INPUT -m statistic --mode random --probability 0.01 -j DROP 

看看man iptables,搜索“statistic”来获取更多信息。

破坏者是一种易于使用的网络故障注入工具。它可以模拟:

网络总分区 远程服务关闭(没有监听预期的端口) 延迟 包丢失 -TCP连接超时(当两个系统被有状态防火墙隔开时经常发生)