aya-rs / aya

Aya is an eBPF library for the Rust programming language, built with a focus on developer experience and operability.
https://aya-rs.dev/book/
Apache License 2.0
3.2k stars 286 forks source link

Allow maps to be created from user-space #46

Open dave-tucker opened 3 years ago

dave-tucker commented 3 years ago

It would be nice if maps could be created from user-space before the program is loaded. This gives more granular control over which maps should (or should not) be pinned than what is available in #45.

As for the API, it's going to be a fairly big change and requires a bit of upfront design.

Typed maps could be able to be instantiated directly, like in the bpf crate, but we'd make the syscalls immediately (or in the case of pinning and the map exists, the use that instead).

let mut my_map  = HashMap<MyPodKey, MyPodValue>::new(255, Some(Path::new("myapp/global")));

The only complication is that created maps need to be available to Bpf::load so we can perform relocations. In which case we'd need to somehow register created maps with the Bpf struct and make ensure that all load functions have a reference to &self

loheagn commented 12 months ago

Hi, is this feature ready now? In my usecases, I want to create maps in userspace dynamically. There is a function bcc_create_map in bcc. I wonder how to do this in aya. :)

astoycos commented 12 months ago

Hiya @loheagn This isn't implemented yet, but I might be able to pick it up here soon! Will post back here with updates.

loheagn commented 12 months ago

Hiya @loheagn This isn't implemented yet, but I might be able to pick it up here soon! Will post back here with updates.

@astoycos Thanks! I found the function aya::maps::MapData::create, can I use this function to create maps? Do you have any suggestions?

astoycos commented 12 months ago

So aya::maps::MapData::create takes

        obj: obj::Map,
        name: &str,
        btf_fd: Option<BorrowedFd<'_>>,

as args meaning the map was defined in bytecode, and I don't think it was designed to be used directly (maybe this function shouldn't actually be public 🤔 ).

The API which @dave-tucker describes above allows direct instantiation of Maps with simple data such as K/V types+size along with pin path etc.

loheagn commented 12 months ago

So aya::maps::MapData::create takes

        obj: obj::Map,
        name: &str,
        btf_fd: Option<BorrowedFd<'_>>,

as args meaning the map was defined in bytecode, and I don't think it was designed to be used directly (maybe this function shouldn't actually be public 🤔 ).

The API which @dave-tucker describes above allows direct instantiation of Maps with simple data such as K/V types+size along with pin path etc.

@astoycos I see the differences. Then I may need some workaround. Thanks again!

astoycos commented 12 months ago

@loheagn PTAL at https://github.com/aya-rs/aya/issues/837 as it aligns with this