xref: /aosp_15_r20/external/liburing/test/drop-submit.c (revision 25da2bea747f3a93b4c30fd9708b0618ef55a0e6)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Description: test IORING_SETUP_SUBMIT_ALL
4  *
5  */
6 #include <errno.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 
11 #include "liburing.h"
12 
test(struct io_uring * ring,int expect_drops)13 static int test(struct io_uring *ring, int expect_drops)
14 {
15 	struct io_uring_sqe *sqe;
16 	char buf[32];
17 	int ret, i;
18 
19 	for (i = 0; i < 4; i++) {
20 		sqe = io_uring_get_sqe(ring);
21 		if (!sqe) {
22 			fprintf(stderr, "get sqe failed\n");
23 			goto err;
24 		}
25 
26 		io_uring_prep_nop(sqe);
27 	}
28 
29 	/* prep two invalid reads, these will fail */
30 	for (i = 0; i < 2; i++) {
31 		sqe = io_uring_get_sqe(ring);
32 		if (!sqe) {
33 			fprintf(stderr, "get sqe failed\n");
34 			goto err;
35 		}
36 
37 		io_uring_prep_read(sqe, 128, buf, sizeof(buf), 0);
38 		sqe->ioprio = (short) -1;
39 	}
40 
41 
42 	ret = io_uring_submit(ring);
43 	if (expect_drops) {
44 		if (ret != 5) {
45 			fprintf(stderr, "drops submit failed: %d\n", ret);
46 			goto err;
47 		}
48 	} else {
49 		if (ret != 6) {
50 			fprintf(stderr, "no drops submit failed: %d\n", ret);
51 			goto err;
52 		}
53 	}
54 
55 	return 0;
56 err:
57 	return 1;
58 }
59 
main(int argc,char * argv[])60 int main(int argc, char *argv[])
61 {
62 	struct io_uring ring;
63 	int ret;
64 
65 	if (argc > 1)
66 		return 0;
67 
68 	ret = io_uring_queue_init(8, &ring, IORING_SETUP_SUBMIT_ALL);
69 	if (ret)
70 		return 0;
71 
72 	ret = test(&ring, 0);
73 	if (ret) {
74 		fprintf(stderr, "test no drops failed\n");
75 		return ret;
76 	}
77 
78 	io_uring_queue_exit(&ring);
79 
80 	ret = io_uring_queue_init(8, &ring, 0);
81 	if (ret) {
82 		fprintf(stderr, "ring setup failed\n");
83 		return 0;
84 	}
85 
86 	ret = test(&ring, 1);
87 	if (ret) {
88 		fprintf(stderr, "test drops failed\n");
89 		return ret;
90 	}
91 
92 	return 0;
93 }
94