2 * Copyright (c) 2004-2009 Voltaire Inc. All rights reserved.
3 * Copyright (c) 2009-2011 Mellanox Technologies LTD. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
37 #endif /* HAVE_CONFIG_H */
45 #include <netinet/in.h>
47 #include <infiniband/umad.h>
48 #include <infiniband/mad.h>
49 #include <complib/cl_nodenamemap.h>
51 #include "ibdiag_common.h"
53 struct ibmad_port *srcport;
55 static int brief, dump_all, multicast;
57 static char *node_name_map_file = NULL;
58 static nn_map_t *node_name_map = NULL;
60 /*******************************************/
62 char *check_switch(ib_portid_t * portid, unsigned int *nports, uint64_t * guid,
63 uint8_t * sw, char *nd)
65 uint8_t ni[IB_SMP_DATA_SIZE] = { 0 };
68 DEBUG("checking node type");
69 if (!smp_query_via(ni, portid, IB_ATTR_NODE_INFO, 0, 0, srcport)) {
70 xdump(stderr, "nodeinfo\n", ni, sizeof ni);
71 return "node info failed: valid addr?";
74 if (!smp_query_via(nd, portid, IB_ATTR_NODE_DESC, 0, 0, srcport))
75 return "node desc failed";
77 mad_decode_field(ni, IB_NODE_TYPE_F, &type);
78 if (type != IB_NODE_SWITCH)
79 return "not a switch";
81 DEBUG("Gathering information about switch");
82 mad_decode_field(ni, IB_NODE_NPORTS_F, nports);
83 mad_decode_field(ni, IB_NODE_GUID_F, guid);
85 if (!smp_query_via(sw, portid, IB_ATTR_SWITCH_INFO, 0, 0, srcport))
86 return "switch info failed: is a switch node?";
91 #define IB_MLIDS_IN_BLOCK (IB_SMP_DATA_SIZE/2)
93 int dump_mlid(char *str, int strlen, unsigned mlid, unsigned nports,
94 uint16_t mft[16][IB_MLIDS_IN_BLOCK])
97 unsigned i, chunk, bit, nonzero = 0;
101 unsigned chunks = ALIGN(nports + 1, 16) / 16;
102 for (i = 0; i < chunks; i++) {
103 mask = ntohs(mft[i][mlid % IB_MLIDS_IN_BLOCK]);
106 n += snprintf(str + n, strlen - n, "%04hx", mask);
112 if (!nonzero && !dump_all) {
118 for (i = 0; i <= nports; i++) {
122 mask = ntohs(mft[chunk][mlid % IB_MLIDS_IN_BLOCK]);
125 str[i * 2] = (mask & (1 << bit)) ? 'x' : ' ';
126 str[i * 2 + 1] = ' ';
128 if (!nonzero && !dump_all) {
136 uint16_t mft[16][IB_MLIDS_IN_BLOCK] = { { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0}, { 0 }, { 0 } };
138 char *dump_multicast_tables(ib_portid_t * portid, unsigned startlid,
141 char nd[IB_SMP_DATA_SIZE] = { 0 };
142 uint8_t sw[IB_SMP_DATA_SIZE] = { 0 };
147 unsigned block, i, j, e, nports, cap, chunks, startblock, lastblock,
152 if ((s = check_switch(portid, &nports, &nodeguid, sw, nd)))
155 mad_decode_field(sw, IB_SW_MCAST_FDB_CAP_F, &cap);
156 mad_decode_field(sw, IB_SW_MCAST_FDB_TOP_F, &top);
158 if (!endlid || endlid > IB_MIN_MCAST_LID + cap - 1)
159 endlid = IB_MIN_MCAST_LID + cap - 1;
160 if (!dump_all && top && top < endlid) {
161 if (top < IB_MIN_MCAST_LID - 1)
162 IBWARN("illegal top mlid %x", top);
168 startlid = IB_MIN_MCAST_LID;
169 else if (startlid < IB_MIN_MCAST_LID) {
170 IBWARN("illegal start mlid %x, set to %x", startlid,
172 startlid = IB_MIN_MCAST_LID;
175 if (endlid > IB_MAX_MCAST_LID) {
176 IBWARN("illegal end mlid %x, truncate to %x", endlid,
178 endlid = IB_MAX_MCAST_LID;
181 mapnd = remap_node_name(node_name_map, nodeguid, nd);
183 printf("Multicast mlids [0x%x-0x%x] of switch %s guid 0x%016" PRIx64
184 " (%s):\n", startlid, endlid, portid2str(portid), nodeguid,
188 printf(" MLid Port Mask\n");
191 for (i = 0, s = str; i <= nports; i++) {
192 *s++ = (i % 10) ? ' ' : '0' + i / 10;
196 printf(" %s\n", str);
198 for (i = 0, s = str; i <= nports; i++)
199 s += sprintf(s, "%d ", i % 10);
200 printf(" Ports: %s\n", str);
204 printf("Switch multicast mlid capability is %d top is 0x%x\n",
207 chunks = ALIGN(nports + 1, 16) / 16;
209 startblock = startlid / IB_MLIDS_IN_BLOCK;
210 lastblock = endlid / IB_MLIDS_IN_BLOCK;
211 for (block = startblock; block <= lastblock; block++) {
212 for (j = 0; j < chunks; j++) {
214 mod = (block - IB_MIN_MCAST_LID / IB_MLIDS_IN_BLOCK)
217 DEBUG("reading block %x chunk %d mod %x", block, j,
219 if (!smp_query_status_via
220 (mft + j, portid, IB_ATTR_MULTICASTFORWTBL, mod, 0,
222 fprintf(stderr, "SubnGet() failed"
223 "; MAD status 0x%x AM 0x%x\n",
229 i = block * IB_MLIDS_IN_BLOCK;
230 e = i + IB_MLIDS_IN_BLOCK;
237 if (dump_mlid(str, sizeof str, i, nports, mft) == 0)
239 printf("0x%04x %s\n", i, str);
244 printf("%d %smlids dumped \n", n, dump_all ? "" : "valid ");
250 int dump_lid(char *str, int strlen, int lid, int valid)
252 char nd[IB_SMP_DATA_SIZE] = { 0 };
253 uint8_t ni[IB_SMP_DATA_SIZE] = { 0 };
254 uint8_t pi[IB_SMP_DATA_SIZE] = { 0 };
255 ib_portid_t lidport = { 0 };
256 static int last_port_lid, base_port_lid;
257 char ntype[50], sguid[30];
258 static uint64_t portguid;
260 int baselid, lmc, type;
269 if (lid <= last_port_lid) {
271 return snprintf(str, strlen,
272 ": (path #%d - illegal port)",
273 lid - base_port_lid);
275 return snprintf(str, strlen,
276 ": (path #%d out of %d)",
277 lid - base_port_lid + 1,
278 last_port_lid - base_port_lid + 1);
280 return snprintf(str, strlen,
281 ": (path #%d out of %d: portguid %s)",
282 lid - base_port_lid + 1,
283 last_port_lid - base_port_lid + 1,
284 mad_dump_val(IB_NODE_PORT_GUID_F, sguid,
285 sizeof sguid, &portguid));
290 return snprintf(str, strlen, ": (illegal port)");
295 if (!smp_query_via(nd, &lidport, IB_ATTR_NODE_DESC, 0, 100, srcport) ||
296 !smp_query_via(pi, &lidport, IB_ATTR_PORT_INFO, 0, 100, srcport) ||
297 !smp_query_via(ni, &lidport, IB_ATTR_NODE_INFO, 0, 100, srcport))
298 return snprintf(str, strlen, ": (unknown node and type)");
300 mad_decode_field(ni, IB_NODE_GUID_F, &nodeguid);
301 mad_decode_field(ni, IB_NODE_PORT_GUID_F, &portguid);
302 mad_decode_field(ni, IB_NODE_TYPE_F, &type);
304 mad_decode_field(pi, IB_PORT_LID_F, &baselid);
305 mad_decode_field(pi, IB_PORT_LMC_F, &lmc);
308 base_port_lid = baselid;
309 last_port_lid = baselid + (1 << lmc) - 1;
312 mapnd = remap_node_name(node_name_map, nodeguid, nd);
314 rc = snprintf(str, strlen, ": (%s portguid %s: '%s')",
315 mad_dump_val(IB_NODE_TYPE_F, ntype, sizeof ntype,
316 &type), mad_dump_val(IB_NODE_PORT_GUID_F,
325 char *dump_unicast_tables(ib_portid_t * portid, int startlid, int endlid)
327 char lft[IB_SMP_DATA_SIZE] = { 0 };
328 char nd[IB_SMP_DATA_SIZE] = { 0 };
329 uint8_t sw[IB_SMP_DATA_SIZE] = { 0 };
332 int block, i, e, top;
334 int n = 0, startblock, endblock;
337 if ((s = check_switch(portid, &nports, &nodeguid, sw, nd)))
340 mad_decode_field(sw, IB_SW_LINEAR_FDB_TOP_F, &top);
342 if (!endlid || endlid > top)
345 if (endlid > IB_MAX_UCAST_LID) {
346 IBWARN("illegal lft top %d, truncate to %d", endlid,
348 endlid = IB_MAX_UCAST_LID;
351 mapnd = remap_node_name(node_name_map, nodeguid, nd);
353 printf("Unicast lids [0x%x-0x%x] of switch %s guid 0x%016" PRIx64
354 " (%s):\n", startlid, endlid, portid2str(portid), nodeguid,
357 DEBUG("Switch top is 0x%x\n", top);
359 printf(" Lid Out Destination\n");
360 printf(" Port Info \n");
361 startblock = startlid / IB_SMP_DATA_SIZE;
362 endblock = ALIGN(endlid, IB_SMP_DATA_SIZE) / IB_SMP_DATA_SIZE;
363 for (block = startblock; block < endblock; block++) {
365 DEBUG("reading block %d", block);
366 if (!smp_query_status_via(lft, portid, IB_ATTR_LINEARFORWTBL, block,
367 0, &status, srcport)) {
368 fprintf(stderr, "SubnGet() failed"
369 "; MAD status 0x%x AM 0x%x\n",
373 i = block * IB_SMP_DATA_SIZE;
374 e = i + IB_SMP_DATA_SIZE;
381 unsigned outport = lft[i % IB_SMP_DATA_SIZE];
382 unsigned valid = (outport <= nports);
384 if (!valid && !dump_all)
386 dump_lid(str, sizeof str, i, valid);
387 printf("0x%04x %03u %s\n", i, outport & 0xff, str);
392 printf("%d %slids dumped \n", n, dump_all ? "" : "valid ");
397 static int process_opt(void *context, int ch, char *optarg)
410 node_name_map_file = strdup(optarg);
418 int main(int argc, char **argv)
420 int mgmt_classes[3] =
421 { IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS };
422 ib_portid_t portid = { 0 };
423 unsigned startlid = 0, endlid = 0;
426 const struct ibdiag_opt opts[] = {
427 {"all", 'a', 0, NULL, "show all lids, even invalid entries"},
428 {"no_dests", 'n', 0, NULL,
429 "do not try to resolve destinations"},
430 {"Multicast", 'M', 0, NULL, "show multicast forwarding tables"},
431 {"node-name-map", 1, 1, "<file>", "node name map file"},
434 char usage_args[] = "[<dest dr_path|lid|guid> [<startlid> [<endlid>]]]";
435 const char *usage_examples[] = {
436 " -- Unicast examples:",
437 "4\t# dump all lids with valid out ports of switch with lid 4",
438 "-a 4\t# same, but dump all lids, even with invalid out ports",
439 "-n 4\t# simple dump format - no destination resolving",
440 "4 10\t# dump lids starting from 10",
441 "4 0x10 0x20\t# dump lid range",
442 "-G 0x08f1040023\t# resolve switch by GUID",
443 "-D 0,1\t# resolve switch by direct path",
444 " -- Multicast examples:",
445 "-M 4\t# dump all non empty mlids of switch with lid 4",
446 "-M 4 0xc010 0xc020\t# same, but with range",
447 "-M -n 4\t# simple dump format",
451 ibdiag_process_opts(argc, argv, NULL, "K", opts, process_opt,
452 usage_args, usage_examples);
461 startlid = strtoul(argv[1], 0, 0);
463 endlid = strtoul(argv[2], 0, 0);
465 node_name_map = open_node_name_map(node_name_map_file);
467 srcport = mad_rpc_open_port(ibd_ca, ibd_ca_port, mgmt_classes, 3);
469 IBEXIT("Failed to open '%s' port '%d'", ibd_ca, ibd_ca_port);
471 smp_mkey_set(srcport, ibd_mkey);
473 if (resolve_portid_str(ibd_ca, ibd_ca_port, &portid, argv[0],
474 ibd_dest_type, ibd_sm_id, srcport) < 0)
475 IBEXIT("can't resolve destination port %s", argv[0]);
478 err = dump_multicast_tables(&portid, startlid, endlid);
480 err = dump_unicast_tables(&portid, startlid, endlid);
483 IBEXIT("dump tables: %s", err);
485 mad_rpc_close_port(srcport);
486 close_node_name_map(node_name_map);