ringbuf-read.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: ring mapped provided buffers with reads
  5. *
  6. */
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <fcntl.h>
  13. #include "liburing.h"
  14. #include "helpers.h"
  15. #define BUF_SIZE 4096
  16. #define NR_BUFS 64
  17. #define FSIZE (BUF_SIZE * NR_BUFS)
  18. #define BR_MASK (NR_BUFS - 1)
  19. static int no_buf_ring;
  20. static int verify_buffer(char *buf, char val)
  21. {
  22. int i;
  23. for (i = 0; i < BUF_SIZE; i++) {
  24. if (buf[i] != val) {
  25. fprintf(stderr, "got %d, wanted %d\n", buf[i], val);
  26. return 1;
  27. }
  28. }
  29. return 0;
  30. }
  31. static int test(const char *filename, int dio, int async)
  32. {
  33. struct io_uring_sqe *sqe;
  34. struct io_uring_cqe *cqe;
  35. struct io_uring ring;
  36. struct io_uring_buf_ring *br;
  37. int ret, fd, i;
  38. char *buf;
  39. void *ptr;
  40. ret = io_uring_queue_init(NR_BUFS, &ring, 0);
  41. if (ret) {
  42. fprintf(stderr, "ring setup failed: %d\n", ret);
  43. return 1;
  44. }
  45. if (dio) {
  46. fd = open(filename, O_DIRECT | O_RDONLY);
  47. if (fd < 0 && errno == EINVAL)
  48. return T_EXIT_SKIP;
  49. } else {
  50. fd = open(filename, O_RDONLY);
  51. }
  52. if (fd < 0) {
  53. if (errno == EPERM || errno == EACCES)
  54. return T_EXIT_SKIP;
  55. perror("open");
  56. return 1;
  57. }
  58. posix_fadvise(fd, 0, FSIZE, POSIX_FADV_DONTNEED);
  59. if (posix_memalign((void **) &buf, 4096, FSIZE))
  60. return 1;
  61. br = io_uring_setup_buf_ring(&ring, NR_BUFS, 1, 0, &ret);
  62. if (!br) {
  63. if (ret == -EINVAL) {
  64. no_buf_ring = 1;
  65. return 0;
  66. }
  67. fprintf(stderr, "Buffer ring register failed %d\n", ret);
  68. return 1;
  69. }
  70. ptr = buf;
  71. for (i = 0; i < NR_BUFS; i++) {
  72. io_uring_buf_ring_add(br, ptr, BUF_SIZE, i + 1, BR_MASK, i);
  73. ptr += BUF_SIZE;
  74. }
  75. io_uring_buf_ring_advance(br, NR_BUFS);
  76. for (i = 0; i < NR_BUFS; i++) {
  77. sqe = io_uring_get_sqe(&ring);
  78. io_uring_prep_read(sqe, fd, NULL, BUF_SIZE, i * BUF_SIZE);
  79. sqe->buf_group = 1;
  80. sqe->flags |= IOSQE_BUFFER_SELECT;
  81. if (async && !(i & 1))
  82. sqe->flags |= IOSQE_ASYNC;
  83. sqe->user_data = i + 1;
  84. }
  85. ret = io_uring_submit(&ring);
  86. if (ret != NR_BUFS) {
  87. fprintf(stderr, "submit: %d\n", ret);
  88. return 1;
  89. }
  90. for (i = 0; i < NR_BUFS; i++) {
  91. int bid, ud;
  92. ret = io_uring_wait_cqe(&ring, &cqe);
  93. if (ret) {
  94. fprintf(stderr, "wait cqe failed %d\n", ret);
  95. return 1;
  96. }
  97. if (cqe->res != BUF_SIZE) {
  98. fprintf(stderr, "cqe res %d\n", cqe->res);
  99. return 1;
  100. }
  101. if (!(cqe->flags & IORING_CQE_F_BUFFER)) {
  102. fprintf(stderr, "no buffer selected\n");
  103. return 1;
  104. }
  105. bid = cqe->flags >> IORING_CQE_BUFFER_SHIFT;
  106. ud = cqe->user_data;
  107. io_uring_cqe_seen(&ring, cqe);
  108. if (verify_buffer(buf + ((bid - 1) * BUF_SIZE), ud))
  109. return 1;
  110. }
  111. free(buf);
  112. return 0;
  113. }
  114. int main(int argc, char *argv[])
  115. {
  116. char buf[BUF_SIZE];
  117. char fname[80];
  118. int ret, fd, i, do_unlink;
  119. if (argc > 1) {
  120. strcpy(fname, argv[1]);
  121. do_unlink = 0;
  122. } else {
  123. sprintf(fname, ".ringbuf-read.%d", getpid());
  124. t_create_file(fname, FSIZE);
  125. do_unlink = 1;
  126. }
  127. fd = open(fname, O_WRONLY);
  128. if (fd < 0) {
  129. if (errno == EPERM || errno == EACCES)
  130. return T_EXIT_SKIP;
  131. perror("open");
  132. goto err;
  133. }
  134. for (i = 0; i < NR_BUFS; i++) {
  135. memset(buf, i + 1, BUF_SIZE);
  136. ret = write(fd, buf, BUF_SIZE);
  137. if (ret != BUF_SIZE) {
  138. fprintf(stderr, "bad file prep write\n");
  139. close(fd);
  140. goto err;
  141. }
  142. }
  143. close(fd);
  144. ret = test(fname, 1, 0);
  145. if (ret == T_EXIT_FAIL) {
  146. fprintf(stderr, "dio test failed\n");
  147. goto err;
  148. }
  149. if (no_buf_ring)
  150. goto pass;
  151. ret = test(fname, 0, 0);
  152. if (ret) {
  153. fprintf(stderr, "buffered test failed\n");
  154. goto err;
  155. }
  156. ret = test(fname, 1, 1);
  157. if (ret == T_EXIT_FAIL) {
  158. fprintf(stderr, "dio async test failed\n");
  159. goto err;
  160. }
  161. ret = test(fname, 0, 1);
  162. if (ret == T_EXIT_FAIL) {
  163. fprintf(stderr, "buffered async test failed\n");
  164. goto err;
  165. }
  166. pass:
  167. ret = T_EXIT_PASS;
  168. goto out;
  169. err:
  170. ret = T_EXIT_FAIL;
  171. out:
  172. if (do_unlink)
  173. unlink(fname);
  174. return ret;
  175. }