diff mbox series

[iproute2-next,2/6] bridge: add parse_stp_state helper

Message ID 20210418120137.2605522-3-razor@blackwall.org
State New
Headers show
Series bridge: vlan: add per-vlan options support | expand

Commit Message

Nikolay Aleksandrov April 18, 2021, 12:01 p.m. UTC
From: Nikolay Aleksandrov <nikolay@nvidia.com>

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

Signed-off-by: Nikolay Aleksandrov <nikolay@nvidia.com>
---
 bridge/br_common.h |  1 +
 bridge/link.c      | 22 +++++++++++++++++-----
 2 files changed, 18 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/bridge/br_common.h b/bridge/br_common.h
index e3f46765ab89..33e56452702b 100644
--- a/bridge/br_common.h
+++ b/bridge/br_common.h
@@ -11,6 +11,7 @@  int print_linkinfo(struct nlmsghdr *n, void *arg);
 int print_mdb_mon(struct nlmsghdr *n, void *arg);
 int print_fdb(struct nlmsghdr *n, void *arg);
 void print_stp_state(__u8 state);
+int parse_stp_state(const char *arg);
 
 int do_fdb(int argc, char **argv);
 int do_mdb(int argc, char **argv);
diff --git a/bridge/link.c b/bridge/link.c
index a8cfa1814986..205a2fe79c1a 100644
--- a/bridge/link.c
+++ b/bridge/link.c
@@ -78,6 +78,21 @@  void print_stp_state(__u8 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)
 {
 	if (mode >= ARRAY_SIZE(hw_mode))
@@ -359,14 +374,11 @@  static int brlink_modify(int argc, char **argv)
 		} else if (strcmp(*argv, "state") == 0) {
 			NEXT_ARG();
 			char *endptr;
-			size_t nstates = ARRAY_SIZE(stp_states);
 
 			state = strtol(*argv, &endptr, 10);
 			if (!(**argv != '\0' && *endptr == '\0')) {
-				for (state = 0; state < nstates; state++)
-					if (strcasecmp(stp_states[state], *argv) == 0)
-						break;
-				if (state == nstates) {
+				state = parse_stp_state(*argv);
+				if (state == -1) {
 					fprintf(stderr,
 						"Error: invalid STP port state\n");
 					return -1;