|
|
@ -49,20 +49,8 @@ impl<T: Deref<Target = Map>, K: Pod, V: Pod> HashMap<T, K, V> {
|
|
|
|
map_type: map_type as u32,
|
|
|
|
map_type: map_type as u32,
|
|
|
|
})?;
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let size = mem::size_of::<K>();
|
|
|
|
check_kv_size::<K, V>(&map)?;
|
|
|
|
let expected = map.obj.def.key_size as usize;
|
|
|
|
let _ = map.fd_or_err()?;
|
|
|
|
if size != expected {
|
|
|
|
|
|
|
|
return Err(MapError::InvalidKeySize { size, expected });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let size = mem::size_of::<V>();
|
|
|
|
|
|
|
|
let expected = map.obj.def.value_size as usize;
|
|
|
|
|
|
|
|
if size != expected {
|
|
|
|
|
|
|
|
return Err(MapError::InvalidValueSize { size, expected });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// make sure the map has been created
|
|
|
|
|
|
|
|
let _fd = map.fd_or_err()?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(HashMap {
|
|
|
|
Ok(HashMap {
|
|
|
|
inner: map,
|
|
|
|
inner: map,
|
|
|
@ -83,13 +71,13 @@ impl<T: Deref<Target = Map>, K: Pod, V: Pod> HashMap<T, K, V> {
|
|
|
|
|
|
|
|
|
|
|
|
/// An iterator visiting all key-value pairs in arbitrary order. The
|
|
|
|
/// An iterator visiting all key-value pairs in arbitrary order. The
|
|
|
|
/// iterator item type is `Result<(K, V), MapError>`.
|
|
|
|
/// iterator item type is `Result<(K, V), MapError>`.
|
|
|
|
pub unsafe fn iter<'coll>(&'coll self) -> MapIter<'coll, K, V> {
|
|
|
|
pub unsafe fn iter(&self) -> MapIter<'_, K, V> {
|
|
|
|
MapIter::new(self)
|
|
|
|
MapIter::new(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// An iterator visiting all keys in arbitrary order. The iterator element
|
|
|
|
/// An iterator visiting all keys in arbitrary order. The iterator element
|
|
|
|
/// type is `Result<K, MapError>`.
|
|
|
|
/// type is `Result<K, MapError>`.
|
|
|
|
pub unsafe fn keys<'coll>(&'coll self) -> MapKeys<'coll, K, V> {
|
|
|
|
pub unsafe fn keys(&self) -> MapKeys<'_, K, V> {
|
|
|
|
MapKeys::new(self)
|
|
|
|
MapKeys::new(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -97,27 +85,12 @@ 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> {
|
|
|
|
/// Inserts a key-value pair into the map.
|
|
|
|
/// Inserts a key-value pair into the map.
|
|
|
|
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()?;
|
|
|
|
insert(&mut self.inner, key, value, flags)
|
|
|
|
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(())
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Removes a key from the map.
|
|
|
|
/// Removes a key from the map.
|
|
|
|
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()?;
|
|
|
|
remove(&mut self.inner, key)
|
|
|
|
bpf_map_delete_elem(fd, key)
|
|
|
|
|
|
|
|
.map(|_| ())
|
|
|
|
|
|
|
|
.map_err(|(code, io_error)| MapError::SyscallError {
|
|
|
|
|
|
|
|
call: "bpf_map_delete_elem".to_owned(),
|
|
|
|
|
|
|
|
code,
|
|
|
|
|
|
|
|
io_error,
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -163,6 +136,43 @@ impl<'a, K: Pod, V: Pod> TryFrom<&'a mut Map> for HashMap<&'a mut Map, K, V> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn check_kv_size<K, V>(map: &Map) -> Result<(), MapError> {
|
|
|
|
|
|
|
|
let size = mem::size_of::<K>();
|
|
|
|
|
|
|
|
let expected = map.obj.def.key_size as usize;
|
|
|
|
|
|
|
|
if size != expected {
|
|
|
|
|
|
|
|
return Err(MapError::InvalidKeySize { size, expected });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let size = mem::size_of::<V>();
|
|
|
|
|
|
|
|
let expected = map.obj.def.value_size as usize;
|
|
|
|
|
|
|
|
Ok(if size != expected {
|
|
|
|
|
|
|
|
return Err(MapError::InvalidValueSize { size, expected });
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn insert<K, V>(map: &mut Map, key: K, value: V, flags: u64) -> Result<(), MapError> {
|
|
|
|
|
|
|
|
let fd = map.fd_or_err()?;
|
|
|
|
|
|
|
|
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(())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn remove<K>(map: &mut Map, key: &K) -> Result<(), MapError> {
|
|
|
|
|
|
|
|
let fd = map.fd_or_err()?;
|
|
|
|
|
|
|
|
bpf_map_delete_elem(fd, key)
|
|
|
|
|
|
|
|
.map(|_| ())
|
|
|
|
|
|
|
|
.map_err(|(code, io_error)| MapError::SyscallError {
|
|
|
|
|
|
|
|
call: "bpf_map_delete_elem".to_owned(),
|
|
|
|
|
|
|
|
code,
|
|
|
|
|
|
|
|
io_error,
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
mod tests {
|
|
|
|
use std::io;
|
|
|
|
use std::io;
|
|
|
|