1 /* SPDX-License-Identifier: MIT */
2 /*
3 * Description: Helpers for tests.
4 */
5 #include <stdlib.h>
6 #include <assert.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <sys/types.h>
12
13 #include "helpers.h"
14 #include "liburing.h"
15
16 /*
17 * Helper for allocating memory in tests.
18 */
t_malloc(size_t size)19 void *t_malloc(size_t size)
20 {
21 void *ret;
22 ret = malloc(size);
23 assert(ret);
24 return ret;
25 }
26
27 /*
28 * Helper for allocating size bytes aligned on a boundary.
29 */
t_posix_memalign(void ** memptr,size_t alignment,size_t size)30 void t_posix_memalign(void **memptr, size_t alignment, size_t size)
31 {
32 int ret;
33 ret = posix_memalign(memptr, alignment, size);
34 assert(!ret);
35 }
36
37 /*
38 * Helper for allocating space for an array of nmemb elements
39 * with size bytes for each element.
40 */
t_calloc(size_t nmemb,size_t size)41 void *t_calloc(size_t nmemb, size_t size)
42 {
43 void *ret;
44 ret = calloc(nmemb, size);
45 assert(ret);
46 return ret;
47 }
48
49 /*
50 * Helper for creating file and write @size byte buf with 0xaa value in the file.
51 */
__t_create_file(const char * file,size_t size,char pattern)52 static void __t_create_file(const char *file, size_t size, char pattern)
53 {
54 ssize_t ret;
55 char *buf;
56 int fd;
57
58 buf = t_malloc(size);
59 memset(buf, pattern, size);
60
61 fd = open(file, O_WRONLY | O_CREAT, 0644);
62 assert(fd >= 0);
63
64 ret = write(fd, buf, size);
65 fsync(fd);
66 close(fd);
67 free(buf);
68 assert(ret == size);
69 }
70
t_create_file(const char * file,size_t size)71 void t_create_file(const char *file, size_t size)
72 {
73 __t_create_file(file, size, 0xaa);
74 }
75
t_create_file_pattern(const char * file,size_t size,char pattern)76 void t_create_file_pattern(const char *file, size_t size, char pattern)
77 {
78 __t_create_file(file, size, pattern);
79 }
80
81 /*
82 * Helper for creating @buf_num number of iovec
83 * with @buf_size bytes buffer of each iovec.
84 */
t_create_buffers(size_t buf_num,size_t buf_size)85 struct iovec *t_create_buffers(size_t buf_num, size_t buf_size)
86 {
87 struct iovec *vecs;
88 int i;
89
90 vecs = t_malloc(buf_num * sizeof(struct iovec));
91 for (i = 0; i < buf_num; i++) {
92 t_posix_memalign(&vecs[i].iov_base, buf_size, buf_size);
93 vecs[i].iov_len = buf_size;
94 }
95 return vecs;
96 }
97
98 /*
99 * Helper for setting up an io_uring instance, skipping if the given user isn't
100 * allowed to.
101 */
t_create_ring_params(int depth,struct io_uring * ring,struct io_uring_params * p)102 enum t_setup_ret t_create_ring_params(int depth, struct io_uring *ring,
103 struct io_uring_params *p)
104 {
105 int ret;
106
107 ret = io_uring_queue_init_params(depth, ring, p);
108 if (!ret)
109 return T_SETUP_OK;
110 if ((p->flags & IORING_SETUP_SQPOLL) && ret == -EPERM && geteuid()) {
111 fprintf(stdout, "SQPOLL skipped for regular user\n");
112 return T_SETUP_SKIP;
113 }
114
115 fprintf(stderr, "queue_init: %s\n", strerror(-ret));
116 return ret;
117 }
118
t_create_ring(int depth,struct io_uring * ring,unsigned int flags)119 enum t_setup_ret t_create_ring(int depth, struct io_uring *ring,
120 unsigned int flags)
121 {
122 struct io_uring_params p = { };
123
124 p.flags = flags;
125 return t_create_ring_params(depth, ring, &p);
126 }
127
t_register_buffers(struct io_uring * ring,const struct iovec * iovecs,unsigned nr_iovecs)128 enum t_setup_ret t_register_buffers(struct io_uring *ring,
129 const struct iovec *iovecs,
130 unsigned nr_iovecs)
131 {
132 int ret;
133
134 ret = io_uring_register_buffers(ring, iovecs, nr_iovecs);
135 if (!ret)
136 return T_SETUP_OK;
137
138 if ((ret == -EPERM || ret == -ENOMEM) && geteuid()) {
139 fprintf(stdout, "too large non-root buffer registration, skip\n");
140 return T_SETUP_SKIP;
141 }
142
143 fprintf(stderr, "buffer register failed: %s\n", strerror(-ret));
144 return ret;
145 }
146