|
|
@ -1,16 +1,15 @@
|
|
|
|
use std::{convert::TryFrom, marker::PhantomData, mem, os::unix::prelude::RawFd};
|
|
|
|
use std::{convert::TryFrom, marker::PhantomData, mem};
|
|
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
use crate::{
|
|
|
|
generated::bpf_map_type::BPF_MAP_TYPE_HASH,
|
|
|
|
generated::bpf_map_type::BPF_MAP_TYPE_HASH,
|
|
|
|
|
|
|
|
maps::{IterableMap, Map, MapError, MapIter, MapKeys},
|
|
|
|
syscalls::{
|
|
|
|
syscalls::{
|
|
|
|
bpf_map_delete_elem, bpf_map_get_next_key, bpf_map_lookup_and_delete_elem,
|
|
|
|
bpf_map_delete_elem, bpf_map_lookup_and_delete_elem, bpf_map_lookup_elem,
|
|
|
|
bpf_map_lookup_elem, bpf_map_update_elem,
|
|
|
|
bpf_map_update_elem,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Pod, RawFd,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
use super::{Map, MapError};
|
|
|
|
|
|
|
|
use crate::Pod;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct HashMap<T: AsRef<Map>, K, V> {
|
|
|
|
pub struct HashMap<T: AsRef<Map>, K, V> {
|
|
|
|
inner: T,
|
|
|
|
inner: T,
|
|
|
|
_k: PhantomData<K>,
|
|
|
|
_k: PhantomData<K>,
|
|
|
@ -82,27 +81,6 @@ impl<T: AsRef<Map> + AsMut<Map>, K: Pod, V: Pod> HashMap<T, K, V> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a, K: Pod, V: Pod> TryFrom<&'a Map> for HashMap<&'a Map, K, V> {
|
|
|
|
|
|
|
|
type Error = MapError;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn try_from(inner: &'a Map) -> Result<HashMap<&'a Map, K, V>, MapError> {
|
|
|
|
|
|
|
|
HashMap::new(inner)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a, K: Pod, V: Pod> TryFrom<&'a mut Map> for HashMap<&'a mut Map, K, V> {
|
|
|
|
|
|
|
|
type Error = MapError;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn try_from(inner: &'a mut Map) -> Result<HashMap<&'a mut Map, K, V>, MapError> {
|
|
|
|
|
|
|
|
HashMap::new(inner)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub(crate) trait IterableMap<K: Pod, V: Pod> {
|
|
|
|
|
|
|
|
fn fd(&self) -> Result<RawFd, MapError>;
|
|
|
|
|
|
|
|
unsafe fn get(&self, key: &K) -> Result<Option<V>, MapError>;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<T: AsRef<Map>, K: Pod, V: Pod> IterableMap<K, V> for HashMap<T, K, V> {
|
|
|
|
impl<T: AsRef<Map>, K: Pod, V: Pod> IterableMap<K, V> for HashMap<T, K, V> {
|
|
|
|
fn fd(&self) -> Result<RawFd, MapError> {
|
|
|
|
fn fd(&self) -> Result<RawFd, MapError> {
|
|
|
|
self.inner.as_ref().fd_or_err()
|
|
|
|
self.inner.as_ref().fd_or_err()
|
|
|
@ -113,103 +91,19 @@ impl<T: AsRef<Map>, K: Pod, V: Pod> IterableMap<K, V> for HashMap<T, K, V> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub struct MapKeys<'coll, K: Pod, V: Pod> {
|
|
|
|
impl<'a, K: Pod, V: Pod> TryFrom<&'a Map> for HashMap<&'a Map, K, V> {
|
|
|
|
map: &'coll dyn IterableMap<K, V>,
|
|
|
|
type Error = MapError;
|
|
|
|
err: bool,
|
|
|
|
|
|
|
|
key: Option<K>,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<'coll, K: Pod, V: Pod> MapKeys<'coll, K, V> {
|
|
|
|
|
|
|
|
fn new(map: &'coll dyn IterableMap<K, V>) -> MapKeys<'coll, K, V> {
|
|
|
|
|
|
|
|
MapKeys {
|
|
|
|
|
|
|
|
map,
|
|
|
|
|
|
|
|
err: false,
|
|
|
|
|
|
|
|
key: None,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<K: Pod, V: Pod> Iterator for MapKeys<'_, K, V> {
|
|
|
|
|
|
|
|
type Item = Result<K, MapError>;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Result<K, MapError>> {
|
|
|
|
|
|
|
|
if self.err {
|
|
|
|
|
|
|
|
return None;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let fd = match self.map.fd() {
|
|
|
|
|
|
|
|
Ok(fd) => fd,
|
|
|
|
|
|
|
|
Err(e) => {
|
|
|
|
|
|
|
|
self.err = true;
|
|
|
|
|
|
|
|
return Some(Err(e));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
match bpf_map_get_next_key(fd, self.key.as_ref()) {
|
|
|
|
|
|
|
|
Ok(Some(key)) => {
|
|
|
|
|
|
|
|
self.key = Some(key);
|
|
|
|
|
|
|
|
return Some(Ok(key));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(None) => {
|
|
|
|
|
|
|
|
self.key = None;
|
|
|
|
|
|
|
|
return None;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Err((code, io_error)) => {
|
|
|
|
|
|
|
|
self.err = true;
|
|
|
|
|
|
|
|
return Some(Err(MapError::GetNextKeyFailed { code, io_error }));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct MapIter<'coll, K: Pod, V: Pod> {
|
|
|
|
|
|
|
|
inner: MapKeys<'coll, K, V>,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<'coll, K: Pod, V: Pod> MapIter<'coll, K, V> {
|
|
|
|
|
|
|
|
fn new(map: &'coll dyn IterableMap<K, V>) -> MapIter<'coll, K, V> {
|
|
|
|
|
|
|
|
MapIter {
|
|
|
|
|
|
|
|
inner: MapKeys::new(map),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<K: Pod, V: Pod> Iterator for MapIter<'_, K, V> {
|
|
|
|
|
|
|
|
type Item = Result<(K, V), MapError>;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
|
|
|
match self.inner.next() {
|
|
|
|
|
|
|
|
Some(Ok(key)) => {
|
|
|
|
|
|
|
|
let value = unsafe { self.inner.map.get(&key) };
|
|
|
|
|
|
|
|
match value {
|
|
|
|
|
|
|
|
Ok(None) => continue,
|
|
|
|
|
|
|
|
Ok(Some(value)) => return Some(Ok((key, value))),
|
|
|
|
|
|
|
|
Err(e) => return Some(Err(e)),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(Err(e)) => return Some(Err(e)),
|
|
|
|
|
|
|
|
None => return None,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl AsRef<Map> for &Map {
|
|
|
|
fn try_from(inner: &'a Map) -> Result<HashMap<&'a Map, K, V>, MapError> {
|
|
|
|
fn as_ref(&self) -> &Map {
|
|
|
|
HashMap::new(inner)
|
|
|
|
self
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl AsRef<Map> for &mut Map {
|
|
|
|
impl<'a, K: Pod, V: Pod> TryFrom<&'a mut Map> for HashMap<&'a mut Map, K, V> {
|
|
|
|
fn as_ref(&self) -> &Map {
|
|
|
|
type Error = MapError;
|
|
|
|
self
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl AsMut<Map> for &mut Map {
|
|
|
|
fn try_from(inner: &'a mut Map) -> Result<HashMap<&'a mut Map, K, V>, MapError> {
|
|
|
|
fn as_mut(&mut self) -> &mut Map {
|
|
|
|
HashMap::new(inner)
|
|
|
|
self
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|