>From b1c2cc8f96a3d88f2ef341ebee0b550fd5bd2a7b Mon Sep 17 00:00:00 2001
From: Ira Weiny <[EMAIL PROTECTED]>
Date: Thu, 20 Nov 2008 08:45:00 -0800
Subject: [PATCH] Convert iblinkinfo.pl to C and use new ibnetdisc library.

Signed-off-by: Ira Weiny <[EMAIL PROTECTED]>
---
 infiniband-diags/Makefile.am           |    9 +-
 infiniband-diags/configure.in          |    2 +
 infiniband-diags/scripts/iblinkinfo.pl |  327 --------------------------
 infiniband-diags/src/iblinkinfo.c      |  393 ++++++++++++++++++++++++++++++++
 4 files changed, 402 insertions(+), 329 deletions(-)
 delete mode 100755 infiniband-diags/scripts/iblinkinfo.pl
 create mode 100644 infiniband-diags/src/iblinkinfo.c

diff --git a/infiniband-diags/Makefile.am b/infiniband-diags/Makefile.am
index c22ba5e..8f26749 100644
--- a/infiniband-diags/Makefile.am
+++ b/infiniband-diags/Makefile.am
@@ -10,7 +10,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.pl
 
 if ENABLE_TEST_UTILS
 sbin_PROGRAMS += src/ibsendtrap src/mcm_rereg_test
@@ -27,7 +27,7 @@ sbin_SCRIPTS = scripts/ibcheckerrs scripts/ibchecknet 
scripts/ibchecknode \
               scripts/dump_lfts.sh scripts/dump_mfts.sh \
               scripts/set_nodedesc.sh \
               scripts/ibqueryerrors.pl scripts/ibswportwatch.pl \
-              scripts/iblinkinfo.pl scripts/ibprintswitch.pl \
+              scripts/ibprintswitch.pl \
               scripts/ibprintca.pl scripts/ibprintrt.pl \
               scripts/ibfindnodesusing.pl scripts/ibidsverify.pl \
               scripts/check_lft_balance.pl
@@ -39,6 +39,11 @@ src_ibnetdiscover_SOURCES = src/ibnetdiscover.c 
src/grouping.c src/ibdiag_common
 src_ibnetdiscover_CFLAGS = -Wall $(DBGFLAGS)
 src_ibnetdiscover_LDFLAGS = -Wl,--rpath -Wl,$(libdir)
 
+src_iblinkinfo_pl_SOURCES = src/iblinkinfo.c
+src_iblinkinfo_pl_CFLAGS = -Wall $(DBGFLAGS)
+src_iblinkinfo_pl_LDFLAGS = -Wl,--rpath -Wl,$(libdir) \
+                       -libcommon -libnetdisc
+
 src_ibping_SOURCES = src/ibping.c src/ibdiag_common.c
 src_ibping_CFLAGS = -Wall $(DBGFLAGS)
 
diff --git a/infiniband-diags/configure.in b/infiniband-diags/configure.in
index d227219..46021d6 100644
--- a/infiniband-diags/configure.in
+++ b/infiniband-diags/configure.in
@@ -46,6 +46,8 @@ AC_CHECK_LIB(osmvendor, osmv_query_sa, [],
        AC_MSG_ERROR([osmv_query_sa() not found. diags require libosmvendor.]), 
[-lopensm])
 AC_CHECK_LIB(opensm, osm_log_init_v2, [],
        AC_MSG_ERROR([osm_log_init_v2() not found. diags require libopensm.]))
