QoS-Update - Komplette grafische Ueberarbeitung
[people/pmueller/ipfire-2.x.git] / config / qos / makeqosscripts.pl
CommitLineData
84cab473
MT
1#!/usr/bin/perl
2#
3# IPFire Scripts
4#
5# This code is distributed under the terms of the GPL
6#
7# (c) The IPFire Team
8#
9
10use strict;
11# enable only the following on debugging purpose
e8babc92 12# use warnings;
84cab473
MT
13
14require '/var/ipfire/general-functions.pl';
15require "${General::swroot}/lang.pl";
16require "${General::swroot}/header.pl";
17
18my %qossettings = ();
19my %checked = ();
20my %netsettings = ();
21my $message = "";
22my $errormessage = "";
23my $c = "";
24my $direntry = "";
25my $classentry = "";
26my $subclassentry = "";
27my $l7ruleentry = "";
28my $portruleentry = "";
e8babc92 29my $tosruleentry = "";
84cab473
MT
30my @tmp = ();
31my @classes = ();
32my @subclasses = ();
33my @l7rules = ();
34my @portrules = ();
e8babc92 35my @tosrules = ();
84cab473
MT
36my @tmpline = ();
37my @classline = ();
38my @subclassline = ();
e8babc92 39my @tosruleline = ();
84cab473
MT
40my @l7ruleline = ();
41my @portruleline = ();
42my @proto = ();
43my %selected= () ;
44my $classfile = "/var/ipfire/qos/classes";
45my $subclassfile = "/var/ipfire/qos/subclasses";
46my $level7file = "/var/ipfire/qos/level7config";
47my $portfile = "/var/ipfire/qos/portconfig";
e8babc92 48my $tosfile = "/var/ipfire/qos/tosconfig";
84cab473
MT
49
50&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
51
52$qossettings{'ENABLED'} = 'off';
53$qossettings{'EDIT'} = 'no';
54$qossettings{'OUT_SPD'} = '';
55$qossettings{'INC_SPD'} = '';
56$qossettings{'DEF_OUT_SPD'} = '';
57$qossettings{'DEF_INC_SPD'} = '';
58$qossettings{'DEFCLASS_INC'} = '';
59$qossettings{'DEFCLASS_OUT'} = '';
60$qossettings{'ACK'} = '';
61$qossettings{'MTU'} = '1492';
62$qossettings{'RED_DEV'} = `cat /var/ipfire/red/iface`;
63$qossettings{'IMQ_DEV'} = 'imq0';
e8babc92 64$qossettings{'TOS'} = '';
84cab473
MT
65$qossettings{'VALID'} = 'yes';
66
67&General::readhash("${General::swroot}/qos/settings", \%qossettings);
68
69open( FILE, "< $classfile" ) or die "Unable to read $classfile";
70@classes = <FILE>;
71close FILE;
72open( FILE, "< $subclassfile" ) or die "Unable to read $subclassfile";
73@subclasses = <FILE>;
74close FILE;
75open( FILE, "< $level7file" ) or die "Unable to read $level7file";
76@l7rules = <FILE>;
77close FILE;
78open( FILE, "< $portfile" ) or die "Unable to read $portfile";
79@portrules = <FILE>;
80close FILE;
e8babc92
MT
81open( FILE, "< $tosfile" ) or die "Unable to read $tosfile";
82@tosrules = <FILE>;
83close FILE;
84cab473
MT
84
85############################################################################################################################
86############################################################################################################################
87
88print <<END
89#/bin/bash
90#################################################
91# This is an autocreated QoS-Script for #
92# IPFIRE #
93# Copyright by the IPFire Team (GPLv2) #
94# www.ipfire.org #
95#################################################
96
97### SYSTEMVARIABLES:
98# RED INTERFACE: $qossettings{'RED_DEV'}
99# IMQ DEVICE: $qossettings{'IMQ_DEV'}
100
101case "\$1" in
102
103 status)
4ed2816f
MT
104 case "\$2" in
105 qdisc)
106 echo "[qdisc]"
107 tc -s qdisc show dev $qossettings{'RED_DEV'}
108 tc -s qdisc show dev $qossettings{'IMQ_DEV'}
109 exit 0
110 ;;
111 class)
112 echo "[class]"
113 tc -s class show dev $qossettings{'RED_DEV'}
114 tc -s class show dev $qossettings{'IMQ_DEV'}
115 exit 0
116 ;;
117 filter)
118 echo "[filter]"
119 tc -s filter show dev $qossettings{'RED_DEV'}
120 tc -s filter show dev $qossettings{'IMQ_DEV'}
121 exit 0
122 ;;
123 iptables)
124 echo "[iptables]"
dd001658
MT
125 iptables -t mangle -n -L QOS-OUT -v -x 2> /dev/null
126 iptables -t mangle -n -L QOS-INC -v -x 2> /dev/null
127 iptables -t mangle -n -L QOS-TOS -v -x 2> /dev/null
4ed2816f
MT
128 exit 0
129 ;;
130 esac
131 \$0 \$1 qdisc
132 \$0 \$1 class
133 \$0 \$1 filter
134 \$0 \$1 iptables
84cab473
MT
135 exit 0
136 ;;
137 start)
84cab473
MT
138 ###
139 ### $qossettings{'RED_DEV'}
140 ###
141
142 ### INIT KERNEL
143 modprobe sch_htb
144
145 ### SET QUEUE LENGTH & MTU - has just to be tested!!! IMPORTANT
146 ip link set dev $qossettings{'RED_DEV'} qlen $qossettings{'QLENGTH'}
147 ip link set dev $qossettings{'RED_DEV'} mtu $qossettings{'MTU'}
148
149 ### ADD HTB QDISC FOR $qossettings{'RED_DEV'}
150 tc qdisc add dev $qossettings{'RED_DEV'} root handle 1: htb default $qossettings{'DEFCLASS_OUT'}
151
152 ### MAIN RATE LIMIT
153 tc class add dev $qossettings{'RED_DEV'} parent 1: classid 1:1 htb rate $qossettings{'OUT_SPD'}kbit
154
155 ### CLASSES FOR $qossettings{'RED_DEV'}
156END
157;
158foreach $classentry (sort @classes)
159{
160 @classline = split( /\;/, $classentry );
161 if ($qossettings{'RED_DEV'} eq $classline[0]) {
162 $qossettings{'DEVICE'} = $classline[0];
163 $qossettings{'CLASS'} = $classline[1];
164 $qossettings{'PRIO'} = $classline[2];
165 $qossettings{'RATE'} = $classline[3];
166 $qossettings{'CEIL'} = $classline[4];
167 $qossettings{'BURST'} = $classline[5];
168 $qossettings{'CBURST'} = $classline[6];
169 print "\ttc class add dev $qossettings{'DEVICE'} parent 1:1 classid 1:$qossettings{'CLASS'} htb rate $qossettings{'RATE'}kbit ceil $qossettings{'CEIL'}kbit prio $qossettings{'PRIO'} ";
4ed2816f 170 if (($qossettings{'BURST'} ne '') && ($qossettings{'BURST'} ne 0)) {
84cab473
MT
171 print "burst $qossettings{'BURST'}k ";
172 }
b684b13b 173 if (($qossettings{'CBURST'} ne '') && ($qossettings{'CBURST'} ne 0)) {
84cab473
MT
174 print "cburst $qossettings{'CBURST'}k";
175 }
176 print "\n";
177 }
178}
179foreach $subclassentry (sort @subclasses) {
180 @subclassline = split( /\;/, $subclassentry );
92b5fba4
MT
181 if ($qossettings{'RED_DEV'} eq $subclassline[0]) {
182 $qossettings{'DEVICE'} = $subclassline[0];
183 $qossettings{'CLASS'} = $subclassline[1];
184 $qossettings{'SCLASS'} = $subclassline[2];
185 $qossettings{'SPRIO'} = $subclassline[3];
186 $qossettings{'SRATE'} = $subclassline[4];
187 $qossettings{'SCEIL'} = $subclassline[5];
188 $qossettings{'SBURST'} = $subclassline[6];
189 $qossettings{'SCBURST'} = $subclassline[7];
190 print "\ttc class add dev $qossettings{'DEVICE'} parent 1:$qossettings{'CLASS'} classid 1:$qossettings{'SCLASS'} htb rate $qossettings{'SRATE'}kbit ceil $qossettings{'SCEIL'}kbit prio $qossettings{'SPRIO'} ";
191 if ($qossettings{'SBURST'} > 0) {
192 print "burst $qossettings{'SBURST'}k ";
193 }
194 if (($qossettings{'SCBURST'} ne '') && ($qossettings{'SCBURST'} ne 0)) {
195 print "cburst $qossettings{'CBURST'}k";
196 }
197 print "\n";
84cab473 198 }
84cab473
MT
199}
200
201print "\n\t### ATTACH QDISC TO LEAF CLASSES\n";
202foreach $classentry (sort @classes)
203{
204 @classline = split( /\;/, $classentry );
205 if ($qossettings{'RED_DEV'} eq $classline[0]) {
206 $qossettings{'DEVICE'} = $classline[0];
207 $qossettings{'CLASS'} = $classline[1];
208 print "\ttc qdisc add dev $qossettings{'DEVICE'} parent 1:$qossettings{'CLASS'} handle $qossettings{'CLASS'}: sfq perturb $qossettings{'SFQ_PERTUB'}\n";
209 }
210}
211foreach $subclassentry (sort @subclasses) {
212 @subclassline = split( /\;/, $subclassentry );
213 if ($qossettings{'RED_DEV'} eq $subclassline[0]) {
214 $qossettings{'DEVICE'} = $subclassline[0];
215 $qossettings{'SCLASS'} = $subclassline[2];
216 print "\ttc qdisc add dev $qossettings{'DEVICE'} parent 1:$qossettings{'SCLASS'} handle $qossettings{'SCLASS'}: sfq perturb $qossettings{'SFQ_PERTUB'}\n";
217 }
218}
219print "\n\t### FILTER TRAFFIC INTO CLASSES\n";
220foreach $classentry (sort @classes)
221{
222 @classline = split( /\;/, $classentry );
223 if ($qossettings{'RED_DEV'} eq $classline[0]) {
224 $qossettings{'DEVICE'} = $classline[0];
225 $qossettings{'CLASS'} = $classline[1];
226 print "\ttc filter add dev $qossettings{'DEVICE'} parent 1:0 prio 0 protocol ip handle $qossettings{'CLASS'} fw flowid 1:$qossettings{'CLASS'}\n";
227 }
228}
229foreach $subclassentry (sort @subclasses) {
230 @subclassline = split( /\;/, $subclassentry );
231 if ($qossettings{'RED_DEV'} eq $subclassline[0]) {
232 $qossettings{'DEVICE'} = $subclassline[0];
233 $qossettings{'CLASS'} = $subclassline[1];
234 $qossettings{'SCLASS'} = $subclassline[2];
92b5fba4 235 print "\ttc filter add dev $qossettings{'DEVICE'} parent 1:0 prio 0 protocol ip handle $qossettings{'SCLASS'} fw flowid 1:$qossettings{'SCLASS'}\n";
84cab473
MT
236 }
237}
238print <<END
239
240 ### ADD QOS-OUT CHAIN TO THE MANGLE TABLE IN IPTABLES
241 iptables -t mangle -N QOS-OUT
e8babc92 242 iptables -t mangle -N QOS-TOS
84cab473 243 iptables -t mangle -I POSTROUTING -o $qossettings{'RED_DEV'} -j QOS-OUT
e8babc92 244 iptables -t mangle -A POSTROUTING -o $qossettings{'RED_DEV'} -j QOS-TOS
84cab473
MT
245
246 ### MARK ACKs
247 iptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -p tcp --tcp-flags SYN,RST SYN -j TOS --set-tos 4
248 iptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -p tcp --tcp-flags SYN,RST SYN -j MARK --set-mark $qossettings{'ACK'}
249 iptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -p tcp --tcp-flags SYN,RST SYN -j RETURN
250
251 iptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -p icmp -m length --length 40:100 -j MARK --set-mark $qossettings{'ACK'}
252 iptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -p icmp -m length --length 40:100 -j RETURN
253
254 iptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -p tcp --syn -m length --length 40:68 -j TOS --set-tos 4
255 iptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -p tcp --syn -m length --length 40:68 -j MARK --set-mark $qossettings{'ACK'}
256 iptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -p tcp --syn -m length --length 40:68 -j RETURN
257
258 iptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -p tcp --tcp-flags ALL SYN,ACK -m length --length 40:68 -j TOS --set-tos 4
259 iptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -p tcp --tcp-flags ALL SYN,ACK -m length --length 40:68 -j MARK --set-mark $qossettings{'ACK'}
260 iptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -p tcp --tcp-flags ALL SYN,ACK -m length --length 40:68 -j RETURN
261
262 iptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -p tcp --tcp-flags ALL ACK -m length --length 40:100 -j TOS --set-tos 4
263 iptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -p tcp --tcp-flags ALL ACK -m length --length 40:100 -j MARK --set-mark $qossettings{'ACK'}
264 iptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -p tcp --tcp-flags ALL ACK -m length --length 40:100 -j RETURN
265
266 iptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -p tcp --tcp-flags ALL RST -j TOS --set-tos 4
267 iptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -p tcp --tcp-flags ALL RST -j MARK --set-mark $qossettings{'ACK'}
268 iptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -p tcp --tcp-flags ALL RST -j RETURN
269
270 iptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -p tcp --tcp-flags ALL ACK,RST -j TOS --set-tos 4
271 iptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -p tcp --tcp-flags ALL ACK,RST -j MARK --set-mark $qossettings{'ACK'}
272 iptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -p tcp --tcp-flags ALL ACK,RST -j RETURN
273
274 iptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -p tcp --tcp-flags ALL ACK,FIN -j TOS --set-tos 4
275 iptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -p tcp --tcp-flags ALL ACK,FIN -j MARK --set-mark $qossettings{'ACK'}
276 iptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -p tcp --tcp-flags ALL ACK,FIN -j RETURN
277
e8babc92
MT
278 ### SET TOS
279END
280;
281 foreach $tosruleentry (sort @tosrules)
282 {
283 @tosruleline = split( /\;/, $tosruleentry );
284 $qossettings{'CLASS'} = $tosruleline[0];
285 $qossettings{'TOS'} = abs $tosruleline[2] * 2;
286 if ( $tosruleline[1] eq $qossettings{'RED_DEV'} )
287 {
288 print "\tiptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -m tos --tos $qossettings{'TOS'} -j MARK --set-mark $qossettings{'CLASS'}\n";
289 print "\tiptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -m tos --tos $qossettings{'TOS'} -j RETURN\n";
290 }
291 }
292
84cab473
MT
293print "\n\t### SET PORT-RULES\n";
294 foreach $portruleentry (sort @portrules)
295 {
296 @portruleline = split( /\;/, $portruleentry );
297 if ( $portruleline[1] eq $qossettings{'RED_DEV'} )
298 {
299 $qossettings{'CLASS'} = $portruleline[0];
300 $qossettings{'DEVICE'} = $portruleline[1];
301 $qossettings{'PPROT'} = $portruleline[2];
302 $qossettings{'QIP'} = $portruleline[3];
303 $qossettings{'QPORT'} = $portruleline[4];
304 $qossettings{'DIP'} = $portruleline[5];
305 $qossettings{'DPORT'} = $portruleline[6];
4ed2816f 306 print "\tiptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} ";
84cab473
MT
307 if ($qossettings{'QIP'} ne ''){
308 print "-s $qossettings{'QIP'} ";
309 }
310 if ($qossettings{'DIP'} ne ''){
311 print "-d $qossettings{'DIP'} ";
312 }
313 print "-p $qossettings{'PPROT'} ";
314 if (($qossettings{'QPORT'} ne '') || ($qossettings{'DPORT'} ne '')){
315 print "-m multiport ";
316 }
317 if ($qossettings{'QPORT'} ne ''){
318 print "--sport $qossettings{'QPORT'} ";
319 }
320 if ($qossettings{'DPORT'} ne ''){
321 print "--dport $qossettings{'DPORT'} ";
322 }
323 print "-j MARK --set-mark $qossettings{'CLASS'}\n";
4ed2816f 324 print "\tiptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} ";
84cab473
MT
325 if ($qossettings{'QIP'} ne ''){
326 print "-s $qossettings{'QIP'} ";
327 }
328 if ($qossettings{'DIP'} ne ''){
329 print "-d $qossettings{'DIP'} ";
330 }
331 print "-p $qossettings{'PPROT'} ";
332 if (($qossettings{'QPORT'} ne '') || ($qossettings{'DPORT'} ne '')){
333 print "-m multiport ";
334 }
335 if ($qossettings{'QPORT'} ne ''){
336 print "--sport $qossettings{'QPORT'} ";
337 }
338 if ($qossettings{'DPORT'} ne ''){
339 print "--dport $qossettings{'DPORT'} ";
340 }
341 print "-j RETURN\n\n";
342 }
343 }
344
f013ab2b
MT
345print <<END
346
347 ### SET LEVEL7-RULES
348END
349;
350 foreach $l7ruleentry (sort @l7rules)
351 {
352 @l7ruleline = split( /\;/, $l7ruleentry );
353 if ( $l7ruleline[1] eq $qossettings{'RED_DEV'} )
354 {
355 $qossettings{'CLASS'} = $l7ruleline[0];
356 $qossettings{'DEVICE'} = $l7ruleline[1];
357 $qossettings{'L7PROT'} = $l7ruleline[2];
358 $qossettings{'QIP'} = $l7ruleline[3];
359 $qossettings{'DIP'} = $l7ruleline[4];
360 print "\tiptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} ";
361 if ($qossettings{'QIP'} ne ''){
362 print "-s $qossettings{'QIP'} ";
363 }
364 if ($qossettings{'DIP'} ne ''){
365 print "-d $qossettings{'DIP'} ";
366 }
367 print "-m layer7 --l7dir /etc/l7-protocols/protocols --l7proto $qossettings{'L7PROT'} -j MARK --set-mark $qossettings{'CLASS'}\n";
368 print "\tiptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} ";
369 if ($qossettings{'QIP'} ne ''){
370 print "-s $qossettings{'QIP'} ";
371 }
372 if ($qossettings{'DIP'} ne ''){
373 print "-d $qossettings{'DIP'} ";
374 }
375 print "-m layer7 --l7dir /etc/l7-protocols/protocols --l7proto $qossettings{'L7PROT'} -j RETURN\n";
376 }
377 }
378
84cab473
MT
379print <<END
380
381 ### REDUNDANT: SET ALL NONMARKED PACKETS TO DEFAULT CLASS
b684b13b 382 iptables -t mangle -A QOS-OUT -o $qossettings{'RED_DEV'} -m mark --mark 0 -j MARK --set-mark $qossettings{'DEFCLASS_OUT'}
84cab473
MT
383
384 ###
385 ### $qossettings{'IMQ_DEV'}
386 ###
387
388 ### BRING UP $qossettings{'IMQ_DEV'}
b684b13b
MT
389 if [ `lsmod | grep -q ipt_IMQ` ]; then
390 insmod ipt_IMQ
391 sleep 2
392 fi
84cab473
MT
393 modprobe imq numdevs=1
394 ip link set $qossettings{'IMQ_DEV'} up
395
396 ### SET QUEUE LENGTH & MTU - has just to be tested!!! IMPORTANT
397 ip link set dev $qossettings{'IMQ_DEV'} qlen $qossettings{'QLENGTH'}
4ed2816f 398 # ip link set dev $qossettings{'IMQ_DEV'} mtu $qossettings{'MTU'}
84cab473
MT
399
400 ### ADD HTB QDISC FOR $qossettings{'IMQ_DEV'}
401 tc qdisc add dev $qossettings{'IMQ_DEV'} root handle 2: htb default $qossettings{'DEFCLASS_INC'}
402
403 ### MAIN RATE LIMIT
404 tc class add dev $qossettings{'IMQ_DEV'} parent 2: classid 2:1 htb rate $qossettings{'INC_SPD'}kbit
405
406 ### CLASSES FOR $qossettings{'IMQ_DEV'}
407END
408;
409foreach $classentry (sort @classes)
410{
411 @classline = split( /\;/, $classentry );
412 if ($qossettings{'IMQ_DEV'} eq $classline[0]) {
413 $qossettings{'DEVICE'} = $classline[0];
414 $qossettings{'CLASS'} = $classline[1];
415 $qossettings{'PRIO'} = $classline[2];
416 $qossettings{'RATE'} = $classline[3];
417 $qossettings{'CEIL'} = $classline[4];
418 $qossettings{'BURST'} = $classline[5];
419 $qossettings{'CBURST'} = $classline[6];
420 print "\ttc class add dev $qossettings{'DEVICE'} parent 2:1 classid 2:$qossettings{'CLASS'} htb rate $qossettings{'RATE'}kbit ceil $qossettings{'CEIL'}kbit prio $qossettings{'PRIO'} ";
4ed2816f 421 if (($qossettings{'BURST'} ne '') && ($qossettings{'BURST'} ne 0)) {
84cab473
MT
422 print "burst $qossettings{'BURST'}k ";
423 }
4ed2816f 424 if (($qossettings{'CBURST'} ne '') && ($qossettings{'CBURST'} ne 0)) {
84cab473
MT
425 print "cburst $qossettings{'CBURST'}k";
426 }
427 print "\n";
428 }
429}
430foreach $subclassentry (sort @subclasses) {
431 @subclassline = split( /\;/, $subclassentry );
432 if ($qossettings{'IMQ_DEV'} eq $subclassline[0]) {
433 $qossettings{'DEVICE'} = $subclassline[0];
434 $qossettings{'CLASS'} = $subclassline[1];
435 $qossettings{'SCLASS'} = $subclassline[2];
436 $qossettings{'SPRIO'} = $subclassline[3];
437 $qossettings{'SRATE'} = $subclassline[4];
438 $qossettings{'SCEIL'} = $subclassline[5];
439 $qossettings{'SBURST'} = $subclassline[6];
440 $qossettings{'SCBURST'} = $subclassline[7];
441 print "\ttc class add dev $qossettings{'DEVICE'} parent 2:$qossettings{'CLASS'} classid 2:$qossettings{'SCLASS'} htb rate $qossettings{'SRATE'}kbit ceil $qossettings{'SCEIL'}kbit prio $qossettings{'SPRIO'} ";
442 if ($qossettings{'SBURST'} > 0) {
443 print "burst $qossettings{'SBURST'}k ";
444 }
4ed2816f 445 if (($qossettings{'SCBURST'} ne '') && ($qossettings{'SCBURST'} ne 0)) {
84cab473
MT
446 print "cburst $qossettings{'CBURST'}k";
447 }
448 print "\n";
449 }
450}
451
452print "\n\t### ATTACH QDISC TO LEAF CLASSES\n";
453foreach $classentry (sort @classes)
454{
455 @classline = split( /\;/, $classentry );
456 if ($qossettings{'IMQ_DEV'} eq $classline[0]) {
457 $qossettings{'DEVICE'} = $classline[0];
458 $qossettings{'CLASS'} = $classline[1];
459 print "\ttc qdisc add dev $qossettings{'DEVICE'} parent 2:$qossettings{'CLASS'} handle $qossettings{'CLASS'}: sfq perturb $qossettings{'SFQ_PERTUB'}\n";
460 }
461}
462foreach $subclassentry (sort @subclasses) {
463 @subclassline = split( /\;/, $subclassentry );
464 if ($qossettings{'IMQ_DEV'} eq $subclassline[0]) {
465 $qossettings{'DEVICE'} = $subclassline[0];
466 $qossettings{'SCLASS'} = $subclassline[2];
467 print "\ttc qdisc add dev $qossettings{'DEVICE'} parent 2:$qossettings{'SCLASS'} handle $qossettings{'SCLASS'}: sfq perturb $qossettings{'SFQ_PERTUB'}\n";
468 }
469}
470print "\n\t### FILTER TRAFFIC INTO CLASSES\n";
471foreach $classentry (sort @classes)
472{
473 @classline = split( /\;/, $classentry );
474 if ($qossettings{'IMQ_DEV'} eq $classline[0]) {
475 $qossettings{'DEVICE'} = $classline[0];
476 $qossettings{'CLASS'} = $classline[1];
477 print "\ttc filter add dev $qossettings{'DEVICE'} parent 2:0 prio 0 protocol ip handle $qossettings{'CLASS'} fw flowid 2:$qossettings{'CLASS'}\n";
478 }
479}
480foreach $subclassentry (sort @subclasses) {
481 @subclassline = split( /\;/, $subclassentry );
482 if ($qossettings{'IMQ_DEV'} eq $subclassline[0]) {
483 $qossettings{'DEVICE'} = $subclassline[0];
484 $qossettings{'CLASS'} = $subclassline[1];
485 $qossettings{'SCLASS'} = $subclassline[2];
92b5fba4 486 print "\ttc filter add dev $qossettings{'DEVICE'} parent 2:0 prio 0 protocol ip handle $qossettings{'SCLASS'} fw flowid 2:$qossettings{'SCLASS'}\n";
84cab473
MT
487 }
488}
489print <<END
490
4ed2816f 491 ### ADD QOS-INC CHAIN TO THE MANGLE TABLE IN IPTABLES
84cab473 492 iptables -t mangle -N QOS-INC
b684b13b
MT
493 iptables -t mangle -A PREROUTING -i $qossettings{'RED_DEV'} -j IMQ --todev 0
494 iptables -t mangle -I PREROUTING -i $qossettings{'RED_DEV'} -j QOS-INC
e8babc92
MT
495 iptables -t mangle -A PREROUTING -i $qossettings{'RED_DEV'} -j QOS-TOS
496
497 ### SET TOS
498END
499;
500 foreach $tosruleentry (sort @tosrules)
501 {
502 @tosruleline = split( /\;/, $tosruleentry );
503 $qossettings{'CLASS'} = $tosruleline[0];
504 $qossettings{'TOS'} = abs $tosruleline[2] * 2;
505 if ( $tosruleline[1] eq $qossettings{'IMQ_DEV'} )
506 {
507 print "\tiptables -t mangle -A QOS-INC -i $qossettings{'RED_DEV'} -m tos --tos $qossettings{'TOS'} -j MARK --set-mark $qossettings{'CLASS'}\n";
508 print "\tiptables -t mangle -A QOS-INC -i $qossettings{'RED_DEV'} -m tos --tos $qossettings{'TOS'} -j RETURN\n";
509 }
510
511 }
512
84cab473
MT
513print "\n\t### SET PORT-RULES\n";
514 foreach $portruleentry (sort @portrules)
515 {
516 @portruleline = split( /\;/, $portruleentry );
517 if ( $portruleline[1] eq $qossettings{'IMQ_DEV'} )
518 {
519 $qossettings{'CLASS'} = $portruleline[0];
520 $qossettings{'DEVICE'} = $portruleline[1];
521 $qossettings{'PPROT'} = $portruleline[2];
522 $qossettings{'QIP'} = $portruleline[3];
523 $qossettings{'QPORT'} = $portruleline[4];
524 $qossettings{'DIP'} = $portruleline[5];
525 $qossettings{'DPORT'} = $portruleline[6];
4ed2816f 526 print "\tiptables -t mangle -A QOS-INC -i $qossettings{'RED_DEV'} ";
84cab473
MT
527 if ($qossettings{'QIP'} ne ''){
528 print "-s $qossettings{'QIP'} ";
529 }
530 if ($qossettings{'DIP'} ne ''){
531 print "-d $qossettings{'DIP'} ";
532 }
533 print "-p $qossettings{'PPROT'} ";
534 if (($qossettings{'QPORT'} ne '') || ($qossettings{'DPORT'} ne '')){
535 print "-m multiport ";
536 }
537 if ($qossettings{'QPORT'} ne ''){
538 print "--sport $qossettings{'QPORT'} ";
539 }
540 if ($qossettings{'DPORT'} ne ''){
541 print "--dport $qossettings{'DPORT'} ";
542 }
543 print "-j MARK --set-mark $qossettings{'CLASS'}\n";
4ed2816f 544 print "\tiptables -t mangle -A QOS-INC -i $qossettings{'RED_DEV'} ";
84cab473
MT
545 if ($qossettings{'QIP'} ne ''){
546 print "-s $qossettings{'QIP'} ";
547 }
548 if ($qossettings{'DIP'} ne ''){
549 print "-d $qossettings{'DIP'} ";
550 }
551 print "-p $qossettings{'PPROT'} ";
552 if (($qossettings{'QPORT'} ne '') || ($qossettings{'DPORT'} ne '')){
553 print "-m multiport ";
554 }
555 if ($qossettings{'QPORT'} ne ''){
556 print "--sport $qossettings{'QPORT'} ";
557 }
558 if ($qossettings{'DPORT'} ne ''){
559 print "--dport $qossettings{'DPORT'} ";
560 }
561 print "-j RETURN\n\n";
562 }
563 }
564
f013ab2b
MT
565print <<END
566
567 ### SET LEVEL7-RULES
568END
569;
570 foreach $l7ruleentry (sort @l7rules)
571 {
572 @l7ruleline = split( /\;/, $l7ruleentry );
573 if ( $l7ruleline[1] eq $qossettings{'IMQ_DEV'} )
574 {
575 $qossettings{'CLASS'} = $l7ruleline[0];
576 $qossettings{'DEVICE'} = $l7ruleline[1];
577 $qossettings{'L7PROT'} = $l7ruleline[2];
578 $qossettings{'QIP'} = $l7ruleline[3];
579 $qossettings{'DIP'} = $l7ruleline[4];
580 print "\tiptables -t mangle -A QOS-INC -i $qossettings{'RED_DEV'} ";
581 if ($qossettings{'QIP'} ne ''){
582 print "-s $qossettings{'QIP'} ";
583 }
584 if ($qossettings{'DIP'} ne ''){
585 print "-d $qossettings{'DIP'} ";
586 }
587 print "-m layer7 --l7dir /etc/l7-protocols/protocols --l7proto $qossettings{'L7PROT'} -j MARK --set-mark $qossettings{'CLASS'}\n";
588 print "\tiptables -t mangle -A QOS-INC -i $qossettings{'RED_DEV'} ";
589 if ($qossettings{'QIP'} ne ''){
590 print "-s $qossettings{'QIP'} ";
591 }
592 if ($qossettings{'DIP'} ne ''){
593 print "-d $qossettings{'DIP'} ";
594 }
595 print "-m layer7 --l7dir /etc/l7-protocols/protocols --l7proto $qossettings{'L7PROT'} -j RETURN\n";
596 }
597 }
598
84cab473 599print <<END
84cab473 600 ### REDUNDANT: SET ALL NONMARKED PACKETS TO DEFAULT CLASS
4ed2816f 601 iptables -t mangle -A QOS-INC -i $qossettings{'RED_DEV'} -m mark --mark 0 -j MARK --set-mark $qossettings{'DEFCLASS_INC'}
84cab473 602
e8babc92
MT
603 ### SETTING TOS BITS
604END
605;
606 foreach $classentry (sort @classes)
607 {
608 @classline = split( /\;/, $classentry );
609 $qossettings{'CLASS'} = $classline[1];
610 $qossettings{'TOS'} = abs $classline[7] * 2;
611 if ($qossettings{'TOS'} ne "0") {
612 print "\tiptables -t mangle -A QOS-TOS -m mark --mark $qossettings{'CLASS'} -j TOS --set-tos $qossettings{'TOS'}\n";
613 print "\tiptables -t mangle -A QOS-TOS -m mark --mark $qossettings{'CLASS'} -j RETURN\n";
614 }
615 }
616 foreach $subclassentry (sort @subclasses)
617 {
618 @subclassline = split( /\;/, $subclassentry );
619 $qossettings{'SUBCLASS'} = $subclassline[1];
620 $qossettings{'TOS'} = $subclassline[8];
621 $qossettings{'TOS'} = abs $qossettings{'TOS'} * 2;
622 if ($qossettings{'TOS'} ne "0") {
623 print "\tiptables -t mangle -A QOS-TOS -m mark --mark $qossettings{'SUBCLASS'} -j TOS --set-tos $qossettings{'TOS'}\n";
624 print "\tiptables -t mangle -A QOS-TOS -m mark --mark $qossettings{'SUBCLASS'} -j RETURN\n";
625 }
626 }
627
628print <<END
629
363fb6af
MT
630 ## STARTING COLLECTOR
631 /usr/local/bin/qosd $qossettings{'RED_DEV'} >/dev/null 2>&1
632 /usr/local/bin/qosd $qossettings{'IMQ_DEV'} >/dev/null 2>&1
633
84cab473
MT
634 echo "Quality of Service was successfully started!"
635 exit 0
636 ;;
4ed2816f 637 clear|stop)
84cab473 638 ### RESET EVERYTHING TO A KNOWN STATE
363fb6af 639 killall -9 qosd
84cab473 640 # DELETE QDISCS
b684b13b
MT
641 tc qdisc del dev $qossettings{'RED_DEV'} root
642 tc qdisc del dev $qossettings{'IMQ_DEV'} root
4ed2816f
MT
643 # STOP IMQ-DEVICE
644 ip link set $qossettings{'IMQ_DEV'} down
645 iptables -t mangle --delete PREROUTING -i $qossettings{'RED_DEV'} -j IMQ --todev 0
646 rmmod imq
84cab473 647 # REMOVE & FLUSH CHAINS
b684b13b 648 iptables -t mangle --delete POSTROUTING -o $qossettings{'RED_DEV'} -j QOS-OUT
e8babc92 649 iptables -t mangle --delete POSTROUTING -o $qossettings{'RED_DEV'} -j QOS-TOS
b684b13b
MT
650 iptables -t mangle --flush QOS-OUT
651 iptables -t mangle --delete-chain QOS-OUT
4ed2816f 652 iptables -t mangle --delete PREROUTING -i $qossettings{'RED_DEV'} -j QOS-INC
e8babc92 653 iptables -t mangle --delete PREROUTING -i $qossettings{'RED_DEV'} -j QOS-TOS
b684b13b
MT
654 iptables -t mangle --flush QOS-INC
655 iptables -t mangle --delete-chain QOS-INC
e8babc92
MT
656 iptables -t mangle --flush QOS-TOS
657 iptables -t mangle --delete-chain QOS-TOS
b684b13b 658 rmmod sch_htb
84cab473
MT
659 echo "Quality of Service was successfully cleared!"
660 ;;
4ed2816f 661 gen|generate)
b684b13b
MT
662 echo -n "Generateing the QoS-Scripts..."
663 /usr/bin/perl /var/ipfire/qos/bin/makeqosscripts.pl > /var/ipfire/qos/bin/qos.sh
664 echo ".Done!"
665 exit 0
666 ;;
4ed2816f
MT
667 restart)
668 ### FIRST CLEAR EVERYTHING
669 \$0 clear
670
671 ### THEN START
672 \$0 start
673 ;;
84cab473
MT
674esac
675### EOF
676END
677;
678
679############################################################################################################################
680############################################################################################################################