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 .lname = "net engine hostname",
57 .type = FIO_OPT_STR_STORE,
58 .cb = str_hostname_cb,
59 .help = "Hostname for net IO engine",
60 .category = FIO_OPT_C_IO,
64 .lname = "net engine port",
66 .off1 = offsetof(struct netio_options, port),
69 .help = "Port to use for TCP or UDP net connections",
70 .category = FIO_OPT_C_IO,
74 .lname = "net engine protocol",
77 .off1 = offsetof(struct netio_options, proto),
78 .help = "Network protocol to use",
80 .category = FIO_OPT_C_IO,
84 .help = "Transmission Control Protocol",
88 .help = "Unreliable Datagram Protocol",
91 .oval = FIO_TYPE_UNIX,
92 .help = "UNIX domain socket",
98 .lname = "net engine listen",
99 .type = FIO_OPT_STR_SET,
100 .off1 = offsetof(struct netio_options, listen),
101 .help = "Listen for incoming TCP connections",
102 .category = FIO_OPT_C_IO,
110 * Return -1 for error and 'nr events' for a positive number
113 static int poll_wait(struct thread_data *td, int fd, short events)
118 while (!td->terminate) {
121 ret = poll(&pfd, 1, -1);
126 td_verror(td, errno, "poll");
134 if (pfd.revents & events)
140 static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
142 struct netio_options *o = td->eo;
145 * Make sure we don't see spurious reads to a receiver, and vice versa
147 if (o->proto == FIO_TYPE_TCP)
150 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
151 (!o->listen && io_u->ddir == DDIR_READ)) {
152 td_verror(td, EINVAL, "bad direction");
159 #ifdef FIO_HAVE_SPLICE
160 static int splice_io_u(int fdin, int fdout, unsigned int len)
165 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
183 * Receive bytes from a socket and fill them into the internal pipe
185 static int splice_in(struct thread_data *td, struct io_u *io_u)
187 struct netio_data *nd = td->io_ops->data;
189 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
193 * Transmit 'len' bytes from the internal pipe
195 static int splice_out(struct thread_data *td, struct io_u *io_u,
198 struct netio_data *nd = td->io_ops->data;
200 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
203 static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
206 .iov_base = io_u->xfer_buf,
211 while (iov.iov_len) {
212 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
231 * vmsplice() pipe to io_u buffer
233 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
236 struct netio_data *nd = td->io_ops->data;
238 return vmsplice_io_u(io_u, nd->pipes[0], len);
242 * vmsplice() io_u to pipe
244 static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
246 struct netio_data *nd = td->io_ops->data;
248 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
252 * splice receive - transfer socket data into a pipe using splice, then map
253 * that pipe data into the io_u using vmsplice.
255 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
259 ret = splice_in(td, io_u);
261 return vmsplice_io_u_out(td, io_u, ret);
267 * splice transmit - map data from the io_u into a pipe by using vmsplice,
268 * then transfer that pipe to a socket using splice.
270 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
274 ret = vmsplice_io_u_in(td, io_u);
276 return splice_out(td, io_u, ret);
281 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
287 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
294 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
296 struct netio_data *nd = td->io_ops->data;
297 struct netio_options *o = td->eo;
298 int ret, flags = OS_MSG_DONTWAIT;
301 if (o->proto == FIO_TYPE_UDP) {
302 struct sockaddr *to = (struct sockaddr *) &nd->addr;
304 ret = sendto(io_u->file->fd, io_u->xfer_buf,
305 io_u->xfer_buflen, flags, to,
309 * if we are going to write more, set MSG_MORE
312 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
316 ret = send(io_u->file->fd, io_u->xfer_buf,
317 io_u->xfer_buflen, flags);
322 ret = poll_wait(td, io_u->file->fd, POLLOUT);
326 flags &= ~OS_MSG_DONTWAIT;
332 static int is_udp_close(struct io_u *io_u, int len)
334 struct udp_close_msg *msg;
336 if (len != sizeof(struct udp_close_msg))
339 msg = io_u->xfer_buf;
340 if (ntohl(msg->magic) != FIO_LINK_CLOSE_MAGIC)
342 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
348 static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
350 struct netio_data *nd = td->io_ops->data;
351 struct netio_options *o = td->eo;
352 int ret, flags = OS_MSG_DONTWAIT;
355 if (o->proto == FIO_TYPE_UDP) {
356 fio_socklen_t len = sizeof(nd->addr);
357 struct sockaddr *from = (struct sockaddr *) &nd->addr;
359 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
360 io_u->xfer_buflen, flags, from, &len);
361 if (is_udp_close(io_u, ret)) {
366 ret = recv(io_u->file->fd, io_u->xfer_buf,
367 io_u->xfer_buflen, flags);
372 ret = poll_wait(td, io_u->file->fd, POLLIN);
375 flags &= ~OS_MSG_DONTWAIT;
376 flags |= MSG_WAITALL;
382 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
384 struct netio_data *nd = td->io_ops->data;
385 struct netio_options *o = td->eo;
388 fio_ro_check(td, io_u);
390 if (io_u->ddir == DDIR_WRITE) {
391 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
392 o->proto == FIO_TYPE_UNIX)
393 ret = fio_netio_send(td, io_u);
395 ret = fio_netio_splice_out(td, io_u);
396 } else if (io_u->ddir == DDIR_READ) {
397 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
398 o->proto == FIO_TYPE_UNIX)
399 ret = fio_netio_recv(td, io_u);
401 ret = fio_netio_splice_in(td, io_u);
403 ret = 0; /* must be a SYNC */
405 if (ret != (int) io_u->xfer_buflen) {
407 io_u->resid = io_u->xfer_buflen - ret;
409 return FIO_Q_COMPLETED;
413 if (io_u->ddir == DDIR_WRITE && err == EMSGSIZE)
421 td_verror(td, io_u->error, "xfer");
423 return FIO_Q_COMPLETED;
426 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
428 struct netio_data *nd = td->io_ops->data;
429 struct netio_options *o = td->eo;
432 if (o->proto == FIO_TYPE_TCP) {
435 } else if (o->proto == FIO_TYPE_UDP) {
438 } else if (o->proto == FIO_TYPE_UNIX) {
442 log_err("fio: bad network type %d\n", o->proto);
447 f->fd = socket(domain, type, 0);
449 td_verror(td, errno, "socket");
453 if (o->proto == FIO_TYPE_UDP)
455 else if (o->proto == FIO_TYPE_TCP) {
456 fio_socklen_t len = sizeof(nd->addr);
458 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
459 td_verror(td, errno, "connect");
464 struct sockaddr_un *addr = &nd->addr_un;
467 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
469 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
470 td_verror(td, errno, "connect");
479 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
481 struct netio_data *nd = td->io_ops->data;
482 struct netio_options *o = td->eo;
483 fio_socklen_t socklen = sizeof(nd->addr);
485 if (o->proto == FIO_TYPE_UDP) {
486 f->fd = nd->listenfd;
490 log_info("fio: waiting for connection\n");
492 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
495 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
497 td_verror(td, errno, "accept");
504 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
507 struct netio_options *o = td->eo;
510 ret = fio_netio_accept(td, f);
512 ret = fio_netio_connect(td, f);
519 static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
521 struct netio_data *nd = td->io_ops->data;
522 struct udp_close_msg msg;
523 struct sockaddr *to = (struct sockaddr *) &nd->addr;
526 msg.magic = htonl(FIO_LINK_CLOSE_MAGIC);
527 msg.cmd = htonl(FIO_LINK_CLOSE);
529 ret = sendto(f->fd, &msg, sizeof(msg), MSG_WAITALL, to,
532 td_verror(td, errno, "sendto udp link close");
535 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
537 struct netio_options *o = td->eo;
540 * If this is an UDP connection, notify the receiver that we are
541 * closing down the link
543 if (o->proto == FIO_TYPE_UDP)
544 fio_netio_udp_close(td, f);
546 return generic_close_file(td, f);
549 static int fio_netio_setup_connect_inet(struct thread_data *td,
550 const char *host, unsigned short port)
552 struct netio_data *nd = td->io_ops->data;
554 nd->addr.sin_family = AF_INET;
555 nd->addr.sin_port = htons(port);
557 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
558 struct hostent *hent;
560 hent = gethostbyname(host);
562 td_verror(td, errno, "gethostbyname");
566 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
572 static int fio_netio_setup_connect_unix(struct thread_data *td,
575 struct netio_data *nd = td->io_ops->data;
576 struct sockaddr_un *soun = &nd->addr_un;
578 soun->sun_family = AF_UNIX;
579 strcpy(soun->sun_path, path);
583 static int fio_netio_setup_connect(struct thread_data *td)
585 struct netio_options *o = td->eo;
587 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
588 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
590 return fio_netio_setup_connect_unix(td, td->o.filename);
593 static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
595 struct netio_data *nd = td->io_ops->data;
596 struct sockaddr_un *addr = &nd->addr_un;
600 fd = socket(AF_UNIX, SOCK_STREAM, 0);
602 log_err("fio: socket: %s\n", strerror(errno));
608 memset(addr, 0, sizeof(*addr));
609 addr->sun_family = AF_UNIX;
610 strcpy(addr->sun_path, path);
613 len = sizeof(addr->sun_family) + strlen(path) + 1;
615 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
616 log_err("fio: bind: %s\n", strerror(errno));
626 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
628 struct netio_data *nd = td->io_ops->data;
629 struct netio_options *o = td->eo;
632 if (o->proto == FIO_TYPE_TCP)
637 fd = socket(AF_INET, type, 0);
639 td_verror(td, errno, "socket");
644 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
645 td_verror(td, errno, "setsockopt");
649 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
650 td_verror(td, errno, "setsockopt");
655 nd->addr.sin_family = AF_INET;
656 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
657 nd->addr.sin_port = htons(port);
659 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
660 td_verror(td, errno, "bind");
668 static int fio_netio_setup_listen(struct thread_data *td)
670 struct netio_data *nd = td->io_ops->data;
671 struct netio_options *o = td->eo;
674 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
675 ret = fio_netio_setup_listen_inet(td, o->port);
677 ret = fio_netio_setup_listen_unix(td, td->o.filename);
681 if (o->proto == FIO_TYPE_UDP)
684 if (listen(nd->listenfd, 10) < 0) {
685 td_verror(td, errno, "listen");
693 static int fio_netio_init(struct thread_data *td)
695 struct netio_options *o = td->eo;
699 log_err("fio: network IO can't be random\n");
703 if (o->proto == FIO_TYPE_UNIX && o->port) {
704 log_err("fio: network IO port not valid with unix socket\n");
706 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
707 log_err("fio: network IO requires port for tcp or udp\n");
711 if (o->proto != FIO_TYPE_TCP) {
713 log_err("fio: listen only valid for TCP proto IO\n");
717 log_err("fio: datagram network connections must be"
721 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
722 log_err("fio: UNIX sockets need host/filename\n");
725 o->listen = td_read(td);
728 if (o->proto != FIO_TYPE_UNIX && o->listen && td->o.filename) {
729 log_err("fio: hostname not valid for inbound network IO\n");
734 ret = fio_netio_setup_listen(td);
736 ret = fio_netio_setup_connect(td);
741 static void fio_netio_cleanup(struct thread_data *td)
743 struct netio_data *nd = td->io_ops->data;
746 if (nd->listenfd != -1)
748 if (nd->pipes[0] != -1)
750 if (nd->pipes[1] != -1)
757 static int fio_netio_setup(struct thread_data *td)
759 struct netio_data *nd;
761 if (!td->files_index) {
762 add_file(td, td->o.filename ?: "net");
763 td->o.nr_files = td->o.nr_files ?: 1;
766 if (!td->io_ops->data) {
767 nd = malloc(sizeof(*nd));;
769 memset(nd, 0, sizeof(*nd));
771 nd->pipes[0] = nd->pipes[1] = -1;
772 td->io_ops->data = nd;
778 #ifdef FIO_HAVE_SPLICE
779 static int fio_netio_setup_splice(struct thread_data *td)
781 struct netio_data *nd;
785 nd = td->io_ops->data;
787 if (pipe(nd->pipes) < 0)
797 static struct ioengine_ops ioengine_splice = {
799 .version = FIO_IOOPS_VERSION,
800 .prep = fio_netio_prep,
801 .queue = fio_netio_queue,
802 .setup = fio_netio_setup_splice,
803 .init = fio_netio_init,
804 .cleanup = fio_netio_cleanup,
805 .open_file = fio_netio_open_file,
806 .close_file = generic_close_file,
808 .option_struct_size = sizeof(struct netio_options),
809 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
810 FIO_SIGTERM | FIO_PIPEIO,
814 static struct ioengine_ops ioengine_rw = {
816 .version = FIO_IOOPS_VERSION,
817 .prep = fio_netio_prep,
818 .queue = fio_netio_queue,
819 .setup = fio_netio_setup,
820 .init = fio_netio_init,
821 .cleanup = fio_netio_cleanup,
822 .open_file = fio_netio_open_file,
823 .close_file = fio_netio_close_file,
825 .option_struct_size = sizeof(struct netio_options),
826 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
827 FIO_SIGTERM | FIO_PIPEIO,
830 static int str_hostname_cb(void *data, const char *input)
832 struct netio_options *o = data;
834 if (o->td->o.filename)
835 free(o->td->o.filename);
836 o->td->o.filename = strdup(input);
840 static void fio_init fio_netio_register(void)
842 register_ioengine(&ioengine_rw);
843 #ifdef FIO_HAVE_SPLICE
844 register_ioengine(&ioengine_splice);
848 static void fio_exit fio_netio_unregister(void)
850 unregister_ioengine(&ioengine_rw);
851 #ifdef FIO_HAVE_SPLICE
852 unregister_ioengine(&ioengine_splice);