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
use bitflags::bitflags;
use libbpf_sys::{
xsk_socket_config, XSK_RING_CONS__DEFAULT_NUM_DESCS, XSK_RING_PROD__DEFAULT_NUM_DESCS,
};
use std::{
convert::{TryFrom, TryInto},
ffi::{CStr, CString, NulError},
str::FromStr,
};
use super::QueueSize;
bitflags! {
pub struct LibbpfFlags: u32 {
const XSK_LIBBPF_FLAGS_INHIBIT_PROG_LOAD = 1;
}
}
bitflags! {
pub struct XdpFlags: u32 {
const XDP_FLAGS_UPDATE_IF_NOEXIST = 1;
const XDP_FLAGS_SKB_MODE = 2;
const XDP_FLAGS_DRV_MODE = 4;
const XDP_FLAGS_HW_MODE = 8;
}
}
bitflags! {
pub struct BindFlags: u16 {
const XDP_COPY = 2;
const XDP_ZEROCOPY = 4;
const XDP_USE_NEED_WAKEUP = 8;
}
}
#[derive(Debug, Clone)]
pub struct Interface(CString);
impl Interface {
pub fn new(name: CString) -> Self {
Self(name)
}
pub(crate) fn as_cstr(&self) -> &CStr {
&self.0
}
}
impl FromStr for Interface {
type Err = NulError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
s.as_bytes().try_into()
}
}
impl TryFrom<&[u8]> for Interface {
type Error = NulError;
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
CString::new(bytes).map(Self)
}
}
impl TryFrom<Vec<u8>> for Interface {
type Error = NulError;
fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
CString::new(bytes).map(Self)
}
}
#[derive(Debug, Default, Clone, Copy)]
pub struct ConfigBuilder {
config: Config,
}
impl ConfigBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn rx_queue_size(&mut self, size: QueueSize) -> &mut Self {
self.config.rx_queue_size = size;
self
}
pub fn tx_queue_size(&mut self, size: QueueSize) -> &mut Self {
self.config.tx_queue_size = size;
self
}
pub fn libbpf_flags(&mut self, flags: LibbpfFlags) -> &mut Self {
self.config.libbpf_flags = flags;
self
}
pub fn xdp_flags(&mut self, flags: XdpFlags) -> &mut Self {
self.config.xdp_flags = flags;
self
}
pub fn bind_flags(&mut self, flags: BindFlags) -> &mut Self {
self.config.bind_flags = flags;
self
}
pub fn build(&self) -> Config {
self.config
}
}
#[derive(Debug, Clone, Copy)]
pub struct Config {
rx_queue_size: QueueSize,
tx_queue_size: QueueSize,
libbpf_flags: LibbpfFlags,
xdp_flags: XdpFlags,
bind_flags: BindFlags,
}
impl Config {
pub fn builder() -> ConfigBuilder {
ConfigBuilder::new()
}
pub fn rx_queue_size(&self) -> QueueSize {
self.rx_queue_size
}
pub fn tx_queue_size(&self) -> QueueSize {
self.tx_queue_size
}
pub fn libbpf_flags(&self) -> &LibbpfFlags {
&self.libbpf_flags
}
pub fn xdp_flags(&self) -> &XdpFlags {
&self.xdp_flags
}
pub fn bind_flags(&self) -> &BindFlags {
&self.bind_flags
}
}
impl Default for Config {
fn default() -> Self {
Self {
rx_queue_size: QueueSize(XSK_RING_CONS__DEFAULT_NUM_DESCS),
tx_queue_size: QueueSize(XSK_RING_PROD__DEFAULT_NUM_DESCS),
libbpf_flags: LibbpfFlags::empty(),
xdp_flags: XdpFlags::empty(),
bind_flags: BindFlags::empty(),
}
}
}
impl From<Config> for xsk_socket_config {
fn from(c: Config) -> Self {
xsk_socket_config {
rx_size: c.rx_queue_size.get(),
tx_size: c.tx_queue_size.get(),
xdp_flags: c.xdp_flags.bits(),
bind_flags: c.bind_flags.bits(),
libbpf_flags: c.libbpf_flags.bits(),
__bindgen_padding_0: Default::default(),
}
}
}