aya icon indicating copy to clipboard operation
aya copied to clipboard

Allow maps to be created from user-space

Open dave-tucker opened this issue 4 years ago • 6 comments

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

dave-tucker avatar Aug 06 '21 10:08 dave-tucker

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. :)

loheagn avatar Nov 14 '23 12:11 loheagn

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 avatar Nov 14 '23 13:11 astoycos

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?

loheagn avatar Nov 14 '23 13:11 loheagn

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 avatar Nov 14 '23 13:11 astoycos

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!

loheagn avatar Nov 14 '23 13:11 loheagn

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

astoycos avatar Nov 14 '23 14:11 astoycos