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

aya-bpf: Add docs for the main lib and few map types #244

Closed
wants to merge 1 commit into from

Conversation

vadorovsky
Copy link
Member

This change adds a summary of the aya-bpf lib and docs for:

  • hash map
  • lpm trie
  • perf event arrays

Signed-off-by: Michal Rostecki [email protected]

@vadorovsky vadorovsky force-pushed the aya-bpf-docs-1 branch 2 times, most recently from be662d2 to fee288a Compare April 13, 2022 08:43
@vadorovsky vadorovsky changed the title aya-bpf: Add docs for the main lib annd few map types aya-bpf: Add docs for the main lib and few map types Apr 13, 2022
@vadorovsky vadorovsky force-pushed the aya-bpf-docs-1 branch 2 times, most recently from 82ef66d to d0263e5 Compare April 17, 2022 10:09
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.

Thanks so much for getting this started!

See my comments. I think the reference docs are generally ok, but I think we need better examples.

I wouldn't worry about the module level documentation too much for now. I think we'll have to be very thorough when we get to that, and possibly cross reference examples with the user space API. I think that's going to be a lot of work so we can probably do it separately/at the end once everything else is in place.

/// # use aya_bpf::programs::LsmContext;
///
/// #[map]
/// static mut MY_MAP: HashMap<u32, u32> = HashMap::with_max_entries(1024, 0);
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 it would be better to have concrete examples. They can still be just 2-3 lines, but with actual (simple) use cases.

Choose a reason for hiding this comment

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

Perhaps given the limits on everyone's time, and the fact that this PR ended up going stale would suggest that follow-up items for expanding this documentation would be good: e.g. we could accept this as-is for now as "better than nothing" and then follow-up issues to extend the documentation further could be created, tagged as good-first-issue so as to provide some smaller, simple places new contributors could jump in and contribute?

/// #[map]
/// static mut MY_MAP: HashMap<u32, u32> = HashMap::with_max_entries(1024, 0);
///
/// # unsafe fn try_test(ctx: &LsmContext) -> Result<i32, i32> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

In all the examples, I think we should explicitly show unsafe usage, so the wrapper funcs shouldn't be unsafe

Choose a reason for hiding this comment

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

+1 if I'm understanding the original intention here it was to try and "scare" the caller of the code into noticing that what they're doing is unsafe, and I get that. However in Rust it's more idiomatic to "wrap" the unsafe functionality with code that carefully considers and accounts for all the potential safety issues and then provide a safe interface to that functionality.

Given my impression that the intention here was meant to alert the reader of the safety issues, I think a good compromise here would be to place the unsafe around the exact thing it needs to be around but make the function itself safe as @alessandrod suggests, but THEN add comments to that code which explains why we must currently do that as unsafe (extra credit if there's an open issue we can link to which tracks making that code safe in the future).

@@ -19,6 +41,16 @@ pub struct HashMap<K, V> {
}

impl<K, V> HashMap<K, V> {
/// Creates a `HashMap` with the maximum number of elements.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Creates an empty `HashMap<K, V>` with the specified maximum number of elements.

Tip: for inspiration on how to phrase the docs for the userspace API, I took inspiration from similar stdlib docs, eg Vec::with_capacity() is a good analogous of ::with_max_entries().

@@ -27,6 +59,17 @@ impl<K, V> HashMap<K, V> {
}
}

/// Creates a `HashMap` pinned in the BPFFS filesystem, with the maximum
Copy link
Collaborator

Choose a reason for hiding this comment

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

Creates an empty `HashMap<K, V>` with the specified maximum number of elements, and pins it to the BPF file system (bpffs).

///
/// # unsafe fn try_test(ctx: &LsmContext) -> Result<i32, i32> {
/// let key: u32 = 13;
/// if let Some(mut value) = MY_MAP.get_mut(&key) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Especially for mutable methods, I think it's important to show examples that make sense and aren't racy. And the documentation should point out that all the mutable methods are potentially racy if the keys aren't carefully chosen.

Choose a reason for hiding this comment

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

+1 we should be careful about examples that might not have clear implications to code readers, as example code often unfortunately finds it's way into production.

@dave-tucker dave-tucker added documentation Improvements or additions to documentation aya-bpf This is about aya-bpf (kernel) labels Apr 28, 2022
@netlify
Copy link

netlify bot commented May 20, 2022

Deploy Preview for aya-rs-docs failed.

Built without sensitive environment variables

Name Link
🔨 Latest commit 61dbd64
🔍 Latest deploy log https://app.netlify.com/sites/aya-rs-docs/deploys/6401c61ae9b44d0008b26c2e

Copy link

@shaneutt shaneutt left a comment

Choose a reason for hiding this comment

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

Overall Looking good: this significantly improves documentation.

I think for docs that could simply use more elaboration it may be fair to create follow-up issues as good-first-issues for newcomers to get a chance to make a small change. However I think there's a couple examples here that need more explanation, or perhaps some re-working before we merge.

bpf/aya-bpf/src/lib.rs Outdated Show resolved Hide resolved
bpf/aya-bpf/src/lib.rs Outdated Show resolved Hide resolved
/// # use aya_bpf::programs::LsmContext;
///
/// #[map]
/// static mut MY_MAP: HashMap<u32, u32> = HashMap::with_max_entries(1024, 0);

Choose a reason for hiding this comment

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

Perhaps given the limits on everyone's time, and the fact that this PR ended up going stale would suggest that follow-up items for expanding this documentation would be good: e.g. we could accept this as-is for now as "better than nothing" and then follow-up issues to extend the documentation further could be created, tagged as good-first-issue so as to provide some smaller, simple places new contributors could jump in and contribute?

/// #[map]
/// static mut MY_MAP: HashMap<u32, u32> = HashMap::with_max_entries(1024, 0);
///
/// # unsafe fn try_test(ctx: &LsmContext) -> Result<i32, i32> {

Choose a reason for hiding this comment

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

+1 if I'm understanding the original intention here it was to try and "scare" the caller of the code into noticing that what they're doing is unsafe, and I get that. However in Rust it's more idiomatic to "wrap" the unsafe functionality with code that carefully considers and accounts for all the potential safety issues and then provide a safe interface to that functionality.

Given my impression that the intention here was meant to alert the reader of the safety issues, I think a good compromise here would be to place the unsafe around the exact thing it needs to be around but make the function itself safe as @alessandrod suggests, but THEN add comments to that code which explains why we must currently do that as unsafe (extra credit if there's an open issue we can link to which tracks making that code safe in the future).

///
/// # unsafe fn try_test(ctx: &LsmContext) -> Result<i32, i32> {
/// let key: u32 = 13;
/// if let Some(mut value) = MY_MAP.get_mut(&key) {

Choose a reason for hiding this comment

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

+1 we should be careful about examples that might not have clear implications to code readers, as example code often unfortunately finds it's way into production.

@dave-tucker dave-tucker added this to the March Release 🗓️ milestone Feb 23, 2023
@vadorovsky vadorovsky force-pushed the aya-bpf-docs-1 branch 2 times, most recently from 5de6a05 to e0f2aed Compare February 27, 2023 15:43
@vadorovsky vadorovsky mentioned this pull request Feb 27, 2023
3 tasks
@vadorovsky
Copy link
Member Author

vadorovsky commented Mar 6, 2023

Closing it. I will submit smaller PRs per map type every day Attempting to document everything at once isn't really working.

@vadorovsky vadorovsky closed this Mar 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
aya-bpf This is about aya-bpf (kernel) documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants