• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Description: test CQ ring overflow
4  *
5  */
6 #include <errno.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <fcntl.h>
12 
13 #include "liburing.h"
14 #include "helpers.h"
15 
queue_n_nops(struct io_uring * ring,int n)16 static int queue_n_nops(struct io_uring *ring, int n)
17 {
18 	struct io_uring_sqe *sqe;
19 	int i, ret;
20 
21 	for (i = 0; i < n; i++) {
22 		sqe = io_uring_get_sqe(ring);
23 		if (!sqe) {
24 			printf("get sqe failed\n");
25 			goto err;
26 		}
27 
28 		io_uring_prep_nop(sqe);
29 	}
30 
31 	ret = io_uring_submit(ring);
32 	if (ret < n) {
33 		printf("Submitted only %d\n", ret);
34 		goto err;
35 	} else if (ret < 0) {
36 		printf("sqe submit failed: %d\n", ret);
37 		goto err;
38 	}
39 
40 	return 0;
41 err:
42 	return 1;
43 }
44 
main(int argc,char * argv[])45 int main(int argc, char *argv[])
46 {
47 	struct io_uring_cqe *cqe;
48 	struct io_uring_params p;
49 	struct io_uring ring;
50 	int i, ret;
51 
52 	if (argc > 1)
53 		return T_EXIT_SKIP;
54 
55 	memset(&p, 0, sizeof(p));
56 	ret = io_uring_queue_init_params(4, &ring, &p);
57 	if (ret) {
58 		printf("ring setup failed\n");
59 		return T_EXIT_FAIL;
60 
61 	}
62 
63 	if (queue_n_nops(&ring, 4))
64 		goto err;
65 	if (queue_n_nops(&ring, 4))
66 		goto err;
67 	if (queue_n_nops(&ring, 4))
68 		goto err;
69 
70 	i = 0;
71 	do {
72 		ret = io_uring_peek_cqe(&ring, &cqe);
73 		if (ret < 0) {
74 			if (ret == -EAGAIN)
75 				break;
76 			printf("wait completion %d\n", ret);
77 			goto err;
78 		}
79 		io_uring_cqe_seen(&ring, cqe);
80 		if (!cqe)
81 			break;
82 		i++;
83 	} while (1);
84 
85 	if (i < 8 ||
86 	    ((*ring.cq.koverflow != 4) && !(p.features & IORING_FEAT_NODROP))) {
87 		printf("CQ overflow fail: %d completions, %u overflow\n", i,
88 				*ring.cq.koverflow);
89 		goto err;
90 	}
91 
92 	io_uring_queue_exit(&ring);
93 	return T_EXIT_PASS;
94 err:
95 	io_uring_queue_exit(&ring);
96 	return T_EXIT_FAIL;
97 }
98