aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/liburing/test/zcrx.c
blob: e69a95cf051d255f56d2ab6da0b39d357e196939 (plain) (blame)
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
/*
 * Simple test case showing using send and recv through io_uring
 */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <pthread.h>
#include <net/if.h>

#include "liburing.h"
#include "helpers.h"

static unsigned int ifidx, rxq;

static long page_size;

/* the hw rxq must consume 128 of these pages, leaving 4 left */
#define AREA_PAGES	132
#define AREA_SZ		(AREA_PAGES * page_size)
#define RQ_ENTRIES	128
/* this is one more than the # of free pages after filling hw rxq */
#define LOOP_COUNT	5
#define DEV_ENV_VAR	"NETIF"
#define RXQ_ENV_VAR	"NETRXQ"
#define RING_FLAGS	(IORING_SETUP_DEFER_TASKRUN | \
			 IORING_SETUP_CQE32 | \
			 IORING_SETUP_SINGLE_ISSUER)

static char str[] = "iv5t4dl500w7wsrf14fsuq8thptto0z7i2q62z1p8dwrv5u4kaxpqhm2rb7bapddi5gfkh7f9695eh46t2o5yap2y43gstbsq3n90bg1i7zx1m4wojoqbuxhsrw4s4y3sh9qp57ovbaa2o9yaqa7d4to2vak1otvgkoxs5t0ovjbe6roginrjeh906kmjn1289jlho9a1bud02ex4xr3cvfcybpl6axnr117p0aesb3070wlvj91en7tpf8nyb1e";

#define MSG_SIZE 512

#define PORT	10202
#define HOST	"127.0.0.1"

static int probe_zcrx(void *area)
{
	struct io_uring_zcrx_area_reg area_reg = {
		.addr = (__u64)(unsigned long)area,
		.len = AREA_SZ,
		.flags = 0,
	};
	struct io_uring_zcrx_ifq_reg reg = {
		.if_idx = ifidx,
		.if_rxq = rxq,
		.rq_entries = RQ_ENTRIES,
		.area_ptr = (__u64)(unsigned long)&area_reg,
	};
	struct io_uring ring;
	int ret;

	ret = t_create_ring(8, &ring, RING_FLAGS);
	if (ret == T_SETUP_SKIP) {
		fprintf(stderr, "required ring flags are not supported, skip\n");
		return T_EXIT_SKIP;
	}
	if (ret) {
		fprintf(stderr, "probe ring setup failure\n");
		return T_EXIT_FAIL;
	}

	ret = io_uring_register_ifq(&ring, &reg);
	if (ret == -EINVAL) {
		fprintf(stderr, "zcrx is not supported, skip\n");
		return T_EXIT_SKIP;
	}
	if (ret) {
		fprintf(stderr, "probe zcrx register fail %i\n", ret);
		return T_EXIT_FAIL;
	}
	io_uring_queue_exit(&ring);
	return T_EXIT_PASS;
}

static int try_register_ifq(struct io_uring_zcrx_ifq_reg *reg)
{
	struct io_uring ring;
	int ret;

	ret = t_create_ring(8, &ring, RING_FLAGS);
	if (ret != T_SETUP_OK) {
		fprintf(stderr, "ring create failed: %d\n", ret);
		exit(T_EXIT_FAIL);
	}

	ret = io_uring_register_ifq(&ring, reg);
	io_uring_queue_exit(&ring);
	return ret;
}

static int test_invalid_if(void *area)
{
	int ret;
	struct io_uring_zcrx_area_reg area_reg = {
		.addr = (__u64)(unsigned long)area,
		.len = AREA_SZ,
		.flags = 0,
	};
	struct io_uring_zcrx_ifq_reg reg = {
		.if_idx = -1,
		.if_rxq = rxq,
		.rq_entries = RQ_ENTRIES,
		.area_ptr = (__u64)(unsigned long)&area_reg,
	};

	ret = try_register_ifq(&reg);
	if (ret != -EINVAL && ret != -ENODEV) {
		fprintf(stderr, "registered invalid IF %i\n", ret);
		return T_EXIT_FAIL;
	}

	reg.if_idx = ifidx;
	reg.if_rxq = -1;

	ret = try_register_ifq(&reg);
	if (ret != -EINVAL) {
		fprintf(stderr, "registered invalid IFQ %i\n", ret);
		return T_EXIT_FAIL;
	}
	return T_EXIT_PASS;
}

