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",
68 .off1 = offsetof(struct netio_options, port),
71 .help = "Port to use for TCP or UDP net connections",
77 .off1 = offsetof(struct netio_options, proto),
78 .help = "Network protocol to use",
83 .help = "Transmission Control Protocol",
87 .help = "User Datagram Protocol",
90 .oval = FIO_TYPE_UNIX,
91 .help = "UNIX domain socket",
95 #ifdef CONFIG_TCP_NODELAY
99 .off1 = offsetof(struct netio_options, nodelay),
100 .help = "Use TCP_NODELAY on TCP connections",
105 .type = FIO_OPT_STR_SET,
106 .off1 = offsetof(struct netio_options, listen),
107 .help = "Listen for incoming TCP connections",
111 .type = FIO_OPT_STR_SET,
112 .off1 = offsetof(struct netio_options, pingpong),
113 .help = "Ping-pong IO requests",
121 * Return -1 for error and 'nr events' for a positive number
124 static int poll_wait(struct thread_data *td, int fd, short events)
129 while (!td->terminate) {
132 ret = poll(&pfd, 1, -1);
137 td_verror(td, errno, "poll");
145 if (pfd.revents & events)
151 static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
153 struct netio_options *o = td->eo;
156 * Make sure we don't see spurious reads to a receiver, and vice versa
158 if (o->proto == FIO_TYPE_TCP)
161 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
162 (!o->listen && io_u->ddir == DDIR_READ)) {
163 td_verror(td, EINVAL, "bad direction");
170 #ifdef CONFIG_LINUX_SPLICE
171 static int splice_io_u(int fdin, int fdout, unsigned int len)
176 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
194 * Receive bytes from a socket and fill them into the internal pipe
196 static int splice_in(struct thread_data *td, struct io_u *io_u)
198 struct netio_data *nd = td->io_ops->data;
200 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
204 * Transmit 'len' bytes from the internal pipe
206 static int splice_out(struct thread_data *td, struct io_u *io_u,
209 struct netio_data *nd = td->io_ops->data;
211 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
214 static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
217 .iov_base = io_u->xfer_buf,
222 while (iov.iov_len) {
223 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
242 * vmsplice() pipe to io_u buffer
244 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
247 struct netio_data *nd = td->io_ops->data;
249 return vmsplice_io_u(io_u, nd->pipes[0], len);
253 * vmsplice() io_u to pipe
255 static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
257 struct netio_data *nd = td->io_ops->data;
259 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
263 * splice receive - transfer socket data into a pipe using splice, then map
264 * that pipe data into the io_u using vmsplice.
266 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
270 ret = splice_in(td, io_u);
272 return vmsplice_io_u_out(td, io_u, ret);
278 * splice transmit - map data from the io_u into a pipe by using vmsplice,
279 * then transfer that pipe to a socket using splice.
281 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
285 ret = vmsplice_io_u_in(td, io_u);
287 return splice_out(td, io_u, ret);
292 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
298 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
305 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
307 struct netio_data *nd = td->io_ops->data;
308 struct netio_options *o = td->eo;
312 if (o->proto == FIO_TYPE_UDP) {
313 struct sockaddr *to = (struct sockaddr *) &nd->addr;
315 ret = sendto(io_u->file->fd, io_u->xfer_buf,
316 io_u->xfer_buflen, flags, to,
320 * if we are going to write more, set MSG_MORE
323 if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
324 td->o.size) && !o->pingpong)
327 ret = send(io_u->file->fd, io_u->xfer_buf,
328 io_u->xfer_buflen, flags);
333 ret = poll_wait(td, io_u->file->fd, POLLOUT);
341 static int is_udp_close(struct io_u *io_u, int len)
343 struct udp_close_msg *msg;
345 if (len != sizeof(struct udp_close_msg))
348 msg = io_u->xfer_buf;
349 if (ntohl(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC)
351 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
357 static int fio_netio_recv(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 socklen_t len = sizeof(nd->addr);
366 struct sockaddr *from = (struct sockaddr *) &nd->addr;
368 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
369 io_u->xfer_buflen, flags, from, &len);
370 if (is_udp_close(io_u, ret)) {
375 ret = recv(io_u->file->fd, io_u->xfer_buf,
376 io_u->xfer_buflen, flags);
380 else if (!ret && (flags & MSG_WAITALL))
383 ret = poll_wait(td, io_u->file->fd, POLLIN);
386 flags |= MSG_WAITALL;
392 static int __fio_netio_queue(struct thread_data *td, struct io_u *io_u,
395 struct netio_data *nd = td->io_ops->data;
396 struct netio_options *o = td->eo;
399 if (ddir == DDIR_WRITE) {
400 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
401 o->proto == FIO_TYPE_UNIX)
402 ret = fio_netio_send(td, io_u);
404 ret = fio_netio_splice_out(td, io_u);
405 } else if (ddir == DDIR_READ) {
406 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
407 o->proto == FIO_TYPE_UNIX)
408 ret = fio_netio_recv(td, io_u);
410 ret = fio_netio_splice_in(td, io_u);
412 ret = 0; /* must be a SYNC */
414 if (ret != (int) io_u->xfer_buflen) {
416 io_u->resid = io_u->xfer_buflen - ret;
418 return FIO_Q_COMPLETED;
422 if (ddir == DDIR_WRITE && err == EMSGSIZE)
430 td_verror(td, io_u->error, "xfer");
432 return FIO_Q_COMPLETED;
435 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
437 struct netio_options *o = td->eo;
440 fio_ro_check(td, io_u);
442 ret = __fio_netio_queue(td, io_u, io_u->ddir);
443 if (!o->pingpong || ret != FIO_Q_COMPLETED)
447 * For ping-pong mode, receive or send reply as needed
449 if (td_read(td) && io_u->ddir == DDIR_READ)
450 ret = __fio_netio_queue(td, io_u, DDIR_WRITE);
451 else if (td_write(td) && io_u->ddir == DDIR_WRITE)
452 ret = __fio_netio_queue(td, io_u, DDIR_READ);
457 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
459 struct netio_data *nd = td->io_ops->data;
460 struct netio_options *o = td->eo;
463 if (o->proto == FIO_TYPE_TCP) {
466 } else if (o->proto == FIO_TYPE_UDP) {
469 } else if (o->proto == FIO_TYPE_UNIX) {
473 log_err("fio: bad network type %d\n", o->proto);
478 f->fd = socket(domain, type, 0);
480 td_verror(td, errno, "socket");
484 #ifdef CONFIG_TCP_NODELAY
485 if (o->nodelay && o->proto == FIO_TYPE_TCP) {
488 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
489 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
495 if (o->proto == FIO_TYPE_UDP)
497 else if (o->proto == FIO_TYPE_TCP) {
498 socklen_t len = sizeof(nd->addr);
500 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
501 td_verror(td, errno, "connect");
506 struct sockaddr_un *addr = &nd->addr_un;
509 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
511 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
512 td_verror(td, errno, "connect");
521 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
523 struct netio_data *nd = td->io_ops->data;
524 struct netio_options *o = td->eo;
525 socklen_t socklen = sizeof(nd->addr);
528 if (o->proto == FIO_TYPE_UDP) {
529 f->fd = nd->listenfd;
533 state = td->runstate;
534 td_set_runstate(td, TD_SETTING_UP);
536 log_info("fio: waiting for connection\n");
538 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
541 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
543 td_verror(td, errno, "accept");
547 #ifdef CONFIG_TCP_NODELAY
548 if (o->nodelay && o->proto == FIO_TYPE_TCP) {
551 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
552 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
559 td_set_runstate(td, state);
562 td_set_runstate(td, state);
566 static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
568 struct netio_data *nd = td->io_ops->data;
569 struct udp_close_msg msg;
570 struct sockaddr *to = (struct sockaddr *) &nd->addr;
573 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
574 msg.cmd = htonl(FIO_LINK_CLOSE);
576 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to,
579 td_verror(td, errno, "sendto udp link close");
582 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
584 struct netio_options *o = td->eo;
587 * If this is an UDP connection, notify the receiver that we are
588 * closing down the link
590 if (o->proto == FIO_TYPE_UDP)
591 fio_netio_udp_close(td, f);
593 return generic_close_file(td, f);
596 static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
598 struct netio_data *nd = td->io_ops->data;
599 struct udp_close_msg msg;
600 struct sockaddr *to = (struct sockaddr *) &nd->addr;
601 socklen_t len = sizeof(nd->addr);
604 ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
606 td_verror(td, errno, "sendto udp link open");
610 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
611 ntohl(msg.cmd) != FIO_LINK_OPEN) {
612 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
620 static int fio_netio_udp_send_open(struct thread_data *td, struct fio_file *f)
622 struct netio_data *nd = td->io_ops->data;
623 struct udp_close_msg msg;
624 struct sockaddr *to = (struct sockaddr *) &nd->addr;
627 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
628 msg.cmd = htonl(FIO_LINK_OPEN);
630 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to,
633 td_verror(td, errno, "sendto udp link open");
640 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
643 struct netio_options *o = td->eo;
646 ret = fio_netio_accept(td, f);
648 ret = fio_netio_connect(td, f);
655 if (o->proto == FIO_TYPE_UDP) {
657 ret = fio_netio_udp_send_open(td, f);
661 state = td->runstate;
662 td_set_runstate(td, TD_SETTING_UP);
663 ret = fio_netio_udp_recv_open(td, f);
664 td_set_runstate(td, state);
669 fio_netio_close_file(td, f);
674 static int fio_netio_setup_connect_inet(struct thread_data *td,
675 const char *host, unsigned short port)
677 struct netio_data *nd = td->io_ops->data;
680 log_err("fio: connect with no host to connect to.\n");
682 log_err("fio: did you forget to set 'listen'?\n");
684 td_verror(td, EINVAL, "no hostname= set");
688 nd->addr.sin_family = AF_INET;
689 nd->addr.sin_port = htons(port);
691 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
692 struct hostent *hent;
694 hent = gethostbyname(host);
696 td_verror(td, errno, "gethostbyname");
700 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
706 static int fio_netio_setup_connect_unix(struct thread_data *td,
709 struct netio_data *nd = td->io_ops->data;
710 struct sockaddr_un *soun = &nd->addr_un;
712 soun->sun_family = AF_UNIX;
713 strcpy(soun->sun_path, path);
717 static int fio_netio_setup_connect(struct thread_data *td)
719 struct netio_options *o = td->eo;
721 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
722 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
724 return fio_netio_setup_connect_unix(td, td->o.filename);
727 static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
729 struct netio_data *nd = td->io_ops->data;
730 struct sockaddr_un *addr = &nd->addr_un;
734 fd = socket(AF_UNIX, SOCK_STREAM, 0);
736 log_err("fio: socket: %s\n", strerror(errno));
742 memset(addr, 0, sizeof(*addr));
743 addr->sun_family = AF_UNIX;
744 strcpy(addr->sun_path, path);
747 len = sizeof(addr->sun_family) + strlen(path) + 1;
749 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
750 log_err("fio: bind: %s\n", strerror(errno));
760 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
762 struct netio_data *nd = td->io_ops->data;
763 struct netio_options *o = td->eo;
766 if (o->proto == FIO_TYPE_TCP)
771 fd = socket(AF_INET, type, 0);
773 td_verror(td, errno, "socket");
778 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
779 td_verror(td, errno, "setsockopt");
783 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) {
784 td_verror(td, errno, "setsockopt");
789 nd->addr.sin_family = AF_INET;
790 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
791 nd->addr.sin_port = htons(port);
793 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
794 td_verror(td, errno, "bind");
802 static int fio_netio_setup_listen(struct thread_data *td)
804 struct netio_data *nd = td->io_ops->data;
805 struct netio_options *o = td->eo;
808 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
809 ret = fio_netio_setup_listen_inet(td, o->port);
811 ret = fio_netio_setup_listen_unix(td, td->o.filename);
815 if (o->proto == FIO_TYPE_UDP)
818 if (listen(nd->listenfd, 10) < 0) {
819 td_verror(td, errno, "listen");
827 static int fio_netio_init(struct thread_data *td)
829 struct netio_options *o = td->eo;
834 WSAStartup(MAKEWORD(2,2), &wsd);
838 log_err("fio: network IO can't be random\n");
842 if (o->proto == FIO_TYPE_UNIX && o->port) {
843 log_err("fio: network IO port not valid with unix socket\n");
845 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
846 log_err("fio: network IO requires port for tcp or udp\n");
850 if (o->proto != FIO_TYPE_TCP) {
852 log_err("fio: listen only valid for TCP proto IO\n");
856 log_err("fio: datagram network connections must be"
860 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
861 log_err("fio: UNIX sockets need host/filename\n");
864 o->listen = td_read(td);
867 if (o->proto != FIO_TYPE_UNIX && o->listen && td->o.filename) {
868 log_err("fio: hostname not valid for inbound network IO\n");
873 ret = fio_netio_setup_listen(td);
875 ret = fio_netio_setup_connect(td);
880 static void fio_netio_cleanup(struct thread_data *td)
882 struct netio_data *nd = td->io_ops->data;
885 if (nd->listenfd != -1)
887 if (nd->pipes[0] != -1)
889 if (nd->pipes[1] != -1)
896 static int fio_netio_setup(struct thread_data *td)
898 struct netio_data *nd;
900 if (!td->files_index) {
901 add_file(td, td->o.filename ?: "net");
902 td->o.nr_files = td->o.nr_files ?: 1;
905 if (!td->io_ops->data) {
906 nd = malloc(sizeof(*nd));;
908 memset(nd, 0, sizeof(*nd));
910 nd->pipes[0] = nd->pipes[1] = -1;
911 td->io_ops->data = nd;
917 static void fio_netio_terminate(struct thread_data *td)
919 kill(td->pid, SIGUSR2);
922 #ifdef CONFIG_LINUX_SPLICE
923 static int fio_netio_setup_splice(struct thread_data *td)
925 struct netio_data *nd;
929 nd = td->io_ops->data;
931 if (pipe(nd->pipes) < 0)
941 static struct ioengine_ops ioengine_splice = {
943 .version = FIO_IOOPS_VERSION,
944 .prep = fio_netio_prep,
945 .queue = fio_netio_queue,
946 .setup = fio_netio_setup_splice,
947 .init = fio_netio_init,
948 .cleanup = fio_netio_cleanup,
949 .open_file = fio_netio_open_file,
950 .close_file = fio_netio_close_file,
951 .terminate = fio_netio_terminate,
953 .option_struct_size = sizeof(struct netio_options),
954 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
959 static struct ioengine_ops ioengine_rw = {
961 .version = FIO_IOOPS_VERSION,
962 .prep = fio_netio_prep,
963 .queue = fio_netio_queue,
964 .setup = fio_netio_setup,
965 .init = fio_netio_init,
966 .cleanup = fio_netio_cleanup,
967 .open_file = fio_netio_open_file,
968 .close_file = fio_netio_close_file,
969 .terminate = fio_netio_terminate,
971 .option_struct_size = sizeof(struct netio_options),
972 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
976 static int str_hostname_cb(void *data, const char *input)
978 struct netio_options *o = data;
980 if (o->td->o.filename)
981 free(o->td->o.filename);
982 o->td->o.filename = strdup(input);
986 static void fio_init fio_netio_register(void)
988 register_ioengine(&ioengine_rw);
989 #ifdef CONFIG_LINUX_SPLICE
990 register_ioengine(&ioengine_splice);
994 static void fio_exit fio_netio_unregister(void)
996 unregister_ioengine(&ioengine_rw);
997 #ifdef CONFIG_LINUX_SPLICE
998 unregister_ioengine(&ioengine_splice);