1219820Sjeff#!/usr/bin/perl
2219820Sjeff#
3219820Sjeff# Copyright (c) 2006 The Regents of the University of California.
4219820Sjeff# Copyright (c) 2007-2008 Voltaire, Inc. All rights reserved.
5219820Sjeff#
6219820Sjeff# Produced at Lawrence Livermore National Laboratory.
7219820Sjeff# Written by Ira Weiny <weiny2@llnl.gov>.
8219820Sjeff#
9219820Sjeff# This software is available to you under a choice of one of two
10219820Sjeff# licenses.  You may choose to be licensed under the terms of the GNU
11219820Sjeff# General Public License (GPL) Version 2, available from the file
12219820Sjeff# COPYING in the main directory of this source tree, or the
13219820Sjeff# OpenIB.org BSD license below:
14219820Sjeff#
15219820Sjeff#     Redistribution and use in source and binary forms, with or
16219820Sjeff#     without modification, are permitted provided that the following
17219820Sjeff#     conditions are met:
18219820Sjeff#
19219820Sjeff#      - Redistributions of source code must retain the above
20219820Sjeff#        copyright notice, this list of conditions and the following
21219820Sjeff#        disclaimer.
22219820Sjeff#
23219820Sjeff#      - Redistributions in binary form must reproduce the above
24219820Sjeff#        copyright notice, this list of conditions and the following
25219820Sjeff#        disclaimer in the documentation and/or other materials
26219820Sjeff#        provided with the distribution.
27219820Sjeff#
28219820Sjeff# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29219820Sjeff# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30219820Sjeff# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31219820Sjeff# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
32219820Sjeff# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
33219820Sjeff# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34219820Sjeff# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35219820Sjeff# SOFTWARE.
36219820Sjeff#
37219820Sjeff
38219820Sjeffuse strict;
39219820Sjeff
40219820Sjeffuse Getopt::Std;
41219820Sjeffuse IBswcountlimits;
42219820Sjeff
43219820Sjeff# =========================================================================
44219820Sjeff#
45219820Sjeffsub usage_and_exit
46219820Sjeff{
47219820Sjeff	my $prog = $_[0];
48219820Sjeff	print "Usage: $prog [-R -l] [-G <ca_guid> | <node_name>]\n";
49219820Sjeff	print "   print only the ca specified from the ibnetdiscover output\n";
50219820Sjeff	print "   -R Recalculate ibnetdiscover information\n";
51219820Sjeff	print "   -l list cas\n";
52219820Sjeff	print "   -C <ca_name> use selected channel adaptor name for queries\n";
53219820Sjeff	print "   -P <ca_port> use selected channel adaptor port for queries\n";
54219820Sjeff	print "   -G node is specified with GUID\n";
55219820Sjeff	exit 2;
56219820Sjeff}
57219820Sjeff
58219820Sjeffmy $argv0          = `basename $0`;
59219820Sjeffmy $regenerate_map = undef;
60219820Sjeffmy $list_hcas      = undef;
61219820Sjeffmy $ca_name        = "";
62219820Sjeffmy $ca_port        = "";
63219820Sjeffmy $name_is_guid   = "no";
64219820Sjeffchomp $argv0;
65219820Sjeffif (!getopts("hRlC:P:G"))         { usage_and_exit $argv0; }
66219820Sjeffif (defined $Getopt::Std::opt_h) { usage_and_exit $argv0; }
67219820Sjeffif (defined $Getopt::Std::opt_R) { $regenerate_map = $Getopt::Std::opt_R; }
68219820Sjeffif (defined $Getopt::Std::opt_l) { $list_hcas      = $Getopt::Std::opt_l; }
69219820Sjeffif (defined $Getopt::Std::opt_C) { $ca_name        = $Getopt::Std::opt_C; }
70219820Sjeffif (defined $Getopt::Std::opt_P) { $ca_port        = $Getopt::Std::opt_P; }
71219820Sjeffif (defined $Getopt::Std::opt_G) { $name_is_guid   = "yes"; }
72219820Sjeff
73219820Sjeffmy $target_hca = $ARGV[0];
74219820Sjeff
75219820Sjeffif ($name_is_guid eq "yes") {
76219820Sjeff	$target_hca = format_guid($target_hca);
77219820Sjeff}
78219820Sjeff
79219820Sjeffmy $cache_file = get_cache_file($ca_name, $ca_port);
80219820Sjeff
81219820Sjeffif ($regenerate_map || !(-f "$cache_file")) {
82219820Sjeff	generate_ibnetdiscover_topology($ca_name, $ca_port);
83219820Sjeff}
84219820Sjeff
85219820Sjeffif ($list_hcas) {
86219820Sjeff	system("ibhosts $cache_file");
87219820Sjeff	exit 1;
88219820Sjeff}
89219820Sjeff
90219820Sjeffif ($target_hca eq "") {
91219820Sjeff	usage_and_exit $argv0;
92219820Sjeff}
93219820Sjeff
94219820Sjeff# =========================================================================
95219820Sjeff#
96219820Sjeffsub main
97219820Sjeff{
98219820Sjeff	my $found_hca = 0;
99219820Sjeff	open IBNET_TOPO, "<$cache_file" or die "Failed to open ibnet topology\n";
100219820Sjeff	my $in_hca = "no";
101219820Sjeff	my %ports  = undef;
102219820Sjeff	while (my $line = <IBNET_TOPO>) {
103219820Sjeff		if ($line =~ /^Ca.*\"H-(.*)\"\s+# (.*)/) {
104219820Sjeff			my $guid = $1;
105219820Sjeff			my $desc = $2;
106219820Sjeff			if ($in_hca eq "yes") {
107219820Sjeff				$in_hca = "no";
108219820Sjeff				foreach my $port (sort { $a <=> $b } (keys %ports)) {
109219820Sjeff					print $ports{$port};
110219820Sjeff				}
111219820Sjeff			}
112219820Sjeff			if ("0x$guid" eq $target_hca || $desc =~ /[\s\"]$target_hca[\s\"]/) {
113219820Sjeff				print $line;
114219820Sjeff				$in_hca    = "yes";
115219820Sjeff				$found_hca++;
116219820Sjeff			}
117219820Sjeff		}
118219820Sjeff		if ($line =~ /^Switch.*/ || $line =~ /^Rt.*/) { $in_hca = "no"; }
119219820Sjeff
120219820Sjeff		if ($line =~ /^\[(\d+)\].*/ && $in_hca eq "yes") {
121219820Sjeff			$ports{$1} = $line;
122219820Sjeff		}
123219820Sjeff
124219820Sjeff	}
125219820Sjeff	if ($found_hca == 0) {
126219820Sjeff		die "\"$target_hca\" not found\n" .
127219820Sjeff			"   Try running with the \"-R\" option.\n" .
128219820Sjeff			"   If still not found the node is probably down.\n";
129219820Sjeff	}
130219820Sjeff	if ($found_hca > 1) {
131219820Sjeff		print "\nWARNING: Found $found_hca CA's with the name \"$target_hca\"\n";
132219820Sjeff	}
133219820Sjeff	close IBNET_TOPO;
134219820Sjeff}
135219820Sjeffmain
136219820Sjeff
137