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