Commit | Line | Data |
---|---|---|
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 | 20 | struct 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 | 30 | static 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 | */ | |
74 | static 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 | */ | |
148 | static 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 | ||
183 | static 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 | |
7101d9c2 JA |
188 | fio_ro_check(td, io_u); |
189 | ||
f24254e1 | 190 | if (io_u->ddir == DDIR_READ) { |
43688575 | 191 | if (sd->vmsplice_to_user) { |
f24254e1 | 192 | ret = fio_splice_read(td, io_u); |
43688575 JA |
193 | /* |
194 | * This kernel doesn't support vmsplice to user | |
195 | * space. Reset the vmsplice_to_user flag, so that | |
196 | * we retry below and don't hit this path again. | |
197 | */ | |
198 | if (ret == -EBADF) | |
199 | sd->vmsplice_to_user = 0; | |
200 | } | |
201 | if (!sd->vmsplice_to_user) | |
f24254e1 JA |
202 | ret = fio_splice_read_old(td, io_u); |
203 | } else if (io_u->ddir == DDIR_WRITE) | |
2866c82d | 204 | ret = fio_splice_write(td, io_u); |
87dc1ab1 JA |
205 | else |
206 | ret = fsync(io_u->file->fd); | |
2866c82d | 207 | |
cec6b55d | 208 | if (ret != (int) io_u->xfer_buflen) { |
22819ec2 | 209 | if (ret >= 0) { |
cec6b55d JA |
210 | io_u->resid = io_u->xfer_buflen - ret; |
211 | io_u->error = 0; | |
36167d82 | 212 | return FIO_Q_COMPLETED; |
2866c82d JA |
213 | } else |
214 | io_u->error = errno; | |
215 | } | |
216 | ||
36167d82 | 217 | if (io_u->error) |
e1161c32 | 218 | td_verror(td, io_u->error, "xfer"); |
2866c82d | 219 | |
36167d82 | 220 | return FIO_Q_COMPLETED; |
2866c82d JA |
221 | } |
222 | ||
223 | static void fio_spliceio_cleanup(struct thread_data *td) | |
224 | { | |
225 | struct spliceio_data *sd = td->io_ops->data; | |
226 | ||
227 | if (sd) { | |
228 | close(sd->pipe[0]); | |
229 | close(sd->pipe[1]); | |
230 | free(sd); | |
231 | td->io_ops->data = NULL; | |
232 | } | |
233 | } | |
234 | ||
235 | static int fio_spliceio_init(struct thread_data *td) | |
236 | { | |
237 | struct spliceio_data *sd = malloc(sizeof(*sd)); | |
238 | ||
2866c82d | 239 | if (pipe(sd->pipe) < 0) { |
e1161c32 | 240 | td_verror(td, errno, "pipe"); |
2866c82d JA |
241 | free(sd); |
242 | return 1; | |
243 | } | |
244 | ||
f24254e1 | 245 | /* |
43688575 | 246 | * Assume this work, we'll reset this if it doesn't |
f24254e1 | 247 | */ |
43688575 | 248 | sd->vmsplice_to_user = 1; |
f24254e1 | 249 | |
81887d5d JA |
250 | /* |
251 | * And if vmsplice_to_user works, we definitely need aligned | |
252 | * buffers. Just set ->odirect to force that. | |
253 | */ | |
254 | if (td_read(td)) | |
255 | td->o.odirect = 1; | |
256 | ||
2866c82d JA |
257 | td->io_ops->data = sd; |
258 | return 0; | |
259 | } | |
260 | ||
5f350952 | 261 | static struct ioengine_ops ioengine = { |
2866c82d JA |
262 | .name = "splice", |
263 | .version = FIO_IOOPS_VERSION, | |
264 | .init = fio_spliceio_init, | |
265 | .queue = fio_spliceio_queue, | |
2866c82d | 266 | .cleanup = fio_spliceio_cleanup, |
b5af8293 JA |
267 | .open_file = generic_open_file, |
268 | .close_file = generic_close_file, | |
2866c82d JA |
269 | .flags = FIO_SYNCIO, |
270 | }; | |
34cfcdaf JA |
271 | |
272 | #else /* FIO_HAVE_SPLICE */ | |
273 | ||
274 | /* | |
275 | * When we have a proper configure system in place, we simply wont build | |
276 | * and install this io engine. For now install a crippled version that | |
277 | * just complains and fails to load. | |
278 | */ | |
279 | static int fio_spliceio_init(struct thread_data fio_unused *td) | |
280 | { | |
281 | fprintf(stderr, "fio: splice not available\n"); | |
282 | return 1; | |
283 | } | |
284 | ||
5f350952 | 285 | static struct ioengine_ops ioengine = { |
34cfcdaf JA |
286 | .name = "splice", |
287 | .version = FIO_IOOPS_VERSION, | |
288 | .init = fio_spliceio_init, | |
289 | }; | |
290 | ||
291 | #endif | |
5f350952 JA |
292 | |
293 | static void fio_init fio_spliceio_register(void) | |
294 | { | |
295 | register_ioengine(&ioengine); | |
296 | } | |
297 | ||
298 | static void fio_exit fio_spliceio_unregister(void) | |
299 | { | |
300 | unregister_ioengine(&ioengine); | |
301 | } |