socket-rw-eagain.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. ret = t_bind_ephemeral_port(recv_s0, &addr);
  39. assert(!ret);
  40. ret = listen(recv_s0, 128);
  41. assert(ret != -1);
  42. p_fd[1] = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
  43. val = 1;
  44. ret = setsockopt(p_fd[1], IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
  45. assert(ret != -1);
  46. int32_t flags = fcntl(p_fd[1], F_GETFL, 0);
  47. assert(flags != -1);
  48. flags |= O_NONBLOCK;
  49. ret = fcntl(p_fd[1], F_SETFL, flags);
  50. assert(ret != -1);
  51. ret = connect(p_fd[1], (struct sockaddr*)&addr, sizeof(addr));
  52. assert(ret == -1);
  53. p_fd[0] = accept(recv_s0, NULL, NULL);
  54. assert(p_fd[0] != -1);
  55. flags = fcntl(p_fd[0], F_GETFL, 0);
  56. assert(flags != -1);
  57. flags |= O_NONBLOCK;
  58. ret = fcntl(p_fd[0], F_SETFL, flags);
  59. assert(ret != -1);
  60. while (1) {
  61. int32_t code;
  62. socklen_t code_len = sizeof(code);
  63. ret = getsockopt(p_fd[1], SOL_SOCKET, SO_ERROR, &code, &code_len);
  64. assert(ret != -1);
  65. if (!code)
  66. break;
  67. }
  68. struct io_uring m_io_uring;
  69. struct io_uring_params p = { };
  70. ret = io_uring_queue_init_params(32, &m_io_uring, &p);
  71. assert(ret >= 0);
  72. if (p.features & IORING_FEAT_FAST_POLL)
  73. return 0;
  74. char recv_buff[128];
  75. char send_buff[128];
  76. {
  77. iov_r[0].iov_base = recv_buff;
  78. iov_r[0].iov_len = sizeof(recv_buff);
  79. struct io_uring_sqe* sqe = io_uring_get_sqe(&m_io_uring);
  80. assert(sqe != NULL);
  81. io_uring_prep_readv(sqe, p_fd[0], iov_r, 1, 0);
  82. sqe->user_data = 1;
  83. }
  84. {
  85. iov_w[0].iov_base = send_buff;
  86. iov_w[0].iov_len = sizeof(send_buff);
  87. struct io_uring_sqe* sqe = io_uring_get_sqe(&m_io_uring);
  88. assert(sqe != NULL);
  89. io_uring_prep_writev(sqe, p_fd[1], iov_w, 1, 0);
  90. sqe->user_data = 2;
  91. }
  92. ret = io_uring_submit_and_wait(&m_io_uring, 2);
  93. assert(ret != -1);
  94. struct io_uring_cqe* cqe;
  95. uint32_t head;
  96. uint32_t count = 0;
  97. while (count != 2) {
  98. io_uring_for_each_cqe(&m_io_uring, head, cqe) {
  99. if (cqe->user_data == 2 && cqe->res != 128) {
  100. fprintf(stderr, "write=%d\n", cqe->res);
  101. goto err;
  102. } else if (cqe->user_data == 1 && cqe->res != -EAGAIN) {
  103. fprintf(stderr, "read=%d\n", cqe->res);
  104. goto err;
  105. }
  106. count++;
  107. }
  108. assert(count <= 2);
  109. io_uring_cq_advance(&m_io_uring, count);
  110. }
  111. io_uring_queue_exit(&m_io_uring);
  112. return 0;
  113. err:
  114. io_uring_queue_exit(&m_io_uring);
  115. return 1;
  116. }