1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
use core::ptr::NonNull;
use alloc::collections::BTreeSet;
use alloc::sync::Arc;
use crate::xdp::{SockAddrXdp, XdpDesc, XdpStatistics, XdpStatisticsV2, XdpUmemReg};
use crate::xsk::{
BufIdx, DeviceControl, DeviceQueue, DeviceQueueRegistration, DeviceRings, IfCtx, RingCons,
RingProd, RingRx, RingTx, Socket, SocketConfig, SocketFd, SocketMmapOffsets, Umem, UmemChunk,
UmemConfig, User, ptr_len,
};
use crate::{Errno, LastErrno};
use spin::RwLock;
impl BufIdx {
/// Convert a slice of raw numbers to buffer indices, in-place.
pub fn from_slice(id: &[u32]) -> &[Self] {
unsafe { &*(id as *const [u32] as *const [Self]) }
}
/// Convert a slice of raw numbers to buffer indices, in-place.
pub fn from_mut_slice(id: &mut [u32]) -> &mut [Self] {
unsafe { &mut *(id as *mut [u32] as *mut [Self]) }
}
/// Convert a slice buffer indices to raw numbers, in-place.
pub fn to_slice(this: &[Self]) -> &[u32] {
unsafe { &*(this as *const [Self] as *const [u32]) }
}
/// Convert a slice buffer indices to raw numbers, in-place.
pub fn to_mut_slice(this: &mut [Self]) -> &mut [u32] {
unsafe { &mut *(this as *mut [Self] as *mut [u32]) }
}
}
impl Umem {
/* Socket options for XDP */
pub(crate) const XDP_MMAP_OFFSETS: libc::c_int = 1;
pub(crate) const XDP_RX_RING: libc::c_int = 2;
pub(crate) const XDP_TX_RING: libc::c_int = 3;
pub(crate) const XDP_UMEM_REG: libc::c_int = 4;
pub(crate) const XDP_UMEM_FILL_RING: libc::c_int = 5;
pub(crate) const XDP_UMEM_COMPLETION_RING: libc::c_int = 6;
pub(crate) const XDP_STATISTICS: libc::c_int = 7;
#[allow(dead_code)]
pub(crate) const XDP_OPTIONS: libc::c_int = 8;
/// Create a new Umem ring.
///
/// # Safety
///
/// The caller passes an area denoting the memory of the ring. It must be valid for the
/// indicated buffer size and count. The caller is also responsible for keeping the mapping
/// alive.
///
/// The area must be page aligned and not exceed i64::MAX in length (on future systems where
/// you could).
pub unsafe fn new(config: UmemConfig, area: NonNull<[u8]>) -> Result<Umem, Errno> {
fn is_page_aligned(area: NonNull<[u8]>) -> bool {
let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) } as usize;
// TODO: use `addr()` as we don't need to expose the pointer here. Just the address as
// an integer and no provenance-preserving cast intended.
(area.as_ptr() as *mut u8 as usize & (page_size - 1)) == 0
}
assert!(config.frame_size > 0, "Invalid frame size");
assert!(
is_page_aligned(area),
"UB: Bad mmap area provided, but caller is responsible for its soundness."
);
let area_size = ptr_len(area.as_ptr());
assert!(
u64::try_from(area_size).is_ok(),
"Unhandled address space calculation"
);
let devices = DeviceControl {
inner: Arc::new(SpinLockedControlSet::default()),
};
// Two steps:
// 1. Create a new XDP socket in the kernel.
// 2. Configure it with the area and size.
// Safety: correct `socket` call.
let umem = Umem {
config,
fd: Arc::new(SocketFd::new()?),
umem_area: area,
devices,
};
Self::configure(&umem)?;
Ok(umem)
}
/// Get the address associated with a buffer, if it is in-bounds.
///
/// # Safety
///
/// No requirements. However, please ensure that _use_ of the pointer is done properly. The
/// pointer is guaranteed to be derived from the `area` passed in the constructor. The method
/// guarantees that it does not _access_ any of the pointers in this process.
pub fn frame(&self, idx: BufIdx) -> Option<UmemChunk> {
let pitch: u32 = self.config.frame_size;
let idx: u32 = idx.0;
let area_size = ptr_len(self.umem_area.as_ptr()) as u64;
// Validate that it fits.
let offset = u64::from(pitch) * u64::from(idx);
if area_size.checked_sub(u64::from(pitch)) < Some(offset) {
return None;
}
// Now: area_size is converted, without loss, from an isize that denotes the [u8] length,
// valid as guaranteed by the caller of the constructor. We have just checked:
//
// `[offset..offset+pitch) < area_size`.
//
// So all of the following is within the bounds of the constructor-guaranteed
// address manipulation.
let base = unsafe { self.umem_area.cast::<u8>().as_ptr().offset(offset as isize) };
debug_assert!(!base.is_null(), "UB: offsetting area within produced NULL");
let slice = core::ptr::slice_from_raw_parts_mut(base, pitch as usize);
let addr = unsafe { NonNull::new_unchecked(slice) };
Some(UmemChunk { addr, offset })
}
/// Count the number of available data frames.
pub fn len_frames(&self) -> u32 {
let area_size = ptr_len(self.umem_area.as_ptr()) as u64;
let count = area_size / u64::from(self.config.frame_size);
u32::try_from(count).unwrap_or(u32::MAX)
}
fn configure(this: &Umem) -> Result<(), Errno> {
let mut mr = XdpUmemReg {
addr: this.umem_area.as_ptr() as *mut u8 as u64,
len: ptr_len(this.umem_area.as_ptr()) as u64,
chunk_size: this.config.frame_size,
headroom: this.config.headroom,
flags: this.config.flags,
..XdpUmemReg::default()
};
let optlen = core::mem::size_of_val(&mr) as libc::socklen_t;
let err = unsafe {
libc::setsockopt(
this.fd.0,
super::SOL_XDP,
Self::XDP_UMEM_REG,
(&mut mr) as *mut _ as *mut libc::c_void,
optlen,
)
};
if err != 0 {
return Err(LastErrno)?;
}
Ok(())
}
/// Configure the fill and completion queue for a interface queue.
///
/// The caller _should_ only call this once for each interface info. However, it's not entirely
/// incorrect to do it multiple times. Just, be careful that the administration becomes extra
/// messy. All code is written under the assumption that only one controller/writer for the
/// user-space portions of each queue is active at a time. The kernel won't care about your
/// broken code and race conditions writing to the same queue concurrently. It's an SPSC.
/// Probably only the first call for each interface succeeds.
pub fn fq_cq(&self, interface: &Socket) -> Result<DeviceQueue, Errno> {
if !self.devices.insert(interface.info.ctx) {
// We know this will just yield `-EBUSY` anyways.
return Err(Errno(libc::EINVAL));
}
struct DropableDevice<'info>(&'info IfCtx, &'info DeviceControl);
impl Drop for DropableDevice<'_> {
fn drop(&mut self) {
self.1.remove(self.0);
}
}
// Okay, got a device. Let's create the queues for it. On failure, cleanup.
let _tmp_device = DropableDevice(&interface.info.ctx, &self.devices);
let sock = &*interface.fd;
Self::configure_cq(sock, &self.config)?;
let map = SocketMmapOffsets::new(sock)?;
// FIXME: should we be configured the `cached_consumer` and `cached_producer` and
// potentially other values, here? The setup produces a very rough clone of _just_ the ring
// itself and none of the logic beyond.
let prod = unsafe { RingProd::fill(sock, &map, self.config.fill_size) }?;
let cons = unsafe { RingCons::comp(sock, &map, self.config.complete_size) }?;
let device = DeviceQueue {
fcq: DeviceRings { map, cons, prod },
socket: Socket {
info: interface.info.clone(),
fd: interface.fd.clone(),
},
registration: DeviceQueueRegistration {
devices: self.devices.clone(),
ctx: interface.info.ctx,
},
};
core::mem::forget(_tmp_device);
Ok(device)
}
/// Configure the device address for a socket.
///
/// Either `rx_size` or `tx_size` must be non-zero, i.e. the call to bind will fail if none of
/// the rings is actually configured.
///
/// Note: if the underlying socket is shared then this will also bind other objects that share
/// the underlying socket file descriptor, this is intended.
pub fn rx_tx(&self, interface: &Socket, config: &SocketConfig) -> Result<User, Errno> {
let sock = &*interface.fd;
Self::configure_rt(sock, config)?;
let map = SocketMmapOffsets::new(sock)?;
Ok(User {
socket: Socket {
info: interface.info.clone(),
fd: interface.fd.clone(),
},
config: Arc::new(config.clone()),
map,
})
}
/// Activate a socket with by binding it to a device.
///
/// This associates the umem region to these queues. This is intended for:
///
/// - sockets that maintain the fill and completion ring for a device queue, i.e. a `fc_cq` was
/// called with the socket and that network interface queue is currently being bound.
///
/// - queues that the umem socket file descriptor is maintaining as a device queue, i.e. the
/// call to `fc_cq` used a socket created with [`Socket::with_shared`] that utilized the
/// [`Umem`] instance.
///
/// Otherwise, when a pure rx/tx socket should be setup use [`DeviceQueue::bind`] with the
/// previously bound socket providing its fill/completion queues.
///
/// The tree of parents should look as follows:
///
/// ```text
/// fd0: umem [+fq/cq for ifq0] [+rx/+tx]
/// |- [fd1: socket +rx/tx on ifq0 if fd0 has fq/cq] Umem::bind(fd0, fd1)
/// |- [fd2: socket +rx/tx on ifq0 if fd0 has fq/cq …] Umem::bind(fd0, fd2)
/// |
/// |- fd3: socket +fq/cq for ifq1 [+rx/tx] Umem::bind(fd0, fd3)
/// | |- fd4: socket +rx/tx on ifq1 DeviceQueue::bind(fd3, fd4)
/// | |- fd5: socket +rx/tx on ifq1 … DeviceQueue::bind(fd3, fd5)
/// |
/// |-fd6: socket +fq/cq for ifq2 [+rx/tx] Umem::bind(fd0, fd6)
/// | |- fd7: socket +rx/tx on ifq1 DeviceQueue::bind(fd6, fd7)
/// | |- …
/// ```
pub fn bind(&self, interface: &User) -> Result<(), Errno> {
Self::bind_at(interface, &self.fd)
}
fn bind_at(interface: &User, umem_sock: &SocketFd) -> Result<(), Errno> {
let mut sxdp = SockAddrXdp {
ifindex: interface.socket.info.ctx.ifindex,
queue_id: interface.socket.info.ctx.queue_id,
flags: interface.config.bind_flags,
..SockAddrXdp::default()
};
// Note: using a separate socket with shared umem requires one dedicated configured cq for
// the interface indicated.
if interface.socket.fd.0 != umem_sock.0 {
sxdp.flags |= SocketConfig::XDP_BIND_SHARED_UMEM;
sxdp.shared_umem_fd = umem_sock.0 as u32;
}
if unsafe {
libc::bind(
interface.socket.fd.0,
(&sxdp) as *const _ as *const libc::sockaddr,
core::mem::size_of_val(&sxdp) as libc::socklen_t,
)
} != 0
{
return Err(LastErrno)?;
}
Ok(())
}
pub(crate) fn configure_cq(fd: &SocketFd, config: &UmemConfig) -> Result<(), Errno> {
if unsafe {
libc::setsockopt(
fd.0,
super::SOL_XDP,
Umem::XDP_UMEM_COMPLETION_RING,
(&config.complete_size) as *const _ as *const libc::c_void,
core::mem::size_of_val(&config.complete_size) as libc::socklen_t,
)
} != 0
{
return Err(LastErrno)?;
}
if unsafe {
libc::setsockopt(
fd.0,
super::SOL_XDP,
Umem::XDP_UMEM_FILL_RING,
(&config.fill_size) as *const _ as *const libc::c_void,
core::mem::size_of_val(&config.fill_size) as libc::socklen_t,
)
} != 0
{
return Err(LastErrno)?;
}
Ok(())
}
pub(crate) fn configure_rt(fd: &SocketFd, config: &SocketConfig) -> Result<(), Errno> {
if let Some(num) = config.rx_size {
if unsafe {
libc::setsockopt(
fd.0,
super::SOL_XDP,
Umem::XDP_RX_RING,
(&num) as *const _ as *const libc::c_void,
core::mem::size_of_val(&num) as libc::socklen_t,
)
} != 0
{
return Err(LastErrno)?;
}
}
if let Some(num) = config.tx_size {
if unsafe {
libc::setsockopt(
fd.0,
super::SOL_XDP,
Umem::XDP_TX_RING,
(&num) as *const _ as *const libc::c_void,
core::mem::size_of_val(&num) as libc::socklen_t,
)
} != 0
{
return Err(LastErrno)?;
}
}
Ok(())
}
}
impl DeviceQueue {
/// Get the statistics of this XDP socket.
#[deprecated = "Consider using `statistics_v2` for additional statistics exposed on >= Linux 5.9"]
pub fn statistics(&self) -> Result<XdpStatistics, Errno> {
XdpStatistics::new(&self.socket.fd)
}
/// Get the statistics of this XDP socket.
pub fn statistics_v2(&self) -> Result<XdpStatisticsV2, Errno> {
XdpStatisticsV2::new(&self.socket.fd)
}
/// Configure a default XDP program.
///
/// This is necessary to start receiving packets on any of the related receive rings, i.e. to
/// start consuming from the fill queue and fill the completion queue.
#[doc(hidden)]
#[deprecated = "Not implemented to reduce scope and weight, use another library to bind a BPF to the socket."]
pub fn setup_xdp_prog(&mut self) -> Result<(), libc::c_int> {
panic!("Not implemented to reduce scope and weight, use another library to bind a BPF to the socket.");
}
/// Bind the socket to a device queue, activate rx/tx queues.
pub fn bind(&self, interface: &User) -> Result<(), Errno> {
Umem::bind_at(interface, &self.socket.fd)
}
}
impl User {
/// Get the statistics of this XDP socket.
#[deprecated = "Consider using `statistics_v2` for additional statistics exposed on >= Linux 5.9"]
pub fn statistics(&self) -> Result<XdpStatistics, Errno> {
XdpStatistics::new(&self.socket.fd)
}
/// Get the statistics of this XDP socket.
pub fn statistics_v2(&self) -> Result<XdpStatisticsV2, Errno> {
XdpStatisticsV2::new(&self.socket.fd)
}
/// Map the RX ring into memory, returning a handle.
///
/// Fails if you did not pass any size for `rx_size` in the configuration, which should be somewhat obvious.
///
/// FIXME: we allow mapping the ring more than once. Not a memory safety problem afaik, but a
/// correctness problem.
pub fn map_rx(&self) -> Result<RingRx, Errno> {
let rx_size = self.config.rx_size.ok_or(Errno(-libc::EINVAL))?.get();
let ring = unsafe { RingCons::rx(&self.socket.fd, &self.map, rx_size) }?;
Ok(RingRx {
fd: self.socket.fd.clone(),
ring,
})
}
/// Map the TX ring into memory, returning a handle.
///
/// Fails if you did not pass any size for `tx_size` in the configuration, which should be somewhat obvious.
///
/// FIXME: we allow mapping the ring more than once. Not a memory safety problem afaik, but a
/// correctness problem.
pub fn map_tx(&self) -> Result<RingTx, Errno> {
let tx_size = self.config.tx_size.ok_or(Errno(-libc::EINVAL))?.get();
let ring = unsafe { RingProd::tx(&self.socket.fd, &self.map, tx_size) }?;
Ok(RingTx {
fd: self.socket.fd.clone(),
ring,
})
}
}
impl SocketConfig {
/// Flag-bit for [`Umem::bind`] that the descriptor is shared.
///
/// Generally, this flag need not be passed directly. Instead, it is set within by the library
/// when the same `Umem` is used for multiple interface/queue combinations.
pub const XDP_BIND_SHARED_UMEM: u16 = 1 << 0;
/// Force copy-mode.
pub const XDP_BIND_COPY: u16 = 1 << 1;
/// Force zero-copy-mode.
/// check if your NIC supports zero-copy mode by searching `XDP_SETUP_XSK_POOL` in linux kernel source code.
pub const XDP_BIND_ZEROCOPY: u16 = 1 << 2;
/// Enable support for need wakeup.
///
/// Needs to be set for [`DeviceQueue::needs_wakeup`] and [`RingTx::needs_wakeup`].
pub const XDP_BIND_NEED_WAKEUP: u16 = 1 << 3;
}
#[derive(Default)]
struct SpinLockedControlSet {
inner: RwLock<BTreeSet<IfCtx>>,
}
impl core::ops::Deref for DeviceControl {
type Target = dyn super::ControlSet;
fn deref(&self) -> &Self::Target {
&*self.inner
}
}
impl super::ControlSet for SpinLockedControlSet {
fn insert(&self, ctx: IfCtx) -> bool {
let mut lock = self.inner.write();
lock.insert(ctx)
}
fn contains(&self, ctx: &IfCtx) -> bool {
let lock = self.inner.read();
lock.contains(ctx)
}
fn remove(&self, ctx: &IfCtx) {
let mut lock = self.inner.write();
lock.remove(ctx);
}
}
impl UmemChunk {
/// Turn this whole chunk into a concrete descriptor for the transmit ring.
///
/// If you've the address or offset are not as returned by the ring then the result is
/// unspecified, but sound. And potentially safe to use, but the kernel may complain.
pub fn as_xdp(self) -> XdpDesc {
let len = ptr_len(self.addr.as_ptr()) as u32;
self.as_xdp_with_len(len)
}
/// Turn into a descriptor with concrete length.
///
/// # Panics
///
/// When debug assertions are enabled, this panics if the length is longer than the address
/// range refers to.
pub fn as_xdp_with_len(self, len: u32) -> XdpDesc {
debug_assert!(
len <= ptr_len(self.addr.as_ptr()) as u32,
"Invalid XDP descriptor length {} for chunk of size {}",
len,
ptr_len(self.addr.as_ptr()) as u32,
);
XdpDesc {
addr: self.offset,
len,
options: 0,
}
}
}