From 259fe7e843d2ffa1a9f5a62872b34a830ae0a497 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBenczykowski?= Date: Thu, 30 Jan 2025 17:12:00 -0800 Subject: [PATCH] bpf_net_helpers: introduce bpf_get_netns_cookie() helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Change-Id: Idccb7191552be8048a02fff2358e7d7898123dd9 --- bpf/progs/bpf_net_helpers.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bpf/progs/bpf_net_helpers.h b/bpf/progs/bpf_net_helpers.h index a5664ba7a9..4085ed459a 100644 --- a/bpf/progs/bpf_net_helpers.h +++ b/bpf/progs/bpf_net_helpers.h @@ -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;