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