Simulate Packet Loss When Testing UDP Applications

Testing User Datagram Protocol (UDP) applications requires verifying how the software behaves under degraded network conditions, as UDP does not provide built-in delivery guarantees, retransmission, or packet ordering. This guide outlines the most effective, straight-to-the-point methods to artificially induce packet loss, latency, and jitter across Linux, macOS, Windows, and application-level testing environments.

1. Linux: Traffic Control (tc) with NetEm

The Linux kernel contains a built-in network emulation module called Network Emulator (netem), managed via the tc (traffic control) command. It operates at the kernel level and requires root privileges.

To target only specific UDP traffic, bind netem to a prio or htb class and use iptables or tc filter matching specific UDP ports.

2. Windows: Clumsy

For Windows environments, Clumsy is an open-source utility that uses the WinDivert library to intercept network packets and modify them before they reach the network stack.

  1. Download and run Clumsy as Administrator.
  2. Under Filtering, set the target criteria (e.g., udp and udp.DstPort == 5000 to capture only your application’s traffic).
  3. Check the Drop function and set the drop percentage (e.g., 15.0%).
  4. Click Start to begin dropping packets and Stop to restore normal operation.

Clumsy also supports out-of-order delivery, lag, duplicate packets, and throttle simulation.

macOS provides both a native graphical tool and command-line packet filtering.

  1. Download Additional Tools for Xcode from the Apple Developer portal.
  2. Install Network Link Conditioner from the Hardware folder.
  3. Open the pane in System Settings, choose a preset (e.g., “100% Loss”, “Very Bad Network”) or create a custom profile specifying exact packet loss and delay percentages.
  4. Toggle the switch to ON.

Option B: pf (Packet Filter) and dummynet (CLI)

Create a dummynet pipe to simulate loss:

sudo dnctl pipe 1 config plr 0.10

(This creates a pipe with a 10% packet loss rate).

Route specific UDP traffic into this pipe by editing /etc/pf.conf or applying a temporary anchor rule referencing port and protocol:

dummynet in proto udp from any to any port 5000 pipe 1

4. Cross-Platform Automated Testing: Toxiproxy

If you need to test UDP reliability in automated CI/CD pipelines, Shopify’s Toxiproxy is a TCP/UDP proxy designed specifically for simulating network chaos.

  1. Start the Toxiproxy daemon.

  2. Register your UDP service as a proxy target via the HTTP API or client libraries (Go, Python, Node, Java, etc.).

  3. Apply the loss toxic to the proxy:

    POST /proxies/{proxy_name}/toxics
    {
      "type": "loss",
      "stream": "downstream",
      "toxicity": 0.15
    }

    (This configures a 15% packet drop rate automatically during test execution).

5. Application-Layer Simulation (Mocking)

When system-level privileges are unavailable, implement an interceptor or wrapper around your UDP socket.