Catch up on stories from the past week (and beyond) at the Slashdot story archive

 



Forgot your password?
typodupeerror
Check out the new SourceForge HTML5 internet speed test! No Flash necessary and runs on all devices. ×
User Journal

Journal IBitOBear's Journal: TCP Shaper Script

#!/bin/bash
### BEGIN INIT INFO
# Provides:          shaper
# Required-Start:    firewall
# Required-Stop:     firewall
# Default-Start:     S
# Default-Stop:      0 6
# Short-Description: Shape traffic on interfaces.
### END INIT INFO

# init script written by shane at knowplace dot org
# this script only creates the qdiscs and classes required for shaping, it
# does NOT create the necessary filters

# modified by Rob White rwhite at pobox dot com
#   mostly to take the link speed as an argument

INTERFACE='eth1'
declare -i INTERFACE_SPEED=768
declare -i BURST=1200
declare -i CBURST=1500

rc_done="  done"
rc_failed="  failed"

declare -i  CEILING=$((INTERFACE_SPEED * 98 / 100))
declare -i  COUNTER
declare -ai WEIGHT=(2 2 2 2 2)
declare -i  SLOTS=${#WEIGHT[@]}
declare -i  AGGREGATE=0
for ((COUNTER=0; COUNTER<SLOTS; ++COUNTER)) do
   AGGREGATE=$((AGGREGATE+WEIGHT[COUNTER]))
done
AGGREGATE=$((AGGREGATE+1))
declare -ai SHARE
declare -i  USED=0

for ((COUNTER=0; COUNTER<SLOTS; ++COUNTER)) do
   USED=$(( USED + (SHARE[$COUNTER]=(CEILING * ${WEIGHT[$COUNTER]} / AGGREGATE)) ))
done
SHARE[$COUNTER]=$(( INTERFACE_SPEED - USED))

declare -i BURST_CEILING=$((CEILING * 100 / 100))
#declare -i BURST_CEILING=$CEILING

return=$rc_done

TC='/sbin/tc'

tc_reset ()
{
        # Reset everything to a known state (cleared)
        $TC qdisc del dev $INTERFACE root 2> /dev/null > /dev/null
}

tc_status ()
{
    echo "[qdisc - $INTERFACE]"
    $TC -s qdisc show dev $INTERFACE
    echo "------------------------"
    echo
    echo "[class - $INTERFACE]"
    $TC -s class show dev $INTERFACE
}

tc_showfilter ()
{
    echo "[filter - $INTERFACE]"
    $TC -s filter show dev $INTERFACE
}

case "$1" in

    start)
        echo -n "Starting traffic shaping"
        tc_reset
        U320="$TC filter add dev $INTERFACE protocol ip parent 1:0 prio 0 u32"
        #
        # dev eth0 - creating qdiscs & classes
        #
        $TC qdisc add dev $INTERFACE root handle 1: htb default $((${#SHARE[@]}+1))0
        $TC class \
            add dev $INTERFACE \
            parent 1: classid 1:1 \
            htb rate ${CEILING}kbps \
                ceil ${BURST_CEILING}kbps \
                ${BURST:+burst ${BURST}b} \
                ${CBURST:+cburst ${CBURST}b}
        for ((COUNTER=0; COUNTER < (${SLOTS} + 1); ++COUNTER)) do
           HANDLE=$((COUNTER+1))0
           $TC class \
               add dev $INTERFACE \
               parent 1:1 classid 1:$HANDLE \
               htb rate ${SHARE[$COUNTER]}kbps \
                   ceil ${BURST_CEILING}kbps \
                   ${BURST:+burst ${BURST}b} \
                   ${CBURST:+cburst ${CBURST}b} \
                   prio $COUNTER
           $TC qdisc \
               add dev $INTERFACE \
               parent 1:$HANDLE \
               handle $HANDLE: \
               sfq perturb 6
        done
        tc_status
        ;;

     stop)
        echo -n "Stopping traffic shaper"
        tc_reset || return=$rc_failed
        echo -e "$return"
        ;;

    restart|reload)
        $0 stop && $0 start || return=$rc_failed
        ;;

    stats|status)
        tc_status
        ;;

    filter)
        tc_showfilter
        ;;

    *)
        echo "Usage: $0 {start|stop|restart|stats|filter}"
        exit 1

esac
test "$return" = "$rc_done" || exit 1
This discussion has been archived. No new comments can be posted.

TCP Shaper Script

Comments Filter:

Never say you know a man until you have divided an inheritance with him.

Working...