2 * Copyright (c) 2006-2009 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 <infiniband/umad.h>
44 #include <infiniband/mad.h>
45 #include <infiniband/iba/ib_types.h>
47 #include "ibdiag_common.h"
49 #define info(fmt, ...) fprintf(stderr, "INFO: " fmt, ## __VA_ARGS__ )
50 #define err(fmt, ...) fprintf(stderr, "ERR: " fmt, ## __VA_ARGS__ )
52 #define dbg(fmt, ...) fprintf(stderr, "DBG: " fmt, ## __VA_ARGS__ )
59 static ibmad_gid_t mgid_ipoib = {
60 0xff, 0x12, 0x40, 0x1b, 0xff, 0xff, 0x00, 0x00,
61 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
64 struct ibmad_port *srcport;
66 uint64_t build_mcm_rec(uint8_t * data, ibmad_gid_t mgid, ibmad_gid_t port_gid)
68 memset(data, 0, IB_SA_DATA_SIZE);
69 mad_set_array(data, 0, IB_SA_MCM_MGID_F, mgid);
70 mad_set_array(data, 0, IB_SA_MCM_PORTGID_F, port_gid);
71 mad_set_field(data, 0, IB_SA_MCM_JOIN_STATE_F, 1);
73 return IB_MCR_COMPMASK_MGID | IB_MCR_COMPMASK_PORT_GID |
74 IB_MCR_COMPMASK_JOIN_STATE;
77 static void build_mcm_rec_umad(void *umad, ib_portid_t * dport, int method,
78 uint64_t comp_mask, uint8_t * data)
82 memset(&rpc, 0, sizeof(rpc));
83 rpc.mgtclass = IB_SA_CLASS;
85 rpc.attr.id = IB_SA_ATTR_MCRECORD;
86 rpc.attr.mod = 0; // ???
88 rpc.datasz = IB_SA_DATA_SIZE;
89 rpc.dataoffs = IB_SA_DATA_OFFS;
91 mad_build_pkt(umad, &rpc, dport, NULL, data);
94 static int rereg_send(int port, int agent, ib_portid_t * dport,
95 uint8_t * umad, int len, int method, ibmad_gid_t port_gid)
97 uint8_t data[IB_SA_DATA_SIZE];
100 comp_mask = build_mcm_rec(data, mgid_ipoib, port_gid);
102 build_mcm_rec_umad(umad, dport, method, comp_mask, data);
103 if (umad_send(port, agent, umad, len, TMO, 0) < 0) {
104 err("umad_send %s failed: %s\n",
105 (method == IB_MAD_METHOD_GET) ? "query" : "non query",
109 dbg("umad_send %d: tid = 0x%016" PRIx64 "\n", method,
110 mad_get_field64(umad_get_mad(umad), 0, IB_MAD_TRID_F));
115 static int rereg_port_gid(int port, int agent, ib_portid_t * dport,
116 uint8_t * umad, int len, ibmad_gid_t port_gid)
118 uint8_t data[IB_SA_DATA_SIZE];
121 comp_mask = build_mcm_rec(data, mgid_ipoib, port_gid);
123 build_mcm_rec_umad(umad, dport, IB_MAD_METHOD_DELETE, comp_mask, data);
124 if (umad_send(port, agent, umad, len, TMO, 0) < 0) {
125 err("umad_send leave failed: %s\n", strerror(errno));
128 dbg("umad_send leave: tid = 0x%016" PRIx64 "\n",
129 mad_get_field64(umad_get_mad(umad), 0, IB_MAD_TRID_F));
131 build_mcm_rec_umad(umad, dport, IB_MAD_METHOD_SET, comp_mask, data);
132 if (umad_send(port, agent, umad, len, TMO, 0) < 0) {
133 err("umad_send join failed: %s\n", strerror(errno));
136 dbg("umad_send join: tid = 0x%016" PRIx64 "\n",
137 mad_get_field64(umad_get_mad(umad), 0, IB_MAD_TRID_F));
148 static int rereg_send_all(int port, int agent, ib_portid_t * dport,
149 struct guid_trid *list, unsigned cnt)
152 int len = umad_size() + 256;
156 info("rereg_send_all... cnt = %u\n", cnt);
158 umad = calloc(1, len);
160 err("cannot alloc mem for umad: %s\n", strerror(errno));
164 for (i = 0; i < cnt; i++) {
166 rereg_port_gid(port, agent, dport, umad, len, list[i].gid);
168 err("rereg_send_all: rereg_port_gid 0x%016" PRIx64
169 " failed\n", ntohll(list[i].guid));
172 list[i].trid = mad_get_field64(umad_get_mad(umad), 0,
176 info("rereg_send_all: sent %u requests\n", cnt * 2);
183 static int rereg_recv(int port, int agent, ib_portid_t * dport,
184 uint8_t * umad, int length, int tmo)
189 while ((ret = umad_recv(port, umad, &len, tmo)) < 0 &&
190 errno == ETIMEDOUT) {
195 err("umad_recv %d failed: %s\n", ret, strerror(errno));
198 dbg("umad_recv (retries %d), tid = 0x%016" PRIx64
199 ": len = %d, status = %d\n", retry,
200 mad_get_field64(umad_get_mad(umad), 0, IB_MAD_TRID_F), len,
206 static int rereg_recv_all(int port, int agent, ib_portid_t * dport,
207 struct guid_trid *list, unsigned cnt)
210 int len = umad_size() + 256;
212 unsigned n, method, status;
215 info("rereg_recv_all...\n");
217 umad = calloc(1, len);
219 err("cannot alloc mem for umad: %s\n", strerror(errno));
224 while (rereg_recv(port, agent, dport, umad, len, TMO) > 0) {
225 dbg("rereg_recv_all: done %d\n", n);
227 mad = umad_get_mad(umad);
229 method = mad_get_field(mad, 0, IB_MAD_METHOD_F);
230 status = mad_get_field(mad, 0, IB_MAD_STATUS_F);
233 dbg("MAD status %x, method %x\n", status, method);
236 (method & 0x7f) == (IB_MAD_METHOD_GET_RESPONSE & 0x7f)) {
237 trid = mad_get_field64(mad, 0, IB_MAD_TRID_F);
238 for (i = 0; i < cnt; i++)
239 if (trid == list[i].trid)
242 err("cannot find trid 0x%016" PRIx64 "\n",
246 info("guid 0x%016" PRIx64
247 ": method = %x status = %x. Resending\n",
248 ntohll(list[i].guid), method, status);
249 rereg_port_gid(port, agent, dport, umad, len,
252 mad_get_field64(umad_get_mad(umad), 0,
257 info("rereg_recv_all: got %u responses\n", n);
263 static int rereg_query_all(int port, int agent, ib_portid_t * dport,
264 struct guid_trid *list, unsigned cnt)
267 int len = umad_size() + 256;
268 unsigned method, status;
272 info("rereg_query_all...\n");
274 umad = calloc(1, len);
276 err("cannot alloc mem for umad: %s\n", strerror(errno));
280 for (i = 0; i < cnt; i++) {
281 ret = rereg_send(port, agent, dport, umad, len,
282 IB_MAD_METHOD_GET, list[i].gid);
284 err("query_all: rereg_send failed.\n");
288 ret = rereg_recv(port, agent, dport, umad, len, TMO);
290 err("query_all: rereg_recv failed.\n");
294 mad = umad_get_mad(umad);
296 method = mad_get_field(mad, 0, IB_MAD_METHOD_F);
297 status = mad_get_field(mad, 0, IB_MAD_STATUS_F);
300 info("guid 0x%016" PRIx64 ": status %x, method %x\n",
301 ntohll(list[i].guid), status, method);
304 info("rereg_query_all: %u queried.\n", cnt);
310 #define MAX_CLIENTS 50
312 static int rereg_and_test_port(char *guid_file, int port, int agent,
313 ib_portid_t * dport, int timeout)
317 ibmad_gid_t port_gid;
318 uint64_t prefix = htonll(0xfe80000000000000ull);
319 uint64_t guid = htonll(0x0002c90200223825ull);
320 struct guid_trid *list;
323 list = calloc(MAX_CLIENTS, sizeof(*list));
325 err("cannot alloc mem for guid/trid list: %s\n",
330 f = fopen(guid_file, "r");
332 err("cannot open %s: %s\n", guid_file, strerror(errno));
336 while (fgets(line, sizeof(line), f)) {
337 guid = strtoull(line, NULL, 0);
339 memcpy(&port_gid[0], &prefix, 8);
340 memcpy(&port_gid[8], &guid, 8);
343 memcpy(list[i].gid, port_gid, sizeof(list[i].gid));
345 if (++i >= MAX_CLIENTS)
350 rereg_send_all(port, agent, dport, list, i);
351 rereg_recv_all(port, agent, dport, list, i);
353 rereg_query_all(port, agent, dport, list, i);
359 int main(int argc, char **argv)
361 char *guid_file = "port_guids.list";
362 int mgmt_classes[2] = { IB_SMI_CLASS, IB_SMI_DIRECT_CLASS };
363 ib_portid_t dport_id;
371 srcport = mad_rpc_open_port(NULL, 0, mgmt_classes, 2);
373 err("Failed to open port");
375 resolve_sm_portid(NULL, 0, &dport_id);
378 dport_id.qkey = IB_DEFAULT_QP1_QKEY;
380 len = umad_size() + 256;
381 umad = calloc(1, len);
383 err("cannot alloc mem for umad: %s\n", strerror(errno));
386 port = mad_rpc_portid(srcport);
388 agent = umad_register(port, IB_SA_CLASS, 2, 0, NULL);
390 rereg_and_test_port(guid_file, port, agent, &dport_id, TMO);
391 mad = umad_get_mad(umad);
394 umad_unregister(port, agent);
395 umad_close_port(port);