maps/xdp: use ProgramFd instead of impl AsRawFd

Not having a generic here allows to pass `None` without specifying the
actual type you don't care about.
reviewable/pr527/r18
Tuetuopay 1 year ago
parent 9ed1d3d281
commit c6754c614e

@ -3,13 +3,14 @@
use std::{
borrow::{Borrow, BorrowMut},
num::NonZeroU32,
os::fd::AsRawFd,
os::fd::{AsFd, AsRawFd},
};
use aya_obj::generated::{bpf_cpumap_val, bpf_cpumap_val__bindgen_ty_1};
use crate::{
maps::{check_bounds, check_kv_size, IterableMap, MapData, MapError},
programs::ProgramFd,
sys::{bpf_map_lookup_elem, bpf_map_update_elem, SyscallError},
Pod,
};
@ -36,7 +37,7 @@ use crate::{
/// let flags = 0;
/// let queue_size = 2048;
/// for i in 0u32..8u32 {
/// cpumap.set(i, queue_size, None::<i32>, flags);
/// cpumap.set(i, queue_size, None, flags);
/// }
///
/// # Ok::<(), aya::BpfError>(())
@ -115,7 +116,7 @@ impl<T: BorrowMut<MapData>> CpuMap<T> {
&mut self,
cpu_index: u32,
queue_size: u32,
program: Option<impl AsRawFd>,
program: Option<&ProgramFd>,
flags: u64,
) -> Result<(), MapError> {
let data = self.inner.borrow_mut();
@ -127,7 +128,9 @@ impl<T: BorrowMut<MapData>> CpuMap<T> {
bpf_prog: bpf_cpumap_val__bindgen_ty_1 {
// Default is valid as the kernel will only consider fd > 0:
// https://github.com/torvalds/linux/blob/2dde18cd1d8fac735875f2e4987f11817cc0bc2c/kernel/bpf/cpumap.c#L466
fd: program.map(|prog| prog.as_raw_fd()).unwrap_or_default(),
fd: program
.map(|prog| prog.as_fd().as_raw_fd())
.unwrap_or_default(),
},
};
bpf_map_update_elem(fd, Some(&cpu_index), &value, flags).map_err(|(_, io_error)| {

@ -3,13 +3,14 @@
use std::{
borrow::{Borrow, BorrowMut},
num::NonZeroU32,
os::fd::AsRawFd,
os::fd::{AsFd, AsRawFd},
};
use aya_obj::generated::{bpf_devmap_val, bpf_devmap_val__bindgen_ty_1};
use crate::{
maps::{check_bounds, check_kv_size, IterableMap, MapData, MapError},
programs::ProgramFd,
sys::{bpf_map_lookup_elem, bpf_map_update_elem, SyscallError},
Pod,
};
@ -31,7 +32,7 @@ use crate::{
/// let mut devmap = DevMap::try_from(bpf.map_mut("IFACES").unwrap())?;
/// let source = 32u32;
/// let dest = 42u32;
/// devmap.set(source, dest, None::<i32>, 0);
/// devmap.set(source, dest, None, 0);
///
/// # Ok::<(), aya::BpfError>(())
/// ```
@ -108,7 +109,7 @@ impl<T: BorrowMut<MapData>> DevMap<T> {
&mut self,
index: u32,
ifindex: u32,
program: Option<impl AsRawFd>,
program: Option<&ProgramFd>,
flags: u64,
) -> Result<(), MapError> {
let data = self.inner.borrow_mut();
@ -121,7 +122,9 @@ impl<T: BorrowMut<MapData>> DevMap<T> {
// Default is valid as the kernel will only consider fd > 0:
// https://github.com/torvalds/linux/blob/2dde18cd1d8fac735875f2e4987f11817cc0bc2c/kernel/bpf/devmap.c#L866
// https://github.com/torvalds/linux/blob/2dde18cd1d8fac735875f2e4987f11817cc0bc2c/kernel/bpf/devmap.c#L918
fd: program.map(|prog| prog.as_raw_fd()).unwrap_or_default(),
fd: program
.map(|prog| prog.as_fd().as_raw_fd())
.unwrap_or_default(),
},
};
bpf_map_update_elem(fd, Some(&index), &value, flags).map_err(|(_, io_error)| {

@ -3,13 +3,14 @@
use std::{
borrow::{Borrow, BorrowMut},
num::NonZeroU32,
os::fd::AsRawFd,
os::fd::{AsFd, AsRawFd},
};
use aya_obj::generated::{bpf_devmap_val, bpf_devmap_val__bindgen_ty_1};
use crate::{
maps::{check_kv_size, hash_map, IterableMap, MapData, MapError, MapIter, MapKeys},
programs::ProgramFd,
sys::{bpf_map_lookup_elem, SyscallError},
};
@ -32,7 +33,7 @@ use super::dev_map::DevMapValue;
/// let mut devmap = DevMapHash::try_from(bpf.map_mut("IFACES").unwrap())?;
/// let flags = 0;
/// let ifindex = 32u32;
/// devmap.insert(ifindex, ifindex, None::<i32>, flags);
/// devmap.insert(ifindex, ifindex, None, flags);
///
/// # Ok::<(), aya::BpfError>(())
/// ```
@ -100,7 +101,7 @@ impl<T: BorrowMut<MapData>> DevMapHash<T> {
&mut self,
key: u32,
ifindex: u32,
program: Option<impl AsRawFd>,
program: Option<&ProgramFd>,
flags: u64,
) -> Result<(), MapError> {
let value = bpf_devmap_val {
@ -109,7 +110,9 @@ impl<T: BorrowMut<MapData>> DevMapHash<T> {
// Default is valid as the kernel will only consider fd > 0:
// https://github.com/torvalds/linux/blob/2dde18cd1d8fac735875f2e4987f11817cc0bc2c/kernel/bpf/devmap.c#L866
// https://github.com/torvalds/linux/blob/2dde18cd1d8fac735875f2e4987f11817cc0bc2c/kernel/bpf/devmap.c#L918
fd: program.map(|prog| prog.as_raw_fd()).unwrap_or_default(),
fd: program
.map(|prog| prog.as_fd().as_raw_fd())
.unwrap_or_default(),
},
};
hash_map::insert(self.inner.borrow_mut(), &key, &value, flags)

@ -1,4 +1,4 @@
use std::{net::UdpSocket, os::fd::AsFd, time::Duration};
use std::{net::UdpSocket, time::Duration};
use aya::{
maps::{Array, CpuMap},
@ -75,7 +75,7 @@ fn cpumap_chain() {
xdp.load().unwrap();
xdp.fd().unwrap()
};
cpus.set(0, 2048, Some(xdp_chain_fd.as_fd()), 0).unwrap();
cpus.set(0, 2048, Some(xdp_chain_fd), 0).unwrap();
// Load the main program
let xdp: &mut Xdp = bpf.program_mut("redirect_cpu").unwrap().try_into().unwrap();

Loading…
Cancel
Save