bpf2go: Generate constant names for maps, programs, and variables
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"
)
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?
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.