diff mbox series

[v2,iproute2,1/2] ip: Move of set_netnsid_from_name() to namespace.c

Message ID 20241011080111.387028-2-shaw.leon@gmail.com (mailing list archive)
State Accepted
Commit 18bbd74b345fda2be7246a65bb280294a4c06c17
Delegated to: Stephen Hemminger
Headers show
Series iplink: Fix link-netns handling | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Xiao Liang Oct. 11, 2024, 8:01 a.m. UTC
Move set_netnsid_from_name() outside for reuse, like what's done for
netns_id_from_name().

Signed-off-by: Xiao Liang <shaw.leon@gmail.com>
---
 include/namespace.h |  2 ++
 ip/ip_common.h      |  2 --
 ip/iplink.c         |  6 +++---
 ip/ipnetns.c        | 28 +++-------------------------
 lib/namespace.c     | 27 +++++++++++++++++++++++++++
 5 files changed, 35 insertions(+), 30 deletions(-)
diff mbox series

Patch

diff --git a/include/namespace.h b/include/namespace.h
index 86000543..98f4af59 100644
--- a/include/namespace.h
+++ b/include/namespace.h
@@ -60,6 +60,8 @@  struct netns_func {
 };
 
 int netns_id_from_name(struct rtnl_handle *rtnl, const char *name);
+int set_netns_id_from_name(struct rtnl_handle *rtnl, const char *name,
+			   int nsid);
 char *netns_name_from_id(int32_t id);
 
 #endif /* __NAMESPACE_H__ */
diff --git a/ip/ip_common.h b/ip/ip_common.h
index 625311c2..726262ab 100644
--- a/ip/ip_common.h
+++ b/ip/ip_common.h
@@ -63,8 +63,6 @@  void netns_nsid_socket_init(void);
 int print_nsid(struct nlmsghdr *n, void *arg);
 int ipstats_print(struct nlmsghdr *n, void *arg);
 char *get_name_from_nsid(int nsid);
-int get_netnsid_from_name(const char *name);
-int set_netnsid_from_name(const char *name, int nsid);
 int do_ipaddr(int argc, char **argv);
 int do_ipaddrlabel(int argc, char **argv);
 int do_iproute(int argc, char **argv);
diff --git a/ip/iplink.c b/ip/iplink.c
index 0dd83ff4..c9168985 100644
--- a/ip/iplink.c
+++ b/ip/iplink.c
@@ -819,11 +819,11 @@  int iplink_parse(int argc, char **argv, struct iplink_req *req, char **type)
 			NEXT_ARG();
 			if (link_netnsid != -1)
 				duparg("link-netns/link-netnsid", *argv);
-			link_netnsid = get_netnsid_from_name(*argv);
+			link_netnsid = netns_id_from_name(&rth, *argv);
 			/* No nsid? Try to assign one. */
 			if (link_netnsid < 0)
-				set_netnsid_from_name(*argv, -1);
-			link_netnsid = get_netnsid_from_name(*argv);
+				set_netns_id_from_name(&rth, *argv, -1);
+			link_netnsid = netns_id_from_name(&rth, *argv);
 			if (link_netnsid < 0)
 				invarg("Invalid \"link-netns\" value\n",
 				       *argv);
diff --git a/ip/ipnetns.c b/ip/ipnetns.c
index 972d7e9c..5c943400 100644
--- a/ip/ipnetns.c
+++ b/ip/ipnetns.c
@@ -104,7 +104,7 @@  static int ipnetns_have_nsid(void)
 	return have_rtnl_getnsid;
 }
 
-int get_netnsid_from_name(const char *name)
+static int get_netnsid_from_name(const char *name)
 {
 	netns_nsid_socket_init();
 
@@ -896,33 +896,11 @@  out_delete:
 	return -1;
 }
 
-int set_netnsid_from_name(const char *name, int nsid)
+static int set_netnsid_from_name(const char *name, int nsid)
 {
-	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_NEWNSID,
-		.g.rtgen_family = AF_UNSPEC,
-	};
-	int fd, err = 0;
-
 	netns_nsid_socket_init();
 
-	fd = netns_get_fd(name);
-	if (fd < 0)
-		return fd;
-
-	addattr32(&req.n, 1024, NETNSA_FD, fd);
-	addattr32(&req.n, 1024, NETNSA_NSID, nsid);
-	if (rtnl_talk(&rth, &req.n, NULL) < 0)
-		err = -2;
-
-	close(fd);
-	return err;
+	return set_netns_id_from_name(&rth, name, nsid);
 }
 
 static int netns_set(int argc, char **argv)
diff --git a/lib/namespace.c b/lib/namespace.c
index d3aeb965..74b7e7ca 100644
--- a/lib/namespace.c
+++ b/lib/namespace.c
@@ -189,6 +189,33 @@  out:
 	return ret;
 }
 
+int set_netns_id_from_name(struct rtnl_handle *rtnl, const char *name, int nsid)
+{
+	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_NEWNSID,
+		.g.rtgen_family = AF_UNSPEC,
+	};
+	int fd, err = 0;
+
+	fd = netns_get_fd(name);
+	if (fd < 0)
+		return fd;
+
+	addattr32(&req.n, 1024, NETNSA_FD, fd);
+	addattr32(&req.n, 1024, NETNSA_NSID, nsid);
+	if (rtnl_talk(rtnl, &req.n, NULL) < 0)
+		err = -2;
+
+	close(fd);
+	return err;
+}
+
 struct netns_name_from_id_ctx {
 	int32_t id;
 	char *name;