Re: [PATCH bpf-next v2 1/3] bpf: Expose bpf_get_socket_cookie to tracing programs

From: Florent Revest
Date: Tue Dec 08 2020 - 15:49:09 EST


On Fri, 2020-12-04 at 11:47 -0800, Martin KaFai Lau wrote:
> On Thu, Dec 03, 2020 at 10:33:28PM +0100, Florent Revest wrote:
> > +const struct bpf_func_proto
> > bpf_get_socket_cookie_sock_tracing_proto = {
> > + .func = bpf_get_socket_cookie_sock,
> > + .gpl_only = false,
> > + .ret_type = RET_INTEGER,
> > + .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
>
> In tracing where it gets a sk pointer, the sk could be NULL.
> A NULL check is required in the helper. Please refer to
> bpf_skc_to_tcp_sock[_proto] as an example.

Ah, good catch! :)

> This proto is in general also useful for non tracing context where
> it can get a hold of a sk pointer. (e.g. another similar usage that
> will have a hold on a sk pointer for BPF_PROG_TYPE_SK_REUSEPORT [0]).

Agreed.

> In case if you don't need sleepable at this point as Daniel
> mentioned in another thread. Does it make sense to rename this
> proto to something like bpf_get_socket_pointer_cookie_proto?

My understanding is that I could have two helpers definitions and
protos, one calling sock_gen_cookie and the other one calling
__sock_gen_cookie. Then I could just use:

return prog->aux->sleepable
? bpf_get_socket_pointer_cookie_sleepable_proto
: bpf_get_socket_pointer_cookie_proto;

Would that work ?