welcome to the home of LeShaper script…

since wondershaper only almost did what i wanted and sometimes pings were slow when they shouldn’t have been and i couldn’t figure out why. i thus followed the “keep it simple stupid” principle, use tbf and prio and created my own version of a “slow down the uplink so the queue is local, always prioritise ssh/dns/fill-in-your-favourite-kind-of-nework-traffic, but give all idle bandwith to whatever can make use of it”-bandwith shaper.

i hope it may also save someone else some trouble. darcs based version control is easy, so why not use it. questions and comments welcome.

darcs repository: darcs repository of leshaper

via darcsweb webfrontend: darcsweb view of darcs repository of leshaper

version of may 2007 inline:

#!/bin/sh

. /etc/rc.conf


# leshaper, based on the fabulous wondershaper:


# Set the following values to somewhat less than your actual download
# and uplink speed. In kilobits. Also set the device that is to be shaped.

DOWNLINK=4000
UPLINK=500
DEV=ppp0

case $1 in
autostart)
	test x"${leshaper:-NO}" = x"NO" && exit 0
	exec $0 start
	;;
start)
        ########## uplink ###############

        # shape the root at UPLINK speed to keep the queue local no matter what
        tc qdisc add dev $DEV root handle 1: tbf rate ${UPLINK}kbit burst 5k latency 50ms mpu 64b
        # prio band on top of that (automagically prioritises interactive traffic according to TOS bits)
        tc qdisc add dev $DEV parent 1: handle 11 prio
                                
        tc qdisc add dev $DEV parent 11:1 handle 111: sfq perturb 10
        tc qdisc add dev $DEV parent 11:2 handle 112: sfq perturb 10
        tc qdisc add dev $DEV parent 11:3 handle 113: sfq perturb 10
                                
        # prioritize small packets (<64 bytes) into first class
        tc filter add dev $DEV parent 11:0 protocol ip prio 1 u32 \
            match ip protocol 6 0xff \
            match u8 0x05 0x0f at 0 \
            match u16 0x0000 0xffc0 at 2 \
            flowid 11:1

	# higher prio for voip
        tc filter add dev $DEV parent 11:0 protocol ip prio 2 u32 match ip src 172.23.1.29/32 flowid 11:1 
        tc filter add dev $DEV parent 11:0 protocol ip prio 2 u32 match ip dport 4569 0xffff flowid 11:1 
        tc filter add dev $DEV parent 11:0 protocol ip prio 2 u32 match ip sport 4569 0xffff flowid 11:1 
        tc filter add dev $DEV parent 11:0 protocol ip prio 2 u32 match ip sport 5004 0xffff flowid 11:1 

        ########## downlink #############

        # slow downloads down to somewhat less than the real speed  to prevent 
        # queuing at our ISP. Tune to see how high you can set it.
        # ISPs tend to have *huge* queues to make sure big downloads are fast
        #
        # attach ingress policer:
                                
        tc qdisc add dev $DEV handle ffff: ingress
                                
        # filter *everything* (0.0.0.0/0), drop everything that's
        # coming in too fast:
        tc filter add dev $DEV parent ffff: protocol ip prio 50 u32 match ip src \
        0.0.0.0/0 police rate ${DOWNLINK}kbit burst 10k drop flowid :1
            
	;;
stop)
        # clean existing down- and uplink qdiscs, hide errors
        #tc qdisc del dev $DEV root    2> /dev/null > /dev/null
        #tc qdisc del dev $DEV ingress 2> /dev/null > /dev/null
        tc qdisc del dev $DEV root
        tc qdisc del dev $DEV ingress
	;;
restart)
	$0 stop
	$0 start
	;;
status)
        tc -s qdisc ls dev $DEV
        tc -s class ls dev $DEV
        ;;
*)
	echo "Usage: $0 {start | stop | restart}"
	;;
esac
exit $?