switchctl.c (4435B)
1 /* 2 * 3 * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file. 4 * 5 * Copyright 2007-2019 Broadcom Inc. All rights reserved. 6 * 7 * Switch Control commands 8 */ 9 10 #include <appl/diag/system.h> 11 #include <appl/diag/parse.h> 12 #include <appl/diag/dport.h> 13 #include <shared/bsl.h> 14 #include <bcm/error.h> 15 #include <bcm/switch.h> 16 17 STATIC char *switch_control_names[] = { 18 BCM_SWITCH_CONTROL_STR 19 }; 20 21 char cmd_switch_control_usage[] = 22 "\n\tSwitchControl [PortBitMap=<pbmp>] [<name> | <name>=<value>]\n"; 23 24 cmd_result_t 25 cmd_switch_control(int unit, args_t *a) 26 { 27 parse_table_t pt; 28 int r; 29 bcm_pbmp_t arg_pbmp; 30 int arg_pbmp_given; 31 bcm_switch_control_t type; 32 int param; 33 int retCode = CMD_OK; 34 bcm_port_t port, dport; 35 char *spec; 36 char name[128], *value; 37 38 if (!sh_check_attached(ARG_CMD(a), unit)) { 39 return CMD_FAIL; 40 } 41 42 if (COUNTOF(switch_control_names) != bcmSwitch__Count) { 43 cli_out("%s: Error: NAMES ARRAY OUT OF SYNC\n", ARG_CMD(a)); 44 return CMD_FAIL; 45 } 46 47 arg_pbmp = PBMP_ALL(unit); 48 49 parse_table_init(unit, &pt); 50 parse_table_add(&pt, "PortBitMap", PQ_DFL|PQ_PBMP|PQ_BCM, 51 (void *)(0), &arg_pbmp, NULL); 52 if (parse_arg_eq(a, &pt) < 0) { 53 parse_arg_eq_done(&pt); 54 cli_out("%s: Error: Unknown option: %s\n", ARG_CMD(a), ARG_CUR(a)); 55 return CMD_FAIL; 56 } 57 58 arg_pbmp_given = ((pt.pt_entries[0].pq_type & PQ_PARSED) != 0); 59 60 parse_arg_eq_done(&pt); 61 62 if ((spec = ARG_GET(a)) == NULL) { 63 if (!arg_pbmp_given) { 64 for (type = 0; type < bcmSwitch__Count; type++) { 65 cli_out("%-40s", switch_control_names[type]); 66 r = bcm_switch_control_get(unit, type, ¶m); 67 if (r < 0) { 68 cli_out("%s\n", bcm_errmsg(r)); 69 } else { 70 cli_out("0x%x\n", (uint32)param); 71 } 72 } 73 } else { 74 /* coverity[overrun-local] */ 75 DPORT_BCM_PBMP_ITER(unit, arg_pbmp, dport, port) { 76 cli_out("%s:\n", BCM_PORT_NAME(unit, port)); 77 for (type = 0; type < bcmSwitch__Count; type++) { 78 cli_out(" %-40s", switch_control_names[type]); 79 r = bcm_switch_control_port_get(unit, port, type, ¶m); 80 if (r < 0) { 81 cli_out("%s\n", bcm_errmsg(r)); 82 } else { 83 cli_out("0x%x\n", (uint32)param); 84 } 85 } 86 } 87 } 88 } else { 89 strncpy(name, spec, sizeof (name)); 90 name[sizeof (name) - 1] = 0; 91 if ((value = strchr(name, '=')) != NULL) { 92 *value++ = 0; 93 } 94 95 for (type = 0; type < bcmSwitch__Count; type++) { 96 if (sal_strcasecmp(name, switch_control_names[type]) == 0) { 97 break; 98 } 99 } 100 101 if (type == bcmSwitch__Count) { 102 int first_hit = 0; 103 cli_out("Unknown switch control: %s\n", name); 104 for (type = 0; type < bcmSwitch__Count; type++) { 105 if (strcaseindex(switch_control_names[type], name) != NULL) { 106 if (first_hit == 0) { 107 cli_out("Did you mean?\n"); 108 first_hit = 1; 109 } 110 cli_out("\t%s\n", switch_control_names[type]); 111 } 112 } 113 return CMD_FAIL; 114 } 115 116 if (value == NULL) { 117 if (!arg_pbmp_given) { 118 r = bcm_switch_control_get(unit, type, ¶m); 119 if (r < 0) { 120 cli_out("bcm_switch_control_get ERROR: %s\n", 121 bcm_errmsg(r)); 122 retCode = CMD_FAIL; 123 } else { 124 cli_out("0x%x\n", (uint32)param); 125 } 126 } else { 127 DPORT_BCM_PBMP_ITER(unit, arg_pbmp, dport, port) { 128 cli_out("%s: ", BCM_PORT_NAME(unit, port)); 129 r = bcm_switch_control_port_get(unit, port, type, ¶m); 130 if (r < 0) { 131 cli_out("bcm_switch_control_port_get ERROR: %s\n", 132 bcm_errmsg(r)); 133 } else { 134 cli_out("0x%x\n", (uint32)param); 135 } 136 } 137 } 138 } else { 139 param = parse_integer(value); 140 141 if (!arg_pbmp_given) { 142 r = bcm_switch_control_set(unit, type, param); 143 if (r < 0) { 144 cli_out("bcm_switch_control_set ERROR: %s\n", 145 bcm_errmsg(r)); 146 retCode = CMD_FAIL; 147 } 148 } else { 149 DPORT_BCM_PBMP_ITER(unit, arg_pbmp, dport, port) { 150 r = bcm_switch_control_port_set(unit, port, type, param); 151 if (r < 0) { 152 cli_out("bcm_switch_control_port_set ERROR: " 153 "port=%d: %s\n", 154 port, bcm_errmsg(r)); 155 retCode = CMD_FAIL; 156 } 157 } 158 } 159 } 160 } 161 162 return retCode; 163 }