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>
24 #include "../verify.h"
30 struct sockaddr_in addr;
31 struct sockaddr_in6 addr6;
32 struct sockaddr_un addr_un;
33 uint64_t udp_send_seq;
34 uint64_t udp_recv_seq;
37 struct netio_options {
38 struct thread_data *td;
42 unsigned int pingpong;
45 unsigned int window_size;
50 struct udp_close_msg {
61 FIO_LINK_CLOSE = 0x89,
62 FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b,
64 FIO_UDP_SEQ_MAGIC = 0x657375716e556563ULL,
73 static int str_hostname_cb(void *data, const char *input);
74 static struct fio_option options[] = {
77 .lname = "net engine hostname",
78 .type = FIO_OPT_STR_STORE,
79 .cb = str_hostname_cb,
80 .help = "Hostname for net IO engine",
81 .category = FIO_OPT_C_ENGINE,
82 .group = FIO_OPT_G_NETIO,
86 .lname = "net engine port",
88 .off1 = offsetof(struct netio_options, port),
91 .help = "Port to use for TCP or UDP net connections",
92 .category = FIO_OPT_C_ENGINE,
93 .group = FIO_OPT_G_NETIO,
97 .lname = "net engine protocol",
100 .off1 = offsetof(struct netio_options, proto),
101 .help = "Network protocol to use",
105 .oval = FIO_TYPE_TCP,
106 .help = "Transmission Control Protocol",
110 .oval = FIO_TYPE_TCP_V6,
111 .help = "Transmission Control Protocol V6",
115 .oval = FIO_TYPE_UDP,
116 .help = "User Datagram Protocol",
120 .oval = FIO_TYPE_UDP_V6,
121 .help = "User Datagram Protocol V6",
125 .oval = FIO_TYPE_UNIX,
126 .help = "UNIX domain socket",
129 .category = FIO_OPT_C_ENGINE,
130 .group = FIO_OPT_G_NETIO,
132 #ifdef CONFIG_TCP_NODELAY
135 .type = FIO_OPT_BOOL,
136 .off1 = offsetof(struct netio_options, nodelay),
137 .help = "Use TCP_NODELAY on TCP connections",
138 .category = FIO_OPT_C_ENGINE,
139 .group = FIO_OPT_G_NETIO,
144 .lname = "net engine listen",
145 .type = FIO_OPT_STR_SET,
146 .off1 = offsetof(struct netio_options, listen),
147 .help = "Listen for incoming TCP connections",
148 .category = FIO_OPT_C_ENGINE,
149 .group = FIO_OPT_G_NETIO,
153 .type = FIO_OPT_STR_SET,
154 .off1 = offsetof(struct netio_options, pingpong),
155 .help = "Ping-pong IO requests",
156 .category = FIO_OPT_C_ENGINE,
157 .group = FIO_OPT_G_NETIO,
161 .lname = "net engine interface",
162 .type = FIO_OPT_STR_STORE,
163 .off1 = offsetof(struct netio_options, intfc),
164 .help = "Network interface to use",
165 .category = FIO_OPT_C_ENGINE,
166 .group = FIO_OPT_G_NETIO,
170 .lname = "net engine multicast ttl",
172 .off1 = offsetof(struct netio_options, ttl),
175 .help = "Time-to-live value for outgoing UDP multicast packets",
176 .category = FIO_OPT_C_ENGINE,
177 .group = FIO_OPT_G_NETIO,
179 #ifdef CONFIG_NET_WINDOWSIZE
181 .name = "window_size",
182 .lname = "Window Size",
184 .off1 = offsetof(struct netio_options, window_size),
186 .help = "Set socket buffer window size",
187 .category = FIO_OPT_C_ENGINE,
188 .group = FIO_OPT_G_NETIO,
191 #ifdef CONFIG_NET_MSS
194 .lname = "Maximum segment size",
196 .off1 = offsetof(struct netio_options, mss),
198 .help = "Set TCP maximum segment size",
199 .category = FIO_OPT_C_ENGINE,
200 .group = FIO_OPT_G_NETIO,
208 static inline int is_udp(struct netio_options *o)
210 return o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_UDP_V6;
213 static inline int is_tcp(struct netio_options *o)
215 return o->proto == FIO_TYPE_TCP || o->proto == FIO_TYPE_TCP_V6;
218 static inline int is_ipv6(struct netio_options *o)
220 return o->proto == FIO_TYPE_UDP_V6 || o->proto == FIO_TYPE_TCP_V6;
223 static int set_window_size(struct thread_data *td, int fd)
225 #ifdef CONFIG_NET_WINDOWSIZE
226 struct netio_options *o = td->eo;
233 rcv = o->listen || o->pingpong;
234 snd = !o->listen || o->pingpong;
235 wss = o->window_size;
239 ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void *) &wss,
242 td_verror(td, errno, "rcvbuf window size");
245 ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void *) &wss,
248 td_verror(td, errno, "sndbuf window size");
253 td_verror(td, -EINVAL, "setsockopt window size");
258 static int set_mss(struct thread_data *td, int fd)
260 #ifdef CONFIG_NET_MSS
261 struct netio_options *o = td->eo;
265 if (!o->mss || !is_tcp(o))
269 ret = setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, (void *) &mss,
272 td_verror(td, errno, "setsockopt TCP_MAXSEG");
276 td_verror(td, -EINVAL, "setsockopt TCP_MAXSEG");
283 * Return -1 for error and 'nr events' for a positive number
286 static int poll_wait(struct thread_data *td, int fd, short events)
291 while (!td->terminate) {
294 ret = poll(&pfd, 1, -1);
299 td_verror(td, errno, "poll");
307 if (pfd.revents & events)
313 static int fio_netio_is_multicast(const char *mcaddr)
315 in_addr_t addr = inet_network(mcaddr);
319 if (inet_network("224.0.0.0") <= addr &&
320 inet_network("239.255.255.255") >= addr)
327 static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
329 struct netio_options *o = td->eo;
332 * Make sure we don't see spurious reads to a receiver, and vice versa
337 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
338 (!o->listen && io_u->ddir == DDIR_READ)) {
339 td_verror(td, EINVAL, "bad direction");
346 #ifdef CONFIG_LINUX_SPLICE
347 static int splice_io_u(int fdin, int fdout, unsigned int len)
352 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
370 * Receive bytes from a socket and fill them into the internal pipe
372 static int splice_in(struct thread_data *td, struct io_u *io_u)
374 struct netio_data *nd = td->io_ops->data;
376 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
380 * Transmit 'len' bytes from the internal pipe
382 static int splice_out(struct thread_data *td, struct io_u *io_u,
385 struct netio_data *nd = td->io_ops->data;
387 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
390 static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
393 .iov_base = io_u->xfer_buf,
398 while (iov.iov_len) {
399 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
418 * vmsplice() pipe to io_u buffer
420 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
423 struct netio_data *nd = td->io_ops->data;
425 return vmsplice_io_u(io_u, nd->pipes[0], len);
429 * vmsplice() io_u to pipe
431 static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
433 struct netio_data *nd = td->io_ops->data;
435 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
439 * splice receive - transfer socket data into a pipe using splice, then map
440 * that pipe data into the io_u using vmsplice.
442 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
446 ret = splice_in(td, io_u);
448 return vmsplice_io_u_out(td, io_u, ret);
454 * splice transmit - map data from the io_u into a pipe by using vmsplice,
455 * then transfer that pipe to a socket using splice.
457 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
461 ret = vmsplice_io_u_in(td, io_u);
463 return splice_out(td, io_u, ret);
468 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
474 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
481 static void store_udp_seq(struct netio_data *nd, struct io_u *io_u)
485 us = io_u->xfer_buf + io_u->xfer_buflen - sizeof(*us);
486 us->magic = cpu_to_le64(FIO_UDP_SEQ_MAGIC);
487 us->seq = cpu_to_le64(nd->udp_send_seq++);
490 static void verify_udp_seq(struct thread_data *td, struct netio_data *nd,
496 us = io_u->xfer_buf + io_u->xfer_buflen - sizeof(*us);
497 if (le64_to_cpu(us->magic) != FIO_UDP_SEQ_MAGIC)
500 seq = le64_to_cpu(us->seq);
502 if (seq != nd->udp_recv_seq)
503 td->ts.drop_io_u[io_u->ddir] += seq - nd->udp_recv_seq;
505 nd->udp_recv_seq = seq + 1;
508 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
510 struct netio_data *nd = td->io_ops->data;
511 struct netio_options *o = td->eo;
516 const struct sockaddr *to;
520 to = (struct sockaddr *) &nd->addr6;
521 len = sizeof(nd->addr6);
523 to = (struct sockaddr *) &nd->addr;
524 len = sizeof(nd->addr);
527 if (td->o.verify == VERIFY_NONE)
528 store_udp_seq(nd, io_u);
530 ret = sendto(io_u->file->fd, io_u->xfer_buf,
531 io_u->xfer_buflen, flags, to, len);
534 * if we are going to write more, set MSG_MORE
537 if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
538 td->o.size) && !o->pingpong)
541 ret = send(io_u->file->fd, io_u->xfer_buf,
542 io_u->xfer_buflen, flags);
547 ret = poll_wait(td, io_u->file->fd, POLLOUT);
555 static int is_udp_close(struct io_u *io_u, int len)
557 struct udp_close_msg *msg;
559 if (len != sizeof(struct udp_close_msg))
562 msg = io_u->xfer_buf;
563 if (ntohl(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC)
565 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
571 static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
573 struct netio_data *nd = td->io_ops->data;
574 struct netio_options *o = td->eo;
579 struct sockaddr *from;
580 socklen_t l, *len = &l;
584 from = (struct sockaddr *) &nd->addr;
585 *len = sizeof(nd->addr);
587 from = (struct sockaddr *) &nd->addr6;
588 *len = sizeof(nd->addr6);
595 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
596 io_u->xfer_buflen, flags, from, len);
598 if (is_udp_close(io_u, ret)) {
603 ret = recv(io_u->file->fd, io_u->xfer_buf,
604 io_u->xfer_buflen, flags);
608 else if (!ret && (flags & MSG_WAITALL))
611 ret = poll_wait(td, io_u->file->fd, POLLIN);
614 flags |= MSG_WAITALL;
617 if (is_udp(o) && td->o.verify == VERIFY_NONE)
618 verify_udp_seq(td, nd, io_u);
623 static int __fio_netio_queue(struct thread_data *td, struct io_u *io_u,
626 struct netio_data *nd = td->io_ops->data;
627 struct netio_options *o = td->eo;
630 if (ddir == DDIR_WRITE) {
631 if (!nd->use_splice || is_udp(o) ||
632 o->proto == FIO_TYPE_UNIX)
633 ret = fio_netio_send(td, io_u);
635 ret = fio_netio_splice_out(td, io_u);
636 } else if (ddir == DDIR_READ) {
637 if (!nd->use_splice || is_udp(o) ||
638 o->proto == FIO_TYPE_UNIX)
639 ret = fio_netio_recv(td, io_u);
641 ret = fio_netio_splice_in(td, io_u);
643 ret = 0; /* must be a SYNC */
645 if (ret != (int) io_u->xfer_buflen) {
647 io_u->resid = io_u->xfer_buflen - ret;
649 return FIO_Q_COMPLETED;
655 if (ddir == DDIR_WRITE && err == EMSGSIZE)
663 td_verror(td, io_u->error, "xfer");
665 return FIO_Q_COMPLETED;
668 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
670 struct netio_options *o = td->eo;
673 fio_ro_check(td, io_u);
675 ret = __fio_netio_queue(td, io_u, io_u->ddir);
676 if (!o->pingpong || ret != FIO_Q_COMPLETED)
680 * For ping-pong mode, receive or send reply as needed
682 if (td_read(td) && io_u->ddir == DDIR_READ)
683 ret = __fio_netio_queue(td, io_u, DDIR_WRITE);
684 else if (td_write(td) && io_u->ddir == DDIR_WRITE)
685 ret = __fio_netio_queue(td, io_u, DDIR_READ);
690 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
692 struct netio_data *nd = td->io_ops->data;
693 struct netio_options *o = td->eo;
696 if (o->proto == FIO_TYPE_TCP) {
699 } else if (o->proto == FIO_TYPE_TCP_V6) {
702 } else if (o->proto == FIO_TYPE_UDP) {
705 } else if (o->proto == FIO_TYPE_UDP_V6) {
708 } else if (o->proto == FIO_TYPE_UNIX) {
712 log_err("fio: bad network type %d\n", o->proto);
717 f->fd = socket(domain, type, 0);
719 td_verror(td, errno, "socket");
723 #ifdef CONFIG_TCP_NODELAY
724 if (o->nodelay && is_tcp(o)) {
727 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
728 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
734 if (set_window_size(td, f->fd)) {
738 if (set_mss(td, f->fd)) {
744 if (!fio_netio_is_multicast(td->o.filename))
747 log_err("fio: multicast not supported on IPv6\n");
753 struct in_addr interface_addr;
755 if (inet_aton(o->intfc, &interface_addr) == 0) {
756 log_err("fio: interface not valid interface IP\n");
760 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_IF, (const char*)&interface_addr, sizeof(interface_addr)) < 0) {
761 td_verror(td, errno, "setsockopt IP_MULTICAST_IF");
766 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_TTL, (const char*)&o->ttl, sizeof(o->ttl)) < 0) {
767 td_verror(td, errno, "setsockopt IP_MULTICAST_TTL");
772 } else if (o->proto == FIO_TYPE_TCP) {
773 socklen_t len = sizeof(nd->addr);
775 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
776 td_verror(td, errno, "connect");
780 } else if (o->proto == FIO_TYPE_TCP_V6) {
781 socklen_t len = sizeof(nd->addr6);
783 if (connect(f->fd, (struct sockaddr *) &nd->addr6, len) < 0) {
784 td_verror(td, errno, "connect");
790 struct sockaddr_un *addr = &nd->addr_un;
793 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
795 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
796 td_verror(td, errno, "connect");
805 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
807 struct netio_data *nd = td->io_ops->data;
808 struct netio_options *o = td->eo;
813 f->fd = nd->listenfd;
817 state = td->runstate;
818 td_set_runstate(td, TD_SETTING_UP);
820 log_info("fio: waiting for connection\n");
822 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
825 if (o->proto == FIO_TYPE_TCP) {
826 socklen = sizeof(nd->addr);
827 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
829 socklen = sizeof(nd->addr6);
830 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr6, &socklen);
834 td_verror(td, errno, "accept");
838 #ifdef CONFIG_TCP_NODELAY
839 if (o->nodelay && is_tcp(o)) {
842 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
843 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
850 td_set_runstate(td, state);
853 td_set_runstate(td, state);
857 static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
859 struct netio_data *nd = td->io_ops->data;
860 struct netio_options *o = td->eo;
861 struct udp_close_msg msg;
867 to = (struct sockaddr *) &nd->addr6;
868 len = sizeof(nd->addr6);
870 to = (struct sockaddr *) &nd->addr;
871 len = sizeof(nd->addr);
874 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
875 msg.cmd = htonl(FIO_LINK_CLOSE);
877 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
879 td_verror(td, errno, "sendto udp link close");
882 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
884 struct netio_options *o = td->eo;
887 * If this is an UDP connection, notify the receiver that we are
888 * closing down the link
891 fio_netio_udp_close(td, f);
893 return generic_close_file(td, f);
896 static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
898 struct netio_data *nd = td->io_ops->data;
899 struct netio_options *o = td->eo;
900 struct udp_close_msg msg;
906 len = sizeof(nd->addr6);
907 to = (struct sockaddr *) &nd->addr6;
909 len = sizeof(nd->addr);
910 to = (struct sockaddr *) &nd->addr;
913 ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
915 td_verror(td, errno, "recvfrom udp link open");
919 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
920 ntohl(msg.cmd) != FIO_LINK_OPEN) {
921 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
926 fio_gettime(&td->start, NULL);
930 static int fio_netio_udp_send_open(struct thread_data *td, struct fio_file *f)
932 struct netio_data *nd = td->io_ops->data;
933 struct netio_options *o = td->eo;
934 struct udp_close_msg msg;
940 len = sizeof(nd->addr6);
941 to = (struct sockaddr *) &nd->addr6;
943 len = sizeof(nd->addr);
944 to = (struct sockaddr *) &nd->addr;
947 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
948 msg.cmd = htonl(FIO_LINK_OPEN);
950 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
952 td_verror(td, errno, "sendto udp link open");
959 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
962 struct netio_options *o = td->eo;
965 ret = fio_netio_accept(td, f);
967 ret = fio_netio_connect(td, f);
976 ret = fio_netio_udp_send_open(td, f);
980 state = td->runstate;
981 td_set_runstate(td, TD_SETTING_UP);
982 ret = fio_netio_udp_recv_open(td, f);
983 td_set_runstate(td, state);
988 fio_netio_close_file(td, f);
993 static int fio_fill_addr(struct thread_data *td, const char *host, int af,
994 void *dst, struct addrinfo **res)
996 struct netio_options *o = td->eo;
997 struct addrinfo hints;
1000 if (inet_pton(af, host, dst))
1003 memset(&hints, 0, sizeof(hints));
1006 hints.ai_socktype = SOCK_STREAM;
1008 hints.ai_socktype = SOCK_DGRAM;
1011 hints.ai_family = AF_INET6;
1013 hints.ai_family = AF_INET;
1015 ret = getaddrinfo(host, NULL, &hints, res);
1020 if (ret == EAI_SYSTEM)
1023 snprintf(str, sizeof(str), "getaddrinfo: %s", gai_strerror(ret));
1024 td_verror(td, e, str);
1031 static int fio_netio_setup_connect_inet(struct thread_data *td,
1032 const char *host, unsigned short port)
1034 struct netio_data *nd = td->io_ops->data;
1035 struct netio_options *o = td->eo;
1036 struct addrinfo *res = NULL;
1041 log_err("fio: connect with no host to connect to.\n");
1043 log_err("fio: did you forget to set 'listen'?\n");
1045 td_verror(td, EINVAL, "no hostname= set");
1049 nd->addr.sin_family = AF_INET;
1050 nd->addr.sin_port = htons(port);
1051 nd->addr6.sin6_family = AF_INET6;
1052 nd->addr6.sin6_port = htons(port);
1056 dst = &nd->addr6.sin6_addr;
1059 dst = &nd->addr.sin_addr;
1062 if (fio_fill_addr(td, host, af, dst, &res))
1069 len = sizeof(nd->addr6.sin6_addr);
1070 src = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
1072 len = sizeof(nd->addr.sin_addr);
1073 src = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
1076 memcpy(dst, src, len);
1081 static int fio_netio_setup_connect_unix(struct thread_data *td,
1084 struct netio_data *nd = td->io_ops->data;
1085 struct sockaddr_un *soun = &nd->addr_un;
1087 soun->sun_family = AF_UNIX;
1088 memset(soun->sun_path, 0, sizeof(soun->sun_path));
1089 strncpy(soun->sun_path, path, sizeof(soun->sun_path) - 1);
1093 static int fio_netio_setup_connect(struct thread_data *td)
1095 struct netio_options *o = td->eo;
1097 if (is_udp(o) || is_tcp(o))
1098 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
1100 return fio_netio_setup_connect_unix(td, td->o.filename);
1103 static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
1105 struct netio_data *nd = td->io_ops->data;
1106 struct sockaddr_un *addr = &nd->addr_un;
1110 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1112 log_err("fio: socket: %s\n", strerror(errno));
1118 memset(addr, 0, sizeof(*addr));
1119 addr->sun_family = AF_UNIX;
1120 strncpy(addr->sun_path, path, sizeof(addr->sun_path) - 1);
1123 len = sizeof(addr->sun_family) + strlen(path) + 1;
1125 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
1126 log_err("fio: bind: %s\n", strerror(errno));
1136 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
1138 struct netio_data *nd = td->io_ops->data;
1139 struct netio_options *o = td->eo;
1141 struct sockaddr_in sin;
1142 struct sockaddr *saddr;
1143 int fd, opt, type, domain;
1146 memset(&sin, 0, sizeof(sin));
1148 if (o->proto == FIO_TYPE_TCP) {
1151 } else if (o->proto == FIO_TYPE_TCP_V6) {
1154 } else if (o->proto == FIO_TYPE_UDP) {
1157 } else if (o->proto == FIO_TYPE_UDP_V6) {
1161 log_err("fio: unknown proto %d\n", o->proto);
1165 fd = socket(domain, type, 0);
1167 td_verror(td, errno, "socket");
1172 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
1173 td_verror(td, errno, "setsockopt");
1178 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) {
1179 td_verror(td, errno, "setsockopt");
1185 if (set_window_size(td, fd)) {
1189 if (set_mss(td, fd)) {
1194 if (td->o.filename) {
1195 if (!is_udp(o) || !fio_netio_is_multicast(td->o.filename)) {
1196 log_err("fio: hostname not valid for non-multicast inbound network IO\n");
1201 log_err("fio: IPv6 not supported for multicast network IO");
1206 inet_aton(td->o.filename, &sin.sin_addr);
1208 mr.imr_multiaddr = sin.sin_addr;
1210 if (inet_aton(o->intfc, &mr.imr_interface) == 0) {
1211 log_err("fio: interface not valid interface IP\n");
1216 mr.imr_interface.s_addr = htonl(INADDR_ANY);
1219 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mr, sizeof(mr)) < 0) {
1220 td_verror(td, errno, "setsockopt IP_ADD_MEMBERSHIP");
1227 saddr = (struct sockaddr *) &nd->addr;
1228 len = sizeof(nd->addr);
1230 nd->addr.sin_family = AF_INET;
1231 nd->addr.sin_addr.s_addr = sin.sin_addr.s_addr ? sin.sin_addr.s_addr : htonl(INADDR_ANY);
1232 nd->addr.sin_port = htons(port);
1234 saddr = (struct sockaddr *) &nd->addr6;
1235 len = sizeof(nd->addr6);
1237 nd->addr6.sin6_family = AF_INET6;
1238 nd->addr6.sin6_addr = in6addr_any;
1239 nd->addr6.sin6_port = htons(port);
1242 if (bind(fd, saddr, len) < 0) {
1244 td_verror(td, errno, "bind");
1252 static int fio_netio_setup_listen(struct thread_data *td)
1254 struct netio_data *nd = td->io_ops->data;
1255 struct netio_options *o = td->eo;
1258 if (is_udp(o) || is_tcp(o))
1259 ret = fio_netio_setup_listen_inet(td, o->port);
1261 ret = fio_netio_setup_listen_unix(td, td->o.filename);
1268 if (listen(nd->listenfd, 10) < 0) {
1269 td_verror(td, errno, "listen");
1277 static int fio_netio_init(struct thread_data *td)
1279 struct netio_options *o = td->eo;
1284 WSAStartup(MAKEWORD(2,2), &wsd);
1287 if (td_random(td)) {
1288 log_err("fio: network IO can't be random\n");
1292 if (o->proto == FIO_TYPE_UNIX && o->port) {
1293 log_err("fio: network IO port not valid with unix socket\n");
1295 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
1296 log_err("fio: network IO requires port for tcp or udp\n");
1302 log_err("fio: listen only valid for TCP proto IO\n");
1306 log_err("fio: datagram network connections must be"
1307 " read OR write\n");
1310 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
1311 log_err("fio: UNIX sockets need host/filename\n");
1314 o->listen = td_read(td);
1318 ret = fio_netio_setup_listen(td);
1320 ret = fio_netio_setup_connect(td);
1325 static void fio_netio_cleanup(struct thread_data *td)
1327 struct netio_data *nd = td->io_ops->data;
1330 if (nd->listenfd != -1)
1331 close(nd->listenfd);
1332 if (nd->pipes[0] != -1)
1333 close(nd->pipes[0]);
1334 if (nd->pipes[1] != -1)
1335 close(nd->pipes[1]);
1341 static int fio_netio_setup(struct thread_data *td)
1343 struct netio_data *nd;
1345 if (!td->files_index) {
1346 add_file(td, td->o.filename ?: "net", 0, 0);
1347 td->o.nr_files = td->o.nr_files ?: 1;
1351 if (!td->io_ops->data) {
1352 nd = malloc(sizeof(*nd));;
1354 memset(nd, 0, sizeof(*nd));
1356 nd->pipes[0] = nd->pipes[1] = -1;
1357 td->io_ops->data = nd;
1363 static void fio_netio_terminate(struct thread_data *td)
1365 kill(td->pid, SIGTERM);
1368 #ifdef CONFIG_LINUX_SPLICE
1369 static int fio_netio_setup_splice(struct thread_data *td)
1371 struct netio_data *nd;
1373 fio_netio_setup(td);
1375 nd = td->io_ops->data;
1377 if (pipe(nd->pipes) < 0)
1387 static struct ioengine_ops ioengine_splice = {
1388 .name = "netsplice",
1389 .version = FIO_IOOPS_VERSION,
1390 .prep = fio_netio_prep,
1391 .queue = fio_netio_queue,
1392 .setup = fio_netio_setup_splice,
1393 .init = fio_netio_init,
1394 .cleanup = fio_netio_cleanup,
1395 .open_file = fio_netio_open_file,
1396 .close_file = fio_netio_close_file,
1397 .terminate = fio_netio_terminate,
1399 .option_struct_size = sizeof(struct netio_options),
1400 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1405 static struct ioengine_ops ioengine_rw = {
1407 .version = FIO_IOOPS_VERSION,
1408 .prep = fio_netio_prep,
1409 .queue = fio_netio_queue,
1410 .setup = fio_netio_setup,
1411 .init = fio_netio_init,
1412 .cleanup = fio_netio_cleanup,
1413 .open_file = fio_netio_open_file,
1414 .close_file = fio_netio_close_file,
1415 .terminate = fio_netio_terminate,
1417 .option_struct_size = sizeof(struct netio_options),
1418 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1419 FIO_PIPEIO | FIO_BIT_BASED,
1422 static int str_hostname_cb(void *data, const char *input)
1424 struct netio_options *o = data;
1426 if (o->td->o.filename)
1427 free(o->td->o.filename);
1428 o->td->o.filename = strdup(input);
1432 static void fio_init fio_netio_register(void)
1434 register_ioengine(&ioengine_rw);
1435 #ifdef CONFIG_LINUX_SPLICE
1436 register_ioengine(&ioengine_splice);
1440 static void fio_exit fio_netio_unregister(void)
1442 unregister_ioengine(&ioengine_rw);
1443 #ifdef CONFIG_LINUX_SPLICE
1444 unregister_ioengine(&ioengine_splice);