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;
461 int type, domain, optval;
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) {
487 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
488 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
494 if (o->proto == FIO_TYPE_UDP)
496 else if (o->proto == FIO_TYPE_TCP) {
497 socklen_t len = sizeof(nd->addr);
499 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
500 td_verror(td, errno, "connect");
505 struct sockaddr_un *addr = &nd->addr_un;
508 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
510 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
511 td_verror(td, errno, "connect");
520 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
522 struct netio_data *nd = td->io_ops->data;
523 struct netio_options *o = td->eo;
524 socklen_t socklen = sizeof(nd->addr);
527 if (o->proto == FIO_TYPE_UDP) {
528 f->fd = nd->listenfd;
532 state = td->runstate;
533 td_set_runstate(td, TD_SETTING_UP);
535 log_info("fio: waiting for connection\n");
537 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
540 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
542 td_verror(td, errno, "accept");
546 #ifdef CONFIG_TCP_NODELAY
547 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));
557 td_set_runstate(td, state);
560 td_set_runstate(td, state);
564 static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
566 struct netio_data *nd = td->io_ops->data;
567 struct udp_close_msg msg;
568 struct sockaddr *to = (struct sockaddr *) &nd->addr;
571 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
572 msg.cmd = htonl(FIO_LINK_CLOSE);
574 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to,
577 td_verror(td, errno, "sendto udp link close");
580 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
582 struct netio_options *o = td->eo;
585 * If this is an UDP connection, notify the receiver that we are
586 * closing down the link
588 if (o->proto == FIO_TYPE_UDP)
589 fio_netio_udp_close(td, f);
591 return generic_close_file(td, f);
594 static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
596 struct netio_data *nd = td->io_ops->data;
597 struct udp_close_msg msg;
598 struct sockaddr *to = (struct sockaddr *) &nd->addr;
599 socklen_t len = sizeof(nd->addr);
602 ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
604 td_verror(td, errno, "sendto udp link open");
608 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
609 ntohl(msg.cmd) != FIO_LINK_OPEN) {
610 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
618 static int fio_netio_udp_send_open(struct thread_data *td, struct fio_file *f)
620 struct netio_data *nd = td->io_ops->data;
621 struct udp_close_msg msg;
622 struct sockaddr *to = (struct sockaddr *) &nd->addr;
625 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
626 msg.cmd = htonl(FIO_LINK_OPEN);
628 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to,
631 td_verror(td, errno, "sendto udp link open");
638 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
641 struct netio_options *o = td->eo;
644 ret = fio_netio_accept(td, f);
646 ret = fio_netio_connect(td, f);
653 if (o->proto == FIO_TYPE_UDP) {
655 ret = fio_netio_udp_send_open(td, f);
659 state = td->runstate;
660 td_set_runstate(td, TD_SETTING_UP);
661 ret = fio_netio_udp_recv_open(td, f);
662 td_set_runstate(td, state);
667 fio_netio_close_file(td, f);
672 static int fio_netio_setup_connect_inet(struct thread_data *td,
673 const char *host, unsigned short port)
675 struct netio_data *nd = td->io_ops->data;
678 log_err("fio: connect with no host to connect to.\n");
680 log_err("fio: did you forget to set 'listen'?\n");
682 td_verror(td, EINVAL, "no hostname= set");
686 nd->addr.sin_family = AF_INET;
687 nd->addr.sin_port = htons(port);
689 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
690 struct hostent *hent;
692 hent = gethostbyname(host);
694 td_verror(td, errno, "gethostbyname");
698 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
704 static int fio_netio_setup_connect_unix(struct thread_data *td,
707 struct netio_data *nd = td->io_ops->data;
708 struct sockaddr_un *soun = &nd->addr_un;
710 soun->sun_family = AF_UNIX;
711 strcpy(soun->sun_path, path);
715 static int fio_netio_setup_connect(struct thread_data *td)
717 struct netio_options *o = td->eo;
719 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
720 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
722 return fio_netio_setup_connect_unix(td, td->o.filename);
725 static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
727 struct netio_data *nd = td->io_ops->data;
728 struct sockaddr_un *addr = &nd->addr_un;
732 fd = socket(AF_UNIX, SOCK_STREAM, 0);
734 log_err("fio: socket: %s\n", strerror(errno));
740 memset(addr, 0, sizeof(*addr));
741 addr->sun_family = AF_UNIX;
742 strcpy(addr->sun_path, path);
745 len = sizeof(addr->sun_family) + strlen(path) + 1;
747 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
748 log_err("fio: bind: %s\n", strerror(errno));
758 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
760 struct netio_data *nd = td->io_ops->data;
761 struct netio_options *o = td->eo;
764 if (o->proto == FIO_TYPE_TCP)
769 fd = socket(AF_INET, type, 0);
771 td_verror(td, errno, "socket");
776 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
777 td_verror(td, errno, "setsockopt");
781 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) {
782 td_verror(td, errno, "setsockopt");
787 nd->addr.sin_family = AF_INET;
788 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
789 nd->addr.sin_port = htons(port);
791 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
792 td_verror(td, errno, "bind");
800 static int fio_netio_setup_listen(struct thread_data *td)
802 struct netio_data *nd = td->io_ops->data;
803 struct netio_options *o = td->eo;
806 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
807 ret = fio_netio_setup_listen_inet(td, o->port);
809 ret = fio_netio_setup_listen_unix(td, td->o.filename);
813 if (o->proto == FIO_TYPE_UDP)
816 if (listen(nd->listenfd, 10) < 0) {
817 td_verror(td, errno, "listen");
825 static int fio_netio_init(struct thread_data *td)
827 struct netio_options *o = td->eo;
832 WSAStartup(MAKEWORD(2,2), &wsd);
836 log_err("fio: network IO can't be random\n");
840 if (o->proto == FIO_TYPE_UNIX && o->port) {
841 log_err("fio: network IO port not valid with unix socket\n");
843 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
844 log_err("fio: network IO requires port for tcp or udp\n");
848 if (o->proto != FIO_TYPE_TCP) {
850 log_err("fio: listen only valid for TCP proto IO\n");
854 log_err("fio: datagram network connections must be"
858 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
859 log_err("fio: UNIX sockets need host/filename\n");
862 o->listen = td_read(td);
865 if (o->proto != FIO_TYPE_UNIX && o->listen && td->o.filename) {
866 log_err("fio: hostname not valid for inbound network IO\n");
871 ret = fio_netio_setup_listen(td);
873 ret = fio_netio_setup_connect(td);
878 static void fio_netio_cleanup(struct thread_data *td)
880 struct netio_data *nd = td->io_ops->data;
883 if (nd->listenfd != -1)
885 if (nd->pipes[0] != -1)
887 if (nd->pipes[1] != -1)
894 static int fio_netio_setup(struct thread_data *td)
896 struct netio_data *nd;
898 if (!td->files_index) {
899 add_file(td, td->o.filename ?: "net");
900 td->o.nr_files = td->o.nr_files ?: 1;
903 if (!td->io_ops->data) {
904 nd = malloc(sizeof(*nd));;
906 memset(nd, 0, sizeof(*nd));
908 nd->pipes[0] = nd->pipes[1] = -1;
909 td->io_ops->data = nd;
915 static void fio_netio_terminate(struct thread_data *td)
917 kill(td->pid, SIGUSR2);
920 #ifdef CONFIG_LINUX_SPLICE
921 static int fio_netio_setup_splice(struct thread_data *td)
923 struct netio_data *nd;
927 nd = td->io_ops->data;
929 if (pipe(nd->pipes) < 0)
939 static struct ioengine_ops ioengine_splice = {
941 .version = FIO_IOOPS_VERSION,
942 .prep = fio_netio_prep,
943 .queue = fio_netio_queue,
944 .setup = fio_netio_setup_splice,
945 .init = fio_netio_init,
946 .cleanup = fio_netio_cleanup,
947 .open_file = fio_netio_open_file,
948 .close_file = fio_netio_close_file,
949 .terminate = fio_netio_terminate,
951 .option_struct_size = sizeof(struct netio_options),
952 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
957 static struct ioengine_ops ioengine_rw = {
959 .version = FIO_IOOPS_VERSION,
960 .prep = fio_netio_prep,
961 .queue = fio_netio_queue,
962 .setup = fio_netio_setup,
963 .init = fio_netio_init,
964 .cleanup = fio_netio_cleanup,
965 .open_file = fio_netio_open_file,
966 .close_file = fio_netio_close_file,
967 .terminate = fio_netio_terminate,
969 .option_struct_size = sizeof(struct netio_options),
970 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
974 static int str_hostname_cb(void *data, const char *input)
976 struct netio_options *o = data;
978 if (o->td->o.filename)
979 free(o->td->o.filename);
980 o->td->o.filename = strdup(input);
984 static void fio_init fio_netio_register(void)
986 register_ioengine(&ioengine_rw);
987 #ifdef CONFIG_LINUX_SPLICE
988 register_ioengine(&ioengine_splice);
992 static void fio_exit fio_netio_unregister(void)
994 unregister_ioengine(&ioengine_rw);
995 #ifdef CONFIG_LINUX_SPLICE
996 unregister_ioengine(&ioengine_splice);