accept-link.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. #include <errno.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <fcntl.h>
  9. #include <assert.h>
  10. #include <pthread.h>
  11. #include <sys/socket.h>
  12. #include <netinet/tcp.h>
  13. #include <netinet/in.h>
  14. #include <poll.h>
  15. #include <arpa/inet.h>
  16. #include "liburing.h"
  17. #include "helpers.h"
  18. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  19. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  20. static int recv_thread_ready = 0;
  21. static int recv_thread_done = 0;
  22. static void signal_var(int *var)
  23. {
  24. pthread_mutex_lock(&mutex);
  25. *var = 1;
  26. pthread_cond_signal(&cond);
  27. pthread_mutex_unlock(&mutex);
  28. }
  29. static void wait_for_var(int *var)
  30. {
  31. pthread_mutex_lock(&mutex);
  32. while (!*var)
  33. pthread_cond_wait(&cond, &mutex);
  34. pthread_mutex_unlock(&mutex);
  35. }
  36. struct data {
  37. unsigned expected[2];
  38. unsigned just_positive[2];
  39. unsigned long timeout;
  40. unsigned short port;
  41. unsigned int addr;
  42. int stop;
  43. };
  44. static void *send_thread(void *arg)
  45. {
  46. struct data *data = arg;
  47. int ret;
  48. wait_for_var(&recv_thread_ready);
  49. if (data->stop)
  50. return NULL;
  51. int s0 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  52. assert(s0 != -1);
  53. struct sockaddr_in addr;
  54. addr.sin_family = AF_INET;
  55. addr.sin_port = data->port;
  56. addr.sin_addr.s_addr = data->addr;
  57. ret = connect(s0, (struct sockaddr*)&addr, sizeof(addr));
  58. assert(ret != -1);
  59. wait_for_var(&recv_thread_done);
  60. close(s0);
  61. return NULL;
  62. }
  63. static void *recv_thread(void *arg)
  64. {
  65. struct data *data = arg;
  66. struct io_uring ring;
  67. int i, ret;
  68. ret = io_uring_queue_init(8, &ring, 0);
  69. assert(ret == 0);
  70. int s0 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  71. assert(s0 != -1);
  72. int32_t val = 1;
  73. ret = setsockopt(s0, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
  74. assert(ret != -1);
  75. ret = setsockopt(s0, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
  76. assert(ret != -1);
  77. struct sockaddr_in addr;
  78. addr.sin_family = AF_INET;
  79. data->addr = inet_addr("127.0.0.1");
  80. addr.sin_addr.s_addr = data->addr;
  81. i = 0;
  82. do {
  83. data->port = htons(1025 + (rand() % 64510));
  84. addr.sin_port = data->port;
  85. if (bind(s0, (struct sockaddr*)&addr, sizeof(addr)) != -1)
  86. break;
  87. } while (++i < 100);
  88. if (i >= 100) {
  89. printf("Can't find good port, skipped\n");
  90. data->stop = 1;
  91. signal_var(&recv_thread_ready);
  92. goto out;
  93. }
  94. ret = listen(s0, 128);
  95. assert(ret != -1);
  96. signal_var(&recv_thread_ready);
  97. struct io_uring_sqe *sqe;
  98. sqe = io_uring_get_sqe(&ring);
  99. assert(sqe != NULL);
  100. io_uring_prep_accept(sqe, s0, NULL, NULL, 0);
  101. sqe->flags |= IOSQE_IO_LINK;
  102. sqe->user_data = 1;
  103. sqe = io_uring_get_sqe(&ring);
  104. assert(sqe != NULL);
  105. struct __kernel_timespec ts;
  106. ts.tv_sec = data->timeout / 1000000000;
  107. ts.tv_nsec = data->timeout % 1000000000;
  108. io_uring_prep_link_timeout(sqe, &ts, 0);
  109. sqe->user_data = 2;
  110. ret = io_uring_submit(&ring);
  111. assert(ret == 2);
  112. for (i = 0; i < 2; i++) {
  113. struct io_uring_cqe *cqe;
  114. int idx;
  115. if (io_uring_wait_cqe(&ring, &cqe)) {
  116. fprintf(stderr, "wait cqe failed\n");
  117. goto err;
  118. }
  119. idx = cqe->user_data - 1;
  120. if (cqe->res != data->expected[idx]) {
  121. if (cqe->res > 0 && data->just_positive[idx])
  122. goto ok;
  123. if (cqe->res == -EBADF) {
  124. fprintf(stdout, "Accept not supported, skipping\n");
  125. data->stop = 1;
  126. goto out;
  127. }
  128. fprintf(stderr, "cqe %" PRIu64 " got %d, wanted %d\n",
  129. (uint64_t) cqe->user_data, cqe->res,
  130. data->expected[idx]);
  131. goto err;
  132. }
  133. ok:
  134. if (cqe->user_data == 1 && cqe->res > 0)
  135. close(cqe->res);
  136. io_uring_cqe_seen(&ring, cqe);
  137. }
  138. signal_var(&recv_thread_done);
  139. out:
  140. close(s0);
  141. return NULL;
  142. err:
  143. close(s0);
  144. return (void *) 1;
  145. }
  146. static int test_accept_timeout(int do_connect, unsigned long timeout)
  147. {
  148. struct io_uring ring;
  149. struct io_uring_params p = {};
  150. pthread_t t1, t2;
  151. struct data d;
  152. void *tret;
  153. int ret, fast_poll;
  154. ret = io_uring_queue_init_params(1, &ring, &p);
  155. if (ret) {
  156. fprintf(stderr, "queue_init: %d\n", ret);
  157. return 1;
  158. }
  159. fast_poll = (p.features & IORING_FEAT_FAST_POLL) != 0;
  160. io_uring_queue_exit(&ring);
  161. recv_thread_ready = 0;
  162. recv_thread_done = 0;
  163. memset(&d, 0, sizeof(d));
  164. d.timeout = timeout;
  165. if (!do_connect) {
  166. if (fast_poll) {
  167. d.expected[0] = -ECANCELED;
  168. d.expected[1] = -ETIME;
  169. } else {
  170. d.expected[0] = -EINTR;
  171. d.expected[1] = -EALREADY;
  172. }
  173. } else {
  174. d.expected[0] = -1U;
  175. d.just_positive[0] = 1;
  176. d.expected[1] = -ECANCELED;
  177. }
  178. pthread_create(&t1, NULL, recv_thread, &d);
  179. if (do_connect)
  180. pthread_create(&t2, NULL, send_thread, &d);
  181. pthread_join(t1, &tret);
  182. if (tret)
  183. ret++;
  184. if (do_connect) {
  185. pthread_join(t2, &tret);
  186. if (tret)
  187. ret++;
  188. }
  189. return ret;
  190. }
  191. int main(int argc, char *argv[])
  192. {
  193. if (argc > 1)
  194. return T_EXIT_SKIP;
  195. if (test_accept_timeout(0, 200000000)) {
  196. fprintf(stderr, "accept timeout 0 failed\n");
  197. return T_EXIT_FAIL;
  198. }
  199. if (test_accept_timeout(1, 1000000000)) {
  200. fprintf(stderr, "accept and connect timeout 0 failed\n");
  201. return T_EXIT_FAIL;
  202. }
  203. return T_EXIT_PASS;
  204. }