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