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.
#!/bin/sh # usage : ./xxx.sh interface [loss] [delay] # If only the interface name is specified, the script # will remove any 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%
| Labels: howto, performance |
|

Comment
Hi, I tried your promising shell-skript. It does execute without errors, but, alas, it does not seem to delay incoming traffic. When running with “bash -x” to see commands issued, everything seems OK:
# bash -x netem.sh eth0 0 250 + if0=eth0 + loss=0 + delay=250ms + '[' 3 -eq 1 ']' + modprobe ifb + ip link set dev ifb0 up + tc qdisc del dev eth0 ingress + tc qdisc add dev eth0 ingress + tc filter add dev eth0 parent ffff: protocol ip u32 match u32 0 0 flowid 1:1 action mirred egress redirect dev ifb0 Action 4 device ifb0 ifindex 4 + tc qdisc del dev ifb0 root + tc qdisc add dev ifb0 root netem delay 250ms loss 0%
However, doing a ping doesn't add any delay:
# ping google.de PING google.de (74.125.230.80) 56(84) bytes of data. 64 bytes from 74.125.230.80: icmp_seq=1 ttl=56 time=51.1 ms 64 bytes from 74.125.230.80: icmp_seq=2 ttl=56 time=50.5 ms
That's really saddening, I was having high hopes that the magical incantations of your script would finally help me in delaying incoming traffic with netem ;)
You have to specify “ms” : tc qdisc add dev eth0 root netem delay 100ms
Works fine for me