Fix typo in 'makeqosscripts.pl'
[ipfire-2.x.git] / config / qos / makeqosscripts.pl
CommitLineData
84cab473 1#!/usr/bin/perl
70df8302
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
38288aee 5# Copyright (C) 2007-2013 IPFire Team <info@ipfire.org> #
70df8302
MT
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###############################################################################
84cab473
MT
21
22use strict;
23# enable only the following on debugging purpose
e8babc92 24# use warnings;
84cab473
MT
25
26require '/var/ipfire/general-functions.pl';
27require "${General::swroot}/lang.pl";
28require "${General::swroot}/header.pl";
29
30my %qossettings = ();
31my %checked = ();
32my %netsettings = ();
33my $message = "";
34my $errormessage = "";
35my $c = "";
36my $direntry = "";
37my $classentry = "";
38my $subclassentry = "";
39my $l7ruleentry = "";
40my $portruleentry = "";
e8babc92 41my $tosruleentry = "";
84cab473
MT
42my @tmp = ();
43my @classes = ();
44my @subclasses = ();
45my @l7rules = ();
46my @portrules = ();
e8babc92 47my @tosrules = ();
84cab473
MT
48my @tmpline = ();
49my @classline = ();
50my @subclassline = ();
e8babc92 51my @tosruleline = ();
84cab473
MT
52my @l7ruleline = ();
53my @portruleline = ();
54my @proto = ();
55my %selected= () ;
56my $classfile = "/var/ipfire/qos/classes";
57my $subclassfile = "/var/ipfire/qos/subclasses";
58my $level7file = "/var/ipfire/qos/level7config";
59my $portfile = "/var/ipfire/qos/portconfig";
e8babc92 60my $tosfile = "/var/ipfire/qos/tosconfig";
2950a55c 61my $fqcodel_options = "limit 800 quantum 500";
84cab473
MT
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'} = '';
84cab473
MT
74$qossettings{'RED_DEV'} = `cat /var/ipfire/red/iface`;
75$qossettings{'IMQ_DEV'} = 'imq0';
e8babc92 76$qossettings{'TOS'} = '';
84cab473 77$qossettings{'VALID'} = 'yes';
88b8ffac 78$qossettings{'IMQ_MODE'} = 'PREROUTING';
84cab473
MT
79
80&General::readhash("${General::swroot}/qos/settings", \%qossettings);
81
82open( FILE, "< $classfile" ) or die "Unable to read $classfile";
83@classes = <FILE>;
84close FILE;
85open( FILE, "< $subclassfile" ) or die "Unable to read $subclassfile";
86@subclasses = <FILE>;
87close FILE;
88open( FILE, "< $level7file" ) or die "Unable to read $level7file";
89@l7rules = <FILE>;
90close FILE;
91open( FILE, "< $portfile" ) or die "Unable to read $portfile";
92@portrules = <FILE>;
93close FILE;
e8babc92
MT
94open( FILE, "< $tosfile" ) or die "Unable to read $tosfile";
95@tosrules = <FILE>;
96close FILE;
84cab473
MT
97
98############################################################################################################################
99############################################################################################################################
100
101print <<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
b3992117
AF
114eval \$(/usr/local/bin/readhash /var/ipfire/main/settings)
115if [ "\$RRDLOG" == "" ]; then
c772568a
AF
116 RRDLOG=/var/log/rrd
117fi
118
84cab473
MT
119case "\$1" in
120
121 status)
4ed2816f
MT
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]"
dd001658
MT
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
4ed2816f
MT
146 exit 0
147 ;;
148 esac
149 \$0 \$1 qdisc
150 \$0 \$1 class
151 \$0 \$1 filter
152 \$0 \$1 iptables
84cab473
MT
153 exit 0
154 ;;
155 start)
84cab473
MT
156 ###
157 ### $qossettings{'RED_DEV'}
158 ###
159
160 ### INIT KERNEL
161 modprobe sch_htb
162
84cab473 163 ### ADD HTB QDISC FOR $qossettings{'RED_DEV'}
38288aee 164 tc qdisc del dev $qossettings{'RED_DEV'} root >/dev/null 2>&1
84cab473
MT
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'}
171END
172;
173foreach $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'} ";
4ed2816f 185 if (($qossettings{'BURST'} ne '') && ($qossettings{'BURST'} ne 0)) {
84cab473
MT
186 print "burst $qossettings{'BURST'}k ";
187 }
b684b13b 188 if (($qossettings{'CBURST'} ne '') && ($qossettings{'CBURST'} ne 0)) {
84cab473
MT
189 print "cburst $qossettings{'CBURST'}k";
190 }
191 print "\n";
192 }
193}
194foreach $subclassentry (sort @subclasses) {
195 @subclassline = split( /\;/, $subclassentry );
92b5fba4
MT
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";
84cab473 213 }
84cab473
MT
214}
215
216print "\n\t### ATTACH QDISC TO LEAF CLASSES\n";
217foreach $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];
24dbd0f7 223 print "\ttc qdisc add dev $qossettings{'DEVICE'} parent 1:$qossettings{'CLASS'} handle $qossettings{'CLASS'}: fq_codel $fqcodel_options\n";
84cab473
MT
224 }
225}
226foreach $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];
24dbd0f7 231 print "\ttc qdisc add dev $qossettings{'DEVICE'} parent 1:$qossettings{'SCLASS'} handle $qossettings{'SCLASS'}: fq_codel $fqcodel_options\n";
84cab473
MT
232 }
233}
234print "\n\t### FILTER TRAFFIC INTO CLASSES\n";
235foreach $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}
244foreach $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];
92b5fba4 250 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
251 }
252}
253print <<END
254
dcd81b26
AF
255 ### add l7-filter to POSTROUTING chain to see all traffic
256 iptables -t mangle -A POSTROUTING -m layer7 --l7proto unset
257
84cab473
MT
258 ### ADD QOS-OUT CHAIN TO THE MANGLE TABLE IN IPTABLES
259 iptables -t mangle -N QOS-OUT
e8babc92 260 iptables -t mangle -N QOS-TOS
84cab473 261 iptables -t mangle -I POSTROUTING -o $qossettings{'RED_DEV'} -j QOS-OUT
e8babc92 262 iptables -t mangle -A POSTROUTING -o $qossettings{'RED_DEV'} -j QOS-TOS
84cab473 263
5487c71b
AF
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
84cab473 267 ### MARK ACKs
4c43d644
AF
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
84cab473 271
4c43d644
AF
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
84cab473 274
4c43d644
AF
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
84cab473 278
4c43d644
AF
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
84cab473 282
4c43d644
AF
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
84cab473 286
4c43d644
AF
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
84cab473 290
4c43d644
AF
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
84cab473 294
4c43d644
AF
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
84cab473 298
e8babc92
MT
299 ### SET TOS
300END
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 {
4c43d644
AF
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";
e8babc92
MT
311 }
312 }
313
84cab473
MT
314print "\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];
4c43d644 327 print "\tiptables -t mangle -A QOS-OUT ";
84cab473
MT
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'} ";
d2414ee7
CS
335# if (($qossettings{'QPORT'} ne '') || ($qossettings{'DPORT'} ne '')){
336# print "-m multiport ";
337# }
84cab473
MT
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";
4c43d644 345 print "\tiptables -t mangle -A QOS-OUT ";
84cab473
MT
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'} ";
d2414ee7
CS
353# if (($qossettings{'QPORT'} ne '') || ($qossettings{'DPORT'} ne '')){
354# print "-m multiport ";
355# }
84cab473
MT
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
f013ab2b
MT
366print <<END
367
368 ### SET LEVEL7-RULES
369END
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];
4c43d644 381 print "\tiptables -t mangle -A QOS-OUT ";
f013ab2b
MT
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";
4c43d644 389 print "\tiptables -t mangle -A QOS-OUT ";
f013ab2b
MT
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
84cab473
MT
400print <<END
401
402 ### REDUNDANT: SET ALL NONMARKED PACKETS TO DEFAULT CLASS
4c43d644 403 iptables -t mangle -A QOS-OUT -m mark --mark 0 -j MARK --set-mark $qossettings{'DEFCLASS_OUT'}
84cab473
MT
404
405 ###
406 ### $qossettings{'IMQ_DEV'}
407 ###
408
409 ### BRING UP $qossettings{'IMQ_DEV'}
b684b13b
MT
410 if [ `lsmod | grep -q ipt_IMQ` ]; then
411 insmod ipt_IMQ
412 sleep 2
413 fi
b3ee263b 414 modprobe imq numdevs=1 numqueues=\$(grep -c "^processor" /proc/cpuinfo || echo 1)
84cab473
MT
415 ip link set $qossettings{'IMQ_DEV'} up
416
84cab473 417 ### ADD HTB QDISC FOR $qossettings{'IMQ_DEV'}
38288aee 418 tc qdisc del dev $qossettings{'IMQ_DEV'} root >/dev/null 2>&1
84cab473
MT
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'}
425END
426;
427foreach $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'} ";
4ed2816f 439 if (($qossettings{'BURST'} ne '') && ($qossettings{'BURST'} ne 0)) {
84cab473
MT
440 print "burst $qossettings{'BURST'}k ";
441 }
4ed2816f 442 if (($qossettings{'CBURST'} ne '') && ($qossettings{'CBURST'} ne 0)) {
84cab473
MT
443 print "cburst $qossettings{'CBURST'}k";
444 }
445 print "\n";
446 }
447}
448foreach $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 }
4ed2816f 463 if (($qossettings{'SCBURST'} ne '') && ($qossettings{'SCBURST'} ne 0)) {
84cab473
MT
464 print "cburst $qossettings{'CBURST'}k";
465 }
466 print "\n";
467 }
468}
469
470print "\n\t### ATTACH QDISC TO LEAF 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];
24dbd0f7 477 print "\ttc qdisc add dev $qossettings{'DEVICE'} parent 2:$qossettings{'CLASS'} handle $qossettings{'CLASS'}: fq_codel $fqcodel_options\n";
84cab473
MT
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{'SCLASS'} = $subclassline[2];
24dbd0f7 485 print "\ttc qdisc add dev $qossettings{'DEVICE'} parent 2:$qossettings{'SCLASS'} handle $qossettings{'SCLASS'}: fq_codel $fqcodel_options\n";
84cab473
MT
486 }
487}
488print "\n\t### FILTER TRAFFIC INTO CLASSES\n";
489foreach $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}
498foreach $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];
92b5fba4 504 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
505 }
506}
88b8ffac
AF
507
508if ( $qossettings{'IMQ_MODE'} eq 'POSTROUTING' )
509{
84cab473
MT
510print <<END
511
4ed2816f 512 ### ADD QOS-INC CHAIN TO THE MANGLE TABLE IN IPTABLES
84cab473 513 iptables -t mangle -N QOS-INC
b83d68f3
AF
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
dd79c399 517 iptables -t mangle -A POSTROUTING -m mark ! --mark 0 ! -o $qossettings{'RED_DEV'} -j IMQ --todev 0
fee974f5
AF
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
e8babc92
MT
520
521 ### SET TOS
522END
523;
88b8ffac
AF
524}
525else
526{
527print <<END
528
529 ### ADD QOS-INC CHAIN TO THE MANGLE TABLE IN IPTABLES
530 iptables -t mangle -N QOS-INC
b83d68f3
AF
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
88b8ffac
AF
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
539END
540;
541}
542
e8babc92
MT
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 {
4c43d644
AF
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";
e8babc92
MT
552 }
553
554 }
555
84cab473
MT
556print "\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];
4c43d644 569 print "\tiptables -t mangle -A QOS-INC ";
84cab473
MT
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'} ";
d2414ee7
CS
577# if (($qossettings{'QPORT'} ne '') || ($qossettings{'DPORT'} ne '')){
578# print "-m multiport ";
579# }
84cab473
MT
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";
4c43d644 587 print "\tiptables -t mangle -A QOS-INC ";
84cab473
MT
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'} ";
d2414ee7
CS
595# if (($qossettings{'QPORT'} ne '') || ($qossettings{'DPORT'} ne '')){
596# print "-m multiport ";
597# }
84cab473
MT
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
f013ab2b
MT
608print <<END
609
610 ### SET LEVEL7-RULES
611END
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];
4c43d644 623 print "\tiptables -t mangle -A QOS-INC ";
f013ab2b
MT
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";
4c43d644 631 print "\tiptables -t mangle -A QOS-INC ";
f013ab2b
MT
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
84cab473 642print <<END
84cab473 643 ### REDUNDANT: SET ALL NONMARKED PACKETS TO DEFAULT CLASS
4c43d644 644 iptables -t mangle -A QOS-INC -m mark --mark 0 -j MARK --set-mark $qossettings{'DEFCLASS_INC'}
84cab473 645
e8babc92
MT
646 ### SETTING TOS BITS
647END
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
671print <<END
672
363fb6af 673 ## STARTING COLLECTOR
d86ddb48
MT
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) &
363fb6af 676
c772568a
AF
677 for i in \$(ls \$RRDLOG/class_*.rrd); do
678 rrdtool update \$i \$(date +%s):
679 done
680
84cab473
MT
681 echo "Quality of Service was successfully started!"
682 exit 0
683 ;;
4ed2816f 684 clear|stop)
84cab473 685 ### RESET EVERYTHING TO A KNOWN STATE
a4109682 686 killall qosd >/dev/null 2>&1
f89a66e0 687 (sleep 3 && killall -9 qosd &>/dev/null) &
84cab473 688 # DELETE QDISCS
24dbd0f7 689 tc qdisc del dev $qossettings{'RED_DEV'} root >/dev/null 2>&1
7f39519e 690 tc qdisc add root dev $qossettings{'RED_DEV'} fq_codel >/dev/null 2>&1
a4109682 691 tc qdisc del dev $qossettings{'IMQ_DEV'} root >/dev/null 2>&1
7f39519e 692 tc qdisc add root dev $qossettings{'IMQ_DEV'} fq_codel >/dev/null 2>&1
4ed2816f 693 # STOP IMQ-DEVICE
a4109682 694 ip link set $qossettings{'IMQ_DEV'} down >/dev/null 2>&1
b83d68f3
AF
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
dd79c399 701 iptables -t mangle --delete POSTROUTING -m mark ! --mark 0 ! -o $qossettings{'RED_DEV'} -j IMQ --todev 0 >/dev/null 2>&1
88b8ffac 702 iptables -t mangle --delete PREROUTING -i $qossettings{'RED_DEV'} -j IMQ --todev 0 >/dev/null 2>&1
a4109682 703 # rmmod imq # this crash on 2.6.25.xx
84cab473 704 # REMOVE & FLUSH CHAINS
a4109682
AF
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
88b8ffac
AF
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
a4109682
AF
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
dcd81b26
AF
717 # remove l7-filter
718 iptables -t mangle --delete POSTROUTING -m layer7 --l7proto unset
719
a4109682 720 rmmod sch_htb >/dev/null 2>&1
c772568a
AF
721
722 for i in \$(ls \$RRDLOG/class_*.rrd); do
723 rrdtool update \$i \$(date +%s):
724 done
725
84cab473
MT
726 echo "Quality of Service was successfully cleared!"
727 ;;
4ed2816f 728 gen|generate)
be7878d5 729 echo -n "Generating the QoS-Scripts..."
b684b13b
MT
730 /usr/bin/perl /var/ipfire/qos/bin/makeqosscripts.pl > /var/ipfire/qos/bin/qos.sh
731 echo ".Done!"
732 exit 0
733 ;;
4ed2816f
MT
734 restart)
735 ### FIRST CLEAR EVERYTHING
736 \$0 clear
737
738 ### THEN START
739 \$0 start
740 ;;
84cab473
MT
741esac
742### EOF
743END
744;
745
746############################################################################################################################
747############################################################################################################################