bpf_dynptr_slice(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’– NAME πŸ’–

okay bestie, this is bpf_dynptr_slice(9) from the Kernel API Manual, and the name lowkey says it all: bpf_dynptr_slice β€” Obtain a read-only pointer to the dynptr data. periodt ✨


✨ SYNOPSIS ✨

__bpf_kfunc void * bpf_dynptr_slice (const struct bpf_dynptr *p , u64
       offset , void *buffer__nullable , u64 buffer__szk );

πŸŽ€ ARGUMENTS πŸŽ€

meet the cast, no cap:

  • p β€” the dynptr whose data slice you're trying to retrieve, bestie πŸ’…
  • offset β€” offset into the dynptr, like where you're pointing in the data
  • buffer__nullable β€” a user-provided buffer to copy contents into. it may be NULL (yes, nullable means nullable)
  • buffer__szk β€” size in bytes of the buffer if present. this is the length of the requested slice, and it must be a constant β€” no vibes-based runtime sizing, okay? πŸ”₯

πŸ¦‹ DESCRIPTION πŸ¦‹

so for non-skb and non-xdp type dynptrs, there is literally no difference between bpf_dynptr_slice and bpf_dynptr_data. same energy, same behavior 🫢

if buffer__nullable is NULL, the call will fail if buffer_opt was needed. that's the rule, bestie ⭐

if your intention is to write to the data slice, please use bpf_dynptr_slice_rdwr instead. this one is read-only, so don't try it πŸ’€

the user must check that the returned pointer is not null before using it. like, always. do not skip the null check 😭

please note that in the case of skb and xdp dynptrs, bpf_dynptr_slice does not change the underlying packet data pointers, so a call to bpf_dynptr_slice will not invalidate any ctx->data/data_end pointers in the bpf program. your ctx pointers stay valid, iconic 🌈


🌸 RETURN 🌸

NULL if the call failed (eg invalid dynptr), or a pointer to a read-only data slice. and that pointer can be either a direct pointer to the data or a pointer to the user provided buffer, with its contents containing the data, if unable to obtain a direct pointer πŸ‘‘

so yeah: check for NULL, then vibe with your read-only slice πŸ“


πŸ”₯ SEE ALSO πŸ”₯

also peep the kernel file ./kernel/bpf/helpers.c πŸ‘€ and the whole extended friend group:


made with love in ./kernel/bpf, October 2026 edition, for bpf_dynptr_slice(9) β€” bye bestie, go check that pointer πŸ’…πŸŽ€

πŸ“¦ where this page comes from