2023/06/20

Use tc and netem to simulate network delay, jitter and packet loss.

netem relies on kernel module:
CONFIG_IFB
CONFIG_NET_SCHED
CONFIG_NET_SCH_NETEM.

tc qdisc add dev br-lan root handle 1: htb

  1. This will create a root qdisc on the br-lan interface for htb

tc class add dev br-lan parent 1: classid 1:1 htb rate 1000mbit
tc qdisc add dev br-lan parent 1:1 netem delay 100ms 10ms
tc filter add dev br-lan parent 1:0 protocol ip u32 match ip sport 5101 0xffff flowid 1:1

  1. Create a class 1:1 under 1: of br-lan and set the speed limit to 1000bps.
  2. For class 1:1 set its netem to delay 100ms and jitter 10ms.
  3. Direct the packet with src port 5101 of the br-lan interface to class 1:1.

tc class add dev br-lan parent 1: classid 1:2 htb rate 1000mbit
tc qdisc add dev br-lan parent 1:2 netem loss 0% duplicate 0% delay 30ms 20ms
tc filter add dev br-lan parent 1:0 protocol ip u32 match ip sport 5001 0xffff flowid 1:2

  1. Create a class 1:2 under 1: of br-lan and set the speed limit to 1000bps.
  2. For class 1:2 set its netem to loss 0%, duplicate 0%, delay 30ms and jitter 20ms.
  3. Direct the packet with src port 5001 of the br-lan interface to class 1:2.