static int test_invalid_ifq_collision(void *area)
{
	struct io_uring ring, ring2;
	struct io_uring_zcrx_area_reg area_reg = {
		.addr = (__u64)(unsigned long)area,
		.len = AREA_SZ,
		.flags = 0,
	};
	struct io_uring_zcrx_ifq_reg reg = {
		.if_idx = ifidx,
		.if_rxq = rxq,
		.rq_entries = RQ_ENTRIES,
		.area_ptr = (__u64)(unsigned long)&area_reg,
	};
	int ret;

	ret = t_create_ring(8, &ring, RING_FLAGS);
	if (ret != T_SETUP_OK) {
		fprintf(stderr, "ring create failed: %d\n", ret);
		return T_EXIT_FAIL;
	}
	ret = t_create_ring(8, &ring2, RING_FLAGS);
	if (ret != T_SETUP_OK) {
		fprintf(stderr, "ring2 create failed: %d\n", ret);
		return T_EXIT_FAIL;
	}

	ret = io_uring_register_ifq(&ring, &reg);
	if (ret) {
		fprintf(stderr, "initial registration failed %i\n", ret);
		return T_EXIT_FAIL;
	}

	/* register taken ifq */
	ret = io_uring_register_ifq(&ring, &reg);
	if (!ret) {
		fprintf(stderr, "registered taken queue\n");
		return T_EXIT_FAIL;
	}

	ret = io_uring_register_ifq(&ring2, &reg);
	if (!ret) {
		fprintf(stderr, "registered taken queue ring2\n");
		return T_EXIT_FAIL;
	}

	io_uring_queue_exit(&ring);
	io_uring_queue_exit(&ring2);
	return T_EXIT_PASS;
}

static int test_rq_setup(void *area)
{
	int ret;
	struct io_uring_zcrx_area_reg area_reg = {
		.addr = (__u64)(unsigned long)area,
		.len = AREA_SZ,
		.flags = 0,
	};

	struct io_uring_zcrx_ifq_reg reg = {
		.if_idx = ifidx,
		.if_rxq = rxq,
		.rq_entries = 0,
		.area_ptr = (__u64)(unsigned long)&area_reg,
	};

	ret = try_register_ifq(&reg);
	if (ret != -EINVAL) {
		fprintf(stderr, "registered 0 rq entries\n");
		return T_EXIT_FAIL;
	}

	reg.rq_entries = (__u32)-1;

	ret = try_register_ifq(&reg);
	if (ret != -EINVAL) {
		fprintf(stderr, "registered unlimited nr of rq entries\n");
		return T_EXIT_FAIL;
	}

	reg.rq_entries = RQ_ENTRIES - 1;
	ret = try_register_ifq(&reg);
	if (ret != 0) {
		fprintf(stderr, "ifq registration failed %i\n", ret);
		return T_EXIT_FAIL;
	}

	if (reg.rq_entries == RQ_ENTRIES - 1) {
		fprintf(stderr, "registered non pow2 refill entries %i\n",
			reg.rq_entries);
		return T_EXIT_FAIL;
	}

	return T_EXIT_PASS;
}

static int test_null_area_reg_struct(void)
{
	int ret;

	struct io_uring_zcrx_ifq_reg reg = {
		.if_idx = ifidx,
		.if_rxq = rxq,
		.rq_entries = RQ_ENTRIES,
		.area_ptr = (__u64)(unsigned long)0,
	};

	ret = try_register_ifq(&reg);
	return ret ? T_EXIT_PASS : T_EXIT_FAIL;
}

static int test_null_area(void)
{
	int ret;

	struct io_uring_zcrx_area_reg area_reg = {
		.addr = (__u64)(unsigned long)0,
		.len = AREA_SZ,
		.flags = 0,
	};

	struct io_uring_zcrx_ifq_reg reg = {
		.if_idx = ifidx,
		.if_rxq = rxq,
		.rq_entries = RQ_ENTRIES,
		.area_ptr = (__u64)(unsigned long)&area_reg,
	};

	ret = try_register_ifq(&reg);
	return ret ? T_EXIT_PASS : T_EXIT_FAIL;
}

