msg-ring-flags.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. int main(int argc, char *argv[])
  103. {
  104. struct io_uring ring, ring2;
  105. pthread_t thread;
  106. struct data d;
  107. void *ret2;
  108. int ret, i;
  109. if (argc > 1)
  110. return T_EXIT_SKIP;
  111. ret = io_uring_queue_init(2, &ring, 0);
  112. if (ret) {
  113. fprintf(stderr, "io_uring_queue_init failed for ring1: %d\n", ret);
  114. return T_EXIT_FAIL;
  115. }
  116. ret = io_uring_queue_init(2, &ring2, 0);
  117. if (ret) {
  118. fprintf(stderr, "io_uring_queue_init failed for ring2: %d\n", ret);
  119. return T_EXIT_FAIL;
  120. }
  121. ret = send_msg(&ring, ring2.ring_fd);
  122. if (ret) {
  123. if (ret != T_EXIT_SKIP)
  124. fprintf(stderr, "send_msg failed: %d\n", ret);
  125. return ret;
  126. }
  127. ret = recv_msg(&ring2);
  128. if (ret) {
  129. fprintf(stderr, "recv_msg failed: %d\n", ret);
  130. return ret;
  131. }
  132. for (i = 0; i < 8; i++) {
  133. ret = send_msg(&ring, ring2.ring_fd);
  134. if (ret) {
  135. if (ret != T_EXIT_SKIP)
  136. fprintf(stderr, "send_msg failed: %d\n", ret);
  137. return ret;
  138. }
  139. }
  140. for (i = 0; i < 8; i++) {
  141. ret = recv_msg(&ring2);
  142. if (ret) {
  143. fprintf(stderr, "recv_msg failed: %d\n", ret);
  144. return ret;
  145. }
  146. }
  147. pthread_barrier_init(&d.barrier, NULL, 2);
  148. d.fd = -1;
  149. pthread_create(&thread, NULL, thread_fn, &d);
  150. pthread_barrier_wait(&d.barrier);
  151. if (d.fd == -1)
  152. return T_EXIT_FAIL;
  153. ret = send_msg(&ring, d.fd);
  154. if (ret) {
  155. fprintf(stderr, "send msg failed: %d\n", ret);
  156. return ret;
  157. }
  158. pthread_join(thread, &ret2);
  159. if (ret2) {
  160. fprintf(stderr, "Remote test failed\n");
  161. return T_EXIT_FAIL;
  162. }
  163. return T_EXIT_PASS;
  164. }