socket.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Simple test case using the socket op
  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 <assert.h>
  16. #include "liburing.h"
  17. #include "helpers.h"
  18. static char str[] = "This is a test of send and recv over io_uring!";
  19. #define MAX_MSG 128
  20. #define HOST "127.0.0.1"
  21. static int no_socket;
  22. static __be32 g_port;
  23. static int recv_prep(struct io_uring *ring, struct iovec *iov, int *sock,
  24. int registerfiles)
  25. {
  26. struct sockaddr_in saddr;
  27. struct io_uring_sqe *sqe;
  28. int sockfd, ret, val, use_fd;
  29. memset(&saddr, 0, sizeof(saddr));
  30. saddr.sin_family = AF_INET;
  31. saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  32. sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  33. if (sockfd < 0) {
  34. perror("socket");
  35. return 1;
  36. }
  37. val = 1;
  38. setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
  39. if (t_bind_ephemeral_port(sockfd, &saddr)) {
  40. perror("bind");
  41. goto err;
  42. }
  43. g_port = saddr.sin_port;
  44. if (registerfiles) {
  45. ret = io_uring_register_files(ring, &sockfd, 1);
  46. if (ret) {
  47. fprintf(stderr, "file reg failed\n");
  48. goto err;
  49. }
  50. use_fd = 0;
  51. } else {
  52. use_fd = sockfd;
  53. }
  54. sqe = io_uring_get_sqe(ring);
  55. io_uring_prep_recv(sqe, use_fd, iov->iov_base, iov->iov_len, 0);
  56. if (registerfiles)
  57. sqe->flags |= IOSQE_FIXED_FILE;
  58. sqe->user_data = 2;
  59. ret = io_uring_submit(ring);
  60. if (ret <= 0) {
  61. fprintf(stderr, "submit failed: %d\n", ret);
  62. goto err;
  63. }
  64. *sock = sockfd;
  65. return 0;
  66. err:
  67. close(sockfd);
  68. return 1;
  69. }
  70. static int do_recv(struct io_uring *ring, struct iovec *iov)
  71. {
  72. struct io_uring_cqe *cqe;
  73. int ret;
  74. ret = io_uring_wait_cqe(ring, &cqe);
  75. if (ret) {
  76. fprintf(stdout, "wait_cqe: %d\n", ret);
  77. goto err;
  78. }
  79. if (cqe->res == -EINVAL) {
  80. fprintf(stdout, "recv not supported, skipping\n");
  81. return 0;
  82. }
  83. if (cqe->res < 0) {
  84. fprintf(stderr, "failed cqe: %d\n", cqe->res);
  85. goto err;
  86. }
  87. if (cqe->res -1 != strlen(str)) {
  88. fprintf(stderr, "got wrong length: %d/%d\n", cqe->res,
  89. (int) strlen(str) + 1);
  90. goto err;
  91. }
  92. if (strcmp(str, iov->iov_base)) {
  93. fprintf(stderr, "string mismatch\n");
  94. goto err;
  95. }
  96. return 0;
  97. err:
  98. return 1;
  99. }
  100. struct recv_data {
  101. pthread_mutex_t mutex;
  102. int use_sqthread;
  103. int registerfiles;
  104. };
  105. static void *recv_fn(void *data)
  106. {
  107. struct recv_data *rd = data;
  108. char buf[MAX_MSG + 1];
  109. struct iovec iov = {
  110. .iov_base = buf,
  111. .iov_len = sizeof(buf) - 1,
  112. };
  113. struct io_uring_params p = { };
  114. struct io_uring ring;
  115. int ret, sock;
  116. if (rd->use_sqthread)
  117. p.flags = IORING_SETUP_SQPOLL;
  118. ret = t_create_ring_params(1, &ring, &p);
  119. if (ret == T_SETUP_SKIP) {
  120. pthread_mutex_unlock(&rd->mutex);
  121. ret = 0;
  122. goto err;
  123. } else if (ret < 0) {
  124. pthread_mutex_unlock(&rd->mutex);
  125. goto err;
  126. }
  127. if (rd->use_sqthread && !rd->registerfiles) {
  128. if (!(p.features & IORING_FEAT_SQPOLL_NONFIXED)) {
  129. fprintf(stdout, "Non-registered SQPOLL not available, skipping\n");
  130. pthread_mutex_unlock(&rd->mutex);
  131. goto err;
  132. }
  133. }
  134. ret = recv_prep(&ring, &iov, &sock, rd->registerfiles);
  135. if (ret) {
  136. fprintf(stderr, "recv_prep failed: %d\n", ret);
  137. goto err;
  138. }
  139. pthread_mutex_unlock(&rd->mutex);
  140. ret = do_recv(&ring, &iov);
  141. close(sock);
  142. io_uring_queue_exit(&ring);
  143. err:
  144. return (void *)(intptr_t)ret;
  145. }
  146. static int fallback_send(struct io_uring *ring, struct sockaddr_in *saddr)
  147. {
  148. struct iovec iov = {
  149. .iov_base = str,
  150. .iov_len = sizeof(str),
  151. };
  152. struct io_uring_cqe *cqe;
  153. struct io_uring_sqe *sqe;
  154. int sockfd, ret;
  155. sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  156. if (sockfd < 0) {
  157. perror("socket");
  158. return 1;
  159. }
  160. ret = connect(sockfd, (struct sockaddr *)saddr, sizeof(*saddr));
  161. if (ret < 0) {
  162. perror("connect");
  163. return 1;
  164. }
  165. sqe = io_uring_get_sqe(ring);
  166. io_uring_prep_send(sqe, sockfd, iov.iov_base, iov.iov_len, 0);
  167. sqe->user_data = 1;
  168. ret = io_uring_submit(ring);
  169. if (ret <= 0) {
  170. fprintf(stderr, "submit failed: %d\n", ret);
  171. goto err;
  172. }
  173. ret = io_uring_wait_cqe(ring, &cqe);
  174. if (cqe->res == -EINVAL) {
  175. fprintf(stdout, "send not supported, skipping\n");
  176. close(sockfd);
  177. return 0;
  178. }
  179. if (cqe->res != iov.iov_len) {
  180. fprintf(stderr, "failed cqe: %d\n", cqe->res);
  181. goto err;
  182. }
  183. close(sockfd);
  184. return 0;
  185. err:
  186. close(sockfd);
  187. return 1;
  188. }
  189. static int do_send(int socket_direct, int alloc)
  190. {
  191. struct sockaddr_in saddr;
  192. struct iovec iov = {
  193. .iov_base = str,
  194. .iov_len = sizeof(str),
  195. };
  196. struct io_uring ring;
  197. struct io_uring_cqe *cqe;
  198. struct io_uring_sqe *sqe;
  199. int sockfd, ret, fd = -1;
  200. ret = io_uring_queue_init(1, &ring, 0);
  201. if (ret) {
  202. fprintf(stderr, "queue init failed: %d\n", ret);
  203. return 1;
  204. }
  205. if (socket_direct) {
  206. ret = io_uring_register_files(&ring, &fd, 1);
  207. if (ret) {
  208. fprintf(stderr, "file register %d\n", ret);
  209. return 1;
  210. }
  211. }
  212. assert(g_port != 0);
  213. memset(&saddr, 0, sizeof(saddr));
  214. saddr.sin_family = AF_INET;
  215. saddr.sin_port = g_port;
  216. inet_pton(AF_INET, HOST, &saddr.sin_addr);
  217. sqe = io_uring_get_sqe(&ring);
  218. if (socket_direct) {
  219. unsigned file_index = 0;
  220. if (alloc)
  221. file_index = IORING_FILE_INDEX_ALLOC - 1;
  222. io_uring_prep_socket_direct(sqe, AF_INET, SOCK_DGRAM, 0,
  223. file_index, 0);
  224. } else {
  225. io_uring_prep_socket(sqe, AF_INET, SOCK_DGRAM, 0, 0);
  226. }
  227. ret = io_uring_submit(&ring);
  228. if (ret != 1) {
  229. fprintf(stderr, "socket submit: %d\n", ret);
  230. return 1;
  231. }
  232. ret = io_uring_wait_cqe(&ring, &cqe);
  233. if (ret) {
  234. fprintf(stderr, "wait_cqe: %d\n", ret);
  235. return 1;
  236. }
  237. if (cqe->res < 0) {
  238. if (cqe->res == -EINVAL) {
  239. no_socket = 1;
  240. io_uring_cqe_seen(&ring, cqe);
  241. return fallback_send(&ring, &saddr);
  242. }
  243. fprintf(stderr, "socket res: %d\n", ret);
  244. return 1;
  245. }
  246. sockfd = cqe->res;
  247. if (socket_direct && !alloc)
  248. sockfd = 0;
  249. io_uring_cqe_seen(&ring, cqe);
  250. sqe = io_uring_get_sqe(&ring);
  251. io_uring_prep_connect(sqe, sockfd, (struct sockaddr *) &saddr,
  252. sizeof(saddr));
  253. if (socket_direct)
  254. sqe->flags |= IOSQE_FIXED_FILE;
  255. ret = io_uring_submit(&ring);
  256. if (ret != 1) {
  257. fprintf(stderr, "connect submit: %d\n", ret);
  258. return 1;
  259. }
  260. ret = io_uring_wait_cqe(&ring, &cqe);
  261. if (ret) {
  262. fprintf(stderr, "wait_cqe: %d\n", ret);
  263. return 1;
  264. }
  265. if (cqe->res < 0) {
  266. fprintf(stderr, "connect res: %d\n", cqe->res);
  267. return 1;
  268. }
  269. io_uring_cqe_seen(&ring, cqe);
  270. sqe = io_uring_get_sqe(&ring);
  271. io_uring_prep_send(sqe, sockfd, iov.iov_base, iov.iov_len, 0);
  272. sqe->user_data = 1;
  273. if (socket_direct)
  274. sqe->flags |= IOSQE_FIXED_FILE;
  275. ret = io_uring_submit(&ring);
  276. if (ret <= 0) {
  277. fprintf(stderr, "submit failed: %d\n", ret);
  278. goto err;
  279. }
  280. ret = io_uring_wait_cqe(&ring, &cqe);
  281. if (cqe->res == -EINVAL) {
  282. fprintf(stdout, "send not supported, skipping\n");
  283. close(sockfd);
  284. return 0;
  285. }
  286. if (cqe->res != iov.iov_len) {
  287. fprintf(stderr, "failed cqe: %d\n", cqe->res);
  288. goto err;
  289. }
  290. close(sockfd);
  291. return 0;
  292. err:
  293. close(sockfd);
  294. return 1;
  295. }
  296. static int test(int use_sqthread, int regfiles, int socket_direct, int alloc)
  297. {
  298. pthread_mutexattr_t attr;
  299. pthread_t recv_thread;
  300. struct recv_data rd;
  301. int ret;
  302. void *retval;
  303. pthread_mutexattr_init(&attr);
  304. pthread_mutexattr_setpshared(&attr, 1);
  305. pthread_mutex_init(&rd.mutex, &attr);
  306. pthread_mutex_lock(&rd.mutex);
  307. rd.use_sqthread = use_sqthread;
  308. rd.registerfiles = regfiles;
  309. ret = pthread_create(&recv_thread, NULL, recv_fn, &rd);
  310. if (ret) {
  311. fprintf(stderr, "Thread create failed: %d\n", ret);
  312. pthread_mutex_unlock(&rd.mutex);
  313. return 1;
  314. }
  315. pthread_mutex_lock(&rd.mutex);
  316. do_send(socket_direct, alloc);
  317. pthread_join(recv_thread, &retval);
  318. return (intptr_t)retval;
  319. }
  320. int main(int argc, char *argv[])
  321. {
  322. int ret;
  323. if (argc > 1)
  324. return 0;
  325. ret = test(0, 0, 0, 0);
  326. if (ret) {
  327. fprintf(stderr, "test sqthread=0 failed\n");
  328. return ret;
  329. }
  330. if (no_socket)
  331. return 0;
  332. ret = test(1, 1, 0, 0);
  333. if (ret) {
  334. fprintf(stderr, "test sqthread=1 reg=1 failed\n");
  335. return ret;
  336. }
  337. ret = test(1, 0, 0, 0);
  338. if (ret) {
  339. fprintf(stderr, "test sqthread=1 reg=0 failed\n");
  340. return ret;
  341. }
  342. ret = test(0, 0, 1, 0);
  343. if (ret) {
  344. fprintf(stderr, "test sqthread=0 direct=1 failed\n");
  345. return ret;
  346. }
  347. ret = test(0, 0, 1, 1);
  348. if (ret) {
  349. fprintf(stderr, "test sqthread=0 direct=alloc failed\n");
  350. return ret;
  351. }
  352. return 0;
  353. }