[PATCH 04/15] rtnl: Keep one global RTNL connection

Andrew Zaborowski <andrew.zaborowski at intel.com>
Newsgroups dev.linux.lists.ell
Message-ID <[email protected]>
To avoid keeping multiple RTNL sockets open, have a singleton l_netlink
rtnl object managed by rtnl.c and available for anyone who needs it to
request a reference.
---
In this version l_rtnl_get() and l_rtnl_put() take an optional rtnl
pointer to save the reference to, but I'm happy to drop the parameter
or use a different style API.  The parameter allows the user to easily
request the rtnl object lazily as in patch 5.

 ell/ell.sym |  2 ++
 ell/rtnl.c  | 41 +++++++++++++++++++++++++++++++++++++++++
 ell/rtnl.h  |  3 +++
 3 files changed, 46 insertions(+)

diff --git a/ell/ell.sym b/ell/ell.sym
index 0f9f884..ea4d06c 100644
--- a/ell/ell.sym
+++ b/ell/ell.sym
@@ -688,6 +688,8 @@ global:
 	l_rtnl_ifaddr_delete;
 	l_rtnl_neighbor_get_hwaddr;
 	l_rtnl_neighbor_set_hwaddr;
+	l_rtnl_get;
+	l_rtnl_put;
 	/* icmp6 */
 	l_icmp6_client_new;
 	l_icmp6_client_free;
diff --git a/ell/rtnl.c b/ell/rtnl.c
index b53d049..fe00b25 100644
--- a/ell/rtnl.c
+++ b/ell/rtnl.c
@@ -43,6 +43,9 @@
 #include "rtnl.h"
 #include "private.h"
 
+static struct l_netlink *rtnl;
+static unsigned int rtnl_use_count;
+
 struct l_rtnl_address {
 	uint8_t family;
 	uint8_t prefix_len;
@@ -1556,3 +1559,41 @@ LIB_EXPORT uint32_t l_rtnl_neighbor_set_hwaddr(struct l_netlink *rtnl,
 				ndmsg, rta_buf - (void *) ndmsg,
 				cb, user_data, destroy);
 }
+
+LIB_EXPORT struct l_netlink *l_rtnl_get(struct l_netlink **ptr)
+
+{
+	if (ptr && *ptr)
+		return *ptr;
+
+	if (!rtnl) {
+		rtnl = l_netlink_new(NETLINK_ROUTE);
+
+		if (unlikely(!rtnl))
+			return NULL;
+	}
+
+	if (ptr)
+		*ptr = rtnl;
+
+	rtnl_use_count++;
+	return rtnl;
+}
+
+LIB_EXPORT void l_rtnl_put(struct l_netlink **ptr)
+{
+	if (ptr) {
+		if (!*ptr)
+			return;
+
+		*ptr = NULL;
+	}
+
+	if (L_WARN_ON(!rtnl_use_count))
+		return;
+
+	--rtnl_use_count;
+
+	if (!rtnl_use_count)
+		l_netlink_destroy(l_steal_ptr(rtnl));
+}
diff --git a/ell/rtnl.h b/ell/rtnl.h
index 2617b1c..4409339 100644
--- a/ell/rtnl.h
+++ b/ell/rtnl.h
@@ -226,6 +226,9 @@ uint32_t l_rtnl_neighbor_set_hwaddr(struct l_netlink *rtnl, int ifindex,
 					void *user_data,
 					l_netlink_destroy_func_t destroy);
 
+struct l_netlink *l_rtnl_get(struct l_netlink **ptr);
+void l_rtnl_put(struct l_netlink **ptr);
+
 #ifdef __cplusplus
 }
 #endif
-- 
2.32.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.