Skip to contentSkip to conversation

Allow rdma dev netns to take a pid

PATCH In review David Ahern 3 participants 11 messages May 12 – May 15

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover Allow rdma dev netns to take a pid

From: David Ahern <[email protected]>

Avoid the extra hurdle of creating an entry in /var/run/netns and allow the netns argument to be a name or a pid.

v2
- update netns_get_fd to handle the pid fallback
- update devlink code

David Ahern (4):
namespace: Add fallback to netns by pid
iplink: Drop pid fallback code for netns
rdma: Allow netns to be specified by pid
devlink: Drop now duplicate pid fallback for netns

 devlink/devlink.c   | 25 +++++++------------------
 ip/iplink.c         | 10 +---------
 lib/namespace.c     | 28 +++++++++++++++++++++-------
 man/man8/rdma-dev.8 |  3 ++-
 rdma/dev.c          | 11 ++++-------
 5 files changed, 35 insertions(+), 42 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/4 namespace: Add fallback to netns by pid

From: David Ahern <[email protected]>

Update netns_get_fd to try the passed in string as a name first. If the netns filesystem entry does not exist, try converting the string to an integer and if successful return an fd to /proc/<pid>/ns/net.

This allows netns_get_fd to uniformly handle the 2 common use cases of specifying network namespace by name and pid.

---
 lib/namespace.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
lib/namespace.c +21 -7
index 74b7e7caf0e5..0bba3a163563 100644
@@ -99,19 +99,33 @@ int netns_switch(char *name)
 	return 0;
 }
 
-int netns_get_fd(const char *name)
+/* try str as the name of the namespace first, then
+ * fallback to pid
+ */
+int netns_get_fd(const char *str)
 {
 	char pathbuf[PATH_MAX];
-	const char *path, *ptr;
+	const char *path;
+	int pid;
+	int fd;
 
-	path = name;
-	ptr = strchr(name, '/');
-	if (!ptr) {
+	path = str;
+	if (!strchr(str, '/')) {
 		snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
-			NETNS_RUN_DIR, name );
+			NETNS_RUN_DIR, str);
 		path = pathbuf;
 	}
-	return open(path, O_RDONLY);
+
+	fd = open(path, O_RDONLY);
+	if (fd >= 0)
+		return fd;
+
+	/* make sure string is an integer */
+	if (get_integer(&pid, str, 0) < 0)
+		return -1;
+
+	snprintf(pathbuf, sizeof(pathbuf), "/proc/%s/ns/net", str);
+	return open(pathbuf, O_RDONLY);
 }
 
 int netns_foreach(int (*func)(char *nsname, void *arg), void *arg)
Signature · 1 line
2.43.0
#3 lore raw

PATCH v2 2/4 iplink: Drop pid fallback code for netns

In reply to David Ahern

From: David Ahern <[email protected]>

Prior commit handles the fallback to pid, so remove the now duplicate code from iplink_parse.

---
 ip/iplink.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
