From 563ce46118258805892bdff97f0e57b2838e0de8 Mon Sep 17 00:00:00 2001 From: Alessandro Decina Date: Fri, 5 Mar 2021 07:28:34 +0000 Subject: [PATCH] aya: maps: group syscall errors into MapError::SyscallError --- aya/src/bpf.rs | 9 ++++++-- aya/src/maps/hash_map.rs | 43 +++++++++++++++++++++++------------ aya/src/maps/mod.rs | 28 +++++++++-------------- aya/src/maps/program_array.rs | 33 +++++++++++++++++++++------ 4 files changed, 73 insertions(+), 40 deletions(-) diff --git a/aya/src/bpf.rs b/aya/src/bpf.rs index 4485d988..0687ee23 100644 --- a/aya/src/bpf.rs +++ b/aya/src/bpf.rs @@ -95,8 +95,13 @@ impl Bpf { let mut map = Map { obj, fd: None }; let fd = map.create()?; if !map.obj.data.is_empty() && map.obj.name != ".bss" { - bpf_map_update_elem_ptr(fd, &0 as *const _, map.obj.data.as_ptr(), 0) - .map_err(|(code, io_error)| MapError::UpdateElementError { code, io_error })?; + bpf_map_update_elem_ptr(fd, &0 as *const _, map.obj.data.as_ptr(), 0).map_err( + |(code, io_error)| MapError::SyscallError { + call: "bpf_map_update_elem".to_owned(), + code, + io_error, + }, + )?; } maps.push(map); } diff --git a/aya/src/maps/hash_map.rs b/aya/src/maps/hash_map.rs index c8fd9d7a..129863f6 100644 --- a/aya/src/maps/hash_map.rs +++ b/aya/src/maps/hash_map.rs @@ -56,8 +56,11 @@ impl, K: Pod, V: Pod> HashMap { pub unsafe fn get(&self, key: &K, flags: u64) -> Result, MapError> { let fd = self.inner.deref().fd_or_err()?; - bpf_map_lookup_elem(fd, key, flags) - .map_err(|(code, io_error)| MapError::LookupElementError { code, io_error }) + bpf_map_lookup_elem(fd, key, flags).map_err(|(code, io_error)| MapError::SyscallError { + call: "bpf_map_lookup_elem".to_owned(), + code, + io_error, + }) } pub unsafe fn iter<'coll>(&'coll self) -> MapIter<'coll, K, V> { @@ -72,22 +75,34 @@ impl, K: Pod, V: Pod> HashMap { impl, K: Pod, V: Pod> HashMap { pub fn insert(&mut self, key: K, value: V, flags: u64) -> Result<(), MapError> { let fd = self.inner.deref_mut().fd_or_err()?; - bpf_map_update_elem(fd, &key, &value, flags) - .map_err(|(code, io_error)| MapError::UpdateElementError { code, io_error })?; + bpf_map_update_elem(fd, &key, &value, flags).map_err(|(code, io_error)| { + MapError::SyscallError { + call: "bpf_map_update_elem".to_owned(), + code, + io_error, + } + })?; Ok(()) } pub unsafe fn pop(&mut self, key: &K) -> Result, MapError> { let fd = self.inner.deref_mut().fd_or_err()?; - bpf_map_lookup_and_delete_elem(fd, key) - .map_err(|(code, io_error)| MapError::LookupAndDeleteElementError { code, io_error }) + bpf_map_lookup_and_delete_elem(fd, key).map_err(|(code, io_error)| MapError::SyscallError { + call: "bpf_map_lookup_and_delete_elem".to_owned(), + code, + io_error, + }) } pub fn remove(&mut self, key: &K) -> Result<(), MapError> { let fd = self.inner.deref_mut().fd_or_err()?; bpf_map_delete_elem(fd, key) .map(|_| ()) - .map_err(|(code, io_error)| MapError::DeleteElementError { code, io_error }) + .map_err(|(code, io_error)| MapError::SyscallError { + call: "bpf_map_delete_elem".to_owned(), + code, + io_error, + }) } } @@ -268,7 +283,7 @@ mod tests { assert!(matches!( hm.insert(1, 42, 0), - Err(MapError::UpdateElementError { code: -1, io_error }) if io_error.raw_os_error() == Some(EFAULT) + Err(MapError::SyscallError { call, code: -1, io_error }) if call == "bpf_map_update_elem" && io_error.raw_os_error() == Some(EFAULT) )); } @@ -303,7 +318,7 @@ mod tests { assert!(matches!( hm.remove(&1), - Err(MapError::DeleteElementError { code: -1, io_error }) if io_error.raw_os_error() == Some(EFAULT) + Err(MapError::SyscallError { call, code: -1, io_error }) if call == "bpf_map_delete_elem" && io_error.raw_os_error() == Some(EFAULT) )); } @@ -337,7 +352,7 @@ mod tests { assert!(matches!( unsafe { hm.get(&1, 0) }, - Err(MapError::LookupElementError { code: -1, io_error }) if io_error.raw_os_error() == Some(EFAULT) + Err(MapError::SyscallError { call, code: -1, io_error }) if call == "bpf_map_lookup_elem" && io_error.raw_os_error() == Some(EFAULT) )); } @@ -370,7 +385,7 @@ mod tests { assert!(matches!( unsafe { hm.pop(&1) }, - Err(MapError::LookupAndDeleteElementError { code: -1, io_error }) if io_error.raw_os_error() == Some(EFAULT) + Err(MapError::SyscallError { call, code: -1, io_error }) if call == "bpf_map_lookup_and_delete_elem" && io_error.raw_os_error() == Some(EFAULT) )); } @@ -499,7 +514,7 @@ mod tests { assert!(matches!(keys.next(), Some(Ok(20)))); assert!(matches!( keys.next(), - Some(Err(MapError::GetNextKeyError { .. })) + Some(Err(MapError::SyscallError { call, .. })) if call == "bpf_map_get_next_key" )); assert!(matches!(keys.next(), None)); } @@ -593,7 +608,7 @@ mod tests { assert!(matches!(iter.next(), Some(Ok((20, 200))))); assert!(matches!( iter.next(), - Some(Err(MapError::GetNextKeyError { .. })) + Some(Err(MapError::SyscallError { call, .. })) if call == "bpf_map_get_next_key" )); assert!(matches!(iter.next(), None)); } @@ -631,7 +646,7 @@ mod tests { assert!(matches!(iter.next(), Some(Ok((10, 100))))); assert!(matches!( iter.next(), - Some(Err(MapError::LookupElementError { .. })) + Some(Err(MapError::SyscallError { call, .. })) if call == "bpf_map_lookup_elem" )); assert!(matches!(iter.next(), Some(Ok((30, 300))))); assert!(matches!(iter.next(), None)); diff --git a/aya/src/maps/mod.rs b/aya/src/maps/mod.rs index 324e4ef5..f33067a9 100644 --- a/aya/src/maps/mod.rs +++ b/aya/src/maps/mod.rs @@ -54,22 +54,12 @@ pub enum MapError { #[error("the program is not loaded")] ProgramNotLoaded, - #[error("the BPF_MAP_UPDATE_ELEM syscall failed with code {code} io_error {io_error}")] - UpdateElementError { code: i64, io_error: io::Error }, - - #[error("the BPF_MAP_LOOKUP_ELEM syscall failed with code {code} io_error {io_error}")] - LookupElementError { code: i64, io_error: io::Error }, - - #[error("the BPF_MAP_DELETE_ELEM syscall failed with code {code} io_error {io_error}")] - DeleteElementError { code: i64, io_error: io::Error }, - - #[error( - "the BPF_MAP_LOOKUP_AND_DELETE_ELEM syscall failed with code {code} io_error {io_error}" - )] - LookupAndDeleteElementError { code: i64, io_error: io::Error }, - - #[error("the BPF_MAP_GET_NEXT_KEY syscall failed with code {code} io_error {io_error}")] - GetNextKeyError { code: i64, io_error: io::Error }, + #[error("the `{call}` syscall failed with code {code} io_error {io_error}")] + SyscallError { + call: String, + code: i64, + io_error: io::Error, + }, #[error("map `{name}` is borrowed mutably")] BorrowError { name: String }, @@ -170,7 +160,11 @@ impl Iterator for MapKeys<'_, K, V> { } Err((code, io_error)) => { self.err = true; - return Some(Err(MapError::GetNextKeyError { code, io_error })); + return Some(Err(MapError::SyscallError { + call: "bpf_map_get_next_key".to_owned(), + code, + io_error, + })); } } } diff --git a/aya/src/maps/program_array.rs b/aya/src/maps/program_array.rs index 8177a472..81f160a8 100644 --- a/aya/src/maps/program_array.rs +++ b/aya/src/maps/program_array.rs @@ -45,8 +45,13 @@ impl> ProgramArray { pub unsafe fn get(&self, key: &u32, flags: u64) -> Result, MapError> { let fd = self.inner.fd_or_err()?; - let fd = bpf_map_lookup_elem(fd, key, flags) - .map_err(|(code, io_error)| MapError::LookupElementError { code, io_error })?; + let fd = bpf_map_lookup_elem(fd, key, flags).map_err(|(code, io_error)| { + MapError::SyscallError { + call: "bpf_map_lookup_elem".to_owned(), + code, + io_error, + } + })?; Ok(fd) } @@ -79,16 +84,26 @@ impl + DerefMut> ProgramArray { self.check_bounds(index)?; let prog_fd = program.fd().ok_or(MapError::ProgramNotLoaded)?; - bpf_map_update_elem(fd, &index, &prog_fd, flags) - .map_err(|(code, io_error)| MapError::UpdateElementError { code, io_error })?; + bpf_map_update_elem(fd, &index, &prog_fd, flags).map_err(|(code, io_error)| { + MapError::SyscallError { + call: "bpf_map_update_elem".to_owned(), + code, + io_error, + } + })?; Ok(()) } pub unsafe fn pop(&mut self, index: &u32) -> Result, MapError> { let fd = self.inner.fd_or_err()?; self.check_bounds(*index)?; - bpf_map_lookup_and_delete_elem(fd, index) - .map_err(|(code, io_error)| MapError::LookupAndDeleteElementError { code, io_error }) + bpf_map_lookup_and_delete_elem(fd, index).map_err(|(code, io_error)| { + MapError::SyscallError { + call: "bpf_map_lookup_and_delete_elem".to_owned(), + code, + io_error, + } + }) } pub fn remove(&mut self, index: &u32) -> Result<(), MapError> { @@ -96,7 +111,11 @@ impl + DerefMut> ProgramArray { self.check_bounds(*index)?; bpf_map_delete_elem(fd, index) .map(|_| ()) - .map_err(|(code, io_error)| MapError::DeleteElementError { code, io_error }) + .map_err(|(code, io_error)| MapError::SyscallError { + call: "bpf_map_delete_elem".to_owned(), + code, + io_error, + }) } }