1/*
2 * Copyright (c) 2004-2008 Voltaire Inc.  All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses.  You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 *     Redistribution and use in source and binary forms, with or
11 *     without modification, are permitted provided that the following
12 *     conditions are met:
13 *
14 *      - Redistributions of source code must retain the above
15 *        copyright notice, this list of conditions and the following
16 *        disclaimer.
17 *
18 *      - Redistributions in binary form must reproduce the above
19 *        copyright notice, this list of conditions and the following
20 *        disclaimer in the documentation and/or other materials
21 *        provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 */
33
34#if HAVE_CONFIG_H
35#  include <config.h>
36#endif /* HAVE_CONFIG_H */
37
38#include <stdio.h>
39#include <stdlib.h>
40#include <unistd.h>
41#include <stdarg.h>
42#include <getopt.h>
43#include <arpa/inet.h>
44
45#include <infiniband/common.h>
46#include <infiniband/umad.h>
47#include <infiniband/mad.h>
48
49#include <sys/socket.h>
50
51#include "ibdiag_common.h"
52
53char *argv0 = "ibaddr";
54
55static int
56ib_resolve_addr(ib_portid_t *portid, int portnum, int show_lid, int show_gid)
57{
58	char   gid_str[INET6_ADDRSTRLEN];
59	uint8_t portinfo[64];
60	uint8_t nodeinfo[64];
61	uint64_t guid, prefix;
62	ibmad_gid_t gid;
63	int lmc;
64
65	if (!smp_query(nodeinfo, portid, IB_ATTR_NODE_INFO, 0, 0))
66		return -1;
67
68	if (!smp_query(portinfo, portid, IB_ATTR_PORT_INFO, portnum, 0))
69		return -1;
70
71	mad_decode_field(portinfo, IB_PORT_LID_F, &portid->lid);
72	mad_decode_field(portinfo, IB_PORT_GID_PREFIX_F, &prefix);
73	mad_decode_field(portinfo, IB_PORT_LMC_F, &lmc);
74	mad_decode_field(nodeinfo, IB_NODE_PORT_GUID_F, &guid);
75
76	mad_encode_field(gid, IB_GID_PREFIX_F, &prefix);
77	mad_encode_field(gid, IB_GID_GUID_F, &guid);
78
79	if (show_gid) {
80		printf("GID %s ", inet_ntop(AF_INET6, gid, gid_str,
81			sizeof gid_str));
82	}
83
84	if (show_lid > 0)
85		printf("LID start 0x%x end 0x%x", portid->lid, portid->lid + (1 << lmc) - 1);
86	else if (show_lid < 0)
87		printf("LID start %d end %d", portid->lid, portid->lid + (1 << lmc) - 1);
88	printf("\n");
89	return 0;
90}
91
92static void
93usage(void)
94{
95	char *basename;
96
97	if (!(basename = strrchr(argv0, '/')))
98		basename = argv0;
99	else
100		basename++;
101
102	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 "
103			"-t(imeout) timeout_ms -V(ersion) -h(elp)] [<lid|dr_path|guid>]\n",
104			basename);
105	fprintf(stderr, "\tExamples:\n");
106	fprintf(stderr, "\t\t%s\t\t\t# local port's address\n", basename);
107	fprintf(stderr, "\t\t%s 32\t\t# show lid range and gid of lid 32\n", basename);
108	fprintf(stderr, "\t\t%s -G 0x8f1040023\t# same but using guid address\n", basename);
109	fprintf(stderr, "\t\t%s -l 32\t\t# show lid range only\n", basename);
110	fprintf(stderr, "\t\t%s -L 32\t\t# show decimal lid range only\n", basename);
111	fprintf(stderr, "\t\t%s -g 32\t\t# show gid address only\n", basename);
112	exit(-1);
113}
114
115int
116main(int argc, char **argv)
117{
118	int mgmt_classes[3] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS};
119	ib_portid_t *sm_id = 0, sm_portid = {0};
120	ib_portid_t portid = {0};
121	extern int ibdebug;
122	int dest_type = IB_DEST_LID;
123	int timeout = 0;	/* use default */
124	int show_lid = 0, show_gid = 0;
125	int port = 0;
126	char *ca = 0;
127	int ca_port = 0;
128
129	static char const str_opts[] = "C:P:t:s:dDGglLVhu";
130	static const struct option long_opts[] = {
131		{ "C", 1, 0, 'C'},
132		{ "P", 1, 0, 'P'},
133		{ "debug", 0, 0, 'd'},
134		{ "Direct", 0, 0, 'D'},
135		{ "Guid", 0, 0, 'G'},
136		{ "gid_show", 0, 0, 'g'},
137		{ "lid_show", 0, 0, 'l'},
138		{ "Lid_show", 0, 0, 'L'},
139		{ "timeout", 1, 0, 't'},
140		{ "sm_port", 1, 0, 's'},
141		{ "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