libbpf-rs icon indicating copy to clipboard operation
libbpf-rs copied to clipboard

Generate key and value structs of BPF maps

Open lbrndnr opened this issue 1 year ago • 1 comments

Hi everybody,

I noticed that the structs used in BPF maps, i.e. the key and value structs, are not available in the skeleton. I found myself redefining the same structs in rust, just so that it's easier to populate the maps from userspace. Am I missing something, or is this actually the easiest way to do it? :)

If this is indeed the case, I think it would be useful to export these structs as well to reduce boilerplate code.

Cheers, Laurin

lbrndnr avatar Oct 19 '24 12:10 lbrndnr

Can you provide an fully working example, please? I think we should be dumping all types available in BTF, which includes keys and values used in BPF maps.

danielocfb avatar Oct 21 '24 16:10 danielocfb

Sure, find attached the example. With my main project I noticed that some structs are exported, but others are not. In the attached project I can't get any struct to get generated. Either way, I think providing a working example of this could be very useful, as discussed in #312.

example.zip

lbrndnr avatar Oct 22 '24 06:10 lbrndnr

In the attached project I can't get any struct to get generated.

struct {
    __uint(type, BPF_MAP_TYPE_SOCKHASH);
    __uint(max_entries, 8192);
    __uint(key_size, sizeof(struct sock_key));
    __uint(value_size, sizeof(int));
} sock_map SEC(".maps");

This map doesn't contain type information. It probably need to be written like this:

struct {
    __uint(type, BPF_MAP_TYPE_SOCKHASH);
    __uint(max_entries, 8192);
    __type(key, struct sock_key);
    __type(value, int);
} sock_map SEC(".maps");

kxxt avatar Oct 22 '24 07:10 kxxt

@kxxt that indeed works, thank you! I noticed that in my main project I was using an outdated version of libbpf, where this doesn't work either. But in the example it works!

lbrndnr avatar Oct 22 '24 07:10 lbrndnr