1235783Skib/*
2235783Skib * Copyright (c) 2004-2008 Voltaire Inc.  All rights reserved.
3235783Skib *
4235783Skib * This software is available to you under a choice of one of two
5235783Skib * licenses.  You may choose to be licensed under the terms of the GNU
6235783Skib * General Public License (GPL) Version 2, available from the file
7235783Skib * COPYING in the main directory of this source tree, or the
8235783Skib * OpenIB.org BSD license below:
9235783Skib *
10235783Skib *     Redistribution and use in source and binary forms, with or
11235783Skib *     without modification, are permitted provided that the following
12235783Skib *     conditions are met:
13235783Skib *
14235783Skib *      - Redistributions of source code must retain the above
15235783Skib *        copyright notice, this list of conditions and the following
16235783Skib *        disclaimer.
17235783Skib *
18235783Skib *      - Redistributions in binary form must reproduce the above
19235783Skib *        copyright notice, this list of conditions and the following
20235783Skib *        disclaimer in the documentation and/or other materials
21235783Skib *        provided with the distribution.
22235783Skib *
23235783Skib * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24235783Skib * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25235783Skib * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26235783Skib * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27235783Skib * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28235783Skib * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29235783Skib * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30235783Skib * SOFTWARE.
31235783Skib *
32235783Skib */
33235783Skib
34235783Skib#if HAVE_CONFIG_H
35235783Skib#  include <config.h>
36235783Skib#endif /* HAVE_CONFIG_H */
37235783Skib
38235783Skib#include <stdio.h>
39235783Skib#include <stdlib.h>
40235783Skib#include <unistd.h>
41235783Skib#include <stdarg.h>
42235783Skib#include <getopt.h>
43235783Skib#include <arpa/inet.h>
44235783Skib
45235783Skib#include <infiniband/common.h>
46235783Skib#include <infiniband/umad.h>
47235783Skib#include <infiniband/mad.h>
48235783Skib
49235783Skib#include <sys/socket.h>
50235783Skib
51235783Skib#include "ibdiag_common.h"
52235783Skib
53235783Skibchar *argv0 = "ibaddr";
54235783Skib
55235783Skibstatic int
56235783Skibib_resolve_addr(ib_portid_t *portid, int portnum, int show_lid, int show_gid)
57235783Skib{
58235783Skib	char   gid_str[INET6_ADDRSTRLEN];
59235783Skib	uint8_t portinfo[64];
60235783Skib	uint8_t nodeinfo[64];
61235783Skib	uint64_t guid, prefix;
62235783Skib	ibmad_gid_t gid;
63235783Skib	int lmc;
64235783Skib
65235783Skib	if (!smp_query(nodeinfo, portid, IB_ATTR_NODE_INFO, 0, 0))
66235783Skib		return -1;
67235783Skib
68235783Skib	if (!smp_query(portinfo, portid, IB_ATTR_PORT_INFO, portnum, 0))
69235783Skib		return -1;
70235783Skib
71235783Skib	mad_decode_field(portinfo, IB_PORT_LID_F, &portid->lid);
72235783Skib	mad_decode_field(portinfo, IB_PORT_GID_PREFIX_F, &prefix);
73235783Skib	mad_decode_field(portinfo, IB_PORT_LMC_F, &lmc);
74235783Skib	mad_decode_field(nodeinfo, IB_NODE_PORT_GUID_F, &guid);
75235783Skib
76235783Skib	mad_encode_field(gid, IB_GID_PREFIX_F, &prefix);
77235783Skib	mad_encode_field(gid, IB_GID_GUID_F, &guid);
78235783Skib
79235783Skib	if (show_gid) {
80235783Skib		printf("GID %s ", inet_ntop(AF_INET6, gid, gid_str,
81235783Skib			sizeof gid_str));
82235783Skib	}
83235783Skib
84235783Skib	if (show_lid > 0)
85235783Skib		printf("LID start 0x%x end 0x%x", portid->lid, portid->lid + (1 << lmc) - 1);
86235783Skib	else if (show_lid < 0)
87235783Skib		printf("LID start %d end %d", portid->lid, portid->lid + (1 << lmc) - 1);
88235783Skib	printf("\n");
89235783Skib	return 0;
90235783Skib}
91235783Skib
92235783Skibstatic void
93235783Skibusage(void)
94235783Skib{
95235783Skib	char *basename;
96235783Skib
97235783Skib	if (!(basename = strrchr(argv0, '/')))
98235783Skib		basename = argv0;
99235783Skib	else
100235783Skib		basename++;
101235783Skib
102235783Skib	fprintf(stderr, "Usage: %s [-d(ebug) -D(irect) -G(uid) -l(id_show) -g(id_show) -s(m_port) sm_lid -C ca_name -P ca_port "
103235783Skib			"-t(imeout) timeout_ms -V(ersion) -h(elp)] [<lid|dr_path|guid>]\n",
104235783Skib			basename);
105235783Skib	fprintf(stderr, "\tExamples:\n");
106235783Skib	fprintf(stderr, "\t\t%s\t\t\t# local port's address\n", basename);
107235783Skib	fprintf(stderr, "\t\t%s 32\t\t# show lid range and gid of lid 32\n", basename);
108235783Skib	fprintf(stderr, "\t\t%s -G 0x8f1040023\t# same but using guid address\n", basename);
109235783Skib	fprintf(stderr, "\t\t%s -l 32\t\t# show lid range only\n", basename);
110235783Skib	fprintf(stderr, "\t\t%s -L 32\t\t# show decimal lid range only\n", basename);
111235783Skib	fprintf(stderr, "\t\t%s -g 32\t\t# show gid address only\n", basename);
112235783Skib	exit(-1);
113235783Skib}
114235783Skib
115235783Skibint
116235783Skibmain(int argc, char **argv)
117235783Skib{
118235783Skib	int mgmt_classes[3] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS};
119235783Skib	ib_portid_t *sm_id = 0, sm_portid = {0};
120235783Skib	ib_portid_t portid = {0};
121235783Skib	extern int ibdebug;
122235783Skib	int dest_type = IB_DEST_LID;
123235783Skib	int timeout = 0;	/* use default */
124235783Skib	int show_lid = 0, show_gid = 0;
125235783Skib	int port = 0;
126235783Skib	char *ca = 0;
127235783Skib	int ca_port = 0;
128235783Skib
129235783Skib	static char const str_opts[] = "C:P:t:s:dDGglLVhu";
130235783Skib	static const struct option long_opts[] = {
131235783Skib		{ "C", 1, 0, 'C'},
132235783Skib		{ "P", 1, 0, 'P'},
133235783Skib		{ "debug", 0, 0, 'd'},
134235783Skib		{ "Direct", 0, 0, 'D'},
135235783Skib		{ "Guid", 0, 0, 'G'},
136235783Skib		{ "gid_show", 0, 0, 'g'},
137235783Skib		{ "lid_show", 0, 0, 'l'},
138235783Skib		{ "Lid_show", 0, 0, 'L'},
139235783Skib		{ "timeout", 1, 0, 't'},
140235783Skib		{ "sm_port", 1, 0, 's'},
141235783Skib		{ "Version", 0, 0, 'V'},
142		{ "help", 0, 0, 'h'},
143		{ "usage", 0, 0, 'u'},
144		{ }
145	};
146
147	argv0 = argv[0];
148
149	while (1) {
150		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
151		if ( ch == -1 )
152			break;
153		switch(ch) {
154		case 'C':
155			ca = optarg;
156			break;
157		case 'P':
158			ca_port = strtoul(optarg, 0, 0);
159			break;
160		case 'd':
161			ibdebug++;
162			break;
163		case 'D':
164			dest_type = IB_DEST_DRPATH;
165			break;
166		case 'g':
167			show_gid++;
168			break;
169		case 'G':
170			dest_type = IB_DEST_GUID;
171			break;
172		case 'l':
173			show_lid++;
174			break;
175		case 'L':
176			show_lid = -100;
177			break;
178		case 's':
179			if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0)
180				IBERROR("can't resolve SM destination port %s", optarg);
181			sm_id = &sm_portid;
182			break;
183		case 't':
184			timeout = strtoul(optarg, 0, 0);
185			madrpc_set_timeout(timeout);
186			break;
187		case 'V':
188			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
189			exit(-1);
190		default:
191			usage();
192			break;
193		}
194	}
195	argc -= optind;
196	argv += optind;
197
198	if (argc > 1)
199		port = strtoul(argv[1], 0, 0);
200
201	if (!show_lid && !show_gid)
202		show_lid = show_gid = 1;
203
204	madrpc_init(ca, ca_port, mgmt_classes, 3);
205
206	if (argc) {
207		if (ib_resolve_portid_str(&portid, argv[0], dest_type, sm_id) < 0)
208			IBERROR("can't resolve destination port %s", argv[0]);
209	} else {
210		if (ib_resolve_self(&portid, &port, 0) < 0)
211			IBERROR("can't resolve self port %s", argv[0]);
212	}
213
214	if (ib_resolve_addr(&portid, port, show_lid, show_gid) < 0)
215		IBERROR("can't resolve requested address");
216	exit(0);
217}
218