2 * Transfer data over the net.
9 #include <netinet/in.h>
10 #include <arpa/inet.h>
17 #define send_to_net(td) ((td)->io_ops->priv)
19 static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
21 struct fio_file *f = io_u->file;
24 * Make sure we don't see spurious reads to a receiver, and vice versa
26 if ((send_to_net(td) && io_u->ddir == DDIR_READ) ||
27 (!send_to_net(td) && io_u->ddir == DDIR_WRITE)) {
28 td_verror(td, EINVAL, "bad direction");
32 if (io_u->ddir == DDIR_SYNC)
34 if (io_u->offset == f->last_completed_pos)
38 * If offset is different from last end position, it's a seek.
39 * As network io is purely sequential, we don't allow seeks.
41 td_verror(td, EINVAL, "cannot seek");
45 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
47 struct fio_file *f = io_u->file;
50 if (io_u->ddir == DDIR_WRITE) {
52 * if we are going to write more, set MSG_MORE
54 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
58 ret = send(f->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
59 } else if (io_u->ddir == DDIR_READ) {
61 ret = recv(f->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
63 ret = 0; /* must be a SYNC */
65 if (ret != (int) io_u->xfer_buflen) {
67 io_u->resid = io_u->xfer_buflen - ret;
69 return FIO_Q_COMPLETED;
75 td_verror(td, io_u->error, "xfer");
77 return FIO_Q_COMPLETED;
80 static int fio_netio_setup_connect(struct thread_data *td, const char *host,
83 struct sockaddr_in addr;
87 memset(&addr, 0, sizeof(addr));
88 addr.sin_family = AF_INET;
89 addr.sin_port = htons(port);
91 if (inet_aton(host, &addr.sin_addr) != 1) {
94 hent = gethostbyname(host);
96 td_verror(td, errno, "gethostbyname");
100 memcpy(&addr.sin_addr, hent->h_addr, 4);
103 for_each_file(td, f, i) {
104 f->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
106 td_verror(td, errno, "socket");
110 if (connect(f->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
111 td_verror(td, errno, "connect");
120 static int fio_netio_accept_connections(struct thread_data *td, int fd,
121 struct sockaddr_in *addr)
123 socklen_t socklen = sizeof(*addr);
124 unsigned int accepts = 0;
127 fprintf(f_out, "fio: waiting for %u connections\n", td->nr_files);
130 * Accept loop. poll for incoming events, accept them. Repeat until we
131 * have all connections.
133 while (!td->terminate && accepts < td->nr_files) {
140 ret = poll(&pfd, 1, -1);
145 td_verror(td, errno, "poll");
151 * should be impossible
153 if (!(pfd.revents & POLLIN))
156 for_each_file(td, f, i) {
160 f->fd = accept(fd, (struct sockaddr *) addr, &socklen);
162 td_verror(td, errno, "accept");
173 static int fio_netio_setup_listen(struct thread_data *td, unsigned short port)
175 struct sockaddr_in addr;
178 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
180 td_verror(td, errno, "socket");
185 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
186 td_verror(td, errno, "setsockopt");
190 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
191 td_verror(td, errno, "setsockopt");
196 memset(&addr, 0, sizeof(addr));
197 addr.sin_family = AF_INET;
198 addr.sin_addr.s_addr = htonl(INADDR_ANY);
199 addr.sin_port = htons(port);
201 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
202 td_verror(td, errno, "bind");
205 if (listen(fd, 1) < 0) {
206 td_verror(td, errno, "listen");
210 return fio_netio_accept_connections(td, fd, &addr);
213 static int fio_netio_setup(struct thread_data *td)
215 char host[64], buf[128];
221 if (!td->total_file_size) {
222 log_err("fio: need size= set\n");
227 log_err("fio: network connections must be read OR write\n");
231 strcpy(buf, td->filename);
233 sep = strchr(buf, ':');
235 log_err("fio: bad network host:port <<%s>>\n", td->filename);
246 ret = fio_netio_setup_listen(td, port);
249 ret = fio_netio_setup_connect(td, host, port);
255 td->io_size = td->total_file_size;
256 td->total_io_size = td->io_size;
258 for_each_file(td, f, i) {
259 f->file_size = td->total_file_size / td->nr_files;
260 f->real_file_size = f->file_size;
263 td->nr_open_files = td->nr_files;
267 static struct ioengine_ops ioengine = {
269 .version = FIO_IOOPS_VERSION,
270 .prep = fio_netio_prep,
271 .queue = fio_netio_queue,
272 .setup = fio_netio_setup,
273 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_SELFOPEN,
276 static void fio_init fio_netio_register(void)
278 register_ioengine(&ioengine);
281 static void fio_exit fio_netio_unregister(void)
283 unregister_ioengine(&ioengine);