|
| 1 | +/* -*- Mode: C; tab-width: 8; c-basic-offset: 2; indent-tabs-mode: nil; -*- */ |
| 2 | + |
| 3 | +#include "util.h" |
| 4 | + |
| 5 | +#define SIZE (4 * 4096) |
| 6 | +#define BLOCKING_ROUNDS 8 |
| 7 | + |
| 8 | +static char out[SIZE]; |
| 9 | + |
| 10 | +struct helper_args { |
| 11 | + pid_t main_tid; |
| 12 | + int read_fd; |
| 13 | + int write_fd; |
| 14 | + int round; |
| 15 | +}; |
| 16 | + |
| 17 | +static char pattern_byte(size_t i) { return (char)(i * 7 + 3); } |
| 18 | + |
| 19 | +/* Checks that p holds the bytes from offset to offset + len of the pattern |
| 20 | + that buf is filled with. */ |
| 21 | +static void check_pattern(const char* p, size_t offset, size_t len) { |
| 22 | + size_t i; |
| 23 | + for (i = 0; i < len; ++i) { |
| 24 | + test_assert(p[i] == pattern_byte(offset + i)); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +static void read_all(int fd, char* buf, size_t size) { |
| 29 | + size_t done = 0; |
| 30 | + while (done < size) { |
| 31 | + ssize_t n = read(fd, buf + done, size - done); |
| 32 | + test_assert(n > 0); |
| 33 | + done += n; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +static int in_vmsplice(pid_t tid, int fd) { |
| 38 | + char path[PATH_MAX]; |
| 39 | + char buf[1024]; |
| 40 | + long nr; |
| 41 | + unsigned long arg1; |
| 42 | + int proc_fd; |
| 43 | + ssize_t len; |
| 44 | + |
| 45 | + sprintf(path, "/proc/self/task/%d/syscall", tid); |
| 46 | + proc_fd = open(path, O_RDONLY); |
| 47 | + test_assert(proc_fd >= 0); |
| 48 | + len = read(proc_fd, buf, sizeof(buf) - 1); |
| 49 | + test_assert(len > 0); |
| 50 | + buf[len] = 0; |
| 51 | + test_assert(0 == close(proc_fd)); |
| 52 | + return sscanf(buf, "%ld 0x%lx", &nr, &arg1) == 2 && nr == SYS_vmsplice && |
| 53 | + arg1 == (unsigned long)fd; |
| 54 | +} |
| 55 | + |
| 56 | +static void wait_for_vmsplice(const struct helper_args* args, int fd) { |
| 57 | + test_assert(args->main_tid > 0); |
| 58 | + while (!in_vmsplice(args->main_tid, fd)) { |
| 59 | + sched_yield(); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/* Writes to the empty pipe the main thread is reading with vmsplice(), then |
| 64 | + waits until the data shows up in the main thread's buffer. */ |
| 65 | +static void* do_write(void* p) { |
| 66 | + const struct helper_args* args = (const struct helper_args*)p; |
| 67 | + char data[100]; |
| 68 | + |
| 69 | + memset(data, 'a' + args->round, sizeof(data)); |
| 70 | + wait_for_vmsplice(args, args->read_fd); |
| 71 | + test_assert(sizeof(data) == write(args->write_fd, data, sizeof(data))); |
| 72 | + while (__atomic_load_n(&out[sizeof(data) - 1], __ATOMIC_SEQ_CST) != |
| 73 | + data[0]) { |
| 74 | + } |
| 75 | + return NULL; |
| 76 | +} |
| 77 | + |
| 78 | +/* Drains the full pipe the main thread is writing with vmsplice(). */ |
| 79 | +static void* do_read(void* p) { |
| 80 | + const struct helper_args* args = (const struct helper_args*)p; |
| 81 | + int pipe_size = fcntl(args->read_fd, F_GETPIPE_SZ); |
| 82 | + char* data; |
| 83 | + |
| 84 | + test_assert(pipe_size > 0); |
| 85 | + data = (char*)malloc(pipe_size + 4096); |
| 86 | + test_assert(data != NULL); |
| 87 | + wait_for_vmsplice(args, args->write_fd); |
| 88 | + read_all(args->read_fd, data, pipe_size + 4096); |
| 89 | + check_pattern(data + pipe_size, 0, 4096); |
| 90 | + free(data); |
| 91 | + return NULL; |
| 92 | +} |
| 93 | + |
| 94 | +int main(void) { |
| 95 | + int fds[2]; |
| 96 | + int closed_fds[2]; |
| 97 | + int rw_fd; |
| 98 | + int null_fd; |
| 99 | + char* buf; |
| 100 | + char* filler; |
| 101 | + char path[PATH_MAX]; |
| 102 | + struct iovec iov[3]; |
| 103 | + struct helper_args args; |
| 104 | + pthread_t thread; |
| 105 | + ssize_t n; |
| 106 | + size_t i; |
| 107 | + int pipe_size; |
| 108 | + int round; |
| 109 | + |
| 110 | + buf = (char*)mmap(NULL, SIZE, PROT_READ | PROT_WRITE, |
| 111 | + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 112 | + test_assert(buf != MAP_FAILED); |
| 113 | + for (i = 0; i < SIZE; ++i) { |
| 114 | + buf[i] = pattern_byte(i); |
| 115 | + } |
| 116 | + test_assert(0 == pipe(fds)); |
| 117 | + |
| 118 | + /* User memory into the pipe (fd is the write end). vmsplice() must leave |
| 119 | + buf unchanged. */ |
| 120 | + iov[0].iov_base = buf; |
| 121 | + iov[0].iov_len = SIZE / 2; |
| 122 | + iov[1].iov_base = buf + SIZE / 2; |
| 123 | + iov[1].iov_len = SIZE / 2; |
| 124 | + n = vmsplice(fds[1], iov, 2, 0); |
| 125 | + test_assert(n == SIZE); |
| 126 | + check_pattern(buf, 0, SIZE); |
| 127 | + read_all(fds[0], out, SIZE); |
| 128 | + check_pattern(out, 0, SIZE); |
| 129 | + |
| 130 | + /* The same with SPLICE_F_GIFT (whole pages). */ |
| 131 | + iov[0].iov_base = buf; |
| 132 | + iov[0].iov_len = SIZE; |
| 133 | + n = vmsplice(fds[1], iov, 1, SPLICE_F_GIFT); |
| 134 | + test_assert(n == SIZE); |
| 135 | + check_pattern(buf, 0, SIZE); |
| 136 | + memset(out, 0, SIZE); |
| 137 | + read_all(fds[0], out, SIZE); |
| 138 | + check_pattern(out, 0, SIZE); |
| 139 | + |
| 140 | + /* A pipe opened for reading and writing: vmsplice() reads user memory |
| 141 | + into the pipe. */ |
| 142 | + sprintf(path, "/proc/self/fd/%d", fds[0]); |
| 143 | + rw_fd = open(path, O_RDWR); |
| 144 | + test_assert(rw_fd >= 0); |
| 145 | + iov[0].iov_base = buf; |
| 146 | + iov[0].iov_len = 4096; |
| 147 | + n = vmsplice(rw_fd, iov, 1, 0); |
| 148 | + test_assert(n == 4096); |
| 149 | + check_pattern(buf, 0, SIZE); |
| 150 | + memset(out, 0, SIZE); |
| 151 | + read_all(fds[0], out, 4096); |
| 152 | + check_pattern(out, 0, 4096); |
| 153 | + test_assert(0 == close(rw_fd)); |
| 154 | + |
| 155 | + /* The pipe into user memory (fd is the read end), scattered over three |
| 156 | + segments; the last is only partly filled. */ |
| 157 | + test_assert(1000 == write(fds[1], buf, 1000)); |
| 158 | + memset(out, 0, SIZE); |
| 159 | + iov[0].iov_base = out; |
| 160 | + iov[0].iov_len = 300; |
| 161 | + iov[1].iov_base = out + 4096; |
| 162 | + iov[1].iov_len = 500; |
| 163 | + iov[2].iov_base = out + 3 * 4096; |
| 164 | + iov[2].iov_len = 4096; |
| 165 | + n = vmsplice(fds[0], iov, 3, 0); |
| 166 | + test_assert(n == 1000); |
| 167 | + check_pattern(out, 0, 300); |
| 168 | + check_pattern(out + 4096, 300, 500); |
| 169 | + check_pattern(out + 3 * 4096, 800, 200); |
| 170 | + for (i = 0; i < 300; ++i) { |
| 171 | + test_assert(out[3 * 4096 + 200 + i] == 0); |
| 172 | + } |
| 173 | + atomic_printf("checksum %d %d %d\n", out[0] + out[299], out[4096 + 499], |
| 174 | + out[3 * 4096 + 199]); |
| 175 | + |
| 176 | + /* vmsplice() from the empty pipe blocks until another thread writes. */ |
| 177 | + args.main_tid = sys_gettid(); |
| 178 | + args.read_fd = fds[0]; |
| 179 | + args.write_fd = fds[1]; |
| 180 | + for (round = 0; round < BLOCKING_ROUNDS; ++round) { |
| 181 | + memset(out, 0, SIZE); |
| 182 | + args.round = round; |
| 183 | + test_assert(0 == pthread_create(&thread, NULL, do_write, &args)); |
| 184 | + iov[0].iov_base = out; |
| 185 | + iov[0].iov_len = SIZE; |
| 186 | + n = vmsplice(fds[0], iov, 1, 0); |
| 187 | + test_assert(n == 100); |
| 188 | + test_assert(0 == pthread_join(thread, NULL)); |
| 189 | + for (i = 0; i < 100; ++i) { |
| 190 | + test_assert(out[i] == 'a' + round); |
| 191 | + } |
| 192 | + test_assert(out[100] == 0); |
| 193 | + } |
| 194 | + |
| 195 | + /* vmsplice() into the full pipe blocks until another thread reads. */ |
| 196 | + pipe_size = fcntl(fds[1], F_GETPIPE_SZ); |
| 197 | + test_assert(pipe_size > 0); |
| 198 | + filler = (char*)malloc(pipe_size); |
| 199 | + test_assert(filler != NULL); |
| 200 | + memset(filler, 'f', pipe_size); |
| 201 | + test_assert(pipe_size == write(fds[1], filler, pipe_size)); |
| 202 | + free(filler); |
| 203 | + test_assert(0 == pthread_create(&thread, NULL, do_read, &args)); |
| 204 | + iov[0].iov_base = buf; |
| 205 | + iov[0].iov_len = 4096; |
| 206 | + n = vmsplice(fds[1], iov, 1, SPLICE_F_GIFT); |
| 207 | + test_assert(n == 4096); |
| 208 | + test_assert(0 == pthread_join(thread, NULL)); |
| 209 | + check_pattern(buf, 0, SIZE); |
| 210 | + |
| 211 | + /* Without writers, vmsplice() from the empty pipe returns 0. */ |
| 212 | + test_assert(0 == pipe(closed_fds)); |
| 213 | + test_assert(0 == close(closed_fds[1])); |
| 214 | + iov[0].iov_base = out; |
| 215 | + iov[0].iov_len = SIZE; |
| 216 | + test_assert(0 == vmsplice(closed_fds[0], iov, 1, 0)); |
| 217 | + test_assert(0 == close(closed_fds[0])); |
| 218 | + |
| 219 | + /* The kernel takes nr_segs as an unsigned int. */ |
| 220 | + test_assert(10 == write(fds[1], buf, 10)); |
| 221 | + memset(out, 0, SIZE); |
| 222 | + n = syscall(SYS_vmsplice, fds[0], iov, |
| 223 | + (unsigned long)(((uint64_t)1 << 32) | 1), 0); |
| 224 | + test_assert(n == 10); |
| 225 | + check_pattern(out, 0, 10); |
| 226 | + |
| 227 | + /* Errors. */ |
| 228 | + test_assert(-1 == vmsplice(fds[0], iov, 1, SPLICE_F_NONBLOCK) && |
| 229 | + errno == EAGAIN); |
| 230 | + test_assert(-1 == vmsplice(-1, iov, 1, 0) && errno == EBADF); |
| 231 | + null_fd = open("/dev/null", O_RDONLY); |
| 232 | + test_assert(null_fd >= 0); |
| 233 | + test_assert(-1 == vmsplice(null_fd, iov, 1, 0) && errno == EBADF); |
| 234 | + test_assert(0 == close(null_fd)); |
| 235 | + test_assert(-1 == vmsplice(fds[0], iov, 1, 0x10000) && errno == EINVAL); |
| 236 | + test_assert(-1 == vmsplice(fds[0], iov, IOV_MAX + 1, 0) && errno == EINVAL); |
| 237 | + test_assert(0 == munmap(buf, SIZE)); |
| 238 | + test_assert(-1 == vmsplice(fds[0], (struct iovec*)buf, 1, 0) && |
| 239 | + errno == EFAULT); |
| 240 | + test_assert(0 == vmsplice(fds[0], iov, 0, 0)); |
| 241 | + test_assert(0 == vmsplice(fds[1], iov, 0, 0)); |
| 242 | + |
| 243 | + atomic_puts("EXIT-SUCCESS"); |
| 244 | + return 0; |
| 245 | +} |
0 commit comments