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

bpf: Add bpf_strncmp helper #871

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

vadorovsky
Copy link
Member

The bpf_strncmp helper allows for better string comparison in eBPF programs.

Kernel patchset:

https://lore.kernel.org/bpf/20211210141652.877186-1-houtao1@huawei.com/

Copy link

netlify bot commented Jan 22, 2024

Deploy Preview for aya-rs-docs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit c8f5da1
🔍 Latest deploy log https://app.netlify.com/sites/aya-rs-docs/deploys/65ae5bad20200900090b26d0
😎 Deploy Preview https://deploy-preview-871--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 mergify bot added aya-bpf This is about aya-bpf (kernel) test A PR that improves test cases or CI labels Jan 22, 2024
@vadorovsky vadorovsky force-pushed the strncmp branch 2 times, most recently from 5b86ae3 to f937b7b Compare January 22, 2024 12:10
The `bpf_strncmp` helper allows for better string comparison in eBPF
programs.

Kernel patchset:

https://lore.kernel.org/bpf/20211210141652.877186-1-houtao1@huawei.com/
let dst = unsafe { ptr.as_mut() };
let TestResult(dst_res) = dst.ok_or(-1)?;

let cmp_res = bpf_strncmp(str_bytes, c"fff");
Copy link
Member Author

Choose a reason for hiding this comment

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

@alessandrod Are you fine with using CStr literals here? I had a bit of dilemma, since their stabilization goes slow. The other alternative could be using byte literals and forcing people to null terminate them.

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 the literal is fine, but both arguments should be &CStr?

/// }
/// ```
#[inline]
pub fn bpf_strncmp(s1: &[u8], s2: &CStr) -> Ordering {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't understand the API why one is a slice and the other is &CStr?

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, the most common way of using bpf_strncmp I've seen (and I'm doing in my projects 😛 ) is comparing a string we got from bpf_probe_read_user_str_bytes (which returns a byte slice) with some literal written in the program (where the CStr literal is very handy).

But yeah, I get your point about consistency. Since our whole bpf_probe_read_*_bytes API returns byte slices, I see two solutions:

  • We just use byte slices. And tell people to use byte literals - I'm mostly convinced to that so far.
  • We make everything return CStr - but nah, we would need to care about null termination checks and so.

So let's maybe make both &[u8]

Copy link

mergify bot commented Apr 20, 2024

@vadorovsky, this pull request is now in conflict and requires a rebase.

@mergify mergify bot added the needs-rebase label Apr 20, 2024
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) needs-rebase test A PR that improves test cases or CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants