This file is indexed.

/usr/share/perl5/IBswcountlimits.pm is in infiniband-diags 1.6.6-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
#!/usr/bin/perl
#
# Copyright (c) 2006 The Regents of the University of California.
# Copyright (c) 2006-2008 Voltaire, Inc. All rights reserved.
#
# Produced at Lawrence Livermore National Laboratory.
# Written by Ira Weiny <weiny2@llnl.gov>.
#            Erez Strauss from Voltaire for help in the get_link_ends code.
#
# This software is available to you under a choice of one of two
# licenses.  You may choose to be licensed under the terms of the GNU
# General Public License (GPL) Version 2, available from the file
# COPYING in the main directory of this source tree, or the
# OpenIB.org BSD license below:
#
#     Redistribution and use in source and binary forms, with or
#     without modification, are permitted provided that the following
#     conditions are met:
#
#      - Redistributions of source code must retain the above
#        copyright notice, this list of conditions and the following
#        disclaimer.
#
#      - Redistributions in binary form must reproduce the above
#        copyright notice, this list of conditions and the following
#        disclaimer in the documentation and/or other materials
#        provided with the distribution.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#

use strict;

%IBswcountlimits::cur_counts      = ();
%IBswcountlimits::new_counts      = ();
@IBswcountlimits::suppress_errors = ();
$IBswcountlimits::link_ends       = undef;
$IBswcountlimits::pause_time      = 10;
$IBswcountlimits::cache_dir       = "/var/cache/infiniband-diags";

# all the PerfMgt counters
@IBswcountlimits::counters = (
	"SymbolErrorCounter",        "LinkErrorRecoveryCounter",
	"LinkDownedCounter",          "PortRcvErrors",
	"PortRcvRemotePhysicalErrors", "PortRcvSwitchRelayErrors",
	"PortXmitDiscards",         "PortXmitConstraintErrors",
	"PortRcvConstraintErrors", "LocalLinkIntegrityErrors",
	"ExcessiveBufferOverrunErrors", "VL15Dropped",
	"PortXmitData",             "PortRcvData",
	"PortXmitPkts",             "PortRcvPkts"
);

# non-critical counters
%IBswcountlimits::error_counters = (
	"SymbolErrorCounter",
"No action is required except if counter is increasing along with LinkErrorRecoveryCounter",
	"LinkErrorRecoveryCounter",
"If this is increasing along with SymbolErrorCounter this may indicate a bad link, run ibswportwatch.pl on this port",
	"LinkDownedCounter",
	"Number of times the port has gone down (Usually for valid reasons)",
	"PortRcvErrors",
"This is a bad link, if the link is internal to a 288 try setting SDR, otherwise check the cable",
	"PortRcvRemotePhysicalErrors",
	"This indicates a problem ELSEWHERE in the fabric.",
	"PortXmitDiscards",
"This is a symptom of congestion and may require tweaking either HOQ or switch lifetime values",
	"PortXmitConstraintErrors",
	"This is a result of bad partitioning, check partition configuration.",
	"PortRcvConstraintErrors",
	"This is a result of bad partitioning, check partition configuration.",
	"LocalLinkIntegrityErrors",
	"May indicate a bad link, run ibswportwatch.pl on this port",
	"ExcessiveBufferOverrunErrors",
"This is a flow control state machine error and can be caused by packets with physical errors",
	"VL15Dropped",
	"check with ibswportwatch.pl, if increasing in SMALL increments, OK",
	"PortRcvSwitchRelayErrors",
	"This counter can increase due to a valid network event"
);

sub check_counters
{
	my $print_action = $_[0];
	my $actions      = undef;

	COUNTER: foreach my $cnt (keys %IBswcountlimits::error_counters) {
		if ($IBswcountlimits::cur_counts{$cnt} > 0) {
			foreach my $sup_cnt (@IBswcountlimits::suppress_errors) {
				if ("$cnt" eq $sup_cnt) { next COUNTER; }
			}
			print " [$cnt == $IBswcountlimits::cur_counts{$cnt}]";
			if ("$print_action" eq "yes") {
				$actions = join " ",
				  (
					$actions,
					"         $cnt: $IBswcountlimits::error_counters{$cnt}\n"
				  );
			}
		}
	}

	if ($actions) {
		print "\n         Actions:\n$actions";
	}
}

