Re: [PATCH bpf-next 11/15] bpftool: add skeleton codegen command

From: Jakub Kicinski
Date: Mon Dec 09 2019 - 20:57:54 EST

On Mon, 9 Dec 2019 17:14:34 -0800, Andrii Nakryiko wrote:
> struct <object-name> {
> /* used by libbpf's skeleton API */
> struct bpf_object_skeleton *skeleton;
> /* bpf_object for libbpf APIs */
> struct bpf_object *obj;
> struct {
> /* for every defined map in BPF object: */
> struct bpf_map *<map-name>;
> } maps;
> struct {
> /* for every program in BPF object: */
> struct bpf_program *<program-name>;
> } progs;
> struct {
> /* for every program in BPF object: */
> struct bpf_link *<program-name>;
> } links;
> /* for every present global data section: */
> struct <object-name>__<one of bss, data, or rodata> {
> /* memory layout of corresponding data section,
> * with every defined variable represented as a struct field
> * with exactly the same type, but without const/volatile
> * modifiers, e.g.:
> */
> int *my_var_1;
> ...
> } *<one of bss, data, or rodata>;
> };

I think I understand how this is useful, but perhaps the problem here
is that we're using C for everything, and simple programs for which
loading the ELF is majority of the code would be better of being
written in a dynamic language like python? Would it perhaps be a
better idea to work on some high-level language bindings than spend
time writing code gens and working around limitations of C?

> This provides great usability improvements:
> - no need to look up maps and programs by name, instead just
> my_obj->maps.my_map or my_obj->progs.my_prog would give necessary
> bpf_map/bpf_program pointers, which user can pass to existing libbpf APIs;
> - pre-defined places for bpf_links, which will be automatically populated for
> program types that libbpf knows how to attach automatically (currently
> tracepoints, kprobe/kretprobe, raw tracepoint and tracing programs). On
> tearing down skeleton, all active bpf_links will be destroyed (meaning BPF
> programs will be detached, if they are attached). For cases in which libbpf
> doesn't know how to auto-attach BPF program, user can manually create link
> after loading skeleton and they will be auto-detached on skeleton
> destruction:
> my_obj->links.my_fancy_prog = bpf_program__attach_cgroup_whatever(
> my_obj->progs.my_fancy_prog, <whatever extra param);
> - it's extremely easy and convenient to work with global data from userspace
> now. Both for read-only and read/write variables, it's possible to
> pre-initialize them before skeleton is loaded:
> skel = my_obj__open(raw_embed_data);
> my_obj->rodata->my_var = 123;
> my_obj__load(skel); /* 123 will be initialization value for my_var */
> After load, if kernel supports mmap() for BPF arrays, user can still read
> (and write for .bss and .data) variables values, but at that point it will
> be directly mmap()-ed to BPF array, backing global variables. This allows to
> seamlessly exchange data with BPF side. From userspace program's POV, all
> the pointers and memory contents stay the same, but mapped kernel memory
> changes to point to created map.
> If kernel doesn't yet support mmap() for BPF arrays, it's still possible to
> use those data section structs to pre-initialize .bss, .data, and .rodata,
> but after load their pointers will be reset to NULL, allowing user code to
> gracefully handle this condition, if necessary.
> Given a big surface area, skeleton is kept as an experimental non-public
> API for now, until more feedback and real-world experience is collected.

That makes no sense to me. bpftool has the same backward compat
requirements as libbpf. You're just pushing the requirements from
one component to the other. Feedback and real-world use cases have
to be exercised before code is merged to any project with backward
compatibility requirements :(

Also please run checkpatch on your patches, and fix reverse xmas tree.
This is bpftool, not libbpf. Creating a separate tool for this codegen
stuff is also an option IMHO.