]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
rtnetlink: add new function rtnl_echo_talk()
authorHangbin Liu <liuhangbin@gmail.com>
Thu, 29 Sep 2022 08:10:16 +0000 (16:10 +0800)
committerDavid Ahern <dsahern@kernel.org>
Thu, 29 Sep 2022 15:05:03 +0000 (09:05 -0600)
Add a new function rtnl_echo_talk() that could be used when the
sub-component supports NLM_F_ECHO flag. With this function we can
remove the redundant code added by commit b264b4c6568c7 ("ip: add
NLM_F_ECHO support").

Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
Signed-off-by: David Ahern <dsahern@kernel.org>
include/libnetlink.h
ip/ipaddress.c
ip/iplink.c
ip/ipnexthop.c
ip/iproute.c
ip/iprule.c
lib/libnetlink.c

index 1c49920d637ef9cfbebefce64412bf0df2aaedc3..c91a22314548bd6d96981261df56ba7ae3274ea9 100644 (file)
@@ -171,6 +171,9 @@ int rtnl_dump_filter_errhndlr_nc(struct rtnl_handle *rth,
 #define rtnl_dump_filter_errhndlr(rth, filter, farg, errhndlr, earg) \
        rtnl_dump_filter_errhndlr_nc(rth, filter, farg, errhndlr, earg, 0)
 
+int rtnl_echo_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, int json,
+                  int (*print_info)(struct nlmsghdr *n, void *arg))
+       __attribute__((warn_unused_result));
 int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
              struct nlmsghdr **answer)
        __attribute__((warn_unused_result));
index 986cfbc3c2691c0d71d919e8ab9474f9b468ec38..456545bb64504b322f626b82882e7ae08932d98b 100644 (file)
@@ -2422,11 +2422,6 @@ static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
        __u32 preferred_lft = INFINITY_LIFE_TIME;
        __u32 valid_lft = INFINITY_LIFE_TIME;
        unsigned int ifa_flags = 0;
-       struct nlmsghdr *answer;
-       int ret;
-
-       if (echo_request)
-               req.n.nlmsg_flags |= NLM_F_ECHO | NLM_F_ACK;
 
        while (argc > 0) {
                if (strcmp(*argv, "peer") == 0 ||
@@ -2609,23 +2604,9 @@ static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
        }
 
        if (echo_request)
-               ret = rtnl_talk(&rth, &req.n, &answer);
-       else
-               ret = rtnl_talk(&rth, &req.n, NULL);
-
-       if (ret < 0)
-               return -2;
+               return rtnl_echo_talk(&rth, &req.n, json, print_addrinfo);
 
-       if (echo_request) {
-               new_json_obj(json);
-               open_json_object(NULL);
-               print_addrinfo(answer, stdout);
-               close_json_object();
-               delete_json_obj();
-               free(answer);
-       }
-
-       return 0;
+       return rtnl_talk(&rth, &req.n, NULL);
 }
 
 int do_ipaddr(int argc, char **argv)
index c8f0b3841344a322251b1dd80a3e722ffc3f3f96..92ce6c47b486cb1b2f65f5f8de5028cfd3cbd5dd 100644 (file)
@@ -1073,16 +1073,12 @@ static int iplink_modify(int cmd, unsigned int flags, int argc, char **argv)
                .n.nlmsg_type = cmd,
                .i.ifi_family = preferred_family,
        };
-       struct nlmsghdr *answer;
        int ret;
 
        ret = iplink_parse(argc, argv, &req, &type);
        if (ret < 0)
                return ret;
 
-       if (echo_request)
-               req.n.nlmsg_flags |= NLM_F_ECHO | NLM_F_ACK;
-
        if (type) {
                struct link_util *lu;
                struct rtattr *linkinfo;
@@ -1128,21 +1124,12 @@ static int iplink_modify(int cmd, unsigned int flags, int argc, char **argv)
        }
 
        if (echo_request)
-               ret = rtnl_talk(&rth, &req.n, &answer);
+               ret = rtnl_echo_talk(&rth, &req.n, json, print_linkinfo);
        else
                ret = rtnl_talk(&rth, &req.n, NULL);
 
-       if (ret < 0)
-               return -2;
-
-       if (echo_request) {
-               new_json_obj(json);
-               open_json_object(NULL);
-               print_linkinfo(answer, stdout);
-               close_json_object();
-               delete_json_obj();
-               free(answer);
-       }
+       if (ret)
+               return ret;
 
        /* remove device from cache; next use can refresh with new data */
        ll_drop_by_index(req.i.ifi_index);
index 59f8f2fb0c140466cd9b29ab0d96b71ed6fedb0e..c87e847f345d38a09cf5363fc96ec9fec202ba51 100644 (file)
@@ -919,12 +919,7 @@ static int ipnh_modify(int cmd, unsigned int flags, int argc, char **argv)
                .n.nlmsg_type = cmd,
                .nhm.nh_family = preferred_family,
        };
-       struct nlmsghdr *answer;
        __u32 nh_flags = 0;
-       int ret;
-
-       if (echo_request)
-               req.n.nlmsg_flags |= NLM_F_ECHO | NLM_F_ACK;
 
        while (argc > 0) {
                if (!strcmp(*argv, "id")) {
@@ -1005,23 +1000,9 @@ static int ipnh_modify(int cmd, unsigned int flags, int argc, char **argv)
        req.nhm.nh_flags = nh_flags;
 
        if (echo_request)
-               ret = rtnl_talk(&rth, &req.n, &answer);
-       else
-               ret = rtnl_talk(&rth, &req.n, NULL);
-
-       if (ret < 0)
-               return -2;
+               return rtnl_echo_talk(&rth, &req.n, json, print_nexthop_nocache);
 
-       if (echo_request) {
-               new_json_obj(json);
-               open_json_object(NULL);
-               print_nexthop_nocache(answer, (void *)stdout);
-               close_json_object();
-               delete_json_obj();
-               free(answer);
-       }
-
-       return 0;
+       return rtnl_talk(&rth, &req.n, NULL);
 }
 
 static int ipnh_get_id(__u32 id)
index 4774aac0f884c74d5d5f4735f8a1ec1d6ca2d18f..8b2d1fbe963b46048721a981cd8d13331f2d069e 100644 (file)
@@ -1123,7 +1123,6 @@ static int iproute_modify(int cmd, unsigned int flags, int argc, char **argv)
        };
        char  mxbuf[256];
        struct rtattr *mxrta = (void *)mxbuf;
-       struct nlmsghdr *answer;
        unsigned int mxlock = 0;
        char  *d = NULL;
        int gw_ok = 0;
@@ -1134,7 +1133,6 @@ static int iproute_modify(int cmd, unsigned int flags, int argc, char **argv)
        int raw = 0;
        int type_ok = 0;
        __u32 nhid = 0;
-       int ret;
 
        if (cmd != RTM_DELROUTE) {
                req.r.rtm_protocol = RTPROT_BOOT;
@@ -1142,9 +1140,6 @@ static int iproute_modify(int cmd, unsigned int flags, int argc, char **argv)
                req.r.rtm_type = RTN_UNICAST;
        }
 
-       if (echo_request)
-               req.n.nlmsg_flags |= NLM_F_ECHO | NLM_F_ACK;
-
        mxrta->rta_type = RTA_METRICS;
        mxrta->rta_len = RTA_LENGTH(0);
 
@@ -1592,23 +1587,9 @@ static int iproute_modify(int cmd, unsigned int flags, int argc, char **argv)
                req.r.rtm_type = RTN_UNICAST;
 
        if (echo_request)
-               ret = rtnl_talk(&rth, &req.n, &answer);
-       else
-               ret = rtnl_talk(&rth, &req.n, NULL);
+               return rtnl_echo_talk(&rth, &req.n, json, print_route);
 
-       if (ret < 0)
-               return -2;
-
-       if (echo_request) {
-               new_json_obj(json);
-               open_json_object(NULL);
-               print_route(answer, (void *)stdout);
-               close_json_object();
-               delete_json_obj();
-               free(answer);
-       }
-
-       return 0;
+       return rtnl_talk(&rth, &req.n, NULL);
 }
 
 static int iproute_flush_cache(void)
index af77e62c9249c30e34c35fc4e9ef8c4333595f9d..8f7504253a8872205820021128d345bde3de1982 100644 (file)
@@ -787,11 +787,6 @@ static int iprule_modify(int cmd, int argc, char **argv)
                .frh.family = preferred_family,
                .frh.action = FR_ACT_UNSPEC,
        };
-       struct nlmsghdr *answer;
-       int ret;
-
-       if (echo_request)
-               req.n.nlmsg_flags |= NLM_F_ECHO | NLM_F_ACK;
 
        if (cmd == RTM_NEWRULE) {
                if (argc == 0) {
@@ -1022,23 +1017,9 @@ static int iprule_modify(int cmd, int argc, char **argv)
                req.frh.table = RT_TABLE_MAIN;
 
        if (echo_request)
-               ret = rtnl_talk(&rth, &req.n, &answer);
-       else
-               ret = rtnl_talk(&rth, &req.n, NULL);
-
-       if (ret < 0)
-               return -2;
-
-       if (echo_request) {
-               new_json_obj(json);
-               open_json_object(NULL);
-               print_rule(answer, stdout);
-               close_json_object();
-               delete_json_obj();
-               free(answer);
-       }
+               return rtnl_echo_talk(&rth, &req.n, json, print_rule);
 
-       return 0;
+       return rtnl_talk(&rth, &req.n, NULL);
 }
 
 int do_iprule(int argc, char **argv)
index 1461b1ca3e77ce857d36764af29182e75761a883..9af0623282e2baa59a69d3ffb9d13641362ecd46 100644 (file)
@@ -1138,6 +1138,28 @@ static int __rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
        return __rtnl_talk_iov(rtnl, &iov, 1, answer, show_rtnl_err, errfn);
 }
 
+int rtnl_echo_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, int json,
+                  int (*print_info)(struct nlmsghdr *n, void *arg))
+{
+       struct nlmsghdr *answer;
+       int ret;
+
+       n->nlmsg_flags |= NLM_F_ECHO | NLM_F_ACK;
+
+       ret = rtnl_talk(rtnl, n, &answer);
+       if (ret)
+               return ret;
+
+       new_json_obj(json);
+       open_json_object(NULL);
+       print_info(answer, stdout);
+       close_json_object();
+       delete_json_obj();
+       free(answer);
+
+       return 0;
+}
+
 int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
              struct nlmsghdr **answer)
 {