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

Implement FdLink conversions #560

Merged
merged 5 commits into from
Jul 13, 2023
Merged

Conversation

astoycos
Copy link
Member

Implement TryFrom functions to convert Fdlink to PerfLink/TracePointLink/ KprobeLink, and UprobeLink and vice-versa.

This allows us to pin taken links for perf programs, ultimately ensuring the link isn't dropped when the loading process exits.

Required for bpfman/bpfman#306

TODO: It'd be nice to add some documentation around this API somewhere...

    let mut bpf = BpfLoader::new()
    // load pinned maps from /sys/fs/bpf/my-program
    .map_pin_path("/sys/fs/bpf")
    // finally load the code
    .load_file("/home/astoycos/go/src/github.com/redhat-et/bpfd/examples/go-tracepoint-counter/bpf_bpfel.o")?;
    let program: &mut TracePoint = bpf.program_mut("tracepoint_kill_recorder").unwrap().try_into()?;
    program.load()?;
    let link_id = program.attach("syscalls", "sys_enter_kill")?;
    program.pin("/sys/fs/bpf/tracepoint_kill_recorder_pin")?;
    let owned_link: TracePointLink = program.take_link(link_id)?;
    let fd_link: FdLink = owned_link.try_into()?;
    fd_link.pin("/sys/fs/bpf/tracepoint_kill_recorder_link")?;

@netlify
Copy link

netlify bot commented Mar 27, 2023

Deploy Preview for aya-rs-docs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 94f554a
🔍 Latest deploy log https://app.netlify.com/sites/aya-rs-docs/deploys/64af5f0b380ab00008a72731
😎 Deploy Preview https://deploy-preview-560--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.

@astoycos
Copy link
Member Author

astoycos commented Mar 27, 2023

Hrm I don't think this error is related:

Error: e: 19:33:45 [ERROR] llvm: Unknown attribute kind (86) (Producer: 'LLVM16.0.0-rust-1.70.0-nightly' Reader: 'LLVM 15.0.7')
          error: failure linking module /home/fedora/aya/test/integration-ebpf/../../target/bpfel-unknown-none/debug/deps/map_test-a9de5ef8344e854d.3hyxcigylsh430vj.rcgu.o
          19:33:45 [ WARN] ignoring file "/tmp/rustcIDSCW0/symbols.o": no embedded bitcode
          

error: could not compile `integration-ebpf` (bin "map_test") due to previous error

Copy link
Member

@dave-tucker dave-tucker left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks, we already do this for XDP and I'd forgotten to do this for the PerfLinks 😓

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!

I agree that this should be documented. We could add docs here https://docs.aya-rs.dev/user/aya/programs/links/struct.fdlink? Explain that FdLink allows you to pin, and that many links can be converted to FdLink by using either into() or try_into(), giving examples of both.

aya/src/programs/kprobe.rs Outdated Show resolved Hide resolved
@astoycos
Copy link
Member Author

astoycos commented May 2, 2023

Sorry It's been so long @alessandrod I fixed the issues and added integration testing for TracePoint, Uprobe and Kprobe programs. I'm happy to do it for perf_event progs here or in a follow up I'm just not 100% sure how to ATM and this is what I got to today. :)

@dave-tucker PTAL at how I also changed the integration test tooling to verify that a program is loaded AND linked in certain cases.

@astoycos
Copy link
Member Author

astoycos commented May 2, 2023

We could add docs here https://docs.aya-rs.dev/user/aya/programs/links/struct.fdlink? Explain that FdLink allows you to pin, and that many links can be converted to FdLink by using either into() or try_into(), giving examples of both.

Shoot and I still need to do this.

@astoycos astoycos force-pushed the fix-perf-link-pin branch 2 times, most recently from 6e5d1a5 to 8307435 Compare May 3, 2023 16:32
@astoycos
Copy link
Member Author

astoycos commented May 3, 2023

Shoot and I still need to do this.

done

aya/src/programs/links.rs Outdated Show resolved Hide resolved
@astoycos
Copy link
Member Author

Poke @alessandrod :)

@astoycos
Copy link
Member Author

Rebased 👍

@astoycos astoycos force-pushed the fix-perf-link-pin branch 2 times, most recently from 2ecb017 to 0f4cb94 Compare July 10, 2023 21:36
@ajwerner
Copy link
Member

@tamird and I are working on redoing the integration test infrastructure such that the tests themselves do not rely on userspace to execute. Today the cycle time of compiling and running tests in the VM is less than wonderful. I chatted with @alessandrod about it here.

The preferred solution would be to have aya provide the relevant APIs for reading these links. If it's easy to implement the functionality you need in aya as opposed to shelling out, would you be open to it?

@astoycos
Copy link
Member Author

Hiya @ajwerner and yeah I totally agree, we need a loaded_links() API for bpfd regardless to go along with the work I did here: #637

I'd rather go ahead and get this bit in for now since I've been carrying it for quite some time 😄 , but in the meantime I'll start working on a basic loaded_links() API.

