aboutsummaryrefslogtreecommitdiffstats
path: root/extra/iproute2-qos/qos.initd
blob: b543748691e7a09a9034ece10b41b5d870a0b7d0 (plain)
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
#!/sbin/runscript
#
###########################################################################################################
# Traffic Control startup script
#
# Copyright (c) 2009 iilluzion
#
# Distributed under GPL-2
###########################################################################################################

PROGRAM=qos
CONFIG=/etc/conf.d/qos
DEBUG=0 #1

opts="compile"

depend() 
{
	need net
}

checkconfig() {
	if [ ! -e $CONFIG ] ; then
		eerror "You need to create $CONFIG first."
	return 1
	fi
}

usage()
{
	echo "usage: $PROGRAM [status|compile|start|stop|restart]"
	echo "   status:  View statistics"
	echo "   compile: Generate traffic control commands"
	echo "   start:   Start traffic control"
	echo "   stop:    Stop traffic control"
	echo "   restart: Restart traffic control"
}

configure()
{
	source $CONFIG $1
}

reset()
{
	for DEV in $DEVICES; do
		tc qdisc show dev $DEV | grep -v "pfifo_fast" | egrep -q "$QDISCS" && $ECHO tc qdisc del dev $DEV root
		tc qdisc show dev $DEV | grep -v "pfifo_fast" | grep  -q ingress && $ECHO tc qdisc del dev $DEV ingress
	done
}

set_leaf_qdisc()
{
	DEV=$1
	PARENT_CLASSID=$2
		PARENT_CLASSID=${PARENT_CLASSID:-1}

	if [ ! "$QDISC_CMD" = "prio" ]; then 
		$ECHO tc qdisc add dev $DEV parent $PARENT_CLASSID:40 handle 40 $INTERACTIVE_LEAF_QDISC
	fi

	$ECHO tc qdisc add dev $DEV parent $PARENT_CLASSID:50 handle 50 $PRIVILEGED_LEAF_QDISC
	$ECHO tc qdisc add dev $DEV parent $PARENT_CLASSID:60 handle 60 $BESTEFFORT_LEAF_QDISC

	if [ $DIRECTION = "DOWN" ]; then 
		$ECHO tc qdisc add dev $DEV parent $PARENT_CLASSID:70 handle 70 $LAN_LEAF_QDISC
	fi

	$ECHO
}

set_filters()
{
	CLASS_TYPES="INTERACTIVE PRIVILEGED BESTEFFORT"
		if [ $DIRECTION = "DOWN" ]; then
			CLASS_TYPES=$CLASS_TYPES" LAN"
		fi 

		PRIVILEGED_FILTER_FLOWID=50
		BESTEFFORT_FILTER_FLOWID=60
		LAN_FILTER_FLOWID=70

	for DEV in $DEVICES; do
		for CLASS_TYPE in $CLASS_TYPES; do
			if [ "$QDISC_CMD" = "prio" -a "$CLASS_TYPE" = "INTERACTIVE" ]; then
				PARENT_CLASSID=1
				INTERACTIVE_FILTER_FLOWID=1
			else
				PARENT_CLASSID=$1
					PARENT_CLASSID=${PARENT_CLASSID:-1}
				INTERACTIVE_FILTER_FLOWID=40
			fi

			for FILTER_NUM in `seq 1 100`; do
				eval FILTER="\$$CLASS_TYPE"_FILTER_$FILTER_NUM
					[ -z "$FILTER" ] && break
				eval FILTER_FLOWID="\$$CLASS_TYPE"_FILTER_FLOWID
					$ECHO tc filter add dev $DEV parent $PARENT_CLASSID:0 $FILTER flowid $PARENT_CLASSID:$FILTER_FLOWID
			done
		done

		$ECHO
	done
}

set_htb()
{
	for DEV in $DEVICES; do
		$ECHO tc qdisc add dev $DEV root handle 1 htb default 60
			$ECHO tc class add dev $DEV parent 1: classid 1:2 htb rate $LAN_RATE
				$ECHO tc class add dev $DEV parent 1:2 classid 1:30 htb rate $WAN_SUB_RATE
					$ECHO tc class add dev $DEV parent 1:30 classid 1:40 htb rate $INTERACTIVE_RATE prio 1
					$ECHO tc class add dev $DEV parent 1:30 classid 1:50 htb rate $PRIVILEGED_RATE ceil $WAN_SUB_RATE prio 3
		    			$ECHO tc class add dev $DEV parent 1:30 classid 1:60 htb rate $BESTEFFORT_RATE ceil $WAN_SUB_RATE prio 6

				if [ $DIRECTION = "DOWN" ]; then 
					$ECHO tc class add dev $DEV parent 1:2 classid 1:70 htb rate $OUT_OF_WAN_RATE prio 7
				fi
	
		set_leaf_qdisc $DEV

		$ECHO
	done

	set_filters
}

set_hfsc()
{
	for DEV in $DEVICES; do
		$ECHO tc qdisc add dev $DEV root handle 1 hfsc default 60
 			$ECHO tc class add dev $DEV parent 1: classid 1:2 hfsc sc rate $LAN_RATE ul rate $LAN_RATE
				$ECHO tc class add dev $DEV parent 1:2 classid 1:30 hfsc sc rate $WAN_SUB_RATE ul rate $WAN_SUB_RATE
					$ECHO tc class add dev $DEV parent 1:30 classid 1:40 hfsc sc umax $INTERACTIVE_HFSC_UMAX dmax $INTERACTIVE_HFSC_DMAX rate $INTERACTIVE_RATE ul rate $INTERACTIVE_RATE
					$ECHO tc class add dev $DEV parent 1:30 classid 1:50 hfsc sc umax $PRIVILEGED_HFSC_UMAX dmax $PRIVILEGED_HFSC_DMAX rate $PRIVILEGED_RATE ul rate $WAN_SUB_RATE
					$ECHO tc class add dev $DEV parent 1:30 classid 1:60 hfsc sc rate $BESTEFFORT_RATE ul rate $WAN_SUB_RATE

				if [ $DIRECTION = "DOWN" ]; then
					$ECHO tc class add dev $DEV parent 1:2 classid 1:70 hfsc sc rate $OUT_OF_WAN_RATE ul rate $OUT_OF_WAN_RATE
				fi

		set_leaf_qdisc $DEV

		$ECHO
	done

	set_filters
}

set_prio()
{
	PARENT_CLASSID=10
	for DEV in $DEVICES; do
		$ECHO tc qdisc add dev $DEV root handle 1 prio bands 2 priomap 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 # by default unclassified traffic goes to flowid 1:2
			$ECHO tc qdisc add dev $DEV parent 1:1 handle 40: tbf rate $INTERACTIVE_RATE burst $INTERACTIVE_PRIO_BURST latency $INTERACTIVE_PRIO_LATENCY
			$ECHO tc qdisc add dev $DEV parent 1:2 handle $PARENT_CLASSID: htb default 60
				$ECHO tc class add dev $DEV parent $PARENT_CLASSID: classid $PARENT_CLASSID:30 htb rate $WAN_SUB_RATE
					$ECHO tc class add dev $DEV parent $PARENT_CLASSID:30 classid $PARENT_CLASSID:50 htb rate $PRIVILEGED_RATE ceil $WAN_SUB_RATE prio 3
					$ECHO tc class add dev $DEV parent $PARENT_CLASSID:30 classid $PARENT_CLASSID:60 htb rate $BESTEFFORT_RATE ceil $WAN_SUB_RATE prio 6

				if [ $DIRECTION = "DOWN" ]; then
					$ECHO tc class add dev $DEV parent 10:1 classid $PARENT_CLASSID:70 htb rate $OUT_OF_WAN_RATE prio 7
					fi

		set_leaf_qdisc $DEV $PARENT_CLASSID

		$ECHO
	done

	set_filters $PARENT_CLASSID
}

set_police()
{
	CLASS_TYPES="INTERACTIVE PRIVILEGED"
		INTERACTIVE_POLICE_FLOWID=1
		PRIVILEGED_POLICE_FLOWID=2

	for DEV in $DEVICES; do
		$ECHO tc qdisc add dev $DEV handle ffff: ingress

		for CLASS_TYPE in $CLASS_TYPES; do
			for FILTER_NUM in `seq 1 100`; do
				eval FILTER="\$$CLASS_TYPE"_FILTER_$FILTER_NUM
					[ -z "$FILTER" ] && break
				eval FILTER_FLOWID="\$$CLASS_TYPE"_POLICE_FLOWID
				eval FILTER_RATE="\$$CLASS_TYPE"_RATE
				eval FILTER_BURST="\$$CLASS_TYPE"_POLICE_BURST
					$ECHO tc filter add dev $DEV parent ffff: $FILTER police rate $FILTER_RATE burst $FILTER_BURST continue flowid $FILTER_FLOWID:
			done
		done

		$ECHO tc filter add dev $DEV parent ffff: protocol ip prio 999 u32 match ip src 0.0.0.0/0 police rate $BESTEFFORT_RATE burst $BESTEFFORT_POLICE_BURST drop flowid :3
 
		$ECHO
	done
}

get_stats()
{
	for DEV in $WAN_DEVICES $LAN_DEVICES; do
		echo $DEV Statistics
		echo
		echo "   Classes:"
		echo "--------------------------"
			$ECHO tc -s class show dev $DEV

		echo
		echo "   Leaf Queuing Disciplines:"
		echo "--------------------------"
			$ECHO tc -s qdisc show dev $DEV

		echo
		echo "   EGRESS Filters:"
		echo "--------------------------"
			$ECHO tc -s filter show dev $DEV
			$ECHO tc -s filter show dev $DEV parent 10: # if PRIO qdisc is applied
		
		echo
		echo "   INGRESS Policing Filters:"
		echo "--------------------------"
			$ECHO tc -s filter show dev $DEV parent ffff:

 		echo
		echo
 	done
}

compile()
{
	DEBUG=1
	
	start
}

start()
{
	checkconfig || return 1

	if [ $DEBUG -gt 0 ]; then 
		ECHO="echo"
	else
		ebegin "Starting QoS"
	fi

	for LINK_DIRECTION in UP DOWN; do
		configure $LINK_DIRECTION
			reset

			eval QDISC_CMD="\$$LINK_DIRECTION"LINK_QDISC
				if [ ! "$QDISC_CMD" = "none" ]; then
					set_$QDISC_CMD
				fi

			eval INGRESS_CMD="\$$LINK_DIRECTION"LINK_INGRESS
				if [ ! "$INGRESS_CMD" = "none" ]; then
 					set_$INGRESS_CMD
				fi
	done

	if [ $DEBUG -eq 0 ]; then
	 	eend $?
	fi
}

stop()
{
	checkconfig || return 1

	if [ $DEBUG -gt 0 ]; then
		ECHO="echo"
	else
		ebegin "Stopping QoS"
	fi

	configure ALL
	reset

	if [ $DEBUG -eq 0 ]; then
		eend $?
	fi
}

restart()
{
	stop
	start
}

status()
{
	get_stats
}