]> code.delx.au - monosys/blob - shaper
docker-cleanup: also delete things more than 1 year old
[monosys] / shaper
1 #!/bin/bash
2
3 # Docs: http://lartc.org/howto
4
5 # Tweakables
6 IFACE="ppp0"
7 VOIP_HOST="$(dig +short sip.internode.on.net)"
8
9 UPLINK_RATE=450
10
11 VOIP_RATE=70
12 HIGH_RATE=130
13 NORM_RATE=240
14 BULK_RATE=10
15
16
17 # Symbolic 'constants'
18 ROOT=1
19 LIMIT=1
20 VOIP_TRAFFIC=10
21 HIGH_TRAFFIC=20
22 NORM_TRAFFIC=30
23 BULK_TRAFFIC=40
24
25
26 # Print status of classes
27 if [ "$1" = "status" ]; then
28 tc -s qdisc ls dev ${IFACE}
29 tc -s class ls dev ${IFACE}
30 exit
31 fi
32
33 set -x
34
35 # clean existing down- and uplink qdiscs, hide errors
36 tc qdisc del dev ${IFACE} root 2> /dev/null > /dev/null
37 tc qdisc del dev ${IFACE} ingress 2> /dev/null > /dev/null
38
39 if [ "$1" = "stop" ]; then
40 exit
41 fi
42
43 cd /
44
45
46 ########## uplink #############
47
48 # install root HTB, point default traffic to NORM_TRAFFIC
49 tc qdisc add dev ${IFACE} \
50 root handle ${ROOT}:0 \
51 htb default ${NORM_TRAFFIC}
52
53
54 # LIMIT class shapes everything at $UPLINK_RATE speed
55 # this prevents huge queues in the DSL modem which destroy latency
56 tc class add dev ${IFACE} \
57 parent ${ROOT}:0 classid ${ROOT}:${LIMIT} \
58 htb rate ${UPLINK_RATE}Kbit ceil ${UPLINK_RATE}Kbit
59
60
61 # VoIP traffic class gets guaranteed bandwidth
62 tc class add dev ${IFACE} \
63 parent ${ROOT}:${LIMIT} classid ${ROOT}:${VOIP_TRAFFIC} \
64 htb rate ${VOIP_RATE}Kbit ceil ${UPLINK_RATE}Kbit prio 0
65
66 # High priority traffic
67 tc class add dev ${IFACE} \
68 parent ${ROOT}:${LIMIT} classid ${ROOT}:${HIGH_TRAFFIC} \
69 htb rate ${HIGH_RATE}Kbit ceil ${UPLINK_RATE}Kbit prio 1
70
71 # Normal priority traffic
72 tc class add dev ${IFACE} \
73 parent ${ROOT}:${LIMIT} classid ${ROOT}:${NORM_TRAFFIC} \
74 htb rate ${NORM_RATE}Kbit ceil ${UPLINK_RATE}Kbit prio 2
75
76 # Bulk traffic gets little default allowance
77 tc class add dev ${IFACE} \
78 parent ${ROOT}:${LIMIT} classid ${ROOT}:${BULK_TRAFFIC} \
79 htb rate ${BULK_RATE}Kbit ceil ${UPLINK_RATE}Kbit prio 3
80
81
82 # Stochastic Fairness
83 tc qdisc add dev ${IFACE} \
84 parent ${ROOT}:${HIGH_TRAFFIC} handle ${HIGH_TRAFFIC}:0 \
85 sfq perturb 10
86 tc qdisc add dev ${IFACE} \
87 parent ${ROOT}:${NORM_TRAFFIC} handle ${NORM_TRAFFIC}:0 \
88 sfq perturb 10
89 tc qdisc add dev ${IFACE} \
90 parent ${ROOT}:${BULK_TRAFFIC} handle ${BULK_TRAFFIC}:0 \
91 sfq perturb 10
92
93
94 # Match VoIP traffic as highest priority
95 tc filter add dev ${IFACE} \
96 parent ${ROOT}:0 protocol ip prio 10 u32 \
97 match ip dst ${VOIP_HOST} flowid ${ROOT}:${VOIP_TRAFFIC}
98
99 # ICMP in the HIGH_TRAFFIC class
100 tc filter add dev ${IFACE} \
101 parent ${ROOT}:0 protocol ip prio 10 u32 \
102 match ip protocol 1 0xff flowid ${ROOT}:${HIGH_TRAFFIC}
103
104 # To speed up downloads while an upload is going on, ACK is HIGH_TRAFFIC
105 tc filter add dev ${IFACE} \
106 parent ${ROOT}:0 protocol ip prio 10 u32 \
107 match ip protocol 6 0xff \
108 match u8 0x05 0x0f at 0 \
109 match u16 0x0000 0xffc0 at 2 \
110 match u8 0x10 0xff at 33 \
111 flowid ${ROOT}:${HIGH_TRAFFIC}
112
113 # TOS Minimum-Delay (eg ssh but not scp) in HIGH_TRAFFIC
114 tc filter add dev ${IFACE} \
115 parent ${ROOT}:0 protocol ip prio 10 u32 \
116 match ip tos 0x10 0xff flowid ${ROOT}:${HIGH_TRAFFIC}
117
118 # TOS Maximise-Throughput (eg rtorrent) in BULK_TRAFFIC
119 tc filter add dev ${IFACE} \
120 parent ${ROOT}:0 protocol ip prio 10 u32 \
121 match ip tos 0x08 0xff flowid ${ROOT}:${BULK_TRAFFIC}
122