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>
30 struct sockaddr_in addr;
31 struct sockaddr_un addr_un;
34 struct udp_close_msg {
40 FIO_LINK_CLOSE = 0x89,
41 FIO_LINK_CLOSE_MAGIC = 0x6c696e6b,
49 * Return -1 for error and 'nr events' for a positive number
52 static int poll_wait(struct thread_data *td, int fd, short events)
57 while (!td->terminate) {
60 ret = poll(&pfd, 1, -1);
65 td_verror(td, errno, "poll");
73 if (pfd.revents & events)
79 static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
81 struct netio_data *nd = td->io_ops->data;
84 * Make sure we don't see spurious reads to a receiver, and vice versa
86 if ((nd->send_to_net && io_u->ddir == DDIR_READ) ||
87 (!nd->send_to_net && io_u->ddir == DDIR_WRITE)) {
88 td_verror(td, EINVAL, "bad direction");
95 #ifdef FIO_HAVE_SPLICE
96 static int splice_io_u(int fdin, int fdout, unsigned int len)
101 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
119 * Receive bytes from a socket and fill them into the internal pipe
121 static int splice_in(struct thread_data *td, struct io_u *io_u)
123 struct netio_data *nd = td->io_ops->data;
125 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
129 * Transmit 'len' bytes from the internal pipe
131 static int splice_out(struct thread_data *td, struct io_u *io_u,
134 struct netio_data *nd = td->io_ops->data;
136 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
139 static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
142 .iov_base = io_u->xfer_buf,
147 while (iov.iov_len) {
148 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
167 * vmsplice() pipe to io_u buffer
169 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
172 struct netio_data *nd = td->io_ops->data;
174 return vmsplice_io_u(io_u, nd->pipes[0], len);
178 * vmsplice() io_u to pipe
180 static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
182 struct netio_data *nd = td->io_ops->data;
184 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
188 * splice receive - transfer socket data into a pipe using splice, then map
189 * that pipe data into the io_u using vmsplice.
191 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
195 ret = splice_in(td, io_u);
197 return vmsplice_io_u_out(td, io_u, ret);
203 * splice transmit - map data from the io_u into a pipe by using vmsplice,
204 * then transfer that pipe to a socket using splice.
206 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
210 ret = vmsplice_io_u_in(td, io_u);
212 return splice_out(td, io_u, ret);
217 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
223 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
230 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
232 struct netio_data *nd = td->io_ops->data;
233 int ret, flags = OS_MSG_DONTWAIT;
236 if (nd->type == FIO_TYPE_UDP) {
237 struct sockaddr *to = (struct sockaddr *) &nd->addr;
239 ret = sendto(io_u->file->fd, io_u->xfer_buf,
240 io_u->xfer_buflen, flags, to,
244 * if we are going to write more, set MSG_MORE
247 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
251 ret = send(io_u->file->fd, io_u->xfer_buf,
252 io_u->xfer_buflen, flags);
257 ret = poll_wait(td, io_u->file->fd, POLLOUT);
261 flags &= ~OS_MSG_DONTWAIT;
267 static int is_udp_close(struct io_u *io_u, int len)
269 struct udp_close_msg *msg;
271 if (len != sizeof(struct udp_close_msg))
274 msg = io_u->xfer_buf;
275 if (ntohl(msg->magic) != FIO_LINK_CLOSE_MAGIC)
277 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
283 static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
285 struct netio_data *nd = td->io_ops->data;
286 int ret, flags = OS_MSG_DONTWAIT;
289 if (nd->type == FIO_TYPE_UDP) {
290 fio_socklen_t len = sizeof(nd->addr);
291 struct sockaddr *from = (struct sockaddr *) &nd->addr;
293 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
294 io_u->xfer_buflen, flags, from, &len);
295 if (is_udp_close(io_u, ret)) {
300 ret = recv(io_u->file->fd, io_u->xfer_buf,
301 io_u->xfer_buflen, flags);
306 ret = poll_wait(td, io_u->file->fd, POLLIN);
309 flags &= ~OS_MSG_DONTWAIT;
310 flags |= MSG_WAITALL;
316 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
318 struct netio_data *nd = td->io_ops->data;
321 fio_ro_check(td, io_u);
323 if (io_u->ddir == DDIR_WRITE) {
324 if (!nd->use_splice || nd->type == FIO_TYPE_UDP ||
325 nd->type == FIO_TYPE_UNIX)
326 ret = fio_netio_send(td, io_u);
328 ret = fio_netio_splice_out(td, io_u);
329 } else if (io_u->ddir == DDIR_READ) {
330 if (!nd->use_splice || nd->type == FIO_TYPE_UDP ||
331 nd->type == FIO_TYPE_UDP)
332 ret = fio_netio_recv(td, io_u);
334 ret = fio_netio_splice_in(td, io_u);
336 ret = 0; /* must be a SYNC */
338 if (ret != (int) io_u->xfer_buflen) {
340 io_u->resid = io_u->xfer_buflen - ret;
342 return FIO_Q_COMPLETED;
346 if (io_u->ddir == DDIR_WRITE && err == EMSGSIZE)
354 td_verror(td, io_u->error, "xfer");
356 return FIO_Q_COMPLETED;
359 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
361 struct netio_data *nd = td->io_ops->data;
364 if (nd->type == FIO_TYPE_TCP) {
367 } else if (nd->type == FIO_TYPE_UDP) {
370 } else if (nd->type == FIO_TYPE_UNIX) {
374 log_err("fio: bad network type %d\n", nd->type);
379 f->fd = socket(domain, type, 0);
381 td_verror(td, errno, "socket");
385 if (nd->type == FIO_TYPE_UDP)
387 else if (nd->type == FIO_TYPE_TCP) {
388 fio_socklen_t len = sizeof(nd->addr);
390 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
391 td_verror(td, errno, "connect");
395 struct sockaddr_un *addr = &nd->addr_un;
398 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
400 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
401 td_verror(td, errno, "connect");
409 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
411 struct netio_data *nd = td->io_ops->data;
412 fio_socklen_t socklen = sizeof(nd->addr);
414 if (nd->type == FIO_TYPE_UDP) {
415 f->fd = nd->listenfd;
419 log_info("fio: waiting for connection\n");
421 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
424 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
426 td_verror(td, errno, "accept");
433 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
438 ret = fio_netio_accept(td, f);
440 ret = fio_netio_connect(td, f);
447 static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
449 struct netio_data *nd = td->io_ops->data;
450 struct udp_close_msg msg;
451 struct sockaddr *to = (struct sockaddr *) &nd->addr;
454 msg.magic = htonl(FIO_LINK_CLOSE_MAGIC);
455 msg.cmd = htonl(FIO_LINK_CLOSE);
457 ret = sendto(f->fd, &msg, sizeof(msg), MSG_WAITALL, to,
460 td_verror(td, errno, "sendto udp link close");
463 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
465 struct netio_data *nd = td->io_ops->data;
468 * If this is an UDP connection, notify the receiver that we are
469 * closing down the link
471 if (nd->type == FIO_TYPE_UDP)
472 fio_netio_udp_close(td, f);
474 return generic_close_file(td, f);
477 static int fio_netio_setup_connect_inet(struct thread_data *td,
478 const char *host, unsigned short port)
480 struct netio_data *nd = td->io_ops->data;
482 nd->addr.sin_family = AF_INET;
483 nd->addr.sin_port = htons(port);
485 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
486 struct hostent *hent;
488 hent = gethostbyname(host);
490 td_verror(td, errno, "gethostbyname");
494 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
500 static int fio_netio_setup_connect_unix(struct thread_data *td,
503 struct netio_data *nd = td->io_ops->data;
504 struct sockaddr_un *soun = &nd->addr_un;
506 soun->sun_family = AF_UNIX;
507 strcpy(soun->sun_path, path);
511 static int fio_netio_setup_connect(struct thread_data *td, const char *host,
514 struct netio_data *nd = td->io_ops->data;
516 if (nd->type == FIO_TYPE_UDP || nd->type == FIO_TYPE_TCP)
517 return fio_netio_setup_connect_inet(td, host, port);
519 return fio_netio_setup_connect_unix(td, host);
522 static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
524 struct netio_data *nd = td->io_ops->data;
525 struct sockaddr_un *addr = &nd->addr_un;
529 fd = socket(AF_UNIX, SOCK_STREAM, 0);
531 log_err("fio: socket: %s\n", strerror(errno));
537 memset(addr, 0, sizeof(*addr));
538 addr->sun_family = AF_UNIX;
539 strcpy(addr->sun_path, path);
542 len = sizeof(addr->sun_family) + strlen(path) + 1;
544 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
545 log_err("fio: bind: %s\n", strerror(errno));
554 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
556 struct netio_data *nd = td->io_ops->data;
559 if (nd->type == FIO_TYPE_TCP)
564 fd = socket(AF_INET, type, 0);
566 td_verror(td, errno, "socket");
571 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
572 td_verror(td, errno, "setsockopt");
576 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
577 td_verror(td, errno, "setsockopt");
582 nd->addr.sin_family = AF_INET;
583 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
584 nd->addr.sin_port = htons(port);
586 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
587 td_verror(td, errno, "bind");
595 static int fio_netio_setup_listen(struct thread_data *td, const char *path,
598 struct netio_data *nd = td->io_ops->data;
601 if (nd->type == FIO_TYPE_UDP || nd->type == FIO_TYPE_TCP)
602 ret = fio_netio_setup_listen_inet(td, port);
604 ret = fio_netio_setup_listen_unix(td, path);
608 if (nd->type == FIO_TYPE_UDP)
611 if (listen(nd->listenfd, 10) < 0) {
612 td_verror(td, errno, "listen");
620 static int fio_netio_init(struct thread_data *td)
622 struct netio_data *nd = td->io_ops->data;
624 char host[64], buf[128];
625 char *sep, *portp, *modep;
629 log_err("fio: network connections must be read OR write\n");
633 log_err("fio: network IO can't be random\n");
637 strcpy(buf, td->o.filename);
639 sep = strchr(buf, ',');
651 sep = strchr(portp, ',');
657 if (!strncmp("tcp", modep, strlen(modep)) ||
658 !strncmp("TCP", modep, strlen(modep)))
659 nd->type = FIO_TYPE_TCP;
660 else if (!strncmp("udp", modep, strlen(modep)) ||
661 !strncmp("UDP", modep, strlen(modep)))
662 nd->type = FIO_TYPE_UDP;
663 else if (!strncmp("unix", modep, strlen(modep)) ||
664 !strncmp("UNIX", modep, strlen(modep)))
665 nd->type = FIO_TYPE_UNIX;
669 if (nd->type != FIO_TYPE_UNIX) {
670 port = strtol(portp, NULL, 10);
671 if (!port || port > 65535)
678 ret = fio_netio_setup_listen(td, host, port);
681 ret = fio_netio_setup_connect(td, host, port);
686 log_err("fio: bad network host/port/protocol: %s\n", td->o.filename);
690 static void fio_netio_cleanup(struct thread_data *td)
692 struct netio_data *nd = td->io_ops->data;
695 if (nd->listenfd != -1)
697 if (nd->pipes[0] != -1)
699 if (nd->pipes[1] != -1)
706 static int fio_netio_setup(struct thread_data *td)
708 struct netio_data *nd;
710 if (!td->io_ops->data) {
711 nd = malloc(sizeof(*nd));;
713 memset(nd, 0, sizeof(*nd));
715 nd->pipes[0] = nd->pipes[1] = -1;
716 td->io_ops->data = nd;
722 #ifdef FIO_HAVE_SPLICE
723 static int fio_netio_setup_splice(struct thread_data *td)
725 struct netio_data *nd;
729 nd = td->io_ops->data;
731 if (pipe(nd->pipes) < 0)
741 static struct ioengine_ops ioengine_splice = {
743 .version = FIO_IOOPS_VERSION,
744 .prep = fio_netio_prep,
745 .queue = fio_netio_queue,
746 .setup = fio_netio_setup_splice,
747 .init = fio_netio_init,
748 .cleanup = fio_netio_cleanup,
749 .open_file = fio_netio_open_file,
750 .close_file = generic_close_file,
751 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
752 FIO_SIGTERM | FIO_PIPEIO,
756 static struct ioengine_ops ioengine_rw = {
758 .version = FIO_IOOPS_VERSION,
759 .prep = fio_netio_prep,
760 .queue = fio_netio_queue,
761 .setup = fio_netio_setup,
762 .init = fio_netio_init,
763 .cleanup = fio_netio_cleanup,
764 .open_file = fio_netio_open_file,
765 .close_file = fio_netio_close_file,
766 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
767 FIO_SIGTERM | FIO_PIPEIO,
770 static void fio_init fio_netio_register(void)
772 register_ioengine(&ioengine_rw);
773 #ifdef FIO_HAVE_SPLICE
774 register_ioengine(&ioengine_splice);
778 static void fio_exit fio_netio_unregister(void)
780 unregister_ioengine(&ioengine_rw);
781 #ifdef FIO_HAVE_SPLICE
782 unregister_ioengine(&ioengine_splice);