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

Introduce AsRawLibbpf trait #581

Merged
merged 1 commit into from
Sep 11, 2023

Conversation

danielocfb
Copy link
Collaborator

libbpf-rs aims to be a convenient but thin wrapper around libbpf functionality. Not all functionality provided of libbpf is exposed through its API. This can quickly become a blocker if, say, some getter is not hooked up, but the value required to perform an operation. In the spirit of being a thin wrapper, it makes sense for us to provide access to the underlying libbpf objects. When nothing else is available, these can be used directly in conjunction with libbpf-sys to, effectively, write Rust-syntax C code.
So far we have provided very ad-hoc accessor methods for retrieving pointers to the underlying libbpf objects. With this change we formalize this approach some more, by introducing the AsLibbpfObject trait and implementing it for major libbpf-rs types.
Using a trait is not the only possible way. We could also continue that path we were on earlier where relevant types had custom named accessors without a common trait (e.g., Map::as_libbpf_bpf_map_ptr). The benefits a trait brings to the table is that it unifies naming and makes it reasonably easy to get an overview of types that have libbpf counterparts.
The risk is that we may have to adjust the trait signature in one way or another if a newly added type has different constraints (e.g., if a libbpf-rs object has a potentially-NULL libbpf object).

libbpf-rs aims to be a convenient but thin wrapper around libbpf
functionality. However, not all functionality provided by libbpf is
exposed through its API. This can quickly become a blocker if, say, some
getter is not hooked up, but the returned value required to perform an
operation.
In the spirit of being a thin wrapper, it makes sense for us to provide
access to the underlying libbpf objects. When nothing else is available,
these can be used directly in conjunction with libbpf-sys to,
effectively, write Rust-syntax C code.
So far we have provided very ad-hoc accessor methods for retrieving
pointers to the underlying libbpf objects. With this change we formalize
this approach some more, by introducing the AsRawLibbpf trait and
implementing it for major libbpf-rs types.
Using a trait is not the only possible way. We could also continue on
the path we were on earlier where relevant types had custom named
accessors without a common trait (e.g., Map::as_libbpf_bpf_map_ptr). The
benefits a trait brings to the table is that it unifies naming and makes
it reasonably easy to get an overview of types that have libbpf
counterparts.
The risk is that we may have to adjust the trait signature in one way or
another if a newly added type has different constraints (e.g., if a
libbpf-rs object has a potentially-NULL libbpf object).

Signed-off-by: Daniel Müller <[email protected]>
@danobi
Copy link
Member

danobi commented Sep 11, 2023

Maybe "AsRawLibbpf"? "AsLibbpfObject" reads amibuous - those words have been used a lot in libbpf-rs as well

@danielocfb
Copy link
Collaborator Author

Maybe "AsRawLibbpf"? "AsLibbpfObject" reads amibuous - those words have been used a lot in libbpf-rs as well

Fine with me, will adjust. Thanks for the feedback.

@danielocfb danielocfb changed the title Introduce AsLibbpfObject trait Introduce AsRawLibbpf trait Sep 11, 2023
@danielocfb danielocfb merged commit 9f60d0a into libbpf:master Sep 11, 2023
10 checks passed
@danielocfb danielocfb deleted the topic/as-libbpf-obj branch September 11, 2023 18:55
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

Successfully merging this pull request may close these issues.

3 participants