#!/bin/bash # Traffic Shaper -- with inspiration from Wondershaper/LARTC Cookbooks TC=/sbin/tc # desired traffic speed TOPLAYERS=5120 # Interface to use DEV=eth0 # The port that squid uses CACHEPORTS="2235 80" if [ "$1" = "status" ] then $TC -s qdisc ls dev $DEV $TC -s class ls dev $DEV exit fi # 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 if [ "$1" = "stop" ] then exit fi # # Uplink # # # Disciplines # # install root HTB $TC qdisc add dev $DEV root handle 1: htb default 20 # set the maximum throughput to our definition (1:1) $TC class add dev $DEV parent 1: classid 1:1 htb rate $[$TOPLAYERS+2048]kbit \ burst 6k # this is the highest priority traffic for interactive work (1:10) $TC class add dev $DEV parent 1:1 classid 1:10 htb rate $[$TOPLAYERS+2048]kbit \ burst 6k prio 1 # bulk and default class (1:20) is the default class $TC class add dev $DEV parent 1:1 classid 1:20 htb rate $[$TOPLAYERS+1024]kbit \ burst 6k prio 2 # the long-term traffic hogs are in this class (1:30) and get the lowest rate $TC class add dev $DEV parent 1:1 classid 1:30 htb rate ${TOPLAYERS}kbit \ burst 6k prio 2 # stochatic fairness will try to service all consumers of our network $TC qdisc add dev $DEV parent 1:10 handle 10: sfq perturb 10 $TC qdisc add dev $DEV parent 1:20 handle 20: sfq perturb 10 $TC qdisc add dev $DEV parent 1:30 handle 30: sfq perturb 10 # # Filters # # TOS Minimum Delay $TC filter add dev $DEV parent 1:0 protocol ip prio 10 u32 \ match ip tos 0x10 0xff flowid 1:10 # ICMP (ip protocol 1) in the interactive class 1:10 $TC filter add dev $DEV parent 1:0 protocol ip prio 10 u32 \ match ip protocol 1 0xff flowid 1:10 # prioritize ACK packets $TC filter add dev $DEV parent 1: protocol ip prio 10 u32 \ match ip protocol 6 0xff \ match u8 0x05 0x0f at 0 \ match u16 0x0000 0xffc0 at 2 \ match u8 0x10 0xff at 33 \ flowid 1:10 # prioritize small packets (<64 bytes) tc filter add dev $DEV parent 1: protocol ip prio 12 u32 \ match ip protocol 6 0xff \ match u8 0x05 0x0f at 0 \ match u16 0x0000 0xffc0 at 2 \ flowid 1:10 # give cache traffic a slower time for a in $CACHEPORTS do $TC filter add dev $DEV parent 1: protocol ip prio 15 u32 \ match ip sport $a 0xffff flowid 1:30 done # rest is 'non-interactive' ie 'bulk' and ends up in 1:20 $TC filter add dev $DEV parent 1: protocol ip prio 18 u32 \ match ip dst 0.0.0.0/0 flowid 1:20