Skip to content

Commit f717b77

Browse files
committed
feat(net): Add Allocate trait
We need to abstract the creation of buffers. Add trait Allocate to PacketBufferMut. Signed-off-by: Fredi Raspall <[email protected]>
1 parent 79463d0 commit f717b77

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

dpdk/src/mem.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use dpdk_sys::{
2626
rte_pktmbuf_tailroom, rte_pktmbuf_trim,
2727
};
2828
// unfortunately, we need the standard library to swap allocators
29-
use net::buffer::{Append, Headroom, Prepend, Tailroom, TrimFromEnd, TrimFromStart};
29+
use net::buffer::{Append, Create, Headroom, Prepend, Tailroom, TrimFromEnd, TrimFromStart};
3030
use std::alloc::System;
3131
use std::ffi::CString;
3232

@@ -422,7 +422,18 @@ impl AsMut<[u8]> for Mbuf {
422422
self.raw_data_mut()
423423
}
424424
}
425-
425+
impl Create for Mbuf {
426+
type PoolType = Pool;
427+
fn create(pool: &mut Self::PoolType) -> Result<Self, ()> {
428+
unsafe {
429+
let raw = dpdk_sys::rte_pktmbuf_alloc(pool.inner().as_mut_ptr());
430+
Ok(Self {
431+
raw: NonNull::new(raw).ok_or_else(|| error!("Mbuf allocation failure"))?,
432+
marker: PhantomData,
433+
})
434+
}
435+
}
436+
}
426437
impl Headroom for Mbuf {
427438
fn headroom(&self) -> u16 {
428439
unsafe { rte_pktmbuf_headroom(self.raw.as_ptr()) }

net/src/buffer/mod.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@ impl<T> PacketBuffer for T where T: AsRef<[u8]> + Headroom + Debug + 'static {}
1919

2020
/// Super trait representing the abstract operations which may be performed on mutable a packet buffer.
2121
pub trait PacketBufferMut:
22-
PacketBuffer + AsMut<[u8]> + Prepend + Send + TrimFromStart + TrimFromEnd + Headroom + Tailroom
22+
PacketBuffer
23+
+ AsMut<[u8]>
24+
+ Prepend
25+
+ Send
26+
+ TrimFromStart
27+
+ TrimFromEnd
28+
+ Headroom
29+
+ Tailroom
30+
+ Create
2331
{
2432
}
2533
impl<T> PacketBufferMut for T where
@@ -31,9 +39,20 @@ impl<T> PacketBufferMut for T where
3139
+ TrimFromEnd
3240
+ Headroom
3341
+ Tailroom
42+
+ Create
3443
{
3544
}
3645

46+
/// Trait representing the ability to create a packet buffer
47+
pub trait Create {
48+
/// Type of auxiliary object to allocate buffers from
49+
type PoolType;
50+
/// Allocate a packet buffer
51+
fn create(pool: &mut Self::PoolType) -> Result<Self, ()>
52+
where
53+
Self: Sized;
54+
}
55+
3756
/// Trait representing the ability to get the unused headroom in a packet buffer.
3857
pub trait Headroom {
3958
/// Get the (unused) headroom in a packet buffer.

net/src/buffer/test_buffer.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
pub use contract::*;
88

99
use crate::buffer::{
10-
Append, Headroom, MemoryBufferNotLongEnough, NotEnoughHeadRoom, NotEnoughTailRoom, Prepend,
11-
Tailroom, TrimFromEnd, TrimFromStart,
10+
Append, Create, Headroom, MemoryBufferNotLongEnough, NotEnoughHeadRoom, NotEnoughTailRoom,
11+
Prepend, Tailroom, TrimFromEnd, TrimFromStart,
1212
};
1313
use tracing::trace;
1414

@@ -103,6 +103,13 @@ impl AsMut<[u8]> for TestBuffer {
103103
}
104104
}
105105

106+
impl Create for TestBuffer {
107+
type PoolType = ();
108+
fn create(_: &mut Self::PoolType) -> Result<Self, ()> {
109+
Ok(Self::new())
110+
}
111+
}
112+
106113
impl Headroom for TestBuffer {
107114
fn headroom(&self) -> u16 {
108115
self.headroom

0 commit comments

Comments
 (0)