1 /* SPDX-License-Identifier: MIT */
2 /*
3 * Description: Helpers for tests.
4 */
5 #ifndef LIBURING_HELPERS_H
6 #define LIBURING_HELPERS_H
7
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11
12 #include "liburing.h"
13 #include "../src/setup.h"
14 #include <arpa/inet.h>
15 #include <sys/time.h>
16
17 enum t_setup_ret {
18 T_SETUP_OK = 0,
19 T_SETUP_SKIP,
20 };
21
22 enum t_test_result {
23 T_EXIT_PASS = 0,
24 T_EXIT_FAIL = 1,
25 T_EXIT_SKIP = 77,
26 };
27
28 /*
29 * Helper for binding socket to an ephemeral port.
30 * The port number to be bound is returned in @addr->sin_port.
31 */
32 int t_bind_ephemeral_port(int fd, struct sockaddr_in *addr);
33
34
35 /*
36 * Helper for allocating memory in tests.
37 */
38 void *t_malloc(size_t size);
39
40
41 /*
42 * Helper for allocating size bytes aligned on a boundary.
43 */
44 void t_posix_memalign(void **memptr, size_t alignment, size_t size);
45
46
47 /*
48 * Helper for allocating space for an array of nmemb elements
49 * with size bytes for each element.
50 */
51 void *t_calloc(size_t nmemb, size_t size);
52
53
54 /*
55 * Helper for creating file and write @size byte buf with 0xaa value in the file.
56 */
57 void t_create_file(const char *file, size_t size);
58
59 /*
60 * Helper for creating file and write @size byte buf with @pattern value in
61 * the file.
62 */
63 void t_create_file_pattern(const char *file, size_t size, char pattern);
64
65 /*
66 * Helper for creating @buf_num number of iovec
67 * with @buf_size bytes buffer of each iovec.
68 */
69 struct iovec *t_create_buffers(size_t buf_num, size_t buf_size);
70
71 /*
72 * Helper for creating connected socket pairs
73 */
74 int t_create_socket_pair(int fd[2], bool stream);
75
76 /*
77 * Helper for setting up a ring and checking for user privs
78 */
79 enum t_setup_ret t_create_ring_params(int depth, struct io_uring *ring,
80 struct io_uring_params *p);
81 enum t_setup_ret t_create_ring(int depth, struct io_uring *ring,
82 unsigned int flags);
83
84 enum t_setup_ret t_register_buffers(struct io_uring *ring,
85 const struct iovec *iovecs,
86 unsigned nr_iovecs);
87
88 bool t_probe_defer_taskrun(void);
89
90 unsigned __io_uring_flush_sq(struct io_uring *ring);
91
t_io_uring_init_sqarray(unsigned entries,struct io_uring * ring,struct io_uring_params * p)92 static inline int t_io_uring_init_sqarray(unsigned entries, struct io_uring *ring,
93 struct io_uring_params *p)
94 {
95 int ret;
96
97 ret = __io_uring_queue_init_params(entries, ring, p, NULL, 0);
98 return ret >= 0 ? 0 : ret;
99 }
100
101 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
102
103 void t_error(int status, int errnum, const char *format, ...);
104
105 unsigned long long mtime_since(const struct timeval *s, const struct timeval *e);
106 unsigned long long mtime_since_now(struct timeval *tv);
107 unsigned long long utime_since(const struct timeval *s, const struct timeval *e);
108 unsigned long long utime_since_now(struct timeval *tv);
109
110 #ifdef __cplusplus
111 }
112 #endif
113
114 #endif
115