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 <linux/vm_sockets.h>
32 #include "../verify.h"
33 #include "../optgroup.h"
40 struct sockaddr_in addr;
41 struct sockaddr_in6 addr6;
42 struct sockaddr_un addr_un;
43 struct sockaddr_vm addr_vm;
44 uint64_t udp_send_seq;
45 uint64_t udp_recv_seq;
48 struct netio_options {
49 struct thread_data *td;
53 unsigned int pingpong;
56 unsigned int window_size;
61 struct udp_close_msg {
73 FIO_LINK_CLOSE = 0x89,
74 FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b,
76 FIO_UDP_SEQ_MAGIC = 0x657375716e556563ULL,
83 FIO_TYPE_VSOCK_STREAM = 6,
86 static int str_hostname_cb(void *data, const char *input);
87 static struct fio_option options[] = {
90 .lname = "net engine hostname",
91 .type = FIO_OPT_STR_STORE,
92 .cb = str_hostname_cb,
93 .help = "Hostname for net IO engine",
94 .category = FIO_OPT_C_ENGINE,
95 .group = FIO_OPT_G_NETIO,
99 .lname = "net engine port",
101 .off1 = offsetof(struct netio_options, port),
104 .help = "Port to use for TCP or UDP net connections",
105 .category = FIO_OPT_C_ENGINE,
106 .group = FIO_OPT_G_NETIO,
110 .lname = "net engine protocol",
113 .off1 = offsetof(struct netio_options, proto),
114 .help = "Network protocol to use",
118 .oval = FIO_TYPE_TCP,
119 .help = "Transmission Control Protocol",
123 .oval = FIO_TYPE_TCP_V6,
124 .help = "Transmission Control Protocol V6",
128 .oval = FIO_TYPE_UDP,
129 .help = "User Datagram Protocol",
133 .oval = FIO_TYPE_UDP_V6,
134 .help = "User Datagram Protocol V6",
138 .oval = FIO_TYPE_UNIX,
139 .help = "UNIX domain socket",
142 .oval = FIO_TYPE_VSOCK_STREAM,
143 .help = "Virtual socket",
146 .category = FIO_OPT_C_ENGINE,
147 .group = FIO_OPT_G_NETIO,
149 #ifdef CONFIG_TCP_NODELAY
153 .type = FIO_OPT_BOOL,
154 .off1 = offsetof(struct netio_options, nodelay),
155 .help = "Use TCP_NODELAY on TCP connections",
156 .category = FIO_OPT_C_ENGINE,
157 .group = FIO_OPT_G_NETIO,
162 .lname = "net engine listen",
163 .type = FIO_OPT_STR_SET,
164 .off1 = offsetof(struct netio_options, listen),
165 .help = "Listen for incoming TCP connections",
166 .category = FIO_OPT_C_ENGINE,
167 .group = FIO_OPT_G_NETIO,
171 .lname = "Ping Pong",
172 .type = FIO_OPT_STR_SET,
173 .off1 = offsetof(struct netio_options, pingpong),
174 .help = "Ping-pong IO requests",
175 .category = FIO_OPT_C_ENGINE,
176 .group = FIO_OPT_G_NETIO,
180 .lname = "net engine interface",
181 .type = FIO_OPT_STR_STORE,
182 .off1 = offsetof(struct netio_options, intfc),
183 .help = "Network interface to use",
184 .category = FIO_OPT_C_ENGINE,
185 .group = FIO_OPT_G_NETIO,
189 .lname = "net engine multicast ttl",
191 .off1 = offsetof(struct netio_options, ttl),
194 .help = "Time-to-live value for outgoing UDP multicast packets",
195 .category = FIO_OPT_C_ENGINE,
196 .group = FIO_OPT_G_NETIO,
198 #ifdef CONFIG_NET_WINDOWSIZE
200 .name = "window_size",
201 .lname = "Window Size",
203 .off1 = offsetof(struct netio_options, window_size),
205 .help = "Set socket buffer window size",
206 .category = FIO_OPT_C_ENGINE,
207 .group = FIO_OPT_G_NETIO,
210 #ifdef CONFIG_NET_MSS
213 .lname = "Maximum segment size",
215 .off1 = offsetof(struct netio_options, mss),
217 .help = "Set TCP maximum segment size",
218 .category = FIO_OPT_C_ENGINE,
219 .group = FIO_OPT_G_NETIO,
227 static inline int is_udp(struct netio_options *o)
229 return o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_UDP_V6;
232 static inline int is_tcp(struct netio_options *o)
234 return o->proto == FIO_TYPE_TCP || o->proto == FIO_TYPE_TCP_V6;
237 static inline int is_ipv6(struct netio_options *o)
239 return o->proto == FIO_TYPE_UDP_V6 || o->proto == FIO_TYPE_TCP_V6;
242 static inline int is_vsock(struct netio_options *o)
244 return o->proto == FIO_TYPE_VSOCK_STREAM;
247 static int set_window_size(struct thread_data *td, int fd)
249 #ifdef CONFIG_NET_WINDOWSIZE
250 struct netio_options *o = td->eo;
257 rcv = o->listen || o->pingpong;
258 snd = !o->listen || o->pingpong;
259 wss = o->window_size;
263 ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void *) &wss,
266 td_verror(td, errno, "rcvbuf window size");
269 ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void *) &wss,
272 td_verror(td, errno, "sndbuf window size");
277 td_verror(td, -EINVAL, "setsockopt window size");
282 static int set_mss(struct thread_data *td, int fd)
284 #ifdef CONFIG_NET_MSS
285 struct netio_options *o = td->eo;
289 if (!o->mss || !is_tcp(o))
293 ret = setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, (void *) &mss,
296 td_verror(td, errno, "setsockopt TCP_MAXSEG");
300 td_verror(td, -EINVAL, "setsockopt TCP_MAXSEG");
307 * Return -1 for error and 'nr events' for a positive number
310 static int poll_wait(struct thread_data *td, int fd, short events)
315 while (!td->terminate) {
318 ret = poll(&pfd, 1, -1);
323 td_verror(td, errno, "poll");
331 if (pfd.revents & events)
337 static int fio_netio_is_multicast(const char *mcaddr)
339 in_addr_t addr = inet_network(mcaddr);
343 if (inet_network("224.0.0.0") <= addr &&
344 inet_network("239.255.255.255") >= addr)
351 static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
353 struct netio_options *o = td->eo;
356 * Make sure we don't see spurious reads to a receiver, and vice versa
361 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
362 (!o->listen && io_u->ddir == DDIR_READ)) {
363 td_verror(td, EINVAL, "bad direction");
370 #ifdef CONFIG_LINUX_SPLICE
371 static int splice_io_u(int fdin, int fdout, unsigned int len)
376 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
394 * Receive bytes from a socket and fill them into the internal pipe
396 static int splice_in(struct thread_data *td, struct io_u *io_u)
398 struct netio_data *nd = td->io_ops_data;
400 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
404 * Transmit 'len' bytes from the internal pipe
406 static int splice_out(struct thread_data *td, struct io_u *io_u,
409 struct netio_data *nd = td->io_ops_data;
411 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
414 static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
417 .iov_base = io_u->xfer_buf,
422 while (iov.iov_len) {
423 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
442 * vmsplice() pipe to io_u buffer
444 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
447 struct netio_data *nd = td->io_ops_data;
449 return vmsplice_io_u(io_u, nd->pipes[0], len);
453 * vmsplice() io_u to pipe
455 static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
457 struct netio_data *nd = td->io_ops_data;
459 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
463 * splice receive - transfer socket data into a pipe using splice, then map
464 * that pipe data into the io_u using vmsplice.
466 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
470 ret = splice_in(td, io_u);
472 return vmsplice_io_u_out(td, io_u, ret);
478 * splice transmit - map data from the io_u into a pipe by using vmsplice,
479 * then transfer that pipe to a socket using splice.
481 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
485 ret = vmsplice_io_u_in(td, io_u);
487 return splice_out(td, io_u, ret);
492 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
498 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
505 static void store_udp_seq(struct netio_data *nd, struct io_u *io_u)
509 if (io_u->xfer_buflen < sizeof(*us))
512 us = io_u->xfer_buf + io_u->xfer_buflen - sizeof(*us);
513 us->magic = cpu_to_le64((uint64_t) FIO_UDP_SEQ_MAGIC);
514 us->bs = cpu_to_le64((uint64_t) io_u->xfer_buflen);
515 us->seq = cpu_to_le64(nd->udp_send_seq++);
518 static void verify_udp_seq(struct thread_data *td, struct netio_data *nd,
524 if (io_u->xfer_buflen < sizeof(*us))
530 us = io_u->xfer_buf + io_u->xfer_buflen - sizeof(*us);
531 if (le64_to_cpu(us->magic) != FIO_UDP_SEQ_MAGIC)
533 if (le64_to_cpu(us->bs) != io_u->xfer_buflen) {
538 seq = le64_to_cpu(us->seq);
540 if (seq != nd->udp_recv_seq)
541 td->ts.drop_io_u[io_u->ddir] += seq - nd->udp_recv_seq;
543 nd->udp_recv_seq = seq + 1;
546 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
548 struct netio_data *nd = td->io_ops_data;
549 struct netio_options *o = td->eo;
554 const struct sockaddr *to;
558 to = (struct sockaddr *) &nd->addr6;
559 len = sizeof(nd->addr6);
561 to = (struct sockaddr *) &nd->addr;
562 len = sizeof(nd->addr);
565 if (td->o.verify == VERIFY_NONE)
566 store_udp_seq(nd, io_u);
568 ret = sendto(io_u->file->fd, io_u->xfer_buf,
569 io_u->xfer_buflen, flags, to, len);
572 * if we are going to write more, set MSG_MORE
575 if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
576 td->o.size) && !o->pingpong)
579 ret = send(io_u->file->fd, io_u->xfer_buf,
580 io_u->xfer_buflen, flags);
585 ret = poll_wait(td, io_u->file->fd, POLLOUT);
593 static int is_close_msg(struct io_u *io_u, int len)
595 struct udp_close_msg *msg;
597 if (len != sizeof(struct udp_close_msg))
600 msg = io_u->xfer_buf;
601 if (le32_to_cpu(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC)
603 if (le32_to_cpu(msg->cmd) != FIO_LINK_CLOSE)
609 static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
611 struct netio_data *nd = td->io_ops_data;
612 struct netio_options *o = td->eo;
617 struct sockaddr *from;
618 socklen_t l, *len = &l;
622 from = (struct sockaddr *) &nd->addr;
623 *len = sizeof(nd->addr);
625 from = (struct sockaddr *) &nd->addr6;
626 *len = sizeof(nd->addr6);
633 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
634 io_u->xfer_buflen, flags, from, len);
636 if (is_close_msg(io_u, ret)) {
641 ret = recv(io_u->file->fd, io_u->xfer_buf,
642 io_u->xfer_buflen, flags);
644 if (is_close_msg(io_u, ret)) {
651 else if (!ret && (flags & MSG_WAITALL))
654 ret = poll_wait(td, io_u->file->fd, POLLIN);
657 flags |= MSG_WAITALL;
660 if (is_udp(o) && td->o.verify == VERIFY_NONE)
661 verify_udp_seq(td, nd, io_u);
666 static enum fio_q_status __fio_netio_queue(struct thread_data *td,
670 struct netio_data *nd = td->io_ops_data;
671 struct netio_options *o = td->eo;
674 if (ddir == DDIR_WRITE) {
675 if (!nd->use_splice || is_udp(o) ||
676 o->proto == FIO_TYPE_UNIX)
677 ret = fio_netio_send(td, io_u);
679 ret = fio_netio_splice_out(td, io_u);
680 } else if (ddir == DDIR_READ) {
681 if (!nd->use_splice || is_udp(o) ||
682 o->proto == FIO_TYPE_UNIX)
683 ret = fio_netio_recv(td, io_u);
685 ret = fio_netio_splice_in(td, io_u);
687 ret = 0; /* must be a SYNC */
689 if (ret != (int) io_u->xfer_buflen) {
691 io_u->resid = io_u->xfer_buflen - ret;
693 return FIO_Q_COMPLETED;
699 if (ddir == DDIR_WRITE && err == EMSGSIZE)
707 td_verror(td, io_u->error, "xfer");
709 return FIO_Q_COMPLETED;
712 static enum fio_q_status fio_netio_queue(struct thread_data *td,
715 struct netio_options *o = td->eo;
718 fio_ro_check(td, io_u);
720 ret = __fio_netio_queue(td, io_u, io_u->ddir);
721 if (!o->pingpong || ret != FIO_Q_COMPLETED)
725 * For ping-pong mode, receive or send reply as needed
727 if (td_read(td) && io_u->ddir == DDIR_READ)
728 ret = __fio_netio_queue(td, io_u, DDIR_WRITE);
729 else if (td_write(td) && io_u->ddir == DDIR_WRITE)
730 ret = __fio_netio_queue(td, io_u, DDIR_READ);
735 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
737 struct netio_data *nd = td->io_ops_data;
738 struct netio_options *o = td->eo;
741 if (o->proto == FIO_TYPE_TCP) {
744 } else if (o->proto == FIO_TYPE_TCP_V6) {
747 } else if (o->proto == FIO_TYPE_UDP) {
750 } else if (o->proto == FIO_TYPE_UDP_V6) {
753 } else if (o->proto == FIO_TYPE_UNIX) {
756 } else if (is_vsock(o)) {
760 log_err("fio: bad network type %d\n", o->proto);
765 f->fd = socket(domain, type, 0);
767 td_verror(td, errno, "socket");
771 #ifdef CONFIG_TCP_NODELAY
772 if (o->nodelay && is_tcp(o)) {
775 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
776 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
782 if (set_window_size(td, f->fd)) {
786 if (set_mss(td, f->fd)) {
792 if (!fio_netio_is_multicast(td->o.filename))
795 log_err("fio: multicast not supported on IPv6\n");
801 struct in_addr interface_addr;
803 if (inet_aton(o->intfc, &interface_addr) == 0) {
804 log_err("fio: interface not valid interface IP\n");
808 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_IF, (const char*)&interface_addr, sizeof(interface_addr)) < 0) {
809 td_verror(td, errno, "setsockopt IP_MULTICAST_IF");
814 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_TTL, (const char*)&o->ttl, sizeof(o->ttl)) < 0) {
815 td_verror(td, errno, "setsockopt IP_MULTICAST_TTL");
820 } else if (o->proto == FIO_TYPE_TCP) {
821 socklen_t len = sizeof(nd->addr);
823 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
824 td_verror(td, errno, "connect");
828 } else if (o->proto == FIO_TYPE_TCP_V6) {
829 socklen_t len = sizeof(nd->addr6);
831 if (connect(f->fd, (struct sockaddr *) &nd->addr6, len) < 0) {
832 td_verror(td, errno, "connect");
836 } else if (is_vsock(o)) {
837 socklen_t len = sizeof(nd->addr_vm);
839 if (connect(f->fd, (struct sockaddr *) &nd->addr_vm, len) < 0) {
840 td_verror(td, errno, "connect");
845 struct sockaddr_un *addr = &nd->addr_un;
848 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
850 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
851 td_verror(td, errno, "connect");
860 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
862 struct netio_data *nd = td->io_ops_data;
863 struct netio_options *o = td->eo;
868 f->fd = nd->listenfd;
872 state = td->runstate;
873 td_set_runstate(td, TD_SETTING_UP);
875 log_info("fio: waiting for connection\n");
877 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
880 if (o->proto == FIO_TYPE_TCP) {
881 socklen = sizeof(nd->addr);
882 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
883 } else if (is_vsock(o)) {
884 socklen = sizeof(nd->addr_vm);
885 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr_vm, &socklen);
887 socklen = sizeof(nd->addr6);
888 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr6, &socklen);
892 td_verror(td, errno, "accept");
896 #ifdef CONFIG_TCP_NODELAY
897 if (o->nodelay && is_tcp(o)) {
900 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
901 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
908 td_set_runstate(td, state);
911 td_set_runstate(td, state);
915 static void fio_netio_send_close(struct thread_data *td, struct fio_file *f)
917 struct netio_data *nd = td->io_ops_data;
918 struct netio_options *o = td->eo;
919 struct udp_close_msg msg;
925 to = (struct sockaddr *) &nd->addr6;
926 len = sizeof(nd->addr6);
927 } else if (is_vsock(o)) {
931 to = (struct sockaddr *) &nd->addr;
932 len = sizeof(nd->addr);
935 msg.magic = cpu_to_le32((uint32_t) FIO_LINK_OPEN_CLOSE_MAGIC);
936 msg.cmd = cpu_to_le32((uint32_t) FIO_LINK_CLOSE);
938 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
940 td_verror(td, errno, "sendto udp link close");
943 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
946 * Notify the receiver that we are closing down the link
948 fio_netio_send_close(td, f);
950 return generic_close_file(td, f);
953 static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
955 struct netio_data *nd = td->io_ops_data;
956 struct netio_options *o = td->eo;
957 struct udp_close_msg msg;
963 len = sizeof(nd->addr6);
964 to = (struct sockaddr *) &nd->addr6;
966 len = sizeof(nd->addr);
967 to = (struct sockaddr *) &nd->addr;
970 ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
972 td_verror(td, errno, "recvfrom udp link open");
976 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
977 ntohl(msg.cmd) != FIO_LINK_OPEN) {
978 log_err("fio: bad udp open magic %x/%x\n",
979 (unsigned int) ntohl(msg.magic),
980 (unsigned int) ntohl(msg.cmd));
984 fio_gettime(&td->start, NULL);
988 static int fio_netio_send_open(struct thread_data *td, struct fio_file *f)
990 struct netio_data *nd = td->io_ops_data;
991 struct netio_options *o = td->eo;
992 struct udp_close_msg msg;
998 len = sizeof(nd->addr6);
999 to = (struct sockaddr *) &nd->addr6;
1000 } else if (is_vsock(o)) {
1001 len = sizeof(nd->addr_vm);
1002 to = (struct sockaddr *) &nd->addr_vm;
1004 len = sizeof(nd->addr);
1005 to = (struct sockaddr *) &nd->addr;
1008 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
1009 msg.cmd = htonl(FIO_LINK_OPEN);
1011 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
1013 td_verror(td, errno, "sendto udp link open");
1020 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
1023 struct netio_options *o = td->eo;
1026 ret = fio_netio_accept(td, f);
1028 ret = fio_netio_connect(td, f);
1037 ret = fio_netio_send_open(td, f);
1041 state = td->runstate;
1042 td_set_runstate(td, TD_SETTING_UP);
1043 ret = fio_netio_udp_recv_open(td, f);
1044 td_set_runstate(td, state);
1049 fio_netio_close_file(td, f);
1054 static int fio_fill_addr(struct thread_data *td, const char *host, int af,
1055 void *dst, struct addrinfo **res)
1057 struct netio_options *o = td->eo;
1058 struct addrinfo hints;
1061 if (inet_pton(af, host, dst))
1064 memset(&hints, 0, sizeof(hints));
1066 if (is_tcp(o) || is_vsock(o))
1067 hints.ai_socktype = SOCK_STREAM;
1069 hints.ai_socktype = SOCK_DGRAM;
1072 hints.ai_family = AF_INET6;
1074 else if (is_vsock(o))
1075 hints.ai_family = AF_VSOCK;
1078 hints.ai_family = AF_INET;
1080 ret = getaddrinfo(host, NULL, &hints, res);
1085 if (ret == EAI_SYSTEM)
1088 snprintf(str, sizeof(str), "getaddrinfo: %s", gai_strerror(ret));
1089 td_verror(td, e, str);
1096 static int fio_netio_setup_connect_inet(struct thread_data *td,
1097 const char *host, unsigned short port)
1099 struct netio_data *nd = td->io_ops_data;
1100 struct netio_options *o = td->eo;
1101 struct addrinfo *res = NULL;
1106 log_err("fio: connect with no host to connect to.\n");
1108 log_err("fio: did you forget to set 'listen'?\n");
1110 td_verror(td, EINVAL, "no hostname= set");
1114 nd->addr.sin_family = AF_INET;
1115 nd->addr.sin_port = htons(port);
1116 nd->addr6.sin6_family = AF_INET6;
1117 nd->addr6.sin6_port = htons(port);
1121 dst = &nd->addr6.sin6_addr;
1124 dst = &nd->addr.sin_addr;
1127 if (fio_fill_addr(td, host, af, dst, &res))
1134 len = sizeof(nd->addr6.sin6_addr);
1135 src = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
1137 len = sizeof(nd->addr.sin_addr);
1138 src = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
1141 memcpy(dst, src, len);
1146 static int fio_netio_setup_connect_unix(struct thread_data *td,
1149 struct netio_data *nd = td->io_ops_data;
1150 struct sockaddr_un *soun = &nd->addr_un;
1152 soun->sun_family = AF_UNIX;
1153 snprintf(soun->sun_path, sizeof(soun->sun_path), "%s", path);
1157 static int fio_netio_setup_connect_vsock(struct thread_data *td,
1158 const char *host, unsigned short port)
1161 struct netio_data *nd = td->io_ops_data;
1162 struct sockaddr_vm *addr = &nd->addr_vm;
1166 log_err("fio: connect with no host to connect to.\n");
1168 log_err("fio: did you forget to set 'listen'?\n");
1170 td_verror(td, EINVAL, "no hostname= set");
1174 addr->svm_family = AF_VSOCK;
1175 addr->svm_port = port;
1179 if (cid < 0 || cid > UINT32_MAX) {
1180 log_err("fio: invalid CID %d\n", cid);
1183 addr->svm_cid = cid;
1188 td_verror(td, -EINVAL, "vsock not supported");
1193 static int fio_netio_setup_connect(struct thread_data *td)
1195 struct netio_options *o = td->eo;
1197 if (is_udp(o) || is_tcp(o))
1198 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
1199 else if (is_vsock(o))
1200 return fio_netio_setup_connect_vsock(td, td->o.filename, o->port);
1202 return fio_netio_setup_connect_unix(td, td->o.filename);
1205 static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
1207 struct netio_data *nd = td->io_ops_data;
1208 struct sockaddr_un *addr = &nd->addr_un;
1212 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1214 log_err("fio: socket: %s\n", strerror(errno));
1220 addr->sun_family = AF_UNIX;
1221 snprintf(addr->sun_path, sizeof(addr->sun_path), "%s", path);
1224 len = sizeof(addr->sun_family) + strlen(path) + 1;
1226 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
1227 log_err("fio: bind: %s\n", strerror(errno));
1237 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
1239 struct netio_data *nd = td->io_ops_data;
1240 struct netio_options *o = td->eo;
1242 struct sockaddr_in sin;
1243 struct sockaddr *saddr;
1244 int fd, opt, type, domain;
1247 memset(&sin, 0, sizeof(sin));
1249 if (o->proto == FIO_TYPE_TCP) {
1252 } else if (o->proto == FIO_TYPE_TCP_V6) {
1255 } else if (o->proto == FIO_TYPE_UDP) {
1258 } else if (o->proto == FIO_TYPE_UDP_V6) {
1262 log_err("fio: unknown proto %d\n", o->proto);
1266 fd = socket(domain, type, 0);
1268 td_verror(td, errno, "socket");
1273 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
1274 td_verror(td, errno, "setsockopt");
1279 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) {
1280 td_verror(td, errno, "setsockopt");
1286 if (set_window_size(td, fd)) {
1290 if (set_mss(td, fd)) {
1295 if (td->o.filename) {
1296 if (!is_udp(o) || !fio_netio_is_multicast(td->o.filename)) {
1297 log_err("fio: hostname not valid for non-multicast inbound network IO\n");
1302 log_err("fio: IPv6 not supported for multicast network IO\n");
1307 inet_aton(td->o.filename, &sin.sin_addr);
1309 mr.imr_multiaddr = sin.sin_addr;
1311 if (inet_aton(o->intfc, &mr.imr_interface) == 0) {
1312 log_err("fio: interface not valid interface IP\n");
1317 mr.imr_interface.s_addr = htonl(INADDR_ANY);
1320 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mr, sizeof(mr)) < 0) {
1321 td_verror(td, errno, "setsockopt IP_ADD_MEMBERSHIP");
1328 saddr = (struct sockaddr *) &nd->addr;
1329 len = sizeof(nd->addr);
1331 nd->addr.sin_family = AF_INET;
1332 nd->addr.sin_addr.s_addr = sin.sin_addr.s_addr ? sin.sin_addr.s_addr : htonl(INADDR_ANY);
1333 nd->addr.sin_port = htons(port);
1335 saddr = (struct sockaddr *) &nd->addr6;
1336 len = sizeof(nd->addr6);
1338 nd->addr6.sin6_family = AF_INET6;
1339 nd->addr6.sin6_addr = in6addr_any;
1340 nd->addr6.sin6_port = htons(port);
1343 if (bind(fd, saddr, len) < 0) {
1345 td_verror(td, errno, "bind");
1353 static int fio_netio_setup_listen_vsock(struct thread_data *td, short port, int type)
1356 struct netio_data *nd = td->io_ops_data;
1357 struct sockaddr_vm *addr = &nd->addr_vm;
1361 fd = socket(AF_VSOCK, type, 0);
1363 td_verror(td, errno, "socket");
1368 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
1369 td_verror(td, errno, "setsockopt");
1374 len = sizeof(*addr);
1376 nd->addr_vm.svm_family = AF_VSOCK;
1377 nd->addr_vm.svm_cid = VMADDR_CID_ANY;
1378 nd->addr_vm.svm_port = port;
1380 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
1381 td_verror(td, errno, "bind");
1389 td_verror(td, -EINVAL, "vsock not supported");
1394 static int fio_netio_setup_listen(struct thread_data *td)
1396 struct netio_data *nd = td->io_ops_data;
1397 struct netio_options *o = td->eo;
1400 if (is_udp(o) || is_tcp(o))
1401 ret = fio_netio_setup_listen_inet(td, o->port);
1402 else if (is_vsock(o))
1403 ret = fio_netio_setup_listen_vsock(td, o->port, SOCK_STREAM);
1405 ret = fio_netio_setup_listen_unix(td, td->o.filename);
1412 if (listen(nd->listenfd, 10) < 0) {
1413 td_verror(td, errno, "listen");
1421 static int fio_netio_init(struct thread_data *td)
1423 struct netio_options *o = td->eo;
1428 WSAStartup(MAKEWORD(2,2), &wsd);
1431 if (td_random(td)) {
1432 log_err("fio: network IO can't be random\n");
1436 if (o->proto == FIO_TYPE_UNIX && o->port) {
1437 log_err("fio: network IO port not valid with unix socket\n");
1439 } else if (is_vsock(o) && !o->port) {
1440 log_err("fio: network IO requires port for vsock\n");
1442 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
1443 log_err("fio: network IO requires port for tcp or udp\n");
1447 o->port += td->subjob_number;
1449 if (!is_tcp(o) && !is_vsock(o)) {
1451 log_err("fio: listen only valid for TCP proto IO\n");
1455 log_err("fio: datagram network connections must be"
1456 " read OR write\n");
1459 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
1460 log_err("fio: UNIX sockets need host/filename\n");
1463 o->listen = td_read(td);
1467 ret = fio_netio_setup_listen(td);
1469 ret = fio_netio_setup_connect(td);
1474 static void fio_netio_cleanup(struct thread_data *td)
1476 struct netio_data *nd = td->io_ops_data;
1479 if (nd->listenfd != -1)
1480 close(nd->listenfd);
1481 if (nd->pipes[0] != -1)
1482 close(nd->pipes[0]);
1483 if (nd->pipes[1] != -1)
1484 close(nd->pipes[1]);
1490 static int fio_netio_setup(struct thread_data *td)
1492 struct netio_data *nd;
1494 if (!td->files_index) {
1495 add_file(td, td->o.filename ?: "net", 0, 0);
1496 td->o.nr_files = td->o.nr_files ?: 1;
1500 if (!td->io_ops_data) {
1501 nd = calloc(1, sizeof(*nd));
1503 nd->pipes[0] = nd->pipes[1] = -1;
1504 td->io_ops_data = nd;
1510 static void fio_netio_terminate(struct thread_data *td)
1512 kill(td->pid, SIGTERM);
1515 #ifdef CONFIG_LINUX_SPLICE
1516 static int fio_netio_setup_splice(struct thread_data *td)
1518 struct netio_data *nd;
1520 fio_netio_setup(td);
1522 nd = td->io_ops_data;
1524 if (pipe(nd->pipes) < 0)
1534 static struct ioengine_ops ioengine_splice = {
1535 .name = "netsplice",
1536 .version = FIO_IOOPS_VERSION,
1537 .prep = fio_netio_prep,
1538 .queue = fio_netio_queue,
1539 .setup = fio_netio_setup_splice,
1540 .init = fio_netio_init,
1541 .cleanup = fio_netio_cleanup,
1542 .open_file = fio_netio_open_file,
1543 .close_file = fio_netio_close_file,
1544 .terminate = fio_netio_terminate,
1546 .option_struct_size = sizeof(struct netio_options),
1547 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1552 static struct ioengine_ops ioengine_rw = {
1554 .version = FIO_IOOPS_VERSION,
1555 .prep = fio_netio_prep,
1556 .queue = fio_netio_queue,
1557 .setup = fio_netio_setup,
1558 .init = fio_netio_init,
1559 .cleanup = fio_netio_cleanup,
1560 .open_file = fio_netio_open_file,
1561 .close_file = fio_netio_close_file,
1562 .terminate = fio_netio_terminate,
1564 .option_struct_size = sizeof(struct netio_options),
1565 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1566 FIO_PIPEIO | FIO_BIT_BASED,
1569 static int str_hostname_cb(void *data, const char *input)
1571 struct netio_options *o = data;
1573 if (o->td->o.filename)
1574 free(o->td->o.filename);
1575 o->td->o.filename = strdup(input);
1579 static void fio_init fio_netio_register(void)
1581 register_ioengine(&ioengine_rw);
1582 #ifdef CONFIG_LINUX_SPLICE
1583 register_ioengine(&ioengine_splice);
1587 static void fio_exit fio_netio_unregister(void)
1589 unregister_ioengine(&ioengine_rw);
1590 #ifdef CONFIG_LINUX_SPLICE
1591 unregister_ioengine(&ioengine_splice);