helpers.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. int ret;
  37. addr->sin_port = 0;
  38. if (bind(fd, (struct sockaddr *)addr, sizeof(*addr)))
  39. return -errno;
  40. addrlen = sizeof(*addr);
  41. ret = getsockname(fd, (struct sockaddr *)addr, &addrlen);
  42. assert(!ret);
  43. assert(addr->sin_port != 0);
  44. return 0;
  45. }
  46. /*
  47. * Helper for allocating size bytes aligned on a boundary.
  48. */
  49. void t_posix_memalign(void **memptr, size_t alignment, size_t size)
  50. {
  51. int ret;
  52. ret = posix_memalign(memptr, alignment, size);
  53. assert(!ret);
  54. }
  55. /*
  56. * Helper for allocating space for an array of nmemb elements
  57. * with size bytes for each element.
  58. */
  59. void *t_calloc(size_t nmemb, size_t size)
  60. {
  61. void *ret;
  62. ret = calloc(nmemb, size);
  63. assert(ret);
  64. return ret;
  65. }
  66. /*
  67. * Helper for creating file and write @size byte buf with 0xaa value in the file.
  68. */
  69. static void __t_create_file(const char *file, size_t size, char pattern)
  70. {
  71. ssize_t ret;
  72. char *buf;
  73. int fd;
  74. buf = t_malloc(size);
  75. memset(buf, pattern, size);
  76. fd = open(file, O_WRONLY | O_CREAT, 0644);
  77. assert(fd >= 0);
  78. ret = write(fd, buf, size);
  79. fsync(fd);
  80. close(fd);
  81. free(buf);
  82. assert(ret == size);
  83. }
  84. void t_create_file(const char *file, size_t size)
  85. {
  86. __t_create_file(file, size, 0xaa);
  87. }
  88. void t_create_file_pattern(const char *file, size_t size, char pattern)
  89. {
  90. __t_create_file(file, size, pattern);
  91. }
  92. /*
  93. * Helper for creating @buf_num number of iovec
  94. * with @buf_size bytes buffer of each iovec.
  95. */
  96. struct iovec *t_create_buffers(size_t buf_num, size_t buf_size)
  97. {
  98. struct iovec *vecs;
  99. int i;
  100. vecs = t_malloc(buf_num * sizeof(struct iovec));
  101. for (i = 0; i < buf_num; i++) {
  102. t_posix_memalign(&vecs[i].iov_base, buf_size, buf_size);
  103. vecs[i].iov_len = buf_size;
  104. }
  105. return vecs;
  106. }
  107. /*
  108. * Helper for setting up an io_uring instance, skipping if the given user isn't
  109. * allowed to.
  110. */
  111. enum t_setup_ret t_create_ring_params(int depth, struct io_uring *ring,
  112. struct io_uring_params *p)
  113. {
  114. int ret;
  115. ret = io_uring_queue_init_params(depth, ring, p);
  116. if (!ret)
  117. return T_SETUP_OK;
  118. if ((p->flags & IORING_SETUP_SQPOLL) && ret == -EPERM && geteuid()) {
  119. fprintf(stdout, "SQPOLL skipped for regular user\n");
  120. return T_SETUP_SKIP;
  121. }
  122. if (ret != -EINVAL)
  123. fprintf(stderr, "queue_init: %s\n", strerror(-ret));
  124. return ret;
  125. }
  126. enum t_setup_ret t_create_ring(int depth, struct io_uring *ring,
  127. unsigned int flags)
  128. {
  129. struct io_uring_params p = { };
  130. p.flags = flags;
  131. return t_create_ring_params(depth, ring, &p);
  132. }
  133. enum t_setup_ret t_register_buffers(struct io_uring *ring,
  134. const struct iovec *iovecs,
  135. unsigned nr_iovecs)
  136. {
  137. int ret;
  138. ret = io_uring_register_buffers(ring, iovecs, nr_iovecs);
  139. if (!ret)
  140. return T_SETUP_OK;
  141. if ((ret == -EPERM || ret == -ENOMEM) && geteuid()) {
  142. fprintf(stdout, "too large non-root buffer registration, skip\n");
  143. return T_SETUP_SKIP;
  144. }
  145. fprintf(stderr, "buffer register failed: %s\n", strerror(-ret));
  146. return ret;
  147. }
  148. int t_create_socket_pair(int fd[2], bool stream)
  149. {
  150. int ret;
  151. int type = stream ? SOCK_STREAM : SOCK_DGRAM;
  152. int val;
  153. struct sockaddr_in serv_addr;
  154. struct sockaddr *paddr;
  155. socklen_t paddrlen;
  156. type |= SOCK_CLOEXEC;
  157. fd[0] = socket(AF_INET, type, 0);
  158. if (fd[0] < 0)
  159. return errno;
  160. fd[1] = socket(AF_INET, type, 0);
  161. if (fd[1] < 0) {
  162. ret = errno;
  163. close(fd[0]);
  164. return ret;
  165. }
  166. val = 1;
  167. if (setsockopt(fd[0], SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)))
  168. goto errno_cleanup;
  169. memset(&serv_addr, 0, sizeof(serv_addr));
  170. serv_addr.sin_family = AF_INET;
  171. serv_addr.sin_port = 0;
  172. inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr);
  173. paddr = (struct sockaddr *)&serv_addr;
  174. paddrlen = sizeof(serv_addr);
  175. if (bind(fd[0], paddr, paddrlen)) {
  176. fprintf(stderr, "bind failed\n");
  177. goto errno_cleanup;
  178. }
  179. if (stream && listen(fd[0], 16)) {
  180. fprintf(stderr, "listen failed\n");
  181. goto errno_cleanup;
  182. }
  183. if (getsockname(fd[0], (struct sockaddr *)&serv_addr,
  184. (socklen_t *)&paddrlen)) {
  185. fprintf(stderr, "getsockname failed\n");
  186. goto errno_cleanup;
  187. }
  188. inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr);
  189. if (connect(fd[1], (struct sockaddr *)&serv_addr, paddrlen)) {
  190. fprintf(stderr, "connect failed\n");
  191. goto errno_cleanup;
  192. }
  193. if (!stream) {
  194. /* connect the other udp side */
  195. if (getsockname(fd[1], (struct sockaddr *)&serv_addr,
  196. (socklen_t *)&paddrlen)) {
  197. fprintf(stderr, "getsockname failed\n");
  198. goto errno_cleanup;
  199. }
  200. inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr);
  201. if (connect(fd[0], (struct sockaddr *)&serv_addr, paddrlen)) {
  202. fprintf(stderr, "connect failed\n");
  203. goto errno_cleanup;
  204. }
  205. return 0;
  206. }
  207. /* for stream case we must accept and cleanup the listen socket */
  208. ret = accept(fd[0], NULL, NULL);
  209. if (ret < 0)
  210. goto errno_cleanup;
  211. close(fd[0]);
  212. fd[0] = ret;
  213. return 0;
  214. errno_cleanup:
  215. ret = errno;
  216. close(fd[0]);
  217. close(fd[1]);
  218. return ret;
  219. }
  220. bool t_probe_defer_taskrun(void)
  221. {
  222. struct io_uring ring;
  223. int ret;
  224. ret = io_uring_queue_init(1, &ring, IORING_SETUP_SINGLE_ISSUER |
  225. IORING_SETUP_DEFER_TASKRUN);
  226. if (ret < 0)
  227. return false;
  228. io_uring_queue_exit(&ring);
  229. return true;
  230. }
  231. /*
  232. * Sync internal state with kernel ring state on the SQ side. Returns the
  233. * number of pending items in the SQ ring, for the shared ring.
  234. */
  235. unsigned __io_uring_flush_sq(struct io_uring *ring)
  236. {
  237. struct io_uring_sq *sq = &ring->sq;
  238. unsigned tail = sq->sqe_tail;
  239. if (sq->sqe_head != tail) {
  240. sq->sqe_head = tail;
  241. /*
  242. * Ensure kernel sees the SQE updates before the tail update.
  243. */
  244. if (!(ring->flags & IORING_SETUP_SQPOLL))
  245. *sq->ktail = tail;
  246. else
  247. io_uring_smp_store_release(sq->ktail, tail);
  248. }
  249. /*
  250. * This load needs to be atomic, since sq->khead is written concurrently
  251. * by the kernel, but it doesn't need to be load_acquire, since the
  252. * kernel doesn't store to the submission queue; it advances khead just
  253. * to indicate that it's finished reading the submission queue entries
  254. * so they're available for us to write to.
  255. */
  256. return tail - IO_URING_READ_ONCE(*sq->khead);
  257. }
  258. /*
  259. * Implementation of error(3), prints an error message and exits.
  260. */
  261. void t_error(int status, int errnum, const char *format, ...)
  262. {
  263. va_list args;
  264. va_start(args, format);
  265. vfprintf(stderr, format, args);
  266. if (errnum)
  267. fprintf(stderr, ": %s", strerror(errnum));
  268. fprintf(stderr, "\n");
  269. va_end(args);
  270. exit(status);
  271. }
  272. unsigned long long mtime_since(const struct timeval *s, const struct timeval *e)
  273. {
  274. long long sec, usec;
  275. sec = e->tv_sec - s->tv_sec;
  276. usec = (e->tv_usec - s->tv_usec);
  277. if (sec > 0 && usec < 0) {
  278. sec--;
  279. usec += 1000000;
  280. }
  281. sec *= 1000;
  282. usec /= 1000;
  283. return sec + usec;
  284. }
  285. unsigned long long mtime_since_now(struct timeval *tv)
  286. {
  287. struct timeval end;
  288. gettimeofday(&end, NULL);
  289. return mtime_since(tv, &end);
  290. }
  291. unsigned long long utime_since(const struct timeval *s, const struct timeval *e)
  292. {
  293. long long sec, usec;
  294. sec = e->tv_sec - s->tv_sec;
  295. usec = (e->tv_usec - s->tv_usec);
  296. if (sec > 0 && usec < 0) {
  297. sec--;
  298. usec += 1000000;
  299. }
  300. sec *= 1000000;
  301. return sec + usec;
  302. }
  303. unsigned long long utime_since_now(struct timeval *tv)
  304. {
  305. struct timeval end;
  306. gettimeofday(&end, NULL);
  307. return utime_since(tv, &end);
  308. }