Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
From: David Ahern <dsahern@kernel.org>
To: stephen@networkplumber.org
Cc: netdev@vger.kernel.org, leonro@nvidia.com,
	linux-rdma@vger.kernel.org, David Ahern <dahern@nvidia.com>
Subject: [PATCH iproute2-next 3/4] rdma: Allow netns to be specified by pid
Date: Thu,  7 May 2026 09:08:34 -0600	[thread overview]
Message-ID: <20260507150836.28105-4-dsahern@kernel.org> (raw)
In-Reply-To: <20260507150836.28105-1-dsahern@kernel.org>

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)


  parent reply	other threads:[~2026-05-07 15:08 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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-07 15:08 ` David Ahern [this message]
2026-05-07 15:08 ` [PATCH iproute2-next 4/4] rdma-dev: Update man page to reflect netns as a pid David Ahern

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260507150836.28105-4-dsahern@kernel.org \
    --to=dsahern@kernel.org \
    --cc=dahern@nvidia.com \
    --cc=leonro@nvidia.com \
    --cc=linux-rdma@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=stephen@networkplumber.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox