4 * IO engine that reads/writes to/from sockets.
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
17 #include <sys/types.h>
19 #include <sys/socket.h>
28 struct sockaddr_in addr;
29 struct sockaddr_un addr_un;
32 struct netio_options {
33 struct thread_data *td;
37 unsigned int pingpong;
40 struct udp_close_msg {
46 FIO_LINK_CLOSE = 0x89,
47 FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b,
55 static int str_hostname_cb(void *data, const char *input);
56 static struct fio_option options[] = {
59 .lname = "net engine hostname",
60 .type = FIO_OPT_STR_STORE,
61 .cb = str_hostname_cb,
62 .help = "Hostname for net IO engine",
63 .category = FIO_OPT_C_IO,
67 .lname = "net engine port",
69 .off1 = offsetof(struct netio_options, port),
72 .help = "Port to use for TCP or UDP net connections",
73 .category = FIO_OPT_C_IO,
77 .lname = "net engine protocol",
80 .off1 = offsetof(struct netio_options, proto),
81 .help = "Network protocol to use",
83 .category = FIO_OPT_C_IO,
87 .help = "Transmission Control Protocol",
91 .help = "User Datagram Protocol",
94 .oval = FIO_TYPE_UNIX,
95 .help = "UNIX domain socket",
101 .lname = "net engine listen",
102 .type = FIO_OPT_STR_SET,
103 .off1 = offsetof(struct netio_options, listen),
104 .help = "Listen for incoming TCP connections",
105 .category = FIO_OPT_C_IO,
109 .type = FIO_OPT_STR_SET,
110 .off1 = offsetof(struct netio_options, pingpong),
111 .help = "Ping-pong IO requests",
119 * Return -1 for error and 'nr events' for a positive number
122 static int poll_wait(struct thread_data *td, int fd, short events)
127 while (!td->terminate) {
130 ret = poll(&pfd, 1, -1);
135 td_verror(td, errno, "poll");
143 if (pfd.revents & events)
149 static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
151 struct netio_options *o = td->eo;
154 * Make sure we don't see spurious reads to a receiver, and vice versa
156 if (o->proto == FIO_TYPE_TCP)
159 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
160 (!o->listen && io_u->ddir == DDIR_READ)) {
161 td_verror(td, EINVAL, "bad direction");
168 #ifdef FIO_HAVE_SPLICE
169 static int splice_io_u(int fdin, int fdout, unsigned int len)
174 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
192 * Receive bytes from a socket and fill them into the internal pipe
194 static int splice_in(struct thread_data *td, struct io_u *io_u)
196 struct netio_data *nd = td->io_ops->data;
198 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
202 * Transmit 'len' bytes from the internal pipe
204 static int splice_out(struct thread_data *td, struct io_u *io_u,
207 struct netio_data *nd = td->io_ops->data;
209 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
212 static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
215 .iov_base = io_u->xfer_buf,
220 while (iov.iov_len) {
221 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
240 * vmsplice() pipe to io_u buffer
242 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
245 struct netio_data *nd = td->io_ops->data;
247 return vmsplice_io_u(io_u, nd->pipes[0], len);
251 * vmsplice() io_u to pipe
253 static int vmsplice_io_u_in(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[1], io_u->xfer_buflen);
261 * splice receive - transfer socket data into a pipe using splice, then map
262 * that pipe data into the io_u using vmsplice.
264 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
268 ret = splice_in(td, io_u);
270 return vmsplice_io_u_out(td, io_u, ret);
276 * splice transmit - map data from the io_u into a pipe by using vmsplice,
277 * then transfer that pipe to a socket using splice.
279 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
283 ret = vmsplice_io_u_in(td, io_u);
285 return splice_out(td, io_u, ret);
290 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
296 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
303 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
305 struct netio_data *nd = td->io_ops->data;
306 struct netio_options *o = td->eo;
310 if (o->proto == FIO_TYPE_UDP) {
311 struct sockaddr *to = (struct sockaddr *) &nd->addr;
313 ret = sendto(io_u->file->fd, io_u->xfer_buf,
314 io_u->xfer_buflen, flags, to,
318 * if we are going to write more, set MSG_MORE
321 if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
322 td->o.size) && !o->pingpong)
325 ret = send(io_u->file->fd, io_u->xfer_buf,
326 io_u->xfer_buflen, flags);
331 ret = poll_wait(td, io_u->file->fd, POLLOUT);
339 static int is_udp_close(struct io_u *io_u, int len)
341 struct udp_close_msg *msg;
343 if (len != sizeof(struct udp_close_msg))
346 msg = io_u->xfer_buf;
347 if (ntohl(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC)
349 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
355 static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
357 struct netio_data *nd = td->io_ops->data;
358 struct netio_options *o = td->eo;
362 if (o->proto == FIO_TYPE_UDP) {
363 fio_socklen_t len = sizeof(nd->addr);
364 struct sockaddr *from = (struct sockaddr *) &nd->addr;
366 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
367 io_u->xfer_buflen, flags, from, &len);
368 if (is_udp_close(io_u, ret)) {
373 ret = recv(io_u->file->fd, io_u->xfer_buf,
374 io_u->xfer_buflen, flags);
378 else if (!ret && (flags & MSG_WAITALL))
381 ret = poll_wait(td, io_u->file->fd, POLLIN);
384 flags |= MSG_WAITALL;
390 static int __fio_netio_queue(struct thread_data *td, struct io_u *io_u,
393 struct netio_data *nd = td->io_ops->data;
394 struct netio_options *o = td->eo;
397 if (ddir == DDIR_WRITE) {
398 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
399 o->proto == FIO_TYPE_UNIX)
400 ret = fio_netio_send(td, io_u);
402 ret = fio_netio_splice_out(td, io_u);
403 } else if (ddir == DDIR_READ) {
404 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
405 o->proto == FIO_TYPE_UNIX)
406 ret = fio_netio_recv(td, io_u);
408 ret = fio_netio_splice_in(td, io_u);
410 ret = 0; /* must be a SYNC */
412 if (ret != (int) io_u->xfer_buflen) {
414 io_u->resid = io_u->xfer_buflen - ret;
416 return FIO_Q_COMPLETED;
420 if (ddir == DDIR_WRITE && err == EMSGSIZE)
428 td_verror(td, io_u->error, "xfer");
430 return FIO_Q_COMPLETED;
433 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
435 struct netio_options *o = td->eo;
438 fio_ro_check(td, io_u);
440 ret = __fio_netio_queue(td, io_u, io_u->ddir);
441 if (!o->pingpong || ret != FIO_Q_COMPLETED)
445 * For ping-pong mode, receive or send reply as needed
447 if (td_read(td) && io_u->ddir == DDIR_READ)
448 ret = __fio_netio_queue(td, io_u, DDIR_WRITE);
449 else if (td_write(td) && io_u->ddir == DDIR_WRITE)
450 ret = __fio_netio_queue(td, io_u, DDIR_READ);
455 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
457 struct netio_data *nd = td->io_ops->data;
458 struct netio_options *o = td->eo;
461 if (o->proto == FIO_TYPE_TCP) {
464 } else if (o->proto == FIO_TYPE_UDP) {
467 } else if (o->proto == FIO_TYPE_UNIX) {
471 log_err("fio: bad network type %d\n", o->proto);
476 f->fd = socket(domain, type, 0);
478 td_verror(td, errno, "socket");
482 if (o->proto == FIO_TYPE_UDP)
484 else if (o->proto == FIO_TYPE_TCP) {
485 fio_socklen_t len = sizeof(nd->addr);
487 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
488 td_verror(td, errno, "connect");
493 struct sockaddr_un *addr = &nd->addr_un;
496 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
498 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
499 td_verror(td, errno, "connect");
508 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
510 struct netio_data *nd = td->io_ops->data;
511 struct netio_options *o = td->eo;
512 fio_socklen_t socklen = sizeof(nd->addr);
515 if (o->proto == FIO_TYPE_UDP) {
516 f->fd = nd->listenfd;
520 state = td->runstate;
521 td_set_runstate(td, TD_SETTING_UP);
523 log_info("fio: waiting for connection\n");
525 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
528 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
530 td_verror(td, errno, "accept");
535 td_set_runstate(td, state);
538 td_set_runstate(td, state);
542 static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
544 struct netio_data *nd = td->io_ops->data;
545 struct udp_close_msg msg;
546 struct sockaddr *to = (struct sockaddr *) &nd->addr;
549 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
550 msg.cmd = htonl(FIO_LINK_CLOSE);
552 ret = sendto(f->fd, &msg, sizeof(msg), MSG_WAITALL, to,
555 td_verror(td, errno, "sendto udp link close");
558 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
560 struct netio_options *o = td->eo;
563 * If this is an UDP connection, notify the receiver that we are
564 * closing down the link
566 if (o->proto == FIO_TYPE_UDP)
567 fio_netio_udp_close(td, f);
569 return generic_close_file(td, f);
572 static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
574 struct netio_data *nd = td->io_ops->data;
575 struct udp_close_msg msg;
576 struct sockaddr *to = (struct sockaddr *) &nd->addr;
577 fio_socklen_t len = sizeof(nd->addr);
580 ret = recvfrom(f->fd, &msg, sizeof(msg), MSG_WAITALL, to, &len);
582 td_verror(td, errno, "sendto udp link open");
586 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
587 ntohl(msg.cmd) != FIO_LINK_OPEN) {
588 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
596 static int fio_netio_udp_send_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;
603 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
604 msg.cmd = htonl(FIO_LINK_OPEN);
606 ret = sendto(f->fd, &msg, sizeof(msg), MSG_WAITALL, to,
609 td_verror(td, errno, "sendto udp link open");
616 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
619 struct netio_options *o = td->eo;
622 ret = fio_netio_accept(td, f);
624 ret = fio_netio_connect(td, f);
631 if (o->proto == FIO_TYPE_UDP) {
633 ret = fio_netio_udp_send_open(td, f);
637 state = td->runstate;
638 td_set_runstate(td, TD_SETTING_UP);
639 ret = fio_netio_udp_recv_open(td, f);
640 td_set_runstate(td, state);
645 fio_netio_close_file(td, f);
650 static int fio_netio_setup_connect_inet(struct thread_data *td,
651 const char *host, unsigned short port)
653 struct netio_data *nd = td->io_ops->data;
656 log_err("fio: connect with no host to connect to.\n");
658 log_err("fio: did you forget to set 'listen'?\n");
660 td_verror(td, EINVAL, "no hostname= set");
664 nd->addr.sin_family = AF_INET;
665 nd->addr.sin_port = htons(port);
667 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
668 struct hostent *hent;
670 hent = gethostbyname(host);
672 td_verror(td, errno, "gethostbyname");
676 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
682 static int fio_netio_setup_connect_unix(struct thread_data *td,
685 struct netio_data *nd = td->io_ops->data;
686 struct sockaddr_un *soun = &nd->addr_un;
688 soun->sun_family = AF_UNIX;
689 strcpy(soun->sun_path, path);
693 static int fio_netio_setup_connect(struct thread_data *td)
695 struct netio_options *o = td->eo;
697 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
698 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
700 return fio_netio_setup_connect_unix(td, td->o.filename);
703 static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
705 struct netio_data *nd = td->io_ops->data;
706 struct sockaddr_un *addr = &nd->addr_un;
710 fd = socket(AF_UNIX, SOCK_STREAM, 0);
712 log_err("fio: socket: %s\n", strerror(errno));
718 memset(addr, 0, sizeof(*addr));
719 addr->sun_family = AF_UNIX;
720 strcpy(addr->sun_path, path);
723 len = sizeof(addr->sun_family) + strlen(path) + 1;
725 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
726 log_err("fio: bind: %s\n", strerror(errno));
736 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
738 struct netio_data *nd = td->io_ops->data;
739 struct netio_options *o = td->eo;
742 if (o->proto == FIO_TYPE_TCP)
747 fd = socket(AF_INET, type, 0);
749 td_verror(td, errno, "socket");
754 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
755 td_verror(td, errno, "setsockopt");
759 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
760 td_verror(td, errno, "setsockopt");
765 nd->addr.sin_family = AF_INET;
766 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
767 nd->addr.sin_port = htons(port);
769 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
770 td_verror(td, errno, "bind");
778 static int fio_netio_setup_listen(struct thread_data *td)
780 struct netio_data *nd = td->io_ops->data;
781 struct netio_options *o = td->eo;
784 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
785 ret = fio_netio_setup_listen_inet(td, o->port);
787 ret = fio_netio_setup_listen_unix(td, td->o.filename);
791 if (o->proto == FIO_TYPE_UDP)
794 if (listen(nd->listenfd, 10) < 0) {
795 td_verror(td, errno, "listen");
803 static int fio_netio_init(struct thread_data *td)
805 struct netio_options *o = td->eo;
810 WSAStartup(MAKEWORD(2,2), &wsd);
814 log_err("fio: network IO can't be random\n");
818 if (o->proto == FIO_TYPE_UNIX && o->port) {
819 log_err("fio: network IO port not valid with unix socket\n");
821 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
822 log_err("fio: network IO requires port for tcp or udp\n");
826 if (o->proto != FIO_TYPE_TCP) {
828 log_err("fio: listen only valid for TCP proto IO\n");
832 log_err("fio: datagram network connections must be"
836 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
837 log_err("fio: UNIX sockets need host/filename\n");
840 o->listen = td_read(td);
843 if (o->proto != FIO_TYPE_UNIX && o->listen && td->o.filename) {
844 log_err("fio: hostname not valid for inbound network IO\n");
849 ret = fio_netio_setup_listen(td);
851 ret = fio_netio_setup_connect(td);
856 static void fio_netio_cleanup(struct thread_data *td)
858 struct netio_data *nd = td->io_ops->data;
861 if (nd->listenfd != -1)
863 if (nd->pipes[0] != -1)
865 if (nd->pipes[1] != -1)
872 static int fio_netio_setup(struct thread_data *td)
874 struct netio_data *nd;
876 if (!td->files_index) {
877 add_file(td, td->o.filename ?: "net");
878 td->o.nr_files = td->o.nr_files ?: 1;
881 if (!td->io_ops->data) {
882 nd = malloc(sizeof(*nd));;
884 memset(nd, 0, sizeof(*nd));
886 nd->pipes[0] = nd->pipes[1] = -1;
887 td->io_ops->data = nd;
893 static void fio_netio_terminate(struct thread_data *td)
895 kill(td->pid, SIGUSR2);
898 #ifdef FIO_HAVE_SPLICE
899 static int fio_netio_setup_splice(struct thread_data *td)
901 struct netio_data *nd;
905 nd = td->io_ops->data;
907 if (pipe(nd->pipes) < 0)
917 static struct ioengine_ops ioengine_splice = {
919 .version = FIO_IOOPS_VERSION,
920 .prep = fio_netio_prep,
921 .queue = fio_netio_queue,
922 .setup = fio_netio_setup_splice,
923 .init = fio_netio_init,
924 .cleanup = fio_netio_cleanup,
925 .open_file = fio_netio_open_file,
926 .close_file = fio_netio_close_file,
927 .terminate = fio_netio_terminate,
929 .option_struct_size = sizeof(struct netio_options),
930 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
935 static struct ioengine_ops ioengine_rw = {
937 .version = FIO_IOOPS_VERSION,
938 .prep = fio_netio_prep,
939 .queue = fio_netio_queue,
940 .setup = fio_netio_setup,
941 .init = fio_netio_init,
942 .cleanup = fio_netio_cleanup,
943 .open_file = fio_netio_open_file,
944 .close_file = fio_netio_close_file,
945 .terminate = fio_netio_terminate,
947 .option_struct_size = sizeof(struct netio_options),
948 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
952 static int str_hostname_cb(void *data, const char *input)
954 struct netio_options *o = data;
956 if (o->td->o.filename)
957 free(o->td->o.filename);
958 o->td->o.filename = strdup(input);
962 static void fio_init fio_netio_register(void)
964 register_ioengine(&ioengine_rw);
965 #ifdef FIO_HAVE_SPLICE
966 register_ioengine(&ioengine_splice);
970 static void fio_exit fio_netio_unregister(void)
972 unregister_ioengine(&ioengine_rw);
973 #ifdef FIO_HAVE_SPLICE
974 unregister_ioengine(&ioengine_splice);