socket-rw.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. ret = t_bind_ephemeral_port(recv_s0, &addr);
  41. assert(!ret);
  42. ret = listen(recv_s0, 128);
  43. assert(ret != -1);
  44. p_fd[1] = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
  45. val = 1;
  46. ret = setsockopt(p_fd[1], IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
  47. assert(ret != -1);
  48. int32_t flags = fcntl(p_fd[1], F_GETFL, 0);
  49. assert(flags != -1);
  50. flags |= O_NONBLOCK;
  51. ret = fcntl(p_fd[1], F_SETFL, flags);
  52. assert(ret != -1);
  53. ret = connect(p_fd[1], (struct sockaddr*)&addr, sizeof(addr));
  54. assert(ret == -1);
  55. flags = fcntl(p_fd[1], F_GETFL, 0);
  56. assert(flags != -1);
  57. flags &= ~O_NONBLOCK;
  58. ret = fcntl(p_fd[1], F_SETFL, flags);
  59. assert(ret != -1);
  60. p_fd[0] = accept(recv_s0, NULL, NULL);
  61. assert(p_fd[0] != -1);
  62. while (1) {
  63. int32_t code;
  64. socklen_t code_len = sizeof(code);
  65. ret = getsockopt(p_fd[1], SOL_SOCKET, SO_ERROR, &code, &code_len);
  66. assert(ret != -1);
  67. if (!code)
  68. break;
  69. }
  70. struct io_uring m_io_uring;
  71. ret = io_uring_queue_init(32, &m_io_uring, 0);
  72. assert(ret >= 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. }
  82. {
  83. iov_w[0].iov_base = send_buff;
  84. iov_w[0].iov_len = sizeof(send_buff);
  85. struct io_uring_sqe* sqe = io_uring_get_sqe(&m_io_uring);
  86. assert(sqe != NULL);
  87. io_uring_prep_writev(sqe, p_fd[1], iov_w, 1, 0);
  88. }
  89. ret = io_uring_submit_and_wait(&m_io_uring, 2);
  90. assert(ret != -1);
  91. struct io_uring_cqe* cqe;
  92. uint32_t head;
  93. uint32_t count = 0;
  94. while (count != 2) {
  95. io_uring_for_each_cqe(&m_io_uring, head, cqe) {
  96. assert(cqe->res == 128);
  97. count++;
  98. }
  99. assert(count <= 2);
  100. io_uring_cq_advance(&m_io_uring, count);
  101. }
  102. io_uring_queue_exit(&m_io_uring);
  103. return 0;
  104. }