On Tue, Dec 17, 2019 at 11:20 PM Martin Lau <kafai@xxxxxx> wrote: > > On Tue, Dec 17, 2019 at 11:03:45PM -0800, Martin Lau wrote: > > On Tue, Dec 17, 2019 at 07:07:23PM -0800, Andrii Nakryiko wrote: > > > On Fri, Dec 13, 2019 at 4:48 PM Martin KaFai Lau <kafai@xxxxxx> wrote: > > > > > > > > This patch adds BPF STRUCT_OPS support to libbpf. > > > > > > > > The only sec_name convention is SEC("struct_ops") to identify the > > > > struct ops implemented in BPF, e.g. > > > > SEC("struct_ops") > > > > struct tcp_congestion_ops dctcp = { > > > > .init = (void *)dctcp_init, /* <-- a bpf_prog */ > > > > /* ... some more func prts ... */ > > > > .name = "bpf_dctcp", > > > > }; > > > > > > > > In the bpf_object__open phase, libbpf will look for the "struct_ops" > > > > elf section and find out what is the btf-type the "struct_ops" is > > > > implementing. Note that the btf-type here is referring to > > > > a type in the bpf_prog.o's btf. It will then collect (through SHT_REL) > > > > where are the bpf progs that the func ptrs are referring to. > > > > > > > > In the bpf_object__load phase, the prepare_struct_ops() will load > > > > the btf_vmlinux and obtain the corresponding kernel's btf-type. > > > > With the kernel's btf-type, it can then set the prog->type, > > > > prog->attach_btf_id and the prog->expected_attach_type. Thus, > > > > the prog's properties do not rely on its section name. > > > > > > > > Currently, the bpf_prog's btf-type ==> btf_vmlinux's btf-type matching > > > > process is as simple as: member-name match + btf-kind match + size match. > > > > If these matching conditions fail, libbpf will reject. > > > > The current targeting support is "struct tcp_congestion_ops" which > > > > most of its members are function pointers. > > > > The member ordering of the bpf_prog's btf-type can be different from > > > > the btf_vmlinux's btf-type. > > > > > > > > Once the prog's properties are all set, > > > > the libbpf will proceed to load all the progs. > > > > > > > > After that, register_struct_ops() will create a map, finalize the > > > > map-value by populating it with the prog-fd, and then register this > > > > "struct_ops" to the kernel by updating the map-value to the map. > > > > > > > > By default, libbpf does not unregister the struct_ops from the kernel > > > > during bpf_object__close(). It can be changed by setting the new > > > > "unreg_st_ops" in bpf_object_open_opts. > > > > > > > > Signed-off-by: Martin KaFai Lau <kafai@xxxxxx> > > > > --- > > > > > > This looks pretty good to me. The big two things is exposing structops > > > as real struct bpf_map, so that users can interact with it using > > > libbpf APIs, as well as splitting struct_ops map creation and > > > registration. bpf_object__load() should only make sure all maps are > > > created, progs are loaded/verified, but none of BPF program can yet be > > > called. Then attach is the phase where registration happens. > > Thanks for the review. > > > > [ ... ] > > > > > > static inline __u64 ptr_to_u64(const void *ptr) > > > > { > > > > return (__u64) (unsigned long) ptr; > > > > @@ -233,6 +239,32 @@ struct bpf_map { > > > > bool reused; > > > > }; > > > > > > > > +struct bpf_struct_ops { > > > > + const char *var_name; > > > > + const char *tname; > > > > + const struct btf_type *type; > > > > + struct bpf_program **progs; > > > > + __u32 *kern_func_off; > > > > + /* e.g. struct tcp_congestion_ops in bpf_prog's btf format */ > > > > + void *data; > > > > + /* e.g. struct __bpf_tcp_congestion_ops in btf_vmlinux's btf > > > > > > Using __bpf_ prefix for this struct_ops-specific types is a bit too > > > generic (e.g., for raw_tp stuff Alexei used btf_trace_). So maybe make > > > it btf_ops_ or btf_structops_? > > Is it a concern on name collision? > > > > The prefix pick is to use a more representative name. > > struct_ops use many bpf pieces and btf is one of them. > > Very soon, all new codes will depend on BTF and btf_ prefix > > could become generic also. > > > > Unlike tracepoint, there is no non-btf version of struct_ops. > May be bpf_struct_ops_? > > It was my early pick but it read quite weird, > bpf_[struct]_<ops>_[tcp_congestion]_<ops>. > > Hence, I go with __bpf_<actual-name-of-the-kernel-struct> in this series. bpf_struct_ops_ is much better, IMO, but given this struct serves only the purpose of providing type information to kernel, I think btf_struct_ops_ is more justified. And this <ops>_xxx_<ops> duplication doesn't bother me at all, again, because it's not directly used in C code. But believe me, having unique prefix is so good, even in the simples case of grepping through vmlinux.h.