DevMapHash

Struct DevMapHash 

Source
pub struct DevMapHash { /* private fields */ }
Expand description

A map of network devices.

XDP programs can use this map to redirect packets to other network devices. It is similar to DevMap, but is an hash map rather than an array. Keys do not need to be contiguous nor start at zero, but there is a hashing cost to every lookup.

§Minimum kernel version

The minimum kernel version required to use this feature is 5.4.

§Examples

use aya_ebpf::{bindings::xdp_action, macros::{map, xdp}, maps::DevMapHash, programs::XdpContext};

#[map]
static MAP: DevMapHash = DevMapHash::with_max_entries(1, 0);

#[xdp]
fn xdp(_ctx: XdpContext) -> u32 {
    MAP.redirect(42, xdp_action::XDP_PASS as u64).unwrap_or(xdp_action::XDP_DROP)
}

Implementations§

Source§

impl DevMapHash

Source

pub const fn with_max_entries(max_entries: u32, flags: u32) -> DevMapHash

Creates a DevMapHash with a set maximum number of elements.

§Examples
use aya_ebpf::{macros::map, maps::DevMapHash};

#[map]
static MAP: DevMapHash = DevMapHash::with_max_entries(8, 0);
Source

pub const fn pinned(max_entries: u32, flags: u32) -> DevMapHash

Creates a DevMapHash with a set maximum number of elements that can be pinned to the BPF File System (bpffs).

§Examples
use aya_ebpf::{macros::map, maps::DevMapHash};

#[map]
static MAP: DevMapHash = DevMapHash::pinned(8, 0);
Source

pub fn get(&self, key: u32) -> Option<DevMapValue>

Retrieves the interface index with key in the map.

To actually redirect a packet, see DevMapHash::redirect.

§Examples
use aya_ebpf::{macros::map, maps::DevMapHash};

#[map]
static MAP: DevMapHash = DevMapHash::with_max_entries(1, 0);

let target_if_index = MAP.get(42).unwrap().if_index;

// redirect to ifindex
Source

pub fn redirect(&self, key: u32, flags: u64) -> Result<u32, u32>

Redirects the current packet on the interface at key.

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 CPU cannot be found.

§Examples
use aya_ebpf::{bindings::xdp_action, macros::{map, xdp}, maps::DevMapHash, programs::XdpContext};

#[map]
static MAP: DevMapHash = DevMapHash::with_max_entries(8, 0);

#[xdp]
fn xdp(_ctx: XdpContext) -> u32 {
    MAP.redirect(7, 0).unwrap_or(xdp_action::XDP_DROP)
}

Trait Implementations§

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.