How can I simulate delayed and dropped packets in Linux?

The standard way to delay and drop packets in Linux is with the netem scheduling policy; this can be applied to any network device, whether an interface or a bridge, using the tc command from the iproute2 set of tools:

https://wiki.linuxfoundation.org/networking/iproute2

A few examples of how to do so are:

  • Add 10ms of delay to every packet transmitted on eth0:
     $ tc qdisc add dev eth0 root netem delay 10ms
  • Add 10ms of delay and 20ms of jitter to every packet bridged by br0:
     $ tc qdisc add dev br0 root netem delay 10ms 20ms
  • Randomly drop approximately one percent of packets transmitted on eth1:
     $ tc qdisc add dev eth1 root netem loss 1%

The netem scheduler has evolved to be a very sophisticated emulator with a rich variety of possible behaviours. The iproute2 man page tc-netem.8 provides more details, while the definitive documentation is in the source code linux-4.XX.X/net/sched/sch_netem.c. This describes in detail the Markov model used, and provides references for further reading.