Blob Blame History Raw
From 9abd0130706603ffc408ecb8a09c7586eca14826 Mon Sep 17 00:00:00 2001
From: Phil Sutter <psutter@redhat.com>
Date: Tue, 28 Feb 2017 16:09:46 +0100
Subject: [PATCH] Allow specifying bridge port STP state by name rather than
 number.

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1417289
Upstream Status: iproute2.git commit 6b8c871dc1045
Conflicts: Changes in removed code due to previously backported double
	   whitespace cleanup - removed double whitespaces from added
	   text manually.

commit 6b8c871dc104576c9f55d87937d6dd445d77f34f
Author: Alex Pilon <alp@alexpilon.ca>
Date:   Thu Feb 19 14:27:46 2015 -0500

    Allow specifying bridge port STP state by name rather than number.

    The existing behaviour forces one to memorize the integer constants for
    STP port states.

        # bridge link set dev dummy0 state 3

    This patch makes it possible to use the lowercased port state name.

        # bridge link set dev dummy0 state forwarding

    Invalid non-integer inputs now cause exit with status -1.

    Signed-off-by: Alex Pilon <alp@alexpilon.ca>
---
 bridge/link.c     | 14 +++++++++++++-
 man/man8/bridge.8 |  4 +++-
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/bridge/link.c b/bridge/link.c
index 8b18931..06a2c0f 100644
--- a/bridge/link.c
+++ b/bridge/link.c
@@ -316,7 +316,19 @@ static int brlink_modify(int argc, char **argv)
 			priority = atoi(*argv);
 		} else if (strcmp(*argv, "state") == 0) {
 			NEXT_ARG();
-			state = atoi(*argv);
+			char *endptr;
+			size_t nstates = sizeof(port_states) / sizeof(*port_states);
+			state = strtol(*argv, &endptr, 10);
+			if (!(**argv != '\0' && *endptr == '\0')) {
+				for (state = 0; state < nstates; state++)
+					if (strcmp(port_states[state], *argv) == 0)
+						break;
+				if (state == nstates) {
+					fprintf(stderr,
+						"Error: invalid STP port state\n");
+					exit(-1);
+				}
+			}
 		} else if (strcmp(*argv, "hwmode") == 0) {
 			NEXT_ARG();
 			flags = BRIDGE_FLAGS_SELF;
diff --git a/man/man8/bridge.8 b/man/man8/bridge.8
index 42fe922..c742c83 100644
--- a/man/man8/bridge.8
+++ b/man/man8/bridge.8
@@ -207,7 +207,9 @@ droot port selectio algorithms.
 .TP
 .BI state " STATE "
 the operation state of the port. This is primarily used by user space STP/RSTP
-implementation. The following is a list of valid values:
+implementation. One may enter a lowercased port state name, or one of the
+numbers below. Negative inputs are ignored, and unrecognized names return an
+error.
 
 .B 0
 - port is DISABLED. Make this port completely inactive.
-- 
1.8.3.1