Simulating WAN Latency with Linux tc: A Two-NIC Bridge That Can Delay Traffic by Up to 60 Seconds
How to reproduce WAN latency with Linux tc and netem. Put a PC with two NICs between a client and a server as a bridge, then add delay, jitter, packet loss and bandwidth limits. Covers one-way vs round-trip delay, the limit and ARP pitfalls of 60-second delays, and cleanup.
Contents
Sometimes you need to test a system that will be used from remote sites, but all you have is a test environment on the office LAN. On the LAN, responses come back instantly, so you cannot tell how the screens freeze on a slow line or whether the timeouts actually work.
I first tried a Windows tool for simulating line latency. It turned out to be rather inflexible: the delay could be set to at most 1000 ms, and the actual delay did not match the configured value. So I looked into adding delay with the Linux tc command instead. It accepts extreme delays such as 60 seconds and behaved quite accurately, which made it very effective.
Based on the setup I used then, this article explains how to put a Linux machine with two NICs between a client and a server and add delay. Beyond plain delay, it also covers jitter, packet loss, bandwidth limits and the points that tend to trip you up when inserting long delays.
Situations where this helps
What you want to check on a delayed line is usually something like the following.
- Business systems used from overseas sites or over VPN: on a line with a round trip of several hundred milliseconds, how long do screen transitions and list views take? The more round trips an application makes per operation, the more latency hurts.
- Timeout and retry settings: does a setting such as “show an error if there is no response for 30 seconds” really work? With a 60-second delay you can check what is shown after the timeout, and even what happens when an impatient user submits again.
- File transfer and sync: throttle bandwidth, add delay, and check how long large files take and whether an interrupted transfer resumes. If you only need to estimate transfer time from bandwidth and size, the file transfer time calculator is enough, but how things actually behave when delay and loss combine can only be seen by reproducing the line.
- Traffic between devices you cannot install software on: monitoring appliances, business terminals, embedded devices and other equipment where you cannot install delay software can still be tested simply by inserting the machine in between.
That last point is also why I chose a two-NIC setup. You touch neither the client nor the server; think of it as plugging a “slow line” into the middle of the cable.
Setup: use a Linux machine with two NICs as a bridge
[Client]──LAN cable──(eth0)[Linux machine br0](eth1)──LAN cable──[Server]
↑ delay is added here ↑
The two NICs of the Linux machine are joined by a bridge (a software switch). From the point of view of the client and the server, there is just one more switch in between, so there is no need to change IP addresses or gateway settings.
You will need the following.
- A Linux machine with two wired NICs. Adding a USB wired LAN adapter to the onboard NIC is fine. Wireless NICs cannot be added to a bridge in normal client mode, so they cannot be used.
- Cables that let you put this machine in series on the segment you want to delay.
- A way to operate the machine during setup and after delay is applied. If you SSH in through the bridge, your own session is delayed too (see below). A local keyboard and monitor, or a third NIC for management, is the safe choice.
The examples below assume Ubuntu or Debian and NIC names eth0 and eth1. Recent distributions usually use names like enp1s0, so check the actual names with ip link and substitute them. Run everything as root.
Creating the bridge
With iproute2 (the ip command)
This procedure uses the ip command that ships by default with current Linux. tc is part of the same iproute2 package, so no extra packages are needed.
# Create bridge br0
ip link add br0 type bridge
# Remove the IP addresses from both NICs and attach them to br0
ip addr flush dev eth0
ip addr flush dev eth1
ip link set eth0 master br0
ip link set eth1 master br0
# Give br0 a management IP address and bring everything up
ip addr add 192.168.1.100/24 dev br0
ip link set eth0 up
ip link set eth1 up
ip link set br0 up
# Check
bridge link
If the output of bridge link lists eth0 and eth1 with master br0, the bridge is ready. At this point, ping the server from the client and note the response time before any delay is added; it makes later comparison easier.
It is also worth checking that both NICs have linked at the expected speed and full duplex. If one side has dropped to 100 Mb/s or half duplex, transfers are slow before you add any delay, and you cannot separate that from the effect of netem. I described how to check this in the article on checking a NIC’s physical state with ethtool.
The IP address on br0 is only for operating the Linux machine itself. It plays no part in the traffic between client and server, so any free address on the test network will do.
With bridge-utils (brctl)
Here is also the method using brctl and ifconfig, which you often see in older guides. addif adds a NIC to a bridge; addbr creates the bridge itself.
apt-get install bridge-utils # Installs brctl (ifconfig comes from net-tools)
brctl addbr br0 # Create the bridge
brctl addif br0 eth0 # Add eth0 to the bridge
brctl addif br0 eth1 # Add eth1 to the bridge
ifconfig eth0 0.0.0.0 up
ifconfig eth1 0.0.0.0 up
ifconfig br0 192.168.1.100 netmask 255.255.255.0 broadcast 192.168.1.255 up
brctl show # Check
Either way the result is the same, so the tc steps that follow are identical.
When NetworkManager is running
On desktop Ubuntu and similar systems, NetworkManager manages the NICs and may rewrite settings you made by hand. If eth0 keeps getting an IP address back even though you built the bridge, mark the NIC as unmanaged with something like nmcli device set eth0 managed no, or create the bridge with nmcli. On server editions using netplan or systemd-networkd, also make sure their configuration does not conflict with your manual settings.
Adding, changing and removing delay
Adding delay
tc qdisc add dev eth1 root netem delay 1000ms
tc qdisc configures the queuing discipline, the mechanism that decides how packets leaving a NIC are handled, and netem is a discipline for adding delay and loss to it. The example above holds every packet sent out of eth1 for 1000 ms before sending it.
Besides ms you can also use s, so 60 seconds can be written as delay 60s. Whereas the Windows tool mentioned at the start topped out at 1000 ms, being able to specify long delays like this directly is a big advantage.
Checking the state
tc qdisc show dev eth1
tc -s qdisc show dev eth1 # Also show statistics such as sent and dropped counts
Changing the delay value
There is no need to rebuild the bridge. change swaps just the value.
tc qdisc change dev eth1 root netem delay 300ms
change is for when netem is already configured. If you want one command that works whether or not it is configured, use replace: it adds the discipline if it is missing and changes it if it exists.
Removing delay
tc qdisc del dev eth1 root
Deleting the root discipline returns the NIC to its default state. tc settings also disappear on reboot, but it is better to remove them explicitly once testing is finished.
One direction or both
netem delays only packets sent out of the NIC you specify. If you miss this, the configured value and the ping results will not match and you will be confused.
| NIC configured | Traffic delayed | Round-trip time of ping from the client |
|---|---|---|
1000 ms on eth1 |
client → server | increases by about 1000 ms |
1000 ms on eth0 |
server → client | increases by about 1000 ms |
500 ms each on eth0 and eth1 |
both directions | increases by about 1000 ms |
Because ping shows the round-trip time, 1000 ms in one direction and 500 ms in each direction look the same in the numbers. The difference shows up with traffic that flows in only one direction, or where sent and received volumes are heavily lopsided. In an upload test, for example, the results may differ depending on whether only the upstream is slow or the downstream acknowledgements are slow too.
To get close to a real WAN line, the natural approach is to apply the one-way delay to both NICs. To reproduce a line with a 200 ms round trip, do this:
tc qdisc add dev eth0 root netem delay 100ms
tc qdisc add dev eth1 root netem delay 100ms
Conversely, if all you want is a state where “the response from the server does not come back”, as in a timeout test, putting a long delay on the server → client NIC (eth0 in this setup) makes the intent clearer.
Reproducing other kinds of bad lines
Besides delay, netem can combine jitter, loss, bandwidth limits and more on a single line.
Jitter
tc qdisc change dev eth1 root netem delay 100ms 20ms
The second value is the jitter range; in this example the delay varies between 80 and 120 ms. Adding distribution normal makes the variation close to a normal distribution with a mean of 100 ms.
tc qdisc change dev eth1 root netem delay 100ms 20ms distribution normal
With jitter, each packet waits a different time, so a packet sent earlier can arrive later (reordering). This can happen on the real internet too, but because TCP treats reordering as a hint to retransmit, large jitter reduces throughput more than you might expect. If you want to test latency and reordering separately, try without jitter first.
Packet loss
tc qdisc change dev eth1 root netem delay 100ms loss 1%
This randomly drops 1% of outgoing packets. It is useful for tests that assume a mobile or congested line. Loss causes TCP retransmissions, so the perceived experience often gets much worse than with delay alone.
You can also specify duplicate (duplicated packets), corrupt (bit errors) and reorder (reordering). The available options are listed in man tc-netem.
Bandwidth limits
tc qdisc change dev eth1 root netem delay 50ms rate 10mbit
Adding rate throttles the send rate to that value. Use it to check how long file transfers or backups take over a narrow line. If large transfers run at a speed different from the setting, the NIC’s offload features (which handle several packets together) may be affecting it, so try turning them off with ethtool -K eth1 gro off gso off tso off and compare.
Delaying only a specific destination
Sometimes there are several servers beyond the bridge and you want to make only one of them a “far-away server”. In that case, use a discipline called prio to sort packets and send only the matching ones to netem.
tc qdisc add dev eth1 root handle 1: prio
tc qdisc add dev eth1 parent 1:3 handle 30: netem delay 200ms
tc filter add dev eth1 parent 1: protocol ip prio 1 u32 match ip dst 192.168.1.20/32 flowid 1:3
The third line sends IPv4 packets destined for 192.168.1.20 to the third band (1:3), where the attached netem delays them by 200 ms. Packets that do not match go through the first two bands and are hardly delayed (low-priority packets may also land in the third band). To remove it, tc qdisc del dev eth1 root clears everything at once, as before.
Caveats for long delays
For a few hundred milliseconds the steps above are all you need, but with extreme delays of 10 or 60 seconds it is easy to stumble on things that have nothing to do with what you are testing.
How many packets netem can hold (limit)
netem keeps delayed packets queued. The cap is limit, which defaults to 1000 packets. A 60-second delay means holding every packet sent during those 60 seconds. If 1500-byte packets keep flowing at 1 Mbps, that is about 83 per second, or about 5000 in 60 seconds, far above the default cap.
Packets beyond the cap are dropped, so what was meant to be a latency test turns into a loss test. For long delays, raise limit.
tc qdisc add dev eth1 root netem delay 60s limit 100000
If dropped in tc -s qdisc show dev eth1 keeps rising, you are most likely hitting the cap.
ARP gives up first
When the bridge delays traffic, not only IP packets but also ARP, which looks up the peer’s MAC address, is delayed. The client waits a few seconds for an ARP reply and, if none comes, concludes the peer is unreachable. With delays longer than a few seconds, communication can fail with “unreachable” before the application traffic even starts.
There are two remedies. One is to register each other’s MAC addresses statically on the client and the server. The other is to use prio from the previous section and send only IPv4 packets to netem.
tc qdisc add dev eth1 root handle 1: prio
tc qdisc add dev eth1 parent 1:3 handle 30: netem delay 60s limit 100000
tc filter add dev eth1 parent 1: protocol ip prio 1 u32 match u32 0 0 flowid 1:3
match u32 0 0 is a condition that matches every IPv4 packet; ARP is not included, so it is not delayed. Note that IPv6 neighbor discovery (the IPv6 counterpart of ARP) travels as IPv6 packets, so when testing over IPv6 you need to think separately about a protocol ipv6 condition.
Your own commands are delayed too
If you SSH into the Linux machine through the bridge, that SSH traffic is delayed as well. Right after adding a 60-second delay, you can end up typing tc qdisc del and waiting more than a minute for it to take effect. As noted at the start, operating from a local console or through a separate NIC is safest.
If you really must work through the bridge, arrange for the delay to be removed automatically after a set time before you add it, for peace of mind.
systemd-run --on-active=10min /usr/sbin/tc qdisc del dev eth1 root
tc qdisc add dev eth1 root netem delay 60s limit 100000
Application timeouts fire first
With a 60-second delay, TCP connection-setup retransmissions or the default timeouts of the OS and middleware may kick in first. Decide what you want to check (the application’s timeout message, or the connection itself failing) before choosing the delay value, and you are less likely to misread the results.
Other things to watch
- Kernels without netem: if you see
Error: Specified qdisc kind is unknown., the kernel lacks the netem module. On kernels such as Ubuntu’s cloud kernels, installinglinux-modules-extra-$(uname -r)may make it available. - Do not create a loop: put the Linux machine in series between the client and the server. Connecting both NICs to the same switch creates a loop, and broadcasts circulating endlessly can bring down the whole network. If you insert it into a production network, coordinate with the administrator beforehand.
- Settings do not persist: both the bridge and the
tcsettings disappear on reboot. If you test under the same conditions every time, collecting the commands above into a shell script makes it easy both to reproduce the conditions and to keep a record. - Always measure the result: after adding delay, check with
pingfrom the client to the server that the round-trip time increased as intended. Recording the configured and measured values side by side makes it easier to explain the test results later.
Cleaning up
When testing is done, remove the delay and the bridge and return the NICs to their original state.
tc qdisc del dev eth0 root
tc qdisc del dev eth1 root
ip link set br0 down
ip link del br0
If you used brctl, detach the NICs with brctl delif br0 eth0 and brctl delif br0 eth1, then remove the bridge with ifconfig br0 down and brctl delbr br0.
With one PC fitted with two NICs, you can create a slow line under the same conditions as often as you like without touching the client or server settings. Compared with the Windows tool and its 1000 ms ceiling, being able to produce extreme values such as 60 seconds accurately was reassuring. Timeout-related bugs are often found only on slow production lines, so it is worth inserting a slow line and trying things out once before release.