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