bpf_net_helpers: introduce bpf_get_netns_cookie() helper

Linux Kernel documentation states:

* u64 bpf_get_netns_cookie(void *ctx)
Description
  Retrieve the cookie (generated by the kernel) of the network
  namespace the input *ctx* is associated with. The network
  namespace cookie remains stable for its lifetime and provides
  a global identifier that can be assumed unique. If *ctx* is
  NULL, then the helper returns the cookie for the initial
  network namespace. The cookie itself is very similar to that
  of **bpf_get_socket_cookie**\ () helper, but for network
  namespaces instead of sockets.
Return
  A 8-byte long opaque number.

This was introduced in Linux 5.7, see get_netns_cookie in
  https://elixir.bootlin.com/linux/v5.7/source/net/core/filter.c
vs
  https://elixir.bootlin.com/linux/v5.6/source/net/core/filter.c

But only for sock & sock_addr, while sock_ops & sk_msg were
added later (present in 6.12)

Test: N/A
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Idccb7191552be8048a02fff2358e7d7898123dd9
This commit is contained in:
Maciej Żenczykowski 2025-01-30 17:12:00 -08:00 • committed by Patrick Rohr
commit 259fe7e843

View file

@ -84,6 +84,8 @@ struct frag_hdr {
#define ETH_IP6_TCP_OFFSET(field) (ETH_HLEN + IP6_TCP_OFFSET(field))
#define ETH_IP6_UDP_OFFSET(field) (ETH_HLEN + IP6_UDP_OFFSET(field))
static uint64_t (*bpf_get_netns_cookie)(void* ctx) = (void*)BPF_FUNC_get_netns_cookie;
// this returns 0 iff skb->sk is NULL
static uint64_t (*bpf_get_socket_cookie)(struct __sk_buff* skb) = (void*)BPF_FUNC_get_socket_cookie;
static uint64_t (*bpf_get_sk_cookie)(struct bpf_sock* sk) = (void*)BPF_FUNC_get_socket_cookie;