#!/bin/bash

# Example of QOS based on DiffServ
#
# Marlon Dutra
# June 22nd, 2009
#
# $Id: qos-diffserv.sh 2044 2009-06-23 01:12:18Z marlon $

# This the total measured uplink
# Hint: get the accurate value by using iperf
# Hint 2: Set UPLINK 90% of your total link
UPLINK=300

# The WAN device
#
# If it's a PPP device, make sure you run this script *after* the ppp session
# is up and the device is all set, e.g. through /etc/ppp/ip-up.d
DEV=eth0

# begin from scratch
tc qdisc del dev $DEV root    2> /dev/null > /dev/null

# root qdisc and shift the last 2 bits from ToS
tc qdisc add dev $DEV handle 1:0 root dsmark indices 64 set_tc_index
tc filter add dev $DEV parent 1:0 protocol ip prio 1 tcindex mask 0xfc shift 2

# Main htb qdisc & class
tc qdisc add dev $DEV parent 1:0 handle 2:0 htb
tc class add dev $DEV parent 2:0 classid 2:1 htb rate ${UPLINK}Kbit ceil ${UPLINK}Kbit

# EF Class (2:10) - 30% reserved - highest priority
tc class add dev $DEV parent 2:1 classid 2:10 htb rate $[ 3 * $UPLINK / 10 ]Kbit ceil ${UPLINK}Kbit prio 1
tc qdisc add dev $DEV parent 2:10 pfifo limit 5
tc filter add dev $DEV parent 2:0 protocol ip prio 1 handle 0x2e tcindex classid 2:10 pass_on

# BE (best-effort) Class (2:20) - 30% reserved - lower priority
tc class add dev $DEV parent 2:1 classid 2:20 htb rate $[ 3 * $UPLINK / 10 ]Kbit ceil ${UPLINK}Kbit prio 10
tc qdisc add dev $DEV parent 2:20 red limit 60KB min 15KB max 45KB burst 20 avpkt 1000 bandwidth ${UPLINK}Kbit probability 0.02
tc filter add dev $DEV parent 2:0 protocol ip prio 2 handle 0 tcindex mask 0 classid 2:20 pass_on
