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_ENGINE,
66 .group = FIO_OPT_G_NETIO,
70 .lname = "net engine port",
72 .off1 = offsetof(struct netio_options, port),
75 .help = "Port to use for TCP or UDP net connections",
76 .category = FIO_OPT_C_ENGINE,
77 .group = FIO_OPT_G_NETIO,
81 .lname = "net engine protocol",
84 .off1 = offsetof(struct netio_options, proto),
85 .help = "Network protocol to use",
87 .category = FIO_OPT_C_IO,
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,
138 * Return -1 for error and 'nr events' for a positive number
141 static int poll_wait(struct thread_data *td, int fd, short events)
146 while (!td->terminate) {
149 ret = poll(&pfd, 1, -1);
154 td_verror(td, errno, "poll");
162 if (pfd.revents & events)
168 static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
170 struct netio_options *o = td->eo;
173 * Make sure we don't see spurious reads to a receiver, and vice versa
175 if (o->proto == FIO_TYPE_TCP)
178 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
179 (!o->listen && io_u->ddir == DDIR_READ)) {
180 td_verror(td, EINVAL, "bad direction");
187 #ifdef CONFIG_LINUX_SPLICE
188 static int splice_io_u(int fdin, int fdout, unsigned int len)
193 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
211 * Receive bytes from a socket and fill them into the internal pipe
213 static int splice_in(struct thread_data *td, struct io_u *io_u)
215 struct netio_data *nd = td->io_ops->data;
217 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
221 * Transmit 'len' bytes from the internal pipe
223 static int splice_out(struct thread_data *td, struct io_u *io_u,
226 struct netio_data *nd = td->io_ops->data;
228 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
231 static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
234 .iov_base = io_u->xfer_buf,
239 while (iov.iov_len) {
240 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
259 * vmsplice() pipe to io_u buffer
261 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
264 struct netio_data *nd = td->io_ops->data;
266 return vmsplice_io_u(io_u, nd->pipes[0], len);
270 * vmsplice() io_u to pipe
272 static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
274 struct netio_data *nd = td->io_ops->data;
276 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
280 * splice receive - transfer socket data into a pipe using splice, then map
281 * that pipe data into the io_u using vmsplice.
283 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
287 ret = splice_in(td, io_u);
289 return vmsplice_io_u_out(td, io_u, ret);
295 * splice transmit - map data from the io_u into a pipe by using vmsplice,
296 * then transfer that pipe to a socket using splice.
298 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
302 ret = vmsplice_io_u_in(td, io_u);
304 return splice_out(td, io_u, ret);
309 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
315 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
322 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
324 struct netio_data *nd = td->io_ops->data;
325 struct netio_options *o = td->eo;
329 if (o->proto == FIO_TYPE_UDP) {
330 struct sockaddr *to = (struct sockaddr *) &nd->addr;
332 ret = sendto(io_u->file->fd, io_u->xfer_buf,
333 io_u->xfer_buflen, flags, to,
337 * if we are going to write more, set MSG_MORE
340 if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
341 td->o.size) && !o->pingpong)
344 ret = send(io_u->file->fd, io_u->xfer_buf,
345 io_u->xfer_buflen, flags);
350 ret = poll_wait(td, io_u->file->fd, POLLOUT);
358 static int is_udp_close(struct io_u *io_u, int len)
360 struct udp_close_msg *msg;
362 if (len != sizeof(struct udp_close_msg))
365 msg = io_u->xfer_buf;
366 if (ntohl(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC)
368 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
374 static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
376 struct netio_data *nd = td->io_ops->data;
377 struct netio_options *o = td->eo;
381 if (o->proto == FIO_TYPE_UDP) {
382 socklen_t len = sizeof(nd->addr);
383 struct sockaddr *from = (struct sockaddr *) &nd->addr;
385 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
386 io_u->xfer_buflen, flags, from, &len);
387 if (is_udp_close(io_u, ret)) {
392 ret = recv(io_u->file->fd, io_u->xfer_buf,
393 io_u->xfer_buflen, flags);
397 else if (!ret && (flags & MSG_WAITALL))
400 ret = poll_wait(td, io_u->file->fd, POLLIN);
403 flags |= MSG_WAITALL;
409 static int __fio_netio_queue(struct thread_data *td, struct io_u *io_u,
412 struct netio_data *nd = td->io_ops->data;
413 struct netio_options *o = td->eo;
416 if (ddir == DDIR_WRITE) {
417 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
418 o->proto == FIO_TYPE_UNIX)
419 ret = fio_netio_send(td, io_u);
421 ret = fio_netio_splice_out(td, io_u);
422 } else if (ddir == DDIR_READ) {
423 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
424 o->proto == FIO_TYPE_UNIX)
425 ret = fio_netio_recv(td, io_u);
427 ret = fio_netio_splice_in(td, io_u);
429 ret = 0; /* must be a SYNC */
431 if (ret != (int) io_u->xfer_buflen) {
433 io_u->resid = io_u->xfer_buflen - ret;
435 return FIO_Q_COMPLETED;
439 if (ddir == DDIR_WRITE && err == EMSGSIZE)
447 td_verror(td, io_u->error, "xfer");
449 return FIO_Q_COMPLETED;
452 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
454 struct netio_options *o = td->eo;
457 fio_ro_check(td, io_u);
459 ret = __fio_netio_queue(td, io_u, io_u->ddir);
460 if (!o->pingpong || ret != FIO_Q_COMPLETED)
464 * For ping-pong mode, receive or send reply as needed
466 if (td_read(td) && io_u->ddir == DDIR_READ)
467 ret = __fio_netio_queue(td, io_u, DDIR_WRITE);
468 else if (td_write(td) && io_u->ddir == DDIR_WRITE)
469 ret = __fio_netio_queue(td, io_u, DDIR_READ);
474 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
476 struct netio_data *nd = td->io_ops->data;
477 struct netio_options *o = td->eo;
480 if (o->proto == FIO_TYPE_TCP) {
483 } else if (o->proto == FIO_TYPE_UDP) {
486 } else if (o->proto == FIO_TYPE_UNIX) {
490 log_err("fio: bad network type %d\n", o->proto);
495 f->fd = socket(domain, type, 0);
497 td_verror(td, errno, "socket");
501 #ifdef CONFIG_TCP_NODELAY
502 if (o->nodelay && o->proto == FIO_TYPE_TCP) {
505 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
506 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
512 if (o->proto == FIO_TYPE_UDP)
514 else if (o->proto == FIO_TYPE_TCP) {
515 socklen_t len = sizeof(nd->addr);
517 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
518 td_verror(td, errno, "connect");
523 struct sockaddr_un *addr = &nd->addr_un;
526 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
528 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
529 td_verror(td, errno, "connect");
538 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
540 struct netio_data *nd = td->io_ops->data;
541 struct netio_options *o = td->eo;
542 socklen_t socklen = sizeof(nd->addr);
545 if (o->proto == FIO_TYPE_UDP) {
546 f->fd = nd->listenfd;
550 state = td->runstate;
551 td_set_runstate(td, TD_SETTING_UP);
553 log_info("fio: waiting for connection\n");
555 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
558 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
560 td_verror(td, errno, "accept");
564 #ifdef CONFIG_TCP_NODELAY
565 if (o->nodelay && o->proto == FIO_TYPE_TCP) {
568 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
569 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
576 td_set_runstate(td, state);
579 td_set_runstate(td, state);
583 static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
585 struct netio_data *nd = td->io_ops->data;
586 struct udp_close_msg msg;
587 struct sockaddr *to = (struct sockaddr *) &nd->addr;
590 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
591 msg.cmd = htonl(FIO_LINK_CLOSE);
593 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to,
596 td_verror(td, errno, "sendto udp link close");
599 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
601 struct netio_options *o = td->eo;
604 * If this is an UDP connection, notify the receiver that we are
605 * closing down the link
607 if (o->proto == FIO_TYPE_UDP)
608 fio_netio_udp_close(td, f);
610 return generic_close_file(td, f);
613 static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
615 struct netio_data *nd = td->io_ops->data;
616 struct udp_close_msg msg;
617 struct sockaddr *to = (struct sockaddr *) &nd->addr;
618 socklen_t len = sizeof(nd->addr);
621 ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
623 td_verror(td, errno, "sendto udp link open");
627 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
628 ntohl(msg.cmd) != FIO_LINK_OPEN) {
629 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
637 static int fio_netio_udp_send_open(struct thread_data *td, struct fio_file *f)
639 struct netio_data *nd = td->io_ops->data;
640 struct udp_close_msg msg;
641 struct sockaddr *to = (struct sockaddr *) &nd->addr;
644 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
645 msg.cmd = htonl(FIO_LINK_OPEN);
647 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to,
650 td_verror(td, errno, "sendto udp link open");
657 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
660 struct netio_options *o = td->eo;
663 ret = fio_netio_accept(td, f);
665 ret = fio_netio_connect(td, f);
672 if (o->proto == FIO_TYPE_UDP) {
674 ret = fio_netio_udp_send_open(td, f);
678 state = td->runstate;
679 td_set_runstate(td, TD_SETTING_UP);
680 ret = fio_netio_udp_recv_open(td, f);
681 td_set_runstate(td, state);
686 fio_netio_close_file(td, f);
691 static int fio_netio_setup_connect_inet(struct thread_data *td,
692 const char *host, unsigned short port)
694 struct netio_data *nd = td->io_ops->data;
697 log_err("fio: connect with no host to connect to.\n");
699 log_err("fio: did you forget to set 'listen'?\n");
701 td_verror(td, EINVAL, "no hostname= set");
705 nd->addr.sin_family = AF_INET;
706 nd->addr.sin_port = htons(port);
708 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
709 struct hostent *hent;
711 hent = gethostbyname(host);
713 td_verror(td, errno, "gethostbyname");
717 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
723 static int fio_netio_setup_connect_unix(struct thread_data *td,
726 struct netio_data *nd = td->io_ops->data;
727 struct sockaddr_un *soun = &nd->addr_un;
729 soun->sun_family = AF_UNIX;
730 strcpy(soun->sun_path, path);
734 static int fio_netio_setup_connect(struct thread_data *td)
736 struct netio_options *o = td->eo;
738 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
739 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
741 return fio_netio_setup_connect_unix(td, td->o.filename);
744 static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
746 struct netio_data *nd = td->io_ops->data;
747 struct sockaddr_un *addr = &nd->addr_un;
751 fd = socket(AF_UNIX, SOCK_STREAM, 0);
753 log_err("fio: socket: %s\n", strerror(errno));
759 memset(addr, 0, sizeof(*addr));
760 addr->sun_family = AF_UNIX;
761 strcpy(addr->sun_path, path);
764 len = sizeof(addr->sun_family) + strlen(path) + 1;
766 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
767 log_err("fio: bind: %s\n", strerror(errno));
777 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
779 struct netio_data *nd = td->io_ops->data;
780 struct netio_options *o = td->eo;
783 if (o->proto == FIO_TYPE_TCP)
788 fd = socket(AF_INET, type, 0);
790 td_verror(td, errno, "socket");
795 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
796 td_verror(td, errno, "setsockopt");
800 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) {
801 td_verror(td, errno, "setsockopt");
806 nd->addr.sin_family = AF_INET;
807 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
808 nd->addr.sin_port = htons(port);
810 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
811 td_verror(td, errno, "bind");
819 static int fio_netio_setup_listen(struct thread_data *td)
821 struct netio_data *nd = td->io_ops->data;
822 struct netio_options *o = td->eo;
825 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
826 ret = fio_netio_setup_listen_inet(td, o->port);
828 ret = fio_netio_setup_listen_unix(td, td->o.filename);
832 if (o->proto == FIO_TYPE_UDP)
835 if (listen(nd->listenfd, 10) < 0) {
836 td_verror(td, errno, "listen");
844 static int fio_netio_init(struct thread_data *td)
846 struct netio_options *o = td->eo;
851 WSAStartup(MAKEWORD(2,2), &wsd);
855 log_err("fio: network IO can't be random\n");
859 if (o->proto == FIO_TYPE_UNIX && o->port) {
860 log_err("fio: network IO port not valid with unix socket\n");
862 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
863 log_err("fio: network IO requires port for tcp or udp\n");
867 if (o->proto != FIO_TYPE_TCP) {
869 log_err("fio: listen only valid for TCP proto IO\n");
873 log_err("fio: datagram network connections must be"
877 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
878 log_err("fio: UNIX sockets need host/filename\n");
881 o->listen = td_read(td);
884 if (o->proto != FIO_TYPE_UNIX && o->listen && td->o.filename) {
885 log_err("fio: hostname not valid for inbound network IO\n");
890 ret = fio_netio_setup_listen(td);
892 ret = fio_netio_setup_connect(td);
897 static void fio_netio_cleanup(struct thread_data *td)
899 struct netio_data *nd = td->io_ops->data;
902 if (nd->listenfd != -1)
904 if (nd->pipes[0] != -1)
906 if (nd->pipes[1] != -1)
913 static int fio_netio_setup(struct thread_data *td)
915 struct netio_data *nd;
917 if (!td->files_index) {
918 add_file(td, td->o.filename ?: "net");
919 td->o.nr_files = td->o.nr_files ?: 1;
922 if (!td->io_ops->data) {
923 nd = malloc(sizeof(*nd));;
925 memset(nd, 0, sizeof(*nd));
927 nd->pipes[0] = nd->pipes[1] = -1;
928 td->io_ops->data = nd;
934 static void fio_netio_terminate(struct thread_data *td)
936 kill(td->pid, SIGUSR2);
939 #ifdef CONFIG_LINUX_SPLICE
940 static int fio_netio_setup_splice(struct thread_data *td)
942 struct netio_data *nd;
946 nd = td->io_ops->data;
948 if (pipe(nd->pipes) < 0)
958 static struct ioengine_ops ioengine_splice = {
960 .version = FIO_IOOPS_VERSION,
961 .prep = fio_netio_prep,
962 .queue = fio_netio_queue,
963 .setup = fio_netio_setup_splice,
964 .init = fio_netio_init,
965 .cleanup = fio_netio_cleanup,
966 .open_file = fio_netio_open_file,
967 .close_file = fio_netio_close_file,
968 .terminate = fio_netio_terminate,
970 .option_struct_size = sizeof(struct netio_options),
971 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
976 static struct ioengine_ops ioengine_rw = {
978 .version = FIO_IOOPS_VERSION,
979 .prep = fio_netio_prep,
980 .queue = fio_netio_queue,
981 .setup = fio_netio_setup,
982 .init = fio_netio_init,
983 .cleanup = fio_netio_cleanup,
984 .open_file = fio_netio_open_file,
985 .close_file = fio_netio_close_file,
986 .terminate = fio_netio_terminate,
988 .option_struct_size = sizeof(struct netio_options),
989 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
990 FIO_PIPEIO | FIO_BIT_BASED,
993 static int str_hostname_cb(void *data, const char *input)
995 struct netio_options *o = data;
997 if (o->td->o.filename)
998 free(o->td->o.filename);
999 o->td->o.filename = strdup(input);
1003 static void fio_init fio_netio_register(void)
1005 register_ioengine(&ioengine_rw);
1006 #ifdef CONFIG_LINUX_SPLICE
1007 register_ioengine(&ioengine_splice);
1011 static void fio_exit fio_netio_unregister(void)
1013 unregister_ioengine(&ioengine_rw);
1014 #ifdef CONFIG_LINUX_SPLICE
1015 unregister_ioengine(&ioengine_splice);