net.c revision 5fdd124a3b811993542825847f207587d5f4661e
1ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe/*
2d4f12dd05cfb2e8e7a72604cd870e10f2394914eJens Axboe * Transfer data over the net.
3ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe */
4ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe#include <stdio.h>
5ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe#include <stdlib.h>
6ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe#include <unistd.h>
7ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe#include <errno.h>
8ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe#include <assert.h>
9ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe#include <netinet/in.h>
10ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe#include <arpa/inet.h>
11ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe#include <netdb.h>
125fdd124a3b811993542825847f207587d5f4661eJens Axboe#include <sys/poll.h>
13ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
14ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe#include "../fio.h"
15ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe#include "../os.h"
16ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
17ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboestruct net_data {
18ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	int send_to_net;
19ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	struct io_u *last_io_u;
20ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe};
21ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
22ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboestatic int fio_netio_getevents(struct thread_data *td, int fio_unused min,
23ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe				int max, struct timespec fio_unused *t)
24ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe{
25ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	assert(max <= 1);
26ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
27ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	/*
28ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	 * we can only have one finished io_u for sync io, since the depth
29ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	 * is always 1
30ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	 */
31ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	if (list_empty(&td->io_u_busylist))
32ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe		return 0;
33ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
34ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	return 1;
35ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe}
36ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
37ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboestatic struct io_u *fio_netio_event(struct thread_data *td, int event)
38ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe{
39ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	struct net_data *nd = td->io_ops->data;
40ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
41ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	assert(event == 0);
42ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
43ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	return nd->last_io_u;
44ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe}
45ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
46ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboestatic int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
47ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe{
48ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	struct net_data *nd = td->io_ops->data;
49ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	struct fio_file *f = io_u->file;
50ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
517a6499dada619928267d26b4629b0c8623dc423aJens Axboe	/*
527a6499dada619928267d26b4629b0c8623dc423aJens Axboe	 * Make sure we don't see spurious reads to a receiver, and vice versa
537a6499dada619928267d26b4629b0c8623dc423aJens Axboe	 */
547a6499dada619928267d26b4629b0c8623dc423aJens Axboe	if ((nd->send_to_net && io_u->ddir == DDIR_READ) ||
557a6499dada619928267d26b4629b0c8623dc423aJens Axboe	    (!nd->send_to_net && io_u->ddir == DDIR_WRITE)) {
567a6499dada619928267d26b4629b0c8623dc423aJens Axboe		printf("boo!\n");
577a6499dada619928267d26b4629b0c8623dc423aJens Axboe		td_verror(td, EINVAL);
587a6499dada619928267d26b4629b0c8623dc423aJens Axboe		return 1;
59ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	}
607a6499dada619928267d26b4629b0c8623dc423aJens Axboe
61ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	if (io_u->ddir == DDIR_SYNC)
62ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe		return 0;
63ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	if (io_u->offset == f->last_completed_pos)
64ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe		return 0;
65ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
66e01547d2b8bf79c3abe14e03c72398cf8998eb1eJens Axboe	/*
67e01547d2b8bf79c3abe14e03c72398cf8998eb1eJens Axboe	 * If offset is different from last end position, it's a seek.
68e01547d2b8bf79c3abe14e03c72398cf8998eb1eJens Axboe	 * As network io is purely sequential, we don't allow seeks.
69e01547d2b8bf79c3abe14e03c72398cf8998eb1eJens Axboe	 */
70ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	td_verror(td, EINVAL);
71ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	return 1;
72ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe}
73ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
74ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboestatic int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
75ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe{
76ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	struct net_data *nd = td->io_ops->data;
77ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	struct fio_file *f = io_u->file;
78d4f12dd05cfb2e8e7a72604cd870e10f2394914eJens Axboe	int ret, flags = 0;
797a6499dada619928267d26b4629b0c8623dc423aJens Axboe
807a6499dada619928267d26b4629b0c8623dc423aJens Axboe	if (io_u->ddir == DDIR_WRITE) {
817a6499dada619928267d26b4629b0c8623dc423aJens Axboe		/*
827a6499dada619928267d26b4629b0c8623dc423aJens Axboe		 * if we are going to write more, set MSG_MORE
837a6499dada619928267d26b4629b0c8623dc423aJens Axboe		 */
847a6499dada619928267d26b4629b0c8623dc423aJens Axboe		if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
857a6499dada619928267d26b4629b0c8623dc423aJens Axboe		    td->io_size)
867a6499dada619928267d26b4629b0c8623dc423aJens Axboe			flags = MSG_MORE;
87ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
887a6499dada619928267d26b4629b0c8623dc423aJens Axboe		ret = send(f->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
89d4f12dd05cfb2e8e7a72604cd870e10f2394914eJens Axboe	} else if (io_u->ddir == DDIR_READ) {
90d4f12dd05cfb2e8e7a72604cd870e10f2394914eJens Axboe		flags = MSG_WAITALL;
91d4f12dd05cfb2e8e7a72604cd870e10f2394914eJens Axboe		ret = recv(f->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
92d4f12dd05cfb2e8e7a72604cd870e10f2394914eJens Axboe	} else
937a6499dada619928267d26b4629b0c8623dc423aJens Axboe		ret = 0;	/* must be a SYNC */
94ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
95cec6b55da1c282b5b91ad346c7804171fccf151eJens Axboe	if (ret != (int) io_u->xfer_buflen) {
96ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe		if (ret > 0) {
97cec6b55da1c282b5b91ad346c7804171fccf151eJens Axboe			io_u->resid = io_u->xfer_buflen - ret;
98cec6b55da1c282b5b91ad346c7804171fccf151eJens Axboe			io_u->error = 0;
99cec6b55da1c282b5b91ad346c7804171fccf151eJens Axboe			return ret;
100ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe		} else
101ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe			io_u->error = errno;
102ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	}
103ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
104ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	if (!io_u->error)
105ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe		nd->last_io_u = io_u;
10695bcd815e5ce55d6cdd8eb83bda5ee411f37bdc9Jens Axboe	else
10795bcd815e5ce55d6cdd8eb83bda5ee411f37bdc9Jens Axboe		td_verror(td, io_u->error);
108ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
109ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	return io_u->error;
110ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe}
111ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
112ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboestatic int fio_netio_setup_connect(struct thread_data *td, const char *host,
113e01547d2b8bf79c3abe14e03c72398cf8998eb1eJens Axboe				   unsigned short port)
114ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe{
115ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	struct sockaddr_in addr;
116ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	struct fio_file *f;
1172fc2698113c087352c1851bf5ebfcad6adb53932Jens Axboe	int i;
118ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
119ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	memset(&addr, 0, sizeof(addr));
120ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	addr.sin_family = AF_INET;
121e01547d2b8bf79c3abe14e03c72398cf8998eb1eJens Axboe	addr.sin_port = htons(port);
122ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
123ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	if (inet_aton(host, &addr.sin_addr) != 1) {
1247a6499dada619928267d26b4629b0c8623dc423aJens Axboe		struct hostent *hent;
125ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
1267a6499dada619928267d26b4629b0c8623dc423aJens Axboe		hent = gethostbyname(host);
127ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe		if (!hent) {
1286bedbfafcffbc7202b5bb621ac5886aafdc0f362Jens Axboe			td_verror(td, errno);
129ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe			return 1;
130ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe		}
131ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
132ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe		memcpy(&addr.sin_addr, hent->h_addr, 4);
133ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	}
134ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
1352fc2698113c087352c1851bf5ebfcad6adb53932Jens Axboe	for_each_file(td, f, i) {
1366bedbfafcffbc7202b5bb621ac5886aafdc0f362Jens Axboe		f->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1372fc2698113c087352c1851bf5ebfcad6adb53932Jens Axboe		if (f->fd < 0) {
1386bedbfafcffbc7202b5bb621ac5886aafdc0f362Jens Axboe			td_verror(td, errno);
1392fc2698113c087352c1851bf5ebfcad6adb53932Jens Axboe			return 1;
1402fc2698113c087352c1851bf5ebfcad6adb53932Jens Axboe		}
141ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
1422fc2698113c087352c1851bf5ebfcad6adb53932Jens Axboe		if (connect(f->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1436bedbfafcffbc7202b5bb621ac5886aafdc0f362Jens Axboe			td_verror(td, errno);
1442fc2698113c087352c1851bf5ebfcad6adb53932Jens Axboe			return 1;
1452fc2698113c087352c1851bf5ebfcad6adb53932Jens Axboe		}
146ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	}
147ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
148ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	return 0;
149ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
150ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe}
151ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
1525fdd124a3b811993542825847f207587d5f4661eJens Axboestatic int fio_netio_accept_connections(struct thread_data *td, int fd,
1535fdd124a3b811993542825847f207587d5f4661eJens Axboe					struct sockaddr_in *addr)
1545fdd124a3b811993542825847f207587d5f4661eJens Axboe{
1555fdd124a3b811993542825847f207587d5f4661eJens Axboe	socklen_t socklen = sizeof(*addr);
1565fdd124a3b811993542825847f207587d5f4661eJens Axboe	unsigned int accepts = 0;
1575fdd124a3b811993542825847f207587d5f4661eJens Axboe	struct pollfd pfd;
1585fdd124a3b811993542825847f207587d5f4661eJens Axboe
1595fdd124a3b811993542825847f207587d5f4661eJens Axboe	fprintf(f_out, "fio: waiting for %u connections\n", td->nr_files);
1605fdd124a3b811993542825847f207587d5f4661eJens Axboe
1615fdd124a3b811993542825847f207587d5f4661eJens Axboe	/*
1625fdd124a3b811993542825847f207587d5f4661eJens Axboe	 * Accept loop. poll for incoming events, accept them. Repeat until we
1635fdd124a3b811993542825847f207587d5f4661eJens Axboe	 * have all connections.
1645fdd124a3b811993542825847f207587d5f4661eJens Axboe	 */
1655fdd124a3b811993542825847f207587d5f4661eJens Axboe	while (!td->terminate && accepts < td->nr_files) {
1665fdd124a3b811993542825847f207587d5f4661eJens Axboe		struct fio_file *f;
1675fdd124a3b811993542825847f207587d5f4661eJens Axboe		int ret, i;
1685fdd124a3b811993542825847f207587d5f4661eJens Axboe
1695fdd124a3b811993542825847f207587d5f4661eJens Axboe		pfd.fd = fd;
1705fdd124a3b811993542825847f207587d5f4661eJens Axboe		pfd.events = POLLIN;
1715fdd124a3b811993542825847f207587d5f4661eJens Axboe
1725fdd124a3b811993542825847f207587d5f4661eJens Axboe		ret = poll(&pfd, 1, -1);
1735fdd124a3b811993542825847f207587d5f4661eJens Axboe		if (ret < 0) {
1745fdd124a3b811993542825847f207587d5f4661eJens Axboe			if (errno == EINTR)
1755fdd124a3b811993542825847f207587d5f4661eJens Axboe				continue;
1765fdd124a3b811993542825847f207587d5f4661eJens Axboe
1775fdd124a3b811993542825847f207587d5f4661eJens Axboe			td_verror(td, errno);
1785fdd124a3b811993542825847f207587d5f4661eJens Axboe			break;
1795fdd124a3b811993542825847f207587d5f4661eJens Axboe		} else if (!ret)
1805fdd124a3b811993542825847f207587d5f4661eJens Axboe			continue;
1815fdd124a3b811993542825847f207587d5f4661eJens Axboe
1825fdd124a3b811993542825847f207587d5f4661eJens Axboe		for_each_file(td, f, i) {
1835fdd124a3b811993542825847f207587d5f4661eJens Axboe			if (f->fd != -1)
1845fdd124a3b811993542825847f207587d5f4661eJens Axboe				continue;
1855fdd124a3b811993542825847f207587d5f4661eJens Axboe
1865fdd124a3b811993542825847f207587d5f4661eJens Axboe			f->fd = accept(fd, (struct sockaddr *) addr, &socklen);
1875fdd124a3b811993542825847f207587d5f4661eJens Axboe			if (f->fd < 0) {
1885fdd124a3b811993542825847f207587d5f4661eJens Axboe				td_verror(td, errno);
1895fdd124a3b811993542825847f207587d5f4661eJens Axboe				return 1;
1905fdd124a3b811993542825847f207587d5f4661eJens Axboe			}
1915fdd124a3b811993542825847f207587d5f4661eJens Axboe			accepts++;
1925fdd124a3b811993542825847f207587d5f4661eJens Axboe			break;
1935fdd124a3b811993542825847f207587d5f4661eJens Axboe		}
1945fdd124a3b811993542825847f207587d5f4661eJens Axboe	}
1955fdd124a3b811993542825847f207587d5f4661eJens Axboe
1965fdd124a3b811993542825847f207587d5f4661eJens Axboe	return 0;
1975fdd124a3b811993542825847f207587d5f4661eJens Axboe}
1985fdd124a3b811993542825847f207587d5f4661eJens Axboe
199e01547d2b8bf79c3abe14e03c72398cf8998eb1eJens Axboestatic int fio_netio_setup_listen(struct thread_data *td, unsigned short port)
200ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe{
201ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	struct sockaddr_in addr;
2025fdd124a3b811993542825847f207587d5f4661eJens Axboe	int fd, opt;
203ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
2046bedbfafcffbc7202b5bb621ac5886aafdc0f362Jens Axboe	fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
205ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	if (fd < 0) {
2066bedbfafcffbc7202b5bb621ac5886aafdc0f362Jens Axboe		td_verror(td, errno);
207ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe		return 1;
208ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	}
209ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
210ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	opt = 1;
211ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
2126bedbfafcffbc7202b5bb621ac5886aafdc0f362Jens Axboe		td_verror(td, errno);
213ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe		return 1;
214ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	}
2156bedbfafcffbc7202b5bb621ac5886aafdc0f362Jens Axboe#ifdef SO_REUSEPORT
2166bedbfafcffbc7202b5bb621ac5886aafdc0f362Jens Axboe	if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
2176bedbfafcffbc7202b5bb621ac5886aafdc0f362Jens Axboe		td_verror(td, errno);
2186bedbfafcffbc7202b5bb621ac5886aafdc0f362Jens Axboe		return 1;
2196bedbfafcffbc7202b5bb621ac5886aafdc0f362Jens Axboe	}
2206bedbfafcffbc7202b5bb621ac5886aafdc0f362Jens Axboe#endif
221ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
222ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	memset(&addr, 0, sizeof(addr));
223ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	addr.sin_family = AF_INET;
224ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	addr.sin_addr.s_addr = htonl(INADDR_ANY);
225e01547d2b8bf79c3abe14e03c72398cf8998eb1eJens Axboe	addr.sin_port = htons(port);
226ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
227ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2286bedbfafcffbc7202b5bb621ac5886aafdc0f362Jens Axboe		td_verror(td, errno);
229ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe		return 1;
230ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	}
231ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	if (listen(fd, 1) < 0) {
2326bedbfafcffbc7202b5bb621ac5886aafdc0f362Jens Axboe		td_verror(td, errno);
233ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe		return 1;
234ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	}
235ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
2365fdd124a3b811993542825847f207587d5f4661eJens Axboe	return fio_netio_accept_connections(td, fd, &addr);
237ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe}
238ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
239ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboestatic int fio_netio_setup(struct thread_data *td)
240ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe{
241e01547d2b8bf79c3abe14e03c72398cf8998eb1eJens Axboe	char host[64], buf[128];
242ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	struct net_data *nd;
243e01547d2b8bf79c3abe14e03c72398cf8998eb1eJens Axboe	unsigned short port;
2442fc2698113c087352c1851bf5ebfcad6adb53932Jens Axboe	struct fio_file *f;
245ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	char *sep;
2462fc2698113c087352c1851bf5ebfcad6adb53932Jens Axboe	int ret, i;
247ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
2487a6499dada619928267d26b4629b0c8623dc423aJens Axboe	if (!td->total_file_size) {
2497a6499dada619928267d26b4629b0c8623dc423aJens Axboe		log_err("fio: need size= set\n");
2507a6499dada619928267d26b4629b0c8623dc423aJens Axboe		return 1;
2517a6499dada619928267d26b4629b0c8623dc423aJens Axboe	}
2527a6499dada619928267d26b4629b0c8623dc423aJens Axboe
253ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	/*
254ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	 * work around for late init call
255ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	 */
256ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	if (td->io_ops->init(td))
257ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe		return 1;
258ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
259ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	nd = td->io_ops->data;
260ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
261ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	if (td->iomix) {
262ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe		log_err("fio: network connections must be read OR write\n");
263ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe		return 1;
264ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	}
265ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
266ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	strcpy(buf, td->filename);
267ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
268ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	sep = strchr(buf, ':');
269ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	if (!sep) {
270ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe		log_err("fio: bad network host:port <<%s>>\n", td->filename);
271ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe		return 1;
272ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	}
273ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
274ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	*sep = '\0';
275ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	sep++;
276ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	strcpy(host, buf);
277e01547d2b8bf79c3abe14e03c72398cf8998eb1eJens Axboe	port = atoi(sep);
278ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
27985eb1d4492ab4ee961b2a2276192298b2009584fJens Axboe	if (td->ddir == DDIR_READ) {
280ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe		nd->send_to_net = 0;
281ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe		ret = fio_netio_setup_listen(td, port);
282ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	} else {
283ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe		nd->send_to_net = 1;
284ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe		ret = fio_netio_setup_connect(td, host, port);
285ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	}
286ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
2872fc2698113c087352c1851bf5ebfcad6adb53932Jens Axboe	if (ret)
2882fc2698113c087352c1851bf5ebfcad6adb53932Jens Axboe		return ret;
2892fc2698113c087352c1851bf5ebfcad6adb53932Jens Axboe
2902fc2698113c087352c1851bf5ebfcad6adb53932Jens Axboe	td->io_size = td->total_file_size;
2912fc2698113c087352c1851bf5ebfcad6adb53932Jens Axboe	td->total_io_size = td->io_size;
2922fc2698113c087352c1851bf5ebfcad6adb53932Jens Axboe
2932fc2698113c087352c1851bf5ebfcad6adb53932Jens Axboe	for_each_file(td, f, i) {
2942fc2698113c087352c1851bf5ebfcad6adb53932Jens Axboe		f->file_size = td->total_file_size / td->nr_files;
2952fc2698113c087352c1851bf5ebfcad6adb53932Jens Axboe		f->real_file_size = f->file_size;
296ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	}
297ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
2982fc2698113c087352c1851bf5ebfcad6adb53932Jens Axboe	return 0;
299ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe}
300ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
301ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboestatic void fio_netio_cleanup(struct thread_data *td)
302ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe{
303ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	if (td->io_ops->data) {
304ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe		free(td->io_ops->data);
305ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe		td->io_ops->data = NULL;
306ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	}
307ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe}
308ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
309ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboestatic int fio_netio_init(struct thread_data *td)
310ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe{
311ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	struct net_data *nd;
312ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
313e01547d2b8bf79c3abe14e03c72398cf8998eb1eJens Axboe	/*
314e01547d2b8bf79c3abe14e03c72398cf8998eb1eJens Axboe	 * Hack to work-around the ->setup() function calling init on its
315e01547d2b8bf79c3abe14e03c72398cf8998eb1eJens Axboe	 * own, since it needs ->io_ops->data to be set up.
316e01547d2b8bf79c3abe14e03c72398cf8998eb1eJens Axboe	 */
317ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	if (td->io_ops->data)
318ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe		return 0;
319ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
320ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	nd  = malloc(sizeof(*nd));
321ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	nd->last_io_u = NULL;
322ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	td->io_ops->data = nd;
323ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	return 0;
324ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe}
325ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
326ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboestatic struct ioengine_ops ioengine = {
327ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	.name		= "net",
328ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	.version	= FIO_IOOPS_VERSION,
329ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	.init		= fio_netio_init,
330ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	.prep		= fio_netio_prep,
331ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	.queue		= fio_netio_queue,
332ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	.getevents	= fio_netio_getevents,
333ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	.event		= fio_netio_event,
334ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	.cleanup	= fio_netio_cleanup,
335ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	.setup		= fio_netio_setup,
336ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	.flags		= FIO_SYNCIO | FIO_NETIO,
337ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe};
338ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
339ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboestatic void fio_init fio_netio_register(void)
340ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe{
341ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	register_ioengine(&ioengine);
342ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe}
343ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe
344ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboestatic void fio_exit fio_netio_unregister(void)
345ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe{
346ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe	unregister_ioengine(&ioengine);
347ed92ac0ce9ce1cc64697272d307d4fa7d18ed64cJens Axboe}
348