socket-rw.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. ret = io_uring_queue_init(32, &m_io_uring, 0);
  71. assert(ret >= 0);
  72. char recv_buff[128];
  73. char send_buff[128];
  74. {
  75. iov_r[0].iov_base = recv_buff;
  76. iov_r[0].iov_len = sizeof(recv_buff);
  77. struct io_uring_sqe* sqe = io_uring_get_sqe(&m_io_uring);
  78. assert(sqe != NULL);
  79. io_uring_prep_readv(sqe, p_fd[0], iov_r, 1, 0);
  80. }
  81. {
  82. iov_w[0].iov_base = send_buff;
  83. iov_w[0].iov_len = sizeof(send_buff);
  84. struct io_uring_sqe* sqe = io_uring_get_sqe(&m_io_uring);
  85. assert(sqe != NULL);
  86. io_uring_prep_writev(sqe, p_fd[1], iov_w, 1, 0);
  87. }
  88. ret = io_uring_submit_and_wait(&m_io_uring, 2);
  89. assert(ret != -1);
  90. struct io_uring_cqe* cqe;
  91. uint32_t head;
  92. uint32_t count = 0;
  93. while (count != 2) {
  94. io_uring_for_each_cqe(&m_io_uring, head, cqe) {
  95. assert(cqe->res == 128);
  96. count++;
  97. }
  98. assert(count <= 2);
  99. io_uring_cq_advance(&m_io_uring, count);
  100. }
  101. io_uring_queue_exit(&m_io_uring);
  102. return 0;
  103. }