Skip to content

Commit

Permalink
sys: extract common SyscallError
Browse files Browse the repository at this point in the history
We currently have 4 copies of this.
  • Loading branch information
tamird committed Aug 1, 2023
1 parent 4cb3ea6 commit de8519a
Show file tree
Hide file tree
Showing 41 changed files with 300 additions and 329 deletions.
16 changes: 10 additions & 6 deletions aya/src/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use crate::{
is_btf_float_supported, is_btf_func_global_supported, is_btf_func_supported,
is_btf_supported, is_btf_type_tag_supported, is_perf_link_supported,
is_probe_read_kernel_supported, is_prog_name_supported, retry_with_verifier_logs,
SyscallError,
},
util::{bytes_of, bytes_of_slice, possible_cpus, POSSIBLE_CPUS},
};
Expand Down Expand Up @@ -505,16 +506,19 @@ impl<'a> BpfLoader<'a> {
};
if !map.obj.data().is_empty() && map.obj.section_kind() != BpfSectionKind::Bss {
bpf_map_update_elem_ptr(fd, &0 as *const _, map.obj.data_mut().as_mut_ptr(), 0)
.map_err(|(_, io_error)| MapError::SyscallError {
.map_err(|(_, io_error)| SyscallError {
call: "bpf_map_update_elem",
io_error,
})?;
})
.map_err(MapError::from)?;
}
if map.obj.section_kind() == BpfSectionKind::Rodata {
bpf_map_freeze(fd).map_err(|(_, io_error)| MapError::SyscallError {
call: "bpf_map_freeze",
io_error,
})?;
bpf_map_freeze(fd)
.map_err(|(_, io_error)| SyscallError {
call: "bpf_map_freeze",
io_error,
})
.map_err(MapError::from)?;
}
maps.insert(name, map);
}
Expand Down
11 changes: 5 additions & 6 deletions aya/src/maps/array/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use crate::{
maps::{check_bounds, check_kv_size, IterableMap, MapData, MapError},
sys::{bpf_map_lookup_elem, bpf_map_update_elem},
sys::{bpf_map_lookup_elem, bpf_map_update_elem, SyscallError},
Pod,
};

Expand Down Expand Up @@ -65,12 +65,11 @@ impl<T: Borrow<MapData>, V: Pod> Array<T, V> {
check_bounds(data, *index)?;
let fd = data.fd_or_err()?;

let value = bpf_map_lookup_elem(fd, index, flags).map_err(|(_, io_error)| {
MapError::SyscallError {
let value =
bpf_map_lookup_elem(fd, index, flags).map_err(|(_, io_error)| SyscallError {
call: "bpf_map_lookup_elem",
io_error,
}
})?;
})?;
value.ok_or(MapError::KeyNotFound)
}

