xref: /freebsd/contrib/ofed/infiniband-diags/src/ibaddr.c (revision 7877fdebeeb35fad1cbbafce22598b1bdf97c786)
1 /*
2  * Copyright (c) 2004-2009 Voltaire Inc.  All rights reserved.
3  * Copyright (c) 2011 Mellanox Technologies LTD.  All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  *
33  */
34 
35 #if HAVE_CONFIG_H
36 #  include <config.h>
37 #endif				/* HAVE_CONFIG_H */
38 
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <getopt.h>
43 #include <arpa/inet.h>
44 
45 #include <infiniband/umad.h>
46 #include <infiniband/mad.h>
47 
48 #include "ibdiag_common.h"
49 
50 struct ibmad_port *srcport;
51 
52 static int ib_resolve_addr(ib_portid_t * portid, int portnum, int show_lid,
53 			   int show_gid)
54 {
55 	char gid_str[INET6_ADDRSTRLEN];
56 	uint8_t portinfo[IB_SMP_DATA_SIZE] = { 0 };
57 	uint8_t nodeinfo[IB_SMP_DATA_SIZE] = { 0 };
58 	uint64_t guid, prefix;
59 	ibmad_gid_t gid;
60 	int lmc;
61 
62 	if (!smp_query_via(nodeinfo, portid, IB_ATTR_NODE_INFO, 0, 0, srcport))
63 		return -1;
64 
65 	if (!smp_query_via(portinfo, portid, IB_ATTR_PORT_INFO, portnum, 0,
66 			   srcport))
67 		return -1;
68 
69 	mad_decode_field(portinfo, IB_PORT_LID_F, &portid->lid);
70 	mad_decode_field(portinfo, IB_PORT_GID_PREFIX_F, &prefix);
71 	mad_decode_field(portinfo, IB_PORT_LMC_F, &lmc);
72 	mad_decode_field(nodeinfo, IB_NODE_PORT_GUID_F, &guid);
73 
74 	mad_encode_field(gid, IB_GID_PREFIX_F, &prefix);
75 	mad_encode_field(gid, IB_GID_GUID_F, &guid);
76 
77 	if (show_gid) {
78 		printf("GID %s ", inet_ntop(AF_INET6, gid, gid_str,
79 					    sizeof gid_str));
80 	}
81 
82 	if (show_lid > 0)
83 		printf("LID start 0x%x end 0x%x", portid->lid,
84 		       portid->lid + (1 << lmc) - 1);
85 	else if (show_lid < 0)
86 		printf("LID start %u end %u", portid->lid,
87 		       portid->lid + (1 << lmc) - 1);
88 	printf("\n");
89 	return 0;
90 }
91 
92 static int show_lid, show_gid;
93 
94 static int process_opt(void *context, int ch, char *optarg)
95 {
96 	switch (ch) {
97 	case 'g':
98 		show_gid = 1;
99 		break;
100 	case 'l':
101 		show_lid++;
102 		break;
103 	case 'L':
104 		show_lid = -100;
105 		break;
106 	default:
107 		return -1;
108 	}
109 	return 0;
110 }
111 
112 int main(int argc, char **argv)
113 {
114 	int mgmt_classes[3] =
115 	    { IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS };
116 	ib_portid_t portid = { 0 };
117 	int port = 0;
118 
119 	const struct ibdiag_opt opts[] = {
120 		{"gid_show", 'g', 0, NULL, "show gid address only"},
121 		{"lid_show", 'l', 0, NULL, "show lid range only"},
122 		{"Lid_show", 'L', 0, NULL, "show lid range (in decimal) only"},
123 		{0}
124 	};
125 	char usage_args[] = "[<lid|dr_path|guid>]";
126 	const char *usage_examples[] = {
127 		"\t\t# local port's address",
128 		"32\t\t# show lid range and gid of lid 32",
129 		"-G 0x8f1040023\t# same but using guid address",
130 		"-l 32\t\t# show lid range only",
131 		"-L 32\t\t# show decimal lid range only",
132 		"-g 32\t\t# show gid address only",
133 		NULL
134 	};
135 
136 	ibdiag_process_opts(argc, argv, NULL, "KL", opts, process_opt,
137 			    usage_args, usage_examples);
138 
139 	argc -= optind;
140 	argv += optind;
141 
142 	if (argc > 1)
143 		port = strtoul(argv[1], 0, 0);
144 
145 	if (!show_lid && !show_gid)
146 		show_lid = show_gid = 1;
147 
148 	srcport = mad_rpc_open_port(ibd_ca, ibd_ca_port, mgmt_classes, 3);
149 	if (!srcport)
150 		IBEXIT("Failed to open '%s' port '%d'", ibd_ca, ibd_ca_port);
151 
152 	smp_mkey_set(srcport, ibd_mkey);
153 
154 	if (argc) {
155 		if (resolve_portid_str(ibd_ca, ibd_ca_port, &portid, argv[0],
156 				       ibd_dest_type, ibd_sm_id, srcport) < 0)
157 			IBEXIT("can't resolve destination port %s", argv[0]);
158 	} else {
159 		if (resolve_self(ibd_ca, ibd_ca_port, &portid, &port, NULL) < 0)
160 			IBEXIT("can't resolve self port %s", argv[0]);
161 	}
162 
163 	if (ib_resolve_addr(&portid, port, show_lid, show_gid) < 0)
164 		IBEXIT("can't resolve requested address");
165 
166 	mad_rpc_close_port(srcport);
167 	exit(0);
168 }
169