Skip to main content

UmemManager

Struct UmemManager 

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

UMEM 管理器

管理 AF_XDP 所需的用户态内存区域。 提供 mmap、lock、HugePage 等功能。

Implementations§

Source§

impl UmemManager

Source

pub fn new(config: UmemConfig) -> Result<Self>

创建 UMEM 管理器

§参数
  • config - UMEM 配置
§返回
  • Result<Self> - 管理器实例
Source

pub fn create(&mut self) -> Result<()>

创建内存映射

§返回
  • Result<()> - 成功或错误
Source

pub fn region(&self) -> Option<&UmemRegion>

获取内存区域

§返回
  • Option<&UmemRegion> - 内存区域引用
Source

pub fn as_ptr(&self) -> *mut u8

获取起始地址

§返回
  • *mut u8 - 起始地址
Source

pub fn slice(&self, offset: usize, len: usize) -> Option<&[u8]>

获取 UMEM 映射区的只读切片(checked 边界,越界返回 None)

用于真实 AF_XDP 模式:内核 DMA 将收包数据写入本映射区, 数据面经本方法零拷贝读取帧内容。

§安全

返回切片的生命周期绑定 &self;映射区所有权由 UmemManager 持有, munmap 仅发生在 drop(调用方以 Arc<UmemManager> 保证使用期间存活)。

Source

pub fn slice_mut(&self, offset: usize, len: usize) -> Option<&mut [u8]>

获取 UMEM 映射区的可变切片(checked 边界,越界返回 None)

用于真实 AF_XDP 模式的 TX 路径:出站帧内容写入本映射区后由内核发送。

§安全

接收 &self 借用并返回 &mut [u8],技术上违反 Rust 别名规则 (aliasing rules)。UMEM 为与内核共享的可变内存(mmap MAP_SHARED), 语义上等价于 UnsafeCell<[u8]>。调用方(单 Owner Worker)在数据面 单线程顺序访问,保证同一帧区域无并发活跃借用,满足 SAFETY 不变量。

§Safety

调用者必须确保同一帧区域不会被两个线程同时通过 slice_mut 获取可变引用。 UMEM 区域是 mmap 映射的共享内存,不由 Rust 所有权模型管理, 帧级别的互斥由 Fill Ring / Completion Ring 的单消费者语义保证。

Source

pub fn page_offset(&self) -> u64

获取页偏移

§返回
  • u64 - 页偏移
Source

pub fn size(&self) -> usize

获取大小

§返回
  • usize - 大小(字节)
Source

pub fn is_initialized(&self) -> bool

是否已初始化

§返回
  • bool - 是否已初始化
Source

pub fn incref(&self) -> Result<u64>

增加引用计数(共享模式)

§Fail-Closed(返回错误而非 panic)
  • UmemManager 尚未调用 create(),返回 UmemError::NotCreated (避免对未映射内存建立引用)。
  • 若引用计数已为 0(说明最后一个引用已触发 munmap),返回 UmemError::MunmapFailed(防止 0→1 复活导致 UAF)。
  • fetch_add 后溢出 u64::MAX,Rust 原子为 wrapping,但 u64 的空间 对任何实际场景均足够(2^64 次),不可能溢出。
§返回
  • Result<u64> - 递增后的新引用计数
Source

pub fn decref(&self) -> u64

减少引用计数

返回递减后的新值。递减到 0 不会在此处执行 munmapmunmap 的唯一入口在 Drop::drop 中通过原子 CAS(1 → 0)触发, 确保每个映射区域精确释放一次且仅一次(无 double-free)。

Trait Implementations§

Source§

impl Debug for UmemManager

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for UmemManager

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more