linux-tor-prio.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/bin/bash
  2. # Written by 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.
  6. # The following configuration works well for a ~5Mbit tor node. It requires
  7. # that you place your Tor traffic on a separate IP from the rest of your
  8. # traffic.
  9. # BEGIN DEVICE PARAMETERS
  10. DEV=eth0
  11. BOX_IP=42.42.42.42
  12. TOR_IP=43.43.43.43
  13. # Average ping to most places on the net, milliseconds
  14. RTT_LATENCY=40
  15. # END DEVICE PARAMETERS
  16. # BEGIN UPLOAD PARAMETERS
  17. # RATE_UP must be less than your connection's upload capacity. If it is
  18. # larger, then the bottleneck will be at your router's queue, which you
  19. # do not control. This will cause congestion and a revert to normal TCP
  20. # fairness no matter what the queing priority is.
  21. RATE_UP=5000
  22. # RATE_UP_TOR is the minimum speed your Tor connections will have.
  23. # They will have at least this much bandwidth for upload
  24. RATE_UP_TOR=1500
  25. # RATE_UP_TOR_CEIL is the maximum rate allowed for all Tor trafic
  26. RATE_UP_TOR_CEIL=5000
  27. CHAIN=OUTPUT
  28. #CHAIN=PREROUTING
  29. #CHAIN=POSTROUTING
  30. # END UPLOAD PARAMETERS
  31. MTU=1500
  32. AVG_PKT=900
  33. # END USER TUNABLE PARAMETERS
  34. # The queue size should be no larger than your bandwidth-delay
  35. # product. This is RT latency*bandwidth/MTU/2
  36. BDP=$(expr $RTT_LATENCY \* $RATE_UP / $AVG_PKT)
  37. # Further research indicates that the BDP calculations should use
  38. # RTT/sqrt(n) where n is the expected number of active connections..
  39. BDP=$(expr $BDP / 4)
  40. if [ "$1" = "status" ]
  41. then
  42. echo "[qdisc]"
  43. tc -s qdisc show dev $DEV
  44. tc -s qdisc show dev imq0
  45. echo "[class]"
  46. tc -s class show dev $DEV
  47. tc -s class show dev imq0
  48. echo "[filter]"
  49. tc -s filter show dev $DEV
  50. tc -s filter show dev imq0
  51. echo "[iptables]"
  52. iptables -t mangle -L TORSHAPER-OUT -v -x 2> /dev/null
  53. exit
  54. fi
  55. # Reset everything to a known state (cleared)
  56. tc qdisc del dev $DEV root 2> /dev/null > /dev/null
  57. tc qdisc del dev imq0 root 2> /dev/null > /dev/null
  58. iptables -t mangle -D POSTROUTING -o $DEV -j TORSHAPER-OUT 2> /dev/null > /dev/null
  59. iptables -t mangle -D PREROUTING -o $DEV -j TORSHAPER-OUT 2> /dev/null > /dev/null
  60. iptables -t mangle -D OUTPUT -o $DEV -j TORSHAPER-OUT 2> /dev/null > /dev/null
  61. iptables -t mangle -F TORSHAPER-OUT 2> /dev/null > /dev/null
  62. iptables -t mangle -X TORSHAPER-OUT 2> /dev/null > /dev/null
  63. ip link set imq0 down 2> /dev/null > /dev/null
  64. rmmod imq 2> /dev/null > /dev/null
  65. if [ "$1" = "stop" ]
  66. then
  67. echo "Shaping removed on $DEV."
  68. exit
  69. fi
  70. # Outbound Shaping (limits total bandwidth to RATE_UP)
  71. ip link set dev $DEV qlen $BDP
  72. # Add HTB root qdisc, default is high prio
  73. tc qdisc add dev $DEV root handle 1: htb default 20
  74. # Add main rate limit class
  75. tc class add dev $DEV parent 1: classid 1:1 htb rate ${RATE_UP}kbit
  76. # Create the two classes, giving Tor at least RATE_UP_TOR kbit and capping
  77. # total upstream at RATE_UP so the queue is under our control.
  78. 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
  79. 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
  80. # Start up pfifo
  81. tc qdisc add dev $DEV parent 1:20 handle 20: pfifo limit $BDP
  82. tc qdisc add dev $DEV parent 1:21 handle 21: pfifo limit $BDP
  83. # filter traffic into classes by fwmark
  84. tc filter add dev $DEV parent 1:0 prio 0 protocol ip handle 20 fw flowid 1:20
  85. tc filter add dev $DEV parent 1:0 prio 0 protocol ip handle 21 fw flowid 1:21
  86. # add TORSHAPER-OUT chain to the mangle table in iptables
  87. iptables -t mangle -N TORSHAPER-OUT
  88. iptables -t mangle -I $CHAIN -o $DEV -j TORSHAPER-OUT
  89. # Set firewall marks
  90. # Low priority to Tor IP
  91. iptables -t mangle -A TORSHAPER-OUT -s $TOR_IP -j MARK --set-mark 21
  92. # High prio for everything else
  93. # Don't bother to use BOX_IP. Box probably has other IPs too...
  94. #iptables -t mangle -A TORSHAPER-OUT -s $BOX_IP -j MARK --set-mark 20
  95. iptables -t mangle -A TORSHAPER-OUT -m mark --mark 0 -j MARK --set-mark 20
  96. echo "Outbound shaping added to $DEV. Rate for Tor upload at least: ${RATE_UP_TOR}Kbyte/sec."