Skip to content

Commit da10a7f

Browse files
committed
bpf-rs: docs: BpfHelper
Signed-off-by: Milan <[email protected]>
1 parent 71c91ba commit da10a7f

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

bpf-feature/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ use std::{
1616
ptr,
1717
};
1818
use thiserror::Error as ThisError;
19-
// TODO: if this is the only use of nix
20-
// then consider just using libc
2119
use nix::{
2220
errno::{errno, Errno},
2321
sys::{
@@ -27,7 +25,6 @@ use nix::{
2725
unistd,
2826
};
2927

30-
// TODO: consider splitting up so that library clients don't need to match against all of them?
3128
#[derive(ThisError, Debug)]
3229
pub enum DetectError {
3330
#[error("failed to access capabilities")]

bpf-rs/src/lib.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TODO: enable #![warn(missing_docs)]
2+
// TODO: enable #![warn(missing_doc_code_examples)]
13
//! `bpf-rs` is a safe, lean library for inspecting and querying eBPF objects. A lot of the
24
//! design & inspiration stems from [bpftool](https://github.com/libbpf/bpftool) internals and
35
//! [libbpf-rs](https://docs.rs/libbpf-rs).
@@ -175,6 +177,8 @@ impl Iterator for ProgramTypeIter {
175177
}
176178
}
177179

180+
/// eBPF program object info. Similar to (but not the same) kernel header's
181+
/// [struct bpf_prog_info](https://github.com/torvalds/linux/blob/672c0c5173427e6b3e2a9bbb7be51ceeec78093a/include/uapi/linux/bpf.h#L5840)
178182
#[derive(Debug)]
179183
#[repr(C)]
180184
pub struct ProgramInfo {
@@ -359,10 +363,25 @@ impl Iterator for MapTypeIter {
359363
}
360364
}
361365

366+
/// eBPF helper function wrapper. See [`bpf-helpers(7)`](https://man7.org/linux/man-pages/man7/bpf-helpers.7.html)
367+
///
368+
/// Tuple field [`BpfHelper::0`] represents the unique id reserved by the kernel to represent the helper function. This
369+
/// unique id works almost as a counter with a max value:
370+
/// [`__BPF_FUNC_MAX_ID`](https://github.com/torvalds/linux/blob/672c0c5173427e6b3e2a9bbb7be51ceeec78093a/include/uapi/linux/bpf.h#L5350).
371+
/// This max limit changes between kernel versions due to the addition of eBPF helper functions.
372+
///
373+
/// For more information on eBPF helper functions, check out (although slightly outdated)
374+
/// [Marsden's Oracle blog post](https://blogs.oracle.com/linux/post/bpf-in-depth-bpf-helper-functions).
362375
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
363376
pub struct BpfHelper(pub u32);
364377

365378
impl BpfHelper {
379+
/// Human-readable name for an eBPF helper function
380+
///
381+
/// In the kernel, eBPF helper functions are usually represented as an int. This tries to create
382+
/// a digestable name for the helper functions but will return `<unknown>` or `<utf8err>` if not
383+
/// possible. For example, in the case that [`BpfHelper::0`] is outside the
384+
/// `__BPF_FUNC_MAX_ID` limit.
366385
pub fn name(&self) -> &'static str {
367386
match usize::try_from(self.0) {
368387
Ok(func_idx) => {
@@ -393,10 +412,17 @@ impl Debug for BpfHelper {
393412
}
394413
}
395414

415+
/// Iterator for the eBPF helper functions
396416
pub struct BpfHelperIter(u32);
397417

398418
impl BpfHelperIter {
399-
// Skips unspec helper
419+
/// Creates an ordered iterator
420+
///
421+
/// Order here is based on the ascending int ids used to represent the helper
422+
/// functions within the kernel. For most cases, this ordering property isn't
423+
/// needed.
424+
///
425+
/// **Note**: Skips `unspec` helper since it's an invalid function
400426
pub fn new() -> Self {
401427
Self(1)
402428
}

0 commit comments

Comments
 (0)