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

查看:251
本文介绍了在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.

其主页上的示例已经显示了如何实现自己的目标ve要求:

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毫秒,而下一个随机元素取决于最后一个元素的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

实际的表(普通表,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.

Prob n = 0.25×Prob n-1 + 0.75×随机

Probn = 0.25 × Probn-1 + 0.75 × Random

注意,如果您没有针对该界面的规则,则应使用tc qdisc add;如果您已经具有针对该界面的规则,则应使用tc qdisc change.尝试在没有规则的界面上使用tc qdisc change会出现错误 RTNETLINK answers: No such file or directory.

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天全站免登陆