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>
29 struct sockaddr_in addr;
30 struct sockaddr_un addr_un;
33 struct netio_options {
34 struct thread_data *td;
38 unsigned int pingpong;
44 struct udp_close_msg {
50 FIO_LINK_CLOSE = 0x89,
51 FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b,
59 static int str_hostname_cb(void *data, const char *input);
60 static struct fio_option options[] = {
63 .lname = "net engine hostname",
64 .type = FIO_OPT_STR_STORE,
65 .cb = str_hostname_cb,
66 .help = "Hostname for net IO engine",
67 .category = FIO_OPT_C_ENGINE,
68 .group = FIO_OPT_G_NETIO,
72 .lname = "net engine port",
74 .off1 = offsetof(struct netio_options, port),
77 .help = "Port to use for TCP or UDP net connections",
78 .category = FIO_OPT_C_ENGINE,
79 .group = FIO_OPT_G_NETIO,
83 .lname = "net engine protocol",
86 .off1 = offsetof(struct netio_options, proto),
87 .help = "Network protocol to use",
92 .help = "Transmission Control Protocol",
96 .help = "User Datagram Protocol",
99 .oval = FIO_TYPE_UNIX,
100 .help = "UNIX domain socket",
103 .category = FIO_OPT_C_ENGINE,
104 .group = FIO_OPT_G_NETIO,
106 #ifdef CONFIG_TCP_NODELAY
109 .type = FIO_OPT_BOOL,
110 .off1 = offsetof(struct netio_options, nodelay),
111 .help = "Use TCP_NODELAY on TCP connections",
112 .category = FIO_OPT_C_ENGINE,
113 .group = FIO_OPT_G_NETIO,
118 .lname = "net engine listen",
119 .type = FIO_OPT_STR_SET,
120 .off1 = offsetof(struct netio_options, listen),
121 .help = "Listen for incoming TCP connections",
122 .category = FIO_OPT_C_ENGINE,
123 .group = FIO_OPT_G_NETIO,
127 .type = FIO_OPT_STR_SET,
128 .off1 = offsetof(struct netio_options, pingpong),
129 .help = "Ping-pong IO requests",
130 .category = FIO_OPT_C_ENGINE,
131 .group = FIO_OPT_G_NETIO,
135 .lname = "net engine interface",
136 .type = FIO_OPT_STR_STORE,
137 .off1 = offsetof(struct netio_options, interface),
138 .help = "Network interface to use",
139 .category = FIO_OPT_C_ENGINE,
140 .group = FIO_OPT_G_NETIO,
144 .lname = "net engine multicast ttl",
146 .off1 = offsetof(struct netio_options, ttl),
149 .help = "Time-to-live value for outgoing UDP multicast packets",
150 .category = FIO_OPT_C_ENGINE,
151 .group = FIO_OPT_G_NETIO,
159 * Return -1 for error and 'nr events' for a positive number
162 static int poll_wait(struct thread_data *td, int fd, short events)
167 while (!td->terminate) {
170 ret = poll(&pfd, 1, -1);
175 td_verror(td, errno, "poll");
183 if (pfd.revents & events)
189 static int fio_netio_is_multicast(const char *mcaddr)
191 in_addr_t addr = inet_network(mcaddr);
195 if (inet_network("224.0.0.0") <= addr &&
196 inet_network("239.255.255.255") >= addr)
203 static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
205 struct netio_options *o = td->eo;
208 * Make sure we don't see spurious reads to a receiver, and vice versa
210 if (o->proto == FIO_TYPE_TCP)
213 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
214 (!o->listen && io_u->ddir == DDIR_READ)) {
215 td_verror(td, EINVAL, "bad direction");
222 #ifdef CONFIG_LINUX_SPLICE
223 static int splice_io_u(int fdin, int fdout, unsigned int len)
228 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
246 * Receive bytes from a socket and fill them into the internal pipe
248 static int splice_in(struct thread_data *td, struct io_u *io_u)
250 struct netio_data *nd = td->io_ops->data;
252 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
256 * Transmit 'len' bytes from the internal pipe
258 static int splice_out(struct thread_data *td, struct io_u *io_u,
261 struct netio_data *nd = td->io_ops->data;
263 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
266 static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
269 .iov_base = io_u->xfer_buf,
274 while (iov.iov_len) {
275 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
294 * vmsplice() pipe to io_u buffer
296 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
299 struct netio_data *nd = td->io_ops->data;
301 return vmsplice_io_u(io_u, nd->pipes[0], len);
305 * vmsplice() io_u to pipe
307 static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
309 struct netio_data *nd = td->io_ops->data;
311 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
315 * splice receive - transfer socket data into a pipe using splice, then map
316 * that pipe data into the io_u using vmsplice.
318 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
322 ret = splice_in(td, io_u);
324 return vmsplice_io_u_out(td, io_u, ret);
330 * splice transmit - map data from the io_u into a pipe by using vmsplice,
331 * then transfer that pipe to a socket using splice.
333 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
337 ret = vmsplice_io_u_in(td, io_u);
339 return splice_out(td, io_u, ret);
344 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
350 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
357 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
359 struct netio_data *nd = td->io_ops->data;
360 struct netio_options *o = td->eo;
364 if (o->proto == FIO_TYPE_UDP) {
365 struct sockaddr *to = (struct sockaddr *) &nd->addr;
367 ret = sendto(io_u->file->fd, io_u->xfer_buf,
368 io_u->xfer_buflen, flags, to,
372 * if we are going to write more, set MSG_MORE
375 if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
376 td->o.size) && !o->pingpong)
379 ret = send(io_u->file->fd, io_u->xfer_buf,
380 io_u->xfer_buflen, flags);
385 ret = poll_wait(td, io_u->file->fd, POLLOUT);
393 static int is_udp_close(struct io_u *io_u, int len)
395 struct udp_close_msg *msg;
397 if (len != sizeof(struct udp_close_msg))
400 msg = io_u->xfer_buf;
401 if (ntohl(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC)
403 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
409 static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
411 struct netio_data *nd = td->io_ops->data;
412 struct netio_options *o = td->eo;
416 if (o->proto == FIO_TYPE_UDP) {
419 struct sockaddr *from;
422 from = (struct sockaddr *) &nd->addr;
423 *len = sizeof(nd->addr);
429 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
430 io_u->xfer_buflen, flags, from, len);
431 if (is_udp_close(io_u, ret)) {
436 ret = recv(io_u->file->fd, io_u->xfer_buf,
437 io_u->xfer_buflen, flags);
441 else if (!ret && (flags & MSG_WAITALL))
444 ret = poll_wait(td, io_u->file->fd, POLLIN);
447 flags |= MSG_WAITALL;
453 static int __fio_netio_queue(struct thread_data *td, struct io_u *io_u,
456 struct netio_data *nd = td->io_ops->data;
457 struct netio_options *o = td->eo;
460 if (ddir == DDIR_WRITE) {
461 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
462 o->proto == FIO_TYPE_UNIX)
463 ret = fio_netio_send(td, io_u);
465 ret = fio_netio_splice_out(td, io_u);
466 } else if (ddir == DDIR_READ) {
467 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
468 o->proto == FIO_TYPE_UNIX)
469 ret = fio_netio_recv(td, io_u);
471 ret = fio_netio_splice_in(td, io_u);
473 ret = 0; /* must be a SYNC */
475 if (ret != (int) io_u->xfer_buflen) {
477 io_u->resid = io_u->xfer_buflen - ret;
479 return FIO_Q_COMPLETED;
483 if (ddir == DDIR_WRITE && err == EMSGSIZE)
491 td_verror(td, io_u->error, "xfer");
493 return FIO_Q_COMPLETED;
496 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
498 struct netio_options *o = td->eo;
501 fio_ro_check(td, io_u);
503 ret = __fio_netio_queue(td, io_u, io_u->ddir);
504 if (!o->pingpong || ret != FIO_Q_COMPLETED)
508 * For ping-pong mode, receive or send reply as needed
510 if (td_read(td) && io_u->ddir == DDIR_READ)
511 ret = __fio_netio_queue(td, io_u, DDIR_WRITE);
512 else if (td_write(td) && io_u->ddir == DDIR_WRITE)
513 ret = __fio_netio_queue(td, io_u, DDIR_READ);
518 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
520 struct netio_data *nd = td->io_ops->data;
521 struct netio_options *o = td->eo;
524 if (o->proto == FIO_TYPE_TCP) {
527 } else if (o->proto == FIO_TYPE_UDP) {
530 } else if (o->proto == FIO_TYPE_UNIX) {
534 log_err("fio: bad network type %d\n", o->proto);
539 f->fd = socket(domain, type, 0);
541 td_verror(td, errno, "socket");
545 #ifdef CONFIG_TCP_NODELAY
546 if (o->nodelay && o->proto == FIO_TYPE_TCP) {
549 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
550 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
556 if (o->proto == FIO_TYPE_UDP) {
557 if (!fio_netio_is_multicast(td->o.filename))
561 struct in_addr interface_addr;
562 if (inet_aton(o->interface, &interface_addr) == 0) {
563 log_err("fio: interface not valid interface IP\n");
567 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_IF, &interface_addr, sizeof(interface_addr)) < 0) {
568 td_verror(td, errno, "setsockopt IP_MULTICAST_IF");
573 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_TTL, &o->ttl, sizeof(o->ttl)) < 0) {
574 td_verror(td, errno, "setsockopt IP_MULTICAST_TTL");
579 } else if (o->proto == FIO_TYPE_TCP) {
580 socklen_t len = sizeof(nd->addr);
582 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
583 td_verror(td, errno, "connect");
588 struct sockaddr_un *addr = &nd->addr_un;
591 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
593 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
594 td_verror(td, errno, "connect");
603 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
605 struct netio_data *nd = td->io_ops->data;
606 struct netio_options *o = td->eo;
607 socklen_t socklen = sizeof(nd->addr);
610 if (o->proto == FIO_TYPE_UDP) {
611 f->fd = nd->listenfd;
615 state = td->runstate;
616 td_set_runstate(td, TD_SETTING_UP);
618 log_info("fio: waiting for connection\n");
620 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
623 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
625 td_verror(td, errno, "accept");
629 #ifdef CONFIG_TCP_NODELAY
630 if (o->nodelay && o->proto == FIO_TYPE_TCP) {
633 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
634 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
641 td_set_runstate(td, state);
644 td_set_runstate(td, state);
648 static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
650 struct netio_data *nd = td->io_ops->data;
651 struct udp_close_msg msg;
652 struct sockaddr *to = (struct sockaddr *) &nd->addr;
655 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
656 msg.cmd = htonl(FIO_LINK_CLOSE);
658 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to,
661 td_verror(td, errno, "sendto udp link close");
664 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
666 struct netio_options *o = td->eo;
669 * If this is an UDP connection, notify the receiver that we are
670 * closing down the link
672 if (o->proto == FIO_TYPE_UDP)
673 fio_netio_udp_close(td, f);
675 return generic_close_file(td, f);
678 static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
680 struct netio_data *nd = td->io_ops->data;
681 struct udp_close_msg msg;
682 struct sockaddr *to = (struct sockaddr *) &nd->addr;
683 socklen_t len = sizeof(nd->addr);
686 ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
688 td_verror(td, errno, "recvfrom udp link open");
692 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
693 ntohl(msg.cmd) != FIO_LINK_OPEN) {
694 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
702 static int fio_netio_udp_send_open(struct thread_data *td, struct fio_file *f)
704 struct netio_data *nd = td->io_ops->data;
705 struct udp_close_msg msg;
706 struct sockaddr *to = (struct sockaddr *) &nd->addr;
709 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
710 msg.cmd = htonl(FIO_LINK_OPEN);
712 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to,
715 td_verror(td, errno, "sendto udp link open");
722 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
725 struct netio_options *o = td->eo;
728 ret = fio_netio_accept(td, f);
730 ret = fio_netio_connect(td, f);
737 if (o->proto == FIO_TYPE_UDP) {
739 ret = fio_netio_udp_send_open(td, f);
743 state = td->runstate;
744 td_set_runstate(td, TD_SETTING_UP);
745 ret = fio_netio_udp_recv_open(td, f);
746 td_set_runstate(td, state);
751 fio_netio_close_file(td, f);
756 static int fio_netio_setup_connect_inet(struct thread_data *td,
757 const char *host, unsigned short port)
759 struct netio_data *nd = td->io_ops->data;
762 log_err("fio: connect with no host to connect to.\n");
764 log_err("fio: did you forget to set 'listen'?\n");
766 td_verror(td, EINVAL, "no hostname= set");
770 nd->addr.sin_family = AF_INET;
771 nd->addr.sin_port = htons(port);
773 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
774 struct hostent *hent;
776 hent = gethostbyname(host);
778 td_verror(td, errno, "gethostbyname");
782 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
788 static int fio_netio_setup_connect_unix(struct thread_data *td,
791 struct netio_data *nd = td->io_ops->data;
792 struct sockaddr_un *soun = &nd->addr_un;
794 soun->sun_family = AF_UNIX;
795 strcpy(soun->sun_path, path);
799 static int fio_netio_setup_connect(struct thread_data *td)
801 struct netio_options *o = td->eo;
803 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
804 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
806 return fio_netio_setup_connect_unix(td, td->o.filename);
809 static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
811 struct netio_data *nd = td->io_ops->data;
812 struct sockaddr_un *addr = &nd->addr_un;
816 fd = socket(AF_UNIX, SOCK_STREAM, 0);
818 log_err("fio: socket: %s\n", strerror(errno));
824 memset(addr, 0, sizeof(*addr));
825 addr->sun_family = AF_UNIX;
826 strcpy(addr->sun_path, path);
829 len = sizeof(addr->sun_family) + strlen(path) + 1;
831 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
832 log_err("fio: bind: %s\n", strerror(errno));
842 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
844 struct netio_data *nd = td->io_ops->data;
845 struct netio_options *o = td->eo;
847 struct sockaddr_in sin;
850 memset(&sin, 0, sizeof(sin));
851 if (o->proto == FIO_TYPE_TCP)
856 fd = socket(AF_INET, type, 0);
858 td_verror(td, errno, "socket");
863 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
864 td_verror(td, errno, "setsockopt");
869 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) {
870 td_verror(td, errno, "setsockopt");
877 if(o->proto != FIO_TYPE_UDP ||
878 !fio_netio_is_multicast(td->o.filename)) {
879 log_err("fio: hostname not valid for non-multicast inbound network IO\n");
884 inet_aton(td->o.filename, &sin.sin_addr);
886 mr.imr_multiaddr = sin.sin_addr;
888 if (inet_aton(o->interface, &mr.imr_interface) == 0) {
889 log_err("fio: interface not valid interface IP\n");
894 mr.imr_interface.s_addr = htonl(INADDR_ANY);
896 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mr, sizeof(mr)) < 0) {
897 td_verror(td, errno, "setsockopt IP_ADD_MEMBERSHIP");
903 nd->addr.sin_family = AF_INET;
904 nd->addr.sin_addr.s_addr = sin.sin_addr.s_addr ? sin.sin_addr.s_addr : htonl(INADDR_ANY);
905 nd->addr.sin_port = htons(port);
907 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
908 td_verror(td, errno, "bind");
916 static int fio_netio_setup_listen(struct thread_data *td)
918 struct netio_data *nd = td->io_ops->data;
919 struct netio_options *o = td->eo;
922 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
923 ret = fio_netio_setup_listen_inet(td, o->port);
925 ret = fio_netio_setup_listen_unix(td, td->o.filename);
929 if (o->proto == FIO_TYPE_UDP)
932 if (listen(nd->listenfd, 10) < 0) {
933 td_verror(td, errno, "listen");
941 static int fio_netio_init(struct thread_data *td)
943 struct netio_options *o = td->eo;
948 WSAStartup(MAKEWORD(2,2), &wsd);
952 log_err("fio: network IO can't be random\n");
956 if (o->proto == FIO_TYPE_UNIX && o->port) {
957 log_err("fio: network IO port not valid with unix socket\n");
959 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
960 log_err("fio: network IO requires port for tcp or udp\n");
964 if (o->proto != FIO_TYPE_TCP) {
966 log_err("fio: listen only valid for TCP proto IO\n");
970 log_err("fio: datagram network connections must be"
974 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
975 log_err("fio: UNIX sockets need host/filename\n");
978 o->listen = td_read(td);
982 ret = fio_netio_setup_listen(td);
984 ret = fio_netio_setup_connect(td);
989 static void fio_netio_cleanup(struct thread_data *td)
991 struct netio_data *nd = td->io_ops->data;
994 if (nd->listenfd != -1)
996 if (nd->pipes[0] != -1)
998 if (nd->pipes[1] != -1)
1005 static int fio_netio_setup(struct thread_data *td)
1007 struct netio_data *nd;
1009 if (!td->files_index) {
1010 add_file(td, td->o.filename ?: "net");
1011 td->o.nr_files = td->o.nr_files ?: 1;
1014 if (!td->io_ops->data) {
1015 nd = malloc(sizeof(*nd));;
1017 memset(nd, 0, sizeof(*nd));
1019 nd->pipes[0] = nd->pipes[1] = -1;
1020 td->io_ops->data = nd;
1026 static void fio_netio_terminate(struct thread_data *td)
1028 kill(td->pid, SIGUSR2);
1031 #ifdef CONFIG_LINUX_SPLICE
1032 static int fio_netio_setup_splice(struct thread_data *td)
1034 struct netio_data *nd;
1036 fio_netio_setup(td);
1038 nd = td->io_ops->data;
1040 if (pipe(nd->pipes) < 0)
1050 static struct ioengine_ops ioengine_splice = {
1051 .name = "netsplice",
1052 .version = FIO_IOOPS_VERSION,
1053 .prep = fio_netio_prep,
1054 .queue = fio_netio_queue,
1055 .setup = fio_netio_setup_splice,
1056 .init = fio_netio_init,
1057 .cleanup = fio_netio_cleanup,
1058 .open_file = fio_netio_open_file,
1059 .close_file = fio_netio_close_file,
1060 .terminate = fio_netio_terminate,
1062 .option_struct_size = sizeof(struct netio_options),
1063 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1068 static struct ioengine_ops ioengine_rw = {
1070 .version = FIO_IOOPS_VERSION,
1071 .prep = fio_netio_prep,
1072 .queue = fio_netio_queue,
1073 .setup = fio_netio_setup,
1074 .init = fio_netio_init,
1075 .cleanup = fio_netio_cleanup,
1076 .open_file = fio_netio_open_file,
1077 .close_file = fio_netio_close_file,
1078 .terminate = fio_netio_terminate,
1080 .option_struct_size = sizeof(struct netio_options),
1081 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1082 FIO_PIPEIO | FIO_BIT_BASED,
1085 static int str_hostname_cb(void *data, const char *input)
1087 struct netio_options *o = data;
1089 if (o->td->o.filename)
1090 free(o->td->o.filename);
1091 o->td->o.filename = strdup(input);
1095 static void fio_init fio_netio_register(void)
1097 register_ioengine(&ioengine_rw);
1098 #ifdef CONFIG_LINUX_SPLICE
1099 register_ioengine(&ioengine_splice);
1103 static void fio_exit fio_netio_unregister(void)
1105 unregister_ioengine(&ioengine_rw);
1106 #ifdef CONFIG_LINUX_SPLICE
1107 unregister_ioengine(&ioengine_splice);