static int test_misaligned_area(void *area)
{
	int ret;
	struct io_uring_zcrx_area_reg area_reg = {
		.addr = (__u64)(unsigned long)(area + 1),
		.len = AREA_SZ,
		.flags = 0,
	};

	struct io_uring_zcrx_ifq_reg reg = {
		.if_idx = ifidx,
		.if_rxq = rxq,
		.rq_entries = RQ_ENTRIES,
		.area_ptr = (__u64)(unsigned long)&area_reg,
	};

	if (!try_register_ifq(&reg))
		return T_EXIT_FAIL;

	area_reg.addr = (__u64)(unsigned long)area;
	area_reg.len = AREA_SZ - 1;
	ret = try_register_ifq(&reg);
	return ret ? T_EXIT_PASS : T_EXIT_FAIL;
}

static int test_larger_than_alloc_area(void *area)
{
	int ret;
	struct io_uring_zcrx_area_reg area_reg = {
		.addr = (__u64)(unsigned long)area,
		.len = AREA_SZ + 4096,
		.flags = 0,
	};

	struct io_uring_zcrx_ifq_reg reg = {
		.if_idx = ifidx,
		.if_rxq = rxq,
		.rq_entries = RQ_ENTRIES,
		.area_ptr = (__u64)(unsigned long)&area_reg,
	};

	ret = try_register_ifq(&reg);
	return ret ? T_EXIT_PASS : T_EXIT_FAIL;
}

