61bdfdd19be81a06fe1ae295b7898546f803de39
[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_in6 addr6;
31         struct sockaddr_un addr_un;
32 };
33
34 struct netio_options {
35         struct thread_data *td;
36         unsigned int port;
37         unsigned int proto;
38         unsigned int listen;
39         unsigned int pingpong;
40         unsigned int nodelay;
41         unsigned int ttl;
42         char *intfc;
43 };
44
45 struct udp_close_msg {
46         uint32_t magic;
47         uint32_t cmd;
48 };
49
50 enum {
51         FIO_LINK_CLOSE = 0x89,
52         FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b,
53         FIO_LINK_OPEN = 0x98,
54
55         FIO_TYPE_TCP    = 1,
56         FIO_TYPE_UDP    = 2,
57         FIO_TYPE_UNIX   = 3,
58         FIO_TYPE_TCP_V6 = 4,
59         FIO_TYPE_UDP_V6 = 5,
60 };
61
62 static int str_hostname_cb(void *data, const char *input);
63 static struct fio_option options[] = {
64         {
65                 .name   = "hostname",
66                 .lname  = "net engine hostname",
67                 .type   = FIO_OPT_STR_STORE,
68                 .cb     = str_hostname_cb,
69                 .help   = "Hostname for net IO engine",
70                 .category = FIO_OPT_C_ENGINE,
71                 .group  = FIO_OPT_G_NETIO,
72         },
73         {
74                 .name   = "port",
75                 .lname  = "net engine port",
76                 .type   = FIO_OPT_INT,
77                 .off1   = offsetof(struct netio_options, port),
78                 .minval = 1,
79                 .maxval = 65535,
80                 .help   = "Port to use for TCP or UDP net connections",
81                 .category = FIO_OPT_C_ENGINE,
82                 .group  = FIO_OPT_G_NETIO,
83         },
84         {
85                 .name   = "protocol",
86                 .lname  = "net engine protocol",
87                 .alias  = "proto",
88                 .type   = FIO_OPT_STR,
89                 .off1   = offsetof(struct netio_options, proto),
90                 .help   = "Network protocol to use",
91                 .def    = "tcp",
92                 .posval = {
93                           { .ival = "tcp",
94                             .oval = FIO_TYPE_TCP,
95                             .help = "Transmission Control Protocol",
96                           },
97 #ifdef CONFIG_IPV6
98                           { .ival = "tcpv6",
99                             .oval = FIO_TYPE_TCP_V6,
100                             .help = "Transmission Control Protocol V6",
101                           },
102 #endif
103                           { .ival = "udp",
104                             .oval = FIO_TYPE_UDP,
105                             .help = "User Datagram Protocol",
106                           },
107 #ifdef CONFIG_IPV6
108                           { .ival = "udpv6",
109                             .oval = FIO_TYPE_UDP_V6,
110                             .help = "User Datagram Protocol V6",
111                           },
112 #endif
113                           { .ival = "unix",
114                             .oval = FIO_TYPE_UNIX,
115                             .help = "UNIX domain socket",
116                           },
117                 },
118                 .category = FIO_OPT_C_ENGINE,
119                 .group  = FIO_OPT_G_NETIO,
120         },
121 #ifdef CONFIG_TCP_NODELAY
122         {
123                 .name   = "nodelay",
124                 .type   = FIO_OPT_BOOL,
125                 .off1   = offsetof(struct netio_options, nodelay),
126                 .help   = "Use TCP_NODELAY on TCP connections",
127                 .category = FIO_OPT_C_ENGINE,
128                 .group  = FIO_OPT_G_NETIO,
129         },
130 #endif
131         {
132                 .name   = "listen",
133                 .lname  = "net engine listen",
134                 .type   = FIO_OPT_STR_SET,
135                 .off1   = offsetof(struct netio_options, listen),
136                 .help   = "Listen for incoming TCP connections",
137                 .category = FIO_OPT_C_ENGINE,
138                 .group  = FIO_OPT_G_NETIO,
139         },
140         {
141                 .name   = "pingpong",
142                 .type   = FIO_OPT_STR_SET,
143                 .off1   = offsetof(struct netio_options, pingpong),
144                 .help   = "Ping-pong IO requests",
145                 .category = FIO_OPT_C_ENGINE,
146                 .group  = FIO_OPT_G_NETIO,
147         },
148         {
149                 .name   = "interface",
150                 .lname  = "net engine interface",
151                 .type   = FIO_OPT_STR_STORE,
152                 .off1   = offsetof(struct netio_options, intfc),
153                 .help   = "Network interface to use",
154                 .category = FIO_OPT_C_ENGINE,
155                 .group  = FIO_OPT_G_NETIO,
156         },
157         {
158                 .name   = "ttl",
159                 .lname  = "net engine multicast ttl",
160                 .type   = FIO_OPT_INT,
161                 .off1   = offsetof(struct netio_options, ttl),
162                 .def    = "1",
163                 .minval = 0,
164                 .help   = "Time-to-live value for outgoing UDP multicast packets",
165                 .category = FIO_OPT_C_ENGINE,
166                 .group  = FIO_OPT_G_NETIO,
167         },
168         {
169                 .name   = NULL,
170         },
171 };
172
173 static inline int is_udp(struct netio_options *o)
174 {
175         return o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_UDP_V6;
176 }
177
178 static inline int is_tcp(struct netio_options *o)
179 {
180         return o->proto == FIO_TYPE_TCP || o->proto == FIO_TYPE_TCP_V6;
181 }
182
183 static inline int is_ipv6(struct netio_options *o)
184 {
185         return o->proto == FIO_TYPE_UDP_V6 || o->proto == FIO_TYPE_TCP_V6;
186 }
187
188 /*
189  * Return -1 for error and 'nr events' for a positive number
190  * of events
191  */
192 static int poll_wait(struct thread_data *td, int fd, short events)
193 {
194         struct pollfd pfd;
195         int ret;
196
197         while (!td->terminate) {
198                 pfd.fd = fd;
199                 pfd.events = events;
200                 ret = poll(&pfd, 1, -1);
201                 if (ret < 0) {
202                         if (errno == EINTR)
203                                 break;
204
205                         td_verror(td, errno, "poll");
206                         return -1;
207                 } else if (!ret)
208                         continue;
209
210                 break;
211         }
212
213         if (pfd.revents & events)
214                 return 1;
215
216         return -1;
217 }
218
219 static int fio_netio_is_multicast(const char *mcaddr)
220 {
221         in_addr_t addr = inet_network(mcaddr);
222         if (addr == -1)
223                 return 0;
224
225         if (inet_network("224.0.0.0") <= addr &&
226             inet_network("239.255.255.255") >= addr)
227                 return 1;
228
229         return 0;
230 }
231
232
233 static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
234 {
235         struct netio_options *o = td->eo;
236
237         /*
238          * Make sure we don't see spurious reads to a receiver, and vice versa
239          */
240         if (is_tcp(o))
241                 return 0;
242
243         if ((o->listen && io_u->ddir == DDIR_WRITE) ||
244             (!o->listen && io_u->ddir == DDIR_READ)) {
245                 td_verror(td, EINVAL, "bad direction");
246                 return 1;
247         }
248
249         return 0;
250 }
251
252 #ifdef CONFIG_LINUX_SPLICE
253 static int splice_io_u(int fdin, int fdout, unsigned int len)
254 {
255         int bytes = 0;
256
257         while (len) {
258                 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
259
260                 if (ret < 0) {
261                         if (!bytes)
262                                 bytes = ret;
263
264                         break;
265                 } else if (!ret)
266                         break;
267
268                 bytes += ret;
269                 len -= ret;
270         }
271
272         return bytes;
273 }
274
275 /*
276  * Receive bytes from a socket and fill them into the internal pipe
277  */
278 static int splice_in(struct thread_data *td, struct io_u *io_u)
279 {
280         struct netio_data *nd = td->io_ops->data;
281
282         return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
283 }
284
285 /*
286  * Transmit 'len' bytes from the internal pipe
287  */
288 static int splice_out(struct thread_data *td, struct io_u *io_u,
289                       unsigned int len)
290 {
291         struct netio_data *nd = td->io_ops->data;
292
293         return splice_io_u(nd->pipes[0], io_u->file->fd, len);
294 }
295
296 static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
297 {
298         struct iovec iov = {
299                 .iov_base = io_u->xfer_buf,
300                 .iov_len = len,
301         };
302         int bytes = 0;
303
304         while (iov.iov_len) {
305                 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
306
307                 if (ret < 0) {
308                         if (!bytes)
309                                 bytes = ret;
310                         break;
311                 } else if (!ret)
312                         break;
313
314                 iov.iov_len -= ret;
315                 iov.iov_base += ret;
316                 bytes += ret;
317         }
318
319         return bytes;
320
321 }
322
323 /*
324  * vmsplice() pipe to io_u buffer
325  */
326 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
327                              unsigned int len)
328 {
329         struct netio_data *nd = td->io_ops->data;
330
331         return vmsplice_io_u(io_u, nd->pipes[0], len);
332 }
333
334 /*
335  * vmsplice() io_u to pipe
336  */
337 static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
338 {
339         struct netio_data *nd = td->io_ops->data;
340
341         return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
342 }
343
344 /*
345  * splice receive - transfer socket data into a pipe using splice, then map
346  * that pipe data into the io_u using vmsplice.
347  */
348 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
349 {
350         int ret;
351
352         ret = splice_in(td, io_u);
353         if (ret > 0)
354                 return vmsplice_io_u_out(td, io_u, ret);
355
356         return ret;
357 }
358
359 /*
360  * splice transmit - map data from the io_u into a pipe by using vmsplice,
361  * then transfer that pipe to a socket using splice.
362  */
363 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
364 {
365         int ret;
366
367         ret = vmsplice_io_u_in(td, io_u);
368         if (ret > 0)
369                 return splice_out(td, io_u, ret);
370
371         return ret;
372 }
373 #else
374 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
375 {
376         errno = EOPNOTSUPP;
377         return -1;
378 }
379
380 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
381 {
382         errno = EOPNOTSUPP;
383         return -1;
384 }
385 #endif
386
387 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
388 {
389         struct netio_data *nd = td->io_ops->data;
390         struct netio_options *o = td->eo;
391         int ret, flags = 0;
392
393         do {
394                 if (is_udp(o)) {
395                         const struct sockaddr *to;
396                         socklen_t len;
397
398                         if (is_ipv6(o)) {
399                                 to = (struct sockaddr *) &nd->addr6;
400                                 len = sizeof(nd->addr6);
401                         } else {
402                                 to = (struct sockaddr *) &nd->addr;
403                                 len = sizeof(nd->addr);
404                         }
405
406                         ret = sendto(io_u->file->fd, io_u->xfer_buf,
407                                         io_u->xfer_buflen, flags, to, len);
408                 } else {
409                         /*
410                          * if we are going to write more, set MSG_MORE
411                          */
412 #ifdef MSG_MORE
413                         if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
414                             td->o.size) && !o->pingpong)
415                                 flags |= MSG_MORE;
416 #endif
417                         ret = send(io_u->file->fd, io_u->xfer_buf,
418                                         io_u->xfer_buflen, flags);
419                 }
420                 if (ret > 0)
421                         break;
422
423                 ret = poll_wait(td, io_u->file->fd, POLLOUT);
424                 if (ret <= 0)
425                         break;
426         } while (1);
427
428         return ret;
429 }
430
431 static int is_udp_close(struct io_u *io_u, int len)
432 {
433         struct udp_close_msg *msg;
434
435         if (len != sizeof(struct udp_close_msg))
436                 return 0;
437
438         msg = io_u->xfer_buf;
439         if (ntohl(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC)
440                 return 0;
441         if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
442                 return 0;
443
444         return 1;
445 }
446
447 static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
448 {
449         struct netio_data *nd = td->io_ops->data;
450         struct netio_options *o = td->eo;
451         int ret, flags = 0;
452
453         do {
454                 if (is_udp(o)) {
455                         struct sockaddr *from;
456                         socklen_t l, *len = &l;
457
458                         if (o->listen) {
459                                 if (!is_ipv6(o)) {
460                                         from = (struct sockaddr *) &nd->addr;
461                                         *len = sizeof(nd->addr);
462                                 } else {
463                                         from = (struct sockaddr *) &nd->addr6;
464                                         *len = sizeof(nd->addr6);
465                                 }
466                         } else {
467                                 from = NULL;
468                                 len = NULL;
469                         }
470
471                         ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
472                                         io_u->xfer_buflen, flags, from, len);
473                         if (is_udp_close(io_u, ret)) {
474                                 td->done = 1;
475                                 return 0;
476                         }
477                 } else {
478                         ret = recv(io_u->file->fd, io_u->xfer_buf,
479                                         io_u->xfer_buflen, flags);
480                 }
481                 if (ret > 0)
482                         break;
483                 else if (!ret && (flags & MSG_WAITALL))
484                         break;
485
486                 ret = poll_wait(td, io_u->file->fd, POLLIN);
487                 if (ret <= 0)
488                         break;
489                 flags |= MSG_WAITALL;
490         } while (1);
491
492         return ret;
493 }
494
495 static int __fio_netio_queue(struct thread_data *td, struct io_u *io_u,
496                              enum fio_ddir ddir)
497 {
498         struct netio_data *nd = td->io_ops->data;
499         struct netio_options *o = td->eo;
500         int ret;
501
502         if (ddir == DDIR_WRITE) {
503                 if (!nd->use_splice || is_udp(o) ||
504                     o->proto == FIO_TYPE_UNIX)
505                         ret = fio_netio_send(td, io_u);
506                 else
507                         ret = fio_netio_splice_out(td, io_u);
508         } else if (ddir == DDIR_READ) {
509                 if (!nd->use_splice || is_udp(o) ||
510                     o->proto == FIO_TYPE_UNIX)
511                         ret = fio_netio_recv(td, io_u);
512                 else
513                         ret = fio_netio_splice_in(td, io_u);
514         } else
515                 ret = 0;        /* must be a SYNC */
516
517         if (ret != (int) io_u->xfer_buflen) {
518                 if (ret > 0) {
519                         io_u->resid = io_u->xfer_buflen - ret;
520                         io_u->error = 0;
521                         return FIO_Q_COMPLETED;
522                 } else if (!ret)
523                         return FIO_Q_BUSY;
524                 else {
525                         int err = errno;
526
527                         if (ddir == DDIR_WRITE && err == EMSGSIZE)
528                                 return FIO_Q_BUSY;
529
530                         io_u->error = err;
531                 }
532         }
533
534         if (io_u->error)
535                 td_verror(td, io_u->error, "xfer");
536
537         return FIO_Q_COMPLETED;
538 }
539
540 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
541 {
542         struct netio_options *o = td->eo;
543         int ret;
544
545         fio_ro_check(td, io_u);
546
547         ret = __fio_netio_queue(td, io_u, io_u->ddir);
548         if (!o->pingpong || ret != FIO_Q_COMPLETED)
549                 return ret;
550
551         /*
552          * For ping-pong mode, receive or send reply as needed
553          */
554         if (td_read(td) && io_u->ddir == DDIR_READ)
555                 ret = __fio_netio_queue(td, io_u, DDIR_WRITE);
556         else if (td_write(td) && io_u->ddir == DDIR_WRITE)
557                 ret = __fio_netio_queue(td, io_u, DDIR_READ);
558
559         return ret;
560 }
561
562 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
563 {
564         struct netio_data *nd = td->io_ops->data;
565         struct netio_options *o = td->eo;
566         int type, domain;
567
568         if (o->proto == FIO_TYPE_TCP) {
569                 domain = AF_INET;
570                 type = SOCK_STREAM;
571         } else if (o->proto == FIO_TYPE_TCP_V6) {
572                 domain = AF_INET6;
573                 type = SOCK_STREAM;
574         } else if (o->proto == FIO_TYPE_UDP) {
575                 domain = AF_INET;
576                 type = SOCK_DGRAM;
577         } else if (o->proto == FIO_TYPE_UDP_V6) {
578                 domain = AF_INET6;
579                 type = SOCK_DGRAM;
580         } else if (o->proto == FIO_TYPE_UNIX) {
581                 domain = AF_UNIX;
582                 type = SOCK_STREAM;
583         } else {
584                 log_err("fio: bad network type %d\n", o->proto);
585                 f->fd = -1;
586                 return 1;
587         }
588
589         f->fd = socket(domain, type, 0);
590         if (f->fd < 0) {
591                 td_verror(td, errno, "socket");
592                 return 1;
593         }
594
595 #ifdef CONFIG_TCP_NODELAY
596         if (o->nodelay && is_tcp(o)) {
597                 int optval = 1;
598
599                 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
600                         log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
601                         return 1;
602                 }
603         }
604 #endif
605
606         if (is_udp(o)) {
607                 if (!fio_netio_is_multicast(td->o.filename))
608                         return 0;
609                 if (is_ipv6(o)) {
610                         log_err("fio: multicast not supported on IPv6\n");
611                         close(f->fd);
612                         return 1;
613                 }
614
615                 if (o->intfc) {
616                         struct in_addr interface_addr;
617
618                         if (inet_aton(o->intfc, &interface_addr) == 0) {
619                                 log_err("fio: interface not valid interface IP\n");
620                                 close(f->fd);
621                                 return 1;
622                         }
623                         if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_IF, (const char*)&interface_addr, sizeof(interface_addr)) < 0) {
624                                 td_verror(td, errno, "setsockopt IP_MULTICAST_IF");
625                                 close(f->fd);
626                                 return 1;
627                         }
628                 }
629                 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_TTL, (const char*)&o->ttl, sizeof(o->ttl)) < 0) {
630                         td_verror(td, errno, "setsockopt IP_MULTICAST_TTL");
631                         close(f->fd);
632                         return 1;
633                 }
634                 return 0;
635         } else if (o->proto == FIO_TYPE_TCP) {
636                 socklen_t len = sizeof(nd->addr);
637
638                 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
639                         td_verror(td, errno, "connect");
640                         close(f->fd);
641                         return 1;
642                 }
643         } else if (o->proto == FIO_TYPE_TCP_V6) {
644                 socklen_t len = sizeof(nd->addr6);
645
646                 if (connect(f->fd, (struct sockaddr *) &nd->addr6, len) < 0) {
647                         td_verror(td, errno, "connect");
648                         close(f->fd);
649                         return 1;
650                 }
651
652         } else {
653                 struct sockaddr_un *addr = &nd->addr_un;
654                 socklen_t len;
655
656                 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
657
658                 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
659                         td_verror(td, errno, "connect");
660                         close(f->fd);
661                         return 1;
662                 }
663         }
664
665         return 0;
666 }
667
668 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
669 {
670         struct netio_data *nd = td->io_ops->data;
671         struct netio_options *o = td->eo;
672         socklen_t socklen;
673         int state;
674
675         if (is_udp(o)) {
676                 f->fd = nd->listenfd;
677                 return 0;
678         }
679
680         state = td->runstate;
681         td_set_runstate(td, TD_SETTING_UP);
682
683         log_info("fio: waiting for connection\n");
684
685         if (poll_wait(td, nd->listenfd, POLLIN) < 0)
686                 goto err;
687
688         if (o->proto == FIO_TYPE_TCP) {
689                 socklen = sizeof(nd->addr);
690                 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
691         } else {
692                 socklen = sizeof(nd->addr6);
693                 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr6, &socklen);
694         }
695
696         if (f->fd < 0) {
697                 td_verror(td, errno, "accept");
698                 goto err;
699         }
700
701 #ifdef CONFIG_TCP_NODELAY
702         if (o->nodelay && is_tcp(o)) {
703                 int optval = 1;
704
705                 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
706                         log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
707                         return 1;
708                 }
709         }
710 #endif
711
712         reset_all_stats(td);
713         td_set_runstate(td, state);
714         return 0;
715 err:
716         td_set_runstate(td, state);
717         return 1;
718 }
719
720 static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
721 {
722         struct netio_data *nd = td->io_ops->data;
723         struct netio_options *o = td->eo;
724         struct udp_close_msg msg;
725         struct sockaddr *to;
726         socklen_t len;
727         int ret;
728
729         if (is_ipv6(o)) {
730                 to = (struct sockaddr *) &nd->addr6;
731                 len = sizeof(nd->addr6);
732         } else {
733                 to = (struct sockaddr *) &nd->addr;
734                 len = sizeof(nd->addr);
735         }
736
737         msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
738         msg.cmd = htonl(FIO_LINK_CLOSE);
739
740         ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
741         if (ret < 0)
742                 td_verror(td, errno, "sendto udp link close");
743 }
744
745 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
746 {
747         struct netio_options *o = td->eo;
748
749         /*
750          * If this is an UDP connection, notify the receiver that we are
751          * closing down the link
752          */
753         if (is_udp(o))
754                 fio_netio_udp_close(td, f);
755
756         return generic_close_file(td, f);
757 }
758
759 static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
760 {
761         struct netio_data *nd = td->io_ops->data;
762         struct netio_options *o = td->eo;
763         struct udp_close_msg msg;
764         struct sockaddr *to;
765         socklen_t len;
766         int ret;
767
768         if (is_ipv6(o)) {
769                 len = sizeof(nd->addr6);
770                 to = (struct sockaddr *) &nd->addr6;
771         } else {
772                 len = sizeof(nd->addr);
773                 to = (struct sockaddr *) &nd->addr;
774         }
775
776         ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
777         if (ret < 0) {
778                 td_verror(td, errno, "recvfrom udp link open");
779                 return ret;
780         }
781
782         if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
783             ntohl(msg.cmd) != FIO_LINK_OPEN) {
784                 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
785                                                                 ntohl(msg.cmd));
786                 return -1;
787         }
788
789         return 0;
790 }
791
792 static int fio_netio_udp_send_open(struct thread_data *td, struct fio_file *f)
793 {
794         struct netio_data *nd = td->io_ops->data;
795         struct netio_options *o = td->eo;
796         struct udp_close_msg msg;
797         struct sockaddr *to;
798         socklen_t len;
799         int ret;
800
801         if (is_ipv6(o)) {
802                 len = sizeof(nd->addr6);
803                 to = (struct sockaddr *) &nd->addr6;
804         } else {
805                 len = sizeof(nd->addr);
806                 to = (struct sockaddr *) &nd->addr;
807         }
808
809         msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
810         msg.cmd = htonl(FIO_LINK_OPEN);
811
812         ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
813         if (ret < 0) {
814                 td_verror(td, errno, "sendto udp link open");
815                 return ret;
816         }
817
818         return 0;
819 }
820
821 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
822 {
823         int ret;
824         struct netio_options *o = td->eo;
825
826         if (o->listen)
827                 ret = fio_netio_accept(td, f);
828         else
829                 ret = fio_netio_connect(td, f);
830
831         if (ret) {
832                 f->fd = -1;
833                 return ret;
834         }
835
836         if (is_udp(o)) {
837                 if (td_write(td))
838                         ret = fio_netio_udp_send_open(td, f);
839                 else {
840                         int state;
841
842                         state = td->runstate;
843                         td_set_runstate(td, TD_SETTING_UP);
844                         ret = fio_netio_udp_recv_open(td, f);
845                         td_set_runstate(td, state);
846                 }
847         }
848
849         if (ret)
850                 fio_netio_close_file(td, f);
851
852         return ret;
853 }
854
855 static int fio_fill_addr(struct thread_data *td, const char *host, int af,
856                          void *dst, struct addrinfo **res)
857 {
858         struct netio_options *o = td->eo;
859         struct addrinfo hints;
860         int ret;
861
862         if (inet_pton(af, host, dst))
863                 return 0;
864
865         memset(&hints, 0, sizeof(hints));
866
867         if (is_tcp(o))
868                 hints.ai_socktype = SOCK_STREAM;
869         else
870                 hints.ai_socktype = SOCK_DGRAM;
871
872         if (is_ipv6(o))
873                 hints.ai_family = AF_INET6;
874         else
875                 hints.ai_family = AF_INET;
876
877         ret = getaddrinfo(host, NULL, &hints, res);
878         if (ret) {
879                 int e = EINVAL;
880                 char str[128];
881
882                 if (ret == EAI_SYSTEM)
883                         e = errno;
884
885                 snprintf(str, sizeof(str), "getaddrinfo: %s", gai_strerror(ret));
886                 td_verror(td, e, str);
887                 return 1;
888         }
889
890         return 0;
891 }
892
893 static int fio_netio_setup_connect_inet(struct thread_data *td,
894                                         const char *host, unsigned short port)
895 {
896         struct netio_data *nd = td->io_ops->data;
897         struct netio_options *o = td->eo;
898         struct addrinfo *res = NULL;
899         void *dst, *src;
900         int af, len;
901
902         if (!host) {
903                 log_err("fio: connect with no host to connect to.\n");
904                 if (td_read(td))
905                         log_err("fio: did you forget to set 'listen'?\n");
906
907                 td_verror(td, EINVAL, "no hostname= set");
908                 return 1;
909         }
910
911         nd->addr.sin_family = AF_INET;
912         nd->addr.sin_port = htons(port);
913         nd->addr6.sin6_family = AF_INET6;
914         nd->addr6.sin6_port = htons(port);
915
916         if (is_ipv6(o)) {
917                 af = AF_INET6;
918                 dst = &nd->addr6.sin6_addr;
919         } else {
920                 af = AF_INET;
921                 dst = &nd->addr.sin_addr;
922         }
923
924         if (fio_fill_addr(td, host, af, dst, &res))
925                 return 1;
926
927         if (!res)
928                 return 0;
929
930         if (is_ipv6(o)) {
931                 len = sizeof(nd->addr6.sin6_addr);
932                 src = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
933         } else {
934                 len = sizeof(nd->addr.sin_addr);
935                 src = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
936         }
937
938         memcpy(dst, src, len);
939         freeaddrinfo(res);
940         return 0;
941 }
942
943 static int fio_netio_setup_connect_unix(struct thread_data *td,
944                                         const char *path)
945 {
946         struct netio_data *nd = td->io_ops->data;
947         struct sockaddr_un *soun = &nd->addr_un;
948
949         soun->sun_family = AF_UNIX;
950         memset(soun->sun_path, 0, sizeof(soun->sun_path));
951         strncpy(soun->sun_path, path, sizeof(soun->sun_path) - 1);
952         return 0;
953 }
954
955 static int fio_netio_setup_connect(struct thread_data *td)
956 {
957         struct netio_options *o = td->eo;
958
959         if (is_udp(o) || is_tcp(o))
960                 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
961         else
962                 return fio_netio_setup_connect_unix(td, td->o.filename);
963 }
964
965 static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
966 {
967         struct netio_data *nd = td->io_ops->data;
968         struct sockaddr_un *addr = &nd->addr_un;
969         mode_t mode;
970         int len, fd;
971
972         fd = socket(AF_UNIX, SOCK_STREAM, 0);
973         if (fd < 0) {
974                 log_err("fio: socket: %s\n", strerror(errno));
975                 return -1;
976         }
977
978         mode = umask(000);
979
980         memset(addr, 0, sizeof(*addr));
981         addr->sun_family = AF_UNIX;
982         strncpy(addr->sun_path, path, sizeof(addr->sun_path) - 1);
983         unlink(path);
984
985         len = sizeof(addr->sun_family) + strlen(path) + 1;
986
987         if (bind(fd, (struct sockaddr *) addr, len) < 0) {
988                 log_err("fio: bind: %s\n", strerror(errno));
989                 close(fd);
990                 return -1;
991         }
992
993         umask(mode);
994         nd->listenfd = fd;
995         return 0;
996 }
997
998 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
999 {
1000         struct netio_data *nd = td->io_ops->data;
1001         struct netio_options *o = td->eo;
1002         struct ip_mreq mr;
1003         struct sockaddr_in sin;
1004         struct sockaddr *saddr;
1005         int fd, opt, type, domain;
1006         socklen_t len;
1007
1008         memset(&sin, 0, sizeof(sin));
1009
1010         if (o->proto == FIO_TYPE_TCP) {
1011                 type = SOCK_STREAM;
1012                 domain = AF_INET;
1013         } else if (o->proto == FIO_TYPE_TCP_V6) {
1014                 type = SOCK_STREAM;
1015                 domain = AF_INET6;
1016         } else if (o->proto == FIO_TYPE_UDP) {
1017                 type = SOCK_DGRAM;
1018                 domain = AF_INET;
1019         } else if (o->proto == FIO_TYPE_UDP_V6) {
1020                 type = SOCK_DGRAM;
1021                 domain = AF_INET6;
1022         } else {
1023                 log_err("fio: unknown proto %d\n", o->proto);
1024                 return 1;
1025         }
1026
1027         fd = socket(domain, type, 0);
1028         if (fd < 0) {
1029                 td_verror(td, errno, "socket");
1030                 return 1;
1031         }
1032
1033         opt = 1;
1034         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
1035                 td_verror(td, errno, "setsockopt");
1036                 close(fd);
1037                 return 1;
1038         }
1039 #ifdef SO_REUSEPORT
1040         if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) {
1041                 td_verror(td, errno, "setsockopt");
1042                 close(fd);
1043                 return 1;
1044         }
1045 #endif
1046
1047         if (td->o.filename) {
1048                 if (!is_udp(o) || !fio_netio_is_multicast(td->o.filename)) {
1049                         log_err("fio: hostname not valid for non-multicast inbound network IO\n");
1050                         close(fd);
1051                         return 1;
1052                 }
1053                 if (is_ipv6(o)) {
1054                         log_err("fio: IPv6 not supported for multicast network IO");
1055                         close(fd);
1056                         return 1;
1057                 }
1058
1059                 inet_aton(td->o.filename, &sin.sin_addr);
1060
1061                 mr.imr_multiaddr = sin.sin_addr;
1062                 if (o->intfc) {
1063                         if (inet_aton(o->intfc, &mr.imr_interface) == 0) {
1064                                 log_err("fio: interface not valid interface IP\n");
1065                                 close(fd);
1066                                 return 1;
1067                         }
1068                 } else {
1069                         mr.imr_interface.s_addr = htonl(INADDR_ANY);
1070                 }
1071
1072                 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mr, sizeof(mr)) < 0) {
1073                         td_verror(td, errno, "setsockopt IP_ADD_MEMBERSHIP");
1074                         close(fd);
1075                         return 1;
1076                 }
1077         }
1078
1079         if (!is_ipv6(o)) {
1080                 saddr = (struct sockaddr *) &nd->addr;
1081                 len = sizeof(nd->addr);
1082
1083                 nd->addr.sin_family = AF_INET;
1084                 nd->addr.sin_addr.s_addr = sin.sin_addr.s_addr ? sin.sin_addr.s_addr : htonl(INADDR_ANY);
1085                 nd->addr.sin_port = htons(port);
1086         } else {
1087                 saddr = (struct sockaddr *) &nd->addr6;
1088                 len = sizeof(nd->addr6);
1089
1090                 nd->addr6.sin6_family = AF_INET6;
1091                 nd->addr6.sin6_addr = in6addr_any;
1092                 nd->addr6.sin6_port = htons(port);
1093         }
1094
1095         if (bind(fd, saddr, len) < 0) {
1096                 close(fd);
1097                 td_verror(td, errno, "bind");
1098                 return 1;
1099         }
1100
1101         nd->listenfd = fd;
1102         return 0;
1103 }
1104
1105 static int fio_netio_setup_listen(struct thread_data *td)
1106 {
1107         struct netio_data *nd = td->io_ops->data;
1108         struct netio_options *o = td->eo;
1109         int ret;
1110
1111         if (is_udp(o) || is_tcp(o))
1112                 ret = fio_netio_setup_listen_inet(td, o->port);
1113         else
1114                 ret = fio_netio_setup_listen_unix(td, td->o.filename);
1115
1116         if (ret)
1117                 return ret;
1118         if (is_udp(o))
1119                 return 0;
1120
1121         if (listen(nd->listenfd, 10) < 0) {
1122                 td_verror(td, errno, "listen");
1123                 nd->listenfd = -1;
1124                 return 1;
1125         }
1126
1127         return 0;
1128 }
1129
1130 static int fio_netio_init(struct thread_data *td)
1131 {
1132         struct netio_options *o = td->eo;
1133         int ret;
1134
1135 #ifdef WIN32
1136         WSADATA wsd;
1137         WSAStartup(MAKEWORD(2,2), &wsd);
1138 #endif
1139
1140         if (td_random(td)) {
1141                 log_err("fio: network IO can't be random\n");
1142                 return 1;
1143         }
1144
1145         if (o->proto == FIO_TYPE_UNIX && o->port) {
1146                 log_err("fio: network IO port not valid with unix socket\n");
1147                 return 1;
1148         } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
1149                 log_err("fio: network IO requires port for tcp or udp\n");
1150                 return 1;
1151         }
1152
1153         if (!is_tcp(o)) {
1154                 if (o->listen) {
1155                         log_err("fio: listen only valid for TCP proto IO\n");
1156                         return 1;
1157                 }
1158                 if (td_rw(td)) {
1159                         log_err("fio: datagram network connections must be"
1160                                    " read OR write\n");
1161                         return 1;
1162                 }
1163                 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
1164                         log_err("fio: UNIX sockets need host/filename\n");
1165                         return 1;
1166                 }
1167                 o->listen = td_read(td);
1168         }
1169
1170         if (o->listen)
1171                 ret = fio_netio_setup_listen(td);
1172         else
1173                 ret = fio_netio_setup_connect(td);
1174
1175         return ret;
1176 }
1177
1178 static void fio_netio_cleanup(struct thread_data *td)
1179 {
1180         struct netio_data *nd = td->io_ops->data;
1181
1182         if (nd) {
1183                 if (nd->listenfd != -1)
1184                         close(nd->listenfd);
1185                 if (nd->pipes[0] != -1)
1186                         close(nd->pipes[0]);
1187                 if (nd->pipes[1] != -1)
1188                         close(nd->pipes[1]);
1189
1190                 free(nd);
1191         }
1192 }
1193
1194 static int fio_netio_setup(struct thread_data *td)
1195 {
1196         struct netio_data *nd;
1197
1198         if (!td->files_index) {
1199                 add_file(td, td->o.filename ?: "net", 0, 0);
1200                 td->o.nr_files = td->o.nr_files ?: 1;
1201                 td->o.open_files++;
1202         }
1203
1204         if (!td->io_ops->data) {
1205                 nd = malloc(sizeof(*nd));;
1206
1207                 memset(nd, 0, sizeof(*nd));
1208                 nd->listenfd = -1;
1209                 nd->pipes[0] = nd->pipes[1] = -1;
1210                 td->io_ops->data = nd;
1211         }
1212
1213         return 0;
1214 }
1215
1216 static void fio_netio_terminate(struct thread_data *td)
1217 {
1218         kill(td->pid, SIGTERM);
1219 }
1220
1221 #ifdef CONFIG_LINUX_SPLICE
1222 static int fio_netio_setup_splice(struct thread_data *td)
1223 {
1224         struct netio_data *nd;
1225
1226         fio_netio_setup(td);
1227
1228         nd = td->io_ops->data;
1229         if (nd) {
1230                 if (pipe(nd->pipes) < 0)
1231                         return 1;
1232
1233                 nd->use_splice = 1;
1234                 return 0;
1235         }
1236
1237         return 1;
1238 }
1239
1240 static struct ioengine_ops ioengine_splice = {
1241         .name                   = "netsplice",
1242         .version                = FIO_IOOPS_VERSION,
1243         .prep                   = fio_netio_prep,
1244         .queue                  = fio_netio_queue,
1245         .setup                  = fio_netio_setup_splice,
1246         .init                   = fio_netio_init,
1247         .cleanup                = fio_netio_cleanup,
1248         .open_file              = fio_netio_open_file,
1249         .close_file             = fio_netio_close_file,
1250         .terminate              = fio_netio_terminate,
1251         .options                = options,
1252         .option_struct_size     = sizeof(struct netio_options),
1253         .flags                  = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1254                                   FIO_PIPEIO,
1255 };
1256 #endif
1257
1258 static struct ioengine_ops ioengine_rw = {
1259         .name                   = "net",
1260         .version                = FIO_IOOPS_VERSION,
1261         .prep                   = fio_netio_prep,
1262         .queue                  = fio_netio_queue,
1263         .setup                  = fio_netio_setup,
1264         .init                   = fio_netio_init,
1265         .cleanup                = fio_netio_cleanup,
1266         .open_file              = fio_netio_open_file,
1267         .close_file             = fio_netio_close_file,
1268         .terminate              = fio_netio_terminate,
1269         .options                = options,
1270         .option_struct_size     = sizeof(struct netio_options),
1271         .flags                  = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1272                                   FIO_PIPEIO | FIO_BIT_BASED,
1273 };
1274
1275 static int str_hostname_cb(void *data, const char *input)
1276 {
1277         struct netio_options *o = data;
1278
1279         if (o->td->o.filename)
1280                 free(o->td->o.filename);
1281         o->td->o.filename = strdup(input);
1282         return 0;
1283 }
1284
1285 static void fio_init fio_netio_register(void)
1286 {
1287         register_ioengine(&ioengine_rw);
1288 #ifdef CONFIG_LINUX_SPLICE
1289         register_ioengine(&ioengine_splice);
1290 #endif
1291 }
1292
1293 static void fio_exit fio_netio_unregister(void)
1294 {
1295         unregister_ioengine(&ioengine_rw);
1296 #ifdef CONFIG_LINUX_SPLICE
1297         unregister_ioengine(&ioengine_splice);
1298 #endif
1299 }