send_recv.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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, int async, int provide)
  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. if (async)
  58. sqe->flags |= IOSQE_ASYNC;
  59. if (provide)
  60. sqe->flags |= IOSQE_BUFFER_SELECT;
  61. sqe->user_data = 2;
  62. ret = io_uring_submit(ring);
  63. if (ret <= 0) {
  64. fprintf(stderr, "submit failed: %d\n", ret);
  65. goto err;
  66. }
  67. *sock = sockfd;
  68. return 0;
  69. err:
  70. close(sockfd);
  71. return 1;
  72. }
  73. static int do_recv(struct io_uring *ring, struct iovec *iov, int enobufs)
  74. {
  75. struct io_uring_cqe *cqe;
  76. int ret;
  77. ret = io_uring_wait_cqe(ring, &cqe);
  78. if (ret) {
  79. fprintf(stdout, "wait_cqe: %d\n", ret);
  80. return 1;
  81. }
  82. if (cqe->res == -EINVAL) {
  83. fprintf(stdout, "recv not supported, skipping\n");
  84. goto out;
  85. }
  86. if (cqe->res == -ENOBUFS && enobufs) {
  87. if (cqe->flags & IORING_CQE_F_SOCK_NONEMPTY) {
  88. fprintf(stdout, "NONEMPTY set on -ENOBUFS\n");
  89. goto err;
  90. }
  91. goto out;
  92. }
  93. if (cqe->res < 0) {
  94. fprintf(stderr, "failed cqe: %d\n", cqe->res);
  95. goto err;
  96. }
  97. if (cqe->res -1 != strlen(str)) {
  98. fprintf(stderr, "got wrong length: %d/%d\n", cqe->res,
  99. (int) strlen(str) + 1);
  100. goto err;
  101. }
  102. if (strcmp(str, iov->iov_base)) {
  103. fprintf(stderr, "string mismatch\n");
  104. goto err;
  105. }
  106. out:
  107. io_uring_cqe_seen(ring, cqe);
  108. return 0;
  109. err:
  110. io_uring_cqe_seen(ring, cqe);
  111. return 1;
  112. }
  113. struct recv_data {
  114. pthread_mutex_t mutex;
  115. int use_sqthread;
  116. int registerfiles;
  117. int async;
  118. int provide;
  119. };
  120. static void *recv_fn(void *data)
  121. {
  122. struct recv_data *rd = data;
  123. char buf[MAX_MSG + 1];
  124. struct iovec iov = {
  125. .iov_base = buf,
  126. .iov_len = sizeof(buf) - 1,
  127. };
  128. struct io_uring_params p = { };
  129. struct io_uring ring;
  130. int ret, sock;
  131. if (rd->use_sqthread)
  132. p.flags = IORING_SETUP_SQPOLL;
  133. ret = t_create_ring_params(1, &ring, &p);
  134. if (ret == T_SETUP_SKIP) {
  135. pthread_mutex_unlock(&rd->mutex);
  136. ret = 0;
  137. goto err;
  138. } else if (ret < 0) {
  139. pthread_mutex_unlock(&rd->mutex);
  140. goto err;
  141. }
  142. if (rd->use_sqthread && !rd->registerfiles) {
  143. if (!(p.features & IORING_FEAT_SQPOLL_NONFIXED)) {
  144. fprintf(stdout, "Non-registered SQPOLL not available, skipping\n");
  145. pthread_mutex_unlock(&rd->mutex);
  146. goto err;
  147. }
  148. }
  149. ret = recv_prep(&ring, &iov, &sock, rd->registerfiles, rd->async,
  150. rd->provide);
  151. if (ret) {
  152. fprintf(stderr, "recv_prep failed: %d\n", ret);
  153. goto err;
  154. }
  155. pthread_mutex_unlock(&rd->mutex);
  156. ret = do_recv(&ring, &iov, rd->provide);
  157. close(sock);
  158. io_uring_queue_exit(&ring);
  159. err:
  160. return (void *)(intptr_t)ret;
  161. }
  162. static int do_send(void)
  163. {
  164. struct sockaddr_in saddr;
  165. struct iovec iov = {
  166. .iov_base = str,
  167. .iov_len = sizeof(str),
  168. };
  169. struct io_uring ring;
  170. struct io_uring_cqe *cqe;
  171. struct io_uring_sqe *sqe;
  172. int sockfd, ret;
  173. ret = io_uring_queue_init(1, &ring, 0);
  174. if (ret) {
  175. fprintf(stderr, "queue init failed: %d\n", ret);
  176. return 1;
  177. }
  178. memset(&saddr, 0, sizeof(saddr));
  179. saddr.sin_family = AF_INET;
  180. saddr.sin_port = htons(PORT);
  181. inet_pton(AF_INET, HOST, &saddr.sin_addr);
  182. sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  183. if (sockfd < 0) {
  184. perror("socket");
  185. goto err2;
  186. }
  187. ret = connect(sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
  188. if (ret < 0) {
  189. perror("connect");
  190. goto err;
  191. }
  192. sqe = io_uring_get_sqe(&ring);
  193. io_uring_prep_send(sqe, sockfd, iov.iov_base, iov.iov_len, 0);
  194. sqe->user_data = 1;
  195. ret = io_uring_submit(&ring);
  196. if (ret <= 0) {
  197. fprintf(stderr, "submit failed: %d\n", ret);
  198. goto err;
  199. }
  200. ret = io_uring_wait_cqe(&ring, &cqe);
  201. if (cqe->res == -EINVAL) {
  202. fprintf(stdout, "send not supported, skipping\n");
  203. goto err;
  204. }
  205. if (cqe->res != iov.iov_len) {
  206. fprintf(stderr, "failed cqe: %d\n", cqe->res);
  207. goto err;
  208. }
  209. close(sockfd);
  210. io_uring_queue_exit(&ring);
  211. return 0;
  212. err:
  213. close(sockfd);
  214. err2:
  215. io_uring_queue_exit(&ring);
  216. return 1;
  217. }
  218. static int test(int use_sqthread, int regfiles, int async, int provide)
  219. {
  220. pthread_mutexattr_t attr;
  221. pthread_t recv_thread;
  222. struct recv_data rd;
  223. int ret;
  224. void *retval;
  225. pthread_mutexattr_init(&attr);
  226. pthread_mutexattr_setpshared(&attr, 1);
  227. pthread_mutex_init(&rd.mutex, &attr);
  228. pthread_mutex_lock(&rd.mutex);
  229. rd.use_sqthread = use_sqthread;
  230. rd.registerfiles = regfiles;
  231. rd.async = async;
  232. rd.provide = provide;
  233. ret = pthread_create(&recv_thread, NULL, recv_fn, &rd);
  234. if (ret) {
  235. fprintf(stderr, "Thread create failed: %d\n", ret);
  236. pthread_mutex_unlock(&rd.mutex);
  237. return 1;
  238. }
  239. pthread_mutex_lock(&rd.mutex);
  240. do_send();
  241. pthread_join(recv_thread, &retval);
  242. return (intptr_t)retval;
  243. }
  244. static int test_invalid(void)
  245. {
  246. struct io_uring ring;
  247. int ret, i;
  248. int fds[2];
  249. struct io_uring_cqe *cqe;
  250. struct io_uring_sqe *sqe;
  251. ret = t_create_ring(8, &ring, IORING_SETUP_SUBMIT_ALL);
  252. if (ret) {
  253. if (ret == -EINVAL)
  254. return 0;
  255. return ret;
  256. }
  257. ret = t_create_socket_pair(fds, true);
  258. if (ret)
  259. return ret;
  260. sqe = io_uring_get_sqe(&ring);
  261. io_uring_prep_sendmsg(sqe, fds[0], NULL, MSG_WAITALL);
  262. sqe->flags |= IOSQE_ASYNC;
  263. sqe = io_uring_get_sqe(&ring);
  264. io_uring_prep_recvmsg(sqe, fds[1], NULL, 0);
  265. sqe->flags |= IOSQE_ASYNC;
  266. ret = io_uring_submit_and_wait(&ring, 2);
  267. if (ret != 2)
  268. return ret;
  269. for (i = 0; i < 2; i++) {
  270. ret = io_uring_peek_cqe(&ring, &cqe);
  271. if (ret || cqe->res != -EFAULT)
  272. return -1;
  273. io_uring_cqe_seen(&ring, cqe);
  274. }
  275. io_uring_queue_exit(&ring);
  276. close(fds[0]);
  277. close(fds[1]);
  278. return 0;
  279. }
  280. int main(int argc, char *argv[])
  281. {
  282. int ret;
  283. if (argc > 1)
  284. return 0;
  285. ret = test_invalid();
  286. if (ret) {
  287. fprintf(stderr, "test_invalid failed\n");
  288. return ret;
  289. }
  290. ret = test(0, 0, 1, 1);
  291. if (ret) {
  292. fprintf(stderr, "test sqthread=0 1 1 failed\n");
  293. return ret;
  294. }
  295. ret = test(1, 1, 1, 1);
  296. if (ret) {
  297. fprintf(stderr, "test sqthread=1 reg=1 1 1 failed\n");
  298. return ret;
  299. }
  300. ret = test(1, 0, 1, 1);
  301. if (ret) {
  302. fprintf(stderr, "test sqthread=1 reg=0 1 1 failed\n");
  303. return ret;
  304. }
  305. ret = test(0, 0, 0, 1);
  306. if (ret) {
  307. fprintf(stderr, "test sqthread=0 0 1 failed\n");
  308. return ret;
  309. }
  310. ret = test(1, 1, 0, 1);
  311. if (ret) {
  312. fprintf(stderr, "test sqthread=1 reg=1 0 1 failed\n");
  313. return ret;
  314. }
  315. ret = test(1, 0, 0, 1);
  316. if (ret) {
  317. fprintf(stderr, "test sqthread=1 reg=0 0 1 failed\n");
  318. return ret;
  319. }
  320. ret = test(0, 0, 1, 0);
  321. if (ret) {
  322. fprintf(stderr, "test sqthread=0 0 1 failed\n");
  323. return ret;
  324. }
  325. ret = test(1, 1, 1, 0);
  326. if (ret) {
  327. fprintf(stderr, "test sqthread=1 reg=1 1 0 failed\n");
  328. return ret;
  329. }
  330. ret = test(1, 0, 1, 0);
  331. if (ret) {
  332. fprintf(stderr, "test sqthread=1 reg=0 1 0 failed\n");
  333. return ret;
  334. }
  335. ret = test(0, 0, 0, 0);
  336. if (ret) {
  337. fprintf(stderr, "test sqthread=0 0 0 failed\n");
  338. return ret;
  339. }
  340. ret = test(1, 1, 0, 0);
  341. if (ret) {
  342. fprintf(stderr, "test sqthread=1 reg=1 0 0 failed\n");
  343. return ret;
  344. }
  345. ret = test(1, 0, 0, 0);
  346. if (ret) {
  347. fprintf(stderr, "test sqthread=1 reg=0 0 0 failed\n");
  348. return ret;
  349. }
  350. return 0;
  351. }