123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- #!/bin/bash
- DEV=eth0
- TOR_UID=$(id -u tor)
- RTT_LATENCY=40
- RATE_UP=5000
- RATE_UP_TOR=1500
- RATE_UP_TOR_CEIL=5000
- CHAIN=OUTPUT
- MTU=1500
- AVG_PKT=900
- BDP=$(expr $RTT_LATENCY \* $RATE_UP / $AVG_PKT)
- BDP=$(expr $BDP / 4)
- 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 TORSHAPER-OUT -v -x 2> /dev/null
- exit
- fi
- 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 TORSHAPER-OUT 2> /dev/null > /dev/null
- iptables -t mangle -D PREROUTING -o $DEV -j TORSHAPER-OUT 2> /dev/null > /dev/null
- iptables -t mangle -D OUTPUT -o $DEV -j TORSHAPER-OUT 2> /dev/null > /dev/null
- iptables -t mangle -F TORSHAPER-OUT 2> /dev/null > /dev/null
- iptables -t mangle -X TORSHAPER-OUT 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
- ip link set dev $DEV qlen $BDP
- tc qdisc add dev $DEV root handle 1: htb default 20
- tc class add dev $DEV parent 1: classid 1:1 htb rate ${RATE_UP}kbit
- 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
- 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
- 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
- iptables -t mangle -N TORSHAPER-OUT
- iptables -t mangle -I $CHAIN -o $DEV -j TORSHAPER-OUT
- if [ ""$TOR_IP == "" ]
- then
- echo "Using UID-based QoS. UID $TOR_UID marked as low priority."
- iptables -t mangle -A TORSHAPER-OUT -m owner --uid-owner $TOR_UID -j MARK --set-mark 21
- else
- echo "Using IP-based QoS. $TOR_IP marked as low priority."
- iptables -t mangle -A TORSHAPER-OUT -s $TOR_IP -j MARK --set-mark 21
- fi
- iptables -t mangle -A TORSHAPER-OUT -m mark --mark 0 -j MARK --set-mark 20
- echo "Outbound shaping added to $DEV. Rate for Tor upload at least: ${RATE_UP_TOR}Kbyte/sec."
|