Qos & Tc

De BlaxWiki
Aller à la navigationAller à la recherche

Afin de restreinte le trafic, nous allons utiliser l'utilitaire TC qui fonctionne avec iptables. Ceci est le script d'init à envoyer au démarrage. Le cas présent est installé sur un serveur proxy, et limite les machines qui passent par ce meme proxy. Dans le cas d'une restriction pour le serveur lui même, il faudra ajuster la règle iptables "IPT -A OUTPUT -t mangle -p tcp --sport 80 -j MARK --set-mark 10"

La restriction ici se fait sur l'interface réseau eth0, pour limiter à 400kb (soit 60ko/s) le download en http.

#!/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.
IF=eth0             # Interface
# Download limit (in mega bits)
DNLD=400kbit          # DOWNLOAD Limit
#
IPT="/sbin/iptables"
start() {
   $IPT -t mangle -F
   $TC qdisc add dev $IF root handle 1:0 htb default 30
   $TC class add dev $IF parent 1:0 classid 1:10 htb rate $DNLD
   $IPT -A OUTPUT -t mangle -p tcp --sport 80 -j MARK --set-mark 10
   $TC filter add dev $IF parent 1:0 prio 0 protocol ip handle 10 fw
flowid 1:10
}

stop() {
# Stop the bandwidth shaping.
   $TC qdisc del dev $IF root
   $IPT --flush
}

restart() {
# Self-explanatory.
   stop
   sleep 1
   start
}

show() {
# Display status of traffic control status.
   $TC -s qdisc ls dev $IF
}

case "$1" in
 start)
   echo -n "Starting bandwidth shaping: "
   start
   echo "done"
   ;;
 stop)
   echo -n "Stopping bandwidth shaping: "
   stop
   echo "done"
   ;;
 restart)
   echo -n "Restarting bandwidth shaping: "
   restart
   echo "done"
   ;;
 show)
   echo "Bandwidth shaping status for $IF:"
   show
   echo ""
   ;;
 *)
   pwd=$(pwd)
   echo "Usage: tc.bash {start|stop|restart|show}"
   ;;
esac
exit 0