web analytics

Netem, tc and qdisc loss and delay emulation examples

Traffic shaping and emulation of network properties (delay, loss) is very useful when conducting experiments aiming at investigating protocol behavior under specific network conditions. One of the tools that can be used under linux is netem (network emulator).

Installing Netem

In order to use netem, you need to install the iproute package.

apt-get install iproute

Netem loss and delay example

Given an interface name, network delay (in milliseconds) and packet loss rate (in percentage), all IP packets traveling through the interface will be delayed for the specified amount, and randomly dropped according to the specified packet loss rate. The script requires ifb (Intermediate Functional Block) and operates on outgoing as well as incoming traffic.

# usage : ./xxx.sh interface [loss] [delay]
# If only the interface name is specified, the script 
# will remove any previously applied delay and loss emulation
if  [ $# -eq 1 ]
echo $#
tc qdisc del dev $if0 ingress
tc qdisc del dev ifb0 root
modprobe ifb
ip link set dev ifb0 up
tc qdisc del dev $if0 ingress
tc qdisc add dev $if0 ingress
tc filter add dev $if0 parent ffff: \
protocol ip u32 match u32 0 0 flowid 1:1 action mirred egress redirect dev ifb0
tc qdisc del dev ifb0 root
tc qdisc add dev ifb0 root netem delay $delay loss $loss%