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

From: David Ahern <[email-masked]>
Date: Tue, 12 May 2026 13:34:06 -0600

From: David Ahern <[email-masked]>

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.

Signed-off-by: David Ahern <[email-masked]>
---
 man/man8/rdma-dev.8 |  3 ++-
 rdma/dev.c          | 11 ++++-------
 2 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/man/man8/rdma-dev.8 b/man/man8/rdma-dev.8
index abc9f405ede5..34ea1c614ca4 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.
diff --git a/rdma/dev.c b/rdma/dev.c
index fd60c1a0e856..c8537912e48e 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,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;
 }
 
-- 
2.43.0