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