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