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"
25 #include "../optgroup.h"
32 struct sockaddr_in addr;
33 struct sockaddr_in6 addr6;
34 struct sockaddr_un addr_un;
35 uint64_t udp_send_seq;
36 uint64_t udp_recv_seq;
39 struct netio_options {
40 struct thread_data *td;
44 unsigned int pingpong;
47 unsigned int window_size;
52 struct udp_close_msg {
64 FIO_LINK_CLOSE = 0x89,
65 FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b,
67 FIO_UDP_SEQ_MAGIC = 0x657375716e556563ULL,
76 static int str_hostname_cb(void *data, const char *input);
77 static struct fio_option options[] = {
80 .lname = "net engine hostname",
81 .type = FIO_OPT_STR_STORE,
82 .cb = str_hostname_cb,
83 .help = "Hostname for net IO engine",
84 .category = FIO_OPT_C_ENGINE,
85 .group = FIO_OPT_G_NETIO,
89 .lname = "net engine port",
91 .off1 = offsetof(struct netio_options, port),
94 .help = "Port to use for TCP or UDP net connections",
95 .category = FIO_OPT_C_ENGINE,
96 .group = FIO_OPT_G_NETIO,
100 .lname = "net engine protocol",
103 .off1 = offsetof(struct netio_options, proto),
104 .help = "Network protocol to use",
108 .oval = FIO_TYPE_TCP,
109 .help = "Transmission Control Protocol",
113 .oval = FIO_TYPE_TCP_V6,
114 .help = "Transmission Control Protocol V6",
118 .oval = FIO_TYPE_UDP,
119 .help = "User Datagram Protocol",
123 .oval = FIO_TYPE_UDP_V6,
124 .help = "User Datagram Protocol V6",
128 .oval = FIO_TYPE_UNIX,
129 .help = "UNIX domain socket",
132 .category = FIO_OPT_C_ENGINE,
133 .group = FIO_OPT_G_NETIO,
135 #ifdef CONFIG_TCP_NODELAY
139 .type = FIO_OPT_BOOL,
140 .off1 = offsetof(struct netio_options, nodelay),
141 .help = "Use TCP_NODELAY on TCP connections",
142 .category = FIO_OPT_C_ENGINE,
143 .group = FIO_OPT_G_NETIO,
148 .lname = "net engine listen",
149 .type = FIO_OPT_STR_SET,
150 .off1 = offsetof(struct netio_options, listen),
151 .help = "Listen for incoming TCP connections",
152 .category = FIO_OPT_C_ENGINE,
153 .group = FIO_OPT_G_NETIO,
157 .lname = "Ping Pong",
158 .type = FIO_OPT_STR_SET,
159 .off1 = offsetof(struct netio_options, pingpong),
160 .help = "Ping-pong IO requests",
161 .category = FIO_OPT_C_ENGINE,
162 .group = FIO_OPT_G_NETIO,
166 .lname = "net engine interface",
167 .type = FIO_OPT_STR_STORE,
168 .off1 = offsetof(struct netio_options, intfc),
169 .help = "Network interface to use",
170 .category = FIO_OPT_C_ENGINE,
171 .group = FIO_OPT_G_NETIO,
175 .lname = "net engine multicast ttl",
177 .off1 = offsetof(struct netio_options, ttl),
180 .help = "Time-to-live value for outgoing UDP multicast packets",
181 .category = FIO_OPT_C_ENGINE,
182 .group = FIO_OPT_G_NETIO,
184 #ifdef CONFIG_NET_WINDOWSIZE
186 .name = "window_size",
187 .lname = "Window Size",
189 .off1 = offsetof(struct netio_options, window_size),
191 .help = "Set socket buffer window size",
192 .category = FIO_OPT_C_ENGINE,
193 .group = FIO_OPT_G_NETIO,
196 #ifdef CONFIG_NET_MSS
199 .lname = "Maximum segment size",
201 .off1 = offsetof(struct netio_options, mss),
203 .help = "Set TCP maximum segment size",
204 .category = FIO_OPT_C_ENGINE,
205 .group = FIO_OPT_G_NETIO,
213 static inline int is_udp(struct netio_options *o)
215 return o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_UDP_V6;
218 static inline int is_tcp(struct netio_options *o)
220 return o->proto == FIO_TYPE_TCP || o->proto == FIO_TYPE_TCP_V6;
223 static inline int is_ipv6(struct netio_options *o)
225 return o->proto == FIO_TYPE_UDP_V6 || o->proto == FIO_TYPE_TCP_V6;
228 static int set_window_size(struct thread_data *td, int fd)
230 #ifdef CONFIG_NET_WINDOWSIZE
231 struct netio_options *o = td->eo;
238 rcv = o->listen || o->pingpong;
239 snd = !o->listen || o->pingpong;
240 wss = o->window_size;
244 ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void *) &wss,
247 td_verror(td, errno, "rcvbuf window size");
250 ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void *) &wss,
253 td_verror(td, errno, "sndbuf window size");
258 td_verror(td, -EINVAL, "setsockopt window size");
263 static int set_mss(struct thread_data *td, int fd)
265 #ifdef CONFIG_NET_MSS
266 struct netio_options *o = td->eo;
270 if (!o->mss || !is_tcp(o))
274 ret = setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, (void *) &mss,
277 td_verror(td, errno, "setsockopt TCP_MAXSEG");
281 td_verror(td, -EINVAL, "setsockopt TCP_MAXSEG");
288 * Return -1 for error and 'nr events' for a positive number
291 static int poll_wait(struct thread_data *td, int fd, short events)
296 while (!td->terminate) {
299 ret = poll(&pfd, 1, -1);
304 td_verror(td, errno, "poll");
312 if (pfd.revents & events)
318 static int fio_netio_is_multicast(const char *mcaddr)
320 in_addr_t addr = inet_network(mcaddr);
324 if (inet_network("224.0.0.0") <= addr &&
325 inet_network("239.255.255.255") >= addr)
332 static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
334 struct netio_options *o = td->eo;
337 * Make sure we don't see spurious reads to a receiver, and vice versa
342 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
343 (!o->listen && io_u->ddir == DDIR_READ)) {
344 td_verror(td, EINVAL, "bad direction");
351 #ifdef CONFIG_LINUX_SPLICE
352 static int splice_io_u(int fdin, int fdout, unsigned int len)
357 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
375 * Receive bytes from a socket and fill them into the internal pipe
377 static int splice_in(struct thread_data *td, struct io_u *io_u)
379 struct netio_data *nd = td->io_ops_data;
381 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
385 * Transmit 'len' bytes from the internal pipe
387 static int splice_out(struct thread_data *td, struct io_u *io_u,
390 struct netio_data *nd = td->io_ops_data;
392 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
395 static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
398 .iov_base = io_u->xfer_buf,
403 while (iov.iov_len) {
404 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
423 * vmsplice() pipe to io_u buffer
425 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
428 struct netio_data *nd = td->io_ops_data;
430 return vmsplice_io_u(io_u, nd->pipes[0], len);
434 * vmsplice() io_u to pipe
436 static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
438 struct netio_data *nd = td->io_ops_data;
440 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
444 * splice receive - transfer socket data into a pipe using splice, then map
445 * that pipe data into the io_u using vmsplice.
447 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
451 ret = splice_in(td, io_u);
453 return vmsplice_io_u_out(td, io_u, ret);
459 * splice transmit - map data from the io_u into a pipe by using vmsplice,
460 * then transfer that pipe to a socket using splice.
462 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
466 ret = vmsplice_io_u_in(td, io_u);
468 return splice_out(td, io_u, ret);
473 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
479 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
486 static void store_udp_seq(struct netio_data *nd, struct io_u *io_u)
490 if (io_u->xfer_buflen < sizeof(*us))
493 us = io_u->xfer_buf + io_u->xfer_buflen - sizeof(*us);
494 us->magic = cpu_to_le64((uint64_t) FIO_UDP_SEQ_MAGIC);
495 us->bs = cpu_to_le64((uint64_t) io_u->xfer_buflen);
496 us->seq = cpu_to_le64(nd->udp_send_seq++);
499 static void verify_udp_seq(struct thread_data *td, struct netio_data *nd,
505 if (io_u->xfer_buflen < sizeof(*us))
511 us = io_u->xfer_buf + io_u->xfer_buflen - sizeof(*us);
512 if (le64_to_cpu(us->magic) != FIO_UDP_SEQ_MAGIC)
514 if (le64_to_cpu(us->bs) != io_u->xfer_buflen) {
519 seq = le64_to_cpu(us->seq);
521 if (seq != nd->udp_recv_seq)
522 td->ts.drop_io_u[io_u->ddir] += seq - nd->udp_recv_seq;
524 nd->udp_recv_seq = seq + 1;
527 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
529 struct netio_data *nd = td->io_ops_data;
530 struct netio_options *o = td->eo;
535 const struct sockaddr *to;
539 to = (struct sockaddr *) &nd->addr6;
540 len = sizeof(nd->addr6);
542 to = (struct sockaddr *) &nd->addr;
543 len = sizeof(nd->addr);
546 if (td->o.verify == VERIFY_NONE)
547 store_udp_seq(nd, io_u);
549 ret = sendto(io_u->file->fd, io_u->xfer_buf,
550 io_u->xfer_buflen, flags, to, len);
553 * if we are going to write more, set MSG_MORE
556 if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
557 td->o.size) && !o->pingpong)
560 ret = send(io_u->file->fd, io_u->xfer_buf,
561 io_u->xfer_buflen, flags);
566 ret = poll_wait(td, io_u->file->fd, POLLOUT);
574 static int is_close_msg(struct io_u *io_u, int len)
576 struct udp_close_msg *msg;
578 if (len != sizeof(struct udp_close_msg))
581 msg = io_u->xfer_buf;
582 if (le32_to_cpu(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC)
584 if (le32_to_cpu(msg->cmd) != FIO_LINK_CLOSE)
590 static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
592 struct netio_data *nd = td->io_ops_data;
593 struct netio_options *o = td->eo;
598 struct sockaddr *from;
599 socklen_t l, *len = &l;
603 from = (struct sockaddr *) &nd->addr;
604 *len = sizeof(nd->addr);
606 from = (struct sockaddr *) &nd->addr6;
607 *len = sizeof(nd->addr6);
614 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
615 io_u->xfer_buflen, flags, from, len);
617 if (is_close_msg(io_u, ret)) {
622 ret = recv(io_u->file->fd, io_u->xfer_buf,
623 io_u->xfer_buflen, flags);
625 if (is_close_msg(io_u, ret)) {
632 else if (!ret && (flags & MSG_WAITALL))
635 ret = poll_wait(td, io_u->file->fd, POLLIN);
638 flags |= MSG_WAITALL;
641 if (is_udp(o) && td->o.verify == VERIFY_NONE)
642 verify_udp_seq(td, nd, io_u);
647 static int __fio_netio_queue(struct thread_data *td, struct io_u *io_u,
650 struct netio_data *nd = td->io_ops_data;
651 struct netio_options *o = td->eo;
654 if (ddir == DDIR_WRITE) {
655 if (!nd->use_splice || is_udp(o) ||
656 o->proto == FIO_TYPE_UNIX)
657 ret = fio_netio_send(td, io_u);
659 ret = fio_netio_splice_out(td, io_u);
660 } else if (ddir == DDIR_READ) {
661 if (!nd->use_splice || is_udp(o) ||
662 o->proto == FIO_TYPE_UNIX)
663 ret = fio_netio_recv(td, io_u);
665 ret = fio_netio_splice_in(td, io_u);
667 ret = 0; /* must be a SYNC */
669 if (ret != (int) io_u->xfer_buflen) {
671 io_u->resid = io_u->xfer_buflen - ret;
673 return FIO_Q_COMPLETED;
679 if (ddir == DDIR_WRITE && err == EMSGSIZE)
687 td_verror(td, io_u->error, "xfer");
689 return FIO_Q_COMPLETED;
692 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
694 struct netio_options *o = td->eo;
697 fio_ro_check(td, io_u);
699 ret = __fio_netio_queue(td, io_u, io_u->ddir);
700 if (!o->pingpong || ret != FIO_Q_COMPLETED)
704 * For ping-pong mode, receive or send reply as needed
706 if (td_read(td) && io_u->ddir == DDIR_READ)
707 ret = __fio_netio_queue(td, io_u, DDIR_WRITE);
708 else if (td_write(td) && io_u->ddir == DDIR_WRITE)
709 ret = __fio_netio_queue(td, io_u, DDIR_READ);
714 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
716 struct netio_data *nd = td->io_ops_data;
717 struct netio_options *o = td->eo;
720 if (o->proto == FIO_TYPE_TCP) {
723 } else if (o->proto == FIO_TYPE_TCP_V6) {
726 } else if (o->proto == FIO_TYPE_UDP) {
729 } else if (o->proto == FIO_TYPE_UDP_V6) {
732 } else if (o->proto == FIO_TYPE_UNIX) {
736 log_err("fio: bad network type %d\n", o->proto);
741 f->fd = socket(domain, type, 0);
743 td_verror(td, errno, "socket");
747 #ifdef CONFIG_TCP_NODELAY
748 if (o->nodelay && is_tcp(o)) {
751 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
752 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
758 if (set_window_size(td, f->fd)) {
762 if (set_mss(td, f->fd)) {
768 if (!fio_netio_is_multicast(td->o.filename))
771 log_err("fio: multicast not supported on IPv6\n");
777 struct in_addr interface_addr;
779 if (inet_aton(o->intfc, &interface_addr) == 0) {
780 log_err("fio: interface not valid interface IP\n");
784 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_IF, (const char*)&interface_addr, sizeof(interface_addr)) < 0) {
785 td_verror(td, errno, "setsockopt IP_MULTICAST_IF");
790 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_TTL, (const char*)&o->ttl, sizeof(o->ttl)) < 0) {
791 td_verror(td, errno, "setsockopt IP_MULTICAST_TTL");
796 } else if (o->proto == FIO_TYPE_TCP) {
797 socklen_t len = sizeof(nd->addr);
799 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
800 td_verror(td, errno, "connect");
804 } else if (o->proto == FIO_TYPE_TCP_V6) {
805 socklen_t len = sizeof(nd->addr6);
807 if (connect(f->fd, (struct sockaddr *) &nd->addr6, len) < 0) {
808 td_verror(td, errno, "connect");
814 struct sockaddr_un *addr = &nd->addr_un;
817 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
819 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
820 td_verror(td, errno, "connect");
829 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
831 struct netio_data *nd = td->io_ops_data;
832 struct netio_options *o = td->eo;
837 f->fd = nd->listenfd;
841 state = td->runstate;
842 td_set_runstate(td, TD_SETTING_UP);
844 log_info("fio: waiting for connection\n");
846 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
849 if (o->proto == FIO_TYPE_TCP) {
850 socklen = sizeof(nd->addr);
851 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
853 socklen = sizeof(nd->addr6);
854 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr6, &socklen);
858 td_verror(td, errno, "accept");
862 #ifdef CONFIG_TCP_NODELAY
863 if (o->nodelay && is_tcp(o)) {
866 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
867 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
874 td_set_runstate(td, state);
877 td_set_runstate(td, state);
881 static void fio_netio_send_close(struct thread_data *td, struct fio_file *f)
883 struct netio_data *nd = td->io_ops_data;
884 struct netio_options *o = td->eo;
885 struct udp_close_msg msg;
891 to = (struct sockaddr *) &nd->addr6;
892 len = sizeof(nd->addr6);
894 to = (struct sockaddr *) &nd->addr;
895 len = sizeof(nd->addr);
898 msg.magic = cpu_to_le32((uint32_t) FIO_LINK_OPEN_CLOSE_MAGIC);
899 msg.cmd = cpu_to_le32((uint32_t) FIO_LINK_CLOSE);
901 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
903 td_verror(td, errno, "sendto udp link close");
906 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
909 * Notify the receiver that we are closing down the link
911 fio_netio_send_close(td, f);
913 return generic_close_file(td, f);
916 static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
918 struct netio_data *nd = td->io_ops_data;
919 struct netio_options *o = td->eo;
920 struct udp_close_msg msg;
926 len = sizeof(nd->addr6);
927 to = (struct sockaddr *) &nd->addr6;
929 len = sizeof(nd->addr);
930 to = (struct sockaddr *) &nd->addr;
933 ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
935 td_verror(td, errno, "recvfrom udp link open");
939 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
940 ntohl(msg.cmd) != FIO_LINK_OPEN) {
941 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
946 fio_gettime(&td->start, NULL);
950 static int fio_netio_send_open(struct thread_data *td, struct fio_file *f)
952 struct netio_data *nd = td->io_ops_data;
953 struct netio_options *o = td->eo;
954 struct udp_close_msg msg;
960 len = sizeof(nd->addr6);
961 to = (struct sockaddr *) &nd->addr6;
963 len = sizeof(nd->addr);
964 to = (struct sockaddr *) &nd->addr;
967 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
968 msg.cmd = htonl(FIO_LINK_OPEN);
970 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
972 td_verror(td, errno, "sendto udp link open");
979 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
982 struct netio_options *o = td->eo;
985 ret = fio_netio_accept(td, f);
987 ret = fio_netio_connect(td, f);
996 ret = fio_netio_send_open(td, f);
1000 state = td->runstate;
1001 td_set_runstate(td, TD_SETTING_UP);
1002 ret = fio_netio_udp_recv_open(td, f);
1003 td_set_runstate(td, state);
1008 fio_netio_close_file(td, f);
1013 static int fio_fill_addr(struct thread_data *td, const char *host, int af,
1014 void *dst, struct addrinfo **res)
1016 struct netio_options *o = td->eo;
1017 struct addrinfo hints;
1020 if (inet_pton(af, host, dst))
1023 memset(&hints, 0, sizeof(hints));
1026 hints.ai_socktype = SOCK_STREAM;
1028 hints.ai_socktype = SOCK_DGRAM;
1031 hints.ai_family = AF_INET6;
1033 hints.ai_family = AF_INET;
1035 ret = getaddrinfo(host, NULL, &hints, res);
1040 if (ret == EAI_SYSTEM)
1043 snprintf(str, sizeof(str), "getaddrinfo: %s", gai_strerror(ret));
1044 td_verror(td, e, str);
1051 static int fio_netio_setup_connect_inet(struct thread_data *td,
1052 const char *host, unsigned short port)
1054 struct netio_data *nd = td->io_ops_data;
1055 struct netio_options *o = td->eo;
1056 struct addrinfo *res = NULL;
1061 log_err("fio: connect with no host to connect to.\n");
1063 log_err("fio: did you forget to set 'listen'?\n");
1065 td_verror(td, EINVAL, "no hostname= set");
1069 nd->addr.sin_family = AF_INET;
1070 nd->addr.sin_port = htons(port);
1071 nd->addr6.sin6_family = AF_INET6;
1072 nd->addr6.sin6_port = htons(port);
1076 dst = &nd->addr6.sin6_addr;
1079 dst = &nd->addr.sin_addr;
1082 if (fio_fill_addr(td, host, af, dst, &res))
1089 len = sizeof(nd->addr6.sin6_addr);
1090 src = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
1092 len = sizeof(nd->addr.sin_addr);
1093 src = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
1096 memcpy(dst, src, len);
1101 static int fio_netio_setup_connect_unix(struct thread_data *td,
1104 struct netio_data *nd = td->io_ops_data;
1105 struct sockaddr_un *soun = &nd->addr_un;
1107 soun->sun_family = AF_UNIX;
1108 memset(soun->sun_path, 0, sizeof(soun->sun_path));
1109 strncpy(soun->sun_path, path, sizeof(soun->sun_path) - 1);
1113 static int fio_netio_setup_connect(struct thread_data *td)
1115 struct netio_options *o = td->eo;
1117 if (is_udp(o) || is_tcp(o))
1118 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
1120 return fio_netio_setup_connect_unix(td, td->o.filename);
1123 static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
1125 struct netio_data *nd = td->io_ops_data;
1126 struct sockaddr_un *addr = &nd->addr_un;
1130 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1132 log_err("fio: socket: %s\n", strerror(errno));
1138 memset(addr, 0, sizeof(*addr));
1139 addr->sun_family = AF_UNIX;
1140 strncpy(addr->sun_path, path, sizeof(addr->sun_path) - 1);
1143 len = sizeof(addr->sun_family) + strlen(path) + 1;
1145 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
1146 log_err("fio: bind: %s\n", strerror(errno));
1156 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
1158 struct netio_data *nd = td->io_ops_data;
1159 struct netio_options *o = td->eo;
1161 struct sockaddr_in sin;
1162 struct sockaddr *saddr;
1163 int fd, opt, type, domain;
1166 memset(&sin, 0, sizeof(sin));
1168 if (o->proto == FIO_TYPE_TCP) {
1171 } else if (o->proto == FIO_TYPE_TCP_V6) {
1174 } else if (o->proto == FIO_TYPE_UDP) {
1177 } else if (o->proto == FIO_TYPE_UDP_V6) {
1181 log_err("fio: unknown proto %d\n", o->proto);
1185 fd = socket(domain, type, 0);
1187 td_verror(td, errno, "socket");
1192 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
1193 td_verror(td, errno, "setsockopt");
1198 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) {
1199 td_verror(td, errno, "setsockopt");
1205 if (set_window_size(td, fd)) {
1209 if (set_mss(td, fd)) {
1214 if (td->o.filename) {
1215 if (!is_udp(o) || !fio_netio_is_multicast(td->o.filename)) {
1216 log_err("fio: hostname not valid for non-multicast inbound network IO\n");
1221 log_err("fio: IPv6 not supported for multicast network IO\n");
1226 inet_aton(td->o.filename, &sin.sin_addr);
1228 mr.imr_multiaddr = sin.sin_addr;
1230 if (inet_aton(o->intfc, &mr.imr_interface) == 0) {
1231 log_err("fio: interface not valid interface IP\n");
1236 mr.imr_interface.s_addr = htonl(INADDR_ANY);
1239 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mr, sizeof(mr)) < 0) {
1240 td_verror(td, errno, "setsockopt IP_ADD_MEMBERSHIP");
1247 saddr = (struct sockaddr *) &nd->addr;
1248 len = sizeof(nd->addr);
1250 nd->addr.sin_family = AF_INET;
1251 nd->addr.sin_addr.s_addr = sin.sin_addr.s_addr ? sin.sin_addr.s_addr : htonl(INADDR_ANY);
1252 nd->addr.sin_port = htons(port);
1254 saddr = (struct sockaddr *) &nd->addr6;
1255 len = sizeof(nd->addr6);
1257 nd->addr6.sin6_family = AF_INET6;
1258 nd->addr6.sin6_addr = in6addr_any;
1259 nd->addr6.sin6_port = htons(port);
1262 if (bind(fd, saddr, len) < 0) {
1264 td_verror(td, errno, "bind");
1272 static int fio_netio_setup_listen(struct thread_data *td)
1274 struct netio_data *nd = td->io_ops_data;
1275 struct netio_options *o = td->eo;
1278 if (is_udp(o) || is_tcp(o))
1279 ret = fio_netio_setup_listen_inet(td, o->port);
1281 ret = fio_netio_setup_listen_unix(td, td->o.filename);
1288 if (listen(nd->listenfd, 10) < 0) {
1289 td_verror(td, errno, "listen");
1297 static int fio_netio_init(struct thread_data *td)
1299 struct netio_options *o = td->eo;
1304 WSAStartup(MAKEWORD(2,2), &wsd);
1307 if (td_random(td)) {
1308 log_err("fio: network IO can't be random\n");
1312 if (o->proto == FIO_TYPE_UNIX && o->port) {
1313 log_err("fio: network IO port not valid with unix socket\n");
1315 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
1316 log_err("fio: network IO requires port for tcp or udp\n");
1320 o->port += td->subjob_number;
1324 log_err("fio: listen only valid for TCP proto IO\n");
1328 log_err("fio: datagram network connections must be"
1329 " read OR write\n");
1332 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
1333 log_err("fio: UNIX sockets need host/filename\n");
1336 o->listen = td_read(td);
1340 ret = fio_netio_setup_listen(td);
1342 ret = fio_netio_setup_connect(td);
1347 static void fio_netio_cleanup(struct thread_data *td)
1349 struct netio_data *nd = td->io_ops_data;
1352 if (nd->listenfd != -1)
1353 close(nd->listenfd);
1354 if (nd->pipes[0] != -1)
1355 close(nd->pipes[0]);
1356 if (nd->pipes[1] != -1)
1357 close(nd->pipes[1]);
1363 static int fio_netio_setup(struct thread_data *td)
1365 struct netio_data *nd;
1367 if (!td->files_index) {
1368 add_file(td, td->o.filename ?: "net", 0, 0);
1369 td->o.nr_files = td->o.nr_files ?: 1;
1373 if (!td->io_ops_data) {
1374 nd = malloc(sizeof(*nd));
1376 memset(nd, 0, sizeof(*nd));
1378 nd->pipes[0] = nd->pipes[1] = -1;
1379 td->io_ops_data = nd;
1385 static void fio_netio_terminate(struct thread_data *td)
1387 kill(td->pid, SIGTERM);
1390 #ifdef CONFIG_LINUX_SPLICE
1391 static int fio_netio_setup_splice(struct thread_data *td)
1393 struct netio_data *nd;
1395 fio_netio_setup(td);
1397 nd = td->io_ops_data;
1399 if (pipe(nd->pipes) < 0)
1409 static struct ioengine_ops ioengine_splice = {
1410 .name = "netsplice",
1411 .version = FIO_IOOPS_VERSION,
1412 .prep = fio_netio_prep,
1413 .queue = fio_netio_queue,
1414 .setup = fio_netio_setup_splice,
1415 .init = fio_netio_init,
1416 .cleanup = fio_netio_cleanup,
1417 .open_file = fio_netio_open_file,
1418 .close_file = fio_netio_close_file,
1419 .terminate = fio_netio_terminate,
1421 .option_struct_size = sizeof(struct netio_options),
1422 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1427 static struct ioengine_ops ioengine_rw = {
1429 .version = FIO_IOOPS_VERSION,
1430 .prep = fio_netio_prep,
1431 .queue = fio_netio_queue,
1432 .setup = fio_netio_setup,
1433 .init = fio_netio_init,
1434 .cleanup = fio_netio_cleanup,
1435 .open_file = fio_netio_open_file,
1436 .close_file = fio_netio_close_file,
1437 .terminate = fio_netio_terminate,
1439 .option_struct_size = sizeof(struct netio_options),
1440 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1441 FIO_PIPEIO | FIO_BIT_BASED,
1444 static int str_hostname_cb(void *data, const char *input)
1446 struct netio_options *o = data;
1448 if (o->td->o.filename)
1449 free(o->td->o.filename);
1450 o->td->o.filename = strdup(input);
1454 static void fio_init fio_netio_register(void)
1456 register_ioengine(&ioengine_rw);
1457 #ifdef CONFIG_LINUX_SPLICE
1458 register_ioengine(&ioengine_splice);
1462 static void fio_exit fio_netio_unregister(void)
1464 unregister_ioengine(&ioengine_rw);
1465 #ifdef CONFIG_LINUX_SPLICE
1466 unregister_ioengine(&ioengine_splice);