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

maps: MapFd and SockMapFd are owned #770

Merged
merged 3 commits into from
Sep 18, 2023
Merged

maps: MapFd and SockMapFd are owned #770

merged 3 commits into from
Sep 18, 2023

Conversation

tamird
Copy link
Member

@tamird tamird commented Aug 29, 2023

MapData::fd is now a MapFd. This means that MapData now closes the
file descriptor on drop. In the future we might consider making MapFd
hold a BorrowedFd but this requires API design work due to overlapping
borrows.

Since SockMapFd is no longer Copy, attach methods to take it by
reference to allow callers to use it multiple times as they are
accustomed to doing.

SockMapFd implements try_clone. MapFd and SockMapFd are now
returned by reference to allow callers to avoid file descriptor cloning
when desired.

This is an API breaking change.

Updates #612.

@netlify
Copy link

netlify bot commented Aug 29, 2023

Deploy Preview for aya-rs-docs ready!

Name Link
🔨 Latest commit 0dacb34
🔍 Latest deploy log https://app.netlify.com/sites/aya-rs-docs/deploys/65085d7595d2cc0008651143
😎 Deploy Preview https://deploy-preview-770--aya-rs-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@mergify
Copy link

mergify bot commented Aug 29, 2023

Hey @alessandrod, this pull request changes the Aya Public API and requires your review.

@mergify mergify bot requested a review from alessandrod August 29, 2023 21:20
@mergify mergify bot added api/needs-review Makes an API change that needs review aya This is about aya (userspace) test A PR that improves test cases or CI labels Aug 29, 2023
@tamird
Copy link
Member Author

tamird commented Aug 29, 2023

cc @nrxus

aya/src/bpf.rs Outdated
@@ -510,7 +510,7 @@ impl<'a> BpfLoader<'a> {

obj.relocate_maps(
maps.iter()
.map(|(s, data)| (s.as_str(), data.fd, &data.obj)),
.map(|(s, data)| (s.as_str(), data.fd().as_fd().as_raw_fd(), &data.obj)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not pass a BorrowedFd<'_> here instead? The less references we have to RawFds the easier it'll be gauge that our file descriptors are all valid.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC the function we're calling here is available in no_std, so BorrowedFd can't appear in its signature.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made that function available only in std. Unclear to me how anyone would use such a thing without access to real file descriptors.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should undo the aya-obj bit. Relocation puts file descriptors in the instruction stream where they must be cast as integers anyway, so lifetime tracking doesn't buy us much and it makes aya-obj essentially useless on no_std.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do that. But can you help me understand how someone would use this in no_std given you can't get your hands on an FD? Or is the idea that the user has an FD through some other means?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's unfortunate that *Fd are defined in std::os, in an ideal world some of the os definitions should be moved to core like it happened for net. But in a no_std program it's easy to imagine that one could still create fds using lower level linux APIs, and it seems silly that we would preclude them from using aya-obj just for a type alias.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is equivalent to the core::net types since those are just scalars like IP addresses. Anyway, done -- I just defined a local alias for RawFd in this crate in no_std.

aya/src/maps/mod.rs Outdated Show resolved Hide resolved
}
}

impl Clone for MapData {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we implement a try_clone() for MapData so maps can still be cloned?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can. In light of #703 I'm not sure how much it matters.

@mergify mergify bot added the aya-obj Relating to the aya-obj crate label Aug 31, 2023
@@ -105,7 +110,11 @@ pub(crate) struct Symbol {

impl Object {
/// Relocates the map references
pub fn relocate_maps<'a, I: Iterator<Item = (&'a str, i32, &'a Map)>>(
#[cfg(feature = "std")]
pub fn relocate_maps<
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes aya-obj a lot less useful without std. Do you think it would be too
painful to make this work with both RawFd and BorrowedFd? (Doesn't seem like but
I haven't tried)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous discussion on this topic is here. How do you get your hands on an FD in no_std?

Copy link
Collaborator

@alessandrod alessandrod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the aya-obj std thing, lgtm

`MapData::fd` is now a `MapFd`. This means that `MapData` now closes the
file descriptor on drop. In the future we might consider making `MapFd`
hold a `BorrowedFd` but this requires API design work due to overlapping
borrows.

Since `SockMapFd` is no longer `Copy`, attach methods to take it by
reference to allow callers to use it multiple times as they are
accustomed to doing.

`SockMapFd` implements `try_clone`. `MapFd` and `SockMapFd` are now
returned by reference to allow callers to avoid file descriptor cloning
when desired.

This is an API breaking change.

Updates #612.
@tamird tamird merged commit 41d01f6 into main Sep 18, 2023
21 checks passed
@tamird tamird deleted the mapfd-is-owned branch September 18, 2023 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api/needs-review Makes an API change that needs review aya This is about aya (userspace) aya-obj Relating to the aya-obj crate test A PR that improves test cases or CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants