Menu

Network Emulation

Network Emulation Using netem

Network performance can be non-deterministic at times, which makes verifying that tools are working correctly for certain failure scenarious a difficult task.  To emulate certain behaviors in a deterministic fashion, it is often necessary to turn to external forces that can install certain behaviors.  netem provides Network Emulation functionality for testing protocols by emulating the properties of wide area networks. The current version emulates variable delay, loss, duplication and re-ordering.

Preliminary Information

The following information assumes that the user has access to the root account, or use of sudo.  Netem is controlled by the command line tool 'tc' which is part of the iproute2 package of tools. The tc command uses shared libraries and data files in the /usr/lib/tc directory, and manipuates kernel level controls to gain the performacne altering effects.  It is recommended that the user save current state before installing any of these emulations. 

This is a cursory introduction to the use of netem for testing performacne monitoring tools, and does not go into depth in many of the features of this powerful tool.  There are more in depth guides available on this subect, including this one: http://www.lartc.org/

All of the examples below use a single hierachical element (e.g. root) that manipulates the behavior of the NIC that specified.  Advanced options make other behaviors and categories possible. 

Packet Loss

To install packet loss as a factor of a percentage, the following commands can be used.  Note that the first command clears out any previous specifications to the NIC/hierarchy used.  It is recommended to run this after you are done emulating any behavior. 

sudo /sbin/tc qdisc delete dev eth0 root
sudo /sbin/tc qdisc add dev eth0 root handle 1: prio
sudo /sbin/tc qdisc add dev eth0 parent 1:1 handle 10: netem loss 10%
sudo /sbin/tc filter add dev eth0 protocol ip parent 1:0 prio 3 u32 match ip dst AAA.BBB.CCC.DDD/32 flowid 1:1

The last line installs a filter that is specific to a specific host or subnet.  When testing your behavior of a WAN with a specific host (or set of hosts) this behavior can be used to limit the impacts of the packet loss.  Without this, all hosts will see the same impact of this emulation. 

Packet Duplication

Packet duplication is also installed as a function of a percentage.  To install this behavior, use the following commands.  Note that the first command clears out any previous specifications to the NIC/hierarchy used.  It is recommended to run this after you are done emulating any behavior.

sudo /sbin/tc qdisc delete dev eth0 root
sudo /sbin/tc qdisc add dev eth0 root handle 1: prio
sudo /sbin/tc qdisc add dev eth0 parent 1:1 handle 10: netem duplicate 10%
sudo /sbin/tc filter add dev eth0 protocol ip parent 1:0 prio 3 u32 match ip dst AAA.BBB.CCC.DDD/32 flowid 1:1

As in the previous example, the final line of the command sequence is used to restrict the behavior to a single host or subnet of your choosing, which is helpful in testing. 

Packet Corruption (Errors)

Packet corruption is the act of changing bits in the packet header or data fields, which will cause checksum operations to fail and the packet will be marked as an error by the receiver.  To install this behavior (as a percentage), use the following commands.  Note that the first command clears out any previous specifications to the NIC/hierarchy used.  It is recommended to run this after you are done emulating any behavior.

sudo /sbin/tc qdisc delete dev eth0 root
sudo /sbin/tc qdisc add dev eth0 root handle 1: prio
sudo /sbin/tc qdisc add dev eth0 parent 1:1 handle 10: netem corrupt 10%
sudo /sbin/tc filter add dev eth0 protocol ip parent 1:0 prio 3 u32 match ip dst AAA.BBB.CCC.DDD/32 flowid 1:1

As in the previous examples, the final line of the command sequence is used to restrict the behavior to a single host or subnet of your choosing, which is helpful in testing.

Packet Ordering

Changing the order of packets will impact TCP flows significantly, as the behavior looks like packet loss, but results in duplication when the late arrivals finally reach the other end.  The following recipe results in 25% of packets (with a correlation of 50%) getting sent immediately, others will be delayed by 10ms.  Note that the first command clears out any previous specifications to the NIC/hierarchy used.  It is recommended to run this after you are done emulating any behavior.

sudo /sbin/tc qdisc delete dev eth0 root
sudo /sbin/tc qdisc add dev eth0 root handle 1: prio
sudo /sbin/tc qdisc add dev eth0 parent 1:1 handle 10: netem delay 10ms reorder 25% 50%
sudo /sbin/tc filter add dev eth0 protocol ip parent 1:0 prio 3 u32 match ip dst AAA.BBB.CCC.DDD/32 flowid 1:1

As in the previous examples, the final line of the command sequence is used to restrict the behavior to a single host or subnet of your choosing, which is helpful in testing.

Removing Emulation

The following command will remove any emulations added to the NIC and specification class:

sudo /sbin/tc qdisc delete dev eth0 root