splice: update to new vmsplice-to-user interface
[fio.git] / engines / splice.c
CommitLineData
2866c82d 1/*
da751ca9
JA
2 * splice engine
3 *
4 * IO engine that transfers data by doing splices to/from pipes and
5 * the files.
2866c82d
JA
6 *
7 */
8#include <stdio.h>
9#include <stdlib.h>
10#include <unistd.h>
11#include <errno.h>
12#include <assert.h>
13#include <sys/poll.h>
81887d5d 14#include <sys/mman.h>
5f350952
JA
15
16#include "../fio.h"
2866c82d 17
34cfcdaf
JA
18#ifdef FIO_HAVE_SPLICE
19
2866c82d 20struct spliceio_data {
2866c82d 21 int pipe[2];
f24254e1 22 int vmsplice_to_user;
2866c82d
JA
23};
24
2866c82d 25/*
f24254e1
JA
26 * vmsplice didn't use to support splicing to user space, this is the old
27 * variant of getting that job done. Doesn't make a lot of sense, but it
28 * uses splices to move data from the source into a pipe.
2866c82d 29 */
f24254e1 30static int fio_splice_read_old(struct thread_data *td, struct io_u *io_u)
2866c82d
JA
31{
32 struct spliceio_data *sd = td->io_ops->data;
53cdc686 33 struct fio_file *f = io_u->file;
2866c82d
JA
34 int ret, ret2, buflen;
35 off_t offset;
36 void *p;
37
38 offset = io_u->offset;
cec6b55d
JA
39 buflen = io_u->xfer_buflen;
40 p = io_u->xfer_buf;
2866c82d
JA
41 while (buflen) {
42 int this_len = buflen;
43
44 if (this_len > SPLICE_DEF_SIZE)
45 this_len = SPLICE_DEF_SIZE;
46
53cdc686 47 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE);
2866c82d
JA
48 if (ret < 0) {
49 if (errno == ENODATA || errno == EAGAIN)
50 continue;
51
b4ba5f30 52 return -errno;
2866c82d
JA
53 }
54
55 buflen -= ret;
56
57 while (ret) {
58 ret2 = read(sd->pipe[0], p, ret);
59 if (ret2 < 0)
b4ba5f30 60 return -errno;
2866c82d
JA
61
62 ret -= ret2;
63 p += ret2;
64 }
65 }
66
cec6b55d 67 return io_u->xfer_buflen;
2866c82d
JA
68}
69
f24254e1
JA
70/*
71 * We can now vmsplice into userspace, so do the transfer by splicing into
72 * a pipe and vmsplicing that into userspace.
73 */
74static int fio_splice_read(struct thread_data *td, struct io_u *io_u)
75{
76 struct spliceio_data *sd = td->io_ops->data;
77 struct fio_file *f = io_u->file;
78 struct iovec iov;
81887d5d 79 int ret = 0 , buflen, mmap_len;
f24254e1 80 off_t offset;
81887d5d 81 void *p, *map;
f24254e1
JA
82
83 offset = io_u->offset;
81887d5d
JA
84 mmap_len = buflen = io_u->xfer_buflen;
85
86 map = mmap(io_u->xfer_buf, buflen, PROT_READ, MAP_PRIVATE|OS_MAP_ANON, 0, 0);
87 if (map == MAP_FAILED) {
88 td_verror(td, errno, "mmap io_u");
89 return -1;
90 }
91
92 p = map;
f24254e1
JA
93 while (buflen) {
94 int this_len = buflen;
81887d5d 95 int flags = 0;
f24254e1 96
81887d5d 97 if (this_len > SPLICE_DEF_SIZE) {
f24254e1 98 this_len = SPLICE_DEF_SIZE;
81887d5d
JA
99 flags = SPLICE_F_MORE;
100 }
f24254e1 101
81887d5d 102 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len,flags);
f24254e1
JA
103 if (ret < 0) {
104 if (errno == ENODATA || errno == EAGAIN)
105 continue;
106
81887d5d
JA
107 td_verror(td, errno, "splice-from-fd");
108 break;
f24254e1
JA
109 }
110
111 buflen -= ret;
112 iov.iov_base = p;
113 iov.iov_len = ret;
114 p += ret;
115
116 while (iov.iov_len) {
117 ret = vmsplice(sd->pipe[0], &iov, 1, SPLICE_F_MOVE);
81887d5d
JA
118 if (ret < 0) {
119 td_verror(td, errno, "vmsplice");
120 break;
121 } else if (!ret) {
122 td_verror(td, ENODATA, "vmsplice");
123 ret = -1;
124 break;
125 }
f24254e1
JA
126
127 iov.iov_len -= ret;
128 iov.iov_base += ret;
129 }
81887d5d
JA
130 if (ret < 0)
131 break;
132 }
133
134 if (munmap(map, mmap_len) < 0) {
135 td_verror(td, errno, "munnap io_u");
136 return -1;
f24254e1 137 }
81887d5d
JA
138 if (ret < 0)
139 return ret;
f24254e1
JA
140
141 return io_u->xfer_buflen;
142}
143
2866c82d
JA
144/*
145 * For splice writing, we can vmsplice our data buffer directly into a
146 * pipe and then splice that to a file.
147 */
148static int fio_splice_write(struct thread_data *td, struct io_u *io_u)
149{
150 struct spliceio_data *sd = td->io_ops->data;
f24254e1
JA
151 struct iovec iov = {
152 .iov_base = io_u->xfer_buf,
153 .iov_len = io_u->xfer_buflen,
2866c82d
JA
154 };
155 struct pollfd pfd = { .fd = sd->pipe[1], .events = POLLOUT, };
53cdc686 156 struct fio_file *f = io_u->file;
2866c82d
JA
157 off_t off = io_u->offset;
158 int ret, ret2;
159
f24254e1 160 while (iov.iov_len) {
2866c82d
JA
161 if (poll(&pfd, 1, -1) < 0)
162 return errno;
163
f24254e1 164 ret = vmsplice(sd->pipe[1], &iov, 1, SPLICE_F_NONBLOCK);
2866c82d 165 if (ret < 0)
b4ba5f30 166 return -errno;
2866c82d 167
f24254e1
JA
168 iov.iov_len -= ret;
169 iov.iov_base += ret;
2866c82d
JA
170
171 while (ret) {
53cdc686 172 ret2 = splice(sd->pipe[0], NULL, f->fd, &off, ret, 0);
2866c82d 173 if (ret2 < 0)
b4ba5f30 174 return -errno;
2866c82d
JA
175
176 ret -= ret2;
177 }
178 }
179
cec6b55d 180 return io_u->xfer_buflen;
2866c82d
JA
181}
182
183static int fio_spliceio_queue(struct thread_data *td, struct io_u *io_u)
184{
f24254e1 185 struct spliceio_data *sd = td->io_ops->data;
cec6b55d 186 int ret;
2866c82d 187
f24254e1 188 if (io_u->ddir == DDIR_READ) {
43688575 189 if (sd->vmsplice_to_user) {
f24254e1 190 ret = fio_splice_read(td, io_u);
43688575
JA
191 /*
192 * This kernel doesn't support vmsplice to user
193 * space. Reset the vmsplice_to_user flag, so that
194 * we retry below and don't hit this path again.
195 */
196 if (ret == -EBADF)
197 sd->vmsplice_to_user = 0;
198 }
199 if (!sd->vmsplice_to_user)
f24254e1
JA
200 ret = fio_splice_read_old(td, io_u);
201 } else if (io_u->ddir == DDIR_WRITE)
2866c82d 202 ret = fio_splice_write(td, io_u);
87dc1ab1
JA
203 else
204 ret = fsync(io_u->file->fd);
2866c82d 205
cec6b55d 206 if (ret != (int) io_u->xfer_buflen) {
22819ec2 207 if (ret >= 0) {
cec6b55d
JA
208 io_u->resid = io_u->xfer_buflen - ret;
209 io_u->error = 0;
36167d82 210 return FIO_Q_COMPLETED;
2866c82d
JA
211 } else
212 io_u->error = errno;
213 }
214
36167d82 215 if (io_u->error)
e1161c32 216 td_verror(td, io_u->error, "xfer");
2866c82d 217
36167d82 218 return FIO_Q_COMPLETED;
2866c82d
JA
219}
220
221static void fio_spliceio_cleanup(struct thread_data *td)
222{
223 struct spliceio_data *sd = td->io_ops->data;
224
225 if (sd) {
226 close(sd->pipe[0]);
227 close(sd->pipe[1]);
228 free(sd);
229 td->io_ops->data = NULL;
230 }
231}
232
233static int fio_spliceio_init(struct thread_data *td)
234{
235 struct spliceio_data *sd = malloc(sizeof(*sd));
236
2866c82d 237 if (pipe(sd->pipe) < 0) {
e1161c32 238 td_verror(td, errno, "pipe");
2866c82d
JA
239 free(sd);
240 return 1;
241 }
242
f24254e1 243 /*
43688575 244 * Assume this work, we'll reset this if it doesn't
f24254e1 245 */
43688575 246 sd->vmsplice_to_user = 1;
f24254e1 247
81887d5d
JA
248 /*
249 * And if vmsplice_to_user works, we definitely need aligned
250 * buffers. Just set ->odirect to force that.
251 */
252 if (td_read(td))
253 td->o.odirect = 1;
254
2866c82d
JA
255 td->io_ops->data = sd;
256 return 0;
257}
258
5f350952 259static struct ioengine_ops ioengine = {
2866c82d
JA
260 .name = "splice",
261 .version = FIO_IOOPS_VERSION,
262 .init = fio_spliceio_init,
263 .queue = fio_spliceio_queue,
2866c82d 264 .cleanup = fio_spliceio_cleanup,
b5af8293
JA
265 .open_file = generic_open_file,
266 .close_file = generic_close_file,
2866c82d
JA
267 .flags = FIO_SYNCIO,
268};
34cfcdaf
JA
269
270#else /* FIO_HAVE_SPLICE */
271
272/*
273 * When we have a proper configure system in place, we simply wont build
274 * and install this io engine. For now install a crippled version that
275 * just complains and fails to load.
276 */
277static int fio_spliceio_init(struct thread_data fio_unused *td)
278{
279 fprintf(stderr, "fio: splice not available\n");
280 return 1;
281}
282
5f350952 283static struct ioengine_ops ioengine = {
34cfcdaf
JA
284 .name = "splice",
285 .version = FIO_IOOPS_VERSION,
286 .init = fio_spliceio_init,
287};
288
289#endif
5f350952
JA
290
291static void fio_init fio_spliceio_register(void)
292{
293 register_ioengine(&ioengine);
294}
295
296static void fio_exit fio_spliceio_unregister(void)
297{
298 unregister_ioengine(&ioengine);
299}