jamesstuff

view scripts/shaper @ 39:b005ac01417d

Virgin mobile credit checking
author James Bunton <jamesbunton@fastmail.fm>
date Tue Dec 02 02:33:07 2008 +1100 (2008-12-02)
parents
children 395cfd0dc08b
line source
1 #!/bin/bash
3 # Tweakables
4 IFACE="ppp0"
5 VOIP_HOST="203.2.134.1"
7 UPLINK_RATE=192
8 DOWNLINK_RATE=1024
9 R2Q=2
11 VOIP_RATE=64
12 HIGH_RATE=32
13 NORM_RATE=64
14 BULK_RATE=32
18 cd /
20 # Print status of classes
21 if [ "$1" = "status" ]
22 then
23 tc -s qdisc ls dev ${IFACE}
24 tc -s class ls dev ${IFACE}
25 exit
26 fi
29 # clean existing down- and uplink qdiscs, hide errors
30 tc qdisc del dev ${IFACE} root 2> /dev/null > /dev/null
31 tc qdisc del dev ${IFACE} ingress 2> /dev/null > /dev/null
33 if [ "$1" = "stop" ]
34 then
35 exit
36 fi
39 # Symbolic 'constants'
40 ROOT=1
41 LIMIT=1
42 VOIP_TRAFFIC=10
43 HIGH_TRAFFIC=20
44 NORM_TRAFFIC=30
45 BULK_TRAFFIC=40
49 ########## uplink #############
51 # install root HTB, point default traffic to NORM_TRAFFIC
52 tc qdisc add dev ${IFACE} \
53 root handle ${ROOT}:0 \
54 htb default ${NORM_TRAFFIC} r2q ${R2Q}
57 # LIMIT class shapes everything at $UPLINK_RATE speed
58 # this prevents huge queues in the DSL modem which destroy latency
59 tc class add dev ${IFACE} \
60 parent ${ROOT}:0 classid ${ROOT}:${LIMIT} \
61 htb rate ${UPLINK_RATE}Kbit ceil ${UPLINK_RATE}Kbit
64 # VoIP traffic class gets guaranteed bandwidth
65 tc class add dev ${IFACE} \
66 parent ${ROOT}:${LIMIT} classid ${ROOT}:${VOIP_TRAFFIC} \
67 htb rate ${VOIP_RATE}Kbit ceil $[3*${VOIP_RATE}/2]Kbit prio 0
69 # High priority traffic
70 tc class add dev ${IFACE} \
71 parent ${ROOT}:${LIMIT} classid ${ROOT}:${HIGH_TRAFFIC} \
72 htb rate ${HIGH_RATE}Kbit ceil ${UPLINK_RATE}Kbit prio 1
74 # Normal priority traffic
75 tc class add dev ${IFACE} \
76 parent ${ROOT}:${LIMIT} classid ${ROOT}:${NORM_TRAFFIC} \
77 htb rate ${NORM_RATE}Kbit ceil ${UPLINK_RATE}Kbit prio 2
79 # Bulk traffic gets little default allowance
80 tc class add dev ${IFACE} \
81 parent ${ROOT}:${LIMIT} classid ${ROOT}:${BULK_TRAFFIC} \
82 htb rate ${BULK_RATE}Kbit ceil ${UPLINK_RATE}Kbit prio 3
84 # Stochastic Fairness
85 tc qdisc add dev ${IFACE} \
86 parent ${ROOT}:${HIGH_TRAFFIC} handle ${HIGH_TRAFFIC}:0 \
87 sfq perturb 10
88 tc qdisc add dev ${IFACE} \
89 parent ${ROOT}:${NORM_TRAFFIC} handle ${NORM_TRAFFIC}:0 \
90 sfq perturb 10
91 tc qdisc add dev ${IFACE} \
92 parent ${ROOT}:${BULK_TRAFFIC} handle ${BULK_TRAFFIC}:0 \
93 sfq perturb 10
99 # Match VoIP traffic as highest priority
100 tc filter add dev ${IFACE} \
101 parent ${ROOT}:0 protocol ip prio 10 u32 \
102 match ip dst ${VOIP_HOST} flowid ${ROOT}:${VOIP_TRAFFIC}
104 # ICMP (ip protocol 1) in the HIGH_TRAFFIC class
105 tc filter add dev ${IFACE} \
106 parent ${ROOT}:0 protocol ip prio 10 u32 \
107 match ip protocol 1 0xff flowid ${ROOT}:${HIGH_TRAFFIC}
109 # TOS Minimum-Delay (eg ssh but not scp) in HIGH_TRAFFIC
110 tc filter add dev ${IFACE} \
111 parent ${ROOT}:0 protocol ip prio 10 u32 \
112 match ip tos 0x10 0xff flowid ${ROOT}:${HIGH_TRAFFIC}
114 # TOS Maximise-Throughput (eg rtorrent) in BULK_TRAFFIC
115 tc filter add dev ${IFACE} \
116 parent ${ROOT}:0 protocol ip prio 10 u32 \
117 match ip tos 0x08 0xff flowid ${ROOT}:${BULK_TRAFFIC}
119 # To speed up downloads while an upload is going on, ACK is HIGH_TRAFFIC
120 tc filter add dev ${IFACE} \
121 parent ${ROOT}:0 protocol ip prio 10 u32 \
122 match ip protocol 6 0xff \
123 match u8 0x05 0x0f at 0 \
124 match u16 0x0000 0xffc0 at 2 \
125 match u8 0x10 0xff at 33 \
126 flowid ${ROOT}:${HIGH_TRAFFIC}
130 ########## downlink #############
132 tc qdisc add dev ${IFACE} \
133 handle ffff: ingress
135 # Matching incoming VoIP traffic here so it isn't policed by the next rule
136 tc filter add dev ${IFACE} \
137 parent ffff: protocol ip prio 1 u32 \
138 match ip src ${VOIP_HOST}/32 \
139 flowid :1
141 # Police everything else - drop packets coming in faster than DOWNLINK_RATE
142 tc filter add dev ${IFACE} \
143 parent ffff: protocol ip prio 2 u32 \
144 match ip src 0.0.0.0/0 \
145 police rate ${DOWNLINK_RATE}Kbit burst 10k drop flowid :2