aya: Make features a lazy_static

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
pull/522/head
Dave Tucker 2 years ago
parent bc83f208b1
commit ce22ca668f

@ -172,6 +172,25 @@ pub struct BtfFeatures {
pub btf_type_tag: bool, pub btf_type_tag: bool,
} }
impl std::fmt::Display for BtfFeatures {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_fmt(format_args!(
"[FEAT PROBE] BTF func support: {}\n\
[FEAT PROBE] BTF global func support: {}\n\
[FEAT PROBE] BTF var and datasec support: {}\n\
[FEAT PROBE] BTF float support: {}\n\
[FEAT PROBE] BTF decl_tag support: {}\n\
[FEAT PROBE] BTF type_tag support: {}",
self.btf_func,
self.btf_func_global,
self.btf_datasec,
self.btf_float,
self.btf_decl_tag,
self.btf_type_tag,
))
}
}
/// Bpf Type Format metadata. /// Bpf Type Format metadata.
/// ///
/// BTF is a kind of debug metadata that allows eBPF programs compiled against one kernel version /// BTF is a kind of debug metadata that allows eBPF programs compiled against one kernel version

@ -65,6 +65,10 @@ unsafe impl<T: Pod, const N: usize> Pod for [T; N] {}
pub use aya_obj::maps::{bpf_map_def, PinningType}; pub use aya_obj::maps::{bpf_map_def, PinningType};
lazy_static! {
pub(crate) static ref FEATURES: Features = Features::new();
}
// Features implements BPF and BTF feature detection // Features implements BPF and BTF feature detection
#[derive(Default, Debug)] #[derive(Default, Debug)]
pub(crate) struct Features { pub(crate) struct Features {
@ -73,42 +77,40 @@ pub(crate) struct Features {
} }
impl Features { impl Features {
fn probe_features(&mut self) { fn new() -> Self {
self.bpf_name = is_prog_name_supported(); let btf = if is_btf_supported() {
debug!("[FEAT PROBE] BPF program name support: {}", self.bpf_name); Some(BtfFeatures {
btf_func: is_btf_func_supported(),
self.btf = if is_btf_supported() { btf_func_global: is_btf_func_global_supported(),
Some(BtfFeatures::default()) btf_datasec: is_btf_datasec_supported(),
btf_float: is_btf_float_supported(),
btf_decl_tag: is_btf_decl_tag_supported(),
btf_type_tag: is_btf_type_tag_supported(),
})
} else { } else {
None None
}; };
debug!("[FEAT PROBE] BTF support: {}", self.btf.is_some()); let f = Features {
bpf_name: is_prog_name_supported(),
if let Some(ref mut btf) = self.btf { btf,
btf.btf_func = is_btf_func_supported(); };
debug!("[FEAT PROBE] BTF func support: {}", btf.btf_func);
btf.btf_func_global = is_btf_func_global_supported();
debug!(
"[FEAT PROBE] BTF global func support: {}",
btf.btf_func_global
);
btf.btf_datasec = is_btf_datasec_supported();
debug!(
"[FEAT PROBE] BTF var and datasec support: {}",
btf.btf_datasec
);
btf.btf_float = is_btf_float_supported();
debug!("[FEAT PROBE] BTF float support: {}", btf.btf_float);
btf.btf_decl_tag = is_btf_decl_tag_supported();
debug!("[FEAT PROBE] BTF decl_tag support: {}", btf.btf_decl_tag);
btf.btf_type_tag = is_btf_type_tag_supported(); debug!("{}", f);
debug!("[FEAT PROBE] BTF type_tag support: {}", btf.btf_type_tag); if let Some(btf) = f.btf.as_ref() {
debug!("{}", btf)
} }
f
}
}
impl std::fmt::Display for Features {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!(
"[FEAT PROBE] BPF program name support: {}\n\
[FEAT PROBE] BTF support: {}",
self.bpf_name,
self.btf.is_some()
))
} }
} }
@ -139,7 +141,6 @@ pub struct BpfLoader<'a> {
map_pin_path: Option<PathBuf>, map_pin_path: Option<PathBuf>,
globals: HashMap<&'a str, &'a [u8]>, globals: HashMap<&'a str, &'a [u8]>,
max_entries: HashMap<&'a str, u32>, max_entries: HashMap<&'a str, u32>,
features: Features,
extensions: HashSet<&'a str>, extensions: HashSet<&'a str>,
verifier_log_level: VerifierLogLevel, verifier_log_level: VerifierLogLevel,
} }
@ -169,14 +170,11 @@ impl Default for VerifierLogLevel {
impl<'a> BpfLoader<'a> { impl<'a> BpfLoader<'a> {
/// Creates a new loader instance. /// Creates a new loader instance.
pub fn new() -> BpfLoader<'a> { pub fn new() -> BpfLoader<'a> {
let mut features = Features::default();
features.probe_features();
BpfLoader { BpfLoader {
btf: Btf::from_sys_fs().ok().map(Cow::Owned), btf: Btf::from_sys_fs().ok().map(Cow::Owned),
map_pin_path: None, map_pin_path: None,
globals: HashMap::new(), globals: HashMap::new(),
max_entries: HashMap::new(), max_entries: HashMap::new(),
features,
extensions: HashSet::new(), extensions: HashSet::new(),
verifier_log_level: VerifierLogLevel::default(), verifier_log_level: VerifierLogLevel::default(),
} }
@ -360,8 +358,8 @@ impl<'a> BpfLoader<'a> {
let mut obj = Object::parse(data)?; let mut obj = Object::parse(data)?;
obj.patch_map_data(self.globals.clone())?; obj.patch_map_data(self.globals.clone())?;
let btf_fd = if let Some(ref btf) = self.features.btf { let btf_fd = if let Some(ref features) = FEATURES.btf {
if let Some(btf) = obj.fixup_and_sanitize_btf(btf)? { if let Some(btf) = obj.fixup_and_sanitize_btf(features)? {
// load btf to the kernel // load btf to the kernel
Some(load_btf(btf.to_bytes())?) Some(load_btf(btf.to_bytes())?)
} else { } else {
@ -449,7 +447,7 @@ impl<'a> BpfLoader<'a> {
.programs .programs
.drain() .drain()
.map(|(name, obj)| { .map(|(name, obj)| {
let prog_name = if self.features.bpf_name { let prog_name = if FEATURES.bpf_name {
Some(name.clone()) Some(name.clone())
} else { } else {
None None

Loading…
Cancel
Save