-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExecute.pm
274 lines (220 loc) · 7.39 KB
/
Execute.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package Dynshaper::Execute;
#######################################################################
#
# description: setzt die Dynshaper-Parameter in tc-Befehle um
#
# author: (c) 2005 Markus Schade <[email protected]>
#
# version: $Id$
#
# license: GPLv2
#
#######################################################################
use strict;
use Carp;
use Dynshaper::Const;
use base qw(Dynshaper::Data);
$ENV{PATH} = '/sbin:/bin';
sub new {
my ($class, %args) = @_;
my $self = bless {}, $class;
$self->load_data();
return $self;
}
sub restart {
my $self = shift;
$self->start();
}
sub start {
my $self = shift;
my $c = {};
my $uclass = 400; #Nutzerklassen beginnen bei 400
my $cmd = '';
my $log = '';
my $log2 = '';
if ($self->get('config',1,'conf_adjust')) {
$self->prepare_config_for_exec()
}
for my $option ('mppath','tcpath','devint','devext',
'bwext','bwint') {
$c->{$option} = $self->get('config',1,"conf_$option");
}
$self->do_log( $DEBUG{NOTICE}, qq(Starting Dynshaper HTB: ));
# Laden der benoetigten Module
$log = qq(mod);
for my $module ("sch_htb", "cls_u32", "cls_fw") {
$log2 = qx($c->{mppath} $module 2>&1);
$self->do_log( $DEBUG{ERROR}, $log2) if $log2;
}
$self->do_log( $DEBUG{NOTICE}, qq($log.));
# jetzt fuer alle Interfaces regeln anlegen
for my $device ( $c->{devint}, $c->{devext} ) {
my $bandwidth=0;
my $exs = $self->get_ptr('excepts');
my $grps = $self->get_ptr('groups');
$log = qq( if:$device);
if ($device eq $c->{devint}) {
$bandwidth=$c->{bwint};
}
else {
$bandwidth=$c->{bwext};
}
# Alle Regeln und Klassen loeschen
$log2 = qx($c->{tcpath} qdisc del dev $device root 2>&1);
# Neue Root qdisc mit HTB anlegen
# default Klasse fuer nicht eingeordneten Traffic 1:30
# Bandbreite wird mit dem Faktor 10 in Quanten umgerechnet
$cmd = qq($c->{tcpath} qdisc add dev $device root handle 1:);
$cmd .= qq( htb default 30 r2q 10);
$log2 .= qx($cmd 2>&1);
# Wurzelklasse erzeugen
$cmd = qq($c->{tcpath} class add dev $device parent 1:);
$cmd .= qq( classid 1:1 htb rate $bandwidth);
$log2 .= qx($cmd 2>&1);
# Linker Zweig (Ausnahmen)
$cmd = qq($c->{tcpath} class add dev $device parent 1:1);
$cmd .= qq( classid 1:2 htb rate $bandwidth);
$log2 .= qx($cmd 2>&1);
# Rechter Zweig (Nutzerklassen)
$cmd = qq($c->{tcpath} class add dev $device parent 1:1);
$cmd .= qq( classid 1:3 htb rate $bandwidth);
$log2 .= qx($cmd 2>&1);
# Defaultklasse (minimale Rate 12kbit/s, nach oben nur durch
# Interface-BB begrenzt) erbt die gesamte freie Bandreite
$cmd = qq($c->{tcpath} class add dev $device parent 1:3);
$cmd .= qq( classid 1:30 htb rate 12kbit ceil $bandwidth prio 5);
$cmd .= qq( quantum 1500);
$log2 .= qx($cmd 2>&1);
$self->do_log( $DEBUG{ERROR}, $log2) if $log2;
$self->do_log( $DEBUG{NOTICE}, $log);
#
# Ausnahmen
#
$log2 = '';
$log = qq( ex:);
foreach my $except (keys %{$exs}) {
$log .= qq($except );
my $ex_in = $self->get('excepts',$except,'conf_in');
my $ex_out = $self->get('excepts',$except,'conf_out');
my $ex_bound = $self->get('excepts',$except,'conf_bound');
my $ex_rate = $self->get('excepts',$except,'conf_rate');
my $ex_prio = $self->get('excepts',$except,'conf_prio');
if (( $device eq $c->{devint} && $ex_in ) ||
( $device eq $c->{devext} && $ex_out)) {
my $bounded = '';
$bounded = $ex_bound ? "ceil $ex_rate" : "ceil $bandwidth";
$cmd = qq($c->{tcpath} class add dev $device parent 1:2 );
$cmd .= qq( classid 1:2$except htb rate $ex_rate prio $ex_prio $bounded);
$log2 .= qx($cmd 2>&1);
if ( $device eq $c->{devint} ) {
for my $match (@$ex_in) {
$cmd = qq($c->{tcpath} filter add dev $device parent 1:0 );
$cmd .= qq(protocol ip prio $ex_prio u32 $match flowid 1:2$except);
$log2 .= qx($cmd 2>&1);
}
}
else {
for my $match (@$ex_out) {
$cmd = qq($c->{tcpath} filter add dev $device parent 1:0 );
$cmd .= qq(protocol ip prio $ex_prio u32 $match flowid 1:2$except);
$log2 .= qx($cmd 2>&1);
}
}
}
$self->do_log( $DEBUG{ERROR}, $log2) if $log2;
} # foreach my $except
$log .= qq(.);
$self->do_log( $DEBUG{NOTICE}, $log);
#
# Nutzerklassen
#
$log2 = '';
$log = qq( grps:);
foreach my $group (sort {$a <=> $b} keys %{$grps}) {
$log .= qq($group );
my $grp_in = $self->get('groups',$group,'conf_in');
my $grp_out = $self->get('groups',$group,'conf_out');
my $grp_rate = $self->get('groups',$group,'conf_rate');
if (( ($device eq $c->{devint}) && $grp_in ) ||
( ($device eq $c->{devext}) && $grp_out)) {
$cmd = qq($c->{tcpath} class add dev $device parent 1:3 );
$cmd .= qq(classid 1:3$group htb rate $grp_rate ceil $grp_rate);
$log2 = qx($cmd 2>&1);
my $nutzer = $self->get_ptr('users');
foreach my $user (keys %{$nutzer}) {
my $user_group = $self->get('users',$user,'gruppe');
if ($group == $user_group) {
my $umarks = $self->get_ptr('users',$user,'fwmark');
# Nutzer ueberspringen, wenn keine fwmarks existieren
# d.h. in der Regel, dass der Nutzer gesperrt ist
# Kommt nur bei DB-gestutzter Ausfuehrung vor, weil hier
# alle Nutzer initialisiert werden, egal ob effektiv
# freigeschaltet oder nicht
next unless defined $umarks;
$cmd = qq($c->{tcpath} class add dev $device parent 1:3$group);
$cmd .= qq( classid 1:$uclass htb rate 12kbit ceil $grp_rate prio 7);
$cmd .= qq( quantum 1500);
$log2 .= qx($cmd 2>&1);
my $oldmark = 0;
foreach my $usermark (@{$umarks}) {
next if $usermark == $oldmark;
$cmd = qq($c->{tcpath} filter add dev $device parent 1:0);
$cmd .= qq( protocol ip prio 100 handle $usermark fw );
$cmd .= qq( flowid 1:$uclass);
$log2 .= qx($cmd 2>&1);
$oldmark=$usermark;
}
$uclass++;
}
} #foreach my $user
$self->do_log( $DEBUG{ERROR}, $log2) if $log2;
} #endif
} #foreach my $group
$log .= qq(.);
$self->do_log( $DEBUG{NOTICE}, $log);
} # for my $device
$self->do_log( $DEBUG{NOTICE}, "\n");
}
sub stop {
my $self = shift;
my $c = {};
my $log = '';
for my $option ('tcpath','devint','devext') {
$c->{$option} = $self->get('config',1,"conf_$option");
}
$self->do_log( $DEBUG{NOTICE}, qq(Stopping Dynshaper HTB: ));
for my $device ( $c->{devint}, $c->{devext} ) {
$log = qx($c->{tcpath} qdisc del dev $device root 2>&1);
$self->do_log( $DEBUG{DEBUG}, $log) if $log;
}
$self->do_log( $DEBUG{NOTICE}, qq(done.\n) );
}
sub list {
my $self = shift;
my $c = {};
my $log = '';
for my $option ('tcpath','devint','devext') {
$c->{$option} = $self->get('config',1,"conf_$option");
}
for my $device ( $c->{devint}, $c->{devext} ) {
$log .= qx($c->{tcpath} qdisc ls dev $device 2>&1);
$log .= qx($c->{tcpath} class ls dev $device 2>&1);
$log .= qx($c->{tcpath} filter ls dev $device 2>&1);
}
$self->do_log( $DEBUG{DEBUG}, $log);
}
sub status {
my $self= shift;
my $c = {};
my $log = '';
for my $option ('tcpath','devint','devext') {
$c->{$option} = $self->get('config',1,"conf_$option");
}
for my $device ( $c->{devint}, $c->{devext} ) {
$log .= qx($c->{tcpath} -s qdisc ls dev $device 2>&1);
$log .= qx($c->{tcpath} -s class ls dev $device 2>&1);
$log .= qx($c->{tcpath} filter ls dev $device 2>&1);
}
$self->do_log( $DEBUG{DEBUG}, $log);
}
1;