blob: 491822efade317298a882cb7c0434d202337c8e8 [file] [log] [blame]
Zhiqiang Liu97499812021-02-21 22:58:12 +08001/* SPDX-License-Identifier: MIT */
2/*
3 * Description: Helpers for tests.
4 */
5#include <stdlib.h>
6#include <assert.h>
Zhiqiang Liufa7ba192021-02-23 19:15:35 +08007#include <string.h>
8#include <stdio.h>
9#include <fcntl.h>
10#include <unistd.h>
Jens Axboe382bec72021-02-24 07:31:45 -070011#include <sys/types.h>
Zhiqiang Liu97499812021-02-21 22:58:12 +080012
13#include "helpers.h"
Zhiqiang Liub6e4a0a2021-02-23 20:25:54 +080014#include "liburing.h"
Zhiqiang Liu97499812021-02-21 22:58:12 +080015
16/*
17 * Helper for allocating memory in tests.
18 */
Jens Axboed84d9012021-02-24 07:12:47 -070019void *t_malloc(size_t size)
Zhiqiang Liu97499812021-02-21 22:58:12 +080020{
21 void *ret;
22 ret = malloc(size);
23 assert(ret);
24 return ret;
25}
Zhiqiang Liu93bec4c2021-02-23 11:42:07 +080026
Zhiqiang Liu93bec4c2021-02-23 11:42:07 +080027/*
28 * Helper for allocating size bytes aligned on a boundary.
29 */
Jens Axboed84d9012021-02-24 07:12:47 -070030void t_posix_memalign(void **memptr, size_t alignment, size_t size)
Zhiqiang Liu93bec4c2021-02-23 11:42:07 +080031{
32 int ret;
33 ret = posix_memalign(memptr, alignment, size);
34 assert(!ret);
35}
Zhiqiang Liub7b089a2021-02-23 14:43:57 +080036
37/*
38 * Helper for allocating space for an array of nmemb elements
39 * with size bytes for each element.
40 */
Jens Axboed84d9012021-02-24 07:12:47 -070041void *t_calloc(size_t nmemb, size_t size)
Zhiqiang Liub7b089a2021-02-23 14:43:57 +080042{
43 void *ret;
44 ret = calloc(nmemb, size);
45 assert(ret);
46 return ret;
47}
48
Zhiqiang Liufa7ba192021-02-23 19:15:35 +080049/*
50 * Helper for creating file and write @size byte buf with 0xaa value in the file.
51 */
Jens Axboe1c90b192022-03-30 11:33:23 -060052static void __t_create_file(const char *file, size_t size, char pattern)
Zhiqiang Liufa7ba192021-02-23 19:15:35 +080053{
54 ssize_t ret;
55 char *buf;
56 int fd;
57
Jens Axboed84d9012021-02-24 07:12:47 -070058 buf = t_malloc(size);
Jens Axboe1c90b192022-03-30 11:33:23 -060059 memset(buf, pattern, size);
Zhiqiang Liufa7ba192021-02-23 19:15:35 +080060
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}
Zhiqiang Liub7b089a2021-02-23 14:43:57 +080070
Jens Axboe1c90b192022-03-30 11:33:23 -060071void t_create_file(const char *file, size_t size)
72{
73 __t_create_file(file, size, 0xaa);
74}
75
76void t_create_file_pattern(const char *file, size_t size, char pattern)
77{
78 __t_create_file(file, size, pattern);
79}
80
Zhiqiang Liub6e4a0a2021-02-23 20:25:54 +080081/*
82 * Helper for creating @buf_num number of iovec
83 * with @buf_size bytes buffer of each iovec.
84 */
Jens Axboed84d9012021-02-24 07:12:47 -070085struct iovec *t_create_buffers(size_t buf_num, size_t buf_size)
Zhiqiang Liub6e4a0a2021-02-23 20:25:54 +080086{
87 struct iovec *vecs;
88 int i;
89
Jens Axboed84d9012021-02-24 07:12:47 -070090 vecs = t_malloc(buf_num * sizeof(struct iovec));
Zhiqiang Liub6e4a0a2021-02-23 20:25:54 +080091 for (i = 0; i < buf_num; i++) {
Jens Axboed84d9012021-02-24 07:12:47 -070092 t_posix_memalign(&vecs[i].iov_base, buf_size, buf_size);
Zhiqiang Liub6e4a0a2021-02-23 20:25:54 +080093 vecs[i].iov_len = buf_size;
94 }
95 return vecs;
96}
Jens Axboe382bec72021-02-24 07:31:45 -070097
98/*
99 * Helper for setting up an io_uring instance, skipping if the given user isn't
100 * allowed to.
101 */
102enum 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
119enum 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}
Pavel Begunkovb3dc8722021-08-25 18:53:36 +0100127
128enum 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}