helpers.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: Helpers for tests.
  5. */
  6. #include <stdlib.h>
  7. #include <assert.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <fcntl.h>
  11. #include <unistd.h>
  12. #include <stdarg.h>
  13. #include <sys/types.h>
  14. #include <arpa/inet.h>
  15. #include <netinet/ip.h>
  16. #include <netinet/tcp.h>
  17. #include "helpers.h"
  18. #include "liburing.h"
  19. /*
  20. * Helper for allocating memory in tests.
  21. */
  22. void *t_malloc(size_t size)
  23. {
  24. void *ret;
  25. ret = malloc(size);
  26. assert(ret);
  27. return ret;
  28. }
  29. /*
  30. * Helper for binding socket to an ephemeral port.
  31. * The port number to be bound is returned in @addr->sin_port.
  32. */
  33. int t_bind_ephemeral_port(int fd, struct sockaddr_in *addr)
  34. {
  35. socklen_t addrlen;
  36. addr->sin_port = 0;
  37. if (bind(fd, (struct sockaddr *)addr, sizeof(*addr)))
  38. return -errno;
  39. addrlen = sizeof(*addr);
  40. assert(!getsockname(fd, (struct sockaddr *)addr, &addrlen));
  41. assert(addr->sin_port != 0);
  42. return 0;
  43. }
  44. /*
  45. * Helper for allocating size bytes aligned on a boundary.
  46. */
  47. void t_posix_memalign(void **memptr, size_t alignment, size_t size)
  48. {
  49. int ret;
  50. ret = posix_memalign(memptr, alignment, size);
  51. assert(!ret);
  52. }
  53. /*
  54. * Helper for allocating space for an array of nmemb elements
  55. * with size bytes for each element.
  56. */
  57. void *t_calloc(size_t nmemb, size_t size)
  58. {
  59. void *ret;
  60. ret = calloc(nmemb, size);
  61. assert(ret);
  62. return ret;
  63. }
  64. /*
  65. * Helper for creating file and write @size byte buf with 0xaa value in the file.
  66. */
  67. static void __t_create_file(const char *file, size_t size, char pattern)
  68. {
  69. ssize_t ret;
  70. char *buf;
  71. int fd;
  72. buf = t_malloc(size);
  73. memset(buf, pattern, size);
  74. fd = open(file, O_WRONLY | O_CREAT, 0644);
  75. assert(fd >= 0);
  76. ret = write(fd, buf, size);
  77. fsync(fd);
  78. close(fd);
  79. free(buf);
  80. assert(ret == size);
  81. }
  82. void t_create_file(const char *file, size_t size)
  83. {
  84. __t_create_file(file, size, 0xaa);
  85. }
  86. void t_create_file_pattern(const char *file, size_t size, char pattern)
  87. {
  88. __t_create_file(file, size, pattern);
  89. }
  90. /*
  91. * Helper for creating @buf_num number of iovec
  92. * with @buf_size bytes buffer of each iovec.
  93. */
  94. struct iovec *t_create_buffers(size_t buf_num, size_t buf_size)
  95. {
  96. struct iovec *vecs;
  97. int i;
  98. vecs = t_malloc(buf_num * sizeof(struct iovec));
  99. for (i = 0; i < buf_num; i++) {
  100. t_posix_memalign(&vecs[i].iov_base, buf_size, buf_size);
  101. vecs[i].iov_len = buf_size;
  102. }
  103. return vecs;
  104. }
  105. /*
  106. * Helper for setting up an io_uring instance, skipping if the given user isn't
  107. * allowed to.
  108. */
  109. enum t_setup_ret t_create_ring_params(int depth, struct io_uring *ring,
  110. struct io_uring_params *p)
  111. {
  112. int ret;
  113. ret = io_uring_queue_init_params(depth, ring, p);
  114. if (!ret)
  115. return T_SETUP_OK;
  116. if ((p->flags & IORING_SETUP_SQPOLL) && ret == -EPERM && geteuid()) {
  117. fprintf(stdout, "SQPOLL skipped for regular user\n");
  118. return T_SETUP_SKIP;
  119. }
  120. if (ret != -EINVAL)
  121. fprintf(stderr, "queue_init: %s\n", strerror(-ret));
  122. return ret;
  123. }
  124. enum t_setup_ret t_create_ring(int depth, struct io_uring *ring,
  125. unsigned int flags)
  126. {
  127. struct io_uring_params p = { };
  128. p.flags = flags;
  129. return t_create_ring_params(depth, ring, &p);
  130. }
  131. enum t_setup_ret t_register_buffers(struct io_uring *ring,
  132. const struct iovec *iovecs,
  133. unsigned nr_iovecs)
  134. {
  135. int ret;
  136. ret = io_uring_register_buffers(ring, iovecs, nr_iovecs);
  137. if (!ret)
  138. return T_SETUP_OK;
  139. if ((ret == -EPERM || ret == -ENOMEM) && geteuid()) {
  140. fprintf(stdout, "too large non-root buffer registration, skip\n");
  141. return T_SETUP_SKIP;
  142. }
  143. fprintf(stderr, "buffer register failed: %s\n", strerror(-ret));
  144. return ret;
  145. }
  146. int t_create_socket_pair(int fd[2], bool stream)
  147. {
  148. int ret;
  149. int type = stream ? SOCK_STREAM : SOCK_DGRAM;
  150. int val;
  151. struct sockaddr_in serv_addr;
  152. struct sockaddr *paddr;
  153. socklen_t paddrlen;
  154. type |= SOCK_CLOEXEC;
  155. fd[0] = socket(AF_INET, type, 0);
  156. if (fd[0] < 0)
  157. return errno;
  158. fd[1] = socket(AF_INET, type, 0);
  159. if (fd[1] < 0) {
  160. ret = errno;
  161. close(fd[0]);
  162. return ret;
  163. }
  164. val = 1;
  165. if (setsockopt(fd[0], SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)))
  166. goto errno_cleanup;
  167. memset(&serv_addr, 0, sizeof(serv_addr));
  168. serv_addr.sin_family = AF_INET;
  169. serv_addr.sin_port = 0;
  170. inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr);
  171. paddr = (struct sockaddr *)&serv_addr;
  172. paddrlen = sizeof(serv_addr);
  173. if (bind(fd[0], paddr, paddrlen)) {
  174. fprintf(stderr, "bind failed\n");
  175. goto errno_cleanup;
  176. }
  177. if (stream && listen(fd[0], 16)) {
  178. fprintf(stderr, "listen failed\n");
  179. goto errno_cleanup;
  180. }
  181. if (getsockname(fd[0], (struct sockaddr *)&serv_addr,
  182. (socklen_t *)&paddrlen)) {
  183. fprintf(stderr, "getsockname failed\n");
  184. goto errno_cleanup;
  185. }
  186. inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr);
  187. if (connect(fd[1], (struct sockaddr *)&serv_addr, paddrlen)) {
  188. fprintf(stderr, "connect failed\n");
  189. goto errno_cleanup;
  190. }
  191. if (!stream) {
  192. /* connect the other udp side */
  193. if (getsockname(fd[1], (struct sockaddr *)&serv_addr,
  194. (socklen_t *)&paddrlen)) {
  195. fprintf(stderr, "getsockname failed\n");
  196. goto errno_cleanup;
  197. }
  198. inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr);
  199. if (connect(fd[0], (struct sockaddr *)&serv_addr, paddrlen)) {
  200. fprintf(stderr, "connect failed\n");
  201. goto errno_cleanup;
  202. }
  203. return 0;
  204. }
  205. /* for stream case we must accept and cleanup the listen socket */
  206. ret = accept(fd[0], NULL, NULL);
  207. if (ret < 0)
  208. goto errno_cleanup;
  209. close(fd[0]);
  210. fd[0] = ret;
  211. return 0;
  212. errno_cleanup:
  213. ret = errno;
  214. close(fd[0]);
  215. close(fd[1]);
  216. return ret;
  217. }
  218. bool t_probe_defer_taskrun(void)
  219. {
  220. struct io_uring ring;
  221. int ret;
  222. ret = io_uring_queue_init(1, &ring, IORING_SETUP_SINGLE_ISSUER |
  223. IORING_SETUP_DEFER_TASKRUN);
  224. if (ret < 0)
  225. return false;
  226. io_uring_queue_exit(&ring);
  227. return true;
  228. }
  229. /*
  230. * Sync internal state with kernel ring state on the SQ side. Returns the
  231. * number of pending items in the SQ ring, for the shared ring.
  232. */
  233. unsigned __io_uring_flush_sq(struct io_uring *ring)
  234. {
  235. struct io_uring_sq *sq = &ring->sq;
  236. unsigned tail = sq->sqe_tail;
  237. if (sq->sqe_head != tail) {
  238. sq->sqe_head = tail;
  239. /*
  240. * Ensure kernel sees the SQE updates before the tail update.
  241. */
  242. if (!(ring->flags & IORING_SETUP_SQPOLL))
  243. IO_URING_WRITE_ONCE(*sq->ktail, tail);
  244. else
  245. io_uring_smp_store_release(sq->ktail, tail);
  246. }
  247. /*
  248. * This _may_ look problematic, as we're not supposed to be reading
  249. * SQ->head without acquire semantics. When we're in SQPOLL mode, the
  250. * kernel submitter could be updating this right now. For non-SQPOLL,
  251. * task itself does it, and there's no potential race. But even for
  252. * SQPOLL, the load is going to be potentially out-of-date the very
  253. * instant it's done, regardless or whether or not it's done
  254. * atomically. Worst case, we're going to be over-estimating what
  255. * we can submit. The point is, we need to be able to deal with this
  256. * situation regardless of any perceived atomicity.
  257. */
  258. return tail - *sq->khead;
  259. }
  260. /*
  261. * Implementation of error(3), prints an error message and exits.
  262. */
  263. void t_error(int status, int errnum, const char *format, ...)
  264. {
  265. va_list args;
  266. va_start(args, format);
  267. vfprintf(stderr, format, args);
  268. if (errnum)
  269. fprintf(stderr, ": %s", strerror(errnum));
  270. fprintf(stderr, "\n");
  271. va_end(args);
  272. exit(status);
  273. }