# Data counters
%IBswcountlimits::data_counters = (
	"PortXmitData",
"Total number of data octets, divided by 4, transmitted on all VLs from the port",
	"PortRcvData",
"Total number of data octets, divided by 4, received on all VLs to the port",
	"PortXmitPkts",
"Total number of packets, excluding link packets, transmitted on all VLs from the port",
	"PortRcvPkts",
"Total number of packets, excluding link packets, received on all VLs to the port"
);

sub check_data_counters
{
	my $print_action = $_[0];
	my $actions      = undef;

	COUNTER: foreach my $cnt (keys %IBswcountlimits::data_counters) {
		print " [$cnt == $IBswcountlimits::cur_counts{$cnt}]";
		if ("$print_action" eq "yes") {
			$actions = join " ",
			  (
				$actions,
				"         $cnt: $IBswcountlimits::data_counters{$cnt}\n"
			  );
		}
	}
	if ($actions) {
		print "\n         Descriptions:\n$actions";
	}
}

sub print_data_rates
{
	COUNTER: foreach my $cnt (keys %IBswcountlimits::data_counters) {
		my $cnt_per_second = calculate_rate(
			$IBswcountlimits::cur_counts{$cnt},
			$IBswcountlimits::new_counts{$cnt}
		);
		print "   $cnt_per_second $cnt/second\n";
	}
}

# =========================================================================
# Rate dependent counters
# calculate the count/sec
# calculate_rate old_count new_count
sub calculate_rate
{
	my $rate    = 0;
	my $old_val = $_[0];
	my $new_val = $_[1];
	my $rate    = ($new_val - $old_val) / $IBswcountlimits::pause_time;
	return ($rate);
}
%IBswcountlimits::rate_dep_thresholds = (
	"SymbolErrorCounter", 10, "LinkErrorRecoveryCounter",        10,
	"PortRcvErrors",    10, "LocalLinkIntegrityErrors", 10,
	"PortXmitDiscards",  10
);

sub check_counter_rates
{
	foreach my $rate_count (keys %IBswcountlimits::rate_dep_thresholds) {
		my $rate = calculate_rate(
			$IBswcountlimits::cur_counts{$rate_count},
			$IBswcountlimits::new_counts{$rate_count}
		);
		if ($rate > $IBswcountlimits::rate_dep_thresholds{$rate_count}) {
			print "Detected excessive rate for $rate_count ($rate cnts/sec)\n";
		} elsif ($rate > 0) {
			print "Detected rate for $rate_count ($rate cnts/sec)\n";
		}
	}
}

# =========================================================================
#
sub clear_counters
{
	# clear the counters
	foreach my $count (@IBswcountlimits::counters) {
		$IBswcountlimits::cur_counts{$count} = 0;
	}
}

# =========================================================================
#
sub any_counts
{
	my $total = 0;
	my $count = 0;
	foreach $count (keys %IBswcountlimits::critical) {
		$total = $total + $IBswcountlimits::cur_counts{$count};
	}
	COUNTER: foreach $count (keys %IBswcountlimits::error_counters) {
		foreach my $sup_cnt (@IBswcountlimits::suppress_errors) {
			if ("$count" eq $sup_cnt) { next COUNTER; }
		}
		$total = $total + $IBswcountlimits::cur_counts{$count};
	}
	return ($total);
}

# =========================================================================
#
sub ensure_cache_dir
{
	if (!(-d "$IBswcountlimits::cache_dir") &&
	    !mkdir($IBswcountlimits::cache_dir, 0700)) {
		die "cannot create $IBswcountlimits::cache_dir: $!\n";
	}
}

# =========================================================================
# get_cache_file(ca_name, ca_port)
#
sub get_cache_file
{
	my $ca_name = $_[0];
	my $ca_port = $_[1];
	ensure_cache_dir;
	return (
		"$IBswcountlimits::cache_dir/ibnetdiscover-$ca_name-$ca_port.topology");
}

# =========================================================================
# get_ca_name_port_param_string(ca_name, ca_port)
#
sub get_ca_name_port_param_string
{
	my $ca_name = $_[0];
	my $ca_port = $_[1];

	if ("$ca_name" ne "") { $ca_name = "-C $ca_name"; }
	if ("$ca_port" ne "") { $ca_port = "-P $ca_port"; }

	return ("$ca_name $ca_port");
}

