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 */
43 #include <arpa/inet.h>
45 #include <infiniband/common.h>
46 #include <infiniband/umad.h>
47 #include <infiniband/mad.h>
49 #include <sys/socket.h>
51 #include "ibdiag_common.h"
53 char *argv0 = "ibaddr";
56 ib_resolve_addr(ib_portid_t *portid, int portnum, int show_lid, int show_gid)
58 char gid_str[INET6_ADDRSTRLEN];
61 uint64_t guid, prefix;
65 if (!smp_query(nodeinfo, portid, IB_ATTR_NODE_INFO, 0, 0))
68 if (!smp_query(portinfo, portid, IB_ATTR_PORT_INFO, portnum, 0))
71 mad_decode_field(portinfo, IB_PORT_LID_F, &portid->lid);
72 mad_decode_field(portinfo, IB_PORT_GID_PREFIX_F, &prefix);
73 mad_decode_field(portinfo, IB_PORT_LMC_F, &lmc);
74 mad_decode_field(nodeinfo, IB_NODE_PORT_GUID_F, &guid);
76 mad_encode_field(gid, IB_GID_PREFIX_F, &prefix);
77 mad_encode_field(gid, IB_GID_GUID_F, &guid);
80 printf("GID %s ", inet_ntop(AF_INET6, gid, gid_str,
85 printf("LID start 0x%x end 0x%x", portid->lid, portid->lid + (1 << lmc) - 1);
86 else if (show_lid < 0)
87 printf("LID start %d end %d", portid->lid, portid->lid + (1 << lmc) - 1);
97 if (!(basename = strrchr(argv0, '/')))
102 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 "
103 "-t(imeout) timeout_ms -V(ersion) -h(elp)] [<lid|dr_path|guid>]\n",
105 fprintf(stderr, "\tExamples:\n");
106 fprintf(stderr, "\t\t%s\t\t\t# local port's address\n", basename);
107 fprintf(stderr, "\t\t%s 32\t\t# show lid range and gid of lid 32\n", basename);
108 fprintf(stderr, "\t\t%s -G 0x8f1040023\t# same but using guid address\n", basename);
109 fprintf(stderr, "\t\t%s -l 32\t\t# show lid range only\n", basename);
110 fprintf(stderr, "\t\t%s -L 32\t\t# show decimal lid range only\n", basename);
111 fprintf(stderr, "\t\t%s -g 32\t\t# show gid address only\n", basename);
116 main(int argc, char **argv)
118 int mgmt_classes[3] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS};
119 ib_portid_t *sm_id = 0, sm_portid = {0};
120 ib_portid_t portid = {0};
122 int dest_type = IB_DEST_LID;
123 int timeout = 0; /* use default */
124 int show_lid = 0, show_gid = 0;
129 static char const str_opts[] = "C:P:t:s:dDGglLVhu";
130 static const struct option long_opts[] = {
133 { "debug", 0, 0, 'd'},
134 { "Direct", 0, 0, 'D'},
135 { "Guid", 0, 0, 'G'},
136 { "gid_show", 0, 0, 'g'},
137 { "lid_show", 0, 0, 'l'},
138 { "Lid_show", 0, 0, 'L'},
139 { "timeout", 1, 0, 't'},
140 { "sm_port", 1, 0, 's'},
141 { "Version", 0, 0, 'V'},
142 { "help", 0, 0, 'h'},
143 { "usage", 0, 0, 'u'},
150 int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
158 ca_port = strtoul(optarg, 0, 0);
164 dest_type = IB_DEST_DRPATH;
170 dest_type = IB_DEST_GUID;
179 if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0)
180 IBERROR("can't resolve SM destination port %s", optarg);
184 timeout = strtoul(optarg, 0, 0);
185 madrpc_set_timeout(timeout);
188 fprintf(stderr, "%s %s\n", argv0, get_build_version() );
199 port = strtoul(argv[1], 0, 0);
201 if (!show_lid && !show_gid)
202 show_lid = show_gid = 1;
204 madrpc_init(ca, ca_port, mgmt_classes, 3);
207 if (ib_resolve_portid_str(&portid, argv[0], dest_type, sm_id) < 0)
208 IBERROR("can't resolve destination port %s", argv[0]);
210 if (ib_resolve_self(&portid, &port, 0) < 0)
211 IBERROR("can't resolve self port %s", argv[0]);
214 if (ib_resolve_addr(&portid, port, show_lid, show_gid) < 0)
215 IBERROR("can't resolve requested address");