Changed name to 'ignore_map_by_type' and created a similar function that does the same by name. Fixed renaming and added the new 3rd argument to relocate_maps() for rbpf test case

pull/968/head
martinsoees 7 months ago
parent c725b9d69e
commit aad4ad4fd3

@ -39,11 +39,11 @@
//! // Relocate the programs //! // Relocate the programs
//! #[cfg(feature = "std")] //! #[cfg(feature = "std")]
//! let text_sections = std::collections::HashSet::new(); //! let text_sections = std::collections::HashSet::new();
//! let deactivate_maps = std::collections::HashMap::new(); //! let ignore_maps = std::collections::HashMap::new();
//! #[cfg(not(feature = "std"))] //! #[cfg(not(feature = "std"))]
//! let text_sections = hashbrown::HashSet::new(); //! let text_sections = hashbrown::HashSet::new();
//! object.relocate_calls(&text_sections).unwrap(); //! object.relocate_calls(&text_sections).unwrap();
//! object.relocate_maps(std::iter::empty(), &text_sections, &deactivate_maps).unwrap(); //! object.relocate_maps(std::iter::empty(), &text_sections, &ignore_maps).unwrap();
//! //!
//! // Run with rbpf //! // Run with rbpf
//! let function = object.functions.get(&object.programs["prog_name"].function_key()).unwrap(); //! let function = object.functions.get(&object.programs["prog_name"].function_key()).unwrap();

@ -137,7 +137,8 @@ pub struct EbpfLoader<'a> {
extensions: HashSet<&'a str>, extensions: HashSet<&'a str>,
verifier_log_level: VerifierLogLevel, verifier_log_level: VerifierLogLevel,
allow_unsupported_maps: bool, allow_unsupported_maps: bool,
deactivate_maps: HashSet<bpf_map_type>, ignore_maps_by_type: HashSet<bpf_map_type>,
ignore_maps_by_name: &'a [&'a str],
} }
/// Builder style API for advanced loading of eBPF programs. /// Builder style API for advanced loading of eBPF programs.
@ -176,7 +177,8 @@ impl<'a> EbpfLoader<'a> {
extensions: HashSet::new(), extensions: HashSet::new(),
verifier_log_level: VerifierLogLevel::default(), verifier_log_level: VerifierLogLevel::default(),
allow_unsupported_maps: false, allow_unsupported_maps: false,
deactivate_maps: HashSet::new(), ignore_maps_by_type: HashSet::new(),
ignore_maps_by_name: &[],
} }
} }
@ -229,9 +231,10 @@ impl<'a> EbpfLoader<'a> {
/// Allows programs containing unsupported maps for the current kernel to be loaded /// Allows programs containing unsupported maps for the current kernel to be loaded
/// by skipping map creation and relocation before loading. /// by skipping map creation and relocation before loading.
/// ///
/// This is useful when you have a single program containing e.g. a `RingBuf` /// This is useful when you have a single ebpf program containing e.g. a `RingBuf`
/// and a `PerfEventArray` and you decide which one to use before loading the bytecode. /// and a `PerfEventArray` and you decide which one to use before loading the bytecode.
/// Can be used with `.set_global()` to signal if the map is supported. ///
/// Must be used with `.set_global()` to signal if the map is supported in the ebpf program.
/// ///
/// # Example /// # Example
/// ///
@ -240,16 +243,44 @@ impl<'a> EbpfLoader<'a> {
/// use aya_obj::generated::bpf_map_type; /// use aya_obj::generated::bpf_map_type;
/// use std::collections::HashSet; /// use std::collections::HashSet;
/// ///
/// let ringbuf_supported = 0;
/// let mut set = HashSet::new(); /// let mut set = HashSet::new();
/// set.insert(bpf_map_type::BPF_MAP_TYPE_RINGBUF); /// set.insert(bpf_map_type::BPF_MAP_TYPE_RINGBUF);
/// let ebpf = EbpfLoader::new() /// let ebpf = EbpfLoader::new()
/// .deactivate_maps(set) /// .ignore_maps_by_type(set)
/// .set_global("RINGBUF_SUPPORTED", &ringbuf_supported, true)
/// .load_file("file.o")?;
/// # Ok::<(), aya::EbpfError>(())
/// ```
///
pub fn ignore_maps_by_type(&mut self, set: HashSet<bpf_map_type>) -> &mut Self {
self.ignore_maps_by_type = set;
self
}
/// Allows programs containing unsupported maps for the current kernel to be loaded
/// by skipping map creation and relocation before loading.
///
/// This is useful when you have a single ebpf program containing e.g. a `RingBuf`
/// and a `PerfEventArray` and you decide which one to use before loading the bytecode.
///
/// Must be used with `.set_global()` to signal if the map is supported in the ebpf program.
///
/// # Example
///
/// ```no_run
/// use aya::EbpfLoader;
///
/// let ringbuf_supported = 0;
/// let ebpf = EbpfLoader::new()
/// .ignore_maps_by_name(&["RINGBUF"])
/// .set_global("RINGBUF_SUPPORTED", &ringbuf_supported, true)
/// .load_file("file.o")?; /// .load_file("file.o")?;
/// # Ok::<(), aya::EbpfError>(()) /// # Ok::<(), aya::EbpfError>(())
/// ``` /// ```
/// ///
pub fn deactivate_maps(&mut self, set: HashSet<bpf_map_type>) -> &mut Self { pub fn ignore_maps_by_name(&mut self, name: &'a [&'a str]) -> &mut Self {
self.deactivate_maps = set; self.ignore_maps_by_name = name;
self self
} }
@ -423,7 +454,8 @@ impl<'a> EbpfLoader<'a> {
extensions, extensions,
verifier_log_level, verifier_log_level,
allow_unsupported_maps, allow_unsupported_maps,
deactivate_maps, ignore_maps_by_type,
ignore_maps_by_name,
} = self; } = self;
let mut obj = Object::parse(data)?; let mut obj = Object::parse(data)?;
obj.patch_map_data(globals.clone())?; obj.patch_map_data(globals.clone())?;
@ -499,7 +531,7 @@ impl<'a> EbpfLoader<'a> {
} }
let map_type: bpf_map_type = obj.map_type().try_into().map_err(MapError::from)?; let map_type: bpf_map_type = obj.map_type().try_into().map_err(MapError::from)?;
if deactivate_maps.contains(&map_type) { if ignore_maps_by_type.contains(&map_type) || ignore_maps_by_name.contains(&name.as_str()) {
ignored_maps.insert(name, obj); ignored_maps.insert(name, obj);
// ignore map creation. The map is saved in `ignored_maps` and filtered out // ignore map creation. The map is saved in `ignored_maps` and filtered out
// in `relocate_maps()` later on // in `relocate_maps()` later on

@ -81,11 +81,13 @@ fn use_map_with_rbpf() {
.iter() .iter()
.map(|((section_index, _), _)| *section_index) .map(|((section_index, _), _)| *section_index)
.collect(); .collect();
let ignored_maps = HashMap::new();
object object
.relocate_maps( .relocate_maps(
maps.iter() maps.iter()
.map(|(s, (fd, map))| (s.as_ref() as &str, *fd, map)), .map(|(s, (fd, map))| (s.as_ref() as &str, *fd, map)),
&text_sections, &text_sections,
&ignored_maps,
) )
.expect("Relocation failed"); .expect("Relocation failed");
// Actually there is no local function call involved. // Actually there is no local function call involved.

Loading…
Cancel
Save