Skip to content

Dynamically adding entries to an eBPF map of maps #436

@pvvm

Description

@pvvm

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 test {
    __u64 value;
    __u64 value1;
};

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions