linux-tor-prio.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/bin/bash
  2. # Written by Marco Bonetti & Mike Perry
  3. # Based on instructions from Dan Singletary's ADSL BW Management HOWTO:
  4. # http://www.faqs.org/docs/Linux-HOWTO/ADSL-Bandwidth-Management-HOWTO.html
  5. # This script is Public Domain.
  6. ############################### README #################################
  7. # This script provides prioritization of Tor traffic below other
  8. # traffic on a Linux server. It has two modes of operation: UID based
  9. # and IP based. The UID based method requires that Tor be launched from
  10. # a specific user ID. The "User" and "Group" Tor config settings are
  11. # insufficient, as they set the UID after the socket is created.
  12. # Here is a three line C wrapper you can use to execute Tor and drop
  13. # privs to UID 501 before it creates any sockets. Change the UID
  14. # to the UID for your tor server user, and compile with
  15. # 'gcc tor_wrap.c -o tor_wrap':
  16. # #include <unistd.h>
  17. # int main(int argc, char **argv) {
  18. # if(setresuid(501, 501, 501) == -1) { perror("setresuid"); return 1; }
  19. # execl("/bin/tor", "/bin/tor", "-f", "/etc/tor/torrc", NULL);
  20. # perror("execl"); return 1;
  21. # }
  22. # The IP setting requires that a separate IP address be dedicated to Tor.
  23. # Your Torrc should be set to bind to this IP for "OutboundBindAddress",
  24. # "ListenAddress", and "Address".
  25. # You should also tune the individual connection rate parameters below
  26. # to your individual connection. In particular, you should leave *some*
  27. # minimum amount of bandwidth for Tor, so that Tor users are not
  28. # completely choked out when you use your server's bandwidth. 30% is
  29. # probably a polite choice.
  30. # To start the shaping, run it as:
  31. # ./linux-tor-prio.sh
  32. # To get status information (useful to verify packets are getting marked
  33. # and prioritized), run:
  34. # ./linux-tor-prio.sh status
  35. # And to stop prioritization:
  36. # ./linux-tor-prio.sh stop
  37. ########################################################################
  38. # BEGIN USER TUNABLE PARAMETERS
  39. DEV=eth0
  40. # NOTE! You must START Tor under this UID. Using the Tor User/Group
  41. # config setting is NOT sufficient.
  42. TOR_UID=$(id -u tor)
  43. # If the UID mechanism doesn't work for you, you can set this parameter
  44. # instead. If set, it will take precedence over the UID setting. Note that
  45. # you need multiple IPs for this to work.
  46. #TOR_IP="42.42.42.42"
  47. # Average ping to most places on the net, milliseconds
  48. RTT_LATENCY=40
  49. # RATE_UP must be less than your connection's upload capacity. If it is
  50. # larger, then the bottleneck will be at your router's queue, which you
  51. # do not control. This will cause congestion and a revert to normal TCP
  52. # fairness no matter what the queing priority is.
  53. RATE_UP=5000
  54. # RATE_UP_TOR is the minimum speed your Tor connections will have.
  55. # They will have at least this much bandwidth for upload. In general,
  56. # you probably shouldn't set this too low, or else Tor users who use
  57. # your node will be completely choked out whenever your machine
  58. # does any other network activity. That is not very fun.
  59. RATE_UP_TOR=1500
  60. # RATE_UP_TOR_CEIL is the maximum rate allowed for all Tor trafic
  61. RATE_UP_TOR_CEIL=5000
  62. CHAIN=OUTPUT
  63. #CHAIN=PREROUTING
  64. #CHAIN=POSTROUTING
  65. MTU=1500
  66. AVG_PKT=900 # should be more like 600 for non-exit nodes
  67. # END USER TUNABLE PARAMETERS
  68. # The queue size should be no larger than your bandwidth-delay
  69. # product. This is RT latency*bandwidth/MTU/2
  70. BDP=$(expr $RTT_LATENCY \* $RATE_UP / $AVG_PKT)
  71. # Further research indicates that the BDP calculations should use
  72. # RTT/sqrt(n) where n is the expected number of active connections..
  73. BDP=$(expr $BDP / 4)
  74. if [ "$1" = "status" ]
  75. then
  76. echo "[qdisc]"
  77. tc -s qdisc show dev $DEV
  78. tc -s qdisc show dev imq0
  79. echo "[class]"
  80. tc -s class show dev $DEV
  81. tc -s class show dev imq0
  82. echo "[filter]"
  83. tc -s filter show dev $DEV
  84. tc -s filter show dev imq0
  85. echo "[iptables]"
  86. iptables -t mangle -L TORSHAPER-OUT -v -x 2> /dev/null
  87. exit
  88. fi
  89. # Reset everything to a known state (cleared)
  90. tc qdisc del dev $DEV root 2> /dev/null > /dev/null
  91. tc qdisc del dev imq0 root 2> /dev/null > /dev/null
  92. iptables -t mangle -D POSTROUTING -o $DEV -j TORSHAPER-OUT 2> /dev/null > /dev/null
  93. iptables -t mangle -D PREROUTING -o $DEV -j TORSHAPER-OUT 2> /dev/null > /dev/null
  94. iptables -t mangle -D OUTPUT -o $DEV -j TORSHAPER-OUT 2> /dev/null > /dev/null
  95. iptables -t mangle -F TORSHAPER-OUT 2> /dev/null > /dev/null
  96. iptables -t mangle -X TORSHAPER-OUT 2> /dev/null > /dev/null
  97. ip link set imq0 down 2> /dev/null > /dev/null
  98. rmmod imq 2> /dev/null > /dev/null
  99. if [ "$1" = "stop" ]
  100. then
  101. echo "Shaping removed on $DEV."
  102. exit
  103. fi
  104. # Outbound Shaping (limits total bandwidth to RATE_UP)
  105. ip link set dev $DEV qlen $BDP
  106. # Add HTB root qdisc, default is high prio
  107. tc qdisc add dev $DEV root handle 1: htb default 20
  108. # Add main rate limit class
  109. tc class add dev $DEV parent 1: classid 1:1 htb rate ${RATE_UP}kbit
  110. # Create the two classes, giving Tor at least RATE_UP_TOR kbit and capping
  111. # total upstream at RATE_UP so the queue is under our control.
  112. 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
  113. 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
  114. # Start up pfifo
  115. tc qdisc add dev $DEV parent 1:20 handle 20: pfifo limit $BDP
  116. tc qdisc add dev $DEV parent 1:21 handle 21: pfifo limit $BDP
  117. # filter traffic into classes by fwmark
  118. tc filter add dev $DEV parent 1:0 prio 0 protocol ip handle 20 fw flowid 1:20
  119. tc filter add dev $DEV parent 1:0 prio 0 protocol ip handle 21 fw flowid 1:21
  120. # add TORSHAPER-OUT chain to the mangle table in iptables
  121. iptables -t mangle -N TORSHAPER-OUT
  122. iptables -t mangle -I $CHAIN -o $DEV -j TORSHAPER-OUT
  123. # Set firewall marks
  124. # Low priority to Tor
  125. if [ ""$TOR_IP == "" ]
  126. then
  127. echo "Using UID-based QoS. UID $TOR_UID marked as low priority."
  128. iptables -t mangle -A TORSHAPER-OUT -m owner --uid-owner $TOR_UID -j MARK --set-mark 21
  129. else
  130. echo "Using IP-based QoS. $TOR_IP marked as low priority."
  131. iptables -t mangle -A TORSHAPER-OUT -s $TOR_IP -j MARK --set-mark 21
  132. fi
  133. # High prio for everything else
  134. iptables -t mangle -A TORSHAPER-OUT -m mark --mark 0 -j MARK --set-mark 20
  135. echo "Outbound shaping added to $DEV. Rate for Tor upload at least: ${RATE_UP_TOR}Kbyte/sec."