Support for 'netsplice' engine
[fio.git] / engines / net.c
CommitLineData
ed92ac0c 1/*
da751ca9
JA
2 * net engine
3 *
4 * IO engine that reads/writes to/from sockets.
5 *
ed92ac0c
JA
6 */
7#include <stdio.h>
8#include <stdlib.h>
9#include <unistd.h>
10#include <errno.h>
11#include <assert.h>
12#include <netinet/in.h>
13#include <arpa/inet.h>
14#include <netdb.h>
5fdd124a 15#include <sys/poll.h>
ed92ac0c
JA
16
17#include "../fio.h"
ed92ac0c 18
b5af8293
JA
19struct netio_data {
20 int listenfd;
21 int send_to_net;
9cce02e8
JA
22 int use_splice;
23 int pipes[2];
b5af8293
JA
24 char host[64];
25 struct sockaddr_in addr;
26};
ed92ac0c
JA
27
28static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
29{
b5af8293 30 struct netio_data *nd = td->io_ops->data;
ed92ac0c
JA
31 struct fio_file *f = io_u->file;
32
7a6499da
JA
33 /*
34 * Make sure we don't see spurious reads to a receiver, and vice versa
35 */
b5af8293
JA
36 if ((nd->send_to_net && io_u->ddir == DDIR_READ) ||
37 (!nd->send_to_net && io_u->ddir == DDIR_WRITE)) {
e1161c32 38 td_verror(td, EINVAL, "bad direction");
7a6499da 39 return 1;
ed92ac0c 40 }
7a6499da 41
ed92ac0c
JA
42 if (io_u->ddir == DDIR_SYNC)
43 return 0;
44 if (io_u->offset == f->last_completed_pos)
45 return 0;
46
e01547d2
JA
47 /*
48 * If offset is different from last end position, it's a seek.
49 * As network io is purely sequential, we don't allow seeks.
50 */
e1161c32 51 td_verror(td, EINVAL, "cannot seek");
ed92ac0c
JA
52 return 1;
53}
54
9cce02e8
JA
55/*
56 * Receive bytes from a socket and fill them into the internal pipe
57 */
58static int splice_in(struct thread_data *td, struct io_u *io_u)
ed92ac0c 59{
9cce02e8
JA
60 struct netio_data *nd = td->io_ops->data;
61 unsigned int len = io_u->xfer_buflen;
ed92ac0c 62 struct fio_file *f = io_u->file;
9cce02e8 63 int bytes = 0;
7a6499da 64
9cce02e8
JA
65 while (len) {
66 int ret = splice(nd->pipes[1], NULL, f->fd, NULL, len, 0);
67
68 if (ret < 0) {
69 if (!bytes)
70 bytes = ret;
71
72 break;
73 } else if (!ret)
74 break;
75
76 bytes += ret;
77 }
78
79 return bytes;
80}
81
82/*
83 * Transmit 'len' bytes from the internal pipe
84 */
85static int splice_out(struct thread_data *td, struct io_u *io_u,
86 unsigned int len)
87{
88 struct netio_data *nd = td->io_ops->data;
89 struct fio_file *f = io_u->file;
90 int bytes = 0;
91
92 while (len) {
93 int ret = splice(nd->pipes[0], NULL, f->fd, NULL, len, 0);
94
95 if (ret < 0) {
96 if (!bytes)
97 bytes = ret;
98
99 break;
100 } else if (!ret)
101 break;
102
103 bytes += ret;
104 len -= ret;
105 }
106
107 return bytes;
108}
109
110/*
111 * vmsplice() pipe to io_u buffer
112 */
113static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
114 unsigned int len)
115{
116 struct netio_data *nd = td->io_ops->data;
117 struct iovec iov = {
118 .iov_base = io_u->xfer_buf,
119 .iov_len = len,
120 };
121 int bytes = 0;
122
123 while (iov.iov_len) {
124 int ret = vmsplice(nd->pipes[0], &iov, 1, 0);
125
126 if (ret < 0) {
127 if (!bytes)
128 bytes = ret;
129 break;
130 } else if (!ret)
131 break;
132
133 iov.iov_len -= ret;
134 if (iov.iov_len)
135 iov.iov_base += ret;
136 }
137
138 return bytes;
139}
140
141/*
142 * vmsplice() io_u to pipe
143 */
144static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
145{
146 struct netio_data *nd = td->io_ops->data;
147 struct iovec iov = {
148 .iov_base = io_u->xfer_buf,
149 .iov_len = io_u->xfer_buflen,
150 };
151 unsigned int bytes = 0;
152
153 while (iov.iov_len) {
154 int ret = vmsplice(nd->pipes[1], &iov, 1, 0);
155
156 if (ret < 0)
157 return -1;
158 else if (!ret)
159 return bytes;
160
161 iov.iov_len -= ret;
162 bytes += ret;
163 if (iov.iov_len)
164 iov.iov_base += ret;
165 }
ed92ac0c 166
9cce02e8
JA
167 return bytes;
168}
169
170static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
171{
172 int ret;
173
174 ret = splice_in(td, io_u);
175 if (ret <= 0)
176 return ret;
177
178 return vmsplice_io_u_out(td, io_u, ret);
179}
180
181static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
182{
183 int ret;
184
185 ret = vmsplice_io_u_in(td, io_u);
186 if (ret <= 0)
187 return ret;
188
189 return splice_out(td, io_u, ret);
190}
191
192static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
193{
194 int flags = 0;
195
196 /*
197 * if we are going to write more, set MSG_MORE
198 */
199 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen < td->o.size)
200 flags = MSG_MORE;
201
202 return send(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
203}
204
205static int fio_netio_recv(struct io_u *io_u)
206{
207 int flags = MSG_WAITALL;
208
209 return recv(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
210}
211
212static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
213{
214 struct netio_data *nd = td->io_ops->data;
215 int ret;
216
217 if (io_u->ddir == DDIR_WRITE) {
218 if (nd->use_splice)
219 ret = fio_netio_splice_out(td, io_u);
220 else
221 ret = fio_netio_send(td, io_u);
d4f12dd0 222 } else if (io_u->ddir == DDIR_READ) {
9cce02e8
JA
223 if (nd->use_splice)
224 ret = fio_netio_splice_in(td, io_u);
225 else
226 ret = fio_netio_recv(io_u);
d4f12dd0 227 } else
7a6499da 228 ret = 0; /* must be a SYNC */
ed92ac0c 229
cec6b55d 230 if (ret != (int) io_u->xfer_buflen) {
22819ec2 231 if (ret >= 0) {
cec6b55d
JA
232 io_u->resid = io_u->xfer_buflen - ret;
233 io_u->error = 0;
36167d82 234 return FIO_Q_COMPLETED;
ed92ac0c
JA
235 } else
236 io_u->error = errno;
237 }
238
36167d82 239 if (io_u->error)
e1161c32 240 td_verror(td, io_u->error, "xfer");
ed92ac0c 241
36167d82 242 return FIO_Q_COMPLETED;
ed92ac0c
JA
243}
244
b5af8293 245static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
ed92ac0c 246{
b5af8293 247 struct netio_data *nd = td->io_ops->data;
ed92ac0c 248
b5af8293
JA
249 f->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
250 if (f->fd < 0) {
251 td_verror(td, errno, "socket");
252 return 1;
ed92ac0c
JA
253 }
254
b5af8293
JA
255 if (connect(f->fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
256 td_verror(td, errno, "connect");
257 return 1;
ed92ac0c
JA
258 }
259
260 return 0;
ed92ac0c
JA
261}
262
b5af8293 263static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
5fdd124a 264{
b5af8293
JA
265 struct netio_data *nd = td->io_ops->data;
266 socklen_t socklen = sizeof(nd->addr);
5fdd124a 267 struct pollfd pfd;
b5af8293 268 int ret;
5fdd124a 269
6d86144d 270 log_info("fio: waiting for connection\n");
5fdd124a
JA
271
272 /*
273 * Accept loop. poll for incoming events, accept them. Repeat until we
274 * have all connections.
275 */
b5af8293
JA
276 while (!td->terminate) {
277 pfd.fd = nd->listenfd;
5fdd124a
JA
278 pfd.events = POLLIN;
279
280 ret = poll(&pfd, 1, -1);
281 if (ret < 0) {
282 if (errno == EINTR)
283 continue;
284
e1161c32 285 td_verror(td, errno, "poll");
5fdd124a
JA
286 break;
287 } else if (!ret)
288 continue;
289
0c09442b
JA
290 /*
291 * should be impossible
292 */
293 if (!(pfd.revents & POLLIN))
294 continue;
295
b5af8293
JA
296 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
297 if (f->fd < 0) {
298 td_verror(td, errno, "accept");
299 return 1;
300 }
301 break;
302 }
5fdd124a 303
b5af8293
JA
304 return 0;
305}
306
307
308static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
309{
310 if (td_read(td))
311 return fio_netio_accept(td, f);
312 else
313 return fio_netio_connect(td, f);
314}
315
316static int fio_netio_setup_connect(struct thread_data *td, const char *host,
317 unsigned short port)
318{
319 struct netio_data *nd = td->io_ops->data;
320
321 nd->addr.sin_family = AF_INET;
322 nd->addr.sin_port = htons(port);
323
324 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
325 struct hostent *hent;
326
327 hent = gethostbyname(host);
328 if (!hent) {
329 td_verror(td, errno, "gethostbyname");
330 return 1;
5fdd124a 331 }
b5af8293
JA
332
333 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
5fdd124a
JA
334 }
335
336 return 0;
337}
338
b5af8293 339static int fio_netio_setup_listen(struct thread_data *td, short port)
ed92ac0c 340{
b5af8293 341 struct netio_data *nd = td->io_ops->data;
5fdd124a 342 int fd, opt;
ed92ac0c 343
6bedbfaf 344 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
ed92ac0c 345 if (fd < 0) {
e1161c32 346 td_verror(td, errno, "socket");
ed92ac0c
JA
347 return 1;
348 }
349
350 opt = 1;
351 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
e1161c32 352 td_verror(td, errno, "setsockopt");
ed92ac0c
JA
353 return 1;
354 }
6bedbfaf
JA
355#ifdef SO_REUSEPORT
356 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
e1161c32 357 td_verror(td, errno, "setsockopt");
6bedbfaf
JA
358 return 1;
359 }
360#endif
ed92ac0c 361
b5af8293
JA
362 nd->addr.sin_family = AF_INET;
363 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
364 nd->addr.sin_port = htons(port);
ed92ac0c 365
b5af8293 366 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
e1161c32 367 td_verror(td, errno, "bind");
ed92ac0c
JA
368 return 1;
369 }
370 if (listen(fd, 1) < 0) {
e1161c32 371 td_verror(td, errno, "listen");
ed92ac0c
JA
372 return 1;
373 }
374
b5af8293
JA
375 nd->listenfd = fd;
376 return 0;
ed92ac0c
JA
377}
378
9bec88e1 379static int fio_netio_init(struct thread_data *td)
ed92ac0c 380{
b5af8293 381 struct netio_data *nd = td->io_ops->data;
e01547d2 382 unsigned short port;
b5af8293 383 char host[64], buf[128];
ed92ac0c 384 char *sep;
af52b345 385 int ret;
ed92ac0c 386
413dd459 387 if (td_rw(td)) {
ed92ac0c
JA
388 log_err("fio: network connections must be read OR write\n");
389 return 1;
390 }
16d55aae
JA
391 if (td_random(td)) {
392 log_err("fio: network IO can't be random\n");
393 return 1;
394 }
ed92ac0c 395
2dc1bbeb 396 strcpy(buf, td->o.filename);
ed92ac0c 397
9f9214f2 398 sep = strchr(buf, '/');
ed92ac0c 399 if (!sep) {
2dc1bbeb 400 log_err("fio: bad network host/port <<%s>>\n", td->o.filename);
ed92ac0c
JA
401 return 1;
402 }
403
404 *sep = '\0';
405 sep++;
406 strcpy(host, buf);
e01547d2 407 port = atoi(sep);
ed92ac0c 408
413dd459 409 if (td_read(td)) {
b5af8293 410 nd->send_to_net = 0;
ed92ac0c
JA
411 ret = fio_netio_setup_listen(td, port);
412 } else {
b5af8293 413 nd->send_to_net = 1;
ed92ac0c
JA
414 ret = fio_netio_setup_connect(td, host, port);
415 }
416
7bb48f84 417 return ret;
ed92ac0c
JA
418}
419
b5af8293 420static void fio_netio_cleanup(struct thread_data *td)
9bec88e1 421{
b5af8293
JA
422 struct netio_data *nd = td->io_ops->data;
423
424 if (nd) {
425 free(nd);
426 td->io_ops->data = NULL;
427 }
428}
429
430static int fio_netio_setup(struct thread_data *td)
431{
7bb48f84 432 struct netio_data *nd;
7bb48f84
JA
433
434 if (!td->io_ops->data) {
435 nd = malloc(sizeof(*nd));;
436
437 memset(nd, 0, sizeof(*nd));
438 nd->listenfd = -1;
439 td->io_ops->data = nd;
7bb48f84 440 }
b5af8293 441
9bec88e1
JA
442 return 0;
443}
444
9cce02e8
JA
445static int fio_netio_setup_splice(struct thread_data *td)
446{
447 struct netio_data *nd;
448
449 fio_netio_setup(td);
450
451 nd = td->io_ops->data;
452 if (nd) {
453 if (pipe(nd->pipes) < 0)
454 return 1;
455
456 nd->use_splice = 1;
457 return 0;
458 }
459
460 return 1;
461}
462
463static struct ioengine_ops ioengine_rw = {
ed92ac0c
JA
464 .name = "net",
465 .version = FIO_IOOPS_VERSION,
ed92ac0c
JA
466 .prep = fio_netio_prep,
467 .queue = fio_netio_queue,
ed92ac0c 468 .setup = fio_netio_setup,
9bec88e1 469 .init = fio_netio_init,
b5af8293
JA
470 .cleanup = fio_netio_cleanup,
471 .open_file = fio_netio_open_file,
472 .close_file = generic_close_file,
473 .flags = FIO_SYNCIO | FIO_DISKLESSIO,
ed92ac0c
JA
474};
475
9cce02e8
JA
476static struct ioengine_ops ioengine_splice = {
477 .name = "netsplice",
478 .version = FIO_IOOPS_VERSION,
479 .prep = fio_netio_prep,
480 .queue = fio_netio_queue,
481 .setup = fio_netio_setup_splice,
482 .init = fio_netio_init,
483 .cleanup = fio_netio_cleanup,
484 .open_file = fio_netio_open_file,
485 .close_file = generic_close_file,
486 .flags = FIO_SYNCIO | FIO_DISKLESSIO,
487};
488
ed92ac0c
JA
489static void fio_init fio_netio_register(void)
490{
9cce02e8
JA
491 register_ioengine(&ioengine_rw);
492 register_ioengine(&ioengine_splice);
ed92ac0c
JA
493}
494
495static void fio_exit fio_netio_unregister(void)
496{
9cce02e8
JA
497 unregister_ioengine(&ioengine_rw);
498 unregister_ioengine(&ioengine_splice);
ed92ac0c 499}