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