How to get access to .bss map? #1861
-
I have an application originally written using libbpf that I have been gradually porting to cilium/ebpf. It uses some global variables to modify the eBPF (XDP) application's behavior after it has been loaded to the kernel. This can be done both by the main userspace daemon managing the program (in which case the new Variables concept works fine), or by separate processes, like CLI calls that load the .bss map from the BPF filesystem (previously pinned) and alter program configurations directly. With libbpf, the skeleton exposes a handle to the But apparently this is not as simple to do with So AFAIU, the pseudo-code bellow represents the steps I have to follow to get a handle to the // Get program info
pinfo, err := prog.Info()
...
// Access list of maps used by the program
mids, err := pinfo.MapIDs()
...
// Iterate over the list of maps to find the one I want
for _, mid := range mids {
m, err := ebpf.NewMapFromID(mid)
...
// Get info to check the map's name
minfo, err := m.Info()
...
if minfo.Name == ".bss" {
// found it!
// pin the map
}
m.Close()
} Is this really the way to implement this use case today? Or is there a simpler approach? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi Matheus, good question! If I understand your post correctly, your problem stems mostly from trying to manage global variables using external tools, right? In that case, as an aside, you can specify an ELF section of your choosing when declaring globals: Now, if you specify a default value for a global in your C code ( I often recommend bpf2go users they declare custom structs to pass to LoadAndAssign (which is what bpf2go uses underlyingly). In your case, something like the following should work:
Typically, looking at Cilium at least, we have dozens of maps and programs in any given Collection, but we only interact with a few of them at load time, like attaching entrypoints to some xdp or tc hooks. Most maps are declared with LIBBPF_PIN_BY_NAME and accessed by other parts of the agent using the pin. Most other programs are tail calls and are inserted into prog arrays automatically. Only the few objects we interact with are mentioned in If you don't want to maintain this struct definition yourself due to its size, you can also opt for Hope this helps! |
Beta Was this translation helpful? Give feedback.
Hi Matheus, good question! If I understand your post correctly, your problem stems mostly from trying to manage global variables using external tools, right?
In that case, as an aside, you can specify an ELF section of your choosing when declaring globals:
uint32_t global_var __section(".data.myconfig");
. This is really useful for dividing globals into categories. It'll also result in more predictable behaviour compared to relying on .bss, since the compiler typically puts any zero-initialized variable there, including static vars with local scope.Now, if you specify a default value for a global in your C code (
uint32_t global_var = 42
), it'll suddenly appear in.data
instead of.bss
, br…