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