xdp-project / xdp-tutorial

XDP tutorial
2.51k stars 583 forks source link

Dynamically adding entries to an eBPF map of maps #436

Open pvvm opened 4 months ago

pvvm commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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