diff mbox series

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

Message ID 233147e872018f538306e5f8dad3f3be07540d81.1603154867.git.me@pmachata.org
State New
Headers show
Series Add a tool for configuration of DCB | expand

Commit Message

Petr Machata Oct. 20, 2020, 12:58 a.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.

Signed-off-by: Petr Machata <me@pmachata.org>
---
 include/utils.h  |  4 ++++
 ip/iplink_vlan.c | 37 ++++++++++++++++---------------------
 lib/utils.c      | 28 ++++++++++++++++++++++++++++
 3 files changed, 48 insertions(+), 21 deletions(-)

Comments

Roman Mashak Oct. 20, 2020, 11:33 a.m. UTC | #1
Petr Machata <me@pmachata.org> writes:


[...]

> +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;

> +

> +	addattr_l(n, 1024, IFLA_VLAN_QOS_MAPPING, &m, sizeof(m));

> +	return 0;

> +}


addatr_l() may fail if netlink buffer size is not sufficient, may be:

return addattr_l(...);

would be better.
Petr Machata Oct. 20, 2020, 8:44 p.m. UTC | #2
Roman Mashak <mrv@mojatatu.com> writes:

> Petr Machata <me@pmachata.org> writes:

>

>

> [...]

>

>> +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;

>> +

>> +	addattr_l(n, 1024, IFLA_VLAN_QOS_MAPPING, &m, sizeof(m));

>> +	return 0;

>> +}

>

> addatr_l() may fail if netlink buffer size is not sufficient, may be:

>

> return addattr_l(...);

>

> would be better.


Ack, makes sense to fix this since I'm moving the code around anyway.
diff mbox series

Patch

diff --git a/include/utils.h b/include/utils.h
index 681110fcf8af..8323e3cf1103 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -339,4 +339,8 @@  int parse_on_off(const char *msg, const char *realval, int *p_err);
 void parse_flag_on_off(const char *msg, const char *realval,
 		       unsigned int *p_flags, unsigned int flag, int *p_ret);
 
+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 66c4c0fb57f1..73aa94acde3c 100644
--- a/ip/iplink_vlan.c
+++ b/ip/iplink_vlan.c
@@ -43,36 +43,31 @@  static void explain(void)
 	print_explain(stderr);
 }
 
+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;
+
+	addattr_l(n, 1024, IFLA_VLAN_QOS_MAPPING, &m, sizeof(m));
+	return 0;
+}
+
 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 fb25c64d36ff..93521a49eaec 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -1774,3 +1774,31 @@  void parse_flag_on_off(const char *msg, const char *realval,
 
 	set_flag(p_flags, flag, 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;
+}