Netem, tc and qdisc for network delay and packet loss emulation: Howto and 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). This is a simple example for using netem to emulate packet loss and network delays.
Installing Netem
In order to use netem, you need may 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.
#!/bin/sh # usage : ./xxx.sh interface [loss] [delay] # if only interface is specified, the script will remove previously # applied delay and loss emulation if0=$1 loss=$2 delay=$3ms if [ $# -eq 1 ] then echo $# tc qdisc del dev $if0 ingress tc qdisc del dev ifb0 root exit fi 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%
