xref: /freebsd/contrib/ofed/infiniband-diags/src/smpquery.c (revision 13ea0450a9c8742119d36f3bf8f47accdce46e54)
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 <string.h>
43 #include <getopt.h>
44 #include <netinet/in.h>
45 
46 #define __STDC_FORMAT_MACROS
47 #include <inttypes.h>
48 
49 #include <infiniband/umad.h>
50 #include <infiniband/mad.h>
51 #include <complib/cl_nodenamemap.h>
52 
53 #include "ibdiag_common.h"
54 
55 struct ibmad_port *srcport;
56 
57 static op_fn_t node_desc, node_info, port_info, switch_info, pkey_table,
58     sl2vl_table, vlarb_table, guid_info, mlnx_ext_port_info, port_info_extended;
59 
60 static const match_rec_t match_tbl[] = {
61 	{"NodeInfo", "NI", node_info, 0, ""},
62 	{"NodeDesc", "ND", node_desc, 0, ""},
63 	{"PortInfo", "PI", port_info, 1, ""},
64 	{"PortInfoExtended", "PIE", port_info_extended, 1, ""},
65 	{"SwitchInfo", "SI", switch_info, 0, ""},
66 	{"PKeyTable", "PKeys", pkey_table, 1, ""},
67 	{"SL2VLTable", "SL2VL", sl2vl_table, 1, ""},
68 	{"VLArbitration", "VLArb", vlarb_table, 1, ""},
69 	{"GUIDInfo", "GI", guid_info, 0, ""},
70 	{"MlnxExtPortInfo", "MEPI", mlnx_ext_port_info, 1, ""},
71 	{0}
72 };
73 
74 static char *node_name_map_file = NULL;
75 static nn_map_t *node_name_map = NULL;
76 static int extended_speeds = 0;
77 
78 /*******************************************/
79 static char *node_desc(ib_portid_t * dest, char **argv, int argc)
80 {
81 	int node_type, l;
82 	uint64_t node_guid;
83 	char nd[IB_SMP_DATA_SIZE] = { 0 };
84 	uint8_t data[IB_SMP_DATA_SIZE] = { 0 };
85 	char dots[128];
86 	char *nodename = NULL;
87 
88 	if (!smp_query_via(data, dest, IB_ATTR_NODE_INFO, 0, 0, srcport))
89 		return "node info query failed";
90 
91 	mad_decode_field(data, IB_NODE_TYPE_F, &node_type);
92 	mad_decode_field(data, IB_NODE_GUID_F, &node_guid);
93 
94 	if (!smp_query_via(nd, dest, IB_ATTR_NODE_DESC, 0, 0, srcport))
95 		return "node desc query failed";
96 
97 	nodename = remap_node_name(node_name_map, node_guid, nd);
98 
99 	l = strlen(nodename);
100 	if (l < 32) {
101 		memset(dots, '.', 32 - l);
102 		dots[32 - l] = '\0';
103 	} else {
104 		dots[0] = '.';
105 		dots[1] = '\0';
106 	}
107 
108 	printf("Node Description:%s%s\n", dots, nodename);
109 	free(nodename);
110 	return 0;
111 }
112 
113 static char *node_info(ib_portid_t * dest, char **argv, int argc)
114 {
115 	char buf[2048];
116 	char data[IB_SMP_DATA_SIZE] = { 0 };
117 
118 	if (!smp_query_via(data, dest, IB_ATTR_NODE_INFO, 0, 0, srcport))
119 		return "node info query failed";
120 
121 	mad_dump_nodeinfo(buf, sizeof buf, data, sizeof data);
122 
123 	printf("# Node info: %s\n%s", portid2str(dest), buf);
124 	return 0;
125 }
126 
127 static char *port_info_extended(ib_portid_t * dest, char **argv, int argc)
128 {
129 	char buf[2048];
130 	uint8_t data[IB_SMP_DATA_SIZE] = { 0 };
131 	int portnum = 0;
132 
133 	if (argc > 0)
134 		portnum = strtol(argv[0], 0, 0);
135 
136 	if (!is_port_info_extended_supported(dest, portnum, srcport))
137 		return "port info extended not supported";
138 
139 	if (!smp_query_via(data, dest, IB_ATTR_PORT_INFO_EXT, portnum, 0,
140 			   srcport))
141 		return "port info extended query failed";
142 
143 	mad_dump_portinfo_ext(buf, sizeof buf, data, sizeof data);
144 	printf("# Port info Extended: %s port %d\n%s", portid2str(dest),
145 	       portnum, buf);
146 	return 0;
147 }
148 
149 static char *port_info(ib_portid_t * dest, char **argv, int argc)
150 {
151 	char data[IB_SMP_DATA_SIZE] = { 0 };
152 	int portnum = 0, orig_portnum;
153 
154 	if (argc > 0)
155 		portnum = strtol(argv[0], 0, 0);
156 	orig_portnum = portnum;
157 	if (extended_speeds)
158 		portnum |= 1 << 31;
159 
160 	if (!smp_query_via(data, dest, IB_ATTR_PORT_INFO, portnum, 0, srcport))
161 		return "port info query failed";
162 
163 	printf("# Port info: %s port %d\n", portid2str(dest), orig_portnum);
164 	dump_portinfo(data, 0);
165 	return 0;
166 }
167 
168 static char *mlnx_ext_port_info(ib_portid_t * dest, char **argv, int argc)
169 {
170 	char buf[2300];
171 	char data[IB_SMP_DATA_SIZE];
172 	int portnum = 0;
173 
174 	if (argc > 0)
175 		portnum = strtol(argv[0], 0, 0);
176 
177 	if (!smp_query_via(data, dest, IB_ATTR_MLNX_EXT_PORT_INFO, portnum, 0, srcport))
178 		return "Mellanox ext port info query failed";
179 
180 	mad_dump_mlnx_ext_port_info(buf, sizeof buf, data, sizeof data);
181 
182 	printf("# MLNX ext Port info: %s port %d\n%s", portid2str(dest), portnum, buf);
183 	return 0;
184 }
185 
186 static char *switch_info(ib_portid_t * dest, char **argv, int argc)
187 {
188 	char buf[2048];
189 	char data[IB_SMP_DATA_SIZE] = { 0 };
190 
191 	if (!smp_query_via(data, dest, IB_ATTR_SWITCH_INFO, 0, 0, srcport))
192 		return "switch info query failed";
193 
194 	mad_dump_switchinfo(buf, sizeof buf, data, sizeof data);
195 
196 	printf("# Switch info: %s\n%s", portid2str(dest), buf);
197 	return 0;
198 }
199 
200 static char *pkey_table(ib_portid_t * dest, char **argv, int argc)
201 {
202 	uint8_t data[IB_SMP_DATA_SIZE] = { 0 };
203 	int i, j, k;
204 	uint16_t *p;
205 	unsigned mod;
206 	int n, t, phy_ports;
207 	int portnum = 0;
208 
209 	if (argc > 0)
210 		portnum = strtol(argv[0], 0, 0);
211 
212 	/* Get the partition capacity */
213 	if (!smp_query_via(data, dest, IB_ATTR_NODE_INFO, 0, 0, srcport))
214 		return "node info query failed";
215 
216 	mad_decode_field(data, IB_NODE_TYPE_F, &t);
217 	mad_decode_field(data, IB_NODE_NPORTS_F, &phy_ports);
218 	if (portnum > phy_ports)
219 		return "invalid port number";
220 
221 	if ((t == IB_NODE_SWITCH) && (portnum != 0)) {
222 		if (!smp_query_via(data, dest, IB_ATTR_SWITCH_INFO, 0, 0,
223 				   srcport))
224 			return "switch info failed";
225 		mad_decode_field(data, IB_SW_PARTITION_ENFORCE_CAP_F, &n);
226 	} else
227 		mad_decode_field(data, IB_NODE_PARTITION_CAP_F, &n);
228 
229 	for (i = 0; i < (n + 31) / 32; i++) {
230 		mod = i | (portnum << 16);
231 		if (!smp_query_via(data, dest, IB_ATTR_PKEY_TBL, mod, 0,
232 				   srcport))
233 			return "pkey table query failed";
234 		if (i + 1 == (n + 31) / 32)
235 			k = ((n + 7 - i * 32) / 8) * 8;
236 		else
237 			k = 32;
238 		p = (uint16_t *) data;
239 		for (j = 0; j < k; j += 8, p += 8) {
240 			printf
241 			    ("%4u: 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x\n",
242 			     (i * 32) + j, ntohs(p[0]), ntohs(p[1]),
243 			     ntohs(p[2]), ntohs(p[3]), ntohs(p[4]), ntohs(p[5]),
244 			     ntohs(p[6]), ntohs(p[7]));
245 		}
246 	}
247 	printf("%d pkeys capacity for this port\n", n);
248 
249 	return 0;
250 }
251 
252 static char *sl2vl_dump_table_entry(ib_portid_t * dest, int in, int out)
253 {
254 	char buf[2048];
255 	char data[IB_SMP_DATA_SIZE] = { 0 };
256 	int portnum = (in << 8) | out;
257 
258 	if (!smp_query_via(data, dest, IB_ATTR_SLVL_TABLE, portnum, 0, srcport))
259 		return "slvl query failed";
260 
261 	mad_dump_sltovl(buf, sizeof buf, data, sizeof data);
262 	printf("ports: in %2d, out %2d: ", in, out);
263 	printf("%s", buf);
264 	return 0;
265 }
266 
267 static char *sl2vl_table(ib_portid_t * dest, char **argv, int argc)
268 {
269 	uint8_t data[IB_SMP_DATA_SIZE] = { 0 };
270 	int type, num_ports, portnum = 0;
271 	int i;
272 	char *ret;
273 
274 	if (argc > 0)
275 		portnum = strtol(argv[0], 0, 0);
276 
277 	if (!smp_query_via(data, dest, IB_ATTR_NODE_INFO, 0, 0, srcport))
278 		return "node info query failed";
279 
280 	mad_decode_field(data, IB_NODE_TYPE_F, &type);
281 	mad_decode_field(data, IB_NODE_NPORTS_F, &num_ports);
282 	if (portnum > num_ports)
283 		return "invalid port number";
284 
285 	printf("# SL2VL table: %s\n", portid2str(dest));
286 	printf("#                 SL: |");
287 	for (i = 0; i < 16; i++)
288 		printf("%2d|", i);
289 	printf("\n");
290 
291 	if (type != IB_NODE_SWITCH)
292 		return sl2vl_dump_table_entry(dest, 0, 0);
293 
294 	for (i = 0; i <= num_ports; i++) {
295 		ret = sl2vl_dump_table_entry(dest, i, portnum);
296 		if (ret)
297 			return ret;
298 	}
299 	return 0;
300 }
301 
302 static char *vlarb_dump_table_entry(ib_portid_t * dest, int portnum, int offset,
303 				    unsigned cap)
304 {
305 	char buf[2048];
306 	char data[IB_SMP_DATA_SIZE] = { 0 };
307 
308 	if (!smp_query_via(data, dest, IB_ATTR_VL_ARBITRATION,
309 			   (offset << 16) | portnum, 0, srcport))
310 		return "vl arb query failed";
311 	mad_dump_vlarbitration(buf, sizeof(buf), data, cap * 2);
312 	printf("%s", buf);
313 	return 0;
314 }
315 
316 static char *vlarb_dump_table(ib_portid_t * dest, int portnum,
317 			      char *name, int offset, int cap)
318 {
319 	char *ret;
320 
321 	printf("# %s priority VL Arbitration Table:", name);
322 	ret = vlarb_dump_table_entry(dest, portnum, offset,
323 				     cap < 32 ? cap : 32);
324 	if (!ret && cap > 32)
325 		ret = vlarb_dump_table_entry(dest, portnum, offset + 1,
326 					     cap - 32);
327 	return ret;
328 }
329 
330 static char *vlarb_table(ib_portid_t * dest, char **argv, int argc)
331 {
332 	uint8_t data[IB_SMP_DATA_SIZE] = { 0 };
333 	int portnum = 0;
334 	int type, enhsp0, lowcap, highcap;
335 	char *ret = 0;
336 
337 	if (argc > 0)
338 		portnum = strtol(argv[0], 0, 0);
339 
340 	/* port number of 0 could mean SP0 or port MAD arrives on */
341 	if (portnum == 0) {
342 		if (!smp_query_via(data, dest, IB_ATTR_NODE_INFO, 0, 0,
343 				   srcport))
344 			return "node info query failed";
345 
346 		mad_decode_field(data, IB_NODE_TYPE_F, &type);
347 		if (type == IB_NODE_SWITCH) {
348 			memset(data, 0, sizeof(data));
349 			if (!smp_query_via(data, dest, IB_ATTR_SWITCH_INFO, 0,
350 					   0, srcport))
351 				return "switch info query failed";
352 			mad_decode_field(data, IB_SW_ENHANCED_PORT0_F, &enhsp0);
353 			if (!enhsp0) {
354 				printf
355 				    ("# No VLArbitration tables (BSP0): %s port %d\n",
356 				     portid2str(dest), 0);
357 				return 0;
358 			}
359 			memset(data, 0, sizeof(data));
360 		}
361 	}
362 
363 	if (!smp_query_via(data, dest, IB_ATTR_PORT_INFO, portnum, 0, srcport))
364 		return "port info query failed";
365 
366 	mad_decode_field(data, IB_PORT_VL_ARBITRATION_LOW_CAP_F, &lowcap);
367 	mad_decode_field(data, IB_PORT_VL_ARBITRATION_HIGH_CAP_F, &highcap);
368 
369 	printf("# VLArbitration tables: %s port %d LowCap %d HighCap %d\n",
370 	       portid2str(dest), portnum, lowcap, highcap);
371 
372 	if (lowcap > 0)
373 		ret = vlarb_dump_table(dest, portnum, "Low", 1, lowcap);
374 
375 	if (!ret && highcap > 0)
376 		ret = vlarb_dump_table(dest, portnum, "High", 3, highcap);
377 
378 	return ret;
379 }
380 
381 static char *guid_info(ib_portid_t * dest, char **argv, int argc)
382 {
383 	uint8_t data[IB_SMP_DATA_SIZE] = { 0 };
384 	int i, j, k;
385 	uint64_t *p;
386 	unsigned mod;
387 	int n;
388 
389 	/* Get the guid capacity */
390 	if (!smp_query_via(data, dest, IB_ATTR_PORT_INFO, 0, 0, srcport))
391 		return "port info failed";
392 	mad_decode_field(data, IB_PORT_GUID_CAP_F, &n);
393 
394 	for (i = 0; i < (n + 7) / 8; i++) {
395 		mod = i;
396 		if (!smp_query_via(data, dest, IB_ATTR_GUID_INFO, mod, 0,
397 				   srcport))
398 			return "guid info query failed";
399 		if (i + 1 == (n + 7) / 8)
400 			k = ((n + 1 - i * 8) / 2) * 2;
401 		else
402 			k = 8;
403 		p = (uint64_t *) data;
404 		for (j = 0; j < k; j += 2, p += 2) {
405 			printf("%4u: 0x%016" PRIx64 " 0x%016" PRIx64 "\n",
406 			       (i * 8) + j, ntohll(p[0]), ntohll(p[1]));
407 		}
408 	}
409 	printf("%d guids capacity for this port\n", n);
410 
411 	return 0;
412 }
413 
414 static int process_opt(void *context, int ch, char *optarg)
415 {
416 	switch (ch) {
417 	case 1:
418 		node_name_map_file = strdup(optarg);
419 		break;
420 	case 'c':
421 		ibd_dest_type = IB_DEST_DRSLID;
422 		break;
423 	case 'x':
424 		extended_speeds = 1;
425 		break;
426 	default:
427 		return -1;
428 	}
429 	return 0;
430 }
431 
432 int main(int argc, char **argv)
433 {
434 	char usage_args[1024];
435 	int mgmt_classes[3] =
436 	    { IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS };
437 	ib_portid_t portid = { 0 };
438 	char *err;
439 	op_fn_t *fn;
440 	const match_rec_t *r;
441 	int n;
442 
443 	const struct ibdiag_opt opts[] = {
444 		{"combined", 'c', 0, NULL,
445 		 "use Combined route address argument"},
446 		{"node-name-map", 1, 1, "<file>", "node name map file"},
447 		{"extended", 'x', 0, NULL, "use extended speeds"},
448 		{0}
449 	};
450 	const char *usage_examples[] = {
451 		"portinfo 3 1\t\t\t\t# portinfo by lid, with port modifier",
452 		"-G switchinfo 0x2C9000100D051 1\t# switchinfo by guid",
453 		"-D nodeinfo 0\t\t\t\t# nodeinfo by direct route",
454 		"-c nodeinfo 6 0,12\t\t\t# nodeinfo by combined route",
455 		NULL
456 	};
457 
458 	n = sprintf(usage_args, "<op> <dest dr_path|lid|guid> [op params]\n"
459 		    "\nSupported ops (and aliases, case insensitive):\n");
460 	for (r = match_tbl; r->name; r++) {
461 		n += snprintf(usage_args + n, sizeof(usage_args) - n,
462 			      "  %s (%s) <addr>%s\n", r->name,
463 			      r->alias ? r->alias : "",
464 			      r->opt_portnum ? " [<portnum>]" : "");
465 		if (n >= sizeof(usage_args))
466 			exit(-1);
467 	}
468 
469 	ibdiag_process_opts(argc, argv, NULL, NULL, opts, process_opt,
470 			    usage_args, usage_examples);
471 
472 	argc -= optind;
473 	argv += optind;
474 
475 	if (argc < 2)
476 		ibdiag_show_usage();
477 
478 	if (!(fn = match_op(match_tbl, argv[0])))
479 		IBEXIT("operation '%s' not supported", argv[0]);
480 
481 	srcport = mad_rpc_open_port(ibd_ca, ibd_ca_port, mgmt_classes, 3);
482 	if (!srcport)
483 		IBEXIT("Failed to open '%s' port '%d'", ibd_ca, ibd_ca_port);
484 
485 	smp_mkey_set(srcport, ibd_mkey);
486 
487 	node_name_map = open_node_name_map(node_name_map_file);
488 
489 	if (ibd_dest_type != IB_DEST_DRSLID) {
490 		if (resolve_portid_str(ibd_ca, ibd_ca_port, &portid, argv[1],
491 				       ibd_dest_type, ibd_sm_id, srcport) < 0)
492 			IBEXIT("can't resolve destination port %s", argv[1]);
493 		if ((err = fn(&portid, argv + 2, argc - 2)))
494 			IBEXIT("operation %s: %s", argv[0], err);
495 	} else {
496 		char concat[64];
497 
498 		memset(concat, 0, 64);
499 		snprintf(concat, sizeof(concat), "%s %s", argv[1], argv[2]);
500 		if (resolve_portid_str(ibd_ca, ibd_ca_port, &portid, concat,
501 				       ibd_dest_type, ibd_sm_id, srcport) < 0)
502 			IBEXIT("can't resolve destination port %s", concat);
503 		if ((err = fn(&portid, argv + 3, argc - 3)))
504 			IBEXIT("operation %s: %s", argv[0], err);
505 	}
506 	close_node_name_map(node_name_map);
507 	mad_rpc_close_port(srcport);
508 	exit(0);
509 }
510