Skip to content

Commit

Permalink
aya/maps: fix libbpf_pin_by_name
Browse files Browse the repository at this point in the history
Aligns with libbpf for the special LIBBPF_PIN_BY_NAME
map flag. Specifically if the flag is provided without a pin path
default to "/sys/fs/bpf".

Signed-off-by: astoycos <astoycos@redhat.com>
  • Loading branch information
astoycos committed Oct 10, 2023
1 parent 66bd85a commit 0bf97eb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
13 changes: 7 additions & 6 deletions aya/src/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,13 @@ impl<'a> BpfLoader<'a> {
let mut map = match obj.pinning() {
PinningType::None => MapData::create(obj, &name, btf_fd)?,
PinningType::ByName => {
let path = map_pin_path.as_ref().ok_or(BpfError::NoPinPath)?;
MapData::create_pinned(path, obj, &name, btf_fd)?
// pin maps in /sys/fs/bpf by default to align with libbpf
// behavior https://github.com/libbpf/libbpf/blob/v1.2.2/src/libbpf.c#L2161.
let path = map_pin_path
.as_deref()
.unwrap_or_else(|| Path::new("/sys/fs/bpf"));

MapData::create_pinned_by_name(path, obj, &name, btf_fd)?
}
};
map.finalize()?;
Expand Down Expand Up @@ -951,10 +956,6 @@ pub enum BpfError {
error: io::Error,
},

/// Pinning requested but no path provided
#[error("pinning requested but no path provided")]
NoPinPath,

/// Unexpected pinning type
#[error("unexpected pinning type {name}")]
UnexpectedPinningType {
Expand Down
3 changes: 1 addition & 2 deletions aya/src/maps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ impl MapData {
Ok(Self { obj, fd })
}

pub(crate) fn create_pinned<P: AsRef<Path>>(
pub(crate) fn create_pinned_by_name<P: AsRef<Path>>(
path: P,
obj: obj::Map,
name: &str,
Expand Down Expand Up @@ -490,7 +490,6 @@ impl MapData {
}
Err(_) => {
let mut map = Self::create(obj, name, btf_fd)?;
let path = path.join(name);
map.pin(&path).map_err(|error| MapError::PinError {
name: Some(name.into()),
error,
Expand Down

0 comments on commit 0bf97eb

Please sign in to comment.