-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbq
executable file
·347 lines (328 loc) · 10.5 KB
/
cbq
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
#!/usr/bin/perl
# CBQ Script, (C) 2001 Chris Kennedy <getdown@groovy.org>
$tc = "/sbin/tc";
$location = "/etc/fw/base/cbq";
$total_bandw = '10Mbit';
$total_rate = '10Mbit';
$total_weigh = '1Mbit';
$active = 1;
$sfq = 0;
($ACTION, $verbose) = @ARGV;
if ($ACTION =~ /^sta/) {
`$0 stop >/dev/null 2>&1`;
$ACTION = 'add';
} elsif ($ACTION =~ /^sto/) {
$ACTION = 'del';
} elsif ($ACTION =~ /^li/) {
opendir(DEVICES,"$location");
@devices = grep !/^\.\.?$/,readdir DEVICES;
closedir(DEVICES);
foreach (@devices) {
$DEV = $_;
$DEV =~ s/\n//g;
if ($DEV !~ /^[a-zA-Z0-9]+\d+$/){next};
print "interface $DEV\n";
&show_rules;
}
exit 0;
} elsif ($ACTION =~ /^cl/) {
opendir(DEVICES,"$location");
@devices = grep !/^\.\.?$/,readdir DEVICES;
closedir(DEVICES);
foreach (@devices) {
$DEV = $_;
$DEV =~ s/\n//g;
if ($DEV !~ /^[a-zA-Z0-9]+\d+$/){next};
print "Interface $DEV\n";
&show_classes;
}
exit 0;
} else {
&error;
exit 1;
}
if ($ACTION eq 'del') {
$NULL = " >/dev/null 2>&1";
}
#######################################################
opendir(DEVICES,"$location");
@devices = grep !/^\.\.?$/,readdir DEVICES;
closedir(DEVICES);
$i = 100; $j = 0;
$x = 0; $y = 1;
foreach (@devices) {
$x += 1; $y += 1;
$i = 100; $j = 0;
$results = '';
$DEV = $_;
$DEV =~ s/\n//g;
if ($DEV !~ /^[a-zA-Z0-9]+\d+$/){next};
$j++;
## ROOT CBQ DEVICE 1:0
$cmd = "$tc qdisc $ACTION dev $DEV root handle $j:0 cbq ";
#$cmd.= "bandwidth $total_bandw avpkt 1000 mpu 64";
$cmd.= "bandwidth $total_bandw avpkt 1000";
# Execute command
if ($active == 1){$results = `$cmd $NULL`};
if ($verbose == 1) {
($a, $b) = split (/ cbq /, $cmd);
($b, $c) = split (/ prio /, $b);
printf ("%s cbq \\\n\t%s", $a, $b);
if($c) {
printf (" \\\n\tprio %s\n", $c);
} else {
printf ("\n");
}
}
## CLASS BASE CBQ DEVICE 1:1
$cmd = "$tc class $ACTION dev $DEV parent $j:0 classid $j:1 cbq ";
#$cmd.= "bandwidth $total_bandw rate $total_rate ";
$cmd.= "rate $total_rate ";
#$cmd.= "weight $total_weigh prio 8 maxburst 20 avpkt 1000 bounded isolated";
$cmd.= "allot 1500 prio 5 bounded isolated";
# Execute command
if ($active == 1){$results.= `$cmd $NULL`};
if ($verbose == 1) {
($a, $b) = split (/ cbq /, $cmd);
($b, $c) = split (/ prio /, $b);
printf (" %s cbq \\\n\t%s", $a, $b);
if($c) {
printf (" \\\n\tprio %s\n", $c);
} else {
printf ("\n");
}
}
if ($ACTION eq 'del') {
next;
}
$priority = 1;
## Get Configuration for Interface
open (RULES, "<$location/$_");
while (<RULES>) {
$rule = $_;
$rule =~ s/\n//g;
if ($rule =~ /^\!/ || $rule =~ /^$/){next};
# CLASS BASE OF BANDWIDTH
if ($rule =~ /^class /) {
# INCREMENT FLOWID
$i++;
($j1, $pri, $indiv_limit, $indiv_weight, $bounded, $isolated) =
split (/ /, $rule);
$cmd = "$tc class $ACTION dev $DEV parent $j:1 classid $j:$i cbq ";
#$cmd.= "bandwidth $total_bandw rate $indiv_limit ";
$cmd.= "rate $indiv_limit ";
#$cmd.= "weight $indiv_weight prio $pri maxburst 20 avpkt 1000 ";
$cmd.= "allot 1500 prio $pri ";
$cmd.= "$bounded $isolated";
# Execute command
if ($active == 1){$results.= `$cmd $NULL`};
if ($verbose == 1) {
($a, $b) = split (/ cbq /, $cmd);
($b, $c) = split (/ prio /, $b);
printf (" %s cbq \\\n\t%s", $a, $b);
if($c) {
printf (" \\\n\tprio %s\n", $c);
} else {
printf ("\n");
}
}
# Choose type of queue, SFQ or TB.
if ($sfq == 1) {
$cmd = "$tc qdisc $ACTION dev $DEV parent $j:$i sfq quantum 1514b ";
$cmd.= "perturb 10 $NULL";
} else {
$cmd = "$tc qdisc $ACTION dev $DEV parent $j:$i tbf ";
#$cmd.= "rate $total_rate buffer $total_rate/8 limit $total_weigh";
$cmd.= "rate $total_rate latency 50ms burst 1540";
}
# Execute command
if ($active == 1){$results.= `$cmd $NULL`};
if ($verbose == 1) {
print " $cmd\n";
}
} elsif ($rule =~ /^ match/) {
$rule =~ s/^\s+//g;
$rule =~ s/\n//g;
$cmd = "$tc filter $ACTION dev $DEV parent $j:0 protocol ip prio $priority u32 ";
#$cmd.= "$rule flowid $j:$i";
$cmd.= "$rule police rate $indiv_limit burst 10k drop flowid $j:$i";
# Execute command
if ($active == 1){$results.= `$cmd $NULL`};
if ($verbose == 1) {
($a, $b) = split (/ u32 /, $cmd);
print " $a u32 \\\n\t$b\n";
print "\n";
}
}
}
close (RULES);
print "\nFinished Adding Traffic Control Rules:\n\n";
}
## SUBROUTINES
sub error
{
print "Usage: $0 [start|stop|list|class] [1]\n";
print "\tstart/stop - Start or Stop Rules.\n";
print "\tlist - List out current Rules.\n";
print "\tclass - List out current Classes Stats.\n";
print "\t1 - Run with 'start 1' to list out commands too.\n";
}
sub show_rules
{
open (RULES, "<$location/$_");
while (<RULES>) {
$rule = $_;
if ($rule =~ /^\!/ || $rule =~ /^$/){next};
print " $rule";
}
print "!\n";
}
sub show_classes
{
@class_output = `$tc -s -d class show dev $DEV`;
@filter_output = `$tc -s -d filter show dev $DEV`;
push @class_output, "class ";
foreach (@class_output) {
if ($_ =~ /^class /) {
if ($line ne '') {
$line =~ s/\n//g;
$line =~ s/\s+/ /g;
$line =~ s/[\(\)\,]//g;
$line =~ s/root/root 0:0/g;
$line =~ s/\:\s/:0 /g;
if ($line !~ /leaf/ && $line !~ /root/) {
$line =~ s/:0 rate/:0 leaf 0: 0 rate/g;
}
if ($line !~ /bounded/ &&
$line !~ /isolated/ &&
$line !~ /cbq\s\d:1\s/)
{
$line =~ s/prio/\(\) prio/g;
}
($j1, $type, $handle, $j2, $parent, $j3, $leaf, $j4, $rate,
$j5, $cell, $flags, $j6, $priority, $j7, $weight, $j8, $allot,
$j9, $level, $j10, $ewma, $j11, $avpkt, $j12, $maxidle,
$j13, $sentbytes, $j14, $sentpkts, $j15, $j16, $dropped,
$j17, $overlimits, $j18, $borrowed, $j19, $overactions,
$j20, $avgidle, $j21, $undertime) = split (/ /, $line);
$hexoff = ''; $match = 0;
foreach $filter (@filter_output) {
if ($filter =~ / flowid $handle /) {
$char1 = ''; $char2 = '';
$match = 1;
}
if ($match == 1 && $filter =~ / match/) {
$filter =~ s/^\s+//g;
($j1, $hexfield, $j2, $offset) = split (/ /, $filter);
($hex1, $hex2) = split (/\//, $hexfield);
while ($hex1 ne '') {
$data = substr($hex1,0,2);
$char = unpack("a4", hex($data));
$char1.= " $char";
$hex1 =~ s/^$data//g;
$counter++;
if ($counter >= 10){print "Looping $hex1\n"; exit 1};
}
$counter = 0;
if ($offset == 16 || $offset == 12) {
$char1 =~ s/^\s+//g;
$char1 =~ s/\s+/./g;
$hex2 =~ s/^0x//g;
while ($hex2 ne '') {
$data = substr($hex2,0,2);
$char = unpack("a4", hex($data));
$char2.= " $char";
$hex2 =~ s/^$data//g;
$counter++;
if ($counter >= 10){print "Looping $hex1\n"; exit 1};
}
$counter = 0;
$char2 =~ s/^\s+//g;
$char2 =~ s/\s+/./g;
$hexoff.= " ip $char1/$char2:";
} elsif ($offset == 8) {
($aa, $a, $b, $c, $d) = split (/ /, $char1);
if ($b == 6) {
$hexoff.= " tcp";
} elsif ($b == 17) {
$hexoff.= " udp";
} elsif ($b == 1) {
$hexoff.= " icmp";
} else {
$hexoff.= " ip $b";
}
} elsif ($offset == 20) {
($aa, $a, $b, $c, $d, $e, $f) = split (/ /, $char1);
$ef = $e * 256;
$port = $ef + $f;
$hexoff.= " port $port:";
} else {
$hexoff.= " match $char1/$hex2 at $offset:";
}
} elsif ($filter !~ / flowid $handle /) {
$match = 0;
}
}
$data = "$type, $handle, $parent, $leaf, $rate, $cell, $flags, $priority,
$weight, $allot, $level, $ewma, $avpkt, $maxidle, $sentbytes,
$sentpkts, $dropped, $overlimits, $borrowed, $overactions, $avgidle,
$undertime, $backlog, $hexoff\n";
$handle =~ s/^\d\://g;
if ($type eq "cbq") {
push @classes, "$handle:$data";
}
$data =~ s/\n/ /g;
$data =~ s/\s+/ /g;
$line = ''; $data = ''; $backlog = '';
}
$line.= $_;
} elsif ($_ =~ /^level /) {
$line.= $_;
} elsif ($_ =~ /^ Sent /) {
$line.= $_;
} elsif ($_ =~ /^ borrowed /) {
$line.= $_;
} elsif ($_ =~ /^\s+backlog /) {
$backlog = $_;
}
}
close (CLASSES);
@classes2 = sort { $a <=> $b } @classes;
@classes = ();
foreach (@classes2) {
$_ =~ s/^(\d+\:+)+//g;
$_ =~ s/\n//g;
$_ =~ s/\s+/ /g;
($type, $handle, $parent, $leaf, $rate, $cell, $flags, $priority,
$weight, $allot, $level, $ewma, $avpkt, $maxidle, $sentbytes,
$sentpkts, $dropped, $overlimits, $borrowed, $overactions, $avgidle,
$undertime, $backlog, $hexoff) = split (/\, /, $_);
if ($detail_stats == 1) {
print "Type: $type Handle: $handle Parent: $parent Leaf: $leaf\n";
print " Rate: $rate Cell: $cell Flags: $flags Priority: $priority\n";
print " Weight: $weight Allot: $allot Level: $level Ewma: $ewma\n";
print " Avpkt: $avpkt Maxidle: $maxidle SentBytes: $sentbytes\n";
print " SentPkts: $sentpkts Dropped: $dropped Overlimits: $overlimits\n";
print " Borrowed: $borrowed Overactions: $overactions Avgidle: $avgidle\n";
print " Undertime: $undertime\n\n";
} else {
@hexarray = split (/: /, $hexoff);
$flags =~ s/boundedisolated/bounded\/isolated/g;
if ($handle =~ /^\d+:0$/) {
print " $handle [$weight] $priority $flags\n";
} elsif ($handle =~ /^\d+:1$/) {
next;
} else {
print " $handle [$rate $weight] $priority $flags\n";
}
print " bytes $sentbytes pkts $sentpkts ";
print "drop $dropped over $overlimits borrow $borrowed ";
print "overaction $overactions $backlog\n";
foreach $hexline (@hexarray) {
$hexline =~ s/://g;
print " match $hexline\n";
}
print "\n";
}
}
}