# =========================================================================
# generate_ibnetdiscover_topology(ca_name, ca_port)
#
sub generate_ibnetdiscover_topology
{
	my $ca_name      = $_[0];
	my $ca_port      = $_[1];
	my $cache_file   = get_cache_file($ca_name, $ca_port);
	my $extra_params = get_ca_name_port_param_string($ca_name, $ca_port);

	if (`ibnetdiscover -g $extra_params > $cache_file`) {
		die "Execution of ibnetdiscover failed: $!\n";
	}
}

# =========================================================================
# get_link_ends(regenerate_map, ca_name, ca_port)
#
sub get_link_ends
{
	my $regenerate_map = $_[0];
	my $ca_name        = $_[1];
	my $ca_port        = $_[2];

	my $cache_file = get_cache_file($ca_name, $ca_port);

	if ($regenerate_map || !(-f "$cache_file")) {
		generate_ibnetdiscover_topology($ca_name, $ca_port);
	}
	open IBNET_TOPO, "<$cache_file"
	  or die "Failed to open ibnet topology: $!\n";
	my $in_switch  = "no";
	my $desc       = "";
	my $guid       = "";
	my $loc_sw_lid = "";

	my $loc_port = "";
	my $line     = "";

	while ($line = <IBNET_TOPO>) {
		if ($line =~ /^Switch.*\"S-(.*)\"\s+#.*\"(.*)\".* lid (\d+).*/) {
			$guid       = $1;
			$desc       = $2;
			$loc_sw_lid = $3;
			$in_switch  = "yes";
		}
		if ($in_switch eq "yes") {
			my $rec = undef;
			if ($line =~
/^\[(\d+)\]\s+\"[HSR]-(.+)\"\[(\d+)\](\(.+\))?\s+#.*\"(.*)\"\.* lid (\d+).*/
			  )
			{
				$loc_port = $1;
				my $rem_guid      = $2;
				my $rem_port      = $3;
				my $rem_port_guid = $4;
				my $rem_desc      = $5;
				my $rem_lid       = $6;
				$rec = {
					loc_guid      => "0x$guid",
					loc_port      => $loc_port,
					loc_ext_port  => "",
					loc_desc      => $desc,
					loc_sw_lid    => $loc_sw_lid,
					rem_guid      => "0x$rem_guid",
					rem_lid       => $rem_lid,
					rem_port      => $rem_port,
					rem_ext_port  => "",
					rem_desc      => $rem_desc,
					rem_port_guid => $rem_port_guid
				};
			}
			if ($line =~
/^\[(\d+)\]\[ext (\d+)\]\s+\"[HSR]-(.+)\"\[(\d+)\](\(.+\))?\s+#.*\"(.*)\"\.* lid (\d+).*/
			  )
			{
				$loc_port = $1;
				my $loc_ext_port  = $2;
				my $rem_guid      = $3;
				my $rem_port      = $4;
				my $rem_port_guid = $5;
				my $rem_desc      = $6;
				my $rem_lid       = $7;
				$rec = {
					loc_guid      => "0x$guid",
					loc_port      => $loc_port,
					loc_ext_port  => $loc_ext_port,
					loc_desc      => $desc,
					loc_sw_lid    => $loc_sw_lid,
					rem_guid      => "0x$rem_guid",
					rem_lid       => $rem_lid,
					rem_port      => $rem_port,
					rem_ext_port  => "",
					rem_desc      => $rem_desc,
					rem_port_guid => $rem_port_guid
				};
			}
			if ($line =~
/^\[(\d+)\]\s+\"[HSR]-(.+)\"\[(\d+)\]\[ext (\d+)\](\(.+\))?\s+#.*\"(.*)\"\.* lid (\d+).*/
			  )
			{
				$loc_port = $1;
				my $rem_guid      = $2;
				my $rem_port      = $3;
				my $rem_ext_port  = $4;
				my $rem_port_guid = $5;
				my $rem_desc      = $6;
				my $rem_lid       = $7;
				$rec = {
					loc_guid      => "0x$guid",
					loc_port      => $loc_port,
					loc_ext_port  => "",
					loc_desc      => $desc,
					loc_sw_lid    => $loc_sw_lid,
					rem_guid      => "0x$rem_guid",
					rem_lid       => $rem_lid,
					rem_port      => $rem_port,
					rem_ext_port  => $rem_ext_port,
					rem_desc      => $rem_desc,
					rem_port_guid => $rem_port_guid
				};
			}
			if ($line =~
/^\[(\d+)\]\[ext (\d+)\]\s+\"[HSR]-(.+)\"\[(\d+)\]\[ext (\d+)\](\(.+\))?\s+#.*\"(.*)\"\.* lid (\d+).*/
			  )
			{
				$loc_port = $1;
				my $loc_ext_port  = $2;
				my $rem_guid      = $3;
				my $rem_port      = $4;
				my $rem_ext_port  = $5;
				my $rem_port_guid = $6;
				my $rem_desc      = $7;
				my $rem_lid       = $8;
				$rec = {
					loc_guid      => "0x$guid",
					loc_port      => $loc_port,
					loc_ext_port  => $loc_ext_port,
					loc_desc      => $desc,
					loc_sw_lid    => $loc_sw_lid,
					rem_guid      => "0x$rem_guid",
					rem_lid       => $rem_lid,
					rem_port      => $rem_port,
					rem_ext_port  => $rem_ext_port,
					rem_desc      => $rem_desc,
					rem_port_guid => $rem_port_guid
				};
			}
			if ($rec) {
				$rec->{rem_port_guid} =~ s/\((.*)\)/$1/;
				$IBswcountlimits::link_ends{"0x$guid"}{$loc_port} = $rec;
			}
		}

		if ($line =~ /^Ca.*/ || $line =~ /^Rt.*/) { $in_switch = "no"; }
	}
	close IBNET_TOPO;
}