Expand All @@ -93,7 +92,7 @@ impl<T: BorrowMut<MapData>, V: Pod> Array<T, V> {
check_bounds(data, index)?;
let fd = data.fd_or_err()?;
bpf_map_update_elem(fd, Some(&index), value.borrow(), flags).map_err(|(_, io_error)| {
MapError::SyscallError {
SyscallError {
call: "bpf_map_update_elem",
io_error,
}
Expand Down
6 changes: 3 additions & 3 deletions aya/src/maps/array/per_cpu_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use crate::{
maps::{check_bounds, check_kv_size, IterableMap, MapData, MapError, PerCpuValues},
sys::{bpf_map_lookup_elem_per_cpu, bpf_map_update_elem_per_cpu},
sys::{bpf_map_lookup_elem_per_cpu, bpf_map_update_elem_per_cpu, SyscallError},
Pod,
};

Expand Down Expand Up @@ -85,7 +85,7 @@ impl<T: Borrow<MapData>, V: Pod> PerCpuArray<T, V> {
let fd = data.fd_or_err()?;

let value = bpf_map_lookup_elem_per_cpu(fd, index, flags).map_err(|(_, io_error)| {
MapError::SyscallError {
SyscallError {
call: "bpf_map_lookup_elem",
io_error,
}
Expand Down Expand Up @@ -113,7 +113,7 @@ impl<T: BorrowMut<MapData>, V: Pod> PerCpuArray<T, V> {
let fd = data.fd_or_err()?;

bpf_map_update_elem_per_cpu(fd, &index, &values, flags).map_err(|(_, io_error)| {
MapError::SyscallError {
SyscallError {
call: "bpf_map_update_elem",
io_error,
}
Expand Down
13 changes: 8 additions & 5 deletions aya/src/maps/array/program_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
use crate::{
maps::{check_bounds, check_kv_size, MapData, MapError, MapKeys},
programs::ProgramFd,
sys::{bpf_map_delete_elem, bpf_map_update_elem},
sys::{bpf_map_delete_elem, bpf_map_update_elem, SyscallError},
};

/// An array of eBPF program file descriptors used as a jump table.
Expand Down Expand Up @@ -80,7 +80,7 @@ impl<T: BorrowMut<MapData>> ProgramArray<T> {
let prog_fd = program.as_raw_fd();

bpf_map_update_elem(fd, Some(&index), &prog_fd, flags).map_err(|(_, io_error)| {
MapError::SyscallError {
SyscallError {
call: "bpf_map_update_elem",
io_error,
}
Expand All @@ -99,9 +99,12 @@ impl<T: BorrowMut<MapData>> ProgramArray<T> {

bpf_map_delete_elem(fd, index)
.map(|_| ())
.map_err(|(_, io_error)| MapError::SyscallError {
call: "bpf_map_delete_elem",
io_error,
.map_err(|(_, io_error)| {
SyscallError {
call: "bpf_map_delete_elem",
io_error,
}
.into()
})
}
}
16 changes: 7 additions & 9 deletions aya/src/maps/bloom_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{borrow::Borrow, marker::PhantomData};

use crate::{
maps::{check_v_size, MapData, MapError},
sys::{bpf_map_lookup_elem_ptr, bpf_map_push_elem},
sys::{bpf_map_lookup_elem_ptr, bpf_map_push_elem, SyscallError},
Pod,
};

Expand Down Expand Up @@ -54,7 +54,7 @@ impl<T: Borrow<MapData>, V: Pod> BloomFilter<T, V> {
let fd = self.inner.borrow().fd_or_err()?;

bpf_map_lookup_elem_ptr::<u32, _>(fd, None, &mut value, flags)
.map_err(|(_, io_error)| MapError::SyscallError {
.map_err(|(_, io_error)| SyscallError {
call: "bpf_map_lookup_elem",
io_error,
})?
Expand All @@ -65,11 +65,9 @@ impl<T: Borrow<MapData>, V: Pod> BloomFilter<T, V> {
/// Inserts a value into the map.
pub fn insert(&self, value: impl Borrow<V>, flags: u64) -> Result<(), MapError> {
let fd = self.inner.borrow().fd_or_err()?;
bpf_map_push_elem(fd, value.borrow(), flags).map_err(|(_, io_error)| {
MapError::SyscallError {
call: "bpf_map_push_elem",
io_error,
}
bpf_map_push_elem(fd, value.borrow(), flags).map_err(|(_, io_error)| SyscallError {
call: "bpf_map_push_elem",
io_error,
})?;
Ok(())
}
Expand Down Expand Up @@ -212,7 +210,7 @@ mod tests {

assert_matches!(
bloom_filter.insert(1, 0),
Err(MapError::SyscallError { call, io_error }) if call == "bpf_map_push_elem" && io_error.raw_os_error() == Some(EFAULT)
Err(MapError::SyscallError(SyscallError { call: "bpf_map_push_elem", io_error })) if io_error.raw_os_error() == Some(EFAULT)
);
}

Expand Down Expand Up @@ -250,7 +248,7 @@ mod tests {

assert_matches!(
bloom_filter.contains(&1, 0),
Err(MapError::SyscallError { call, io_error }) if call == "bpf_map_lookup_elem" && io_error.raw_os_error() == Some(EFAULT)
Err(MapError::SyscallError(SyscallError { call: "bpf_map_lookup_elem", io_error })) if io_error.raw_os_error() == Some(EFAULT)
);
}

Expand Down
31 changes: 19 additions & 12 deletions aya/src/maps/hash_map/hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use crate::{
maps::{check_kv_size, hash_map, IterableMap, MapData, MapError, MapIter, MapKeys},
sys::bpf_map_lookup_elem,
sys::{bpf_map_lookup_elem, SyscallError},
Pod,
};

Expand Down Expand Up @@ -54,11 +54,9 @@ impl<T: Borrow<MapData>, K: Pod, V: Pod> HashMap<T, K, V> {
/// Returns a copy of the value associated with the key.
pub fn get(&self, key: &K, flags: u64) -> Result<V, MapError> {
let fd = self.inner.borrow().fd_or_err()?;
let value = bpf_map_lookup_elem(fd, key, flags).map_err(|(_, io_error)| {
MapError::SyscallError {
call: "bpf_map_lookup_elem",
io_error,
}
let value = bpf_map_lookup_elem(fd, key, flags).map_err(|(_, io_error)| SyscallError {
call: "bpf_map_lookup_elem",
io_error,
})?;
value.ok_or(MapError::KeyNotFound)
}
Expand Down Expand Up @@ -292,7 +290,7 @@ mod tests {

assert_matches!(
hm.insert(1, 42, 0),
Err(MapError::SyscallError { call, io_error }) if call == "bpf_map_update_elem" && io_error.raw_os_error() == Some(EFAULT)
Err(MapError::SyscallError(SyscallError { call: "bpf_map_update_elem", io_error })) if io_error.raw_os_error() == Some(EFAULT)
);
}

Expand Down Expand Up @@ -352,7 +350,7 @@ mod tests {

assert_matches!(
hm.remove(&1),
Err(MapError::SyscallError { call, io_error }) if call == "bpf_map_delete_elem" && io_error.raw_os_error() == Some(EFAULT)
Err(MapError::SyscallError(SyscallError { call: "bpf_map_delete_elem", io_error })) if io_error.raw_os_error() == Some(EFAULT)
);
}

Expand Down Expand Up @@ -390,7 +388,7 @@ mod tests {

assert_matches!(
hm.get(&1, 0),
Err(MapError::SyscallError { call, io_error }) if call == "bpf_map_lookup_elem" && io_error.raw_os_error() == Some(EFAULT)
Err(MapError::SyscallError(SyscallError { call: "bpf_map_lookup_elem", io_error })) if io_error.raw_os_error() == Some(EFAULT)
);
}

Expand Down Expand Up @@ -535,7 +533,10 @@ mod tests {
assert_matches!(keys.next(), Some(Ok(20)));
assert_matches!(
keys.next(),
Some(Err(MapError::SyscallError { call, .. })) if call == "bpf_map_get_next_key"
Some(Err(MapError::SyscallError(SyscallError {
call: "bpf_map_get_next_key",
io_error: _
})))
);
assert_matches!(keys.next(), None);
}
Expand Down Expand Up @@ -647,7 +648,10 @@ mod tests {
assert_matches!(iter.next(), Some(Ok((20, 200))));
assert_matches!(
iter.next(),
Some(Err(MapError::SyscallError { call, .. })) if call == "bpf_map_get_next_key"
Some(Err(MapError::SyscallError(SyscallError {
call: "bpf_map_get_next_key",
io_error: _
})))
);
assert_matches!(iter.next(), None);
}
Expand Down Expand Up @@ -691,7 +695,10 @@ mod tests {
assert_matches!(iter.next(), Some(Ok((10, 100))));
assert_matches!(
iter.next(),
Some(Err(MapError::SyscallError { call, .. })) if call == "bpf_map_lookup_elem"
Some(Err(MapError::SyscallError(SyscallError {
call: "bpf_map_lookup_elem",
io_error: _
})))
);
assert_matches!(iter.next(), Some(Ok((30, 300))));
assert_matches!(iter.next(), None);
Expand Down
19 changes: 10 additions & 9 deletions aya/src/maps/hash_map/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Hash map types.
use crate::{
maps::MapError,
sys::{bpf_map_delete_elem, bpf_map_update_elem},
sys::{bpf_map_delete_elem, bpf_map_update_elem, SyscallError},
Pod,
};

Expand All @@ -21,11 +21,9 @@ pub(crate) fn insert<K: Pod, V: Pod>(
flags: u64,
) -> Result<(), MapError> {
let fd = map.fd_or_err()?;
bpf_map_update_elem(fd, Some(key), value, flags).map_err(|(_, io_error)| {
MapError::SyscallError {
call: "bpf_map_update_elem",
io_error,
}
bpf_map_update_elem(fd, Some(key), value, flags).map_err(|(_, io_error)| SyscallError {
call: "bpf_map_update_elem",
io_error,
})?;

Ok(())
Expand All @@ -35,8 +33,11 @@ pub(crate) fn remove<K: Pod>(map: &MapData, key: &K) -> Result<(), MapError> {
let fd = map.fd_or_err()?;
bpf_map_delete_elem(fd, key)
.map(|_| ())
.map_err(|(_, io_error)| MapError::SyscallError {
call: "bpf_map_delete_elem",
io_error,
.map_err(|(_, io_error)| {
SyscallError {
call: "bpf_map_delete_elem",
io_error,
}
.into()
})
}
11 changes: 5 additions & 6 deletions aya/src/maps/hash_map/per_cpu_hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
maps::{
check_kv_size, hash_map, IterableMap, MapData, MapError, MapIter, MapKeys, PerCpuValues,
},
sys::{bpf_map_lookup_elem_per_cpu, bpf_map_update_elem_per_cpu},
sys::{bpf_map_lookup_elem_per_cpu, bpf_map_update_elem_per_cpu, SyscallError},
Pod,
};

Expand Down Expand Up @@ -64,12 +64,11 @@ impl<T: Borrow<MapData>, K: Pod, V: Pod> PerCpuHashMap<T, K, V> {
/// Returns a slice of values - one for each CPU - associated with the key.
pub fn get(&self, key: &K, flags: u64) -> Result<PerCpuValues<V>, MapError> {
let fd = self.inner.borrow().fd_or_err()?;
let values = bpf_map_lookup_elem_per_cpu(fd, key, flags).map_err(|(_, io_error)| {
MapError::SyscallError {
let values =
bpf_map_lookup_elem_per_cpu(fd, key, flags).map_err(|(_, io_error)| SyscallError {
call: "bpf_map_lookup_elem",
io_error,
}
})?;
})?;
values.ok_or(MapError::KeyNotFound)
}

Expand Down Expand Up @@ -123,7 +122,7 @@ impl<T: BorrowMut<MapData>, K: Pod, V: Pod> PerCpuHashMap<T, K, V> {
) -> Result<(), MapError> {
let fd = self.inner.borrow_mut().fd_or_err()?;
bpf_map_update_elem_per_cpu(fd, key.borrow(), &values, flags).map_err(
|(_, io_error)| MapError::SyscallError {
|(_, io_error)| SyscallError {
call: "bpf_map_update_elem",
io_error,
},
Expand Down
Loading

0 comments on commit de8519a

Please sign in to comment.