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"
31 struct sockaddr_in addr;
32 struct sockaddr_in6 addr6;
33 struct sockaddr_un addr_un;
34 uint64_t udp_send_seq;
35 uint64_t udp_recv_seq;
38 struct netio_options {
39 struct thread_data *td;
43 unsigned int pingpong;
46 unsigned int window_size;
51 struct udp_close_msg {
63 FIO_LINK_CLOSE = 0x89,
64 FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b,
66 FIO_UDP_SEQ_MAGIC = 0x657375716e556563ULL,
75 static int str_hostname_cb(void *data, const char *input);
76 static struct fio_option options[] = {
79 .lname = "net engine hostname",
80 .type = FIO_OPT_STR_STORE,
81 .cb = str_hostname_cb,
82 .help = "Hostname for net IO engine",
83 .category = FIO_OPT_C_ENGINE,
84 .group = FIO_OPT_G_NETIO,
88 .lname = "net engine port",
90 .off1 = offsetof(struct netio_options, port),
93 .help = "Port to use for TCP or UDP net connections",
94 .category = FIO_OPT_C_ENGINE,
95 .group = FIO_OPT_G_NETIO,
99 .lname = "net engine protocol",
102 .off1 = offsetof(struct netio_options, proto),
103 .help = "Network protocol to use",
107 .oval = FIO_TYPE_TCP,
108 .help = "Transmission Control Protocol",
112 .oval = FIO_TYPE_TCP_V6,
113 .help = "Transmission Control Protocol V6",
117 .oval = FIO_TYPE_UDP,
118 .help = "User Datagram Protocol",
122 .oval = FIO_TYPE_UDP_V6,
123 .help = "User Datagram Protocol V6",
127 .oval = FIO_TYPE_UNIX,
128 .help = "UNIX domain socket",
131 .category = FIO_OPT_C_ENGINE,
132 .group = FIO_OPT_G_NETIO,
134 #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 .type = FIO_OPT_STR_SET,
156 .off1 = offsetof(struct netio_options, pingpong),
157 .help = "Ping-pong IO requests",
158 .category = FIO_OPT_C_ENGINE,
159 .group = FIO_OPT_G_NETIO,
163 .lname = "net engine interface",
164 .type = FIO_OPT_STR_STORE,
165 .off1 = offsetof(struct netio_options, intfc),
166 .help = "Network interface to use",
167 .category = FIO_OPT_C_ENGINE,
168 .group = FIO_OPT_G_NETIO,
172 .lname = "net engine multicast ttl",
174 .off1 = offsetof(struct netio_options, ttl),
177 .help = "Time-to-live value for outgoing UDP multicast packets",
178 .category = FIO_OPT_C_ENGINE,
179 .group = FIO_OPT_G_NETIO,
181 #ifdef CONFIG_NET_WINDOWSIZE
183 .name = "window_size",
184 .lname = "Window Size",
186 .off1 = offsetof(struct netio_options, window_size),
188 .help = "Set socket buffer window size",
189 .category = FIO_OPT_C_ENGINE,
190 .group = FIO_OPT_G_NETIO,
193 #ifdef CONFIG_NET_MSS
196 .lname = "Maximum segment size",
198 .off1 = offsetof(struct netio_options, mss),
200 .help = "Set TCP maximum segment size",
201 .category = FIO_OPT_C_ENGINE,
202 .group = FIO_OPT_G_NETIO,
210 static inline int is_udp(struct netio_options *o)
212 return o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_UDP_V6;
215 static inline int is_tcp(struct netio_options *o)
217 return o->proto == FIO_TYPE_TCP || o->proto == FIO_TYPE_TCP_V6;
220 static inline int is_ipv6(struct netio_options *o)
222 return o->proto == FIO_TYPE_UDP_V6 || o->proto == FIO_TYPE_TCP_V6;
225 static int set_window_size(struct thread_data *td, int fd)
227 #ifdef CONFIG_NET_WINDOWSIZE
228 struct netio_options *o = td->eo;
235 rcv = o->listen || o->pingpong;
236 snd = !o->listen || o->pingpong;
237 wss = o->window_size;
241 ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void *) &wss,
244 td_verror(td, errno, "rcvbuf window size");
247 ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void *) &wss,
250 td_verror(td, errno, "sndbuf window size");
255 td_verror(td, -EINVAL, "setsockopt window size");
260 static int set_mss(struct thread_data *td, int fd)
262 #ifdef CONFIG_NET_MSS
263 struct netio_options *o = td->eo;
267 if (!o->mss || !is_tcp(o))
271 ret = setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, (void *) &mss,
274 td_verror(td, errno, "setsockopt TCP_MAXSEG");
278 td_verror(td, -EINVAL, "setsockopt TCP_MAXSEG");
285 * Return -1 for error and 'nr events' for a positive number
288 static int poll_wait(struct thread_data *td, int fd, short events)
293 while (!td->terminate) {
296 ret = poll(&pfd, 1, -1);
301 td_verror(td, errno, "poll");
309 if (pfd.revents & events)
315 static int fio_netio_is_multicast(const char *mcaddr)
317 in_addr_t addr = inet_network(mcaddr);
321 if (inet_network("224.0.0.0") <= addr &&
322 inet_network("239.255.255.255") >= addr)
329 static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
331 struct netio_options *o = td->eo;
334 * Make sure we don't see spurious reads to a receiver, and vice versa
339 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
340 (!o->listen && io_u->ddir == DDIR_READ)) {
341 td_verror(td, EINVAL, "bad direction");
348 #ifdef CONFIG_LINUX_SPLICE
349 static int splice_io_u(int fdin, int fdout, unsigned int len)
354 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
372 * Receive bytes from a socket and fill them into the internal pipe
374 static int splice_in(struct thread_data *td, struct io_u *io_u)
376 struct netio_data *nd = td->io_ops->data;
378 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
382 * Transmit 'len' bytes from the internal pipe
384 static int splice_out(struct thread_data *td, struct io_u *io_u,
387 struct netio_data *nd = td->io_ops->data;
389 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
392 static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
395 .iov_base = io_u->xfer_buf,
400 while (iov.iov_len) {
401 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
420 * vmsplice() pipe to io_u buffer
422 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
425 struct netio_data *nd = td->io_ops->data;
427 return vmsplice_io_u(io_u, nd->pipes[0], len);
431 * vmsplice() io_u to pipe
433 static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
435 struct netio_data *nd = td->io_ops->data;
437 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
441 * splice receive - transfer socket data into a pipe using splice, then map
442 * that pipe data into the io_u using vmsplice.
444 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
448 ret = splice_in(td, io_u);
450 return vmsplice_io_u_out(td, io_u, ret);
456 * splice transmit - map data from the io_u into a pipe by using vmsplice,
457 * then transfer that pipe to a socket using splice.
459 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
463 ret = vmsplice_io_u_in(td, io_u);
465 return splice_out(td, io_u, ret);
470 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
476 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
483 static void store_udp_seq(struct netio_data *nd, struct io_u *io_u)
487 us = io_u->xfer_buf + io_u->xfer_buflen - sizeof(*us);
488 us->magic = cpu_to_le64((uint64_t) FIO_UDP_SEQ_MAGIC);
489 us->bs = cpu_to_le64((uint64_t) io_u->xfer_buflen);
490 us->seq = cpu_to_le64(nd->udp_send_seq++);
493 static void verify_udp_seq(struct thread_data *td, struct netio_data *nd,
502 us = io_u->xfer_buf + io_u->xfer_buflen - sizeof(*us);
503 if (le64_to_cpu(us->magic) != FIO_UDP_SEQ_MAGIC)
505 if (le64_to_cpu(us->bs) != io_u->xfer_buflen) {
510 seq = le64_to_cpu(us->seq);
512 if (seq != nd->udp_recv_seq)
513 td->ts.drop_io_u[io_u->ddir] += seq - nd->udp_recv_seq;
515 nd->udp_recv_seq = seq + 1;
518 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
520 struct netio_data *nd = td->io_ops->data;
521 struct netio_options *o = td->eo;
526 const struct sockaddr *to;
530 to = (struct sockaddr *) &nd->addr6;
531 len = sizeof(nd->addr6);
533 to = (struct sockaddr *) &nd->addr;
534 len = sizeof(nd->addr);
537 if (td->o.verify == VERIFY_NONE)
538 store_udp_seq(nd, io_u);
540 ret = sendto(io_u->file->fd, io_u->xfer_buf,
541 io_u->xfer_buflen, flags, to, len);
544 * if we are going to write more, set MSG_MORE
547 if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
548 td->o.size) && !o->pingpong)
551 ret = send(io_u->file->fd, io_u->xfer_buf,
552 io_u->xfer_buflen, flags);
557 ret = poll_wait(td, io_u->file->fd, POLLOUT);
565 static int is_close_msg(struct io_u *io_u, int len)
567 struct udp_close_msg *msg;
569 if (len != sizeof(struct udp_close_msg))
572 msg = io_u->xfer_buf;
573 if (le32_to_cpu(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC)
575 if (le32_to_cpu(msg->cmd) != FIO_LINK_CLOSE)
581 static int fio_netio_recv(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;
589 struct sockaddr *from;
590 socklen_t l, *len = &l;
594 from = (struct sockaddr *) &nd->addr;
595 *len = sizeof(nd->addr);
597 from = (struct sockaddr *) &nd->addr6;
598 *len = sizeof(nd->addr6);
605 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
606 io_u->xfer_buflen, flags, from, len);
608 if (is_close_msg(io_u, ret)) {
613 ret = recv(io_u->file->fd, io_u->xfer_buf,
614 io_u->xfer_buflen, flags);
616 if (is_close_msg(io_u, ret)) {
623 else if (!ret && (flags & MSG_WAITALL))
626 ret = poll_wait(td, io_u->file->fd, POLLIN);
629 flags |= MSG_WAITALL;
632 if (is_udp(o) && td->o.verify == VERIFY_NONE)
633 verify_udp_seq(td, nd, io_u);
638 static int __fio_netio_queue(struct thread_data *td, struct io_u *io_u,
641 struct netio_data *nd = td->io_ops->data;
642 struct netio_options *o = td->eo;
645 if (ddir == DDIR_WRITE) {
646 if (!nd->use_splice || is_udp(o) ||
647 o->proto == FIO_TYPE_UNIX)
648 ret = fio_netio_send(td, io_u);
650 ret = fio_netio_splice_out(td, io_u);
651 } else if (ddir == DDIR_READ) {
652 if (!nd->use_splice || is_udp(o) ||
653 o->proto == FIO_TYPE_UNIX)
654 ret = fio_netio_recv(td, io_u);
656 ret = fio_netio_splice_in(td, io_u);
658 ret = 0; /* must be a SYNC */
660 if (ret != (int) io_u->xfer_buflen) {
662 io_u->resid = io_u->xfer_buflen - ret;
664 return FIO_Q_COMPLETED;
670 if (ddir == DDIR_WRITE && err == EMSGSIZE)
678 td_verror(td, io_u->error, "xfer");
680 return FIO_Q_COMPLETED;
683 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
685 struct netio_options *o = td->eo;
688 fio_ro_check(td, io_u);
690 ret = __fio_netio_queue(td, io_u, io_u->ddir);
691 if (!o->pingpong || ret != FIO_Q_COMPLETED)
695 * For ping-pong mode, receive or send reply as needed
697 if (td_read(td) && io_u->ddir == DDIR_READ)
698 ret = __fio_netio_queue(td, io_u, DDIR_WRITE);
699 else if (td_write(td) && io_u->ddir == DDIR_WRITE)
700 ret = __fio_netio_queue(td, io_u, DDIR_READ);
705 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
707 struct netio_data *nd = td->io_ops->data;
708 struct netio_options *o = td->eo;
711 if (o->proto == FIO_TYPE_TCP) {
714 } else if (o->proto == FIO_TYPE_TCP_V6) {
717 } else if (o->proto == FIO_TYPE_UDP) {
720 } else if (o->proto == FIO_TYPE_UDP_V6) {
723 } else if (o->proto == FIO_TYPE_UNIX) {
727 log_err("fio: bad network type %d\n", o->proto);
732 f->fd = socket(domain, type, 0);
734 td_verror(td, errno, "socket");
738 #ifdef CONFIG_TCP_NODELAY
739 if (o->nodelay && is_tcp(o)) {
742 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
743 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
749 if (set_window_size(td, f->fd)) {
753 if (set_mss(td, f->fd)) {
759 if (!fio_netio_is_multicast(td->o.filename))
762 log_err("fio: multicast not supported on IPv6\n");
768 struct in_addr interface_addr;
770 if (inet_aton(o->intfc, &interface_addr) == 0) {
771 log_err("fio: interface not valid interface IP\n");
775 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_IF, (const char*)&interface_addr, sizeof(interface_addr)) < 0) {
776 td_verror(td, errno, "setsockopt IP_MULTICAST_IF");
781 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_TTL, (const char*)&o->ttl, sizeof(o->ttl)) < 0) {
782 td_verror(td, errno, "setsockopt IP_MULTICAST_TTL");
787 } else if (o->proto == FIO_TYPE_TCP) {
788 socklen_t len = sizeof(nd->addr);
790 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
791 td_verror(td, errno, "connect");
795 } else if (o->proto == FIO_TYPE_TCP_V6) {
796 socklen_t len = sizeof(nd->addr6);
798 if (connect(f->fd, (struct sockaddr *) &nd->addr6, len) < 0) {
799 td_verror(td, errno, "connect");
805 struct sockaddr_un *addr = &nd->addr_un;
808 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
810 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
811 td_verror(td, errno, "connect");
820 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
822 struct netio_data *nd = td->io_ops->data;
823 struct netio_options *o = td->eo;
828 f->fd = nd->listenfd;
832 state = td->runstate;
833 td_set_runstate(td, TD_SETTING_UP);
835 log_info("fio: waiting for connection\n");
837 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
840 if (o->proto == FIO_TYPE_TCP) {
841 socklen = sizeof(nd->addr);
842 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
844 socklen = sizeof(nd->addr6);
845 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr6, &socklen);
849 td_verror(td, errno, "accept");
853 #ifdef CONFIG_TCP_NODELAY
854 if (o->nodelay && is_tcp(o)) {
857 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
858 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
865 td_set_runstate(td, state);
868 td_set_runstate(td, state);
872 static void fio_netio_send_close(struct thread_data *td, struct fio_file *f)
874 struct netio_data *nd = td->io_ops->data;
875 struct netio_options *o = td->eo;
876 struct udp_close_msg msg;
882 to = (struct sockaddr *) &nd->addr6;
883 len = sizeof(nd->addr6);
885 to = (struct sockaddr *) &nd->addr;
886 len = sizeof(nd->addr);
889 msg.magic = cpu_to_le32((uint32_t) FIO_LINK_OPEN_CLOSE_MAGIC);
890 msg.cmd = cpu_to_le32((uint32_t) FIO_LINK_CLOSE);
892 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
894 td_verror(td, errno, "sendto udp link close");
897 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
900 * Notify the receiver that we are closing down the link
902 fio_netio_send_close(td, f);
904 return generic_close_file(td, f);
907 static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
909 struct netio_data *nd = td->io_ops->data;
910 struct netio_options *o = td->eo;
911 struct udp_close_msg msg;
917 len = sizeof(nd->addr6);
918 to = (struct sockaddr *) &nd->addr6;
920 len = sizeof(nd->addr);
921 to = (struct sockaddr *) &nd->addr;
924 ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
926 td_verror(td, errno, "recvfrom udp link open");
930 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
931 ntohl(msg.cmd) != FIO_LINK_OPEN) {
932 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
937 fio_gettime(&td->start, NULL);
941 static int fio_netio_send_open(struct thread_data *td, struct fio_file *f)
943 struct netio_data *nd = td->io_ops->data;
944 struct netio_options *o = td->eo;
945 struct udp_close_msg msg;
951 len = sizeof(nd->addr6);
952 to = (struct sockaddr *) &nd->addr6;
954 len = sizeof(nd->addr);
955 to = (struct sockaddr *) &nd->addr;
958 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
959 msg.cmd = htonl(FIO_LINK_OPEN);
961 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
963 td_verror(td, errno, "sendto udp link open");
970 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
973 struct netio_options *o = td->eo;
976 ret = fio_netio_accept(td, f);
978 ret = fio_netio_connect(td, f);
987 ret = fio_netio_send_open(td, f);
991 state = td->runstate;
992 td_set_runstate(td, TD_SETTING_UP);
993 ret = fio_netio_udp_recv_open(td, f);
994 td_set_runstate(td, state);
999 fio_netio_close_file(td, f);
1004 static int fio_fill_addr(struct thread_data *td, const char *host, int af,
1005 void *dst, struct addrinfo **res)
1007 struct netio_options *o = td->eo;
1008 struct addrinfo hints;
1011 if (inet_pton(af, host, dst))
1014 memset(&hints, 0, sizeof(hints));
1017 hints.ai_socktype = SOCK_STREAM;
1019 hints.ai_socktype = SOCK_DGRAM;
1022 hints.ai_family = AF_INET6;
1024 hints.ai_family = AF_INET;
1026 ret = getaddrinfo(host, NULL, &hints, res);
1031 if (ret == EAI_SYSTEM)
1034 snprintf(str, sizeof(str), "getaddrinfo: %s", gai_strerror(ret));
1035 td_verror(td, e, str);
1042 static int fio_netio_setup_connect_inet(struct thread_data *td,
1043 const char *host, unsigned short port)
1045 struct netio_data *nd = td->io_ops->data;
1046 struct netio_options *o = td->eo;
1047 struct addrinfo *res = NULL;
1052 log_err("fio: connect with no host to connect to.\n");
1054 log_err("fio: did you forget to set 'listen'?\n");
1056 td_verror(td, EINVAL, "no hostname= set");
1060 nd->addr.sin_family = AF_INET;
1061 nd->addr.sin_port = htons(port);
1062 nd->addr6.sin6_family = AF_INET6;
1063 nd->addr6.sin6_port = htons(port);
1067 dst = &nd->addr6.sin6_addr;
1070 dst = &nd->addr.sin_addr;
1073 if (fio_fill_addr(td, host, af, dst, &res))
1080 len = sizeof(nd->addr6.sin6_addr);
1081 src = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
1083 len = sizeof(nd->addr.sin_addr);
1084 src = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
1087 memcpy(dst, src, len);
1092 static int fio_netio_setup_connect_unix(struct thread_data *td,
1095 struct netio_data *nd = td->io_ops->data;
1096 struct sockaddr_un *soun = &nd->addr_un;
1098 soun->sun_family = AF_UNIX;
1099 memset(soun->sun_path, 0, sizeof(soun->sun_path));
1100 strncpy(soun->sun_path, path, sizeof(soun->sun_path) - 1);
1104 static int fio_netio_setup_connect(struct thread_data *td)
1106 struct netio_options *o = td->eo;
1108 if (is_udp(o) || is_tcp(o))
1109 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
1111 return fio_netio_setup_connect_unix(td, td->o.filename);
1114 static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
1116 struct netio_data *nd = td->io_ops->data;
1117 struct sockaddr_un *addr = &nd->addr_un;
1121 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1123 log_err("fio: socket: %s\n", strerror(errno));
1129 memset(addr, 0, sizeof(*addr));
1130 addr->sun_family = AF_UNIX;
1131 strncpy(addr->sun_path, path, sizeof(addr->sun_path) - 1);
1134 len = sizeof(addr->sun_family) + strlen(path) + 1;
1136 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
1137 log_err("fio: bind: %s\n", strerror(errno));
1147 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
1149 struct netio_data *nd = td->io_ops->data;
1150 struct netio_options *o = td->eo;
1152 struct sockaddr_in sin;
1153 struct sockaddr *saddr;
1154 int fd, opt, type, domain;
1157 memset(&sin, 0, sizeof(sin));
1159 if (o->proto == FIO_TYPE_TCP) {
1162 } else if (o->proto == FIO_TYPE_TCP_V6) {
1165 } else if (o->proto == FIO_TYPE_UDP) {
1168 } else if (o->proto == FIO_TYPE_UDP_V6) {
1172 log_err("fio: unknown proto %d\n", o->proto);
1176 fd = socket(domain, type, 0);
1178 td_verror(td, errno, "socket");
1183 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
1184 td_verror(td, errno, "setsockopt");
1189 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) {
1190 td_verror(td, errno, "setsockopt");
1196 if (set_window_size(td, fd)) {
1200 if (set_mss(td, fd)) {
1205 if (td->o.filename) {
1206 if (!is_udp(o) || !fio_netio_is_multicast(td->o.filename)) {
1207 log_err("fio: hostname not valid for non-multicast inbound network IO\n");
1212 log_err("fio: IPv6 not supported for multicast network IO");
1217 inet_aton(td->o.filename, &sin.sin_addr);
1219 mr.imr_multiaddr = sin.sin_addr;
1221 if (inet_aton(o->intfc, &mr.imr_interface) == 0) {
1222 log_err("fio: interface not valid interface IP\n");
1227 mr.imr_interface.s_addr = htonl(INADDR_ANY);
1230 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mr, sizeof(mr)) < 0) {
1231 td_verror(td, errno, "setsockopt IP_ADD_MEMBERSHIP");
1238 saddr = (struct sockaddr *) &nd->addr;
1239 len = sizeof(nd->addr);
1241 nd->addr.sin_family = AF_INET;
1242 nd->addr.sin_addr.s_addr = sin.sin_addr.s_addr ? sin.sin_addr.s_addr : htonl(INADDR_ANY);
1243 nd->addr.sin_port = htons(port);
1245 saddr = (struct sockaddr *) &nd->addr6;
1246 len = sizeof(nd->addr6);
1248 nd->addr6.sin6_family = AF_INET6;
1249 nd->addr6.sin6_addr = in6addr_any;
1250 nd->addr6.sin6_port = htons(port);
1253 if (bind(fd, saddr, len) < 0) {
1255 td_verror(td, errno, "bind");
1263 static int fio_netio_setup_listen(struct thread_data *td)
1265 struct netio_data *nd = td->io_ops->data;
1266 struct netio_options *o = td->eo;
1269 if (is_udp(o) || is_tcp(o))
1270 ret = fio_netio_setup_listen_inet(td, o->port);
1272 ret = fio_netio_setup_listen_unix(td, td->o.filename);
1279 if (listen(nd->listenfd, 10) < 0) {
1280 td_verror(td, errno, "listen");
1288 static int fio_netio_init(struct thread_data *td)
1290 struct netio_options *o = td->eo;
1295 WSAStartup(MAKEWORD(2,2), &wsd);
1298 if (td_random(td)) {
1299 log_err("fio: network IO can't be random\n");
1303 if (o->proto == FIO_TYPE_UNIX && o->port) {
1304 log_err("fio: network IO port not valid with unix socket\n");
1306 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
1307 log_err("fio: network IO requires port for tcp or udp\n");
1311 o->port += td->subjob_number;
1315 log_err("fio: listen only valid for TCP proto IO\n");
1319 log_err("fio: datagram network connections must be"
1320 " read OR write\n");
1323 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
1324 log_err("fio: UNIX sockets need host/filename\n");
1327 o->listen = td_read(td);
1331 ret = fio_netio_setup_listen(td);
1333 ret = fio_netio_setup_connect(td);
1338 static void fio_netio_cleanup(struct thread_data *td)
1340 struct netio_data *nd = td->io_ops->data;
1343 if (nd->listenfd != -1)
1344 close(nd->listenfd);
1345 if (nd->pipes[0] != -1)
1346 close(nd->pipes[0]);
1347 if (nd->pipes[1] != -1)
1348 close(nd->pipes[1]);
1354 static int fio_netio_setup(struct thread_data *td)
1356 struct netio_data *nd;
1358 if (!td->files_index) {
1359 add_file(td, td->o.filename ?: "net", 0, 0);
1360 td->o.nr_files = td->o.nr_files ?: 1;
1364 if (!td->io_ops->data) {
1365 nd = malloc(sizeof(*nd));;
1367 memset(nd, 0, sizeof(*nd));
1369 nd->pipes[0] = nd->pipes[1] = -1;
1370 td->io_ops->data = nd;
1376 static void fio_netio_terminate(struct thread_data *td)
1378 kill(td->pid, SIGTERM);
1381 #ifdef CONFIG_LINUX_SPLICE
1382 static int fio_netio_setup_splice(struct thread_data *td)
1384 struct netio_data *nd;
1386 fio_netio_setup(td);
1388 nd = td->io_ops->data;
1390 if (pipe(nd->pipes) < 0)
1400 static struct ioengine_ops ioengine_splice = {
1401 .name = "netsplice",
1402 .version = FIO_IOOPS_VERSION,
1403 .prep = fio_netio_prep,
1404 .queue = fio_netio_queue,
1405 .setup = fio_netio_setup_splice,
1406 .init = fio_netio_init,
1407 .cleanup = fio_netio_cleanup,
1408 .open_file = fio_netio_open_file,
1409 .close_file = fio_netio_close_file,
1410 .terminate = fio_netio_terminate,
1412 .option_struct_size = sizeof(struct netio_options),
1413 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1418 static struct ioengine_ops ioengine_rw = {
1420 .version = FIO_IOOPS_VERSION,
1421 .prep = fio_netio_prep,
1422 .queue = fio_netio_queue,
1423 .setup = fio_netio_setup,
1424 .init = fio_netio_init,
1425 .cleanup = fio_netio_cleanup,
1426 .open_file = fio_netio_open_file,
1427 .close_file = fio_netio_close_file,
1428 .terminate = fio_netio_terminate,
1430 .option_struct_size = sizeof(struct netio_options),
1431 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1432 FIO_PIPEIO | FIO_BIT_BASED,
1435 static int str_hostname_cb(void *data, const char *input)
1437 struct netio_options *o = data;
1439 if (o->td->o.filename)
1440 free(o->td->o.filename);
1441 o->td->o.filename = strdup(input);
1445 static void fio_init fio_netio_register(void)
1447 register_ioengine(&ioengine_rw);
1448 #ifdef CONFIG_LINUX_SPLICE
1449 register_ioengine(&ioengine_splice);
1453 static void fio_exit fio_netio_unregister(void)
1455 unregister_ioengine(&ioengine_rw);
1456 #ifdef CONFIG_LINUX_SPLICE
1457 unregister_ioengine(&ioengine_splice);