How to simulate latency on Linux

Some people asked me (… well, only one person) how was it possible to add latency between hosts in one of my previous articles about the effects of latency on throughput. When I decided to write the article, my searches led me to tc: Adding Simulated Network Latency To Your Linux Server. Of the different options available, this looked like the best one since there was no need to use a middlebox between the hosts.

How does it work?NetEm (Network Emulation) is Linux kernel functionality that allows the emulation or WAN link properties controlling delay, jitter, packet loss, packet duplication, reordering and more. This is the perfect companion to test and analyze network protocols (just try and add some loss to study TCP behavior.)

In the command line, it is controlled by the command tc qdisc (Traffic Control, Queueing Disciplines).

Let us look at an example. Adding 160ms latency to interface enp0s3.

-------------------------------------------------
 # tc qdisc add dev enp0s3 root netem delay 160ms
-------------------------------------------------

This command adds 160ms delay to the interface. This means that if the delay between hosts is already at 25ms, after the command it will be 185ms.

You can check tc/netem configuration using the command:

-------------------------------------------------
 # tc -s qdisc
...
qdisc netem 8001: dev enp0s3 root refcnt 2 limit 1000 delay 160.0ms
 Sent 90 bytes 1 pkt (dropped 0, overlimits 0 requeues 0)
 backlog 0b 0p requeues 0
...
-------------------------------------------------

Now, lets consider simulating a link with 5% packet loss.

-------------------------------------------------
 # tc qdisc change dev enp0s3 root netem loss 10%

 # tc -s qdisc
...
qdisc netem 8001: dev enp0s3 root refcnt 2 limit 1000 loss 10%
 Sent 1164 bytes 14 pkt (dropped 0, overlimits 0 requeues 0)
 backlog 0b 0p requeues 0
...
-------------------------------------------------

In the command above, I hope you noticed the change option and the fact that the previously configured delay was gone. If youdid any change using netem, the only commands allowed after that are change and del. You can change more than one option in the same command with add or change.

-------------------------------------------------
 # tc qdisc change dev enp0s3 root netem loss 14% delay 500ms

 # tc -s qdisc
...
qdisc netem 8001: dev enp0s3 root refcnt 2 limit 1000 delay 500.0ms loss 14%
 Sent 8067 bytes 93 pkt (dropped 5, overlimits 0 requeues 0)
 backlog 0b 0p requeues 0
...
-------------------------------------------------

When you decide that you want the interface configurations back to their default state, use the del option.

-------------------------------------------------
 # tc qdisc del dev enp0s3 root netem
-------------------------------------------------

Simple!

Be aware that in all the commands above, all red text is variable and may change depending on the configuration.

The tc and netem commands allow for a very granular control of the traffic including rate-limiting. If you need help at any time just use man tc or man netem.

—-

tc: Adding Simulated Network Latency To Your Linux Server
http://bencane.com/2012/07/16/tc-adding-simulated-network-latency-to-your-linux-server/

Netem: Linux Foundation
https://wiki.linuxfoundation.org/networking/netem

Netem: Man Pages
https://manpages.debian.org/testing/iproute2/tc-netem.8.en.html

TC
http://man7.org/linux/man-pages/man8/tc.8.html

Leave a Reply

Your email address will not be published. Required fields are marked *