read-mshot-empty.c 3.3 KB

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