socket-rw-offset.c 3.1 KB

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