]> git.baikalelectronics.ru Git - kernel.git/commitdiff
net: retrieve netns cookie via getsocketopt
authorMartynas Pumputis <m@lambda.lt>
Wed, 23 Jun 2021 13:56:45 +0000 (15:56 +0200)
committerDavid S. Miller <davem@davemloft.net>
Thu, 24 Jun 2021 18:13:05 +0000 (11:13 -0700)
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 3d368ab87cf6 ("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>
arch/alpha/include/uapi/asm/socket.h
arch/mips/include/uapi/asm/socket.h
arch/parisc/include/uapi/asm/socket.h
arch/sparc/include/uapi/asm/socket.h
include/uapi/asm-generic/socket.h
net/core/sock.c

index 57420356ce4c1e6254aa2e196e58b8e1bc877c70..6b3daba60987e4218ea316931017b1b6a23058bd 100644 (file)
 #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
index 2d949969313b656f838222e93e2a31f7a5766b0b..cdf404a831b25f1bdf98b0ce9166a98cb42b1134 100644 (file)
 #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
index f60904329bbcc686579ab06e7bfdad77a33e0e80..5b5351cdcb3386de03aa4ad643fb5d4c939c40e3 100644 (file)
 #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
index 848a22fbac20ed6e65f18cceae71194d6366ef47..92675dc380fa287097db3eeb89deaa64a2761280 100644 (file)
 #define SO_PREFER_BUSY_POLL     0x0048
 #define SO_BUSY_POLL_BUDGET     0x0049
 
+#define SO_NETNS_COOKIE          0x0050
+
 #if !defined(__KERNEL__)
 
 
index 4dcd13d097a9e7f40aec3d09c902bc7e9b5e32c0..d588c244ec2faae1c3ee0ae131ce6e41c0276e36 100644 (file)
 #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__))
index ddfa88082a2bccb193cf6e58e034ce7af5e0abc0..a2337b37eba6df6b2505a2fa5ea7acd2eb820735 100644 (file)
@@ -1635,6 +1635,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).