ebpf icon indicating copy to clipboard operation
ebpf copied to clipboard

bpf2go: Generate constant names for maps, programs, and variables

Open mattijons opened this issue 4 months ago • 2 comments

Idea https://github.com/cilium/ebpf/discussions/1833.

Add bpf2go functionality that generates constant string names for maps, programs, and variables. Useful when working with ebpf.Collection and ebpf.CollectionSpec.

Example output from TestObjects (now TestObjectsAndConstants).

// Constant names for all maps, variables, and programs as defined in the ELF file.
//
// They can be passed to ebpf.CollectionSpec and ebpf.Collection map fields.
const (
        // bar map names.
        barMapNameMap1 = "map1"
        // bar variable names.
        barVariableNameVar1 = "var_1"
        // bar program names.
        barProgramNameProgFoo1 = "prog_foo_1"
)

mattijons avatar Sep 02 '25 11:09 mattijons

What is the benefit of turning strings into constants? Some type of compile time error I guess?

My gut feeling is against adding this: usually names for maps and programs don't change that often, and in my experience are used only in a few places.

Maybe you can explain why you want to use bpf2go while still going via collection and collectionspec instead of the generated types?

lmb avatar Sep 11 '25 11:09 lmb

Maybe you can explain why you want to use bpf2go while still going via collection and collectionspec instead of the generated types?

IMO precisely to get the best of both worlds: compile-time codegen and ELF embedding + 'fearless' access of e.g. spec.Programs[SomeProgram] without really requiring a nil check, for maximum flexibility. If the prog gets moved or renamed, that code will break at compile time instead of at runtime.

ti-mo avatar Sep 11 '25 11:09 ti-mo