4 * IO engine that reads/writes to/from sockets.
12 #include <netinet/in.h>
13 #include <netinet/tcp.h>
14 #include <arpa/inet.h>
18 #include <sys/socket.h>
22 #include "../verify.h"
23 #include "../optgroup.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 {
62 FIO_LINK_CLOSE = 0x89,
63 FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b,
65 FIO_UDP_SEQ_MAGIC = 0x657375716e556563ULL,
74 static int str_hostname_cb(void *data, const char *input);
75 static struct fio_option options[] = {
78 .lname = "net engine hostname",
79 .type = FIO_OPT_STR_STORE,
80 .cb = str_hostname_cb,
81 .help = "Hostname for net IO engine",
82 .category = FIO_OPT_C_ENGINE,
83 .group = FIO_OPT_G_NETIO,
87 .lname = "net engine port",
89 .off1 = offsetof(struct netio_options, port),
92 .help = "Port to use for TCP or UDP net connections",
93 .category = FIO_OPT_C_ENGINE,
94 .group = FIO_OPT_G_NETIO,
98 .lname = "net engine protocol",
101 .off1 = offsetof(struct netio_options, proto),
102 .help = "Network protocol to use",
106 .oval = FIO_TYPE_TCP,
107 .help = "Transmission Control Protocol",
111 .oval = FIO_TYPE_TCP_V6,
112 .help = "Transmission Control Protocol V6",
116 .oval = FIO_TYPE_UDP,
117 .help = "User Datagram Protocol",
121 .oval = FIO_TYPE_UDP_V6,
122 .help = "User Datagram Protocol V6",
126 .oval = FIO_TYPE_UNIX,
127 .help = "UNIX domain socket",
130 .category = FIO_OPT_C_ENGINE,
131 .group = FIO_OPT_G_NETIO,
133 #ifdef CONFIG_TCP_NODELAY
137 .type = FIO_OPT_BOOL,
138 .off1 = offsetof(struct netio_options, nodelay),
139 .help = "Use TCP_NODELAY on TCP connections",
140 .category = FIO_OPT_C_ENGINE,
141 .group = FIO_OPT_G_NETIO,
146 .lname = "net engine listen",
147 .type = FIO_OPT_STR_SET,
148 .off1 = offsetof(struct netio_options, listen),
149 .help = "Listen for incoming TCP connections",
150 .category = FIO_OPT_C_ENGINE,
151 .group = FIO_OPT_G_NETIO,
155 .lname = "Ping Pong",
156 .type = FIO_OPT_STR_SET,
157 .off1 = offsetof(struct netio_options, pingpong),
158 .help = "Ping-pong IO requests",
159 .category = FIO_OPT_C_ENGINE,
160 .group = FIO_OPT_G_NETIO,
164 .lname = "net engine interface",
165 .type = FIO_OPT_STR_STORE,
166 .off1 = offsetof(struct netio_options, intfc),
167 .help = "Network interface to use",
168 .category = FIO_OPT_C_ENGINE,
169 .group = FIO_OPT_G_NETIO,
173 .lname = "net engine multicast ttl",
175 .off1 = offsetof(struct netio_options, ttl),
178 .help = "Time-to-live value for outgoing UDP multicast packets",
179 .category = FIO_OPT_C_ENGINE,
180 .group = FIO_OPT_G_NETIO,
182 #ifdef CONFIG_NET_WINDOWSIZE
184 .name = "window_size",
185 .lname = "Window Size",
187 .off1 = offsetof(struct netio_options, window_size),
189 .help = "Set socket buffer window size",
190 .category = FIO_OPT_C_ENGINE,
191 .group = FIO_OPT_G_NETIO,
194 #ifdef CONFIG_NET_MSS
197 .lname = "Maximum segment size",
199 .off1 = offsetof(struct netio_options, mss),
201 .help = "Set TCP maximum segment size",
202 .category = FIO_OPT_C_ENGINE,
203 .group = FIO_OPT_G_NETIO,
211 static inline int is_udp(struct netio_options *o)
213 return o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_UDP_V6;
216 static inline int is_tcp(struct netio_options *o)
218 return o->proto == FIO_TYPE_TCP || o->proto == FIO_TYPE_TCP_V6;
221 static inline int is_ipv6(struct netio_options *o)
223 return o->proto == FIO_TYPE_UDP_V6 || o->proto == FIO_TYPE_TCP_V6;
226 static int set_window_size(struct thread_data *td, int fd)
228 #ifdef CONFIG_NET_WINDOWSIZE
229 struct netio_options *o = td->eo;
236 rcv = o->listen || o->pingpong;
237 snd = !o->listen || o->pingpong;
238 wss = o->window_size;
242 ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void *) &wss,
245 td_verror(td, errno, "rcvbuf window size");
248 ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void *) &wss,
251 td_verror(td, errno, "sndbuf window size");
256 td_verror(td, -EINVAL, "setsockopt window size");
261 static int set_mss(struct thread_data *td, int fd)
263 #ifdef CONFIG_NET_MSS
264 struct netio_options *o = td->eo;
268 if (!o->mss || !is_tcp(o))
272 ret = setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, (void *) &mss,
275 td_verror(td, errno, "setsockopt TCP_MAXSEG");
279 td_verror(td, -EINVAL, "setsockopt TCP_MAXSEG");
286 * Return -1 for error and 'nr events' for a positive number
289 static int poll_wait(struct thread_data *td, int fd, short events)
294 while (!td->terminate) {
297 ret = poll(&pfd, 1, -1);
302 td_verror(td, errno, "poll");
310 if (pfd.revents & events)
316 static int fio_netio_is_multicast(const char *mcaddr)
318 in_addr_t addr = inet_network(mcaddr);
322 if (inet_network("224.0.0.0") <= addr &&
323 inet_network("239.255.255.255") >= addr)
330 static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
332 struct netio_options *o = td->eo;
335 * Make sure we don't see spurious reads to a receiver, and vice versa
340 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
341 (!o->listen && io_u->ddir == DDIR_READ)) {
342 td_verror(td, EINVAL, "bad direction");
349 #ifdef CONFIG_LINUX_SPLICE
350 static int splice_io_u(int fdin, int fdout, unsigned int len)
355 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
373 * Receive bytes from a socket and fill them into the internal pipe
375 static int splice_in(struct thread_data *td, struct io_u *io_u)
377 struct netio_data *nd = td->io_ops_data;
379 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
383 * Transmit 'len' bytes from the internal pipe
385 static int splice_out(struct thread_data *td, struct io_u *io_u,
388 struct netio_data *nd = td->io_ops_data;
390 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
393 static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
396 .iov_base = io_u->xfer_buf,
401 while (iov.iov_len) {
402 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
421 * vmsplice() pipe to io_u buffer
423 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
426 struct netio_data *nd = td->io_ops_data;
428 return vmsplice_io_u(io_u, nd->pipes[0], len);
432 * vmsplice() io_u to pipe
434 static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
436 struct netio_data *nd = td->io_ops_data;
438 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
442 * splice receive - transfer socket data into a pipe using splice, then map
443 * that pipe data into the io_u using vmsplice.
445 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
449 ret = splice_in(td, io_u);
451 return vmsplice_io_u_out(td, io_u, ret);
457 * splice transmit - map data from the io_u into a pipe by using vmsplice,
458 * then transfer that pipe to a socket using splice.
460 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
464 ret = vmsplice_io_u_in(td, io_u);
466 return splice_out(td, io_u, ret);
471 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
477 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
484 static void store_udp_seq(struct netio_data *nd, struct io_u *io_u)
488 if (io_u->xfer_buflen < sizeof(*us))
491 us = io_u->xfer_buf + io_u->xfer_buflen - sizeof(*us);
492 us->magic = cpu_to_le64((uint64_t) FIO_UDP_SEQ_MAGIC);
493 us->bs = cpu_to_le64((uint64_t) io_u->xfer_buflen);
494 us->seq = cpu_to_le64(nd->udp_send_seq++);
497 static void verify_udp_seq(struct thread_data *td, struct netio_data *nd,
503 if (io_u->xfer_buflen < sizeof(*us))
509 us = io_u->xfer_buf + io_u->xfer_buflen - sizeof(*us);
510 if (le64_to_cpu(us->magic) != FIO_UDP_SEQ_MAGIC)
512 if (le64_to_cpu(us->bs) != io_u->xfer_buflen) {
517 seq = le64_to_cpu(us->seq);
519 if (seq != nd->udp_recv_seq)
520 td->ts.drop_io_u[io_u->ddir] += seq - nd->udp_recv_seq;
522 nd->udp_recv_seq = seq + 1;
525 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
527 struct netio_data *nd = td->io_ops_data;
528 struct netio_options *o = td->eo;
533 const struct sockaddr *to;
537 to = (struct sockaddr *) &nd->addr6;
538 len = sizeof(nd->addr6);
540 to = (struct sockaddr *) &nd->addr;
541 len = sizeof(nd->addr);
544 if (td->o.verify == VERIFY_NONE)
545 store_udp_seq(nd, io_u);
547 ret = sendto(io_u->file->fd, io_u->xfer_buf,
548 io_u->xfer_buflen, flags, to, len);
551 * if we are going to write more, set MSG_MORE
554 if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
555 td->o.size) && !o->pingpong)
558 ret = send(io_u->file->fd, io_u->xfer_buf,
559 io_u->xfer_buflen, flags);
564 ret = poll_wait(td, io_u->file->fd, POLLOUT);
572 static int is_close_msg(struct io_u *io_u, int len)
574 struct udp_close_msg *msg;
576 if (len != sizeof(struct udp_close_msg))
579 msg = io_u->xfer_buf;
580 if (le32_to_cpu(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC)
582 if (le32_to_cpu(msg->cmd) != FIO_LINK_CLOSE)
588 static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
590 struct netio_data *nd = td->io_ops_data;
591 struct netio_options *o = td->eo;
596 struct sockaddr *from;
597 socklen_t l, *len = &l;
601 from = (struct sockaddr *) &nd->addr;
602 *len = sizeof(nd->addr);
604 from = (struct sockaddr *) &nd->addr6;
605 *len = sizeof(nd->addr6);
612 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
613 io_u->xfer_buflen, flags, from, len);
615 if (is_close_msg(io_u, ret)) {
620 ret = recv(io_u->file->fd, io_u->xfer_buf,
621 io_u->xfer_buflen, flags);
623 if (is_close_msg(io_u, ret)) {
630 else if (!ret && (flags & MSG_WAITALL))
633 ret = poll_wait(td, io_u->file->fd, POLLIN);
636 flags |= MSG_WAITALL;
639 if (is_udp(o) && td->o.verify == VERIFY_NONE)
640 verify_udp_seq(td, nd, io_u);
645 static enum fio_q_status __fio_netio_queue(struct thread_data *td,
649 struct netio_data *nd = td->io_ops_data;
650 struct netio_options *o = td->eo;
653 if (ddir == DDIR_WRITE) {
654 if (!nd->use_splice || is_udp(o) ||
655 o->proto == FIO_TYPE_UNIX)
656 ret = fio_netio_send(td, io_u);
658 ret = fio_netio_splice_out(td, io_u);
659 } else if (ddir == DDIR_READ) {
660 if (!nd->use_splice || is_udp(o) ||
661 o->proto == FIO_TYPE_UNIX)
662 ret = fio_netio_recv(td, io_u);
664 ret = fio_netio_splice_in(td, io_u);
666 ret = 0; /* must be a SYNC */
668 if (ret != (int) io_u->xfer_buflen) {
670 io_u->resid = io_u->xfer_buflen - ret;
672 return FIO_Q_COMPLETED;
678 if (ddir == DDIR_WRITE && err == EMSGSIZE)
686 td_verror(td, io_u->error, "xfer");
688 return FIO_Q_COMPLETED;
691 static enum fio_q_status fio_netio_queue(struct thread_data *td,
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",
942 (unsigned int) ntohl(msg.magic),
943 (unsigned int) ntohl(msg.cmd));
947 fio_gettime(&td->start, NULL);
951 static int fio_netio_send_open(struct thread_data *td, struct fio_file *f)
953 struct netio_data *nd = td->io_ops_data;
954 struct netio_options *o = td->eo;
955 struct udp_close_msg msg;
961 len = sizeof(nd->addr6);
962 to = (struct sockaddr *) &nd->addr6;
964 len = sizeof(nd->addr);
965 to = (struct sockaddr *) &nd->addr;
968 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
969 msg.cmd = htonl(FIO_LINK_OPEN);
971 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
973 td_verror(td, errno, "sendto udp link open");
980 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
983 struct netio_options *o = td->eo;
986 ret = fio_netio_accept(td, f);
988 ret = fio_netio_connect(td, f);
997 ret = fio_netio_send_open(td, f);
1001 state = td->runstate;
1002 td_set_runstate(td, TD_SETTING_UP);
1003 ret = fio_netio_udp_recv_open(td, f);
1004 td_set_runstate(td, state);
1009 fio_netio_close_file(td, f);
1014 static int fio_fill_addr(struct thread_data *td, const char *host, int af,
1015 void *dst, struct addrinfo **res)
1017 struct netio_options *o = td->eo;
1018 struct addrinfo hints;
1021 if (inet_pton(af, host, dst))
1024 memset(&hints, 0, sizeof(hints));
1027 hints.ai_socktype = SOCK_STREAM;
1029 hints.ai_socktype = SOCK_DGRAM;
1032 hints.ai_family = AF_INET6;
1034 hints.ai_family = AF_INET;
1036 ret = getaddrinfo(host, NULL, &hints, res);
1041 if (ret == EAI_SYSTEM)
1044 snprintf(str, sizeof(str), "getaddrinfo: %s", gai_strerror(ret));
1045 td_verror(td, e, str);
1052 static int fio_netio_setup_connect_inet(struct thread_data *td,
1053 const char *host, unsigned short port)
1055 struct netio_data *nd = td->io_ops_data;
1056 struct netio_options *o = td->eo;
1057 struct addrinfo *res = NULL;
1062 log_err("fio: connect with no host to connect to.\n");
1064 log_err("fio: did you forget to set 'listen'?\n");
1066 td_verror(td, EINVAL, "no hostname= set");
1070 nd->addr.sin_family = AF_INET;
1071 nd->addr.sin_port = htons(port);
1072 nd->addr6.sin6_family = AF_INET6;
1073 nd->addr6.sin6_port = htons(port);
1077 dst = &nd->addr6.sin6_addr;
1080 dst = &nd->addr.sin_addr;
1083 if (fio_fill_addr(td, host, af, dst, &res))
1090 len = sizeof(nd->addr6.sin6_addr);
1091 src = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
1093 len = sizeof(nd->addr.sin_addr);
1094 src = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
1097 memcpy(dst, src, len);
1102 static int fio_netio_setup_connect_unix(struct thread_data *td,
1105 struct netio_data *nd = td->io_ops_data;
1106 struct sockaddr_un *soun = &nd->addr_un;
1108 soun->sun_family = AF_UNIX;
1109 snprintf(soun->sun_path, sizeof(soun->sun_path), "%s", path);
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 addr->sun_family = AF_UNIX;
1139 snprintf(addr->sun_path, sizeof(addr->sun_path), "%s", path);
1142 len = sizeof(addr->sun_family) + strlen(path) + 1;
1144 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
1145 log_err("fio: bind: %s\n", strerror(errno));
1155 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
1157 struct netio_data *nd = td->io_ops_data;
1158 struct netio_options *o = td->eo;
1160 struct sockaddr_in sin;
1161 struct sockaddr *saddr;
1162 int fd, opt, type, domain;
1165 memset(&sin, 0, sizeof(sin));
1167 if (o->proto == FIO_TYPE_TCP) {
1170 } else if (o->proto == FIO_TYPE_TCP_V6) {
1173 } else if (o->proto == FIO_TYPE_UDP) {
1176 } else if (o->proto == FIO_TYPE_UDP_V6) {
1180 log_err("fio: unknown proto %d\n", o->proto);
1184 fd = socket(domain, type, 0);
1186 td_verror(td, errno, "socket");
1191 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
1192 td_verror(td, errno, "setsockopt");
1197 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) {
1198 td_verror(td, errno, "setsockopt");
1204 if (set_window_size(td, fd)) {
1208 if (set_mss(td, fd)) {
1213 if (td->o.filename) {
1214 if (!is_udp(o) || !fio_netio_is_multicast(td->o.filename)) {
1215 log_err("fio: hostname not valid for non-multicast inbound network IO\n");
1220 log_err("fio: IPv6 not supported for multicast network IO\n");
1225 inet_aton(td->o.filename, &sin.sin_addr);
1227 mr.imr_multiaddr = sin.sin_addr;
1229 if (inet_aton(o->intfc, &mr.imr_interface) == 0) {
1230 log_err("fio: interface not valid interface IP\n");
1235 mr.imr_interface.s_addr = htonl(INADDR_ANY);
1238 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mr, sizeof(mr)) < 0) {
1239 td_verror(td, errno, "setsockopt IP_ADD_MEMBERSHIP");
1246 saddr = (struct sockaddr *) &nd->addr;
1247 len = sizeof(nd->addr);
1249 nd->addr.sin_family = AF_INET;
1250 nd->addr.sin_addr.s_addr = sin.sin_addr.s_addr ? sin.sin_addr.s_addr : htonl(INADDR_ANY);
1251 nd->addr.sin_port = htons(port);
1253 saddr = (struct sockaddr *) &nd->addr6;
1254 len = sizeof(nd->addr6);
1256 nd->addr6.sin6_family = AF_INET6;
1257 nd->addr6.sin6_addr = in6addr_any;
1258 nd->addr6.sin6_port = htons(port);
1261 if (bind(fd, saddr, len) < 0) {
1263 td_verror(td, errno, "bind");
1271 static int fio_netio_setup_listen(struct thread_data *td)
1273 struct netio_data *nd = td->io_ops_data;
1274 struct netio_options *o = td->eo;
1277 if (is_udp(o) || is_tcp(o))
1278 ret = fio_netio_setup_listen_inet(td, o->port);
1280 ret = fio_netio_setup_listen_unix(td, td->o.filename);
1287 if (listen(nd->listenfd, 10) < 0) {
1288 td_verror(td, errno, "listen");
1296 static int fio_netio_init(struct thread_data *td)
1298 struct netio_options *o = td->eo;
1303 WSAStartup(MAKEWORD(2,2), &wsd);
1306 if (td_random(td)) {
1307 log_err("fio: network IO can't be random\n");
1311 if (o->proto == FIO_TYPE_UNIX && o->port) {
1312 log_err("fio: network IO port not valid with unix socket\n");
1314 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
1315 log_err("fio: network IO requires port for tcp or udp\n");
1319 o->port += td->subjob_number;
1323 log_err("fio: listen only valid for TCP proto IO\n");
1327 log_err("fio: datagram network connections must be"
1328 " read OR write\n");
1331 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
1332 log_err("fio: UNIX sockets need host/filename\n");
1335 o->listen = td_read(td);
1339 ret = fio_netio_setup_listen(td);
1341 ret = fio_netio_setup_connect(td);
1346 static void fio_netio_cleanup(struct thread_data *td)
1348 struct netio_data *nd = td->io_ops_data;
1351 if (nd->listenfd != -1)
1352 close(nd->listenfd);
1353 if (nd->pipes[0] != -1)
1354 close(nd->pipes[0]);
1355 if (nd->pipes[1] != -1)
1356 close(nd->pipes[1]);
1362 static int fio_netio_setup(struct thread_data *td)
1364 struct netio_data *nd;
1366 if (!td->files_index) {
1367 add_file(td, td->o.filename ?: "net", 0, 0);
1368 td->o.nr_files = td->o.nr_files ?: 1;
1372 if (!td->io_ops_data) {
1373 nd = calloc(1, sizeof(*nd));
1375 nd->pipes[0] = nd->pipes[1] = -1;
1376 td->io_ops_data = nd;
1382 static void fio_netio_terminate(struct thread_data *td)
1384 kill(td->pid, SIGTERM);
1387 #ifdef CONFIG_LINUX_SPLICE
1388 static int fio_netio_setup_splice(struct thread_data *td)
1390 struct netio_data *nd;
1392 fio_netio_setup(td);
1394 nd = td->io_ops_data;
1396 if (pipe(nd->pipes) < 0)
1406 static struct ioengine_ops ioengine_splice = {
1407 .name = "netsplice",
1408 .version = FIO_IOOPS_VERSION,
1409 .prep = fio_netio_prep,
1410 .queue = fio_netio_queue,
1411 .setup = fio_netio_setup_splice,
1412 .init = fio_netio_init,
1413 .cleanup = fio_netio_cleanup,
1414 .open_file = fio_netio_open_file,
1415 .close_file = fio_netio_close_file,
1416 .terminate = fio_netio_terminate,
1418 .option_struct_size = sizeof(struct netio_options),
1419 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1424 static struct ioengine_ops ioengine_rw = {
1426 .version = FIO_IOOPS_VERSION,
1427 .prep = fio_netio_prep,
1428 .queue = fio_netio_queue,
1429 .setup = fio_netio_setup,
1430 .init = fio_netio_init,
1431 .cleanup = fio_netio_cleanup,
1432 .open_file = fio_netio_open_file,
1433 .close_file = fio_netio_close_file,
1434 .terminate = fio_netio_terminate,
1436 .option_struct_size = sizeof(struct netio_options),
1437 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1438 FIO_PIPEIO | FIO_BIT_BASED,
1441 static int str_hostname_cb(void *data, const char *input)
1443 struct netio_options *o = data;
1445 if (o->td->o.filename)
1446 free(o->td->o.filename);
1447 o->td->o.filename = strdup(input);
1451 static void fio_init fio_netio_register(void)
1453 register_ioengine(&ioengine_rw);
1454 #ifdef CONFIG_LINUX_SPLICE
1455 register_ioengine(&ioengine_splice);
1459 static void fio_exit fio_netio_unregister(void)
1461 unregister_ioengine(&ioengine_rw);
1462 #ifdef CONFIG_LINUX_SPLICE
1463 unregister_ioengine(&ioengine_splice);