+AC_CHECK_LIB(ibnetdisc, ibnd_discover_fabric, [],
+       AC_MSG_ERROR([ibnd_discover_fabric() not found. diags require 
libibnetdisc.]))
 fi
 
 dnl Checks for header files.
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 <[EMAIL PROTECTED]>.
-#
-# 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/src/iblinkinfo.c 
b/infiniband-diags/src/iblinkinfo.c
new file mode 100644
index 0000000..1d503bb
--- /dev/null
+++ b/infiniband-diags/src/iblinkinfo.c
@@ -0,0 +1,393 @@
+/*
+ * 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)
+{
+       int max_speed = 0;
+
+       int max_width = get_max(port->info.link_width_supported
+                               & port->remoteport->info.link_width_supported);
+       if ((max_width & port->info.link_width_active) == 0) {
+               // we are not at the max supported width
+               // print what we could be at.
+               snprintf(width_msg, msg_size, "Could be %s",
+                       ibnd_linkwidth_str(max_width));
+       }
+
+       max_speed = get_max(port->info.link_speed_supported
+                               & port->remoteport->info.link_speed_supported);
+       if ((max_speed & port->info.link_speed_active) == 0) {
+               // we are not at the max supported speed
+               // print what we could be at.
+               snprintf(speed_msg, msg_size, "Could be %s",
+                       ibnd_linkspeed_str(max_speed));
+       }
+}
+
+void
+print_port(ibnd_node_t *node, ibnd_port_t *port)
+{
+       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];
+
+       if (!port)
+               return;
+
+       remote_guid_str[0] = '\0';
+       remote_str[0] = '\0';
+       link_str[0] = '\0';
+       width_msg[0] = '\0';
+       speed_msg[0] = '\0';
+
+       if (port->remoteport) {
+               char  remote_name_buf[256];
+               strncpy(remote_name_buf, port->remoteport->node->nodedesc, 256);
+
+               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%016lx ",
+                                       port->remoteport->guid);
+                       } else {
+                               snprintf(remote_guid_str, 256,
+                                       "0x%016lx ",
+                                       port->remoteport->node->info.nodeguid);
+                       }
+               }
+
+               snprintf(remote_str, 256,
+                       "%s%6d %4d[%2s] \"%s\" (%s %s)\n",
+                       remote_guid_str,
+                       port->remoteport->info.lid ?
+                               port->remoteport->info.lid :
+                               port->remoteport->node->smalid,
+                       port->remoteport->portnum,
+                       ext_port_str,
+                       remap_node_name(node_name_map,
+                               port->remoteport->node->info.nodeguid,
+                               remote_name_buf),
+                       width_msg,
+                       speed_msg
+                       );
+       } else {
+               snprintf(remote_str, 256,
+                       "%6s %4s[%2s] \"\" ( )\n", "", "", "");
+       }
+
+       if (add_sw_settings) {
+               snprintf(link_str, 256,
+                       "(%3s %s %6s/%8s) (HOQ:%d VL_Stall:%d)",
+                       ibnd_linkwidth_str(port->info.link_width_active),
+                       ibnd_linkspeed_str(port->info.link_speed_active),
+                       ibnd_linkstate_str(port->info.link_state),
+                       ibnd_physstate_str(port->info.phys_state),
+                       port->info.hoq_lifetime,
+                       port->info.vl_stall_count
+                       );
+       } else {
+               snprintf(link_str, 256,
+                       "(%3s %s %6s/%8s)",
+                       ibnd_linkwidth_str(port->info.link_width_active),
+                       ibnd_linkspeed_str(port->info.link_speed_active),
+                       ibnd_linkstate_str(port->info.link_state),
+                       ibnd_physstate_str(port->info.phys_state)
+                       );
+       }
+
+       if (port->ext_portnum)
+               snprintf(ext_port_str, 256, "%d", port->ext_portnum);
+       else
+               ext_port_str[0] = '\0';
+
+       if (line_mode) {
+               char  name_buf[256];
+               strncpy(name_buf, node->nodedesc, 256);
+               printf("0x%016lx \"%30s\" %6d %4d[%2s] ==%s==>  %s",
+                       node->info.nodeguid,
+                       remap_node_name(node_name_map,
+                               node->info.nodeguid,
+                               name_buf),
+                       node->smalid, port->portnum,
+                       ext_port_str,
+                       link_str,
+                       remote_str
+                       );
+       } else {
+               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  name_buf[256];
+               strncpy(name_buf, node->nodedesc, 256);
+               printf("Switch 0x%016lx %s:\n",
+                       node->info.nodeguid,
+                       remap_node_name(node_name_map,
+                               node->info.nodeguid,
+                               name_buf));
+       }
+
+       for (i = 1; i <= node->info.numports; i++) {
+               ibnd_port_t *port = node->ports[i];
+               if (!port)
+                       continue;
+               if (!down_links_only || port->info.link_state == 
IBND_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"
+               ,
+                       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:";
+       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},
+               { "from", 1, 0, 'f'},
+               { }
+       };
+
+       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;
+               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, IBND_SWITCH_NODE, 
NULL);
+       }
+
+       ibnd_destroy_fabric(fabric);
+
+       close_node_name_map(node_name_map);
+       exit(0);
+}
-- 
1.5.4.5

_______________________________________________
general mailing list
[email protected]
http://lists.openfabrics.org/cgi-bin/mailman/listinfo/general

To unsubscribe, please visit http://openib.org/mailman/listinfo/openib-general

Reply via email to