static int test_area_access(void)
{
	struct io_uring_zcrx_area_reg area_reg = {
		.len = AREA_SZ,
		.flags = 0,
	};
	struct io_uring_zcrx_ifq_reg reg = {
		.if_idx = ifidx,
		.if_rxq = rxq,
		.rq_entries = RQ_ENTRIES,
		.area_ptr = (__u64)(unsigned long)&area_reg,
	};
	int i, ret;
	void *area;

	for (i = 0; i < 2; i++) {
		int ro = i & 1;
		int prot = ro ? PROT_READ : PROT_WRITE;

		area = mmap(NULL, AREA_SZ, prot,
			    MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
		if (area == MAP_FAILED) {
			perror("mmap");
			return T_EXIT_FAIL;
		}

		area_reg.addr = (__u64)(unsigned long)area;

		ret = try_register_ifq(&reg);
		if (ret != -EFAULT) {
			fprintf(stderr, "registered unaccessible memory\n");
			return T_EXIT_FAIL;
		}

		munmap(area, AREA_SZ);
	}

	return T_EXIT_PASS;
}

static int create_ring_with_ifq(struct io_uring *ring, void *area, __u32 *id)
{
	struct io_uring_zcrx_area_reg area_reg = {
		.addr = (__u64)(unsigned long)area,
		.len = AREA_SZ,
		.flags = 0,
	};
	struct io_uring_zcrx_ifq_reg reg = {
		.if_idx = ifidx,
		.if_rxq = rxq,
		.rq_entries = RQ_ENTRIES,
		.area_ptr = (__u64)(unsigned long)&area_reg,
	};
	int ret;

	ret = t_create_ring(128, ring, RING_FLAGS);
	if (ret != T_SETUP_OK) {
		fprintf(stderr, "ring create failed: %d\n", ret);
		return T_EXIT_FAIL;
	}

	ret = io_uring_register_ifq(ring, &reg);
	if (ret) {
		io_uring_queue_exit(ring);
		fprintf(stderr, "ifq register failed %d\n", ret);
		return T_EXIT_FAIL;
	}
	*id = reg.zcrx_id;
	return 0;
}

static void test_io_uring_prep_zcrx(struct io_uring_sqe *sqe, int fd, int ifq)
{
	io_uring_prep_rw(IORING_OP_RECV_ZC, sqe, fd, NULL, 0, 0);
	sqe->zcrx_ifq_idx = ifq;
	sqe->ioprio |= IORING_RECV_MULTISHOT;
}

static struct io_uring_cqe *submit_and_wait_one(struct io_uring *ring)
{
	struct io_uring_cqe *cqe;
	int ret;

	ret = io_uring_submit(ring);
	if (ret != 1) {
		fprintf(stderr, "sqe submit failed: %d\n", ret);
		return NULL;
	}

	ret = io_uring_wait_cqe(ring, &cqe);
	if (ret < 0) {
		fprintf(stderr, "wait completion %d\n", ret);
		return NULL;
	}

	return cqe;
}

static int test_invalid_zcrx_request(void *area)
{
	struct io_uring_cqe *cqe;
	struct io_uring_sqe *sqe;
	struct io_uring ring;
	__u32 zcrx_id;
	int ret, fds[2];

	ret = create_ring_with_ifq(&ring, area, &zcrx_id);
	if (ret != T_SETUP_OK) {
		fprintf(stderr, "ifq-ring create failed: %d\n", ret);
		return T_EXIT_FAIL;
	}

	ret = t_create_socket_pair(fds, true);
	if (ret) {
		fprintf(stderr, "t_create_socket_pair failed: %d\n", ret);
		return ret;
	}

	/* invalid file */
	sqe = io_uring_get_sqe(&ring);
	test_io_uring_prep_zcrx(sqe, ring.ring_fd, zcrx_id);

	cqe = submit_and_wait_one(&ring);
	if (!cqe) {
		fprintf(stderr, "submit_and_wait_one failed\n");
		return T_EXIT_FAIL;
	}
	if (cqe->flags & IORING_CQE_F_MORE) {
		fprintf(stderr, "unexpected F_MORE for invalid fd\n");
		return T_EXIT_FAIL;
	}
	if (cqe->res != -ENOTSOCK) {
		fprintf(stderr, "zcrx for non-socket file\n");
		return T_EXIT_FAIL;
	}
	io_uring_cqe_seen(&ring, cqe);

	/* invalid ifq idx */
	sqe = io_uring_get_sqe(&ring);
	test_io_uring_prep_zcrx(sqe, fds[0], zcrx_id + 1);

	cqe = submit_and_wait_one(&ring);
	if (!cqe) {
		fprintf(stderr, "submit_and_wait_one failed\n");
		return T_EXIT_FAIL;
	}
	if (cqe->flags & IORING_CQE_F_MORE) {
		fprintf(stderr, "unexpected F_MORE for invalid fd\n");
		return T_EXIT_FAIL;
	}
	if (cqe->res != -EINVAL) {
		fprintf(stderr, "zcrx recv with non-existent zcrx ifq\n");
		return T_EXIT_FAIL;
	}
	io_uring_cqe_seen(&ring, cqe);

	close(fds[0]);
	close(fds[1]);
	io_uring_queue_exit(&ring);
	return 0;
}

struct recv_data {
	pthread_barrier_t connect;
	pthread_barrier_t startup;
	pthread_barrier_t barrier;
	pthread_barrier_t finish;

	int accept_fd;
	char buf[MSG_SIZE];
	void *area;
	void *ring_ptr;
	unsigned int ring_sz;
	struct io_uring_zcrx_rq rq_ring;
};

static int recv_prep(struct io_uring *ring, struct recv_data *rd, int *sock,
		     __u32 zcrx_id)
{
	struct sockaddr_in saddr;
	struct io_uring_sqe *sqe;
	int sockfd, ret, val, use_fd;
	socklen_t socklen;

	memset(&saddr, 0, sizeof(saddr));
	saddr.sin_family = AF_INET;
	saddr.sin_addr.s_addr = htonl(INADDR_ANY);
	saddr.sin_port = htons(PORT);

	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (sockfd < 0) {
		perror("socket");
		return 1;
	}

	val = 1;
	setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));

	ret = bind(sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
	if (ret < 0) {
		perror("bind");
		goto err;
	}

	ret = listen(sockfd, 1);
	if (ret < 0) {
		perror("listen");
		goto err;
	}

	pthread_barrier_wait(&rd->connect);

	socklen = sizeof(saddr);
	use_fd = accept(sockfd, (struct sockaddr *)&saddr, &socklen);
	if (use_fd < 0) {
		perror("accept");
		goto err;
	}

	rd->accept_fd = use_fd;
	pthread_barrier_wait(&rd->startup);
	pthread_barrier_wait(&rd->barrier);

	sqe = io_uring_get_sqe(ring);
	io_uring_prep_rw(IORING_OP_RECV_ZC, sqe, use_fd, NULL, 0, 0);
	sqe->zcrx_ifq_idx = zcrx_id;
	sqe->ioprio |= IORING_RECV_MULTISHOT;
	sqe->user_data = 2;

	ret = io_uring_submit(ring);
	if (ret <= 0) {
		fprintf(stderr, "submit failed: %d\n", ret);
		goto err;
	}

	*sock = sockfd;
	return 0;
err:
	close(sockfd);
	return 1;
}

