bridge: add parse_stp_state helper

Add a helper which parses an STP state string to its numeric value.

Signed-off-by: Nikolay Aleksandrov <nikolay@nvidia.com>
Signed-off-by: David Ahern <dsahern@kernel.org>
This commit is contained in:
Nikolay Aleksandrov 2021-04-18 15:01:33 +03:00 committed by David Ahern
parent f07516c3b0
commit f2f52fcabe
2 changed files with 18 additions and 5 deletions

View File

@ -11,6 +11,7 @@ int print_linkinfo(struct nlmsghdr *n, void *arg);
int print_mdb_mon(struct nlmsghdr *n, void *arg); int print_mdb_mon(struct nlmsghdr *n, void *arg);
int print_fdb(struct nlmsghdr *n, void *arg); int print_fdb(struct nlmsghdr *n, void *arg);
void print_stp_state(__u8 state); void print_stp_state(__u8 state);
int parse_stp_state(const char *arg);
int do_fdb(int argc, char **argv); int do_fdb(int argc, char **argv);
int do_mdb(int argc, char **argv); int do_mdb(int argc, char **argv);

View File

@ -78,6 +78,21 @@ void print_stp_state(__u8 state)
"state (%d) ", state); "state (%d) ", state);
} }
int parse_stp_state(const char *arg)
{
size_t nstates = ARRAY_SIZE(stp_states);
int state;
for (state = 0; state < nstates; state++)
if (strcmp(stp_states[state], arg) == 0)
break;
if (state == nstates)
state = -1;
return state;
}
static void print_hwmode(__u16 mode) static void print_hwmode(__u16 mode)
{ {
if (mode >= ARRAY_SIZE(hw_mode)) if (mode >= ARRAY_SIZE(hw_mode))
@ -359,14 +374,11 @@ static int brlink_modify(int argc, char **argv)
} else if (strcmp(*argv, "state") == 0) { } else if (strcmp(*argv, "state") == 0) {
NEXT_ARG(); NEXT_ARG();
char *endptr; char *endptr;
size_t nstates = ARRAY_SIZE(stp_states);
state = strtol(*argv, &endptr, 10); state = strtol(*argv, &endptr, 10);
if (!(**argv != '\0' && *endptr == '\0')) { if (!(**argv != '\0' && *endptr == '\0')) {
for (state = 0; state < nstates; state++) state = parse_stp_state(*argv);
if (strcasecmp(stp_states[state], *argv) == 0) if (state == -1) {
break;
if (state == nstates) {
fprintf(stderr, fprintf(stderr,
"Error: invalid STP port state\n"); "Error: invalid STP port state\n");
return -1; return -1;