Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
* [PATCH iproute2-next 0/4] Allow rdma dev netns to take a pid
@ 2026-05-07 15:08 David Ahern
  2026-05-07 15:08 ` [PATCH iproute2-next 1/4] namespace: Add function to return fd for netns by pid David Ahern
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: David Ahern @ 2026-05-07 15:08 UTC (permalink / raw)
  To: stephen; +Cc: netdev, leonro, linux-rdma, David Ahern

From: David Ahern <dahern@nvidia.com>

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

David Ahern (4):
  namespace: Add function to return fd for netns by pid
  iplink: Update iplink_parse to use netns_get_fd_pid
  rdma: Allow netns to be specified by pid
  rdma-dev: Update man page to reflect netns as a pid

 include/namespace.h |  1 +
 ip/iplink.c         | 12 +++---------
 lib/namespace.c     | 13 +++++++++++++
 man/man8/rdma-dev.8 |  3 ++-
 rdma/dev.c          | 15 ++++++++-------
 5 files changed, 27 insertions(+), 17 deletions(-)

-- 
re-send; attempt yesterday did not have Leon and linux-rdma and did not
make it to netdev.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH iproute2-next 1/4] namespace: Add function to return fd for netns by pid
  2026-05-07 15:08 [PATCH iproute2-next 0/4] Allow rdma dev netns to take a pid David Ahern
@ 2026-05-07 15:08 ` David Ahern
  2026-05-07 15:08 ` [PATCH iproute2-next 2/4] iplink: Update iplink_parse to use netns_get_fd_pid David Ahern
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: David Ahern @ 2026-05-07 15:08 UTC (permalink / raw)
  To: stephen; +Cc: netdev, leonro, linux-rdma, David Ahern

From: David Ahern <dahern@nvidia.com>

Add netns_get_fd_pid - verifies string is an integer,
then returns an fd to /proc/$pid/ns/net.

Signed-off-by: David Ahern <dahern@nvidia.com>
---
 include/namespace.h |  1 +
 lib/namespace.c     | 13 +++++++++++++
 2 files changed, 14 insertions(+)

diff --git a/include/namespace.h b/include/namespace.h
index 98f4af59..6a6fa438 100644
--- a/include/namespace.h
+++ b/include/namespace.h
@@ -52,6 +52,7 @@ static inline int setns(int fd, int nstype)
 
 int netns_switch(char *netns);
 int netns_get_fd(const char *netns);
+int netns_get_fd_pid(const char *pidstr);
 int netns_foreach(int (*func)(char *nsname, void *arg), void *arg);
 
 struct netns_func {
diff --git a/lib/namespace.c b/lib/namespace.c
index 74b7e7ca..f391c694 100644
--- a/lib/namespace.c
+++ b/lib/namespace.c
@@ -114,6 +114,19 @@ int netns_get_fd(const char *name)
 	return open(path, O_RDONLY);
 }
 
+int netns_get_fd_pid(const char *pidstr)
+{
+	char path[PATH_MAX];
+	int pid;
+
+	/* make sure it is an integer */
+	if (get_integer(&pid, pidstr, 0) < 0)
+		return -1;
+
+	snprintf(path, sizeof(path), "/proc/%s/ns/net", pidstr);
+	return open(path, O_RDONLY);
+}
+
 int netns_foreach(int (*func)(char *nsname, void *arg), void *arg)
 {
 	DIR *dir;
-- 
2.50.1 (Apple Git-155)


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH iproute2-next 2/4] iplink: Update iplink_parse to use netns_get_fd_pid
  2026-05-07 15:08 [PATCH iproute2-next 0/4] Allow rdma dev netns to take a pid David Ahern
  2026-05-07 15:08 ` [PATCH iproute2-next 1/4] namespace: Add function to return fd for netns by pid David Ahern
@ 2026-05-07 15:08 ` David Ahern
  2026-05-10 10:01   ` Leon Romanovsky
  2026-05-07 15:08 ` [PATCH iproute2-next 3/4] rdma: Allow netns to be specified by pid David Ahern
  2026-05-07 15:08 ` [PATCH iproute2-next 4/4] rdma-dev: Update man page to reflect netns as a pid David Ahern
  3 siblings, 1 reply; 8+ messages in thread
From: David Ahern @ 2026-05-07 15:08 UTC (permalink / raw)
  To: stephen; +Cc: netdev, leonro, linux-rdma, David Ahern

From: David Ahern <dahern@nvidia.com>

Drop the open coding of proc/pid/ns/net in favor of
netns_get_fd_pid.

