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