static struct io_uring_zcrx_rqe* get_refill_entry(struct io_uring_zcrx_rq *rq_ring)
{
	unsigned mask = rq_ring->ring_entries - 1;
	struct io_uring_zcrx_rqe* rqe;

	rqe = &rq_ring->rqes[rq_ring->rq_tail & mask];
	rq_ring->rq_tail++;
	return rqe;
}

static void refill_garbage(struct recv_data *rd, uint64_t area_token)
{
	struct io_uring_zcrx_rq *rq_ring = &rd->rq_ring;
	struct io_uring_zcrx_rqe* rqe;
	int i = 0;

	/* invalid area */
	rqe = get_refill_entry(rq_ring);
	rqe->off = (area_token + 1) << IORING_ZCRX_AREA_SHIFT;
	i++;

	/* invalid area offset */
	rqe = get_refill_entry(rq_ring);
	rqe->off = AREA_SZ | (area_token << IORING_ZCRX_AREA_SHIFT);
	rqe->off += AREA_SZ;
	i++;

	for (; i < rq_ring->ring_entries; i++) {
		rqe = get_refill_entry(rq_ring);
		rqe->off = ((uint64_t)1 << IORING_ZCRX_AREA_SHIFT) - 1;
	}

	io_uring_smp_store_release(rq_ring->ktail, rq_ring->rq_tail);
}

static int do_recv(struct io_uring *ring, struct recv_data *rd,
		   uint64_t refill_area_token)
{
	struct io_uring_cqe *cqe;
	struct io_uring_zcrx_cqe *zcqe;
	int i, ret;

	refill_garbage(rd, refill_area_token);

	for (i = 0; i < LOOP_COUNT - 1; i++) {
		uint64_t off, mask = (1ULL << IORING_ZCRX_AREA_SHIFT) - 1;
		void *addr;

		ret = io_uring_wait_cqe(ring, &cqe);
		if (ret) {
			fprintf(stdout, "wait_cqe: %d\n", ret);
			return 1;
		}
		if (cqe->res == -EINVAL) {
			fprintf(stdout, "recv not supported, skipping\n");
			goto out;
		}
		if (cqe->res < 0) {
			fprintf(stderr, "failed recv cqe: %d\n", cqe->res);
			goto err;
		}
		if (cqe->res - 1 != strlen(str)) {
			fprintf(stderr, "got wrong length: %d/%d\n", cqe->res,
								(int) strlen(str) + 1);
			goto err;
		}

		zcqe = (struct io_uring_zcrx_cqe *)(cqe + 1);
		off = zcqe->off & mask;
		addr = (char *) rd->area + off;
		ret = strncmp(str, addr, sizeof(str));
		if (ret != 0) {
			fprintf(stderr, "recv incorrect payload: %s\n", (const char *)addr);
			goto err;
		}

		io_uring_cqe_seen(ring, cqe);
	}

	ret = io_uring_wait_cqe(ring, &cqe);
	if (ret) {
		fprintf(stdout, "wait_cqe: %d\n", ret);
		return 1;
	}
	if (cqe->res != -ENOMEM) {
		fprintf(stdout, "final recv cqe did not return ENOMEM\n");
		goto err;
	}

out:
	io_uring_cqe_seen(ring, cqe);
	pthread_barrier_wait(&rd->finish);
	return 0;
err:
	io_uring_cqe_seen(ring, cqe);
	pthread_barrier_wait(&rd->finish);
	return 1;
}

