[PATCH v2 2/2] iproute: codeshrink initalization of netlink request

Natanael Copa ncopa at alpinelinux.org
Tue Feb 4 12:23:40 UTC 2020


reduce size by de-duplicate initialization of netlink reqest structure
initialization.

function                                             old     new   delta
init_rtnlmsg_rtmsg_req                                 -      34     +34
init_nlmsg_ifinfomsg_req                               -      34     +34
set_master                                           191     171     -20
iproute_modify                                      1241    1218     -23
iproute_get                                          880     857     -23
do_add_or_delete                                    1344    1319     -25
iprule_modify                                        955     928     -27
------------------------------------------------------------------------------

v2: unmodified, but re-sent for convienience.

(add/remove: 2/0 grow/shrink: 0/5 up/down: 68/-118)           Total: -50
bytes
   text	   data	    bss	    dec	    hex	filename
 876517	  15636	   2080	 894233	  da519	busybox_old
 876467	  15636	   2080	 894183	  da4e7	busybox_unstripped
---
 networking/libiproute/ip_common.h |  7 ++++++
 networking/libiproute/iplink.c    | 37 ++++++++++++++-----------------
 networking/libiproute/iproute.c   | 35 ++++++++++++-----------------
 networking/libiproute/iprule.c    | 12 ++--------
 4 files changed, 40 insertions(+), 51 deletions(-)

diff --git a/networking/libiproute/ip_common.h b/networking/libiproute/ip_common.h
index 15f9bb4df..5230a5b20 100644
--- a/networking/libiproute/ip_common.h
+++ b/networking/libiproute/ip_common.h
@@ -15,6 +15,13 @@
 
 PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
 
+struct rtnlmsg_rtmsg_req {
+	struct nlmsghdr n;
+	struct rtmsg    r;
+	char            buf[1024];
+};
+
+void FAST_FUNC init_rtnlmsg_rtmsg_req(struct rtnlmsg_rtmsg_req *req);
 char FAST_FUNC **ip_parse_common_args(char **argv);
 //int FAST_FUNC print_neigh(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg);
 int FAST_FUNC ipaddr_list_or_flush(char **argv, int flush);
diff --git a/networking/libiproute/iplink.c b/networking/libiproute/iplink.c
index 1a1064bdc..32a0613e0 100644
--- a/networking/libiproute/iplink.c
+++ b/networking/libiproute/iplink.c
@@ -43,6 +43,12 @@ struct ifla_vlan_flags {
 };
 #endif
 
+struct nlmsg_ifinfomsg_req {
+	struct nlmsghdr  n;
+	struct ifinfomsg i;
+	char             buf[1024];
+};
+
 /* taken from linux/sockios.h */
 #define SIOCSIFNAME  0x8923  /* set interface name */
 
@@ -55,6 +61,13 @@ struct ifla_vlan_flags {
 
 #define str_on_off "on\0""off\0"
 
+static void init_nlmsg_ifinfomsg_req(struct nlmsg_ifinfomsg_req *req) {
+	memset(req, 0, sizeof(*req));
+	req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
+	req->n.nlmsg_flags = NLM_F_REQUEST;
+	req->i.ifi_family = preferred_family;
+}
+
 /* Exits on error */
 static int get_ctl_fd(void)
 {
@@ -131,18 +144,10 @@ static void set_mtu(char *dev, int mtu)
 static void set_master(char *dev, int master)
 {
 	struct rtnl_handle rth;
-	struct {
-		struct nlmsghdr  n;
-		struct ifinfomsg i;
-		char             buf[1024];
-	} req;
-
-	memset(&req, 0, sizeof(req));
+	struct nlmsg_ifinfomsg_req req;
 
-	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
-	req.n.nlmsg_flags = NLM_F_REQUEST;
+	init_nlmsg_ifinfomsg_req(&req);
 	req.n.nlmsg_type = RTM_NEWLINK;
-	req.i.ifi_family = preferred_family;
 
 	xrtnl_open(&rth);
 	req.i.ifi_index = xll_name_to_index(dev);
@@ -596,11 +601,7 @@ static int do_add_or_delete(char **argv, const unsigned rtm)
 		ARG_address,
 	};
 	struct rtnl_handle rth;
-	struct {
-		struct nlmsghdr  n;
-		struct ifinfomsg i;
-		char             buf[1024];
-	} req;
+	struct nlmsg_ifinfomsg_req req;
 	smalluint arg;
 	char *name_str = NULL;
 	char *link_str = NULL;
@@ -608,12 +609,8 @@ static int do_add_or_delete(char **argv, const unsigned rtm)
 	char *dev_str = NULL;
 	char *address_str = NULL;
 
-	memset(&req, 0, sizeof(req));
-
-	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
-	req.n.nlmsg_flags = NLM_F_REQUEST;
+	init_nlmsg_ifinfomsg_req(&req);
 	req.n.nlmsg_type = rtm;
-	req.i.ifi_family = preferred_family;
 	if (rtm == RTM_NEWLINK)
 		req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
 
diff --git a/networking/libiproute/iproute.c b/networking/libiproute/iproute.c
index 9a58fdbb5..cf562fd5f 100644
--- a/networking/libiproute/iproute.c
+++ b/networking/libiproute/iproute.c
@@ -348,6 +348,15 @@ static int str_is_lock(const char *str)
 	return strcmp(str, "lock") == 0;
 }
 
+void init_rtnlmsg_rtmsg_req(struct rtnlmsg_rtmsg_req *req)
+{
+	memset(req, 0, sizeof(*req));
+	req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
+	if (NLM_F_REQUEST)
+		req->n.nlmsg_flags = NLM_F_REQUEST;
+	req->r.rtm_family = preferred_family;
+}
+
 /* Return value becomes exitcode. It's okay to not return at all */
 static int iproute_modify(int cmd, unsigned flags, char **argv)
 {
@@ -384,11 +393,7 @@ IF_FEATURE_IP_RULE(ARG_table,)
 		type_ok = 1 << 3
 	};
 	struct rtnl_handle rth;
-	struct {
-		struct nlmsghdr n;
-		struct rtmsg    r;
-		char            buf[1024];
-	} req;
+	struct rtnlmsg_rtmsg_req req;
 	char mxbuf[256];
 	struct rtattr * mxrta = (void*)mxbuf;
 	unsigned mxlock = 0;
@@ -397,12 +402,10 @@ IF_FEATURE_IP_RULE(ARG_table,)
 	smalluint scope_ok = 0;
 	int arg;
 
-	memset(&req, 0, sizeof(req));
+	init_rtnlmsg_rtmsg_req(&req);
 
-	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
-	req.n.nlmsg_flags = NLM_F_REQUEST | flags;
+	req.n.nlmsg_flags |= flags;
 	req.n.nlmsg_type = cmd;
-	req.r.rtm_family = preferred_family;
 	if (RT_TABLE_MAIN != 0) /* if it is zero, memset already did it */
 		req.r.rtm_table = RT_TABLE_MAIN;
 	if (RT_SCOPE_NOWHERE != 0)
@@ -961,16 +964,11 @@ static int iproute_list_or_flush(char **argv, int flush)
 	return 0;
 }
 
-
 /* Return value becomes exitcode. It's okay to not return at all */
 static int iproute_get(char **argv)
 {
 	struct rtnl_handle rth;
-	struct {
-		struct nlmsghdr n;
-		struct rtmsg    r;
-		char            buf[1024];
-	} req;
+	struct rtnlmsg_rtmsg_req req;
 	char *idev = NULL;
 	char *odev = NULL;
 	bool connected = 0;
@@ -978,16 +976,11 @@ static int iproute_get(char **argv)
 	static const char options[] ALIGN1 =
 		"from\0""iif\0""oif\0""dev\0""notify\0""connected\0""to\0";
 
-	memset(&req, 0, sizeof(req));
-
+	init_rtnlmsg_rtmsg_req(&req);
 	iproute_reset_filter();
 
-	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
-	if (NLM_F_REQUEST)
-		req.n.nlmsg_flags = NLM_F_REQUEST;
 	if (RTM_GETROUTE)
 		req.n.nlmsg_type = RTM_GETROUTE;
-	req.r.rtm_family = preferred_family;
 	/*req.r.rtm_table = 0; - memset did this already */
 	/*req.r.rtm_protocol = 0;*/
 	/*req.r.rtm_scope = 0;*/
diff --git a/networking/libiproute/iprule.c b/networking/libiproute/iprule.c
index 0ce0dfeef..5ac34149a 100644
--- a/networking/libiproute/iprule.c
+++ b/networking/libiproute/iprule.c
@@ -201,19 +201,11 @@ static int iprule_modify(int cmd, char **argv)
 {
 	bool table_ok = 0;
 	struct rtnl_handle rth;
-	struct {
-		struct nlmsghdr n;
-		struct rtmsg    r;
-		char            buf[1024];
-	} req;
+	struct rtnlmsg_rtmsg_req req;
 	smalluint key;
 
-	memset(&req, 0, sizeof(req));
-
+	init_rtnlmsg_rtmsg_req(&req);
 	req.n.nlmsg_type = cmd;
-	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
-	req.n.nlmsg_flags = NLM_F_REQUEST;
-	req.r.rtm_family = preferred_family;
 	req.r.rtm_protocol = RTPROT_BOOT;
 	if (RT_SCOPE_UNIVERSE != 0)
 		req.r.rtm_scope = RT_SCOPE_UNIVERSE;
-- 
2.25.0



More information about the busybox mailing list