summaryrefslogtreecommitdiff
path: root/scripts/vyatta-show-queueing.pl
blob: 657a44c11fb4a551e134c2c1a06cf915c7e9774d (plain)
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
#!/usr/bin/perl
#
# Module: vyatta-show-queueing.pl
#
# **** 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) 2007 Vyatta, Inc.
# All Rights Reserved.
#
# Author: Stephen Hemminger
# Date: July 2008
# Description: Script to display QoS information in pretty form
#
# **** End License ****
#

use strict;
use warnings;

use Getopt::Long;
use Tree::Simple;

use lib "/opt/vyatta/share/perl5/";
use Vyatta::Interface;
use Vyatta::Misc;

my $intf_type;

# Map from kernel qdisc names to configuration names
my %qdisc_types = (
    'pfifo_fast' => 'default',
    'sfq'        => 'fair-queue',
    'tbf'        => 'rate-limit',
    'htb'        => 'traffic-shaper',
    'pfifo'      => 'drop-tail',
    'red'        => 'random-detect',
    'ingress'    => 'traffic-limiter',
    'drr'        => 'round-robin',
    'prio'       => 'priority-queue',
    'netem'      => 'network-emulator',
    'gred'	 => 'weighted-random',
);

# Convert from kernel to vyatta nams
sub shaper {
    my $qdisc  = shift;
    my $shaper = $qdisc_types{$qdisc};

    return $shaper ? $shaper : '[' . $qdisc . ']';
}

sub show_brief {
    my %ingress;

    print "Output Queues:\n";
    my $fmt = "%-10s %-16s %10s %10s %10s\n";
    printf $fmt, 'Interface', 'Qos-Policy', 'Sent', 'Dropped', 'Overlimit';

    # Read qdisc info
    open( my $tc, '/sbin/tc -s qdisc ls |' ) or die 'tc command failed';

    my @lines;
    my ( $qdisc, $parent, $ifname, $id );
    my $root = 'root';

    while (<$tc>) {
        chomp;
        my @fields = split;
        if ( $fields[0] eq 'qdisc' ) {
	    my ($ptype, $pid);
	    # Examples:
            # qdisc sfq 8003: dev eth1 root limit 127p quantum 1514b
	    # qdisc gred 2: dev eth0 parent 1:
            ( undef, $qdisc, $id, undef, $ifname, $ptype, $pid ) = @fields;

	    $parent = ($ptype eq 'parent') ? $pid : $ptype;
            next;
        }

        # skip unwanted extra stats
        next if ( $fields[0] ne 'Sent' );

        #  Sent 13860 bytes 88 pkt (dropped 0, overlimits 0 requeues 0)
        my ( undef, $sent, undef, undef, undef, undef, $drop, undef, $over ) =
          @fields;

        # punctuation was never jamal's strong suit
        $drop =~ s/,$//;

        if ( $id eq 'ffff:' ) {
            $ingress{$ifname} =
              [ $ifname, shaper($qdisc), $sent, $drop, $over ];
        } elsif ( $qdisc eq 'dsmark' ) {
	    # dsmark is used as a top-level before htb or gred
	    $root = $id;
	} elsif ( $parent eq $root ) {
	    $root = 'root';
            if ($intf_type) {
                my $intf = new Vyatta::Interface($ifname);
                next unless ( $intf && ( $intf->type() eq $intf_type ) );
            }

	    push @lines, sprintf $fmt, $ifname, shaper($qdisc), $sent, $drop, $over;
        }
    }
    close $tc;
    print sort @lines;

    if (%ingress) {
        print "\nInput:\n";
        printf $fmt, 'Ifname', 'Qos-Policy', 'Received', 'Dropped', 'Overlimit';

        foreach my $name ( keys %ingress ) {
            my $args = $ingress{$name};
            printf $fmt, @$args;
        }
    }
}

# Sort by class id which is a string of form major:minor
# NB: numbers are hex
sub byclassid {
    my ($a1, $a2) = ($a =~ m/([0-9a-f]+):([0-9a-f]+)/);
    my ($b1, $b2) = ($b =~ m/([0-9a-f]+):([0-9a-f]+)/);

    return hex($a2) <=> hex($b2) if ($a1 == $b1);
    return hex($a1) <=> hex($b1);
}

sub class2tree {
    my ( $classes, $parentid, $parent ) = @_;

    foreach my $id ( sort byclassid keys %{$classes} ) {
        my $class = $classes->{$id};
        next unless ( $class->{parent} && $class->{parent} eq $parentid );
        my $node = Tree::Simple->new( $class->{info} );
	$parent->addChild($node);
        class2tree( $classes, $id, $node );
    }

    return $parent;
}

# Build a tree of output information
# (This is N^2 but not a big issue)
sub get_class {
    my ( $interface, $rootq, $qdisc ) = @_;
    my %classes;

    open( my $tc, "/sbin/tc -s class show dev $interface |" )
      or die 'tc command failed: $!';

    my ( $id, $name, $sent, $drop, $over, $root, $leaf, $parent );

    while (<$tc>) {
        chomp;
        /^class/ && do {
	    # class htb 1:1 root rate 1000Kbit ceil 1000Kbit burst 1600b cburst 1600b
	    # class htb 1:2 parent 1:1 leaf 8001:
	    # class ieee80211 :2 parent 8001:
            my ( $l, $q, $t );
            ( undef, $name, $id, $t, $parent, $l, $q ) = split;
	    $leaf = undef;
	    if ($t eq 'root') {
		$parent = undef;
	    } elsif ($t eq 'parent') {
		if ($l eq 'leaf') {
		    $q =~ s/:$//;
		    $leaf = hex($q);
		}
	    } else {
		die "confused by tc class output for type 'class $name $id $t'";
	    }
            next;
        };

        /^ Sent/ && do {
            #  Sent 13860 bytes 88 pkt (dropped 0, overlimits 0 requeues 0)
            ( undef, $sent, undef, undef, undef, undef, $drop, undef, $over ) =
              split;

            # fix silly punctuation bug in tc
            $drop =~ s/,$//;
            next;
        };

        /^ rate/ && do {
            #  rate 0bit 0pps backlog 0b 23p requeues 0
            my ( undef, $rate, undef, undef, undef, $backlog ) = split;
            $backlog =~ s/p$//;
            $rate    =~ s/bit$//;

	    # split $id of form 1:10 into parent, child id
	    my ($maj, $min) = ($id =~ m/([0-9a-f]+):([0-9a-f]+)/);

	    # TODO handle nested classes??
	    next if (hex($maj) != $rootq);

	    # record info for display
            my @args = ( hex($min) );
            if ($leaf) {
		my $qdisc_info = $qdisc->{$leaf};
		die "info for $leaf is unknown" unless $qdisc_info;
                push @args, @{ $qdisc_info };
            } else {
                push @args, shaper($name), $sent, $drop, $over, $rate, $backlog;
            }

            $classes{$id} = {
                id     => $id,
                parent => $parent,
                info   => \@args,
            };

            $root = $classes{$id} unless $parent;
            next;
          }
    }
    close $tc;
    return unless $root;

    return class2tree( \%classes, $root->{id},
        Tree::Simple->new( $root->{info}, Tree::Simple->ROOT ) );
}

# This collects all the qdisc information into one hash
# and root queue id and reference to map of qdisc to statistics
sub get_qdisc {
    my $interface = shift;
    my %qdisc;
    my $root;

    open( my $tc, "/sbin/tc -s qdisc show dev $interface |" )
      or die 'tc command failed: $!';

    my ( $qid, $name, $sent, $drop, $over );
    while (<$tc>) {
        chomp;
        /^qdisc/ && do {
            # qdisc htb 1: root r2q 10 default 20 direct_packets...
	    my $t;

            ( undef, $name, $qid, $t ) = split;
            $qid =~ s/:.*$//;
            $qid = hex($qid);

            $root = $qid if ( $t eq 'root' );
            next;
        };

        /^ Sent/ && do {
            #  Sent 13860 bytes 88 pkt (dropped 0, overlimits 0 requeues 0)
            ( undef, $sent, undef, undef, undef, undef, $drop, undef, $over ) =
              split;

            # fix silly punctuation bug in tc
            $drop =~ s/,$//;
            next;
        };

        /^ rate/ && do {
            # rate 0bit 0pps backlog 0b 23p requeues 0
            my ( undef, $rate, undef, undef, undef, $backlog ) = split;

            $backlog =~ s/p$//;
            $rate    =~ s/bit$//;

            $qdisc{$qid} = [ shaper($name), $sent, $drop, $over, $rate, $backlog ];
          }
    }
    close $tc;

    return ( $root, \%qdisc );
}

my $INGRESS = 0xffff;

sub show_queues {
    my ( $interface, $root, $qdisc ) = @_;
    my $args = $qdisc->{$root};
    return unless $args;

    my $fmt     = "%-10s %-16s %-10s %-9s %-9s %-9s %s\n";
    print "\n$interface ", ( ( $root eq $INGRESS ) ? 'Input' : 'Output' ),
      " Queueing:\n";
    printf $fmt, 'Class', 'Qos-Policy',
      ( ( $root eq $INGRESS ) ? 'Received' : 'Sent' ),
      'Dropped', 'Overlimit', 'Rate', 'Queued';

    printf $fmt, 'root', @{$args};

    my $tree = get_class( $interface, $root, $qdisc );
    return unless $tree;

    $tree->traverse( sub { my $_tree = shift;
			   my @args = @{ $_tree->getNodeValue() };
			   my $id = shift @args;
			   printf $fmt, ('  ' x $_tree->getDepth() . $id ), @args;
		     });
}

sub show {
    my $interface = shift;
    my ( $root, $qdisc ) = get_qdisc($interface);

    show_queues( $interface, $root,    $qdisc ) if $root;
    show_queues( $interface, $INGRESS, $qdisc );
}

sub usage {
    print "Usage: $0 [--type={ethernet,serial}] --brief\n";
    print "       $0 interface(s)\n";
    exit 1;
}

GetOptions(
    'type=s' => \$intf_type,
    'brief'  => sub { show_brief(); exit 0; },
) or usage();

# if no arguments given, rebuild ARGV with list of all interfaces
if ( $#ARGV == -1 ) {
    foreach my $ifname ( getInterfaces() ) {
        if ($intf_type) {
            my $intf = new Vyatta::Interface($ifname);
            next unless ( $intf && $intf_type eq $intf->type() );
        }
        push @ARGV, $ifname;
    }
}

foreach my $interface ( sort @ARGV ) {
    show($interface);
}