Skip to content

Commit 2d4df73

Browse files
authored
feat(s2n-quic-xdp): add umem hugepage option (#2145)
1 parent 440f4f4 commit 2d4df73

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

tools/xdp/s2n-quic-xdp/src/mmap.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ pub struct Mmap {
1919
len: usize,
2020
}
2121

22+
#[derive(Debug)]
23+
pub enum Options {
24+
Huge,
25+
Fd(RawFd),
26+
}
27+
2228
/// Safety: Mmap pointer can be sent between threads
2329
unsafe impl Send for Mmap {}
2430

@@ -28,8 +34,12 @@ unsafe impl Sync for Mmap {}
2834
impl Mmap {
2935
/// Creates a new mmap'd region, with an optional file descriptor.
3036
#[inline]
31-
pub fn new(len: usize, offset: usize, fd: Option<RawFd>) -> Result<Self> {
32-
let addr = mmap(len, offset, fd)?;
37+
pub fn new(len: usize, offset: usize, flags: Option<Options>) -> Result<Self> {
38+
let addr = match flags {
39+
Some(Options::Huge) => mmap(len, offset, None, true),
40+
Some(Options::Fd(fd)) => mmap(len, offset, Some(fd), false),
41+
None => mmap(len, offset, None, false),
42+
}?;
3343
Ok(Self { addr, len })
3444
}
3545

tools/xdp/s2n-quic-xdp/src/ring.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use crate::{
55
if_xdp::{MmapOffsets, RingFlags, RingOffsetV2, RxTxDescriptor, UmemDescriptor},
6-
mmap::Mmap,
6+
mmap::{self, Mmap},
77
socket, syscall,
88
};
99
use core::{fmt, mem::size_of, ptr::NonNull};
@@ -94,7 +94,7 @@ macro_rules! impl_producer {
9494
// Use the hard-coded offset of the ring type
9595
let offset = MmapOffsets::$offset;
9696

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

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

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

198198
let (cursor, flags) = unsafe {
199199
// Safety: `area` lives as long as `cursor`

tools/xdp/s2n-quic-xdp/src/syscall.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,15 @@ pub fn busy_poll<Fd: AsRawFd>(fd: &Fd) -> Result<u32> {
195195
///
196196
/// See [xsk.c](https://github.com/xdp-project/xdp-tools/blob/a76e7a2b156b8cfe38992206abe9df1df0a29e38/lib/libxdp/xsk.c#L273).
197197
#[inline]
198-
pub fn mmap(len: usize, offset: usize, fd: Option<RawFd>) -> Result<NonNull<c_void>> {
199-
let flags = if fd.is_some() {
198+
pub fn mmap(len: usize, offset: usize, fd: Option<RawFd>, huge: bool) -> Result<NonNull<c_void>> {
199+
let mut flags = if fd.is_some() {
200200
libc::MAP_SHARED | libc::MAP_POPULATE
201201
} else {
202202
libc::MAP_PRIVATE | libc::MAP_ANONYMOUS
203203
};
204+
if huge {
205+
flags |= libc::MAP_HUGETLB;
206+
}
204207

205208
// See:
206209
// * Fill https://github.com/xdp-project/xdp-tools/blob/a76e7a2b156b8cfe38992206abe9df1df0a29e38/lib/libxdp/xsk.c#L273

tools/xdp/s2n-quic-xdp/src/umem.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use crate::{
55
if_xdp::{RxTxDescriptor, UmemDescriptor, UmemFlags, UmemReg},
6-
mmap::Mmap,
6+
mmap::{self, Mmap},
77
syscall, Result,
88
};
99
use core::ptr::NonNull;
@@ -22,6 +22,8 @@ pub struct Builder {
2222
pub frame_headroom: u32,
2323
/// The flags for the Umem
2424
pub flags: UmemFlags,
25+
/// Back the umem with a hugepage
26+
pub hugepage: bool,
2527
}
2628

2729
impl Default for Builder {
@@ -31,14 +33,20 @@ impl Default for Builder {
3133
frame_count: 1024,
3234
frame_headroom: 0,
3335
flags: Default::default(),
36+
hugepage: false,
3437
}
3538
}
3639
}
3740

3841
impl Builder {
3942
pub fn build(self) -> Result<Umem> {
4043
let len = self.frame_size as usize * self.frame_count as usize;
41-
let area = Mmap::new(len, 0, None)?;
44+
let options = if self.hugepage {
45+
Some(mmap::Options::Huge)
46+
} else {
47+
None
48+
};
49+
let area = Mmap::new(len, 0, options)?;
4250
let area = Arc::new(area);
4351
let mem = area.addr().cast();
4452

0 commit comments

Comments
 (0)