# =========================================================================
# get_num_ports(switch_guid, ca_name, ca_port)
#
sub get_num_ports
{
	my $guid         = $_[0];
	my $ca_name      = $_[1];
	my $ca_port      = $_[2];
	my $num_ports    = 0;
	my $extra_params = get_ca_name_port_param_string($ca_name, $ca_port);

	my $data         = `smpquery $extra_params -G nodeinfo $guid` ||
		die "'smpquery $extra_params -G nodeinfo $guid' faild\n";
	my @lines        = split("\n", $data);
	my $pkt_lifetime = "";
	foreach my $line (@lines) {
		if ($line =~ /^NumPorts:\.+(.*)/) { $num_ports = $1; }
	}
	return ($num_ports);
}

# =========================================================================
# format_guid(guid)
# The diags store the guids as strings.  This converts the guid supplied
# to the correct string format.
# eg: 0x0008f10400411f56 == 0x8f10400411f56
#
sub format_guid
{
	my $guid     = $_[0];
	my $guid_str = "";

	$guid =~ tr/[A-F]/[a-f]/;
	if ($guid =~ /0x(.*)/) {
		$guid_str = sprintf("0x%016s", $1);
	} else {
		$guid_str = sprintf("0x%016s", $guid);
	}
	return ($guid_str);
}

# =========================================================================
# convert_dr_to_guid(direct_route)
#
sub convert_dr_to_guid
{
	my $guid = undef;

	my $data = `smpquery nodeinfo -D $_[0]` ||
		die "'mpquery nodeinfo -D $_[0]' failed\n";
	my @lines = split("\n", $data);
	foreach my $line (@lines) {
		if ($line =~ /^PortGuid:\.+(.*)/) { $guid = $1; }
	}
	return format_guid($guid);
}

# =========================================================================
# get_node_type(guid_or_direct_route)
#
sub get_node_type
{
	my $type      = undef;
	my $query_arg = "smpquery nodeinfo ";
	if ($_[0] =~ /x/) {
		# assume arg is a guid if contains an x
		$query_arg .= "-G " . $_[0];
	} else {
		# assume arg is a direct path
		$query_arg .= "-D " . $_[0];
	}

	my $data = `$query_arg` ||
		die "'$query_arg' failed\n";
	my @lines = split("\n", $data);
	foreach my $line (@lines) {
		if ($line =~ /^NodeType:\.+(.*)/) { $type = $1; }
	}
	return $type;
}

# =========================================================================
# is_switch(guid_or_direct_route)
#
sub is_switch
{
	my $node_type = &get_node_type($_[0]);
	return ($node_type =~ /Switch/);
}