static void *recv_fn(void *data)
{
	struct recv_data *rd = data;
	struct io_uring_params p = { };
	struct io_uring ring;
	int ret, sock;
	struct io_uring_zcrx_area_reg area_reg = {
		.addr = (__u64)(unsigned long)rd->area,
		.len = AREA_SZ,
		.flags = 0,
	};
	struct io_uring_zcrx_ifq_reg reg = {
		.if_idx = ifidx,
		.if_rxq = rxq,
		.rq_entries = RQ_ENTRIES,
		.area_ptr = (__u64)(unsigned long)&area_reg,
	};

	p.flags = RING_FLAGS;
	ret = t_create_ring_params(8, &ring, &p);
	if (ret == T_SETUP_SKIP) {
		ret = 0;
		goto err;
	} else if (ret < 0) {
		goto err;
	}

	ret = io_uring_register_ifq(&ring, &reg);
	if (ret != 0) {
		fprintf(stderr, "register_ifq failed: %d\n", ret);
		goto err_ring_exit;
	}

	/*
	rd->ring_ptr = mmap(
		0,
		reg.offsets.mmap_sz,
		PROT_READ | PROT_WRITE,
		MAP_SHARED | MAP_POPULATE,
		ring.enter_ring_fd,
		IORING_OFF_RQ_RING
	);

	rd->ring_sz = reg.offsets.mmap_sz;
	*/
	rd->rq_ring.khead = (__u32*)((char*)rd->ring_ptr + reg.offsets.head);
	rd->rq_ring.ktail = (__u32*)((char*)rd->ring_ptr + reg.offsets.tail);
	rd->rq_ring.rqes = (struct io_uring_zcrx_rqe*)((char*)rd->ring_ptr + reg.offsets.rqes);
	rd->rq_ring.rq_tail = 0;
	rd->rq_ring.ring_entries = reg.rq_entries;

	ret = recv_prep(&ring, rd, &sock, reg.zcrx_id);
	if (ret) {
		fprintf(stderr, "recv_prep failed: %d\n", ret);
		goto err;
	}
	ret = do_recv(&ring, rd, area_reg.rq_area_token);

	close(sock);
	close(rd->accept_fd);
err_ring_exit:
	io_uring_queue_exit(&ring);
err:
	return (void *)(intptr_t)ret;
}

