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",
99 .oval = FIO_TYPE_TCP_V6,
100 .help = "Transmission Control Protocol V6",
104 .oval = FIO_TYPE_UDP,
105 .help = "User Datagram Protocol",
109 .oval = FIO_TYPE_UDP_V6,
110 .help = "User Datagram Protocol V6",
114 .oval = FIO_TYPE_UNIX,
115 .help = "UNIX domain socket",
118 .category = FIO_OPT_C_ENGINE,
119 .group = FIO_OPT_G_NETIO,
121 #ifdef CONFIG_TCP_NODELAY
124 .type = FIO_OPT_BOOL,
125 .off1 = offsetof(struct netio_options, nodelay),
126 .help = "Use TCP_NODELAY on TCP connections",
127 .category = FIO_OPT_C_ENGINE,
128 .group = FIO_OPT_G_NETIO,
133 .lname = "net engine listen",
134 .type = FIO_OPT_STR_SET,
135 .off1 = offsetof(struct netio_options, listen),
136 .help = "Listen for incoming TCP connections",
137 .category = FIO_OPT_C_ENGINE,
138 .group = FIO_OPT_G_NETIO,
142 .type = FIO_OPT_STR_SET,
143 .off1 = offsetof(struct netio_options, pingpong),
144 .help = "Ping-pong IO requests",
145 .category = FIO_OPT_C_ENGINE,
146 .group = FIO_OPT_G_NETIO,
150 .lname = "net engine interface",
151 .type = FIO_OPT_STR_STORE,
152 .off1 = offsetof(struct netio_options, intfc),
153 .help = "Network interface to use",
154 .category = FIO_OPT_C_ENGINE,
155 .group = FIO_OPT_G_NETIO,
159 .lname = "net engine multicast ttl",
161 .off1 = offsetof(struct netio_options, ttl),
164 .help = "Time-to-live value for outgoing UDP multicast packets",
165 .category = FIO_OPT_C_ENGINE,
166 .group = FIO_OPT_G_NETIO,
173 static inline int is_udp(struct netio_options *o)
175 return o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_UDP_V6;
178 static inline int is_tcp(struct netio_options *o)
180 return o->proto == FIO_TYPE_TCP || o->proto == FIO_TYPE_TCP_V6;
183 static inline int is_ipv6(struct netio_options *o)
185 return o->proto == FIO_TYPE_UDP_V6 || o->proto == FIO_TYPE_TCP_V6;
189 * Return -1 for error and 'nr events' for a positive number
192 static int poll_wait(struct thread_data *td, int fd, short events)
197 while (!td->terminate) {
200 ret = poll(&pfd, 1, -1);
205 td_verror(td, errno, "poll");
213 if (pfd.revents & events)
219 static int fio_netio_is_multicast(const char *mcaddr)
221 in_addr_t addr = inet_network(mcaddr);
225 if (inet_network("224.0.0.0") <= addr &&
226 inet_network("239.255.255.255") >= addr)
233 static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
235 struct netio_options *o = td->eo;
238 * Make sure we don't see spurious reads to a receiver, and vice versa
243 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
244 (!o->listen && io_u->ddir == DDIR_READ)) {
245 td_verror(td, EINVAL, "bad direction");
252 #ifdef CONFIG_LINUX_SPLICE
253 static int splice_io_u(int fdin, int fdout, unsigned int len)
258 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
276 * Receive bytes from a socket and fill them into the internal pipe
278 static int splice_in(struct thread_data *td, struct io_u *io_u)
280 struct netio_data *nd = td->io_ops->data;
282 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
286 * Transmit 'len' bytes from the internal pipe
288 static int splice_out(struct thread_data *td, struct io_u *io_u,
291 struct netio_data *nd = td->io_ops->data;
293 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
296 static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
299 .iov_base = io_u->xfer_buf,
304 while (iov.iov_len) {
305 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
324 * vmsplice() pipe to io_u buffer
326 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
329 struct netio_data *nd = td->io_ops->data;
331 return vmsplice_io_u(io_u, nd->pipes[0], len);
335 * vmsplice() io_u to pipe
337 static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
339 struct netio_data *nd = td->io_ops->data;
341 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
345 * splice receive - transfer socket data into a pipe using splice, then map
346 * that pipe data into the io_u using vmsplice.
348 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
352 ret = splice_in(td, io_u);
354 return vmsplice_io_u_out(td, io_u, ret);
360 * splice transmit - map data from the io_u into a pipe by using vmsplice,
361 * then transfer that pipe to a socket using splice.
363 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
367 ret = vmsplice_io_u_in(td, io_u);
369 return splice_out(td, io_u, ret);
374 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
380 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
387 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
389 struct netio_data *nd = td->io_ops->data;
390 struct netio_options *o = td->eo;
399 to = (struct sockaddr *) &nd->addr6;
400 len = sizeof(nd->addr6);
402 to = (struct sockaddr *) &nd->addr;
403 len = sizeof(nd->addr);
406 ret = sendto(io_u->file->fd, io_u->xfer_buf,
407 io_u->xfer_buflen, flags, to, len);
410 * if we are going to write more, set MSG_MORE
413 if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
414 td->o.size) && !o->pingpong)
417 ret = send(io_u->file->fd, io_u->xfer_buf,
418 io_u->xfer_buflen, flags);
423 ret = poll_wait(td, io_u->file->fd, POLLOUT);
431 static int is_udp_close(struct io_u *io_u, int len)
433 struct udp_close_msg *msg;
435 if (len != sizeof(struct udp_close_msg))
438 msg = io_u->xfer_buf;
439 if (ntohl(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC)
441 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
447 static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
449 struct netio_data *nd = td->io_ops->data;
450 struct netio_options *o = td->eo;
455 struct sockaddr *from;
456 socklen_t l, *len = &l;
460 from = (struct sockaddr *) &nd->addr;
461 *len = sizeof(nd->addr);
463 from = (struct sockaddr *) &nd->addr6;
464 *len = sizeof(nd->addr6);
471 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
472 io_u->xfer_buflen, flags, from, len);
473 if (is_udp_close(io_u, ret)) {
478 ret = recv(io_u->file->fd, io_u->xfer_buf,
479 io_u->xfer_buflen, flags);
483 else if (!ret && (flags & MSG_WAITALL))
486 ret = poll_wait(td, io_u->file->fd, POLLIN);
489 flags |= MSG_WAITALL;
495 static int __fio_netio_queue(struct thread_data *td, struct io_u *io_u,
498 struct netio_data *nd = td->io_ops->data;
499 struct netio_options *o = td->eo;
502 if (ddir == DDIR_WRITE) {
503 if (!nd->use_splice || is_udp(o) ||
504 o->proto == FIO_TYPE_UNIX)
505 ret = fio_netio_send(td, io_u);
507 ret = fio_netio_splice_out(td, io_u);
508 } else if (ddir == DDIR_READ) {
509 if (!nd->use_splice || is_udp(o) ||
510 o->proto == FIO_TYPE_UNIX)
511 ret = fio_netio_recv(td, io_u);
513 ret = fio_netio_splice_in(td, io_u);
515 ret = 0; /* must be a SYNC */
517 if (ret != (int) io_u->xfer_buflen) {
519 io_u->resid = io_u->xfer_buflen - ret;
521 return FIO_Q_COMPLETED;
525 if (ddir == DDIR_WRITE && err == EMSGSIZE)
533 td_verror(td, io_u->error, "xfer");
535 return FIO_Q_COMPLETED;
538 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
540 struct netio_options *o = td->eo;
543 fio_ro_check(td, io_u);
545 ret = __fio_netio_queue(td, io_u, io_u->ddir);
546 if (!o->pingpong || ret != FIO_Q_COMPLETED)
550 * For ping-pong mode, receive or send reply as needed
552 if (td_read(td) && io_u->ddir == DDIR_READ)
553 ret = __fio_netio_queue(td, io_u, DDIR_WRITE);
554 else if (td_write(td) && io_u->ddir == DDIR_WRITE)
555 ret = __fio_netio_queue(td, io_u, DDIR_READ);
560 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
562 struct netio_data *nd = td->io_ops->data;
563 struct netio_options *o = td->eo;
566 if (o->proto == FIO_TYPE_TCP) {
569 } else if (o->proto == FIO_TYPE_TCP_V6) {
572 } else if (o->proto == FIO_TYPE_UDP) {
575 } else if (o->proto == FIO_TYPE_UDP_V6) {
578 } else if (o->proto == FIO_TYPE_UNIX) {
582 log_err("fio: bad network type %d\n", o->proto);
587 f->fd = socket(domain, type, 0);
589 td_verror(td, errno, "socket");
593 #ifdef CONFIG_TCP_NODELAY
594 if (o->nodelay && is_tcp(o)) {
597 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
598 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
605 if (!fio_netio_is_multicast(td->o.filename))
608 log_err("fio: multicast not supported on IPv6\n");
614 struct in_addr interface_addr;
616 if (inet_aton(o->intfc, &interface_addr) == 0) {
617 log_err("fio: interface not valid interface IP\n");
621 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_IF, (const char*)&interface_addr, sizeof(interface_addr)) < 0) {
622 td_verror(td, errno, "setsockopt IP_MULTICAST_IF");
627 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_TTL, (const char*)&o->ttl, sizeof(o->ttl)) < 0) {
628 td_verror(td, errno, "setsockopt IP_MULTICAST_TTL");
633 } else if (o->proto == FIO_TYPE_TCP) {
634 socklen_t len = sizeof(nd->addr);
636 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
637 td_verror(td, errno, "connect");
641 } else if (o->proto == FIO_TYPE_TCP_V6) {
642 socklen_t len = sizeof(nd->addr6);
644 if (connect(f->fd, (struct sockaddr *) &nd->addr6, len) < 0) {
645 td_verror(td, errno, "connect");
651 struct sockaddr_un *addr = &nd->addr_un;
654 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
656 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
657 td_verror(td, errno, "connect");
666 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
668 struct netio_data *nd = td->io_ops->data;
669 struct netio_options *o = td->eo;
674 f->fd = nd->listenfd;
678 state = td->runstate;
679 td_set_runstate(td, TD_SETTING_UP);
681 log_info("fio: waiting for connection\n");
683 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
686 if (o->proto == FIO_TYPE_TCP) {
687 socklen = sizeof(nd->addr);
688 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
690 socklen = sizeof(nd->addr6);
691 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr6, &socklen);
695 td_verror(td, errno, "accept");
699 #ifdef CONFIG_TCP_NODELAY
700 if (o->nodelay && is_tcp(o)) {
703 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
704 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
711 td_set_runstate(td, state);
714 td_set_runstate(td, state);
718 static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
720 struct netio_data *nd = td->io_ops->data;
721 struct netio_options *o = td->eo;
722 struct udp_close_msg msg;
728 to = (struct sockaddr *) &nd->addr6;
729 len = sizeof(nd->addr6);
731 to = (struct sockaddr *) &nd->addr;
732 len = sizeof(nd->addr);
735 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
736 msg.cmd = htonl(FIO_LINK_CLOSE);
738 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
740 td_verror(td, errno, "sendto udp link close");
743 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
745 struct netio_options *o = td->eo;
748 * If this is an UDP connection, notify the receiver that we are
749 * closing down the link
752 fio_netio_udp_close(td, f);
754 return generic_close_file(td, f);
757 static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
759 struct netio_data *nd = td->io_ops->data;
760 struct netio_options *o = td->eo;
761 struct udp_close_msg msg;
767 len = sizeof(nd->addr6);
768 to = (struct sockaddr *) &nd->addr6;
770 len = sizeof(nd->addr);
771 to = (struct sockaddr *) &nd->addr;
774 ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
776 td_verror(td, errno, "recvfrom udp link open");
780 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
781 ntohl(msg.cmd) != FIO_LINK_OPEN) {
782 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
790 static int fio_netio_udp_send_open(struct thread_data *td, struct fio_file *f)
792 struct netio_data *nd = td->io_ops->data;
793 struct netio_options *o = td->eo;
794 struct udp_close_msg msg;
800 len = sizeof(nd->addr6);
801 to = (struct sockaddr *) &nd->addr6;
803 len = sizeof(nd->addr);
804 to = (struct sockaddr *) &nd->addr;
807 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
808 msg.cmd = htonl(FIO_LINK_OPEN);
810 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
812 td_verror(td, errno, "sendto udp link open");
819 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
822 struct netio_options *o = td->eo;
825 ret = fio_netio_accept(td, f);
827 ret = fio_netio_connect(td, f);
836 ret = fio_netio_udp_send_open(td, f);
840 state = td->runstate;
841 td_set_runstate(td, TD_SETTING_UP);
842 ret = fio_netio_udp_recv_open(td, f);
843 td_set_runstate(td, state);
848 fio_netio_close_file(td, f);
853 static int fio_fill_addr(struct thread_data *td, const char *host, int af,
854 void *dst, struct addrinfo **res)
856 struct netio_options *o = td->eo;
857 struct addrinfo hints;
860 if (inet_pton(af, host, dst))
863 memset(&hints, 0, sizeof(hints));
866 hints.ai_socktype = SOCK_STREAM;
868 hints.ai_socktype = SOCK_DGRAM;
871 hints.ai_family = AF_INET6;
873 hints.ai_family = AF_INET;
875 ret = getaddrinfo(host, NULL, &hints, res);
880 if (ret == EAI_SYSTEM)
883 snprintf(str, sizeof(str), "getaddrinfo: %s", gai_strerror(ret));
884 td_verror(td, e, str);
891 static int fio_netio_setup_connect_inet(struct thread_data *td,
892 const char *host, unsigned short port)
894 struct netio_data *nd = td->io_ops->data;
895 struct netio_options *o = td->eo;
896 struct addrinfo *res = NULL;
901 log_err("fio: connect with no host to connect to.\n");
903 log_err("fio: did you forget to set 'listen'?\n");
905 td_verror(td, EINVAL, "no hostname= set");
909 nd->addr.sin_family = AF_INET;
910 nd->addr.sin_port = htons(port);
911 nd->addr6.sin6_family = AF_INET6;
912 nd->addr6.sin6_port = htons(port);
922 if (fio_fill_addr(td, host, af, dst, &res))
929 len = sizeof(nd->addr6.sin6_addr);
930 src = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
932 len = sizeof(nd->addr.sin_addr);
933 src = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
936 memcpy(dst, src, len);
941 static int fio_netio_setup_connect_unix(struct thread_data *td,
944 struct netio_data *nd = td->io_ops->data;
945 struct sockaddr_un *soun = &nd->addr_un;
947 soun->sun_family = AF_UNIX;
948 strcpy(soun->sun_path, path);
952 static int fio_netio_setup_connect(struct thread_data *td)
954 struct netio_options *o = td->eo;
956 if (is_udp(o) || is_tcp(o))
957 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
959 return fio_netio_setup_connect_unix(td, td->o.filename);
962 static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
964 struct netio_data *nd = td->io_ops->data;
965 struct sockaddr_un *addr = &nd->addr_un;
969 fd = socket(AF_UNIX, SOCK_STREAM, 0);
971 log_err("fio: socket: %s\n", strerror(errno));
977 memset(addr, 0, sizeof(*addr));
978 addr->sun_family = AF_UNIX;
979 strcpy(addr->sun_path, path);
982 len = sizeof(addr->sun_family) + strlen(path) + 1;
984 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
985 log_err("fio: bind: %s\n", strerror(errno));
995 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
997 struct netio_data *nd = td->io_ops->data;
998 struct netio_options *o = td->eo;
1000 struct sockaddr_in sin;
1001 struct sockaddr_in6 sin6;
1002 struct sockaddr *saddr;
1003 int fd, opt, type, domain;
1006 memset(&sin, 0, sizeof(sin));
1007 memset(&sin6, 0, sizeof(sin6));
1009 if (o->proto == FIO_TYPE_TCP) {
1012 } else if (o->proto == FIO_TYPE_TCP_V6) {
1015 } else if (o->proto == FIO_TYPE_UDP) {
1018 } else if (o->proto == FIO_TYPE_UDP_V6) {
1022 log_err("fio: unknown proto %d\n", o->proto);
1026 fd = socket(domain, type, 0);
1028 td_verror(td, errno, "socket");
1033 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
1034 td_verror(td, errno, "setsockopt");
1039 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) {
1040 td_verror(td, errno, "setsockopt");
1046 if (td->o.filename) {
1047 if (!is_udp(o) || !fio_netio_is_multicast(td->o.filename)) {
1048 log_err("fio: hostname not valid for non-multicast inbound network IO\n");
1053 log_err("fio: IPv6 not supported for multicast network IO");
1058 inet_aton(td->o.filename, &sin.sin_addr);
1060 mr.imr_multiaddr = sin.sin_addr;
1062 if (inet_aton(o->intfc, &mr.imr_interface) == 0) {
1063 log_err("fio: interface not valid interface IP\n");
1068 mr.imr_interface.s_addr = htonl(INADDR_ANY);
1071 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mr, sizeof(mr)) < 0) {
1072 td_verror(td, errno, "setsockopt IP_ADD_MEMBERSHIP");
1079 saddr = (struct sockaddr *) &nd->addr;
1080 len = sizeof(nd->addr);
1082 nd->addr.sin_family = AF_INET;
1083 nd->addr.sin_addr.s_addr = sin.sin_addr.s_addr ? sin.sin_addr.s_addr : htonl(INADDR_ANY);
1084 nd->addr.sin_port = htons(port);
1086 saddr = (struct sockaddr *) &nd->addr6;
1087 len = sizeof(nd->addr6);
1089 nd->addr6.sin6_family = AF_INET6;
1090 nd->addr6.sin6_addr = sin6.sin6_addr.s6_addr ? sin6.sin6_addr : in6addr_any;
1091 nd->addr6.sin6_port = htons(port);
1094 if (bind(fd, saddr, len) < 0) {
1095 td_verror(td, errno, "bind");
1103 static int fio_netio_setup_listen(struct thread_data *td)
1105 struct netio_data *nd = td->io_ops->data;
1106 struct netio_options *o = td->eo;
1109 if (is_udp(o) || is_tcp(o))
1110 ret = fio_netio_setup_listen_inet(td, o->port);
1112 ret = fio_netio_setup_listen_unix(td, td->o.filename);
1119 if (listen(nd->listenfd, 10) < 0) {
1120 td_verror(td, errno, "listen");
1128 static int fio_netio_init(struct thread_data *td)
1130 struct netio_options *o = td->eo;
1135 WSAStartup(MAKEWORD(2,2), &wsd);
1138 if (td_random(td)) {
1139 log_err("fio: network IO can't be random\n");
1143 if (o->proto == FIO_TYPE_UNIX && o->port) {
1144 log_err("fio: network IO port not valid with unix socket\n");
1146 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
1147 log_err("fio: network IO requires port for tcp or udp\n");
1153 log_err("fio: listen only valid for TCP proto IO\n");
1157 log_err("fio: datagram network connections must be"
1158 " read OR write\n");
1161 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
1162 log_err("fio: UNIX sockets need host/filename\n");
1165 o->listen = td_read(td);
1169 ret = fio_netio_setup_listen(td);
1171 ret = fio_netio_setup_connect(td);
1176 static void fio_netio_cleanup(struct thread_data *td)
1178 struct netio_data *nd = td->io_ops->data;
1181 if (nd->listenfd != -1)
1182 close(nd->listenfd);
1183 if (nd->pipes[0] != -1)
1184 close(nd->pipes[0]);
1185 if (nd->pipes[1] != -1)
1186 close(nd->pipes[1]);
1192 static int fio_netio_setup(struct thread_data *td)
1194 struct netio_data *nd;
1196 if (!td->files_index) {
1197 add_file(td, td->o.filename ?: "net");
1198 td->o.nr_files = td->o.nr_files ?: 1;
1201 if (!td->io_ops->data) {
1202 nd = malloc(sizeof(*nd));;
1204 memset(nd, 0, sizeof(*nd));
1206 nd->pipes[0] = nd->pipes[1] = -1;
1207 td->io_ops->data = nd;
1213 static void fio_netio_terminate(struct thread_data *td)
1215 kill(td->pid, SIGUSR2);
1218 #ifdef CONFIG_LINUX_SPLICE
1219 static int fio_netio_setup_splice(struct thread_data *td)
1221 struct netio_data *nd;
1223 fio_netio_setup(td);
1225 nd = td->io_ops->data;
1227 if (pipe(nd->pipes) < 0)
1237 static struct ioengine_ops ioengine_splice = {
1238 .name = "netsplice",
1239 .version = FIO_IOOPS_VERSION,
1240 .prep = fio_netio_prep,
1241 .queue = fio_netio_queue,
1242 .setup = fio_netio_setup_splice,
1243 .init = fio_netio_init,
1244 .cleanup = fio_netio_cleanup,
1245 .open_file = fio_netio_open_file,
1246 .close_file = fio_netio_close_file,
1247 .terminate = fio_netio_terminate,
1249 .option_struct_size = sizeof(struct netio_options),
1250 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1255 static struct ioengine_ops ioengine_rw = {
1257 .version = FIO_IOOPS_VERSION,
1258 .prep = fio_netio_prep,
1259 .queue = fio_netio_queue,
1260 .setup = fio_netio_setup,
1261 .init = fio_netio_init,
1262 .cleanup = fio_netio_cleanup,
1263 .open_file = fio_netio_open_file,
1264 .close_file = fio_netio_close_file,
1265 .terminate = fio_netio_terminate,
1267 .option_struct_size = sizeof(struct netio_options),
1268 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1269 FIO_PIPEIO | FIO_BIT_BASED,
1272 static int str_hostname_cb(void *data, const char *input)
1274 struct netio_options *o = data;
1276 if (o->td->o.filename)
1277 free(o->td->o.filename);
1278 o->td->o.filename = strdup(input);
1282 static void fio_init fio_netio_register(void)
1284 register_ioengine(&ioengine_rw);
1285 #ifdef CONFIG_LINUX_SPLICE
1286 register_ioengine(&ioengine_splice);
1290 static void fio_exit fio_netio_unregister(void)
1292 unregister_ioengine(&ioengine_rw);
1293 #ifdef CONFIG_LINUX_SPLICE
1294 unregister_ioengine(&ioengine_splice);