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.
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:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
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.
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
38 #endif /* HAVE_CONFIG_H */
46 #include <netinet/in.h>
50 #define __STDC_FORMAT_MACROS
53 #include <infiniband/mad.h>
55 #include "ibdiag_common.h"
57 struct ibmad_port *srcport;
59 static ibmad_gid_t dgid;
62 static op_fn_t congestion_key_info;
63 static op_fn_t switch_congestion_setting;
64 static op_fn_t switch_port_congestion_setting;
65 static op_fn_t ca_congestion_setting;
66 static op_fn_t congestion_control_table;
68 static 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> ..."},
86 /*******************************************/
87 static char *parselonglongint(char *arg, uint64_t *val)
92 *val = strtoull(arg, &endptr, 0);
93 if ((endptr && *endptr != '\0')
96 return "value out of range";
97 return "invalid integer input";
103 static char *parseint(char *arg, uint32_t *val, int hexonly)
108 *val = strtoul(arg, &endptr, hexonly ? 16 : 0);
109 if ((endptr && *endptr != '\0')
112 return "value out of range";
113 return "invalid integer input";
119 static char *congestion_key_info(ib_portid_t * dest, char **argv, int argc)
121 uint8_t rcv[IB_CC_DATA_SZ] = { 0 };
122 uint8_t payload[IB_CC_DATA_SZ] = { 0 };
124 uint32_t cc_keyprotectbit;
125 uint32_t cc_keyleaseperiod;
126 uint32_t cc_keyviolations;
130 return "invalid number of parameters for CongestionKeyInfo";
132 if ((errstr = parselonglongint(argv[0], &cc_key)))
134 if ((errstr = parseint(argv[1], &cc_keyprotectbit, 0)))
136 if ((errstr = parseint(argv[2], &cc_keyleaseperiod, 0)))
138 if ((errstr = parseint(argv[3], &cc_keyviolations, 0)))
141 if (cc_keyprotectbit != 0 && cc_keyprotectbit != 1)
142 return "invalid cc_keyprotectbit value";
144 if (cc_keyleaseperiod > USHRT_MAX)
145 return "invalid cc_keyleaseperiod value";
147 if (cc_keyviolations > USHRT_MAX)
148 return "invalid cc_keyviolations value";
150 mad_set_field64(payload,
152 IB_CC_CONGESTION_KEY_INFO_CC_KEY_F,
155 mad_encode_field(payload,
156 IB_CC_CONGESTION_KEY_INFO_CC_KEY_PROTECT_BIT_F,
159 mad_encode_field(payload,
160 IB_CC_CONGESTION_KEY_INFO_CC_KEY_LEASE_PERIOD_F,
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
167 mad_encode_field(payload,
168 IB_CC_CONGESTION_KEY_INFO_CC_KEY_VIOLATIONS_F,
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";
179 /* parse like it's a hypothetical 256 bit hex code */
180 static char *parse256(char *arg, uint8_t *buf)
187 if (!strncmp(arg, "0x", 2) || !strncmp(arg, "0X", 2))
190 for (ptr = arg; *ptr; ptr++) {
192 return "invalid hex digit read";
197 return "hex code too long";
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.
203 startindex = 32 - ((numdigits - 1) / 2) - 1;
205 for (i = startindex; i <= 31; i++) {
210 /* I can't help but think there is a strtoX that
211 * will do this for me, but I can't find it.
213 if (i == startindex && numdigits % 2) {
222 if ((errstr = parseint(tmp, &tmpint, 1)))
230 static char *parsecct(char *arg, uint32_t *shift, uint32_t *multiplier)
232 char buf[1024] = { 0 };
238 if (!(ptr = strchr(buf, ':')))
239 return "ccts are formatted shift:multiplier";
244 if ((errstr = parseint(buf, shift, 0)))
247 if ((errstr = parseint(ptr, multiplier, 0)))
253 static char *switch_congestion_setting(ib_portid_t * dest, char **argv, int argc)
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 };
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;
270 return "invalid number of parameters for SwitchCongestionSetting";
272 if ((errstr = parseint(argv[0], &control_map, 0)))
275 if ((errstr = parse256(argv[1], victim_mask)))
278 if ((errstr = parse256(argv[2], credit_mask)))
281 if ((errstr = parseint(argv[3], &threshold, 0)))
284 if ((errstr = parseint(argv[4], &packet_size, 0)))
287 if ((errstr = parseint(argv[5], &cs_threshold, 0)))
290 if ((errstr = parsecct(argv[6], &cs_returndelay_s, &cs_returndelay_m)))
293 cs_returndelay = cs_returndelay_m;
294 cs_returndelay |= (cs_returndelay_s << 14);
296 if ((errstr = parseint(argv[7], &marking_rate, 0)))
299 mad_encode_field(payload,
300 IB_CC_SWITCH_CONGESTION_SETTING_CONTROL_MAP_F,
303 mad_set_array(payload,
305 IB_CC_SWITCH_CONGESTION_SETTING_VICTIM_MASK_F,
308 mad_set_array(payload,
310 IB_CC_SWITCH_CONGESTION_SETTING_CREDIT_MASK_F,
313 mad_encode_field(payload,
314 IB_CC_SWITCH_CONGESTION_SETTING_THRESHOLD_F,
317 mad_encode_field(payload,
318 IB_CC_SWITCH_CONGESTION_SETTING_PACKET_SIZE_F,
321 mad_encode_field(payload,
322 IB_CC_SWITCH_CONGESTION_SETTING_CS_THRESHOLD_F,
325 mad_encode_field(payload,
326 IB_CC_SWITCH_CONGESTION_SETTING_CS_RETURN_DELAY_F,
329 mad_encode_field(payload,
330 IB_CC_SWITCH_CONGESTION_SETTING_MARKING_RATE_F,
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";
340 static char *switch_port_congestion_setting(ib_portid_t * dest, char **argv, int argc)
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 };
347 uint32_t control_type;
349 uint32_t packet_size;
350 uint32_t cong_parm_marking_rate;
357 return "invalid number of parameters for SwitchPortCongestion";
359 if ((errstr = parseint(argv[0], &portnum, 0)))
362 if ((errstr = parseint(argv[1], &valid, 0)))
365 if ((errstr = parseint(argv[2], &control_type, 0)))
368 if ((errstr = parseint(argv[3], &threshold, 0)))
371 if ((errstr = parseint(argv[4], &packet_size, 0)))
374 if ((errstr = parseint(argv[5], &cong_parm_marking_rate, 0)))
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";
381 mad_decode_field((uint8_t *)data, IB_NODE_TYPE_F, &type);
382 mad_decode_field((uint8_t *)data, IB_NODE_NPORTS_F, &numports);
384 if (type != IB_NODE_SWITCH)
385 return "destination not a switch";
387 if (portnum > numports)
388 return "invalid port number specified";
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";
395 ptr = payload + (((portnum % 32) * 4));
397 mad_encode_field(ptr,
398 IB_CC_SWITCH_PORT_CONGESTION_SETTING_ELEMENT_VALID_F,
401 mad_encode_field(ptr,
402 IB_CC_SWITCH_PORT_CONGESTION_SETTING_ELEMENT_CONTROL_TYPE_F,
405 mad_encode_field(ptr,
406 IB_CC_SWITCH_PORT_CONGESTION_SETTING_ELEMENT_THRESHOLD_F,
409 mad_encode_field(ptr,
410 IB_CC_SWITCH_PORT_CONGESTION_SETTING_ELEMENT_PACKET_SIZE_F,
413 mad_encode_field(ptr,
414 IB_CC_SWITCH_PORT_CONGESTION_SETTING_ELEMENT_CONG_PARM_MARKING_RATE_F,
415 &cong_parm_marking_rate);
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";
424 static char *ca_congestion_setting(ib_portid_t * dest, char **argv, int argc)
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;
431 uint32_t ccti_increase;
432 uint32_t trigger_threshold;
438 return "invalid number of parameters for CACongestionSetting";
440 if ((errstr = parseint(argv[0], &port_control, 0)))
443 if ((errstr = parseint(argv[1], &control_map, 0)))
446 if ((errstr = parseint(argv[2], &ccti_timer, 0)))
449 if ((errstr = parseint(argv[3], &ccti_increase, 0)))
452 if ((errstr = parseint(argv[4], &trigger_threshold, 0)))
455 if ((errstr = parseint(argv[5], &ccti_min, 0)))
458 mad_encode_field(payload,
459 IB_CC_CA_CONGESTION_SETTING_PORT_CONTROL_F,
462 mad_encode_field(payload,
463 IB_CC_CA_CONGESTION_SETTING_CONTROL_MAP_F,
466 for (i = 0; i < 16; i++) {
469 if (!(control_map & (0x1 << i)))
472 ptr = payload + 2 + 2 + i * 8;
474 mad_encode_field(ptr,
475 IB_CC_CA_CONGESTION_ENTRY_CCTI_TIMER_F,
478 mad_encode_field(ptr,
479 IB_CC_CA_CONGESTION_ENTRY_CCTI_INCREASE_F,
482 mad_encode_field(ptr,
483 IB_CC_CA_CONGESTION_ENTRY_TRIGGER_THRESHOLD_F,
486 mad_encode_field(ptr,
487 IB_CC_CA_CONGESTION_ENTRY_CCTI_MIN_F,
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";
498 static char *congestion_control_table(ib_portid_t * dest, char **argv, int argc)
500 uint8_t rcv[IB_CC_DATA_SZ] = { 0 };
501 uint8_t payload[IB_CC_DATA_SZ] = { 0 };
504 uint32_t cctshifts[64];
505 uint32_t cctmults[64];
509 if (argc < 2 || argc > 66)
510 return "invalid number of parameters for CongestionControlTable";
512 if ((errstr = parseint(argv[0], &ccti_limit, 0)))
515 if ((errstr = parseint(argv[1], &index, 0)))
518 if (ccti_limit && (ccti_limit + 1) != (index * 64 + (argc - 2)))
519 return "invalid number of cct entries input given ccti_limit and index";
521 for (i = 0; i < (argc - 2); i++) {
522 if ((errstr = parsecct(argv[i + 2], &cctshifts[i], &cctmults[i])))
526 mad_encode_field(payload,
527 IB_CC_CONGESTION_CONTROL_TABLE_CCTI_LIMIT_F,
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,
535 mad_encode_field(payload + 4 + i * 2,
536 IB_CC_CONGESTION_CONTROL_TABLE_ENTRY_CCT_MULTIPLIER_F,
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";
547 static int process_opt(void *context, int ch, char *optarg)
551 cckey = (uint64_t) strtoull(optarg, 0, 0);
554 if (!inet_pton(AF_INET6, optarg, &dgid)) {
555 fprintf(stderr, "dgid format is wrong!\n");
567 int main(int argc, char **argv)
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 };
574 const match_rec_t *r;
577 const struct ibdiag_opt opts[] = {
578 {"cckey", 'c', 1, "<key>", "CC key"},
579 {"dgid", 25, 1, NULL, "remote gid (IPv6 format)"},
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",
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))
607 ibdiag_process_opts(argc, argv, NULL, "DK", opts, process_opt,
608 usage_args, usage_examples);
616 if (!(fn = match_op(match_tbl, argv[0])))
617 IBEXIT("operation '%s' not supported", argv[0]);
619 srcport = mad_rpc_open_port(ibd_ca, ibd_ca_port, mgmt_classes, 3);
621 IBEXIT("Failed to open '%s' port '%d'", ibd_ca, ibd_ca_port);
623 smp_mkey_set(srcport, ibd_mkey);
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]);
631 portid.grh_present = 1;
632 memcpy(&portid.gid, &dgid, sizeof(portid.gid));
634 if ((err = fn(&portid, argv + 2, argc - 2)))
635 IBEXIT("operation %s: %s", argv[0], err);
637 mad_rpc_close_port(srcport);