2 * Copyright (c) 2004-2008 Voltaire Inc. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
36 #endif /* HAVE_CONFIG_H */
46 #include <netinet/in.h>
49 #include <infiniband/common.h>
50 #include <infiniband/umad.h>
51 #include <infiniband/mad.h>
52 #include <infiniband/complib/cl_nodenamemap.h>
54 #include "ibdiag_common.h"
56 static int dest_type = IB_DEST_LID;
61 char *argv0 = "ibroute";
63 /*******************************************/
66 check_switch(ib_portid_t *portid, int *nports, uint64_t *guid,
67 uint8_t *sw, char *nd)
69 uint8_t ni[IB_SMP_DATA_SIZE] = {0};
72 DEBUG("checking node type");
73 if (!smp_query(ni, portid, IB_ATTR_NODE_INFO, 0, 0)) {
74 xdump(stderr, "nodeinfo\n", ni, sizeof ni);
75 return "node info failed: valid addr?";
78 if (!smp_query(nd, portid, IB_ATTR_NODE_DESC, 0, 0))
79 return "node desc failed";
81 mad_decode_field(ni, IB_NODE_TYPE_F, &type);
82 if (type != IB_NODE_SWITCH)
83 return "not a switch";
85 DEBUG("Gathering information about switch");
86 mad_decode_field(ni, IB_NODE_NPORTS_F, nports);
87 mad_decode_field(ni, IB_NODE_GUID_F, guid);
89 if (!smp_query(sw, portid, IB_ATTR_SWITCH_INFO, 0, 0))
90 return "switch info failed: is a switch node?";
95 #define IB_MLIDS_IN_BLOCK (IB_SMP_DATA_SIZE/2)
98 dump_mlid(char *str, int strlen, int mlid, int nports,
99 uint16_t mft[16][IB_MLIDS_IN_BLOCK])
106 int n = 0, chunks = ALIGN(nports + 1, 16) / 16;
107 for (i = 0; i < chunks; i++) {
108 mask = ntohs(mft[i][mlid%IB_MLIDS_IN_BLOCK]);
111 n += snprintf(str + n, strlen - n, "%04hx", mask);
117 if (!nonzero && !dump_all) {
123 for (i = 0; i <= nports; i++) {
127 mask = ntohs(mft[chunk][mlid%IB_MLIDS_IN_BLOCK]);
130 str[i*2] = (mask & (1 << bit)) ? 'x' : ' ';
133 if (!nonzero && !dump_all) {
141 uint16_t mft[16][IB_MLIDS_IN_BLOCK];
144 dump_multicast_tables(ib_portid_t *portid, int startlid, int endlid)
146 char nd[IB_SMP_DATA_SIZE] = {0};
147 uint8_t sw[IB_SMP_DATA_SIZE] = {0};
152 int block, i, j, e, nports, cap, chunks;
153 int n = 0, startblock, lastblock;
155 if ((s = check_switch(portid, &nports, &nodeguid, sw, nd)))
158 mad_decode_field(sw, IB_SW_MCAST_FDB_CAP_F, &cap);
160 if (!endlid || endlid > IB_MIN_MCAST_LID + cap - 1)
161 endlid = IB_MIN_MCAST_LID + cap - 1;
164 startlid = IB_MIN_MCAST_LID;
166 if (startlid < IB_MIN_MCAST_LID) {
167 IBWARN("illegal start mlid %x, set to %x", startlid, IB_MIN_MCAST_LID);
168 startlid = IB_MIN_MCAST_LID;
171 if (endlid > IB_MAX_MCAST_LID) {
172 IBWARN("illegal end mlid %x, truncate to %x", endlid, IB_MAX_MCAST_LID);
173 endlid = IB_MAX_MCAST_LID;
176 printf("Multicast mlids [0x%x-0x%x] of switch %s guid 0x%016" PRIx64 " (%s):\n",
177 startlid, endlid, portid2str(portid), nodeguid, clean_nodedesc(nd));
180 printf(" MLid Port Mask\n");
183 for (i = 0, s = str; i <= nports; i++) {
184 *s++ = (i%10) ? ' ' : '0' + i/10;
188 printf(" %s\n", str);
190 for (i = 0, s = str; i <= nports; i++)
191 s += sprintf(s, "%d ", i%10);
192 printf(" Ports: %s\n", str);
196 printf("Switch muticast mlids capability is 0x%d\n", cap);
198 chunks = ALIGN(nports + 1, 16) / 16;
200 startblock = startlid / IB_MLIDS_IN_BLOCK;
201 lastblock = endlid / IB_MLIDS_IN_BLOCK;
202 for (block = startblock; block <= lastblock; block++) {
203 for (j = 0; j < chunks; j++) {
204 mod = (block - IB_MIN_MCAST_LID/IB_MLIDS_IN_BLOCK) | (j << 28);
206 DEBUG("reading block %x chunk %d mod %x", block, j, mod);
207 if (!smp_query(mft + j, portid, IB_ATTR_MULTICASTFORWTBL, mod, 0))
208 return "multicast forwarding table get failed";
211 i = block * IB_MLIDS_IN_BLOCK;
212 e = i + IB_MLIDS_IN_BLOCK;
219 if (dump_mlid(str, sizeof str, i, nports, mft) == 0)
221 printf("0x%04x %s\n", i, str);
226 printf("%d %smlids dumped \n", n, dump_all ? "" : "valid ");
231 dump_lid(char *str, int strlen, int lid, int valid)
233 char nd[IB_SMP_DATA_SIZE] = {0};
234 uint8_t ni[IB_SMP_DATA_SIZE] = {0};
235 uint8_t pi[IB_SMP_DATA_SIZE] = {0};
236 ib_portid_t lidport = {0};
237 static int last_port_lid, base_port_lid;
238 char ntype[50], sguid[30], desc[64];
239 static uint64_t portguid;
240 int baselid, lmc, type;
247 if (lid <= last_port_lid) {
249 return snprintf(str, strlen, ": (path #%d - illegal port)",
250 lid - base_port_lid);
252 return snprintf(str, strlen,
253 ": (path #%d out of %d)",
254 lid - base_port_lid + 1,
255 last_port_lid - base_port_lid + 1);
257 return snprintf(str, strlen,
258 ": (path #%d out of %d: portguid %s)",
259 lid - base_port_lid + 1,
260 last_port_lid - base_port_lid + 1,
261 mad_dump_val(IB_NODE_PORT_GUID_F, sguid, sizeof sguid, &portguid));
266 return snprintf(str, strlen, ": (illegal port)");
271 if (!smp_query(nd, &lidport, IB_ATTR_NODE_DESC, 0, 100) ||
272 !smp_query(pi, &lidport, IB_ATTR_PORT_INFO, 0, 100) ||
273 !smp_query(ni, &lidport, IB_ATTR_NODE_INFO, 0, 100))
274 return snprintf(str, strlen, ": (unknown node and type)");
276 mad_decode_field(ni, IB_NODE_PORT_GUID_F, &portguid);
277 mad_decode_field(ni, IB_NODE_TYPE_F, &type);
279 mad_decode_field(pi, IB_PORT_LID_F, &baselid);
280 mad_decode_field(pi, IB_PORT_LMC_F, &lmc);
283 base_port_lid = baselid;
284 last_port_lid = baselid + (1 << lmc) - 1;
287 return snprintf(str, strlen, ": (%s portguid %s: %s)",
288 mad_dump_val(IB_NODE_TYPE_F, ntype, sizeof ntype, &type),
289 mad_dump_val(IB_NODE_PORT_GUID_F, sguid, sizeof sguid, &portguid),
290 mad_dump_val(IB_NODE_DESC_F, desc, sizeof desc, clean_nodedesc(nd)));
294 dump_unicast_tables(ib_portid_t *portid, int startlid, int endlid)
296 char lft[IB_SMP_DATA_SIZE];
297 char nd[IB_SMP_DATA_SIZE];
298 uint8_t sw[IB_SMP_DATA_SIZE];
301 int block, i, e, nports, top;
302 int n = 0, startblock, endblock;
304 if ((s = check_switch(portid, &nports, &nodeguid, sw, nd)))
307 mad_decode_field(sw, IB_SW_LINEAR_FDB_TOP_F, &top);
309 if (!endlid || endlid > top)
312 if (endlid > IB_MAX_UCAST_LID) {
313 IBWARN("ilegal lft top %d, truncate to %d", endlid, IB_MAX_UCAST_LID);
314 endlid = IB_MAX_UCAST_LID;
317 printf("Unicast lids [0x%x-0x%x] of switch %s guid 0x%016" PRIx64 " (%s):\n",
318 startlid, endlid, portid2str(portid), nodeguid, clean_nodedesc(nd));
320 DEBUG("Switch top is 0x%x\n", top);
322 printf(" Lid Out Destination\n");
323 printf(" Port Info \n");
324 startblock = startlid / IB_SMP_DATA_SIZE;
325 endblock = ALIGN(endlid, IB_SMP_DATA_SIZE) / IB_SMP_DATA_SIZE;
326 for (block = startblock; block <= endblock; block++) {
327 DEBUG("reading block %d", block);
328 if (!smp_query(lft, portid, IB_ATTR_LINEARFORWTBL, block, 0))
329 return "linear forwarding table get failed";
330 i = block * IB_SMP_DATA_SIZE;
331 e = i + IB_SMP_DATA_SIZE;
338 unsigned outport = lft[i % IB_SMP_DATA_SIZE];
339 unsigned valid = (outport <= nports);
341 if (!valid && !dump_all)
343 dump_lid(str, sizeof str, i, valid);
344 printf("0x%04x %03u %s\n", i, outport & 0xff, str);
349 printf("%d %slids dumped \n", n, dump_all ? "" : "valid ");
358 if (!(basename = strrchr(argv0, '/')))
363 fprintf(stderr, "Usage: %s [-d(ebug)] -a(ll) -n(o_dests) -v(erbose) -D(irect) -G(uid) -M(ulticast) -s smlid -V(ersion) -C ca_name -P ca_port "
364 "-t(imeout) timeout_ms] [<dest dr_path|lid|guid> [<startlid> [<endlid>]]]\n",
366 fprintf(stderr, "\n\tUnicast examples:\n");
367 fprintf(stderr, "\t\t%s 4\t# dump all lids with valid out ports of switch with lid 4\n", basename);
368 fprintf(stderr, "\t\t%s -a 4\t# same, but dump all lids, even with invalid out ports\n", basename);
369 fprintf(stderr, "\t\t%s -n 4\t# simple dump format - no destination resolving\n", basename);
370 fprintf(stderr, "\t\t%s 4 10\t# dump lids starting from 10\n", basename);
371 fprintf(stderr, "\t\t%s 4 0x10 0x20\t# dump lid range\n", basename);
372 fprintf(stderr, "\t\t%s -G 0x08f1040023\t# resolve switch by GUID\n", basename);
373 fprintf(stderr, "\t\t%s -D 0,1\t# resolve switch by direct path\n", basename);
375 fprintf(stderr, "\n\tMulticast examples:\n");
376 fprintf(stderr, "\t\t%s -M 4\t# dump all non empty mlids of switch with lid 4\n", basename);
377 fprintf(stderr, "\t\t%s -M 4 0xc010 0xc020\t# same, but with range\n", basename);
378 fprintf(stderr, "\t\t%s -M -n 4\t# simple dump format\n", basename);
383 main(int argc, char **argv)
385 int mgmt_classes[3] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS};
386 ib_portid_t portid = {0};
387 ib_portid_t *sm_id = 0, sm_portid = {0};
389 int multicast = 0, startlid = 0, endlid = 0;
394 static char const str_opts[] = "C:P:t:s:danvDGMVhu";
395 static const struct option long_opts[] = {
398 { "debug", 0, 0, 'd'},
400 { "no_dests", 0, 0, 'n'},
401 { "verbose", 0, 0, 'v'},
402 { "Direct", 0, 0, 'D'},
403 { "Guid", 0, 0, 'G'},
404 { "Multicast", 0, 0, 'M'},
405 { "timeout", 1, 0, 't'},
407 { "Version", 0, 0, 'V'},
408 { "help", 0, 0, 'h'},
409 { "usage", 0, 0, 'u'},
416 int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
424 ca_port = strtoul(optarg, 0, 0);
433 dest_type = IB_DEST_DRPATH;
436 dest_type = IB_DEST_GUID;
445 if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0)
446 IBERROR("can't resolve SM destination port %s", optarg);
450 timeout = strtoul(optarg, 0, 0);
451 madrpc_set_timeout(timeout);
454 madrpc_show_errors(1);
458 fprintf(stderr, "%s %s\n", argv0, get_build_version() );
472 startlid = strtoul(argv[1], 0, 0);
474 endlid = strtoul(argv[2], 0, 0);
476 madrpc_init(ca, ca_port, mgmt_classes, 3);
479 if (ib_resolve_self(&portid, 0, 0) < 0)
480 IBERROR("can't resolve self addr");
482 if (ib_resolve_portid_str(&portid, argv[0], dest_type, sm_id) < 0)
483 IBERROR("can't resolve destination port %s", argv[1]);
487 err = dump_multicast_tables(&portid, startlid, endlid);
489 err = dump_unicast_tables(&portid, startlid, endlid);
492 IBERROR("dump tables: %s", err);