diff --git a/bpf/aya-bpf/src/maps/array.rs b/bpf/aya-bpf/src/maps/array.rs index 53b596ca..546d68a7 100644 --- a/bpf/aya-bpf/src/maps/array.rs +++ b/bpf/aya-bpf/src/maps/array.rs @@ -1,10 +1,10 @@ use core::{cell::UnsafeCell, marker::PhantomData, mem, ptr::NonNull}; -use aya_bpf_cty::c_void; +use aya_bpf_cty::{c_void, c_long}; use crate::{ bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_ARRAY}, - helpers::bpf_map_lookup_elem, + helpers::{bpf_map_lookup_elem, bpf_map_update_elem}, maps::PinningType, }; @@ -57,4 +57,18 @@ impl Array { NonNull::new(value as *mut T).map(|p| p.as_ref()) } } + + pub fn set(&self, index: u32, value: &T, flags: u64) -> Result<(), c_long> { + unsafe { + let ret = unsafe { + bpf_map_update_elem( + self.def.get() as *mut _, + &index as *const _ as *const c_void, + value as *const _ as *const _, + flags, + ) + }; + (ret >= 0).then(|| ()).ok_or(ret) + } + } } diff --git a/bpf/aya-bpf/src/maps/per_cpu_array.rs b/bpf/aya-bpf/src/maps/per_cpu_array.rs index 5785de6e..91e5d363 100644 --- a/bpf/aya-bpf/src/maps/per_cpu_array.rs +++ b/bpf/aya-bpf/src/maps/per_cpu_array.rs @@ -1,10 +1,10 @@ use core::{cell::UnsafeCell, marker::PhantomData, mem, ptr::NonNull}; -use aya_bpf_cty::c_void; +use aya_bpf_cty::{c_void, c_long}; use crate::{ bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_PERCPU_ARRAY}, - helpers::bpf_map_lookup_elem, + helpers::{bpf_map_lookup_elem, bpf_map_update_elem}, maps::PinningType, }; @@ -73,4 +73,18 @@ impl PerCpuArray { ); NonNull::new(ptr as *mut T) } + + pub fn set(&self, index: u32, value: &T, flags: u64) -> Result<(), c_long> { + unsafe { + let ret = unsafe { + bpf_map_update_elem( + self.def.get() as *mut _, + &index as *const _ as *const c_void, + value as *const _ as *const _, + flags, + ) + }; + (ret >= 0).then(|| ()).ok_or(ret) + } + } }