]> git.proxmox.com Git - mirror_iproute2.git/commitdiff
lib: Extract from iplink_vlan a helper to parse key:value arrays
authorPetr Machata <me@pmachata.org>
Thu, 12 Nov 2020 22:24:44 +0000 (23:24 +0100)
committerDavid Ahern <dsahern@gmail.com>
Sat, 14 Nov 2020 02:43:15 +0000 (19:43 -0700)
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>
Signed-off-by: David Ahern <dsahern@gmail.com>
include/utils.h
ip/iplink_vlan.c
lib/utils.c

index d7653273af5f6caf393c8d3c85b4542862c1f7ee..2d1a587cb1ef970e19f5ea68e99d98aa9659d434 100644 (file)
@@ -329,4 +329,8 @@ int parse_one_of(const char *msg, const char *realval, const char * const *list,
                 size_t len, int *p_err);
 bool parse_on_off(const char *msg, const char *realval, int *p_err);
 
+int parse_mapping(int *argcp, char ***argvp,
+                 int (*mapping_cb)(__u32 key, char *value, void *data),
+                 void *mapping_cb_data);
+
 #endif /* __UTILS_H__ */
index 1e6817f5de3d235df6b600bb18798cd84079ade4..dadc349db16c10d4136cfd533b3d9038a3bc474a 100644 (file)
@@ -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;
 }
 
index eab0839a13c789d2294136a036d1f566c4f8e4ee..1dfaaf564915da6a2e8efdfb32ce2f0c3593e64a 100644 (file)
@@ -1763,3 +1763,31 @@ bool parse_on_off(const char *msg, const char *realval, int *p_err)
 
        return parse_one_of(msg, realval, values_on_off, ARRAY_SIZE(values_on_off), p_err);
 }
+
+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;
+}