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