Search code examples
ebpfbpfxdp-bpf

Dynamically adding entries to an eBPF map of maps


I want to create an eBPF hash of maps and add entries to this map gradually. Since BPF programs cannot add new entires to outer maps, my only option is to do that in the userspace program. And I found out a way to do that by defining a outer map and the expected inner map in the BPF program:

struct inner_array {
    __uint(type, BPF_MAP_TYPE_ARRAY);
    __type(key, __u32);
    __type(value, __u32);
    __uint(max_entries, 4);
};

struct {
    __uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
    __uint(max_entries, 10);
    __type(key, __u32);
    __array(values, struct inner_array);
} outer_hash SEC(".maps");

While the userspace program uses bpf_map_create to create a new inner map and bpf_map_update_elem to include it in the outer map.

However, instead of the inner map being an array of integers, I'd like for it to be an array of structs, such as:

struct inner_array {
    __uint(type, BPF_MAP_TYPE_ARRAY);
    __type(key, __u32);
    __type(value, struct test);
    __uint(max_entries, 4);
};

But when try to load the program, the following error is announced:

libbpf: map 'outer_hash.inner': can't determine value size for type [37]: -22.

And the thing is, I know that I can specify that the inner map should hold structs if I declare the inner maps statically, but I want to add new entries to a map dynamically at userspace. So, what can I do to have dynamic outer map entries while still having structs in the inner maps?


Solution

  • I found out that a possible solution is to set a pointer to the inner map as one of the values of the outer map, such as:

    struct inner_array {
        __uint(type, BPF_MAP_TYPE_ARRAY);
        __type(key, __u32);
        __type(value, struct test);
        __uint(max_entries, 4);
    } inner_array SEC(".maps");
    
    struct {
        __uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
        __uint(max_entries, 5 + 1);
        __type(key, __u32);
        __array(values, struct inner_array);
    } outer_hash SEC(".maps") = {
        .values = {(void*)&inner_array},
    };
    

    Here, I set max number of entries as 5 + 1 because it represents the 5 entries I want the outer map to have + 1 for this default entry. From what I noticed, the key to this default will be 0. But I can fill the other inner maps with whatever keys I want.