Last active
September 4, 2019 05:13
-
-
Save nyrahul/71dcf812f3c03032202c38afc2d23854 to your computer and use it in GitHub Desktop.
Use 'tc netem' to set delay/latency and loss parameters on an interface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
usage() | |
{ | |
cat << EOF | |
USAGE: $0 [-hc] [-a <adapter>] [-d <delay-param>] [-l <loss-param>] | |
OPTIONS: | |
-h Show this message | |
-a <adapter> Set the adapter | |
-d <delay> Set delay/latency parameters (as accepted by netem[1]) | |
-l <loss> Set loss parameters (as accepted by netem[1]) | |
[1]: https://wiki.linuxfoundation.org/networking/netem | |
EXAMPLES: | |
$0 -a eth0 -d 100ms ... set latency 100ms | |
$0 -a eth0 -d "100ms 10ms 25%" ... set latency 100ms, +/-10ms, with next random element depending 25% on prev one. | |
$0 -a eth0 -l 0.1% ... causes 1/10th of a percent (i.e 1 out of 1000) packets to be randomly dropped. | |
$0 -a eth0 -l 0.3% 25% ... cause 0.3% pkt lost, and successive probability depends by a quarter of last one. | |
$0 -c -a eth0 ... clear all config | |
EOF | |
exit 1 | |
} | |
clear_cfg() | |
{ | |
# Delete existing settings | |
tc qdisc del dev $IFACE root | |
[[ "$MODE" == "clear" ]] && echo "cleared config" && exit 0 | |
} | |
while getopts ha:l:d:c o | |
do case "$o" in | |
h) usage | |
exit 1;; | |
d) DELAY_PARAM=$OPTARG;; | |
l) LOSS_PARAM=$OPTARG;; | |
a) IFACE=$OPTARG;; | |
c) MODE="clear";; | |
[?]) usage | |
esac | |
done | |
[[ "$IFACE" == "" ]] && echo "Need interface" && usage | |
ip link show "$IFACE" > /dev/null | |
[[ $? -ne 0 ]] && echo "do not have link [$IFACE]" && usage | |
clear_cfg | |
CMD="" | |
[[ "$DELAY_PARAM" != "" ]] && CMD="$CMD delay $DELAY_PARAM" | |
[[ "$LOSS_PARAM" != "" ]] && CMD="$CMD loss $LOSS_PARAM" | |
tc qdisc add dev $IFACE root netem $CMD |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment