-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsfreport.pl
3216 lines (2889 loc) · 102 KB
/
sfreport.pl
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
# SPDX-License-Identifier: GPL-2.0-only
# Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
# Copyright (C) 2019-2022, Xilinx, Inc.
# Copyright (C) 2007-2019, Solarflare Communications.
# Reporting tool for AMD Solarflare under linux
# 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, incorporated herein by reference.
use strict;
use File::Basename;
use FileHandle;
use POSIX;
use Socket;
use Getopt::Long;
# AMD Solarflare device/driver identifiers.
use constant driver_name_re => 'sfc\w*|onload|xilinx_efct';
use constant rpm_name_prefixes =>
'kernel-module-sfc-', 'kmod-solarflare-sfc-', 'sfc-dkms', 'sfutils', 'onload', 'openonload', 'enterpriseonload', 'solar_capture', 'sfptp',
'kernel-module-xilinx-efct', 'tcpdirect';
use constant deb_name_prefixes => 'sfc-modules-', 'xilinx-efct', 'onload', 'enterpriseonload', 'tcpdirect';
use constant EFX_VENDID_SFC => 0x1924;
use constant EFX_VENDID_XILINX => 0x10ee;
# General formatting.
use constant format_text => 0;
use constant format_html => 1;
use constant format_minimal => 2;
# Table formatting.
use constant gutter_width => 2;
use constant orient_horiz => 0;
use constant orient_vert => 1;
use constant values_format_default => 0;
use constant values_format_pre => 1;
# Interest types.
use constant interest_error => 0; # probable serious error
use constant interest_warn => 1; # general warning
use constant interest_perf => 2; # performance problem
use constant interest_badpkt => 3; # bad packet received
# CSS classes and text labels for interesting values.
my @interest_css_classes = ("error", "warn", "perf", "badpkt");
my @interest_labels = ("Error", "Warning", "Performance Warning",
"Bad Packet Warning");
my $VERSION = "4.16.1";
my $USER = "ROOT USER";
my $DATE = localtime();
my $UPTIME = '';
# Rules for what's interesting.
# Keys are pseudo-type names. Values are arrays of pseudo-tuples of
# conditions and interest types. Conditions are strings of the form
# attribute-name comparison-operator attribute-name or
# attribute-name comparison-operator integer.
my %interest_rules =
(net_stats_sfc => [['rx_lt64 != 0', interest_badpkt],
['rx_gtjumbo != 0', interest_badpkt],
['rx_bad_gtjumbo != 0', interest_badpkt],
['rx_bad_lt64 != 0', interest_badpkt],
['rx_bad != 0', interest_badpkt],
['rx_align_error != 0', interest_badpkt],
['rx_symbol_error != 0', interest_badpkt],
['rx_internal_error != 0', interest_error],
['rx_length_error != 0', interest_badpkt],
['tx_lt64 != 0', interest_error],
['tx_gtjumbo != 0', interest_error],
['tx_non_tcpudp != 0', interest_error],
['tx_mac_src_error != 0', interest_error],
['tx_ip_src_error != 0', interest_error],
# The following may be reported via ethtool
# or only through debugfs/procfs, depending
# on the driver version.
['rx_nodesc_drop_cnt != 0', interest_perf],
['rx_nodesc_drops != 0', interest_perf],
['rx_reset != 0', interest_warn],
['rx_frm_trunc != 0', interest_perf],
['rx_ip_hdr_chksum_err != 0', interest_badpkt],
['rx_tcp_udp_chksum_err != 0', interest_badpkt],
['rx_char_error_lane0 != 0', interest_error],
['rx_char_error_lane1 != 0', interest_error],
['rx_char_error_lane2 != 0', interest_error],
['rx_char_error_lane3 != 0', interest_error],
['rx_disp_error_lane0 != 0', interest_error],
['rx_disp_error_lane1 != 0', interest_error],
['rx_disp_error_lane2 != 0', interest_error],
['rx_disp_error_lane3 != 0', interest_error],
['rx_pm_trunc_bb_overflow != 0', interest_error],
['rx_pm_discard_bb_overflow != 0', interest_error],
['rx_pm_trunc_vfifo_full != 0', interest_perf],
['rx_pm_discard_vfifo_full != 0', interest_perf],
['rx_pm_trunc_qbb != 0', interest_error],
['rx_pm_discard_qbb != 0', interest_error],
['rx_pm_discard_mapping != 0', interest_warn],
['rx_dp_q_disabled_packets != 0', interest_warn]],
sfc_nic => [['n_rx_nodesc_drop_cnt != 0', interest_perf]],
sfc_errors => [['rx_reset != 0', interest_warn]],
sfc_eventqueue => [['n_rx_frm_trunc != 0', interest_perf],
['n_rx_ip_hdr_chksum_err != 0', interest_badpkt],
['n_rx_tcp_udp_chksum_err != 0', interest_badpkt],
['n_rx_overlength != 0', interest_badpkt]]);
# Extend $interest_rules{'net_stats_sfc'} to have port_ variants
my @orig_net_stats_sfc = @{$interest_rules{'net_stats_sfc'}};
foreach(@orig_net_stats_sfc) {
my $port_rule = [ "port_" . $_->[0], $_->[1] ];
push @{$interest_rules{'net_stats_sfc'}}, $port_rule;
}
my ($os_type, $hostname, $os_release, $os_version, $arch) = POSIX::uname();
my $arch_is_x86 = ($arch =~ /^(?:i[x3456]86|(?:x86[-_]|amd)64)$/);
my $arch_is_powerpc = ($arch =~ /^p(?:ower)?pc(?:64)?$/);
# Structure is a base class for wrapping C structures.
package Structure;
use overload '""' => \&_pack;
sub new {
my $class = shift;
my $data = ($#_ == 0) ? shift : '';
my $self = bless({_data => pack('a' . $class->_structure_len, $data)},
$class);
while ($#_ > 0) {
my $name = shift;
my $value = shift;
_set($self, $name, $value);
}
return $self;
}
sub _pack {
my $self = shift;
return $self->{_data};
}
sub _get {
my ($self, $name) = @_;
my $class = ref($self);
my $field_def = $self->_structure_fields->{$name};
if (defined($field_def)) {
my ($offset, $format) = @$field_def;
return unpack($format, substr($self->{_data}, $offset));
}
}
sub _set {
my ($self, $name, $value) = @_;
my $class = ref($self);
my $field_def = $self->_structure_fields->{$name};
if (defined($field_def)) {
my ($offset, $format) = @$field_def;
my $packed_value = pack($format, ($format =~ /^P/) ? $$value : $value);
substr($self->{_data}, $offset, length($packed_value)) = $packed_value;
}
}
sub AUTOLOAD {
my $self = shift;
my $class = ref($self);
my $name = $Structure::AUTOLOAD;
$name =~ s/^\w+:://;
if ($#_ < 0) {
return _get($self, $name);
} else {
return _set($self, $name, @_);
}
}
package Ifreq;
use base qw(Structure);
use constant _structure_fields => {ifr_name => [0, 'Z16'],
ifr_hwaddr => [16, 'SC14'],
ifr_addr => [16, 'SC14'],
ifr_dstaddr => [16, 'SC14'],
ifr_broadaddr => [16, 'SC14'],
ifr_netmask => [16, 'SC14'],
ifr_flags => [16, 's'],
ifr_mtu => [16, 'i!'],
ifr_map => [16, 'L!L!SCCC'],
ifr_slave => [16, 'Z16'],
ifr_data => [16, 'P44'],
ifr_ifindex => [16, 'i!'],
ifr_bandwidth => [16, 'i!'],
ifr_qlen => [16, 'i!'],
ifr_newname => [16, 'Z16']};
use constant _structure_len => 32;
my $socket;
sub get_socket {
use Socket;
if (!defined($socket)) {
$socket = new FileHandle;
socket($socket, PF_INET, SOCK_STREAM, 0) or die "socket: $!";
}
return $socket;
}
package EthtoolDrvinfo;
use base qw(Structure);
use constant _structure_fields => {cmd => [0, 'L'],
driver => [4, 'Z32'],
version => [36, 'Z32'],
fw_version => [68, 'Z32'],
bus_info => [100, 'Z32'],
n_priv_flags => [176, 'L'],
n_stats => [180, 'L'],
testinfo_len => [184, 'L'],
eedump_len => [188, 'L'],
regdump_len => [192, 'L']};
use constant _structure_len => 196;
package Ethtool;
use constant SIOCETHTOOL => 0x8946;
use constant ETHTOOL_GDRVINFO => 0x00000003;
# ... to be continued
sub do_cmd {
my ($if_name, $ecmd) = @_;
my $ecmd_blob = "$ecmd";
my $ifreq = new Ifreq(ifr_name => $if_name, ifr_data => \$ecmd_blob);
return undef unless ioctl(Ifreq::get_socket(), SIOCETHTOOL, $ifreq);
# Construct a new wrapper of the same type
return ref($ecmd)->new($ecmd_blob);
}
package EfxSockIoctl;
use base qw(Structure);
use constant _structure_fields => {cmd => [0, 'S'],
u => [4, 'a1000']};
use constant _structure_len => 1004; # this is quite arbitrary
use constant SIOCEFX => 0x89f3;
use constant EFX_GET_TS_CONFIG => 0xef25;
# ... to be continued
package HwtstampConfig;
use base qw(Structure);
use constant _structure_fields => {flags => [0, 'i'],
tx_type => [4, 'i'],
rx_filter => [8, 'i']};
use constant _structure_len => 12;
use constant SIOCGHWTSTAMP => 0x89b1;
package main;
# Some utility functions we need.
# Tell whether the given string starts with the given prefix string.
sub startswith {
my ($s, $prefix) = @_;
return substr($s, 0, length($prefix)) eq $prefix;
}
sub max {
my $result;
for (@_) {
if (!defined($result) || $_ > $result) {
$result = $_;
}
}
$result;
}
# Return sum of a sequence of numbers.
sub sum {
my $result = 0;
for (@_) {
$result += $_;
}
$result;
}
sub first_defined {
for (@_) {
return $_ if defined($_);
}
}
sub pairs {
use integer;
my @result;
for my $i (0 .. $#_ / 2) {
push @result, [$_[2 * $i], $_[2 * $i + 1]];
}
return @result;
}
sub html_encode {
my $result = shift;
$result =~ s/([\&<\'\"])/'&#'.ord($1).';'/eg;
return $result;
}
my $out_file;
my $out_format = format_text;
my @interesting_stuff = ();
# Print a table of values and apply interest rules to them.
# title title string to show above the table
# type_name (pseudo-)type name of values (may be undef)
# attributes ref to array of names for the attributes to show in columns
# values ref to array of values to show in rows;
# each value is a ref to an array with values in the same
# order as the given names
# orientation if orient_horiz, values are in rows;
# if orient_vert, values are in columns
# values_fmt if values_format_pre, values are preformatted text
# if values_format_default, use default formatting
sub tabulate {
my ($title, $type_name, $attributes, $values, $orientation, $values_fmt, $id) = @_;
my @col_widths;
my @cell_texts;
my @cell_interest;
$orientation = orient_horiz unless defined($orientation);
$orientation == orient_horiz or $orientation == orient_vert or die;
$values_fmt = values_format_default unless defined($values_fmt);
$values_fmt == values_format_default
or $values_fmt == values_format_pre
or die;
for my $j (0..$#$attributes) {
my $cell_text = $attributes->[$j];
my ($x, $y);
if ($orientation == orient_horiz) {
($x, $y) = ($j, 0);
} else {
($x, $y) = (0, $j);
}
$cell_texts[$y]->[$x] = $cell_text;
$col_widths[$x] = max($col_widths[$x] || 0, length($cell_text));
}
for my $i (0..$#$values) {
my $value = $values->[$i];
my $value_interest = apply_interest_rules($type_name, $value);
for my $j (0..$#$attributes) {
my $attr_value;
if (ref($value) eq 'HASH') {
$attr_value = $value->{$attributes->[$j]};
} elsif (ref($value) eq 'ARRAY') {
$attr_value = $value->[$j];
}
my $cell_text = defined($attr_value) ? $attr_value : "<N/A>";
my ($x, $y);
if ($orientation == orient_horiz) {
($x, $y) = ($j, 1 + $i);
} else {
($x, $y) = (1 + $i, $j);
}
$cell_texts[$y]->[$x] = $cell_text;
if (exists($value_interest->{$attributes->[$j]})) {
$cell_interest[$y]->[$x] =
$value_interest->{$attributes->[$j]};
}
$col_widths[$x] = max($col_widths[$x] || 0, length($cell_text));
}
}
print_heading($title, $id);
if ($out_format == format_html) {
$out_file->print(" <table class="
.($orientation == orient_horiz ? 'horiz' : 'vert')
.">\n");
for my $y (0..$#cell_texts) {
$out_file->print(" <tr>\n");
for my $x (0..$#{$cell_texts[$y]}) {
my $head_cell = ($orientation == orient_horiz ? $y : $x) == 0;
my $elem_name = $head_cell ? 'th' : 'td';
my $cell_text = $cell_texts[$y]->[$x];
$out_file->print(" <$elem_name");
# If this cell is interesting, style it accordingly
# and define an identifier so the summary can link to
# it.
if (defined($cell_interest[$y]->[$x])) {
$out_file->print(" class="
. ($interest_css_classes
[$cell_interest[$y]->[$x]->[0]])
. " id=match" .
$cell_interest[$y]->[$x]->[1]);
}
$out_file->print(">");
if (!$head_cell && $values_fmt == values_format_pre) {
print_preformatted($cell_text);
} else {
$out_file->print(html_encode($cell_text));
}
$out_file->print("</$elem_name>\n");
}
if ($orientation == orient_vert && $#$values == -1 && $y == 0) {
$out_file->print(" <td rowspan=".($#$attributes + 1).">"
."<em>none found</em></td>\n");
}
$out_file->print(" </tr>\n");
}
if ($orientation == orient_horiz && $#$values == -1
&& $#$attributes >= 0) {
$out_file->print(" <tr>\n"
." <td colspan=".($#$attributes + 1).">"
."<em>none found</em></td>\n"
." </tr>\n");
} elsif ($#$values == -1 && $#$attributes == -1) {
$out_file->print(" <tr><td><em>none found</em></td></tr>\n");
}
$out_file->print(" </table>\n");
} else {
for my $y (0..$#cell_texts) {
my $row = '';
for my $x (0..$#{$cell_texts[$y]}) {
my $cell_text = $cell_texts[$y]->[$x];
my $pad = $col_widths[$x] - length($cell_text);
$row .= $cell_text . (' ' x ($pad + gutter_width));
if ($orientation == orient_vert && $x == 0) {
$row = substr($row, 0, -1) . '| ';
}
}
if ($orientation == orient_vert && $#$values == -1 && $y == 0) {
$row .= ' ' x gutter_width . 'none found';
}
$out_file->print("$row\n");
if ($orientation == orient_horiz && $y == 0
&& $#$attributes >= 0) {
my $table_width = (sum(@col_widths)
+ gutter_width * $#$attributes);
$out_file->print('=' x $table_width . "\n");
}
}
if (($orientation == orient_horiz || $#$attributes == -1)
&& $#$values == -1) {
$out_file->print("none found\n");
}
$out_file->print("\n");
}
if ( $id ) {
print_footer( $id );
}
}
sub print_text {
my $text = shift;
$out_file->print($out_format == format_html ? html_encode($text) : $text);
}
sub print_heading {
my $text = shift;
my $id = shift;
my $hide = shift;
my $display = 'block';
my $link = ' Hide...';
if ( $hide ) {
$display = 'none';
$link = ' ...Show';
}
if ($out_format == format_html) {
if ( $id ) {
$out_file->print("<table rows=1 cols=2 style='border:none;'><tr style='border:none;'><td style='border:none;'>");
$out_file->print("<a name='$id'><h2>".html_encode($text)."</h2></name></td>");
$out_file->print("<td style='border:none;'><a id='$id\_l' href='\#$id' onclick='toggle(\"$id\"); return false;'>$link</a></td></tr></table>\n");
$out_file->print("<div style='display:$display' id='$id\_c'>\n");
} else {
$out_file->print(" <h2>".html_encode($text)."</h2>\n");
}
} else {
$out_file->print("$text\n\n");
}
}
sub print_footer {
my $id = shift;
if ($out_format == format_html) {
if ( $id ) {
$out_file->print("<a href='\#$id' onclick='toggle(\"$id\");'>Hide $id</a><br>\n");
}
$out_file->print("</div>\n");
}
}
sub print_bold {
my $text = shift;
if ($out_format == format_html) {
$out_file->print(" <h4>".html_encode($text)."</h4>\n");
} else {
$out_file->print("$text\n\n");
}
}
sub print_warning {
my $text = shift;
if ($out_format == format_html) {
$out_file->print(" <p><em>".html_encode($text)."</em></p>\n");
} else {
$out_file->print("$text\n\n");
}
}
sub begin_preformatted {
my $use_delimiters = shift;
if ($out_format == format_html) {
$out_file->print(" <pre>");
} elsif ($use_delimiters) {
$out_file->print("--- BEGIN ---\n");
}
}
sub end_preformatted {
my $use_delimiters = shift;
if ($out_format == format_html) {
$out_file->print("</pre>\n");
} elsif ($use_delimiters) {
$out_file->print("--- END ---\n\n");
} else {
$out_file->print("\n");
}
}
sub print_preformatted {
my ($text, $use_delimiters) = @_;
begin_preformatted($use_delimiters);
print_text($text);
end_preformatted($use_delimiters);
}
# Return the entire contents of a binary file as a string.
# Return undef on failure.
sub read_file {
my ($path, $offset) = @_;
my $result;
my $fd = POSIX::open($path, &POSIX::O_RDONLY);
if (defined($fd)) {
if (defined($offset)) {
POSIX::lseek($fd, $offset, 0);
}
$result = '';
my ($buf, $length);
$! = 0;
while (($length = POSIX::read($fd, $buf, 65536))
&& $length != 0) {
$result .= substr($buf, 0, $length);
}
my $saved_errno = $!;
POSIX::close($fd);
if ($! = $saved_errno) {
$result = undef;
}
}
return $result;
}
# Return a reference to an array of the names in a directory.
# Return undef on failure.
sub list_dir {
my ($path) = @_;
my $result;
if (my $dir = POSIX::opendir($path)) {
$! = 0;
my @result = grep(!/^\.\.?$/, readdir($dir));
@result = reverse(@result);
my $saved_errno = $!;
POSIX::closedir($dir);
$! = $saved_errno;
$result = $! ? undef : \@result;
}
return $result;
}
# Return a reference to an array of file names and contents found in
# a directory. The optional name_filter parameter is used to check
# whether a file of the given name should be included. The optional
# value_map is used to modify the value.
sub read_dir_files {
my ($dir_path, $name_filter, $value_map) = @_;
my $result;
if (my $list = list_dir($dir_path)) {
$result = [];
for (@$list) {
my $file_path = "$dir_path/$_";
if (-f $file_path
&& (!defined($name_filter) || &$name_filter())) {
my $name = $_;
local $_ = read_file($file_path);
&$value_map() if defined($value_map) && defined($_);
push @$result, [$name, $_];
}
}
}
return $result;
}
sub find_sfc_debug_dir {
my @sfc_paths = ();
for my $path ('/proc/driver', '/debug', '/sys/kernel/debug') {
for my $driver ('sfc', 'sfc_ef100') {
push @sfc_paths, "$path/$driver" if -d "$path/$driver";
}
}
return @sfc_paths;
}
sub find_x3_debug_dir {
my @xlnx_paths = ();
for my $path ('/proc/driver', '/debug', '/sys/kernel/debug') {
for my $driver ('xilinx_efct') {
push @xlnx_paths, "$path/$driver" if -d "$path/$driver";
}
}
return @xlnx_paths;
}
sub get_sfc_drvinfo {
my %sfc_drvinfo;
my $sfc_present = 0;
if (my $list = list_dir('/sys/class/net')) {
for my $iface_name (@$list) {
my $drvinfo = Ethtool::do_cmd(
$iface_name,
new EthtoolDrvinfo(cmd => Ethtool::ETHTOOL_GDRVINFO));
next unless defined($drvinfo);
if ($drvinfo->driver eq 'sfc' || $drvinfo->driver eq 'sfc_ef100' || $drvinfo->driver eq 'xilinx_efct') {
$sfc_drvinfo{$iface_name} = $drvinfo;
}
if ($drvinfo->driver eq 'sfc' && !(my $test = new FileHandle("sfupdate 2>&1 |")) && !($sfc_present)) {
STDERR->print("WARNING: Unable to run sfutils (sfkey, sfboot) commands while sfc driver is loaded.\n"
."Please make sure the Solarflare Linux Utilities package is installed.\n"
."NIC configuration (sfboot) and licensing information (sfkey) are unavailable.\n");
$sfc_present = 1;
}
}
}
return \%sfc_drvinfo;
}
sub get_hwtstamp_config {
my $iface_name = shift;
my $config = new HwtstampConfig;
my $config_blob = "$config";
my $ifreq = new Ifreq(ifr_name => $iface_name);
# Standard ioctl
$ifreq->ifr_data(\$config_blob);
if (ioctl(Ifreq::get_socket(), HwtstampConfig::SIOCGHWTSTAMP, "$ifreq")) {
return new HwtstampConfig($config_blob);
}
# Private ioctl
my $efxsi = new EfxSockIoctl(cmd => EfxSockIoctl::EFX_GET_TS_CONFIG);
my $efxsi_blob = "$efxsi";
$ifreq->ifr_data(\$efxsi_blob);
if (ioctl(Ifreq::get_socket(), EfxSockIoctl::SIOCEFX, "$ifreq")) {
$efxsi = new EfxSockIoctl($efxsi_blob);
return new HwtstampConfig($efxsi->u);
}
return undef;
}
sub get_iface_bus_addr {
my $iface_name = shift;
if (my $device_path = readlink("/sys/class/net/$iface_name/device")) {
return File::Basename::basename($device_path);
} else {
return undef;
}
}
sub get_iface_mac_addr {
my $iface_name = shift;
if (my $dev_addr = read_file("/sys/class/net/$iface_name/address")) {
$dev_addr =~ s/\n//s;
return $dev_addr;
} else {
return undef;
}
}
sub get_onload_version {
my $onload_ver;
if (my $module_ver = read_file("/sys/module/onload/version")) {
$module_ver =~ s/\n//s;
$onload_ver = $module_ver;
} else {
$onload_ver = 'N/A';
}
return $onload_ver;
}
sub get_xilinx_efct_version {
my $efct_ver;
if (my $module_ver = read_file("/sys/module/xilinx_efct/version")) {
$module_ver =~ s/\n//s;
$efct_ver = $module_ver;
} else {
$efct_ver = 'N/A';
}
return $efct_ver;
}
sub get_linux_cpuinfo {
my @log_procs;
my $log_id;
my $cpus_dir = '/sys/devices/system/cpu';
my $nodes_dir = '/sys/devices/system/node';
my %x86_cpuinfo_map = (
'cpu family' => 'family',
'model' => 'model',
'cpu variation' => 'variation',
'stepping' => 'stepping',
'vendor_id' => 'vendor',
'model name' => 'full_name',
'cpu MHz' => 'clock_mhz',
'core id' => 'core_id',
'physical id' => 'physical_id'
);
my %powerpc_cpuinfo_map = (
'cpu' => 'family',
'clock' => 'clock_mhz'
);
my $cpuinfo_map;
if ($arch_is_x86) {
$cpuinfo_map = \%x86_cpuinfo_map;
} elsif ($arch_is_powerpc) {
$cpuinfo_map = \%powerpc_cpuinfo_map;
}
# Try to read generic cputopology
if (my $cpu_list = list_dir($cpus_dir)) {
my $have_phys_id;
for my $name (@$cpu_list) {
next unless $name =~ /^cpu(\d+)$/;
$log_id = $1;
my $topo_dir = "$cpus_dir/$name/topology";
next unless -d $topo_dir;
my $proc = {};
$proc->{core_id} = read_file("$topo_dir/core_id");
my $phys_id = read_file("$topo_dir/physical_package_id");
if ($phys_id >= 0) {
$have_phys_id = 1;
$proc->{physical_id} = $phys_id;
}
$log_procs[$log_id] = $proc;
}
if (!$have_phys_id) {
# Assume NUMA nodes are physical packages
if (my $node_list = list_dir($nodes_dir)) {
for my $name (@$node_list) {
next unless $name =~ /^node(\d+)$/;
my $node_id = $1;
my $cpu_list = list_dir("$nodes_dir/$name");
next unless $cpu_list;
for my $name (@$cpu_list) {
next unless $name =~ /^cpu(\d+)$/;
$log_procs[$1]->{physical_id} = $node_id;
}
}
}
}
}
# Parse /proc/cpuinfo into per-logical-processor hashes.
if (defined($cpuinfo_map) &&
(my $file = new FileHandle('/proc/cpuinfo', 'r'))) {
while (<$file>) {
if (/^([^:]*?)[ \t]*:[ \t]*(.*)\n$/) {
my ($key, $value) = ($1, $2);
if ($key eq 'processor') {
$log_id = $value;
$log_procs[$log_id] ||= {};
} elsif (defined($key = $cpuinfo_map->{$key})) {
if ($key eq 'clock_mhz') {
$value =~ s/(?:\.0+)? *MHz$//;
}
$log_procs[$log_id]->{$key} = $value;
}
}
}
$file->close();
}
return undef unless @log_procs;
# Group processors into physical processors and count logical
# processors.
my @phys_procs = ();
my @phys_proc_cores = ();
for $log_id (0..$#log_procs) {
my $proc = $log_procs[$log_id];
next unless defined($proc); # there may be gaps in numbering
my $phys_id = first_defined($proc->{'physical_id'}, $log_id);
if (defined($phys_procs[$phys_id])) {
++$phys_procs[$phys_id]->{n_log_procs};
} else {
$phys_procs[$phys_id] = {%$proc};
$phys_procs[$phys_id]->{n_log_procs} = 1;
$phys_procs[$phys_id]->{n_cores} = 1;
$phys_proc_cores[$phys_id] = {};
}
my $core_id = $proc->{core_id};
if (defined($core_id)) {
$phys_proc_cores[$phys_id]->{$core_id} = 1;
$phys_procs[$phys_id]->{n_cores} =
scalar(keys(%{$phys_proc_cores[$phys_id]}));
}
}
return \@phys_procs;
}
# Produce a system summary along the lines of msinfo32 output.
sub print_system_summary {
my $smbios = shift;
my @attributes = ('OS Name', 'Version', 'Architecture');
my @value = ($os_type, "$os_release $os_version", $arch);
if ($os_type eq 'Linux') {
$_ = read_file('/proc/cmdline');
chomp;
push @attributes, 'Kernel Command Line';
push @value, $_;
# Newer distributions implement LSB release information so
# look for that first. If that fails look in /etc/*-release
# (RPM-based distributions use these) and /etc/debian_version.
my $distribution;
$_ = `lsb_release -d 2>/dev/null`;
if ($? == 0 && /^Description:[ \t]*(.*)\n$/) {
$distribution = $1;
} else {
my @release_files = glob('/etc/*-release');
if ($#release_files >= 0) {
$distribution = read_file($release_files[0]);
} elsif (-f '/etc/debian_version') {
$distribution = 'Debian ' . read_file('/etc/debian_version');
}
}
$distribution =~ s/\n.*//s if defined($distribution);
push @attributes, 'Distribution';
push @value, $distribution;
}
push @attributes, 'System Name';
push @value, $hostname;
push @attributes, 'System Manufacturer';
push @value, $smbios->get_single_string(1, 4);
push @attributes, 'System Model';
push @value, $smbios->get_single_string(1, 5);
if ($os_type eq 'Linux') {
if (my $phys_procs = get_linux_cpuinfo()) {
for my $proc (@$phys_procs) {
next unless defined($proc); # there may be gaps in numbering
my $proc_desc = '';
for (['family', '', 'Family ', '',
'unknown'],
['model', ' ', ' Model ', ''],
['variation', ' ', ' Variation ', ''],
['stepping', ' ', ' Stepping ', ''],
['vendor', ' ', ' Vendor ', ''],
['full_name', ', ', undef, ''],
['clock_mhz', undef, ', ', ' MHz'],
['n_cores', undef, ', ', ' Core(s)'],
['n_log_procs', undef, ', ',
' Logical Processor(s)', '']) {
my ($key, $prefix, $num_prefix, $suffix, $default) = @$_;
my $value = $proc->{$key};
if (!defined($value)) {
$proc_desc .= $default if defined($default);
} elsif ($value =~ /^[\d.]+$/) {
$proc_desc .= "$num_prefix$value$suffix";
} else {
$proc_desc .= "$prefix$value$suffix";
}
}
push @attributes, 'Processor';
push @value, $proc_desc;
}
}
}
if ($smbios->expected) {
push @attributes, 'BIOS Version/Date';
my $bios_id = $smbios->get_single_string(0, 4); # vendor
if (defined($bios_id)) {
my $bios_version = $smbios->get_single_string(0, 5);
if (defined($bios_version)) {
$bios_id .= ' ' . $bios_version;
my $bios_date = $smbios->get_single_string(0, 8);
if (defined($bios_date)) {
$bios_id .= ', ' . $bios_date;
}
}
}
push @value, $bios_id;
}
if ($os_type eq 'Linux') {
push @attributes, 'SELinux';
my $selinux_status = 'Disabled';
my $selinux_enforce = read_file("/sys/fs/selinux/enforce");
if (defined($selinux_enforce)) {
$selinux_status = $selinux_enforce == '1' ? 'Enforcing' : 'Permissive';
}
push @value, $selinux_status;
if (my $meminfo_file = new FileHandle('/proc/meminfo', 'r')) {
my %meminfo = ();
while (<$meminfo_file>) {
if (/^([^ ]+):[ \t]*(\d+) kB\n$/) {
$meminfo{$1} = $2;
}
}
$meminfo_file->close();
push @attributes, ('Total Physical Memory',
'Free Physical Memory',
'Available Physical Memory',
'Total Virtual Memory',
'Available Virtual Memory',
'Page File Space');
push @value, map(sprintf('%d MB', $_ / 1024),
$meminfo{MemTotal},
$meminfo{MemFree},
$meminfo{MemAvailable},
# Count all physical memory and swap
# minus kernel allocations as virtual
# memory. Assume kernel code and static
# data is excluded from MemTotal.
$meminfo{MemTotal} + $meminfo{SwapTotal}
- $meminfo{Slab} - $meminfo{PageTables},
# Count all free physical memory and
# swap, plus all buffers and cache, as
# 'available' (even though the cache
# includes all user code!).
$meminfo{MemFree} + $meminfo{SwapFree}
+ $meminfo{Buffers} + $meminfo{Cached}
+ $meminfo{SwapCached},
$meminfo{SwapTotal});
}
}
tabulate('System Summary', undef, \@attributes, [\@value], orient_vert);
} # print_system_summary
sub print_physical_memory {
my $smbios = shift;
my @mem_arrays = $smbios->get_by_type(16);
my @mem_devices = $smbios->get_by_type(17);
my @values = ();
if (@mem_arrays && @mem_devices) {
my %mem_arrays;
for (@mem_arrays) {
my ($handle, $loc, $use, $slots) = unpack('x2vCCx7v', $_->[0]);
# Only count slots on the motherboard for system memory
$mem_arrays{$handle} = 1 if $loc == 3 && $use == 3;
}
my (%mem_sets, %mem_sets_pop);
for (@mem_devices) {
my ($header, $strings) = @$_;
my ($array, $size_code, $slot_str_i, $bank_str_i) =
unpack('x4vx6vx2CC', $header);
if ($mem_arrays{$array}) {
my $size;
if ($size_code != 0 && $size_code != 0xffff) {
$size = (($size_code & 0x7fff) *
(($size_code & 0x8000) ? 1 : 1024));
}
push @values, [SmbiosInfo::get_string($strings, $slot_str_i),
SmbiosInfo::get_string($strings, $bank_str_i),
($size_code == 0) ? 0 : 1,
$size];
}
}
}
print_heading('Hardware', 'hw');
tabulate('Physical memory slots',
undef,
[qw(slot_id bank_id filled size_kbytes)],
\@values,
orient_vert);
} # print physical_memory
# SmbiosInfo is a reimplementation of some of dmidecode, which we can't
# rely on being installed.
package SmbiosInfo;
sub new {
use PerlIO;
my $self = bless({
expected => ($arch_is_x86 || $arch eq 'ia64'),
handle_index => {},