fix: pass BTF object by reference in order to allow multiple eBPF programs to share it and save memory (closes #30). (#31)

pull/33/head
Simone Margaritelli 3 years ago committed by GitHub
parent 55ba0538f2
commit b4b019e447
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -90,7 +90,7 @@ impl Bpf {
path: path.to_owned(), path: path.to_owned(),
error, error,
})?, })?,
Btf::from_sys_fs().ok(), Btf::from_sys_fs().ok().as_ref(),
) )
} }
@ -98,7 +98,8 @@ impl Bpf {
/// ///
/// Parses the object code contained in `data` and initializes the [maps](crate::maps) defined /// Parses the object code contained in `data` and initializes the [maps](crate::maps) defined
/// in it. If `target_btf` is not `None` and `data` includes BTF debug info, [BTF](Btf) relocations /// in it. If `target_btf` is not `None` and `data` includes BTF debug info, [BTF](Btf) relocations
/// are applied as well. /// are applied as well. In order to allow sharing of a single [BTF](Btf) object among multiple
/// eBPF programs, `target_btf` is passed by reference.
/// ///
/// # Examples /// # Examples
/// ///
@ -108,10 +109,10 @@ impl Bpf {
/// ///
/// let data = fs::read("file.o").unwrap(); /// let data = fs::read("file.o").unwrap();
/// // load the BTF data from /sys/kernel/btf/vmlinux /// // load the BTF data from /sys/kernel/btf/vmlinux
/// let bpf = Bpf::load(&data, Some(Btf::from_sys_fs()?)); /// let bpf = Bpf::load(&data, Btf::from_sys_fs().ok().as_ref());
/// # Ok::<(), aya::BpfError>(()) /// # Ok::<(), aya::BpfError>(())
/// ``` /// ```
pub fn load(data: &[u8], target_btf: Option<Btf>) -> Result<Bpf, BpfError> { pub fn load(data: &[u8], target_btf: Option<&Btf>) -> Result<Bpf, BpfError> {
let mut obj = Object::parse(data)?; let mut obj = Object::parse(data)?;
if let Some(btf) = target_btf { if let Some(btf) = target_btf {

@ -151,7 +151,7 @@ impl Relocation {
} }
impl Object { impl Object {
pub fn relocate_btf(&mut self, target_btf: Btf) -> Result<(), BpfError> { pub fn relocate_btf(&mut self, target_btf: &Btf) -> Result<(), BpfError> {
let (local_btf, btf_ext) = match (&self.btf, &self.btf_ext) { let (local_btf, btf_ext) = match (&self.btf, &self.btf_ext) {
(Some(btf), Some(btf_ext)) => (btf, btf_ext), (Some(btf), Some(btf_ext)) => (btf, btf_ext),
_ => return Ok(()), _ => return Ok(()),
@ -174,13 +174,8 @@ impl Object {
function: section_name.to_owned(), function: section_name.to_owned(),
error: Box::new(RelocationError::ProgramNotFound), error: Box::new(RelocationError::ProgramNotFound),
})?; })?;
match relocate_btf_program( match relocate_btf_program(program, relos, local_btf, target_btf, &mut candidates_cache)
program, {
relos,
local_btf,
&target_btf,
&mut candidates_cache,
) {
Ok(_) => {} Ok(_) => {}
Err(ErrorWrapper::BtfError(e)) => return Err(e.into()), Err(ErrorWrapper::BtfError(e)) => return Err(e.into()),
Err(ErrorWrapper::RelocationError(error)) => { Err(ErrorWrapper::RelocationError(error)) => {

Loading…
Cancel
Save