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