|
|
@ -9,6 +9,48 @@ use crate::{
|
|
|
|
maps::PinningType,
|
|
|
|
maps::PinningType,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// An array of AF_XDP sockets.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// XDP programs can use this map to redirect packets to a target AF_XDP socket using the
|
|
|
|
|
|
|
|
/// `XDP_REDIRECT` action.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Minimum kernel version
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// The minimum kernel version required to use this feature is 4.18.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Examples
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// ```rust,no_run
|
|
|
|
|
|
|
|
/// use aya_bpf::{bindings::xdp_action, macros::{map, xdp}, maps::XskMap, programs::XdpContext};
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// #[map]
|
|
|
|
|
|
|
|
/// static SOCKS: XskMap = XskMap::with_max_entries(8, 0);
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// #[xdp]
|
|
|
|
|
|
|
|
/// fn xdp(ctx, XdpContext) -> i32 {
|
|
|
|
|
|
|
|
/// let queue_id = unsafe { (*ctx.ctx).rx_queue_index };
|
|
|
|
|
|
|
|
/// MAP.redirect(queue_id, xdp_action::XDP_DROP as u64)
|
|
|
|
|
|
|
|
/// }
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Queue management
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// Packets received on a RX queue can only be redirected to sockets bound on the same queue. Most
|
|
|
|
|
|
|
|
/// hardware NICs have multiple RX queue to spread the load across multiple CPU cores using RSS.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// Three strategies are possible:
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// - Reduce the RX queue count to a single one. This option is great for development, but is
|
|
|
|
|
|
|
|
/// detrimental for performance as the single CPU core recieving packets will get overwhelmed.
|
|
|
|
|
|
|
|
/// Setting the queue count for a NIC can be achieved using `ethtool -L <ifname> combined 1`.
|
|
|
|
|
|
|
|
/// - Create a socket for every RX queue. Most modern NICs will have an RX queue per CPU thread, so
|
|
|
|
|
|
|
|
/// a socket per CPU thread is best for performance. To dynamically size the map depending on the
|
|
|
|
|
|
|
|
/// recieve queue count, see the userspace documentation of `CpuMap`.
|
|
|
|
|
|
|
|
/// - Create a single socket and use a [`CpuMap`](super::CpuMap) to redirect the packet to the
|
|
|
|
|
|
|
|
/// correct CPU core. This way, the packet is sent to another CPU, and a chained XDP program can
|
|
|
|
|
|
|
|
/// the redirect to the AF_XDP socket. Using a single socket simplifies the userspace code but
|
|
|
|
|
|
|
|
/// will not perform great unless not a lot of traffic is redirected to the socket. Regular
|
|
|
|
|
|
|
|
/// traffic however will not be impacted, contrary to reducing the queue count.
|
|
|
|
#[repr(transparent)]
|
|
|
|
#[repr(transparent)]
|
|
|
|
pub struct XskMap {
|
|
|
|
pub struct XskMap {
|
|
|
|
def: UnsafeCell<bpf_map_def>,
|
|
|
|
def: UnsafeCell<bpf_map_def>,
|
|
|
@ -17,6 +59,16 @@ pub struct XskMap {
|
|
|
|
unsafe impl Sync for XskMap {}
|
|
|
|
unsafe impl Sync for XskMap {}
|
|
|
|
|
|
|
|
|
|
|
|
impl XskMap {
|
|
|
|
impl XskMap {
|
|
|
|
|
|
|
|
/// Creates a [`XskMap`] with a set maximum number of elements.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Examples
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// ```rust,no_run
|
|
|
|
|
|
|
|
/// use aya_bpf::{macros::map, maps::XskMap};
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// #[map]
|
|
|
|
|
|
|
|
/// static SOCKS: XskMap::with_max_entries(8, 0);
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
pub const fn with_max_entries(max_entries: u32, flags: u32) -> XskMap {
|
|
|
|
pub const fn with_max_entries(max_entries: u32, flags: u32) -> XskMap {
|
|
|
|
XskMap {
|
|
|
|
XskMap {
|
|
|
|
def: UnsafeCell::new(bpf_map_def {
|
|
|
|
def: UnsafeCell::new(bpf_map_def {
|
|
|
@ -31,6 +83,17 @@ impl XskMap {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Creates a [`XskMap`] with a set maximum number of elements that can be pinned to the BPF
|
|
|
|
|
|
|
|
/// filesystem (bpffs).
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Examples
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// ```rust,no_run
|
|
|
|
|
|
|
|
/// use aya_bpf::{macros::map, maps::XskMap};
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// #[map]
|
|
|
|
|
|
|
|
/// static SOCKS: XskMap::pinned(8, 0);
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
pub const fn pinned(max_entries: u32, flags: u32) -> XskMap {
|
|
|
|
pub const fn pinned(max_entries: u32, flags: u32) -> XskMap {
|
|
|
|
XskMap {
|
|
|
|
XskMap {
|
|
|
|
def: UnsafeCell::new(bpf_map_def {
|
|
|
|
def: UnsafeCell::new(bpf_map_def {
|
|
|
@ -45,6 +108,20 @@ impl XskMap {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Retrieves the queue to which the socket is bound at `index` in the array.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// To actually redirect a packet, see [`XskMap::redirect`].
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Examples
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// ```rust,no_run
|
|
|
|
|
|
|
|
/// use aya_bpf::{macros::map, maps::XskMap};
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// #[map]
|
|
|
|
|
|
|
|
/// static SOCKS: XskMap = XskMap::with_max_entries(8, 0);
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// let queue_id = SOCKS.get(0);
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
#[inline(always)]
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn get(&self, index: u32) -> Option<u32> {
|
|
|
|
pub fn get(&self, index: u32) -> Option<u32> {
|
|
|
|
unsafe {
|
|
|
|
unsafe {
|
|
|
@ -56,6 +133,28 @@ impl XskMap {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Redirects the current packet to the AF_XDP socket at `index`.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// The lower two bits of `flags` are used for the return code if the map lookup fails, which
|
|
|
|
|
|
|
|
/// can be used as the XDP program's return code if a matching socket cannot be found.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// However, if the socket at `index` is bound to a RX queue which is not the current RX queue,
|
|
|
|
|
|
|
|
/// the packet will be dropped.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Examples
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// ```rust,no_run
|
|
|
|
|
|
|
|
/// use aya_bpf::{bindings::xdp_action, macros::{map, xdp}, maps::XskMap, programs::XdpContext};
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// #[map]
|
|
|
|
|
|
|
|
/// static SOCKS: XskMap = XskMap::with_max_entries(8, 0);
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// #[xdp]
|
|
|
|
|
|
|
|
/// fn xdp(ctx, XdpContext) -> i32 {
|
|
|
|
|
|
|
|
/// let queue_id = unsafe { (*ctx.ctx).rx_queue_index };
|
|
|
|
|
|
|
|
/// MAP.redirect(queue_id, xdp_action::XDP_DROP as u64)
|
|
|
|
|
|
|
|
/// }
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
#[inline(always)]
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn redirect(&self, index: u32, flags: u64) -> u32 {
|
|
|
|
pub fn redirect(&self, index: u32, flags: u64) -> u32 {
|
|
|
|
unsafe {
|
|
|
|
unsafe {
|
|
|
|