xref: /freebsd/contrib/ofed/infiniband-diags/src/smpdump.c (revision 87181516ef48be852d5e5fee53c6e0dbfc62f21e)
1 /*
2  * Copyright (c) 2004-2009 Voltaire Inc.  All rights reserved.
3  *
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:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
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.
22  *
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
30  * SOFTWARE.
31  *
32  */
33 
34 #define _GNU_SOURCE
35 
36 #if HAVE_CONFIG_H
37 #  include <config.h>
38 #endif				/* HAVE_CONFIG_H */
39 
40 #include <inttypes.h>
41 #include <string.h>
42 #include <errno.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <getopt.h>
46 #include <netinet/in.h>
47 
48 #include <infiniband/mad.h>
49 #include <infiniband/umad.h>
50 
51 #include <ibdiag_common.h>
52 
53 static int mad_agent;
54 static int drmad_tid = 0x123;
55 
56 typedef struct {
57 	char path[64];
58 	int hop_cnt;
59 } DRPath;
60 
61 struct drsmp {
62 	uint8_t base_version;
63 	uint8_t mgmt_class;
64 	uint8_t class_version;
65 	uint8_t method;
66 	uint16_t status;
67 	uint8_t hop_ptr;
68 	uint8_t hop_cnt;
69 	uint64_t tid;
70 	uint16_t attr_id;
71 	uint16_t resv;
72 	uint32_t attr_mod;
73 	uint64_t mkey;
74 	uint16_t dr_slid;
75 	uint16_t dr_dlid;
76 	uint8_t reserved[28];
77 	uint8_t data[64];
78 	uint8_t initial_path[64];
79 	uint8_t return_path[64];
80 };
81 
drsmp_get_init(void * umad,DRPath * path,int attr,int mod)82 void drsmp_get_init(void *umad, DRPath * path, int attr, int mod)
83 {
84 	struct drsmp *smp = (struct drsmp *)(umad_get_mad(umad));
85 
86 	memset(smp, 0, sizeof(*smp));
87 
88 	smp->base_version = 1;
89 	smp->mgmt_class = IB_SMI_DIRECT_CLASS;
90 	smp->class_version = 1;
91 
92 	smp->method = 1;
93 	smp->attr_id = (uint16_t) htons((uint16_t) attr);
94 	smp->attr_mod = htonl(mod);
95 	smp->tid = htonll(drmad_tid++);
96 	smp->dr_slid = 0xffff;
97 	smp->dr_dlid = 0xffff;
98 
99 	umad_set_addr(umad, 0xffff, 0, 0, 0);
100 
101 	if (path)
102 		memcpy(smp->initial_path, path->path, path->hop_cnt + 1);
103 
104 	smp->hop_cnt = (uint8_t) path->hop_cnt;
105 }
106 
smp_get_init(void * umad,int lid,int attr,int mod)107 void smp_get_init(void *umad, int lid, int attr, int mod)
108 {
109 	struct drsmp *smp = (struct drsmp *)(umad_get_mad(umad));
110 
111 	memset(smp, 0, sizeof(*smp));
112 
113 	smp->base_version = 1;
114 	smp->mgmt_class = IB_SMI_CLASS;
115 	smp->class_version = 1;
116 
117 	smp->method = 1;
118 	smp->attr_id = (uint16_t) htons((uint16_t) attr);
119 	smp->attr_mod = htonl(mod);
120 	smp->tid = htonll(drmad_tid++);
121 
122 	umad_set_addr(umad, lid, 0, 0, 0);
123 }
124 
drsmp_set_init(void * umad,DRPath * path,int attr,int mod,void * data)125 void drsmp_set_init(void *umad, DRPath * path, int attr, int mod, void *data)
126 {
127 	struct drsmp *smp = (struct drsmp *)(umad_get_mad(umad));
128 
129 	memset(smp, 0, sizeof(*smp));
130 
131 	smp->method = 2;	/* SET */
132 	smp->attr_id = (uint16_t) htons((uint16_t) attr);
133 	smp->attr_mod = htonl(mod);
134 	smp->tid = htonll(drmad_tid++);
135 	smp->dr_slid = 0xffff;
136 	smp->dr_dlid = 0xffff;
137 
138 	umad_set_addr(umad, 0xffff, 0, 0, 0);
139 
140 	if (path)
141 		memcpy(smp->initial_path, path->path, path->hop_cnt + 1);
142 
143 	if (data)
144 		memcpy(smp->data, data, sizeof smp->data);
145 
146 	smp->hop_cnt = (uint8_t) path->hop_cnt;
147 }
148 
drmad_status_str(struct drsmp * drsmp)149 char *drmad_status_str(struct drsmp *drsmp)
150 {
151 	switch (drsmp->status) {
152 	case 0:
153 		return "success";
154 	case ETIMEDOUT:
155 		return "timeout";
156 	}
157 	return "unknown error";
158 }
159 
str2DRPath(char * str,DRPath * path)160 int str2DRPath(char *str, DRPath * path)
161 {
162 	char *s;
163 
164 	path->hop_cnt = -1;
165 
166 	DEBUG("DR str: %s", str);
167 	while (str && *str) {
168 		if ((s = strchr(str, ',')))
169 			*s = 0;
170 		path->path[++path->hop_cnt] = (char)atoi(str);
171 		if (!s)
172 			break;
173 		str = s + 1;
174 	}
175 
176 #if 0
177 	if (path->path[0] != 0 ||
178 	    (path->hop_cnt > 0 && dev_port && path->path[1] != dev_port)) {
179 		DEBUG("hop 0 != 0 or hop 1 != dev_port");
180 		return -1;
181 	}
182 #endif
183 
184 	return path->hop_cnt;
185 }
186 
187 static int dump_char, mgmt_class = IB_SMI_CLASS;
188 
process_opt(void * context,int ch,char * optarg)189 static int process_opt(void *context, int ch, char *optarg)
190 {
191 	switch (ch) {
192 	case 's':
193 		dump_char++;
194 		break;
195 	case 'D':
196 		mgmt_class = IB_SMI_DIRECT_CLASS;
197 		break;
198 	case 'L':
199 		mgmt_class = IB_SMI_CLASS;
200 		break;
201 	default:
202 		return -1;
203 	}
204 	return 0;
205 }
206 
207 #ifndef strdupa
208 #define	strdupa(_s)						\
209 ({								\
210 	char *_d;						\
211 	int _len;						\
212 								\
213 	_len = strlen(_s) + 1;					\
214 	_d = alloca(_len);					\
215 	if (_d)							\
216 		memcpy(_d, _s, _len);				\
217 	_d;							\
218 })
219 #endif
220 
main(int argc,char * argv[])221 int main(int argc, char *argv[])
222 {
223 	int dlid = 0;
224 	void *umad;
225 	struct drsmp *smp;
226 	int i, portid, mod = 0, attr;
227 	DRPath path;
228 	uint8_t *desc;
229 	int length;
230 
231 	const struct ibdiag_opt opts[] = {
232 		{"string", 's', 0, NULL, ""},
233 		{0}
234 	};
235 	char usage_args[] = "<dlid|dr_path> <attr> [mod]";
236 	const char *usage_examples[] = {
237 		" -- DR routed examples:",
238 		"-D 0,1,2,3,5 16	# NODE DESC",
239 		"-D 0,1,2 0x15 2	# PORT INFO, port 2",
240 		" -- LID routed examples:",
241 		"3 0x15 2	# PORT INFO, lid 3 port 2",
242 		"0xa0 0x11	# NODE INFO, lid 0xa0",
243 		NULL
244 	};
245 
246 	ibd_timeout = 1000;
247 
248 	ibdiag_process_opts(argc, argv, NULL, "GKs", opts, process_opt,
249 			    usage_args, usage_examples);
250 
251 	argc -= optind;
252 	argv += optind;
253 
254 	if (argc < 2)
255 		ibdiag_show_usage();
256 
257 	if (mgmt_class == IB_SMI_DIRECT_CLASS &&
258 	    str2DRPath(strdupa(argv[0]), &path) < 0)
259 		IBPANIC("bad path str '%s'", argv[0]);
260 
261 	if (mgmt_class == IB_SMI_CLASS)
262 		dlid = strtoul(argv[0], 0, 0);
263 
264 	attr = strtoul(argv[1], 0, 0);
265 	if (argc > 2)
266 		mod = strtoul(argv[2], 0, 0);
267 
268 	if (umad_init() < 0)
269 		IBPANIC("can't init UMAD library");
270 
271 	if ((portid = umad_open_port(ibd_ca, ibd_ca_port)) < 0)
272 		IBPANIC("can't open UMAD port (%s:%d)", ibd_ca, ibd_ca_port);
273 
274 	if ((mad_agent = umad_register(portid, mgmt_class, 1, 0, 0)) < 0)
275 		IBPANIC("Couldn't register agent for SMPs");
276 
277 	if (!(umad = umad_alloc(1, umad_size() + IB_MAD_SIZE)))
278 		IBPANIC("can't alloc MAD");
279 
280 	smp = umad_get_mad(umad);
281 
282 	if (mgmt_class == IB_SMI_DIRECT_CLASS)
283 		drsmp_get_init(umad, &path, attr, mod);
284 	else
285 		smp_get_init(umad, dlid, attr, mod);
286 
287 	if (ibdebug > 1)
288 		xdump(stderr, "before send:\n", smp, 256);
289 
290 	length = IB_MAD_SIZE;
291 	if (umad_send(portid, mad_agent, umad, length, ibd_timeout, 0) < 0)
292 		IBPANIC("send failed");
293 
294 	if (umad_recv(portid, umad, &length, -1) != mad_agent)
295 		IBPANIC("recv error: %s", drmad_status_str(smp));
296 
297 	if (!dump_char) {
298 		xdump(stdout, 0, smp->data, 64);
299 		if (smp->status)
300 			fprintf(stdout, "SMP status: 0x%x\n",
301 				ntohs(smp->status));
302 		goto exit;
303 	}
304 
305 	desc = smp->data;
306 	for (i = 0; i < 64; ++i) {
307 		if (!desc[i])
308 			break;
309 		putchar(desc[i]);
310 	}
311 	putchar('\n');
312 	if (smp->status)
313 		fprintf(stdout, "SMP status: 0x%x\n", ntohs(smp->status));
314 
315 exit:
316 	umad_free(umad);
317 	return 0;
318 }
319