forked from dansiemon/linux-qos-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
src-3tos.sh
executable file
·461 lines (379 loc) · 13.3 KB
/
src-3tos.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#!/bin/bash
##
# Dan Siemon <[email protected]>
# http://www.coverfire.com
#
# License: Affero GPLv3
#
# This script is designed to be used on router placed before a bottleneck link.
# Set the rate to be slightly below the bottleneck rate so that the router
# owns the queue. That is, there is no queueing in the DSL or cable device.
#
# This script attempts to create per-host fairness on the network
# and for each host three priority classes. Per-host fairness is created
# by having NUM_HOST_BUCKETS classes and hashing hosts across them. Set
# NUM_HOST_BUCKETS to something sane for your network.
#
# Experimental results can be found at:
# https://www.coverfire.com/archives/2013/01/01/improving-my-home-internet-performance/
#
# The hierarchy looks like:
#
# ASCII:
#
# Interface
# |
# HTB 1:1
# / \
# Host Bucket 1 .. NUM_HOST_BUCKETS [Classes 1:10-1:(10+NUM_HOST_BUCKETS)]
# |
# DRR
# / | \
# High Normal Low [DRR: With three classes]
# |
# Leaf QDisc [Choose the type of the leaf QDisc below]
#
# The tree is created and the QDiscs are named in depth first order.
#
######################
# Config
######################
#TC="/usr/local/sbin/tc"
TC=`which tc`
#_DEBUG="on"
#_CDEBUG="on"
DEVICE="ppp0"
# The number of host buckets. All hosts are hashed into one of these buckets
# so you'll want this to approximate (but probably be lower) the number of hosts
# in your network.
NUM_HOST_BUCKETS=16
# The number of flow buckets within each high, normal and low class.
# If SFQ, SFB or FQ_CODEL are used this value is not used as these QDiscs
# have many embedded queues.
NUM_FLOW_BUCKETS=32
####
# Bandwidth rates
####
# All rates are kbit/sec.
# RATE should be set to just under your link rate.
RATE="6500"
####
# Queue size
####
# Size the queue. Only used with the simple FIFO QDiscs
# ie not SFQ, FQ_CODEL. Fun for experimentation but you
# probably don't want to use these simple QDiscs.
FIFO_LEN=100
####
# How often to perturb the hashes.
####
# This should probably be on the order of minutes so as to avoid the packet
# reordering which can happen when the flows are redistributed
# into different queues. Some of the new QDiscs may handle reordering properly.
#PERTURB=5
PERTURB=300
####
# Packet overhead
####
# Examples:
# ADSL:
# - http://www.adsl-optimizer.dk/thesis/
# (http://web.archive.org/web/20090422131547/http://www.adsl-optimizer.dk/thesis/)
# - If you are using ADSL you probably want LINKLAYER="atm" too.
# VDSL2 (without ATM) w/ PPPoE:
# - 40 bytes for 802.3
# - 8 bytes for PPPoE
OVERHEAD=48
####
# Set linklayer to one of ethernet,adsl (adsl == atm).
####
#LINKLAYER="adsl"
LINKLAYER="ethernet"
####
# The MTU of the underlying interface.
####
MTU="1492"
####
# The keys that are used to identify individual flows.
####
# For 5-tuple (flow) fairness
#FLOW_KEYS="src,dst,proto,proto-src,proto-dst"
# For 5-tuple (flow) fairness when the same device is performing NAT
FLOW_KEYS="nfct-src,nfct-dst,nfct-proto,nfct-proto-src,nfct-proto-dst"
####
# The keys that are used to identify a host's traffic.
####
# No NAT
#HOST_KEYS="src"
# With local device doing NAT
HOST_KEYS="nfct-src"
# Set R2Q (HTB knob) low if you use low bitrates. You may see warning from the kernel
# in /var/log/messages indicating this value should be modified. If you set the
# MTU/QUANTUM changing this isn't required.
#R2Q=2
####
# Choose the type of queue for each of the three per host priority classes
# Support options:
# drr
# sfq
# fq_codel
# sfb
# pfifo_head_drop
# pfifo
####
HIGH_PRIORITY_QDISC_TYPE="fq_codel"
NORMAL_PRIORITY_QDISC_TYPE="fq_codel"
LOW_PRIORITY_QDISC_TYPE="fq_codel"
###########################################
###########################################
# Other than picking QDisc type there is nothing to change below here.
###########################################
###########################################
######################
# Expand the config variables to tc arguments if they are defined.
######################
if [ "${OVERHEAD}" != "" ]; then
OVERHEAD="overhead ${OVERHEAD}"
fi
if [ "${LINKLAYER}" != "" ]; then
LINKLAYER="linklayer ${LINKLAYER}"
fi
if [ "${R2Q}" != "" ]; then
R2Q="r2q ${R2Q}"
fi
if [ "${PERTURB}" != "" ]; then
PERTURB="perturb ${PERTURB}"
fi
QUANTUM=${MTU}
if [ "${QUANTUM}" != "" ]; then
QUANTUM="quantum ${QUANTUM}"
fi
######################
# Utility functions
######################
function DEBUG()
{
[ "$_DEBUG" == "on" ] && "$@"
}
# Debug function for printing the tc command lines.
function CDEBUG()
{
[ "$_CDEBUG" == "on" ] && "$@"
}
function hex_replace {
if [[ "$1" =~ ":" ]]; then
QDISC=${1%%:*}
CLASS=${1##*:}
if [ "${CLASS}" == "" ]; then
D2H=`printf "%x:" ${QDISC}`
else
D2H=`printf "%x:%x" ${QDISC} ${CLASS}`
fi
else
D2H=`printf "%x" $1`
fi
}
###
# Function to wrap the tc command and convert the QDisc and class
# identifiers to hex before calling tc.
###
function tc_h {
OUTPUT="${TC} "
PTMP=$@
CDEBUG printf "Command before: %s\n" "${PTMP}"
while [ "$1" != "" ]; do
case "$1" in
# The tc parameters which take major:minor as an argument
"classid" | "flowid" | "parent" | "baseclass" | "handle")
hex_replace $2
OUTPUT="${OUTPUT} $1 ${D2H} "
shift
;;
* )
OUTPUT="${OUTPUT} $1 "
esac
shift
done
CDEBUG printf "Command after: ${OUTPUT}\n"
# Run the command.
${OUTPUT}
}
function get_next_free_major {
if [ "${FREE_MAJOR}" == "" ]; then
FREE_MAJOR=2 # Assumes 1 is used.
return
fi
FREE_MAJOR=$(expr ${FREE_MAJOR} + 1)
}
######################
# Functions to create QDiscs at the leaves.
######################
function drr {
PARENT=$1
HANDLE=$2
# Create the QDisc.
tc_h qdisc add dev ${DEVICE} parent ${PARENT} handle ${HANDLE} drr
# Create NUM_FLOW_BUCKETS classes and add a pfifo_head_drop to each.
for J in `seq ${NUM_FLOW_BUCKETS}`; do
tc_h class add dev ${DEVICE} parent ${HANDLE} classid ${HANDLE}:${J} drr ${QUANTUM}
tc_h qdisc add dev ${DEVICE} parent ${HANDLE}:${J} pfifo_head_drop limit ${FIFO_LEN}
done
# Add a filter to direct the packets.
tc_h filter add dev ${DEVICE} prio 1 protocol ip parent ${HANDLE}: handle 1 flow hash keys ${FLOW_KEYS} divisor ${NUM_FLOW_BUCKETS} ${PERTURB} baseclass ${HANDLE}:1
}
function sfq {
PARENT=$1
HANDLE=$2
DEBUG printf "\t\t\tsfq parent %s handle %s\n" ${PARENT} ${HANDLE}
#tc_h qdisc add dev ${DEVICE} parent ${PARENT} handle ${HANDLE} sfq limit ${FIFO_LEN} ${QUANTUM} divisor 1024
tc_h qdisc add dev ${DEVICE} parent ${PARENT} handle ${HANDLE} sfq ${QUANTUM} divisor 1024
# Don't use the SFQ default classifier.
tc_h filter add dev ${DEVICE} prio 1 protocol ip parent ${HANDLE}: handle 1 flow hash keys ${FLOW_KEYS} divisor 1024 ${PERTURB} baseclass ${HANDLE}:1
}
function fq_codel {
PARENT=$1
HANDLE=$2
DEBUG printf "\t\t\tfq_codel parent %s handle %s\n" ${PARENT} ${HANDLE}
tc_h qdisc add dev ${DEVICE} parent ${PARENT} handle ${HANDLE} fq_codel ${QUANTUM} flows 4096
# Don't use the default classifier.
tc_h filter add dev ${DEVICE} prio 1 protocol ip parent ${HANDLE}: handle 1 flow hash keys ${FLOW_KEYS} divisor 4096 ${PERTURB} baseclass ${HANDLE}:1
}
function sfb {
PARENT=$1
HANDLE=$2
#tc_h qdisc add dev ${DEVICE} parent ${PARENT} handle ${HANDLE} sfb
tc_h qdisc add dev ${DEVICE} parent ${PARENT} handle ${HANDLE} sfb target 20 max 25 increment 0.005 decrement 0.0001
# TODO - Should this have divisor?
tc_h filter add dev ${DEVICE} prio 1 protocol ip parent ${HANDLE}: handle 1 flow hash keys ${FLOW_KEYS} divisor 1024 ${PERTURB}
}
function pfifo_head_drop {
PARENT=$1
HANDLE=$2
tc_h qdisc add dev ${DEVICE} parent ${PARENT} handle ${HANDLE} pfifo_head_drop limit ${FIFO_LEN}
}
function pfifo {
PARENT=$1
HANDLE=$2
tc_h qdisc add dev ${DEVICE} parent ${PARENT} handle ${HANDLE} pfifo limit ${FIFO_LEN}
}
function priority_class_qdisc {
PARENT=$2
HANDLE=$3
case "$1" in
"drr" )
drr ${PARENT} ${HANDLE}
;;
"sfq" )
sfq ${PARENT} ${HANDLE}
;;
"fq_codel" )
fq_codel ${PARENT} ${HANDLE}
;;
"sfb" )
sfb ${PARENT} ${HANDLE}
;;
"pfifo_head_drop" )
pfifo_head_drop ${PARENT} ${HANDLE}
;;
"pfifo" )
pfifo ${PARENT} ${HANDLE}
;;
* )
echo "Error: Unknown leaf QDisc type"
exit
;;
esac
}
######################
# The real work starts here.
######################
# Calculate the divided rate value for use later.
DIV_RATE=`expr ${RATE} / ${NUM_HOST_BUCKETS}`
echo "Number of host buckets: ${NUM_HOST_BUCKETS}"
echo "Rate per host (DIV_RATE):" ${DIV_RATE}
# Delete any existing QDiscs if they exist.
tc_h qdisc del dev ${DEVICE} root
# HTB QDisc at the root. Default all traffic into the prio qdisc.
tc_h qdisc add dev ${DEVICE} root handle 1: htb ${R2Q}
# Create a top level class with the max rate.
tc_h class add dev ${DEVICE} parent 1: classid 1:1 htb rate ${RATE}kbit ${QUANTUM} prio 0 ${LINKLAYER} ${OVERHEAD}
######
# Create NUM_HOST_BUCKETS classes within the top-level class.
# Within each of these create a DRR with three classes which implement the three priorities.
# Within each priority class create the configured leaf QDisc.
######
for HOST_NUM in `seq ${NUM_HOST_BUCKETS}`; do
DEBUG printf "Create host class: %i\n" $HOST_NUM
QID=`expr ${HOST_NUM} '+' 9` # 1+9=10 - Start host buckets at 10. Arbitrary.
DEBUG printf "\tQID: %i\n" ${QID}
tc_h class add dev ${DEVICE} parent 1:1 classid 1:${QID} htb rate ${DIV_RATE}kbit ceil ${RATE}kbit ${QUANTUM} prio 0 ${LINKLAYER} ${OVERHEAD}
######
# Within each host class create a DRR QDisc within which we'll create the
# high, normal and low priority classes.
######
get_next_free_major
SUB_MAJOR=${FREE_MAJOR}
tc_h qdisc add dev ${DEVICE} parent 1:${QID} handle ${SUB_MAJOR}: drr
# Filter from the host class to the DRR within it.
tc_h filter add dev ${DEVICE} prio 2 protocol ip parent 1:${QID} u32 match ip dst 0.0.0.0/0 flowid ${SUB_MAJOR}:0
###
# High priority class
###
DEBUG printf "\t\tHigh: %i\n" ${QID_1}
tc_h class add dev ${DEVICE} parent ${SUB_MAJOR}: classid ${SUB_MAJOR}:1 drr ${QUANTUM}
# Create the leaf QDisc for this priority class.
get_next_free_major
SUB_PRIO_MAJOR=${FREE_MAJOR}
priority_class_qdisc ${HIGH_PRIORITY_QDISC_TYPE} ${SUB_MAJOR}:1 ${SUB_PRIO_MAJOR}
###
# Normal priority class
###
DEBUG printf "\t\tNormal: %i\n" ${QID_2}
tc_h class add dev ${DEVICE} parent ${SUB_MAJOR}: classid ${SUB_MAJOR}:2 drr ${QUANTUM}
# Create the leaf QDisc for this priority class.
get_next_free_major
SUB_PRIO_MAJOR=${FREE_MAJOR}
priority_class_qdisc ${NORMAL_PRIORITY_QDISC_TYPE} ${SUB_MAJOR}:2 ${SUB_PRIO_MAJOR}
###
# Low priority class
###
DEBUG printf "\t\tLow: %i\n" ${QID_3}
tc_h class add dev ${DEVICE} parent ${SUB_MAJOR}: classid ${SUB_MAJOR}:3 drr ${QUANTUM}
# Create the leaf QDisc for this priority class.
get_next_free_major
SUB_PRIO_MAJOR=${FREE_MAJOR}
priority_class_qdisc ${LOW_PRIORITY_QDISC_TYPE} ${SUB_MAJOR}:3 ${SUB_PRIO_MAJOR}
######
# Add filters to classify based on the TOS bits into the high, normal and low priority classes.
# Only mask against the three (used) TOS bits. The final two bits are used for ECN.
# TOS field is XXXDTRXX.
# X= Not part of the TOS field.
# D= Delay bit
# T= Throughput bit
# R= Reliability bit
#
# OpenSSH terminal sets D.
# OpenSSH SCP/SFTP sets T.
# It's easy to configure the Transmission Bittorrent client to set T (settings.json).
# For home VoIP devices I use an iptables rule to set all of their traffic to have D.
#
# The thinking behind the below rules is to use D as an indication of delay sensitive
# and T as an indication of background (big transfer). All other combinations are put into
# default which is effectively a medium priority.
######
DEBUG printf "\t\tCreating filters\n"
# D bit set.
tc_h filter add dev ${DEVICE} parent ${SUB_MAJOR}: protocol ip prio 10 u32 match ip tos 0x10 0x1c flowid ${SUB_MAJOR}:1
# Diffserv expedited forwarding. Put this in the high priority class.
# Some VoIP clients set this (ie Ekiga).
# DSCP=b8
tc_h filter add dev ${DEVICE} parent ${SUB_MAJOR}: protocol ip prio 10 u32 match ip tos 0xb8 0xfc flowid ${SUB_MAJOR}:1
# T bit set.
tc_h filter add dev ${DEVICE} parent ${SUB_MAJOR}: protocol ip prio 10 u32 match ip tos 0x08 0x1c flowid ${SUB_MAJOR}:3
# Everything else into default.
tc_h filter add dev ${DEVICE} parent ${SUB_MAJOR}: protocol ip prio 10 u32 match ip tos 0x00 0x00 flowid ${SUB_MAJOR}:2
done
# Send everything that hits the top level QDisc to the top class.
tc_h filter add dev ${DEVICE} prio 1 protocol ip parent 1:0 u32 match ip dst 0.0.0.0/0 flowid 1:1
# From the top level class hash into the host classes.
tc_h filter add dev ${DEVICE} prio 1 protocol ip parent 1:1 handle 1 flow hash keys ${HOST_KEYS} divisor ${NUM_HOST_BUCKETS} ${PERTURB} baseclass 1:10