Merge branch 'master' into gfio
[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>
842805f5 10#include <signal.h>
ed92ac0c
JA
11#include <errno.h>
12#include <assert.h>
13#include <netinet/in.h>
70a7878c 14#include <netinet/tcp.h>
ed92ac0c
JA
15#include <arpa/inet.h>
16#include <netdb.h>
5fdd124a 17#include <sys/poll.h>
7292056a 18#include <sys/types.h>
0fd666bf 19#include <sys/stat.h>
7292056a 20#include <sys/socket.h>
0fd666bf 21#include <sys/un.h>
ed92ac0c
JA
22
23#include "../fio.h"
ed92ac0c 24
b5af8293
JA
25struct netio_data {
26 int listenfd;
9cce02e8
JA
27 int use_splice;
28 int pipes[2];
b5af8293 29 struct sockaddr_in addr;
0fd666bf 30 struct sockaddr_un addr_un;
b5af8293 31};
ed92ac0c 32
de890a1e
SL
33struct netio_options {
34 struct thread_data *td;
35 unsigned int port;
36 unsigned int proto;
37 unsigned int listen;
6f73a7f8 38 unsigned int pingpong;
70a7878c 39 unsigned int nodelay;
de890a1e
SL
40};
41
664fb3bd
JA
42struct udp_close_msg {
43 uint32_t magic;
44 uint32_t cmd;
45};
46
47enum {
48 FIO_LINK_CLOSE = 0x89,
b96d2430
JA
49 FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b,
50 FIO_LINK_OPEN = 0x98,
0fd666bf
JA
51
52 FIO_TYPE_TCP = 1,
53 FIO_TYPE_UDP = 2,
54 FIO_TYPE_UNIX = 3,
664fb3bd
JA
55};
56
de890a1e
SL
57static int str_hostname_cb(void *data, const char *input);
58static struct fio_option options[] = {
59 {
60 .name = "hostname",
e8b0e958 61 .lname = "net engine hostname",
de890a1e
SL
62 .type = FIO_OPT_STR_STORE,
63 .cb = str_hostname_cb,
64 .help = "Hostname for net IO engine",
e8b0e958 65 .category = FIO_OPT_C_IO,
de890a1e
SL
66 },
67 {
68 .name = "port",
e8b0e958 69 .lname = "net engine port",
de890a1e
SL
70 .type = FIO_OPT_INT,
71 .off1 = offsetof(struct netio_options, port),
72 .minval = 1,
73 .maxval = 65535,
74 .help = "Port to use for TCP or UDP net connections",
e8b0e958 75 .category = FIO_OPT_C_IO,
de890a1e
SL
76 },
77 {
78 .name = "protocol",
e8b0e958 79 .lname = "net engine protocol",
de890a1e
SL
80 .alias = "proto",
81 .type = FIO_OPT_STR,
82 .off1 = offsetof(struct netio_options, proto),
83 .help = "Network protocol to use",
84 .def = "tcp",
e8b0e958 85 .category = FIO_OPT_C_IO,
de890a1e
SL
86 .posval = {
87 { .ival = "tcp",
88 .oval = FIO_TYPE_TCP,
89 .help = "Transmission Control Protocol",
90 },
91 { .ival = "udp",
92 .oval = FIO_TYPE_UDP,
f5cc3d0e 93 .help = "User Datagram Protocol",
de890a1e
SL
94 },
95 { .ival = "unix",
96 .oval = FIO_TYPE_UNIX,
97 .help = "UNIX domain socket",
98 },
99 },
100 },
1eafa37a 101#ifdef CONFIG_TCP_NODELAY
70a7878c
SN
102 {
103 .name = "nodelay",
104 .type = FIO_OPT_BOOL,
105 .off1 = offsetof(struct netio_options, nodelay),
106 .help = "Use TCP_NODELAY on TCP connections",
107 },
1eafa37a 108#endif
de890a1e
SL
109 {
110 .name = "listen",
e8b0e958 111 .lname = "net engine listen",
de890a1e
SL
112 .type = FIO_OPT_STR_SET,
113 .off1 = offsetof(struct netio_options, listen),
114 .help = "Listen for incoming TCP connections",
e8b0e958 115 .category = FIO_OPT_C_IO,
de890a1e 116 },
6f73a7f8
JA
117 {
118 .name = "pingpong",
119 .type = FIO_OPT_STR_SET,
120 .off1 = offsetof(struct netio_options, pingpong),
121 .help = "Ping-pong IO requests",
122 },
de890a1e
SL
123 {
124 .name = NULL,
125 },
126};
127
371d456c
JA
128/*
129 * Return -1 for error and 'nr events' for a positive number
130 * of events
131 */
132static int poll_wait(struct thread_data *td, int fd, short events)
133{
134 struct pollfd pfd;
135 int ret;
136
137 while (!td->terminate) {
138 pfd.fd = fd;
139 pfd.events = events;
140 ret = poll(&pfd, 1, -1);
141 if (ret < 0) {
142 if (errno == EINTR)
d5b388a5 143 break;
371d456c
JA
144
145 td_verror(td, errno, "poll");
146 return -1;
147 } else if (!ret)
148 continue;
149
150 break;
151 }
152
153 if (pfd.revents & events)
154 return 1;
371d456c
JA
155
156 return -1;
157}
158
ed92ac0c
JA
159static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
160{
de890a1e 161 struct netio_options *o = td->eo;
ed92ac0c 162
7a6499da
JA
163 /*
164 * Make sure we don't see spurious reads to a receiver, and vice versa
165 */
de890a1e
SL
166 if (o->proto == FIO_TYPE_TCP)
167 return 0;
168
169 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
170 (!o->listen && io_u->ddir == DDIR_READ)) {
e1161c32 171 td_verror(td, EINVAL, "bad direction");
7a6499da 172 return 1;
ed92ac0c 173 }
3f457bea 174
f85ac25a 175 return 0;
ed92ac0c
JA
176}
177
67bf9823 178#ifdef CONFIG_LINUX_SPLICE
cd963e18 179static int splice_io_u(int fdin, int fdout, unsigned int len)
ed92ac0c 180{
9cce02e8 181 int bytes = 0;
7a6499da 182
9cce02e8 183 while (len) {
cd963e18 184 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
9cce02e8
JA
185
186 if (ret < 0) {
187 if (!bytes)
188 bytes = ret;
189
190 break;
191 } else if (!ret)
192 break;
193
194 bytes += ret;
f657a2fb 195 len -= ret;
9cce02e8
JA
196 }
197
198 return bytes;
199}
200
201/*
cd963e18 202 * Receive bytes from a socket and fill them into the internal pipe
9cce02e8 203 */
cd963e18 204static int splice_in(struct thread_data *td, struct io_u *io_u)
9cce02e8
JA
205{
206 struct netio_data *nd = td->io_ops->data;
9cce02e8 207
cd963e18 208 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
9cce02e8
JA
209}
210
211/*
cd963e18 212 * Transmit 'len' bytes from the internal pipe
9cce02e8 213 */
cd963e18
JA
214static int splice_out(struct thread_data *td, struct io_u *io_u,
215 unsigned int len)
9cce02e8
JA
216{
217 struct netio_data *nd = td->io_ops->data;
cd963e18
JA
218
219 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
220}
221
222static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
223{
9cce02e8
JA
224 struct iovec iov = {
225 .iov_base = io_u->xfer_buf,
226 .iov_len = len,
227 };
228 int bytes = 0;
229
230 while (iov.iov_len) {
cd963e18 231 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
9cce02e8
JA
232
233 if (ret < 0) {
234 if (!bytes)
235 bytes = ret;
236 break;
237 } else if (!ret)
238 break;
239
240 iov.iov_len -= ret;
cd963e18 241 iov.iov_base += ret;
f657a2fb 242 bytes += ret;
9cce02e8
JA
243 }
244
245 return bytes;
cd963e18 246
9cce02e8
JA
247}
248
249/*
cd963e18 250 * vmsplice() pipe to io_u buffer
9cce02e8 251 */
cd963e18
JA
252static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
253 unsigned int len)
9cce02e8
JA
254{
255 struct netio_data *nd = td->io_ops->data;
9cce02e8 256
cd963e18
JA
257 return vmsplice_io_u(io_u, nd->pipes[0], len);
258}
9cce02e8 259
cd963e18
JA
260/*
261 * vmsplice() io_u to pipe
262 */
263static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
264{
265 struct netio_data *nd = td->io_ops->data;
ed92ac0c 266
cd963e18 267 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
9cce02e8
JA
268}
269
cd963e18
JA
270/*
271 * splice receive - transfer socket data into a pipe using splice, then map
272 * that pipe data into the io_u using vmsplice.
273 */
9cce02e8
JA
274static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
275{
276 int ret;
277
278 ret = splice_in(td, io_u);
cd963e18
JA
279 if (ret > 0)
280 return vmsplice_io_u_out(td, io_u, ret);
9cce02e8 281
cd963e18 282 return ret;
9cce02e8
JA
283}
284
cd963e18
JA
285/*
286 * splice transmit - map data from the io_u into a pipe by using vmsplice,
287 * then transfer that pipe to a socket using splice.
288 */
9cce02e8
JA
289static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
290{
291 int ret;
292
293 ret = vmsplice_io_u_in(td, io_u);
cd963e18
JA
294 if (ret > 0)
295 return splice_out(td, io_u, ret);
9cce02e8 296
cd963e18 297 return ret;
9cce02e8 298}
5921e80c
JA
299#else
300static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
301{
af8771b9 302 errno = EOPNOTSUPP;
5921e80c
JA
303 return -1;
304}
305
306static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
307{
af8771b9 308 errno = EOPNOTSUPP;
5921e80c
JA
309 return -1;
310}
311#endif
9cce02e8
JA
312
313static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
314{
414c2a3e 315 struct netio_data *nd = td->io_ops->data;
de890a1e 316 struct netio_options *o = td->eo;
6f73a7f8 317 int ret, flags = 0;
371d456c 318
664fb3bd 319 do {
de890a1e 320 if (o->proto == FIO_TYPE_UDP) {
62b38926
JA
321 struct sockaddr *to = (struct sockaddr *) &nd->addr;
322
664fb3bd 323 ret = sendto(io_u->file->fd, io_u->xfer_buf,
62b38926
JA
324 io_u->xfer_buflen, flags, to,
325 sizeof(*to));
664fb3bd
JA
326 } else {
327 /*
328 * if we are going to write more, set MSG_MORE
329 */
5921e80c 330#ifdef MSG_MORE
6f73a7f8
JA
331 if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
332 td->o.size) && !o->pingpong)
664fb3bd 333 flags |= MSG_MORE;
5921e80c 334#endif
664fb3bd
JA
335 ret = send(io_u->file->fd, io_u->xfer_buf,
336 io_u->xfer_buflen, flags);
337 }
338 if (ret > 0)
339 break;
9cce02e8 340
664fb3bd
JA
341 ret = poll_wait(td, io_u->file->fd, POLLOUT);
342 if (ret <= 0)
343 break;
664fb3bd
JA
344 } while (1);
345
346 return ret;
347}
348
349static int is_udp_close(struct io_u *io_u, int len)
350{
351 struct udp_close_msg *msg;
352
353 if (len != sizeof(struct udp_close_msg))
354 return 0;
355
356 msg = io_u->xfer_buf;
b96d2430 357 if (ntohl(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC)
664fb3bd
JA
358 return 0;
359 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
360 return 0;
361
362 return 1;
9cce02e8
JA
363}
364
414c2a3e 365static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
9cce02e8 366{
414c2a3e 367 struct netio_data *nd = td->io_ops->data;
de890a1e 368 struct netio_options *o = td->eo;
6f73a7f8 369 int ret, flags = 0;
664fb3bd
JA
370
371 do {
de890a1e 372 if (o->proto == FIO_TYPE_UDP) {
67bf9823 373 socklen_t len = sizeof(nd->addr);
62b38926 374 struct sockaddr *from = (struct sockaddr *) &nd->addr;
664fb3bd
JA
375
376 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
62b38926 377 io_u->xfer_buflen, flags, from, &len);
664fb3bd
JA
378 if (is_udp_close(io_u, ret)) {
379 td->done = 1;
380 return 0;
381 }
382 } else {
383 ret = recv(io_u->file->fd, io_u->xfer_buf,
384 io_u->xfer_buflen, flags);
385 }
386 if (ret > 0)
387 break;
7d988f68
JA
388 else if (!ret && (flags & MSG_WAITALL))
389 break;
9cce02e8 390
664fb3bd
JA
391 ret = poll_wait(td, io_u->file->fd, POLLIN);
392 if (ret <= 0)
393 break;
664fb3bd
JA
394 flags |= MSG_WAITALL;
395 } while (1);
414c2a3e 396
664fb3bd 397 return ret;
9cce02e8
JA
398}
399
6f73a7f8
JA
400static int __fio_netio_queue(struct thread_data *td, struct io_u *io_u,
401 enum fio_ddir ddir)
9cce02e8
JA
402{
403 struct netio_data *nd = td->io_ops->data;
de890a1e 404 struct netio_options *o = td->eo;
9cce02e8
JA
405 int ret;
406
6f73a7f8 407 if (ddir == DDIR_WRITE) {
de890a1e
SL
408 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
409 o->proto == FIO_TYPE_UNIX)
9cce02e8 410 ret = fio_netio_send(td, io_u);
414c2a3e
JA
411 else
412 ret = fio_netio_splice_out(td, io_u);
6f73a7f8 413 } else if (ddir == DDIR_READ) {
de890a1e
SL
414 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
415 o->proto == FIO_TYPE_UNIX)
414c2a3e 416 ret = fio_netio_recv(td, io_u);
9cce02e8 417 else
414c2a3e 418 ret = fio_netio_splice_in(td, io_u);
d4f12dd0 419 } else
7a6499da 420 ret = 0; /* must be a SYNC */
ed92ac0c 421
cec6b55d 422 if (ret != (int) io_u->xfer_buflen) {
22819ec2 423 if (ret >= 0) {
cec6b55d
JA
424 io_u->resid = io_u->xfer_buflen - ret;
425 io_u->error = 0;
36167d82 426 return FIO_Q_COMPLETED;
414c2a3e
JA
427 } else {
428 int err = errno;
429
6f73a7f8 430 if (ddir == DDIR_WRITE && err == EMSGSIZE)
414c2a3e
JA
431 return FIO_Q_BUSY;
432
433 io_u->error = err;
434 }
ed92ac0c
JA
435 }
436
36167d82 437 if (io_u->error)
e1161c32 438 td_verror(td, io_u->error, "xfer");
ed92ac0c 439
36167d82 440 return FIO_Q_COMPLETED;
ed92ac0c
JA
441}
442
6f73a7f8
JA
443static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
444{
445 struct netio_options *o = td->eo;
446 int ret;
447
448 fio_ro_check(td, io_u);
449
450 ret = __fio_netio_queue(td, io_u, io_u->ddir);
451 if (!o->pingpong || ret != FIO_Q_COMPLETED)
452 return ret;
453
454 /*
455 * For ping-pong mode, receive or send reply as needed
456 */
457 if (td_read(td) && io_u->ddir == DDIR_READ)
458 ret = __fio_netio_queue(td, io_u, DDIR_WRITE);
459 else if (td_write(td) && io_u->ddir == DDIR_WRITE)
460 ret = __fio_netio_queue(td, io_u, DDIR_READ);
461
462 return ret;
463}
464
b5af8293 465static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
ed92ac0c 466{
b5af8293 467 struct netio_data *nd = td->io_ops->data;
de890a1e 468 struct netio_options *o = td->eo;
6264c7a8 469 int type, domain;
414c2a3e 470
de890a1e 471 if (o->proto == FIO_TYPE_TCP) {
0fd666bf 472 domain = AF_INET;
414c2a3e 473 type = SOCK_STREAM;
de890a1e 474 } else if (o->proto == FIO_TYPE_UDP) {
0fd666bf 475 domain = AF_INET;
414c2a3e 476 type = SOCK_DGRAM;
de890a1e 477 } else if (o->proto == FIO_TYPE_UNIX) {
0fd666bf
JA
478 domain = AF_UNIX;
479 type = SOCK_STREAM;
480 } else {
de890a1e 481 log_err("fio: bad network type %d\n", o->proto);
0fd666bf
JA
482 f->fd = -1;
483 return 1;
484 }
ed92ac0c 485
0fd666bf 486 f->fd = socket(domain, type, 0);
b5af8293
JA
487 if (f->fd < 0) {
488 td_verror(td, errno, "socket");
489 return 1;
ed92ac0c
JA
490 }
491
1eafa37a 492#ifdef CONFIG_TCP_NODELAY
70a7878c 493 if (o->nodelay && o->proto == FIO_TYPE_TCP) {
6264c7a8
JA
494 int optval = 1;
495
26e594a5 496 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
70a7878c
SN
497 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
498 return 1;
499 }
500 }
1eafa37a 501#endif
70a7878c 502
de890a1e 503 if (o->proto == FIO_TYPE_UDP)
414c2a3e 504 return 0;
de890a1e 505 else if (o->proto == FIO_TYPE_TCP) {
67bf9823 506 socklen_t len = sizeof(nd->addr);
414c2a3e 507
0fd666bf
JA
508 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
509 td_verror(td, errno, "connect");
b94cba47 510 close(f->fd);
0fd666bf
JA
511 return 1;
512 }
513 } else {
514 struct sockaddr_un *addr = &nd->addr_un;
67bf9823 515 socklen_t len;
0fd666bf
JA
516
517 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
518
519 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
520 td_verror(td, errno, "connect");
b94cba47 521 close(f->fd);
0fd666bf
JA
522 return 1;
523 }
ed92ac0c
JA
524 }
525
526 return 0;
ed92ac0c
JA
527}
528
b5af8293 529static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
5fdd124a 530{
b5af8293 531 struct netio_data *nd = td->io_ops->data;
de890a1e 532 struct netio_options *o = td->eo;
67bf9823 533 socklen_t socklen = sizeof(nd->addr);
6264c7a8 534 int state;
5fdd124a 535
de890a1e 536 if (o->proto == FIO_TYPE_UDP) {
414c2a3e
JA
537 f->fd = nd->listenfd;
538 return 0;
539 }
540
859088d3
JA
541 state = td->runstate;
542 td_set_runstate(td, TD_SETTING_UP);
543
6d86144d 544 log_info("fio: waiting for connection\n");
5fdd124a 545
371d456c 546 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
859088d3 547 goto err;
0c09442b 548
371d456c
JA
549 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
550 if (f->fd < 0) {
551 td_verror(td, errno, "accept");
859088d3 552 goto err;
b5af8293 553 }
5fdd124a 554
1eafa37a 555#ifdef CONFIG_TCP_NODELAY
70a7878c 556 if (o->nodelay && o->proto == FIO_TYPE_TCP) {
6264c7a8
JA
557 int optval = 1;
558
26e594a5 559 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
70a7878c
SN
560 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
561 return 1;
562 }
563 }
1eafa37a 564#endif
70a7878c 565
0cae16ff 566 reset_all_stats(td);
859088d3 567 td_set_runstate(td, state);
b5af8293 568 return 0;
859088d3
JA
569err:
570 td_set_runstate(td, state);
571 return 1;
b5af8293
JA
572}
573
664fb3bd
JA
574static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
575{
576 struct netio_data *nd = td->io_ops->data;
577 struct udp_close_msg msg;
62b38926 578 struct sockaddr *to = (struct sockaddr *) &nd->addr;
664fb3bd
JA
579 int ret;
580
b96d2430 581 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
664fb3bd
JA
582 msg.cmd = htonl(FIO_LINK_CLOSE);
583
1f81991e 584 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to,
664fb3bd
JA
585 sizeof(nd->addr));
586 if (ret < 0)
587 td_verror(td, errno, "sendto udp link close");
588}
589
590static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
591{
de890a1e 592 struct netio_options *o = td->eo;
664fb3bd
JA
593
594 /*
595 * If this is an UDP connection, notify the receiver that we are
596 * closing down the link
597 */
de890a1e 598 if (o->proto == FIO_TYPE_UDP)
664fb3bd
JA
599 fio_netio_udp_close(td, f);
600
601 return generic_close_file(td, f);
602}
603
b96d2430
JA
604static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
605{
606 struct netio_data *nd = td->io_ops->data;
607 struct udp_close_msg msg;
608 struct sockaddr *to = (struct sockaddr *) &nd->addr;
67bf9823 609 socklen_t len = sizeof(nd->addr);
b96d2430
JA
610 int ret;
611
1f81991e 612 ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
b96d2430
JA
613 if (ret < 0) {
614 td_verror(td, errno, "sendto udp link open");
615 return ret;
616 }
617
618 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
619 ntohl(msg.cmd) != FIO_LINK_OPEN) {
620 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
621 ntohl(msg.cmd));
622 return -1;
623 }
624
625 return 0;
626}
627
628static int fio_netio_udp_send_open(struct thread_data *td, struct fio_file *f)
629{
630 struct netio_data *nd = td->io_ops->data;
631 struct udp_close_msg msg;
632 struct sockaddr *to = (struct sockaddr *) &nd->addr;
633 int ret;
634
635 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
636 msg.cmd = htonl(FIO_LINK_OPEN);
637
1f81991e 638 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to,
b96d2430
JA
639 sizeof(nd->addr));
640 if (ret < 0) {
641 td_verror(td, errno, "sendto udp link open");
642 return ret;
643 }
644
645 return 0;
646}
647
648static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
649{
650 int ret;
651 struct netio_options *o = td->eo;
652
653 if (o->listen)
654 ret = fio_netio_accept(td, f);
655 else
656 ret = fio_netio_connect(td, f);
657
658 if (ret) {
659 f->fd = -1;
660 return ret;
661 }
662
663 if (o->proto == FIO_TYPE_UDP) {
664 if (td_write(td))
665 ret = fio_netio_udp_send_open(td, f);
666 else {
667 int state;
668
669 state = td->runstate;
670 td_set_runstate(td, TD_SETTING_UP);
671 ret = fio_netio_udp_recv_open(td, f);
672 td_set_runstate(td, state);
673 }
674 }
675
676 if (ret)
677 fio_netio_close_file(td, f);
678
679 return ret;
680}
681
0fd666bf
JA
682static int fio_netio_setup_connect_inet(struct thread_data *td,
683 const char *host, unsigned short port)
b5af8293
JA
684{
685 struct netio_data *nd = td->io_ops->data;
686
166dce4b
JA
687 if (!host) {
688 log_err("fio: connect with no host to connect to.\n");
689 if (td_read(td))
690 log_err("fio: did you forget to set 'listen'?\n");
691
692 td_verror(td, EINVAL, "no hostname= set");
693 return 1;
694 }
695
b5af8293
JA
696 nd->addr.sin_family = AF_INET;
697 nd->addr.sin_port = htons(port);
698
699 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
700 struct hostent *hent;
701
702 hent = gethostbyname(host);
703 if (!hent) {
704 td_verror(td, errno, "gethostbyname");
705 return 1;
5fdd124a 706 }
b5af8293
JA
707
708 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
5fdd124a
JA
709 }
710
711 return 0;
712}
713
0fd666bf
JA
714static int fio_netio_setup_connect_unix(struct thread_data *td,
715 const char *path)
716{
717 struct netio_data *nd = td->io_ops->data;
718 struct sockaddr_un *soun = &nd->addr_un;
719
720 soun->sun_family = AF_UNIX;
721 strcpy(soun->sun_path, path);
722 return 0;
723}
724
de890a1e 725static int fio_netio_setup_connect(struct thread_data *td)
0fd666bf 726{
de890a1e 727 struct netio_options *o = td->eo;
0fd666bf 728
de890a1e
SL
729 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
730 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
0fd666bf 731 else
de890a1e 732 return fio_netio_setup_connect_unix(td, td->o.filename);
0fd666bf
JA
733}
734
735static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
736{
737 struct netio_data *nd = td->io_ops->data;
738 struct sockaddr_un *addr = &nd->addr_un;
739 mode_t mode;
740 int len, fd;
741
742 fd = socket(AF_UNIX, SOCK_STREAM, 0);
743 if (fd < 0) {
744 log_err("fio: socket: %s\n", strerror(errno));
745 return -1;
746 }
747
748 mode = umask(000);
749
750 memset(addr, 0, sizeof(*addr));
751 addr->sun_family = AF_UNIX;
752 strcpy(addr->sun_path, path);
753 unlink(path);
754
755 len = sizeof(addr->sun_family) + strlen(path) + 1;
756
757 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
758 log_err("fio: bind: %s\n", strerror(errno));
b94cba47 759 close(fd);
0fd666bf
JA
760 return -1;
761 }
762
763 umask(mode);
764 nd->listenfd = fd;
765 return 0;
766}
767
768static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
ed92ac0c 769{
b5af8293 770 struct netio_data *nd = td->io_ops->data;
de890a1e 771 struct netio_options *o = td->eo;
414c2a3e 772 int fd, opt, type;
ed92ac0c 773
de890a1e 774 if (o->proto == FIO_TYPE_TCP)
414c2a3e
JA
775 type = SOCK_STREAM;
776 else
777 type = SOCK_DGRAM;
778
0fd666bf 779 fd = socket(AF_INET, type, 0);
ed92ac0c 780 if (fd < 0) {
e1161c32 781 td_verror(td, errno, "socket");
ed92ac0c
JA
782 return 1;
783 }
784
785 opt = 1;
26e594a5 786 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
e1161c32 787 td_verror(td, errno, "setsockopt");
ed92ac0c
JA
788 return 1;
789 }
6bedbfaf 790#ifdef SO_REUSEPORT
26e594a5 791 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) {
e1161c32 792 td_verror(td, errno, "setsockopt");
6bedbfaf
JA
793 return 1;
794 }
795#endif
ed92ac0c 796
b5af8293
JA
797 nd->addr.sin_family = AF_INET;
798 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
799 nd->addr.sin_port = htons(port);
ed92ac0c 800
b5af8293 801 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
e1161c32 802 td_verror(td, errno, "bind");
ed92ac0c
JA
803 return 1;
804 }
0fd666bf
JA
805
806 nd->listenfd = fd;
807 return 0;
808}
809
de890a1e 810static int fio_netio_setup_listen(struct thread_data *td)
0fd666bf
JA
811{
812 struct netio_data *nd = td->io_ops->data;
de890a1e 813 struct netio_options *o = td->eo;
0fd666bf
JA
814 int ret;
815
de890a1e
SL
816 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
817 ret = fio_netio_setup_listen_inet(td, o->port);
0fd666bf 818 else
de890a1e 819 ret = fio_netio_setup_listen_unix(td, td->o.filename);
0fd666bf
JA
820
821 if (ret)
822 return ret;
de890a1e 823 if (o->proto == FIO_TYPE_UDP)
0fd666bf
JA
824 return 0;
825
826 if (listen(nd->listenfd, 10) < 0) {
e1161c32 827 td_verror(td, errno, "listen");
0fd666bf 828 nd->listenfd = -1;
ed92ac0c
JA
829 return 1;
830 }
831
b5af8293 832 return 0;
ed92ac0c
JA
833}
834
9bec88e1 835static int fio_netio_init(struct thread_data *td)
ed92ac0c 836{
de890a1e 837 struct netio_options *o = td->eo;
af52b345 838 int ret;
ed92ac0c 839
3f457bea
BC
840#ifdef WIN32
841 WSADATA wsd;
842 WSAStartup(MAKEWORD(2,2), &wsd);
843#endif
844
16d55aae
JA
845 if (td_random(td)) {
846 log_err("fio: network IO can't be random\n");
847 return 1;
848 }
ed92ac0c 849
de890a1e
SL
850 if (o->proto == FIO_TYPE_UNIX && o->port) {
851 log_err("fio: network IO port not valid with unix socket\n");
852 return 1;
853 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
854 log_err("fio: network IO requires port for tcp or udp\n");
855 return 1;
856 }
ed92ac0c 857
de890a1e
SL
858 if (o->proto != FIO_TYPE_TCP) {
859 if (o->listen) {
9b986065
JA
860 log_err("fio: listen only valid for TCP proto IO\n");
861 return 1;
de890a1e
SL
862 }
863 if (td_rw(td)) {
9b986065 864 log_err("fio: datagram network connections must be"
de890a1e 865 " read OR write\n");
9b986065
JA
866 return 1;
867 }
868 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
869 log_err("fio: UNIX sockets need host/filename\n");
870 return 1;
de890a1e
SL
871 }
872 o->listen = td_read(td);
873 }
443662ef 874
de890a1e
SL
875 if (o->proto != FIO_TYPE_UNIX && o->listen && td->o.filename) {
876 log_err("fio: hostname not valid for inbound network IO\n");
877 return 1;
414c2a3e 878 }
0fd666bf 879
de890a1e
SL
880 if (o->listen)
881 ret = fio_netio_setup_listen(td);
0fd666bf 882 else
de890a1e 883 ret = fio_netio_setup_connect(td);
ed92ac0c 884
7bb48f84 885 return ret;
ed92ac0c
JA
886}
887
b5af8293 888static void fio_netio_cleanup(struct thread_data *td)
9bec88e1 889{
b5af8293
JA
890 struct netio_data *nd = td->io_ops->data;
891
892 if (nd) {
64b24cd8
JA
893 if (nd->listenfd != -1)
894 close(nd->listenfd);
895 if (nd->pipes[0] != -1)
896 close(nd->pipes[0]);
897 if (nd->pipes[1] != -1)
898 close(nd->pipes[1]);
899
b5af8293 900 free(nd);
b5af8293
JA
901 }
902}
903
904static int fio_netio_setup(struct thread_data *td)
905{
7bb48f84 906 struct netio_data *nd;
7bb48f84 907
de890a1e
SL
908 if (!td->files_index) {
909 add_file(td, td->o.filename ?: "net");
910 td->o.nr_files = td->o.nr_files ?: 1;
911 }
912
7bb48f84
JA
913 if (!td->io_ops->data) {
914 nd = malloc(sizeof(*nd));;
915
916 memset(nd, 0, sizeof(*nd));
917 nd->listenfd = -1;
64b24cd8 918 nd->pipes[0] = nd->pipes[1] = -1;
7bb48f84 919 td->io_ops->data = nd;
7bb48f84 920 }
b5af8293 921
9bec88e1
JA
922 return 0;
923}
924
36d80bc7
JA
925static void fio_netio_terminate(struct thread_data *td)
926{
927 kill(td->pid, SIGUSR2);
928}
929
67bf9823 930#ifdef CONFIG_LINUX_SPLICE
9cce02e8
JA
931static int fio_netio_setup_splice(struct thread_data *td)
932{
933 struct netio_data *nd;
934
935 fio_netio_setup(td);
936
937 nd = td->io_ops->data;
938 if (nd) {
939 if (pipe(nd->pipes) < 0)
940 return 1;
941
942 nd->use_splice = 1;
943 return 0;
944 }
945
946 return 1;
947}
948
5921e80c 949static struct ioengine_ops ioengine_splice = {
de890a1e
SL
950 .name = "netsplice",
951 .version = FIO_IOOPS_VERSION,
952 .prep = fio_netio_prep,
953 .queue = fio_netio_queue,
954 .setup = fio_netio_setup_splice,
955 .init = fio_netio_init,
956 .cleanup = fio_netio_cleanup,
957 .open_file = fio_netio_open_file,
36d80bc7
JA
958 .close_file = fio_netio_close_file,
959 .terminate = fio_netio_terminate,
de890a1e
SL
960 .options = options,
961 .option_struct_size = sizeof(struct netio_options),
962 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
36d80bc7 963 FIO_PIPEIO,
ed92ac0c 964};
5921e80c 965#endif
ed92ac0c 966
5921e80c 967static struct ioengine_ops ioengine_rw = {
de890a1e
SL
968 .name = "net",
969 .version = FIO_IOOPS_VERSION,
970 .prep = fio_netio_prep,
971 .queue = fio_netio_queue,
972 .setup = fio_netio_setup,
973 .init = fio_netio_init,
974 .cleanup = fio_netio_cleanup,
975 .open_file = fio_netio_open_file,
976 .close_file = fio_netio_close_file,
36d80bc7 977 .terminate = fio_netio_terminate,
de890a1e
SL
978 .options = options,
979 .option_struct_size = sizeof(struct netio_options),
980 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
36d80bc7 981 FIO_PIPEIO,
9cce02e8
JA
982};
983
de890a1e
SL
984static int str_hostname_cb(void *data, const char *input)
985{
986 struct netio_options *o = data;
987
988 if (o->td->o.filename)
989 free(o->td->o.filename);
990 o->td->o.filename = strdup(input);
991 return 0;
992}
993
ed92ac0c
JA
994static void fio_init fio_netio_register(void)
995{
9cce02e8 996 register_ioengine(&ioengine_rw);
67bf9823 997#ifdef CONFIG_LINUX_SPLICE
9cce02e8 998 register_ioengine(&ioengine_splice);
5921e80c 999#endif
ed92ac0c
JA
1000}
1001
1002static void fio_exit fio_netio_unregister(void)
1003{
9cce02e8 1004 unregister_ioengine(&ioengine_rw);
67bf9823 1005#ifdef CONFIG_LINUX_SPLICE
9cce02e8 1006 unregister_ioengine(&ioengine_splice);
5921e80c 1007#endif
ed92ac0c 1008}