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