Signed-off-by: David Ahern <dahern@nvidia.com>
---
 ip/iplink.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)

diff --git a/ip/iplink.c b/ip/iplink.c
index eae51438..6c4586ee 100644
--- a/ip/iplink.c
+++ b/ip/iplink.c
@@ -650,19 +650,13 @@ 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)
+				netns = netns_get_fd_pid(*argv);
 			if (netns < 0)
 				invarg("Invalid \"netns\" value\n", *argv);
 
-- 
2.50.1 (Apple Git-155)


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH iproute2-next 3/4] rdma: Allow netns to be specified by pid
  2026-05-07 15:08 [PATCH iproute2-next 0/4] Allow rdma dev netns to take a pid David Ahern
  2026-05-07 15:08 ` [PATCH iproute2-next 1/4] namespace: Add function to return fd for netns by pid David Ahern
  2026-05-07 15:08 ` [PATCH iproute2-next 2/4] iplink: Update iplink_parse to use netns_get_fd_pid David Ahern
@ 2026-05-07 15:08 ` David Ahern
  2026-05-07 15:08 ` [PATCH iproute2-next 4/4] rdma-dev: Update man page to reflect netns as a pid David Ahern
  3 siblings, 0 replies; 8+ messages in thread
From: David Ahern @ 2026-05-07 15:08 UTC (permalink / raw)
  To: stephen; +Cc: netdev, leonro, linux-rdma, David Ahern

From: David Ahern <dahern@nvidia.com>

Update rdma dev to work like ip link where the netns can be a
name or a pid.

Update dev_set_netns to use netns_get_fd instead of open coding
use of NETNS_RUN_DIR. If netns_get_fd fails, try netns_get_fd_pid.

Signed-off-by: David Ahern <dahern@nvidia.com>
---
 rdma/dev.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/rdma/dev.c b/rdma/dev.c
index fd60c1a0..f9b782eb 100644
--- a/rdma/dev.c
+++ b/rdma/dev.c
@@ -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,9 +312,9 @@ static int dev_set_name(struct rd *rd)
 
 static int dev_set_netns(struct rd *rd)
 {
-	char *netns_path;
+	char *arg = rd_argv(rd);
+	int netns, pid;
 	uint32_t seq;
-	int netns;
 	int ret;
 
 	if (rd_no_arg(rd)) {
@@ -321,10 +322,11 @@ 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;
+	/* try by name then by pid */
+	netns = netns_get_fd(arg);
+	if (netns < 0)
+		netns = netns_get_fd_pid(arg);
 
-	netns = open(netns_path, O_RDONLY | O_CLOEXEC);
 	if (netns < 0) {
 		fprintf(stderr, "Cannot open network namespace \"%s\": %s\n",
 			rd_argv(rd), strerror(errno));
@@ -339,7 +341,6 @@ static int dev_set_netns(struct rd *rd)
 	ret = rd_sendrecv_msg(rd, seq);
 	close(netns);
 done:
-	free(netns_path);
 	return ret;
 }
 
-- 
2.50.1 (Apple Git-155)


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH iproute2-next 4/4] rdma-dev: Update man page to reflect netns as a pid
  2026-05-07 15:08 [PATCH iproute2-next 0/4] Allow rdma dev netns to take a pid David Ahern
                   ` (2 preceding siblings ...)
  2026-05-07 15:08 ` [PATCH iproute2-next 3/4] rdma: Allow netns to be specified by pid David Ahern
@ 2026-05-07 15:08 ` David Ahern
  3 siblings, 0 replies; 8+ messages in thread
From: David Ahern @ 2026-05-07 15:08 UTC (permalink / raw)
  To: stephen; +Cc: netdev, leonro, linux-rdma, David Ahern

From: David Ahern <dahern@nvidia.com>

Signed-off-by: David Ahern <dahern@nvidia.com>
---
 man/man8/rdma-dev.8 | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/man/man8/rdma-dev.8 b/man/man8/rdma-dev.8
index abc9f405..9ddad2dd 100644
--- a/man/man8/rdma-dev.8
+++ b/man/man8/rdma-dev.8
@@ -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.
-- 
2.50.1 (Apple Git-155)


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH iproute2-next 2/4] iplink: Update iplink_parse to use netns_get_fd_pid
  2026-05-07 15:08 ` [PATCH iproute2-next 2/4] iplink: Update iplink_parse to use netns_get_fd_pid David Ahern
@ 2026-05-10 10:01   ` Leon Romanovsky
  2026-05-12 15:23     ` David Ahern
  0 siblings, 1 reply; 8+ messages in thread
From: Leon Romanovsky @ 2026-05-10 10:01 UTC (permalink / raw)
  To: David Ahern; +Cc: stephen, netdev, linux-rdma, David Ahern

On Thu, May 07, 2026 at 09:08:33AM -0600, David Ahern wrote:
> From: David Ahern <dahern@nvidia.com>
> 
> Drop the open coding of proc/pid/ns/net in favor of
> netns_get_fd_pid.
> 
> Signed-off-by: David Ahern <dahern@nvidia.com>
> ---
>  ip/iplink.c | 12 +++---------
>  1 file changed, 3 insertions(+), 9 deletions(-)
> 
> diff --git a/ip/iplink.c b/ip/iplink.c
> index eae51438..6c4586ee 100644
> --- a/ip/iplink.c
> +++ b/ip/iplink.c
> @@ -650,19 +650,13 @@ 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)
> +				netns = netns_get_fd_pid(*argv);

