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

feat: Improved bpf_loop probe #301

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/bpf-feature-autodetect/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
fn main() -> Result<(), Box<dyn std::error::Error>> {
bpf_builder::build("test_lsm", "src/test_lsm.bpf.c")
bpf_builder::build("test_bpf_loop", "src/test_bpf_loop.bpf.c")?;
bpf_builder::build("test_lsm", "src/test_lsm.bpf.c")?;
Ok(())
}
29 changes: 29 additions & 0 deletions crates/bpf-feature-autodetect/src/bpf_loop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use aya::{include_bytes_aligned, programs::TracePoint, Bpf, BpfError};
use log::warn;

fn load_probe() -> Result<(), BpfError> {
let mut bpf = Bpf::load(include_bytes_aligned!(concat!(
env!("OUT_DIR"),
"/test_bpf_loop.none.bpf.o"
)))?;
let program: &mut TracePoint = bpf.program_mut("probe_bpf_loop").unwrap().try_into()?;
program.load()?;
Ok(())
}

pub fn bpf_loop_supported() -> bool {
match load_probe() {
Ok(_) => true,
Err(e) => {
let err_msg = "`bpf_loop` helper is not supported by the kernel";

if let Ok(true) = std::env::var("RUST_BACKTRACE").map(|s| s == "1") {
warn!("{err_msg}: {e}");
} else {
warn!("{err_msg}");
}

false
}
}
}
13 changes: 6 additions & 7 deletions crates/bpf-feature-autodetect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ use libc::SYS_bpf;
use thiserror::Error;

pub mod atomic;
pub mod bpf_loop;
pub mod func;
pub mod insn;
pub mod kernel_version;
pub mod lsm;

use crate::{atomic::atomics_supported, func::func_id_supported, lsm::lsm_supported};
use crate::{
atomic::atomics_supported, bpf_loop::bpf_loop_supported, func::func_id_supported,
lsm::lsm_supported,
};

/// Size of the eBPF verifier log.
const LOG_SIZE: usize = 4096;
Expand All @@ -34,12 +38,7 @@ pub fn autodetect_features() -> BpfFeatures {
bpf_func_id::BPF_FUNC_get_current_task_btf,
BpfProgType::BPF_PROG_TYPE_CGROUP_SKB,
),
bpf_loop: func_id_supported(
"bpf_loop",
bpf_func_id::BPF_FUNC_loop,
// Program type doesn't matter here.
BpfProgType::BPF_PROG_TYPE_KPROBE,
),
bpf_loop: bpf_loop_supported(),
lsm: lsm_supported(),
}
}
Expand Down
12 changes: 12 additions & 0 deletions crates/bpf-feature-autodetect/src/test_bpf_loop.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>

static int callback(void *ctx, u32 index) {
return 0;
}

SEC("tracepoint")
int probe_bpf_loop(void *ctx) {
bpf_loop(5, callback, NULL, 0);
return 0;
}