Skip to content

Commit e8b9eab

Browse files
brbdavem330
authored andcommitted
net: retrieve netns cookie via getsocketopt
It's getting more common to run nested container environments for testing cloud software. One of such examples is Kind [1] which runs a Kubernetes cluster in Docker containers on a single host. Each container acts as a Kubernetes node, and thus can run any Pod (aka container) inside the former. This approach simplifies testing a lot, as it eliminates complicated VM setups. Unfortunately, such a setup breaks some functionality when cgroupv2 BPF programs are used for load-balancing. The load-balancer BPF program needs to detect whether a request originates from the host netns or a container netns in order to allow some access, e.g. to a service via a loopback IP address. Typically, the programs detect this by comparing netns cookies with the one of the init ns via a call to bpf_get_netns_cookie(NULL). However, in nested environments the latter cannot be used given the Kubernetes node's netns is outside the init ns. To fix this, we need to pass the Kubernetes node netns cookie to the program in a different way: by extending getsockopt() with a SO_NETNS_COOKIE option, the orchestrator which runs in the Kubernetes node netns can retrieve the cookie and pass it to the program instead. Thus, this is following up on Eric's commit 3d368ab ("net: initialize net->net_cookie at netns setup") to allow retrieval via SO_NETNS_COOKIE. This is also in line in how we retrieve socket cookie via SO_COOKIE. [1] https://kind.sigs.k8s.io/ Signed-off-by: Lorenz Bauer <lmb@cloudflare.com> Signed-off-by: Martynas Pumputis <m@lambda.lt> Cc: Eric Dumazet <edumazet@google.com> Reviewed-by: Eric Dumazet <edumazet@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 35713d9 commit e8b9eab

6 files changed

Lines changed: 17 additions & 0 deletions

File tree

‎arch/alpha/include/uapi/asm/socket.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@
127127
#define SO_PREFER_BUSY_POLL 69
128128
#define SO_BUSY_POLL_BUDGET 70
129129

130+
#define SO_NETNS_COOKIE 71
131+
130132
#if !defined(__KERNEL__)
131133

132134
#if __BITS_PER_LONG == 64

‎arch/mips/include/uapi/asm/socket.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@
138138
#define SO_PREFER_BUSY_POLL 69
139139
#define SO_BUSY_POLL_BUDGET 70
140140

141+
#define SO_NETNS_COOKIE 71
142+
141143
#if !defined(__KERNEL__)
142144

143145
#if __BITS_PER_LONG == 64

‎arch/parisc/include/uapi/asm/socket.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@
119119
#define SO_PREFER_BUSY_POLL 0x4043
120120
#define SO_BUSY_POLL_BUDGET 0x4044
121121

122+
#define SO_NETNS_COOKIE 0x4045
123+
122124
#if !defined(__KERNEL__)
123125

124126
#if __BITS_PER_LONG == 64

‎arch/sparc/include/uapi/asm/socket.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@
120120
#define SO_PREFER_BUSY_POLL 0x0048
121121
#define SO_BUSY_POLL_BUDGET 0x0049
122122

123+
#define SO_NETNS_COOKIE 0x0050
124+
123125
#if !defined(__KERNEL__)
124126

125127

‎include/uapi/asm-generic/socket.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@
122122
#define SO_PREFER_BUSY_POLL 69
123123
#define SO_BUSY_POLL_BUDGET 70
124124

125+
#define SO_NETNS_COOKIE 71
126+
125127
#if !defined(__KERNEL__)
126128

127129
#if __BITS_PER_LONG == 64 || (defined(__x86_64__) && defined(__ILP32__))

‎net/core/sock.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,6 +1635,13 @@ int sock_getsockopt(struct socket *sock, int level, int optname,
16351635
v.val = sk->sk_bound_dev_if;
16361636
break;
16371637

1638+
case SO_NETNS_COOKIE:
1639+
lv = sizeof(u64);
1640+
if (len != lv)
1641+
return -EINVAL;
1642+
v.val64 = sock_net(sk)->net_cookie;
1643+
break;
1644+
16381645
default:
16391646
/* We implement the SO_SNDLOWAT etc to not be settable
16401647
* (1003.1g 7).

0 commit comments

Comments
 (0)