Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor map API #397

Merged
merged 9 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aya/src/maps/array/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
/// # let mut bpf = aya::Bpf::load(&[])?;
/// use aya::maps::Array;
///
/// let mut array: Array<_, u32> = bpf.map_mut("ARRAY")?.try_into()?;
/// let mut array = Array::try_from(bpf.map_mut("ARRAY")?)?;
/// array.set(1, 42, 0)?;
/// assert_eq!(array.get(&1, 0)?, 42);
/// # Ok::<(), aya::BpfError>(())
Expand Down
2 changes: 1 addition & 1 deletion aya/src/maps/array/per_cpu_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use crate::{
/// use aya::maps::{PerCpuArray, PerCpuValues};
/// use aya::util::nr_cpus;
///
/// let mut array: PerCpuArray<_,u32> = bpf.map_mut("ARRAY")?.try_into()?;
/// let mut array = PerCpuArray::try_from(bpf.map_mut("ARRAY")?)?;
///
/// // set array[1] = 42 for all cpus
/// let nr_cpus = nr_cpus()?;
Expand Down
2 changes: 1 addition & 1 deletion aya/src/maps/array/program_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
/// use aya::maps::ProgramArray;
/// use aya::programs::CgroupSkb;
///
/// let mut prog_array: ProgramArray<_> = bpf.take_map("JUMP_TABLE")?.try_into()?;
/// let mut prog_array = ProgramArray::try_from(bpf.take_map("JUMP_TABLE")?)?;
/// let prog_0: &CgroupSkb = bpf.program("example_prog_0").unwrap().try_into()?;
/// let prog_0_fd = prog_0.fd().unwrap();
/// let prog_1: &CgroupSkb = bpf.program("example_prog_1").unwrap().try_into()?;
Expand Down
2 changes: 1 addition & 1 deletion aya/src/maps/bloom_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
/// # let mut bpf = aya::Bpf::load(&[])?;
/// use aya::maps::bloom_filter::BloomFilter;
///
/// let mut bloom_filter: BloomFilter<_,u32> = bpf.map_mut("BLOOM_FILTER")?.try_into()?;
/// let mut bloom_filter = BloomFilter::try_from(bpf.map_mut("BLOOM_FILTER")?)?;
///
/// bloom_filter.insert(1, 0)?;
///
Expand Down
2 changes: 1 addition & 1 deletion aya/src/maps/hash_map/hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
/// # let mut bpf = aya::Bpf::load(&[])?;
/// use aya::maps::HashMap;
///
/// let mut redirect_ports: HashMap<_, u32, u32> = bpf.map_mut("REDIRECT_PORTS")?.try_into()?;
/// let mut redirect_ports = HashMap::try_from(bpf.map_mut("REDIRECT_PORTS")?)?;
///
/// // redirect port 80 to 8080
/// redirect_ports.insert(80, 8080, 0);
Expand Down
6 changes: 3 additions & 3 deletions aya/src/maps/hash_map/per_cpu_hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ use crate::{
/// # Examples
///
/// ```no_run
/// # let bpf = aya::Bpf::load(&[])?;
/// # let mut bpf = aya::Bpf::load(&[])?;
/// use aya::maps::PerCpuHashMap;
///
/// const CPU_IDS: u8 = 1;
/// const WAKEUPS: u8 = 2;
///
/// let mut hm = PerCpuHashMap::<_, u8, u32>::try_from(bpf.map("COUNTERS")?)?;
/// let mut hm = PerCpuHashMap::<_, u8, u32>::try_from(bpf.map_mut("PER_CPU_STORAGE")?)?;
/// let cpu_ids = unsafe { hm.get(&CPU_IDS, 0)? };
/// let wakeups = unsafe { hm.get(&WAKEUPS, 0)? };
/// for (cpu_id, wakeups) in cpu_ids.iter().zip(wakeups.iter()) {
Expand Down Expand Up @@ -107,7 +107,7 @@ impl<T: AsMut<MapData>, K: Pod, V: Pod> PerCpuHashMap<T, K, V> {
///
/// const RETRIES: u8 = 1;
///
/// let mut hm: PerCpuHashMap::<_, u8, u32> = bpf.map_mut("PER_CPU_STORAGE")?.try_into()?;
/// let mut hm = PerCpuHashMap::<_, u8, u32>::try_from(bpf.map_mut("PER_CPU_STORAGE")?)?;
/// hm.insert(
/// RETRIES,
/// PerCpuValues::try_from(vec![3u32; nr_cpus()?])?,
Expand Down
2 changes: 1 addition & 1 deletion aya/src/maps/lpm_trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
/// use aya::maps::lpm_trie::{LpmTrie, Key};
/// use std::net::Ipv4Addr;
///
/// let mut trie: LpmTrie<_,u32,u32> = bpf.map_mut("LPM_TRIE")?.try_into()?;
/// let mut trie = LpmTrie::try_from(bpf.map_mut("LPM_TRIE")?)?;
/// let ipaddr = Ipv4Addr::new(8, 8, 8, 8);
/// // The following represents a key for the "8.8.8.8/16" subnet.
/// // The first argument - the prefix length - represents how many bytes should be matched against. The second argument is the actual data to be matched.
Expand Down
18 changes: 9 additions & 9 deletions aya/src/maps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//! use aya::maps::SockMap;
//! use aya::programs::SkMsg;
//!
//! let mut intercept_egress: SockMap<_> = bpf.map_mut("INTERCEPT_EGRESS")?.try_into()?;
//! let intercept_egress = SockMap::try_from(bpf.map_mut("INTERCEPT_EGRESS")?)?;
//! let map_fd = intercept_egress.fd()?;
//! let prog: &mut SkMsg = bpf.program_mut("intercept_egress_packet").unwrap().try_into()?;
//! prog.load()?;
Expand Down Expand Up @@ -281,7 +281,7 @@ macro_rules! impl_try_from_map {
fn try_from(map: &'a Map) -> Result<$tx<&'a MapData>, MapError> {
match map {
Map::$ty(m) => {
$tx::<&'a MapData>::new(m)
$tx::new(m)
},
_ => Err(MapError::UnexpectedMapType),
}
Expand All @@ -294,7 +294,7 @@ macro_rules! impl_try_from_map {
fn try_from(map: &'a mut Map) -> Result<$tx<&'a mut MapData>, MapError> {
match map {
Map::$ty(m) => {
$tx::<&'a mut MapData>::new(m)
$tx::new(m)
},
_ => Err(MapError::UnexpectedMapType),
}
Expand All @@ -307,7 +307,7 @@ macro_rules! impl_try_from_map {
fn try_from(map: Map) -> Result<$tx<MapData>, MapError> {
match map {
Map::$ty(m) => {
$tx::<MapData>::new(m)
$tx::new(m)
},
_ => Err(MapError::UnexpectedMapType),
}
Expand Down Expand Up @@ -339,7 +339,7 @@ macro_rules! impl_try_from_map_generic_key_or_value {
fn try_from(map: &'a Map) -> Result<$ty<&'a MapData , V>, MapError> {
match map {
Map::$ty(m) => {
$ty::<&'a MapData,V>::new(m)
$ty::new(m)
},
_ => Err(MapError::UnexpectedMapType),
}
Expand All @@ -352,7 +352,7 @@ macro_rules! impl_try_from_map_generic_key_or_value {
fn try_from(map: &'a mut Map) -> Result<$ty<&'a mut MapData, V>, MapError> {
match map {
Map::$ty(m) => {
$ty::<&'a mut MapData,V>::new(m)
$ty::new(m)
},
_ => Err(MapError::UnexpectedMapType),
}
Expand All @@ -365,7 +365,7 @@ macro_rules! impl_try_from_map_generic_key_or_value {
fn try_from(map: Map) -> Result<$ty<MapData, V>, MapError> {
match map {
Map::$ty(m) => {
$ty::<MapData,V>::new(m)
$ty::new(m)
},
_ => Err(MapError::UnexpectedMapType),
}
Expand All @@ -386,7 +386,7 @@ macro_rules! impl_try_from_map_generic_key_and_value {
fn try_from(map: &'a Map) -> Result<$ty<&'a MapData,V,K>, MapError> {
match map {
Map::$ty(m) => {
$ty::<&'a MapData,V,K>::new(m)
$ty::new(m)
},
_ => Err(MapError::UnexpectedMapType),
}
Expand All @@ -399,7 +399,7 @@ macro_rules! impl_try_from_map_generic_key_and_value {
fn try_from(map: &'a mut Map) -> Result<$ty<&'a mut MapData, V, K>, MapError> {
match map {
Map::$ty(m) => {
$ty::<&'a mut MapData,V,K>::new(m)
$ty::new(m)
},
_ => Err(MapError::UnexpectedMapType),
}
Expand Down
2 changes: 1 addition & 1 deletion aya/src/maps/perf/async_perf_event_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use crate::maps::{
/// use tokio::task; // or async_std::task
///
/// // try to convert the PERF_ARRAY map to an AsyncPerfEventArray
/// let mut perf_array: AsyncPerfEventArray<_> =bpf.take_map("PERF_ARRAY")?.try_into()?;
/// let mut perf_array = AsyncPerfEventArray::try_from(bpf.take_map("PERF_ARRAY")?)?;
///
/// for cpu_id in online_cpus()? {
/// // open a separate perf buffer for each cpu
Expand Down
2 changes: 1 addition & 1 deletion aya/src/maps/perf/perf_event_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl<T: AsMut<MapData> + AsRef<MapData>> AsRawFd for PerfEventArrayBuffer<T> {
/// use aya::util::online_cpus;
/// use bytes::BytesMut;
///
/// let mut perf_array: PerfEventArray<_> = bpf.map_mut("EVENTS")?.try_into()?;
/// let mut perf_array = PerfEventArray::try_from(bpf.map_mut("EVENTS")?)?;
///
/// // eBPF programs are going to write to the EVENTS perf array, using the id of the CPU they're
/// // running on as the array index.
Expand Down
2 changes: 1 addition & 1 deletion aya/src/maps/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
/// # let mut bpf = aya::Bpf::load(&[])?;
/// use aya::maps::Queue;
///
/// let mut queue: Queue<_, u32> = bpf.map_mut("ARRAY")?.try_into()?;
/// let mut queue = Queue::try_from(bpf.map_mut("ARRAY")?)?;
/// queue.push(42, 0)?;
/// queue.push(43, 0)?;
/// assert_eq!(queue.pop(0)?, 42);
Expand Down
10 changes: 6 additions & 4 deletions aya/src/maps/sock/sock_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ use crate::{
/// use aya::maps::SockHash;
/// use aya::programs::SkMsg;
///
/// let intercept_egress: SockHash<_, u32> = bpf.map("INTERCEPT_EGRESS")?.try_into()?;
/// let mut intercept_egress = SockHash::<_, u32>::try_from(bpf.map("INTERCEPT_EGRESS")?)?;
astoycos marked this conversation as resolved.
Show resolved Hide resolved
/// let map_fd = intercept_egress.fd()?;
///
/// let prog: &mut SkMsg = bpf.program_mut("intercept_egress_packet").unwrap().try_into()?;
/// prog.load()?;
/// prog.attach(map_fd)?;
///
/// let mut client = TcpStream::connect("127.0.0.1:1234")?;
/// let mut intercept_egress: SockHash<_, u32> = bpf.map_mut("INTERCEPT_EGRESS")?.try_into()?;
/// let mut intercept_egress = SockHash::try_from(bpf.map_mut("INTERCEPT_EGRESS")?)?;
///
/// intercept_egress.insert(1234, client.as_raw_fd(), 0)?;
///
Expand Down Expand Up @@ -104,8 +104,10 @@ impl<T: AsRef<MapData>, K: Pod> SockHash<T, K> {
MapKeys::new(self.inner.as_ref())
}

/// Returns the map's file descriptor, used for instances where programs
/// are attached to maps.
/// Returns the map's file descriptor.
///
/// The returned file descriptor can be used to attach programs that work with
/// socket maps, like [`SkMsg`] and [`SkSkb`].
pub fn fd(&self) -> Result<SockMapFd, MapError> {
Ok(SockMapFd(self.inner.as_ref().fd_or_err()?))
}
Expand Down
8 changes: 5 additions & 3 deletions aya/src/maps/sock/sock_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::{
/// use aya::maps::SockMap;
/// use aya::programs::SkSkb;
///
/// let intercept_ingress: SockMap<_> = bpf.map("INTERCEPT_INGRESS")?.try_into()?;
/// let intercept_ingress = SockMap::try_from(bpf.map("INTERCEPT_INGRESS")?)?;
/// let map_fd = intercept_ingress.fd()?;
///
/// let prog: &mut SkSkb = bpf.program_mut("intercept_ingress_packet").unwrap().try_into()?;
Expand Down Expand Up @@ -60,8 +60,10 @@ impl<T: AsRef<MapData>> SockMap<T> {
MapKeys::new(self.inner.as_ref())
}

/// Returns the map's file descriptor, used for instances where programs
/// are attached to maps.
/// Returns the map's file descriptor.
///
/// The returned file descriptor can be used to attach programs that work with
/// socket maps, like [`SkMsg`] and [`SkSkb`].
pub fn fd(&self) -> Result<SockMapFd, MapError> {
Ok(SockMapFd(self.inner.as_ref().fd_or_err()?))
}
Expand Down
2 changes: 1 addition & 1 deletion aya/src/maps/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
/// # let mut bpf = aya::Bpf::load(&[])?;
/// use aya::maps::Stack;
///
/// let mut stack: Stack<_, u32> = bpf.map_mut("STACK")?.try_into()?;
/// let mut stack = Stack::try_from(bpf.map_mut("STACK")?)?;
/// stack.push(42, 0)?;
/// stack.push(43, 0)?;
/// assert_eq!(stack.pop(0)?, 43);
Expand Down