linux-tor-prio.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.
  6. # BEGIN USER TUNABLE PARAMETERS
  7. DEV=eth0
  8. # NOTE! You must START Tor under this UID. Using the Tor User/Group
  9. # config setting is NOT sufficient.
  10. TOR_UID=$(id -u tor)
  11. # If the UID mechanism doesn't work for you, you can set this parameter
  12. # instead. If set, it will take precedence over the UID setting. Note that
  13. # you need multiple IPs for this to work.
  14. #TOR_IP="42.42.42.42"
  15. # Average ping to most places on the net, milliseconds
  16. RTT_LATENCY=40
  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. MTU=1500
  31. AVG_PKT=900
  32. # END USER TUNABLE PARAMETERS
  33. # The queue size should be no larger than your bandwidth-delay
  34. # product. This is RT latency*bandwidth/MTU/2
  35. BDP=$(expr $RTT_LATENCY \* $RATE_UP / $AVG_PKT)
  36. # Further research indicates that the BDP calculations should use
  37. # RTT/sqrt(n) where n is the expected number of active connections..
  38. BDP=$(expr $BDP / 4)
  39. if [ "$1" = "status" ]
  40. then
  41. echo "[qdisc]"
  42. tc -s qdisc show dev $DEV
  43. tc -s qdisc show dev imq0
  44. echo "[class]"
  45. tc -s class show dev $DEV
  46. tc -s class show dev imq0
  47. echo "[filter]"
  48. tc -s filter show dev $DEV
  49. tc -s filter show dev imq0
  50. echo "[iptables]"
  51. iptables -t mangle -L TORSHAPER-OUT -v -x 2> /dev/null
  52. exit
  53. fi
  54. # Reset everything to a known state (cleared)
  55. tc qdisc del dev $DEV root 2> /dev/null > /dev/null
  56. tc qdisc del dev imq0 root 2> /dev/null > /dev/null
  57. iptables -t mangle -D POSTROUTING -o $DEV -j TORSHAPER-OUT 2> /dev/null > /dev/null
  58. iptables -t mangle -D PREROUTING -o $DEV -j TORSHAPER-OUT 2> /dev/null > /dev/null
  59. iptables -t mangle -D OUTPUT -o $DEV -j TORSHAPER-OUT 2> /dev/null > /dev/null
  60. iptables -t mangle -F TORSHAPER-OUT 2> /dev/null > /dev/null
  61. iptables -t mangle -X TORSHAPER-OUT 2> /dev/null > /dev/null
  62. ip link set imq0 down 2> /dev/null > /dev/null
  63. rmmod imq 2> /dev/null > /dev/null
  64. if [ "$1" = "stop" ]
  65. then
  66. echo "Shaping removed on $DEV."
  67. exit
  68. fi
  69. # Outbound Shaping (limits total bandwidth to RATE_UP)
  70. ip link set dev $DEV qlen $BDP
  71. # Add HTB root qdisc, default is high prio
  72. tc qdisc add dev $DEV root handle 1: htb default 20
  73. # Add main rate limit class
  74. tc class add dev $DEV parent 1: classid 1:1 htb rate ${RATE_UP}kbit
  75. # Create the two classes, giving Tor at least RATE_UP_TOR kbit and capping
  76. # total upstream at RATE_UP so the queue is under our control.
  77. 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
  78. 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
  79. # Start up pfifo
  80. tc qdisc add dev $DEV parent 1:20 handle 20: pfifo limit $BDP
  81. tc qdisc add dev $DEV parent 1:21 handle 21: pfifo limit $BDP
  82. # filter traffic into classes by fwmark
  83. tc filter add dev $DEV parent 1:0 prio 0 protocol ip handle 20 fw flowid 1:20
  84. tc filter add dev $DEV parent 1:0 prio 0 protocol ip handle 21 fw flowid 1:21
  85. # add TORSHAPER-OUT chain to the mangle table in iptables
  86. iptables -t mangle -N TORSHAPER-OUT
  87. iptables -t mangle -I $CHAIN -o $DEV -j TORSHAPER-OUT
  88. # Set firewall marks
  89. # Low priority to Tor
  90. if [ ""$TOR_IP == "" ]
  91. then
  92. echo "Using UID-based QoS. UID $TOR_UID marked as low priority."
  93. iptables -t mangle -A TORSHAPER-OUT -m owner --uid-owner $TOR_UID -j MARK --set-mark 21
  94. else
  95. echo "Using IP-based QoS. $TOR_IP marked as low priority."
  96. iptables -t mangle -A TORSHAPER-OUT -s $TOR_IP -j MARK --set-mark 21
  97. fi
  98. # High prio for everything else
  99. iptables -t mangle -A TORSHAPER-OUT -m mark --mark 0 -j MARK --set-mark 20
  100. echo "Outbound shaping added to $DEV. Rate for Tor upload at least: ${RATE_UP_TOR}Kbyte/sec."