diff mbox series

[net-next,v4,1/7] ip/ipnetns: move internals of get_netnsid_from_name() into namespace.c

Message ID 20231027101403.958745-2-jiri@resnulli.us (mailing list archive)
State Superseded
Delegated to: David Ahern
Headers show
Series expose devlink instances relationships | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for net-next
netdev/apply fail Patch does not apply to net-next

Commit Message

Jiri Pirko Oct. 27, 2023, 10:13 a.m. UTC
From: Jiri Pirko <jiri@nvidia.com>

In order to be able to reuse get_netnsid_from_name() function outside of
ip code, move the internals to lib/namespace.c to a new function called
netns_id_from_name().

Signed-off-by: Jiri Pirko <jiri@nvidia.com>
---
v3->v4:
- removed namespace.h include
v2->v3:
- s/netns_netnsid_from_name/netns_id_from_name/
v1->v2:
- new patch
---
 include/namespace.h |  2 ++
 ip/ipnetns.c        | 45 +----------------------------------------
 lib/namespace.c     | 49 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 52 insertions(+), 44 deletions(-)

Comments

David Ahern Nov. 6, 2023, 5:11 p.m. UTC | #1
On 10/27/23 4:13 AM, Jiri Pirko wrote:
> From: Jiri Pirko <jiri@nvidia.com>
> 
> In order to be able to reuse get_netnsid_from_name() function outside of
> ip code, move the internals to lib/namespace.c to a new function called
> netns_id_from_name().
> 
> Signed-off-by: Jiri Pirko <jiri@nvidia.com>
> ---
> v3->v4:
> - removed namespace.h include
> v2->v3:
> - s/netns_netnsid_from_name/netns_id_from_name/
> v1->v2:
> - new patch
> ---
>  include/namespace.h |  2 ++
>  ip/ipnetns.c        | 45 +----------------------------------------
>  lib/namespace.c     | 49 +++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 52 insertions(+), 44 deletions(-)
> 


dcb
    CC       dcb.o
In file included from dcb.c:11:
../include/namespace.h:61:31: warning: ‘struct rtnl_handle’ declared
inside parameter list will not be visible outside of this definition or
declaration
   61 | int netns_id_from_name(struct rtnl_handle *rtnl, const char *name);
      |                               ^~~~~~~~~~~

--
pw-bot: cr
Jiri Pirko Nov. 7, 2023, 7:24 a.m. UTC | #2
Mon, Nov 06, 2023 at 06:11:29PM CET, dsahern@gmail.com wrote:
>On 10/27/23 4:13 AM, Jiri Pirko wrote:
>> From: Jiri Pirko <jiri@nvidia.com>
>> 
>> In order to be able to reuse get_netnsid_from_name() function outside of
>> ip code, move the internals to lib/namespace.c to a new function called
>> netns_id_from_name().
>> 
>> Signed-off-by: Jiri Pirko <jiri@nvidia.com>
>> ---
>> v3->v4:
>> - removed namespace.h include
>> v2->v3:
>> - s/netns_netnsid_from_name/netns_id_from_name/
>> v1->v2:
>> - new patch
>> ---
>>  include/namespace.h |  2 ++
>>  ip/ipnetns.c        | 45 +----------------------------------------
>>  lib/namespace.c     | 49 +++++++++++++++++++++++++++++++++++++++++++++
>>  3 files changed, 52 insertions(+), 44 deletions(-)
>> 
>
>
>dcb
>    CC       dcb.o
>In file included from dcb.c:11:
>../include/namespace.h:61:31: warning: ‘struct rtnl_handle’ declared
>inside parameter list will not be visible outside of this definition or
>declaration
>   61 | int netns_id_from_name(struct rtnl_handle *rtnl, const char *name);
>      |                               ^~~~~~~~~~~

Ah, I wonder why I didn't hit it. Will fix, thanks!

>
>--
>pw-bot: cr
diff mbox series

Patch

diff --git a/include/namespace.h b/include/namespace.h
index e47f9b5d49d1..6483630b8082 100644
--- a/include/namespace.h
+++ b/include/namespace.h
@@ -58,4 +58,6 @@  struct netns_func {
 	void *arg;
 };
 
+int netns_id_from_name(struct rtnl_handle *rtnl, const char *name);
+
 #endif /* __NAMESPACE_H__ */
