📜 ⬆️ ⬇️

Emulation of the impact of global networks

This blog is usually written articles where Linux seems to be user-friendly, the text below is most likely related to the section “Linux is not for everyone” and will be of interest to a much narrower circle of harabrachiteli.

Often there is a task to investigate the effect of delays, losses and jitter on the operation of a network application. This task is primarily faced by experts who are engaged in the development or implementation of VoIP solutions, online games, streaming media content. With the wide spread of wireless data networks, such as GPRS, CDMA, satellite communication systems, the study of the influence of data network parameters on the operation of applications becomes particularly important.

Consider the scheme with which you can simulate the effect of various parameters of the data network on the application under study. For modeling it is very convenient to use the GNU / Linux operating system, in which all the necessary tools exist. The linux kernel includes a netem module that provides functionality for WAN emulation. The current version of the module has the following functions:
')
delay emulation, with different distribution function
loss emulation
packet replay emulation
packet emulation
packet distortion emulation

This module is enabled by default in most modern distributions based on the Linux 2.6 kernel (Fedora, OpenSuse, Gentoo, Debian, Mandriva, Ubuntu) and is controlled by the tc command from the iproute2 package. If your distribution does not include this module, you can enable it yourself:

Networking -->
Networking Options -->
QoS and/or fair queuing -->
Network emulator


To study the effect of transport network parameters on an application, the easiest way is to use a dedicated computer, which works according to the scheme shown in Fig. 1. On a dedicated computer, the ports are combined into a bridge (ethernet bridge), which allows you to transparently transfer packets from one interface to another and act as a switch. Such a solution is optimal, so now we can filter the traffic and also change the required parameters of the transport stream.



In order to configure the bridge, the bridge-utils package must be installed on the system. To create the proposed configuration, you need to create a bridge:

#brctl add br0

Add to it the necessary interfaces:

#brctl addif br0 eth0
#brctl addif br0 eth1

Configure the address on the br0 interface to access the computer over the network:

#ifconfig br0 <>

After that, ethernet frames coming to one interface will be sent to another. It is worth noting that the forwarded packets pass through netfilter, thus filtering traffic can be controlled by iptables at the network level and ebtables at the data link layer of the OSI model.

Packet delay emulation

The simplest example is adding a root qdisc that emulates delay.

# tc qdisc add dev eth1 root netem delay 800ms

It is worth remembering that we can only control outgoing traffic, so this command in our scheme will set the delay of data that goes in the direction from the server client to 800ms. To make our scenario more realistic, you can add a standard deviation. Later we will change the root qdisc.

# tc qdisc change dev eth0 root netem delay 800ms 100ms

Now the delay will change with a deviation of 100ms.

In the netem module, it is possible to set an uneven distribution of the delay. For example, to set the normal distribution function, you need to do the following:

# tc qdisc change dev eth0 root netem delay 100ms 20ms distribution normal

Other distribution tables (normal, pareto, paretonormal) are installed along with iproute2 in the / usr / lib / tc directory. It will not be difficult to generate your distribution table, reflecting the delay of the communication channel, based on experimental data.

Emulation of packet flow changes

Random packet loss is set in percent.

# tc qdisc change dev eth0 root netem loss 0.1%

This will result in a loss of 1 out of 1000 packages. As an option, you can add a packet loss correlation. This will cause the random number generator to be “less random”. This can be used to emulate packet burst.

# tc qdisc change dev eth0 root netem loss 0.5% 25%

In this example, 0.5% of packets will be lost, and the probability of losing a packet grows by a quarter if the previous one was lost.

Duplication of packets is set in the same way as loss.

# tc qdisc change dev eth0 root netem duplicate 1%

In new kernels (2.6.16 and older), white noise may be added to the packets. This feature is defined as well as packet loss:

# tc qdisc change dev eth0 root netem corrupt 0.1%

Another situation that occurs in networks with delays is the mixing of packets, when a packet sent earlier arrives later than the packet sent after it. In the netem module there are two methods of mixing. The simplest is the gap method. This method mixes every Nth packet.

# tc qdisc change dev eth0 root netem gap 5 delay 10ms

In this example, every 5th packet will be sent immediately, while the rest will be delayed for 10ms. This will cause the packets to come in a different order from the way they were sent. This behavior is useful for simple debugging of the transport protocol.
The following reoder method is much closer to real life. With it, you can specify the percentage of packets that can be mixed.

# tc qdisc change dev eth0 root netem delay 10ms reorder 25% 50%

In this example, 25% of packets (with a correlation of 50%) will be sent immediately, while the rest will be delayed by 10 ms.

The third possible mixing option.

# tc qdisc change dev eth0 root netem delay 100ms 75ms

If one packet receives a delay of 100ms, and the next packet sent 1ms later receives a delay of 50ms (100ms - 50ms jitter), then the second packet will be sent earlier.

It is worth noting that to implement any of the methods you need to use a delay.

The netem module is a traffic management discipline, so it can be used in conjunction with other disciplines, such as TBF, CBQ, etc. You can also build chains of disciplines and apply various filters to them.


# tc qdisc add dev eth0 root handle 1: prio
# tc qdisc add dev eth0 parent 1:3 handle 30: netem \
delay 200ms 10ms distribution normal
# tc qdisc add dev eth0 parent 30:1 tbf rate 20kbit buffer 1600 limit 3000
# tc filter add dev eth0 protocol ip parent 1:0 prio 3 u32 \
match ip dst 65.172.181.4/32 flowid 1:3


In this example, we create the root priority discipline, attach the netem discipline to the third priority, and then add velocity shaping.

As we can see, the Linux OS tools allow us to simulate any anomalies that may occur on the network. Thus, any system administrator can perform the necessary testing when deploying distributed services and implementing new protocols.

Used source http://www.linux-foundation.org/en/Net:Netem

Crosspost from my blog: m0sia.ru/node/67

If you point out my mistakes, I will be grateful.

Source: https://habr.com/ru/post/24046/


All Articles