From 36edf092541574633ff03f7deb8b95003b2bcdd2 Mon Sep 17 00:00:00 2001 From: Andrew Burkett Date: Thu, 23 Jun 2022 08:39:31 -0700 Subject: [PATCH] Have bpf_map_update_elem take Option<&K> for key bpf_map_update_elem is used in lieu of bpf_map_push_elem to maintain support for kernel version < 4.20. The kernel expects a null pointer for the key for this use case. With this change, if you pass None as key to `bpf_map_update_elem`, it will pass null as key. --- aya/src/maps/array/array.rs | 2 +- aya/src/maps/array/program_array.rs | 2 +- aya/src/maps/hash_map/mod.rs | 2 +- aya/src/maps/lpm_trie.rs | 2 +- aya/src/maps/perf/perf_event_array.rs | 2 +- aya/src/maps/sock/sock_map.rs | 2 +- aya/src/maps/stack.rs | 2 +- aya/src/sys/bpf.rs | 11 +++++++++-- 8 files changed, 16 insertions(+), 9 deletions(-) diff --git a/aya/src/maps/array/array.rs b/aya/src/maps/array/array.rs index d6f1988a..6b5e5fa2 100644 --- a/aya/src/maps/array/array.rs +++ b/aya/src/maps/array/array.rs @@ -118,7 +118,7 @@ impl + DerefMut, V: Pod> Array { pub fn set(&mut self, index: u32, value: V, flags: u64) -> Result<(), MapError> { let fd = self.inner.fd_or_err()?; 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 { call: "bpf_map_update_elem".to_owned(), code, diff --git a/aya/src/maps/array/program_array.rs b/aya/src/maps/array/program_array.rs index 0ceff2b9..2f1df3c6 100644 --- a/aya/src/maps/array/program_array.rs +++ b/aya/src/maps/array/program_array.rs @@ -103,7 +103,7 @@ impl + DerefMut> ProgramArray { self.check_bounds(index)?; let prog_fd = program.fd().ok_or(MapError::ProgramNotLoaded)?; - 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 { call: "bpf_map_update_elem".to_owned(), code, diff --git a/aya/src/maps/hash_map/mod.rs b/aya/src/maps/hash_map/mod.rs index c6e99405..349f67b3 100644 --- a/aya/src/maps/hash_map/mod.rs +++ b/aya/src/maps/hash_map/mod.rs @@ -29,7 +29,7 @@ pub(crate) fn check_kv_size(map: &Map) -> Result<(), MapError> { pub(crate) fn insert(map: &mut Map, key: K, value: V, flags: u64) -> Result<(), MapError> { 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 { call: "bpf_map_update_elem".to_owned(), code, diff --git a/aya/src/maps/lpm_trie.rs b/aya/src/maps/lpm_trie.rs index 3266a8bf..473d92e4 100644 --- a/aya/src/maps/lpm_trie.rs +++ b/aya/src/maps/lpm_trie.rs @@ -145,7 +145,7 @@ impl, K: Pod, V: Pod> LpmTrie { /// Inserts a key value pair into the map. pub fn insert(&self, key: &Key, value: V, flags: u64) -> Result<(), MapError> { 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 { call: "bpf_map_update_elem".to_owned(), code, diff --git a/aya/src/maps/perf/perf_event_array.rs b/aya/src/maps/perf/perf_event_array.rs index ff4d02ac..eede6009 100644 --- a/aya/src/maps/perf/perf_event_array.rs +++ b/aya/src/maps/perf/perf_event_array.rs @@ -191,7 +191,7 @@ impl> PerfEventArray { // this cannot fail as new() checks that the fd is open let map_fd = self.map.fd_or_err().unwrap(); 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)?; Ok(PerfEventArrayBuffer { diff --git a/aya/src/maps/sock/sock_map.rs b/aya/src/maps/sock/sock_map.rs index 0a0b9c13..d9624a7e 100644 --- a/aya/src/maps/sock/sock_map.rs +++ b/aya/src/maps/sock/sock_map.rs @@ -90,7 +90,7 @@ impl + DerefMut> SockMap { pub fn set(&mut self, index: u32, socket: &I, flags: u64) -> Result<(), MapError> { let fd = self.inner.fd_or_err()?; 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 { call: "bpf_map_update_elem".to_owned(), code, diff --git a/aya/src/maps/stack.rs b/aya/src/maps/stack.rs index c8f36083..b22b049c 100644 --- a/aya/src/maps/stack.rs +++ b/aya/src/maps/stack.rs @@ -99,7 +99,7 @@ impl + DerefMut, V: Pod> Stack { /// [`MapError::SyscallError`] if `bpf_map_update_elem` fails. pub fn push(&mut self, value: V, flags: u64) -> Result<(), MapError> { 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 { call: "bpf_map_update_elem".to_owned(), code, diff --git a/aya/src/sys/bpf.rs b/aya/src/sys/bpf.rs index d6223434..d8423848 100644 --- a/aya/src/sys/bpf.rs +++ b/aya/src/sys/bpf.rs @@ -223,12 +223,19 @@ pub(crate) fn bpf_map_lookup_elem_ptr( } } -pub(crate) fn bpf_map_update_elem(fd: RawFd, key: &K, value: &V, flags: u64) -> SysResult { +pub(crate) fn bpf_map_update_elem( + fd: RawFd, + key: Option<&K>, + value: &V, + flags: u64, +) -> SysResult { let mut attr = unsafe { mem::zeroed::() }; let u = unsafe { &mut attr.__bindgen_anon_2 }; u.map_fd = fd as u32; - u.key = key as *const _ as u64; + if let Some(key) = key { + u.key = key as *const _ as u64; + } u.__bindgen_anon_1.value = value as *const _ as u64; u.flags = flags;