libaio: Add hint on libaio engine init failing
[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);
256 if (ret < 0) {
257 if (errno == EINTR)
258 continue;
259
e1161c32 260 td_verror(td, errno, "poll");
5fdd124a
JA
261 break;
262 } else if (!ret)
263 continue;
264
0c09442b
JA
265 /*
266 * should be impossible
267 */
268 if (!(pfd.revents & POLLIN))
269 continue;
270
b5af8293
JA
271 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
272 if (f->fd < 0) {
273 td_verror(td, errno, "accept");
274 return 1;
275 }
276 break;
277 }
5fdd124a 278
b5af8293
JA
279 return 0;
280}
281
b5af8293
JA
282static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
283{
284 if (td_read(td))
285 return fio_netio_accept(td, f);
286 else
287 return fio_netio_connect(td, f);
288}
289
290static int fio_netio_setup_connect(struct thread_data *td, const char *host,
291 unsigned short port)
292{
293 struct netio_data *nd = td->io_ops->data;
294
295 nd->addr.sin_family = AF_INET;
296 nd->addr.sin_port = htons(port);
297
298 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
299 struct hostent *hent;
300
301 hent = gethostbyname(host);
302 if (!hent) {
303 td_verror(td, errno, "gethostbyname");
304 return 1;
5fdd124a 305 }
b5af8293
JA
306
307 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
5fdd124a
JA
308 }
309
310 return 0;
311}
312
b5af8293 313static int fio_netio_setup_listen(struct thread_data *td, short port)
ed92ac0c 314{
b5af8293 315 struct netio_data *nd = td->io_ops->data;
5fdd124a 316 int fd, opt;
ed92ac0c 317
6bedbfaf 318 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
ed92ac0c 319 if (fd < 0) {
e1161c32 320 td_verror(td, errno, "socket");
ed92ac0c
JA
321 return 1;
322 }
323
324 opt = 1;
325 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
e1161c32 326 td_verror(td, errno, "setsockopt");
ed92ac0c
JA
327 return 1;
328 }
6bedbfaf
JA
329#ifdef SO_REUSEPORT
330 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
e1161c32 331 td_verror(td, errno, "setsockopt");
6bedbfaf
JA
332 return 1;
333 }
334#endif
ed92ac0c 335
b5af8293
JA
336 nd->addr.sin_family = AF_INET;
337 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
338 nd->addr.sin_port = htons(port);
ed92ac0c 339
b5af8293 340 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
e1161c32 341 td_verror(td, errno, "bind");
ed92ac0c
JA
342 return 1;
343 }
344 if (listen(fd, 1) < 0) {
e1161c32 345 td_verror(td, errno, "listen");
ed92ac0c
JA
346 return 1;
347 }
348
b5af8293
JA
349 nd->listenfd = fd;
350 return 0;
ed92ac0c
JA
351}
352
9bec88e1 353static int fio_netio_init(struct thread_data *td)
ed92ac0c 354{
b5af8293 355 struct netio_data *nd = td->io_ops->data;
e01547d2 356 unsigned short port;
b5af8293 357 char host[64], buf[128];
ed92ac0c 358 char *sep;
af52b345 359 int ret;
ed92ac0c 360
413dd459 361 if (td_rw(td)) {
ed92ac0c
JA
362 log_err("fio: network connections must be read OR write\n");
363 return 1;
364 }
16d55aae
JA
365 if (td_random(td)) {
366 log_err("fio: network IO can't be random\n");
367 return 1;
368 }
ed92ac0c 369
2dc1bbeb 370 strcpy(buf, td->o.filename);
ed92ac0c 371
9f9214f2 372 sep = strchr(buf, '/');
ed92ac0c 373 if (!sep) {
2dc1bbeb 374 log_err("fio: bad network host/port <<%s>>\n", td->o.filename);
ed92ac0c
JA
375 return 1;
376 }
377
378 *sep = '\0';
379 sep++;
380 strcpy(host, buf);
e01547d2 381 port = atoi(sep);
ed92ac0c 382
413dd459 383 if (td_read(td)) {
b5af8293 384 nd->send_to_net = 0;
ed92ac0c
JA
385 ret = fio_netio_setup_listen(td, port);
386 } else {
b5af8293 387 nd->send_to_net = 1;
ed92ac0c
JA
388 ret = fio_netio_setup_connect(td, host, port);
389 }
390
7bb48f84 391 return ret;
ed92ac0c
JA
392}
393
b5af8293 394static void fio_netio_cleanup(struct thread_data *td)
9bec88e1 395{
b5af8293
JA
396 struct netio_data *nd = td->io_ops->data;
397
398 if (nd) {
64b24cd8
JA
399 if (nd->listenfd != -1)
400 close(nd->listenfd);
401 if (nd->pipes[0] != -1)
402 close(nd->pipes[0]);
403 if (nd->pipes[1] != -1)
404 close(nd->pipes[1]);
405
b5af8293
JA
406 free(nd);
407 td->io_ops->data = NULL;
408 }
409}
410
411static int fio_netio_setup(struct thread_data *td)
412{
7bb48f84 413 struct netio_data *nd;
7bb48f84
JA
414
415 if (!td->io_ops->data) {
416 nd = malloc(sizeof(*nd));;
417
418 memset(nd, 0, sizeof(*nd));
419 nd->listenfd = -1;
64b24cd8 420 nd->pipes[0] = nd->pipes[1] = -1;
7bb48f84 421 td->io_ops->data = nd;
7bb48f84 422 }
b5af8293 423
9bec88e1
JA
424 return 0;
425}
426
9cce02e8
JA
427static int fio_netio_setup_splice(struct thread_data *td)
428{
429 struct netio_data *nd;
430
431 fio_netio_setup(td);
432
433 nd = td->io_ops->data;
434 if (nd) {
435 if (pipe(nd->pipes) < 0)
436 return 1;
437
438 nd->use_splice = 1;
439 return 0;
440 }
441
442 return 1;
443}
444
445static struct ioengine_ops ioengine_rw = {
ed92ac0c
JA
446 .name = "net",
447 .version = FIO_IOOPS_VERSION,
ed92ac0c
JA
448 .prep = fio_netio_prep,
449 .queue = fio_netio_queue,
ed92ac0c 450 .setup = fio_netio_setup,
9bec88e1 451 .init = fio_netio_init,
b5af8293
JA
452 .cleanup = fio_netio_cleanup,
453 .open_file = fio_netio_open_file,
454 .close_file = generic_close_file,
ad830ed7
JA
455 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
456 FIO_SIGQUIT,
ed92ac0c
JA
457};
458
9cce02e8
JA
459static struct ioengine_ops ioengine_splice = {
460 .name = "netsplice",
461 .version = FIO_IOOPS_VERSION,
462 .prep = fio_netio_prep,
463 .queue = fio_netio_queue,
464 .setup = fio_netio_setup_splice,
465 .init = fio_netio_init,
466 .cleanup = fio_netio_cleanup,
467 .open_file = fio_netio_open_file,
468 .close_file = generic_close_file,
ad830ed7
JA
469 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
470 FIO_SIGQUIT,
9cce02e8
JA
471};
472
ed92ac0c
JA
473static void fio_init fio_netio_register(void)
474{
9cce02e8
JA
475 register_ioengine(&ioengine_rw);
476 register_ioengine(&ioengine_splice);
ed92ac0c
JA
477}
478
479static void fio_exit fio_netio_unregister(void)
480{
9cce02e8
JA
481 unregister_ioengine(&ioengine_rw);
482 unregister_ioengine(&ioengine_splice);
ed92ac0c 483}