Copy link
Member

@tamird tamird left a comment

Choose a reason for hiding this comment

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

Reviewed 7 of 8 files at r1, all commit messages.
Reviewable status: 7 of 8 files reviewed, 8 unresolved discussions (waiting on @alessandrod, @astoycos, and @dave-tucker)


aya/src/programs/kprobe.rs line 140 at r1 (raw file):

    fn try_from(fd_link: FdLink) -> Result<Self, Self::Error> {
        // unwrap of fd_link.fd will not panic since it's only None when being dropped.

there's no unwrap here?


aya/src/programs/links.rs line 89 at r1 (raw file):

/// process closes. Additionally many program specific links can be converted into
/// FdLinks by using either into() or try_into() implementations. For examples of such functions
/// see [`TracePointLink::try_from()`](crate::programs::trace_point::TracePointLink::try_from()) and [`FdLink::try_from()`].

nit: long line


aya/src/programs/perf_event.rs line 209 at r1 (raw file):

    fn try_from(fd_link: FdLink) -> Result<Self, Self::Error> {
        // unwrap of fd_link.fd will not panic since it's only None when being dropped.

ditto?


aya/src/programs/trace_point.rs line 135 at r1 (raw file):

    fn try_from(fd_link: FdLink) -> Result<Self, Self::Error> {
        // unwrap of fd_link.fd will not panic since it's only None when being dropped.

ditto


aya/src/programs/uprobe.rs line 180 at r1 (raw file):

    fn try_from(fd_link: FdLink) -> Result<Self, Self::Error> {
        // unwrap of fd_link.fd will not panic since it's only None when being dropped.

here


test/integration-test/tests/load.rs line 67 at r1 (raw file):

}

macro_rules! assert_loaded_and_linked {

can this just be assert_loaded followed by assert linked? i don't think we need to retry the bpftool invocation separately from waiting for loadedness

Copy link
Member

@tamird tamird left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 8 files at r1.
Reviewable status: all files reviewed, 9 unresolved discussions (waiting on @alessandrod, @astoycos, and @dave-tucker)


test/integration-ebpf/src/test.rs line 23 at r1 (raw file):

#[kprobe]
// truncated name to match bpftool output

the names were previously truncated to 15 characters, this comment is detritus

@astoycos astoycos force-pushed the fix-perf-link-pin branch 2 times, most recently from 70d667f to 9ce2283 Compare July 12, 2023 18:27
@astoycos
Copy link
Member Author

astoycos commented Jul 12, 2023

can this just be assert_loaded followed by assert linked? i don't think we need to retry the bpftool invocation separately from waiting for loadedness

This is the only thing I didn't get to, simply because these functions are built as macros and if we were to reuse them I'd need to return the program id from assert_loaded!() since we need that ID to find the corresponding link. This will change anyways with #645 so I think it's alright for now.

Copy link
Member

@tamird tamird left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 8 of 8 files at r2, 2 of 2 files at r3, all commit messages.
Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @alessandrod, @astoycos, and @dave-tucker)


test/integration-test/tests/load.rs line 59 at r3 (raw file):

fn is_linked(prog_id: &u32) -> bool {
    let output = Command::new("bpftool").args(["link"]).output();
    let output = match output {

i think this is output.expect(...)

@astoycos astoycos force-pushed the fix-perf-link-pin branch 2 times, most recently from f1c085e to f9ff234 Compare July 12, 2023 19:55
Copy link
Member

@tamird tamird left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r5, 4 of 4 files at r7, all commit messages.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @alessandrod and @dave-tucker)

Implement TryFrom functions to convert Fdlink to PerfLink/TracePointLink/
KprobeLink, and UprobeLink and vice-versa.

This allows us to pin taken links for perf programs, ultimately
ensuring the link isn't dropped when the loading process exits.

Signed-off-by: Andrew Stoycos <astoycos@redhat.com>
We need bpftool to add tests for the link APIs since we don't yet have
and aya API for listing links.

Signed-off-by: Andrew Stoycos <astoycos@redhat.com>
Add integration testing for link pinning and
loading/unloading of tracepoint, kprobe, and
uprobe programs.

Redo how we utilize bpftool to verify that programs
are loaded to be explicit with names. Also add a helper
to verify that a program is loaded AND linked.

Signed-off-by: Andrew Stoycos <astoycos@redhat.com>
Signed-off-by: Andrew Stoycos <astoycos@redhat.com>
in the integration tests we recenctly switched to using
our internal api to list programs. I was seeing times when
this would race and panic internally (program fd was deleted
by aya WHILE we were trying to get it).  This ensures that
the list succeeded without panicking.

Signed-off-by: Andrew Stoycos <astoycos@redhat.com>
@astoycos
Copy link
Member Author

Whelp ended up having to rebase again :/

@astoycos astoycos merged commit edb7baf into aya-rs:main Jul 13, 2023
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.

5 participants