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