Skip to content

Commit

Permalink
feat(aya): Implement Maps/Arrays of Maps
Browse files Browse the repository at this point in the history
This commit implements the Array of Maps and Hash of Maps to eBPF.

Signed-off-by: Dave Tucker <[email protected]>
  • Loading branch information
dave-tucker committed Jan 31, 2025
1 parent f9edaf9 commit 078c638
Show file tree
Hide file tree
Showing 35 changed files with 1,110 additions and 83 deletions.
20 changes: 12 additions & 8 deletions aya-obj/src/btf/btf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ pub enum BtfError {
/// unable to get symbol name
#[error("Unable to get symbol name")]
InvalidSymbolName,

/// an error occurred while parsing a the BTF data
#[error("BTF error: {0}")]
BtfError(String),
}

/// Available BTF features
Expand Down Expand Up @@ -591,14 +595,6 @@ impl Btf {
for e in entries.iter_mut() {
if let BtfType::Var(var) = types.type_by_id(e.btf_type)? {
let var_name = self.string_at(var.name_offset)?;
if var.linkage == VarLinkage::Static {
debug!(
"{} {}: VAR {}: fixup not required",
kind, name, var_name
);
continue;
}

let offset = match symbol_offsets.get(var_name.as_ref()) {
Some(offset) => offset,
None => {
Expand All @@ -612,6 +608,14 @@ impl Btf {
"{} {}: VAR {}: fixup offset {}",
kind, name, var_name, offset
);

if var.linkage == VarLinkage::Static {
debug!(
"{} {}: VAR {}: linkage fixup not required",
kind, name, var_name
);
continue;
}
} else {
return Err(BtfError::InvalidDatasec);
}
Expand Down
2 changes: 1 addition & 1 deletion aya-obj/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
//! #[cfg(not(feature = "std"))]
//! let text_sections = hashbrown::HashSet::new();
//! object.relocate_calls(&text_sections).unwrap();
//! object.relocate_maps(std::iter::empty(), &text_sections).unwrap();
//! object.relocate_map_references(std::iter::empty(), &text_sections).unwrap();
//!
//! // Run with rbpf
//! let function = object.functions.get(&object.programs["prog_name"].function_key()).unwrap();
Expand Down
68 changes: 67 additions & 1 deletion aya-obj/src/maps.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Map struct and type bindings.
use alloc::vec::Vec;
use alloc::{collections::BTreeMap, vec::Vec};
use core::mem;

use crate::{EbpfSectionKind, InvalidTypeBinding};
Expand Down Expand Up @@ -248,6 +248,64 @@ impl Map {
Map::Btf(m) => Some(m.symbol_index),
}
}

/// Sets the inner map definition, in case of a map of maps
pub fn set_legacy_inner(&mut self, inner_def: &Map) {
match self {
Map::Legacy(m) => {
if let Map::Legacy(inner_def) = inner_def {
m.inner_def = Some(inner_def.def);
} else {
panic!("inner map is not a legacy map");
}
}
Map::Btf(_) => panic!("inner map already set"),
}
}

/// Returns the inner map definition, in case of a map of maps
pub fn inner(&self) -> Option<Map> {
match self {
Map::Legacy(m) => m.inner_def.as_ref().map(|inner_def| {
Map::Legacy(LegacyMap {
def: *inner_def,
inner_def: None,
section_index: m.section_index,
section_kind: m.section_kind,
symbol_index: m.symbol_index,
data: Vec::new(),
initial_slots: BTreeMap::new(),
})
}),
Map::Btf(m) => m.inner_def.as_ref().map(|inner_def| {
Map::Btf(BtfMap {
def: *inner_def,
inner_def: None,
section_index: m.section_index,
symbol_index: m.symbol_index,
data: Vec::new(),
initial_slots: BTreeMap::new(),
})
}),
}
}

/// Places the file descriptor of an inner map into the initial slots of a
/// map-of-maps. The map is placed at the given index.
pub(crate) fn set_initial_map_fd(&mut self, index: usize, inner_map_fd: i32) -> bool {
match self {
Map::Legacy(m) => m.initial_slots.insert(index, inner_map_fd).is_none(),
Map::Btf(m) => m.initial_slots.insert(index, inner_map_fd).is_none(),
}
}

/// Returns the initial slots of a map-of-maps
pub fn initial_map_fds(&self) -> &BTreeMap<usize, i32> {
match self {
Map::Legacy(m) => &m.initial_slots,
Map::Btf(m) => &m.initial_slots,
}
}
}

/// A map declared with legacy BPF map declaration style, most likely from a `maps` section.
Expand All @@ -258,6 +316,8 @@ impl Map {
pub struct LegacyMap {
/// The definition of the map
pub def: bpf_map_def,
/// The defintion of the inner map, in case of a map of maps
pub inner_def: Option<bpf_map_def>,
/// The section index
pub section_index: usize,
/// The section kind
Expand All @@ -270,14 +330,20 @@ pub struct LegacyMap {
pub symbol_index: Option<usize>,
/// The map data
pub data: Vec<u8>,
/// The initial slots of a map-of-maps
pub initial_slots: BTreeMap<usize, i32>,
}

/// A BTF-defined map, most likely from a `.maps` section.
#[derive(Debug, Clone)]
pub struct BtfMap {
/// The definition of the map
pub def: BtfMapDef,
/// The defintion of the inner map, in case of a map of maps
pub inner_def: Option<BtfMapDef>,
pub(crate) section_index: usize,
pub(crate) symbol_index: usize,
pub(crate) data: Vec<u8>,
/// The initial slots of a map-of-maps
pub initial_slots: BTreeMap<usize, i32>,
}
Loading

0 comments on commit 078c638

Please sign in to comment.