1219820Sjeff/*
2219820Sjeff * Copyright (c) 2004-2008 Voltaire Inc.  All rights reserved.
3219820Sjeff *
4219820Sjeff * This software is available to you under a choice of one of two
5219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
6219820Sjeff * General Public License (GPL) Version 2, available from the file
7219820Sjeff * COPYING in the main directory of this source tree, or the
8219820Sjeff * OpenIB.org BSD license below:
9219820Sjeff *
10219820Sjeff *     Redistribution and use in source and binary forms, with or
11219820Sjeff *     without modification, are permitted provided that the following
12219820Sjeff *     conditions are met:
13219820Sjeff *
14219820Sjeff *      - Redistributions of source code must retain the above
15219820Sjeff *        copyright notice, this list of conditions and the following
16219820Sjeff *        disclaimer.
17219820Sjeff *
18219820Sjeff *      - Redistributions in binary form must reproduce the above
19219820Sjeff *        copyright notice, this list of conditions and the following
20219820Sjeff *        disclaimer in the documentation and/or other materials
21219820Sjeff *        provided with the distribution.
22219820Sjeff *
23219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30219820Sjeff * SOFTWARE.
31219820Sjeff *
32219820Sjeff */
33219820Sjeff
34219820Sjeff#if HAVE_CONFIG_H
35219820Sjeff#  include <config.h>
36219820Sjeff#endif /* HAVE_CONFIG_H */
37219820Sjeff
38219820Sjeff#include <stdio.h>
39219820Sjeff#include <stdlib.h>
40219820Sjeff#include <unistd.h>
41219820Sjeff#include <stdarg.h>
42219820Sjeff#include <getopt.h>
43219820Sjeff#include <arpa/inet.h>
44219820Sjeff
45219820Sjeff#include <infiniband/common.h>
46219820Sjeff#include <infiniband/umad.h>
47219820Sjeff#include <infiniband/mad.h>
48219820Sjeff
49219820Sjeff#include <sys/socket.h>
50219820Sjeff
51219820Sjeff#include "ibdiag_common.h"
52219820Sjeff
53219820Sjeffchar *argv0 = "ibaddr";
54219820Sjeff
55219820Sjeffstatic int
56219820Sjeffib_resolve_addr(ib_portid_t *portid, int portnum, int show_lid, int show_gid)
57219820Sjeff{
58219820Sjeff	char   gid_str[INET6_ADDRSTRLEN];
59219820Sjeff	uint8_t portinfo[64];
60219820Sjeff	uint8_t nodeinfo[64];
61219820Sjeff	uint64_t guid, prefix;
62219820Sjeff	ibmad_gid_t gid;
63219820Sjeff	int lmc;
64219820Sjeff
65219820Sjeff	if (!smp_query(nodeinfo, portid, IB_ATTR_NODE_INFO, 0, 0))
66219820Sjeff		return -1;
67219820Sjeff
68219820Sjeff	if (!smp_query(portinfo, portid, IB_ATTR_PORT_INFO, portnum, 0))
69219820Sjeff		return -1;
70219820Sjeff
71219820Sjeff	mad_decode_field(portinfo, IB_PORT_LID_F, &portid->lid);
72219820Sjeff	mad_decode_field(portinfo, IB_PORT_GID_PREFIX_F, &prefix);
73219820Sjeff	mad_decode_field(portinfo, IB_PORT_LMC_F, &lmc);
74219820Sjeff	mad_decode_field(nodeinfo, IB_NODE_PORT_GUID_F, &guid);
75219820Sjeff
76219820Sjeff	mad_encode_field(gid, IB_GID_PREFIX_F, &prefix);
77219820Sjeff	mad_encode_field(gid, IB_GID_GUID_F, &guid);
78219820Sjeff
79219820Sjeff	if (show_gid) {
80219820Sjeff		printf("GID %s ", inet_ntop(AF_INET6, gid, gid_str,
81219820Sjeff			sizeof gid_str));
82219820Sjeff	}
83219820Sjeff
84219820Sjeff	if (show_lid > 0)
85219820Sjeff		printf("LID start 0x%x end 0x%x", portid->lid, portid->lid + (1 << lmc) - 1);
86219820Sjeff	else if (show_lid < 0)
87219820Sjeff		printf("LID start %d end %d", portid->lid, portid->lid + (1 << lmc) - 1);
88219820Sjeff	printf("\n");
89219820Sjeff	return 0;
90219820Sjeff}
91219820Sjeff
92219820Sjeffstatic void
93219820Sjeffusage(void)
94219820Sjeff{
95219820Sjeff	char *basename;
96219820Sjeff
97219820Sjeff	if (!(basename = strrchr(argv0, '/')))
98219820Sjeff		basename = argv0;
99219820Sjeff	else
100219820Sjeff		basename++;
101219820Sjeff
102219820Sjeff	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 "
103219820Sjeff			"-t(imeout) timeout_ms -V(ersion) -h(elp)] [<lid|dr_path|guid>]\n",
104219820Sjeff			basename);
105219820Sjeff	fprintf(stderr, "\tExamples:\n");
106219820Sjeff	fprintf(stderr, "\t\t%s\t\t\t# local port's address\n", basename);
107219820Sjeff	fprintf(stderr, "\t\t%s 32\t\t# show lid range and gid of lid 32\n", basename);
108219820Sjeff	fprintf(stderr, "\t\t%s -G 0x8f1040023\t# same but using guid address\n", basename);
109219820Sjeff	fprintf(stderr, "\t\t%s -l 32\t\t# show lid range only\n", basename);
110219820Sjeff	fprintf(stderr, "\t\t%s -L 32\t\t# show decimal lid range only\n", basename);
111219820Sjeff	fprintf(stderr, "\t\t%s -g 32\t\t# show gid address only\n", basename);
112219820Sjeff	exit(-1);
113219820Sjeff}
114219820Sjeff
115219820Sjeffint
116219820Sjeffmain(int argc, char **argv)
117219820Sjeff{
118219820Sjeff	int mgmt_classes[3] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS};
119219820Sjeff	ib_portid_t *sm_id = 0, sm_portid = {0};
120219820Sjeff	ib_portid_t portid = {0};
121219820Sjeff	extern int ibdebug;
122219820Sjeff	int dest_type = IB_DEST_LID;
123219820Sjeff	int timeout = 0;	/* use default */
124219820Sjeff	int show_lid = 0, show_gid = 0;
125219820Sjeff	int port = 0;
126219820Sjeff	char *ca = 0;
127219820Sjeff	int ca_port = 0;
128219820Sjeff
129219820Sjeff	static char const str_opts[] = "C:P:t:s:dDGglLVhu";
130219820Sjeff	static const struct option long_opts[] = {
131219820Sjeff		{ "C", 1, 0, 'C'},
132219820Sjeff		{ "P", 1, 0, 'P'},
133219820Sjeff		{ "debug", 0, 0, 'd'},
134219820Sjeff		{ "Direct", 0, 0, 'D'},
135219820Sjeff		{ "Guid", 0, 0, 'G'},
136219820Sjeff		{ "gid_show", 0, 0, 'g'},
137219820Sjeff		{ "lid_show", 0, 0, 'l'},
138219820Sjeff		{ "Lid_show", 0, 0, 'L'},
139219820Sjeff		{ "timeout", 1, 0, 't'},
140219820Sjeff		{ "sm_port", 1, 0, 's'},
141219820Sjeff		{ "Version", 0, 0, 'V'},
142219820Sjeff		{ "help", 0, 0, 'h'},
143219820Sjeff		{ "usage", 0, 0, 'u'},
144219820Sjeff		{ }
145219820Sjeff	};
146219820Sjeff
147219820Sjeff	argv0 = argv[0];
148219820Sjeff
149219820Sjeff	while (1) {
150219820Sjeff		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
151219820Sjeff		if ( ch == -1 )
152219820Sjeff			break;
153219820Sjeff		switch(ch) {
154219820Sjeff		case 'C':
155219820Sjeff			ca = optarg;
156219820Sjeff			break;
157219820Sjeff		case 'P':
158219820Sjeff			ca_port = strtoul(optarg, 0, 0);
159219820Sjeff			break;
160219820Sjeff		case 'd':
161219820Sjeff			ibdebug++;
162219820Sjeff			break;
163219820Sjeff		case 'D':
164219820Sjeff			dest_type = IB_DEST_DRPATH;
165219820Sjeff			break;
166219820Sjeff		case 'g':
167219820Sjeff			show_gid++;
168219820Sjeff			break;
169219820Sjeff		case 'G':
170219820Sjeff			dest_type = IB_DEST_GUID;
171219820Sjeff			break;
172219820Sjeff		case 'l':
173219820Sjeff			show_lid++;
174219820Sjeff			break;
175219820Sjeff		case 'L':
176219820Sjeff			show_lid = -100;
177219820Sjeff			break;
178219820Sjeff		case 's':
179219820Sjeff			if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0)
180219820Sjeff				IBERROR("can't resolve SM destination port %s", optarg);
181219820Sjeff			sm_id = &sm_portid;
182219820Sjeff			break;
183219820Sjeff		case 't':
184219820Sjeff			timeout = strtoul(optarg, 0, 0);
185219820Sjeff			madrpc_set_timeout(timeout);
186219820Sjeff			break;
187219820Sjeff		case 'V':
188219820Sjeff			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
189219820Sjeff			exit(-1);
190219820Sjeff		default:
191219820Sjeff			usage();
192219820Sjeff			break;
193219820Sjeff		}
194219820Sjeff	}
195219820Sjeff	argc -= optind;
196219820Sjeff	argv += optind;
197219820Sjeff
198219820Sjeff	if (argc > 1)
199219820Sjeff		port = strtoul(argv[1], 0, 0);
200219820Sjeff
201219820Sjeff	if (!show_lid && !show_gid)
202219820Sjeff		show_lid = show_gid = 1;
203219820Sjeff
204219820Sjeff	madrpc_init(ca, ca_port, mgmt_classes, 3);
205219820Sjeff
206219820Sjeff	if (argc) {
207219820Sjeff		if (ib_resolve_portid_str(&portid, argv[0], dest_type, sm_id) < 0)
208219820Sjeff			IBERROR("can't resolve destination port %s", argv[0]);
209219820Sjeff	} else {
210219820Sjeff		if (ib_resolve_self(&portid, &port, 0) < 0)
211219820Sjeff			IBERROR("can't resolve self port %s", argv[0]);
212219820Sjeff	}
213219820Sjeff
214219820Sjeff	if (ib_resolve_addr(&portid, port, show_lid, show_gid) < 0)
215219820Sjeff		IBERROR("can't resolve requested address");
216219820Sjeff	exit(0);
217219820Sjeff}
218