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