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 v2 1/4] namespace: Add fallback to netns by pid
Date: Tue, 12 May 2026 13:34:04 -0600 [thread overview]
Message-ID: <20260512193412.32019-2-dsahern@kernel.org> (raw)
In-Reply-To: <20260512193412.32019-1-dsahern@kernel.org>
From: David Ahern <dahern@nvidia.com>
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.
Signed-off-by: David Ahern <dahern@nvidia.com>
---
lib/namespace.c | 28 +++++++++++++++++++++-------
1 file changed, 21 insertions(+), 7 deletions(-)
diff --git a/lib/namespace.c b/lib/namespace.c
index 74b7e7caf0e5..0bba3a163563 100644
--- a/lib/namespace.c
+++ b/lib/namespace.c
@@ -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)
--
2.43.0
next prev parent reply other threads:[~2026-05-12 19:34 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-12 19:34 [PATCH iproute2-next v2 0/4] Allow rdma dev netns to take a pid David Ahern
2026-05-12 19:34 ` David Ahern [this message]
2026-05-13 5:49 ` [PATCH iproute2-next v2 1/4] namespace: Add fallback to netns by pid Leon Romanovsky
2026-05-12 19:34 ` [PATCH iproute2-next v2 2/4] iplink: Drop pid fallback code for netns David Ahern
2026-05-13 5:49 ` Leon Romanovsky
2026-05-12 19:34 ` [PATCH iproute2-next v2 3/4] rdma: Allow netns to be specified by pid David Ahern
2026-05-13 5:49 ` Leon Romanovsky
2026-05-12 19:34 ` [PATCH iproute2-next v2 4/4] devlink: Drop now duplicate pid fallback for netns David Ahern
2026-05-13 5:40 ` Leon Romanovsky
2026-05-14 1:06 ` David Ahern
2026-05-15 19:30 ` [PATCH iproute2-next v2 0/4] Allow rdma dev netns to take a pid patchwork-bot+netdevbpf
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=20260512193412.32019-2-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.