aya: Make features a lazy_static

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
pull/329/head
Dave Tucker 2 years ago
parent 2f40e623e0
commit 730cbabe31

@ -116,6 +116,10 @@ impl Default for 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 {
@ -131,41 +135,43 @@ pub(crate) struct Features {
} }
impl Features { impl Features {
fn probe_features(&mut self) { fn new() -> Self {
self.bpf_name = is_prog_name_supported(); let mut f = Features {
debug!("[FEAT PROBE] BPF program name support: {}", self.bpf_name); bpf_name: is_prog_name_supported(),
bpf_cookie: is_bpf_cookie_supported(),
self.bpf_cookie = is_bpf_cookie_supported(); btf: is_btf_supported(),
debug!("[FEAT PROBE] BPF cookie support: {}", self.bpf_cookie); ..Default::default()
};
self.btf = is_btf_supported(); debug!("[FEAT PROBE] BPF program name support: {}", f.bpf_name);
debug!("[FEAT PROBE] BTF support: {}", self.btf); debug!("[FEAT PROBE] BPF cookie support: {}", f.bpf_cookie);
debug!("[FEAT PROBE] BTF support: {}", f.btf);
if self.btf { if f.btf {
self.btf_func = is_btf_func_supported(); f.btf_func = is_btf_func_supported();
debug!("[FEAT PROBE] BTF func support: {}", self.btf_func); debug!("[FEAT PROBE] BTF func support: {}", f.btf_func);
self.btf_func_global = is_btf_func_global_supported(); f.btf_func_global = is_btf_func_global_supported();
debug!( debug!(
"[FEAT PROBE] BTF global func support: {}", "[FEAT PROBE] BTF global func support: {}",
self.btf_func_global f.btf_func_global
); );
self.btf_datasec = is_btf_datasec_supported(); f.btf_datasec = is_btf_datasec_supported();
debug!( debug!(
"[FEAT PROBE] BTF var and datasec support: {}", "[FEAT PROBE] BTF var and datasec support: {}",
self.btf_datasec f.btf_datasec
); );
self.btf_float = is_btf_float_supported(); f.btf_float = is_btf_float_supported();
debug!("[FEAT PROBE] BTF float support: {}", self.btf_float); debug!("[FEAT PROBE] BTF float support: {}", f.btf_float);
self.btf_decl_tag = is_btf_decl_tag_supported(); f.btf_decl_tag = is_btf_decl_tag_supported();
debug!("[FEAT PROBE] BTF decl_tag support: {}", self.btf_decl_tag); debug!("[FEAT PROBE] BTF decl_tag support: {}", f.btf_decl_tag);
self.btf_type_tag = is_btf_type_tag_supported(); f.btf_type_tag = is_btf_type_tag_supported();
debug!("[FEAT PROBE] BTF type_tag support: {}", self.btf_type_tag); debug!("[FEAT PROBE] BTF type_tag support: {}", f.btf_type_tag);
} }
f
} }
} }
@ -196,7 +202,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,
} }
@ -226,14 +231,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(),
} }
@ -417,12 +419,12 @@ 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 self.features.btf { let btf_fd = if FEATURES.btf {
if let Some(ref mut obj_btf) = obj.btf { if let Some(ref mut obj_btf) = obj.btf {
// fixup btf // fixup btf
let section_data = obj.section_sizes.clone(); let section_data = obj.section_sizes.clone();
let symbol_offsets = obj.symbol_offset_by_name.clone(); let symbol_offsets = obj.symbol_offset_by_name.clone();
obj_btf.fixup_and_sanitize(&section_data, &symbol_offsets, &self.features)?; obj_btf.fixup_and_sanitize(&section_data, &symbol_offsets, &FEATURES)?;
// load btf to the kernel // load btf to the kernel
let raw_btf = obj_btf.to_bytes(); let raw_btf = obj_btf.to_bytes();
Some(load_btf(raw_btf)?) Some(load_btf(raw_btf)?)
@ -508,7 +510,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