It would be good to have a single function that handles the entire
netns_get_fd() → netns < 0 → netns_get_fd_pid() sequence internally. This
logic is used by at least iplink and rdmatool.

Thanks

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH iproute2-next 2/4] iplink: Update iplink_parse to use netns_get_fd_pid
  2026-05-10 10:01   ` Leon Romanovsky
@ 2026-05-12 15:23     ` David Ahern
  2026-05-12 16:12       ` Leon Romanovsky
  0 siblings, 1 reply; 8+ messages in thread
From: David Ahern @ 2026-05-12 15:23 UTC (permalink / raw)
  To: Leon Romanovsky; +Cc: stephen, netdev, linux-rdma, David Ahern

On 5/10/26 4:01 AM, Leon Romanovsky wrote:
>> diff --git a/ip/iplink.c b/ip/iplink.c
>> index eae51438..6c4586ee 100644
>> --- a/ip/iplink.c
>> +++ b/ip/iplink.c
>> @@ -650,19 +650,13 @@ 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)
>> +				netns = netns_get_fd_pid(*argv);
> 
> It would be good to have a single function that handles the entire
> netns_get_fd() → netns < 0 → netns_get_fd_pid() sequence internally. This
> logic is used by at least iplink and rdmatool.
> 

I considered that. devlink usage of netns_get_fd and its handling of pid
vs name makes it a more complicated change.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH iproute2-next 2/4] iplink: Update iplink_parse to use netns_get_fd_pid
  2026-05-12 15:23     ` David Ahern
@ 2026-05-12 16:12       ` Leon Romanovsky
  0 siblings, 0 replies; 8+ messages in thread
From: Leon Romanovsky @ 2026-05-12 16:12 UTC (permalink / raw)
  To: David Ahern; +Cc: stephen, netdev, linux-rdma, David Ahern

On Tue, May 12, 2026 at 09:23:52AM -0600, David Ahern wrote:
> On 5/10/26 4:01 AM, Leon Romanovsky wrote:
> >> diff --git a/ip/iplink.c b/ip/iplink.c
> >> index eae51438..6c4586ee 100644
> >> --- a/ip/iplink.c
> >> +++ b/ip/iplink.c
> >> @@ -650,19 +650,13 @@ 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)
> >> +				netns = netns_get_fd_pid(*argv);
> > 
> > It would be good to have a single function that handles the entire
> > netns_get_fd() → netns < 0 → netns_get_fd_pid() sequence internally. This
> > logic is used by at least iplink and rdmatool.
> > 
> 
> I considered that. devlink usage of netns_get_fd and its handling of pid
> vs name makes it a more complicated change.

You are not modifying devlink in this series, so rather than introducing
netns_get_fd_pid(), introduce netns_get_fd_fallback_pid() or another
appropriately named helper.

Thanks


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-05-12 16:13 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-07 15:08 [PATCH iproute2-next 0/4] Allow rdma dev netns to take a pid David Ahern
2026-05-07 15:08 ` [PATCH iproute2-next 1/4] namespace: Add function to return fd for netns by pid David Ahern
2026-05-07 15:08 ` [PATCH iproute2-next 2/4] iplink: Update iplink_parse to use netns_get_fd_pid David Ahern
2026-05-10 10:01   ` Leon Romanovsky
2026-05-12 15:23     ` David Ahern
2026-05-12 16:12       ` Leon Romanovsky
2026-05-07 15:08 ` [PATCH iproute2-next 3/4] rdma: Allow netns to be specified by pid David Ahern
2026-05-07 15:08 ` [PATCH iproute2-next 4/4] rdma-dev: Update man page to reflect netns as a pid David Ahern

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox