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");
396 struct sockaddr_un *addr = &nd->addr_un;
399 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
401 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
402 td_verror(td, errno, "connect");
411 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
413 struct netio_data *nd = td->io_ops->data;
414 fio_socklen_t socklen = sizeof(nd->addr);
416 if (nd->type == FIO_TYPE_UDP) {
417 f->fd = nd->listenfd;
421 log_info("fio: waiting for connection\n");
423 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
426 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
428 td_verror(td, errno, "accept");
435 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
440 ret = fio_netio_accept(td, f);
442 ret = fio_netio_connect(td, f);
449 static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
451 struct netio_data *nd = td->io_ops->data;
452 struct udp_close_msg msg;
453 struct sockaddr *to = (struct sockaddr *) &nd->addr;
456 msg.magic = htonl(FIO_LINK_CLOSE_MAGIC);
457 msg.cmd = htonl(FIO_LINK_CLOSE);
459 ret = sendto(f->fd, &msg, sizeof(msg), MSG_WAITALL, to,
462 td_verror(td, errno, "sendto udp link close");
465 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
467 struct netio_data *nd = td->io_ops->data;
470 * If this is an UDP connection, notify the receiver that we are
471 * closing down the link
473 if (nd->type == FIO_TYPE_UDP)
474 fio_netio_udp_close(td, f);
476 return generic_close_file(td, f);
479 static int fio_netio_setup_connect_inet(struct thread_data *td,
480 const char *host, unsigned short port)
482 struct netio_data *nd = td->io_ops->data;
484 nd->addr.sin_family = AF_INET;
485 nd->addr.sin_port = htons(port);
487 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
488 struct hostent *hent;
490 hent = gethostbyname(host);
492 td_verror(td, errno, "gethostbyname");
496 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
502 static int fio_netio_setup_connect_unix(struct thread_data *td,
505 struct netio_data *nd = td->io_ops->data;
506 struct sockaddr_un *soun = &nd->addr_un;
508 soun->sun_family = AF_UNIX;
509 strcpy(soun->sun_path, path);
513 static int fio_netio_setup_connect(struct thread_data *td, const char *host,
516 struct netio_data *nd = td->io_ops->data;
518 if (nd->type == FIO_TYPE_UDP || nd->type == FIO_TYPE_TCP)
519 return fio_netio_setup_connect_inet(td, host, port);
521 return fio_netio_setup_connect_unix(td, host);
524 static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
526 struct netio_data *nd = td->io_ops->data;
527 struct sockaddr_un *addr = &nd->addr_un;
531 fd = socket(AF_UNIX, SOCK_STREAM, 0);
533 log_err("fio: socket: %s\n", strerror(errno));
539 memset(addr, 0, sizeof(*addr));
540 addr->sun_family = AF_UNIX;
541 strcpy(addr->sun_path, path);
544 len = sizeof(addr->sun_family) + strlen(path) + 1;
546 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
547 log_err("fio: bind: %s\n", strerror(errno));
557 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
559 struct netio_data *nd = td->io_ops->data;
562 if (nd->type == FIO_TYPE_TCP)
567 fd = socket(AF_INET, type, 0);
569 td_verror(td, errno, "socket");
574 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
575 td_verror(td, errno, "setsockopt");
579 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
580 td_verror(td, errno, "setsockopt");
585 nd->addr.sin_family = AF_INET;
586 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
587 nd->addr.sin_port = htons(port);
589 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
590 td_verror(td, errno, "bind");
598 static int fio_netio_setup_listen(struct thread_data *td, const char *path,
601 struct netio_data *nd = td->io_ops->data;
604 if (nd->type == FIO_TYPE_UDP || nd->type == FIO_TYPE_TCP)
605 ret = fio_netio_setup_listen_inet(td, port);
607 ret = fio_netio_setup_listen_unix(td, path);
611 if (nd->type == FIO_TYPE_UDP)
614 if (listen(nd->listenfd, 10) < 0) {
615 td_verror(td, errno, "listen");
623 static int fio_netio_init(struct thread_data *td)
625 struct netio_data *nd = td->io_ops->data;
627 char host[64], buf[128];
628 char *sep, *portp, *modep;
632 log_err("fio: network connections must be read OR write\n");
636 log_err("fio: network IO can't be random\n");
640 strcpy(buf, td->o.filename);
642 sep = strchr(buf, ',');
654 sep = strchr(portp, ',');
660 if (!strncmp("tcp", modep, strlen(modep)) ||
661 !strncmp("TCP", modep, strlen(modep)))
662 nd->type = FIO_TYPE_TCP;
663 else if (!strncmp("udp", modep, strlen(modep)) ||
664 !strncmp("UDP", modep, strlen(modep)))
665 nd->type = FIO_TYPE_UDP;
666 else if (!strncmp("unix", modep, strlen(modep)) ||
667 !strncmp("UNIX", modep, strlen(modep)))
668 nd->type = FIO_TYPE_UNIX;
672 if (nd->type != FIO_TYPE_UNIX) {
673 port = strtol(portp, NULL, 10);
674 if (!port || port > 65535)
681 ret = fio_netio_setup_listen(td, host, port);
684 ret = fio_netio_setup_connect(td, host, port);
689 log_err("fio: bad network host/port/protocol: %s\n", td->o.filename);
693 static void fio_netio_cleanup(struct thread_data *td)
695 struct netio_data *nd = td->io_ops->data;
698 if (nd->listenfd != -1)
700 if (nd->pipes[0] != -1)
702 if (nd->pipes[1] != -1)
709 static int fio_netio_setup(struct thread_data *td)
711 struct netio_data *nd;
713 if (!td->io_ops->data) {
714 nd = malloc(sizeof(*nd));;
716 memset(nd, 0, sizeof(*nd));
718 nd->pipes[0] = nd->pipes[1] = -1;
719 td->io_ops->data = nd;
725 #ifdef FIO_HAVE_SPLICE
726 static int fio_netio_setup_splice(struct thread_data *td)
728 struct netio_data *nd;
732 nd = td->io_ops->data;
734 if (pipe(nd->pipes) < 0)
744 static struct ioengine_ops ioengine_splice = {
746 .version = FIO_IOOPS_VERSION,
747 .prep = fio_netio_prep,
748 .queue = fio_netio_queue,
749 .setup = fio_netio_setup_splice,
750 .init = fio_netio_init,
751 .cleanup = fio_netio_cleanup,
752 .open_file = fio_netio_open_file,
753 .close_file = generic_close_file,
754 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
755 FIO_SIGTERM | FIO_PIPEIO,
759 static struct ioengine_ops ioengine_rw = {
761 .version = FIO_IOOPS_VERSION,
762 .prep = fio_netio_prep,
763 .queue = fio_netio_queue,
764 .setup = fio_netio_setup,
765 .init = fio_netio_init,
766 .cleanup = fio_netio_cleanup,
767 .open_file = fio_netio_open_file,
768 .close_file = fio_netio_close_file,
769 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
770 FIO_SIGTERM | FIO_PIPEIO,
773 static void fio_init fio_netio_register(void)
775 register_ioengine(&ioengine_rw);
776 #ifdef FIO_HAVE_SPLICE
777 register_ioengine(&ioengine_splice);
781 static void fio_exit fio_netio_unregister(void)
783 unregister_ioengine(&ioengine_rw);
784 #ifdef FIO_HAVE_SPLICE
785 unregister_ioengine(&ioengine_splice);