-
Say, we have... struct FOO
{
__u64 foo;
__u64 bar;
};
struct {
__uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
__uint(max_entries, 8192);
__type(key, __u32);
__array(values, struct FOO);
} baz SEC(".maps"); ...how do I correctly load and set up things on the Go side? I've tried to find examples, but had hits only on testdata and unfortunately can't see how to do it. Are the inner maps automatically created for all entries of the outer map or do I need to allocate them myself? Is there a way to resize the outer map at creation time? Any help greatly appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 11 replies
-
@thediveo Could you elaborate on what you want to accomplish on the Go side? I assume you want to create new inner maps and store them in the outer map? Either way, the example we have in testdata is quite representative, but this is an alternative version that doesn't explicitly declare the inner map as a separate map in the ELF: struct {
__uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
__type(key, uint32_t);
__type(value, uint32_t);
__uint(max_entries, 16);
__array(values, struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, uint64_t);
__type(value, uint64_t);
__uint(max_entries, 1024);
});
} outer_map __section(".maps");
Reading through https://docs.kernel.org/bpf/map_of_maps.html may be helpful, it explains the difference between HASH_OF_MAPS and ARRAY_OF_MAPS. tl;dr: the inner maps themselves (not the array/hash data structures powering the outer map!) are never preallocated. All map creations are performed by user space. A shorthand for this is using
The outer map definition can be found in To build on my previous point: here's how to create more instances of the inner map and insert them into the outer map: spec, _ := LoadCollectionSpec(file)
inner := spec.Maps["outer_map"].InnerMap
coll, _ := NewCollection(spec)
for i := range 10 {
m, _ := NewMap(inner)
_ = coll.Maps["outer_map"].Put(uint32(i), m)
} This will instantiate 10 inner maps and insert them into Hope this helped! |
Beta Was this translation helpful? Give feedback.
@thediveo Could you elaborate on what you want to accomplish on the Go side? I assume you want to create new inner maps and store them in the outer map? Either way, the example we have in testdata is quite representative, but this is an alternative version that doesn't explicitly declare the inner map as a separate map in the ELF: