xref: /freebsd/contrib/ofed/infiniband-diags/src/ibsendtrap.c (revision 87181516ef48be852d5e5fee53c6e0dbfc62f21e)
1 /*
2  * Copyright (c) 2008 Lawrence Livermore National Security
3  * Copyright (c) 2008-2009 Voltaire Inc.  All rights reserved.
4  * Copyright (c) 2009 HNR Consulting.  All rights reserved.
5  * Copyright (c) 2011 Mellanox Technologies LTD.  All rights reserved.
6  *
7  * Produced at Lawrence Livermore National Laboratory.
8  * Written by Ira Weiny <weiny2@llnl.gov>.
9  *
10  * This software is available to you under a choice of one of two
11  * licenses.  You may choose to be licensed under the terms of the GNU
12  * General Public License (GPL) Version 2, available from the file
13  * COPYING in the main directory of this source tree, or the
14  * OpenIB.org BSD license below:
15  *
16  *     Redistribution and use in source and binary forms, with or
17  *     without modification, are permitted provided that the following
18  *     conditions are met:
19  *
20  *      - Redistributions of source code must retain the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer.
23  *
24  *      - Redistributions in binary form must reproduce the above
25  *        copyright notice, this list of conditions and the following
26  *        disclaimer in the documentation and/or other materials
27  *        provided with the distribution.
28  *
29  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
33  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
34  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
35  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36  * SOFTWARE.
37  *
38  */
39 
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <unistd.h>
43 #include <string.h>
44 
45 #define _GNU_SOURCE
46 #include <getopt.h>
47 
48 #include <infiniband/mad.h>
49 #include <iba/ib_types.h>
50 
51 #include "ibdiag_common.h"
52 
53 struct ibmad_port *srcport;
54 /* for local link integrity */
55 int error_port = 1;
56 
get_node_type(ib_portid_t * port)57 static uint16_t get_node_type(ib_portid_t * port)
58 {
59 	uint16_t node_type = IB_NODE_TYPE_CA;
60 	uint8_t data[IB_SMP_DATA_SIZE] = { 0 };
61 
62 	if (smp_query_via(data, port, IB_ATTR_NODE_INFO, 0, 0, srcport))
63 		node_type = (uint16_t) mad_get_field(data, 0, IB_NODE_TYPE_F);
64 	return node_type;
65 }
66 
get_cap_mask(ib_portid_t * port)67 static uint32_t get_cap_mask(ib_portid_t * port)
68 {
69 	uint8_t data[IB_SMP_DATA_SIZE] = { 0 };
70 	uint32_t cap_mask = 0;
71 
72 	if (smp_query_via(data, port, IB_ATTR_PORT_INFO, 0, 0, srcport))
73 		cap_mask = (uint32_t) mad_get_field(data, 0, IB_PORT_CAPMASK_F);
74 	return cap_mask;
75 }
76 
build_trap145(ib_mad_notice_attr_t * n,ib_portid_t * port)77 static void build_trap145(ib_mad_notice_attr_t * n, ib_portid_t * port)
78 {
79 	n->generic_type = 0x80 | IB_NOTICE_TYPE_INFO;
80 	n->g_or_v.generic.prod_type_lsb = cl_hton16(get_node_type(port));
81 	n->g_or_v.generic.trap_num = cl_hton16(145);
82 	n->issuer_lid = cl_hton16((uint16_t) port->lid);
83 	n->data_details.ntc_145.new_sys_guid = cl_hton64(0x1234567812345678);
84 }
85 
build_trap144_local(ib_mad_notice_attr_t * n,ib_portid_t * port)86 static void build_trap144_local(ib_mad_notice_attr_t * n, ib_portid_t * port)
87 {
88 	n->generic_type = 0x80 | IB_NOTICE_TYPE_INFO;
89 	n->g_or_v.generic.prod_type_lsb = cl_hton16(get_node_type(port));
90 	n->g_or_v.generic.trap_num = cl_hton16(144);
91 	n->issuer_lid = cl_hton16((uint16_t) port->lid);
92 	n->data_details.ntc_144.lid = n->issuer_lid;
93 	n->data_details.ntc_144.new_cap_mask = cl_hton32(get_cap_mask(port));
94 	n->data_details.ntc_144.local_changes =
95 	    TRAP_144_MASK_OTHER_LOCAL_CHANGES;
96 }
97 
build_trap144_nodedesc(ib_mad_notice_attr_t * n,ib_portid_t * port)98 static void build_trap144_nodedesc(ib_mad_notice_attr_t * n, ib_portid_t * port)
99 {
100 	build_trap144_local(n, port);
101 	n->data_details.ntc_144.change_flgs =
102 	    TRAP_144_MASK_NODE_DESCRIPTION_CHANGE;
103 }
104 
build_trap144_linkspeed(ib_mad_notice_attr_t * n,ib_portid_t * port)105 static void build_trap144_linkspeed(ib_mad_notice_attr_t * n,
106 				    ib_portid_t * port)
107 {
108 	build_trap144_local(n, port);
109 	n->data_details.ntc_144.change_flgs =
110 	    TRAP_144_MASK_LINK_SPEED_ENABLE_CHANGE;
111 }
112 
build_trap129(ib_mad_notice_attr_t * n,ib_portid_t * port)113 static void build_trap129(ib_mad_notice_attr_t * n, ib_portid_t * port)
114 {
115 	n->generic_type = 0x80 | IB_NOTICE_TYPE_URGENT;
116 	n->g_or_v.generic.prod_type_lsb = cl_hton16(get_node_type(port));
117 	n->g_or_v.generic.trap_num = cl_hton16(129);
118 	n->issuer_lid = cl_hton16((uint16_t) port->lid);
119 	n->data_details.ntc_129_131.lid = n->issuer_lid;
120 	n->data_details.ntc_129_131.pad = 0;
121 	n->data_details.ntc_129_131.port_num = (uint8_t) error_port;
122 }
123 
build_trap256_local(ib_mad_notice_attr_t * n,ib_portid_t * port)124 static void build_trap256_local(ib_mad_notice_attr_t * n, ib_portid_t * port)
125 {
126 	n->generic_type = 0x80 | IB_NOTICE_TYPE_SECURITY;
127 	n->g_or_v.generic.prod_type_lsb = cl_hton16(get_node_type(port));
128 	n->g_or_v.generic.trap_num = cl_hton16(256);
129 	n->issuer_lid = cl_hton16((uint16_t) port->lid);
130 	n->data_details.ntc_256.lid = n->issuer_lid;
131 	n->data_details.ntc_256.dr_slid = 0xffff;
132 	n->data_details.ntc_256.method = 1;
133 	n->data_details.ntc_256.attr_id = cl_ntoh16(0x15);
134 	n->data_details.ntc_256.attr_mod = cl_ntoh32(0x12);
135 	n->data_details.ntc_256.mkey = cl_ntoh64(0x1234567812345678);
136 }
137 
build_trap256_lid(ib_mad_notice_attr_t * n,ib_portid_t * port)138 static void build_trap256_lid(ib_mad_notice_attr_t * n, ib_portid_t * port)
139 {
140 	build_trap256_local(n, port);
141 	n->data_details.ntc_256.dr_trunc_hop = 0;
142 }
143 
build_trap256_dr(ib_mad_notice_attr_t * n,ib_portid_t * port)144 static void build_trap256_dr(ib_mad_notice_attr_t * n, ib_portid_t * port)
145 {
146 	build_trap256_local(n, port);
147 	n->data_details.ntc_256.dr_trunc_hop = 0x80 | 0x4;
148 	n->data_details.ntc_256.dr_rtn_path[0] = 5;
149 	n->data_details.ntc_256.dr_rtn_path[1] = 6;
150 	n->data_details.ntc_256.dr_rtn_path[2] = 7;
151 	n->data_details.ntc_256.dr_rtn_path[3] = 8;
152 }
153 
build_trap257_258(ib_mad_notice_attr_t * n,ib_portid_t * port,uint16_t trap_num)154 static void build_trap257_258(ib_mad_notice_attr_t * n, ib_portid_t * port,
155 			      uint16_t trap_num)
156 {
157 	n->generic_type = 0x80 | IB_NOTICE_TYPE_SECURITY;
158 	n->g_or_v.generic.prod_type_lsb = cl_hton16(get_node_type(port));
159 	n->g_or_v.generic.trap_num = cl_hton16(trap_num);
160 	n->issuer_lid = cl_hton16((uint16_t) port->lid);
161 	n->data_details.ntc_257_258.lid1 = cl_hton16(1);
162 	n->data_details.ntc_257_258.lid2 = cl_hton16(2);
163 	n->data_details.ntc_257_258.key = cl_hton32(0x12345678);
164 	n->data_details.ntc_257_258.qp1 = cl_hton32(0x010101);
165 	n->data_details.ntc_257_258.qp2 = cl_hton32(0x020202);
166 	n->data_details.ntc_257_258.gid1.unicast.prefix = cl_ntoh64(0xf8c0000000000001);
167 	n->data_details.ntc_257_258.gid1.unicast.interface_id = cl_ntoh64(0x1111222233334444);
168 	n->data_details.ntc_257_258.gid2.unicast.prefix = cl_ntoh64(0xf8c0000000000001);
169 	n->data_details.ntc_257_258.gid2.unicast.interface_id = cl_ntoh64(0x5678567812341234);
170 }
171 
build_trap257(ib_mad_notice_attr_t * n,ib_portid_t * port)172 static void build_trap257(ib_mad_notice_attr_t * n, ib_portid_t * port)
173 {
174 	build_trap257_258(n, port, 257);
175 }
176 
build_trap258(ib_mad_notice_attr_t * n,ib_portid_t * port)177 static void build_trap258(ib_mad_notice_attr_t * n, ib_portid_t * port)
178 {
179 	build_trap257_258(n, port, 258);
180 }
181 
send_trap(void (* build)(ib_mad_notice_attr_t *,ib_portid_t *))182 static int send_trap(void (*build) (ib_mad_notice_attr_t *, ib_portid_t *))
183 {
184 	ib_portid_t sm_port;
185 	ib_portid_t selfportid;
186 	int selfport;
187 	ib_rpc_t trap_rpc;
188 	ib_mad_notice_attr_t notice;
189 
190 	if (resolve_self(ibd_ca, ibd_ca_port, &selfportid, &selfport, NULL))
191 		IBEXIT("can't resolve self");
192 
193 	if (resolve_sm_portid(ibd_ca, ibd_ca_port, &sm_port))
194 		IBEXIT("can't resolve SM destination port");
195 
196 	memset(&trap_rpc, 0, sizeof(trap_rpc));
197 	trap_rpc.mgtclass = IB_SMI_CLASS;
198 	trap_rpc.method = IB_MAD_METHOD_TRAP;
199 	trap_rpc.trid = mad_trid();
200 	trap_rpc.attr.id = NOTICE;
201 	trap_rpc.datasz = IB_SMP_DATA_SIZE;
202 	trap_rpc.dataoffs = IB_SMP_DATA_OFFS;
203 
204 	memset(&notice, 0, sizeof(notice));
205 	build(&notice, &selfportid);
206 
207 	return mad_send_via(&trap_rpc, &sm_port, NULL, &notice, srcport);
208 }
209 
210 typedef struct _trap_def {
211 	char *trap_name;
212 	void (*build_func) (ib_mad_notice_attr_t *, ib_portid_t *);
213 } trap_def_t;
214 
215 static const trap_def_t traps[] = {
216 	{"node_desc_change", build_trap144_nodedesc},
217 	{"link_speed_enabled_change", build_trap144_linkspeed},
218 	{"local_link_integrity", build_trap129},
219 	{"sys_image_guid_change", build_trap145},
220 	{"mkey_lid", build_trap256_lid},
221 	{"mkey_dr", build_trap256_dr},
222 	{"pkey", build_trap257},
223 	{"qkey", build_trap258},
224 	{NULL, NULL}
225 };
226 
process_send_trap(char * trap_name)227 int process_send_trap(char *trap_name)
228 {
229 	int i;
230 
231 	for (i = 0; traps[i].trap_name; i++)
232 		if (strcmp(traps[i].trap_name, trap_name) == 0)
233 			return send_trap(traps[i].build_func);
234 	ibdiag_show_usage();
235 	return 1;
236 }
237 
main(int argc,char ** argv)238 int main(int argc, char **argv)
239 {
240 	char usage_args[1024];
241 	int mgmt_classes[2] = { IB_SMI_CLASS, IB_SMI_DIRECT_CLASS };
242 	char *trap_name = NULL;
243 	int i, n, rc;
244 
245 	n = sprintf(usage_args, "[<trap_name>] [<error_port>]\n"
246 		    "\nArgument <trap_name> can be one of the following:\n");
247 	for (i = 0; traps[i].trap_name; i++) {
248 		n += snprintf(usage_args + n, sizeof(usage_args) - n,
249 			      "  %s\n", traps[i].trap_name);
250 		if (n >= sizeof(usage_args))
251 			exit(-1);
252 	}
253 	snprintf(usage_args + n, sizeof(usage_args) - n,
254 		 "\n  default behavior is to send \"%s\"", traps[0].trap_name);
255 
256 	ibdiag_process_opts(argc, argv, NULL, "DGKL", NULL, NULL,
257 			    usage_args, NULL);
258 
259 	argc -= optind;
260 	argv += optind;
261 
262 	trap_name = argv[0] ? argv[0] : traps[0].trap_name;
263 
264 	if (argc > 1)
265 		error_port = atoi(argv[1]);
266 
267 	madrpc_show_errors(1);
268 
269 	srcport = mad_rpc_open_port(ibd_ca, ibd_ca_port, mgmt_classes, 2);
270 	if (!srcport)
271 		IBEXIT("Failed to open '%s' port '%d'", ibd_ca, ibd_ca_port);
272 
273 	smp_mkey_set(srcport, ibd_mkey);
274 
275 	rc = process_send_trap(trap_name);
276 	mad_rpc_close_port(srcport);
277 	return rc;
278 }
279