Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically adding entries to an eBPF map of maps #436

Open
pvvm opened this issue Jul 17, 2024 · 2 comments
Open

Dynamically adding entries to an eBPF map of maps #436

pvvm opened this issue Jul 17, 2024 · 2 comments

Comments

@pvvm
Copy link

pvvm commented Jul 17, 2024

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?

@ncshy
Copy link

ncshy commented Jul 19, 2024

From man bpf page,

 int bpf_create_map(enum bpf_map_type map_type,
                                 unsigned int key_size,
                                 unsigned int value_size,
                                 unsigned int max_entries)
                  {
                      union bpf_attr attr = {
                          .map_type    = map_type,
                          .key_size    = key_size,
                          .value_size  = value_size,
                          .max_entries = max_entries
                      };

The value size has to be defined as bytes. So try using :

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

@pvvm
Copy link
Author

pvvm commented Jul 19, 2024

Oh that makes sense, thanks! I've updated it with your suggestion and it isn't showing me that error anymore.
However, when I try running the following code:

int inner_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "test", sizeof(__u32), sizeof(struct test), 4, 0);
int err = bpf_map_update_elem(map_fd, &key, &inner_fd, BPF_ANY);

err receives -22 returned from bpf_map_update_elem, which means that an argument is invalid.

In that same example with integer as the value of the inner map, when I used sizeof(__u32), instead of sizeof(struct test), it was working normally. So, I imagine that this might be the incorrect argument, but I wonder why

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

No branches or pull requests

2 participants