Tuesday, June 18, 2013

Bandwidth Control with HTB


This is old script that I did in the past for bandiwth control :

---------------------------------------------------------------------------------------------------------
#!/bin/bash
# Script para regras de controle de banda para VOIP
#
# chkconfig: 2345 90 80
# description: qosvoip script
#

BANDA_LIMITE=5120kbit
IFACE_all=`ifconfig | awk '{print $1}' | egrep '^eth|^tun' | fmt -w 200`

# Carregando o módulo
/sbin/modprobe sch_htb
/sbin/modprobe sch_sfq

function start() {

for IFACE in $IFACE_all

do
        tc qdisc add dev $IFACE root handle 1: htb default 30
        tc class add dev $IFACE parent 1: classid 1:1 htb rate $BANDA_LIMITE

        tc class add dev $IFACE parent 1:1 classid 1:10 htb rate 512kbit ceil 1024kbit
        tc class add dev $IFACE parent 1:1 classid 1:20 htb rate 512kbit ceil 1024kbit
        tc class add dev $IFACE parent 1:1 classid 1:30 htb rate 4096kbit ceil $BANDA_LIMITE

        tc qdisc add dev $IFACE parent 1:10 handle 10: sfq perturb 10
        tc qdisc add dev $IFACE parent 1:20 handle 20: sfq perturb 10
        tc qdisc add dev $IFACE parent 1:30 handle 30: pfifo


        tc filter add dev $IFACE parent 1:0 protocol ip prio 1 u32 match ip dport 10000 0x3C00 flowid 1:10
        tc filter add dev $IFACE parent 1:0 protocol ip prio 1 u32 match ip sport 10000 0x3C00 flowid 1:10
        tc filter add dev $IFACE parent 1:0 protocol ip prio 1 u32 match ip dport 5060 0xffff flowid 1:20
done

    echo -ne "************************************************************************\n"
    echo -ne "QOS VOIP MARINGA\n"
    echo -ne "Filtros carregados\n"
    echo -ne "************************************************************************\n"
}

function stop() {

for IFACE in $IFACE_all
do
        tc qdisc del dev $IFACE root 2> /dev/null > /dev/null
done

    echo -ne "************************************************************************\n"
    echo -ne "Filtros removidos...\n"
    echo -ne "************************************************************************\n"
}

function restart() {
        stop
    sleep 1
        start
}

case "$1" in
        'start')
                start
        ;;
        'restart')
                restart
        ;;
        'stop')
                stop
        ;;
        *)
        echo "usage: start|restart|stop"
esac


---------------------------------------------------------------------------------------------------------

1 comment: