socket-rw-eagain.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Check that a readv on a nonblocking socket queued before a writev doesn't
  5. * wait for data to arrive.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stdint.h>
  10. #include <assert.h>
  11. #include <errno.h>
  12. #include <fcntl.h>
  13. #include <unistd.h>
  14. #include <sys/socket.h>
  15. #include <sys/un.h>
  16. #include <netinet/tcp.h>
  17. #include <netinet/in.h>
  18. #include <arpa/inet.h>
  19. #include "liburing.h"
  20. #include "helpers.h"
  21. int main(int argc, char *argv[])
  22. {
  23. int p_fd[2], ret;
  24. int32_t recv_s0;
  25. int32_t val = 1;
  26. struct sockaddr_in addr;
  27. struct iovec iov_r[1], iov_w[1];
  28. if (argc > 1)
  29. return 0;
  30. srand(getpid());
  31. recv_s0 = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
  32. ret = setsockopt(recv_s0, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
  33. assert(ret != -1);
  34. ret = setsockopt(recv_s0, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
  35. assert(ret != -1);
  36. addr.sin_family = AF_INET;
  37. addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  38. assert(!t_bind_ephemeral_port(recv_s0, &addr));
  39. ret = listen(recv_s0, 128);
  40. assert(ret != -1);
  41. p_fd[1] = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
  42. val = 1;
  43. ret = setsockopt(p_fd[1], IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
  44. assert(ret != -1);
  45. int32_t flags = fcntl(p_fd[1], F_GETFL, 0);
  46. assert(flags != -1);
  47. flags |= O_NONBLOCK;
  48. ret = fcntl(p_fd[1], F_SETFL, flags);
  49. assert(ret != -1);
  50. ret = connect(p_fd[1], (struct sockaddr*)&addr, sizeof(addr));
  51. assert(ret == -1);
  52. p_fd[0] = accept(recv_s0, NULL, NULL);
  53. assert(p_fd[0] != -1);
  54. flags = fcntl(p_fd[0], F_GETFL, 0);
  55. assert(flags != -1);
  56. flags |= O_NONBLOCK;
  57. ret = fcntl(p_fd[0], F_SETFL, flags);
  58. assert(ret != -1);
  59. while (1) {
  60. int32_t code;
  61. socklen_t code_len = sizeof(code);
  62. ret = getsockopt(p_fd[1], SOL_SOCKET, SO_ERROR, &code, &code_len);
  63. assert(ret != -1);
  64. if (!code)
  65. break;
  66. }
  67. struct io_uring m_io_uring;
  68. struct io_uring_params p = { };
  69. ret = io_uring_queue_init_params(32, &m_io_uring, &p);
  70. assert(ret >= 0);
  71. if (p.features & IORING_FEAT_FAST_POLL)
  72. return 0;
  73. char recv_buff[128];
  74. char send_buff[128];
  75. {
  76. iov_r[0].iov_base = recv_buff;
  77. iov_r[0].iov_len = sizeof(recv_buff);
  78. struct io_uring_sqe* sqe = io_uring_get_sqe(&m_io_uring);
  79. assert(sqe != NULL);
  80. io_uring_prep_readv(sqe, p_fd[0], iov_r, 1, 0);
  81. sqe->user_data = 1;
  82. }
  83. {
  84. iov_w[0].iov_base = send_buff;
  85. iov_w[0].iov_len = sizeof(send_buff);
  86. struct io_uring_sqe* sqe = io_uring_get_sqe(&m_io_uring);
  87. assert(sqe != NULL);
  88. io_uring_prep_writev(sqe, p_fd[1], iov_w, 1, 0);
  89. sqe->user_data = 2;
  90. }
  91. ret = io_uring_submit_and_wait(&m_io_uring, 2);
  92. assert(ret != -1);
  93. struct io_uring_cqe* cqe;
  94. uint32_t head;
  95. uint32_t count = 0;
  96. while (count != 2) {
  97. io_uring_for_each_cqe(&m_io_uring, head, cqe) {
  98. if (cqe->user_data == 2 && cqe->res != 128) {
  99. fprintf(stderr, "write=%d\n", cqe->res);
  100. goto err;
  101. } else if (cqe->user_data == 1 && cqe->res != -EAGAIN) {
  102. fprintf(stderr, "read=%d\n", cqe->res);
  103. goto err;
  104. }
  105. count++;
  106. }
  107. assert(count <= 2);
  108. io_uring_cq_advance(&m_io_uring, count);
  109. }
  110. io_uring_queue_exit(&m_io_uring);
  111. return 0;
  112. err:
  113. io_uring_queue_exit(&m_io_uring);
  114. return 1;
  115. }