]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
nexthop: Extract a helper to parse a NH ID
authorPetr Machata <petrm@nvidia.com>
Wed, 17 Mar 2021 12:54:32 +0000 (13:54 +0100)
committerDavid Ahern <dsahern@kernel.org>
Fri, 19 Mar 2021 15:00:43 +0000 (15:00 +0000)
NH ID extraction is a common operation, and will become more common still
with the resilient NH groups support. Add a helper that does what it
usually done and returns the parsed NH ID.

Signed-off-by: Petr Machata <petrm@nvidia.com>
Signed-off-by: David Ahern <dsahern@kernel.org>
ip/ipnexthop.c

index 20cde586596b7434b94749b074f5785f742a3459..126b0b17cab409788ee195b42788857ede74c52b 100644 (file)
@@ -327,6 +327,15 @@ static int add_nh_group_attr(struct nlmsghdr *n, int maxlen, char *argv)
        return addattr_l(n, maxlen, NHA_GROUP, grps, count * sizeof(*grps));
 }
 
+static int ipnh_parse_id(const char *argv)
+{
+       __u32 id;
+
+       if (get_unsigned(&id, argv, 0))
+               invarg("invalid id value", argv);
+       return id;
+}
+
 static int ipnh_modify(int cmd, unsigned int flags, int argc, char **argv)
 {
        struct {
@@ -343,12 +352,9 @@ static int ipnh_modify(int cmd, unsigned int flags, int argc, char **argv)
 
        while (argc > 0) {
                if (!strcmp(*argv, "id")) {
-                       __u32 id;
-
                        NEXT_ARG();
-                       if (get_unsigned(&id, *argv, 0))
-                               invarg("invalid id value", *argv);
-                       addattr32(&req.n, sizeof(req), NHA_ID, id);
+                       addattr32(&req.n, sizeof(req), NHA_ID,
+                                 ipnh_parse_id(*argv));
                } else if (!strcmp(*argv, "dev")) {
                        int ifindex;
 
@@ -485,12 +491,8 @@ static int ipnh_list_flush(int argc, char **argv, int action)
                        if (!filter.master)
                                invarg("VRF does not exist\n", *argv);
                } else if (!strcmp(*argv, "id")) {
-                       __u32 id;
-
                        NEXT_ARG();
-                       if (get_unsigned(&id, *argv, 0))
-                               invarg("invalid id value", *argv);
-                       return ipnh_get_id(id);
+                       return ipnh_get_id(ipnh_parse_id(*argv));
                } else if (!matches(*argv, "protocol")) {
                        __u32 proto;
 
@@ -536,8 +538,7 @@ static int ipnh_get(int argc, char **argv)
        while (argc > 0) {
                if (!strcmp(*argv, "id")) {
                        NEXT_ARG();
-                       if (get_unsigned(&id, *argv, 0))
-                               invarg("invalid id value", *argv);
+                       id = ipnh_parse_id(*argv);
                } else  {
                        usage();
                }