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