poll-v-poll.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: test io_uring poll handling
  5. *
  6. */
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <signal.h>
  13. #include <fcntl.h>
  14. #include <poll.h>
  15. #include <sys/wait.h>
  16. #include <sys/select.h>
  17. #include <pthread.h>
  18. #include <sys/epoll.h>
  19. #include "liburing.h"
  20. struct thread_data {
  21. struct io_uring *ring;
  22. int fd;
  23. int events;
  24. const char *test;
  25. int out[2];
  26. };
  27. static void *epoll_wait_fn(void *data)
  28. {
  29. struct thread_data *td = data;
  30. struct epoll_event ev;
  31. if (epoll_wait(td->fd, &ev, 1, -1) < 0) {
  32. perror("epoll_wait");
  33. goto err;
  34. }
  35. return NULL;
  36. err:
  37. return (void *) 1;
  38. }
  39. static void *iou_poll(void *data)
  40. {
  41. struct thread_data *td = data;
  42. struct io_uring_sqe *sqe;
  43. struct io_uring_cqe *cqe;
  44. int ret;
  45. sqe = io_uring_get_sqe(td->ring);
  46. io_uring_prep_poll_add(sqe, td->fd, td->events);
  47. ret = io_uring_submit(td->ring);
  48. if (ret != 1) {
  49. fprintf(stderr, "submit got %d\n", ret);
  50. goto err;
  51. }
  52. ret = io_uring_wait_cqe(td->ring, &cqe);
  53. if (ret) {
  54. fprintf(stderr, "wait_cqe: %d\n", ret);
  55. goto err;
  56. }
  57. td->out[0] = cqe->res & 0x3f;
  58. io_uring_cqe_seen(td->ring, cqe);
  59. return NULL;
  60. err:
  61. return (void *) 1;
  62. }
  63. static void *poll_pipe(void *data)
  64. {
  65. struct thread_data *td = data;
  66. struct pollfd pfd;
  67. int ret;
  68. pfd.fd = td->fd;
  69. pfd.events = td->events;
  70. ret = poll(&pfd, 1, -1);
  71. if (ret < 0)
  72. perror("poll");
  73. td->out[1] = pfd.revents;
  74. return NULL;
  75. }
  76. static int do_pipe_pollin_test(struct io_uring *ring)
  77. {
  78. struct thread_data td;
  79. pthread_t threads[2];
  80. int ret, pipe1[2];
  81. char buf;
  82. if (pipe(pipe1) < 0) {
  83. perror("pipe");
  84. return 1;
  85. }
  86. td.ring = ring;
  87. td.fd = pipe1[0];
  88. td.events = POLLIN;
  89. td.test = __FUNCTION__;
  90. pthread_create(&threads[1], NULL, iou_poll, &td);
  91. pthread_create(&threads[0], NULL, poll_pipe, &td);
  92. usleep(100000);
  93. buf = 0x89;
  94. ret = write(pipe1[1], &buf, sizeof(buf));
  95. if (ret != sizeof(buf)) {
  96. fprintf(stderr, "write failed: %d\n", ret);
  97. return 1;
  98. }
  99. pthread_join(threads[0], NULL);
  100. pthread_join(threads[1], NULL);
  101. if (td.out[0] != td.out[1]) {
  102. fprintf(stderr, "%s: res %x/%x differ\n", __FUNCTION__,
  103. td.out[0], td.out[1]);
  104. return 1;
  105. }
  106. return 0;
  107. }
  108. static int do_pipe_pollout_test(struct io_uring *ring)
  109. {
  110. struct thread_data td;
  111. pthread_t threads[2];
  112. int ret, pipe1[2];
  113. char buf;
  114. if (pipe(pipe1) < 0) {
  115. perror("pipe");
  116. return 1;
  117. }
  118. td.ring = ring;
  119. td.fd = pipe1[1];
  120. td.events = POLLOUT;
  121. td.test = __FUNCTION__;
  122. pthread_create(&threads[0], NULL, poll_pipe, &td);
  123. pthread_create(&threads[1], NULL, iou_poll, &td);
  124. usleep(100000);
  125. buf = 0x89;
  126. ret = write(pipe1[1], &buf, sizeof(buf));
  127. if (ret != sizeof(buf)) {
  128. fprintf(stderr, "write failed: %d\n", ret);
  129. return 1;
  130. }
  131. pthread_join(threads[0], NULL);
  132. pthread_join(threads[1], NULL);
  133. if (td.out[0] != td.out[1]) {
  134. fprintf(stderr, "%s: res %x/%x differ\n", __FUNCTION__,
  135. td.out[0], td.out[1]);
  136. return 1;
  137. }
  138. return 0;
  139. }
  140. static int do_fd_test(struct io_uring *ring, const char *fname, int events)
  141. {
  142. struct thread_data td;
  143. pthread_t threads[2];
  144. int fd;
  145. fd = open(fname, O_RDONLY);
  146. if (fd < 0) {
  147. perror("open");
  148. return 1;
  149. }
  150. td.ring = ring;
  151. td.fd = fd;
  152. td.events = events;
  153. td.test = __FUNCTION__;
  154. pthread_create(&threads[0], NULL, poll_pipe, &td);
  155. pthread_create(&threads[1], NULL, iou_poll, &td);
  156. pthread_join(threads[0], NULL);
  157. pthread_join(threads[1], NULL);
  158. if (td.out[0] != td.out[1]) {
  159. fprintf(stderr, "%s: res %x/%x differ\n", __FUNCTION__,
  160. td.out[0], td.out[1]);
  161. return 1;
  162. }
  163. return 0;
  164. }
  165. static int iou_epoll_ctl(struct io_uring *ring, int epfd, int fd,
  166. struct epoll_event *ev)
  167. {
  168. struct io_uring_sqe *sqe;
  169. struct io_uring_cqe *cqe;
  170. int ret;
  171. sqe = io_uring_get_sqe(ring);
  172. if (!sqe) {
  173. fprintf(stderr, "Failed to get sqe\n");
  174. return 1;
  175. }
  176. io_uring_prep_epoll_ctl(sqe, epfd, fd, EPOLL_CTL_ADD, ev);
  177. ret = io_uring_submit(ring);
  178. if (ret != 1) {
  179. fprintf(stderr, "submit: %d\n", ret);
  180. return 1;
  181. }
  182. ret = io_uring_wait_cqe(ring, &cqe);
  183. if (ret) {
  184. fprintf(stderr, "wait_cqe: %d\n", ret);
  185. return 1;
  186. }
  187. ret = cqe->res;
  188. io_uring_cqe_seen(ring, cqe);
  189. return ret;
  190. }
  191. static int do_test_epoll(struct io_uring *ring, int iou_epoll_add)
  192. {
  193. struct epoll_event ev;
  194. struct thread_data td;
  195. pthread_t threads[2];
  196. int ret, pipe1[2];
  197. char buf;
  198. int fd;
  199. fd = epoll_create1(0);
  200. if (fd < 0) {
  201. perror("epoll_create");
  202. return 1;
  203. }
  204. if (pipe(pipe1) < 0) {
  205. perror("pipe");
  206. return 1;
  207. }
  208. ev.events = EPOLLIN;
  209. ev.data.fd = pipe1[0];
  210. if (!iou_epoll_add) {
  211. if (epoll_ctl(fd, EPOLL_CTL_ADD, pipe1[0], &ev) < 0) {
  212. perror("epoll_ctrl");
  213. return 1;
  214. }
  215. } else {
  216. ret = iou_epoll_ctl(ring, fd, pipe1[0], &ev);
  217. if (ret == -EINVAL) {
  218. fprintf(stdout, "epoll not supported, skipping\n");
  219. return 0;
  220. } else if (ret < 0) {
  221. return 1;
  222. }
  223. }
  224. td.ring = ring;
  225. td.fd = fd;
  226. td.events = POLLIN;
  227. td.test = __FUNCTION__;
  228. pthread_create(&threads[0], NULL, iou_poll, &td);
  229. pthread_create(&threads[1], NULL, epoll_wait_fn, &td);
  230. usleep(100000);
  231. buf = 0x89;
  232. ret = write(pipe1[1], &buf, sizeof(buf));
  233. if (ret != sizeof(buf)) {
  234. fprintf(stderr, "write failed: %d\n", ret);
  235. return 1;
  236. }
  237. pthread_join(threads[0], NULL);
  238. pthread_join(threads[1], NULL);
  239. return 0;
  240. }
  241. int main(int argc, char *argv[])
  242. {
  243. struct io_uring ring;
  244. const char *fname;
  245. int ret;
  246. ret = io_uring_queue_init(1, &ring, 0);
  247. if (ret) {
  248. fprintf(stderr, "ring setup failed\n");
  249. return 1;
  250. }
  251. ret = do_pipe_pollin_test(&ring);
  252. if (ret) {
  253. fprintf(stderr, "pipe pollin test failed\n");
  254. return ret;
  255. }
  256. ret = do_pipe_pollout_test(&ring);
  257. if (ret) {
  258. fprintf(stderr, "pipe pollout test failed\n");
  259. return ret;
  260. }
  261. ret = do_test_epoll(&ring, 0);
  262. if (ret) {
  263. fprintf(stderr, "epoll test 0 failed\n");
  264. return ret;
  265. }
  266. ret = do_test_epoll(&ring, 1);
  267. if (ret) {
  268. fprintf(stderr, "epoll test 1 failed\n");
  269. return ret;
  270. }
  271. if (argc > 1)
  272. fname = argv[1];
  273. else
  274. fname = argv[0];
  275. ret = do_fd_test(&ring, fname, POLLIN);
  276. if (ret) {
  277. fprintf(stderr, "fd test IN failed\n");
  278. return ret;
  279. }
  280. ret = do_fd_test(&ring, fname, POLLOUT);
  281. if (ret) {
  282. fprintf(stderr, "fd test OUT failed\n");
  283. return ret;
  284. }
  285. ret = do_fd_test(&ring, fname, POLLOUT | POLLIN);
  286. if (ret) {
  287. fprintf(stderr, "fd test IN|OUT failed\n");
  288. return ret;
  289. }
  290. return 0;
  291. }