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