1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
# Traffic shaper
# This is a extended form of Hierarchal Token Bucket with
# more admin friendly features. Similar in spirt to other shaper scripts
# such as wondershaper.
#
# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2008 Vyatta, Inc.
# All Rights Reserved.
# **** End License ****
{
package ShaperClass;
use strict;
require VyattaConfig;
use VyattaQosMatch;
my %fields = (
id => undef,
dsmark => undef,
_priority => undef,
_rate => undef,
_ceiling => undef,
_burst => undef,
_match => undef,
_limit => undef,
_qdisc => undef,
);
sub new {
my ( $that, $config, $id ) = @_;
my $class = ref($that) || $that;
my $self = {%fields};
$self->{id} = $id;
bless $self, $class;
$self->_define($config);
return $self;
}
sub _define {
my ( $self, $config, $id ) = @_;
my $level = $config->setLevel();
my @matches = ();
$self->{_rate} = $config->returnValue("bandwidth");
defined $self->{_rate} or die "$level bandwidth not defined\n";
$self->{_priority} = $config->returnValue("priority");
$self->{_ceiling} = $config->returnValue("ceiling");
$self->{_burst} = $config->returnValue("burst");
$self->{_limit} = $config->returnValue("queue-limit");
$self->{_qdisc} = $config->returnValue("queue-type");
$self->{dsmark} =
VyattaQosUtil::getDsfield($config->returnValue("set-dscp"));
foreach my $match ($config->listNodes("match")) {
$config->setLevel("$level match $match");
push @matches, new VyattaQosMatch($config);
}
$self->{_match} = \@matches;
}
sub matchRules {
my ($self) = @_;
my $matches = $self->{_match};
return @$matches;
}
sub _getPercentRate {
my ($rate, $speed) = @_;
if ( ! defined $rate ) {
return; # leave rate undef
}
# Rate might be a percentage of speed
if ( $rate =~ /%$/ ) {
my $percent = substr( $rate, 0, length($rate) - 1 );
if ( $percent < 0 || $percent > 100 ) {
die "Invalid percentage bandwidth: $percent\n";
}
$rate = ( $percent * $speed ) / 100.;
} else {
$rate = VyattaQosUtil::getRate($rate);
}
return $rate;
}
sub rateCheck {
my ($self, $limit, $level) = @_;
my $rate = _getPercentRate($self->{_rate}, $limit);
if ($rate > $limit) {
printf STDERR
"Warning: $level\nbandwidth %dKbps > overall bandwidth %dKbps\n",
$rate / 1000, $limit / 1000;
}
my $ceil = _getPercentRate($self->{_ceiling}, $limit);
if (defined $ceil && $ceil < $rate) {
printf STDERR
"Warning: $level\nceiling %dKbps < class bandwidth %dKbps\n",
$ceil / 1000, $rate / 1000;
}
}
sub prioQdisc {
my ($self, $out, $dev, $rate) = @_;
my $prio_id = 0x4000 + $self->{id};
my $limit = $self->{_limit};
print {$out} "handle $prio_id: prio\n";
if ($limit) {
foreach my $i (qw/1 2 3/) {
printf {$out} "qdisc add dev %s parent %x:%d pfifo limit %d\n",
$dev, $prio_id, $i, $limit;
}
}
}
sub sfqQdisc {
my ($self, $out, $dev, $rate ) = @_;
print ${out} "sfq";
print ${out} " limit $self->{_limit}" if ($self->{_limit});
print ${out} "\n";
}
sub fifoQdisc {
my ($self, $out, $dev, $rate) = @_;
print ${out} "pfifo";
print ${out} " limit $self->{_limit}" if ($self->{_limit});
print ${out} "\n";
}
# Red is has way to many configuration options
# make some assumptions to make this sane (based on LARTC)
# average size := 1000 bytes
# limit := queue-limit * average
# max := limit / 8
# min := max / 3
# burst := (2 * min + max) / (3 * average)
sub redQdisc {
my ($self, $out, $dev, $rate) = @_;
my $limit = $self->{_limit};
my $avg = 1000;
my $qlimit;
if (defined $limit) {
$qlimit = $limit * $avg; # red limit in bytes
} else {
# rate is in bits/sec so queue-limit = 8 * 500ms * rate
$qlimit = $rate / 2;
}
my $qmax = $qlimit / 8;
my $qmin = $qmax / 3;
printf ${out} "red limit %d min %d max %d avpkt %d",
$qlimit, $qmin, $qmax, $avg;
printf ${out} " burst %d probability 0.02 bandwidth %d ecn\n",
(2 * $qmin + $qmax) / (3 * $avg), $rate / 1000;
}
my %qdiscOptions = (
'priority' => \&prioQdisc,
'fair-queue' => \&sfqQdisc,
'random-detect' => \&redQdisc,
'drop-tail' => \&fifoQdisc,
);
sub htbClass {
my ( $self, $out, $dev, $parent, $speed ) = @_;
my $rate = _getPercentRate($self->{_rate}, $speed);
my $ceil = _getPercentRate($self->{_ceiling}, $speed);
printf ${out} "class add dev %s parent %x:1 classid %x:%x htb rate %s",
$dev, $parent, $parent, $self->{id}, $rate;
print ${out} " ceil $self->{_ceiling}" if ( defined $self->{_ceiling} );
print ${out} " burst $self->{_burst}" if ( defined $self->{_burst} );
print ${out} " prio $self->{_priority}" if ( defined $self->{_priority} );
print {$out} "\n";
# create leaf qdisc
my $q = $qdiscOptions{$self->{_qdisc}};
if (defined $q) {
printf {$out} "qdisc add dev %s parent %x:%x ",
$dev, $parent, $self->{id};
$q->($self, $out, $dev, $rate);
} else {
die "Unknown queue type $self->{_qdisc}\n";
}
}
sub dsmarkClass {
my ( $self, $out, $parent, $dev ) = @_;
printf ${out} "class change dev %s classid %x:%x dsmark",
$dev, $parent, $self->{id};
if ($self->{dsmark}) {
print ${out} " mask 0 value $self->{dsmark}\n";
} else {
print ${out} " mask 0xff value 0\n";
}
}
}
package VyattaQosTrafficShaper;
use strict;
require VyattaConfig;
use VyattaQosUtil;
my %fields = (
_level => undef,
_rate => undef,
_classes => undef,
);
# new VyattaQosTrafficShaper($config)
# Create a new instance based on config information
sub new {
my ( $that, $config, $name ) = @_;
my $self = {%fields};
my $class = ref($that) || $that;
bless $self, $class;
$self->_define($config);
return $self;
}
# Rate can be something like "auto" or "10.2mbit"
sub _getAutoRate {
my ($rate, $dev) = @_;
if ( $rate eq "auto" ) {
$rate = VyattaQosUtil::interfaceRate($dev);
if ( ! defined $rate ) {
die "Interface speed defined as auto but can't get rate from $dev\n";
}
} else {
$rate = VyattaQosUtil::getRate($rate);
}
return $rate;
}
# Setup new instance.
# Assumes caller has done $config->setLevel to "traffic-shaper $name"
sub _define {
my ( $self, $config ) = @_;
my $level = $config->setLevel();
my @classes = ( );
$self->{_rate} = $config->returnValue("bandwidth");
$self->{_level} = $level;
$config->exists("default")
or die "$level configuration not complete: missing default class\n";
# make sure no clash of different types of tc filters
my %matchTypes = ();
foreach my $class ( $config->listNodes("class")) {
foreach my $match ( $config->listNodes("class $class match") ) {
foreach my $type ( $config->listNodes("class $class match $match") ) {
$matchTypes{$type} = "$class match $match";
}
}
}
if (scalar keys %matchTypes > 1 && $matchTypes{ip}) {
print "Match type conflict:\n";
while (my ($type, $usage) = each(%matchTypes)) {
print " class $usage $type\n";
}
die "$level can not match on both ip and other types\n";
}
$config->setLevel("$level default");
push @classes, new ShaperClass($config, -1);
$config->setLevel($level);
foreach my $id ( $config->listNodes("class") ) {
$config->setLevel("$level class $id");
push @classes, new ShaperClass( $config, $id );
}
$self->{_classes} = \@classes;
}
sub commands {
my ( $self, $out, $dev ) = @_;
my $rate = _getAutoRate($self->{_rate}, $dev);
my $classes = $self->{_classes};
my %dsmark = ();
my $default = shift @$classes;
my $maxid = 1;
$default->rateCheck($rate, "$self->{_level} default");
foreach my $class (@$classes) {
$class->rateCheck($rate, "$self->{_level} class $class->{id}");
# find largest class id
if (defined $class->{id} && $class->{id} > $maxid) {
$maxid = $class->{id};
}
}
# fill in id of default
$default->{id} = ++$maxid;
unshift @$classes, $default;
# Check if we need dsmrk
my $usedsmark;
foreach my $class (@$classes) {
if (defined $class->{dsmark}) {
$usedsmark = 1;
last;
}
}
my $parent = 1;
my $root = "root";
if ($usedsmark) {
# dsmark max index must be power of 2
my $indices = $maxid + 1;
while (($indices & ($indices - 1)) != 0) {
++$indices;
}
print {$out} "qdisc add dev $dev handle 1:0 root dsmark"
. " indices $indices default_index $default->{id}\n";
foreach my $class (@$classes) {
$class->dsmarkClass($out, 1, $dev);
if ($class->{dsmark}) {
foreach my $match ($class->matchRules()) {
$match->filter($out, $dev, 1, $class->{id});
}
}
}
$parent = $indices + 1;
$root = "parent 1:1"
}
printf {$out} "qdisc add dev %s %s handle %x: htb default %x\n",
$dev, $root, $parent, $default->{id};
printf {$out} "class add dev %s parent %x: classid %x:1 htb rate %s\n",
$dev, $parent, $parent, $rate;
foreach my $class (@$classes) {
$class->htbClass($out, $dev, $parent, $rate);
foreach my $match ($class->matchRules()) {
$match->filter($out, $dev, $parent, $class->{id});
}
}
}
1;
|