#!/bin/bash
# Written by Dan Singletary (8/7/02)
# Modified by Mike Perry (11/04/04) 

# NOTE: The following configuration works well for a ~5Mbit tor node

# BEGIN DEVICE PARAMETERS

DEV=eth0
BOX_IP=42.42.42.42
TOR_IP=43.43.43.43

# Average ping to most places on the net, milliseconds
RTT_LATENCY=40

# END DEVICE PARAMETERS
# BEGIN UPLOAD PARAMETERS

# RATE_UP must be less than your connection's upload capacity. If it is
# larger, then the bottleneck will be at your router's queue, which you
# do not control. This will cause latency no matter what the queing priority 
# is.
RATE_UP=5000

# RATE_UP_TOR is the minimum speed your Tor connections will have. 
# They will have at least this much bandwidth for upload
RATE_UP_TOR=1500

# RATE_UP_TOR_CEIL is the maximum rate allowed for all Tor trafic
RATE_UP_TOR_CEIL=5000

CHAIN=OUTPUT
#CHAIN=PREROUTING
#CHAIN=POSTROUTING

# END UPLOAD PARAMETERS

MTU=1500
AVG_PKT=900

# END USER TUNABLE PARAMETERS

# Whatever you do, do NOT enable fair queuing. It is horrible for Tor. When
# each connection has its own queue, lots of connections will drive your
# latency for non-TOR stuff through the roof. This is bad.  (Google for head
# of line blocking for more info)

# The queue size should be no larger than your bandwidth-delay
# product. This is RT latency*bandwidth/MTU/2

BDP=$(expr $RTT_LATENCY \* $RATE_UP / $AVG_PKT) 

# Further research indicates that the BDP calculations should use
# RTT/sqrt(n) where n is the expected number of connections..

BDP=$(expr $BDP / 4)

# 
# End Configuration Options
#


if [ "$1" = "status" ]
then
        echo "[qdisc]"
        tc -s qdisc show dev $DEV
        tc -s qdisc show dev imq0
        echo "[class]"
        tc -s class show dev $DEV
        tc -s class show dev imq0
        echo "[filter]"
        tc -s filter show dev $DEV
        tc -s filter show dev imq0
        echo "[iptables]"
        iptables -t mangle -L MYSHAPER-OUT -v -x 2> /dev/null
        iptables -t mangle -L MYSHAPER-IN -v -x 2> /dev/null
        exit
fi


# Reset everything to a known state (cleared)
tc qdisc del dev $DEV root    2> /dev/null > /dev/null
tc qdisc del dev imq0 root 2> /dev/null > /dev/null
iptables -t mangle -D POSTROUTING -o $DEV -j MYSHAPER-OUT 2> /dev/null > /dev/null
iptables -t mangle -D PREROUTING -o $DEV -j MYSHAPER-OUT 2> /dev/null > /dev/null
iptables -t mangle -D $CHAIN -o $DEV -j MYSHAPER-OUT 2> /dev/null > /dev/null
iptables -t mangle -F MYSHAPER-OUT 2> /dev/null > /dev/null
iptables -t mangle -X MYSHAPER-OUT 2> /dev/null > /dev/null
iptables -t mangle -D PREROUTING -i $DEV -j MYSHAPER-IN 2> /dev/null > /dev/null
iptables -t mangle -F MYSHAPER-IN 2> /dev/null > /dev/null
iptables -t mangle -X MYSHAPER-IN 2> /dev/null > /dev/null
ip link set imq0 down 2> /dev/null > /dev/null
rmmod imq 2> /dev/null > /dev/null


if [ "$1" = "stop" ] 
then 
        echo "Shaping removed on $DEV."
        exit
fi


###########################################################
#
# Outbound Shaping (limits total bandwidth to RATE_UP)

ip link set dev $DEV qlen $BDP

# add HTB root qdisc, default is low prio
tc qdisc add dev $DEV root handle 1: htb default 21

# add main rate limit classes
tc class add dev $DEV parent 1: classid 1:1 htb rate ${RATE_UP}kbit

# add leaf classes - We grant each class at LEAST it's "fair share" of
#                    bandwidth.this way no class will ever be starved by 
#                    another class. Each class is also permitted to 
#                    consume all of the available bandwidth if no other 
#                    classes are in use.
tc class add dev $DEV parent 1:1 classid 1:20 htb rate $(expr $RATE_UP - $RATE_UP_TOR)kbit ceil ${RATE_UP}kbit prio 0
tc class add dev $DEV parent 1:1 classid 1:21 htb rate $[$RATE_UP_TOR]kbit ceil ${RATE_UP_TOR_CEIL}kbit prio 10

# Start up pfifo
tc qdisc add dev $DEV parent 1:20 handle 20: pfifo limit $BDP
tc qdisc add dev $DEV parent 1:21 handle 21: pfifo limit $BDP

# filter traffic into classes by fwmark - here we direct traffic into priority
# class according to
#                                         the fwmark set on the packet (we set
#                                         fwmark with iptables
#                                         later).  Note that above we've set
#                                         the default priority
#                                         class to 1:21 so unmarked packets
#                                         (or packets marked with
#                                         unfamiliar IDs) will be defaulted to
#                                         the lowest priority
#                                         class.
tc filter add dev $DEV parent 1:0 prio 0 protocol ip handle 20 fw flowid 1:20
tc filter add dev $DEV parent 1:0 prio 0 protocol ip handle 21 fw flowid 1:21


# add MYSHAPER-OUT chain to the mangle table in iptables - this sets up the
# table we'll use
#                                                      to filter and mark
#                                                      packets.
iptables -t mangle -N MYSHAPER-OUT
iptables -t mangle -I $CHAIN -o $DEV -j MYSHAPER-OUT


# add fwmark entries to classify different types of traffic - Set fwmark from
# 20-21 according to
#                                                             desired class.
#                                                             20 is highest
#                                                             prio.

# Low priority to Tor IP
iptables -t mangle -A MYSHAPER-OUT -s $TOR_IP -j MARK --set-mark 21

# High prio for everything else
#iptables -t mangle -A MYSHAPER-OUT -s $BOX_IP -j MARK --set-mark 20 
iptables -t mangle -A MYSHAPER-OUT -m mark --mark 0 -j MARK --set-mark 20


# Done with outbound shaping
#
####################################################


echo "Outbound shaping added to $DEV.  Rate for Tor upload at least: ${RATE_UP_TOR}Kbyte/sec."

