diff mbox series

[iproute2-next] ip: nexthop: keep cache netlink socket open

Message ID 20211004090328.2329012-1-razor@blackwall.org (mailing list archive)
State Accepted
Delegated to: David Ahern
Headers show
Series [iproute2-next] ip: nexthop: keep cache netlink socket open | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Nikolay Aleksandrov Oct. 4, 2021, 9:03 a.m. UTC
From: Nikolay Aleksandrov <nikolay@nvidia.com>

Since we use the cache netlink socket for each nexthop we can keep it open
instead of opening and closing it on every add call. The socket is opened
once, on the first add call and then reused for the rest.

Suggested-by: David Ahern <dsahern@gmail.com>
Signed-off-by: Nikolay Aleksandrov <nikolay@nvidia.com>
---
I actually had this in my initial patchset, but switched it with the
open/close on each call. TBH, I don't recall why, perhaps to be the same
as the link cache. I don't see a reason not to keep the socket open.

I've re-run the stress test and the selftests, all look good.

 ip/ipnexthop.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

Comments

David Ahern Oct. 5, 2021, 2:35 p.m. UTC | #1
On 10/4/21 3:03 AM, Nikolay Aleksandrov wrote:
> From: Nikolay Aleksandrov <nikolay@nvidia.com>
> 
> Since we use the cache netlink socket for each nexthop we can keep it open
> instead of opening and closing it on every add call. The socket is opened
> once, on the first add call and then reused for the rest.
> 
> Suggested-by: David Ahern <dsahern@gmail.com>
> Signed-off-by: Nikolay Aleksandrov <nikolay@nvidia.com>
> ---
> I actually had this in my initial patchset, but switched it with the
> open/close on each call. TBH, I don't recall why, perhaps to be the same
> as the link cache. I don't see a reason not to keep the socket open.
> 
> I've re-run the stress test and the selftests, all look good.
> 
>  ip/ipnexthop.c | 9 +++++----
>  1 file changed, 5 insertions(+), 4 deletions(-)
> 

applied to iproute2-next. Thanks,
diff mbox series

Patch

diff --git a/ip/ipnexthop.c b/ip/ipnexthop.c
index b4d44a86429c..83a5540e771c 100644
--- a/ip/ipnexthop.c
+++ b/ip/ipnexthop.c
@@ -35,6 +35,7 @@  enum {
 			NLMSG_ALIGN(sizeof(struct nhmsg))))
 
 static struct hlist_head nh_cache[NH_CACHE_SIZE];
+static struct rtnl_handle nh_cache_rth = { .fd = -1 };
 
 static void usage(void) __attribute__((noreturn));
 
@@ -563,14 +564,15 @@  static int __ipnh_cache_parse_nlmsg(const struct nlmsghdr *n,
 
 static struct nh_entry *ipnh_cache_add(__u32 nh_id)
 {
-	struct rtnl_handle cache_rth = { .fd = -1 };
 	struct nlmsghdr *answer = NULL;
 	struct nh_entry *nhe = NULL;
 
-	if (rtnl_open(&cache_rth, 0) < 0)
+	if (nh_cache_rth.fd < 0 && rtnl_open(&nh_cache_rth, 0) < 0) {
+		nh_cache_rth.fd = -1;
 		goto out;
+	}
 
-	if (__ipnh_get_id(&cache_rth, nh_id, &answer) < 0)
+	if (__ipnh_get_id(&nh_cache_rth, nh_id, &answer) < 0)
 		goto out;
 
 	nhe = malloc(sizeof(*nhe));
@@ -585,7 +587,6 @@  static struct nh_entry *ipnh_cache_add(__u32 nh_id)
 out:
 	if (answer)
 		free(answer);
-	rtnl_close(&cache_rth);
 
 	return nhe;