diff mbox series

[iproute2-next,v2,07/11] lib: Extract from iplink_vlan a helper to parse key:value arrays

Message ID 15cf4f253c8a261e9df1c1d6e6115194945a0c21.1604059429.git.me@pmachata.org (mailing list archive)
State Not Applicable
Delegated to: Stephen Hemminger
Headers show
Series Add a tool for configuration of DCB | expand

Commit Message

Petr Machata Oct. 30, 2020, 12:29 p.m. UTC
VLAN netdevices have two similar attributes: ingress-qos-map and
egress-qos-map. These attributes can be configured with a series of
802.1-priority-to-skb-priority (and vice versa) mappings. A reusable helper
along those lines will be handy for configuration of various
priority-to-tc, tc-to-algorithm, and other arrays in DCB.

Therefore extract the logic to a function parse_mapping(), move to utils.c,
and dispatch to utils.c from iplink_vlan.c. That necessitates extraction of
a VLAN-specific parse_qos_mapping(). Do that, and propagate addattr_l()
return value up, unlike the original.

Signed-off-by: Petr Machata <me@pmachata.org>
---

Notes:
    v2:
    - In parse_qos_mapping(), propagate return value from addattr_l()
      [Roman Mashak]

 include/utils.h  |  4 ++++
 ip/iplink_vlan.c | 36 +++++++++++++++---------------------
 lib/utils.c      | 28 ++++++++++++++++++++++++++++
 3 files changed, 47 insertions(+), 21 deletions(-)
diff mbox series

Patch

diff --git a/include/utils.h b/include/utils.h
index e3cdb098834a..1c72221ae92c 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -330,4 +330,8 @@  int parse_one_of(const char *msg, const char *realval, const char * const *list,
 int parse_on_off(const char *msg, const char *realval, int *p_err);
 void print_on_off_bool(FILE *fp, const char *flag, bool val);
 
+int parse_mapping(int *argcp, char ***argvp,
+		  int (*mapping_cb)(__u32 key, char *value, void *data),
+		  void *mapping_cb_data);
+
 #endif /* __UTILS_H__ */
diff --git a/ip/iplink_vlan.c b/ip/iplink_vlan.c
index 1e6817f5de3d..dadc349db16c 100644
--- a/ip/iplink_vlan.c
+++ b/ip/iplink_vlan.c
@@ -49,36 +49,30 @@  static int on_off(const char *msg, const char *arg)
 	return -1;
 }
 
+static int parse_qos_mapping(__u32 key, char *value, void *data)
+{
+	struct nlmsghdr *n = data;
+	struct ifla_vlan_qos_mapping m = {
+		.from = key,
+	};
+
+	if (get_u32(&m.to, value, 0))
+		return 1;
+
+	return addattr_l(n, 1024, IFLA_VLAN_QOS_MAPPING, &m, sizeof(m));
+}
+
 static int vlan_parse_qos_map(int *argcp, char ***argvp, struct nlmsghdr *n,
 			      int attrtype)
 {
-	int argc = *argcp;
-	char **argv = *argvp;
-	struct ifla_vlan_qos_mapping m;
 	struct rtattr *tail;
 
 	tail = addattr_nest(n, 1024, attrtype);
 
-	while (argc > 0) {
-		char *colon = strchr(*argv, ':');
-
-		if (!colon)
-			break;
-		*colon = '\0';
-
-		if (get_u32(&m.from, *argv, 0))
-			return 1;
-		if (get_u32(&m.to, colon + 1, 0))
-			return 1;
-		argc--, argv++;
-
-		addattr_l(n, 1024, IFLA_VLAN_QOS_MAPPING, &m, sizeof(m));
-	}
+	if (parse_mapping(argcp, argvp, &parse_qos_mapping, n))
+		return 1;
 
 	addattr_nest_end(n, tail);
-
-	*argcp = argc;
-	*argvp = argv;
 	return 0;
 }
 
diff --git a/lib/utils.c b/lib/utils.c
index 8deec86ecbcd..aba7cc0960cd 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -1771,3 +1771,31 @@  void print_on_off_bool(FILE *fp, const char *flag, bool val)
 	else
 		fprintf(fp, "%s %s ", flag, val ? "on" : "off");
 }
+
+int parse_mapping(int *argcp, char ***argvp,
+		  int (*mapping_cb)(__u32 key, char *value, void *data),
+		  void *mapping_cb_data)
+{
+	int argc = *argcp;
+	char **argv = *argvp;
+
+	while (argc > 0) {
+		char *colon = strchr(*argv, ':');
+		__u32 key;
+
+		if (!colon)
+			break;
+		*colon = '\0';
+
+		if (get_u32(&key, *argv, 0))
+			return 1;
+		if (mapping_cb(key, colon + 1, mapping_cb_data))
+			return 1;
+
+		argc--, argv++;
+	}
+
+	*argcp = argc;
+	*argvp = argv;
+	return 0;
+}