Applications can behave differently on a slow connection even when every request eventually succeeds. On Linux, tc can apply a fixed netem delay on a test interface so you can see how software responds.

Ping round-trip time with 100 ms of outbound delay

A root netem queue discipline holds packets before they leave the interface. A ping sent through that interface encounters the delay on the outgoing leg, so its round-trip time rises by about the amount of delay you set.

The queue discipline lives in the running kernel. Removing the interface or rebooting clears the delay.

Use a sudo-enabled account on a Linux system with iproute2 and the iputils version of ping. The peer must answer pings across the link from another host or network namespace.
Related: How to connect network namespaces with veth pairs

Run each command in the network namespace that owns the sending interface, with no sudo for the queue and ping checks if your shell is already there. If you followed the veth setup, use veth-app in app in place of lab0 and ping the address of veth-db in db (192.0.2.2 in the linked setup), prefixing each command from the host with sudo ip netns exec app in place of any leading sudo.

Steps to simulate network latency with tc netem:

A root delay affects all packets leaving the interface, including traffic for remote sessions. Use a link dedicated to testing.

  1. Inspect the root queue discipline on your test interface, shown as lab0 throughout.
    $ tc qdisc show dev lab0 root
    qdisc noqueue 0: root refcnt 2 

    Continue only if the root queue discipline is noqueue. A different root queue discipline may be handling traffic shaping you depend on.

  2. Measure the baseline round-trip time through lab0 to your test peer, shown as 192.0.2.2 throughout.
    $ ping -n -q -c 3 -I lab0 192.0.2.2
    ##### snipped #####
    3 packets transmitted, 3 received, 0% packet loss, time 2032ms
    rtt min/avg/max/mdev = 0.045/0.210/0.490/0.198 ms

    The second number in min/avg/max/mdev gives the average, here 0.210 ms. If any probe gets no reply, fix connectivity and rerun the baseline before adding delay.

  3. Add a fixed outbound delay on lab0, using 100ms as the sample delay.
    $ sudo tc qdisc add dev lab0 root handle 1: netem delay 100ms

    The handle 1: identifies the new queue discipline. To remove the delay after testing, use sudo tc qdisc del dev lab0 root handle 1: netem while that handle still identifies the queue discipline you added.

  4. Confirm that the average round-trip time through lab0 to 192.0.2.2 rises by about the configured delay.
    $ ping -n -q -c 3 -I lab0 192.0.2.2
    ##### snipped #####
    3 packets transmitted, 3 received, 0% packet loss, time 2008ms
    rtt min/avg/max/mdev = 102.999/103.772/104.517/0.620 ms

    All three probes should get replies. With 100ms of delay, the average should rise by about 100 ms. Here it rose from 0.210 ms to 103.772 ms.

Share this guide