This file is indexed.

/usr/sbin/check_lft_balance is in infiniband-diags 1.6.1-1.

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

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
#!/usr/bin/perl
#
# Copyright (C) 2001-2003 The Regents of the University of California.
# Copyright (c) 2006 The Regents of the University of California.
# Copyright (c) 2007-2008 Voltaire, Inc. All rights reserved.
#
# Produced at Lawrence Livermore National Laboratory.
# Written by Ira Weiny <weiny2@llnl.gov>
#            Jim Garlick <garlick@llnl.gov>
#            Albert Chu <chu11@llnl.gov>
#
# 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;

use Getopt::Std;

my $ibnetdiscover_cache = "";
my $dump_lft_file       = "";
my $verbose             = 0;

my $switch_lid                            = undef;
my $switch_guid                           = undef;
my $switch_name                           = undef;
my %switch_port_count                     = ();
my @switch_maybe_directly_connected_hosts = ();
my $host                                  = undef;
my @host_ports                            = ();

my @lft_lines = ();
my $lft_line;

my $lids_per_port;
my $lids_per_port_calculated;

my $heuristic_flag = 0;

sub usage
{
	my $prog = `basename $0`;

	chomp($prog);
	print "Usage: $prog -l lft-output -i ibnetdiscover-cache [-e] [-v]\n";
	print "  Generate lft-output via \"dump_lfts.sh > lft-output\"\n";
	print "  Generate ibnetdiscover-cache via \"ibnetdiscover --cache ibnetdiscover-cache\"\n";
	print "  -e turn on heuristic(s) to look at switch balances deeper\n";
	print "  -v verbose output, output all switches\n";
	exit 2;
}

sub is_port_up
{
	my $iblinkinfo_output = $_[0];
	my $port              = $_[1];
	my $decport;
	my @lines;
	my $line;

	$port =~ /0+(.+)/;
	$decport = $1;

	# Add a space if necessary
	if ($decport >= 1 && $decport <= 9) {
		$decport = " $decport";
	}

	@lines = split("\n", $iblinkinfo_output);
	foreach $line (@lines) {
		if ($line =~ /$decport\[..\] ==/) {
			if ($line =~ /Down/) {
				return 0;
			}
			else {
				return 1;
			}
		}
	}

	# return 0 if not found
	return 0;
}

sub is_directly_connected
{
	my $iblinkinfo_output = $_[0];
	my $port              = $_[1];
	my $decport;
	my $str;
	my $rv = 0;
	my $host_tmp;
	my @lines;
	my $line;

	if (($switch_port_count{$port} != $lids_per_port)
		|| !(@switch_maybe_directly_connected_hosts))
	{
		return $rv;
	}

	$port =~ /0+(.+)/;
	$decport = $1;

	# Add a space if necessary
	if ($decport >= 1 && $decport <= 9) {
		$decport = " $decport";
	}

	@lines = split("\n", $iblinkinfo_output);
	foreach $line (@lines) {
		if ($line =~ /$decport\[..\] ==/) {
			$str = $line;
		}
	}

	if ($str =~ "Active") {
		$str =~
/[\d]+[\s]+[\d]+\[.+\]  \=\=.+\=\=>[\s]+[\d]+[\s]+[\d]+\[.+\] \"(.+)\".+/;
		for $host_tmp (@switch_maybe_directly_connected_hosts) {
			if ($1 == $host_tmp) {
				$rv = 1;
				last;
			}
		}
	}

	return $rv;
}

