Limit outgoing bandwidth

We manage some servers where we needed to limit outgoing bandwidth to 500mbps. The servers are used for streaming video
content so we decided to limit the outgoing traffic on port 80 and port 443.

We used the following bash script based on traffic control shapping (tc).

#!/bin/bash
TC=/sbin/tc

IF=eth0 # Interface
U32="$TC filter add dev $IF protocol ip parent 1:0 prio 1 u32"

start() {
$TC qdisc add dev $IF root handle 1 htb default 100 r2q 100
$TC class add dev $IF parent 1: classid 1:2 htb rate 900Mbit burst 1000Mbit

$TC class add dev $IF parent 1:2 classid 1:30 htb rate 450Mbit ceil 495Mbit prio 1
$TC qdisc add dev $IF parent 1:30 handle 30 sfq perturb 1
$U32 match ip sport 80 0xffff match ip protocol 6 0xff classid 1:30
$U32 match ip sport 443 0xffff match ip protocol 6 0xff classid 1:30

}

stop() {
$TC qdisc del dev $IF root

}

restart() {

stop
sleep 1
start

}

show() {
$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: limits {start|stop|restart|show}"
;;

esac

exit 0

Hire us for more complex traffic shapping rules