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 .lname = "net engine hostname",
62 .type = FIO_OPT_STR_STORE,
63 .cb = str_hostname_cb,
64 .help = "Hostname for net IO engine",
65 .category = FIO_OPT_C_IO,
69 .lname = "net engine port",
71 .off1 = offsetof(struct netio_options, port),
74 .help = "Port to use for TCP or UDP net connections",
75 .category = FIO_OPT_C_IO,
79 .lname = "net engine protocol",
82 .off1 = offsetof(struct netio_options, proto),
83 .help = "Network protocol to use",
85 .category = FIO_OPT_C_IO,
89 .help = "Transmission Control Protocol",
93 .help = "User Datagram Protocol",
96 .oval = FIO_TYPE_UNIX,
97 .help = "UNIX domain socket",
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",
111 .lname = "net engine listen",
112 .type = FIO_OPT_STR_SET,
113 .off1 = offsetof(struct netio_options, listen),
114 .help = "Listen for incoming TCP connections",
115 .category = FIO_OPT_C_IO,
119 .type = FIO_OPT_STR_SET,
120 .off1 = offsetof(struct netio_options, pingpong),
121 .help = "Ping-pong IO requests",
129 * Return -1 for error and 'nr events' for a positive number
132 static int poll_wait(struct thread_data *td, int fd, short events)
137 while (!td->terminate) {
140 ret = poll(&pfd, 1, -1);
145 td_verror(td, errno, "poll");
153 if (pfd.revents & events)
159 static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
161 struct netio_options *o = td->eo;
164 * Make sure we don't see spurious reads to a receiver, and vice versa
166 if (o->proto == FIO_TYPE_TCP)
169 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
170 (!o->listen && io_u->ddir == DDIR_READ)) {
171 td_verror(td, EINVAL, "bad direction");
178 #ifdef CONFIG_LINUX_SPLICE
179 static int splice_io_u(int fdin, int fdout, unsigned int len)
184 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
202 * Receive bytes from a socket and fill them into the internal pipe
204 static int splice_in(struct thread_data *td, struct io_u *io_u)
206 struct netio_data *nd = td->io_ops->data;
208 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
212 * Transmit 'len' bytes from the internal pipe
214 static int splice_out(struct thread_data *td, struct io_u *io_u,
217 struct netio_data *nd = td->io_ops->data;
219 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
222 static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
225 .iov_base = io_u->xfer_buf,
230 while (iov.iov_len) {
231 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
250 * vmsplice() pipe to io_u buffer
252 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
255 struct netio_data *nd = td->io_ops->data;
257 return vmsplice_io_u(io_u, nd->pipes[0], len);
261 * vmsplice() io_u to pipe
263 static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
265 struct netio_data *nd = td->io_ops->data;
267 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
271 * splice receive - transfer socket data into a pipe using splice, then map
272 * that pipe data into the io_u using vmsplice.
274 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
278 ret = splice_in(td, io_u);
280 return vmsplice_io_u_out(td, io_u, ret);
286 * splice transmit - map data from the io_u into a pipe by using vmsplice,
287 * then transfer that pipe to a socket using splice.
289 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
293 ret = vmsplice_io_u_in(td, io_u);
295 return splice_out(td, io_u, ret);
300 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
306 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
313 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
315 struct netio_data *nd = td->io_ops->data;
316 struct netio_options *o = td->eo;
320 if (o->proto == FIO_TYPE_UDP) {
321 struct sockaddr *to = (struct sockaddr *) &nd->addr;
323 ret = sendto(io_u->file->fd, io_u->xfer_buf,
324 io_u->xfer_buflen, flags, to,
328 * if we are going to write more, set MSG_MORE
331 if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
332 td->o.size) && !o->pingpong)
335 ret = send(io_u->file->fd, io_u->xfer_buf,
336 io_u->xfer_buflen, flags);
341 ret = poll_wait(td, io_u->file->fd, POLLOUT);
349 static int is_udp_close(struct io_u *io_u, int len)
351 struct udp_close_msg *msg;
353 if (len != sizeof(struct udp_close_msg))
356 msg = io_u->xfer_buf;
357 if (ntohl(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC)
359 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
365 static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
367 struct netio_data *nd = td->io_ops->data;
368 struct netio_options *o = td->eo;
372 if (o->proto == FIO_TYPE_UDP) {
373 socklen_t len = sizeof(nd->addr);
374 struct sockaddr *from = (struct sockaddr *) &nd->addr;
376 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
377 io_u->xfer_buflen, flags, from, &len);
378 if (is_udp_close(io_u, ret)) {
383 ret = recv(io_u->file->fd, io_u->xfer_buf,
384 io_u->xfer_buflen, flags);
388 else if (!ret && (flags & MSG_WAITALL))
391 ret = poll_wait(td, io_u->file->fd, POLLIN);
394 flags |= MSG_WAITALL;
400 static int __fio_netio_queue(struct thread_data *td, struct io_u *io_u,
403 struct netio_data *nd = td->io_ops->data;
404 struct netio_options *o = td->eo;
407 if (ddir == DDIR_WRITE) {
408 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
409 o->proto == FIO_TYPE_UNIX)
410 ret = fio_netio_send(td, io_u);
412 ret = fio_netio_splice_out(td, io_u);
413 } else if (ddir == DDIR_READ) {
414 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
415 o->proto == FIO_TYPE_UNIX)
416 ret = fio_netio_recv(td, io_u);
418 ret = fio_netio_splice_in(td, io_u);
420 ret = 0; /* must be a SYNC */
422 if (ret != (int) io_u->xfer_buflen) {
424 io_u->resid = io_u->xfer_buflen - ret;
426 return FIO_Q_COMPLETED;
430 if (ddir == DDIR_WRITE && err == EMSGSIZE)
438 td_verror(td, io_u->error, "xfer");
440 return FIO_Q_COMPLETED;
443 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
445 struct netio_options *o = td->eo;
448 fio_ro_check(td, io_u);
450 ret = __fio_netio_queue(td, io_u, io_u->ddir);
451 if (!o->pingpong || ret != FIO_Q_COMPLETED)
455 * For ping-pong mode, receive or send reply as needed
457 if (td_read(td) && io_u->ddir == DDIR_READ)
458 ret = __fio_netio_queue(td, io_u, DDIR_WRITE);
459 else if (td_write(td) && io_u->ddir == DDIR_WRITE)
460 ret = __fio_netio_queue(td, io_u, DDIR_READ);
465 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
467 struct netio_data *nd = td->io_ops->data;
468 struct netio_options *o = td->eo;
471 if (o->proto == FIO_TYPE_TCP) {
474 } else if (o->proto == FIO_TYPE_UDP) {
477 } else if (o->proto == FIO_TYPE_UNIX) {
481 log_err("fio: bad network type %d\n", o->proto);
486 f->fd = socket(domain, type, 0);
488 td_verror(td, errno, "socket");
492 #ifdef CONFIG_TCP_NODELAY
493 if (o->nodelay && o->proto == FIO_TYPE_TCP) {
496 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
497 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
503 if (o->proto == FIO_TYPE_UDP)
505 else if (o->proto == FIO_TYPE_TCP) {
506 socklen_t len = sizeof(nd->addr);
508 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
509 td_verror(td, errno, "connect");
514 struct sockaddr_un *addr = &nd->addr_un;
517 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
519 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
520 td_verror(td, errno, "connect");
529 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
531 struct netio_data *nd = td->io_ops->data;
532 struct netio_options *o = td->eo;
533 socklen_t socklen = sizeof(nd->addr);
536 if (o->proto == FIO_TYPE_UDP) {
537 f->fd = nd->listenfd;
541 state = td->runstate;
542 td_set_runstate(td, TD_SETTING_UP);
544 log_info("fio: waiting for connection\n");
546 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
549 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
551 td_verror(td, errno, "accept");
555 #ifdef CONFIG_TCP_NODELAY
556 if (o->nodelay && o->proto == FIO_TYPE_TCP) {
559 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
560 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
567 td_set_runstate(td, state);
570 td_set_runstate(td, state);
574 static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
576 struct netio_data *nd = td->io_ops->data;
577 struct udp_close_msg msg;
578 struct sockaddr *to = (struct sockaddr *) &nd->addr;
581 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
582 msg.cmd = htonl(FIO_LINK_CLOSE);
584 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to,
587 td_verror(td, errno, "sendto udp link close");
590 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
592 struct netio_options *o = td->eo;
595 * If this is an UDP connection, notify the receiver that we are
596 * closing down the link
598 if (o->proto == FIO_TYPE_UDP)
599 fio_netio_udp_close(td, f);
601 return generic_close_file(td, f);
604 static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
606 struct netio_data *nd = td->io_ops->data;
607 struct udp_close_msg msg;
608 struct sockaddr *to = (struct sockaddr *) &nd->addr;
609 socklen_t len = sizeof(nd->addr);
612 ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
614 td_verror(td, errno, "sendto udp link open");
618 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
619 ntohl(msg.cmd) != FIO_LINK_OPEN) {
620 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
628 static int fio_netio_udp_send_open(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_OPEN);
638 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to,
641 td_verror(td, errno, "sendto udp link open");
648 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
651 struct netio_options *o = td->eo;
654 ret = fio_netio_accept(td, f);
656 ret = fio_netio_connect(td, f);
663 if (o->proto == FIO_TYPE_UDP) {
665 ret = fio_netio_udp_send_open(td, f);
669 state = td->runstate;
670 td_set_runstate(td, TD_SETTING_UP);
671 ret = fio_netio_udp_recv_open(td, f);
672 td_set_runstate(td, state);
677 fio_netio_close_file(td, f);
682 static int fio_netio_setup_connect_inet(struct thread_data *td,
683 const char *host, unsigned short port)
685 struct netio_data *nd = td->io_ops->data;
688 log_err("fio: connect with no host to connect to.\n");
690 log_err("fio: did you forget to set 'listen'?\n");
692 td_verror(td, EINVAL, "no hostname= set");
696 nd->addr.sin_family = AF_INET;
697 nd->addr.sin_port = htons(port);
699 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
700 struct hostent *hent;
702 hent = gethostbyname(host);
704 td_verror(td, errno, "gethostbyname");
708 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
714 static int fio_netio_setup_connect_unix(struct thread_data *td,
717 struct netio_data *nd = td->io_ops->data;
718 struct sockaddr_un *soun = &nd->addr_un;
720 soun->sun_family = AF_UNIX;
721 strcpy(soun->sun_path, path);
725 static int fio_netio_setup_connect(struct thread_data *td)
727 struct netio_options *o = td->eo;
729 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
730 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
732 return fio_netio_setup_connect_unix(td, td->o.filename);
735 static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
737 struct netio_data *nd = td->io_ops->data;
738 struct sockaddr_un *addr = &nd->addr_un;
742 fd = socket(AF_UNIX, SOCK_STREAM, 0);
744 log_err("fio: socket: %s\n", strerror(errno));
750 memset(addr, 0, sizeof(*addr));
751 addr->sun_family = AF_UNIX;
752 strcpy(addr->sun_path, path);
755 len = sizeof(addr->sun_family) + strlen(path) + 1;
757 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
758 log_err("fio: bind: %s\n", strerror(errno));
768 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
770 struct netio_data *nd = td->io_ops->data;
771 struct netio_options *o = td->eo;
774 if (o->proto == FIO_TYPE_TCP)
779 fd = socket(AF_INET, type, 0);
781 td_verror(td, errno, "socket");
786 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
787 td_verror(td, errno, "setsockopt");
791 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) {
792 td_verror(td, errno, "setsockopt");
797 nd->addr.sin_family = AF_INET;
798 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
799 nd->addr.sin_port = htons(port);
801 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
802 td_verror(td, errno, "bind");
810 static int fio_netio_setup_listen(struct thread_data *td)
812 struct netio_data *nd = td->io_ops->data;
813 struct netio_options *o = td->eo;
816 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
817 ret = fio_netio_setup_listen_inet(td, o->port);
819 ret = fio_netio_setup_listen_unix(td, td->o.filename);
823 if (o->proto == FIO_TYPE_UDP)
826 if (listen(nd->listenfd, 10) < 0) {
827 td_verror(td, errno, "listen");
835 static int fio_netio_init(struct thread_data *td)
837 struct netio_options *o = td->eo;
842 WSAStartup(MAKEWORD(2,2), &wsd);
846 log_err("fio: network IO can't be random\n");
850 if (o->proto == FIO_TYPE_UNIX && o->port) {
851 log_err("fio: network IO port not valid with unix socket\n");
853 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
854 log_err("fio: network IO requires port for tcp or udp\n");
858 if (o->proto != FIO_TYPE_TCP) {
860 log_err("fio: listen only valid for TCP proto IO\n");
864 log_err("fio: datagram network connections must be"
868 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
869 log_err("fio: UNIX sockets need host/filename\n");
872 o->listen = td_read(td);
875 if (o->proto != FIO_TYPE_UNIX && o->listen && td->o.filename) {
876 log_err("fio: hostname not valid for inbound network IO\n");
881 ret = fio_netio_setup_listen(td);
883 ret = fio_netio_setup_connect(td);
888 static void fio_netio_cleanup(struct thread_data *td)
890 struct netio_data *nd = td->io_ops->data;
893 if (nd->listenfd != -1)
895 if (nd->pipes[0] != -1)
897 if (nd->pipes[1] != -1)
904 static int fio_netio_setup(struct thread_data *td)
906 struct netio_data *nd;
908 if (!td->files_index) {
909 add_file(td, td->o.filename ?: "net");
910 td->o.nr_files = td->o.nr_files ?: 1;
913 if (!td->io_ops->data) {
914 nd = malloc(sizeof(*nd));;
916 memset(nd, 0, sizeof(*nd));
918 nd->pipes[0] = nd->pipes[1] = -1;
919 td->io_ops->data = nd;
925 static void fio_netio_terminate(struct thread_data *td)
927 kill(td->pid, SIGUSR2);
930 #ifdef CONFIG_LINUX_SPLICE
931 static int fio_netio_setup_splice(struct thread_data *td)
933 struct netio_data *nd;
937 nd = td->io_ops->data;
939 if (pipe(nd->pipes) < 0)
949 static struct ioengine_ops ioengine_splice = {
951 .version = FIO_IOOPS_VERSION,
952 .prep = fio_netio_prep,
953 .queue = fio_netio_queue,
954 .setup = fio_netio_setup_splice,
955 .init = fio_netio_init,
956 .cleanup = fio_netio_cleanup,
957 .open_file = fio_netio_open_file,
958 .close_file = fio_netio_close_file,
959 .terminate = fio_netio_terminate,
961 .option_struct_size = sizeof(struct netio_options),
962 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
967 static struct ioengine_ops ioengine_rw = {
969 .version = FIO_IOOPS_VERSION,
970 .prep = fio_netio_prep,
971 .queue = fio_netio_queue,
972 .setup = fio_netio_setup,
973 .init = fio_netio_init,
974 .cleanup = fio_netio_cleanup,
975 .open_file = fio_netio_open_file,
976 .close_file = fio_netio_close_file,
977 .terminate = fio_netio_terminate,
979 .option_struct_size = sizeof(struct netio_options),
980 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
981 FIO_PIPEIO | FIO_BIT_BASED,
984 static int str_hostname_cb(void *data, const char *input)
986 struct netio_options *o = data;
988 if (o->td->o.filename)
989 free(o->td->o.filename);
990 o->td->o.filename = strdup(input);
994 static void fio_init fio_netio_register(void)
996 register_ioengine(&ioengine_rw);
997 #ifdef CONFIG_LINUX_SPLICE
998 register_ioengine(&ioengine_splice);
1002 static void fio_exit fio_netio_unregister(void)
1004 unregister_ioengine(&ioengine_rw);
1005 #ifdef CONFIG_LINUX_SPLICE
1006 unregister_ioengine(&ioengine_splice);