sub output_switch_port_usage
{
	my $min_usage = 999999;
	my $max_usage = 0;
	my $min_usage2 = 999999;
	my $max_usage2 = 0;
	my @ports     = (
		"001", "002", "003", "004", "005", "006", "007", "008",
		"009", "010", "011", "012", "013", "014", "015", "016",
		"017", "018", "019", "020", "021", "022", "023", "024",
		"025", "026", "027", "028", "029", "030", "031", "032",
		"033", "034", "035", "036"
	);
	my @output_ports = ();
	my @double_check_ports = ();
	my $port;
	my $iblinkinfo_output;
	my $is_unbalanced = 0;
	my $ports_on_switch = 0;
	my $all_zero_flag = 1;
	my $ret;

        $iblinkinfo_output = `iblinkinfo --load-cache $ibnetdiscover_cache -S $switch_guid`;

	for $port (@ports) {
		if (!defined($switch_port_count{$port})) {
			$switch_port_count{$port} = 0;
		}

		if ($switch_port_count{$port} == 0) {
			# If port is down, don't use it in this calculation
			$ret = is_port_up($iblinkinfo_output, $port);
			if ($ret == 0) {
				next;
			}
		}

		$ports_on_switch++;

		# If port is directly connected to a node, don't use
		# it in this calculation.
		if (is_directly_connected($iblinkinfo_output, $port) == 1) {
			next;
		}

		# Save off ports that should be output later
		push(@output_ports, $port);

		if ($switch_port_count{$port} < $min_usage) {
			$min_usage = $switch_port_count{$port};
		}
		if ($switch_port_count{$port} > $max_usage) {
			$max_usage = $switch_port_count{$port};
		}
	}

	if ($max_usage > ($min_usage + 1)) {
		$is_unbalanced = 1;
	}

	# In the event this is a switch lineboard, it will almost always never
	# balanced.  Half the ports go up to the spine, and the rest of the ports
	# go down to HCAs.  So we will do a special heuristic:
	#
	# If about 1/2 of the remaining ports are balanced, then we will consider the
	# entire switch balanced.
	#
	# Also, we do this only if there are enough alive ports on the switch to care.
	# I picked 12 somewhat randomly
	if ($heuristic_flag == 1
	    && $is_unbalanced == 1
	    && $ports_on_switch > 12) {

		@double_check_ports = ();

		for $port (@output_ports) {
			if ($switch_port_count{$port} == $max_usage
			    || $switch_port_count{$port} == ($max_usage - 1)
			    || $switch_port_count{$port} == 0) {
				next;
			}

			push(@double_check_ports, $port);
		}

		# we'll call half +/- 1 "about half"
		if (@double_check_ports == int($ports_on_switch / 2)
		    || @double_check_ports == int($ports_on_switch / 2) + 1
		    || @double_check_ports == int($ports_on_switch / 2) - 1) {
			for $port (@double_check_ports) {
				if ($switch_port_count{$port} < $min_usage2) {
					$min_usage2 = $switch_port_count{$port};
				}
				if ($switch_port_count{$port} > $max_usage2) {
					$max_usage2 = $switch_port_count{$port};
				}
			}

			if (!($max_usage2 > ($min_usage2 + 1))) {
				$is_unbalanced = 0;
			}
		}
	}

	# Another special case is when you have a non-fully-populated switch
	# Many ports will be zero.  So if all active ports != max or max-1 are = 0
	# we will also consider this balanced.
	if ($heuristic_flag == 1
	    && $is_unbalanced == 1
	    && $ports_on_switch > 12) {

		@double_check_ports = ();

		for $port (@output_ports) {
			if ($switch_port_count{$port} == $max_usage
			    || $switch_port_count{$port} == ($max_usage - 1)) {
				next;
			}

			push(@double_check_ports, $port);
		}

		for $port (@double_check_ports) {
			if ($switch_port_count{$port} != 0) {
				$all_zero_flag = 0;
				last;
			}
		}

		if ($all_zero_flag == 1) {
			$is_unbalanced = 0;
		}
	}

	if ($verbose || $is_unbalanced == 1) {
		if ($is_unbalanced == 1) {
			print "Unbalanced Switch Port Usage: ";
			print "$switch_name, $switch_guid, $switch_lid\n";
		} else {
			print
			  "Switch Port Usage: $switch_name, $switch_guid, $switch_lid\n";
		}
		for $port (@output_ports) {
			print "Port $port: $switch_port_count{$port}\n";
		}
	}
}

sub process_host_ports
{
	my $test_port;
	my $tmp;
	my $flag = 0;

	if (@host_ports == $lids_per_port) {
		# Are all the host ports identical?
		$test_port = $host_ports[0];
		for $tmp (@host_ports) {
			if ($tmp != $test_port) {
				$flag = 1;
				last;
			}
		}
		# If all host ports are identical, maybe its directly
		# connected to a host.
		if ($flag == 0) {
			push(@switch_maybe_directly_connected_hosts, $host);
		}
	}
}

if (!getopts("hl:i:ve")) {
	usage();
}

if (defined($main::opt_h)) {
	usage();
}

if (defined($main::opt_l)) {
	$dump_lft_file = $main::opt_l;
} else {
	print STDERR ("Must specify dump lfts file\n");
	usage();
	exit 1;
}

if (defined($main::opt_i)) {
	$ibnetdiscover_cache = $main::opt_i;
} else {
	print STDERR ("Must specify ibnetdiscover cache\n");
	usage();
	exit 1;
}

if (defined($main::opt_v)) {
	$verbose = 1;
}

if (defined($main::opt_e)) {
	$heuristic_flag = 1;
}

if (!open(FH, "< $dump_lft_file")) {
	print STDERR ("Couldn't open dump lfts file: $dump_lft_file: $!\n");
}

@lft_lines = <FH>;

foreach $lft_line (@lft_lines) {
	chomp($lft_line);
	if ($lft_line =~ /Unicast/) {
		$lft_line =~ /Unicast lids .+ of switch Lid (.+) guid (.+) \((.+)\)/;
		if (@host_ports) {
			process_host_ports();
		}
		if (defined($switch_name)) {
			output_switch_port_usage();
		}
		$switch_lid                            = $1;
		$switch_guid                           = $2;
		$switch_name                           = $3;
		@switch_maybe_directly_connected_hosts = ();
		%switch_port_count                     = ();
		@host_ports                            = ();
		$lids_per_port                         = 0;
		$lids_per_port_calculated              = 0;
	} elsif ($lft_line =~ /Channel/ || $lft_line =~ /Router/) {
		$lft_line =~ /.+ (.+) : \(.+ portguid .+: '(.+)'\)/;
		$host = $2;
		$switch_port_count{$1}++;
		if (@host_ports) {
			process_host_ports();
		}
		@host_ports = ($1);

		if ($lids_per_port == 0) {
			$lids_per_port++;
		} else {
			$lids_per_port_calculated++;
		}
	} elsif ($lft_line =~ /path/) {
		$lft_line =~ /.+ (.+) : \(path #. out of .: portguid .+\)/;
		$switch_port_count{$1}++;
		if ($lids_per_port_calculated == 0) {
			$lids_per_port++;
		}
		push(@host_ports, $1);
	} else {
		if ($lids_per_port) {
			$lids_per_port_calculated++;
		}
		next;
	}
}

if (@host_ports) {
	process_host_ports();
}
output_switch_port_usage();