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