ip/iplink.c +1 -9
index eae51438d0be..2c052d2970ca 100644
@@ -650,19 +650,11 @@ int iplink_parse(int argc, char **argv, struct iplink_req *req, char **type)
 			if (offload && name == dev)
 				dev = NULL;
 		} else if (strcmp(*argv, "netns") == 0) {
-			int pid;
-
 			NEXT_ARG();
 			if (netns != -1)
 				duparg("netns", *argv);
+			/* try by name then by pid */
 			netns = netns_get_fd(*argv);
-			if (netns < 0 && get_integer(&pid, *argv, 0) == 0) {
-				char path[PATH_MAX];
-
-				snprintf(path, sizeof(path), "/proc/%d/ns/net",
-					 pid);
-				netns = open(path, O_RDONLY);
-			}
 			if (netns < 0)
 				invarg("Invalid \"netns\" value\n", *argv);
 
Signature · 1 line
2.43.0
#5 lore raw

PATCH v2 3/4 rdma: Allow netns to be specified by pid

In reply to David Ahern

From: David Ahern <[email protected]>

Update rdma dev to work like ip link where the netns can be a name or a pid by using netns_get_fd. Update man page and help accordingly.

---
 man/man8/rdma-dev.8 |  3 ++-
 rdma/dev.c          | 11 ++++-------
 2 files changed, 6 insertions(+), 8 deletions(-)
man/man8/rdma-dev.8 +2 -1
index abc9f405ede5..34ea1c614ca4 100644
@@ -32,7 +32,7 @@ rdma-dev \- RDMA device configuration
 .B rdma dev set
 .RI "[ " DEV " ]"
 .BR netns
-.BR NSNAME
+.BR { NSNAME | PID }
 
 .ti -8
 .B rdma dev set
@@ -104,6 +104,7 @@ Renames the mlx5_3 device to rdma_0.
 .RE
 .PP
 rdma dev set mlx5_3 netns foo
+rdma dev set mlx5_3 netns 1234
 .RS 4
 Changes the network namespace of RDMA device to foo where foo was
 previously created using the iproute2 ip command.
rdma/dev.c +4 -7
index fd60c1a0e856..c8537912e48e 100644
@@ -6,6 +6,7 @@
 
 #include <fcntl.h>
 #include "rdma.h"
+#include "namespace.h"
 
 static int dev_help(struct rd *rd)
 {
@@ -13,7 +14,7 @@ static int dev_help(struct rd *rd)
 	pr_out("       %s dev add DEVNAME type TYPE parent PARENT_DEVNAME\n", rd->filename);
 	pr_out("       %s dev delete DEVNAME\n", rd->filename);
 	pr_out("       %s dev set [DEV] name DEVNAME\n", rd->filename);
-	pr_out("       %s dev set [DEV] netns NSNAME\n", rd->filename);
+	pr_out("       %s dev set [DEV] netns { NSNAME | PID }\n", rd->filename);
 	pr_out("       %s dev set [DEV] adaptive-moderation [on|off]\n", rd->filename);
 	return 0;
 }
@@ -311,7 +312,7 @@ static int dev_set_name(struct rd *rd)
 
 static int dev_set_netns(struct rd *rd)
 {
-	char *netns_path;
+	char *arg = rd_argv(rd);
 	uint32_t seq;
 	int netns;
 	int ret;
@@ -321,10 +322,7 @@ static int dev_set_netns(struct rd *rd)
 		return -EINVAL;
 	}
 
-	if (asprintf(&netns_path, "%s/%s", NETNS_RUN_DIR, rd_argv(rd)) < 0)
-		return -ENOMEM;
-
-	netns = open(netns_path, O_RDONLY | O_CLOEXEC);
+	netns = netns_get_fd(arg);
 	if (netns < 0) {
 		fprintf(stderr, "Cannot open network namespace \"%s\": %s\n",
 			rd_argv(rd), strerror(errno));
@@ -339,7 +337,6 @@ static int dev_set_netns(struct rd *rd)
 	ret = rd_sendrecv_msg(rd, seq);
 	close(netns);
 done:
-	free(netns_path);
 	return ret;
 }
 
Signature · 1 line
2.43.0
#7 lore raw

PATCH v2 4/4 devlink: Drop now duplicate pid fallback for netns

In reply to David Ahern

From: David Ahern <[email protected]>

Now that netns_get_fd handles by name and pid, the special handling in devlink to fallback to PID can be removed with both cases handled by the FD attribute.

---
 devlink/devlink.c | 25 +++++++------------------
 1 file changed, 7 insertions(+), 18 deletions(-)
devlink/devlink.c +7 -18
index 730515a78950..908a0e32be2b 100644
@@ -428,12 +428,6 @@ static void dl_arg_inc(struct dl *dl)
 	dl->argv++;
 }
 
-static void dl_arg_dec(struct dl *dl)
-{
-	dl->argc++;
-	dl->argv--;
-}
-
 static char *dl_argv_next(struct dl *dl)
 {
 	char *ret;
@@ -2153,14 +2147,12 @@ static int dl_argv_parse(struct dl *dl, uint64_t o_required,
 			err = dl_argv_str(dl, &netns_str);
 			if (err)
 				return err;
-			opts->netns = netns_get_fd(netns_str);
-			if ((int)opts->netns < 0) {
-				dl_arg_dec(dl);
-				err = dl_argv_uint32_t(dl, &opts->netns);
-				if (err)
-					return err;
-				opts->netns_is_pid = true;
-			}
+
+			err = netns_get_fd(netns_str);
+			if (err < 0)
+				return err;
+
+			opts->netns = err;
 			o_found |= DL_OPT_NETNS;
 		} else if (dl_argv_match(dl, "action") &&
 			   (o_all & DL_OPT_RELOAD_ACTION)) {
@@ -2725,10 +2717,7 @@ static void dl_opts_put(struct nlmsghdr *nlh, struct dl *dl)
 		mnl_attr_put_u8(nlh, DEVLINK_ATTR_TRAP_ACTION,
 				opts->trap_action);
 	if (opts->present & DL_OPT_NETNS)
-		mnl_attr_put_u32(nlh,
-				 opts->netns_is_pid ? DEVLINK_ATTR_NETNS_PID :
-						      DEVLINK_ATTR_NETNS_FD,
-				 opts->netns);
+		mnl_attr_put_u32(nlh, DEVLINK_ATTR_NETNS_FD, opts->netns);
 	if (opts->present & DL_OPT_RELOAD_ACTION)
 		mnl_attr_put_u8(nlh, DEVLINK_ATTR_RELOAD_ACTION,
 				opts->reload_action);
Signature · 1 line
2.43.0
maintainer
#8 lore raw

Re: [PATCH iproute2-next v2 4/4] devlink: Drop now duplicate pid fallback for netns

Quoted 10 lines · David Ahern

From: David Ahern <[email protected]>

Now that netns_get_fd handles by name and pid, the special handling in devlink to fallback to PID can be removed with both cases handled by the FD attribute.

---
 devlink/devlink.c | 25 +++++++------------------
 1 file changed, 7 insertions(+), 18 deletions(-)

<...>

Quoted 25 lines

static char *dl_argv_next(struct dl *dl)
{

devlink/devlink.c +6 -8
 	char *ret;
@@ -2153,14 +2147,12 @@ static int dl_argv_parse(struct dl *dl, uint64_t o_required,
 			err = dl_argv_str(dl, &netns_str);
 			if (err)
 				return err;
-			opts->netns = netns_get_fd(netns_str);
-			if ((int)opts->netns < 0) {
-				dl_arg_dec(dl);
-				err = dl_argv_uint32_t(dl, &opts->netns);
-				if (err)
-					return err;
-				opts->netns_is_pid = true;
-			}
+
+			err = netns_get_fd(netns_str);
+			if (err < 0)
+				return err;
+
+			opts->netns = err;
 			o_found |= DL_OPT_NETNS;
 		} else if (dl_argv_match(dl, "action") &&
 			   (o_all & DL_OPT_RELOAD_ACTION)) {

devlink/devlink.c static void dl_opts_put(struct nlmsghdr *nlh, struct dl *dl) · line 2720

Quoted 8 lines
devlink/devlink.c +1 -4
@@ -2725,10 +2717,7 @@ static void dl_opts_put(struct nlmsghdr *nlh, struct dl *dl)
 		mnl_attr_put_u8(nlh, DEVLINK_ATTR_TRAP_ACTION,
 				opts->trap_action);
 	if (opts->present & DL_OPT_NETNS)
-		mnl_attr_put_u32(nlh,
-				 opts->netns_is_pid ? DEVLINK_ATTR_NETNS_PID :
-						      DEVLINK_ATTR_NETNS_FD,
-				 opts->netns);
+		mnl_attr_put_u32(nlh, DEVLINK_ATTR_NETNS_FD, opts->netns);

You can also remove netns_is_pid from struct dl_opts.

It is unfortunate that devlink implements DEVLINK_ATTR_NETNS_PID in the kernel, given that user space can provide this functionality with trivial effort.

Thanks

maintainer Reviewed-by on v2 1/4
#2 lore raw

Re: [PATCH iproute2-next v2 1/4] namespace: Add fallback to netns by pid

In reply to David Ahern
Quoted 6 lines · David Ahern

From: David Ahern <[email protected]>

Update netns_get_fd to try the passed in string as a name first. If the netns filesystem entry does not exist, try converting the string to an integer and if successful return an fd to /proc/<pid>/ns/net.

Quoted 8 lines

This allows netns_get_fd to uniformly handle the 2 common use cases of specifying network namespace by name and pid.

---
 lib/namespace.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)

Thanks,

maintainer Reviewed-by on v2 2/4
#4 lore raw

Re: [PATCH iproute2-next v2 2/4] iplink: Drop pid fallback code for netns

In reply to David Ahern
Quoted 10 lines · David Ahern

From: David Ahern <[email protected]>

Prior commit handles the fallback to pid, so remove the now duplicate code from iplink_parse.

---
 ip/iplink.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)

Thanks,

maintainer Reviewed-by on v2 3/4
#6 lore raw

Re: [PATCH iproute2-next v2 3/4] rdma: Allow netns to be specified by pid

In reply to David Ahern
Quoted 17 lines · David Ahern

From: David Ahern <[email protected]>

Update rdma dev to work like ip link where the netns can be a name or a pid by using netns_get_fd. Update man page and help accordingly.

---
 man/man8/rdma-dev.8 |  3 ++-
 rdma/dev.c          | 11 ++++-------
 2 files changed, 6 insertions(+), 8 deletions(-)
man/man8/rdma-dev.8 +0 -0
index abc9f405ede5..34ea1c614ca4 100644

man/man8/rdma-dev.8 rdma-dev \- RDMA device configuration · line 36

Quoted 6 lines
man/man8/rdma-dev.8 +1 -1
@@ -32,7 +32,7 @@ rdma-dev \- RDMA device configuration
 .B rdma dev set
 .RI "[ " DEV " ]"
 .BR netns
-.BR NSNAME
+.BR { NSNAME | PID }
 

Thanks,

#9 lore raw

Re: [PATCH iproute2-next v2 4/4] devlink: Drop now duplicate pid fallback for netns

In reply to Leon Romanovsky
Quoted 2 lines · Leon Romanovsky

You can also remove netns_is_pid from struct dl_opts.

I left it because of the uapi. I will wait for any more comments, and if that is the only one I can add the removal when I apply.

Quoted 4 lines

It is unfortunate that devlink implements DEVLINK_ATTR_NETNS_PID in the kernel, given that user space can provide this functionality with trivial effort.

yea. really stunned that uapi exists.

#10 lore raw
In reply to David Ahern
This series was applied to iproute2/iproute2-next.git (main) by David Ahern <[email protected]>:

Hello:

This series was applied to iproute2/iproute2-next.git (main) by David Ahern <[email protected]>:

Quoted 10 lines · On Tue, 12 May 2026 13:34:03 -0600 you wrote:

From: David Ahern <[email protected]>

Avoid the extra hurdle of creating an entry in /var/run/netns and allow the netns argument to be a name or a pid.

v2
- update netns_get_fd to handle the pid fallback
- update devlink code

[...]

Here is the summary with links:
- [iproute2-next,v2,1/4] namespace: Add fallback to netns by pid

    https://git.kernel.org/pub/scm/network/iproute2/iproute2-next.git/commit/?id=d6a1612bacfe
  - [iproute2-next,v2,2/4] iplink: Drop pid fallback code for netns
    https://git.kernel.org/pub/scm/network/iproute2/iproute2-next.git/commit/?id=22061a6354c0
  - [iproute2-next,v2,3/4] rdma: Allow netns to be specified by pid
    https://git.kernel.org/pub/scm/network/iproute2/iproute2-next.git/commit/?id=2a8b53446ff5

- [iproute2-next,v2,4/4] devlink: Drop now duplicate pid fallback for netns (no matching commit)

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html