I was looking at the different types of BPF
program, and noticed that for different program types the context is being passed differently.
Example:
For program type
BPF_PROG_TYPE_SOCK_OPS
, an object of typestruct bpf_sock_ops_kern
is passed. However, the BPF program of this type takes a reference tostruct bpf_sock_ops
. Why is it done this way and where is the "translation" frombpf_sock_ops_kern
tobpf_sock_ops
?For program type
BPF_PROG_TYPE_CGROUP_SKB
, an object of typestruct sk_buff
is passed (e.g., in__cgroup_bpf_run_filter_skb
), but the BPF program expects a minimized version,struct __sk_buff
.
So I looked at the struct bpf_verifier_ops
function callbacks, but they seem to only adjust the offsets in BPF instructions, as they are called by the BPF verifier.
I'd be glad if someone could shed light on how the BPF context is defined. Thanks.
bpf_sock_ops_kern
is just a subset ofbpf_sock_ops
. To convert,sock_filter_convert_ctx_access
only need to advance the pointer after the firstsk
field. The verifier will then ensure that fields after the union are not accessed. I've looked into the second case yet. – pchaignobpf_convert_ctx_access
matches on each possible required offset on__sk_buff
, one by one, and converts them to the equivalent offset in thesk_buff
object. Does that answer your question? I'll make a proper answer if that's the case. – pchaignobpf_sock_ops
and__sk_buff
). For example, you can see the process for__sk_buff
described by Alexei here, with more details in the PATCH description. – pchaignostruct __sk_buff
has little to do with performance but is used mostly for simplicity, to offer a cleaner interface to BPF users (only offer the fields that can be accessed from BPF). It's converted in the verifier withbpf_convert_ctx_access
, as mentioned already. Then you have additional checks innet/core/filter.c
(for networking), to make sure the user can read from, possibly write to, each of the fields of the struct. Seetc_cls_act_is_valid_access()
function for example. (I'm less familiar with tracing bits.) – Qeole