Skip to contentSkip to conversation

Expose network namespace cookies to user space

PATCH In review Lorenz Bauer 2 participants 6 messages Feb 19, 2021 – Mar 12, 2021

Subsystems (11)
lore
Share & tools
lore
#0 lore raw

v3 cover Expose network namespace cookies to user space

We're working on a user space control plane for the BPF sk_lookup hook [1]. The hook attaches to a network namespace and allows control over which socket receives a new connection / packet.

I'm proposing to add a new getsockopt and a netns ioctl to retrieve netns cookies, which allows identifying which netns a socket belongs to.

1: https://www.kernel.org/doc/html/latest/bpf/prog_sk_lookup.html

Changes in v3:
- Use sock_net unconditionally
- Fix unused variable in nsfs ioctl
- Be strict about getsockopt value size

Changes in v2:
- Rebase on top of Eric Dumazet's netns cookie simplification

Lorenz Bauer (4):
net: add SO_NETNS_COOKIE socket option
nsfs: add an ioctl to discover the network namespace cookie tools/testing: add test for NS_GET_COOKIE tools/testing: add a selftest for SO_NETNS_COOKIE

 arch/alpha/include/uapi/asm/socket.h          |  2 +
 arch/mips/include/uapi/asm/socket.h           |  2 +
 arch/parisc/include/uapi/asm/socket.h         |  2 +
 arch/sparc/include/uapi/asm/socket.h          |  2 +
 fs/nsfs.c                                     |  7 +++
 include/uapi/asm-generic/socket.h             |  2 +
 include/uapi/linux/nsfs.h                     |  2 +
 net/core/sock.c                               |  7 +++
 tools/testing/selftests/net/.gitignore        |  1 +
 tools/testing/selftests/net/Makefile          |  2 +-
 tools/testing/selftests/net/config            |  1 +
 tools/testing/selftests/net/so_netns_cookie.c | 61 +++++++++++++++++++
 tools/testing/selftests/nsfs/.gitignore       |  1 +
 tools/testing/selftests/nsfs/Makefile         |  2 +-
 tools/testing/selftests/nsfs/config           |  1 +
 tools/testing/selftests/nsfs/netns.c          | 57 +++++++++++++++++
 16 files changed, 150 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/net/so_netns_cookie.c
 create mode 100644 tools/testing/selftests/nsfs/netns.c
Signature · 1 line
2.27.0
#1 lore raw

PATCH v3 1/4 net: add SO_NETNS_COOKIE socket option

We need to distinguish which network namespace a socket belongs to. BPF has the useful bpf_get_netns_cookie helper for this, but accessing it from user space isn't possible. Add a read-only socket option that returns the netns cookie, similar to SO_COOKIE. If network namespaces are disabled, SO_NETNS_COOKIE returns the cookie of init_net.

---
 arch/alpha/include/uapi/asm/socket.h  | 2 ++
 arch/mips/include/uapi/asm/socket.h   | 2 ++
 arch/parisc/include/uapi/asm/socket.h | 2 ++
 arch/sparc/include/uapi/asm/socket.h  | 2 ++
 include/uapi/asm-generic/socket.h     | 2 ++
 net/core/sock.c                       | 7 +++++++
 6 files changed, 17 insertions(+)
arch/alpha/include/uapi/asm/socket.h +2 -0
index 57420356ce4c..6b3daba60987 100644
@@ -127,6 +127,8 @@
 #define SO_PREFER_BUSY_POLL	69
 #define SO_BUSY_POLL_BUDGET	70
 
+#define SO_NETNS_COOKIE		71
+
 #if !defined(__KERNEL__)
 
 #if __BITS_PER_LONG == 64
arch/mips/include/uapi/asm/socket.h +2 -0
index 2d949969313b..cdf404a831b2 100644
@@ -138,6 +138,8 @@
 #define SO_PREFER_BUSY_POLL	69
 #define SO_BUSY_POLL_BUDGET	70
 
+#define SO_NETNS_COOKIE		71
+
 #if !defined(__KERNEL__)
 
 #if __BITS_PER_LONG == 64
arch/parisc/include/uapi/asm/socket.h +2 -0
index f60904329bbc..5b5351cdcb33 100644
@@ -119,6 +119,8 @@
 #define SO_PREFER_BUSY_POLL	0x4043
 #define SO_BUSY_POLL_BUDGET	0x4044
 
+#define SO_NETNS_COOKIE		0x4045
+
 #if !defined(__KERNEL__)
 
 #if __BITS_PER_LONG == 64
arch/sparc/include/uapi/asm/socket.h +2 -0
index 848a22fbac20..ff79db753dce 100644
@@ -120,6 +120,8 @@
 #define SO_PREFER_BUSY_POLL	 0x0048
 #define SO_BUSY_POLL_BUDGET	 0x0049
 
+#define SO_NETNS_COOKIE		 0x004a
+
 #if !defined(__KERNEL__)
 
 
include/uapi/asm-generic/socket.h +2 -0
index 4dcd13d097a9..d588c244ec2f 100644
@@ -122,6 +122,8 @@
 #define SO_PREFER_BUSY_POLL	69
 #define SO_BUSY_POLL_BUDGET	70
 
+#define SO_NETNS_COOKIE		71
+
 #if !defined(__KERNEL__)
 
 #if __BITS_PER_LONG == 64 || (defined(__x86_64__) && defined(__ILP32__))
net/core/sock.c +7 -0
index 0ed98f20448a..84db011a192f 100644
@@ -1614,6 +1614,13 @@ int sock_getsockopt(struct socket *sock, int level, int optname,
 		v.val = sk->sk_bound_dev_if;
 		break;
 
+	case SO_NETNS_COOKIE:
+		lv = sizeof(u64);
+		if (len != lv)
+			return -EINVAL;
+		v.val64 = sock_net(sk)->net_cookie;
+		break;
+
 	default:
 		/* We implement the SO_SNDLOWAT etc to not be settable
 		 * (1003.1g 7).
Signature · 1 line
2.27.0
#2 lore raw

PATCH v3 2/4 nsfs: add an ioctl to discover the network namespace cookie

In reply to Lorenz Bauer

Network namespaces have a globally unique non-zero identifier aka a cookie, in line with socket cookies. Add an ioctl to retrieve the cookie from user space without going via BPF.

---
 fs/nsfs.c                 | 7 +++++++
 include/uapi/linux/nsfs.h | 2 ++
 2 files changed, 9 insertions(+)
fs/nsfs.c +7 -0
index 800c1d0eb0d0..48198a1b1685 100644
@@ -11,6 +11,7 @@
 #include <linux/user_namespace.h>
 #include <linux/nsfs.h>
 #include <linux/uaccess.h>
+#include <net/net_namespace.h>
 
 #include "internal.h"
 
@@ -191,6 +192,7 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
 	struct user_namespace *user_ns;
 	struct ns_common *ns = get_proc_ns(file_inode(filp));
 	uid_t __user *argp;
+	struct net *net_ns;
 	uid_t uid;
 
 	switch (ioctl) {
@@ -209,6 +211,11 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
 		argp = (uid_t __user *) arg;
 		uid = from_kuid_munged(current_user_ns(), user_ns->owner);
 		return put_user(uid, argp);
+	case NS_GET_COOKIE:
+		if (ns->ops->type != CLONE_NEWNET)
+			return -EINVAL;
+		net_ns = container_of(ns, struct net, ns);
+		return put_user(net_ns->net_cookie, (u64 __user *)arg);
 	default:
 		return -ENOTTY;
 	}
include/uapi/linux/nsfs.h +2 -0
index a0c8552b64ee..86611c2cf908 100644
@@ -15,5 +15,7 @@
 #define NS_GET_NSTYPE		_IO(NSIO, 0x3)
 /* Get owner UID (in the caller's user namespace) for a user namespace */
 #define NS_GET_OWNER_UID	_IO(NSIO, 0x4)
+/* Returns a unique non-zero identifier for a network namespace */
+#define NS_GET_COOKIE		_IO(NSIO, 0x5)
 
 #endif /* __LINUX_NSFS_H */
Signature · 1 line
2.27.0
#3 lore raw

PATCH v3 3/4 tools/testing: add test for NS_GET_COOKIE

In reply to Lorenz Bauer

Check that NS_GET_COOKIE returns a non-zero value, and that distinct network namespaces have different cookies.

---
 tools/testing/selftests/nsfs/.gitignore |  1 +
 tools/testing/selftests/nsfs/Makefile   |  2 +-
 tools/testing/selftests/nsfs/config     |  1 +
 tools/testing/selftests/nsfs/netns.c    | 57 +++++++++++++++++++++++++
 4 files changed, 60 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/nsfs/netns.c
tools/testing/selftests/nsfs/.gitignore +1 -0
index ed79ebdf286e..ca31b216215b 100644
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 owner
 pidns
+netns
tools/testing/selftests/nsfs/Makefile +1 -1
index dd9bd50b7b93..93793cdb5a7c 100644
@@ -1,5 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0-only
-TEST_GEN_PROGS := owner pidns
+TEST_GEN_PROGS := owner pidns netns
 
 CFLAGS := -Wall -Werror
 
tools/testing/selftests/nsfs/config +1 -0
index 598d0a225fc9..ea654f6a4cd9 100644
@@ -1,3 +1,4 @@
 CONFIG_USER_NS=y
 CONFIG_UTS_NS=y
 CONFIG_PID_NS=y
+CONFIG_NET_NS=y
tools/testing/selftests/nsfs/netns.c new file +57 -0

57 lines not on this page · raw message

Signature · 1 line
2.27.0
#4 lore raw

PATCH v3 4/4 tools/testing: add a selftest for SO_NETNS_COOKIE

In reply to Lorenz Bauer

Make sure that SO_NETNS_COOKIE returns a non-zero value, and that sockets from different namespaces have a distinct cookie value.

---
 tools/testing/selftests/net/.gitignore        |  1 +
 tools/testing/selftests/net/Makefile          |  2 +-
 tools/testing/selftests/net/config            |  1 +
 tools/testing/selftests/net/so_netns_cookie.c | 61 +++++++++++++++++++
 4 files changed, 64 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/net/so_netns_cookie.c
tools/testing/selftests/net/.gitignore +1 -0
index 61ae899cfc17..19deb9cdf72f 100644
@@ -30,3 +30,4 @@ hwtstamp_config
 rxtimestamp
 timestamping
 txtimestamp
+so_netns_cookie
tools/testing/selftests/net/Makefile +1 -1
index 25f198bec0b2..91bb372f5ba5 100644
@@ -28,7 +28,7 @@ TEST_GEN_FILES =  socket nettest
 TEST_GEN_FILES += psock_fanout psock_tpacket msg_zerocopy reuseport_addr_any
 TEST_GEN_FILES += tcp_mmap tcp_inq psock_snd txring_overwrite
 TEST_GEN_FILES += udpgso udpgso_bench_tx udpgso_bench_rx ip_defrag
-TEST_GEN_FILES += so_txtime ipv6_flowlabel ipv6_flowlabel_mgr
+TEST_GEN_FILES += so_txtime ipv6_flowlabel ipv6_flowlabel_mgr so_netns_cookie
 TEST_GEN_FILES += tcp_fastopen_backup_key
 TEST_GEN_FILES += fin_ack_lat
 TEST_GEN_FILES += reuseaddr_ports_exhausted
tools/testing/selftests/net/config +1 -0
index 614d5477365a..6f905b53904f 100644
@@ -1,4 +1,5 @@
 CONFIG_USER_NS=y
+CONFIG_NET_NS=y
 CONFIG_BPF_SYSCALL=y
 CONFIG_TEST_BPF=m
 CONFIG_NUMA=y
tools/testing/selftests/net/so_netns_cookie.c new file +61 -0

61 lines not on this page · raw message

Signature · 1 line
2.27.0
#5 lore raw
In reply to Lorenz Bauer
Quoted 3 lines · Lorenz Bauer

We're working on a user space control plane for the BPF sk_lookup hook [1]. The hook attaches to a network namespace and allows control over which socket receives a new connection / packet.

We are developing a net stack latency tracing tool, which need net_cookie to distinguish different net namespace. Besides that, our container management system need to read net_cookie from userspace.

In [0], you said you would give up this patch set. Could you reconsider continuing with these patches? Because we also need them.

net_cookie could be an unified net namespace ID to replace netns inode, but there are lots of work to do.

[0]: https://lkml.org/lkml/2021/3/10/254

Cheers,
Tony Lu

Quoted 43 lines

I'm proposing to add a new getsockopt and a netns ioctl to retrieve netns cookies, which allows identifying which netns a socket belongs to.

1: https://www.kernel.org/doc/html/latest/bpf/prog_sk_lookup.html

Changes in v3:
- Use sock_net unconditionally
- Fix unused variable in nsfs ioctl
- Be strict about getsockopt value size

Changes in v2:
- Rebase on top of Eric Dumazet's netns cookie simplification

Lorenz Bauer (4):
net: add SO_NETNS_COOKIE socket option
nsfs: add an ioctl to discover the network namespace cookie tools/testing: add test for NS_GET_COOKIE tools/testing: add a selftest for SO_NETNS_COOKIE

 arch/alpha/include/uapi/asm/socket.h          |  2 +
 arch/mips/include/uapi/asm/socket.h           |  2 +
 arch/parisc/include/uapi/asm/socket.h         |  2 +
 arch/sparc/include/uapi/asm/socket.h          |  2 +
 fs/nsfs.c                                     |  7 +++
 include/uapi/asm-generic/socket.h             |  2 +
 include/uapi/linux/nsfs.h                     |  2 +
 net/core/sock.c                               |  7 +++
 tools/testing/selftests/net/.gitignore        |  1 +
 tools/testing/selftests/net/Makefile          |  2 +-
 tools/testing/selftests/net/config            |  1 +
 tools/testing/selftests/net/so_netns_cookie.c | 61 +++++++++++++++++++
 tools/testing/selftests/nsfs/.gitignore       |  1 +
 tools/testing/selftests/nsfs/Makefile         |  2 +-
 tools/testing/selftests/nsfs/config           |  1 +
 tools/testing/selftests/nsfs/netns.c          | 57 +++++++++++++++++
 16 files changed, 150 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/net/so_netns_cookie.c
 create mode 100644 tools/testing/selftests/nsfs/netns.c
Signature · 1 line
2.27.0