4 * IO engine that reads/writes to/from sockets.
12 #include <netinet/in.h>
13 #include <arpa/inet.h>
16 #include <sys/types.h>
18 #include <sys/socket.h>
27 struct sockaddr_in addr;
28 struct sockaddr_un addr_un;
31 struct netio_options {
32 struct thread_data *td;
38 struct udp_close_msg {
44 FIO_LINK_CLOSE = 0x89,
45 FIO_LINK_CLOSE_MAGIC = 0x6c696e6b,
52 static int str_hostname_cb(void *data, const char *input);
53 static struct fio_option options[] = {
56 .type = FIO_OPT_STR_STORE,
57 .cb = str_hostname_cb,
58 .help = "Hostname for net IO engine",
63 .off1 = offsetof(struct netio_options, port),
66 .help = "Port to use for TCP or UDP net connections",
72 .off1 = offsetof(struct netio_options, proto),
73 .help = "Network protocol to use",
78 .help = "Transmission Control Protocol",
82 .help = "Unreliable Datagram Protocol",
85 .oval = FIO_TYPE_UNIX,
86 .help = "UNIX domain socket",
92 .type = FIO_OPT_STR_SET,
93 .off1 = offsetof(struct netio_options, listen),
94 .help = "Listen for incoming TCP connections",
102 * Return -1 for error and 'nr events' for a positive number
105 static int poll_wait(struct thread_data *td, int fd, short events)
110 while (!td->terminate) {
113 ret = poll(&pfd, 1, -1);
118 td_verror(td, errno, "poll");
126 if (pfd.revents & events)
132 static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
134 struct netio_options *o = td->eo;
137 * Make sure we don't see spurious reads to a receiver, and vice versa
139 if (o->proto == FIO_TYPE_TCP)
142 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
143 (!o->listen && io_u->ddir == DDIR_READ)) {
144 td_verror(td, EINVAL, "bad direction");
151 #ifdef FIO_HAVE_SPLICE
152 static int splice_io_u(int fdin, int fdout, unsigned int len)
157 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
175 * Receive bytes from a socket and fill them into the internal pipe
177 static int splice_in(struct thread_data *td, struct io_u *io_u)
179 struct netio_data *nd = td->io_ops->data;
181 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
185 * Transmit 'len' bytes from the internal pipe
187 static int splice_out(struct thread_data *td, struct io_u *io_u,
190 struct netio_data *nd = td->io_ops->data;
192 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
195 static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
198 .iov_base = io_u->xfer_buf,
203 while (iov.iov_len) {
204 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
223 * vmsplice() pipe to io_u buffer
225 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
228 struct netio_data *nd = td->io_ops->data;
230 return vmsplice_io_u(io_u, nd->pipes[0], len);
234 * vmsplice() io_u to pipe
236 static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
238 struct netio_data *nd = td->io_ops->data;
240 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
244 * splice receive - transfer socket data into a pipe using splice, then map
245 * that pipe data into the io_u using vmsplice.
247 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
251 ret = splice_in(td, io_u);
253 return vmsplice_io_u_out(td, io_u, ret);
259 * splice transmit - map data from the io_u into a pipe by using vmsplice,
260 * then transfer that pipe to a socket using splice.
262 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
266 ret = vmsplice_io_u_in(td, io_u);
268 return splice_out(td, io_u, ret);
273 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
279 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
286 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
288 struct netio_data *nd = td->io_ops->data;
289 struct netio_options *o = td->eo;
290 int ret, flags = OS_MSG_DONTWAIT;
293 if (o->proto == FIO_TYPE_UDP) {
294 struct sockaddr *to = (struct sockaddr *) &nd->addr;
296 ret = sendto(io_u->file->fd, io_u->xfer_buf,
297 io_u->xfer_buflen, flags, to,
301 * if we are going to write more, set MSG_MORE
304 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
308 ret = send(io_u->file->fd, io_u->xfer_buf,
309 io_u->xfer_buflen, flags);
314 ret = poll_wait(td, io_u->file->fd, POLLOUT);
318 flags &= ~OS_MSG_DONTWAIT;
324 static int is_udp_close(struct io_u *io_u, int len)
326 struct udp_close_msg *msg;
328 if (len != sizeof(struct udp_close_msg))
331 msg = io_u->xfer_buf;
332 if (ntohl(msg->magic) != FIO_LINK_CLOSE_MAGIC)
334 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
340 static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
342 struct netio_data *nd = td->io_ops->data;
343 struct netio_options *o = td->eo;
344 int ret, flags = OS_MSG_DONTWAIT;
347 if (o->proto == FIO_TYPE_UDP) {
348 fio_socklen_t len = sizeof(nd->addr);
349 struct sockaddr *from = (struct sockaddr *) &nd->addr;
351 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
352 io_u->xfer_buflen, flags, from, &len);
353 if (is_udp_close(io_u, ret)) {
358 ret = recv(io_u->file->fd, io_u->xfer_buf,
359 io_u->xfer_buflen, flags);
364 ret = poll_wait(td, io_u->file->fd, POLLIN);
367 flags &= ~OS_MSG_DONTWAIT;
368 flags |= MSG_WAITALL;
374 static int fio_netio_queue(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;
380 fio_ro_check(td, io_u);
382 if (io_u->ddir == DDIR_WRITE) {
383 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
384 o->proto == FIO_TYPE_UNIX)
385 ret = fio_netio_send(td, io_u);
387 ret = fio_netio_splice_out(td, io_u);
388 } else if (io_u->ddir == DDIR_READ) {
389 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
390 o->proto == FIO_TYPE_UNIX)
391 ret = fio_netio_recv(td, io_u);
393 ret = fio_netio_splice_in(td, io_u);
395 ret = 0; /* must be a SYNC */
397 if (ret != (int) io_u->xfer_buflen) {
399 io_u->resid = io_u->xfer_buflen - ret;
401 return FIO_Q_COMPLETED;
405 if (io_u->ddir == DDIR_WRITE && err == EMSGSIZE)
413 td_verror(td, io_u->error, "xfer");
415 return FIO_Q_COMPLETED;
418 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
420 struct netio_data *nd = td->io_ops->data;
421 struct netio_options *o = td->eo;
424 if (o->proto == FIO_TYPE_TCP) {
427 } else if (o->proto == FIO_TYPE_UDP) {
430 } else if (o->proto == FIO_TYPE_UNIX) {
434 log_err("fio: bad network type %d\n", o->proto);
439 f->fd = socket(domain, type, 0);
441 td_verror(td, errno, "socket");
445 if (o->proto == FIO_TYPE_UDP)
447 else if (o->proto == FIO_TYPE_TCP) {
448 fio_socklen_t len = sizeof(nd->addr);
450 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
451 td_verror(td, errno, "connect");
456 struct sockaddr_un *addr = &nd->addr_un;
459 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
461 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
462 td_verror(td, errno, "connect");
471 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
473 struct netio_data *nd = td->io_ops->data;
474 struct netio_options *o = td->eo;
475 fio_socklen_t socklen = sizeof(nd->addr);
477 if (o->proto == FIO_TYPE_UDP) {
478 f->fd = nd->listenfd;
482 log_info("fio: waiting for connection\n");
484 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
487 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
489 td_verror(td, errno, "accept");
496 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
501 ret = fio_netio_accept(td, f);
503 ret = fio_netio_connect(td, f);
510 static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
512 struct netio_data *nd = td->io_ops->data;
513 struct udp_close_msg msg;
514 struct sockaddr *to = (struct sockaddr *) &nd->addr;
517 msg.magic = htonl(FIO_LINK_CLOSE_MAGIC);
518 msg.cmd = htonl(FIO_LINK_CLOSE);
520 ret = sendto(f->fd, &msg, sizeof(msg), MSG_WAITALL, to,
523 td_verror(td, errno, "sendto udp link close");
526 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
528 struct netio_options *o = td->eo;
531 * If this is an UDP connection, notify the receiver that we are
532 * closing down the link
534 if (o->proto == FIO_TYPE_UDP)
535 fio_netio_udp_close(td, f);
537 return generic_close_file(td, f);
540 static int fio_netio_setup_connect_inet(struct thread_data *td,
541 const char *host, unsigned short port)
543 struct netio_data *nd = td->io_ops->data;
545 nd->addr.sin_family = AF_INET;
546 nd->addr.sin_port = htons(port);
548 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
549 struct hostent *hent;
551 hent = gethostbyname(host);
553 td_verror(td, errno, "gethostbyname");
557 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
563 static int fio_netio_setup_connect_unix(struct thread_data *td,
566 struct netio_data *nd = td->io_ops->data;
567 struct sockaddr_un *soun = &nd->addr_un;
569 soun->sun_family = AF_UNIX;
570 strcpy(soun->sun_path, path);
574 static int fio_netio_setup_connect(struct thread_data *td)
576 struct netio_options *o = td->eo;
578 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
579 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
581 return fio_netio_setup_connect_unix(td, td->o.filename);
584 static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
586 struct netio_data *nd = td->io_ops->data;
587 struct sockaddr_un *addr = &nd->addr_un;
591 fd = socket(AF_UNIX, SOCK_STREAM, 0);
593 log_err("fio: socket: %s\n", strerror(errno));
599 memset(addr, 0, sizeof(*addr));
600 addr->sun_family = AF_UNIX;
601 strcpy(addr->sun_path, path);
604 len = sizeof(addr->sun_family) + strlen(path) + 1;
606 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
607 log_err("fio: bind: %s\n", strerror(errno));
617 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
619 struct netio_data *nd = td->io_ops->data;
620 struct netio_options *o = td->eo;
623 if (o->proto == FIO_TYPE_TCP)
628 fd = socket(AF_INET, type, 0);
630 td_verror(td, errno, "socket");
635 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
636 td_verror(td, errno, "setsockopt");
640 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
641 td_verror(td, errno, "setsockopt");
646 nd->addr.sin_family = AF_INET;
647 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
648 nd->addr.sin_port = htons(port);
650 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
651 td_verror(td, errno, "bind");
659 static int fio_netio_setup_listen(struct thread_data *td)
661 struct netio_data *nd = td->io_ops->data;
662 struct netio_options *o = td->eo;
665 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
666 ret = fio_netio_setup_listen_inet(td, o->port);
668 ret = fio_netio_setup_listen_unix(td, td->o.filename);
672 if (o->proto == FIO_TYPE_UDP)
675 if (listen(nd->listenfd, 10) < 0) {
676 td_verror(td, errno, "listen");
684 static int fio_netio_init(struct thread_data *td)
686 struct netio_options *o = td->eo;
690 log_err("fio: network IO can't be random\n");
694 if (o->proto == FIO_TYPE_UNIX && o->port) {
695 log_err("fio: network IO port not valid with unix socket\n");
697 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
698 log_err("fio: network IO requires port for tcp or udp\n");
702 if (o->proto != FIO_TYPE_TCP) {
704 log_err("fio: listen only valid for TCP proto IO\n");
708 log_err("fio: datagram network connections must be"
712 o->listen = td_read(td);
715 if (o->proto != FIO_TYPE_UNIX && o->listen && td->o.filename) {
716 log_err("fio: hostname not valid for inbound network IO\n");
721 ret = fio_netio_setup_listen(td);
723 ret = fio_netio_setup_connect(td);
728 static void fio_netio_cleanup(struct thread_data *td)
730 struct netio_data *nd = td->io_ops->data;
733 if (nd->listenfd != -1)
735 if (nd->pipes[0] != -1)
737 if (nd->pipes[1] != -1)
744 static int fio_netio_setup(struct thread_data *td)
746 struct netio_data *nd;
748 if (!td->files_index) {
749 add_file(td, td->o.filename ?: "net");
750 td->o.nr_files = td->o.nr_files ?: 1;
753 if (!td->io_ops->data) {
754 nd = malloc(sizeof(*nd));;
756 memset(nd, 0, sizeof(*nd));
758 nd->pipes[0] = nd->pipes[1] = -1;
759 td->io_ops->data = nd;
765 #ifdef FIO_HAVE_SPLICE
766 static int fio_netio_setup_splice(struct thread_data *td)
768 struct netio_data *nd;
772 nd = td->io_ops->data;
774 if (pipe(nd->pipes) < 0)
784 static struct ioengine_ops ioengine_splice = {
786 .version = FIO_IOOPS_VERSION,
787 .prep = fio_netio_prep,
788 .queue = fio_netio_queue,
789 .setup = fio_netio_setup_splice,
790 .init = fio_netio_init,
791 .cleanup = fio_netio_cleanup,
792 .open_file = fio_netio_open_file,
793 .close_file = generic_close_file,
795 .option_struct_size = sizeof(struct netio_options),
796 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
797 FIO_SIGTERM | FIO_PIPEIO,
801 static struct ioengine_ops ioengine_rw = {
803 .version = FIO_IOOPS_VERSION,
804 .prep = fio_netio_prep,
805 .queue = fio_netio_queue,
806 .setup = fio_netio_setup,
807 .init = fio_netio_init,
808 .cleanup = fio_netio_cleanup,
809 .open_file = fio_netio_open_file,
810 .close_file = fio_netio_close_file,
812 .option_struct_size = sizeof(struct netio_options),
813 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
814 FIO_SIGTERM | FIO_PIPEIO,
817 static int str_hostname_cb(void *data, const char *input)
819 struct netio_options *o = data;
821 if (o->td->o.filename)
822 free(o->td->o.filename);
823 o->td->o.filename = strdup(input);
827 static void fio_init fio_netio_register(void)
829 register_ioengine(&ioengine_rw);
830 #ifdef FIO_HAVE_SPLICE
831 register_ioengine(&ioengine_splice);
835 static void fio_exit fio_netio_unregister(void)
837 unregister_ioengine(&ioengine_rw);
838 #ifdef FIO_HAVE_SPLICE
839 unregister_ioengine(&ioengine_splice);