net engine: termination fixes
[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>
7292056a
JA
16#include <sys/types.h>
17#include <sys/socket.h>
ed92ac0c
JA
18
19#include "../fio.h"
ed92ac0c 20
b5af8293
JA
21struct netio_data {
22 int listenfd;
23 int send_to_net;
9cce02e8 24 int use_splice;
414c2a3e 25 int net_protocol;
9cce02e8 26 int pipes[2];
b5af8293
JA
27 char host[64];
28 struct sockaddr_in addr;
29};
ed92ac0c 30
371d456c
JA
31/*
32 * Return -1 for error and 'nr events' for a positive number
33 * of events
34 */
35static int poll_wait(struct thread_data *td, int fd, short events)
36{
37 struct pollfd pfd;
38 int ret;
39
40 while (!td->terminate) {
41 pfd.fd = fd;
42 pfd.events = events;
43 ret = poll(&pfd, 1, -1);
44 if (ret < 0) {
45 if (errno == EINTR)
d5b388a5 46 break;
371d456c
JA
47
48 td_verror(td, errno, "poll");
49 return -1;
50 } else if (!ret)
51 continue;
52
53 break;
54 }
55
56 if (pfd.revents & events)
57 return 1;
371d456c
JA
58
59 return -1;
60}
61
ed92ac0c
JA
62static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
63{
b5af8293 64 struct netio_data *nd = td->io_ops->data;
ed92ac0c 65
7a6499da
JA
66 /*
67 * Make sure we don't see spurious reads to a receiver, and vice versa
68 */
b5af8293
JA
69 if ((nd->send_to_net && io_u->ddir == DDIR_READ) ||
70 (!nd->send_to_net && io_u->ddir == DDIR_WRITE)) {
e1161c32 71 td_verror(td, EINVAL, "bad direction");
7a6499da 72 return 1;
ed92ac0c 73 }
7a6499da 74
f85ac25a 75 return 0;
ed92ac0c
JA
76}
77
5921e80c 78#ifdef FIO_HAVE_SPLICE
cd963e18 79static int splice_io_u(int fdin, int fdout, unsigned int len)
ed92ac0c 80{
9cce02e8 81 int bytes = 0;
7a6499da 82
9cce02e8 83 while (len) {
cd963e18 84 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
9cce02e8
JA
85
86 if (ret < 0) {
87 if (!bytes)
88 bytes = ret;
89
90 break;
91 } else if (!ret)
92 break;
93
94 bytes += ret;
f657a2fb 95 len -= ret;
9cce02e8
JA
96 }
97
98 return bytes;
99}
100
101/*
cd963e18 102 * Receive bytes from a socket and fill them into the internal pipe
9cce02e8 103 */
cd963e18 104static int splice_in(struct thread_data *td, struct io_u *io_u)
9cce02e8
JA
105{
106 struct netio_data *nd = td->io_ops->data;
9cce02e8 107
cd963e18 108 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
9cce02e8
JA
109}
110
111/*
cd963e18 112 * Transmit 'len' bytes from the internal pipe
9cce02e8 113 */
cd963e18
JA
114static int splice_out(struct thread_data *td, struct io_u *io_u,
115 unsigned int len)
9cce02e8
JA
116{
117 struct netio_data *nd = td->io_ops->data;
cd963e18
JA
118
119 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
120}
121
122static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
123{
9cce02e8
JA
124 struct iovec iov = {
125 .iov_base = io_u->xfer_buf,
126 .iov_len = len,
127 };
128 int bytes = 0;
129
130 while (iov.iov_len) {
cd963e18 131 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
9cce02e8
JA
132
133 if (ret < 0) {
134 if (!bytes)
135 bytes = ret;
136 break;
137 } else if (!ret)
138 break;
139
140 iov.iov_len -= ret;
cd963e18 141 iov.iov_base += ret;
f657a2fb 142 bytes += ret;
9cce02e8
JA
143 }
144
145 return bytes;
cd963e18 146
9cce02e8
JA
147}
148
149/*
cd963e18 150 * vmsplice() pipe to io_u buffer
9cce02e8 151 */
cd963e18
JA
152static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
153 unsigned int len)
9cce02e8
JA
154{
155 struct netio_data *nd = td->io_ops->data;
9cce02e8 156
cd963e18
JA
157 return vmsplice_io_u(io_u, nd->pipes[0], len);
158}
9cce02e8 159
cd963e18
JA
160/*
161 * vmsplice() io_u to pipe
162 */
163static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
164{
165 struct netio_data *nd = td->io_ops->data;
ed92ac0c 166
cd963e18 167 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
9cce02e8
JA
168}
169
cd963e18
JA
170/*
171 * splice receive - transfer socket data into a pipe using splice, then map
172 * that pipe data into the io_u using vmsplice.
173 */
9cce02e8
JA
174static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
175{
176 int ret;
177
178 ret = splice_in(td, io_u);
cd963e18
JA
179 if (ret > 0)
180 return vmsplice_io_u_out(td, io_u, ret);
9cce02e8 181
cd963e18 182 return ret;
9cce02e8
JA
183}
184
cd963e18
JA
185/*
186 * splice transmit - map data from the io_u into a pipe by using vmsplice,
187 * then transfer that pipe to a socket using splice.
188 */
9cce02e8
JA
189static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
190{
191 int ret;
192
193 ret = vmsplice_io_u_in(td, io_u);
cd963e18
JA
194 if (ret > 0)
195 return splice_out(td, io_u, ret);
9cce02e8 196
cd963e18 197 return ret;
9cce02e8 198}
5921e80c
JA
199#else
200static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
201{
af8771b9 202 errno = EOPNOTSUPP;
5921e80c
JA
203 return -1;
204}
205
206static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
207{
af8771b9 208 errno = EOPNOTSUPP;
5921e80c
JA
209 return -1;
210}
211#endif
9cce02e8
JA
212
213static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
214{
414c2a3e 215 struct netio_data *nd = td->io_ops->data;
371d456c
JA
216 int ret, flags = 0;
217
218 ret = poll_wait(td, io_u->file->fd, POLLOUT);
219 if (ret <= 0)
220 return ret;
9cce02e8
JA
221
222 /*
223 * if we are going to write more, set MSG_MORE
224 */
5921e80c 225#ifdef MSG_MORE
9cce02e8
JA
226 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen < td->o.size)
227 flags = MSG_MORE;
5921e80c 228#endif
9cce02e8 229
414c2a3e
JA
230 if (nd->net_protocol == IPPROTO_UDP) {
231 return sendto(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen,
35651820 232 0, &nd->addr, sizeof(nd->addr));
414c2a3e
JA
233 } else {
234 return send(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen,
235 flags);
236 }
9cce02e8
JA
237}
238
414c2a3e 239static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
9cce02e8 240{
414c2a3e 241 struct netio_data *nd = td->io_ops->data;
371d456c
JA
242 int ret, flags = MSG_WAITALL;
243
244 ret = poll_wait(td, io_u->file->fd, POLLIN);
245 if (ret <= 0)
246 return ret;
9cce02e8 247
414c2a3e
JA
248 if (nd->net_protocol == IPPROTO_UDP) {
249 socklen_t len = sizeof(nd->addr);
250
251 return recvfrom(io_u->file->fd, io_u->xfer_buf,
252 io_u->xfer_buflen, 0, &nd->addr, &len);
253 } else {
254 return recv(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen,
255 flags);
256 }
9cce02e8
JA
257}
258
259static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
260{
261 struct netio_data *nd = td->io_ops->data;
262 int ret;
263
7101d9c2
JA
264 fio_ro_check(td, io_u);
265
9cce02e8 266 if (io_u->ddir == DDIR_WRITE) {
414c2a3e 267 if (!nd->use_splice || nd->net_protocol == IPPROTO_UDP)
9cce02e8 268 ret = fio_netio_send(td, io_u);
414c2a3e
JA
269 else
270 ret = fio_netio_splice_out(td, io_u);
d4f12dd0 271 } else if (io_u->ddir == DDIR_READ) {
414c2a3e
JA
272 if (!nd->use_splice || nd->net_protocol == IPPROTO_UDP)
273 ret = fio_netio_recv(td, io_u);
9cce02e8 274 else
414c2a3e 275 ret = fio_netio_splice_in(td, io_u);
d4f12dd0 276 } else
7a6499da 277 ret = 0; /* must be a SYNC */
ed92ac0c 278
cec6b55d 279 if (ret != (int) io_u->xfer_buflen) {
22819ec2 280 if (ret >= 0) {
cec6b55d
JA
281 io_u->resid = io_u->xfer_buflen - ret;
282 io_u->error = 0;
36167d82 283 return FIO_Q_COMPLETED;
414c2a3e
JA
284 } else {
285 int err = errno;
286
287 if (io_u->ddir == DDIR_WRITE && err == EMSGSIZE)
288 return FIO_Q_BUSY;
289
290 io_u->error = err;
291 }
ed92ac0c
JA
292 }
293
36167d82 294 if (io_u->error)
e1161c32 295 td_verror(td, io_u->error, "xfer");
ed92ac0c 296
36167d82 297 return FIO_Q_COMPLETED;
ed92ac0c
JA
298}
299
b5af8293 300static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
ed92ac0c 301{
b5af8293 302 struct netio_data *nd = td->io_ops->data;
414c2a3e
JA
303 int type;
304
305 if (nd->net_protocol == IPPROTO_TCP)
306 type = SOCK_STREAM;
307 else
308 type = SOCK_DGRAM;
ed92ac0c 309
414c2a3e 310 f->fd = socket(AF_INET, type, nd->net_protocol);
b5af8293
JA
311 if (f->fd < 0) {
312 td_verror(td, errno, "socket");
313 return 1;
ed92ac0c
JA
314 }
315
414c2a3e
JA
316 if (nd->net_protocol == IPPROTO_UDP)
317 return 0;
318
b5af8293
JA
319 if (connect(f->fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
320 td_verror(td, errno, "connect");
321 return 1;
ed92ac0c
JA
322 }
323
324 return 0;
ed92ac0c
JA
325}
326
b5af8293 327static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
5fdd124a 328{
b5af8293
JA
329 struct netio_data *nd = td->io_ops->data;
330 socklen_t socklen = sizeof(nd->addr);
5fdd124a 331
414c2a3e
JA
332 if (nd->net_protocol == IPPROTO_UDP) {
333 f->fd = nd->listenfd;
334 return 0;
335 }
336
6d86144d 337 log_info("fio: waiting for connection\n");
5fdd124a 338
371d456c
JA
339 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
340 return 1;
0c09442b 341
371d456c
JA
342 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
343 if (f->fd < 0) {
344 td_verror(td, errno, "accept");
345 return 1;
b5af8293 346 }
5fdd124a 347
b5af8293
JA
348 return 0;
349}
350
b5af8293
JA
351static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
352{
353 if (td_read(td))
354 return fio_netio_accept(td, f);
355 else
356 return fio_netio_connect(td, f);
357}
358
359static int fio_netio_setup_connect(struct thread_data *td, const char *host,
360 unsigned short port)
361{
362 struct netio_data *nd = td->io_ops->data;
363
364 nd->addr.sin_family = AF_INET;
365 nd->addr.sin_port = htons(port);
366
367 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
368 struct hostent *hent;
369
370 hent = gethostbyname(host);
371 if (!hent) {
372 td_verror(td, errno, "gethostbyname");
373 return 1;
5fdd124a 374 }
b5af8293
JA
375
376 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
5fdd124a
JA
377 }
378
379 return 0;
380}
381
b5af8293 382static int fio_netio_setup_listen(struct thread_data *td, short port)
ed92ac0c 383{
b5af8293 384 struct netio_data *nd = td->io_ops->data;
414c2a3e 385 int fd, opt, type;
ed92ac0c 386
414c2a3e
JA
387 if (nd->net_protocol == IPPROTO_TCP)
388 type = SOCK_STREAM;
389 else
390 type = SOCK_DGRAM;
391
392 fd = socket(AF_INET, type, nd->net_protocol);
ed92ac0c 393 if (fd < 0) {
e1161c32 394 td_verror(td, errno, "socket");
ed92ac0c
JA
395 return 1;
396 }
397
398 opt = 1;
399 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
e1161c32 400 td_verror(td, errno, "setsockopt");
ed92ac0c
JA
401 return 1;
402 }
6bedbfaf
JA
403#ifdef SO_REUSEPORT
404 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
e1161c32 405 td_verror(td, errno, "setsockopt");
6bedbfaf
JA
406 return 1;
407 }
408#endif
ed92ac0c 409
b5af8293
JA
410 nd->addr.sin_family = AF_INET;
411 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
412 nd->addr.sin_port = htons(port);
ed92ac0c 413
b5af8293 414 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
e1161c32 415 td_verror(td, errno, "bind");
ed92ac0c
JA
416 return 1;
417 }
414c2a3e 418 if (nd->net_protocol == IPPROTO_TCP && listen(fd, 1) < 0) {
e1161c32 419 td_verror(td, errno, "listen");
ed92ac0c
JA
420 return 1;
421 }
422
b5af8293
JA
423 nd->listenfd = fd;
424 return 0;
ed92ac0c
JA
425}
426
9bec88e1 427static int fio_netio_init(struct thread_data *td)
ed92ac0c 428{
b5af8293 429 struct netio_data *nd = td->io_ops->data;
443662ef 430 unsigned int port;
b5af8293 431 char host[64], buf[128];
414c2a3e 432 char *sep, *portp, *modep;
af52b345 433 int ret;
ed92ac0c 434
413dd459 435 if (td_rw(td)) {
ed92ac0c
JA
436 log_err("fio: network connections must be read OR write\n");
437 return 1;
438 }
16d55aae
JA
439 if (td_random(td)) {
440 log_err("fio: network IO can't be random\n");
441 return 1;
442 }
ed92ac0c 443
2dc1bbeb 444 strcpy(buf, td->o.filename);
ed92ac0c 445
9f9214f2 446 sep = strchr(buf, '/');
443662ef
JA
447 if (!sep)
448 goto bad_host;
ed92ac0c
JA
449
450 *sep = '\0';
451 sep++;
452 strcpy(host, buf);
443662ef
JA
453 if (!strlen(host))
454 goto bad_host;
455
414c2a3e
JA
456 modep = NULL;
457 portp = sep;
458 sep = strchr(portp, '/');
459 if (sep) {
460 *sep = '\0';
461 modep = sep + 1;
462 }
463
464 port = strtol(portp, NULL, 10);
443662ef
JA
465 if (!port || port > 65535)
466 goto bad_host;
ed92ac0c 467
414c2a3e 468 if (modep) {
3f8fc5ad
JA
469 if (!strncmp("tcp", modep, strlen(modep)) ||
470 !strncmp("TCP", modep, strlen(modep)))
414c2a3e 471 nd->net_protocol = IPPROTO_TCP;
3f8fc5ad
JA
472 else if (!strncmp("udp", modep, strlen(modep)) ||
473 !strncmp("UDP", modep, strlen(modep)))
414c2a3e
JA
474 nd->net_protocol = IPPROTO_UDP;
475 else
476 goto bad_host;
477 } else
478 nd->net_protocol = IPPROTO_TCP;
479
413dd459 480 if (td_read(td)) {
b5af8293 481 nd->send_to_net = 0;
ed92ac0c
JA
482 ret = fio_netio_setup_listen(td, port);
483 } else {
b5af8293 484 nd->send_to_net = 1;
ed92ac0c
JA
485 ret = fio_netio_setup_connect(td, host, port);
486 }
487
7bb48f84 488 return ret;
443662ef 489bad_host:
414c2a3e 490 log_err("fio: bad network host/port/protocol: %s\n", td->o.filename);
443662ef 491 return 1;
ed92ac0c
JA
492}
493
b5af8293 494static void fio_netio_cleanup(struct thread_data *td)
9bec88e1 495{
b5af8293
JA
496 struct netio_data *nd = td->io_ops->data;
497
498 if (nd) {
64b24cd8
JA
499 if (nd->listenfd != -1)
500 close(nd->listenfd);
501 if (nd->pipes[0] != -1)
502 close(nd->pipes[0]);
503 if (nd->pipes[1] != -1)
504 close(nd->pipes[1]);
505
b5af8293 506 free(nd);
b5af8293
JA
507 }
508}
509
510static int fio_netio_setup(struct thread_data *td)
511{
7bb48f84 512 struct netio_data *nd;
7bb48f84
JA
513
514 if (!td->io_ops->data) {
515 nd = malloc(sizeof(*nd));;
516
517 memset(nd, 0, sizeof(*nd));
518 nd->listenfd = -1;
64b24cd8 519 nd->pipes[0] = nd->pipes[1] = -1;
7bb48f84 520 td->io_ops->data = nd;
7bb48f84 521 }
b5af8293 522
9bec88e1
JA
523 return 0;
524}
525
5921e80c 526#ifdef FIO_HAVE_SPLICE
9cce02e8
JA
527static int fio_netio_setup_splice(struct thread_data *td)
528{
529 struct netio_data *nd;
530
531 fio_netio_setup(td);
532
533 nd = td->io_ops->data;
534 if (nd) {
535 if (pipe(nd->pipes) < 0)
536 return 1;
537
538 nd->use_splice = 1;
539 return 0;
540 }
541
542 return 1;
543}
544
5921e80c
JA
545static struct ioengine_ops ioengine_splice = {
546 .name = "netsplice",
ed92ac0c 547 .version = FIO_IOOPS_VERSION,
ed92ac0c
JA
548 .prep = fio_netio_prep,
549 .queue = fio_netio_queue,
5921e80c 550 .setup = fio_netio_setup_splice,
9bec88e1 551 .init = fio_netio_init,
b5af8293
JA
552 .cleanup = fio_netio_cleanup,
553 .open_file = fio_netio_open_file,
554 .close_file = generic_close_file,
ad830ed7
JA
555 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
556 FIO_SIGQUIT,
ed92ac0c 557};
5921e80c 558#endif
ed92ac0c 559
5921e80c
JA
560static struct ioengine_ops ioengine_rw = {
561 .name = "net",
9cce02e8
JA
562 .version = FIO_IOOPS_VERSION,
563 .prep = fio_netio_prep,
564 .queue = fio_netio_queue,
5921e80c 565 .setup = fio_netio_setup,
9cce02e8
JA
566 .init = fio_netio_init,
567 .cleanup = fio_netio_cleanup,
568 .open_file = fio_netio_open_file,
569 .close_file = generic_close_file,
ad830ed7
JA
570 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
571 FIO_SIGQUIT,
9cce02e8
JA
572};
573
ed92ac0c
JA
574static void fio_init fio_netio_register(void)
575{
9cce02e8 576 register_ioengine(&ioengine_rw);
5921e80c 577#ifdef FIO_HAVE_SPLICE
9cce02e8 578 register_ioengine(&ioengine_splice);
5921e80c 579#endif
ed92ac0c
JA
580}
581
582static void fio_exit fio_netio_unregister(void)
583{
9cce02e8 584 unregister_ioengine(&ioengine_rw);
5921e80c 585#ifdef FIO_HAVE_SPLICE
9cce02e8 586 unregister_ioengine(&ioengine_splice);
5921e80c 587#endif
ed92ac0c 588}