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