在 Linux 上模拟延迟和丢弃的数据包 [英] Simulate delayed and dropped packets on Linux

查看:27
本文介绍了在 Linux 上模拟延迟和丢弃的数据包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Linux 上模拟 UDPTCP 的数据包延迟和丢失,以衡量应用程序的性能.有没有简单的方法可以做到这一点?

I would like to simulate packet delay and loss for UDP and TCP on Linux to measure the performance of an application. Is there a simple way to do this?

推荐答案

netem 利用已内置于 Linux 和用户空间实用程序中的功能来模拟网络.这实际上是 Mark 的回答所指的内容,但名称不同.

netem leverages functionality already built into Linux and userspace utilities to simulate networks. This is actually what Mark's answer refers to, by a different name.

他们的主页上的示例已经展示了如何实现目标'我要求:

The examples on their homepage already show how you can achieve what you've asked for:

这是最简单的例子,它只是为所有离开本地以太网的数据包增加了固定量的延迟.

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

现在对本地网络上的主机进行简单的 ping 测试应该显示增加了 100 毫秒.延迟受内核时钟分辨率 (Hz) 的限制.在大多数 2.4 系统上,系统时钟以 100 Hz 运行,允许以 10 毫秒为增量延迟.在 2.6 上,该值是从 1000 到 100 Hz 的配置参数.

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.

后面的例子只是改变参数而不重新加载qdisc

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

这会导致增加的延迟为 100 ± 10 毫秒.网络延迟变化不是完全随机的,因此模拟也存在相关值.

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%

这会导致增加的延迟为 100 ± 10 ms,下一个随机元素依赖于最后一个元素的 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.

通常,网络中的延迟是不均匀的.更常见的是使用像正态分布这样的东西来描述延迟的变化.netem 学科可以用一个表格来指定一个非均匀分布.

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

实际表(normal、pareto、paretonormal)作为iproute2编译的一部分生成并放置在/usr/lib/tc中;因此可以根据实验数据做出一些努力来制作自己的分布.

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.

'tc' 命令中以百分比指定随机丢包率.最小可能的非零值是:

Random packet loss is specified in the 'tc' command in percent. The smallest possible non-zero value is:

2−32 = 0.0000000232%

2−32 = 0.0000000232%

# tc qdisc change dev eth0 root netem loss 0.1%

这会导致 1/10%(即 1000 个中的 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%

这将导致 0.3% 的数据包丢失,并且每个连续的概率取决于最后一个的四分之一.

This will cause 0.3% of packets to be lost, and each successive probability depends by a quarter on the last one.

概率n = 0.25 × 概率n-1 + 0.75 × 随机

Probn = 0.25 × Probn-1 + 0.75 × Random

注意,如果你没有该接口的规则,你应该使用 tc qdisc add 或者如果你已经有规则,则应该使用 tc qdisc change那个界面.尝试在没有规则的界面上使用 tc qdisc change 将给出错误 RTNETLINK 回答:没有这样的文件或目录.

Note that you should use tc qdisc add if you have no rules for that interface or tc qdisc change if you already have rules for that interface. Attempting to use tc qdisc change on an interface with no rules will give the error RTNETLINK answers: No such file or directory.

这篇关于在 Linux 上模拟延迟和丢弃的数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