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;
47 struct udp_close_msg {
53 FIO_LINK_CLOSE = 0x89,
54 FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b,
64 static int str_hostname_cb(void *data, const char *input);
65 static struct fio_option options[] = {
68 .lname = "net engine hostname",
69 .type = FIO_OPT_STR_STORE,
70 .cb = str_hostname_cb,
71 .help = "Hostname for net IO engine",
72 .category = FIO_OPT_C_ENGINE,
73 .group = FIO_OPT_G_NETIO,
77 .lname = "net engine port",
79 .off1 = offsetof(struct netio_options, port),
82 .help = "Port to use for TCP or UDP net connections",
83 .category = FIO_OPT_C_ENGINE,
84 .group = FIO_OPT_G_NETIO,
88 .lname = "net engine protocol",
91 .off1 = offsetof(struct netio_options, proto),
92 .help = "Network protocol to use",
97 .help = "Transmission Control Protocol",
101 .oval = FIO_TYPE_TCP_V6,
102 .help = "Transmission Control Protocol V6",
106 .oval = FIO_TYPE_UDP,
107 .help = "User Datagram Protocol",
111 .oval = FIO_TYPE_UDP_V6,
112 .help = "User Datagram Protocol V6",
116 .oval = FIO_TYPE_UNIX,
117 .help = "UNIX domain socket",
120 .category = FIO_OPT_C_ENGINE,
121 .group = FIO_OPT_G_NETIO,
123 #ifdef CONFIG_TCP_NODELAY
126 .type = FIO_OPT_BOOL,
127 .off1 = offsetof(struct netio_options, nodelay),
128 .help = "Use TCP_NODELAY on TCP connections",
129 .category = FIO_OPT_C_ENGINE,
130 .group = FIO_OPT_G_NETIO,
135 .lname = "net engine listen",
136 .type = FIO_OPT_STR_SET,
137 .off1 = offsetof(struct netio_options, listen),
138 .help = "Listen for incoming TCP connections",
139 .category = FIO_OPT_C_ENGINE,
140 .group = FIO_OPT_G_NETIO,
144 .type = FIO_OPT_STR_SET,
145 .off1 = offsetof(struct netio_options, pingpong),
146 .help = "Ping-pong IO requests",
147 .category = FIO_OPT_C_ENGINE,
148 .group = FIO_OPT_G_NETIO,
152 .lname = "net engine interface",
153 .type = FIO_OPT_STR_STORE,
154 .off1 = offsetof(struct netio_options, intfc),
155 .help = "Network interface to use",
156 .category = FIO_OPT_C_ENGINE,
157 .group = FIO_OPT_G_NETIO,
161 .lname = "net engine multicast ttl",
163 .off1 = offsetof(struct netio_options, ttl),
166 .help = "Time-to-live value for outgoing UDP multicast packets",
167 .category = FIO_OPT_C_ENGINE,
168 .group = FIO_OPT_G_NETIO,
170 #ifdef CONFIG_NET_WINDOWSIZE
172 .name = "window_size",
173 .lname = "Window Size",
175 .off1 = offsetof(struct netio_options, window_size),
177 .help = "Set socket buffer window size",
178 .category = FIO_OPT_C_ENGINE,
179 .group = FIO_OPT_G_NETIO,
182 #ifdef CONFIG_NET_MSS
185 .lname = "Maximum segment size",
187 .off1 = offsetof(struct netio_options, mss),
189 .help = "Set TCP maximum segment size",
190 .category = FIO_OPT_C_ENGINE,
191 .group = FIO_OPT_G_NETIO,
199 static inline int is_udp(struct netio_options *o)
201 return o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_UDP_V6;
204 static inline int is_tcp(struct netio_options *o)
206 return o->proto == FIO_TYPE_TCP || o->proto == FIO_TYPE_TCP_V6;
209 static inline int is_ipv6(struct netio_options *o)
211 return o->proto == FIO_TYPE_UDP_V6 || o->proto == FIO_TYPE_TCP_V6;
214 static int set_window_size(struct thread_data *td, int fd)
216 #ifdef CONFIG_NET_WINDOWSIZE
217 struct netio_options *o = td->eo;
224 rcv = o->listen || o->pingpong;
225 snd = !o->listen || o->pingpong;
226 wss = o->window_size;
230 ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void *) &wss,
233 td_verror(td, errno, "rcvbuf window size");
236 ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void *) &wss,
239 td_verror(td, errno, "sndbuf window size");
244 td_verror(td, -EINVAL, "setsockopt window size");
249 static int set_mss(struct thread_data *td, int fd)
251 #ifdef CONFIG_NET_MSS
252 struct netio_options *o = td->eo;
256 if (!o->mss || !is_tcp(o))
260 ret = setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, (void *) &mss,
263 td_verror(td, errno, "setsockopt TCP_MAXSEG");
267 td_verror(td, -EINVAL, "setsockopt TCP_MAXSEG");
274 * Return -1 for error and 'nr events' for a positive number
277 static int poll_wait(struct thread_data *td, int fd, short events)
282 while (!td->terminate) {
285 ret = poll(&pfd, 1, -1);
290 td_verror(td, errno, "poll");
298 if (pfd.revents & events)
304 static int fio_netio_is_multicast(const char *mcaddr)
306 in_addr_t addr = inet_network(mcaddr);
310 if (inet_network("224.0.0.0") <= addr &&
311 inet_network("239.255.255.255") >= addr)
318 static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
320 struct netio_options *o = td->eo;
323 * Make sure we don't see spurious reads to a receiver, and vice versa
328 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
329 (!o->listen && io_u->ddir == DDIR_READ)) {
330 td_verror(td, EINVAL, "bad direction");
337 #ifdef CONFIG_LINUX_SPLICE
338 static int splice_io_u(int fdin, int fdout, unsigned int len)
343 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
361 * Receive bytes from a socket and fill them into the internal pipe
363 static int splice_in(struct thread_data *td, struct io_u *io_u)
365 struct netio_data *nd = td->io_ops->data;
367 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
371 * Transmit 'len' bytes from the internal pipe
373 static int splice_out(struct thread_data *td, struct io_u *io_u,
376 struct netio_data *nd = td->io_ops->data;
378 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
381 static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
384 .iov_base = io_u->xfer_buf,
389 while (iov.iov_len) {
390 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
409 * vmsplice() pipe to io_u buffer
411 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
414 struct netio_data *nd = td->io_ops->data;
416 return vmsplice_io_u(io_u, nd->pipes[0], len);
420 * vmsplice() io_u to pipe
422 static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
424 struct netio_data *nd = td->io_ops->data;
426 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
430 * splice receive - transfer socket data into a pipe using splice, then map
431 * that pipe data into the io_u using vmsplice.
433 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
437 ret = splice_in(td, io_u);
439 return vmsplice_io_u_out(td, io_u, ret);
445 * splice transmit - map data from the io_u into a pipe by using vmsplice,
446 * then transfer that pipe to a socket using splice.
448 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
452 ret = vmsplice_io_u_in(td, io_u);
454 return splice_out(td, io_u, ret);
459 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
465 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
472 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
474 struct netio_data *nd = td->io_ops->data;
475 struct netio_options *o = td->eo;
480 const struct sockaddr *to;
484 to = (struct sockaddr *) &nd->addr6;
485 len = sizeof(nd->addr6);
487 to = (struct sockaddr *) &nd->addr;
488 len = sizeof(nd->addr);
491 ret = sendto(io_u->file->fd, io_u->xfer_buf,
492 io_u->xfer_buflen, flags, to, len);
495 * if we are going to write more, set MSG_MORE
498 if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
499 td->o.size) && !o->pingpong)
502 ret = send(io_u->file->fd, io_u->xfer_buf,
503 io_u->xfer_buflen, flags);
508 ret = poll_wait(td, io_u->file->fd, POLLOUT);
516 static int is_udp_close(struct io_u *io_u, int len)
518 struct udp_close_msg *msg;
520 if (len != sizeof(struct udp_close_msg))
523 msg = io_u->xfer_buf;
524 if (ntohl(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC)
526 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
532 static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
534 struct netio_data *nd = td->io_ops->data;
535 struct netio_options *o = td->eo;
540 struct sockaddr *from;
541 socklen_t l, *len = &l;
545 from = (struct sockaddr *) &nd->addr;
546 *len = sizeof(nd->addr);
548 from = (struct sockaddr *) &nd->addr6;
549 *len = sizeof(nd->addr6);
556 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
557 io_u->xfer_buflen, flags, from, len);
558 if (is_udp_close(io_u, ret)) {
563 ret = recv(io_u->file->fd, io_u->xfer_buf,
564 io_u->xfer_buflen, flags);
568 else if (!ret && (flags & MSG_WAITALL))
571 ret = poll_wait(td, io_u->file->fd, POLLIN);
574 flags |= MSG_WAITALL;
580 static int __fio_netio_queue(struct thread_data *td, struct io_u *io_u,
583 struct netio_data *nd = td->io_ops->data;
584 struct netio_options *o = td->eo;
587 if (ddir == DDIR_WRITE) {
588 if (!nd->use_splice || is_udp(o) ||
589 o->proto == FIO_TYPE_UNIX)
590 ret = fio_netio_send(td, io_u);
592 ret = fio_netio_splice_out(td, io_u);
593 } else if (ddir == DDIR_READ) {
594 if (!nd->use_splice || is_udp(o) ||
595 o->proto == FIO_TYPE_UNIX)
596 ret = fio_netio_recv(td, io_u);
598 ret = fio_netio_splice_in(td, io_u);
600 ret = 0; /* must be a SYNC */
602 if (ret != (int) io_u->xfer_buflen) {
604 io_u->resid = io_u->xfer_buflen - ret;
606 return FIO_Q_COMPLETED;
612 if (ddir == DDIR_WRITE && err == EMSGSIZE)
620 td_verror(td, io_u->error, "xfer");
622 return FIO_Q_COMPLETED;
625 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
627 struct netio_options *o = td->eo;
630 fio_ro_check(td, io_u);
632 ret = __fio_netio_queue(td, io_u, io_u->ddir);
633 if (!o->pingpong || ret != FIO_Q_COMPLETED)
637 * For ping-pong mode, receive or send reply as needed
639 if (td_read(td) && io_u->ddir == DDIR_READ)
640 ret = __fio_netio_queue(td, io_u, DDIR_WRITE);
641 else if (td_write(td) && io_u->ddir == DDIR_WRITE)
642 ret = __fio_netio_queue(td, io_u, DDIR_READ);
647 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
649 struct netio_data *nd = td->io_ops->data;
650 struct netio_options *o = td->eo;
653 if (o->proto == FIO_TYPE_TCP) {
656 } else if (o->proto == FIO_TYPE_TCP_V6) {
659 } else if (o->proto == FIO_TYPE_UDP) {
662 } else if (o->proto == FIO_TYPE_UDP_V6) {
665 } else if (o->proto == FIO_TYPE_UNIX) {
669 log_err("fio: bad network type %d\n", o->proto);
674 f->fd = socket(domain, type, 0);
676 td_verror(td, errno, "socket");
680 #ifdef CONFIG_TCP_NODELAY
681 if (o->nodelay && is_tcp(o)) {
684 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
685 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
691 if (set_window_size(td, f->fd)) {
695 if (set_mss(td, f->fd)) {
701 if (!fio_netio_is_multicast(td->o.filename))
704 log_err("fio: multicast not supported on IPv6\n");
710 struct in_addr interface_addr;
712 if (inet_aton(o->intfc, &interface_addr) == 0) {
713 log_err("fio: interface not valid interface IP\n");
717 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_IF, (const char*)&interface_addr, sizeof(interface_addr)) < 0) {
718 td_verror(td, errno, "setsockopt IP_MULTICAST_IF");
723 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_TTL, (const char*)&o->ttl, sizeof(o->ttl)) < 0) {
724 td_verror(td, errno, "setsockopt IP_MULTICAST_TTL");
729 } else if (o->proto == FIO_TYPE_TCP) {
730 socklen_t len = sizeof(nd->addr);
732 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
733 td_verror(td, errno, "connect");
737 } else if (o->proto == FIO_TYPE_TCP_V6) {
738 socklen_t len = sizeof(nd->addr6);
740 if (connect(f->fd, (struct sockaddr *) &nd->addr6, len) < 0) {
741 td_verror(td, errno, "connect");
747 struct sockaddr_un *addr = &nd->addr_un;
750 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
752 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
753 td_verror(td, errno, "connect");
762 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
764 struct netio_data *nd = td->io_ops->data;
765 struct netio_options *o = td->eo;
770 f->fd = nd->listenfd;
774 state = td->runstate;
775 td_set_runstate(td, TD_SETTING_UP);
777 log_info("fio: waiting for connection\n");
779 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
782 if (o->proto == FIO_TYPE_TCP) {
783 socklen = sizeof(nd->addr);
784 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
786 socklen = sizeof(nd->addr6);
787 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr6, &socklen);
791 td_verror(td, errno, "accept");
795 #ifdef CONFIG_TCP_NODELAY
796 if (o->nodelay && is_tcp(o)) {
799 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
800 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
807 td_set_runstate(td, state);
810 td_set_runstate(td, state);
814 static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
816 struct netio_data *nd = td->io_ops->data;
817 struct netio_options *o = td->eo;
818 struct udp_close_msg msg;
824 to = (struct sockaddr *) &nd->addr6;
825 len = sizeof(nd->addr6);
827 to = (struct sockaddr *) &nd->addr;
828 len = sizeof(nd->addr);
831 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
832 msg.cmd = htonl(FIO_LINK_CLOSE);
834 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
836 td_verror(td, errno, "sendto udp link close");
839 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
841 struct netio_options *o = td->eo;
844 * If this is an UDP connection, notify the receiver that we are
845 * closing down the link
848 fio_netio_udp_close(td, f);
850 return generic_close_file(td, f);
853 static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
855 struct netio_data *nd = td->io_ops->data;
856 struct netio_options *o = td->eo;
857 struct udp_close_msg msg;
863 len = sizeof(nd->addr6);
864 to = (struct sockaddr *) &nd->addr6;
866 len = sizeof(nd->addr);
867 to = (struct sockaddr *) &nd->addr;
870 ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
872 td_verror(td, errno, "recvfrom udp link open");
876 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
877 ntohl(msg.cmd) != FIO_LINK_OPEN) {
878 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
886 static int fio_netio_udp_send_open(struct thread_data *td, struct fio_file *f)
888 struct netio_data *nd = td->io_ops->data;
889 struct netio_options *o = td->eo;
890 struct udp_close_msg msg;
896 len = sizeof(nd->addr6);
897 to = (struct sockaddr *) &nd->addr6;
899 len = sizeof(nd->addr);
900 to = (struct sockaddr *) &nd->addr;
903 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
904 msg.cmd = htonl(FIO_LINK_OPEN);
906 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
908 td_verror(td, errno, "sendto udp link open");
915 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
918 struct netio_options *o = td->eo;
921 ret = fio_netio_accept(td, f);
923 ret = fio_netio_connect(td, f);
932 ret = fio_netio_udp_send_open(td, f);
936 state = td->runstate;
937 td_set_runstate(td, TD_SETTING_UP);
938 ret = fio_netio_udp_recv_open(td, f);
939 td_set_runstate(td, state);
944 fio_netio_close_file(td, f);
949 static int fio_fill_addr(struct thread_data *td, const char *host, int af,
950 void *dst, struct addrinfo **res)
952 struct netio_options *o = td->eo;
953 struct addrinfo hints;
956 if (inet_pton(af, host, dst))
959 memset(&hints, 0, sizeof(hints));
962 hints.ai_socktype = SOCK_STREAM;
964 hints.ai_socktype = SOCK_DGRAM;
967 hints.ai_family = AF_INET6;
969 hints.ai_family = AF_INET;
971 ret = getaddrinfo(host, NULL, &hints, res);
976 if (ret == EAI_SYSTEM)
979 snprintf(str, sizeof(str), "getaddrinfo: %s", gai_strerror(ret));
980 td_verror(td, e, str);
987 static int fio_netio_setup_connect_inet(struct thread_data *td,
988 const char *host, unsigned short port)
990 struct netio_data *nd = td->io_ops->data;
991 struct netio_options *o = td->eo;
992 struct addrinfo *res = NULL;
997 log_err("fio: connect with no host to connect to.\n");
999 log_err("fio: did you forget to set 'listen'?\n");
1001 td_verror(td, EINVAL, "no hostname= set");
1005 nd->addr.sin_family = AF_INET;
1006 nd->addr.sin_port = htons(port);
1007 nd->addr6.sin6_family = AF_INET6;
1008 nd->addr6.sin6_port = htons(port);
1012 dst = &nd->addr6.sin6_addr;
1015 dst = &nd->addr.sin_addr;
1018 if (fio_fill_addr(td, host, af, dst, &res))
1025 len = sizeof(nd->addr6.sin6_addr);
1026 src = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
1028 len = sizeof(nd->addr.sin_addr);
1029 src = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
1032 memcpy(dst, src, len);
1037 static int fio_netio_setup_connect_unix(struct thread_data *td,
1040 struct netio_data *nd = td->io_ops->data;
1041 struct sockaddr_un *soun = &nd->addr_un;
1043 soun->sun_family = AF_UNIX;
1044 memset(soun->sun_path, 0, sizeof(soun->sun_path));
1045 strncpy(soun->sun_path, path, sizeof(soun->sun_path) - 1);
1049 static int fio_netio_setup_connect(struct thread_data *td)
1051 struct netio_options *o = td->eo;
1053 if (is_udp(o) || is_tcp(o))
1054 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
1056 return fio_netio_setup_connect_unix(td, td->o.filename);
1059 static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
1061 struct netio_data *nd = td->io_ops->data;
1062 struct sockaddr_un *addr = &nd->addr_un;
1066 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1068 log_err("fio: socket: %s\n", strerror(errno));
1074 memset(addr, 0, sizeof(*addr));
1075 addr->sun_family = AF_UNIX;
1076 strncpy(addr->sun_path, path, sizeof(addr->sun_path) - 1);
1079 len = sizeof(addr->sun_family) + strlen(path) + 1;
1081 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
1082 log_err("fio: bind: %s\n", strerror(errno));
1092 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
1094 struct netio_data *nd = td->io_ops->data;
1095 struct netio_options *o = td->eo;
1097 struct sockaddr_in sin;
1098 struct sockaddr *saddr;
1099 int fd, opt, type, domain;
1102 memset(&sin, 0, sizeof(sin));
1104 if (o->proto == FIO_TYPE_TCP) {
1107 } else if (o->proto == FIO_TYPE_TCP_V6) {
1110 } else if (o->proto == FIO_TYPE_UDP) {
1113 } else if (o->proto == FIO_TYPE_UDP_V6) {
1117 log_err("fio: unknown proto %d\n", o->proto);
1121 fd = socket(domain, type, 0);
1123 td_verror(td, errno, "socket");
1128 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
1129 td_verror(td, errno, "setsockopt");
1134 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) {
1135 td_verror(td, errno, "setsockopt");
1141 if (set_window_size(td, fd)) {
1145 if (set_mss(td, fd)) {
1150 if (td->o.filename) {
1151 if (!is_udp(o) || !fio_netio_is_multicast(td->o.filename)) {
1152 log_err("fio: hostname not valid for non-multicast inbound network IO\n");
1157 log_err("fio: IPv6 not supported for multicast network IO");
1162 inet_aton(td->o.filename, &sin.sin_addr);
1164 mr.imr_multiaddr = sin.sin_addr;
1166 if (inet_aton(o->intfc, &mr.imr_interface) == 0) {
1167 log_err("fio: interface not valid interface IP\n");
1172 mr.imr_interface.s_addr = htonl(INADDR_ANY);
1175 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mr, sizeof(mr)) < 0) {
1176 td_verror(td, errno, "setsockopt IP_ADD_MEMBERSHIP");
1183 saddr = (struct sockaddr *) &nd->addr;
1184 len = sizeof(nd->addr);
1186 nd->addr.sin_family = AF_INET;
1187 nd->addr.sin_addr.s_addr = sin.sin_addr.s_addr ? sin.sin_addr.s_addr : htonl(INADDR_ANY);
1188 nd->addr.sin_port = htons(port);
1190 saddr = (struct sockaddr *) &nd->addr6;
1191 len = sizeof(nd->addr6);
1193 nd->addr6.sin6_family = AF_INET6;
1194 nd->addr6.sin6_addr = in6addr_any;
1195 nd->addr6.sin6_port = htons(port);
1198 if (bind(fd, saddr, len) < 0) {
1200 td_verror(td, errno, "bind");
1208 static int fio_netio_setup_listen(struct thread_data *td)
1210 struct netio_data *nd = td->io_ops->data;
1211 struct netio_options *o = td->eo;
1214 if (is_udp(o) || is_tcp(o))
1215 ret = fio_netio_setup_listen_inet(td, o->port);
1217 ret = fio_netio_setup_listen_unix(td, td->o.filename);
1224 if (listen(nd->listenfd, 10) < 0) {
1225 td_verror(td, errno, "listen");
1233 static int fio_netio_init(struct thread_data *td)
1235 struct netio_options *o = td->eo;
1240 WSAStartup(MAKEWORD(2,2), &wsd);
1243 if (td_random(td)) {
1244 log_err("fio: network IO can't be random\n");
1248 if (o->proto == FIO_TYPE_UNIX && o->port) {
1249 log_err("fio: network IO port not valid with unix socket\n");
1251 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
1252 log_err("fio: network IO requires port for tcp or udp\n");
1258 log_err("fio: listen only valid for TCP proto IO\n");
1262 log_err("fio: datagram network connections must be"
1263 " read OR write\n");
1266 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
1267 log_err("fio: UNIX sockets need host/filename\n");
1270 o->listen = td_read(td);
1274 ret = fio_netio_setup_listen(td);
1276 ret = fio_netio_setup_connect(td);
1281 static void fio_netio_cleanup(struct thread_data *td)
1283 struct netio_data *nd = td->io_ops->data;
1286 if (nd->listenfd != -1)
1287 close(nd->listenfd);
1288 if (nd->pipes[0] != -1)
1289 close(nd->pipes[0]);
1290 if (nd->pipes[1] != -1)
1291 close(nd->pipes[1]);
1297 static int fio_netio_setup(struct thread_data *td)
1299 struct netio_data *nd;
1301 if (!td->files_index) {
1302 add_file(td, td->o.filename ?: "net", 0, 0);
1303 td->o.nr_files = td->o.nr_files ?: 1;
1307 if (!td->io_ops->data) {
1308 nd = malloc(sizeof(*nd));;
1310 memset(nd, 0, sizeof(*nd));
1312 nd->pipes[0] = nd->pipes[1] = -1;
1313 td->io_ops->data = nd;
1319 static void fio_netio_terminate(struct thread_data *td)
1321 kill(td->pid, SIGTERM);
1324 #ifdef CONFIG_LINUX_SPLICE
1325 static int fio_netio_setup_splice(struct thread_data *td)
1327 struct netio_data *nd;
1329 fio_netio_setup(td);
1331 nd = td->io_ops->data;
1333 if (pipe(nd->pipes) < 0)
1343 static struct ioengine_ops ioengine_splice = {
1344 .name = "netsplice",
1345 .version = FIO_IOOPS_VERSION,
1346 .prep = fio_netio_prep,
1347 .queue = fio_netio_queue,
1348 .setup = fio_netio_setup_splice,
1349 .init = fio_netio_init,
1350 .cleanup = fio_netio_cleanup,
1351 .open_file = fio_netio_open_file,
1352 .close_file = fio_netio_close_file,
1353 .terminate = fio_netio_terminate,
1355 .option_struct_size = sizeof(struct netio_options),
1356 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1361 static struct ioengine_ops ioengine_rw = {
1363 .version = FIO_IOOPS_VERSION,
1364 .prep = fio_netio_prep,
1365 .queue = fio_netio_queue,
1366 .setup = fio_netio_setup,
1367 .init = fio_netio_init,
1368 .cleanup = fio_netio_cleanup,
1369 .open_file = fio_netio_open_file,
1370 .close_file = fio_netio_close_file,
1371 .terminate = fio_netio_terminate,
1373 .option_struct_size = sizeof(struct netio_options),
1374 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1375 FIO_PIPEIO | FIO_BIT_BASED,
1378 static int str_hostname_cb(void *data, const char *input)
1380 struct netio_options *o = data;
1382 if (o->td->o.filename)
1383 free(o->td->o.filename);
1384 o->td->o.filename = strdup(input);
1388 static void fio_init fio_netio_register(void)
1390 register_ioengine(&ioengine_rw);
1391 #ifdef CONFIG_LINUX_SPLICE
1392 register_ioengine(&ioengine_splice);
1396 static void fio_exit fio_netio_unregister(void)
1398 unregister_ioengine(&ioengine_rw);
1399 #ifdef CONFIG_LINUX_SPLICE
1400 unregister_ioengine(&ioengine_splice);