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;
43 struct udp_close_msg {
49 FIO_LINK_CLOSE = 0x89,
50 FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b,
58 static int str_hostname_cb(void *data, const char *input);
59 static struct fio_option options[] = {
62 .lname = "net engine hostname",
63 .type = FIO_OPT_STR_STORE,
64 .cb = str_hostname_cb,
65 .help = "Hostname for net IO engine",
66 .category = FIO_OPT_C_ENGINE,
67 .group = FIO_OPT_G_NETIO,
71 .lname = "net engine port",
73 .off1 = offsetof(struct netio_options, port),
76 .help = "Port to use for TCP or UDP net connections",
77 .category = FIO_OPT_C_ENGINE,
78 .group = FIO_OPT_G_NETIO,
82 .lname = "net engine protocol",
85 .off1 = offsetof(struct netio_options, proto),
86 .help = "Network protocol to use",
91 .help = "Transmission Control Protocol",
95 .help = "User Datagram Protocol",
98 .oval = FIO_TYPE_UNIX,
99 .help = "UNIX domain socket",
102 .category = FIO_OPT_C_ENGINE,
103 .group = FIO_OPT_G_NETIO,
105 #ifdef CONFIG_TCP_NODELAY
108 .type = FIO_OPT_BOOL,
109 .off1 = offsetof(struct netio_options, nodelay),
110 .help = "Use TCP_NODELAY on TCP connections",
111 .category = FIO_OPT_C_ENGINE,
112 .group = FIO_OPT_G_NETIO,
117 .lname = "net engine listen",
118 .type = FIO_OPT_STR_SET,
119 .off1 = offsetof(struct netio_options, listen),
120 .help = "Listen for incoming TCP connections",
121 .category = FIO_OPT_C_ENGINE,
122 .group = FIO_OPT_G_NETIO,
126 .type = FIO_OPT_STR_SET,
127 .off1 = offsetof(struct netio_options, pingpong),
128 .help = "Ping-pong IO requests",
129 .category = FIO_OPT_C_ENGINE,
130 .group = FIO_OPT_G_NETIO,
134 .lname = "net engine interface",
135 .type = FIO_OPT_STR_STORE,
136 .off1 = offsetof(struct netio_options, interface),
137 .help = "Network interface to use",
138 .category = FIO_OPT_C_ENGINE,
139 .group = FIO_OPT_G_NETIO,
147 * Return -1 for error and 'nr events' for a positive number
150 static int poll_wait(struct thread_data *td, int fd, short events)
155 while (!td->terminate) {
158 ret = poll(&pfd, 1, -1);
163 td_verror(td, errno, "poll");
171 if (pfd.revents & events)
177 static int fio_netio_is_multicast(const char *mcaddr)
179 in_addr_t addr = inet_network(mcaddr);
183 if (inet_network("224.0.0.0") <= addr &&
184 inet_network("239.255.255.255") >= addr)
191 static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
193 struct netio_options *o = td->eo;
196 * Make sure we don't see spurious reads to a receiver, and vice versa
198 if (o->proto == FIO_TYPE_TCP)
201 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
202 (!o->listen && io_u->ddir == DDIR_READ)) {
203 td_verror(td, EINVAL, "bad direction");
210 #ifdef CONFIG_LINUX_SPLICE
211 static int splice_io_u(int fdin, int fdout, unsigned int len)
216 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
234 * Receive bytes from a socket and fill them into the internal pipe
236 static int splice_in(struct thread_data *td, struct io_u *io_u)
238 struct netio_data *nd = td->io_ops->data;
240 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
244 * Transmit 'len' bytes from the internal pipe
246 static int splice_out(struct thread_data *td, struct io_u *io_u,
249 struct netio_data *nd = td->io_ops->data;
251 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
254 static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
257 .iov_base = io_u->xfer_buf,
262 while (iov.iov_len) {
263 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
282 * vmsplice() pipe to io_u buffer
284 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
287 struct netio_data *nd = td->io_ops->data;
289 return vmsplice_io_u(io_u, nd->pipes[0], len);
293 * vmsplice() io_u to pipe
295 static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
297 struct netio_data *nd = td->io_ops->data;
299 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
303 * splice receive - transfer socket data into a pipe using splice, then map
304 * that pipe data into the io_u using vmsplice.
306 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
310 ret = splice_in(td, io_u);
312 return vmsplice_io_u_out(td, io_u, ret);
318 * splice transmit - map data from the io_u into a pipe by using vmsplice,
319 * then transfer that pipe to a socket using splice.
321 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
325 ret = vmsplice_io_u_in(td, io_u);
327 return splice_out(td, io_u, ret);
332 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
338 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
345 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
347 struct netio_data *nd = td->io_ops->data;
348 struct netio_options *o = td->eo;
352 if (o->proto == FIO_TYPE_UDP) {
353 struct sockaddr *to = (struct sockaddr *) &nd->addr;
355 ret = sendto(io_u->file->fd, io_u->xfer_buf,
356 io_u->xfer_buflen, flags, to,
360 * if we are going to write more, set MSG_MORE
363 if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
364 td->o.size) && !o->pingpong)
367 ret = send(io_u->file->fd, io_u->xfer_buf,
368 io_u->xfer_buflen, flags);
373 ret = poll_wait(td, io_u->file->fd, POLLOUT);
381 static int is_udp_close(struct io_u *io_u, int len)
383 struct udp_close_msg *msg;
385 if (len != sizeof(struct udp_close_msg))
388 msg = io_u->xfer_buf;
389 if (ntohl(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC)
391 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
397 static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
399 struct netio_data *nd = td->io_ops->data;
400 struct netio_options *o = td->eo;
404 if (o->proto == FIO_TYPE_UDP) {
407 struct sockaddr *from;
410 from = (struct sockaddr *) &nd->addr;
411 *len = sizeof(nd->addr);
417 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
418 io_u->xfer_buflen, flags, from, len);
419 if (is_udp_close(io_u, ret)) {
424 ret = recv(io_u->file->fd, io_u->xfer_buf,
425 io_u->xfer_buflen, flags);
429 else if (!ret && (flags & MSG_WAITALL))
432 ret = poll_wait(td, io_u->file->fd, POLLIN);
435 flags |= MSG_WAITALL;
441 static int __fio_netio_queue(struct thread_data *td, struct io_u *io_u,
444 struct netio_data *nd = td->io_ops->data;
445 struct netio_options *o = td->eo;
448 if (ddir == DDIR_WRITE) {
449 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
450 o->proto == FIO_TYPE_UNIX)
451 ret = fio_netio_send(td, io_u);
453 ret = fio_netio_splice_out(td, io_u);
454 } else if (ddir == DDIR_READ) {
455 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
456 o->proto == FIO_TYPE_UNIX)
457 ret = fio_netio_recv(td, io_u);
459 ret = fio_netio_splice_in(td, io_u);
461 ret = 0; /* must be a SYNC */
463 if (ret != (int) io_u->xfer_buflen) {
465 io_u->resid = io_u->xfer_buflen - ret;
467 return FIO_Q_COMPLETED;
471 if (ddir == DDIR_WRITE && err == EMSGSIZE)
479 td_verror(td, io_u->error, "xfer");
481 return FIO_Q_COMPLETED;
484 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
486 struct netio_options *o = td->eo;
489 fio_ro_check(td, io_u);
491 ret = __fio_netio_queue(td, io_u, io_u->ddir);
492 if (!o->pingpong || ret != FIO_Q_COMPLETED)
496 * For ping-pong mode, receive or send reply as needed
498 if (td_read(td) && io_u->ddir == DDIR_READ)
499 ret = __fio_netio_queue(td, io_u, DDIR_WRITE);
500 else if (td_write(td) && io_u->ddir == DDIR_WRITE)
501 ret = __fio_netio_queue(td, io_u, DDIR_READ);
506 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
508 struct netio_data *nd = td->io_ops->data;
509 struct netio_options *o = td->eo;
512 if (o->proto == FIO_TYPE_TCP) {
515 } else if (o->proto == FIO_TYPE_UDP) {
518 } else if (o->proto == FIO_TYPE_UNIX) {
522 log_err("fio: bad network type %d\n", o->proto);
527 f->fd = socket(domain, type, 0);
529 td_verror(td, errno, "socket");
533 #ifdef CONFIG_TCP_NODELAY
534 if (o->nodelay && o->proto == FIO_TYPE_TCP) {
537 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
538 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
544 if (o->proto == FIO_TYPE_UDP) {
545 if (o->interface && fio_netio_is_multicast(td->o.filename)) {
546 struct in_addr interface_addr;
547 if (inet_aton(o->interface, &interface_addr) == 0) {
548 log_err("fio: interface not valid interface IP\n");
552 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_IF, &interface_addr, sizeof(interface_addr)) < 0) {
553 td_verror(td, errno, "setsockopt IP_MULTICAST_IF");
559 } else if (o->proto == FIO_TYPE_TCP) {
560 socklen_t len = sizeof(nd->addr);
562 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
563 td_verror(td, errno, "connect");
568 struct sockaddr_un *addr = &nd->addr_un;
571 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
573 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
574 td_verror(td, errno, "connect");
583 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
585 struct netio_data *nd = td->io_ops->data;
586 struct netio_options *o = td->eo;
587 socklen_t socklen = sizeof(nd->addr);
590 if (o->proto == FIO_TYPE_UDP) {
591 f->fd = nd->listenfd;
595 state = td->runstate;
596 td_set_runstate(td, TD_SETTING_UP);
598 log_info("fio: waiting for connection\n");
600 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
603 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
605 td_verror(td, errno, "accept");
609 #ifdef CONFIG_TCP_NODELAY
610 if (o->nodelay && o->proto == FIO_TYPE_TCP) {
613 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
614 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
621 td_set_runstate(td, state);
624 td_set_runstate(td, state);
628 static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
630 struct netio_data *nd = td->io_ops->data;
631 struct udp_close_msg msg;
632 struct sockaddr *to = (struct sockaddr *) &nd->addr;
635 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
636 msg.cmd = htonl(FIO_LINK_CLOSE);
638 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to,
641 td_verror(td, errno, "sendto udp link close");
644 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
646 struct netio_options *o = td->eo;
649 * If this is an UDP connection, notify the receiver that we are
650 * closing down the link
652 if (o->proto == FIO_TYPE_UDP)
653 fio_netio_udp_close(td, f);
655 return generic_close_file(td, f);
658 static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
660 struct netio_data *nd = td->io_ops->data;
661 struct udp_close_msg msg;
662 struct sockaddr *to = (struct sockaddr *) &nd->addr;
663 socklen_t len = sizeof(nd->addr);
666 ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
668 td_verror(td, errno, "recvfrom udp link open");
672 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
673 ntohl(msg.cmd) != FIO_LINK_OPEN) {
674 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
682 static int fio_netio_udp_send_open(struct thread_data *td, struct fio_file *f)
684 struct netio_data *nd = td->io_ops->data;
685 struct udp_close_msg msg;
686 struct sockaddr *to = (struct sockaddr *) &nd->addr;
689 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
690 msg.cmd = htonl(FIO_LINK_OPEN);
692 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to,
695 td_verror(td, errno, "sendto udp link open");
702 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
705 struct netio_options *o = td->eo;
708 ret = fio_netio_accept(td, f);
710 ret = fio_netio_connect(td, f);
717 if (o->proto == FIO_TYPE_UDP) {
719 ret = fio_netio_udp_send_open(td, f);
723 state = td->runstate;
724 td_set_runstate(td, TD_SETTING_UP);
725 ret = fio_netio_udp_recv_open(td, f);
726 td_set_runstate(td, state);
731 fio_netio_close_file(td, f);
736 static int fio_netio_setup_connect_inet(struct thread_data *td,
737 const char *host, unsigned short port)
739 struct netio_data *nd = td->io_ops->data;
742 log_err("fio: connect with no host to connect to.\n");
744 log_err("fio: did you forget to set 'listen'?\n");
746 td_verror(td, EINVAL, "no hostname= set");
750 nd->addr.sin_family = AF_INET;
751 nd->addr.sin_port = htons(port);
753 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
754 struct hostent *hent;
756 hent = gethostbyname(host);
758 td_verror(td, errno, "gethostbyname");
762 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
768 static int fio_netio_setup_connect_unix(struct thread_data *td,
771 struct netio_data *nd = td->io_ops->data;
772 struct sockaddr_un *soun = &nd->addr_un;
774 soun->sun_family = AF_UNIX;
775 strcpy(soun->sun_path, path);
779 static int fio_netio_setup_connect(struct thread_data *td)
781 struct netio_options *o = td->eo;
783 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
784 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
786 return fio_netio_setup_connect_unix(td, td->o.filename);
789 static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
791 struct netio_data *nd = td->io_ops->data;
792 struct sockaddr_un *addr = &nd->addr_un;
796 fd = socket(AF_UNIX, SOCK_STREAM, 0);
798 log_err("fio: socket: %s\n", strerror(errno));
804 memset(addr, 0, sizeof(*addr));
805 addr->sun_family = AF_UNIX;
806 strcpy(addr->sun_path, path);
809 len = sizeof(addr->sun_family) + strlen(path) + 1;
811 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
812 log_err("fio: bind: %s\n", strerror(errno));
822 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
824 struct netio_data *nd = td->io_ops->data;
825 struct netio_options *o = td->eo;
827 struct sockaddr_in sin;
830 memset(&sin, 0, sizeof(sin));
831 if (o->proto == FIO_TYPE_TCP)
836 fd = socket(AF_INET, type, 0);
838 td_verror(td, errno, "socket");
843 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
844 td_verror(td, errno, "setsockopt");
849 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) {
850 td_verror(td, errno, "setsockopt");
857 if(o->proto != FIO_TYPE_UDP ||
858 !fio_netio_is_multicast(td->o.filename)) {
859 log_err("fio: hostname not valid for non-multicast inbound network IO\n");
864 inet_aton(td->o.filename, &sin.sin_addr);
866 mr.imr_multiaddr = sin.sin_addr;
868 if (inet_aton(o->interface, &mr.imr_interface) == 0) {
869 log_err("fio: interface not valid interface IP\n");
874 mr.imr_interface.s_addr = htonl(INADDR_ANY);
876 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mr, sizeof(mr)) < 0) {
877 td_verror(td, errno, "setsockopt IP_ADD_MEMBERSHIP");
883 nd->addr.sin_family = AF_INET;
884 nd->addr.sin_addr.s_addr = sin.sin_addr.s_addr ? sin.sin_addr.s_addr : htonl(INADDR_ANY);
885 nd->addr.sin_port = htons(port);
887 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
888 td_verror(td, errno, "bind");
896 static int fio_netio_setup_listen(struct thread_data *td)
898 struct netio_data *nd = td->io_ops->data;
899 struct netio_options *o = td->eo;
902 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
903 ret = fio_netio_setup_listen_inet(td, o->port);
905 ret = fio_netio_setup_listen_unix(td, td->o.filename);
909 if (o->proto == FIO_TYPE_UDP)
912 if (listen(nd->listenfd, 10) < 0) {
913 td_verror(td, errno, "listen");
921 static int fio_netio_init(struct thread_data *td)
923 struct netio_options *o = td->eo;
928 WSAStartup(MAKEWORD(2,2), &wsd);
932 log_err("fio: network IO can't be random\n");
936 if (o->proto == FIO_TYPE_UNIX && o->port) {
937 log_err("fio: network IO port not valid with unix socket\n");
939 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
940 log_err("fio: network IO requires port for tcp or udp\n");
944 if (o->proto != FIO_TYPE_TCP) {
946 log_err("fio: listen only valid for TCP proto IO\n");
950 log_err("fio: datagram network connections must be"
954 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
955 log_err("fio: UNIX sockets need host/filename\n");
958 o->listen = td_read(td);
962 ret = fio_netio_setup_listen(td);
964 ret = fio_netio_setup_connect(td);
969 static void fio_netio_cleanup(struct thread_data *td)
971 struct netio_data *nd = td->io_ops->data;
974 if (nd->listenfd != -1)
976 if (nd->pipes[0] != -1)
978 if (nd->pipes[1] != -1)
985 static int fio_netio_setup(struct thread_data *td)
987 struct netio_data *nd;
989 if (!td->files_index) {
990 add_file(td, td->o.filename ?: "net");
991 td->o.nr_files = td->o.nr_files ?: 1;
994 if (!td->io_ops->data) {
995 nd = malloc(sizeof(*nd));;
997 memset(nd, 0, sizeof(*nd));
999 nd->pipes[0] = nd->pipes[1] = -1;
1000 td->io_ops->data = nd;
1006 static void fio_netio_terminate(struct thread_data *td)
1008 kill(td->pid, SIGUSR2);
1011 #ifdef CONFIG_LINUX_SPLICE
1012 static int fio_netio_setup_splice(struct thread_data *td)
1014 struct netio_data *nd;
1016 fio_netio_setup(td);
1018 nd = td->io_ops->data;
1020 if (pipe(nd->pipes) < 0)
1030 static struct ioengine_ops ioengine_splice = {
1031 .name = "netsplice",
1032 .version = FIO_IOOPS_VERSION,
1033 .prep = fio_netio_prep,
1034 .queue = fio_netio_queue,
1035 .setup = fio_netio_setup_splice,
1036 .init = fio_netio_init,
1037 .cleanup = fio_netio_cleanup,
1038 .open_file = fio_netio_open_file,
1039 .close_file = fio_netio_close_file,
1040 .terminate = fio_netio_terminate,
1042 .option_struct_size = sizeof(struct netio_options),
1043 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1048 static struct ioengine_ops ioengine_rw = {
1050 .version = FIO_IOOPS_VERSION,
1051 .prep = fio_netio_prep,
1052 .queue = fio_netio_queue,
1053 .setup = fio_netio_setup,
1054 .init = fio_netio_init,
1055 .cleanup = fio_netio_cleanup,
1056 .open_file = fio_netio_open_file,
1057 .close_file = fio_netio_close_file,
1058 .terminate = fio_netio_terminate,
1060 .option_struct_size = sizeof(struct netio_options),
1061 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1062 FIO_PIPEIO | FIO_BIT_BASED,
1065 static int str_hostname_cb(void *data, const char *input)
1067 struct netio_options *o = data;
1069 if (o->td->o.filename)
1070 free(o->td->o.filename);
1071 o->td->o.filename = strdup(input);
1075 static void fio_init fio_netio_register(void)
1077 register_ioengine(&ioengine_rw);
1078 #ifdef CONFIG_LINUX_SPLICE
1079 register_ioengine(&ioengine_splice);
1083 static void fio_exit fio_netio_unregister(void)
1085 unregister_ioengine(&ioengine_rw);
1086 #ifdef CONFIG_LINUX_SPLICE
1087 unregister_ioengine(&ioengine_splice);