|
|
@ -2,12 +2,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
use std::{
|
|
|
|
use std::{
|
|
|
|
borrow::{Borrow, BorrowMut},
|
|
|
|
borrow::{Borrow, BorrowMut},
|
|
|
|
os::fd::{AsFd, AsRawFd, RawFd},
|
|
|
|
os::fd::{AsFd, AsRawFd, BorrowedFd, RawFd},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
use crate::{
|
|
|
|
maps::{MapData, MapError, check_bounds, check_kv_size},
|
|
|
|
maps::{MapData, MapError, check_bounds, check_kv_size},
|
|
|
|
sys::{SyscallError, bpf_map_update_elem},
|
|
|
|
sys::{SyscallError, bpf_map_delete_elem, bpf_map_update_elem},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// An array of AF_XDP sockets.
|
|
|
|
/// An array of AF_XDP sockets.
|
|
|
@ -57,6 +57,16 @@ impl<T: Borrow<MapData>> XskMap<T> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<T: BorrowMut<MapData>> XskMap<T> {
|
|
|
|
impl<T: BorrowMut<MapData>> XskMap<T> {
|
|
|
|
|
|
|
|
fn with_fd(
|
|
|
|
|
|
|
|
&mut self,
|
|
|
|
|
|
|
|
index: u32,
|
|
|
|
|
|
|
|
f: impl FnOnce(BorrowedFd<'_>) -> Result<(), SyscallError>,
|
|
|
|
|
|
|
|
) -> Result<(), MapError> {
|
|
|
|
|
|
|
|
let data = self.inner.borrow_mut();
|
|
|
|
|
|
|
|
check_bounds(data, index)?;
|
|
|
|
|
|
|
|
f(data.fd().as_fd()).map_err(Into::into)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Sets the `AF_XDP` socket at a given index.
|
|
|
|
/// Sets the `AF_XDP` socket at a given index.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// When redirecting a packet, the `AF_XDP` socket at `index` will recieve the packet. Note
|
|
|
|
/// When redirecting a packet, the `AF_XDP` socket at `index` will recieve the packet. Note
|
|
|
@ -68,14 +78,28 @@ impl<T: BorrowMut<MapData>> XskMap<T> {
|
|
|
|
/// Returns [`MapError::OutOfBounds`] if `index` is out of bounds, [`MapError::SyscallError`]
|
|
|
|
/// Returns [`MapError::OutOfBounds`] if `index` is out of bounds, [`MapError::SyscallError`]
|
|
|
|
/// if `bpf_map_update_elem` fails.
|
|
|
|
/// if `bpf_map_update_elem` fails.
|
|
|
|
pub fn set(&mut self, index: u32, socket_fd: impl AsRawFd, flags: u64) -> Result<(), MapError> {
|
|
|
|
pub fn set(&mut self, index: u32, socket_fd: impl AsRawFd, flags: u64) -> Result<(), MapError> {
|
|
|
|
let data = self.inner.borrow_mut();
|
|
|
|
self.with_fd(index, |fd| {
|
|
|
|
check_bounds(data, index)?;
|
|
|
|
bpf_map_update_elem(fd, Some(&index), &socket_fd.as_raw_fd(), flags).map_err(
|
|
|
|
let fd = data.fd().as_fd();
|
|
|
|
|io_error| SyscallError {
|
|
|
|
bpf_map_update_elem(fd, Some(&index), &socket_fd.as_raw_fd(), flags)
|
|
|
|
call: "bpf_map_update_elem",
|
|
|
|
.map_err(|io_error| SyscallError {
|
|
|
|
io_error,
|
|
|
|
call: "bpf_map_update_elem",
|
|
|
|
},
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Un-sets the `AF_XDP` socket at a given index.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Errors
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// Returns [`MapError::OutOfBounds`] if `index` is out of bounds, [`MapError::SyscallError`]
|
|
|
|
|
|
|
|
/// if `bpf_map_delete_elem` fails.
|
|
|
|
|
|
|
|
pub fn unset(&mut self, index: u32) -> Result<(), MapError> {
|
|
|
|
|
|
|
|
self.with_fd(index, |fd| {
|
|
|
|
|
|
|
|
bpf_map_delete_elem(fd, &index).map_err(|io_error| SyscallError {
|
|
|
|
|
|
|
|
call: "bpf_map_delete_elem",
|
|
|
|
io_error,
|
|
|
|
io_error,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.map_err(Into::into)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|