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

Generate key and value structs of BPF maps #972

Closed
lbrndnr opened this issue Oct 19, 2024 · 4 comments
Closed

Generate key and value structs of BPF maps #972

lbrndnr opened this issue Oct 19, 2024 · 4 comments

Comments

@lbrndnr
Copy link
Contributor

lbrndnr commented Oct 19, 2024

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
Copy link
Collaborator

danielocfb commented Oct 21, 2024

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
Copy link
Contributor Author

lbrndnr commented Oct 22, 2024

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
Copy link
Contributor

kxxt commented Oct 22, 2024

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
Copy link
Contributor Author

lbrndnr commented Oct 22, 2024

@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 lbrndnr closed this as completed Oct 22, 2024
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

3 participants