Merge pull request #328 from drewkett/map-update-no-key

Have bpf_map_update_elem take Option<&K> for key
pull/330/head
Alessandro Decina 2 years ago committed by GitHub
commit a301a56316
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -118,7 +118,7 @@ impl<T: Deref<Target = Map> + DerefMut<Target = Map>, V: Pod> Array<T, V> {
pub fn set(&mut self, index: u32, value: V, flags: u64) -> Result<(), MapError> { pub fn set(&mut self, index: u32, value: V, flags: u64) -> Result<(), MapError> {
let fd = self.inner.fd_or_err()?; let fd = self.inner.fd_or_err()?;
self.check_bounds(index)?; self.check_bounds(index)?;
bpf_map_update_elem(fd, &index, &value, flags).map_err(|(code, io_error)| { bpf_map_update_elem(fd, Some(&index), &value, flags).map_err(|(code, io_error)| {
MapError::SyscallError { MapError::SyscallError {
call: "bpf_map_update_elem".to_owned(), call: "bpf_map_update_elem".to_owned(),
code, code,

@ -105,7 +105,7 @@ impl<T: Deref<Target = Map> + DerefMut<Target = Map>> ProgramArray<T> {
self.check_bounds(index)?; self.check_bounds(index)?;
let prog_fd = program.as_raw_fd(); let prog_fd = program.as_raw_fd();
bpf_map_update_elem(fd, &index, &prog_fd, flags).map_err(|(code, io_error)| { bpf_map_update_elem(fd, Some(&index), &prog_fd, flags).map_err(|(code, io_error)| {
MapError::SyscallError { MapError::SyscallError {
call: "bpf_map_update_elem".to_owned(), call: "bpf_map_update_elem".to_owned(),
code, code,

@ -29,7 +29,7 @@ pub(crate) fn check_kv_size<K, V>(map: &Map) -> Result<(), MapError> {
pub(crate) fn insert<K, V>(map: &mut Map, key: K, value: V, flags: u64) -> Result<(), MapError> { pub(crate) fn insert<K, V>(map: &mut Map, key: K, value: V, flags: u64) -> Result<(), MapError> {
let fd = map.fd_or_err()?; let fd = map.fd_or_err()?;
bpf_map_update_elem(fd, &key, &value, flags).map_err(|(code, io_error)| { bpf_map_update_elem(fd, Some(&key), &value, flags).map_err(|(code, io_error)| {
MapError::SyscallError { MapError::SyscallError {
call: "bpf_map_update_elem".to_owned(), call: "bpf_map_update_elem".to_owned(),
code, code,

@ -145,7 +145,7 @@ impl<T: Deref<Target = Map>, K: Pod, V: Pod> LpmTrie<T, K, V> {
/// Inserts a key value pair into the map. /// Inserts a key value pair into the map.
pub fn insert(&self, key: &Key<K>, value: V, flags: u64) -> Result<(), MapError> { pub fn insert(&self, key: &Key<K>, value: V, flags: u64) -> Result<(), MapError> {
let fd = self.inner.deref().fd_or_err()?; let fd = self.inner.deref().fd_or_err()?;
bpf_map_update_elem(fd, key, &value, flags).map_err(|(code, io_error)| { bpf_map_update_elem(fd, Some(key), &value, flags).map_err(|(code, io_error)| {
MapError::SyscallError { MapError::SyscallError {
call: "bpf_map_update_elem".to_owned(), call: "bpf_map_update_elem".to_owned(),
code, code,

@ -191,7 +191,7 @@ impl<T: DerefMut<Target = Map>> PerfEventArray<T> {
// this cannot fail as new() checks that the fd is open // this cannot fail as new() checks that the fd is open
let map_fd = self.map.fd_or_err().unwrap(); let map_fd = self.map.fd_or_err().unwrap();
let buf = PerfBuffer::open(index, self.page_size, page_count.unwrap_or(2))?; let buf = PerfBuffer::open(index, self.page_size, page_count.unwrap_or(2))?;
bpf_map_update_elem(map_fd, &index, &buf.as_raw_fd(), 0) bpf_map_update_elem(map_fd, Some(&index), &buf.as_raw_fd(), 0)
.map_err(|(_, io_error)| io_error)?; .map_err(|(_, io_error)| io_error)?;
Ok(PerfEventArrayBuffer { Ok(PerfEventArrayBuffer {

@ -90,7 +90,7 @@ impl<T: Deref<Target = Map> + DerefMut<Target = Map>> SockMap<T> {
pub fn set<I: AsRawFd>(&mut self, index: u32, socket: &I, flags: u64) -> Result<(), MapError> { pub fn set<I: AsRawFd>(&mut self, index: u32, socket: &I, flags: u64) -> Result<(), MapError> {
let fd = self.inner.fd_or_err()?; let fd = self.inner.fd_or_err()?;
self.check_bounds(index)?; self.check_bounds(index)?;
bpf_map_update_elem(fd, &index, &socket.as_raw_fd(), flags).map_err( bpf_map_update_elem(fd, Some(&index), &socket.as_raw_fd(), flags).map_err(
|(code, io_error)| MapError::SyscallError { |(code, io_error)| MapError::SyscallError {
call: "bpf_map_update_elem".to_owned(), call: "bpf_map_update_elem".to_owned(),
code, code,

@ -99,7 +99,7 @@ impl<T: Deref<Target = Map> + DerefMut<Target = Map>, V: Pod> Stack<T, V> {
/// [`MapError::SyscallError`] if `bpf_map_update_elem` fails. /// [`MapError::SyscallError`] if `bpf_map_update_elem` fails.
pub fn push(&mut self, value: V, flags: u64) -> Result<(), MapError> { pub fn push(&mut self, value: V, flags: u64) -> Result<(), MapError> {
let fd = self.inner.fd_or_err()?; let fd = self.inner.fd_or_err()?;
bpf_map_update_elem(fd, &0, &value, flags).map_err(|(code, io_error)| { bpf_map_update_elem(fd, None::<&u32>, &value, flags).map_err(|(code, io_error)| {
MapError::SyscallError { MapError::SyscallError {
call: "bpf_map_update_elem".to_owned(), call: "bpf_map_update_elem".to_owned(),
code, code,

@ -225,12 +225,19 @@ pub(crate) fn bpf_map_lookup_elem_ptr<K: Pod, V>(
} }
} }
pub(crate) fn bpf_map_update_elem<K, V>(fd: RawFd, key: &K, value: &V, flags: u64) -> SysResult { pub(crate) fn bpf_map_update_elem<K, V>(
fd: RawFd,
key: Option<&K>,
value: &V,
flags: u64,
) -> SysResult {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() }; let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
let u = unsafe { &mut attr.__bindgen_anon_2 }; let u = unsafe { &mut attr.__bindgen_anon_2 };
u.map_fd = fd as u32; u.map_fd = fd as u32;
if let Some(key) = key {
u.key = key as *const _ as u64; u.key = key as *const _ as u64;
}
u.__bindgen_anon_1.value = value as *const _ as u64; u.__bindgen_anon_1.value = value as *const _ as u64;
u.flags = flags; u.flags = flags;

Loading…
Cancel
Save