Skip to content

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

Merged
ti-mo merged 1 commit intocilium:mainfrom
mattijons:bpf2go-string-constants
Apr 1, 2026
Merged

bpf2go: Generate constant names for maps, programs, and variables#1860
ti-mo merged 1 commit intocilium:mainfrom
mattijons:bpf2go-string-constants

Conversation

@mattijons
Copy link
Copy Markdown
Contributor

@mattijons mattijons commented Sep 2, 2025

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:

// Names of all BPF objects in the ELF.
//
// Used for safe lookups in a Collection or CollectionSpec.
const (
	barMapMap1      = "map1"
	barProgProgFoo1 = "prog_foo_1"
	barVarVar1      = "var_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 2 times, most recently from 76b761a to eafdbd2 Compare September 2, 2025 11:43
@lmb
Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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.

This commit emits constants for BPF object names for safe lookups in
Collection(Spec).Maps/Programs/Variables.

Signed-off-by: Matthías Á. Jónsson <matthiasasgeir@gmail.com>
Co-authored-by: Timo Beckers <timo@isovalent.com>
@ti-mo ti-mo force-pushed the bpf2go-string-constants branch from eafdbd2 to a369ac8 Compare April 1, 2026 09:55
@ti-mo ti-mo self-requested a review as a code owner April 1, 2026 09:55
@ti-mo ti-mo merged commit f83819e into cilium:main Apr 1, 2026
19 checks passed
@ti-mo
Copy link
Copy Markdown
Contributor

ti-mo commented Apr 1, 2026

@mattijons Thanks for working on this, I've removed most of the fluff around the generated code and shortened the intermediate 'prefixes' to Map/Prog/Var.

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