[ofa-general] [PATCH v3 2/3] Convert iblinkinfo.pl to C and use new ibnetdisc library.

Ira Weiny weiny2 at llnl.gov
Fri Apr 3 15:42:54 PDT 2009


>From a677ae35fe7a5966f05b5859df8f00e9b18df864 Mon Sep 17 00:00:00 2001
From: Ira Weiny <weiny2 at llnl.gov>
Date: Fri, 3 Apr 2009 15:28:18 -0700
Subject: [PATCH] Convert iblinkinfo.pl to C and use new ibnetdisc library.

Signed-off-by: Ira Weiny <weiny2 at llnl.gov>
---
 infiniband-diags/Makefile.am              |    7 +-
 infiniband-diags/configure.in             |    1 +
 infiniband-diags/scripts/iblinkinfo.pl    |  327 ------------------------
 infiniband-diags/scripts/iblinkinfo.pl.in |   40 +++
 infiniband-diags/src/iblinkinfo.c         |  386 +++++++++++++++++++++++++++++
 5 files changed, 432 insertions(+), 329 deletions(-)
 delete mode 100755 infiniband-diags/scripts/iblinkinfo.pl
 create mode 100755 infiniband-diags/scripts/iblinkinfo.pl.in
 create mode 100644 infiniband-diags/src/iblinkinfo.c

diff --git a/infiniband-diags/Makefile.am b/infiniband-diags/Makefile.am
index 7b8523a..b480a4a 100644
--- a/infiniband-diags/Makefile.am
+++ b/infiniband-diags/Makefile.am
@@ -1,6 +1,7 @@
 SUBDIRS = libibnetdisc
 
-INCLUDES = -I$(top_builddir)/include/ -I$(srcdir)/include -I$(includedir) -I$(includedir)/infiniband
+INCLUDES = -I$(top_builddir)/include/ -I$(srcdir)/include -I$(includedir) -I$(includedir)/infiniband \
+	-I$(top_builddir)/libibnetdisc/include
 
 if DEBUG
 DBGFLAGS = -ggdb -D_DEBUG_
@@ -11,7 +12,7 @@ endif
 sbin_PROGRAMS = src/ibaddr src/ibnetdiscover src/ibping src/ibportstate \
 	        src/ibroute src/ibstat src/ibsysstat src/ibtracert \
 	        src/perfquery src/sminfo src/smpdump src/smpquery \
-	        src/saquery src/vendstat
+	        src/saquery src/vendstat src/iblinkinfo
 
 if ENABLE_TEST_UTILS
 sbin_PROGRAMS += src/ibsendtrap src/mcm_rereg_test
@@ -55,6 +56,8 @@ src_saquery_SOURCES = src/saquery.c
 src_ibsendtrap_SOURCES = src/ibsendtrap.c
 src_vendstat_SOURCES = src/vendstat.c
 src_mcm_rereg_test_SOURCES = src/mcm_rereg_test.c
+src_iblinkinfo_SOURCES = src/iblinkinfo.c
+src_iblinkinfo_LDADD = -libnetdisc
 
 man_MANS = man/ibaddr.8 man/ibcheckerrors.8 man/ibcheckerrs.8 \
 	man/ibchecknet.8 man/ibchecknode.8 man/ibcheckport.8 \
diff --git a/infiniband-diags/configure.in b/infiniband-diags/configure.in
index 2b73167..4516dfa 100644
--- a/infiniband-diags/configure.in
+++ b/infiniband-diags/configure.in
@@ -166,6 +166,7 @@ AC_CONFIG_FILES([\
         scripts/ibnodes \
         scripts/ibswitches \
 	scripts/ibrouters \
+	scripts/iblinkinfo.pl \
    libibnetdisc/Makefile
 ])
 AC_OUTPUT
diff --git a/infiniband-diags/scripts/iblinkinfo.pl b/infiniband-diags/scripts/iblinkinfo.pl
deleted file mode 100755
index b6b27ce..0000000
--- a/infiniband-diags/scripts/iblinkinfo.pl
+++ /dev/null
@@ -1,327 +0,0 @@
-#!/usr/bin/perl
-#
-# 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 at 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;
-use IBswcountlimits;
-
-sub usage_and_exit
-{
-	my $prog = $_[0];
-	print
-"Usage: $prog [-Rhclp -S <guid> -D <direct route> -C <ca_name> -P <ca_port>]\n";
-	print
-"   Report link speed and connection for each port of each switch which is active\n";
-	print "   -h This help message\n";
-	print
-"   -R Recalculate ibnetdiscover information (Default is to reuse ibnetdiscover output)\n";
-	print
-"   -D <direct route> output only the switch specified by direct route path\n";
-	print "   -S <guid> output only the switch specified by <guid> (hex format)\n";
-	print "   -d print only down links\n";
-	print
-	  "   -l (line mode) print all information for each link on each line\n";
-	print
-"   -p print additional switch settings (PktLifeTime,HoqLife,VLStallCount)\n";
-	print "   -c print port capabilities (enabled/supported values)\n";
-	print "   -C <ca_name> use selected Channel Adaptor name for queries\n";
-	print "   -P <ca_port> use selected channel adaptor port for queries\n";
-	print "   -g print port guids instead of node guids\n";
-	exit 2;
-}
-
-my $argv0              = `basename $0`;
-my $regenerate_map     = undef;
-my $single_switch      = undef;
-my $direct_route       = undef;
-my $line_mode          = undef;
-my $print_add_switch   = undef;
-my $print_extended_cap = undef;
-my $only_down_links    = undef;
-my $ca_name            = "";
-my $ca_port            = "";
-my $print_port_guids   = undef;
-my $switch_found       = "no";
-chomp $argv0;
-
-if (!getopts("hcpldRS:D:C:P:g")) { usage_and_exit $argv0; }
-if (defined $Getopt::Std::opt_h) { usage_and_exit $argv0; }
-if (defined $Getopt::Std::opt_D) { $direct_route   = $Getopt::Std::opt_D; }
-if (defined $Getopt::Std::opt_R) { $regenerate_map = $Getopt::Std::opt_R; }
-if (defined $Getopt::Std::opt_S) {
-	$single_switch = format_guid($Getopt::Std::opt_S);
-}
-if (defined $Getopt::Std::opt_d) { $only_down_links    = $Getopt::Std::opt_d; }
-if (defined $Getopt::Std::opt_l) { $line_mode          = $Getopt::Std::opt_l; }
-if (defined $Getopt::Std::opt_p) { $print_add_switch   = $Getopt::Std::opt_p; }
-if (defined $Getopt::Std::opt_c) { $print_extended_cap = $Getopt::Std::opt_c; }
-if (defined $Getopt::Std::opt_C) { $ca_name            = $Getopt::Std::opt_C; }
-if (defined $Getopt::Std::opt_P) { $ca_port            = $Getopt::Std::opt_P; }
-if (defined $Getopt::Std::opt_g) { $print_port_guids   = $Getopt::Std::opt_g; }
-
-my $extra_smpquery_params = get_ca_name_port_param_string($ca_name, $ca_port);
-
-sub main
-{
-	get_link_ends($regenerate_map, $ca_name, $ca_port);
-	if (defined($direct_route)) {
-		# convert DR to guid, then use original single_switch option
-		$single_switch = convert_dr_to_guid($direct_route);
-		if (!defined($single_switch) || !is_switch($single_switch)) {
-			printf("The direct route (%s) does not map to a switch.\n",
-				$direct_route);
-			return;
-		}
-	}
-	foreach my $switch (sort (keys(%IBswcountlimits::link_ends))) {
-		if ($single_switch && $switch ne $single_switch) {
-			next;
-		} else {
-			$switch_found = "yes";
-		}
-		my $switch_prompt = "no";
-		my $num_ports = get_num_ports($switch, $ca_name, $ca_port);
-		if ($num_ports == 0) {
-			printf("ERROR: switch $switch has 0 ports???\n");
-		}
-		my @output_lines    = undef;
-		my $pkt_lifetime    = "";
-		my $pkt_life_prompt = "";
-		my $port_timeouts   = "";
-		my $print_switch    = "yes";
-		if ($only_down_links) { $print_switch = "no"; }
-		if ($print_add_switch) {
-			my $data = `smpquery $extra_smpquery_params -G switchinfo $switch`;
-			if ($data eq "") {
-				printf("ERROR: failed to get switchinfo for $switch\n");
-			}
-			my @lines = split("\n", $data);
-			foreach my $line (@lines) {
-				if ($line =~ /^LifeTime:\.+(.*)/) { $pkt_lifetime = $1; }
-			}
-			$pkt_life_prompt = sprintf(" (LT: %2s)", $pkt_lifetime);
-		}
-		foreach my $port (1 .. $num_ports) {
-			my $hr = $IBswcountlimits::link_ends{$switch}{$port};
-			if ($switch_prompt eq "no" && !$line_mode) {
-				my $switch_name = "";
-				my $tmp_port = $port;
-				while ($switch_name eq "" && $tmp_port <= $num_ports) {
-					# the first port is down find switch name with up port
-					my $hr = $IBswcountlimits::link_ends{$switch}{$tmp_port};
-					$switch_name = $hr->{loc_desc};
-					$tmp_port++;
-				}
-				if ($switch_name eq "") {
-					printf(
-						"WARNING: Switch Name not found for $switch\n");
-				}
-				push(
-					@output_lines,
-					sprintf(
-						"Switch %18s %s%s:\n",
-						$switch, $switch_name, $pkt_life_prompt
-					)
-				);
-				$switch_prompt = "yes";
-			}
-			my $data =
-			  `smpquery $extra_smpquery_params -G portinfo $switch $port`;
-			if ($data eq "") {
-				printf(
-					"ERROR: failed to get portinfo for $switch port $port\n");
-			}
-			my @lines          = split("\n", $data);
-			my $speed          = "";
-			my $speed_sup      = "";
-			my $speed_enable   = "";
-			my $width          = "";
-			my $width_sup      = "";
-			my $width_enable   = "";
-			my $state          = "";
-			my $hoq_life       = "";
-			my $vl_stall       = "";
-			my $phy_link_state = "";
-
-			foreach my $line (@lines) {
-				if ($line =~ /^LinkSpeedActive:\.+(.*)/) { $speed = $1; }
-				if ($line =~ /^LinkSpeedEnabled:\.+(.*)/) {
-					$speed_enable = $1;
-				}
-				if ($line =~ /^LinkSpeedSupported:\.+(.*)/) { $speed_sup = $1; }
-				if ($line =~ /^LinkWidthActive:\.+(.*)/)    { $width     = $1; }
-				if ($line =~ /^LinkWidthEnabled:\.+(.*)/) {
-					$width_enable = $1;
-				}
-				if ($line =~ /^LinkWidthSupported:\.+(.*)/) { $width_sup = $1; }
-				if ($line =~ /^LinkState:\.+(.*)/)          { $state     = $1; }
-				if ($line =~ /^HoqLife:\.+(.*)/)            { $hoq_life  = $1; }
-				if ($line =~ /^VLStallCount:\.+(.*)/)       { $vl_stall  = $1; }
-				if ($line =~ /^PhysLinkState:\.+(.*)/) { $phy_link_state = $1; }
-			}
-			my $rem_port         = $hr->{rem_port};
-			my $rem_lid          = $hr->{rem_lid};
-			my $rem_speed_sup    = "";
-			my $rem_speed_enable = "";
-			my $rem_width_sup    = "";
-			my $rem_width_enable = "";
-			if ($rem_lid ne "" && $rem_port ne "") {
-				$data =
-				  `smpquery $extra_smpquery_params portinfo $rem_lid $rem_port`;
-				if ($data eq "") {
-					printf(
-						"ERROR: failed to get portinfo for $switch port $port\n"
-					);
-				}
-				my @lines = split("\n", $data);
-				foreach my $line (@lines) {
-					if ($line =~ /^LinkSpeedEnabled:\.+(.*)/) {
-						$rem_speed_enable = $1;
-					}
-					if ($line =~ /^LinkSpeedSupported:\.+(.*)/) {
-						$rem_speed_sup = $1;
-					}
-					if ($line =~ /^LinkWidthEnabled:\.+(.*)/) {
-						$rem_width_enable = $1;
-					}
-					if ($line =~ /^LinkWidthSupported:\.+(.*)/) {
-						$rem_width_sup = $1;
-					}
-				}
-			}
-			my $capabilities = "";
-			if ($print_extended_cap) {
-				$capabilities = sprintf("(%3s %s %6s / %8s [%s/%s][%s/%s])",
-					$width, $speed, $state, $phy_link_state, $width_enable,
-					$width_sup, $speed_enable, $speed_sup);
-			} else {
-				$capabilities = sprintf("(%3s %s %6s / %8s)",
-					$width, $speed, $state, $phy_link_state);
-			}
-			if ($print_add_switch) {
-				$port_timeouts =
-				  sprintf(" (HOQ:%s VL_Stall:%s)", $hoq_life, $vl_stall);
-			}
-			if (!$only_down_links || ($only_down_links && $state eq "Down")) {
-				my $width_msg = "";
-				my $speed_msg = "";
-				if ($rem_width_enable ne "" && $rem_width_sup ne "") {
-					if (   $width_enable =~ /12X/
-						&& $rem_width_enable =~ /12X/
-						&& $width !~ /12X/)
-					{
-						$width_msg = "Could be 12X";
-					} else {
-						if (   $width_enable =~ /8X/
-							&& $rem_width_enable =~ /8X/
-							&& $width !~ /8X/)
-						{
-							$width_msg = "Could be 8X";
-						} else {
-							if (   $width_enable =~ /4X/
-								&& $rem_width_enable =~ /4X/
-								&& $width !~ /4X/)
-							{
-								$width_msg = "Could be 4X";
-							}
-						}
-					}
-				}
-				if ($rem_speed_enable ne "" && $rem_speed_sup ne "") {
-					if (   $speed_enable =~ /10\.0/
-						&& $rem_speed_enable =~ /10\.0/
-						&& $speed !~ /10\.0/)
-					{
-						$speed_msg = "Could be 10.0 Gbps";
-					} else {
-						if (   $speed_enable =~ /5\.0/
-							&& $rem_speed_enable =~ /5\.0/
-							&& $speed !~ /5\.0/)
-						{
-							$speed_msg = "Could be 5.0 Gbps";
-						}
-					}
-				}
-
-				if ($line_mode) {
-					my $line_begin = sprintf("%18s \"%30s\"%s",
-						$switch, $hr->{loc_desc}, $pkt_life_prompt);
-					my $ext_guid = sprintf("%18s", $hr->{rem_guid});
-					if ($print_port_guids && $hr->{rem_port_guid} ne "") {
-						$ext_guid = sprintf("0x%016s", $hr->{rem_port_guid});
-					}
-					push(
-						@output_lines,
-						sprintf(
-"%s %6s %4s[%2s]  ==%s%s==>  %18s %6s %4s[%2s] \"%s\" ( %s %s)\n",
-							$line_begin,     $hr->{loc_sw_lid},
-							$port,           $hr->{loc_ext_port},
-							$capabilities,   $port_timeouts,
-							$ext_guid,       $hr->{rem_lid},
-							$hr->{rem_port}, $hr->{rem_ext_port},
-							$hr->{rem_desc}, $width_msg,
-							$speed_msg
-						)
-					);
-				} else {
-					push(
-						@output_lines,
-						sprintf(
-" %6s %4s[%2s]  ==%s%s==>  %6s %4s[%2s] \"%s\" ( %s %s)\n",
-							$hr->{loc_sw_lid},   $port,
-							$hr->{loc_ext_port}, $capabilities,
-							$port_timeouts,      $hr->{rem_lid},
-							$hr->{rem_port},     $hr->{rem_ext_port},
-							$hr->{rem_desc},     $width_msg,
-							$speed_msg
-						)
-					);
-				}
-				$print_switch = "yes";
-			}
-		}
-		if ($print_switch eq "yes") {
-			foreach my $line (@output_lines) { print $line; }
-		}
-	}
-	if ($single_switch && $switch_found ne "yes") {
-		printf("Switch \"%s\" not found.\n", $single_switch);
-	}
-}
-main;
-
diff --git a/infiniband-diags/scripts/iblinkinfo.pl.in b/infiniband-diags/scripts/iblinkinfo.pl.in
new file mode 100755
index 0000000..c81570d
--- /dev/null
+++ b/infiniband-diags/scripts/iblinkinfo.pl.in
@@ -0,0 +1,40 @@
+#!/usr/bin/perl
+#
+# Copyright (c) 2009 Lawrence Livermore National Security
+#
+# Produced at Lawrence Livermore National Laboratory.
+# Written by Ira Weiny <weiny2 at 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.
+#
+
+
+# this is not just a wrapper for the C based utility
+$str = join " ", at ARGV;
+exec "@IBSCRIPTPATH@/iblinkinfo $str";
diff --git a/infiniband-diags/src/iblinkinfo.c b/infiniband-diags/src/iblinkinfo.c
new file mode 100644
index 0000000..1e43788
--- /dev/null
+++ b/infiniband-diags/src/iblinkinfo.c
@@ -0,0 +1,386 @@
+/*
+ * Copyright (c) 2004-2007 Voltaire Inc.  All rights reserved.
+ * Copyright (c) 2007 Xsigo Systems Inc.  All rights reserved.
+ * Copyright (c) 2008 Lawrence Livermore National Lab.  All rights reserved.
+ *
+ * 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.
+ *
+ */
+
+#if HAVE_CONFIG_H
+#  include <config.h>
+#endif /* HAVE_CONFIG_H */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdarg.h>
+#include <time.h>
+#include <string.h>
+#include <getopt.h>
+#include <errno.h>
+#include <inttypes.h>
+
+#include <infiniband/complib/cl_nodenamemap.h>
+#include <infiniband/ibnetdisc.h>
+
+char *argv0 = "iblinkinfotest";
+static FILE *f;
+
+static char *node_name_map_file = NULL;
+static nn_map_t *node_name_map = NULL;
+
+static int timeout_ms = 500;
+
+static int down_links_only = 0;
+static int line_mode = 0;
+static int add_sw_settings = 0;
+static int print_port_guids = 0;
+
+static unsigned int
+get_max(unsigned int num)
+{
+	unsigned int v = num; // 32-bit word to find the log base 2 of
+	unsigned r = 0; // r will be lg(v)
+
+	while (v >>= 1) // unroll for more speed...
+	{
+		r++;
+	}
+
+	return (1 << r);
+}
+
+void
+get_msg(char *width_msg, char *speed_msg, int msg_size, ibnd_port_t *port)
+{
+	char buf[64];
+	uint32_t max_speed = 0;
+
+	uint32_t max_width = get_max(mad_get_field(port->info, 0,
+					IB_PORT_LINK_WIDTH_SUPPORTED_F)
+				& mad_get_field(port->remoteport->info, 0,
+					IB_PORT_LINK_WIDTH_SUPPORTED_F));
+	if ((max_width & mad_get_field(port->info, 0,
+				IB_PORT_LINK_WIDTH_ACTIVE_F)) == 0) {
+		// we are not at the max supported width
+		// print what we could be at.
+		snprintf(width_msg, msg_size, "Could be %s",
+			mad_dump_val(IB_PORT_LINK_WIDTH_ACTIVE_F,
+				buf, 64, &max_width));
+	}
+
+	max_speed = get_max(mad_get_field(port->info, 0,
+					IB_PORT_LINK_SPEED_SUPPORTED_F)
+				& mad_get_field(port->remoteport->info, 0,
+					IB_PORT_LINK_SPEED_SUPPORTED_F));
+	if ((max_speed & mad_get_field(port->info, 0,
+				IB_PORT_LINK_SPEED_ACTIVE_F)) == 0) {
+		// we are not at the max supported speed
+		// print what we could be at.
+		snprintf(speed_msg, msg_size, "Could be %s",
+			mad_dump_val(IB_PORT_LINK_SPEED_ACTIVE_F,
+				buf, 64, &max_speed));
+	}
+}
+
+void
+print_port(ibnd_node_t *node, ibnd_port_t *port)
+{
+	char width[64], speed[64], state[64], physstate[64];
+	char remote_guid_str[256];
+	char remote_str[256];
+	char link_str[256];
+	char width_msg[256];
+	char speed_msg[256];
+	char ext_port_str[256];
+	int iwidth = mad_get_field(port->info, 0, IB_PORT_LINK_WIDTH_ACTIVE_F);
+	int ispeed = mad_get_field(port->info, 0, IB_PORT_LINK_SPEED_ACTIVE_F);
+	int istate = mad_get_field(port->info, 0, IB_PORT_STATE_F);
+	int iphystate = mad_get_field(port->info, 0, IB_PORT_PHYS_STATE_F);
+	int n = 0;
+
+	if (!port)
+		return;
+
+	remote_guid_str[0] = '\0';
+	remote_str[0] = '\0';
+	link_str[0] = '\0';
+	width_msg[0] = '\0';
+	speed_msg[0] = '\0';
+
+	n = snprintf(link_str, 256, "(%3s %s %6s/%8s)",
+		mad_dump_val(IB_PORT_LINK_WIDTH_ACTIVE_F, width, 64, &iwidth),
+		mad_dump_val(IB_PORT_LINK_SPEED_ACTIVE_F, speed, 64, &ispeed),
+		mad_dump_val(IB_PORT_STATE_F, state, 64, &istate),
+		mad_dump_val(IB_PORT_PHYS_STATE_F, physstate, 64, &iphystate));
+
+	if (add_sw_settings)
+		snprintf(link_str+n, 256-n,
+			" (HOQ:%d VL_Stall:%d)",
+			mad_get_field(port->info, 0, IB_PORT_HOQ_LIFE_F),
+			mad_get_field(port->info, 0, IB_PORT_VL_STALL_COUNT_F));
+
+	if (port->remoteport) {
+		char *remap = remap_node_name(node_name_map, port->remoteport->node->guid,
+				port->remoteport->node->nodedesc);
+
+		if (port->remoteport->ext_portnum)
+			snprintf(ext_port_str, 256, "%d", port->remoteport->ext_portnum);
+		else
+			ext_port_str[0] = '\0';
+
+		get_msg(width_msg, speed_msg, 256, port);
+
+		if (line_mode) {
+			if (print_port_guids)
+				snprintf(remote_guid_str, 256, "0x%016"PRIx64" ",
+					port->remoteport->guid);
+			else
+				snprintf(remote_guid_str, 256, "0x%016"PRIx64" ",
+					port->remoteport->node->guid);
+		}
+
+		snprintf(remote_str, 256,
+			"%s%6d %4d[%2s] \"%s\" (%s %s)\n",
+			remote_guid_str,
+			port->remoteport->base_lid ?  port->remoteport->base_lid :
+				port->remoteport->node->smalid,
+			port->remoteport->portnum,
+			ext_port_str,
+			remap,
+			width_msg,
+			speed_msg);
+		free(remap);
+	} else
+		snprintf(remote_str, 256, "           [  ] \"\" ( )\n");
+
+	if (port->ext_portnum)
+		snprintf(ext_port_str, 256, "%d", port->ext_portnum);
+	else
+		ext_port_str[0] = '\0';
+
+	if (line_mode) {
+		char *remap = remap_node_name(node_name_map, node->guid,
+					node->nodedesc);
+		printf("0x%016"PRIx64" \"%30s\" ", node->guid, remap);
+		free(remap);
+	} else
+		printf("      ");
+
+	printf("%6d %4d[%2s] ==%s==>  %s",
+		node->smalid, port->portnum, ext_port_str, link_str, remote_str);
+}
+
+void
+print_switch(ibnd_node_t *node, void *user_data)
+{
+	int i = 0;
+
+	if (!line_mode) {
+		char *remap = remap_node_name(node_name_map, node->guid,
+					node->nodedesc);
+		printf("Switch 0x%016"PRIx64" %s:\n", node->guid, remap);
+		free(remap);
+	}
+
+	for (i = 1; i <= node->numports; i++) {
+		ibnd_port_t *port = node->ports[i];
+		if (!port)
+			continue;
+		if (!down_links_only ||
+				mad_get_field(port->info, 0, IB_PORT_STATE_F) == IB_LINK_DOWN) {
+			print_port(node, port);
+		}
+	}
+}
+
+void
+usage(void)
+{
+	fprintf(stderr,
+		"Usage: %s [-hclp -S <guid> -D <direct route> -C <ca_name> -P <ca_port>]\n"
+		"   Report link speed and connection for each port of each switch which is active\n"
+		"   -h This help message\n"
+		"   -S <guid> output only the node specified by guid\n"
+		"   -D <direct route> print only node specified by <direct route>\n"
+		"   -f <dr_path> specify node to start \"from\"\n"
+		"   -n <hops> Number of hops to include away from specified node\n"
+		"   -d print only down links\n"
+		"   -l (line mode) print all information for each link on each line\n"
+		"   -p print additional switch settings (PktLifeTime,HoqLife,VLStallCount)\n"
+
+
+		"   -t <timeout_ms> timeout for any single fabric query\n"
+		"   -s show progress during scan\n"
+		"   --node-name-map <map_file> use specified node name map\n"
+
+		"   -C <ca_name> use selected Channel Adaptor name for queries\n"
+		"   -P <ca_port> use selected channel adaptor port for queries\n"
+		"   -g print port guids instead of node guids\n"
+		"   --debug print debug messages\n"
+		"   -R (this option is obsolete and does nothing)\n"
+		,
+			argv0);
+	exit(-1);
+}
+
+int
+main(int argc, char **argv)
+{
+	char *ca = 0;
+	int ca_port = 0;
+	ibnd_fabric_t *fabric = NULL;
+	uint64_t guid = 0;
+	char *dr_path = NULL;
+	char *from = NULL;
+	int hops = 0;
+	ib_portid_t port_id;
+
+	static char const str_opts[] = "S:D:n:C:P:t:sldgphuf:R";
+	static const struct option long_opts[] = {
+		{ "S", 1, 0, 'S'},
+		{ "D", 1, 0, 'D'},
+		{ "num-hops", 1, 0, 'n'},
+		{ "down-links-only", 0, 0, 'd'},
+		{ "line-mode", 0, 0, 'l'},
+		{ "ca-name", 1, 0, 'C'},
+		{ "ca-port", 1, 0, 'P'},
+		{ "timeout", 1, 0, 't'},
+		{ "show", 0, 0, 's'},
+		{ "print-port-guids", 0, 0, 'g'},
+		{ "print-additional", 0, 0, 'p'},
+		{ "help", 0, 0, 'h'},
+		{ "usage", 0, 0, 'u'},
+		{ "node-name-map", 1, 0, 1},
+		{ "debug", 0, 0, 2},
+		{ "compat", 0, 0, 3},
+		{ "from", 1, 0, 'f'},
+		{ "R", 0, 0, 'R'},
+		{ }
+	};
+
+	f = stdout;
+
+	argv0 = argv[0];
+
+	while (1) {
+		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
+		if ( ch == -1 )
+			break;
+		switch(ch) {
+		case 1:
+			node_name_map_file = strdup(optarg);
+			break;
+		case 2:
+			ibnd_debug(1);
+			break;
+		case 'f':
+			from = strdup(optarg);
+			break;
+		case 'C':
+			ca = strdup(optarg);
+			break;
+		case 'P':
+			ca_port = strtoul(optarg, 0, 0);
+			break;
+		case 'D':
+			dr_path = strdup(optarg);
+			break;
+		case 'n':
+			hops = (int)strtol(optarg, NULL, 0);
+			break;
+		case 'd':
+			down_links_only = 1;
+			break;
+		case 'l':
+			line_mode = 1;
+			break;
+		case 't':
+			timeout_ms = strtoul(optarg, 0, 0);
+			break;
+		case 's':
+			ibnd_show_progress(1);
+			break;
+		case 'g':
+			print_port_guids = 1;
+			break;
+		case 'S':
+			guid = (uint64_t)strtoull(optarg, 0, 0);
+			break;
+		case 'p':
+			add_sw_settings = 1;
+			break;
+		case 'R':
+			/* GNDN */
+			break;
+		default:
+			usage();
+			break;
+		}
+	}
+	argc -= optind;
+	argv += optind;
+
+	if (argc && !(f = fopen(argv[0], "w")))
+		fprintf(stderr, "can't open file %s for writing", argv[0]);
+
+	node_name_map = open_node_name_map(node_name_map_file);
+
+	if (from) {
+		/* only scan part of the fabric */
+		str2drpath(&(port_id.drpath), from, 0, 0);
+		if ((fabric = ibnd_discover_fabric(ca, ca_port, timeout_ms, &port_id, hops)) == NULL) {
+			fprintf(stderr, "discover failed\n");
+			exit(1);
+		}
+		guid = 0;
+	} else {
+		if ((fabric = ibnd_discover_fabric(ca, ca_port, timeout_ms, NULL, -1)) == NULL) {
+			fprintf(stderr, "discover failed\n");
+			exit(1);
+		}
+	}
+
+	if (guid) {
+		ibnd_node_t *sw = ibnd_find_node_guid(fabric, guid);
+		print_switch(sw, NULL);
+	} else if (dr_path) {
+		ibnd_node_t *sw = ibnd_find_node_dr(fabric, dr_path);
+		print_switch(sw, NULL);
+	} else {
+		ibnd_iter_nodes_type(fabric, print_switch, IB_NODE_SWITCH, NULL);
+	}
+
+	ibnd_destroy_fabric(fabric);
+
+	close_node_name_map(node_name_map);
+	exit(0);
+}
-- 
1.5.4.5




More information about the general mailing list