Outils d'utilisateurs

Outils du Site


benchmarking:link-emulation

Émulation de lien réseau

Parfois, on peut avoir besoin de simuler un lien WAN avec divers critères (pertes de paquets, bande passante, latence, etc.). Ce petit script permet de mettre en place avec Traffic Control ce type d'émulation ;)

#!/bin/bash
 
#
#  tc uses the following units when passed as a parameter.
#  kbps: Kilobytes per second
#  mbps: Megabytes per second
#  kbit: Kilobits per second
#  mbit: Megabits per second
#  bps: Bytes per second
#       Amounts of data can be specified in:
#       kb or k: Kilobytes
#       mb or m: Megabytes
#       mbit: Megabits
#       kbit: Kilobits
#  To get the byte figure from bits, divide the number by 8 bit
#
 
#
# Name of the traffic control command.
TC=/sbin/tc
 
# The network interface we're planning on limiting bandwidth and latency
IF=ovirtmgmt             # Interface
 
# Destination IP to which you need to apply emulation
DESTIP=172.16.0.2
 
# Link Bandwidth
BWD=1000Mbit
 
# Upload limit (in mega bits)
UPLDLT=1mbit          # UPLOAD Limit
 
# Added latency (in milliseconds)
LNCY=300ms
 
# Packet loss to emulate
PLOSS=0%
 
start() {
 
$TC qdisc add dev $IF handle 1: root htb
$TC class add dev $IF parent 1:1 classid 1:11 htb rate $BWD
# $TC class add dev $IF parent 1:1 classid 1:12 htb rate $BWD
# $TC class add dev $IF parent 1:1 classid 1:13 htb rate $BWD
 
$TC qdisc add dev $IF parent 1:11 handle 10: netem delay $LNCY rate $UPLDLT
# $TC qdisc add dev $IF parent 1:12 handle 20: tbf rate $UPLDLT limit $UPLDLT burst $UPLDLT
# $TC qdisc add dev $UF parent 1:13 handle 20: netem loss $PLOSS delay 100ms
 
# IP and Port filter
# $TC filter add dev $IF protocol ip prio 1 u32 match ip protocol 17 0xff match ip dport 5000 0xffff match ip dst 192.168.1.1 flowid 1:11
# IP filter
$TC filter add dev $IF protocol ip prio 1 u32 match ip dst $DESTIP flowid 1:11
# Port filter
# $TC filter add dev $IF protocol ip prio 1 u32 match ip dport 5001 0xffff flowid 1:13
 
}
 
stop() {
 
# Stop the link shaping.
$TC qdisc del dev $IF root
 
}
 
restart() {
 
# Self-explanatory.
stop
sleep 1
start
 
}
 
show() {
 
# Display traffic control status.
$TC -s qdisc ls dev $IF
 
}
 
case "$1" in
 
start)
 
echo -n "Starting link emulation: "
start
echo "done"
;;
 
stop)
 
echo -n "Stopping link emulation: "
stop
echo "done"
;;
 
restart)
 
echo -n "Restarting link emulation: "
restart
echo "done"
;;
 
show)
 
echo "Link emulation status for $IF:"
show
echo ""
;;
 
*)
 
pwd=$(pwd)
echo "Usage: network_em.bash {start|stop|restart|show}"
;;
 
esac
exit 0
benchmarking/link-emulation.txt · Dernière modification: 2012/08/29 16:29 par nekolover