Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/tools/testing/selftests/drivers/net/hw/iou-zcrx.c
53747 views
1
// SPDX-License-Identifier: GPL-2.0
2
#include <assert.h>
3
#include <errno.h>
4
#include <error.h>
5
#include <fcntl.h>
6
#include <limits.h>
7
#include <stdbool.h>
8
#include <stdint.h>
9
#include <stdio.h>
10
#include <stdlib.h>
11
#include <string.h>
12
#include <unistd.h>
13
14
#include <arpa/inet.h>
15
#include <linux/mman.h>
16
#include <linux/errqueue.h>
17
#include <linux/if_packet.h>
18
#include <linux/ipv6.h>
19
#include <linux/socket.h>
20
#include <linux/sockios.h>
21
#include <net/ethernet.h>
22
#include <net/if.h>
23
#include <netinet/in.h>
24
#include <netinet/ip.h>
25
#include <netinet/ip6.h>
26
#include <netinet/tcp.h>
27
#include <netinet/udp.h>
28
#include <sys/epoll.h>
29
#include <sys/ioctl.h>
30
#include <sys/mman.h>
31
#include <sys/resource.h>
32
#include <sys/socket.h>
33
#include <sys/stat.h>
34
#include <sys/time.h>
35
#include <sys/types.h>
36
#include <sys/un.h>
37
#include <sys/wait.h>
38
39
#include <liburing.h>
40
41
#define SKIP_CODE 42
42
43
struct t_io_uring_zcrx_ifq_reg {
44
__u32 if_idx;
45
__u32 if_rxq;
46
__u32 rq_entries;
47
__u32 flags;
48
49
__u64 area_ptr; /* pointer to struct io_uring_zcrx_area_reg */
50
__u64 region_ptr; /* struct io_uring_region_desc * */
51
52
struct io_uring_zcrx_offsets offsets;
53
__u32 zcrx_id;
54
__u32 rx_buf_len;
55
__u64 __resv[3];
56
};
57
58
static long page_size;
59
#define AREA_SIZE (8192 * page_size)
60
#define SEND_SIZE (512 * 4096)
61
#define min(a, b) \
62
({ \
63
typeof(a) _a = (a); \
64
typeof(b) _b = (b); \
65
_a < _b ? _a : _b; \
66
})
67
#define min_t(t, a, b) \
68
({ \
69
t _ta = (a); \
70
t _tb = (b); \
71
min(_ta, _tb); \
72
})
73
74
#define ALIGN_UP(v, align) (((v) + (align) - 1) & ~((align) - 1))
75
76
static int cfg_server;
77
static int cfg_client;
78
static int cfg_port = 8000;
79
static int cfg_payload_len;
80
static const char *cfg_ifname;
81
static int cfg_queue_id = -1;
82
static bool cfg_oneshot;
83
static int cfg_oneshot_recvs;
84
static int cfg_send_size = SEND_SIZE;
85
static struct sockaddr_in6 cfg_addr;
86
static unsigned int cfg_rx_buf_len;
87
static bool cfg_dry_run;
88
89
static char *payload;
90
static void *area_ptr;
91
static void *ring_ptr;
92
static size_t ring_size;
93
static struct io_uring_zcrx_rq rq_ring;
94
static unsigned long area_token;
95
static int connfd;
96
static bool stop;
97
static size_t received;
98
99
static unsigned long gettimeofday_ms(void)
100
{
101
struct timeval tv;
102
103
gettimeofday(&tv, NULL);
104
return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
105
}
106
107
static int parse_address(const char *str, int port, struct sockaddr_in6 *sin6)
108
{
109
int ret;
110
111
sin6->sin6_family = AF_INET6;
112
sin6->sin6_port = htons(port);
113
114
ret = inet_pton(sin6->sin6_family, str, &sin6->sin6_addr);
115
if (ret != 1) {
116
/* fallback to plain IPv4 */
117
ret = inet_pton(AF_INET, str, &sin6->sin6_addr.s6_addr32[3]);
118
if (ret != 1)
119
return -1;
120
121
/* add ::ffff prefix */
122
sin6->sin6_addr.s6_addr32[0] = 0;
123
sin6->sin6_addr.s6_addr32[1] = 0;
124
sin6->sin6_addr.s6_addr16[4] = 0;
125
sin6->sin6_addr.s6_addr16[5] = 0xffff;
126
}
127
128
return 0;
129
}
130
131
static inline size_t get_refill_ring_size(unsigned int rq_entries)
132
{
133
size_t size;
134
135
ring_size = rq_entries * sizeof(struct io_uring_zcrx_rqe);
136
/* add space for the header (head/tail/etc.) */
137
ring_size += page_size;
138
return ALIGN_UP(ring_size, page_size);
139
}
140
141
static void setup_zcrx(struct io_uring *ring)
142
{
143
unsigned int ifindex;
144
unsigned int rq_entries = 4096;
145
int ret;
146
147
ifindex = if_nametoindex(cfg_ifname);
148
if (!ifindex)
149
error(1, 0, "bad interface name: %s", cfg_ifname);
150
151
if (cfg_rx_buf_len && cfg_rx_buf_len != page_size) {
152
area_ptr = mmap(NULL,
153
AREA_SIZE,
154
PROT_READ | PROT_WRITE,
155
MAP_ANONYMOUS | MAP_PRIVATE |
156
MAP_HUGETLB | MAP_HUGE_2MB,
157
-1,
158
0);
159
if (area_ptr == MAP_FAILED) {
160
printf("Can't allocate huge pages\n");
161
exit(SKIP_CODE);
162
}
163
} else {
164
area_ptr = mmap(NULL,
165
AREA_SIZE,
166
PROT_READ | PROT_WRITE,
167
MAP_ANONYMOUS | MAP_PRIVATE,
168
0,
169
0);
170
if (area_ptr == MAP_FAILED)
171
error(1, 0, "mmap(): zero copy area");
172
}
173
174
ring_size = get_refill_ring_size(rq_entries);
175
ring_ptr = mmap(NULL,
176
ring_size,
177
PROT_READ | PROT_WRITE,
178
MAP_ANONYMOUS | MAP_PRIVATE,
179
0,
180
0);
181
182
struct io_uring_region_desc region_reg = {
183
.size = ring_size,
184
.user_addr = (__u64)(unsigned long)ring_ptr,
185
.flags = IORING_MEM_REGION_TYPE_USER,
186
};
187
188
struct io_uring_zcrx_area_reg area_reg = {
189
.addr = (__u64)(unsigned long)area_ptr,
190
.len = AREA_SIZE,
191
.flags = 0,
192
};
193
194
struct t_io_uring_zcrx_ifq_reg reg = {
195
.if_idx = ifindex,
196
.if_rxq = cfg_queue_id,
197
.rq_entries = rq_entries,
198
.area_ptr = (__u64)(unsigned long)&area_reg,
199
.region_ptr = (__u64)(unsigned long)&region_reg,
200
.rx_buf_len = cfg_rx_buf_len,
201
};
202
203
ret = io_uring_register_ifq(ring, (void *)&reg);
204
if (cfg_rx_buf_len && (ret == -EINVAL || ret == -EOPNOTSUPP ||
205
ret == -ERANGE)) {
206
printf("Large chunks are not supported %i\n", ret);
207
exit(SKIP_CODE);
208
} else if (ret) {
209
error(1, 0, "io_uring_register_ifq(): %d", ret);
210
}
211
212
rq_ring.khead = (unsigned int *)((char *)ring_ptr + reg.offsets.head);
213
rq_ring.ktail = (unsigned int *)((char *)ring_ptr + reg.offsets.tail);
214
rq_ring.rqes = (struct io_uring_zcrx_rqe *)((char *)ring_ptr + reg.offsets.rqes);
215
rq_ring.rq_tail = 0;
216
rq_ring.ring_entries = reg.rq_entries;
217
218
area_token = area_reg.rq_area_token;
219
}
220
221
static void add_accept(struct io_uring *ring, int sockfd)
222
{
223
struct io_uring_sqe *sqe;
224
225
sqe = io_uring_get_sqe(ring);
226
227
io_uring_prep_accept(sqe, sockfd, NULL, NULL, 0);
228
sqe->user_data = 1;
229
}
230
231
static void add_recvzc(struct io_uring *ring, int sockfd)
232
{
233
struct io_uring_sqe *sqe;
234
235
sqe = io_uring_get_sqe(ring);
236
237
io_uring_prep_rw(IORING_OP_RECV_ZC, sqe, sockfd, NULL, 0, 0);
238
sqe->ioprio |= IORING_RECV_MULTISHOT;
239
sqe->user_data = 2;
240
}
241
242
static void add_recvzc_oneshot(struct io_uring *ring, int sockfd, size_t len)
243
{
244
struct io_uring_sqe *sqe;
245
246
sqe = io_uring_get_sqe(ring);
247
248
io_uring_prep_rw(IORING_OP_RECV_ZC, sqe, sockfd, NULL, len, 0);
249
sqe->ioprio |= IORING_RECV_MULTISHOT;
250
sqe->user_data = 2;
251
}
252
253
static void process_accept(struct io_uring *ring, struct io_uring_cqe *cqe)
254
{
255
if (cqe->res < 0)
256
error(1, 0, "accept()");
257
if (connfd)
258
error(1, 0, "Unexpected second connection");
259
260
connfd = cqe->res;
261
if (cfg_oneshot)
262
add_recvzc_oneshot(ring, connfd, page_size);
263
else
264
add_recvzc(ring, connfd);
265
}
266
267
static void process_recvzc(struct io_uring *ring, struct io_uring_cqe *cqe)
268
{
269
unsigned rq_mask = rq_ring.ring_entries - 1;
270
struct io_uring_zcrx_cqe *rcqe;
271
struct io_uring_zcrx_rqe *rqe;
272
struct io_uring_sqe *sqe;
273
uint64_t mask;
274
char *data;
275
ssize_t n;
276
int i;
277
278
if (cqe->res == 0 && cqe->flags == 0 && cfg_oneshot_recvs == 0) {
279
stop = true;
280
return;
281
}
282
283
if (cqe->res < 0)
284
error(1, 0, "recvzc(): %d", cqe->res);
285
286
if (cfg_oneshot) {
287
if (cqe->res == 0 && cqe->flags == 0 && cfg_oneshot_recvs) {
288
add_recvzc_oneshot(ring, connfd, page_size);
289
cfg_oneshot_recvs--;
290
}
291
} else if (!(cqe->flags & IORING_CQE_F_MORE)) {
292
add_recvzc(ring, connfd);
293
}
294
295
rcqe = (struct io_uring_zcrx_cqe *)(cqe + 1);
296
297
n = cqe->res;
298
mask = (1ULL << IORING_ZCRX_AREA_SHIFT) - 1;
299
data = (char *)area_ptr + (rcqe->off & mask);
300
301
for (i = 0; i < n; i++) {
302
if (*(data + i) != payload[(received + i)])
303
error(1, 0, "payload mismatch at %d", i);
304
}
305
received += n;
306
307
rqe = &rq_ring.rqes[(rq_ring.rq_tail & rq_mask)];
308
rqe->off = (rcqe->off & ~IORING_ZCRX_AREA_MASK) | area_token;
309
rqe->len = cqe->res;
310
io_uring_smp_store_release(rq_ring.ktail, ++rq_ring.rq_tail);
311
}
312
313
static void server_loop(struct io_uring *ring)
314
{
315
struct io_uring_cqe *cqe;
316
unsigned int count = 0;
317
unsigned int head;
318
int i, ret;
319
320
io_uring_submit_and_wait(ring, 1);
321
322
io_uring_for_each_cqe(ring, head, cqe) {
323
if (cqe->user_data == 1)
324
process_accept(ring, cqe);
325
else if (cqe->user_data == 2)
326
process_recvzc(ring, cqe);
327
else
328
error(1, 0, "unknown cqe");
329
count++;
330
}
331
io_uring_cq_advance(ring, count);
332
}
333
334
static void run_server(void)
335
{
336
unsigned int flags = 0;
337
struct io_uring ring;
338
int fd, enable, ret;
339
uint64_t tstop;
340
341
fd = socket(AF_INET6, SOCK_STREAM, 0);
342
if (fd == -1)
343
error(1, 0, "socket()");
344
345
enable = 1;
346
ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int));
347
if (ret < 0)
348
error(1, 0, "setsockopt(SO_REUSEADDR)");
349
350
ret = bind(fd, (struct sockaddr *)&cfg_addr, sizeof(cfg_addr));
351
if (ret < 0)
352
error(1, 0, "bind()");
353
354
if (listen(fd, 1024) < 0)
355
error(1, 0, "listen()");
356
357
flags |= IORING_SETUP_COOP_TASKRUN;
358
flags |= IORING_SETUP_SINGLE_ISSUER;
359
flags |= IORING_SETUP_DEFER_TASKRUN;
360
flags |= IORING_SETUP_SUBMIT_ALL;
361
flags |= IORING_SETUP_CQE32;
362
363
io_uring_queue_init(512, &ring, flags);
364
365
setup_zcrx(&ring);
366
if (cfg_dry_run)
367
return;
368
369
add_accept(&ring, fd);
370
371
tstop = gettimeofday_ms() + 5000;
372
while (!stop && gettimeofday_ms() < tstop)
373
server_loop(&ring);
374
375
if (!stop)
376
error(1, 0, "test failed\n");
377
}
378
379
static void run_client(void)
380
{
381
ssize_t to_send = cfg_send_size;
382
ssize_t sent = 0;
383
ssize_t chunk, res;
384
int fd;
385
386
fd = socket(AF_INET6, SOCK_STREAM, 0);
387
if (fd == -1)
388
error(1, 0, "socket()");
389
390
if (connect(fd, (struct sockaddr *)&cfg_addr, sizeof(cfg_addr)))
391
error(1, 0, "connect()");
392
393
while (to_send) {
394
void *src = &payload[sent];
395
396
chunk = min_t(ssize_t, cfg_payload_len, to_send);
397
res = send(fd, src, chunk, 0);
398
if (res < 0)
399
error(1, 0, "send(): %zd", sent);
400
sent += res;
401
to_send -= res;
402
}
403
404
close(fd);
405
}
406
407
static void usage(const char *filepath)
408
{
409
error(1, 0, "Usage: %s (-4|-6) (-s|-c) -h<server_ip> -p<port> "
410
"-l<payload_size> -i<ifname> -q<rxq_id>", filepath);
411
}
412
413
static void parse_opts(int argc, char **argv)
414
{
415
const int max_payload_len = SEND_SIZE -
416
sizeof(struct ipv6hdr) -
417
sizeof(struct tcphdr) -
418
40 /* max tcp options */;
419
struct sockaddr_in6 *addr6 = (void *) &cfg_addr;
420
char *addr = NULL;
421
int ret;
422
int c;
423
424
if (argc <= 1)
425
usage(argv[0]);
426
cfg_payload_len = max_payload_len;
427
428
while ((c = getopt(argc, argv, "sch:p:l:i:q:o:z:x:d")) != -1) {
429
switch (c) {
430
case 's':
431
if (cfg_client)
432
error(1, 0, "Pass one of -s or -c");
433
cfg_server = 1;
434
break;
435
case 'c':
436
if (cfg_server)
437
error(1, 0, "Pass one of -s or -c");
438
cfg_client = 1;
439
break;
440
case 'h':
441
addr = optarg;
442
break;
443
case 'p':
444
cfg_port = strtoul(optarg, NULL, 0);
445
break;
446
case 'l':
447
cfg_payload_len = strtoul(optarg, NULL, 0);
448
break;
449
case 'i':
450
cfg_ifname = optarg;
451
break;
452
case 'q':
453
cfg_queue_id = strtoul(optarg, NULL, 0);
454
break;
455
case 'o': {
456
cfg_oneshot = true;
457
cfg_oneshot_recvs = strtoul(optarg, NULL, 0);
458
break;
459
}
460
case 'z':
461
cfg_send_size = strtoul(optarg, NULL, 0);
462
break;
463
case 'x':
464
cfg_rx_buf_len = page_size * strtoul(optarg, NULL, 0);
465
break;
466
case 'd':
467
cfg_dry_run = true;
468
break;
469
}
470
}
471
472
if (cfg_server && addr)
473
error(1, 0, "Receiver cannot have -h specified");
474
475
memset(addr6, 0, sizeof(*addr6));
476
addr6->sin6_family = AF_INET6;
477
addr6->sin6_port = htons(cfg_port);
478
addr6->sin6_addr = in6addr_any;
479
if (addr) {
480
ret = parse_address(addr, cfg_port, addr6);
481
if (ret)
482
error(1, 0, "receiver address parse error: %s", addr);
483
}
484
485
if (cfg_payload_len > max_payload_len)
486
error(1, 0, "-l: payload exceeds max (%d)", max_payload_len);
487
}
488
489
int main(int argc, char **argv)
490
{
491
const char *cfg_test = argv[argc - 1];
492
int i;
493
494
page_size = sysconf(_SC_PAGESIZE);
495
if (page_size < 0)
496
return 1;
497
498
if (posix_memalign((void **)&payload, page_size, SEND_SIZE))
499
return 1;
500
501
parse_opts(argc, argv);
502
503
for (i = 0; i < SEND_SIZE; i++)
504
payload[i] = 'a' + (i % 26);
505
506
if (cfg_server)
507
run_server();
508
else if (cfg_client)
509
run_client();
510
511
return 0;
512
}
513
514