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;
395 const struct sockaddr *to;
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;
527 if (ddir == DDIR_WRITE && err == EMSGSIZE)
535 td_verror(td, io_u->error, "xfer");
537 return FIO_Q_COMPLETED;
540 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
542 struct netio_options *o = td->eo;
545 fio_ro_check(td, io_u);
547 ret = __fio_netio_queue(td, io_u, io_u->ddir);
548 if (!o->pingpong || ret != FIO_Q_COMPLETED)
552 * For ping-pong mode, receive or send reply as needed
554 if (td_read(td) && io_u->ddir == DDIR_READ)
555 ret = __fio_netio_queue(td, io_u, DDIR_WRITE);
556 else if (td_write(td) && io_u->ddir == DDIR_WRITE)
557 ret = __fio_netio_queue(td, io_u, DDIR_READ);
562 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
564 struct netio_data *nd = td->io_ops->data;
565 struct netio_options *o = td->eo;
568 if (o->proto == FIO_TYPE_TCP) {
571 } else if (o->proto == FIO_TYPE_TCP_V6) {
574 } else if (o->proto == FIO_TYPE_UDP) {
577 } else if (o->proto == FIO_TYPE_UDP_V6) {
580 } else if (o->proto == FIO_TYPE_UNIX) {
584 log_err("fio: bad network type %d\n", o->proto);
589 f->fd = socket(domain, type, 0);
591 td_verror(td, errno, "socket");
595 #ifdef CONFIG_TCP_NODELAY
596 if (o->nodelay && is_tcp(o)) {
599 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
600 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
607 if (!fio_netio_is_multicast(td->o.filename))
610 log_err("fio: multicast not supported on IPv6\n");
616 struct in_addr interface_addr;
618 if (inet_aton(o->intfc, &interface_addr) == 0) {
619 log_err("fio: interface not valid interface IP\n");
623 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_IF, (const char*)&interface_addr, sizeof(interface_addr)) < 0) {
624 td_verror(td, errno, "setsockopt IP_MULTICAST_IF");
629 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_TTL, (const char*)&o->ttl, sizeof(o->ttl)) < 0) {
630 td_verror(td, errno, "setsockopt IP_MULTICAST_TTL");
635 } else if (o->proto == FIO_TYPE_TCP) {
636 socklen_t len = sizeof(nd->addr);
638 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
639 td_verror(td, errno, "connect");
643 } else if (o->proto == FIO_TYPE_TCP_V6) {
644 socklen_t len = sizeof(nd->addr6);
646 if (connect(f->fd, (struct sockaddr *) &nd->addr6, len) < 0) {
647 td_verror(td, errno, "connect");
653 struct sockaddr_un *addr = &nd->addr_un;
656 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
658 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
659 td_verror(td, errno, "connect");
668 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
670 struct netio_data *nd = td->io_ops->data;
671 struct netio_options *o = td->eo;
676 f->fd = nd->listenfd;
680 state = td->runstate;
681 td_set_runstate(td, TD_SETTING_UP);
683 log_info("fio: waiting for connection\n");
685 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
688 if (o->proto == FIO_TYPE_TCP) {
689 socklen = sizeof(nd->addr);
690 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
692 socklen = sizeof(nd->addr6);
693 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr6, &socklen);
697 td_verror(td, errno, "accept");
701 #ifdef CONFIG_TCP_NODELAY
702 if (o->nodelay && is_tcp(o)) {
705 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
706 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
713 td_set_runstate(td, state);
716 td_set_runstate(td, state);
720 static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
722 struct netio_data *nd = td->io_ops->data;
723 struct netio_options *o = td->eo;
724 struct udp_close_msg msg;
730 to = (struct sockaddr *) &nd->addr6;
731 len = sizeof(nd->addr6);
733 to = (struct sockaddr *) &nd->addr;
734 len = sizeof(nd->addr);
737 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
738 msg.cmd = htonl(FIO_LINK_CLOSE);
740 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
742 td_verror(td, errno, "sendto udp link close");
745 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
747 struct netio_options *o = td->eo;
750 * If this is an UDP connection, notify the receiver that we are
751 * closing down the link
754 fio_netio_udp_close(td, f);
756 return generic_close_file(td, f);
759 static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
761 struct netio_data *nd = td->io_ops->data;
762 struct netio_options *o = td->eo;
763 struct udp_close_msg msg;
769 len = sizeof(nd->addr6);
770 to = (struct sockaddr *) &nd->addr6;
772 len = sizeof(nd->addr);
773 to = (struct sockaddr *) &nd->addr;
776 ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
778 td_verror(td, errno, "recvfrom udp link open");
782 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
783 ntohl(msg.cmd) != FIO_LINK_OPEN) {
784 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
792 static int fio_netio_udp_send_open(struct thread_data *td, struct fio_file *f)
794 struct netio_data *nd = td->io_ops->data;
795 struct netio_options *o = td->eo;
796 struct udp_close_msg msg;
802 len = sizeof(nd->addr6);
803 to = (struct sockaddr *) &nd->addr6;
805 len = sizeof(nd->addr);
806 to = (struct sockaddr *) &nd->addr;
809 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
810 msg.cmd = htonl(FIO_LINK_OPEN);
812 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
814 td_verror(td, errno, "sendto udp link open");
821 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
824 struct netio_options *o = td->eo;
827 ret = fio_netio_accept(td, f);
829 ret = fio_netio_connect(td, f);
838 ret = fio_netio_udp_send_open(td, f);
842 state = td->runstate;
843 td_set_runstate(td, TD_SETTING_UP);
844 ret = fio_netio_udp_recv_open(td, f);
845 td_set_runstate(td, state);
850 fio_netio_close_file(td, f);
855 static int fio_fill_addr(struct thread_data *td, const char *host, int af,
856 void *dst, struct addrinfo **res)
858 struct netio_options *o = td->eo;
859 struct addrinfo hints;
862 if (inet_pton(af, host, dst))
865 memset(&hints, 0, sizeof(hints));
868 hints.ai_socktype = SOCK_STREAM;
870 hints.ai_socktype = SOCK_DGRAM;
873 hints.ai_family = AF_INET6;
875 hints.ai_family = AF_INET;
877 ret = getaddrinfo(host, NULL, &hints, res);
882 if (ret == EAI_SYSTEM)
885 snprintf(str, sizeof(str), "getaddrinfo: %s", gai_strerror(ret));
886 td_verror(td, e, str);
893 static int fio_netio_setup_connect_inet(struct thread_data *td,
894 const char *host, unsigned short port)
896 struct netio_data *nd = td->io_ops->data;
897 struct netio_options *o = td->eo;
898 struct addrinfo *res = NULL;
903 log_err("fio: connect with no host to connect to.\n");
905 log_err("fio: did you forget to set 'listen'?\n");
907 td_verror(td, EINVAL, "no hostname= set");
911 nd->addr.sin_family = AF_INET;
912 nd->addr.sin_port = htons(port);
913 nd->addr6.sin6_family = AF_INET6;
914 nd->addr6.sin6_port = htons(port);
918 dst = &nd->addr6.sin6_addr;
921 dst = &nd->addr.sin_addr;
924 if (fio_fill_addr(td, host, af, dst, &res))
931 len = sizeof(nd->addr6.sin6_addr);
932 src = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
934 len = sizeof(nd->addr.sin_addr);
935 src = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
938 memcpy(dst, src, len);
943 static int fio_netio_setup_connect_unix(struct thread_data *td,
946 struct netio_data *nd = td->io_ops->data;
947 struct sockaddr_un *soun = &nd->addr_un;
949 soun->sun_family = AF_UNIX;
950 memset(soun->sun_path, 0, sizeof(soun->sun_path));
951 strncpy(soun->sun_path, path, sizeof(soun->sun_path) - 1);
955 static int fio_netio_setup_connect(struct thread_data *td)
957 struct netio_options *o = td->eo;
959 if (is_udp(o) || is_tcp(o))
960 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
962 return fio_netio_setup_connect_unix(td, td->o.filename);
965 static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
967 struct netio_data *nd = td->io_ops->data;
968 struct sockaddr_un *addr = &nd->addr_un;
972 fd = socket(AF_UNIX, SOCK_STREAM, 0);
974 log_err("fio: socket: %s\n", strerror(errno));
980 memset(addr, 0, sizeof(*addr));
981 addr->sun_family = AF_UNIX;
982 strncpy(addr->sun_path, path, sizeof(addr->sun_path) - 1);
985 len = sizeof(addr->sun_family) + strlen(path) + 1;
987 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
988 log_err("fio: bind: %s\n", strerror(errno));
998 static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
1000 struct netio_data *nd = td->io_ops->data;
1001 struct netio_options *o = td->eo;
1003 struct sockaddr_in sin;
1004 struct sockaddr *saddr;
1005 int fd, opt, type, domain;
1008 memset(&sin, 0, sizeof(sin));
1010 if (o->proto == FIO_TYPE_TCP) {
1013 } else if (o->proto == FIO_TYPE_TCP_V6) {
1016 } else if (o->proto == FIO_TYPE_UDP) {
1019 } else if (o->proto == FIO_TYPE_UDP_V6) {
1023 log_err("fio: unknown proto %d\n", o->proto);
1027 fd = socket(domain, type, 0);
1029 td_verror(td, errno, "socket");
1034 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
1035 td_verror(td, errno, "setsockopt");
1040 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) {
1041 td_verror(td, errno, "setsockopt");
1047 if (td->o.filename) {
1048 if (!is_udp(o) || !fio_netio_is_multicast(td->o.filename)) {
1049 log_err("fio: hostname not valid for non-multicast inbound network IO\n");
1054 log_err("fio: IPv6 not supported for multicast network IO");
1059 inet_aton(td->o.filename, &sin.sin_addr);
1061 mr.imr_multiaddr = sin.sin_addr;
1063 if (inet_aton(o->intfc, &mr.imr_interface) == 0) {
1064 log_err("fio: interface not valid interface IP\n");
1069 mr.imr_interface.s_addr = htonl(INADDR_ANY);
1072 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mr, sizeof(mr)) < 0) {
1073 td_verror(td, errno, "setsockopt IP_ADD_MEMBERSHIP");
1080 saddr = (struct sockaddr *) &nd->addr;
1081 len = sizeof(nd->addr);
1083 nd->addr.sin_family = AF_INET;
1084 nd->addr.sin_addr.s_addr = sin.sin_addr.s_addr ? sin.sin_addr.s_addr : htonl(INADDR_ANY);
1085 nd->addr.sin_port = htons(port);
1087 saddr = (struct sockaddr *) &nd->addr6;
1088 len = sizeof(nd->addr6);
1090 nd->addr6.sin6_family = AF_INET6;
1091 nd->addr6.sin6_addr = in6addr_any;
1092 nd->addr6.sin6_port = htons(port);
1095 if (bind(fd, saddr, len) < 0) {
1097 td_verror(td, errno, "bind");
1105 static int fio_netio_setup_listen(struct thread_data *td)
1107 struct netio_data *nd = td->io_ops->data;
1108 struct netio_options *o = td->eo;
1111 if (is_udp(o) || is_tcp(o))
1112 ret = fio_netio_setup_listen_inet(td, o->port);
1114 ret = fio_netio_setup_listen_unix(td, td->o.filename);
1121 if (listen(nd->listenfd, 10) < 0) {
1122 td_verror(td, errno, "listen");
1130 static int fio_netio_init(struct thread_data *td)
1132 struct netio_options *o = td->eo;
1137 WSAStartup(MAKEWORD(2,2), &wsd);
1140 if (td_random(td)) {
1141 log_err("fio: network IO can't be random\n");
1145 if (o->proto == FIO_TYPE_UNIX && o->port) {
1146 log_err("fio: network IO port not valid with unix socket\n");
1148 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
1149 log_err("fio: network IO requires port for tcp or udp\n");
1155 log_err("fio: listen only valid for TCP proto IO\n");
1159 log_err("fio: datagram network connections must be"
1160 " read OR write\n");
1163 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
1164 log_err("fio: UNIX sockets need host/filename\n");
1167 o->listen = td_read(td);
1171 ret = fio_netio_setup_listen(td);
1173 ret = fio_netio_setup_connect(td);
1178 static void fio_netio_cleanup(struct thread_data *td)
1180 struct netio_data *nd = td->io_ops->data;
1183 if (nd->listenfd != -1)
1184 close(nd->listenfd);
1185 if (nd->pipes[0] != -1)
1186 close(nd->pipes[0]);
1187 if (nd->pipes[1] != -1)
1188 close(nd->pipes[1]);
1194 static int fio_netio_setup(struct thread_data *td)
1196 struct netio_data *nd;
1198 if (!td->files_index) {
1199 add_file(td, td->o.filename ?: "net", 0, 0);
1200 td->o.nr_files = td->o.nr_files ?: 1;
1204 if (!td->io_ops->data) {
1205 nd = malloc(sizeof(*nd));;
1207 memset(nd, 0, sizeof(*nd));
1209 nd->pipes[0] = nd->pipes[1] = -1;
1210 td->io_ops->data = nd;
1216 static void fio_netio_terminate(struct thread_data *td)
1218 kill(td->pid, SIGTERM);
1221 #ifdef CONFIG_LINUX_SPLICE
1222 static int fio_netio_setup_splice(struct thread_data *td)
1224 struct netio_data *nd;
1226 fio_netio_setup(td);
1228 nd = td->io_ops->data;
1230 if (pipe(nd->pipes) < 0)
1240 static struct ioengine_ops ioengine_splice = {
1241 .name = "netsplice",
1242 .version = FIO_IOOPS_VERSION,
1243 .prep = fio_netio_prep,
1244 .queue = fio_netio_queue,
1245 .setup = fio_netio_setup_splice,
1246 .init = fio_netio_init,
1247 .cleanup = fio_netio_cleanup,
1248 .open_file = fio_netio_open_file,
1249 .close_file = fio_netio_close_file,
1250 .terminate = fio_netio_terminate,
1252 .option_struct_size = sizeof(struct netio_options),
1253 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1258 static struct ioengine_ops ioengine_rw = {
1260 .version = FIO_IOOPS_VERSION,
1261 .prep = fio_netio_prep,
1262 .queue = fio_netio_queue,
1263 .setup = fio_netio_setup,
1264 .init = fio_netio_init,
1265 .cleanup = fio_netio_cleanup,
1266 .open_file = fio_netio_open_file,
1267 .close_file = fio_netio_close_file,
1268 .terminate = fio_netio_terminate,
1270 .option_struct_size = sizeof(struct netio_options),
1271 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
1272 FIO_PIPEIO | FIO_BIT_BASED,
1275 static int str_hostname_cb(void *data, const char *input)
1277 struct netio_options *o = data;
1279 if (o->td->o.filename)
1280 free(o->td->o.filename);
1281 o->td->o.filename = strdup(input);
1285 static void fio_init fio_netio_register(void)
1287 register_ioengine(&ioengine_rw);
1288 #ifdef CONFIG_LINUX_SPLICE
1289 register_ioengine(&ioengine_splice);
1293 static void fio_exit fio_netio_unregister(void)
1295 unregister_ioengine(&ioengine_rw);
1296 #ifdef CONFIG_LINUX_SPLICE
1297 unregister_ioengine(&ioengine_splice);