Zhiqiang Liu | 9749981 | 2021-02-21 22:58:12 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: MIT */ |
| 2 | /* |
| 3 | * Description: Helpers for tests. |
| 4 | */ |
| 5 | #include <stdlib.h> |
| 6 | #include <assert.h> |
Zhiqiang Liu | fa7ba19 | 2021-02-23 19:15:35 +0800 | [diff] [blame] | 7 | #include <string.h> |
| 8 | #include <stdio.h> |
| 9 | #include <fcntl.h> |
| 10 | #include <unistd.h> |
Jens Axboe | 382bec7 | 2021-02-24 07:31:45 -0700 | [diff] [blame] | 11 | #include <sys/types.h> |
Zhiqiang Liu | 9749981 | 2021-02-21 22:58:12 +0800 | [diff] [blame] | 12 | |
| 13 | #include "helpers.h" |
Zhiqiang Liu | b6e4a0a | 2021-02-23 20:25:54 +0800 | [diff] [blame] | 14 | #include "liburing.h" |
Zhiqiang Liu | 9749981 | 2021-02-21 22:58:12 +0800 | [diff] [blame] | 15 | |
| 16 | /* |
| 17 | * Helper for allocating memory in tests. |
| 18 | */ |
Jens Axboe | d84d901 | 2021-02-24 07:12:47 -0700 | [diff] [blame] | 19 | void *t_malloc(size_t size) |
Zhiqiang Liu | 9749981 | 2021-02-21 22:58:12 +0800 | [diff] [blame] | 20 | { |
| 21 | void *ret; |
| 22 | ret = malloc(size); |
| 23 | assert(ret); |
| 24 | return ret; |
| 25 | } |
Zhiqiang Liu | 93bec4c | 2021-02-23 11:42:07 +0800 | [diff] [blame] | 26 | |
Zhiqiang Liu | 93bec4c | 2021-02-23 11:42:07 +0800 | [diff] [blame] | 27 | /* |
| 28 | * Helper for allocating size bytes aligned on a boundary. |
| 29 | */ |
Jens Axboe | d84d901 | 2021-02-24 07:12:47 -0700 | [diff] [blame] | 30 | void t_posix_memalign(void **memptr, size_t alignment, size_t size) |
Zhiqiang Liu | 93bec4c | 2021-02-23 11:42:07 +0800 | [diff] [blame] | 31 | { |
| 32 | int ret; |
| 33 | ret = posix_memalign(memptr, alignment, size); |
| 34 | assert(!ret); |
| 35 | } |
Zhiqiang Liu | b7b089a | 2021-02-23 14:43:57 +0800 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * Helper for allocating space for an array of nmemb elements |
| 39 | * with size bytes for each element. |
| 40 | */ |
Jens Axboe | d84d901 | 2021-02-24 07:12:47 -0700 | [diff] [blame] | 41 | void *t_calloc(size_t nmemb, size_t size) |
Zhiqiang Liu | b7b089a | 2021-02-23 14:43:57 +0800 | [diff] [blame] | 42 | { |
| 43 | void *ret; |
| 44 | ret = calloc(nmemb, size); |
| 45 | assert(ret); |
| 46 | return ret; |
| 47 | } |
| 48 | |
Zhiqiang Liu | fa7ba19 | 2021-02-23 19:15:35 +0800 | [diff] [blame] | 49 | /* |
| 50 | * Helper for creating file and write @size byte buf with 0xaa value in the file. |
| 51 | */ |
Jens Axboe | 1c90b19 | 2022-03-30 11:33:23 -0600 | [diff] [blame] | 52 | static void __t_create_file(const char *file, size_t size, char pattern) |
Zhiqiang Liu | fa7ba19 | 2021-02-23 19:15:35 +0800 | [diff] [blame] | 53 | { |
| 54 | ssize_t ret; |
| 55 | char *buf; |
| 56 | int fd; |
| 57 | |
Jens Axboe | d84d901 | 2021-02-24 07:12:47 -0700 | [diff] [blame] | 58 | buf = t_malloc(size); |
Jens Axboe | 1c90b19 | 2022-03-30 11:33:23 -0600 | [diff] [blame] | 59 | memset(buf, pattern, size); |
Zhiqiang Liu | fa7ba19 | 2021-02-23 19:15:35 +0800 | [diff] [blame] | 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 | } |
Zhiqiang Liu | b7b089a | 2021-02-23 14:43:57 +0800 | [diff] [blame] | 70 | |
Jens Axboe | 1c90b19 | 2022-03-30 11:33:23 -0600 | [diff] [blame] | 71 | void t_create_file(const char *file, size_t size) |
| 72 | { |
| 73 | __t_create_file(file, size, 0xaa); |
| 74 | } |
| 75 | |
| 76 | void t_create_file_pattern(const char *file, size_t size, char pattern) |
| 77 | { |
| 78 | __t_create_file(file, size, pattern); |
| 79 | } |
| 80 | |
Zhiqiang Liu | b6e4a0a | 2021-02-23 20:25:54 +0800 | [diff] [blame] | 81 | /* |
| 82 | * Helper for creating @buf_num number of iovec |
| 83 | * with @buf_size bytes buffer of each iovec. |
| 84 | */ |
Jens Axboe | d84d901 | 2021-02-24 07:12:47 -0700 | [diff] [blame] | 85 | struct iovec *t_create_buffers(size_t buf_num, size_t buf_size) |
Zhiqiang Liu | b6e4a0a | 2021-02-23 20:25:54 +0800 | [diff] [blame] | 86 | { |
| 87 | struct iovec *vecs; |
| 88 | int i; |
| 89 | |
Jens Axboe | d84d901 | 2021-02-24 07:12:47 -0700 | [diff] [blame] | 90 | vecs = t_malloc(buf_num * sizeof(struct iovec)); |
Zhiqiang Liu | b6e4a0a | 2021-02-23 20:25:54 +0800 | [diff] [blame] | 91 | for (i = 0; i < buf_num; i++) { |
Jens Axboe | d84d901 | 2021-02-24 07:12:47 -0700 | [diff] [blame] | 92 | t_posix_memalign(&vecs[i].iov_base, buf_size, buf_size); |
Zhiqiang Liu | b6e4a0a | 2021-02-23 20:25:54 +0800 | [diff] [blame] | 93 | vecs[i].iov_len = buf_size; |
| 94 | } |
| 95 | return vecs; |
| 96 | } |
Jens Axboe | 382bec7 | 2021-02-24 07:31:45 -0700 | [diff] [blame] | 97 | |
| 98 | /* |
| 99 | * Helper for setting up an io_uring instance, skipping if the given user isn't |
| 100 | * allowed to. |
| 101 | */ |
| 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 | |
| 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 | } |
Pavel Begunkov | b3dc872 | 2021-08-25 18:53:36 +0100 | [diff] [blame] | 127 | |
| 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 | } |