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