Browse Source

bpf: add HashMap::insert

Alessandro Decina 3 years ago
parent
commit
ab8d512b60
1 changed files with 16 additions and 2 deletions
  1. 16 2
      bpf/aya-bpf/src/maps/hash_map.rs

+ 16 - 2
bpf/aya-bpf/src/maps/hash_map.rs

@@ -1,10 +1,10 @@
 use core::{marker::PhantomData, mem};
 
-use aya_bpf_cty::c_void;
+use aya_bpf_cty::{c_long, c_void};
 
 use crate::{
     bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_HASH},
-    helpers::bpf_map_lookup_elem,
+    helpers::{bpf_map_lookup_elem, bpf_map_update_elem},
 };
 
 #[repr(transparent)]
@@ -41,4 +41,18 @@ impl<K, V> HashMap<K, V> {
             Some(&*(value as *const V))
         }
     }
+
+    pub unsafe fn insert(&mut self, key: &K, value: &V, flags: u64) -> Result<(), c_long> {
+        let ret = bpf_map_update_elem(
+            &mut self.def as *mut _ as *mut _,
+            key as *const _ as *const _,
+            value as *const _ as *const _,
+            flags,
+        );
+        if ret < 0 {
+            return Err(ret);
+        }
+
+        Ok(())
+    }
 }