Skip to content

Commit

Permalink
Fixups in response to alessandrod review
Browse files Browse the repository at this point in the history
Move BpfError::UnsupportedMap into MapError and add a map_type field to
the error.

Update the `allow_unsupported_maps` function comment.

Update the logic to check if any unsupported maps are being used. More
specifically only search a program's maps if `allow_unsupported_maps` is
set and then use the iter function `try_for_each` with a match statement
which allows us to return the inner map type if it's unsupported.

Signed-off-by: Andrew Stoycos <astoycos@redhat.com>
  • Loading branch information
astoycos committed Jun 20, 2023
1 parent b5719c5 commit 17930a8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
32 changes: 14 additions & 18 deletions aya/src/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,13 @@ impl<'a> BpfLoader<'a> {
self
}

/// Allows bytecode containing maps unsupported by Aya to be loaded.
/// Allows programs containing unsupported maps to be loaded.
///
/// By default, programs containing maps unsupported by Aya will not be loaded.
/// This function changes the default behavior, allowing programs to be loaded.
/// This should only be used in cases where you do not require access to eBPF
/// maps from this crate.
/// By default programs containing unsupported maps will fail to load. This
/// method can be used to configure the loader so that unsupported maps will
/// be loaded, but won't be accessible from userspace. Can be useful when
/// using unsupported maps that are only accessed from eBPF code and don't
/// require any userspace interaction.
///
/// # Example
///
Expand Down Expand Up @@ -629,15 +630,14 @@ impl<'a> BpfLoader<'a> {
.map(parse_map)
.collect::<Result<HashMap<String, Map>, BpfError>>()?;

if !self.allow_unsupported_maps
&& maps
.iter()
.filter(|(_, x)| matches!(x, Map::Unsupported(_)))
.count()
!= 0
{
return Err(BpfError::UnsupportedMap);
}
if !self.allow_unsupported_maps {
maps.iter().try_for_each(|(_, x)| match x {
Map::Unsupported(map) => Err(BpfError::MapError(MapError::Unsupported {
map_type: map.obj.map_type(),
})),
_ => Ok(()),
})?;
};

Ok(Bpf { maps, programs })
}
Expand Down Expand Up @@ -931,10 +931,6 @@ pub enum BpfError {
#[error("program error: {0}")]
/// A program error
ProgramError(#[from] ProgramError),

/// Unsupported Map type
#[error("Unsupported map types found")]
UnsupportedMap,
}

fn load_btf(raw_btf: Vec<u8>) -> Result<RawFd, BtfError> {
Expand Down
7 changes: 7 additions & 0 deletions aya/src/maps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ pub enum MapError {
#[source]
error: PinError,
},

/// Unsupported Map type
#[error("Unsupported map type found {map_type}")]
Unsupported {
/// The map type
map_type: u32,
},
}

/// A map file descriptor.
Expand Down

0 comments on commit 17930a8

Please sign in to comment.