Skip to contentSkip to conversation

Allow rdma dev netns to take a pid

PATCH Superseded David Ahern 2 participants 8 messages May 7 – May 12

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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.

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(-)
Signature · 2 lines
re-send; attempt yesterday did not have Leon and linux-rdma and did not
make it to netdev.
#1 lore raw

PATCH 1/4 namespace: Add function to return fd for netns by pid

From: David Ahern <[email protected]>

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

---
 include/namespace.h |  1 +
 lib/namespace.c     | 13 +++++++++++++
 2 files changed, 14 insertions(+)
include/namespace.h +1 -0
index 98f4af59..6a6fa438 100644
@@ -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 {
lib/namespace.c +13 -0
index 74b7e7ca..f391c694 100644
@@ -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;
Signature · 1 line
2.50.1 (Apple Git-155)
#2 lore raw

PATCH 2/4 iplink: Update iplink_parse to use netns_get_fd_pid

In reply to David Ahern

From: David Ahern <[email protected]>

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

---
 ip/iplink.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
ip/iplink.c +3 -9
index eae51438..6c4586ee 100644
@@ -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);
 
Signature · 1 line
2.50.1 (Apple Git-155)
#6 lore raw

PATCH 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.

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.

---
 rdma/dev.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
rdma/dev.c +8 -7
index fd60c1a0..f9b782eb 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,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;
 }
 
Signature · 1 line
2.50.1 (Apple Git-155)
#7 lore raw

PATCH 4/4 rdma-dev: Update man page to reflect netns as a pid

In reply to David Ahern

From: David Ahern <[email protected]>

---
 man/man8/rdma-dev.8 | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
man/man8/rdma-dev.8 +2 -1
index abc9f405..9ddad2dd 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.
Signature · 1 line
2.50.1 (Apple Git-155)
maintainer
#3 lore raw

Re: [PATCH iproute2-next 2/4] iplink: Update iplink_parse to use netns_get_fd_pid

In reply to David Ahern
Quoted 24 lines · David Ahern

From: David Ahern <[email protected]>

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

---
 ip/iplink.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
ip/iplink.c +1 -2
index eae51438..6c4586ee 100644
@@ -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 */

ip/iplink.c int iplink_parse(int argc, char **argv, struct iplink_req *req, char **type) · line 659

Quoted 10 lines
ip/iplink.c +2 -7
 			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

#4 lore raw

Re: [PATCH iproute2-next 2/4] iplink: Update iplink_parse to use netns_get_fd_pid

Quoted 20 lines · Leon Romanovsky
Quoted 20 lines
ip/iplink.c +1 -7
index eae51438..6c4586ee 100644
@@ -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);
Quoted 9 lines

ip/iplink.c int iplink_parse(int argc, char **argv, struct iplink_req *req, char **type) · line 659

Quoted 4 lines
ip/iplink.c +2 -2
-				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.

maintainer
#5 lore raw

Re: [PATCH iproute2-next 2/4] iplink: Update iplink_parse to use netns_get_fd_pid

Quoted 31 lines · David Ahern
Quoted 20 lines · Leon Romanovsky
Quoted 20 lines
ip/iplink.c +1 -7
index eae51438..6c4586ee 100644
@@ -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);
Quoted 9 lines

ip/iplink.c int iplink_parse(int argc, char **argv, struct iplink_req *req, char **type) · line 659

Quoted 4 lines
ip/iplink.c +2 -2
-				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.

Quoted 2 lines

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