diff --git a/ip/ipnetns.c b/ip/ipnetns.c
index 9d996832aef8..0ae46a874a0c 100644
--- a/ip/ipnetns.c
+++ b/ip/ipnetns.c
@@ -105,52 +105,9 @@  static int ipnetns_have_nsid(void)
 
 int get_netnsid_from_name(const char *name)
 {
-	struct {
-		struct nlmsghdr n;
-		struct rtgenmsg g;
-		char            buf[1024];
-	} req = {
-		.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
-		.n.nlmsg_flags = NLM_F_REQUEST,
-		.n.nlmsg_type = RTM_GETNSID,
-		.g.rtgen_family = AF_UNSPEC,
-	};
-	struct nlmsghdr *answer;
-	struct rtattr *tb[NETNSA_MAX + 1];
-	struct rtgenmsg *rthdr;
-	int len, fd, ret = -1;
-
 	netns_nsid_socket_init();
 
-	fd = netns_get_fd(name);
-	if (fd < 0)
-		return fd;
-
-	addattr32(&req.n, 1024, NETNSA_FD, fd);
-	if (rtnl_talk(&rtnsh, &req.n, &answer) < 0) {
-		close(fd);
-		return -2;
-	}
-	close(fd);
-
-	/* Validate message and parse attributes */
-	if (answer->nlmsg_type == NLMSG_ERROR)
-		goto out;
-
-	rthdr = NLMSG_DATA(answer);
-	len = answer->nlmsg_len - NLMSG_SPACE(sizeof(*rthdr));
-	if (len < 0)
-		goto out;
-
-	parse_rtattr(tb, NETNSA_MAX, NETNS_RTA(rthdr), len);
-
-	if (tb[NETNSA_NSID]) {
-		ret = rta_getattr_s32(tb[NETNSA_NSID]);
-	}
-
-out:
-	free(answer);
-	return ret;
+	return netns_id_from_name(&rtnsh, name);
 }
 
 struct nsid_cache {
diff --git a/lib/namespace.c b/lib/namespace.c
index 1202fa85f97d..f03f4bbabceb 100644
--- a/lib/namespace.c
+++ b/lib/namespace.c
@@ -7,9 +7,11 @@ 
 #include <fcntl.h>
 #include <dirent.h>
 #include <limits.h>
+#include <linux/net_namespace.h>
 
 #include "utils.h"
 #include "namespace.h"
+#include "libnetlink.h"
 
 static void bind_etc(const char *name)
 {
@@ -139,3 +141,50 @@  int netns_foreach(int (*func)(char *nsname, void *arg), void *arg)
 	closedir(dir);
 	return 0;
 }
+
+int netns_id_from_name(struct rtnl_handle *rtnl, const char *name)
+{
+	struct {
+		struct nlmsghdr n;
+		struct rtgenmsg g;
+		char            buf[1024];
+	} req = {
+		.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
+		.n.nlmsg_flags = NLM_F_REQUEST,
+		.n.nlmsg_type = RTM_GETNSID,
+		.g.rtgen_family = AF_UNSPEC,
+	};
+	struct nlmsghdr *answer;
+	struct rtattr *tb[NETNSA_MAX + 1];
+	struct rtgenmsg *rthdr;
+	int len, fd, ret = -1;
+
+	fd = netns_get_fd(name);
+	if (fd < 0)
+		return fd;
+
+	addattr32(&req.n, 1024, NETNSA_FD, fd);
+	if (rtnl_talk(rtnl, &req.n, &answer) < 0) {
+		close(fd);
+		return -2;
+	}
+	close(fd);
+
+	/* Validate message and parse attributes */
+	if (answer->nlmsg_type == NLMSG_ERROR)
+		goto out;
+
+	rthdr = NLMSG_DATA(answer);
+	len = answer->nlmsg_len - NLMSG_SPACE(sizeof(*rthdr));
+	if (len < 0)
+		goto out;
+
+	parse_rtattr(tb, NETNSA_MAX, NETNS_RTA(rthdr), len);
+
+	if (tb[NETNSA_NSID])
+		ret = rta_getattr_s32(tb[NETNSA_NSID]);
+
+out:
+	free(answer);
+	return ret;
+}