xref: /freebsd/contrib/ofed/infiniband-diags/src/ibccconfig.c (revision 87181516ef48be852d5e5fee53c6e0dbfc62f21e)
1*d6b92ffaSHans Petter Selasky /*
2*d6b92ffaSHans Petter Selasky  * Copyright (c) 2004-2009 Voltaire Inc.  All rights reserved.
3*d6b92ffaSHans Petter Selasky  * Copyright (c) 2011 Mellanox Technologies LTD.  All rights reserved.
4*d6b92ffaSHans Petter Selasky  * Copyright (c) 2011 Lawrence Livermore National Lab.  All rights reserved.
5*d6b92ffaSHans Petter Selasky  *
6*d6b92ffaSHans Petter Selasky  * This software is available to you under a choice of one of two
7*d6b92ffaSHans Petter Selasky  * licenses.  You may choose to be licensed under the terms of the GNU
8*d6b92ffaSHans Petter Selasky  * General Public License (GPL) Version 2, available from the file
9*d6b92ffaSHans Petter Selasky  * COPYING in the main directory of this source tree, or the
10*d6b92ffaSHans Petter Selasky  * OpenIB.org BSD license below:
11*d6b92ffaSHans Petter Selasky  *
12*d6b92ffaSHans Petter Selasky  *     Redistribution and use in source and binary forms, with or
13*d6b92ffaSHans Petter Selasky  *     without modification, are permitted provided that the following
14*d6b92ffaSHans Petter Selasky  *     conditions are met:
15*d6b92ffaSHans Petter Selasky  *
16*d6b92ffaSHans Petter Selasky  *      - Redistributions of source code must retain the above
17*d6b92ffaSHans Petter Selasky  *        copyright notice, this list of conditions and the following
18*d6b92ffaSHans Petter Selasky  *        disclaimer.
19*d6b92ffaSHans Petter Selasky  *
20*d6b92ffaSHans Petter Selasky  *      - Redistributions in binary form must reproduce the above
21*d6b92ffaSHans Petter Selasky  *        copyright notice, this list of conditions and the following
22*d6b92ffaSHans Petter Selasky  *        disclaimer in the documentation and/or other materials
23*d6b92ffaSHans Petter Selasky  *        provided with the distribution.
24*d6b92ffaSHans Petter Selasky  *
25*d6b92ffaSHans Petter Selasky  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26*d6b92ffaSHans Petter Selasky  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27*d6b92ffaSHans Petter Selasky  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28*d6b92ffaSHans Petter Selasky  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29*d6b92ffaSHans Petter Selasky  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30*d6b92ffaSHans Petter Selasky  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31*d6b92ffaSHans Petter Selasky  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32*d6b92ffaSHans Petter Selasky  * SOFTWARE.
33*d6b92ffaSHans Petter Selasky  *
34*d6b92ffaSHans Petter Selasky  */
35*d6b92ffaSHans Petter Selasky 
36*d6b92ffaSHans Petter Selasky #if HAVE_CONFIG_H
37*d6b92ffaSHans Petter Selasky #  include <config.h>
38*d6b92ffaSHans Petter Selasky #endif				/* HAVE_CONFIG_H */
39*d6b92ffaSHans Petter Selasky 
40*d6b92ffaSHans Petter Selasky #include <stdio.h>
41*d6b92ffaSHans Petter Selasky #include <stdlib.h>
42*d6b92ffaSHans Petter Selasky #include <unistd.h>
43*d6b92ffaSHans Petter Selasky #include <string.h>
44*d6b92ffaSHans Petter Selasky #include <getopt.h>
45*d6b92ffaSHans Petter Selasky #include <errno.h>
46*d6b92ffaSHans Petter Selasky #include <netinet/in.h>
47*d6b92ffaSHans Petter Selasky #include <limits.h>
48*d6b92ffaSHans Petter Selasky #include <ctype.h>
49*d6b92ffaSHans Petter Selasky 
50*d6b92ffaSHans Petter Selasky #define __STDC_FORMAT_MACROS
51*d6b92ffaSHans Petter Selasky #include <inttypes.h>
52*d6b92ffaSHans Petter Selasky 
53*d6b92ffaSHans Petter Selasky #include <infiniband/mad.h>
54*d6b92ffaSHans Petter Selasky 
55*d6b92ffaSHans Petter Selasky #include "ibdiag_common.h"
56*d6b92ffaSHans Petter Selasky 
57*d6b92ffaSHans Petter Selasky struct ibmad_port *srcport;
58*d6b92ffaSHans Petter Selasky 
59*d6b92ffaSHans Petter Selasky static ibmad_gid_t dgid;
60*d6b92ffaSHans Petter Selasky static int with_grh;
61*d6b92ffaSHans Petter Selasky 
62*d6b92ffaSHans Petter Selasky static op_fn_t congestion_key_info;
63*d6b92ffaSHans Petter Selasky static op_fn_t switch_congestion_setting;
64*d6b92ffaSHans Petter Selasky static op_fn_t switch_port_congestion_setting;
65*d6b92ffaSHans Petter Selasky static op_fn_t ca_congestion_setting;
66*d6b92ffaSHans Petter Selasky static op_fn_t congestion_control_table;
67*d6b92ffaSHans Petter Selasky 
68*d6b92ffaSHans Petter Selasky static const match_rec_t match_tbl[] = {
69*d6b92ffaSHans Petter Selasky 	{"CongestionKeyInfo", "CK", congestion_key_info, 0,
70*d6b92ffaSHans Petter Selasky 	 "<cckey> <cckeyprotectbit> <cckeyleaseperiod> <cckeyviolations>"},
71*d6b92ffaSHans Petter Selasky 	{"SwitchCongestionSetting", "SS", switch_congestion_setting, 0,
72*d6b92ffaSHans Petter Selasky 	 "<controlmap> <victimmask> <creditmask> <threshold> <packetsize> "
73*d6b92ffaSHans Petter Selasky 	 "<csthreshold> <csreturndelay> <markingrate>"},
74*d6b92ffaSHans Petter Selasky 	{"SwitchPortCongestionSetting", "SP", switch_port_congestion_setting, 1,
75*d6b92ffaSHans Petter Selasky 	 "<valid> <control_type> <threshold> <packet_size> <cong_parm_marking_rate>"},
76*d6b92ffaSHans Petter Selasky 	{"CACongestionSetting", "CS", ca_congestion_setting, 0,
77*d6b92ffaSHans Petter Selasky 	 "<port_control> <control_map> <ccti_timer> <ccti_increase> "
78*d6b92ffaSHans Petter Selasky 	 "<trigger_threshold> <ccti_min>"},
79*d6b92ffaSHans Petter Selasky 	{"CongestionControlTable", "CT", congestion_control_table, 0,
80*d6b92ffaSHans Petter Selasky 	 "<cctilimit> <index> <cctentry> <cctentry> ..."},
81*d6b92ffaSHans Petter Selasky 	{0}
82*d6b92ffaSHans Petter Selasky };
83*d6b92ffaSHans Petter Selasky 
84*d6b92ffaSHans Petter Selasky uint64_t cckey = 0;
85*d6b92ffaSHans Petter Selasky 
86*d6b92ffaSHans Petter Selasky /*******************************************/
parselonglongint(char * arg,uint64_t * val)87*d6b92ffaSHans Petter Selasky static char *parselonglongint(char *arg, uint64_t *val)
88*d6b92ffaSHans Petter Selasky {
89*d6b92ffaSHans Petter Selasky 	char *endptr = NULL;
90*d6b92ffaSHans Petter Selasky 
91*d6b92ffaSHans Petter Selasky 	errno = 0;
92*d6b92ffaSHans Petter Selasky 	*val = strtoull(arg, &endptr, 0);
93*d6b92ffaSHans Petter Selasky 	if ((endptr && *endptr != '\0')
94*d6b92ffaSHans Petter Selasky 	    || errno != 0) {
95*d6b92ffaSHans Petter Selasky 		if (errno == ERANGE)
96*d6b92ffaSHans Petter Selasky 			return "value out of range";
97*d6b92ffaSHans Petter Selasky 		return "invalid integer input";
98*d6b92ffaSHans Petter Selasky 	}
99*d6b92ffaSHans Petter Selasky 
100*d6b92ffaSHans Petter Selasky 	return NULL;
101*d6b92ffaSHans Petter Selasky }
102*d6b92ffaSHans Petter Selasky 
parseint(char * arg,uint32_t * val,int hexonly)103*d6b92ffaSHans Petter Selasky static char *parseint(char *arg, uint32_t *val, int hexonly)
104*d6b92ffaSHans Petter Selasky {
105*d6b92ffaSHans Petter Selasky 	char *endptr = NULL;
106*d6b92ffaSHans Petter Selasky 
107*d6b92ffaSHans Petter Selasky 	errno = 0;
108*d6b92ffaSHans Petter Selasky 	*val = strtoul(arg, &endptr, hexonly ? 16 : 0);
109*d6b92ffaSHans Petter Selasky 	if ((endptr && *endptr != '\0')
110*d6b92ffaSHans Petter Selasky 	    || errno != 0) {
111*d6b92ffaSHans Petter Selasky 		if (errno == ERANGE)
112*d6b92ffaSHans Petter Selasky 			return "value out of range";
113*d6b92ffaSHans Petter Selasky 		return "invalid integer input";
114*d6b92ffaSHans Petter Selasky 	}
115*d6b92ffaSHans Petter Selasky 
116*d6b92ffaSHans Petter Selasky 	return NULL;
117*d6b92ffaSHans Petter Selasky }
118*d6b92ffaSHans Petter Selasky 
congestion_key_info(ib_portid_t * dest,char ** argv,int argc)119*d6b92ffaSHans Petter Selasky static char *congestion_key_info(ib_portid_t * dest, char **argv, int argc)
120*d6b92ffaSHans Petter Selasky {
121*d6b92ffaSHans Petter Selasky 	uint8_t rcv[IB_CC_DATA_SZ] = { 0 };
122*d6b92ffaSHans Petter Selasky 	uint8_t payload[IB_CC_DATA_SZ] = { 0 };
123*d6b92ffaSHans Petter Selasky 	uint64_t cc_key;
124*d6b92ffaSHans Petter Selasky 	uint32_t cc_keyprotectbit;
125*d6b92ffaSHans Petter Selasky 	uint32_t cc_keyleaseperiod;
126*d6b92ffaSHans Petter Selasky 	uint32_t cc_keyviolations;
127*d6b92ffaSHans Petter Selasky 	char *errstr;
128*d6b92ffaSHans Petter Selasky 
129*d6b92ffaSHans Petter Selasky 	if (argc != 4)
130*d6b92ffaSHans Petter Selasky 		return "invalid number of parameters for CongestionKeyInfo";
131*d6b92ffaSHans Petter Selasky 
132*d6b92ffaSHans Petter Selasky 	if ((errstr = parselonglongint(argv[0], &cc_key)))
133*d6b92ffaSHans Petter Selasky 		return errstr;
134*d6b92ffaSHans Petter Selasky 	if ((errstr = parseint(argv[1], &cc_keyprotectbit, 0)))
135*d6b92ffaSHans Petter Selasky 		return errstr;
136*d6b92ffaSHans Petter Selasky 	if ((errstr = parseint(argv[2], &cc_keyleaseperiod, 0)))
137*d6b92ffaSHans Petter Selasky 		return errstr;
138*d6b92ffaSHans Petter Selasky 	if ((errstr = parseint(argv[3], &cc_keyviolations, 0)))
139*d6b92ffaSHans Petter Selasky 		return errstr;
140*d6b92ffaSHans Petter Selasky 
141*d6b92ffaSHans Petter Selasky 	if (cc_keyprotectbit != 0 && cc_keyprotectbit != 1)
142*d6b92ffaSHans Petter Selasky 		return "invalid cc_keyprotectbit value";
143*d6b92ffaSHans Petter Selasky 
144*d6b92ffaSHans Petter Selasky 	if (cc_keyleaseperiod > USHRT_MAX)
145*d6b92ffaSHans Petter Selasky 		return "invalid cc_keyleaseperiod value";
146*d6b92ffaSHans Petter Selasky 
147*d6b92ffaSHans Petter Selasky 	if (cc_keyviolations > USHRT_MAX)
148*d6b92ffaSHans Petter Selasky 		return "invalid cc_keyviolations value";
149*d6b92ffaSHans Petter Selasky 
150*d6b92ffaSHans Petter Selasky 	mad_set_field64(payload,
151*d6b92ffaSHans Petter Selasky 			0,
152*d6b92ffaSHans Petter Selasky 			IB_CC_CONGESTION_KEY_INFO_CC_KEY_F,
153*d6b92ffaSHans Petter Selasky 			cc_key);
154*d6b92ffaSHans Petter Selasky 
155*d6b92ffaSHans Petter Selasky 	mad_encode_field(payload,
156*d6b92ffaSHans Petter Selasky 			 IB_CC_CONGESTION_KEY_INFO_CC_KEY_PROTECT_BIT_F,
157*d6b92ffaSHans Petter Selasky 			 &cc_keyprotectbit);
158*d6b92ffaSHans Petter Selasky 
159*d6b92ffaSHans Petter Selasky 	mad_encode_field(payload,
160*d6b92ffaSHans Petter Selasky 			 IB_CC_CONGESTION_KEY_INFO_CC_KEY_LEASE_PERIOD_F,
161*d6b92ffaSHans Petter Selasky 			 &cc_keyleaseperiod);
162*d6b92ffaSHans Petter Selasky 
163*d6b92ffaSHans Petter Selasky 	/* spec says "setting the counter to a value other than zero results
164*d6b92ffaSHans Petter Selasky 	 * in the counter being left unchanged.  So if user wants no change,
165*d6b92ffaSHans Petter Selasky 	 * they gotta input non-zero
166*d6b92ffaSHans Petter Selasky 	 */
167*d6b92ffaSHans Petter Selasky         mad_encode_field(payload,
168*d6b92ffaSHans Petter Selasky 			 IB_CC_CONGESTION_KEY_INFO_CC_KEY_VIOLATIONS_F,
169*d6b92ffaSHans Petter Selasky 			 &cc_keyviolations);
170*d6b92ffaSHans Petter Selasky 
171*d6b92ffaSHans Petter Selasky 	if (!cc_config_status_via(payload, rcv, dest, IB_CC_ATTR_CONGESTION_KEY_INFO,
172*d6b92ffaSHans Petter Selasky 				  0, 0, NULL, srcport, cckey))
173*d6b92ffaSHans Petter Selasky 		return "congestion key info config failed";
174*d6b92ffaSHans Petter Selasky 
175*d6b92ffaSHans Petter Selasky 	return NULL;
176*d6b92ffaSHans Petter Selasky }
177*d6b92ffaSHans Petter Selasky 
178*d6b92ffaSHans Petter Selasky 
179*d6b92ffaSHans Petter Selasky /* parse like it's a hypothetical 256 bit hex code */
parse256(char * arg,uint8_t * buf)180*d6b92ffaSHans Petter Selasky static char *parse256(char *arg, uint8_t *buf)
181*d6b92ffaSHans Petter Selasky {
182*d6b92ffaSHans Petter Selasky 	int numdigits = 0;
183*d6b92ffaSHans Petter Selasky 	int startindex;
184*d6b92ffaSHans Petter Selasky 	char *ptr;
185*d6b92ffaSHans Petter Selasky 	int i;
186*d6b92ffaSHans Petter Selasky 
187*d6b92ffaSHans Petter Selasky 	if (!strncmp(arg, "0x", 2) || !strncmp(arg, "0X", 2))
188*d6b92ffaSHans Petter Selasky 		arg += 2;
189*d6b92ffaSHans Petter Selasky 
190*d6b92ffaSHans Petter Selasky 	for (ptr = arg; *ptr; ptr++) {
191*d6b92ffaSHans Petter Selasky 		if (!isxdigit(*ptr))
192*d6b92ffaSHans Petter Selasky 			return "invalid hex digit read";
193*d6b92ffaSHans Petter Selasky 		numdigits++;
194*d6b92ffaSHans Petter Selasky 	}
195*d6b92ffaSHans Petter Selasky 
196*d6b92ffaSHans Petter Selasky 	if (numdigits > 64)
197*d6b92ffaSHans Petter Selasky 		return "hex code too long";
198*d6b92ffaSHans Petter Selasky 
199*d6b92ffaSHans Petter Selasky 	/* we need to imagine that this is like a 256-bit int stored
200*d6b92ffaSHans Petter Selasky 	 * in big endian.  So we need to find the first index
201*d6b92ffaSHans Petter Selasky 	 * point where the user's input would start in our array.
202*d6b92ffaSHans Petter Selasky 	 */
203*d6b92ffaSHans Petter Selasky 	startindex = 32 - ((numdigits - 1) / 2) - 1;
204*d6b92ffaSHans Petter Selasky 
205*d6b92ffaSHans Petter Selasky 	for (i = startindex; i <= 31; i++) {
206*d6b92ffaSHans Petter Selasky 		char tmp[3] = { 0 };
207*d6b92ffaSHans Petter Selasky 		uint32_t tmpint;
208*d6b92ffaSHans Petter Selasky 		char *errstr;
209*d6b92ffaSHans Petter Selasky 
210*d6b92ffaSHans Petter Selasky 		/* I can't help but think there is a strtoX that
211*d6b92ffaSHans Petter Selasky 		 * will do this for me, but I can't find it.
212*d6b92ffaSHans Petter Selasky 		 */
213*d6b92ffaSHans Petter Selasky 		if (i == startindex && numdigits % 2) {
214*d6b92ffaSHans Petter Selasky 			memcpy(tmp, arg, 1);
215*d6b92ffaSHans Petter Selasky 			arg++;
216*d6b92ffaSHans Petter Selasky 		}
217*d6b92ffaSHans Petter Selasky 		else {
218*d6b92ffaSHans Petter Selasky 			memcpy(tmp, arg, 2);
219*d6b92ffaSHans Petter Selasky 			arg += 2;
220*d6b92ffaSHans Petter Selasky 		}
221*d6b92ffaSHans Petter Selasky 
222*d6b92ffaSHans Petter Selasky 		if ((errstr = parseint(tmp, &tmpint, 1)))
223*d6b92ffaSHans Petter Selasky 			return errstr;
224*d6b92ffaSHans Petter Selasky 		buf[i] = tmpint;
225*d6b92ffaSHans Petter Selasky 	}
226*d6b92ffaSHans Petter Selasky 
227*d6b92ffaSHans Petter Selasky 	return NULL;
228*d6b92ffaSHans Petter Selasky }
229*d6b92ffaSHans Petter Selasky 
parsecct(char * arg,uint32_t * shift,uint32_t * multiplier)230*d6b92ffaSHans Petter Selasky static char *parsecct(char *arg, uint32_t *shift, uint32_t *multiplier)
231*d6b92ffaSHans Petter Selasky {
232*d6b92ffaSHans Petter Selasky 	char buf[1024] = { 0 };
233*d6b92ffaSHans Petter Selasky 	char *errstr;
234*d6b92ffaSHans Petter Selasky 	char *ptr;
235*d6b92ffaSHans Petter Selasky 
236*d6b92ffaSHans Petter Selasky 	strcpy(buf, arg);
237*d6b92ffaSHans Petter Selasky 
238*d6b92ffaSHans Petter Selasky 	if (!(ptr = strchr(buf, ':')))
239*d6b92ffaSHans Petter Selasky 		return "ccts are formatted shift:multiplier";
240*d6b92ffaSHans Petter Selasky 
241*d6b92ffaSHans Petter Selasky 	*ptr = '\0';
242*d6b92ffaSHans Petter Selasky 	ptr++;
243*d6b92ffaSHans Petter Selasky 
244*d6b92ffaSHans Petter Selasky 	if ((errstr = parseint(buf, shift, 0)))
245*d6b92ffaSHans Petter Selasky 		return errstr;
246*d6b92ffaSHans Petter Selasky 
247*d6b92ffaSHans Petter Selasky 	if ((errstr = parseint(ptr, multiplier, 0)))
248*d6b92ffaSHans Petter Selasky 		return errstr;
249*d6b92ffaSHans Petter Selasky 
250*d6b92ffaSHans Petter Selasky 	return NULL;
251*d6b92ffaSHans Petter Selasky }
252*d6b92ffaSHans Petter Selasky 
switch_congestion_setting(ib_portid_t * dest,char ** argv,int argc)253*d6b92ffaSHans Petter Selasky static char *switch_congestion_setting(ib_portid_t * dest, char **argv, int argc)
254*d6b92ffaSHans Petter Selasky {
255*d6b92ffaSHans Petter Selasky 	uint8_t rcv[IB_CC_DATA_SZ] = { 0 };
256*d6b92ffaSHans Petter Selasky 	uint8_t payload[IB_CC_DATA_SZ] = { 0 };
257*d6b92ffaSHans Petter Selasky 	uint32_t control_map;
258*d6b92ffaSHans Petter Selasky 	uint8_t victim_mask[32] = { 0 };
259*d6b92ffaSHans Petter Selasky 	uint8_t credit_mask[32] = { 0 };
260*d6b92ffaSHans Petter Selasky 	uint32_t threshold;
261*d6b92ffaSHans Petter Selasky 	uint32_t packet_size;
262*d6b92ffaSHans Petter Selasky 	uint32_t cs_threshold;
263*d6b92ffaSHans Petter Selasky 	uint32_t cs_returndelay_s;
264*d6b92ffaSHans Petter Selasky 	uint32_t cs_returndelay_m;
265*d6b92ffaSHans Petter Selasky 	uint32_t cs_returndelay;
266*d6b92ffaSHans Petter Selasky 	uint32_t marking_rate;
267*d6b92ffaSHans Petter Selasky 	char *errstr;
268*d6b92ffaSHans Petter Selasky 
269*d6b92ffaSHans Petter Selasky 	if (argc != 8)
270*d6b92ffaSHans Petter Selasky 		return "invalid number of parameters for SwitchCongestionSetting";
271*d6b92ffaSHans Petter Selasky 
272*d6b92ffaSHans Petter Selasky 	if ((errstr = parseint(argv[0], &control_map, 0)))
273*d6b92ffaSHans Petter Selasky 		return errstr;
274*d6b92ffaSHans Petter Selasky 
275*d6b92ffaSHans Petter Selasky 	if ((errstr = parse256(argv[1], victim_mask)))
276*d6b92ffaSHans Petter Selasky 		return errstr;
277*d6b92ffaSHans Petter Selasky 
278*d6b92ffaSHans Petter Selasky 	if ((errstr = parse256(argv[2], credit_mask)))
279*d6b92ffaSHans Petter Selasky 		return errstr;
280*d6b92ffaSHans Petter Selasky 
281*d6b92ffaSHans Petter Selasky 	if ((errstr = parseint(argv[3], &threshold, 0)))
282*d6b92ffaSHans Petter Selasky 		return errstr;
283*d6b92ffaSHans Petter Selasky 
284*d6b92ffaSHans Petter Selasky 	if ((errstr = parseint(argv[4], &packet_size, 0)))
285*d6b92ffaSHans Petter Selasky 		return errstr;
286*d6b92ffaSHans Petter Selasky 
287*d6b92ffaSHans Petter Selasky 	if ((errstr = parseint(argv[5], &cs_threshold, 0)))
288*d6b92ffaSHans Petter Selasky 		return errstr;
289*d6b92ffaSHans Petter Selasky 
290*d6b92ffaSHans Petter Selasky 	if ((errstr = parsecct(argv[6], &cs_returndelay_s, &cs_returndelay_m)))
291*d6b92ffaSHans Petter Selasky 		return errstr;
292*d6b92ffaSHans Petter Selasky 
293*d6b92ffaSHans Petter Selasky 	cs_returndelay = cs_returndelay_m;
294*d6b92ffaSHans Petter Selasky 	cs_returndelay |= (cs_returndelay_s << 14);
295*d6b92ffaSHans Petter Selasky 
296*d6b92ffaSHans Petter Selasky 	if ((errstr = parseint(argv[7], &marking_rate, 0)))
297*d6b92ffaSHans Petter Selasky 		return errstr;
298*d6b92ffaSHans Petter Selasky 
299*d6b92ffaSHans Petter Selasky 	mad_encode_field(payload,
300*d6b92ffaSHans Petter Selasky 			 IB_CC_SWITCH_CONGESTION_SETTING_CONTROL_MAP_F,
301*d6b92ffaSHans Petter Selasky 			 &control_map);
302*d6b92ffaSHans Petter Selasky 
303*d6b92ffaSHans Petter Selasky 	mad_set_array(payload,
304*d6b92ffaSHans Petter Selasky 		      0,
305*d6b92ffaSHans Petter Selasky 		      IB_CC_SWITCH_CONGESTION_SETTING_VICTIM_MASK_F,
306*d6b92ffaSHans Petter Selasky 		      victim_mask);
307*d6b92ffaSHans Petter Selasky 
308*d6b92ffaSHans Petter Selasky 	mad_set_array(payload,
309*d6b92ffaSHans Petter Selasky 		      0,
310*d6b92ffaSHans Petter Selasky 		      IB_CC_SWITCH_CONGESTION_SETTING_CREDIT_MASK_F,
311*d6b92ffaSHans Petter Selasky 		      credit_mask);
312*d6b92ffaSHans Petter Selasky 
313*d6b92ffaSHans Petter Selasky 	mad_encode_field(payload,
314*d6b92ffaSHans Petter Selasky 			 IB_CC_SWITCH_CONGESTION_SETTING_THRESHOLD_F,
315*d6b92ffaSHans Petter Selasky 			 &threshold);
316*d6b92ffaSHans Petter Selasky 
317*d6b92ffaSHans Petter Selasky 	mad_encode_field(payload,
318*d6b92ffaSHans Petter Selasky 			 IB_CC_SWITCH_CONGESTION_SETTING_PACKET_SIZE_F,
319*d6b92ffaSHans Petter Selasky 			 &packet_size);
320*d6b92ffaSHans Petter Selasky 
321*d6b92ffaSHans Petter Selasky 	mad_encode_field(payload,
322*d6b92ffaSHans Petter Selasky 			 IB_CC_SWITCH_CONGESTION_SETTING_CS_THRESHOLD_F,
323*d6b92ffaSHans Petter Selasky 			 &cs_threshold);
324*d6b92ffaSHans Petter Selasky 
325*d6b92ffaSHans Petter Selasky 	mad_encode_field(payload,
326*d6b92ffaSHans Petter Selasky 			 IB_CC_SWITCH_CONGESTION_SETTING_CS_RETURN_DELAY_F,
327*d6b92ffaSHans Petter Selasky 			 &cs_returndelay);
328*d6b92ffaSHans Petter Selasky 
329*d6b92ffaSHans Petter Selasky 	mad_encode_field(payload,
330*d6b92ffaSHans Petter Selasky 			 IB_CC_SWITCH_CONGESTION_SETTING_MARKING_RATE_F,
331*d6b92ffaSHans Petter Selasky 			 &marking_rate);
332*d6b92ffaSHans Petter Selasky 
333*d6b92ffaSHans Petter Selasky 	if (!cc_config_status_via(payload, rcv, dest, IB_CC_ATTR_SWITCH_CONGESTION_SETTING,
334*d6b92ffaSHans Petter Selasky 				  0, 0, NULL, srcport, cckey))
335*d6b92ffaSHans Petter Selasky 		return "switch congestion setting config failed";
336*d6b92ffaSHans Petter Selasky 
337*d6b92ffaSHans Petter Selasky 	return NULL;
338*d6b92ffaSHans Petter Selasky }
339*d6b92ffaSHans Petter Selasky 
switch_port_congestion_setting(ib_portid_t * dest,char ** argv,int argc)340*d6b92ffaSHans Petter Selasky static char *switch_port_congestion_setting(ib_portid_t * dest, char **argv, int argc)
341*d6b92ffaSHans Petter Selasky {
342*d6b92ffaSHans Petter Selasky 	uint8_t rcv[IB_CC_DATA_SZ] = { 0 };
343*d6b92ffaSHans Petter Selasky 	uint8_t payload[IB_CC_DATA_SZ] = { 0 };
344*d6b92ffaSHans Petter Selasky 	uint8_t data[IB_CC_DATA_SZ] = { 0 };
345*d6b92ffaSHans Petter Selasky 	uint32_t portnum;
346*d6b92ffaSHans Petter Selasky 	uint32_t valid;
347*d6b92ffaSHans Petter Selasky 	uint32_t control_type;
348*d6b92ffaSHans Petter Selasky 	uint32_t threshold;
349*d6b92ffaSHans Petter Selasky 	uint32_t packet_size;
350*d6b92ffaSHans Petter Selasky 	uint32_t cong_parm_marking_rate;
351*d6b92ffaSHans Petter Selasky 	uint32_t type;
352*d6b92ffaSHans Petter Selasky 	uint32_t numports;
353*d6b92ffaSHans Petter Selasky 	uint8_t *ptr;
354*d6b92ffaSHans Petter Selasky 	char *errstr;
355*d6b92ffaSHans Petter Selasky 
356*d6b92ffaSHans Petter Selasky 	if (argc != 6)
357*d6b92ffaSHans Petter Selasky 		return "invalid number of parameters for SwitchPortCongestion";
358*d6b92ffaSHans Petter Selasky 
359*d6b92ffaSHans Petter Selasky 	if ((errstr = parseint(argv[0], &portnum, 0)))
360*d6b92ffaSHans Petter Selasky 		return errstr;
361*d6b92ffaSHans Petter Selasky 
362*d6b92ffaSHans Petter Selasky 	if ((errstr = parseint(argv[1], &valid, 0)))
363*d6b92ffaSHans Petter Selasky 		return errstr;
364*d6b92ffaSHans Petter Selasky 
365*d6b92ffaSHans Petter Selasky 	if ((errstr = parseint(argv[2], &control_type, 0)))
366*d6b92ffaSHans Petter Selasky 		return errstr;
367*d6b92ffaSHans Petter Selasky 
368*d6b92ffaSHans Petter Selasky 	if ((errstr = parseint(argv[3], &threshold, 0)))
369*d6b92ffaSHans Petter Selasky 		return errstr;
370*d6b92ffaSHans Petter Selasky 
371*d6b92ffaSHans Petter Selasky 	if ((errstr = parseint(argv[4], &packet_size, 0)))
372*d6b92ffaSHans Petter Selasky 		return errstr;
373*d6b92ffaSHans Petter Selasky 
374*d6b92ffaSHans Petter Selasky 	if ((errstr = parseint(argv[5], &cong_parm_marking_rate, 0)))
375*d6b92ffaSHans Petter Selasky 		return errstr;
376*d6b92ffaSHans Petter Selasky 
377*d6b92ffaSHans Petter Selasky 	/* Figure out number of ports first */
378*d6b92ffaSHans Petter Selasky 	if (!smp_query_via(data, dest, IB_ATTR_NODE_INFO, 0, 0, srcport))
379*d6b92ffaSHans Petter Selasky 		return "node info config failed";
380*d6b92ffaSHans Petter Selasky 
381*d6b92ffaSHans Petter Selasky 	mad_decode_field((uint8_t *)data, IB_NODE_TYPE_F, &type);
382*d6b92ffaSHans Petter Selasky 	mad_decode_field((uint8_t *)data, IB_NODE_NPORTS_F, &numports);
383*d6b92ffaSHans Petter Selasky 
384*d6b92ffaSHans Petter Selasky 	if (type != IB_NODE_SWITCH)
385*d6b92ffaSHans Petter Selasky 		return "destination not a switch";
386*d6b92ffaSHans Petter Selasky 
387*d6b92ffaSHans Petter Selasky 	if (portnum > numports)
388*d6b92ffaSHans Petter Selasky 		return "invalid port number specified";
389*d6b92ffaSHans Petter Selasky 
390*d6b92ffaSHans Petter Selasky 	/* We are modifying only 1 port, so get the current config */
391*d6b92ffaSHans Petter Selasky 	if (!cc_query_status_via(payload, dest, IB_CC_ATTR_SWITCH_PORT_CONGESTION_SETTING,
392*d6b92ffaSHans Petter Selasky 				 portnum / 32, 0, NULL, srcport, cckey))
393*d6b92ffaSHans Petter Selasky 		return "switch port congestion setting query failed";
394*d6b92ffaSHans Petter Selasky 
395*d6b92ffaSHans Petter Selasky 	ptr = payload + (((portnum % 32) * 4));
396*d6b92ffaSHans Petter Selasky 
397*d6b92ffaSHans Petter Selasky 	mad_encode_field(ptr,
398*d6b92ffaSHans Petter Selasky 			 IB_CC_SWITCH_PORT_CONGESTION_SETTING_ELEMENT_VALID_F,
399*d6b92ffaSHans Petter Selasky 			 &valid);
400*d6b92ffaSHans Petter Selasky 
401*d6b92ffaSHans Petter Selasky 	mad_encode_field(ptr,
402*d6b92ffaSHans Petter Selasky 			 IB_CC_SWITCH_PORT_CONGESTION_SETTING_ELEMENT_CONTROL_TYPE_F,
403*d6b92ffaSHans Petter Selasky 			 &control_type);
404*d6b92ffaSHans Petter Selasky 
405*d6b92ffaSHans Petter Selasky 	mad_encode_field(ptr,
406*d6b92ffaSHans Petter Selasky 			 IB_CC_SWITCH_PORT_CONGESTION_SETTING_ELEMENT_THRESHOLD_F,
407*d6b92ffaSHans Petter Selasky 			 &threshold);
408*d6b92ffaSHans Petter Selasky 
409*d6b92ffaSHans Petter Selasky 	mad_encode_field(ptr,
410*d6b92ffaSHans Petter Selasky 			 IB_CC_SWITCH_PORT_CONGESTION_SETTING_ELEMENT_PACKET_SIZE_F,
411*d6b92ffaSHans Petter Selasky 			 &packet_size);
412*d6b92ffaSHans Petter Selasky 
413*d6b92ffaSHans Petter Selasky 	mad_encode_field(ptr,
414*d6b92ffaSHans Petter Selasky 			 IB_CC_SWITCH_PORT_CONGESTION_SETTING_ELEMENT_CONG_PARM_MARKING_RATE_F,
415*d6b92ffaSHans Petter Selasky 			 &cong_parm_marking_rate);
416*d6b92ffaSHans Petter Selasky 
417*d6b92ffaSHans Petter Selasky 	if (!cc_config_status_via(payload, rcv, dest, IB_CC_ATTR_SWITCH_PORT_CONGESTION_SETTING,
418*d6b92ffaSHans Petter Selasky 				  portnum / 32, 0, NULL, srcport, cckey))
419*d6b92ffaSHans Petter Selasky 		return "switch port congestion setting config failed";
420*d6b92ffaSHans Petter Selasky 
421*d6b92ffaSHans Petter Selasky 	return NULL;
422*d6b92ffaSHans Petter Selasky }
423*d6b92ffaSHans Petter Selasky 
ca_congestion_setting(ib_portid_t * dest,char ** argv,int argc)424*d6b92ffaSHans Petter Selasky static char *ca_congestion_setting(ib_portid_t * dest, char **argv, int argc)
425*d6b92ffaSHans Petter Selasky {
426*d6b92ffaSHans Petter Selasky 	uint8_t rcv[IB_CC_DATA_SZ] = { 0 };
427*d6b92ffaSHans Petter Selasky 	uint8_t payload[IB_CC_DATA_SZ] = { 0 };
428*d6b92ffaSHans Petter Selasky 	uint32_t port_control;
429*d6b92ffaSHans Petter Selasky 	uint32_t control_map;
430*d6b92ffaSHans Petter Selasky 	uint32_t ccti_timer;
431*d6b92ffaSHans Petter Selasky 	uint32_t ccti_increase;
432*d6b92ffaSHans Petter Selasky 	uint32_t trigger_threshold;
433*d6b92ffaSHans Petter Selasky 	uint32_t ccti_min;
434*d6b92ffaSHans Petter Selasky 	char *errstr;
435*d6b92ffaSHans Petter Selasky 	int i;
436*d6b92ffaSHans Petter Selasky 
437*d6b92ffaSHans Petter Selasky 	if (argc != 6)
438*d6b92ffaSHans Petter Selasky 		return "invalid number of parameters for CACongestionSetting";
439*d6b92ffaSHans Petter Selasky 
440*d6b92ffaSHans Petter Selasky 	if ((errstr = parseint(argv[0], &port_control, 0)))
441*d6b92ffaSHans Petter Selasky 		return errstr;
442*d6b92ffaSHans Petter Selasky 
443*d6b92ffaSHans Petter Selasky 	if ((errstr = parseint(argv[1], &control_map, 0)))
444*d6b92ffaSHans Petter Selasky 		return errstr;
445*d6b92ffaSHans Petter Selasky 
446*d6b92ffaSHans Petter Selasky 	if ((errstr = parseint(argv[2], &ccti_timer, 0)))
447*d6b92ffaSHans Petter Selasky 		return errstr;
448*d6b92ffaSHans Petter Selasky 
449*d6b92ffaSHans Petter Selasky 	if ((errstr = parseint(argv[3], &ccti_increase, 0)))
450*d6b92ffaSHans Petter Selasky 		return errstr;
451*d6b92ffaSHans Petter Selasky 
452*d6b92ffaSHans Petter Selasky 	if ((errstr = parseint(argv[4], &trigger_threshold, 0)))
453*d6b92ffaSHans Petter Selasky 		return errstr;
454*d6b92ffaSHans Petter Selasky 
455*d6b92ffaSHans Petter Selasky 	if ((errstr = parseint(argv[5], &ccti_min, 0)))
456*d6b92ffaSHans Petter Selasky 		return errstr;
457*d6b92ffaSHans Petter Selasky 
458*d6b92ffaSHans Petter Selasky 	mad_encode_field(payload,
459*d6b92ffaSHans Petter Selasky 			 IB_CC_CA_CONGESTION_SETTING_PORT_CONTROL_F,
460*d6b92ffaSHans Petter Selasky 			 &port_control);
461*d6b92ffaSHans Petter Selasky 
462*d6b92ffaSHans Petter Selasky 	mad_encode_field(payload,
463*d6b92ffaSHans Petter Selasky 			 IB_CC_CA_CONGESTION_SETTING_CONTROL_MAP_F,
464*d6b92ffaSHans Petter Selasky 			 &control_map);
465*d6b92ffaSHans Petter Selasky 
466*d6b92ffaSHans Petter Selasky 	for (i = 0; i < 16; i++) {
467*d6b92ffaSHans Petter Selasky 		uint8_t *ptr;
468*d6b92ffaSHans Petter Selasky 
469*d6b92ffaSHans Petter Selasky 		if (!(control_map & (0x1 << i)))
470*d6b92ffaSHans Petter Selasky 			continue;
471*d6b92ffaSHans Petter Selasky 
472*d6b92ffaSHans Petter Selasky 		ptr = payload + 2 + 2 + i * 8;
473*d6b92ffaSHans Petter Selasky 
474*d6b92ffaSHans Petter Selasky 		mad_encode_field(ptr,
475*d6b92ffaSHans Petter Selasky 				 IB_CC_CA_CONGESTION_ENTRY_CCTI_TIMER_F,
476*d6b92ffaSHans Petter Selasky 				 &ccti_timer);
477*d6b92ffaSHans Petter Selasky 
478*d6b92ffaSHans Petter Selasky 		mad_encode_field(ptr,
479*d6b92ffaSHans Petter Selasky 				 IB_CC_CA_CONGESTION_ENTRY_CCTI_INCREASE_F,
480*d6b92ffaSHans Petter Selasky 				 &ccti_increase);
481*d6b92ffaSHans Petter Selasky 
482*d6b92ffaSHans Petter Selasky 		mad_encode_field(ptr,
483*d6b92ffaSHans Petter Selasky 				 IB_CC_CA_CONGESTION_ENTRY_TRIGGER_THRESHOLD_F,
484*d6b92ffaSHans Petter Selasky 				 &trigger_threshold);
485*d6b92ffaSHans Petter Selasky 
486*d6b92ffaSHans Petter Selasky 		mad_encode_field(ptr,
487*d6b92ffaSHans Petter Selasky 				 IB_CC_CA_CONGESTION_ENTRY_CCTI_MIN_F,
488*d6b92ffaSHans Petter Selasky 				 &ccti_min);
489*d6b92ffaSHans Petter Selasky 	}
490*d6b92ffaSHans Petter Selasky 
491*d6b92ffaSHans Petter Selasky 	if (!cc_config_status_via(payload, rcv, dest, IB_CC_ATTR_CA_CONGESTION_SETTING,
492*d6b92ffaSHans Petter Selasky 				  0, 0, NULL, srcport, cckey))
493*d6b92ffaSHans Petter Selasky 		return "ca congestion setting config failed";
494*d6b92ffaSHans Petter Selasky 
495*d6b92ffaSHans Petter Selasky 	return NULL;
496*d6b92ffaSHans Petter Selasky }
497*d6b92ffaSHans Petter Selasky 
congestion_control_table(ib_portid_t * dest,char ** argv,int argc)498*d6b92ffaSHans Petter Selasky static char *congestion_control_table(ib_portid_t * dest, char **argv, int argc)
499*d6b92ffaSHans Petter Selasky {
500*d6b92ffaSHans Petter Selasky 	uint8_t rcv[IB_CC_DATA_SZ] = { 0 };
501*d6b92ffaSHans Petter Selasky 	uint8_t payload[IB_CC_DATA_SZ] = { 0 };
502*d6b92ffaSHans Petter Selasky 	uint32_t ccti_limit;
503*d6b92ffaSHans Petter Selasky 	uint32_t index;
504*d6b92ffaSHans Petter Selasky 	uint32_t cctshifts[64];
505*d6b92ffaSHans Petter Selasky 	uint32_t cctmults[64];
506*d6b92ffaSHans Petter Selasky 	char *errstr;
507*d6b92ffaSHans Petter Selasky 	int i;
508*d6b92ffaSHans Petter Selasky 
509*d6b92ffaSHans Petter Selasky 	if (argc < 2 || argc > 66)
510*d6b92ffaSHans Petter Selasky 		return "invalid number of parameters for CongestionControlTable";
511*d6b92ffaSHans Petter Selasky 
512*d6b92ffaSHans Petter Selasky 	if ((errstr = parseint(argv[0], &ccti_limit, 0)))
513*d6b92ffaSHans Petter Selasky 		return errstr;
514*d6b92ffaSHans Petter Selasky 
515*d6b92ffaSHans Petter Selasky 	if ((errstr = parseint(argv[1], &index, 0)))
516*d6b92ffaSHans Petter Selasky 		return errstr;
517*d6b92ffaSHans Petter Selasky 
518*d6b92ffaSHans Petter Selasky 	if (ccti_limit && (ccti_limit + 1) != (index * 64 + (argc - 2)))
519*d6b92ffaSHans Petter Selasky 		return "invalid number of cct entries input given ccti_limit and index";
520*d6b92ffaSHans Petter Selasky 
521*d6b92ffaSHans Petter Selasky 	for (i = 0; i < (argc - 2); i++) {
522*d6b92ffaSHans Petter Selasky 		if ((errstr = parsecct(argv[i + 2], &cctshifts[i], &cctmults[i])))
523*d6b92ffaSHans Petter Selasky 			return errstr;
524*d6b92ffaSHans Petter Selasky 	}
525*d6b92ffaSHans Petter Selasky 
526*d6b92ffaSHans Petter Selasky 	mad_encode_field(payload,
527*d6b92ffaSHans Petter Selasky 			 IB_CC_CONGESTION_CONTROL_TABLE_CCTI_LIMIT_F,
528*d6b92ffaSHans Petter Selasky 			 &ccti_limit);
529*d6b92ffaSHans Petter Selasky 
530*d6b92ffaSHans Petter Selasky 	for (i = 0; i < (argc - 2); i++) {
531*d6b92ffaSHans Petter Selasky 		mad_encode_field(payload + 4 + i * 2,
532*d6b92ffaSHans Petter Selasky 				 IB_CC_CONGESTION_CONTROL_TABLE_ENTRY_CCT_SHIFT_F,
533*d6b92ffaSHans Petter Selasky 				 &cctshifts[i]);
534*d6b92ffaSHans Petter Selasky 
535*d6b92ffaSHans Petter Selasky 		mad_encode_field(payload + 4 + i * 2,
536*d6b92ffaSHans Petter Selasky 				 IB_CC_CONGESTION_CONTROL_TABLE_ENTRY_CCT_MULTIPLIER_F,
537*d6b92ffaSHans Petter Selasky 				 &cctmults[i]);
538*d6b92ffaSHans Petter Selasky 	}
539*d6b92ffaSHans Petter Selasky 
540*d6b92ffaSHans Petter Selasky 	if (!cc_config_status_via(payload, rcv, dest, IB_CC_ATTR_CONGESTION_CONTROL_TABLE,
541*d6b92ffaSHans Petter Selasky 				  index, 0, NULL, srcport, cckey))
542*d6b92ffaSHans Petter Selasky 		return "congestion control table config failed";
543*d6b92ffaSHans Petter Selasky 
544*d6b92ffaSHans Petter Selasky 	return NULL;
545*d6b92ffaSHans Petter Selasky }
546*d6b92ffaSHans Petter Selasky 
process_opt(void * context,int ch,char * optarg)547*d6b92ffaSHans Petter Selasky static int process_opt(void *context, int ch, char *optarg)
548*d6b92ffaSHans Petter Selasky {
549*d6b92ffaSHans Petter Selasky 	switch (ch) {
550*d6b92ffaSHans Petter Selasky 	case 'c':
551*d6b92ffaSHans Petter Selasky 		cckey = (uint64_t) strtoull(optarg, 0, 0);
552*d6b92ffaSHans Petter Selasky 		break;
553*d6b92ffaSHans Petter Selasky 	case 25:
554*d6b92ffaSHans Petter Selasky 		if (!inet_pton(AF_INET6, optarg, &dgid)) {
555*d6b92ffaSHans Petter Selasky 			fprintf(stderr, "dgid format is wrong!\n");
556*d6b92ffaSHans Petter Selasky 			ibdiag_show_usage();
557*d6b92ffaSHans Petter Selasky 			return 1;
558*d6b92ffaSHans Petter Selasky 		}
559*d6b92ffaSHans Petter Selasky 		with_grh = 1;
560*d6b92ffaSHans Petter Selasky 		break;
561*d6b92ffaSHans Petter Selasky 	default:
562*d6b92ffaSHans Petter Selasky 		return -1;
563*d6b92ffaSHans Petter Selasky 	}
564*d6b92ffaSHans Petter Selasky 	return 0;
565*d6b92ffaSHans Petter Selasky }
566*d6b92ffaSHans Petter Selasky 
main(int argc,char ** argv)567*d6b92ffaSHans Petter Selasky int main(int argc, char **argv)
568*d6b92ffaSHans Petter Selasky {
569*d6b92ffaSHans Petter Selasky 	char usage_args[1024];
570*d6b92ffaSHans Petter Selasky 	int mgmt_classes[3] = { IB_SMI_CLASS, IB_SA_CLASS, IB_CC_CLASS };
571*d6b92ffaSHans Petter Selasky 	ib_portid_t portid = { 0 };
572*d6b92ffaSHans Petter Selasky 	char *err;
573*d6b92ffaSHans Petter Selasky 	op_fn_t *fn;
574*d6b92ffaSHans Petter Selasky 	const match_rec_t *r;
575*d6b92ffaSHans Petter Selasky 	int n;
576*d6b92ffaSHans Petter Selasky 
577*d6b92ffaSHans Petter Selasky 	const struct ibdiag_opt opts[] = {
578*d6b92ffaSHans Petter Selasky 		{"cckey", 'c', 1, "<key>", "CC key"},
579*d6b92ffaSHans Petter Selasky 		{"dgid", 25, 1, NULL, "remote gid (IPv6 format)"},
580*d6b92ffaSHans Petter Selasky 		{0}
581*d6b92ffaSHans Petter Selasky 	};
582*d6b92ffaSHans Petter Selasky 	const char *usage_examples[] = {
583*d6b92ffaSHans Petter Selasky 		"SwitchCongestionSetting 2 0x1F 0x1FFFFFFFFF 0x0 0xF 8 0 0:0 1\t# Configure Switch Congestion Settings",
584*d6b92ffaSHans Petter Selasky 		"CACongestionSetting 1 0 0x3 150 1 0 0\t\t# Configure CA Congestion Settings to SL 0 and SL 1",
585*d6b92ffaSHans Petter Selasky 		"CACongestionSetting 1 0 0x4 200 1 0 0\t\t# Configure CA Congestion Settings to SL 2",
586*d6b92ffaSHans Petter Selasky 		"CongestionControlTable 1 63 0 0:0 0:1 ...\t# Configure first block of Congestion Control Table",
587*d6b92ffaSHans Petter Selasky 		"CongestionControlTable 1 127 0 0:64 0:65 ...\t# Configure second block of Congestion Control Table",
588*d6b92ffaSHans Petter Selasky 		NULL
589*d6b92ffaSHans Petter Selasky 	};
590*d6b92ffaSHans Petter Selasky 
591*d6b92ffaSHans Petter Selasky 	n = sprintf(usage_args, "[-c key] <op> <lid|guid>\n"
592*d6b92ffaSHans Petter Selasky 		    "\nWARNING -- You should understand what you are "
593*d6b92ffaSHans Petter Selasky 		    "doing before using this tool.  Misuse of this "
594*d6b92ffaSHans Petter Selasky 		    "tool could result in a broken fabric.\n"
595*d6b92ffaSHans Petter Selasky 		    "\nSupported ops (and aliases, case insensitive):\n");
596*d6b92ffaSHans Petter Selasky 	for (r = match_tbl; r->name; r++) {
597*d6b92ffaSHans Petter Selasky 		n += snprintf(usage_args + n, sizeof(usage_args) - n,
598*d6b92ffaSHans Petter Selasky 			      "  %s (%s) <lid|guid>%s%s%s\n", r->name,
599*d6b92ffaSHans Petter Selasky 			      r->alias ? r->alias : "",
600*d6b92ffaSHans Petter Selasky 			      r->opt_portnum ? " <portnum>" : "",
601*d6b92ffaSHans Petter Selasky 			      r->ops_extra ? " " : "",
602*d6b92ffaSHans Petter Selasky 			      r->ops_extra ? r->ops_extra : "");
603*d6b92ffaSHans Petter Selasky 		if (n >= sizeof(usage_args))
604*d6b92ffaSHans Petter Selasky 			exit(-1);
605*d6b92ffaSHans Petter Selasky 	}
606*d6b92ffaSHans Petter Selasky 
607*d6b92ffaSHans Petter Selasky 	ibdiag_process_opts(argc, argv, NULL, "DK", opts, process_opt,
608*d6b92ffaSHans Petter Selasky 			    usage_args, usage_examples);
609*d6b92ffaSHans Petter Selasky 
610*d6b92ffaSHans Petter Selasky 	argc -= optind;
611*d6b92ffaSHans Petter Selasky 	argv += optind;
612*d6b92ffaSHans Petter Selasky 
613*d6b92ffaSHans Petter Selasky 	if (argc < 2)
614*d6b92ffaSHans Petter Selasky 		ibdiag_show_usage();
615*d6b92ffaSHans Petter Selasky 
616*d6b92ffaSHans Petter Selasky 	if (!(fn = match_op(match_tbl, argv[0])))
617*d6b92ffaSHans Petter Selasky 		IBEXIT("operation '%s' not supported", argv[0]);
618*d6b92ffaSHans Petter Selasky 
619*d6b92ffaSHans Petter Selasky 	srcport = mad_rpc_open_port(ibd_ca, ibd_ca_port, mgmt_classes, 3);
620*d6b92ffaSHans Petter Selasky 	if (!srcport)
621*d6b92ffaSHans Petter Selasky 		IBEXIT("Failed to open '%s' port '%d'", ibd_ca, ibd_ca_port);
622*d6b92ffaSHans Petter Selasky 
623*d6b92ffaSHans Petter Selasky 	smp_mkey_set(srcport, ibd_mkey);
624*d6b92ffaSHans Petter Selasky 
625*d6b92ffaSHans Petter Selasky 	if (with_grh && ibd_dest_type != IB_DEST_LID)
626*d6b92ffaSHans Petter Selasky 		IBEXIT("When using GRH, LID should be provided");
627*d6b92ffaSHans Petter Selasky 	if (resolve_portid_str(ibd_ca, ibd_ca_port, &portid, argv[1],
628*d6b92ffaSHans Petter Selasky 			       ibd_dest_type, ibd_sm_id, srcport) < 0)
629*d6b92ffaSHans Petter Selasky 		IBEXIT("can't resolve destination %s", argv[1]);
630*d6b92ffaSHans Petter Selasky 	if (with_grh) {
631*d6b92ffaSHans Petter Selasky 		portid.grh_present = 1;
632*d6b92ffaSHans Petter Selasky 		memcpy(&portid.gid, &dgid, sizeof(portid.gid));
633*d6b92ffaSHans Petter Selasky 	}
634*d6b92ffaSHans Petter Selasky 	if ((err = fn(&portid, argv + 2, argc - 2)))
635*d6b92ffaSHans Petter Selasky 		IBEXIT("operation %s: %s", argv[0], err);
636*d6b92ffaSHans Petter Selasky 
637*d6b92ffaSHans Petter Selasky 	mad_rpc_close_port(srcport);
638*d6b92ffaSHans Petter Selasky 	exit(0);
639*d6b92ffaSHans Petter Selasky }
640