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
138 .type = FIO_OPT_BOOL,
139 .off1 = offsetof(struct netio_options, nodelay),
140 .help = "Use TCP_NODELAY on TCP connections",
141 .category = FIO_OPT_C_ENGINE,
142 .group = FIO_OPT_G_NETIO,
147 .lname = "net engine listen",
148 .type = FIO_OPT_STR_SET,
149 .off1 = offsetof(struct netio_options, listen),
150 .help = "Listen for incoming TCP connections",
151 .category = FIO_OPT_C_ENGINE,
152 .group = FIO_OPT_G_NETIO,
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 int __fio_netio_queue(struct thread_data *td, struct io_u *io_u,
648 struct netio_data *nd = td->io_ops_data;
649 struct netio_options *o = td->eo;
652 if (ddir == DDIR_WRITE) {
653 if (!nd->use_splice || is_udp(o) ||
654 o->proto == FIO_TYPE_UNIX)
655 ret = fio_netio_send(td, io_u);
657 ret = fio_netio_splice_out(td, io_u);
658 } else if (ddir == DDIR_READ) {
659 if (!nd->use_splice || is_udp(o) ||
660 o->proto == FIO_TYPE_UNIX)
661 ret = fio_netio_recv(td, io_u);
663 ret = fio_netio_splice_in(td, io_u);
665 ret = 0; /* must be a SYNC */
667 if (ret != (int) io_u->xfer_buflen) {
669 io_u->resid = io_u->xfer_buflen - ret;
671 return FIO_Q_COMPLETED;
677 if (ddir == DDIR_WRITE && err == EMSGSIZE)
685 td_verror(td, io_u->error, "xfer");
687 return FIO_Q_COMPLETED;
690 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
692 struct netio_options *o = td->eo;
695 fio_ro_check(td, io_u);
697 ret = __fio_netio_queue(td, io_u, io_u->ddir);
698 if (!o->pingpong || ret != FIO_Q_COMPLETED)
702 * For ping-pong mode, receive or send reply as needed
704 if (td_read(td) && io_u->ddir == DDIR_READ)
705 ret = __fio_netio_queue(td, io_u, DDIR_WRITE);
706 else if (td_write(td) && io_u->ddir == DDIR_WRITE)
707 ret = __fio_netio_queue(td, io_u, DDIR_READ);
712 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
714 struct netio_data *nd = td->io_ops_data;
715 struct netio_options *o = td->eo;
718 if (o->proto == FIO_TYPE_TCP) {
721 } else if (o->proto == FIO_TYPE_TCP_V6) {
724 } else if (o->proto == FIO_TYPE_UDP) {
727 } else if (o->proto == FIO_TYPE_UDP_V6) {
730 } else if (o->proto == FIO_TYPE_UNIX) {
734 log_err("fio: bad network type %d\n", o->proto);
739 f->fd = socket(domain, type, 0);
741 td_verror(td, errno, "socket");
745 #ifdef CONFIG_TCP_NODELAY
746 if (o->nodelay && is_tcp(o)) {
749 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
750 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
756 if (set_window_size(td, f->fd)) {
760 if (set_mss(td, f->fd)) {
766 if (!fio_netio_is_multicast(td->o.filename))
769 log_err("fio: multicast not supported on IPv6\n");
775 struct in_addr interface_addr;
777 if (inet_aton(o->intfc, &interface_addr) == 0) {
778 log_err("fio: interface not valid interface IP\n");
782 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_IF, (const char*)&interface_addr, sizeof(interface_addr)) < 0) {
783 td_verror(td, errno, "setsockopt IP_MULTICAST_IF");
788 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_TTL, (const char*)&o->ttl, sizeof(o->ttl)) < 0) {
789 td_verror(td, errno, "setsockopt IP_MULTICAST_TTL");
794 } else if (o->proto == FIO_TYPE_TCP) {
795 socklen_t len = sizeof(nd->addr);
797 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
798 td_verror(td, errno, "connect");
802 } else if (o->proto == FIO_TYPE_TCP_V6) {
803 socklen_t len = sizeof(nd->addr6);
805 if (connect(f->fd, (struct sockaddr *) &nd->addr6, len) < 0) {
806 td_verror(td, errno, "connect");
812 struct sockaddr_un *addr = &nd->addr_un;
815 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
817 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
818 td_verror(td, errno, "connect");
827 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
829 struct netio_data *nd = td->io_ops_data;
830 struct netio_options *o = td->eo;
835 f->fd = nd->listenfd;
839 state = td->runstate;
840 td_set_runstate(td, TD_SETTING_UP);
842 log_info("fio: waiting for connection\n");
844 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
847 if (o->proto == FIO_TYPE_TCP) {
848 socklen = sizeof(nd->addr);
849 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
851 socklen = sizeof(nd->addr6);
852 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr6, &socklen);
856 td_verror(td, errno, "accept");
860 #ifdef CONFIG_TCP_NODELAY
861 if (o->nodelay && is_tcp(o)) {
864 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
865 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
872 td_set_runstate(td, state);
875 td_set_runstate(td, state);
879 static void fio_netio_send_close(struct thread_data *td, struct fio_file *f)
881 struct netio_data *nd = td->io_ops_data;
882 struct netio_options *o = td->eo;
883 struct udp_close_msg msg;
889 to = (struct sockaddr *) &nd->addr6;
890 len = sizeof(nd->addr6);
892 to = (struct sockaddr *) &nd->addr;
893 len = sizeof(nd->addr);
896 msg.magic = cpu_to_le32((uint32_t) FIO_LINK_OPEN_CLOSE_MAGIC);
897 msg.cmd = cpu_to_le32((uint32_t) FIO_LINK_CLOSE);
899 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
901 td_verror(td, errno, "sendto udp link close");
904 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
907 * Notify the receiver that we are closing down the link
909 fio_netio_send_close(td, f);
911 return generic_close_file(td, f);
914 static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
916 struct netio_data *nd = td->io_ops_data;
917 struct netio_options *o = td->eo;
918 struct udp_close_msg msg;
924 len = sizeof(nd->addr6);
925 to = (struct sockaddr *) &nd->addr6;
927 len = sizeof(nd->addr);
928 to = (struct sockaddr *) &nd->addr;
931 ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
933 td_verror(td, errno, "recvfrom udp link open");
937 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
938 ntohl(msg.cmd) != FIO_LINK_OPEN) {
939 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
944 fio_gettime(&td->start, NULL);
948 static int fio_netio_send_open(struct thread_data *td, struct fio_file *f)
950 struct netio_data *nd = td->io_ops_data;
951 struct netio_options *o = td->eo;
952 struct udp_close_msg msg;
958 len = sizeof(nd->addr6);
959 to = (struct sockaddr *) &nd->addr6;
961 len = sizeof(nd->addr);
962 to = (struct sockaddr *) &nd->addr;
965 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
966 msg.cmd = htonl(FIO_LINK_OPEN);
968 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
970 td_verror(td, errno, "sendto udp link open");
977 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
980 struct netio_options *o = td->eo;
983 ret = fio_netio_accept(td, f);
985 ret = fio_netio_connect(td, f);
994 ret = fio_netio_send_open(td, f);
998 state = td->runstate;
999 td_set_runstate(td, TD_SETTING_UP);
1000 ret = fio_netio_udp_recv_open(td, f);
1001 td_set_runstate(td, state);
1006 fio_netio_close_file(td, f);
1011 static int fio_fill_addr(struct thread_data *td, const char *host, int af,
1012 void *dst, struct addrinfo **res)
1014 struct netio_options *o = td->eo;
1015 struct addrinfo hints;
1018 if (inet_pton(af, host, dst))
1021 memset(&hints, 0, sizeof(hints));
1024 hints.ai_socktype = SOCK_STREAM;
1026 hints.ai_socktype = SOCK_DGRAM;
1029 hints.ai_family = AF_INET6;
1031 hints.ai_family = AF_INET;
1033 ret = getaddrinfo(host, NULL, &hints, res);
1038 if (ret == EAI_SYSTEM)
1041 snprintf(str, sizeof(str), "getaddrinfo: %s", gai_strerror(ret));
1042 td_verror(td, e, str);
1049 static int fio_netio_setup_connect_inet(struct thread_data *td,
1050 const char *host, unsigned short port)
1052 struct netio_data *nd = td->io_ops_data;
1053 struct netio_options *o = td->eo;
1054 struct addrinfo *res = NULL;
1059 log_err("fio: connect with no host to connect to.\n");
1061 log_err("fio: did you forget to set 'listen'?\n");
1063 td_verror(td, EINVAL, "no hostname= set");
1067 nd->addr.sin_family = AF_INET;
1068 nd->addr.sin_port = htons(port);
1069 nd->addr6.sin6_family = AF_INET6;
1070 nd->addr6.sin6_port = htons(port);
1074 dst = &nd->addr6.sin6_addr;
1077 dst = &nd->addr.sin_addr;
1080 if (fio_fill_addr(td, host, af, dst, &res))
1087 len = sizeof(nd->addr6.sin6_addr);
1088 src = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
1090 len = sizeof(nd->addr.sin_addr);
1091 src = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
1094 memcpy(dst, src, len);
1099 static int fio_netio_setup_connect_unix(struct thread_data *td,
1102 struct netio_data *nd = td->io_ops_data;
1103 struct sockaddr_un *soun = &nd->addr_un;
1105 soun->sun_family = AF_UNIX;
1106 memset(soun->sun_path, 0, sizeof(soun->sun_path));
1107 strncpy(soun->sun_path, path, sizeof(soun->sun_path) - 1);
1111 static int fio_netio_setup_connect(struct thread_data *td)
1113 struct netio_options *o = td->eo;
1115 if (is_udp(o) || is_tcp(o))
1116 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
1118 return fio_netio_setup_connect_unix(td, td->o.filename);
1121 static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
1123 struct netio_data *nd = td->io_ops_data;
1124 struct sockaddr_un *addr = &nd->addr_un;
1128 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1130 log_err("fio: socket: %s\n", strerror(errno));
1136 memset(addr, 0, sizeof(*addr));
1137 addr->sun_family = AF_UNIX;
1138 strncpy(addr->sun_path, path, sizeof(addr->sun_path) - 1);
1141 len = sizeof(addr->sun_family) + strlen(path) + 1;
1143 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
1144 log_err("fio: bind: %s\n", strerror(errno));
1154 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
1156 struct netio_data *nd = td->io_ops_data;
1157 struct netio_options *o = td->eo;
1159 struct sockaddr_in sin;
1160 struct sockaddr *saddr;
1161 int fd, opt, type, domain;
1164 memset(&sin, 0, sizeof(sin));
1166 if (o->proto == FIO_TYPE_TCP) {
1169 } else if (o->proto == FIO_TYPE_TCP_V6) {
1172 } else if (o->proto == FIO_TYPE_UDP) {
1175 } else if (o->proto == FIO_TYPE_UDP_V6) {
1179 log_err("fio: unknown proto %d\n", o->proto);
1183 fd = socket(domain, type, 0);
1185 td_verror(td, errno, "socket");
1190 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
1191 td_verror(td, errno, "setsockopt");
1196 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) {
1197 td_verror(td, errno, "setsockopt");
1203 if (set_window_size(td, fd)) {
1207 if (set_mss(td, fd)) {
1212 if (td->o.filename) {
1213 if (!is_udp(o) || !fio_netio_is_multicast(td->o.filename)) {
1214 log_err("fio: hostname not valid for non-multicast inbound network IO\n");
1219 log_err("fio: IPv6 not supported for multicast network IO");
1224 inet_aton(td->o.filename, &sin.sin_addr);
1226 mr.imr_multiaddr = sin.sin_addr;
1228 if (inet_aton(o->intfc, &mr.imr_interface) == 0) {
1229 log_err("fio: interface not valid interface IP\n");
1234 mr.imr_interface.s_addr = htonl(INADDR_ANY);
1237 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mr, sizeof(mr)) < 0) {
1238 td_verror(td, errno, "setsockopt IP_ADD_MEMBERSHIP");
1245 saddr = (struct sockaddr *) &nd->addr;
1246 len = sizeof(nd->addr);
1248 nd->addr.sin_family = AF_INET;
1249 nd->addr.sin_addr.s_addr = sin.sin_addr.s_addr ? sin.sin_addr.s_addr : htonl(INADDR_ANY);
1250 nd->addr.sin_port = htons(port);
1252 saddr = (struct sockaddr *) &nd->addr6;
1253 len = sizeof(nd->addr6);
1255 nd->addr6.sin6_family = AF_INET6;
1256 nd->addr6.sin6_addr = in6addr_any;
1257 nd->addr6.sin6_port = htons(port);
1260 if (bind(fd, saddr, len) < 0) {
1262 td_verror(td, errno, "bind");
1270 static int fio_netio_setup_listen(struct thread_data *td)
1272 struct netio_data *nd = td->io_ops_data;
1273 struct netio_options *o = td->eo;
1276 if (is_udp(o) || is_tcp(o))
1277 ret = fio_netio_setup_listen_inet(td, o->port);
1279 ret = fio_netio_setup_listen_unix(td, td->o.filename);
1286 if (listen(nd->listenfd, 10) < 0) {
1287 td_verror(td, errno, "listen");
1295 static int fio_netio_init(struct thread_data *td)
1297 struct netio_options *o = td->eo;
1302 WSAStartup(MAKEWORD(2,2), &wsd);
1305 if (td_random(td)) {
1306 log_err("fio: network IO can't be random\n");
1310 if (o->proto == FIO_TYPE_UNIX && o->port) {
1311 log_err("fio: network IO port not valid with unix socket\n");
1313 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
1314 log_err("fio: network IO requires port for tcp or udp\n");
1318 o->port += td->subjob_number;
1322 log_err("fio: listen only valid for TCP proto IO\n");
1326 log_err("fio: datagram network connections must be"
1327 " read OR write\n");
1330 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
1331 log_err("fio: UNIX sockets need host/filename\n");
1334 o->listen = td_read(td);
1338 ret = fio_netio_setup_listen(td);
1340 ret = fio_netio_setup_connect(td);
1345 static void fio_netio_cleanup(struct thread_data *td)
1347 struct netio_data *nd = td->io_ops_data;
1350 if (nd->listenfd != -1)
1351 close(nd->listenfd);
1352 if (nd->pipes[0] != -1)
1353 close(nd->pipes[0]);
1354 if (nd->pipes[1] != -1)
1355 close(nd->pipes[1]);
1361 static int fio_netio_setup(struct thread_data *td)
1363 struct netio_data *nd;
1365 if (!td->files_index) {
1366 add_file(td, td->o.filename ?: "net", 0, 0);
1367 td->o.nr_files = td->o.nr_files ?: 1;
1371 if (!td->io_ops_data) {
1372 nd = malloc(sizeof(*nd));;
1374 memset(nd, 0, sizeof(*nd));
1376 nd->pipes[0] = nd->pipes[1] = -1;
1377 td->io_ops_data = nd;
1383 static void fio_netio_terminate(struct thread_data *td)
1385 kill(td->pid, SIGTERM);
1388 #ifdef CONFIG_LINUX_SPLICE
1389 static int fio_netio_setup_splice(struct thread_data *td)
1391 struct netio_data *nd;
1393 fio_netio_setup(td);
1395 nd = td->io_ops_data;
1397 if (pipe(nd->pipes) < 0)
1407 static struct ioengine_ops ioengine_splice = {
1408 .name = "netsplice",
1409 .version = FIO_IOOPS_VERSION,
1410 .prep = fio_netio_prep,
1411 .queue = fio_netio_queue,
1412 .setup = fio_netio_setup_splice,
1413 .init = fio_netio_init,
1414 .cleanup = fio_netio_cleanup,
1415 .open_file = fio_netio_open_file,
1416 .close_file = fio_netio_close_file,
1417 .terminate = fio_netio_terminate,
1419 .option_struct_size = sizeof(struct netio_options),
1420 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1425 static struct ioengine_ops ioengine_rw = {
1427 .version = FIO_IOOPS_VERSION,
1428 .prep = fio_netio_prep,
1429 .queue = fio_netio_queue,
1430 .setup = fio_netio_setup,
1431 .init = fio_netio_init,
1432 .cleanup = fio_netio_cleanup,
1433 .open_file = fio_netio_open_file,
1434 .close_file = fio_netio_close_file,
1435 .terminate = fio_netio_terminate,
1437 .option_struct_size = sizeof(struct netio_options),
1438 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1439 FIO_PIPEIO | FIO_BIT_BASED,
1442 static int str_hostname_cb(void *data, const char *input)
1444 struct netio_options *o = data;
1446 if (o->td->o.filename)
1447 free(o->td->o.filename);
1448 o->td->o.filename = strdup(input);
1452 static void fio_init fio_netio_register(void)
1454 register_ioengine(&ioengine_rw);
1455 #ifdef CONFIG_LINUX_SPLICE
1456 register_ioengine(&ioengine_splice);
1460 static void fio_exit fio_netio_unregister(void)
1462 unregister_ioengine(&ioengine_rw);
1463 #ifdef CONFIG_LINUX_SPLICE
1464 unregister_ioengine(&ioengine_splice);