net engine: cleanup the splice handling
[fio.git] / engines / splice.c
1 /*
2  * splice engine
3  *
4  * IO engine that transfers data by doing splices to/from pipes and
5  * the files.
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>
14
15 #include "../fio.h"
16
17 #ifdef FIO_HAVE_SPLICE
18
19 struct spliceio_data {
20         int pipe[2];
21         int vmsplice_to_user;
22 };
23
24 /*
25  * vmsplice didn't use to support splicing to user space, this is the old
26  * variant of getting that job done. Doesn't make a lot of sense, but it
27  * uses splices to move data from the source into a pipe.
28  */
29 static int fio_splice_read_old(struct thread_data *td, struct io_u *io_u)
30 {
31         struct spliceio_data *sd = td->io_ops->data;
32         struct fio_file *f = io_u->file;
33         int ret, ret2, buflen;
34         off_t offset;
35         void *p;
36
37         offset = io_u->offset;
38         buflen = io_u->xfer_buflen;
39         p = io_u->xfer_buf;
40         while (buflen) {
41                 int this_len = buflen;
42
43                 if (this_len > SPLICE_DEF_SIZE)
44                         this_len = SPLICE_DEF_SIZE;
45
46                 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE);
47                 if (ret < 0) {
48                         if (errno == ENODATA || errno == EAGAIN)
49                                 continue;
50
51                         return -errno;
52                 }
53
54                 buflen -= ret;
55
56                 while (ret) {
57                         ret2 = read(sd->pipe[0], p, ret);
58                         if (ret2 < 0)
59                                 return -errno;
60
61                         ret -= ret2;
62                         p += ret2;
63                 }
64         }
65
66         return io_u->xfer_buflen;
67 }
68
69 /*
70  * We can now vmsplice into userspace, so do the transfer by splicing into
71  * a pipe and vmsplicing that into userspace.
72  */
73 static int fio_splice_read(struct thread_data *td, struct io_u *io_u)
74 {
75         struct spliceio_data *sd = td->io_ops->data;
76         struct fio_file *f = io_u->file;
77         struct iovec iov;
78         int ret, buflen;
79         off_t offset;
80         void *p;
81
82         offset = io_u->offset;
83         buflen = io_u->xfer_buflen;
84         p = io_u->xfer_buf;
85         while (buflen) {
86                 int this_len = buflen;
87
88                 if (this_len > SPLICE_DEF_SIZE)
89                         this_len = SPLICE_DEF_SIZE;
90
91                 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE);
92                 if (ret < 0) {
93                         if (errno == ENODATA || errno == EAGAIN)
94                                 continue;
95
96                         return -errno;
97                 }
98
99                 buflen -= ret;
100                 iov.iov_base = p;
101                 iov.iov_len = ret;
102                 p += ret;
103
104                 while (iov.iov_len) {
105                         ret = vmsplice(sd->pipe[0], &iov, 1, SPLICE_F_MOVE);
106                         if (ret < 0)
107                                 return -errno;
108                         else if (!ret)
109                                 return -ENODATA;
110
111                         iov.iov_len -= ret;
112                         iov.iov_base += ret;
113                 }
114         }
115
116         return io_u->xfer_buflen;
117 }
118
119
120 /*
121  * For splice writing, we can vmsplice our data buffer directly into a
122  * pipe and then splice that to a file.
123  */
124 static int fio_splice_write(struct thread_data *td, struct io_u *io_u)
125 {
126         struct spliceio_data *sd = td->io_ops->data;
127         struct iovec iov = {
128                 .iov_base = io_u->xfer_buf,
129                 .iov_len = io_u->xfer_buflen,
130         };
131         struct pollfd pfd = { .fd = sd->pipe[1], .events = POLLOUT, };
132         struct fio_file *f = io_u->file;
133         off_t off = io_u->offset;
134         int ret, ret2;
135
136         while (iov.iov_len) {
137                 if (poll(&pfd, 1, -1) < 0)
138                         return errno;
139
140                 ret = vmsplice(sd->pipe[1], &iov, 1, SPLICE_F_NONBLOCK);
141                 if (ret < 0)
142                         return -errno;
143
144                 iov.iov_len -= ret;
145                 iov.iov_base += ret;
146
147                 while (ret) {
148                         ret2 = splice(sd->pipe[0], NULL, f->fd, &off, ret, 0);
149                         if (ret2 < 0)
150                                 return -errno;
151
152                         ret -= ret2;
153                 }
154         }
155
156         return io_u->xfer_buflen;
157 }
158
159 static int fio_spliceio_queue(struct thread_data *td, struct io_u *io_u)
160 {
161         struct spliceio_data *sd = td->io_ops->data;
162         int ret;
163
164         if (io_u->ddir == DDIR_READ) {
165                 if (sd->vmsplice_to_user)
166                         ret = fio_splice_read(td, io_u);
167                 else
168                         ret = fio_splice_read_old(td, io_u);
169         } else if (io_u->ddir == DDIR_WRITE)
170                 ret = fio_splice_write(td, io_u);
171         else
172                 ret = fsync(io_u->file->fd);
173
174         if (ret != (int) io_u->xfer_buflen) {
175                 if (ret >= 0) {
176                         io_u->resid = io_u->xfer_buflen - ret;
177                         io_u->error = 0;
178                         return FIO_Q_COMPLETED;
179                 } else
180                         io_u->error = errno;
181         }
182
183         if (io_u->error)
184                 td_verror(td, io_u->error, "xfer");
185
186         return FIO_Q_COMPLETED;
187 }
188
189 static void fio_spliceio_cleanup(struct thread_data *td)
190 {
191         struct spliceio_data *sd = td->io_ops->data;
192
193         if (sd) {
194                 close(sd->pipe[0]);
195                 close(sd->pipe[1]);
196                 free(sd);
197                 td->io_ops->data = NULL;
198         }
199 }
200
201 static int fio_spliceio_init(struct thread_data *td)
202 {
203         struct spliceio_data *sd = malloc(sizeof(*sd));
204
205         if (pipe(sd->pipe) < 0) {
206                 td_verror(td, errno, "pipe");
207                 free(sd);
208                 return 1;
209         }
210
211         /*
212          * need some check for enabling this, for now just leave it disabled
213          */
214         sd->vmsplice_to_user = 0;
215
216         td->io_ops->data = sd;
217         return 0;
218 }
219
220 static struct ioengine_ops ioengine = {
221         .name           = "splice",
222         .version        = FIO_IOOPS_VERSION,
223         .init           = fio_spliceio_init,
224         .queue          = fio_spliceio_queue,
225         .cleanup        = fio_spliceio_cleanup,
226         .open_file      = generic_open_file,
227         .close_file     = generic_close_file,
228         .flags          = FIO_SYNCIO,
229 };
230
231 #else /* FIO_HAVE_SPLICE */
232
233 /*
234  * When we have a proper configure system in place, we simply wont build
235  * and install this io engine. For now install a crippled version that
236  * just complains and fails to load.
237  */
238 static int fio_spliceio_init(struct thread_data fio_unused *td)
239 {
240         fprintf(stderr, "fio: splice not available\n");
241         return 1;
242 }
243
244 static struct ioengine_ops ioengine = {
245         .name           = "splice",
246         .version        = FIO_IOOPS_VERSION,
247         .init           = fio_spliceio_init,
248 };
249
250 #endif
251
252 static void fio_init fio_spliceio_register(void)
253 {
254         register_ioengine(&ioengine);
255 }
256
257 static void fio_exit fio_spliceio_unregister(void)
258 {
259         unregister_ioengine(&ioengine);
260 }