4 * IO engine that reads/writes to/from sockets.
13 #include <netinet/in.h>
14 #include <netinet/tcp.h>
15 #include <arpa/inet.h>
18 #include <sys/types.h>
20 #include <sys/socket.h>
29 struct sockaddr_in addr;
30 struct sockaddr_in6 addr6;
31 struct sockaddr_un addr_un;
34 struct netio_options {
35 struct thread_data *td;
39 unsigned int pingpong;
45 struct udp_close_msg {
51 FIO_LINK_CLOSE = 0x89,
52 FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b,
62 static int str_hostname_cb(void *data, const char *input);
63 static struct fio_option options[] = {
66 .lname = "net engine hostname",
67 .type = FIO_OPT_STR_STORE,
68 .cb = str_hostname_cb,
69 .help = "Hostname for net IO engine",
70 .category = FIO_OPT_C_ENGINE,
71 .group = FIO_OPT_G_NETIO,
75 .lname = "net engine port",
77 .off1 = offsetof(struct netio_options, port),
80 .help = "Port to use for TCP or UDP net connections",
81 .category = FIO_OPT_C_ENGINE,
82 .group = FIO_OPT_G_NETIO,
86 .lname = "net engine protocol",
89 .off1 = offsetof(struct netio_options, proto),
90 .help = "Network protocol to use",
95 .help = "Transmission Control Protocol",
98 .oval = FIO_TYPE_TCP_V6,
99 .help = "Transmission Control Protocol V6",
102 .oval = FIO_TYPE_UDP,
103 .help = "User Datagram Protocol",
106 .oval = FIO_TYPE_UDP_V6,
107 .help = "User Datagram Protocol V6",
110 .oval = FIO_TYPE_UNIX,
111 .help = "UNIX domain socket",
114 .category = FIO_OPT_C_ENGINE,
115 .group = FIO_OPT_G_NETIO,
117 #ifdef CONFIG_TCP_NODELAY
120 .type = FIO_OPT_BOOL,
121 .off1 = offsetof(struct netio_options, nodelay),
122 .help = "Use TCP_NODELAY on TCP connections",
123 .category = FIO_OPT_C_ENGINE,
124 .group = FIO_OPT_G_NETIO,
129 .lname = "net engine listen",
130 .type = FIO_OPT_STR_SET,
131 .off1 = offsetof(struct netio_options, listen),
132 .help = "Listen for incoming TCP connections",
133 .category = FIO_OPT_C_ENGINE,
134 .group = FIO_OPT_G_NETIO,
138 .type = FIO_OPT_STR_SET,
139 .off1 = offsetof(struct netio_options, pingpong),
140 .help = "Ping-pong IO requests",
141 .category = FIO_OPT_C_ENGINE,
142 .group = FIO_OPT_G_NETIO,
146 .lname = "net engine interface",
147 .type = FIO_OPT_STR_STORE,
148 .off1 = offsetof(struct netio_options, intfc),
149 .help = "Network interface to use",
150 .category = FIO_OPT_C_ENGINE,
151 .group = FIO_OPT_G_NETIO,
155 .lname = "net engine multicast ttl",
157 .off1 = offsetof(struct netio_options, ttl),
160 .help = "Time-to-live value for outgoing UDP multicast packets",
161 .category = FIO_OPT_C_ENGINE,
162 .group = FIO_OPT_G_NETIO,
169 static inline int is_udp(struct netio_options *o)
171 return o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_UDP_V6;
174 static inline int is_tcp(struct netio_options *o)
176 return o->proto == FIO_TYPE_TCP || o->proto == FIO_TYPE_TCP_V6;
179 static inline int is_ipv6(struct netio_options *o)
181 return o->proto == FIO_TYPE_UDP_V6 || o->proto == FIO_TYPE_TCP_V6;
185 * Return -1 for error and 'nr events' for a positive number
188 static int poll_wait(struct thread_data *td, int fd, short events)
193 while (!td->terminate) {
196 ret = poll(&pfd, 1, -1);
201 td_verror(td, errno, "poll");
209 if (pfd.revents & events)
215 static int fio_netio_is_multicast(const char *mcaddr)
217 in_addr_t addr = inet_network(mcaddr);
221 if (inet_network("224.0.0.0") <= addr &&
222 inet_network("239.255.255.255") >= addr)
229 static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
231 struct netio_options *o = td->eo;
234 * Make sure we don't see spurious reads to a receiver, and vice versa
239 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
240 (!o->listen && io_u->ddir == DDIR_READ)) {
241 td_verror(td, EINVAL, "bad direction");
248 #ifdef CONFIG_LINUX_SPLICE
249 static int splice_io_u(int fdin, int fdout, unsigned int len)
254 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
272 * Receive bytes from a socket and fill them into the internal pipe
274 static int splice_in(struct thread_data *td, struct io_u *io_u)
276 struct netio_data *nd = td->io_ops->data;
278 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
282 * Transmit 'len' bytes from the internal pipe
284 static int splice_out(struct thread_data *td, struct io_u *io_u,
287 struct netio_data *nd = td->io_ops->data;
289 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
292 static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
295 .iov_base = io_u->xfer_buf,
300 while (iov.iov_len) {
301 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
320 * vmsplice() pipe to io_u buffer
322 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
325 struct netio_data *nd = td->io_ops->data;
327 return vmsplice_io_u(io_u, nd->pipes[0], len);
331 * vmsplice() io_u to pipe
333 static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
335 struct netio_data *nd = td->io_ops->data;
337 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
341 * splice receive - transfer socket data into a pipe using splice, then map
342 * that pipe data into the io_u using vmsplice.
344 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
348 ret = splice_in(td, io_u);
350 return vmsplice_io_u_out(td, io_u, ret);
356 * splice transmit - map data from the io_u into a pipe by using vmsplice,
357 * then transfer that pipe to a socket using splice.
359 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
363 ret = vmsplice_io_u_in(td, io_u);
365 return splice_out(td, io_u, ret);
370 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
376 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
383 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
385 struct netio_data *nd = td->io_ops->data;
386 struct netio_options *o = td->eo;
395 to = (struct sockaddr *) &nd->addr6;
396 len = sizeof(nd->addr6);
398 to = (struct sockaddr *) &nd->addr;
399 len = sizeof(nd->addr);
402 ret = sendto(io_u->file->fd, io_u->xfer_buf,
403 io_u->xfer_buflen, flags, to, len);
406 * if we are going to write more, set MSG_MORE
409 if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
410 td->o.size) && !o->pingpong)
413 ret = send(io_u->file->fd, io_u->xfer_buf,
414 io_u->xfer_buflen, flags);
419 ret = poll_wait(td, io_u->file->fd, POLLOUT);
427 static int is_udp_close(struct io_u *io_u, int len)
429 struct udp_close_msg *msg;
431 if (len != sizeof(struct udp_close_msg))
434 msg = io_u->xfer_buf;
435 if (ntohl(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC)
437 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
443 static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
445 struct netio_data *nd = td->io_ops->data;
446 struct netio_options *o = td->eo;
451 struct sockaddr *from;
452 socklen_t l, *len = &l;
456 from = (struct sockaddr *) &nd->addr;
457 *len = sizeof(nd->addr);
459 from = (struct sockaddr *) &nd->addr6;
460 *len = sizeof(nd->addr6);
467 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
468 io_u->xfer_buflen, flags, from, len);
469 if (is_udp_close(io_u, ret)) {
474 ret = recv(io_u->file->fd, io_u->xfer_buf,
475 io_u->xfer_buflen, flags);
479 else if (!ret && (flags & MSG_WAITALL))
482 ret = poll_wait(td, io_u->file->fd, POLLIN);
485 flags |= MSG_WAITALL;
491 static int __fio_netio_queue(struct thread_data *td, struct io_u *io_u,
494 struct netio_data *nd = td->io_ops->data;
495 struct netio_options *o = td->eo;
498 if (ddir == DDIR_WRITE) {
499 if (!nd->use_splice || is_udp(o) ||
500 o->proto == FIO_TYPE_UNIX)
501 ret = fio_netio_send(td, io_u);
503 ret = fio_netio_splice_out(td, io_u);
504 } else if (ddir == DDIR_READ) {
505 if (!nd->use_splice || is_udp(o) ||
506 o->proto == FIO_TYPE_UNIX)
507 ret = fio_netio_recv(td, io_u);
509 ret = fio_netio_splice_in(td, io_u);
511 ret = 0; /* must be a SYNC */
513 if (ret != (int) io_u->xfer_buflen) {
515 io_u->resid = io_u->xfer_buflen - ret;
517 return FIO_Q_COMPLETED;
521 if (ddir == DDIR_WRITE && err == EMSGSIZE)
529 td_verror(td, io_u->error, "xfer");
531 return FIO_Q_COMPLETED;
534 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
536 struct netio_options *o = td->eo;
539 fio_ro_check(td, io_u);
541 ret = __fio_netio_queue(td, io_u, io_u->ddir);
542 if (!o->pingpong || ret != FIO_Q_COMPLETED)
546 * For ping-pong mode, receive or send reply as needed
548 if (td_read(td) && io_u->ddir == DDIR_READ)
549 ret = __fio_netio_queue(td, io_u, DDIR_WRITE);
550 else if (td_write(td) && io_u->ddir == DDIR_WRITE)
551 ret = __fio_netio_queue(td, io_u, DDIR_READ);
556 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
558 struct netio_data *nd = td->io_ops->data;
559 struct netio_options *o = td->eo;
562 if (o->proto == FIO_TYPE_TCP) {
565 } else if (o->proto == FIO_TYPE_TCP_V6) {
568 } else if (o->proto == FIO_TYPE_UDP) {
571 } else if (o->proto == FIO_TYPE_UDP_V6) {
574 } else if (o->proto == FIO_TYPE_UNIX) {
578 log_err("fio: bad network type %d\n", o->proto);
583 f->fd = socket(domain, type, 0);
585 td_verror(td, errno, "socket");
589 #ifdef CONFIG_TCP_NODELAY
590 if (o->nodelay && is_tcp(o)) {
593 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
594 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
601 if (!fio_netio_is_multicast(td->o.filename))
604 log_err("fio: multicast not supported on IPv6\n");
610 struct in_addr interface_addr;
612 if (inet_aton(o->intfc, &interface_addr) == 0) {
613 log_err("fio: interface not valid interface IP\n");
617 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_IF, (const char*)&interface_addr, sizeof(interface_addr)) < 0) {
618 td_verror(td, errno, "setsockopt IP_MULTICAST_IF");
623 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_TTL, (const char*)&o->ttl, sizeof(o->ttl)) < 0) {
624 td_verror(td, errno, "setsockopt IP_MULTICAST_TTL");
629 } else if (o->proto == FIO_TYPE_TCP) {
630 socklen_t len = sizeof(nd->addr);
632 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
633 td_verror(td, errno, "connect");
637 } else if (o->proto == FIO_TYPE_TCP_V6) {
638 socklen_t len = sizeof(nd->addr6);
640 if (connect(f->fd, (struct sockaddr *) &nd->addr6, len) < 0) {
641 td_verror(td, errno, "connect");
647 struct sockaddr_un *addr = &nd->addr_un;
650 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
652 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
653 td_verror(td, errno, "connect");
662 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
664 struct netio_data *nd = td->io_ops->data;
665 struct netio_options *o = td->eo;
670 f->fd = nd->listenfd;
674 state = td->runstate;
675 td_set_runstate(td, TD_SETTING_UP);
677 log_info("fio: waiting for connection\n");
679 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
682 if (o->proto == FIO_TYPE_TCP) {
683 socklen = sizeof(nd->addr);
684 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
686 socklen = sizeof(nd->addr6);
687 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr6, &socklen);
691 td_verror(td, errno, "accept");
695 #ifdef CONFIG_TCP_NODELAY
696 if (o->nodelay && is_tcp(o)) {
699 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
700 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
707 td_set_runstate(td, state);
710 td_set_runstate(td, state);
714 static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
716 struct netio_data *nd = td->io_ops->data;
717 struct netio_options *o = td->eo;
718 struct udp_close_msg msg;
724 to = (struct sockaddr *) &nd->addr6;
725 len = sizeof(nd->addr6);
727 to = (struct sockaddr *) &nd->addr;
728 len = sizeof(nd->addr);
731 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
732 msg.cmd = htonl(FIO_LINK_CLOSE);
734 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
736 td_verror(td, errno, "sendto udp link close");
739 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
741 struct netio_options *o = td->eo;
744 * If this is an UDP connection, notify the receiver that we are
745 * closing down the link
748 fio_netio_udp_close(td, f);
750 return generic_close_file(td, f);
753 static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
755 struct netio_data *nd = td->io_ops->data;
756 struct netio_options *o = td->eo;
757 struct udp_close_msg msg;
763 len = sizeof(nd->addr6);
764 to = (struct sockaddr *) &nd->addr6;
766 len = sizeof(nd->addr);
767 to = (struct sockaddr *) &nd->addr;
770 ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
772 td_verror(td, errno, "recvfrom udp link open");
776 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
777 ntohl(msg.cmd) != FIO_LINK_OPEN) {
778 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
786 static int fio_netio_udp_send_open(struct thread_data *td, struct fio_file *f)
788 struct netio_data *nd = td->io_ops->data;
789 struct netio_options *o = td->eo;
790 struct udp_close_msg msg;
796 len = sizeof(nd->addr6);
797 to = (struct sockaddr *) &nd->addr6;
799 len = sizeof(nd->addr);
800 to = (struct sockaddr *) &nd->addr;
803 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
804 msg.cmd = htonl(FIO_LINK_OPEN);
806 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
808 td_verror(td, errno, "sendto udp link open");
815 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
818 struct netio_options *o = td->eo;
821 ret = fio_netio_accept(td, f);
823 ret = fio_netio_connect(td, f);
832 ret = fio_netio_udp_send_open(td, f);
836 state = td->runstate;
837 td_set_runstate(td, TD_SETTING_UP);
838 ret = fio_netio_udp_recv_open(td, f);
839 td_set_runstate(td, state);
844 fio_netio_close_file(td, f);
849 static int fio_fill_addr(struct thread_data *td, const char *host, int af,
850 void *dst, struct addrinfo **res)
852 struct netio_options *o = td->eo;
853 struct addrinfo hints;
856 if (inet_pton(af, host, dst))
859 memset(&hints, 0, sizeof(hints));
862 hints.ai_socktype = SOCK_STREAM;
864 hints.ai_socktype = SOCK_DGRAM;
867 hints.ai_family = AF_INET6;
869 hints.ai_family = AF_INET;
871 ret = getaddrinfo(host, NULL, &hints, res);
876 if (ret == EAI_SYSTEM)
879 snprintf(str, sizeof(str), "getaddrinfo: %s", gai_strerror(ret));
880 td_verror(td, e, str);
887 static int fio_netio_setup_connect_inet(struct thread_data *td,
888 const char *host, unsigned short port)
890 struct netio_data *nd = td->io_ops->data;
891 struct netio_options *o = td->eo;
892 struct addrinfo *res = NULL;
897 log_err("fio: connect with no host to connect to.\n");
899 log_err("fio: did you forget to set 'listen'?\n");
901 td_verror(td, EINVAL, "no hostname= set");
905 nd->addr.sin_family = AF_INET;
906 nd->addr.sin_port = htons(port);
907 nd->addr6.sin6_family = AF_INET6;
908 nd->addr6.sin6_port = htons(port);
918 if (fio_fill_addr(td, host, af, dst, &res))
925 len = sizeof(nd->addr6.sin6_addr);
926 src = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
928 len = sizeof(nd->addr.sin_addr);
929 src = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
932 memcpy(dst, src, len);
937 static int fio_netio_setup_connect_unix(struct thread_data *td,
940 struct netio_data *nd = td->io_ops->data;
941 struct sockaddr_un *soun = &nd->addr_un;
943 soun->sun_family = AF_UNIX;
944 strcpy(soun->sun_path, path);
948 static int fio_netio_setup_connect(struct thread_data *td)
950 struct netio_options *o = td->eo;
952 if (is_udp(o) || is_tcp(o))
953 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
955 return fio_netio_setup_connect_unix(td, td->o.filename);
958 static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
960 struct netio_data *nd = td->io_ops->data;
961 struct sockaddr_un *addr = &nd->addr_un;
965 fd = socket(AF_UNIX, SOCK_STREAM, 0);
967 log_err("fio: socket: %s\n", strerror(errno));
973 memset(addr, 0, sizeof(*addr));
974 addr->sun_family = AF_UNIX;
975 strcpy(addr->sun_path, path);
978 len = sizeof(addr->sun_family) + strlen(path) + 1;
980 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
981 log_err("fio: bind: %s\n", strerror(errno));
991 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
993 struct netio_data *nd = td->io_ops->data;
994 struct netio_options *o = td->eo;
996 struct sockaddr_in sin;
997 struct sockaddr_in6 sin6;
998 struct sockaddr *saddr;
999 int fd, opt, type, domain;
1002 memset(&sin, 0, sizeof(sin));
1003 memset(&sin6, 0, sizeof(sin6));
1005 if (o->proto == FIO_TYPE_TCP) {
1008 } else if (o->proto == FIO_TYPE_TCP_V6) {
1011 } else if (o->proto == FIO_TYPE_UDP) {
1014 } else if (o->proto == FIO_TYPE_UDP_V6) {
1018 log_err("fio: unknown proto %d\n", o->proto);
1022 fd = socket(domain, type, 0);
1024 td_verror(td, errno, "socket");
1029 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
1030 td_verror(td, errno, "setsockopt");
1035 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) {
1036 td_verror(td, errno, "setsockopt");
1042 if (td->o.filename){
1043 if (!is_udp(o) || !fio_netio_is_multicast(td->o.filename)) {
1044 log_err("fio: hostname not valid for non-multicast inbound network IO\n");
1049 inet_aton(td->o.filename, &sin.sin_addr);
1051 mr.imr_multiaddr = sin.sin_addr;
1053 if (inet_aton(o->intfc, &mr.imr_interface) == 0) {
1054 log_err("fio: interface not valid interface IP\n");
1059 mr.imr_interface.s_addr = htonl(INADDR_ANY);
1061 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mr, sizeof(mr)) < 0) {
1062 td_verror(td, errno, "setsockopt IP_ADD_MEMBERSHIP");
1069 saddr = (struct sockaddr *) &nd->addr;
1070 len = sizeof(nd->addr);
1072 nd->addr.sin_family = AF_INET;
1073 nd->addr.sin_addr.s_addr = sin.sin_addr.s_addr ? sin.sin_addr.s_addr : htonl(INADDR_ANY);
1074 nd->addr.sin_port = htons(port);
1076 saddr = (struct sockaddr *) &nd->addr6;
1077 len = sizeof(nd->addr6);
1079 nd->addr6.sin6_family = AF_INET6;
1080 nd->addr6.sin6_addr = sin6.sin6_addr.s6_addr ? sin6.sin6_addr : in6addr_any;
1081 nd->addr6.sin6_port = htons(port);
1084 if (bind(fd, saddr, len) < 0) {
1085 td_verror(td, errno, "bind");
1093 static int fio_netio_setup_listen(struct thread_data *td)
1095 struct netio_data *nd = td->io_ops->data;
1096 struct netio_options *o = td->eo;
1099 if (is_udp(o) || is_tcp(o))
1100 ret = fio_netio_setup_listen_inet(td, o->port);
1102 ret = fio_netio_setup_listen_unix(td, td->o.filename);
1109 if (listen(nd->listenfd, 10) < 0) {
1110 td_verror(td, errno, "listen");
1118 static int fio_netio_init(struct thread_data *td)
1120 struct netio_options *o = td->eo;
1125 WSAStartup(MAKEWORD(2,2), &wsd);
1128 if (td_random(td)) {
1129 log_err("fio: network IO can't be random\n");
1133 if (o->proto == FIO_TYPE_UNIX && o->port) {
1134 log_err("fio: network IO port not valid with unix socket\n");
1136 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
1137 log_err("fio: network IO requires port for tcp or udp\n");
1143 log_err("fio: listen only valid for TCP proto IO\n");
1147 log_err("fio: datagram network connections must be"
1148 " read OR write\n");
1151 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
1152 log_err("fio: UNIX sockets need host/filename\n");
1155 o->listen = td_read(td);
1159 ret = fio_netio_setup_listen(td);
1161 ret = fio_netio_setup_connect(td);
1166 static void fio_netio_cleanup(struct thread_data *td)
1168 struct netio_data *nd = td->io_ops->data;
1171 if (nd->listenfd != -1)
1172 close(nd->listenfd);
1173 if (nd->pipes[0] != -1)
1174 close(nd->pipes[0]);
1175 if (nd->pipes[1] != -1)
1176 close(nd->pipes[1]);
1182 static int fio_netio_setup(struct thread_data *td)
1184 struct netio_data *nd;
1186 if (!td->files_index) {
1187 add_file(td, td->o.filename ?: "net");
1188 td->o.nr_files = td->o.nr_files ?: 1;
1191 if (!td->io_ops->data) {
1192 nd = malloc(sizeof(*nd));;
1194 memset(nd, 0, sizeof(*nd));
1196 nd->pipes[0] = nd->pipes[1] = -1;
1197 td->io_ops->data = nd;
1203 static void fio_netio_terminate(struct thread_data *td)
1205 kill(td->pid, SIGUSR2);
1208 #ifdef CONFIG_LINUX_SPLICE
1209 static int fio_netio_setup_splice(struct thread_data *td)
1211 struct netio_data *nd;
1213 fio_netio_setup(td);
1215 nd = td->io_ops->data;
1217 if (pipe(nd->pipes) < 0)
1227 static struct ioengine_ops ioengine_splice = {
1228 .name = "netsplice",
1229 .version = FIO_IOOPS_VERSION,
1230 .prep = fio_netio_prep,
1231 .queue = fio_netio_queue,
1232 .setup = fio_netio_setup_splice,
1233 .init = fio_netio_init,
1234 .cleanup = fio_netio_cleanup,
1235 .open_file = fio_netio_open_file,
1236 .close_file = fio_netio_close_file,
1237 .terminate = fio_netio_terminate,
1239 .option_struct_size = sizeof(struct netio_options),
1240 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1245 static struct ioengine_ops ioengine_rw = {
1247 .version = FIO_IOOPS_VERSION,
1248 .prep = fio_netio_prep,
1249 .queue = fio_netio_queue,
1250 .setup = fio_netio_setup,
1251 .init = fio_netio_init,
1252 .cleanup = fio_netio_cleanup,
1253 .open_file = fio_netio_open_file,
1254 .close_file = fio_netio_close_file,
1255 .terminate = fio_netio_terminate,
1257 .option_struct_size = sizeof(struct netio_options),
1258 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1259 FIO_PIPEIO | FIO_BIT_BASED,
1262 static int str_hostname_cb(void *data, const char *input)
1264 struct netio_options *o = data;
1266 if (o->td->o.filename)
1267 free(o->td->o.filename);
1268 o->td->o.filename = strdup(input);
1272 static void fio_init fio_netio_register(void)
1274 register_ioengine(&ioengine_rw);
1275 #ifdef CONFIG_LINUX_SPLICE
1276 register_ioengine(&ioengine_splice);
1280 static void fio_exit fio_netio_unregister(void)
1282 unregister_ioengine(&ioengine_rw);
1283 #ifdef CONFIG_LINUX_SPLICE
1284 unregister_ioengine(&ioengine_splice);