net engine: implement option "nodelay" for TCP sockets
[fio.git] / engines / net.c
... / ...
CommitLineData
1/*
2 * net engine
3 *
4 * IO engine that reads/writes to/from sockets.
5 *
6 */
7#include <stdio.h>
8#include <stdlib.h>
9#include <unistd.h>
10#include <signal.h>
11#include <errno.h>
12#include <assert.h>
13#include <netinet/in.h>
14#include <netinet/tcp.h>
15#include <arpa/inet.h>
16#include <netdb.h>
17#include <sys/poll.h>
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <sys/socket.h>
21#include <sys/un.h>
22
23#include "../fio.h"
24
25struct netio_data {
26 int listenfd;
27 int use_splice;
28 int pipes[2];
29 struct sockaddr_in addr;
30 struct sockaddr_un addr_un;
31};
32
33struct netio_options {
34 struct thread_data *td;
35 unsigned int port;
36 unsigned int proto;
37 unsigned int listen;
38 unsigned int pingpong;
39 unsigned int nodelay;
40};
41
42struct udp_close_msg {
43 uint32_t magic;
44 uint32_t cmd;
45};
46
47enum {
48 FIO_LINK_CLOSE = 0x89,
49 FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b,
50 FIO_LINK_OPEN = 0x98,
51
52 FIO_TYPE_TCP = 1,
53 FIO_TYPE_UDP = 2,
54 FIO_TYPE_UNIX = 3,
55};
56
57static int str_hostname_cb(void *data, const char *input);
58static struct fio_option options[] = {
59 {
60 .name = "hostname",
61 .type = FIO_OPT_STR_STORE,
62 .cb = str_hostname_cb,
63 .help = "Hostname for net IO engine",
64 },
65 {
66 .name = "port",
67 .type = FIO_OPT_INT,
68 .off1 = offsetof(struct netio_options, port),
69 .minval = 1,
70 .maxval = 65535,
71 .help = "Port to use for TCP or UDP net connections",
72 },
73 {
74 .name = "protocol",
75 .alias = "proto",
76 .type = FIO_OPT_STR,
77 .off1 = offsetof(struct netio_options, proto),
78 .help = "Network protocol to use",
79 .def = "tcp",
80 .posval = {
81 { .ival = "tcp",
82 .oval = FIO_TYPE_TCP,
83 .help = "Transmission Control Protocol",
84 },
85 { .ival = "udp",
86 .oval = FIO_TYPE_UDP,
87 .help = "User Datagram Protocol",
88 },
89 { .ival = "unix",
90 .oval = FIO_TYPE_UNIX,
91 .help = "UNIX domain socket",
92 },
93 },
94 },
95 {
96 .name = "nodelay",
97 .type = FIO_OPT_BOOL,
98 .off1 = offsetof(struct netio_options, nodelay),
99 .help = "Use TCP_NODELAY on TCP connections",
100 },
101 {
102 .name = "listen",
103 .type = FIO_OPT_STR_SET,
104 .off1 = offsetof(struct netio_options, listen),
105 .help = "Listen for incoming TCP connections",
106 },
107 {
108 .name = "pingpong",
109 .type = FIO_OPT_STR_SET,
110 .off1 = offsetof(struct netio_options, pingpong),
111 .help = "Ping-pong IO requests",
112 },
113 {
114 .name = NULL,
115 },
116};
117
118/*
119 * Return -1 for error and 'nr events' for a positive number
120 * of events
121 */
122static int poll_wait(struct thread_data *td, int fd, short events)
123{
124 struct pollfd pfd;
125 int ret;
126
127 while (!td->terminate) {
128 pfd.fd = fd;
129 pfd.events = events;
130 ret = poll(&pfd, 1, -1);
131 if (ret < 0) {
132 if (errno == EINTR)
133 break;
134
135 td_verror(td, errno, "poll");
136 return -1;
137 } else if (!ret)
138 continue;
139
140 break;
141 }
142
143 if (pfd.revents & events)
144 return 1;
145
146 return -1;
147}
148
149static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
150{
151 struct netio_options *o = td->eo;
152
153 /*
154 * Make sure we don't see spurious reads to a receiver, and vice versa
155 */
156 if (o->proto == FIO_TYPE_TCP)
157 return 0;
158
159 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
160 (!o->listen && io_u->ddir == DDIR_READ)) {
161 td_verror(td, EINVAL, "bad direction");
162 return 1;
163 }
164
165 return 0;
166}
167
168#ifdef CONFIG_LINUX_SPLICE
169static int splice_io_u(int fdin, int fdout, unsigned int len)
170{
171 int bytes = 0;
172
173 while (len) {
174 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
175
176 if (ret < 0) {
177 if (!bytes)
178 bytes = ret;
179
180 break;
181 } else if (!ret)
182 break;
183
184 bytes += ret;
185 len -= ret;
186 }
187
188 return bytes;
189}
190
191/*
192 * Receive bytes from a socket and fill them into the internal pipe
193 */
194static int splice_in(struct thread_data *td, struct io_u *io_u)
195{
196 struct netio_data *nd = td->io_ops->data;
197
198 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
199}
200
201/*
202 * Transmit 'len' bytes from the internal pipe
203 */
204static int splice_out(struct thread_data *td, struct io_u *io_u,
205 unsigned int len)
206{
207 struct netio_data *nd = td->io_ops->data;
208
209 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
210}
211
212static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
213{
214 struct iovec iov = {
215 .iov_base = io_u->xfer_buf,
216 .iov_len = len,
217 };
218 int bytes = 0;
219
220 while (iov.iov_len) {
221 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
222
223 if (ret < 0) {
224 if (!bytes)
225 bytes = ret;
226 break;
227 } else if (!ret)
228 break;
229
230 iov.iov_len -= ret;
231 iov.iov_base += ret;
232 bytes += ret;
233 }
234
235 return bytes;
236
237}
238
239/*
240 * vmsplice() pipe to io_u buffer
241 */
242static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
243 unsigned int len)
244{
245 struct netio_data *nd = td->io_ops->data;
246
247 return vmsplice_io_u(io_u, nd->pipes[0], len);
248}
249
250/*
251 * vmsplice() io_u to pipe
252 */
253static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
254{
255 struct netio_data *nd = td->io_ops->data;
256
257 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
258}
259
260/*
261 * splice receive - transfer socket data into a pipe using splice, then map
262 * that pipe data into the io_u using vmsplice.
263 */
264static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
265{
266 int ret;
267
268 ret = splice_in(td, io_u);
269 if (ret > 0)
270 return vmsplice_io_u_out(td, io_u, ret);
271
272 return ret;
273}
274
275/*
276 * splice transmit - map data from the io_u into a pipe by using vmsplice,
277 * then transfer that pipe to a socket using splice.
278 */
279static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
280{
281 int ret;
282
283 ret = vmsplice_io_u_in(td, io_u);
284 if (ret > 0)
285 return splice_out(td, io_u, ret);
286
287 return ret;
288}
289#else
290static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
291{
292 errno = EOPNOTSUPP;
293 return -1;
294}
295
296static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
297{
298 errno = EOPNOTSUPP;
299 return -1;
300}
301#endif
302
303static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
304{
305 struct netio_data *nd = td->io_ops->data;
306 struct netio_options *o = td->eo;
307 int ret, flags = 0;
308
309 do {
310 if (o->proto == FIO_TYPE_UDP) {
311 struct sockaddr *to = (struct sockaddr *) &nd->addr;
312
313 ret = sendto(io_u->file->fd, io_u->xfer_buf,
314 io_u->xfer_buflen, flags, to,
315 sizeof(*to));
316 } else {
317 /*
318 * if we are going to write more, set MSG_MORE
319 */
320#ifdef MSG_MORE
321 if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
322 td->o.size) && !o->pingpong)
323 flags |= MSG_MORE;
324#endif
325 ret = send(io_u->file->fd, io_u->xfer_buf,
326 io_u->xfer_buflen, flags);
327 }
328 if (ret > 0)
329 break;
330
331 ret = poll_wait(td, io_u->file->fd, POLLOUT);
332 if (ret <= 0)
333 break;
334 } while (1);
335
336 return ret;
337}
338
339static int is_udp_close(struct io_u *io_u, int len)
340{
341 struct udp_close_msg *msg;
342
343 if (len != sizeof(struct udp_close_msg))
344 return 0;
345
346 msg = io_u->xfer_buf;
347 if (ntohl(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC)
348 return 0;
349 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
350 return 0;
351
352 return 1;
353}
354
355static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
356{
357 struct netio_data *nd = td->io_ops->data;
358 struct netio_options *o = td->eo;
359 int ret, flags = 0;
360
361 do {
362 if (o->proto == FIO_TYPE_UDP) {
363 socklen_t len = sizeof(nd->addr);
364 struct sockaddr *from = (struct sockaddr *) &nd->addr;
365
366 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
367 io_u->xfer_buflen, flags, from, &len);
368 if (is_udp_close(io_u, ret)) {
369 td->done = 1;
370 return 0;
371 }
372 } else {
373 ret = recv(io_u->file->fd, io_u->xfer_buf,
374 io_u->xfer_buflen, flags);
375 }
376 if (ret > 0)
377 break;
378 else if (!ret && (flags & MSG_WAITALL))
379 break;
380
381 ret = poll_wait(td, io_u->file->fd, POLLIN);
382 if (ret <= 0)
383 break;
384 flags |= MSG_WAITALL;
385 } while (1);
386
387 return ret;
388}
389
390static int __fio_netio_queue(struct thread_data *td, struct io_u *io_u,
391 enum fio_ddir ddir)
392{
393 struct netio_data *nd = td->io_ops->data;
394 struct netio_options *o = td->eo;
395 int ret;
396
397 if (ddir == DDIR_WRITE) {
398 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
399 o->proto == FIO_TYPE_UNIX)
400 ret = fio_netio_send(td, io_u);
401 else
402 ret = fio_netio_splice_out(td, io_u);
403 } else if (ddir == DDIR_READ) {
404 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
405 o->proto == FIO_TYPE_UNIX)
406 ret = fio_netio_recv(td, io_u);
407 else
408 ret = fio_netio_splice_in(td, io_u);
409 } else
410 ret = 0; /* must be a SYNC */
411
412 if (ret != (int) io_u->xfer_buflen) {
413 if (ret >= 0) {
414 io_u->resid = io_u->xfer_buflen - ret;
415 io_u->error = 0;
416 return FIO_Q_COMPLETED;
417 } else {
418 int err = errno;
419
420 if (ddir == DDIR_WRITE && err == EMSGSIZE)
421 return FIO_Q_BUSY;
422
423 io_u->error = err;
424 }
425 }
426
427 if (io_u->error)
428 td_verror(td, io_u->error, "xfer");
429
430 return FIO_Q_COMPLETED;
431}
432
433static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
434{
435 struct netio_options *o = td->eo;
436 int ret;
437
438 fio_ro_check(td, io_u);
439
440 ret = __fio_netio_queue(td, io_u, io_u->ddir);
441 if (!o->pingpong || ret != FIO_Q_COMPLETED)
442 return ret;
443
444 /*
445 * For ping-pong mode, receive or send reply as needed
446 */
447 if (td_read(td) && io_u->ddir == DDIR_READ)
448 ret = __fio_netio_queue(td, io_u, DDIR_WRITE);
449 else if (td_write(td) && io_u->ddir == DDIR_WRITE)
450 ret = __fio_netio_queue(td, io_u, DDIR_READ);
451
452 return ret;
453}
454
455static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
456{
457 struct netio_data *nd = td->io_ops->data;
458 struct netio_options *o = td->eo;
459 int type, domain, optval;
460
461 if (o->proto == FIO_TYPE_TCP) {
462 domain = AF_INET;
463 type = SOCK_STREAM;
464 } else if (o->proto == FIO_TYPE_UDP) {
465 domain = AF_INET;
466 type = SOCK_DGRAM;
467 } else if (o->proto == FIO_TYPE_UNIX) {
468 domain = AF_UNIX;
469 type = SOCK_STREAM;
470 } else {
471 log_err("fio: bad network type %d\n", o->proto);
472 f->fd = -1;
473 return 1;
474 }
475
476 f->fd = socket(domain, type, 0);
477 if (f->fd < 0) {
478 td_verror(td, errno, "socket");
479 return 1;
480 }
481
482 if (o->nodelay && o->proto == FIO_TYPE_TCP) {
483 optval = 1;
484 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof(int)) < 0) {
485 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
486 return 1;
487 }
488 }
489
490 if (o->proto == FIO_TYPE_UDP)
491 return 0;
492 else if (o->proto == FIO_TYPE_TCP) {
493 socklen_t len = sizeof(nd->addr);
494
495 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
496 td_verror(td, errno, "connect");
497 close(f->fd);
498 return 1;
499 }
500 } else {
501 struct sockaddr_un *addr = &nd->addr_un;
502 socklen_t len;
503
504 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
505
506 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
507 td_verror(td, errno, "connect");
508 close(f->fd);
509 return 1;
510 }
511 }
512
513 return 0;
514}
515
516static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
517{
518 struct netio_data *nd = td->io_ops->data;
519 struct netio_options *o = td->eo;
520 socklen_t socklen = sizeof(nd->addr);
521 int state, optval;
522
523 if (o->proto == FIO_TYPE_UDP) {
524 f->fd = nd->listenfd;
525 return 0;
526 }
527
528 state = td->runstate;
529 td_set_runstate(td, TD_SETTING_UP);
530
531 log_info("fio: waiting for connection\n");
532
533 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
534 goto err;
535
536 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
537 if (f->fd < 0) {
538 td_verror(td, errno, "accept");
539 goto err;
540 }
541
542 if (o->nodelay && o->proto == FIO_TYPE_TCP) {
543 optval = 1;
544 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof(int)) < 0) {
545 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
546 return 1;
547 }
548 }
549
550 reset_all_stats(td);
551 td_set_runstate(td, state);
552 return 0;
553err:
554 td_set_runstate(td, state);
555 return 1;
556}
557
558static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
559{
560 struct netio_data *nd = td->io_ops->data;
561 struct udp_close_msg msg;
562 struct sockaddr *to = (struct sockaddr *) &nd->addr;
563 int ret;
564
565 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
566 msg.cmd = htonl(FIO_LINK_CLOSE);
567
568 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to,
569 sizeof(nd->addr));
570 if (ret < 0)
571 td_verror(td, errno, "sendto udp link close");
572}
573
574static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
575{
576 struct netio_options *o = td->eo;
577
578 /*
579 * If this is an UDP connection, notify the receiver that we are
580 * closing down the link
581 */
582 if (o->proto == FIO_TYPE_UDP)
583 fio_netio_udp_close(td, f);
584
585 return generic_close_file(td, f);
586}
587
588static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
589{
590 struct netio_data *nd = td->io_ops->data;
591 struct udp_close_msg msg;
592 struct sockaddr *to = (struct sockaddr *) &nd->addr;
593 socklen_t len = sizeof(nd->addr);
594 int ret;
595
596 ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
597 if (ret < 0) {
598 td_verror(td, errno, "sendto udp link open");
599 return ret;
600 }
601
602 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
603 ntohl(msg.cmd) != FIO_LINK_OPEN) {
604 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
605 ntohl(msg.cmd));
606 return -1;
607 }
608
609 return 0;
610}
611
612static int fio_netio_udp_send_open(struct thread_data *td, struct fio_file *f)
613{
614 struct netio_data *nd = td->io_ops->data;
615 struct udp_close_msg msg;
616 struct sockaddr *to = (struct sockaddr *) &nd->addr;
617 int ret;
618
619 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
620 msg.cmd = htonl(FIO_LINK_OPEN);
621
622 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to,
623 sizeof(nd->addr));
624 if (ret < 0) {
625 td_verror(td, errno, "sendto udp link open");
626 return ret;
627 }
628
629 return 0;
630}
631
632static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
633{
634 int ret;
635 struct netio_options *o = td->eo;
636
637 if (o->listen)
638 ret = fio_netio_accept(td, f);
639 else
640 ret = fio_netio_connect(td, f);
641
642 if (ret) {
643 f->fd = -1;
644 return ret;
645 }
646
647 if (o->proto == FIO_TYPE_UDP) {
648 if (td_write(td))
649 ret = fio_netio_udp_send_open(td, f);
650 else {
651 int state;
652
653 state = td->runstate;
654 td_set_runstate(td, TD_SETTING_UP);
655 ret = fio_netio_udp_recv_open(td, f);
656 td_set_runstate(td, state);
657 }
658 }
659
660 if (ret)
661 fio_netio_close_file(td, f);
662
663 return ret;
664}
665
666static int fio_netio_setup_connect_inet(struct thread_data *td,
667 const char *host, unsigned short port)
668{
669 struct netio_data *nd = td->io_ops->data;
670
671 if (!host) {
672 log_err("fio: connect with no host to connect to.\n");
673 if (td_read(td))
674 log_err("fio: did you forget to set 'listen'?\n");
675
676 td_verror(td, EINVAL, "no hostname= set");
677 return 1;
678 }
679
680 nd->addr.sin_family = AF_INET;
681 nd->addr.sin_port = htons(port);
682
683 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
684 struct hostent *hent;
685
686 hent = gethostbyname(host);
687 if (!hent) {
688 td_verror(td, errno, "gethostbyname");
689 return 1;
690 }
691
692 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
693 }
694
695 return 0;
696}
697
698static int fio_netio_setup_connect_unix(struct thread_data *td,
699 const char *path)
700{
701 struct netio_data *nd = td->io_ops->data;
702 struct sockaddr_un *soun = &nd->addr_un;
703
704 soun->sun_family = AF_UNIX;
705 strcpy(soun->sun_path, path);
706 return 0;
707}
708
709static int fio_netio_setup_connect(struct thread_data *td)
710{
711 struct netio_options *o = td->eo;
712
713 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
714 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
715 else
716 return fio_netio_setup_connect_unix(td, td->o.filename);
717}
718
719static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
720{
721 struct netio_data *nd = td->io_ops->data;
722 struct sockaddr_un *addr = &nd->addr_un;
723 mode_t mode;
724 int len, fd;
725
726 fd = socket(AF_UNIX, SOCK_STREAM, 0);
727 if (fd < 0) {
728 log_err("fio: socket: %s\n", strerror(errno));
729 return -1;
730 }
731
732 mode = umask(000);
733
734 memset(addr, 0, sizeof(*addr));
735 addr->sun_family = AF_UNIX;
736 strcpy(addr->sun_path, path);
737 unlink(path);
738
739 len = sizeof(addr->sun_family) + strlen(path) + 1;
740
741 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
742 log_err("fio: bind: %s\n", strerror(errno));
743 close(fd);
744 return -1;
745 }
746
747 umask(mode);
748 nd->listenfd = fd;
749 return 0;
750}
751
752static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
753{
754 struct netio_data *nd = td->io_ops->data;
755 struct netio_options *o = td->eo;
756 int fd, opt, type;
757
758 if (o->proto == FIO_TYPE_TCP)
759 type = SOCK_STREAM;
760 else
761 type = SOCK_DGRAM;
762
763 fd = socket(AF_INET, type, 0);
764 if (fd < 0) {
765 td_verror(td, errno, "socket");
766 return 1;
767 }
768
769 opt = 1;
770 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*)&opt, sizeof(opt)) < 0) {
771 td_verror(td, errno, "setsockopt");
772 return 1;
773 }
774#ifdef SO_REUSEPORT
775 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
776 td_verror(td, errno, "setsockopt");
777 return 1;
778 }
779#endif
780
781 nd->addr.sin_family = AF_INET;
782 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
783 nd->addr.sin_port = htons(port);
784
785 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
786 td_verror(td, errno, "bind");
787 return 1;
788 }
789
790 nd->listenfd = fd;
791 return 0;
792}
793
794static int fio_netio_setup_listen(struct thread_data *td)
795{
796 struct netio_data *nd = td->io_ops->data;
797 struct netio_options *o = td->eo;
798 int ret;
799
800 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
801 ret = fio_netio_setup_listen_inet(td, o->port);
802 else
803 ret = fio_netio_setup_listen_unix(td, td->o.filename);
804
805 if (ret)
806 return ret;
807 if (o->proto == FIO_TYPE_UDP)
808 return 0;
809
810 if (listen(nd->listenfd, 10) < 0) {
811 td_verror(td, errno, "listen");
812 nd->listenfd = -1;
813 return 1;
814 }
815
816 return 0;
817}
818
819static int fio_netio_init(struct thread_data *td)
820{
821 struct netio_options *o = td->eo;
822 int ret;
823
824#ifdef WIN32
825 WSADATA wsd;
826 WSAStartup(MAKEWORD(2,2), &wsd);
827#endif
828
829 if (td_random(td)) {
830 log_err("fio: network IO can't be random\n");
831 return 1;
832 }
833
834 if (o->proto == FIO_TYPE_UNIX && o->port) {
835 log_err("fio: network IO port not valid with unix socket\n");
836 return 1;
837 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
838 log_err("fio: network IO requires port for tcp or udp\n");
839 return 1;
840 }
841
842 if (o->proto != FIO_TYPE_TCP) {
843 if (o->listen) {
844 log_err("fio: listen only valid for TCP proto IO\n");
845 return 1;
846 }
847 if (td_rw(td)) {
848 log_err("fio: datagram network connections must be"
849 " read OR write\n");
850 return 1;
851 }
852 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
853 log_err("fio: UNIX sockets need host/filename\n");
854 return 1;
855 }
856 o->listen = td_read(td);
857 }
858
859 if (o->proto != FIO_TYPE_UNIX && o->listen && td->o.filename) {
860 log_err("fio: hostname not valid for inbound network IO\n");
861 return 1;
862 }
863
864 if (o->listen)
865 ret = fio_netio_setup_listen(td);
866 else
867 ret = fio_netio_setup_connect(td);
868
869 return ret;
870}
871
872static void fio_netio_cleanup(struct thread_data *td)
873{
874 struct netio_data *nd = td->io_ops->data;
875
876 if (nd) {
877 if (nd->listenfd != -1)
878 close(nd->listenfd);
879 if (nd->pipes[0] != -1)
880 close(nd->pipes[0]);
881 if (nd->pipes[1] != -1)
882 close(nd->pipes[1]);
883
884 free(nd);
885 }
886}
887
888static int fio_netio_setup(struct thread_data *td)
889{
890 struct netio_data *nd;
891
892 if (!td->files_index) {
893 add_file(td, td->o.filename ?: "net");
894 td->o.nr_files = td->o.nr_files ?: 1;
895 }
896
897 if (!td->io_ops->data) {
898 nd = malloc(sizeof(*nd));;
899
900 memset(nd, 0, sizeof(*nd));
901 nd->listenfd = -1;
902 nd->pipes[0] = nd->pipes[1] = -1;
903 td->io_ops->data = nd;
904 }
905
906 return 0;
907}
908
909static void fio_netio_terminate(struct thread_data *td)
910{
911 kill(td->pid, SIGUSR2);
912}
913
914#ifdef CONFIG_LINUX_SPLICE
915static int fio_netio_setup_splice(struct thread_data *td)
916{
917 struct netio_data *nd;
918
919 fio_netio_setup(td);
920
921 nd = td->io_ops->data;
922 if (nd) {
923 if (pipe(nd->pipes) < 0)
924 return 1;
925
926 nd->use_splice = 1;
927 return 0;
928 }
929
930 return 1;
931}
932
933static struct ioengine_ops ioengine_splice = {
934 .name = "netsplice",
935 .version = FIO_IOOPS_VERSION,
936 .prep = fio_netio_prep,
937 .queue = fio_netio_queue,
938 .setup = fio_netio_setup_splice,
939 .init = fio_netio_init,
940 .cleanup = fio_netio_cleanup,
941 .open_file = fio_netio_open_file,
942 .close_file = fio_netio_close_file,
943 .terminate = fio_netio_terminate,
944 .options = options,
945 .option_struct_size = sizeof(struct netio_options),
946 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
947 FIO_PIPEIO,
948};
949#endif
950
951static struct ioengine_ops ioengine_rw = {
952 .name = "net",
953 .version = FIO_IOOPS_VERSION,
954 .prep = fio_netio_prep,
955 .queue = fio_netio_queue,
956 .setup = fio_netio_setup,
957 .init = fio_netio_init,
958 .cleanup = fio_netio_cleanup,
959 .open_file = fio_netio_open_file,
960 .close_file = fio_netio_close_file,
961 .terminate = fio_netio_terminate,
962 .options = options,
963 .option_struct_size = sizeof(struct netio_options),
964 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
965 FIO_PIPEIO,
966};
967
968static int str_hostname_cb(void *data, const char *input)
969{
970 struct netio_options *o = data;
971
972 if (o->td->o.filename)
973 free(o->td->o.filename);
974 o->td->o.filename = strdup(input);
975 return 0;
976}
977
978static void fio_init fio_netio_register(void)
979{
980 register_ioengine(&ioengine_rw);
981#ifdef CONFIG_LINUX_SPLICE
982 register_ioengine(&ioengine_splice);
983#endif
984}
985
986static void fio_exit fio_netio_unregister(void)
987{
988 unregister_ioengine(&ioengine_rw);
989#ifdef CONFIG_LINUX_SPLICE
990 unregister_ioengine(&ioengine_splice);
991#endif
992}