Simulate poor network connection with Netem

PUBLISHED ON MARCH 22, 2017 — LINUX, NETWORKING

Requirements

  • iproute2
  • iptables
  • dnsmasq

Two network cards are required:

  • eth0: attached to your internal LAN, which should be able to reach the Internet
  • eth1: to be used by the devices that should experience the poor simulated network conditions. You can either attach a wired device directly or an access point to it.

Setup

NAT

We will configure NAT via masquerading on eth0 so that devices connected through eth1 are able to access the Internet.

ip link set eth1 up
# for the next step, any other subnet not already used on your machine will be ok too
ip addr add 192.168.5.1/24 dev eth1
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

DHCP and DNS forwarding

We will use dnsmasq to provide devices connected via eth1 a dynamic IP through DHCP and to let your machine act as a DNS server for them.

Copy the following in /etc/dnsmasq.conf:

interface=eth1
domain-needed
bogus-priv
no-resolv
no-poll
server=8.8.8.8
no-hosts
# the following lines must match the subnet used by eth1
dhcp-range=192.168.5.20,192.168.5.50,72h
dhcp-option=option:router,192.168.5.1

Netem configuration

The general syntax of netem is:

tc qdisc <add|change> dev <device> root netem loss <%drop> <%drop_correlation> delay <delay> <delay_variance> <%delay_correlation>

add must be used the first time you give the command to create a queue distribution with the given parameters and to associate it with the network interface. To apply a different configuration, use change instead.

The queue distribution can be configured with the following parameters:

  • <device>: the network interface to use
  • <%drop>: the percentage [0, 100] of packages to drop
  • <%drop_correlation>: the percentage [0, 100] correlation to drop a packet with the drop decision for the previous packet
  • <delay>: the average uniform delay to add to packets
  • <delay_variance>: the jitter to add to packets
  • <%delay_correlation>: the percentage [0, 100] correlation to delay a packet with the delay decision for the previous packet

For example, the following command:

tc qdisc change dev eth1 root netem loss 30% 25% delay 200ms 160ms 20%

will drop 30% of packets with a correlation of 25%, and add a 200ms of delay with 160 ms of jitter and a correlation of 20%.

For more information, see Netem manual and the following tutorial: https://www.excentis.com/blog/use-linux-traffic-control-impairment-node-test-environment-part-2

TAGS: LINUX, NETWORKING