Simulate Network Anomalies with TC and Netem
Netem and TC brief overview
Netem is a network emulation module provided by Linux 2.6 and later kernels. It can be used on a good LAN to simulate complex Internet transmission performance, such as low bandwidth, latency, packet loss, and so on. Many Linux distributions with kernel 2.6+ enable this module by default, such as Fedora, Ubuntu, Redhat, OpenSuse, CentOS, Debian, etc.
TC is a user-space tool in Linux, short for Traffic Control. TC controls the operating mode of the Netem module. In other words, to use Netem you need at least two conditions: the Netem module must be enabled in the kernel, and the corresponding user-space tool TC must be available.
- Delay all packets by 100ms:
$ tc qdisc add dev enp0s5 root netem delay 100ms - Simulate packet loss:
$ tc qdisc change dev enp0s5 root netem loss 50% - Simulate packet duplication:
$ tc qdisc change dev enp0s5 root netem duplicate 50% - Simulate packet corruption:
tc qdisc change dev enp0s5 root netem corrupt 2% - Simulate packet reordering (every 5 packets (5th, 10th, 15th…) are sent normally, others are delayed 100ms):
tc qdisc change dev enp0s5 root netem reorder 50% gap 3 delay 100ms
View transmission settings for enp0s5
$ tc qdisc show dev enp0s5
Wondershaper
Set download to 200kb/s and upload to 150kb/s
$ sudo wondershaper enp0s5 200 150
Remove rate limit
$ sudo wondershaper clear enp0s5
Comcast
$ comcast --device=enp0s5 --latency=250 \
--target-bw=1000 \
--default-bw=1000000 \
--packet-loss=10% \
--target-addr=8.8.8.8,10.0.0.0/24 \
--target-proto=tcp,udp,icmp \
--target-port=80,22,1000:2000
--devicespecifies the target NIC as enp0s5.--latencyspecifies a 250ms delay.--target-bwspecifies the target bandwidth.--default-bwspecifies the default bandwidth.--packet-lossspecifies the packet loss rate.--target-addr/--target-proto/--target-portapply the configuration above to packets that match these conditions.
