libbpf / libbpf-rs

Minimal and opinionated eBPF tooling for the Rust ecosystem
Other
790 stars 138 forks source link

Generate key and value structs of BPF maps #972

Closed lbrndnr closed 1 month ago

lbrndnr commented 1 month ago

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

danielocfb commented 1 month ago

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.

lbrndnr commented 1 month ago

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

kxxt commented 1 month ago

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");
lbrndnr commented 1 month ago

@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!