-
Notifications
You must be signed in to change notification settings - Fork 0
/
xdp.c
346 lines (315 loc) · 8.15 KB
/
xdp.c
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
#include <linux/if_xdp.h>
#include <linux/if_link.h>
#include <linux/bpf.h>
//#include <bpf/libbpf.h>
#include <xdp/xsk.h>
#include "subr.h"
#include "global.h"
#define XDP_FRAME_SIZE XSK_UMEM__DEFAULT_FRAME_SIZE
#define FRAME_INVALID UINT64_MAX
#define XDP_FRAME_NUM \
(2 * (XSK_RING_CONS__DEFAULT_NUM_DESCS + XSK_RING_PROD__DEFAULT_NUM_DESCS))
struct xdp_queue {
struct xsk_ring_prod xq_fill;
struct xsk_ring_cons xq_comp;
struct xsk_ring_prod xq_tx;
struct xsk_ring_cons xq_rx;
int xq_tx_outstanding;
int xq_fd;
int xq_frame_free;
void *xq_buf;
struct xsk_umem *xq_umem;
struct xsk_socket *xq_xsk;
void *xq_tx_buf;
uint32_t xq_tx_idx;
uint64_t xq_frame[XDP_FRAME_NUM];
};
static uint64_t
alloc_frame(struct xdp_queue *q)
{
uint64_t frame;
if (q->xq_frame_free == 0) {
return FRAME_INVALID;
}
frame = q->xq_frame[--q->xq_frame_free];
q->xq_frame[q->xq_frame_free] = FRAME_INVALID;
return frame;
}
static void
free_frame(struct xdp_queue *q, uint64_t frame)
{
assert(q->xq_frame_free < XDP_FRAME_NUM);
q->xq_frame[q->xq_frame_free++] = frame;
}
static int
get_interface_queue_num(const char *ifname)
{
struct ifreq req;
int fd;
struct ethtool_channels cmd;
fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (fd == -1) {
panic(errno, "Get interface (%s) channels error: socket() failed", ifname);
}
strzcpy(req.ifr_name, ifname, sizeof(req.ifr_name));
req.ifr_data = (void *)&cmd;
cmd.cmd = ETHTOOL_GCHANNELS;
if (ioctl(fd, SIOCETHTOOL, &req) == -1) {
panic(errno, "%s: ioctl(ETHTOOL_GCHANNELS) failed", ifname);
}
close(fd);
return cmd.combined_count + cmd.rx_count;
}
static void
xdp_init_queue(struct xdp_queue *q, const char *ifname, int queue_id)
{
int i, rc, size;
uint32_t idx;
struct xsk_socket_config cfg;
memset(q, 0, sizeof(*q));
size = XDP_FRAME_NUM * XDP_FRAME_SIZE;
if (posix_memalign(&q->xq_buf, getpagesize(), size)) {
panic(errno, "posix_memalign(%d) failed", size);
}
for (i = 0; i < XDP_FRAME_NUM ; ++i) {
q->xq_frame[i] = i * XDP_FRAME_SIZE;
}
q->xq_frame_free = XDP_FRAME_NUM;
size = XDP_FRAME_NUM * XDP_FRAME_SIZE;
rc = xsk_umem__create(&q->xq_umem, q->xq_buf, size, &q->xq_fill, &q->xq_comp, NULL);
if (rc < 0) {
panic(-rc, "xsk_umem__create() failed");
}
memset(&cfg, 0, sizeof(cfg));
cfg.rx_size = XSK_RING_CONS__DEFAULT_NUM_DESCS;
cfg.tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS;
rc = xsk_socket__create(&q->xq_xsk, ifname, queue_id, q->xq_umem,
&q->xq_rx, &q->xq_tx, &cfg);
if (rc < 0) {
panic(-rc, "xsk_socket__create() failed");
}
idx = UINT32_MAX;
rc = xsk_ring_prod__reserve(&q->xq_fill, XSK_RING_PROD__DEFAULT_NUM_DESCS, &idx);
if (rc != XSK_RING_PROD__DEFAULT_NUM_DESCS) {
panic(0, "xsk_ring_prod__reserve() failed");
}
assert(idx != UINT32_MAX);
for (i = 0; i < XSK_RING_PROD__DEFAULT_NUM_DESCS; i++, idx++) {
*xsk_ring_prod__fill_addr(&q->xq_fill, idx) = alloc_frame(q);
}
xsk_ring_prod__submit(&q->xq_fill, XSK_RING_PROD__DEFAULT_NUM_DESCS);
q->xq_fd = xsk_socket__fd(q->xq_xsk);
}
static void
xdp_init_if(struct thread *t)
{
int rc, i, ifindex;
ifindex = if_nametoindex(t->t_ifname);
if (ifindex == 0) {
panic(errno, "if_nametoindex('%s') failed", t->t_ifname);
}
t->t_rss_queue_num = get_interface_queue_num(t->t_ifname);
if (t->t_rss_queue_id < RSS_QUEUE_ID_MAX) {
t->t_xdp_queue_num = 1;
if (t->t_rss_queue_num > 1) {
t->t_rss_key_size = read_rss_key(t->t_ifname, &t->t_rss_key);
}
} else {
t->t_xdp_queue_num = t->t_rss_queue_num;
}
rc = bpf_xdp_query_id(ifindex, 0, &t->t_xdp_prog_id);
if (rc < 0) {
panic(-rc, "bpf_xdp_query_id() failed");
}
t->t_xdp_queues = xmalloc(t->t_xdp_queue_num * sizeof(struct xdp_queue));
if (t->t_rss_queue_id < RSS_QUEUE_ID_MAX) {
xdp_init_queue(&t->t_xdp_queues[0], t->t_ifname, t->t_rss_queue_id);
} else {
for (i = 0; i < t->t_xdp_queue_num; ++i) {
xdp_init_queue(&t->t_xdp_queues[i], t->t_ifname, i);
}
}
for (i = 0; i < t->t_xdp_queue_num; ++i) {
multiplexer_add(t, t->t_xdp_queues[i].xq_fd);
}
}
static void
xdp_init(struct thread *threads, int n_threads)
{
int i;
for (i = 0; i < n_threads; ++i) {
xdp_init_if(threads + i);
}
}
static void *
xdp_get_tx_buf(struct packet *pkt)
{
int i, rc;
void *buf;
uint64_t addr;
struct xdp_queue *q;
for (i = 0; i < current->t_xdp_queue_num; ++i) {
q = current->t_xdp_queues + i;
if (q->xq_tx_buf != NULL) {
buf = q->xq_tx_buf;
q->xq_tx_buf = NULL;
pkt->pkt.idx = q->xq_tx_idx;
pkt->pkt.queue_idx = i;
return buf;
}
if (q->xq_frame_free == 0) {
continue;
}
rc = xsk_ring_prod__reserve(&q->xq_tx, 1, &pkt->pkt.idx);
assert(rc <= 1);
if (rc == 1) {
addr = alloc_frame(q);
xsk_ring_prod__tx_desc(&q->xq_tx, pkt->pkt.idx)->addr = addr;
addr = xsk_umem__add_offset_to_addr(addr);
buf = xsk_umem__get_data(q->xq_buf, addr);
pkt->pkt.queue_idx = i;
return buf;
} else {
multiplexer_pollout(i);
}
}
return NULL;
}
bool
xdp_is_tx_throttled(void)
{
int i;
struct xdp_queue *q;
for (i = 0; i < current->t_xdp_queue_num; ++i) {
if ((multiplexer_get_events(i) & POLLOUT) == 0) {
q = current->t_xdp_queues + i;
if (xsk_prod_nb_free(&q->xq_tx, 1) > 0) {
return false;
} else {
multiplexer_pollout(i);
}
}
}
return true;
}
void
xdp_init_tx_packet(struct packet *pkt)
{
void *buf;
pkt->pkt.len = 0;
buf = xdp_get_tx_buf(pkt);
if (buf == NULL) {
pkt->pkt.buf = pkt->pkt_body;
} else {
pkt->pkt.buf = buf;
}
}
void
xdp_deinit_tx_packet(struct packet *pkt)
{
struct xdp_queue *q;
if (pkt->pkt.buf != pkt->pkt_body && pkt->pkt.buf != NULL) {
q = current->t_xdp_queues + pkt->pkt.queue_idx;
assert(q->xq_tx_buf == NULL);
q->xq_tx_buf = pkt->pkt.buf;
q->xq_tx_idx = pkt->pkt.idx;
pkt->pkt.buf = NULL;
}
}
bool
xdp_tx_packet(struct packet *pkt)
{
void *buf;
struct xdp_queue *q;
assert(pkt->pkt.len);
if (pkt->pkt.buf == pkt->pkt_body) {
buf = xdp_get_tx_buf(pkt);
if (buf == NULL) {
add_pending_packet(pkt);
return false;
}
memcpy(buf, pkt->pkt.buf, pkt->pkt.len);
pkt->pkt.buf = buf;
}
q = current->t_xdp_queues + pkt->pkt.queue_idx;
xsk_ring_prod__tx_desc(&q->xq_tx, pkt->pkt.idx)->len = pkt->pkt.len;
xsk_ring_prod__submit(&q->xq_tx, 1);
q->xq_tx_outstanding++;
pkt->pkt.buf = NULL;
return true;
}
void
xdp_tx(void)
{
int i, j, n;
uint32_t idx;
uint64_t addr;
struct xdp_queue *q;
for (i = 0; i < current->t_xdp_queue_num; ++i) {
q = current->t_xdp_queues + i;
if (q->xq_tx_outstanding == 0) {
continue;
}
sendto(q->xq_fd, NULL, 0, MSG_DONTWAIT, NULL, 0);
idx = UINT32_MAX;
n = xsk_ring_cons__peek(&q->xq_comp, XSK_RING_CONS__DEFAULT_NUM_DESCS, &idx);
if (n <= 0) {
continue;
}
assert(idx != UINT32_MAX);
for (j = 0; j < n; ++j, ++idx) {
addr = *xsk_ring_cons__comp_addr(&q->xq_comp, idx);
free_frame(q, addr);
}
xsk_ring_cons__release(&q->xq_comp, n);
assert(n <= q->xq_tx_outstanding);
q->xq_tx_outstanding -= n;
}
}
int
xdp_rx(int queue_id)
{
int i, n, m, rc, len;
uint32_t idx_rx, idx_fill;
uint64_t addr, frame;
struct xdp_queue *q;
q = current->t_xdp_queues + queue_id;
idx_rx = 0;
n = xsk_ring_cons__peek(&q->xq_rx, XSK_RING_CONS__DEFAULT_NUM_DESCS, &idx_rx);
if (n == 0) {
return 0;
}
for (i = 0; i < n; ++i) {
addr = xsk_ring_cons__rx_desc(&q->xq_rx, idx_rx + i)->addr;
frame = xsk_umem__extract_addr(addr);
addr = xsk_umem__add_offset_to_addr(addr);
len = xsk_ring_cons__rx_desc(&q->xq_rx, idx_rx + i)->len;
io_process(xsk_umem__get_data(q->xq_buf, addr), len);
free_frame(q, frame);
}
xsk_ring_cons__release(&q->xq_rx, n);
m = xsk_prod_nb_free(&q->xq_fill, q->xq_frame_free);
if (m > 0) {
m = MIN(m, q->xq_frame_free);
idx_fill = UINT32_MAX;
rc = xsk_ring_prod__reserve(&q->xq_fill, m, &idx_fill);
assert(rc == m);
assert(idx_fill != UINT32_MAX);
UNUSED(rc);
for (i = 0; i < m; ++i, ++idx_fill) {
frame = alloc_frame(q);
*xsk_ring_prod__fill_addr(&q->xq_fill, idx_fill) = frame;
}
xsk_ring_prod__submit(&q->xq_fill, m);
}
return n;
}
struct transport_ops xdp_ops = {
.tr_io_init_op = xdp_init,
.tr_io_is_tx_throttled_op = xdp_is_tx_throttled,
.tr_io_init_tx_packet_op = xdp_init_tx_packet,
.tr_io_deinit_tx_packet_op = xdp_deinit_tx_packet,
.tr_io_tx_packet_op = xdp_tx_packet,
.tr_io_rx_op = xdp_rx,
.tr_io_tx_op = xdp_tx,
};