• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Description: test that multishot read correctly keeps reading until all
4  *		data has been emptied. the original implementation failed
5  *		to do so, if the available buffer size was less than what
6  *		was available, hence requiring multiple reads to empty the
7  *		file buffer.
8  */
9 #include <stdio.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <assert.h>
14 #include <pthread.h>
15 #include <sys/time.h>
16 
17 #include "liburing.h"
18 #include "helpers.h"
19 
20 #define BGID		17
21 #define NR_BUFS		4
22 #define BR_MASK		(NR_BUFS - 1)
23 #define BUF_SIZE	32
24 
do_write(int fd,void * buf,int buf_size)25 static int do_write(int fd, void *buf, int buf_size)
26 {
27 	int ret;
28 
29 	ret = write(fd, buf, buf_size);
30 	if (ret < 0) {
31 		perror("write");
32 		return 0;
33 	} else if (ret != buf_size) {
34 		fprintf(stderr, "bad write size %d\n", ret);
35 		return 0;
36 	}
37 
38 	return 1;
39 }
40 
thread_fn(void * data)41 static void *thread_fn(void *data)
42 {
43 	char w1[BUF_SIZE], w2[BUF_SIZE];
44 	int *fds = data;
45 
46 	memset(w1, 0x11, BUF_SIZE);
47 	memset(w2, 0x22, BUF_SIZE);
48 
49 	if (!do_write(fds[1], w1, BUF_SIZE))
50 		return NULL;
51 	if (!do_write(fds[1], w2, BUF_SIZE))
52 		return NULL;
53 
54 	usleep(100000);
55 
56 	if (!do_write(fds[1], w1, BUF_SIZE))
57 		return NULL;
58 	if (!do_write(fds[1], w2, BUF_SIZE))
59 		return NULL;
60 
61 	return NULL;
62 }
63 
main(int argc,char * argv[])64 int main(int argc, char *argv[])
65 {
66 	struct io_uring_buf_ring *br;
67 	struct io_uring_sqe *sqe;
68 	struct io_uring_cqe *cqe;
69 	struct io_uring ring;
70 	pthread_t thread;
71 	int i, ret, fds[2];
72 	void *buf, *tret;
73 
74 	if (argc > 1)
75 		return T_EXIT_SKIP;
76 
77 	if (pipe(fds) < 0) {
78 		perror("pipe");
79 		return T_EXIT_FAIL;
80 	}
81 
82 	ret = io_uring_queue_init(8, &ring, 0);
83 	if (ret) {
84 		fprintf(stderr, "queue_init: %d\n", ret);
85 		return T_EXIT_FAIL;
86 	}
87 
88 	br = io_uring_setup_buf_ring(&ring, NR_BUFS, BGID, 0, &ret);
89 	if (!br) {
90 		if (ret == -EINVAL)
91 			return T_EXIT_SKIP;
92 		fprintf(stderr, "failed buffer ring %d\n", ret);
93 		return T_EXIT_FAIL;
94 	}
95 
96 	buf = malloc(NR_BUFS * BUF_SIZE);
97 	for (i = 0; i < NR_BUFS; i++) {
98 		void *this_buf = buf + i * BUF_SIZE;
99 
100 		io_uring_buf_ring_add(br, this_buf, BUF_SIZE, i, BR_MASK, i);
101 	}
102 	io_uring_buf_ring_advance(br, NR_BUFS);
103 
104 	sqe = io_uring_get_sqe(&ring);
105 	io_uring_prep_read_multishot(sqe, fds[0], 0, 0, BGID);
106 
107 	ret = io_uring_submit(&ring);
108 	if (ret != 1) {
109 		fprintf(stderr, "bad submit %d\n", ret);
110 		return T_EXIT_FAIL;
111 	}
112 
113 	/*
114 	 * read multishot not available would be ready as a cqe when
115 	 * submission returns, check and skip if not.
116 	 */
117 	ret = io_uring_peek_cqe(&ring, &cqe);
118 	if (!ret) {
119 		if (cqe->res == -EINVAL || cqe->res == -EBADF) {
120 			free(buf);
121 			return T_EXIT_SKIP;
122 		}
123 	}
124 
125 	pthread_create(&thread, NULL, thread_fn, fds);
126 
127 	for (i = 0; i < 4; i++) {
128 		int buf_index;
129 
130 		ret = io_uring_wait_cqe(&ring, &cqe);
131 		if (ret) {
132 			fprintf(stderr, "wait %d\n", ret);
133 			break;
134 		}
135 
136 		if (cqe->res != BUF_SIZE) {
137 			fprintf(stderr, "size %d\n", cqe->res);
138 			return T_EXIT_FAIL;
139 		}
140 		if (!(cqe->flags & IORING_CQE_F_BUFFER)) {
141 			fprintf(stderr, "buffer not set\n");
142 			return T_EXIT_FAIL;
143 		}
144 		if (!(cqe->flags & IORING_CQE_F_MORE)) {
145 			fprintf(stderr, "more not set\n");
146 			return T_EXIT_FAIL;
147 		}
148 		buf_index = cqe->flags >> 16;
149 		assert(buf_index >= 0 && buf_index <= NR_BUFS);
150 		io_uring_cqe_seen(&ring, cqe);
151 	}
152 
153 	pthread_join(thread, &tret);
154 	io_uring_free_buf_ring(&ring, br, NR_BUFS, BGID);
155 	io_uring_queue_exit(&ring);
156 	free(buf);
157 	return T_EXIT_PASS;
158 }
159