tweaks
[tor.git] / contrib / linux-tor-prio.sh
blob6451de8f6b3594ba1b98add4a90a0c674101f189
1 #!/bin/bash
2 # Written by Marco Bonetti & Mike Perry
3 # Based on instructions from Dan Singletary's ADSL Bandwidth Management HOWTO
4 # http://www.faqs.org/docs/Linux-HOWTO/ADSL-Bandwidth-Management-HOWTO.html
5 # This script is Public Domain.
7 # BEGIN USER TUNABLE PARAMETERS
9 DEV=eth0
11 # NOTE! You must START Tor under this UID. Using the Tor User/Group
12 # config setting is NOT sufficient.
13 TOR_UID=$(id -u tor)
15 # If the UID mechanism doesn't work for you, you can set this parameter
16 # instead. If set, it will take precedence over the UID setting. Note that
17 # you need multiple IPs for this to work.
18 #TOR_IP="42.42.42.42"
20 # Average ping to most places on the net, milliseconds
21 RTT_LATENCY=40
23 # RATE_UP must be less than your connection's upload capacity. If it is
24 # larger, then the bottleneck will be at your router's queue, which you
25 # do not control. This will cause congestion and a revert to normal TCP
26 # fairness no matter what the queing priority is.
27 RATE_UP=5000
29 # RATE_UP_TOR is the minimum speed your Tor connections will have.
30 # They will have at least this much bandwidth for upload
31 RATE_UP_TOR=1500
33 # RATE_UP_TOR_CEIL is the maximum rate allowed for all Tor trafic
34 RATE_UP_TOR_CEIL=5000
36 CHAIN=OUTPUT
37 #CHAIN=PREROUTING
38 #CHAIN=POSTROUTING
40 MTU=1500
41 AVG_PKT=900
43 # END USER TUNABLE PARAMETERS
45 # The queue size should be no larger than your bandwidth-delay
46 # product. This is RT latency*bandwidth/MTU/2
48 BDP=$(expr $RTT_LATENCY \* $RATE_UP / $AVG_PKT)
50 # Further research indicates that the BDP calculations should use
51 # RTT/sqrt(n) where n is the expected number of active connections..
53 BDP=$(expr $BDP / 4)
55 if [ "$1" = "status" ]
56 then
57 echo "[qdisc]"
58 tc -s qdisc show dev $DEV
59 tc -s qdisc show dev imq0
60 echo "[class]"
61 tc -s class show dev $DEV
62 tc -s class show dev imq0
63 echo "[filter]"
64 tc -s filter show dev $DEV
65 tc -s filter show dev imq0
66 echo "[iptables]"
67 iptables -t mangle -L TORSHAPER-OUT -v -x 2> /dev/null
68 exit
72 # Reset everything to a known state (cleared)
73 tc qdisc del dev $DEV root 2> /dev/null > /dev/null
74 tc qdisc del dev imq0 root 2> /dev/null > /dev/null
75 iptables -t mangle -D POSTROUTING -o $DEV -j TORSHAPER-OUT 2> /dev/null > /dev/null
76 iptables -t mangle -D PREROUTING -o $DEV -j TORSHAPER-OUT 2> /dev/null > /dev/null
77 iptables -t mangle -D OUTPUT -o $DEV -j TORSHAPER-OUT 2> /dev/null > /dev/null
78 iptables -t mangle -F TORSHAPER-OUT 2> /dev/null > /dev/null
79 iptables -t mangle -X TORSHAPER-OUT 2> /dev/null > /dev/null
80 ip link set imq0 down 2> /dev/null > /dev/null
81 rmmod imq 2> /dev/null > /dev/null
83 if [ "$1" = "stop" ]
84 then
85 echo "Shaping removed on $DEV."
86 exit
89 # Outbound Shaping (limits total bandwidth to RATE_UP)
91 ip link set dev $DEV qlen $BDP
93 # Add HTB root qdisc, default is high prio
94 tc qdisc add dev $DEV root handle 1: htb default 20
96 # Add main rate limit class
97 tc class add dev $DEV parent 1: classid 1:1 htb rate ${RATE_UP}kbit
99 # Create the two classes, giving Tor at least RATE_UP_TOR kbit and capping
100 # total upstream at RATE_UP so the queue is under our control.
101 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
102 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
104 # Start up pfifo
105 tc qdisc add dev $DEV parent 1:20 handle 20: pfifo limit $BDP
106 tc qdisc add dev $DEV parent 1:21 handle 21: pfifo limit $BDP
108 # filter traffic into classes by fwmark
109 tc filter add dev $DEV parent 1:0 prio 0 protocol ip handle 20 fw flowid 1:20
110 tc filter add dev $DEV parent 1:0 prio 0 protocol ip handle 21 fw flowid 1:21
112 # add TORSHAPER-OUT chain to the mangle table in iptables
113 iptables -t mangle -N TORSHAPER-OUT
114 iptables -t mangle -I $CHAIN -o $DEV -j TORSHAPER-OUT
117 # Set firewall marks
118 # Low priority to Tor
119 if [ ""$TOR_IP == "" ]
120 then
121 echo "Using UID-based QoS. UID $TOR_UID marked as low priority."
122 iptables -t mangle -A TORSHAPER-OUT -m owner --uid-owner $TOR_UID -j MARK --set-mark 21
123 else
124 echo "Using IP-based QoS. $TOR_IP marked as low priority."
125 iptables -t mangle -A TORSHAPER-OUT -s $TOR_IP -j MARK --set-mark 21
128 # High prio for everything else
129 iptables -t mangle -A TORSHAPER-OUT -m mark --mark 0 -j MARK --set-mark 20
131 echo "Outbound shaping added to $DEV. Rate for Tor upload at least: ${RATE_UP_TOR}Kbyte/sec."