|
|
@ -56,8 +56,11 @@ 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> {
|
|
|
|
pub unsafe fn get(&self, key: &K, flags: u64) -> Result<Option<V>, MapError> {
|
|
|
|
let fd = self.inner.deref().fd_or_err()?;
|
|
|
|
let fd = self.inner.deref().fd_or_err()?;
|
|
|
|
bpf_map_lookup_elem(fd, key, flags)
|
|
|
|
bpf_map_lookup_elem(fd, key, flags).map_err(|(code, io_error)| MapError::SyscallError {
|
|
|
|
.map_err(|(code, io_error)| MapError::LookupElementError { code, io_error })
|
|
|
|
call: "bpf_map_lookup_elem".to_owned(),
|
|
|
|
|
|
|
|
code,
|
|
|
|
|
|
|
|
io_error,
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub unsafe fn iter<'coll>(&'coll self) -> MapIter<'coll, K, V> {
|
|
|
|
pub unsafe fn iter<'coll>(&'coll self) -> MapIter<'coll, K, V> {
|
|
|
@ -72,22 +75,34 @@ impl<T: Deref<Target = Map>, K: Pod, V: Pod> HashMap<T, K, V> {
|
|
|
|
impl<T: DerefMut<Target = Map>, K: Pod, V: Pod> HashMap<T, K, V> {
|
|
|
|
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> {
|
|
|
|
pub fn insert(&mut self, key: K, value: V, flags: u64) -> Result<(), MapError> {
|
|
|
|
let fd = self.inner.deref_mut().fd_or_err()?;
|
|
|
|
let fd = self.inner.deref_mut().fd_or_err()?;
|
|
|
|
bpf_map_update_elem(fd, &key, &value, flags)
|
|
|
|
bpf_map_update_elem(fd, &key, &value, flags).map_err(|(code, io_error)| {
|
|
|
|
.map_err(|(code, io_error)| MapError::UpdateElementError { code, io_error })?;
|
|
|
|
MapError::SyscallError {
|
|
|
|
|
|
|
|
call: "bpf_map_update_elem".to_owned(),
|
|
|
|
|
|
|
|
code,
|
|
|
|
|
|
|
|
io_error,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})?;
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub unsafe fn pop(&mut self, key: &K) -> Result<Option<V>, MapError> {
|
|
|
|
pub unsafe fn pop(&mut self, key: &K) -> Result<Option<V>, MapError> {
|
|
|
|
let fd = self.inner.deref_mut().fd_or_err()?;
|
|
|
|
let fd = self.inner.deref_mut().fd_or_err()?;
|
|
|
|
bpf_map_lookup_and_delete_elem(fd, key)
|
|
|
|
bpf_map_lookup_and_delete_elem(fd, key).map_err(|(code, io_error)| MapError::SyscallError {
|
|
|
|
.map_err(|(code, io_error)| MapError::LookupAndDeleteElementError { code, io_error })
|
|
|
|
call: "bpf_map_lookup_and_delete_elem".to_owned(),
|
|
|
|
|
|
|
|
code,
|
|
|
|
|
|
|
|
io_error,
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn remove(&mut self, key: &K) -> Result<(), MapError> {
|
|
|
|
pub fn remove(&mut self, key: &K) -> Result<(), MapError> {
|
|
|
|
let fd = self.inner.deref_mut().fd_or_err()?;
|
|
|
|
let fd = self.inner.deref_mut().fd_or_err()?;
|
|
|
|
bpf_map_delete_elem(fd, key)
|
|
|
|
bpf_map_delete_elem(fd, key)
|
|
|
|
.map(|_| ())
|
|
|
|
.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!(
|
|
|
|
assert!(matches!(
|
|
|
|
hm.insert(1, 42, 0),
|
|
|
|
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!(
|
|
|
|
assert!(matches!(
|
|
|
|
hm.remove(&1),
|
|
|
|
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!(
|
|
|
|
assert!(matches!(
|
|
|
|
unsafe { hm.get(&1, 0) },
|
|
|
|
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!(
|
|
|
|
assert!(matches!(
|
|
|
|
unsafe { hm.pop(&1) },
|
|
|
|
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(Ok(20))));
|
|
|
|
assert!(matches!(
|
|
|
|
assert!(matches!(
|
|
|
|
keys.next(),
|
|
|
|
keys.next(),
|
|
|
|
Some(Err(MapError::GetNextKeyError { .. }))
|
|
|
|
Some(Err(MapError::SyscallError { call, .. })) if call == "bpf_map_get_next_key"
|
|
|
|
));
|
|
|
|
));
|
|
|
|
assert!(matches!(keys.next(), None));
|
|
|
|
assert!(matches!(keys.next(), None));
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -593,7 +608,7 @@ mod tests {
|
|
|
|
assert!(matches!(iter.next(), Some(Ok((20, 200)))));
|
|
|
|
assert!(matches!(iter.next(), Some(Ok((20, 200)))));
|
|
|
|
assert!(matches!(
|
|
|
|
assert!(matches!(
|
|
|
|
iter.next(),
|
|
|
|
iter.next(),
|
|
|
|
Some(Err(MapError::GetNextKeyError { .. }))
|
|
|
|
Some(Err(MapError::SyscallError { call, .. })) if call == "bpf_map_get_next_key"
|
|
|
|
));
|
|
|
|
));
|
|
|
|
assert!(matches!(iter.next(), None));
|
|
|
|
assert!(matches!(iter.next(), None));
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -631,7 +646,7 @@ mod tests {
|
|
|
|
assert!(matches!(iter.next(), Some(Ok((10, 100)))));
|
|
|
|
assert!(matches!(iter.next(), Some(Ok((10, 100)))));
|
|
|
|
assert!(matches!(
|
|
|
|
assert!(matches!(
|
|
|
|
iter.next(),
|
|
|
|
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(), Some(Ok((30, 300)))));
|
|
|
|
assert!(matches!(iter.next(), None));
|
|
|
|
assert!(matches!(iter.next(), None));
|
|
|
|