4 * IO engine that reads/writes to/from sockets.
13 #include <netinet/in.h>
14 #include <netinet/tcp.h>
15 #include <arpa/inet.h>
18 #include <sys/types.h>
20 #include <sys/socket.h>
29 struct sockaddr_in addr;
30 struct sockaddr_in6 addr6;
31 struct sockaddr_un addr_un;
34 struct netio_options {
35 struct thread_data *td;
39 unsigned int pingpong;
42 unsigned int window_size;
46 struct udp_close_msg {
52 FIO_LINK_CLOSE = 0x89,
53 FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b,
63 static int str_hostname_cb(void *data, const char *input);
64 static struct fio_option options[] = {
67 .lname = "net engine hostname",
68 .type = FIO_OPT_STR_STORE,
69 .cb = str_hostname_cb,
70 .help = "Hostname for net IO engine",
71 .category = FIO_OPT_C_ENGINE,
72 .group = FIO_OPT_G_NETIO,
76 .lname = "net engine port",
78 .off1 = offsetof(struct netio_options, port),
81 .help = "Port to use for TCP or UDP net connections",
82 .category = FIO_OPT_C_ENGINE,
83 .group = FIO_OPT_G_NETIO,
87 .lname = "net engine protocol",
90 .off1 = offsetof(struct netio_options, proto),
91 .help = "Network protocol to use",
96 .help = "Transmission Control Protocol",
100 .oval = FIO_TYPE_TCP_V6,
101 .help = "Transmission Control Protocol V6",
105 .oval = FIO_TYPE_UDP,
106 .help = "User Datagram Protocol",
110 .oval = FIO_TYPE_UDP_V6,
111 .help = "User Datagram Protocol V6",
115 .oval = FIO_TYPE_UNIX,
116 .help = "UNIX domain socket",
119 .category = FIO_OPT_C_ENGINE,
120 .group = FIO_OPT_G_NETIO,
122 #ifdef CONFIG_TCP_NODELAY
125 .type = FIO_OPT_BOOL,
126 .off1 = offsetof(struct netio_options, nodelay),
127 .help = "Use TCP_NODELAY on TCP connections",
128 .category = FIO_OPT_C_ENGINE,
129 .group = FIO_OPT_G_NETIO,
134 .lname = "net engine listen",
135 .type = FIO_OPT_STR_SET,
136 .off1 = offsetof(struct netio_options, listen),
137 .help = "Listen for incoming TCP connections",
138 .category = FIO_OPT_C_ENGINE,
139 .group = FIO_OPT_G_NETIO,
143 .type = FIO_OPT_STR_SET,
144 .off1 = offsetof(struct netio_options, pingpong),
145 .help = "Ping-pong IO requests",
146 .category = FIO_OPT_C_ENGINE,
147 .group = FIO_OPT_G_NETIO,
151 .lname = "net engine interface",
152 .type = FIO_OPT_STR_STORE,
153 .off1 = offsetof(struct netio_options, intfc),
154 .help = "Network interface to use",
155 .category = FIO_OPT_C_ENGINE,
156 .group = FIO_OPT_G_NETIO,
160 .lname = "net engine multicast ttl",
162 .off1 = offsetof(struct netio_options, ttl),
165 .help = "Time-to-live value for outgoing UDP multicast packets",
166 .category = FIO_OPT_C_ENGINE,
167 .group = FIO_OPT_G_NETIO,
169 #ifdef CONFIG_NET_WINDOWSIZE
171 .name = "window_size",
172 .lname = "Window Size",
174 .off1 = offsetof(struct netio_options, window_size),
176 .help = "Set socket buffer window size",
177 .category = FIO_OPT_C_ENGINE,
178 .group = FIO_OPT_G_NETIO,
186 static inline int is_udp(struct netio_options *o)
188 return o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_UDP_V6;
191 static inline int is_tcp(struct netio_options *o)
193 return o->proto == FIO_TYPE_TCP || o->proto == FIO_TYPE_TCP_V6;
196 static inline int is_ipv6(struct netio_options *o)
198 return o->proto == FIO_TYPE_UDP_V6 || o->proto == FIO_TYPE_TCP_V6;
201 static int set_window_size(struct thread_data *td, int fd)
203 #ifdef CONFIG_NET_WINDOWSIZE
204 struct netio_options *o = td->eo;
211 rcv = o->listen || o->pingpong;
212 snd = !o->listen || o->pingpong;
213 wss = o->window_size;
217 ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void *) &wss,
220 td_verror(td, errno, "rcvbuf window size");
223 ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void *) &wss,
226 td_verror(td, errno, "sndbuf window size");
231 td_verror(td, -EINVAL, "setsockopt window size");
237 * Return -1 for error and 'nr events' for a positive number
240 static int poll_wait(struct thread_data *td, int fd, short events)
245 while (!td->terminate) {
248 ret = poll(&pfd, 1, -1);
253 td_verror(td, errno, "poll");
261 if (pfd.revents & events)
267 static int fio_netio_is_multicast(const char *mcaddr)
269 in_addr_t addr = inet_network(mcaddr);
273 if (inet_network("224.0.0.0") <= addr &&
274 inet_network("239.255.255.255") >= addr)
281 static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
283 struct netio_options *o = td->eo;
286 * Make sure we don't see spurious reads to a receiver, and vice versa
291 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
292 (!o->listen && io_u->ddir == DDIR_READ)) {
293 td_verror(td, EINVAL, "bad direction");
300 #ifdef CONFIG_LINUX_SPLICE
301 static int splice_io_u(int fdin, int fdout, unsigned int len)
306 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
324 * Receive bytes from a socket and fill them into the internal pipe
326 static int splice_in(struct thread_data *td, struct io_u *io_u)
328 struct netio_data *nd = td->io_ops->data;
330 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
334 * Transmit 'len' bytes from the internal pipe
336 static int splice_out(struct thread_data *td, struct io_u *io_u,
339 struct netio_data *nd = td->io_ops->data;
341 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
344 static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
347 .iov_base = io_u->xfer_buf,
352 while (iov.iov_len) {
353 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
372 * vmsplice() pipe to io_u buffer
374 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
377 struct netio_data *nd = td->io_ops->data;
379 return vmsplice_io_u(io_u, nd->pipes[0], len);
383 * vmsplice() io_u to pipe
385 static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
387 struct netio_data *nd = td->io_ops->data;
389 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
393 * splice receive - transfer socket data into a pipe using splice, then map
394 * that pipe data into the io_u using vmsplice.
396 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
400 ret = splice_in(td, io_u);
402 return vmsplice_io_u_out(td, io_u, ret);
408 * splice transmit - map data from the io_u into a pipe by using vmsplice,
409 * then transfer that pipe to a socket using splice.
411 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
415 ret = vmsplice_io_u_in(td, io_u);
417 return splice_out(td, io_u, ret);
422 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
428 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
435 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
437 struct netio_data *nd = td->io_ops->data;
438 struct netio_options *o = td->eo;
443 const struct sockaddr *to;
447 to = (struct sockaddr *) &nd->addr6;
448 len = sizeof(nd->addr6);
450 to = (struct sockaddr *) &nd->addr;
451 len = sizeof(nd->addr);
454 ret = sendto(io_u->file->fd, io_u->xfer_buf,
455 io_u->xfer_buflen, flags, to, len);
458 * if we are going to write more, set MSG_MORE
461 if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
462 td->o.size) && !o->pingpong)
465 ret = send(io_u->file->fd, io_u->xfer_buf,
466 io_u->xfer_buflen, flags);
471 ret = poll_wait(td, io_u->file->fd, POLLOUT);
479 static int is_udp_close(struct io_u *io_u, int len)
481 struct udp_close_msg *msg;
483 if (len != sizeof(struct udp_close_msg))
486 msg = io_u->xfer_buf;
487 if (ntohl(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC)
489 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
495 static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
497 struct netio_data *nd = td->io_ops->data;
498 struct netio_options *o = td->eo;
503 struct sockaddr *from;
504 socklen_t l, *len = &l;
508 from = (struct sockaddr *) &nd->addr;
509 *len = sizeof(nd->addr);
511 from = (struct sockaddr *) &nd->addr6;
512 *len = sizeof(nd->addr6);
519 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
520 io_u->xfer_buflen, flags, from, len);
521 if (is_udp_close(io_u, ret)) {
526 ret = recv(io_u->file->fd, io_u->xfer_buf,
527 io_u->xfer_buflen, flags);
531 else if (!ret && (flags & MSG_WAITALL))
534 ret = poll_wait(td, io_u->file->fd, POLLIN);
537 flags |= MSG_WAITALL;
543 static int __fio_netio_queue(struct thread_data *td, struct io_u *io_u,
546 struct netio_data *nd = td->io_ops->data;
547 struct netio_options *o = td->eo;
550 if (ddir == DDIR_WRITE) {
551 if (!nd->use_splice || is_udp(o) ||
552 o->proto == FIO_TYPE_UNIX)
553 ret = fio_netio_send(td, io_u);
555 ret = fio_netio_splice_out(td, io_u);
556 } else if (ddir == DDIR_READ) {
557 if (!nd->use_splice || is_udp(o) ||
558 o->proto == FIO_TYPE_UNIX)
559 ret = fio_netio_recv(td, io_u);
561 ret = fio_netio_splice_in(td, io_u);
563 ret = 0; /* must be a SYNC */
565 if (ret != (int) io_u->xfer_buflen) {
567 io_u->resid = io_u->xfer_buflen - ret;
569 return FIO_Q_COMPLETED;
575 if (ddir == DDIR_WRITE && err == EMSGSIZE)
583 td_verror(td, io_u->error, "xfer");
585 return FIO_Q_COMPLETED;
588 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
590 struct netio_options *o = td->eo;
593 fio_ro_check(td, io_u);
595 ret = __fio_netio_queue(td, io_u, io_u->ddir);
596 if (!o->pingpong || ret != FIO_Q_COMPLETED)
600 * For ping-pong mode, receive or send reply as needed
602 if (td_read(td) && io_u->ddir == DDIR_READ)
603 ret = __fio_netio_queue(td, io_u, DDIR_WRITE);
604 else if (td_write(td) && io_u->ddir == DDIR_WRITE)
605 ret = __fio_netio_queue(td, io_u, DDIR_READ);
610 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
612 struct netio_data *nd = td->io_ops->data;
613 struct netio_options *o = td->eo;
616 if (o->proto == FIO_TYPE_TCP) {
619 } else if (o->proto == FIO_TYPE_TCP_V6) {
622 } else if (o->proto == FIO_TYPE_UDP) {
625 } else if (o->proto == FIO_TYPE_UDP_V6) {
628 } else if (o->proto == FIO_TYPE_UNIX) {
632 log_err("fio: bad network type %d\n", o->proto);
637 f->fd = socket(domain, type, 0);
639 td_verror(td, errno, "socket");
643 #ifdef CONFIG_TCP_NODELAY
644 if (o->nodelay && is_tcp(o)) {
647 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
648 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
654 if (set_window_size(td, f->fd)) {
660 if (!fio_netio_is_multicast(td->o.filename))
663 log_err("fio: multicast not supported on IPv6\n");
669 struct in_addr interface_addr;
671 if (inet_aton(o->intfc, &interface_addr) == 0) {
672 log_err("fio: interface not valid interface IP\n");
676 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_IF, (const char*)&interface_addr, sizeof(interface_addr)) < 0) {
677 td_verror(td, errno, "setsockopt IP_MULTICAST_IF");
682 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_TTL, (const char*)&o->ttl, sizeof(o->ttl)) < 0) {
683 td_verror(td, errno, "setsockopt IP_MULTICAST_TTL");
688 } else if (o->proto == FIO_TYPE_TCP) {
689 socklen_t len = sizeof(nd->addr);
691 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
692 td_verror(td, errno, "connect");
696 } else if (o->proto == FIO_TYPE_TCP_V6) {
697 socklen_t len = sizeof(nd->addr6);
699 if (connect(f->fd, (struct sockaddr *) &nd->addr6, len) < 0) {
700 td_verror(td, errno, "connect");
706 struct sockaddr_un *addr = &nd->addr_un;
709 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
711 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
712 td_verror(td, errno, "connect");
721 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
723 struct netio_data *nd = td->io_ops->data;
724 struct netio_options *o = td->eo;
729 f->fd = nd->listenfd;
733 state = td->runstate;
734 td_set_runstate(td, TD_SETTING_UP);
736 log_info("fio: waiting for connection\n");
738 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
741 if (o->proto == FIO_TYPE_TCP) {
742 socklen = sizeof(nd->addr);
743 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
745 socklen = sizeof(nd->addr6);
746 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr6, &socklen);
750 td_verror(td, errno, "accept");
754 #ifdef CONFIG_TCP_NODELAY
755 if (o->nodelay && is_tcp(o)) {
758 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
759 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
766 td_set_runstate(td, state);
769 td_set_runstate(td, state);
773 static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
775 struct netio_data *nd = td->io_ops->data;
776 struct netio_options *o = td->eo;
777 struct udp_close_msg msg;
783 to = (struct sockaddr *) &nd->addr6;
784 len = sizeof(nd->addr6);
786 to = (struct sockaddr *) &nd->addr;
787 len = sizeof(nd->addr);
790 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
791 msg.cmd = htonl(FIO_LINK_CLOSE);
793 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
795 td_verror(td, errno, "sendto udp link close");
798 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
800 struct netio_options *o = td->eo;
803 * If this is an UDP connection, notify the receiver that we are
804 * closing down the link
807 fio_netio_udp_close(td, f);
809 return generic_close_file(td, f);
812 static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
814 struct netio_data *nd = td->io_ops->data;
815 struct netio_options *o = td->eo;
816 struct udp_close_msg msg;
822 len = sizeof(nd->addr6);
823 to = (struct sockaddr *) &nd->addr6;
825 len = sizeof(nd->addr);
826 to = (struct sockaddr *) &nd->addr;
829 ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
831 td_verror(td, errno, "recvfrom udp link open");
835 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
836 ntohl(msg.cmd) != FIO_LINK_OPEN) {
837 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
845 static int fio_netio_udp_send_open(struct thread_data *td, struct fio_file *f)
847 struct netio_data *nd = td->io_ops->data;
848 struct netio_options *o = td->eo;
849 struct udp_close_msg msg;
855 len = sizeof(nd->addr6);
856 to = (struct sockaddr *) &nd->addr6;
858 len = sizeof(nd->addr);
859 to = (struct sockaddr *) &nd->addr;
862 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
863 msg.cmd = htonl(FIO_LINK_OPEN);
865 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
867 td_verror(td, errno, "sendto udp link open");
874 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
877 struct netio_options *o = td->eo;
880 ret = fio_netio_accept(td, f);
882 ret = fio_netio_connect(td, f);
891 ret = fio_netio_udp_send_open(td, f);
895 state = td->runstate;
896 td_set_runstate(td, TD_SETTING_UP);
897 ret = fio_netio_udp_recv_open(td, f);
898 td_set_runstate(td, state);
903 fio_netio_close_file(td, f);
908 static int fio_fill_addr(struct thread_data *td, const char *host, int af,
909 void *dst, struct addrinfo **res)
911 struct netio_options *o = td->eo;
912 struct addrinfo hints;
915 if (inet_pton(af, host, dst))
918 memset(&hints, 0, sizeof(hints));
921 hints.ai_socktype = SOCK_STREAM;
923 hints.ai_socktype = SOCK_DGRAM;
926 hints.ai_family = AF_INET6;
928 hints.ai_family = AF_INET;
930 ret = getaddrinfo(host, NULL, &hints, res);
935 if (ret == EAI_SYSTEM)
938 snprintf(str, sizeof(str), "getaddrinfo: %s", gai_strerror(ret));
939 td_verror(td, e, str);
946 static int fio_netio_setup_connect_inet(struct thread_data *td,
947 const char *host, unsigned short port)
949 struct netio_data *nd = td->io_ops->data;
950 struct netio_options *o = td->eo;
951 struct addrinfo *res = NULL;
956 log_err("fio: connect with no host to connect to.\n");
958 log_err("fio: did you forget to set 'listen'?\n");
960 td_verror(td, EINVAL, "no hostname= set");
964 nd->addr.sin_family = AF_INET;
965 nd->addr.sin_port = htons(port);
966 nd->addr6.sin6_family = AF_INET6;
967 nd->addr6.sin6_port = htons(port);
971 dst = &nd->addr6.sin6_addr;
974 dst = &nd->addr.sin_addr;
977 if (fio_fill_addr(td, host, af, dst, &res))
984 len = sizeof(nd->addr6.sin6_addr);
985 src = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
987 len = sizeof(nd->addr.sin_addr);
988 src = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
991 memcpy(dst, src, len);
996 static int fio_netio_setup_connect_unix(struct thread_data *td,
999 struct netio_data *nd = td->io_ops->data;
1000 struct sockaddr_un *soun = &nd->addr_un;
1002 soun->sun_family = AF_UNIX;
1003 memset(soun->sun_path, 0, sizeof(soun->sun_path));
1004 strncpy(soun->sun_path, path, sizeof(soun->sun_path) - 1);
1008 static int fio_netio_setup_connect(struct thread_data *td)
1010 struct netio_options *o = td->eo;
1012 if (is_udp(o) || is_tcp(o))
1013 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
1015 return fio_netio_setup_connect_unix(td, td->o.filename);
1018 static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
1020 struct netio_data *nd = td->io_ops->data;
1021 struct sockaddr_un *addr = &nd->addr_un;
1025 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1027 log_err("fio: socket: %s\n", strerror(errno));
1033 memset(addr, 0, sizeof(*addr));
1034 addr->sun_family = AF_UNIX;
1035 strncpy(addr->sun_path, path, sizeof(addr->sun_path) - 1);
1038 len = sizeof(addr->sun_family) + strlen(path) + 1;
1040 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
1041 log_err("fio: bind: %s\n", strerror(errno));
1051 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
1053 struct netio_data *nd = td->io_ops->data;
1054 struct netio_options *o = td->eo;
1056 struct sockaddr_in sin;
1057 struct sockaddr *saddr;
1058 int fd, opt, type, domain;
1061 memset(&sin, 0, sizeof(sin));
1063 if (o->proto == FIO_TYPE_TCP) {
1066 } else if (o->proto == FIO_TYPE_TCP_V6) {
1069 } else if (o->proto == FIO_TYPE_UDP) {
1072 } else if (o->proto == FIO_TYPE_UDP_V6) {
1076 log_err("fio: unknown proto %d\n", o->proto);
1080 fd = socket(domain, type, 0);
1082 td_verror(td, errno, "socket");
1087 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
1088 td_verror(td, errno, "setsockopt");
1093 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) {
1094 td_verror(td, errno, "setsockopt");
1100 if (set_window_size(td, fd)) {
1105 if (td->o.filename) {
1106 if (!is_udp(o) || !fio_netio_is_multicast(td->o.filename)) {
1107 log_err("fio: hostname not valid for non-multicast inbound network IO\n");
1112 log_err("fio: IPv6 not supported for multicast network IO");
1117 inet_aton(td->o.filename, &sin.sin_addr);
1119 mr.imr_multiaddr = sin.sin_addr;
1121 if (inet_aton(o->intfc, &mr.imr_interface) == 0) {
1122 log_err("fio: interface not valid interface IP\n");
1127 mr.imr_interface.s_addr = htonl(INADDR_ANY);
1130 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mr, sizeof(mr)) < 0) {
1131 td_verror(td, errno, "setsockopt IP_ADD_MEMBERSHIP");
1138 saddr = (struct sockaddr *) &nd->addr;
1139 len = sizeof(nd->addr);
1141 nd->addr.sin_family = AF_INET;
1142 nd->addr.sin_addr.s_addr = sin.sin_addr.s_addr ? sin.sin_addr.s_addr : htonl(INADDR_ANY);
1143 nd->addr.sin_port = htons(port);
1145 saddr = (struct sockaddr *) &nd->addr6;
1146 len = sizeof(nd->addr6);
1148 nd->addr6.sin6_family = AF_INET6;
1149 nd->addr6.sin6_addr = in6addr_any;
1150 nd->addr6.sin6_port = htons(port);
1153 if (bind(fd, saddr, len) < 0) {
1155 td_verror(td, errno, "bind");
1163 static int fio_netio_setup_listen(struct thread_data *td)
1165 struct netio_data *nd = td->io_ops->data;
1166 struct netio_options *o = td->eo;
1169 if (is_udp(o) || is_tcp(o))
1170 ret = fio_netio_setup_listen_inet(td, o->port);
1172 ret = fio_netio_setup_listen_unix(td, td->o.filename);
1179 if (listen(nd->listenfd, 10) < 0) {
1180 td_verror(td, errno, "listen");
1188 static int fio_netio_init(struct thread_data *td)
1190 struct netio_options *o = td->eo;
1195 WSAStartup(MAKEWORD(2,2), &wsd);
1198 if (td_random(td)) {
1199 log_err("fio: network IO can't be random\n");
1203 if (o->proto == FIO_TYPE_UNIX && o->port) {
1204 log_err("fio: network IO port not valid with unix socket\n");
1206 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
1207 log_err("fio: network IO requires port for tcp or udp\n");
1213 log_err("fio: listen only valid for TCP proto IO\n");
1217 log_err("fio: datagram network connections must be"
1218 " read OR write\n");
1221 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
1222 log_err("fio: UNIX sockets need host/filename\n");
1225 o->listen = td_read(td);
1229 ret = fio_netio_setup_listen(td);
1231 ret = fio_netio_setup_connect(td);
1236 static void fio_netio_cleanup(struct thread_data *td)
1238 struct netio_data *nd = td->io_ops->data;
1241 if (nd->listenfd != -1)
1242 close(nd->listenfd);
1243 if (nd->pipes[0] != -1)
1244 close(nd->pipes[0]);
1245 if (nd->pipes[1] != -1)
1246 close(nd->pipes[1]);
1252 static int fio_netio_setup(struct thread_data *td)
1254 struct netio_data *nd;
1256 if (!td->files_index) {
1257 add_file(td, td->o.filename ?: "net", 0, 0);
1258 td->o.nr_files = td->o.nr_files ?: 1;
1262 if (!td->io_ops->data) {
1263 nd = malloc(sizeof(*nd));;
1265 memset(nd, 0, sizeof(*nd));
1267 nd->pipes[0] = nd->pipes[1] = -1;
1268 td->io_ops->data = nd;
1274 static void fio_netio_terminate(struct thread_data *td)
1276 kill(td->pid, SIGTERM);
1279 #ifdef CONFIG_LINUX_SPLICE
1280 static int fio_netio_setup_splice(struct thread_data *td)
1282 struct netio_data *nd;
1284 fio_netio_setup(td);
1286 nd = td->io_ops->data;
1288 if (pipe(nd->pipes) < 0)
1298 static struct ioengine_ops ioengine_splice = {
1299 .name = "netsplice",
1300 .version = FIO_IOOPS_VERSION,
1301 .prep = fio_netio_prep,
1302 .queue = fio_netio_queue,
1303 .setup = fio_netio_setup_splice,
1304 .init = fio_netio_init,
1305 .cleanup = fio_netio_cleanup,
1306 .open_file = fio_netio_open_file,
1307 .close_file = fio_netio_close_file,
1308 .terminate = fio_netio_terminate,
1310 .option_struct_size = sizeof(struct netio_options),
1311 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1316 static struct ioengine_ops ioengine_rw = {
1318 .version = FIO_IOOPS_VERSION,
1319 .prep = fio_netio_prep,
1320 .queue = fio_netio_queue,
1321 .setup = fio_netio_setup,
1322 .init = fio_netio_init,
1323 .cleanup = fio_netio_cleanup,
1324 .open_file = fio_netio_open_file,
1325 .close_file = fio_netio_close_file,
1326 .terminate = fio_netio_terminate,
1328 .option_struct_size = sizeof(struct netio_options),
1329 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1330 FIO_PIPEIO | FIO_BIT_BASED,
1333 static int str_hostname_cb(void *data, const char *input)
1335 struct netio_options *o = data;
1337 if (o->td->o.filename)
1338 free(o->td->o.filename);
1339 o->td->o.filename = strdup(input);
1343 static void fio_init fio_netio_register(void)
1345 register_ioengine(&ioengine_rw);
1346 #ifdef CONFIG_LINUX_SPLICE
1347 register_ioengine(&ioengine_splice);
1351 static void fio_exit fio_netio_unregister(void)
1353 unregister_ioengine(&ioengine_rw);
1354 #ifdef CONFIG_LINUX_SPLICE
1355 unregister_ioengine(&ioengine_splice);