Skip to content

Commit

Permalink
feat: Autodetect bpf_loop availability
Browse files Browse the repository at this point in the history
  • Loading branch information
vadorovsky committed May 23, 2024
1 parent aa4da82 commit b9ba99e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
2 changes: 1 addition & 1 deletion crates/bpf-builder/include/loop.bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// Note: callback_fn must be declared as `static __always_inline` to satisfy the
// verifier. For some reason, having this double call to the same non-inline
// function seems to cause issues.
#ifdef FEATURE_FN_POINTERS
#ifdef FEATURE_BPF_LOOP
#define LOOP(max_iterations, max_unroll, callback_fn, ctx) \
if (bpf_core_enum_value_exists(enum bpf_func_id, BPF_FUNC_loop)) { \
bpf_loop(max_iterations, callback_fn, ctx, 0); \
Expand Down
4 changes: 3 additions & 1 deletion crates/bpf-common/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,16 @@ impl BpfContext {
Pinning::Disabled => format!("{PINNED_MAPS_PATH}_tmp"),
};

let features = autodetect_features();

Ok(Self {
pinning,
btf: Arc::new(btf),
perf_pages,
pinning_path,
log_level,
kernel_version,
features: autodetect_features(),
features,
})
}

Expand Down
6 changes: 5 additions & 1 deletion crates/bpf-feature-autodetect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ pub fn autodetect_features() -> BpfFeatures {
bpf_func_id::BPF_FUNC_get_current_task_btf,
BpfProgType::BPF_PROG_TYPE_CGROUP_SKB,
),
fn_pointers: true,
bpf_loop: func_id_supported(
bpf_func_id::BPF_FUNC_loop,
// Program type doesn't matter here.
BpfProgType::BPF_PROG_TYPE_KPROBE,
),
lsm: lsm_supported(),
}
}
Expand Down
40 changes: 20 additions & 20 deletions crates/bpf-features/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use quote::{quote, ToTokens};
pub struct BpfFeatures {
pub atomics: bool,
pub cgroup_skb_task_btf: bool,
pub fn_pointers: bool,
pub bpf_loop: bool,
pub lsm: bool,
}

Expand All @@ -22,11 +22,11 @@ impl BpfFeatures {
if self.cgroup_skb_task_btf {
feature_codes.push('c');
}
if self.fn_pointers {
feature_codes.push('f');
if self.bpf_loop {
feature_codes.push('l');
}
if self.lsm {
feature_codes.push('l');
feature_codes.push('s');
}

if feature_codes.is_empty() {
Expand All @@ -45,8 +45,8 @@ impl BpfFeatures {
if self.cgroup_skb_task_btf {
args.push("-DFEATURE_CGROUP_TASK_BTF".to_string());
}
if self.fn_pointers {
args.push("-DFEATURE_FN_POINTERS".to_string());
if self.bpf_loop {
args.push("-DFEATURE_BPF_LOOP".to_string());
}
if self.lsm {
args.push("-DFEATURE_LSM".to_string());
Expand All @@ -60,12 +60,12 @@ impl BpfFeatures {

for atomics in [true, false] {
for cgroup_skb_task_btf in [true, false] {
for fn_pointers in [true, false] {
for bpf_loop in [true, false] {
for lsm in [true, false] {
let features = Self {
atomics,
cgroup_skb_task_btf,
fn_pointers,
bpf_loop,
lsm,
};
combinations.insert(
Expand All @@ -85,14 +85,14 @@ impl ToTokens for BpfFeatures {
fn to_tokens(&self, tokens: &mut TokenStream) {
let atomics = self.atomics;
let cgroup_skb_task_btf = self.cgroup_skb_task_btf;
let fn_pointers = self.fn_pointers;
let bpf_loop = self.bpf_loop;
let lsm = self.lsm;

let generated = quote! {
BpfFeatures {
atomics: #atomics,
cgroup_skb_task_btf: #cgroup_skb_task_btf,
fn_pointers: #fn_pointers,
bpf_loop: #bpf_loop,
lsm: #lsm,
}
};
Expand All @@ -110,7 +110,7 @@ mod test {
let features = BpfFeatures {
atomics: false,
cgroup_skb_task_btf: false,
fn_pointers: false,
bpf_loop: false,
lsm: false,
};
assert_eq!(features.bpf_objfile_suffix(), "none.bpf.o");
Expand All @@ -119,7 +119,7 @@ mod test {
let features = BpfFeatures {
atomics: true,
cgroup_skb_task_btf: false,
fn_pointers: false,
bpf_loop: false,
lsm: false,
};
assert_eq!(features.bpf_objfile_suffix(), "a.bpf.o");
Expand All @@ -128,7 +128,7 @@ mod test {
let features = BpfFeatures {
atomics: true,
cgroup_skb_task_btf: true,
fn_pointers: false,
bpf_loop: false,
lsm: false,
};
assert_eq!(features.bpf_objfile_suffix(), "ac.bpf.o");
Expand All @@ -140,40 +140,40 @@ mod test {
let features = BpfFeatures {
atomics: true,
cgroup_skb_task_btf: true,
fn_pointers: true,
bpf_loop: true,
lsm: false,
};
assert_eq!(features.bpf_objfile_suffix(), "acf.bpf.o");
assert_eq!(features.bpf_objfile_suffix(), "acl.bpf.o");
assert_eq!(
features.build_args().as_slice(),
&[
"-DFEATURE_ATOMICS",
"-DFEATURE_CGROUP_TASK_BTF",
"-DFEATURE_FN_POINTERS"
"-DFEATURE_BPF_LOOP"
]
);

let features = BpfFeatures {
atomics: true,
cgroup_skb_task_btf: true,
fn_pointers: true,
bpf_loop: true,
lsm: true,
};
assert_eq!(features.bpf_objfile_suffix(), "acfl.bpf.o");
assert_eq!(features.bpf_objfile_suffix(), "acls.bpf.o");
assert_eq!(
features.build_args(),
&[
"-DFEATURE_ATOMICS",
"-DFEATURE_CGROUP_TASK_BTF",
"-DFEATURE_FN_POINTERS",
"-DFEATURE_BPF_LOOP",
"-DFEATURE_LSM"
]
);

let features = BpfFeatures {
atomics: false,
cgroup_skb_task_btf: true,
fn_pointers: false,
bpf_loop: false,
lsm: false,
};
assert_eq!(features.bpf_objfile_suffix(), "c.bpf.o");
Expand Down

0 comments on commit b9ba99e

Please sign in to comment.