poll-link.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 "helpers.h"
  17. #include "liburing.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 is_mask[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 sockaddr_in addr;
  47. struct data *data = arg;
  48. int s0;
  49. wait_for_var(&recv_thread_ready);
  50. s0 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  51. assert(s0 != -1);
  52. addr.sin_family = AF_INET;
  53. addr.sin_port = data->port;
  54. addr.sin_addr.s_addr = data->addr;
  55. if (connect(s0, (struct sockaddr*)&addr, sizeof(addr)) != -1)
  56. wait_for_var(&recv_thread_done);
  57. close(s0);
  58. return 0;
  59. }
  60. static void *recv_thread(void *arg)
  61. {
  62. struct sockaddr_in addr = { };
  63. struct data *data = arg;
  64. struct io_uring_sqe *sqe;
  65. struct io_uring ring;
  66. int i, ret;
  67. ret = io_uring_queue_init(8, &ring, 0);
  68. assert(ret == 0);
  69. int s0 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  70. assert(s0 != -1);
  71. int32_t val = 1;
  72. ret = setsockopt(s0, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
  73. assert(ret != -1);
  74. ret = setsockopt(s0, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
  75. assert(ret != -1);
  76. addr.sin_family = AF_INET;
  77. data->addr = inet_addr("127.0.0.1");
  78. addr.sin_addr.s_addr = data->addr;
  79. if (t_bind_ephemeral_port(s0, &addr)) {
  80. perror("bind");
  81. data->stop = 1;
  82. signal_var(&recv_thread_ready);
  83. goto err;
  84. }
  85. data->port = addr.sin_port;
  86. ret = listen(s0, 128);
  87. assert(ret != -1);
  88. signal_var(&recv_thread_ready);
  89. sqe = io_uring_get_sqe(&ring);
  90. assert(sqe != NULL);
  91. io_uring_prep_poll_add(sqe, s0, POLLIN | POLLHUP | POLLERR);
  92. sqe->flags |= IOSQE_IO_LINK;
  93. sqe->user_data = 1;
  94. sqe = io_uring_get_sqe(&ring);
  95. assert(sqe != NULL);
  96. struct __kernel_timespec ts;
  97. ts.tv_sec = data->timeout / 1000000000;
  98. ts.tv_nsec = data->timeout % 1000000000;
  99. io_uring_prep_link_timeout(sqe, &ts, 0);
  100. sqe->user_data = 2;
  101. ret = io_uring_submit(&ring);
  102. assert(ret == 2);
  103. for (i = 0; i < 2; i++) {
  104. struct io_uring_cqe *cqe;
  105. int idx;
  106. if (io_uring_wait_cqe(&ring, &cqe)) {
  107. fprintf(stderr, "wait cqe failed\n");
  108. goto err;
  109. }
  110. idx = cqe->user_data - 1;
  111. if (data->is_mask[idx] && !(data->expected[idx] & cqe->res)) {
  112. fprintf(stderr, "cqe %" PRIu64 " got %x, wanted mask %x\n",
  113. (uint64_t) cqe->user_data, cqe->res,
  114. data->expected[idx]);
  115. goto err;
  116. } else if (!data->is_mask[idx] && cqe->res != data->expected[idx]) {
  117. fprintf(stderr, "cqe %" PRIu64 " got %d, wanted %d\n",
  118. (uint64_t) cqe->user_data, cqe->res,
  119. data->expected[idx]);
  120. goto err;
  121. }
  122. io_uring_cqe_seen(&ring, cqe);
  123. }
  124. signal_var(&recv_thread_done);
  125. close(s0);
  126. io_uring_queue_exit(&ring);
  127. return NULL;
  128. err:
  129. signal_var(&recv_thread_done);
  130. close(s0);
  131. io_uring_queue_exit(&ring);
  132. return (void *) 1;
  133. }
  134. static int test_poll_timeout(int do_connect, unsigned long timeout)
  135. {
  136. pthread_t t1, t2;
  137. struct data d;
  138. void *tret;
  139. int ret = 0;
  140. recv_thread_ready = 0;
  141. recv_thread_done = 0;
  142. memset(&d, 0, sizeof(d));
  143. d.timeout = timeout;
  144. if (!do_connect) {
  145. d.expected[0] = -ECANCELED;
  146. d.expected[1] = -ETIME;
  147. } else {
  148. d.expected[0] = POLLIN;
  149. d.is_mask[0] = 1;
  150. d.expected[1] = -ECANCELED;
  151. }
  152. pthread_create(&t1, NULL, recv_thread, &d);
  153. if (do_connect)
  154. pthread_create(&t2, NULL, send_thread, &d);
  155. pthread_join(t1, &tret);
  156. if (tret)
  157. ret++;
  158. if (do_connect) {
  159. pthread_join(t2, &tret);
  160. if (tret)
  161. ret++;
  162. }
  163. return ret;
  164. }
  165. int main(int argc, char *argv[])
  166. {
  167. if (argc > 1)
  168. return 0;
  169. srand(getpid());
  170. if (test_poll_timeout(0, 200000000)) {
  171. fprintf(stderr, "poll timeout 0 failed\n");
  172. return 1;
  173. }
  174. if (test_poll_timeout(1, 1000000000)) {
  175. fprintf(stderr, "poll timeout 1 failed\n");
  176. return 1;
  177. }
  178. return 0;
  179. }