Change the suffix of errors from *Failed to *Error

pull/1/head
Alessandro Decina 4 years ago
parent d4e282535b
commit 160e0be6d6

@ -56,7 +56,7 @@ impl Bpf {
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::UpdateElementFailed { code, io_error })?;
.map_err(|(code, io_error)| MapError::UpdateElementError { code, io_error })?;
}
maps.push(map);
}

@ -52,7 +52,7 @@ impl<T: Deref<Target = Map>, K: Pod, V: Pod> HashMap<T, K, V> {
pub unsafe fn get(&self, key: &K, flags: u64) -> Result<Option<V>, MapError> {
let fd = self.inner.deref().fd_or_err()?;
bpf_map_lookup_elem(fd, key, flags)
.map_err(|(code, io_error)| MapError::LookupElementFailed { code, io_error })
.map_err(|(code, io_error)| MapError::LookupElementError { code, io_error })
}
pub unsafe fn iter<'coll>(&'coll self) -> MapIter<'coll, K, V> {
@ -68,21 +68,21 @@ impl<T: DerefMut<Target = Map>, K: Pod, V: Pod> HashMap<T, K, V> {
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::UpdateElementFailed { code, io_error })?;
.map_err(|(code, io_error)| MapError::UpdateElementError { code, io_error })?;
Ok(())
}
pub unsafe fn pop(&mut self, key: &K) -> Result<Option<V>, MapError> {
let fd = self.inner.deref_mut().fd_or_err()?;
bpf_map_lookup_and_delete_elem(fd, key)
.map_err(|(code, io_error)| MapError::LookupAndDeleteElementFailed { code, io_error })
.map_err(|(code, io_error)| MapError::LookupAndDeleteElementError { 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::DeleteElementFailed { code, io_error })
.map_err(|(code, io_error)| MapError::DeleteElementError { code, io_error })
}
}
@ -254,7 +254,7 @@ mod tests {
assert!(matches!(
hm.insert(1, 42, 0),
Err(MapError::UpdateElementFailed { code: -1, io_error }) if io_error.raw_os_error() == Some(EFAULT)
Err(MapError::UpdateElementError { code: -1, io_error }) if io_error.raw_os_error() == Some(EFAULT)
));
}
@ -300,7 +300,7 @@ mod tests {
assert!(matches!(
hm.remove(&1),
Err(MapError::DeleteElementFailed { code: -1, io_error }) if io_error.raw_os_error() == Some(EFAULT)
Err(MapError::DeleteElementError { code: -1, io_error }) if io_error.raw_os_error() == Some(EFAULT)
));
}
@ -348,7 +348,7 @@ mod tests {
assert!(matches!(
unsafe { hm.get(&1, 0) },
Err(MapError::LookupElementFailed { code: -1, io_error }) if io_error.raw_os_error() == Some(EFAULT)
Err(MapError::LookupElementError { code: -1, io_error }) if io_error.raw_os_error() == Some(EFAULT)
));
}
@ -395,7 +395,7 @@ mod tests {
assert!(matches!(
unsafe { hm.pop(&1) },
Err(MapError::LookupAndDeleteElementFailed { code: -1, io_error }) if io_error.raw_os_error() == Some(EFAULT)
Err(MapError::LookupAndDeleteElementError { code: -1, io_error }) if io_error.raw_os_error() == Some(EFAULT)
));
}
@ -524,7 +524,7 @@ mod tests {
assert!(matches!(keys.next(), Some(Ok(20))));
assert!(matches!(
keys.next(),
Some(Err(MapError::GetNextKeyFailed { .. }))
Some(Err(MapError::GetNextKeyError { .. }))
));
assert!(matches!(keys.next(), None));
}
@ -618,7 +618,7 @@ mod tests {
assert!(matches!(iter.next(), Some(Ok((20, 200)))));
assert!(matches!(
iter.next(),
Some(Err(MapError::GetNextKeyFailed { .. }))
Some(Err(MapError::GetNextKeyError { .. }))
));
assert!(matches!(iter.next(), None));
}
@ -656,7 +656,7 @@ mod tests {
assert!(matches!(iter.next(), Some(Ok((10, 100)))));
assert!(matches!(
iter.next(),
Some(Err(MapError::LookupElementFailed { .. }))
Some(Err(MapError::LookupElementError { .. }))
));
assert!(matches!(iter.next(), Some(Ok((30, 300)))));
assert!(matches!(iter.next(), None));

@ -32,7 +32,7 @@ pub enum MapError {
AlreadyCreated { name: String },
#[error("failed to create map `{name}`: {code}")]
CreateFailed {
CreateError {
name: String,
code: i64,
io_error: io::Error,
@ -51,21 +51,21 @@ pub enum MapError {
ProgramNotLoaded,
#[error("the BPF_MAP_UPDATE_ELEM syscall failed with code {code} io_error {io_error}")]
UpdateElementFailed { code: i64, 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}")]
LookupElementFailed { code: i64, 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}")]
DeleteElementFailed { code: i64, 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}"
)]
LookupAndDeleteElementFailed { code: i64, 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}")]
GetNextKeyFailed { code: i64, io_error: io::Error },
GetNextKeyError { code: i64, io_error: io::Error },
#[error("map `{name}` is borrowed mutably")]
BorrowError { name: String },
@ -91,7 +91,7 @@ impl Map {
CString::new(name.clone()).map_err(|_| MapError::InvalidName { name: name.clone() })?;
let fd = bpf_create_map(&c_name, &self.obj.def).map_err(|(code, io_error)| {
MapError::CreateFailed {
MapError::CreateError {
name,
code,
io_error,
@ -158,7 +158,7 @@ impl<K: Pod, V: Pod> Iterator for MapKeys<'_, K, V> {
}
Err((code, io_error)) => {
self.err = true;
return Some(Err(MapError::GetNextKeyFailed { code, io_error }));
return Some(Err(MapError::GetNextKeyError { code, io_error }));
}
}
}
@ -255,8 +255,8 @@ mod tests {
let mut map = new_map("foo");
let ret = map.create();
assert!(matches!(ret, Err(MapError::CreateFailed { .. })));
if let Err(MapError::CreateFailed {
assert!(matches!(ret, Err(MapError::CreateError { .. })));
if let Err(MapError::CreateError {
name,
code,
io_error,

@ -32,19 +32,19 @@ pub enum PerfBufferError {
InvalidPageCount { page_count: usize },
#[error("perf_event_open failed: {io_error}")]
OpenFailed {
OpenError {
#[source]
io_error: io::Error,
},
#[error("mmap failed: {io_error}")]
MMapFailed {
MMapError {
#[source]
io_error: io::Error,
},
#[error("PERF_EVENT_IOC_ENABLE failed: {io_error}")]
PerfEventEnableFailed {
PerfEventEnableError {
#[source]
io_error: io::Error,
},
@ -80,7 +80,7 @@ impl PerfBuffer {
}
let fd = perf_event_open(cpu_id as i32)
.map_err(|(_, io_error)| PerfBufferError::OpenFailed { io_error })?
.map_err(|(_, io_error)| PerfBufferError::OpenError { io_error })?
as RawFd;
let size = page_size * page_count;
let buf = unsafe {
@ -94,7 +94,7 @@ impl PerfBuffer {
)
};
if buf == MAP_FAILED {
return Err(PerfBufferError::MMapFailed {
return Err(PerfBufferError::MMapError {
io_error: io::Error::last_os_error(),
});
}
@ -107,7 +107,7 @@ impl PerfBuffer {
};
perf_event_ioctl(fd, PERF_EVENT_IOC_ENABLE, 0)
.map_err(|(_, io_error)| PerfBufferError::PerfEventEnableFailed { io_error })?;
.map_err(|(_, io_error)| PerfBufferError::PerfEventEnableError { io_error })?;
Ok(perf_buf)
}
@ -267,7 +267,7 @@ pub enum PerfMapError {
PerfBufferError(#[from] PerfBufferError),
#[error("bpf_map_update_elem failed: {io_error}")]
UpdateElementFailed {
UpdateElementError {
#[source]
io_error: io::Error,
},
@ -315,7 +315,7 @@ impl<T: DerefMut<Target = Map>> PerfMap<T> {
let map_fd = self.map.fd_or_err()?;
let buf = PerfBuffer::open(index, self.page_size, page_count.unwrap_or(2))?;
bpf_map_update_elem(map_fd, &index, &buf.fd, 0)
.map_err(|(_, io_error)| PerfMapError::UpdateElementFailed { io_error })?;
.map_err(|(_, io_error)| PerfMapError::UpdateElementError { io_error })?;
Ok(PerfMapBuffer {
buf,

@ -46,7 +46,7 @@ impl<T: Deref<Target = Map>> ProgramArray<T> {
pub unsafe fn get(&self, key: &u32, flags: u64) -> Result<Option<RawFd>, MapError> {
let fd = self.inner.deref().fd_or_err()?;
let fd = bpf_map_lookup_elem(fd, key, flags)
.map_err(|(code, io_error)| MapError::LookupElementFailed { code, io_error })?;
.map_err(|(code, io_error)| MapError::LookupElementError { code, io_error })?;
Ok(fd)
}
@ -80,7 +80,7 @@ impl<T: Deref<Target = Map> + DerefMut<Target = Map>> ProgramArray<T> {
let prog_fd = program.fd().ok_or(MapError::ProgramNotLoaded)?;
bpf_map_update_elem(fd, &index, &prog_fd, flags)
.map_err(|(code, io_error)| MapError::UpdateElementFailed { code, io_error })?;
.map_err(|(code, io_error)| MapError::UpdateElementError { code, io_error })?;
Ok(())
}
@ -88,7 +88,7 @@ impl<T: Deref<Target = Map> + DerefMut<Target = Map>> ProgramArray<T> {
let fd = self.inner.deref().fd_or_err()?;
self.check_bounds(*index)?;
bpf_map_lookup_and_delete_elem(fd, index)
.map_err(|(code, io_error)| MapError::LookupAndDeleteElementFailed { code, io_error })
.map_err(|(code, io_error)| MapError::LookupAndDeleteElementError { code, io_error })
}
pub fn remove(&mut self, index: &u32) -> Result<(), MapError> {
@ -96,7 +96,7 @@ impl<T: Deref<Target = Map> + DerefMut<Target = Map>> ProgramArray<T> {
self.check_bounds(*index)?;
bpf_map_delete_elem(fd, index)
.map(|_| ())
.map_err(|(code, io_error)| MapError::DeleteElementFailed { code, io_error })
.map_err(|(code, io_error)| MapError::DeleteElementError { code, io_error })
}
}

@ -33,7 +33,7 @@ pub enum ProgramError {
NotLoaded { program: String },
#[error("the BPF_PROG_LOAD syscall for `{program}` failed: {io_error}\nVerifier output:\n{verifier_log}")]
LoadFailed {
LoadError {
program: String,
io_error: io::Error,
verifier_log: String,
@ -43,23 +43,23 @@ pub enum ProgramError {
AlreadyDetached,
#[error("the perf_event_open syscall failed: {io_error}")]
PerfEventOpenFailed { io_error: io::Error },
PerfEventOpenError { io_error: io::Error },
#[error("PERF_EVENT_IOC_SET_BPF/PERF_EVENT_IOC_ENABLE failed: {io_error}")]
PerfEventAttachFailed { io_error: io::Error },
PerfEventAttachError { io_error: io::Error },
#[error("the program {program} is not attached")]
NotAttached { program: String },
#[error("error attaching {program}: BPF_LINK_CREATE failed with {io_error}")]
BpfLinkCreateFailed {
BpfLinkCreateError {
program: String,
#[source]
io_error: io::Error,
},
#[error("error attaching XDP program using netlink: {io_error}")]
NetlinkXdpFailed {
NetlinkXdpError {
program: String,
#[source]
io_error: io::Error,
@ -244,7 +244,7 @@ fn load_program(prog_type: c_uint, data: &mut ProgramData) -> Result<(), Program
if let Err((_, io_error)) = ret {
log_buf.truncate();
return Err(ProgramError::LoadFailed {
return Err(ProgramError::LoadError {
program: name.clone(),
io_error,
verifier_log: log_buf.as_c_str().unwrap().to_string_lossy().to_string(),

@ -38,9 +38,9 @@ pub(crate) fn perf_attach(data: &mut ProgramData, fd: RawFd) -> Result<impl Link
let prog_fd = data.fd_or_err()?;
perf_event_ioctl(fd, PERF_EVENT_IOC_SET_BPF, prog_fd)
.map_err(|(_, io_error)| ProgramError::PerfEventAttachFailed { io_error })?;
.map_err(|(_, io_error)| ProgramError::PerfEventAttachError { io_error })?;
perf_event_ioctl(fd, PERF_EVENT_IOC_ENABLE, 0)
.map_err(|(_, io_error)| ProgramError::PerfEventAttachFailed { io_error })?;
.map_err(|(_, io_error)| ProgramError::PerfEventAttachError { io_error })?;
Ok(LinkRef::new(&link))
}

@ -142,7 +142,7 @@ fn attach(
};
let fd = perf_event_open_probe(perf_ty, ret_bit, name, offset, pid)
.map_err(|(_code, io_error)| ProgramError::PerfEventOpenFailed { io_error })?
.map_err(|(_code, io_error)| ProgramError::PerfEventOpenError { io_error })?
as i32;
perf_attach(program_data, fd)

@ -17,7 +17,7 @@ impl TracePoint {
pub fn attach(&mut self, category: &str, name: &str) -> Result<impl Link, ProgramError> {
let id = read_sys_fs_trace_point_id(category, name)?;
let fd = perf_event_open_trace_point(id)
.map_err(|(_code, io_error)| ProgramError::PerfEventOpenFailed { io_error })?
.map_err(|(_code, io_error)| ProgramError::PerfEventOpenError { io_error })?
as i32;
perf_attach(&mut self.data, fd)

@ -40,7 +40,7 @@ impl Xdp {
if k_ver >= (5, 7, 0) {
let link_fd =
bpf_link_create(prog_fd, if_index, BPF_XDP, 0).map_err(|(_, io_error)| {
ProgramError::BpfLinkCreateFailed {
ProgramError::BpfLinkCreateError {
program: self.name(),
io_error,
}
@ -51,7 +51,7 @@ impl Xdp {
Ok(LinkRef::new(&link))
} else {
unsafe { netlink_set_xdp_fd(if_index, prog_fd, None, 0) }.map_err(|io_error| {
ProgramError::NetlinkXdpFailed {
ProgramError::NetlinkXdpError {
program: self.name(),
io_error,
}

Loading…
Cancel
Save