msg-ring-flags.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: test ring messaging with flags command
  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 <pthread.h>
  14. #include "liburing.h"
  15. #include "helpers.h"
  16. #define CUSTOM_FLAG 0x42
  17. #define USER_DATA 0x5aa5
  18. #define LEN 0x20
  19. #define ID 0x1
  20. struct data {
  21. pthread_barrier_t barrier;
  22. int fd;
  23. };
  24. static int recv_msg(struct io_uring *ring)
  25. {
  26. struct io_uring_cqe *cqe;
  27. int ret;
  28. ret = io_uring_wait_cqe(ring, &cqe);
  29. if (ret) {
  30. fprintf(stderr, "wait cqe %d\n", ret);
  31. return T_EXIT_FAIL;
  32. }
  33. if (cqe->user_data != USER_DATA) {
  34. fprintf(stderr, "user_data %llx\n", (long long) cqe->user_data);
  35. return T_EXIT_FAIL;
  36. }
  37. if (cqe->res != LEN) {
  38. fprintf(stderr, "len %x\n", cqe->res);
  39. return T_EXIT_FAIL;
  40. }
  41. if (cqe->flags != CUSTOM_FLAG) {
  42. fprintf(stderr, "flags %x\n", cqe->flags);
  43. return T_EXIT_FAIL;
  44. }
  45. return T_EXIT_PASS;
  46. }
  47. static int send_msg(struct io_uring *ring, int target_fd)
  48. {
  49. struct io_uring_cqe *cqe;
  50. struct io_uring_sqe *sqe;
  51. int ret;
  52. sqe = io_uring_get_sqe(ring);
  53. if (!sqe) {
  54. fprintf(stderr, "get sqe failed\n");
  55. return T_EXIT_FAIL;
  56. }
  57. io_uring_prep_msg_ring_cqe_flags(sqe, target_fd, LEN, USER_DATA,
  58. 0, CUSTOM_FLAG);
  59. sqe->user_data = ID;
  60. ret = io_uring_submit(ring);
  61. if (ret <= 0) {
  62. if (ret == -EINVAL)
  63. return T_EXIT_SKIP;
  64. fprintf(stderr, "sqe submit failed: %d\n", ret);
  65. return T_EXIT_FAIL;
  66. }
  67. ret = io_uring_wait_cqe(ring, &cqe);
  68. if (ret < 0) {
  69. fprintf(stderr, "wait completion %d\n", ret);
  70. return T_EXIT_FAIL;
  71. }
  72. if (cqe->res != 0) {
  73. if (cqe->res == -EINVAL)
  74. return T_EXIT_SKIP;
  75. fprintf(stderr, "cqe res %d\n", cqe->res);
  76. return T_EXIT_FAIL;
  77. }
  78. if (cqe->user_data != ID) {
  79. fprintf(stderr, "user_data %llx\n", (long long) cqe->user_data);
  80. return T_EXIT_FAIL;
  81. }
  82. io_uring_cqe_seen(ring, cqe);
  83. return T_EXIT_PASS;
  84. }
  85. static void *thread_fn(void *data)
  86. {
  87. struct data *d = data;
  88. struct io_uring ring;
  89. int ret;
  90. ret = io_uring_queue_init(2, &ring, IORING_SETUP_DEFER_TASKRUN | IORING_SETUP_SINGLE_ISSUER);
  91. if (ret) {
  92. fprintf(stderr, "ring init failed %d\n", ret);
  93. pthread_barrier_wait(&d->barrier);
  94. return NULL;
  95. }
  96. d->fd = ring.ring_fd;
  97. pthread_barrier_wait(&d->barrier);
  98. if (recv_msg(&ring))
  99. return (void *) 1;
  100. return NULL;
  101. }
  102. static int test(int ring_flags)
  103. {
  104. struct io_uring ring, ring2;
  105. pthread_t thread;
  106. struct data d;
  107. void *ret2;
  108. int ret, i;
  109. ret = io_uring_queue_init(2, &ring, ring_flags);
  110. if (ret) {
  111. fprintf(stderr, "io_uring_queue_init failed for ring1: %d\n", ret);
  112. return T_EXIT_FAIL;
  113. }
  114. ret = io_uring_queue_init(2, &ring2, ring_flags);
  115. if (ret) {
  116. fprintf(stderr, "io_uring_queue_init failed for ring2: %d\n", ret);
  117. return T_EXIT_FAIL;
  118. }
  119. ret = send_msg(&ring, ring2.ring_fd);
  120. if (ret) {
  121. if (ret != T_EXIT_SKIP)
  122. fprintf(stderr, "send_msg failed: %d\n", ret);
  123. return ret;
  124. }
  125. ret = recv_msg(&ring2);
  126. if (ret) {
  127. fprintf(stderr, "recv_msg failed: %d\n", ret);
  128. return ret;
  129. }
  130. for (i = 0; i < 8; i++) {
  131. ret = send_msg(&ring, ring2.ring_fd);
  132. if (ret) {
  133. if (ret != T_EXIT_SKIP)
  134. fprintf(stderr, "send_msg failed: %d\n", ret);
  135. return ret;
  136. }
  137. }
  138. for (i = 0; i < 8; i++) {
  139. ret = recv_msg(&ring2);
  140. if (ret) {
  141. fprintf(stderr, "recv_msg failed: %d\n", ret);
  142. return ret;
  143. }
  144. }
  145. pthread_barrier_init(&d.barrier, NULL, 2);
  146. d.fd = -1;
  147. pthread_create(&thread, NULL, thread_fn, &d);
  148. pthread_barrier_wait(&d.barrier);
  149. if (d.fd == -1)
  150. return T_EXIT_FAIL;
  151. ret = send_msg(&ring, d.fd);
  152. if (ret) {
  153. fprintf(stderr, "send msg failed: %d\n", ret);
  154. return ret;
  155. }
  156. pthread_join(thread, &ret2);
  157. if (ret2) {
  158. fprintf(stderr, "Remote test failed\n");
  159. return T_EXIT_FAIL;
  160. }
  161. return T_EXIT_PASS;
  162. }
  163. int main(int argc, char *argv[])
  164. {
  165. int ret;
  166. if (argc > 1)
  167. return T_EXIT_SKIP;
  168. ret = test(0);
  169. if (ret == T_EXIT_FAIL) {
  170. fprintf(stderr, "test ring_flags 0 failed\n");
  171. return ret;
  172. } else if (ret == T_EXIT_SKIP)
  173. return ret;
  174. ret = test(IORING_SETUP_SINGLE_ISSUER|IORING_SETUP_DEFER_TASKRUN);
  175. if (ret == T_EXIT_FAIL) {
  176. fprintf(stderr, "test ring_flags defer failed\n");
  177. return ret;
  178. }
  179. return ret;
  180. }