static int do_send(struct recv_data *rd)
{
	struct sockaddr_in saddr;
	struct iovec iov = {
		.iov_base = str,
		.iov_len = sizeof(str),
	};
	struct io_uring ring;
	struct io_uring_cqe *cqe;
	struct io_uring_sqe *sqe;
	int i, sockfd, ret;

	ret = io_uring_queue_init(8, &ring, 0);
	if (ret) {
		fprintf(stderr, "queue init failed: %d\n", ret);
		return 1;
	}

	memset(&saddr, 0, sizeof(saddr));
	saddr.sin_family = AF_INET;
	saddr.sin_port = htons(PORT);
	inet_pton(AF_INET, HOST, &saddr.sin_addr);

	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (sockfd < 0) {
		perror("socket");
		goto err2;
	}

	pthread_barrier_wait(&rd->connect);

	ret = connect(sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
	if (ret < 0) {
		perror("connect");
		goto err;
	}

	pthread_barrier_wait(&rd->startup);

	for (i = 0; i < LOOP_COUNT; i++) {
		sqe = io_uring_get_sqe(&ring);
		io_uring_prep_send(sqe, sockfd, iov.iov_base, iov.iov_len, 0);
		sqe->user_data = 1;
	}

	ret = io_uring_submit(&ring);
	if (ret <= 0) {
		fprintf(stderr, "submit failed: %d\n", ret);
		goto err;
	}

	pthread_barrier_wait(&rd->barrier);

	ret = io_uring_wait_cqe(&ring, &cqe);
	if (cqe->res == -EINVAL) {
		fprintf(stdout, "send not supported, skipping\n");
		goto err;
	}
	if (cqe->res != iov.iov_len) {
		fprintf(stderr, "failed cqe: %d\n", cqe->res);
		goto err;
	}

	pthread_barrier_wait(&rd->finish);

	close(sockfd);
	io_uring_queue_exit(&ring);
	return 0;

err:
	close(sockfd);
err2:
	io_uring_queue_exit(&ring);
	pthread_barrier_wait(&rd->finish);
	return 1;
}

static int test_recv(void *area)
{
	pthread_t recv_thread;
	struct recv_data rd;
	int ret;
	void *retval;

	memset(&rd, 0, sizeof(rd));
	pthread_barrier_init(&rd.connect, NULL, 2);
	pthread_barrier_init(&rd.startup, NULL, 2);
	pthread_barrier_init(&rd.barrier, NULL, 2);
	pthread_barrier_init(&rd.finish, NULL, 2);
	rd.area = area;

	ret = pthread_create(&recv_thread, NULL, recv_fn, &rd);
	if (ret) {
		fprintf(stderr, "Thread create failed: %d\n", ret);
		return 1;
	}

	do_send(&rd);
	pthread_join(recv_thread, &retval);
	return (intptr_t)retval;
}

int main(int argc, char *argv[])
{
	char *dev, *rxq_str, *rxq_end;
	void *area_outer, *area;
	int ret;

	if (argc > 1)
		return T_EXIT_SKIP;

	page_size = sysconf(_SC_PAGESIZE);
	if (page_size < 0) {
		perror("sysconf(_SC_PAGESIZE)");
		return T_EXIT_FAIL;
	}

	area_outer = mmap(NULL, AREA_SZ + 8192, PROT_NONE,
		MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE, -1, 0);
	if (area_outer == MAP_FAILED) {
		perror("mmap");
		return T_EXIT_FAIL;
	}

	area = mmap(area_outer, AREA_SZ, PROT_READ | PROT_WRITE,
			MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
	if (area == MAP_FAILED) {
		perror("mmap");
		return T_EXIT_FAIL;
	}

	dev = getenv(DEV_ENV_VAR);
	if (!dev)
		return T_EXIT_SKIP;

	ifidx = if_nametoindex(dev);
	if (!ifidx)
		return T_EXIT_SKIP;

	rxq_str = getenv(RXQ_ENV_VAR);
	if (!rxq_str)
		return T_EXIT_SKIP;

	rxq = strtol(rxq_str, &rxq_end, 10);
	if (rxq_end == rxq_str || *rxq_end != '\0')
		return T_EXIT_SKIP;

	ret = probe_zcrx(area);
	if (ret != T_EXIT_PASS)
		return ret;

	ret = test_rq_setup(area);
	if (ret) {
		fprintf(stderr, "test_invalid_reg_struct failed\n");
		return ret;
	}

	ret = test_null_area_reg_struct();
	if (ret) {
		fprintf(stderr, "test_null_area_reg_struct failed\n");
		return ret;
	}

	ret = test_null_area();
	if (ret) {
		fprintf(stderr, "test_null_area failed\n");
		return ret;
	}

	ret = test_misaligned_area(area);
	if (ret) {
		fprintf(stderr, "test_misaligned_area failed\n");
		return ret;
	}

	ret = test_larger_than_alloc_area(area);
	if (ret) {
		fprintf(stderr, "test_larger_than_alloc_area failed\n");
		return ret;
	}

	ret = test_area_access();
	if (ret) {
		fprintf(stderr, "test_area_access failed\n");
		return ret;
	}

	ret = test_invalid_if(area);
	if (ret) {
		fprintf(stderr, "test_invalid_if failed\n");
		return ret;
	}

	ret = test_invalid_ifq_collision(area);
	if (ret) {
		fprintf(stderr, "test_invalid_ifq_collision failed\n");
		return ret;
	}

	ret = test_invalid_zcrx_request(area);
	if (ret) {
		fprintf(stderr, "test_invalid_ifq_collision failed\n");
		return ret;
	}

	ret = test_recv(area);
	if (ret) {
		fprintf(stderr, "test_recv failed\n");
		return ret;
	}

	munmap(area, AREA_SZ);
	return T_EXIT_PASS;
}