Applied cargo fmt to fix formatting

pull/968/head
martinsoees 7 months ago
parent c3084fd6a7
commit b05ab1599d

@ -32,7 +32,7 @@
//! //!
//! ```no_run //! ```no_run
//! use aya_obj::{generated::bpf_insn, Object, Map}; //! use aya_obj::{generated::bpf_insn, Object, Map};
//! //!
//! // Parse the object file //! // Parse the object file
//! let bytes = std::fs::read("program.o").unwrap(); //! let bytes = std::fs::read("program.o").unwrap();
//! let mut object = Object::parse(&bytes).unwrap(); //! let mut object = Object::parse(&bytes).unwrap();

@ -228,7 +228,6 @@ fn relocate_maps<'a, I: Iterator<Item = &'a Relocation>>(
index: rel.symbol_index, index: rel.symbol_index,
})?; })?;
let Some(section_index) = sym.section_index else { let Some(section_index) = sym.section_index else {
// this is not a map relocation // this is not a map relocation
continue; continue;
@ -240,7 +239,7 @@ fn relocate_maps<'a, I: Iterator<Item = &'a Relocation>>(
} }
let (_name, fd, map) = if ignored_by_symbol.contains(&rel.symbol_index) { let (_name, fd, map) = if ignored_by_symbol.contains(&rel.symbol_index) {
continue continue;
} else if let Some(m) = maps_by_symbol.get(&rel.symbol_index) { } else if let Some(m) = maps_by_symbol.get(&rel.symbol_index) {
let map = &m.2; let map = &m.2;
debug!( debug!(
@ -252,7 +251,7 @@ fn relocate_maps<'a, I: Iterator<Item = &'a Relocation>>(
debug_assert_eq!(map.symbol_index().unwrap(), rel.symbol_index); debug_assert_eq!(map.symbol_index().unwrap(), rel.symbol_index);
m m
} else if ignored_by_section.contains(&section_index) { } else if ignored_by_section.contains(&section_index) {
continue continue;
} else { } else {
let Some(m) = maps_by_section.get(&section_index) else { let Some(m) = maps_by_section.get(&section_index) else {
debug!("failed relocating map by section index {}", section_index); debug!("failed relocating map by section index {}", section_index);

@ -21,8 +21,8 @@ use thiserror::Error;
use crate::{ use crate::{
generated::{ generated::{
bpf_map_type::{self, *}, AYA_PERF_EVENT_IOC_DISABLE, AYA_PERF_EVENT_IOC_ENABLE, bpf_map_type::{self, *},
AYA_PERF_EVENT_IOC_SET_BPF, AYA_PERF_EVENT_IOC_DISABLE, AYA_PERF_EVENT_IOC_ENABLE, AYA_PERF_EVENT_IOC_SET_BPF,
}, },
maps::{Map, MapData, MapError}, maps::{Map, MapData, MapError},
obj::{ obj::{
@ -230,19 +230,19 @@ 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 ebpf 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.
/// ///
/// Must be used with `.set_global()` to signal if the map is supported in the ebpf program. /// Must be used with `.set_global()` to signal if the map is supported in the ebpf program.
/// ///
/// # Example /// # Example
/// ///
/// ```no_run /// ```no_run
/// use aya::EbpfLoader; /// use aya::EbpfLoader;
/// 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 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);
@ -252,7 +252,7 @@ impl<'a> EbpfLoader<'a> {
/// .load_file("file.o")?; /// .load_file("file.o")?;
/// # Ok::<(), aya::EbpfError>(()) /// # Ok::<(), aya::EbpfError>(())
/// ``` /// ```
/// ///
pub fn ignore_maps_by_type(&mut self, set: HashSet<bpf_map_type>) -> &mut Self { pub fn ignore_maps_by_type(&mut self, set: HashSet<bpf_map_type>) -> &mut Self {
self.ignore_maps_by_type = set; self.ignore_maps_by_type = set;
self self
@ -260,17 +260,17 @@ 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 ebpf 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.
/// ///
/// Must be used with `.set_global()` to signal if the map is supported in the ebpf program. /// Must be used with `.set_global()` to signal if the map is supported in the ebpf program.
/// ///
/// # Example /// # Example
/// ///
/// ```no_run /// ```no_run
/// use aya::EbpfLoader; /// use aya::EbpfLoader;
/// ///
/// let ringbuf_supported = 0; /// let ringbuf_supported = 0;
/// let ebpf = EbpfLoader::new() /// let ebpf = EbpfLoader::new()
/// .ignore_maps_by_name(&["RINGBUF"]) /// .ignore_maps_by_name(&["RINGBUF"])
@ -278,7 +278,7 @@ impl<'a> EbpfLoader<'a> {
/// .load_file("file.o")?; /// .load_file("file.o")?;
/// # Ok::<(), aya::EbpfError>(()) /// # Ok::<(), aya::EbpfError>(())
/// ``` /// ```
/// ///
pub fn ignore_maps_by_name(&mut self, name: &'a [&'a str]) -> &mut Self { pub fn ignore_maps_by_name(&mut self, name: &'a [&'a str]) -> &mut Self {
self.ignore_maps_by_name = name; self.ignore_maps_by_name = name;
self self
@ -531,7 +531,9 @@ 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 ignore_maps_by_type.contains(&map_type) || ignore_maps_by_name.contains(&name.as_str()) { 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

@ -11,13 +11,15 @@ enum DisableMapRelocation<'a> {
#[test] #[test]
fn test_ignored_map_relocation_by_type() { fn test_ignored_map_relocation_by_type() {
let mut ebpf = relocation_load_and_attach("test_ignored_map_relocation", crate::IGNORE_MAP, DisableMapRelocation::ByType(bpf_map_type::BPF_MAP_TYPE_RINGBUF)); let mut ebpf = relocation_load_and_attach(
"test_ignored_map_relocation",
crate::IGNORE_MAP,
DisableMapRelocation::ByType(bpf_map_type::BPF_MAP_TYPE_RINGBUF),
);
let perf = ebpf let perf = ebpf.take_map("PERFBUF");
.take_map("PERFBUF");
let ring = ebpf let ring = ebpf.take_map("RINGBUF");
.take_map("RINGBUF");
assert!(perf.is_some()); assert!(perf.is_some());
assert!(ring.is_none()); assert!(ring.is_none());
@ -25,13 +27,15 @@ fn test_ignored_map_relocation_by_type() {
#[test] #[test]
fn test_ignored_map_relocation_by_name() { fn test_ignored_map_relocation_by_name() {
let mut ebpf = relocation_load_and_attach("test_ignored_map_relocation", crate::IGNORE_MAP, DisableMapRelocation::ByName("RINGBUF")); let mut ebpf = relocation_load_and_attach(
"test_ignored_map_relocation",
crate::IGNORE_MAP,
DisableMapRelocation::ByName("RINGBUF"),
);
let perf = ebpf let perf = ebpf.take_map("PERFBUF");
.take_map("PERFBUF");
let ring = ebpf let ring = ebpf.take_map("RINGBUF");
.take_map("RINGBUF");
assert!(perf.is_some()); assert!(perf.is_some());
assert!(ring.is_none()); assert!(ring.is_none());
@ -69,7 +73,11 @@ fn text_64_64_reloc() {
assert_eq!(m.get(&1, 0).unwrap(), 3); assert_eq!(m.get(&1, 0).unwrap(), 3);
} }
fn relocation_load_and_attach(name: &str, bytes: &[u8], disable_type: DisableMapRelocation) -> Ebpf { fn relocation_load_and_attach(
name: &str,
bytes: &[u8],
disable_type: DisableMapRelocation,
) -> Ebpf {
let mut ebpf = match disable_type { let mut ebpf = match disable_type {
DisableMapRelocation::ByType(bmt) => { DisableMapRelocation::ByType(bmt) => {
let mut set = HashSet::new(); let mut set = HashSet::new();
@ -80,13 +88,11 @@ fn relocation_load_and_attach(name: &str, bytes: &[u8], disable_type: DisableMap
.load(bytes) .load(bytes)
.unwrap() .unwrap()
} }
DisableMapRelocation::ByName(name) => { DisableMapRelocation::ByName(name) => EbpfLoader::new()
EbpfLoader::new() .ignore_maps_by_name(&[name])
.ignore_maps_by_name(&[name]) .set_global("RINGBUF_SUPPORTED", &0, true)
.set_global("RINGBUF_SUPPORTED", &0, true) .load(bytes)
.load(bytes) .unwrap(),
.unwrap()
}
}; };
let prog: &mut UProbe = ebpf.program_mut(name).unwrap().try_into().unwrap(); let prog: &mut UProbe = ebpf.program_mut(name).unwrap().try_into().unwrap();

Loading…
Cancel
Save