Skip to content

Commit

Permalink
feat(s2n-quic-xdp): add umem hugepage option (#2145)
Browse files Browse the repository at this point in the history
  • Loading branch information
ollie-etl authored Mar 14, 2024
1 parent 440f4f4 commit 2d4df73
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
14 changes: 12 additions & 2 deletions tools/xdp/s2n-quic-xdp/src/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ pub struct Mmap {
len: usize,
}

#[derive(Debug)]
pub enum Options {
Huge,
Fd(RawFd),
}

/// Safety: Mmap pointer can be sent between threads
unsafe impl Send for Mmap {}

Expand All @@ -28,8 +34,12 @@ unsafe impl Sync for Mmap {}
impl Mmap {
/// Creates a new mmap'd region, with an optional file descriptor.
#[inline]
pub fn new(len: usize, offset: usize, fd: Option<RawFd>) -> Result<Self> {
let addr = mmap(len, offset, fd)?;
pub fn new(len: usize, offset: usize, flags: Option<Options>) -> Result<Self> {
let addr = match flags {
Some(Options::Huge) => mmap(len, offset, None, true),
Some(Options::Fd(fd)) => mmap(len, offset, Some(fd), false),
None => mmap(len, offset, None, false),
}?;
Ok(Self { addr, len })
}

Expand Down
6 changes: 3 additions & 3 deletions tools/xdp/s2n-quic-xdp/src/ring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::{
if_xdp::{MmapOffsets, RingFlags, RingOffsetV2, RxTxDescriptor, UmemDescriptor},
mmap::Mmap,
mmap::{self, Mmap},
socket, syscall,
};
use core::{fmt, mem::size_of, ptr::NonNull};
Expand Down Expand Up @@ -94,7 +94,7 @@ macro_rules! impl_producer {
// Use the hard-coded offset of the ring type
let offset = MmapOffsets::$offset;

let area = Mmap::new(len, offset, Some(socket.as_raw_fd()))?;
let area = Mmap::new(len, offset, Some(mmap::Options::Fd(socket.as_raw_fd())))?;

let (cursor, flags) = unsafe {
// Safety: `area` lives as long as `cursor`
Expand Down Expand Up @@ -193,7 +193,7 @@ macro_rules! impl_consumer {
// Use the hard-coded offset of the ring type
let offset = MmapOffsets::$offset;

let area = Mmap::new(len, offset, Some(socket.as_raw_fd()))?;
let area = Mmap::new(len, offset, Some(mmap::Options::Fd(socket.as_raw_fd())))?;

let (cursor, flags) = unsafe {
// Safety: `area` lives as long as `cursor`
Expand Down
7 changes: 5 additions & 2 deletions tools/xdp/s2n-quic-xdp/src/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,15 @@ pub fn busy_poll<Fd: AsRawFd>(fd: &Fd) -> Result<u32> {
///
/// See [xsk.c](https://github.com/xdp-project/xdp-tools/blob/a76e7a2b156b8cfe38992206abe9df1df0a29e38/lib/libxdp/xsk.c#L273).
#[inline]
pub fn mmap(len: usize, offset: usize, fd: Option<RawFd>) -> Result<NonNull<c_void>> {
let flags = if fd.is_some() {
pub fn mmap(len: usize, offset: usize, fd: Option<RawFd>, huge: bool) -> Result<NonNull<c_void>> {
let mut flags = if fd.is_some() {
libc::MAP_SHARED | libc::MAP_POPULATE
} else {
libc::MAP_PRIVATE | libc::MAP_ANONYMOUS
};
if huge {
flags |= libc::MAP_HUGETLB;
}

// See:
// * Fill https://github.com/xdp-project/xdp-tools/blob/a76e7a2b156b8cfe38992206abe9df1df0a29e38/lib/libxdp/xsk.c#L273
Expand Down
12 changes: 10 additions & 2 deletions tools/xdp/s2n-quic-xdp/src/umem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::{
if_xdp::{RxTxDescriptor, UmemDescriptor, UmemFlags, UmemReg},
mmap::Mmap,
mmap::{self, Mmap},
syscall, Result,
};
use core::ptr::NonNull;
Expand All @@ -22,6 +22,8 @@ pub struct Builder {
pub frame_headroom: u32,
/// The flags for the Umem
pub flags: UmemFlags,
/// Back the umem with a hugepage
pub hugepage: bool,
}

impl Default for Builder {
Expand All @@ -31,14 +33,20 @@ impl Default for Builder {
frame_count: 1024,
frame_headroom: 0,
flags: Default::default(),
hugepage: false,
}
}
}

impl Builder {
pub fn build(self) -> Result<Umem> {
let len = self.frame_size as usize * self.frame_count as usize;
let area = Mmap::new(len, 0, None)?;
let options = if self.hugepage {
Some(mmap::Options::Huge)
} else {
None
};
let area = Mmap::new(len, 0, options)?;
let area = Arc::new(area);
let mem = area.addr().cast();

Expand Down

0 comments on commit 2d4df73

Please sign in to comment.