send_recv.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Simple test case showing using send and recv through io_uring
  5. */
  6. #include <errno.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <arpa/inet.h>
  12. #include <sys/types.h>
  13. #include <sys/socket.h>
  14. #include <pthread.h>
  15. #include "liburing.h"
  16. #include "helpers.h"
  17. static char str[] = "This is a test of send and recv over io_uring!";
  18. #define MAX_MSG 128
  19. #define PORT 10202
  20. #define HOST "127.0.0.1"
  21. static int recv_prep(struct io_uring *ring, struct iovec *iov, int *sock,
  22. int registerfiles)
  23. {
  24. struct sockaddr_in saddr;
  25. struct io_uring_sqe *sqe;
  26. int sockfd, ret, val, use_fd;
  27. memset(&saddr, 0, sizeof(saddr));
  28. saddr.sin_family = AF_INET;
  29. saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  30. saddr.sin_port = htons(PORT);
  31. sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  32. if (sockfd < 0) {
  33. perror("socket");
  34. return 1;
  35. }
  36. val = 1;
  37. setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
  38. ret = bind(sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
  39. if (ret < 0) {
  40. perror("bind");
  41. goto err;
  42. }
  43. if (registerfiles) {
  44. ret = io_uring_register_files(ring, &sockfd, 1);
  45. if (ret) {
  46. fprintf(stderr, "file reg failed\n");
  47. goto err;
  48. }
  49. use_fd = 0;
  50. } else {
  51. use_fd = sockfd;
  52. }
  53. sqe = io_uring_get_sqe(ring);
  54. io_uring_prep_recv(sqe, use_fd, iov->iov_base, iov->iov_len, 0);
  55. if (registerfiles)
  56. sqe->flags |= IOSQE_FIXED_FILE;
  57. sqe->user_data = 2;
  58. ret = io_uring_submit(ring);
  59. if (ret <= 0) {
  60. fprintf(stderr, "submit failed: %d\n", ret);
  61. goto err;
  62. }
  63. *sock = sockfd;
  64. return 0;
  65. err:
  66. close(sockfd);
  67. return 1;
  68. }
  69. static int do_recv(struct io_uring *ring, struct iovec *iov)
  70. {
  71. struct io_uring_cqe *cqe;
  72. int ret;
  73. ret = io_uring_wait_cqe(ring, &cqe);
  74. if (ret) {
  75. fprintf(stdout, "wait_cqe: %d\n", ret);
  76. goto err;
  77. }
  78. if (cqe->res == -EINVAL) {
  79. fprintf(stdout, "recv not supported, skipping\n");
  80. return 0;
  81. }
  82. if (cqe->res < 0) {
  83. fprintf(stderr, "failed cqe: %d\n", cqe->res);
  84. goto err;
  85. }
  86. if (cqe->res -1 != strlen(str)) {
  87. fprintf(stderr, "got wrong length: %d/%d\n", cqe->res,
  88. (int) strlen(str) + 1);
  89. goto err;
  90. }
  91. if (strcmp(str, iov->iov_base)) {
  92. fprintf(stderr, "string mismatch\n");
  93. goto err;
  94. }
  95. return 0;
  96. err:
  97. return 1;
  98. }
  99. struct recv_data {
  100. pthread_mutex_t mutex;
  101. int use_sqthread;
  102. int registerfiles;
  103. };
  104. static void *recv_fn(void *data)
  105. {
  106. struct recv_data *rd = data;
  107. char buf[MAX_MSG + 1];
  108. struct iovec iov = {
  109. .iov_base = buf,
  110. .iov_len = sizeof(buf) - 1,
  111. };
  112. struct io_uring_params p = { };
  113. struct io_uring ring;
  114. int ret, sock;
  115. if (rd->use_sqthread)
  116. p.flags = IORING_SETUP_SQPOLL;
  117. ret = t_create_ring_params(1, &ring, &p);
  118. if (ret == T_SETUP_SKIP) {
  119. pthread_mutex_unlock(&rd->mutex);
  120. ret = 0;
  121. goto err;
  122. } else if (ret < 0) {
  123. pthread_mutex_unlock(&rd->mutex);
  124. goto err;
  125. }
  126. if (rd->use_sqthread && !rd->registerfiles) {
  127. if (!(p.features & IORING_FEAT_SQPOLL_NONFIXED)) {
  128. fprintf(stdout, "Non-registered SQPOLL not available, skipping\n");
  129. pthread_mutex_unlock(&rd->mutex);
  130. goto err;
  131. }
  132. }
  133. ret = recv_prep(&ring, &iov, &sock, rd->registerfiles);
  134. if (ret) {
  135. fprintf(stderr, "recv_prep failed: %d\n", ret);
  136. goto err;
  137. }
  138. pthread_mutex_unlock(&rd->mutex);
  139. ret = do_recv(&ring, &iov);
  140. close(sock);
  141. io_uring_queue_exit(&ring);
  142. err:
  143. return (void *)(intptr_t)ret;
  144. }
  145. static int do_send(void)
  146. {
  147. struct sockaddr_in saddr;
  148. struct iovec iov = {
  149. .iov_base = str,
  150. .iov_len = sizeof(str),
  151. };
  152. struct io_uring ring;
  153. struct io_uring_cqe *cqe;
  154. struct io_uring_sqe *sqe;
  155. int sockfd, ret;
  156. ret = io_uring_queue_init(1, &ring, 0);
  157. if (ret) {
  158. fprintf(stderr, "queue init failed: %d\n", ret);
  159. return 1;
  160. }
  161. memset(&saddr, 0, sizeof(saddr));
  162. saddr.sin_family = AF_INET;
  163. saddr.sin_port = htons(PORT);
  164. inet_pton(AF_INET, HOST, &saddr.sin_addr);
  165. sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  166. if (sockfd < 0) {
  167. perror("socket");
  168. goto err2;
  169. }
  170. ret = connect(sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
  171. if (ret < 0) {
  172. perror("connect");
  173. goto err;
  174. }
  175. sqe = io_uring_get_sqe(&ring);
  176. io_uring_prep_send(sqe, sockfd, iov.iov_base, iov.iov_len, 0);
  177. sqe->user_data = 1;
  178. ret = io_uring_submit(&ring);
  179. if (ret <= 0) {
  180. fprintf(stderr, "submit failed: %d\n", ret);
  181. goto err;
  182. }
  183. ret = io_uring_wait_cqe(&ring, &cqe);
  184. if (cqe->res == -EINVAL) {
  185. fprintf(stdout, "send not supported, skipping\n");
  186. goto err;
  187. }
  188. if (cqe->res != iov.iov_len) {
  189. fprintf(stderr, "failed cqe: %d\n", cqe->res);
  190. goto err;
  191. }
  192. close(sockfd);
  193. io_uring_queue_exit(&ring);
  194. return 0;
  195. err:
  196. close(sockfd);
  197. err2:
  198. io_uring_queue_exit(&ring);
  199. return 1;
  200. }
  201. static int test(int use_sqthread, int regfiles)
  202. {
  203. pthread_mutexattr_t attr;
  204. pthread_t recv_thread;
  205. struct recv_data rd;
  206. int ret;
  207. void *retval;
  208. pthread_mutexattr_init(&attr);
  209. pthread_mutexattr_setpshared(&attr, 1);
  210. pthread_mutex_init(&rd.mutex, &attr);
  211. pthread_mutex_lock(&rd.mutex);
  212. rd.use_sqthread = use_sqthread;
  213. rd.registerfiles = regfiles;
  214. ret = pthread_create(&recv_thread, NULL, recv_fn, &rd);
  215. if (ret) {
  216. fprintf(stderr, "Thread create failed: %d\n", ret);
  217. pthread_mutex_unlock(&rd.mutex);
  218. return 1;
  219. }
  220. pthread_mutex_lock(&rd.mutex);
  221. do_send();
  222. pthread_join(recv_thread, &retval);
  223. return (intptr_t)retval;
  224. }
  225. static int test_invalid(void)
  226. {
  227. struct io_uring ring;
  228. int ret, i;
  229. int fds[2];
  230. struct io_uring_cqe *cqe;
  231. struct io_uring_sqe *sqe;
  232. ret = t_create_ring(8, &ring, 0);
  233. if (ret)
  234. return ret;
  235. ret = t_create_socket_pair(fds, true);
  236. if (ret)
  237. return ret;
  238. sqe = io_uring_get_sqe(&ring);
  239. io_uring_prep_sendmsg(sqe, fds[0], NULL, MSG_WAITALL);
  240. sqe->flags |= IOSQE_ASYNC;
  241. sqe = io_uring_get_sqe(&ring);
  242. io_uring_prep_recvmsg(sqe, fds[1], NULL, 0);
  243. sqe->flags |= IOSQE_ASYNC;
  244. ret = io_uring_submit_and_wait(&ring, 2);
  245. if (ret != 2)
  246. return ret;
  247. for (i = 0; i < 2; i++) {
  248. ret = io_uring_peek_cqe(&ring, &cqe);
  249. if (ret || cqe->res != -EFAULT)
  250. return -1;
  251. io_uring_cqe_seen(&ring, cqe);
  252. }
  253. io_uring_queue_exit(&ring);
  254. close(fds[0]);
  255. close(fds[1]);
  256. return 0;
  257. }
  258. int main(int argc, char *argv[])
  259. {
  260. int ret;
  261. if (argc > 1)
  262. return 0;
  263. ret = test_invalid();
  264. if (ret) {
  265. fprintf(stderr, "test_invalid failed\n");
  266. return ret;
  267. }
  268. ret = test(0, 0);
  269. if (ret) {
  270. fprintf(stderr, "test sqthread=0 failed\n");
  271. return ret;
  272. }
  273. ret = test(1, 1);
  274. if (ret) {
  275. fprintf(stderr, "test sqthread=1 reg=1 failed\n");
  276. return ret;
  277. }
  278. ret = test(1, 0);
  279. if (ret) {
  280. fprintf(stderr, "test sqthread=1 reg=0 failed\n");
  281. return ret;
  282. }
  283. return 0;
  284. }