Skip to content

Conversation

mattijons
Copy link

Idea #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 mattijons requested a review from a team as a code owner September 2, 2025 11:41
@mattijons mattijons force-pushed the bpf2go-string-constants branch from c8dcfcf to 76b761a Compare September 2, 2025 11:43
@mattijons mattijons force-pushed the bpf2go-string-constants branch from 76b761a to eafdbd2 Compare September 2, 2025 11:43
@lmb
Copy link
Collaborator

lmb commented Sep 11, 2025

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?

@ti-mo
Copy link
Collaborator

ti-mo commented Sep 11, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants