Simulate Network Latency with tc

Simulating Network Latency with tc

The tc command (Traffic Control) is a powerful Linux utility for shaping, scheduling, and emulating network traffic. Combined with the netem qdisc (network emulator), tc allows you to simulate latency, packet loss, jitter, duplication, and more—ideal for testing applications under real-world network conditions without needing specialized hardware.

Why Emulate Latency

  • Validate application performance in high-latency environments (satellite links, VPN tunnels).
  • Stress-test microservices, CI/CD pipelines, and streaming platforms.
  • Reproduce intermittent network issues in development and staging.
  • Compare VPN solutions (like OpenVPN or WireGuard) under varied latency.

Prerequisites

  1. A Linux kernel supporting netem (most distributions do).
  2. Root or sudo privileges.
  3. tc installed (from the iproute2 package).
  4. Basic networking knowledge (interfaces, routes).

Fundamentals of tc and netem

The tc tool structures traffic control into three components:

  • Qdisc (Queueing discipline): The scheduler attached to an interface (e.g., pfifo, netem).
  • Class: Subdivisions within a qdisc (used by classful qdiscs like htb).
  • Filter: Rules that direct packets to specific classes or qdiscs (matching by IP, port, protocol).

netem is a qdisc that can introduce delay, loss, duplication, and reordering. It is often combined with a classful scheduler like htb for bandwidth guarantees alongside latency simulation.

Basic Latency Simulation

Add Fixed Delay

To introduce a fixed delay of 100ms on interface eth0:

sudo tc qdisc add dev eth0 root netem delay 100ms

Verify:

tc qdisc show dev eth0

Remove the Emulation

sudo tc qdisc del dev eth0 root netem

Advanced Latency Patterns

Variable Delay (Jitter)

Combine a base delay with variability:

sudo tc qdisc add dev eth0 root netem delay 100ms 20ms distribution normal

This adds a 100ms delay with a standard deviation of 20ms, following a normal distribution.

Packet Loss, Duplication, and Reordering

  • Loss 0.5%: loss 0.5%
  • Duplicate 1%: duplicate 1%
  • Reorder 25% with 50ms gap: reorder 25% 50ms
sudo tc qdisc add dev eth0 root netem delay 100ms 10ms loss 0.5% duplicate 1% reorder 25% 50ms

Combining Bandwidth and Latency

To simulate both bandwidth limits and latency, nest htb and netem:

  1. Create htb root qdisc limiting to 5mbit:
  2. sudo tc qdisc add dev eth0 root handle 1: htb default 10
    sudo tc class add dev eth0 parent 1: classid 1:10 htb rate 5mbit ceil 5mbit
  3. Attach netem to that class:
  4. sudo tc qdisc add dev eth0 parent 1:10 handle 10: netem delay 150ms

Targeted Emulation with Filters

Use filters to apply latency only to certain traffic. Example: add 200ms delay for HTTP (port 80) only:

  1. Root qdisc:
  2. sudo tc qdisc add dev eth0 root handle 1: prio
  3. Create a netem qdisc on band 3:
  4. sudo tc qdisc add dev eth0 parent 1:3 handle 30: netem delay 200ms
  5. Filter HTTP traffic into band 3:
  6. sudo tc filter add dev eth0 protocol ip parent 1: prio 1 u32 
     match ip dport 80 0xffff flowid 1:3

Use Cases and Best Practices

Application Testing

  • Web applications under high-latency links.
  • VoIP and video conferencing jitter tolerance.
  • Mobile apps on 3G/4G networks.

VPN Performance Evaluation

Assess the impact of latency on tunneling protocols and encryption overhead:

Simulate worst-case scenarios, such as satellite uplinks, to ensure your VPN stays stable.

CI/CD and Integration Tests

Automate tc rules in test pipelines to catch latency-related regressions early. Combine with network namespace isolation (ip netns) for reproducible environments.

Troubleshooting and Tips

  • Persist Rules: tc rules are ephemeral. Use startup scripts or network-manager hooks to reapply.
  • Measure Impact: Combine ping, iperf3, and tshark to validate.
  • Cleanup: Remove all qdiscs with tc qdisc del dev eth0 root if things go awry.
  • Monitoring: Use tc -s qdisc for statistics on packet drops and latency distributions.

Conclusion

By mastering tc and netem, you gain fine-grained control over network impairments. Whether you’re troubleshooting production issues, benchmarking VPNs, or validating quality-of-service, tc offers a versatile toolkit. Integrate these techniques into your development lifecycle to build resilient, high-performance applications that thrive under diverse network conditions.

Download TXT




Leave a Reply

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