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)
499 struct netio_options *o = td->eo;
502 ret = fio_netio_accept(td, f);
504 ret = fio_netio_connect(td, f);
511 static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
513 struct netio_data *nd = td->io_ops->data;
514 struct udp_close_msg msg;
515 struct sockaddr *to = (struct sockaddr *) &nd->addr;
518 msg.magic = htonl(FIO_LINK_CLOSE_MAGIC);
519 msg.cmd = htonl(FIO_LINK_CLOSE);
521 ret = sendto(f->fd, &msg, sizeof(msg), MSG_WAITALL, to,
524 td_verror(td, errno, "sendto udp link close");
527 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
529 struct netio_options *o = td->eo;
532 * If this is an UDP connection, notify the receiver that we are
533 * closing down the link
535 if (o->proto == FIO_TYPE_UDP)
536 fio_netio_udp_close(td, f);
538 return generic_close_file(td, f);
541 static int fio_netio_setup_connect_inet(struct thread_data *td,
542 const char *host, unsigned short port)
544 struct netio_data *nd = td->io_ops->data;
546 nd->addr.sin_family = AF_INET;
547 nd->addr.sin_port = htons(port);
549 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
550 struct hostent *hent;
552 hent = gethostbyname(host);
554 td_verror(td, errno, "gethostbyname");
558 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
564 static int fio_netio_setup_connect_unix(struct thread_data *td,
567 struct netio_data *nd = td->io_ops->data;
568 struct sockaddr_un *soun = &nd->addr_un;
570 soun->sun_family = AF_UNIX;
571 strcpy(soun->sun_path, path);
575 static int fio_netio_setup_connect(struct thread_data *td)
577 struct netio_options *o = td->eo;
579 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
580 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
582 return fio_netio_setup_connect_unix(td, td->o.filename);
585 static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
587 struct netio_data *nd = td->io_ops->data;
588 struct sockaddr_un *addr = &nd->addr_un;
592 fd = socket(AF_UNIX, SOCK_STREAM, 0);
594 log_err("fio: socket: %s\n", strerror(errno));
600 memset(addr, 0, sizeof(*addr));
601 addr->sun_family = AF_UNIX;
602 strcpy(addr->sun_path, path);
605 len = sizeof(addr->sun_family) + strlen(path) + 1;
607 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
608 log_err("fio: bind: %s\n", strerror(errno));
618 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
620 struct netio_data *nd = td->io_ops->data;
621 struct netio_options *o = td->eo;
624 if (o->proto == FIO_TYPE_TCP)
629 fd = socket(AF_INET, type, 0);
631 td_verror(td, errno, "socket");
636 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
637 td_verror(td, errno, "setsockopt");
641 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
642 td_verror(td, errno, "setsockopt");
647 nd->addr.sin_family = AF_INET;
648 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
649 nd->addr.sin_port = htons(port);
651 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
652 td_verror(td, errno, "bind");
660 static int fio_netio_setup_listen(struct thread_data *td)
662 struct netio_data *nd = td->io_ops->data;
663 struct netio_options *o = td->eo;
666 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
667 ret = fio_netio_setup_listen_inet(td, o->port);
669 ret = fio_netio_setup_listen_unix(td, td->o.filename);
673 if (o->proto == FIO_TYPE_UDP)
676 if (listen(nd->listenfd, 10) < 0) {
677 td_verror(td, errno, "listen");
685 static int fio_netio_init(struct thread_data *td)
687 struct netio_options *o = td->eo;
691 log_err("fio: network IO can't be random\n");
695 if (o->proto == FIO_TYPE_UNIX && o->port) {
696 log_err("fio: network IO port not valid with unix socket\n");
698 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
699 log_err("fio: network IO requires port for tcp or udp\n");
703 if (o->proto != FIO_TYPE_TCP) {
705 log_err("fio: listen only valid for TCP proto IO\n");
709 log_err("fio: datagram network connections must be"
713 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
714 log_err("fio: UNIX sockets need host/filename\n");
717 o->listen = td_read(td);
720 if (o->proto != FIO_TYPE_UNIX && o->listen && td->o.filename) {
721 log_err("fio: hostname not valid for inbound network IO\n");
726 ret = fio_netio_setup_listen(td);
728 ret = fio_netio_setup_connect(td);
733 static void fio_netio_cleanup(struct thread_data *td)
735 struct netio_data *nd = td->io_ops->data;
738 if (nd->listenfd != -1)
740 if (nd->pipes[0] != -1)
742 if (nd->pipes[1] != -1)
749 static int fio_netio_setup(struct thread_data *td)
751 struct netio_data *nd;
753 if (!td->files_index) {
754 add_file(td, td->o.filename ?: "net");
755 td->o.nr_files = td->o.nr_files ?: 1;
758 if (!td->io_ops->data) {
759 nd = malloc(sizeof(*nd));;
761 memset(nd, 0, sizeof(*nd));
763 nd->pipes[0] = nd->pipes[1] = -1;
764 td->io_ops->data = nd;
770 #ifdef FIO_HAVE_SPLICE
771 static int fio_netio_setup_splice(struct thread_data *td)
773 struct netio_data *nd;
777 nd = td->io_ops->data;
779 if (pipe(nd->pipes) < 0)
789 static struct ioengine_ops ioengine_splice = {
791 .version = FIO_IOOPS_VERSION,
792 .prep = fio_netio_prep,
793 .queue = fio_netio_queue,
794 .setup = fio_netio_setup_splice,
795 .init = fio_netio_init,
796 .cleanup = fio_netio_cleanup,
797 .open_file = fio_netio_open_file,
798 .close_file = generic_close_file,
800 .option_struct_size = sizeof(struct netio_options),
801 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
802 FIO_SIGTERM | FIO_PIPEIO,
806 static struct ioengine_ops ioengine_rw = {
808 .version = FIO_IOOPS_VERSION,
809 .prep = fio_netio_prep,
810 .queue = fio_netio_queue,
811 .setup = fio_netio_setup,
812 .init = fio_netio_init,
813 .cleanup = fio_netio_cleanup,
814 .open_file = fio_netio_open_file,
815 .close_file = fio_netio_close_file,
817 .option_struct_size = sizeof(struct netio_options),
818 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
819 FIO_SIGTERM | FIO_PIPEIO,
822 static int str_hostname_cb(void *data, const char *input)
824 struct netio_options *o = data;
826 if (o->td->o.filename)
827 free(o->td->o.filename);
828 o->td->o.filename = strdup(input);
832 static void fio_init fio_netio_register(void)
834 register_ioengine(&ioengine_rw);
835 #ifdef FIO_HAVE_SPLICE
836 register_ioengine(&ioengine_splice);
840 static void fio_exit fio_netio_unregister(void)
842 unregister_ioengine(&ioengine_rw);
843 #ifdef FIO_HAVE_SPLICE
844 unregister_ioengine(&ioengine_splice);