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;
42 struct udp_close_msg {
48 FIO_LINK_CLOSE = 0x89,
49 FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b,
57 static int str_hostname_cb(void *data, const char *input);
58 static struct fio_option options[] = {
61 .type = FIO_OPT_STR_STORE,
62 .cb = str_hostname_cb,
63 .help = "Hostname for net IO engine",
64 .category = FIO_OPT_C_ENGINE,
65 .group = FIO_OPT_G_NETIO,
70 .off1 = offsetof(struct netio_options, port),
73 .help = "Port to use for TCP or UDP net connections",
74 .category = FIO_OPT_C_ENGINE,
75 .group = FIO_OPT_G_NETIO,
81 .off1 = offsetof(struct netio_options, proto),
82 .help = "Network protocol to use",
87 .help = "Transmission Control Protocol",
91 .help = "User Datagram Protocol",
94 .oval = FIO_TYPE_UNIX,
95 .help = "UNIX domain socket",
98 .category = FIO_OPT_C_ENGINE,
99 .group = FIO_OPT_G_NETIO,
101 #ifdef CONFIG_TCP_NODELAY
104 .type = FIO_OPT_BOOL,
105 .off1 = offsetof(struct netio_options, nodelay),
106 .help = "Use TCP_NODELAY on TCP connections",
107 .category = FIO_OPT_C_ENGINE,
108 .group = FIO_OPT_G_NETIO,
113 .type = FIO_OPT_STR_SET,
114 .off1 = offsetof(struct netio_options, listen),
115 .help = "Listen for incoming TCP connections",
116 .category = FIO_OPT_C_ENGINE,
117 .group = FIO_OPT_G_NETIO,
121 .type = FIO_OPT_STR_SET,
122 .off1 = offsetof(struct netio_options, pingpong),
123 .help = "Ping-pong IO requests",
124 .category = FIO_OPT_C_ENGINE,
125 .group = FIO_OPT_G_NETIO,
133 * Return -1 for error and 'nr events' for a positive number
136 static int poll_wait(struct thread_data *td, int fd, short events)
141 while (!td->terminate) {
144 ret = poll(&pfd, 1, -1);
149 td_verror(td, errno, "poll");
157 if (pfd.revents & events)
163 static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
165 struct netio_options *o = td->eo;
168 * Make sure we don't see spurious reads to a receiver, and vice versa
170 if (o->proto == FIO_TYPE_TCP)
173 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
174 (!o->listen && io_u->ddir == DDIR_READ)) {
175 td_verror(td, EINVAL, "bad direction");
182 #ifdef CONFIG_LINUX_SPLICE
183 static int splice_io_u(int fdin, int fdout, unsigned int len)
188 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
206 * Receive bytes from a socket and fill them into the internal pipe
208 static int splice_in(struct thread_data *td, struct io_u *io_u)
210 struct netio_data *nd = td->io_ops->data;
212 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
216 * Transmit 'len' bytes from the internal pipe
218 static int splice_out(struct thread_data *td, struct io_u *io_u,
221 struct netio_data *nd = td->io_ops->data;
223 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
226 static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
229 .iov_base = io_u->xfer_buf,
234 while (iov.iov_len) {
235 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
254 * vmsplice() pipe to io_u buffer
256 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
259 struct netio_data *nd = td->io_ops->data;
261 return vmsplice_io_u(io_u, nd->pipes[0], len);
265 * vmsplice() io_u to pipe
267 static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
269 struct netio_data *nd = td->io_ops->data;
271 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
275 * splice receive - transfer socket data into a pipe using splice, then map
276 * that pipe data into the io_u using vmsplice.
278 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
282 ret = splice_in(td, io_u);
284 return vmsplice_io_u_out(td, io_u, ret);
290 * splice transmit - map data from the io_u into a pipe by using vmsplice,
291 * then transfer that pipe to a socket using splice.
293 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
297 ret = vmsplice_io_u_in(td, io_u);
299 return splice_out(td, io_u, ret);
304 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
310 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
317 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
319 struct netio_data *nd = td->io_ops->data;
320 struct netio_options *o = td->eo;
324 if (o->proto == FIO_TYPE_UDP) {
325 struct sockaddr *to = (struct sockaddr *) &nd->addr;
327 ret = sendto(io_u->file->fd, io_u->xfer_buf,
328 io_u->xfer_buflen, flags, to,
332 * if we are going to write more, set MSG_MORE
335 if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
336 td->o.size) && !o->pingpong)
339 ret = send(io_u->file->fd, io_u->xfer_buf,
340 io_u->xfer_buflen, flags);
345 ret = poll_wait(td, io_u->file->fd, POLLOUT);
353 static int is_udp_close(struct io_u *io_u, int len)
355 struct udp_close_msg *msg;
357 if (len != sizeof(struct udp_close_msg))
360 msg = io_u->xfer_buf;
361 if (ntohl(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC)
363 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
369 static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
371 struct netio_data *nd = td->io_ops->data;
372 struct netio_options *o = td->eo;
376 if (o->proto == FIO_TYPE_UDP) {
377 socklen_t len = sizeof(nd->addr);
378 struct sockaddr *from = (struct sockaddr *) &nd->addr;
380 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
381 io_u->xfer_buflen, flags, from, &len);
382 if (is_udp_close(io_u, ret)) {
387 ret = recv(io_u->file->fd, io_u->xfer_buf,
388 io_u->xfer_buflen, flags);
392 else if (!ret && (flags & MSG_WAITALL))
395 ret = poll_wait(td, io_u->file->fd, POLLIN);
398 flags |= MSG_WAITALL;
404 static int __fio_netio_queue(struct thread_data *td, struct io_u *io_u,
407 struct netio_data *nd = td->io_ops->data;
408 struct netio_options *o = td->eo;
411 if (ddir == DDIR_WRITE) {
412 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
413 o->proto == FIO_TYPE_UNIX)
414 ret = fio_netio_send(td, io_u);
416 ret = fio_netio_splice_out(td, io_u);
417 } else if (ddir == DDIR_READ) {
418 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
419 o->proto == FIO_TYPE_UNIX)
420 ret = fio_netio_recv(td, io_u);
422 ret = fio_netio_splice_in(td, io_u);
424 ret = 0; /* must be a SYNC */
426 if (ret != (int) io_u->xfer_buflen) {
428 io_u->resid = io_u->xfer_buflen - ret;
430 return FIO_Q_COMPLETED;
434 if (ddir == DDIR_WRITE && err == EMSGSIZE)
442 td_verror(td, io_u->error, "xfer");
444 return FIO_Q_COMPLETED;
447 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
449 struct netio_options *o = td->eo;
452 fio_ro_check(td, io_u);
454 ret = __fio_netio_queue(td, io_u, io_u->ddir);
455 if (!o->pingpong || ret != FIO_Q_COMPLETED)
459 * For ping-pong mode, receive or send reply as needed
461 if (td_read(td) && io_u->ddir == DDIR_READ)
462 ret = __fio_netio_queue(td, io_u, DDIR_WRITE);
463 else if (td_write(td) && io_u->ddir == DDIR_WRITE)
464 ret = __fio_netio_queue(td, io_u, DDIR_READ);
469 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
471 struct netio_data *nd = td->io_ops->data;
472 struct netio_options *o = td->eo;
475 if (o->proto == FIO_TYPE_TCP) {
478 } else if (o->proto == FIO_TYPE_UDP) {
481 } else if (o->proto == FIO_TYPE_UNIX) {
485 log_err("fio: bad network type %d\n", o->proto);
490 f->fd = socket(domain, type, 0);
492 td_verror(td, errno, "socket");
496 #ifdef CONFIG_TCP_NODELAY
497 if (o->nodelay && o->proto == FIO_TYPE_TCP) {
500 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
501 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
507 if (o->proto == FIO_TYPE_UDP)
509 else if (o->proto == FIO_TYPE_TCP) {
510 socklen_t len = sizeof(nd->addr);
512 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
513 td_verror(td, errno, "connect");
518 struct sockaddr_un *addr = &nd->addr_un;
521 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
523 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
524 td_verror(td, errno, "connect");
533 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
535 struct netio_data *nd = td->io_ops->data;
536 struct netio_options *o = td->eo;
537 socklen_t socklen = sizeof(nd->addr);
540 if (o->proto == FIO_TYPE_UDP) {
541 f->fd = nd->listenfd;
545 state = td->runstate;
546 td_set_runstate(td, TD_SETTING_UP);
548 log_info("fio: waiting for connection\n");
550 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
553 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
555 td_verror(td, errno, "accept");
559 #ifdef CONFIG_TCP_NODELAY
560 if (o->nodelay && o->proto == FIO_TYPE_TCP) {
563 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
564 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
571 td_set_runstate(td, state);
574 td_set_runstate(td, state);
578 static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
580 struct netio_data *nd = td->io_ops->data;
581 struct udp_close_msg msg;
582 struct sockaddr *to = (struct sockaddr *) &nd->addr;
585 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
586 msg.cmd = htonl(FIO_LINK_CLOSE);
588 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to,
591 td_verror(td, errno, "sendto udp link close");
594 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
596 struct netio_options *o = td->eo;
599 * If this is an UDP connection, notify the receiver that we are
600 * closing down the link
602 if (o->proto == FIO_TYPE_UDP)
603 fio_netio_udp_close(td, f);
605 return generic_close_file(td, f);
608 static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
610 struct netio_data *nd = td->io_ops->data;
611 struct udp_close_msg msg;
612 struct sockaddr *to = (struct sockaddr *) &nd->addr;
613 socklen_t len = sizeof(nd->addr);
616 ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
618 td_verror(td, errno, "sendto udp link open");
622 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
623 ntohl(msg.cmd) != FIO_LINK_OPEN) {
624 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
632 static int fio_netio_udp_send_open(struct thread_data *td, struct fio_file *f)
634 struct netio_data *nd = td->io_ops->data;
635 struct udp_close_msg msg;
636 struct sockaddr *to = (struct sockaddr *) &nd->addr;
639 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
640 msg.cmd = htonl(FIO_LINK_OPEN);
642 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to,
645 td_verror(td, errno, "sendto udp link open");
652 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
655 struct netio_options *o = td->eo;
658 ret = fio_netio_accept(td, f);
660 ret = fio_netio_connect(td, f);
667 if (o->proto == FIO_TYPE_UDP) {
669 ret = fio_netio_udp_send_open(td, f);
673 state = td->runstate;
674 td_set_runstate(td, TD_SETTING_UP);
675 ret = fio_netio_udp_recv_open(td, f);
676 td_set_runstate(td, state);
681 fio_netio_close_file(td, f);
686 static int fio_netio_setup_connect_inet(struct thread_data *td,
687 const char *host, unsigned short port)
689 struct netio_data *nd = td->io_ops->data;
692 log_err("fio: connect with no host to connect to.\n");
694 log_err("fio: did you forget to set 'listen'?\n");
696 td_verror(td, EINVAL, "no hostname= set");
700 nd->addr.sin_family = AF_INET;
701 nd->addr.sin_port = htons(port);
703 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
704 struct hostent *hent;
706 hent = gethostbyname(host);
708 td_verror(td, errno, "gethostbyname");
712 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
718 static int fio_netio_setup_connect_unix(struct thread_data *td,
721 struct netio_data *nd = td->io_ops->data;
722 struct sockaddr_un *soun = &nd->addr_un;
724 soun->sun_family = AF_UNIX;
725 strcpy(soun->sun_path, path);
729 static int fio_netio_setup_connect(struct thread_data *td)
731 struct netio_options *o = td->eo;
733 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
734 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
736 return fio_netio_setup_connect_unix(td, td->o.filename);
739 static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
741 struct netio_data *nd = td->io_ops->data;
742 struct sockaddr_un *addr = &nd->addr_un;
746 fd = socket(AF_UNIX, SOCK_STREAM, 0);
748 log_err("fio: socket: %s\n", strerror(errno));
754 memset(addr, 0, sizeof(*addr));
755 addr->sun_family = AF_UNIX;
756 strcpy(addr->sun_path, path);
759 len = sizeof(addr->sun_family) + strlen(path) + 1;
761 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
762 log_err("fio: bind: %s\n", strerror(errno));
772 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
774 struct netio_data *nd = td->io_ops->data;
775 struct netio_options *o = td->eo;
778 if (o->proto == FIO_TYPE_TCP)
783 fd = socket(AF_INET, type, 0);
785 td_verror(td, errno, "socket");
790 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
791 td_verror(td, errno, "setsockopt");
795 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) {
796 td_verror(td, errno, "setsockopt");
801 nd->addr.sin_family = AF_INET;
802 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
803 nd->addr.sin_port = htons(port);
805 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
806 td_verror(td, errno, "bind");
814 static int fio_netio_setup_listen(struct thread_data *td)
816 struct netio_data *nd = td->io_ops->data;
817 struct netio_options *o = td->eo;
820 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
821 ret = fio_netio_setup_listen_inet(td, o->port);
823 ret = fio_netio_setup_listen_unix(td, td->o.filename);
827 if (o->proto == FIO_TYPE_UDP)
830 if (listen(nd->listenfd, 10) < 0) {
831 td_verror(td, errno, "listen");
839 static int fio_netio_init(struct thread_data *td)
841 struct netio_options *o = td->eo;
846 WSAStartup(MAKEWORD(2,2), &wsd);
850 log_err("fio: network IO can't be random\n");
854 if (o->proto == FIO_TYPE_UNIX && o->port) {
855 log_err("fio: network IO port not valid with unix socket\n");
857 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
858 log_err("fio: network IO requires port for tcp or udp\n");
862 if (o->proto != FIO_TYPE_TCP) {
864 log_err("fio: listen only valid for TCP proto IO\n");
868 log_err("fio: datagram network connections must be"
872 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
873 log_err("fio: UNIX sockets need host/filename\n");
876 o->listen = td_read(td);
879 if (o->proto != FIO_TYPE_UNIX && o->listen && td->o.filename) {
880 log_err("fio: hostname not valid for inbound network IO\n");
885 ret = fio_netio_setup_listen(td);
887 ret = fio_netio_setup_connect(td);
892 static void fio_netio_cleanup(struct thread_data *td)
894 struct netio_data *nd = td->io_ops->data;
897 if (nd->listenfd != -1)
899 if (nd->pipes[0] != -1)
901 if (nd->pipes[1] != -1)
908 static int fio_netio_setup(struct thread_data *td)
910 struct netio_data *nd;
912 if (!td->files_index) {
913 add_file(td, td->o.filename ?: "net");
914 td->o.nr_files = td->o.nr_files ?: 1;
917 if (!td->io_ops->data) {
918 nd = malloc(sizeof(*nd));;
920 memset(nd, 0, sizeof(*nd));
922 nd->pipes[0] = nd->pipes[1] = -1;
923 td->io_ops->data = nd;
929 static void fio_netio_terminate(struct thread_data *td)
931 kill(td->pid, SIGUSR2);
934 #ifdef CONFIG_LINUX_SPLICE
935 static int fio_netio_setup_splice(struct thread_data *td)
937 struct netio_data *nd;
941 nd = td->io_ops->data;
943 if (pipe(nd->pipes) < 0)
953 static struct ioengine_ops ioengine_splice = {
955 .version = FIO_IOOPS_VERSION,
956 .prep = fio_netio_prep,
957 .queue = fio_netio_queue,
958 .setup = fio_netio_setup_splice,
959 .init = fio_netio_init,
960 .cleanup = fio_netio_cleanup,
961 .open_file = fio_netio_open_file,
962 .close_file = fio_netio_close_file,
963 .terminate = fio_netio_terminate,
965 .option_struct_size = sizeof(struct netio_options),
966 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
971 static struct ioengine_ops ioengine_rw = {
973 .version = FIO_IOOPS_VERSION,
974 .prep = fio_netio_prep,
975 .queue = fio_netio_queue,
976 .setup = fio_netio_setup,
977 .init = fio_netio_init,
978 .cleanup = fio_netio_cleanup,
979 .open_file = fio_netio_open_file,
980 .close_file = fio_netio_close_file,
981 .terminate = fio_netio_terminate,
983 .option_struct_size = sizeof(struct netio_options),
984 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
985 FIO_PIPEIO | FIO_BIT_BASED,
988 static int str_hostname_cb(void *data, const char *input)
990 struct netio_options *o = data;
992 if (o->td->o.filename)
993 free(o->td->o.filename);
994 o->td->o.filename = strdup(input);
998 static void fio_init fio_netio_register(void)
1000 register_ioengine(&ioengine_rw);
1001 #ifdef CONFIG_LINUX_SPLICE
1002 register_ioengine(&ioengine_splice);
1006 static void fio_exit fio_netio_unregister(void)
1008 unregister_ioengine(&ioengine_rw);
1009 #ifdef CONFIG_LINUX_SPLICE
1010 unregister_ioengine(&ioengine_splice);