Jobs must always open their own files
[fio.git] / engines / net.c
CommitLineData
ed92ac0c 1/*
da751ca9
JA
2 * net engine
3 *
4 * IO engine that reads/writes to/from sockets.
5 *
ed92ac0c
JA
6 */
7#include <stdio.h>
8#include <stdlib.h>
9#include <unistd.h>
10#include <errno.h>
11#include <assert.h>
12#include <netinet/in.h>
13#include <arpa/inet.h>
14#include <netdb.h>
5fdd124a 15#include <sys/poll.h>
ed92ac0c
JA
16
17#include "../fio.h"
ed92ac0c 18
b5af8293
JA
19struct netio_data {
20 int listenfd;
21 int send_to_net;
9cce02e8
JA
22 int use_splice;
23 int pipes[2];
b5af8293
JA
24 char host[64];
25 struct sockaddr_in addr;
26};
ed92ac0c
JA
27
28static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
29{
b5af8293 30 struct netio_data *nd = td->io_ops->data;
ed92ac0c 31
7a6499da
JA
32 /*
33 * Make sure we don't see spurious reads to a receiver, and vice versa
34 */
b5af8293
JA
35 if ((nd->send_to_net && io_u->ddir == DDIR_READ) ||
36 (!nd->send_to_net && io_u->ddir == DDIR_WRITE)) {
e1161c32 37 td_verror(td, EINVAL, "bad direction");
7a6499da 38 return 1;
ed92ac0c 39 }
7a6499da 40
f85ac25a 41 return 0;
ed92ac0c
JA
42}
43
cd963e18 44static int splice_io_u(int fdin, int fdout, unsigned int len)
ed92ac0c 45{
9cce02e8 46 int bytes = 0;
7a6499da 47
9cce02e8 48 while (len) {
cd963e18 49 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
9cce02e8
JA
50
51 if (ret < 0) {
52 if (!bytes)
53 bytes = ret;
54
55 break;
56 } else if (!ret)
57 break;
58
59 bytes += ret;
f657a2fb 60 len -= ret;
9cce02e8
JA
61 }
62
63 return bytes;
64}
65
66/*
cd963e18 67 * Receive bytes from a socket and fill them into the internal pipe
9cce02e8 68 */
cd963e18 69static int splice_in(struct thread_data *td, struct io_u *io_u)
9cce02e8
JA
70{
71 struct netio_data *nd = td->io_ops->data;
9cce02e8 72
cd963e18 73 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
9cce02e8
JA
74}
75
76/*
cd963e18 77 * Transmit 'len' bytes from the internal pipe
9cce02e8 78 */
cd963e18
JA
79static int splice_out(struct thread_data *td, struct io_u *io_u,
80 unsigned int len)
9cce02e8
JA
81{
82 struct netio_data *nd = td->io_ops->data;
cd963e18
JA
83
84 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
85}
86
87static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
88{
9cce02e8
JA
89 struct iovec iov = {
90 .iov_base = io_u->xfer_buf,
91 .iov_len = len,
92 };
93 int bytes = 0;
94
95 while (iov.iov_len) {
cd963e18 96 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
9cce02e8
JA
97
98 if (ret < 0) {
99 if (!bytes)
100 bytes = ret;
101 break;
102 } else if (!ret)
103 break;
104
105 iov.iov_len -= ret;
cd963e18 106 iov.iov_base += ret;
f657a2fb 107 bytes += ret;
9cce02e8
JA
108 }
109
110 return bytes;
cd963e18 111
9cce02e8
JA
112}
113
114/*
cd963e18 115 * vmsplice() pipe to io_u buffer
9cce02e8 116 */
cd963e18
JA
117static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
118 unsigned int len)
9cce02e8
JA
119{
120 struct netio_data *nd = td->io_ops->data;
9cce02e8 121
cd963e18
JA
122 return vmsplice_io_u(io_u, nd->pipes[0], len);
123}
9cce02e8 124
cd963e18
JA
125/*
126 * vmsplice() io_u to pipe
127 */
128static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
129{
130 struct netio_data *nd = td->io_ops->data;
ed92ac0c 131
cd963e18 132 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
9cce02e8
JA
133}
134
cd963e18
JA
135/*
136 * splice receive - transfer socket data into a pipe using splice, then map
137 * that pipe data into the io_u using vmsplice.
138 */
9cce02e8
JA
139static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
140{
141 int ret;
142
143 ret = splice_in(td, io_u);
cd963e18
JA
144 if (ret > 0)
145 return vmsplice_io_u_out(td, io_u, ret);
9cce02e8 146
cd963e18 147 return ret;
9cce02e8
JA
148}
149
cd963e18
JA
150/*
151 * splice transmit - map data from the io_u into a pipe by using vmsplice,
152 * then transfer that pipe to a socket using splice.
153 */
9cce02e8
JA
154static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
155{
156 int ret;
157
158 ret = vmsplice_io_u_in(td, io_u);
cd963e18
JA
159 if (ret > 0)
160 return splice_out(td, io_u, ret);
9cce02e8 161
cd963e18 162 return ret;
9cce02e8
JA
163}
164
165static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
166{
167 int flags = 0;
168
169 /*
170 * if we are going to write more, set MSG_MORE
171 */
172 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen < td->o.size)
173 flags = MSG_MORE;
174
175 return send(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
176}
177
178static int fio_netio_recv(struct io_u *io_u)
179{
180 int flags = MSG_WAITALL;
181
182 return recv(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
183}
184
185static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
186{
187 struct netio_data *nd = td->io_ops->data;
188 int ret;
189
7101d9c2
JA
190 fio_ro_check(td, io_u);
191
9cce02e8
JA
192 if (io_u->ddir == DDIR_WRITE) {
193 if (nd->use_splice)
194 ret = fio_netio_splice_out(td, io_u);
195 else
196 ret = fio_netio_send(td, io_u);
d4f12dd0 197 } else if (io_u->ddir == DDIR_READ) {
9cce02e8
JA
198 if (nd->use_splice)
199 ret = fio_netio_splice_in(td, io_u);
200 else
201 ret = fio_netio_recv(io_u);
d4f12dd0 202 } else
7a6499da 203 ret = 0; /* must be a SYNC */
ed92ac0c 204
cec6b55d 205 if (ret != (int) io_u->xfer_buflen) {
22819ec2 206 if (ret >= 0) {
cec6b55d
JA
207 io_u->resid = io_u->xfer_buflen - ret;
208 io_u->error = 0;
36167d82 209 return FIO_Q_COMPLETED;
ed92ac0c
JA
210 } else
211 io_u->error = errno;
212 }
213
36167d82 214 if (io_u->error)
e1161c32 215 td_verror(td, io_u->error, "xfer");
ed92ac0c 216
36167d82 217 return FIO_Q_COMPLETED;
ed92ac0c
JA
218}
219
b5af8293 220static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
ed92ac0c 221{
b5af8293 222 struct netio_data *nd = td->io_ops->data;
ed92ac0c 223
b5af8293
JA
224 f->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
225 if (f->fd < 0) {
226 td_verror(td, errno, "socket");
227 return 1;
ed92ac0c
JA
228 }
229
b5af8293
JA
230 if (connect(f->fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
231 td_verror(td, errno, "connect");
232 return 1;
ed92ac0c
JA
233 }
234
235 return 0;
ed92ac0c
JA
236}
237
b5af8293 238static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
5fdd124a 239{
b5af8293
JA
240 struct netio_data *nd = td->io_ops->data;
241 socklen_t socklen = sizeof(nd->addr);
5fdd124a 242 struct pollfd pfd;
b5af8293 243 int ret;
5fdd124a 244
6d86144d 245 log_info("fio: waiting for connection\n");
5fdd124a
JA
246
247 /*
248 * Accept loop. poll for incoming events, accept them. Repeat until we
249 * have all connections.
250 */
b5af8293
JA
251 while (!td->terminate) {
252 pfd.fd = nd->listenfd;
5fdd124a
JA
253 pfd.events = POLLIN;
254
255 ret = poll(&pfd, 1, -1);
ad830ed7 256 printf("got ret %d\n", ret);
5fdd124a
JA
257 if (ret < 0) {
258 if (errno == EINTR)
259 continue;
260
e1161c32 261 td_verror(td, errno, "poll");
5fdd124a
JA
262 break;
263 } else if (!ret)
264 continue;
265
0c09442b
JA
266 /*
267 * should be impossible
268 */
269 if (!(pfd.revents & POLLIN))
270 continue;
271
b5af8293
JA
272 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
273 if (f->fd < 0) {
274 td_verror(td, errno, "accept");
275 return 1;
276 }
277 break;
278 }
5fdd124a 279
b5af8293
JA
280 return 0;
281}
282
b5af8293
JA
283static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
284{
285 if (td_read(td))
286 return fio_netio_accept(td, f);
287 else
288 return fio_netio_connect(td, f);
289}
290
291static int fio_netio_setup_connect(struct thread_data *td, const char *host,
292 unsigned short port)
293{
294 struct netio_data *nd = td->io_ops->data;
295
296 nd->addr.sin_family = AF_INET;
297 nd->addr.sin_port = htons(port);
298
299 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
300 struct hostent *hent;
301
302 hent = gethostbyname(host);
303 if (!hent) {
304 td_verror(td, errno, "gethostbyname");
305 return 1;
5fdd124a 306 }
b5af8293
JA
307
308 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
5fdd124a
JA
309 }
310
311 return 0;
312}
313
b5af8293 314static int fio_netio_setup_listen(struct thread_data *td, short port)
ed92ac0c 315{
b5af8293 316 struct netio_data *nd = td->io_ops->data;
5fdd124a 317 int fd, opt;
ed92ac0c 318
6bedbfaf 319 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
ed92ac0c 320 if (fd < 0) {
e1161c32 321 td_verror(td, errno, "socket");
ed92ac0c
JA
322 return 1;
323 }
324
325 opt = 1;
326 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
e1161c32 327 td_verror(td, errno, "setsockopt");
ed92ac0c
JA
328 return 1;
329 }
6bedbfaf
JA
330#ifdef SO_REUSEPORT
331 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
e1161c32 332 td_verror(td, errno, "setsockopt");
6bedbfaf
JA
333 return 1;
334 }
335#endif
ed92ac0c 336
b5af8293
JA
337 nd->addr.sin_family = AF_INET;
338 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
339 nd->addr.sin_port = htons(port);
ed92ac0c 340
b5af8293 341 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
e1161c32 342 td_verror(td, errno, "bind");
ed92ac0c
JA
343 return 1;
344 }
345 if (listen(fd, 1) < 0) {
e1161c32 346 td_verror(td, errno, "listen");
ed92ac0c
JA
347 return 1;
348 }
349
b5af8293
JA
350 nd->listenfd = fd;
351 return 0;
ed92ac0c
JA
352}
353
9bec88e1 354static int fio_netio_init(struct thread_data *td)
ed92ac0c 355{
b5af8293 356 struct netio_data *nd = td->io_ops->data;
e01547d2 357 unsigned short port;
b5af8293 358 char host[64], buf[128];
ed92ac0c 359 char *sep;
af52b345 360 int ret;
ed92ac0c 361
413dd459 362 if (td_rw(td)) {
ed92ac0c
JA
363 log_err("fio: network connections must be read OR write\n");
364 return 1;
365 }
16d55aae
JA
366 if (td_random(td)) {
367 log_err("fio: network IO can't be random\n");
368 return 1;
369 }
ed92ac0c 370
2dc1bbeb 371 strcpy(buf, td->o.filename);
ed92ac0c 372
9f9214f2 373 sep = strchr(buf, '/');
ed92ac0c 374 if (!sep) {
2dc1bbeb 375 log_err("fio: bad network host/port <<%s>>\n", td->o.filename);
ed92ac0c
JA
376 return 1;
377 }
378
379 *sep = '\0';
380 sep++;
381 strcpy(host, buf);
e01547d2 382 port = atoi(sep);
ed92ac0c 383
413dd459 384 if (td_read(td)) {
b5af8293 385 nd->send_to_net = 0;
ed92ac0c
JA
386 ret = fio_netio_setup_listen(td, port);
387 } else {
b5af8293 388 nd->send_to_net = 1;
ed92ac0c
JA
389 ret = fio_netio_setup_connect(td, host, port);
390 }
391
7bb48f84 392 return ret;
ed92ac0c
JA
393}
394
b5af8293 395static void fio_netio_cleanup(struct thread_data *td)
9bec88e1 396{
b5af8293
JA
397 struct netio_data *nd = td->io_ops->data;
398
399 if (nd) {
64b24cd8
JA
400 if (nd->listenfd != -1)
401 close(nd->listenfd);
402 if (nd->pipes[0] != -1)
403 close(nd->pipes[0]);
404 if (nd->pipes[1] != -1)
405 close(nd->pipes[1]);
406
b5af8293
JA
407 free(nd);
408 td->io_ops->data = NULL;
409 }
410}
411
412static int fio_netio_setup(struct thread_data *td)
413{
7bb48f84 414 struct netio_data *nd;
7bb48f84
JA
415
416 if (!td->io_ops->data) {
417 nd = malloc(sizeof(*nd));;
418
419 memset(nd, 0, sizeof(*nd));
420 nd->listenfd = -1;
64b24cd8 421 nd->pipes[0] = nd->pipes[1] = -1;
7bb48f84 422 td->io_ops->data = nd;
7bb48f84 423 }
b5af8293 424
9bec88e1
JA
425 return 0;
426}
427
9cce02e8
JA
428static int fio_netio_setup_splice(struct thread_data *td)
429{
430 struct netio_data *nd;
431
432 fio_netio_setup(td);
433
434 nd = td->io_ops->data;
435 if (nd) {
436 if (pipe(nd->pipes) < 0)
437 return 1;
438
439 nd->use_splice = 1;
440 return 0;
441 }
442
443 return 1;
444}
445
446static struct ioengine_ops ioengine_rw = {
ed92ac0c
JA
447 .name = "net",
448 .version = FIO_IOOPS_VERSION,
ed92ac0c
JA
449 .prep = fio_netio_prep,
450 .queue = fio_netio_queue,
ed92ac0c 451 .setup = fio_netio_setup,
9bec88e1 452 .init = fio_netio_init,
b5af8293
JA
453 .cleanup = fio_netio_cleanup,
454 .open_file = fio_netio_open_file,
455 .close_file = generic_close_file,
ad830ed7
JA
456 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
457 FIO_SIGQUIT,
ed92ac0c
JA
458};
459
9cce02e8
JA
460static struct ioengine_ops ioengine_splice = {
461 .name = "netsplice",
462 .version = FIO_IOOPS_VERSION,
463 .prep = fio_netio_prep,
464 .queue = fio_netio_queue,
465 .setup = fio_netio_setup_splice,
466 .init = fio_netio_init,
467 .cleanup = fio_netio_cleanup,
468 .open_file = fio_netio_open_file,
469 .close_file = generic_close_file,
ad830ed7
JA
470 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
471 FIO_SIGQUIT,
9cce02e8
JA
472};
473
ed92ac0c
JA
474static void fio_init fio_netio_register(void)
475{
9cce02e8
JA
476 register_ioengine(&ioengine_rw);
477 register_ioengine(&ioengine_splice);
ed92ac0c
JA
478}
479
480static void fio_exit fio_netio_unregister(void)
481{
9cce02e8
JA
482 unregister_ioengine(&ioengine_rw);
483 unregister_ioengine(&ioengine_splice);
ed92ac0c 484}