Merge branch 'master' into gfio
[fio.git] / engines / net.c
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 <arpa/inet.h>
15 #include <netdb.h>
16 #include <sys/poll.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/socket.h>
20 #include <sys/un.h>
21
22 #include "../fio.h"
23
24 struct netio_data {
25         int listenfd;
26         int use_splice;
27         int pipes[2];
28         struct sockaddr_in addr;
29         struct sockaddr_un addr_un;
30 };
31
32 struct netio_options {
33         struct thread_data *td;
34         unsigned int port;
35         unsigned int proto;
36         unsigned int listen;
37         unsigned int pingpong;
38 };
39
40 struct udp_close_msg {
41         uint32_t magic;
42         uint32_t cmd;
43 };
44
45 enum {
46         FIO_LINK_CLOSE = 0x89,
47         FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b,
48         FIO_LINK_OPEN = 0x98,
49
50         FIO_TYPE_TCP    = 1,
51         FIO_TYPE_UDP    = 2,
52         FIO_TYPE_UNIX   = 3,
53 };
54
55 static int str_hostname_cb(void *data, const char *input);
56 static struct fio_option options[] = {
57         {
58                 .name   = "hostname",
59                 .lname  = "net engine hostname",
60                 .type   = FIO_OPT_STR_STORE,
61                 .cb     = str_hostname_cb,
62                 .help   = "Hostname for net IO engine",
63                 .category = FIO_OPT_C_IO,
64         },
65         {
66                 .name   = "port",
67                 .lname  = "net engine port",
68                 .type   = FIO_OPT_INT,
69                 .off1   = offsetof(struct netio_options, port),
70                 .minval = 1,
71                 .maxval = 65535,
72                 .help   = "Port to use for TCP or UDP net connections",
73                 .category = FIO_OPT_C_IO,
74         },
75         {
76                 .name   = "protocol",
77                 .lname  = "net engine protocol",
78                 .alias  = "proto",
79                 .type   = FIO_OPT_STR,
80                 .off1   = offsetof(struct netio_options, proto),
81                 .help   = "Network protocol to use",
82                 .def    = "tcp",
83                 .category = FIO_OPT_C_IO,
84                 .posval = {
85                           { .ival = "tcp",
86                             .oval = FIO_TYPE_TCP,
87                             .help = "Transmission Control Protocol",
88                           },
89                           { .ival = "udp",
90                             .oval = FIO_TYPE_UDP,
91                             .help = "User Datagram Protocol",
92                           },
93                           { .ival = "unix",
94                             .oval = FIO_TYPE_UNIX,
95                             .help = "UNIX domain socket",
96                           },
97                 },
98         },
99         {
100                 .name   = "listen",
101                 .lname  = "net engine listen",
102                 .type   = FIO_OPT_STR_SET,
103                 .off1   = offsetof(struct netio_options, listen),
104                 .help   = "Listen for incoming TCP connections",
105                 .category = FIO_OPT_C_IO,
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  */
122 static 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
149 static 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 FIO_HAVE_SPLICE
169 static 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  */
194 static 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  */
204 static 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
212 static 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  */
242 static 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  */
253 static 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  */
264 static 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  */
279 static 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
290 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
291 {
292         errno = EOPNOTSUPP;
293         return -1;
294 }
295
296 static 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
303 static 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
339 static 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
355 static 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                         fio_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
390 static 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
433 static 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
455 static 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;
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->proto == FIO_TYPE_UDP)
483                 return 0;
484         else if (o->proto == FIO_TYPE_TCP) {
485                 fio_socklen_t len = sizeof(nd->addr);
486
487                 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
488                         td_verror(td, errno, "connect");
489                         close(f->fd);
490                         return 1;
491                 }
492         } else {
493                 struct sockaddr_un *addr = &nd->addr_un;
494                 fio_socklen_t len;
495
496                 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
497
498                 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
499                         td_verror(td, errno, "connect");
500                         close(f->fd);
501                         return 1;
502                 }
503         }
504
505         return 0;
506 }
507
508 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
509 {
510         struct netio_data *nd = td->io_ops->data;
511         struct netio_options *o = td->eo;
512         fio_socklen_t socklen = sizeof(nd->addr);
513         int state;
514
515         if (o->proto == FIO_TYPE_UDP) {
516                 f->fd = nd->listenfd;
517                 return 0;
518         }
519
520         state = td->runstate;
521         td_set_runstate(td, TD_SETTING_UP);
522
523         log_info("fio: waiting for connection\n");
524
525         if (poll_wait(td, nd->listenfd, POLLIN) < 0)
526                 goto err;
527
528         f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
529         if (f->fd < 0) {
530                 td_verror(td, errno, "accept");
531                 goto err;
532         }
533
534         reset_all_stats(td);
535         td_set_runstate(td, state);
536         return 0;
537 err:
538         td_set_runstate(td, state);
539         return 1;
540 }
541
542 static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
543 {
544         struct netio_data *nd = td->io_ops->data;
545         struct udp_close_msg msg;
546         struct sockaddr *to = (struct sockaddr *) &nd->addr;
547         int ret;
548
549         msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
550         msg.cmd = htonl(FIO_LINK_CLOSE);
551
552         ret = sendto(f->fd, &msg, sizeof(msg), MSG_WAITALL, to,
553                         sizeof(nd->addr));
554         if (ret < 0)
555                 td_verror(td, errno, "sendto udp link close");
556 }
557
558 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
559 {
560         struct netio_options *o = td->eo;
561
562         /*
563          * If this is an UDP connection, notify the receiver that we are
564          * closing down the link
565          */
566         if (o->proto == FIO_TYPE_UDP)
567                 fio_netio_udp_close(td, f);
568
569         return generic_close_file(td, f);
570 }
571
572 static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
573 {
574         struct netio_data *nd = td->io_ops->data;
575         struct udp_close_msg msg;
576         struct sockaddr *to = (struct sockaddr *) &nd->addr;
577         fio_socklen_t len = sizeof(nd->addr);
578         int ret;
579
580         ret = recvfrom(f->fd, &msg, sizeof(msg), MSG_WAITALL, to, &len);
581         if (ret < 0) {
582                 td_verror(td, errno, "sendto udp link open");
583                 return ret;
584         }
585
586         if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
587             ntohl(msg.cmd) != FIO_LINK_OPEN) {
588                 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
589                                                                 ntohl(msg.cmd));
590                 return -1;
591         }
592
593         return 0;
594 }
595
596 static int fio_netio_udp_send_open(struct thread_data *td, struct fio_file *f)
597 {
598         struct netio_data *nd = td->io_ops->data;
599         struct udp_close_msg msg;
600         struct sockaddr *to = (struct sockaddr *) &nd->addr;
601         int ret;
602
603         msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
604         msg.cmd = htonl(FIO_LINK_OPEN);
605
606         ret = sendto(f->fd, &msg, sizeof(msg), MSG_WAITALL, to,
607                         sizeof(nd->addr));
608         if (ret < 0) {
609                 td_verror(td, errno, "sendto udp link open");
610                 return ret;
611         }
612
613         return 0;
614 }
615
616 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
617 {
618         int ret;
619         struct netio_options *o = td->eo;
620
621         if (o->listen)
622                 ret = fio_netio_accept(td, f);
623         else
624                 ret = fio_netio_connect(td, f);
625
626         if (ret) {
627                 f->fd = -1;
628                 return ret;
629         }
630
631         if (o->proto == FIO_TYPE_UDP) {
632                 if (td_write(td))
633                         ret = fio_netio_udp_send_open(td, f);
634                 else {
635                         int state;
636
637                         state = td->runstate;
638                         td_set_runstate(td, TD_SETTING_UP);
639                         ret = fio_netio_udp_recv_open(td, f);
640                         td_set_runstate(td, state);
641                 }
642         }
643
644         if (ret)
645                 fio_netio_close_file(td, f);
646
647         return ret;
648 }
649
650 static int fio_netio_setup_connect_inet(struct thread_data *td,
651                                         const char *host, unsigned short port)
652 {
653         struct netio_data *nd = td->io_ops->data;
654
655         if (!host) {
656                 log_err("fio: connect with no host to connect to.\n");
657                 if (td_read(td))
658                         log_err("fio: did you forget to set 'listen'?\n");
659
660                 td_verror(td, EINVAL, "no hostname= set");
661                 return 1;
662         }
663
664         nd->addr.sin_family = AF_INET;
665         nd->addr.sin_port = htons(port);
666
667         if (inet_aton(host, &nd->addr.sin_addr) != 1) {
668                 struct hostent *hent;
669
670                 hent = gethostbyname(host);
671                 if (!hent) {
672                         td_verror(td, errno, "gethostbyname");
673                         return 1;
674                 }
675
676                 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
677         }
678
679         return 0;
680 }
681
682 static int fio_netio_setup_connect_unix(struct thread_data *td,
683                                         const char *path)
684 {
685         struct netio_data *nd = td->io_ops->data;
686         struct sockaddr_un *soun = &nd->addr_un;
687
688         soun->sun_family = AF_UNIX;
689         strcpy(soun->sun_path, path);
690         return 0;
691 }
692
693 static int fio_netio_setup_connect(struct thread_data *td)
694 {
695         struct netio_options *o = td->eo;
696
697         if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
698                 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
699         else
700                 return fio_netio_setup_connect_unix(td, td->o.filename);
701 }
702
703 static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
704 {
705         struct netio_data *nd = td->io_ops->data;
706         struct sockaddr_un *addr = &nd->addr_un;
707         mode_t mode;
708         int len, fd;
709
710         fd = socket(AF_UNIX, SOCK_STREAM, 0);
711         if (fd < 0) {
712                 log_err("fio: socket: %s\n", strerror(errno));
713                 return -1;
714         }
715
716         mode = umask(000);
717
718         memset(addr, 0, sizeof(*addr));
719         addr->sun_family = AF_UNIX;
720         strcpy(addr->sun_path, path);
721         unlink(path);
722
723         len = sizeof(addr->sun_family) + strlen(path) + 1;
724
725         if (bind(fd, (struct sockaddr *) addr, len) < 0) {
726                 log_err("fio: bind: %s\n", strerror(errno));
727                 close(fd);
728                 return -1;
729         }
730
731         umask(mode);
732         nd->listenfd = fd;
733         return 0;
734 }
735
736 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
737 {
738         struct netio_data *nd = td->io_ops->data;
739         struct netio_options *o = td->eo;
740         int fd, opt, type;
741
742         if (o->proto == FIO_TYPE_TCP)
743                 type = SOCK_STREAM;
744         else
745                 type = SOCK_DGRAM;
746
747         fd = socket(AF_INET, type, 0);
748         if (fd < 0) {
749                 td_verror(td, errno, "socket");
750                 return 1;
751         }
752
753         opt = 1;
754         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
755                 td_verror(td, errno, "setsockopt");
756                 return 1;
757         }
758 #ifdef SO_REUSEPORT
759         if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
760                 td_verror(td, errno, "setsockopt");
761                 return 1;
762         }
763 #endif
764
765         nd->addr.sin_family = AF_INET;
766         nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
767         nd->addr.sin_port = htons(port);
768
769         if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
770                 td_verror(td, errno, "bind");
771                 return 1;
772         }
773
774         nd->listenfd = fd;
775         return 0;
776 }
777
778 static int fio_netio_setup_listen(struct thread_data *td)
779 {
780         struct netio_data *nd = td->io_ops->data;
781         struct netio_options *o = td->eo;
782         int ret;
783
784         if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
785                 ret = fio_netio_setup_listen_inet(td, o->port);
786         else
787                 ret = fio_netio_setup_listen_unix(td, td->o.filename);
788
789         if (ret)
790                 return ret;
791         if (o->proto == FIO_TYPE_UDP)
792                 return 0;
793
794         if (listen(nd->listenfd, 10) < 0) {
795                 td_verror(td, errno, "listen");
796                 nd->listenfd = -1;
797                 return 1;
798         }
799
800         return 0;
801 }
802
803 static int fio_netio_init(struct thread_data *td)
804 {
805         struct netio_options *o = td->eo;
806         int ret;
807
808 #ifdef WIN32
809         WSADATA wsd;
810         WSAStartup(MAKEWORD(2,2), &wsd);
811 #endif
812
813         if (td_random(td)) {
814                 log_err("fio: network IO can't be random\n");
815                 return 1;
816         }
817
818         if (o->proto == FIO_TYPE_UNIX && o->port) {
819                 log_err("fio: network IO port not valid with unix socket\n");
820                 return 1;
821         } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
822                 log_err("fio: network IO requires port for tcp or udp\n");
823                 return 1;
824         }
825
826         if (o->proto != FIO_TYPE_TCP) {
827                 if (o->listen) {
828                         log_err("fio: listen only valid for TCP proto IO\n");
829                         return 1;
830                 }
831                 if (td_rw(td)) {
832                         log_err("fio: datagram network connections must be"
833                                    " read OR write\n");
834                         return 1;
835                 }
836                 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
837                         log_err("fio: UNIX sockets need host/filename\n");
838                         return 1;
839                 }
840                 o->listen = td_read(td);
841         }
842
843         if (o->proto != FIO_TYPE_UNIX && o->listen && td->o.filename) {
844                 log_err("fio: hostname not valid for inbound network IO\n");
845                 return 1;
846         }
847
848         if (o->listen)
849                 ret = fio_netio_setup_listen(td);
850         else
851                 ret = fio_netio_setup_connect(td);
852
853         return ret;
854 }
855
856 static void fio_netio_cleanup(struct thread_data *td)
857 {
858         struct netio_data *nd = td->io_ops->data;
859
860         if (nd) {
861                 if (nd->listenfd != -1)
862                         close(nd->listenfd);
863                 if (nd->pipes[0] != -1)
864                         close(nd->pipes[0]);
865                 if (nd->pipes[1] != -1)
866                         close(nd->pipes[1]);
867
868                 free(nd);
869         }
870 }
871
872 static int fio_netio_setup(struct thread_data *td)
873 {
874         struct netio_data *nd;
875
876         if (!td->files_index) {
877                 add_file(td, td->o.filename ?: "net");
878                 td->o.nr_files = td->o.nr_files ?: 1;
879         }
880
881         if (!td->io_ops->data) {
882                 nd = malloc(sizeof(*nd));;
883
884                 memset(nd, 0, sizeof(*nd));
885                 nd->listenfd = -1;
886                 nd->pipes[0] = nd->pipes[1] = -1;
887                 td->io_ops->data = nd;
888         }
889
890         return 0;
891 }
892
893 static void fio_netio_terminate(struct thread_data *td)
894 {
895         kill(td->pid, SIGUSR2);
896 }
897
898 #ifdef FIO_HAVE_SPLICE
899 static int fio_netio_setup_splice(struct thread_data *td)
900 {
901         struct netio_data *nd;
902
903         fio_netio_setup(td);
904
905         nd = td->io_ops->data;
906         if (nd) {
907                 if (pipe(nd->pipes) < 0)
908                         return 1;
909
910                 nd->use_splice = 1;
911                 return 0;
912         }
913
914         return 1;
915 }
916
917 static struct ioengine_ops ioengine_splice = {
918         .name                   = "netsplice",
919         .version                = FIO_IOOPS_VERSION,
920         .prep                   = fio_netio_prep,
921         .queue                  = fio_netio_queue,
922         .setup                  = fio_netio_setup_splice,
923         .init                   = fio_netio_init,
924         .cleanup                = fio_netio_cleanup,
925         .open_file              = fio_netio_open_file,
926         .close_file             = fio_netio_close_file,
927         .terminate              = fio_netio_terminate,
928         .options                = options,
929         .option_struct_size     = sizeof(struct netio_options),
930         .flags                  = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
931                                   FIO_PIPEIO,
932 };
933 #endif
934
935 static struct ioengine_ops ioengine_rw = {
936         .name                   = "net",
937         .version                = FIO_IOOPS_VERSION,
938         .prep                   = fio_netio_prep,
939         .queue                  = fio_netio_queue,
940         .setup                  = fio_netio_setup,
941         .init                   = fio_netio_init,
942         .cleanup                = fio_netio_cleanup,
943         .open_file              = fio_netio_open_file,
944         .close_file             = fio_netio_close_file,
945         .terminate              = fio_netio_terminate,
946         .options                = options,
947         .option_struct_size     = sizeof(struct netio_options),
948         .flags                  = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
949                                   FIO_PIPEIO,
950 };
951
952 static int str_hostname_cb(void *data, const char *input)
953 {
954         struct netio_options *o = data;
955
956         if (o->td->o.filename)
957                 free(o->td->o.filename);
958         o->td->o.filename = strdup(input);
959         return 0;
960 }
961
962 static void fio_init fio_netio_register(void)
963 {
964         register_ioengine(&ioengine_rw);
965 #ifdef FIO_HAVE_SPLICE
966         register_ioengine(&ioengine_splice);
967 #endif
968 }
969
970 static void fio_exit fio_netio_unregister(void)
971 {
972         unregister_ioengine(&ioengine_rw);
973 #ifdef FIO_HAVE_SPLICE
974         unregister_ioengine(&ioengine_splice);
975 #endif
976 }