aya-bpf: Implement set() for array maps

pull/316/head
Sebastian Bernauer 3 years ago
parent 9d4fd4888c
commit ad69e30cd1

@ -1,10 +1,10 @@
use core::{cell::UnsafeCell, marker::PhantomData, mem, ptr::NonNull}; 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::{ use crate::{
bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_ARRAY}, 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, maps::PinningType,
}; };
@ -57,4 +57,18 @@ impl<T> Array<T> {
NonNull::new(value as *mut T).map(|p| p.as_ref()) 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)
}
}
} }

@ -1,10 +1,10 @@
use core::{cell::UnsafeCell, marker::PhantomData, mem, ptr::NonNull}; 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::{ use crate::{
bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_PERCPU_ARRAY}, 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, maps::PinningType,
}; };
@ -73,4 +73,18 @@ impl<T> PerCpuArray<T> {
); );
NonNull::new(ptr as *mut T) 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)
}
}
} }

Loading…
Cancel
Save