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; |
49ccb8c1 | 30 | struct sockaddr_in6 addr6; |
0fd666bf | 31 | struct sockaddr_un addr_un; |
b5af8293 | 32 | }; |
ed92ac0c | 33 | |
de890a1e SL |
34 | struct netio_options { |
35 | struct thread_data *td; | |
36 | unsigned int port; | |
37 | unsigned int proto; | |
38 | unsigned int listen; | |
6f73a7f8 | 39 | unsigned int pingpong; |
70a7878c | 40 | unsigned int nodelay; |
d3a623de | 41 | unsigned int ttl; |
1008602c | 42 | unsigned int window_size; |
e5f34d95 | 43 | unsigned int mss; |
f16b7405 | 44 | char *intfc; |
de890a1e SL |
45 | }; |
46 | ||
664fb3bd JA |
47 | struct udp_close_msg { |
48 | uint32_t magic; | |
49 | uint32_t cmd; | |
50 | }; | |
51 | ||
52 | enum { | |
53 | FIO_LINK_CLOSE = 0x89, | |
b96d2430 JA |
54 | FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b, |
55 | FIO_LINK_OPEN = 0x98, | |
0fd666bf JA |
56 | |
57 | FIO_TYPE_TCP = 1, | |
58 | FIO_TYPE_UDP = 2, | |
59 | FIO_TYPE_UNIX = 3, | |
49ccb8c1 JA |
60 | FIO_TYPE_TCP_V6 = 4, |
61 | FIO_TYPE_UDP_V6 = 5, | |
664fb3bd JA |
62 | }; |
63 | ||
de890a1e SL |
64 | static int str_hostname_cb(void *data, const char *input); |
65 | static struct fio_option options[] = { | |
66 | { | |
67 | .name = "hostname", | |
e8b0e958 | 68 | .lname = "net engine hostname", |
de890a1e SL |
69 | .type = FIO_OPT_STR_STORE, |
70 | .cb = str_hostname_cb, | |
71 | .help = "Hostname for net IO engine", | |
e90a0adf JA |
72 | .category = FIO_OPT_C_ENGINE, |
73 | .group = FIO_OPT_G_NETIO, | |
de890a1e SL |
74 | }, |
75 | { | |
76 | .name = "port", | |
e8b0e958 | 77 | .lname = "net engine port", |
de890a1e SL |
78 | .type = FIO_OPT_INT, |
79 | .off1 = offsetof(struct netio_options, port), | |
80 | .minval = 1, | |
81 | .maxval = 65535, | |
82 | .help = "Port to use for TCP or UDP net connections", | |
e90a0adf JA |
83 | .category = FIO_OPT_C_ENGINE, |
84 | .group = FIO_OPT_G_NETIO, | |
de890a1e SL |
85 | }, |
86 | { | |
87 | .name = "protocol", | |
e8b0e958 | 88 | .lname = "net engine protocol", |
de890a1e SL |
89 | .alias = "proto", |
90 | .type = FIO_OPT_STR, | |
91 | .off1 = offsetof(struct netio_options, proto), | |
92 | .help = "Network protocol to use", | |
93 | .def = "tcp", | |
94 | .posval = { | |
95 | { .ival = "tcp", | |
96 | .oval = FIO_TYPE_TCP, | |
97 | .help = "Transmission Control Protocol", | |
98 | }, | |
eb232310 | 99 | #ifdef CONFIG_IPV6 |
49ccb8c1 JA |
100 | { .ival = "tcpv6", |
101 | .oval = FIO_TYPE_TCP_V6, | |
102 | .help = "Transmission Control Protocol V6", | |
103 | }, | |
eb232310 | 104 | #endif |
de890a1e SL |
105 | { .ival = "udp", |
106 | .oval = FIO_TYPE_UDP, | |
f5cc3d0e | 107 | .help = "User Datagram Protocol", |
de890a1e | 108 | }, |
eb232310 | 109 | #ifdef CONFIG_IPV6 |
49ccb8c1 JA |
110 | { .ival = "udpv6", |
111 | .oval = FIO_TYPE_UDP_V6, | |
112 | .help = "User Datagram Protocol V6", | |
113 | }, | |
eb232310 | 114 | #endif |
de890a1e SL |
115 | { .ival = "unix", |
116 | .oval = FIO_TYPE_UNIX, | |
117 | .help = "UNIX domain socket", | |
118 | }, | |
119 | }, | |
e90a0adf JA |
120 | .category = FIO_OPT_C_ENGINE, |
121 | .group = FIO_OPT_G_NETIO, | |
de890a1e | 122 | }, |
1eafa37a | 123 | #ifdef CONFIG_TCP_NODELAY |
70a7878c SN |
124 | { |
125 | .name = "nodelay", | |
126 | .type = FIO_OPT_BOOL, | |
127 | .off1 = offsetof(struct netio_options, nodelay), | |
128 | .help = "Use TCP_NODELAY on TCP connections", | |
e90a0adf JA |
129 | .category = FIO_OPT_C_ENGINE, |
130 | .group = FIO_OPT_G_NETIO, | |
70a7878c | 131 | }, |
1eafa37a | 132 | #endif |
de890a1e SL |
133 | { |
134 | .name = "listen", | |
e8b0e958 | 135 | .lname = "net engine listen", |
de890a1e SL |
136 | .type = FIO_OPT_STR_SET, |
137 | .off1 = offsetof(struct netio_options, listen), | |
138 | .help = "Listen for incoming TCP connections", | |
e90a0adf JA |
139 | .category = FIO_OPT_C_ENGINE, |
140 | .group = FIO_OPT_G_NETIO, | |
de890a1e | 141 | }, |
6f73a7f8 JA |
142 | { |
143 | .name = "pingpong", | |
144 | .type = FIO_OPT_STR_SET, | |
145 | .off1 = offsetof(struct netio_options, pingpong), | |
146 | .help = "Ping-pong IO requests", | |
e90a0adf JA |
147 | .category = FIO_OPT_C_ENGINE, |
148 | .group = FIO_OPT_G_NETIO, | |
6f73a7f8 | 149 | }, |
b93b6a2e SB |
150 | { |
151 | .name = "interface", | |
152 | .lname = "net engine interface", | |
153 | .type = FIO_OPT_STR_STORE, | |
f16b7405 | 154 | .off1 = offsetof(struct netio_options, intfc), |
b93b6a2e SB |
155 | .help = "Network interface to use", |
156 | .category = FIO_OPT_C_ENGINE, | |
157 | .group = FIO_OPT_G_NETIO, | |
158 | }, | |
d3a623de SB |
159 | { |
160 | .name = "ttl", | |
161 | .lname = "net engine multicast ttl", | |
162 | .type = FIO_OPT_INT, | |
163 | .off1 = offsetof(struct netio_options, ttl), | |
164 | .def = "1", | |
165 | .minval = 0, | |
166 | .help = "Time-to-live value for outgoing UDP multicast packets", | |
167 | .category = FIO_OPT_C_ENGINE, | |
168 | .group = FIO_OPT_G_NETIO, | |
169 | }, | |
1008602c JA |
170 | #ifdef CONFIG_NET_WINDOWSIZE |
171 | { | |
172 | .name = "window_size", | |
173 | .lname = "Window Size", | |
174 | .type = FIO_OPT_INT, | |
175 | .off1 = offsetof(struct netio_options, window_size), | |
176 | .minval = 0, | |
177 | .help = "Set socket buffer window size", | |
178 | .category = FIO_OPT_C_ENGINE, | |
179 | .group = FIO_OPT_G_NETIO, | |
180 | }, | |
e5f34d95 JA |
181 | #endif |
182 | #ifdef CONFIG_NET_MSS | |
183 | { | |
184 | .name = "mss", | |
185 | .lname = "Maximum segment size", | |
186 | .type = FIO_OPT_INT, | |
187 | .off1 = offsetof(struct netio_options, mss), | |
188 | .minval = 0, | |
189 | .help = "Set TCP maximum segment size", | |
190 | .category = FIO_OPT_C_ENGINE, | |
191 | .group = FIO_OPT_G_NETIO, | |
192 | }, | |
1008602c | 193 | #endif |
de890a1e SL |
194 | { |
195 | .name = NULL, | |
196 | }, | |
197 | }; | |
198 | ||
49ccb8c1 JA |
199 | static inline int is_udp(struct netio_options *o) |
200 | { | |
201 | return o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_UDP_V6; | |
202 | } | |
203 | ||
204 | static inline int is_tcp(struct netio_options *o) | |
205 | { | |
206 | return o->proto == FIO_TYPE_TCP || o->proto == FIO_TYPE_TCP_V6; | |
207 | } | |
208 | ||
209 | static inline int is_ipv6(struct netio_options *o) | |
210 | { | |
211 | return o->proto == FIO_TYPE_UDP_V6 || o->proto == FIO_TYPE_TCP_V6; | |
212 | } | |
213 | ||
1008602c JA |
214 | static int set_window_size(struct thread_data *td, int fd) |
215 | { | |
216 | #ifdef CONFIG_NET_WINDOWSIZE | |
217 | struct netio_options *o = td->eo; | |
218 | unsigned int wss; | |
219 | int snd, rcv, ret; | |
220 | ||
221 | if (!o->window_size) | |
222 | return 0; | |
223 | ||
224 | rcv = o->listen || o->pingpong; | |
225 | snd = !o->listen || o->pingpong; | |
226 | wss = o->window_size; | |
227 | ret = 0; | |
228 | ||
229 | if (rcv) { | |
230 | ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void *) &wss, | |
231 | sizeof(wss)); | |
232 | if (ret < 0) | |
233 | td_verror(td, errno, "rcvbuf window size"); | |
234 | } | |
235 | if (snd && !ret) { | |
236 | ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void *) &wss, | |
237 | sizeof(wss)); | |
238 | if (ret < 0) | |
239 | td_verror(td, errno, "sndbuf window size"); | |
240 | } | |
241 | ||
242 | return ret; | |
243 | #else | |
244 | td_verror(td, -EINVAL, "setsockopt window size"); | |
245 | return -1; | |
246 | #endif | |
247 | } | |
248 | ||
e5f34d95 JA |
249 | static int set_mss(struct thread_data *td, int fd) |
250 | { | |
251 | #ifdef CONFIG_NET_MSS | |
252 | struct netio_options *o = td->eo; | |
253 | unsigned int mss; | |
254 | int ret; | |
255 | ||
256 | if (!o->mss || !is_tcp(o)) | |
257 | return 0; | |
258 | ||
259 | mss = o->mss; | |
260 | ret = setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, (void *) &mss, | |
261 | sizeof(mss)); | |
262 | if (ret < 0) | |
263 | td_verror(td, errno, "setsockopt TCP_MAXSEG"); | |
264 | ||
265 | return ret; | |
266 | #else | |
267 | td_verror(td, -EINVAL, "setsockopt TCP_MAXSEG"); | |
268 | return -1; | |
269 | #endif | |
270 | } | |
271 | ||
272 | ||
371d456c JA |
273 | /* |
274 | * Return -1 for error and 'nr events' for a positive number | |
275 | * of events | |
276 | */ | |
277 | static int poll_wait(struct thread_data *td, int fd, short events) | |
278 | { | |
279 | struct pollfd pfd; | |
280 | int ret; | |
281 | ||
282 | while (!td->terminate) { | |
283 | pfd.fd = fd; | |
284 | pfd.events = events; | |
285 | ret = poll(&pfd, 1, -1); | |
286 | if (ret < 0) { | |
287 | if (errno == EINTR) | |
d5b388a5 | 288 | break; |
371d456c JA |
289 | |
290 | td_verror(td, errno, "poll"); | |
291 | return -1; | |
292 | } else if (!ret) | |
293 | continue; | |
294 | ||
295 | break; | |
296 | } | |
297 | ||
298 | if (pfd.revents & events) | |
299 | return 1; | |
371d456c JA |
300 | |
301 | return -1; | |
302 | } | |
303 | ||
b511c9aa SB |
304 | static int fio_netio_is_multicast(const char *mcaddr) |
305 | { | |
306 | in_addr_t addr = inet_network(mcaddr); | |
307 | if (addr == -1) | |
308 | return 0; | |
309 | ||
310 | if (inet_network("224.0.0.0") <= addr && | |
311 | inet_network("239.255.255.255") >= addr) | |
312 | return 1; | |
313 | ||
314 | return 0; | |
315 | } | |
316 | ||
317 | ||
ed92ac0c JA |
318 | static int fio_netio_prep(struct thread_data *td, struct io_u *io_u) |
319 | { | |
de890a1e | 320 | struct netio_options *o = td->eo; |
ed92ac0c | 321 | |
7a6499da JA |
322 | /* |
323 | * Make sure we don't see spurious reads to a receiver, and vice versa | |
324 | */ | |
49ccb8c1 | 325 | if (is_tcp(o)) |
de890a1e SL |
326 | return 0; |
327 | ||
328 | if ((o->listen && io_u->ddir == DDIR_WRITE) || | |
329 | (!o->listen && io_u->ddir == DDIR_READ)) { | |
e1161c32 | 330 | td_verror(td, EINVAL, "bad direction"); |
7a6499da | 331 | return 1; |
ed92ac0c | 332 | } |
3f457bea | 333 | |
f85ac25a | 334 | return 0; |
ed92ac0c JA |
335 | } |
336 | ||
67bf9823 | 337 | #ifdef CONFIG_LINUX_SPLICE |
cd963e18 | 338 | static int splice_io_u(int fdin, int fdout, unsigned int len) |
ed92ac0c | 339 | { |
9cce02e8 | 340 | int bytes = 0; |
7a6499da | 341 | |
9cce02e8 | 342 | while (len) { |
cd963e18 | 343 | int ret = splice(fdin, NULL, fdout, NULL, len, 0); |
9cce02e8 JA |
344 | |
345 | if (ret < 0) { | |
346 | if (!bytes) | |
347 | bytes = ret; | |
348 | ||
349 | break; | |
350 | } else if (!ret) | |
351 | break; | |
352 | ||
353 | bytes += ret; | |
f657a2fb | 354 | len -= ret; |
9cce02e8 JA |
355 | } |
356 | ||
357 | return bytes; | |
358 | } | |
359 | ||
360 | /* | |
cd963e18 | 361 | * Receive bytes from a socket and fill them into the internal pipe |
9cce02e8 | 362 | */ |
cd963e18 | 363 | static int splice_in(struct thread_data *td, struct io_u *io_u) |
9cce02e8 JA |
364 | { |
365 | struct netio_data *nd = td->io_ops->data; | |
9cce02e8 | 366 | |
cd963e18 | 367 | return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen); |
9cce02e8 JA |
368 | } |
369 | ||
370 | /* | |
cd963e18 | 371 | * Transmit 'len' bytes from the internal pipe |
9cce02e8 | 372 | */ |
cd963e18 JA |
373 | static int splice_out(struct thread_data *td, struct io_u *io_u, |
374 | unsigned int len) | |
9cce02e8 JA |
375 | { |
376 | struct netio_data *nd = td->io_ops->data; | |
cd963e18 JA |
377 | |
378 | return splice_io_u(nd->pipes[0], io_u->file->fd, len); | |
379 | } | |
380 | ||
381 | static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len) | |
382 | { | |
9cce02e8 JA |
383 | struct iovec iov = { |
384 | .iov_base = io_u->xfer_buf, | |
385 | .iov_len = len, | |
386 | }; | |
387 | int bytes = 0; | |
388 | ||
389 | while (iov.iov_len) { | |
cd963e18 | 390 | int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE); |
9cce02e8 JA |
391 | |
392 | if (ret < 0) { | |
393 | if (!bytes) | |
394 | bytes = ret; | |
395 | break; | |
396 | } else if (!ret) | |
397 | break; | |
398 | ||
399 | iov.iov_len -= ret; | |
cd963e18 | 400 | iov.iov_base += ret; |
f657a2fb | 401 | bytes += ret; |
9cce02e8 JA |
402 | } |
403 | ||
404 | return bytes; | |
cd963e18 | 405 | |
9cce02e8 JA |
406 | } |
407 | ||
408 | /* | |
cd963e18 | 409 | * vmsplice() pipe to io_u buffer |
9cce02e8 | 410 | */ |
cd963e18 JA |
411 | static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u, |
412 | unsigned int len) | |
9cce02e8 JA |
413 | { |
414 | struct netio_data *nd = td->io_ops->data; | |
9cce02e8 | 415 | |
cd963e18 JA |
416 | return vmsplice_io_u(io_u, nd->pipes[0], len); |
417 | } | |
9cce02e8 | 418 | |
cd963e18 JA |
419 | /* |
420 | * vmsplice() io_u to pipe | |
421 | */ | |
422 | static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u) | |
423 | { | |
424 | struct netio_data *nd = td->io_ops->data; | |
ed92ac0c | 425 | |
cd963e18 | 426 | return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen); |
9cce02e8 JA |
427 | } |
428 | ||
cd963e18 JA |
429 | /* |
430 | * splice receive - transfer socket data into a pipe using splice, then map | |
431 | * that pipe data into the io_u using vmsplice. | |
432 | */ | |
9cce02e8 JA |
433 | static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u) |
434 | { | |
435 | int ret; | |
436 | ||
437 | ret = splice_in(td, io_u); | |
cd963e18 JA |
438 | if (ret > 0) |
439 | return vmsplice_io_u_out(td, io_u, ret); | |
9cce02e8 | 440 | |
cd963e18 | 441 | return ret; |
9cce02e8 JA |
442 | } |
443 | ||
cd963e18 JA |
444 | /* |
445 | * splice transmit - map data from the io_u into a pipe by using vmsplice, | |
446 | * then transfer that pipe to a socket using splice. | |
447 | */ | |
9cce02e8 JA |
448 | static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u) |
449 | { | |
450 | int ret; | |
451 | ||
452 | ret = vmsplice_io_u_in(td, io_u); | |
cd963e18 JA |
453 | if (ret > 0) |
454 | return splice_out(td, io_u, ret); | |
9cce02e8 | 455 | |
cd963e18 | 456 | return ret; |
9cce02e8 | 457 | } |
5921e80c JA |
458 | #else |
459 | static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u) | |
460 | { | |
af8771b9 | 461 | errno = EOPNOTSUPP; |
5921e80c JA |
462 | return -1; |
463 | } | |
464 | ||
465 | static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u) | |
466 | { | |
af8771b9 | 467 | errno = EOPNOTSUPP; |
5921e80c JA |
468 | return -1; |
469 | } | |
470 | #endif | |
9cce02e8 JA |
471 | |
472 | static int fio_netio_send(struct thread_data *td, struct io_u *io_u) | |
473 | { | |
414c2a3e | 474 | struct netio_data *nd = td->io_ops->data; |
de890a1e | 475 | struct netio_options *o = td->eo; |
6f73a7f8 | 476 | int ret, flags = 0; |
371d456c | 477 | |
664fb3bd | 478 | do { |
49ccb8c1 | 479 | if (is_udp(o)) { |
10aa136b | 480 | const struct sockaddr *to; |
49ccb8c1 JA |
481 | socklen_t len; |
482 | ||
483 | if (is_ipv6(o)) { | |
484 | to = (struct sockaddr *) &nd->addr6; | |
485 | len = sizeof(nd->addr6); | |
486 | } else { | |
487 | to = (struct sockaddr *) &nd->addr; | |
488 | len = sizeof(nd->addr); | |
489 | } | |
62b38926 | 490 | |
664fb3bd | 491 | ret = sendto(io_u->file->fd, io_u->xfer_buf, |
49ccb8c1 | 492 | io_u->xfer_buflen, flags, to, len); |
664fb3bd JA |
493 | } else { |
494 | /* | |
495 | * if we are going to write more, set MSG_MORE | |
496 | */ | |
5921e80c | 497 | #ifdef MSG_MORE |
6f73a7f8 JA |
498 | if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen < |
499 | td->o.size) && !o->pingpong) | |
664fb3bd | 500 | flags |= MSG_MORE; |
5921e80c | 501 | #endif |
664fb3bd JA |
502 | ret = send(io_u->file->fd, io_u->xfer_buf, |
503 | io_u->xfer_buflen, flags); | |
504 | } | |
505 | if (ret > 0) | |
506 | break; | |
9cce02e8 | 507 | |
664fb3bd JA |
508 | ret = poll_wait(td, io_u->file->fd, POLLOUT); |
509 | if (ret <= 0) | |
510 | break; | |
664fb3bd JA |
511 | } while (1); |
512 | ||
513 | return ret; | |
514 | } | |
515 | ||
516 | static int is_udp_close(struct io_u *io_u, int len) | |
517 | { | |
518 | struct udp_close_msg *msg; | |
519 | ||
520 | if (len != sizeof(struct udp_close_msg)) | |
521 | return 0; | |
522 | ||
523 | msg = io_u->xfer_buf; | |
b96d2430 | 524 | if (ntohl(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC) |
664fb3bd JA |
525 | return 0; |
526 | if (ntohl(msg->cmd) != FIO_LINK_CLOSE) | |
527 | return 0; | |
528 | ||
529 | return 1; | |
9cce02e8 JA |
530 | } |
531 | ||
414c2a3e | 532 | static int fio_netio_recv(struct thread_data *td, struct io_u *io_u) |
9cce02e8 | 533 | { |
414c2a3e | 534 | struct netio_data *nd = td->io_ops->data; |
de890a1e | 535 | struct netio_options *o = td->eo; |
6f73a7f8 | 536 | int ret, flags = 0; |
664fb3bd JA |
537 | |
538 | do { | |
49ccb8c1 | 539 | if (is_udp(o)) { |
b511c9aa | 540 | struct sockaddr *from; |
49ccb8c1 | 541 | socklen_t l, *len = &l; |
b511c9aa SB |
542 | |
543 | if (o->listen) { | |
49ccb8c1 JA |
544 | if (!is_ipv6(o)) { |
545 | from = (struct sockaddr *) &nd->addr; | |
546 | *len = sizeof(nd->addr); | |
547 | } else { | |
548 | from = (struct sockaddr *) &nd->addr6; | |
549 | *len = sizeof(nd->addr6); | |
550 | } | |
b511c9aa SB |
551 | } else { |
552 | from = NULL; | |
553 | len = NULL; | |
554 | } | |
664fb3bd JA |
555 | |
556 | ret = recvfrom(io_u->file->fd, io_u->xfer_buf, | |
b511c9aa | 557 | io_u->xfer_buflen, flags, from, len); |
664fb3bd JA |
558 | if (is_udp_close(io_u, ret)) { |
559 | td->done = 1; | |
560 | return 0; | |
561 | } | |
562 | } else { | |
563 | ret = recv(io_u->file->fd, io_u->xfer_buf, | |
564 | io_u->xfer_buflen, flags); | |
565 | } | |
566 | if (ret > 0) | |
567 | break; | |
7d988f68 JA |
568 | else if (!ret && (flags & MSG_WAITALL)) |
569 | break; | |
9cce02e8 | 570 | |
664fb3bd JA |
571 | ret = poll_wait(td, io_u->file->fd, POLLIN); |
572 | if (ret <= 0) | |
573 | break; | |
664fb3bd JA |
574 | flags |= MSG_WAITALL; |
575 | } while (1); | |
414c2a3e | 576 | |
664fb3bd | 577 | return ret; |
9cce02e8 JA |
578 | } |
579 | ||
6f73a7f8 JA |
580 | static int __fio_netio_queue(struct thread_data *td, struct io_u *io_u, |
581 | enum fio_ddir ddir) | |
9cce02e8 JA |
582 | { |
583 | struct netio_data *nd = td->io_ops->data; | |
de890a1e | 584 | struct netio_options *o = td->eo; |
9cce02e8 JA |
585 | int ret; |
586 | ||
6f73a7f8 | 587 | if (ddir == DDIR_WRITE) { |
49ccb8c1 | 588 | if (!nd->use_splice || is_udp(o) || |
de890a1e | 589 | o->proto == FIO_TYPE_UNIX) |
9cce02e8 | 590 | ret = fio_netio_send(td, io_u); |
414c2a3e JA |
591 | else |
592 | ret = fio_netio_splice_out(td, io_u); | |
6f73a7f8 | 593 | } else if (ddir == DDIR_READ) { |
49ccb8c1 | 594 | if (!nd->use_splice || is_udp(o) || |
de890a1e | 595 | o->proto == FIO_TYPE_UNIX) |
414c2a3e | 596 | ret = fio_netio_recv(td, io_u); |
9cce02e8 | 597 | else |
414c2a3e | 598 | ret = fio_netio_splice_in(td, io_u); |
d4f12dd0 | 599 | } else |
7a6499da | 600 | ret = 0; /* must be a SYNC */ |
ed92ac0c | 601 | |
cec6b55d | 602 | if (ret != (int) io_u->xfer_buflen) { |
f6846c65 | 603 | if (ret > 0) { |
cec6b55d JA |
604 | io_u->resid = io_u->xfer_buflen - ret; |
605 | io_u->error = 0; | |
36167d82 | 606 | return FIO_Q_COMPLETED; |
f6846c65 JA |
607 | } else if (!ret) |
608 | return FIO_Q_BUSY; | |
609 | else { | |
414c2a3e JA |
610 | int err = errno; |
611 | ||
6f73a7f8 | 612 | if (ddir == DDIR_WRITE && err == EMSGSIZE) |
414c2a3e JA |
613 | return FIO_Q_BUSY; |
614 | ||
615 | io_u->error = err; | |
616 | } | |
ed92ac0c JA |
617 | } |
618 | ||
36167d82 | 619 | if (io_u->error) |
e1161c32 | 620 | td_verror(td, io_u->error, "xfer"); |
ed92ac0c | 621 | |
36167d82 | 622 | return FIO_Q_COMPLETED; |
ed92ac0c JA |
623 | } |
624 | ||
6f73a7f8 JA |
625 | static int fio_netio_queue(struct thread_data *td, struct io_u *io_u) |
626 | { | |
627 | struct netio_options *o = td->eo; | |
628 | int ret; | |
629 | ||
630 | fio_ro_check(td, io_u); | |
631 | ||
632 | ret = __fio_netio_queue(td, io_u, io_u->ddir); | |
633 | if (!o->pingpong || ret != FIO_Q_COMPLETED) | |
634 | return ret; | |
635 | ||
636 | /* | |
637 | * For ping-pong mode, receive or send reply as needed | |
638 | */ | |
639 | if (td_read(td) && io_u->ddir == DDIR_READ) | |
640 | ret = __fio_netio_queue(td, io_u, DDIR_WRITE); | |
641 | else if (td_write(td) && io_u->ddir == DDIR_WRITE) | |
642 | ret = __fio_netio_queue(td, io_u, DDIR_READ); | |
643 | ||
644 | return ret; | |
645 | } | |
646 | ||
b5af8293 | 647 | static int fio_netio_connect(struct thread_data *td, struct fio_file *f) |
ed92ac0c | 648 | { |
b5af8293 | 649 | struct netio_data *nd = td->io_ops->data; |
de890a1e | 650 | struct netio_options *o = td->eo; |
6264c7a8 | 651 | int type, domain; |
414c2a3e | 652 | |
de890a1e | 653 | if (o->proto == FIO_TYPE_TCP) { |
0fd666bf | 654 | domain = AF_INET; |
414c2a3e | 655 | type = SOCK_STREAM; |
49ccb8c1 JA |
656 | } else if (o->proto == FIO_TYPE_TCP_V6) { |
657 | domain = AF_INET6; | |
658 | type = SOCK_STREAM; | |
de890a1e | 659 | } else if (o->proto == FIO_TYPE_UDP) { |
0fd666bf | 660 | domain = AF_INET; |
414c2a3e | 661 | type = SOCK_DGRAM; |
49ccb8c1 JA |
662 | } else if (o->proto == FIO_TYPE_UDP_V6) { |
663 | domain = AF_INET6; | |
664 | type = SOCK_DGRAM; | |
de890a1e | 665 | } else if (o->proto == FIO_TYPE_UNIX) { |
0fd666bf JA |
666 | domain = AF_UNIX; |
667 | type = SOCK_STREAM; | |
668 | } else { | |
de890a1e | 669 | log_err("fio: bad network type %d\n", o->proto); |
0fd666bf JA |
670 | f->fd = -1; |
671 | return 1; | |
672 | } | |
ed92ac0c | 673 | |
0fd666bf | 674 | f->fd = socket(domain, type, 0); |
b5af8293 JA |
675 | if (f->fd < 0) { |
676 | td_verror(td, errno, "socket"); | |
677 | return 1; | |
ed92ac0c JA |
678 | } |
679 | ||
1eafa37a | 680 | #ifdef CONFIG_TCP_NODELAY |
49ccb8c1 | 681 | if (o->nodelay && is_tcp(o)) { |
6264c7a8 JA |
682 | int optval = 1; |
683 | ||
26e594a5 | 684 | if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) { |
70a7878c SN |
685 | log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno)); |
686 | return 1; | |
687 | } | |
688 | } | |
1eafa37a | 689 | #endif |
70a7878c | 690 | |
1008602c JA |
691 | if (set_window_size(td, f->fd)) { |
692 | close(f->fd); | |
693 | return 1; | |
694 | } | |
e5f34d95 JA |
695 | if (set_mss(td, f->fd)) { |
696 | close(f->fd); | |
697 | return 1; | |
698 | } | |
1008602c | 699 | |
49ccb8c1 | 700 | if (is_udp(o)) { |
d3a623de SB |
701 | if (!fio_netio_is_multicast(td->o.filename)) |
702 | return 0; | |
49ccb8c1 JA |
703 | if (is_ipv6(o)) { |
704 | log_err("fio: multicast not supported on IPv6\n"); | |
705 | close(f->fd); | |
706 | return 1; | |
707 | } | |
d3a623de | 708 | |
f16b7405 | 709 | if (o->intfc) { |
b93b6a2e | 710 | struct in_addr interface_addr; |
49ccb8c1 | 711 | |
f16b7405 | 712 | if (inet_aton(o->intfc, &interface_addr) == 0) { |
b93b6a2e SB |
713 | log_err("fio: interface not valid interface IP\n"); |
714 | close(f->fd); | |
715 | return 1; | |
716 | } | |
f16b7405 | 717 | if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_IF, (const char*)&interface_addr, sizeof(interface_addr)) < 0) { |
b93b6a2e SB |
718 | td_verror(td, errno, "setsockopt IP_MULTICAST_IF"); |
719 | close(f->fd); | |
720 | return 1; | |
721 | } | |
722 | } | |
f16b7405 | 723 | if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_TTL, (const char*)&o->ttl, sizeof(o->ttl)) < 0) { |
d3a623de SB |
724 | td_verror(td, errno, "setsockopt IP_MULTICAST_TTL"); |
725 | close(f->fd); | |
726 | return 1; | |
727 | } | |
414c2a3e | 728 | return 0; |
b93b6a2e | 729 | } else if (o->proto == FIO_TYPE_TCP) { |
67bf9823 | 730 | socklen_t len = sizeof(nd->addr); |
414c2a3e | 731 | |
0fd666bf JA |
732 | if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) { |
733 | td_verror(td, errno, "connect"); | |
b94cba47 | 734 | close(f->fd); |
0fd666bf JA |
735 | return 1; |
736 | } | |
49ccb8c1 JA |
737 | } else if (o->proto == FIO_TYPE_TCP_V6) { |
738 | socklen_t len = sizeof(nd->addr6); | |
739 | ||
740 | if (connect(f->fd, (struct sockaddr *) &nd->addr6, len) < 0) { | |
741 | td_verror(td, errno, "connect"); | |
742 | close(f->fd); | |
743 | return 1; | |
744 | } | |
745 | ||
0fd666bf JA |
746 | } else { |
747 | struct sockaddr_un *addr = &nd->addr_un; | |
67bf9823 | 748 | socklen_t len; |
0fd666bf JA |
749 | |
750 | len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1; | |
751 | ||
752 | if (connect(f->fd, (struct sockaddr *) addr, len) < 0) { | |
753 | td_verror(td, errno, "connect"); | |
b94cba47 | 754 | close(f->fd); |
0fd666bf JA |
755 | return 1; |
756 | } | |
ed92ac0c JA |
757 | } |
758 | ||
759 | return 0; | |
ed92ac0c JA |
760 | } |
761 | ||
b5af8293 | 762 | static int fio_netio_accept(struct thread_data *td, struct fio_file *f) |
5fdd124a | 763 | { |
b5af8293 | 764 | struct netio_data *nd = td->io_ops->data; |
de890a1e | 765 | struct netio_options *o = td->eo; |
49ccb8c1 | 766 | socklen_t socklen; |
6264c7a8 | 767 | int state; |
5fdd124a | 768 | |
49ccb8c1 | 769 | if (is_udp(o)) { |
414c2a3e JA |
770 | f->fd = nd->listenfd; |
771 | return 0; | |
772 | } | |
773 | ||
859088d3 JA |
774 | state = td->runstate; |
775 | td_set_runstate(td, TD_SETTING_UP); | |
776 | ||
6d86144d | 777 | log_info("fio: waiting for connection\n"); |
5fdd124a | 778 | |
371d456c | 779 | if (poll_wait(td, nd->listenfd, POLLIN) < 0) |
859088d3 | 780 | goto err; |
0c09442b | 781 | |
49ccb8c1 JA |
782 | if (o->proto == FIO_TYPE_TCP) { |
783 | socklen = sizeof(nd->addr); | |
784 | f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen); | |
785 | } else { | |
786 | socklen = sizeof(nd->addr6); | |
787 | f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr6, &socklen); | |
788 | } | |
789 | ||
371d456c JA |
790 | if (f->fd < 0) { |
791 | td_verror(td, errno, "accept"); | |
859088d3 | 792 | goto err; |
b5af8293 | 793 | } |
5fdd124a | 794 | |
1eafa37a | 795 | #ifdef CONFIG_TCP_NODELAY |
49ccb8c1 | 796 | if (o->nodelay && is_tcp(o)) { |
6264c7a8 JA |
797 | int optval = 1; |
798 | ||
26e594a5 | 799 | if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) { |
70a7878c SN |
800 | log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno)); |
801 | return 1; | |
802 | } | |
803 | } | |
1eafa37a | 804 | #endif |
70a7878c | 805 | |
0cae16ff | 806 | reset_all_stats(td); |
859088d3 | 807 | td_set_runstate(td, state); |
b5af8293 | 808 | return 0; |
859088d3 JA |
809 | err: |
810 | td_set_runstate(td, state); | |
811 | return 1; | |
b5af8293 JA |
812 | } |
813 | ||
664fb3bd JA |
814 | static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f) |
815 | { | |
816 | struct netio_data *nd = td->io_ops->data; | |
49ccb8c1 | 817 | struct netio_options *o = td->eo; |
664fb3bd | 818 | struct udp_close_msg msg; |
49ccb8c1 JA |
819 | struct sockaddr *to; |
820 | socklen_t len; | |
664fb3bd JA |
821 | int ret; |
822 | ||
49ccb8c1 JA |
823 | if (is_ipv6(o)) { |
824 | to = (struct sockaddr *) &nd->addr6; | |
825 | len = sizeof(nd->addr6); | |
826 | } else { | |
827 | to = (struct sockaddr *) &nd->addr; | |
828 | len = sizeof(nd->addr); | |
829 | } | |
830 | ||
b96d2430 | 831 | msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC); |
664fb3bd JA |
832 | msg.cmd = htonl(FIO_LINK_CLOSE); |
833 | ||
49ccb8c1 | 834 | ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len); |
664fb3bd JA |
835 | if (ret < 0) |
836 | td_verror(td, errno, "sendto udp link close"); | |
837 | } | |
838 | ||
839 | static int fio_netio_close_file(struct thread_data *td, struct fio_file *f) | |
840 | { | |
de890a1e | 841 | struct netio_options *o = td->eo; |
664fb3bd JA |
842 | |
843 | /* | |
844 | * If this is an UDP connection, notify the receiver that we are | |
845 | * closing down the link | |
846 | */ | |
49ccb8c1 | 847 | if (is_udp(o)) |
664fb3bd JA |
848 | fio_netio_udp_close(td, f); |
849 | ||
850 | return generic_close_file(td, f); | |
851 | } | |
852 | ||
b96d2430 JA |
853 | static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f) |
854 | { | |
855 | struct netio_data *nd = td->io_ops->data; | |
49ccb8c1 | 856 | struct netio_options *o = td->eo; |
b96d2430 | 857 | struct udp_close_msg msg; |
49ccb8c1 JA |
858 | struct sockaddr *to; |
859 | socklen_t len; | |
b96d2430 JA |
860 | int ret; |
861 | ||
49ccb8c1 JA |
862 | if (is_ipv6(o)) { |
863 | len = sizeof(nd->addr6); | |
864 | to = (struct sockaddr *) &nd->addr6; | |
865 | } else { | |
866 | len = sizeof(nd->addr); | |
867 | to = (struct sockaddr *) &nd->addr; | |
868 | } | |
869 | ||
1f81991e | 870 | ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len); |
b96d2430 | 871 | if (ret < 0) { |
ee7062fd | 872 | td_verror(td, errno, "recvfrom udp link open"); |
b96d2430 JA |
873 | return ret; |
874 | } | |
875 | ||
876 | if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC || | |
877 | ntohl(msg.cmd) != FIO_LINK_OPEN) { | |
878 | log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic), | |
879 | ntohl(msg.cmd)); | |
880 | return -1; | |
881 | } | |
882 | ||
883 | return 0; | |
884 | } | |
885 | ||
886 | static int fio_netio_udp_send_open(struct thread_data *td, struct fio_file *f) | |
887 | { | |
888 | struct netio_data *nd = td->io_ops->data; | |
49ccb8c1 | 889 | struct netio_options *o = td->eo; |
b96d2430 | 890 | struct udp_close_msg msg; |
49ccb8c1 JA |
891 | struct sockaddr *to; |
892 | socklen_t len; | |
b96d2430 JA |
893 | int ret; |
894 | ||
49ccb8c1 JA |
895 | if (is_ipv6(o)) { |
896 | len = sizeof(nd->addr6); | |
897 | to = (struct sockaddr *) &nd->addr6; | |
898 | } else { | |
899 | len = sizeof(nd->addr); | |
900 | to = (struct sockaddr *) &nd->addr; | |
901 | } | |
902 | ||
b96d2430 JA |
903 | msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC); |
904 | msg.cmd = htonl(FIO_LINK_OPEN); | |
905 | ||
49ccb8c1 | 906 | ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len); |
b96d2430 JA |
907 | if (ret < 0) { |
908 | td_verror(td, errno, "sendto udp link open"); | |
909 | return ret; | |
910 | } | |
911 | ||
912 | return 0; | |
913 | } | |
914 | ||
915 | static int fio_netio_open_file(struct thread_data *td, struct fio_file *f) | |
916 | { | |
917 | int ret; | |
918 | struct netio_options *o = td->eo; | |
919 | ||
920 | if (o->listen) | |
921 | ret = fio_netio_accept(td, f); | |
922 | else | |
923 | ret = fio_netio_connect(td, f); | |
924 | ||
925 | if (ret) { | |
926 | f->fd = -1; | |
927 | return ret; | |
928 | } | |
929 | ||
49ccb8c1 | 930 | if (is_udp(o)) { |
b96d2430 JA |
931 | if (td_write(td)) |
932 | ret = fio_netio_udp_send_open(td, f); | |
933 | else { | |
934 | int state; | |
935 | ||
936 | state = td->runstate; | |
937 | td_set_runstate(td, TD_SETTING_UP); | |
938 | ret = fio_netio_udp_recv_open(td, f); | |
939 | td_set_runstate(td, state); | |
940 | } | |
941 | } | |
942 | ||
943 | if (ret) | |
944 | fio_netio_close_file(td, f); | |
945 | ||
946 | return ret; | |
947 | } | |
948 | ||
0b783341 JA |
949 | static int fio_fill_addr(struct thread_data *td, const char *host, int af, |
950 | void *dst, struct addrinfo **res) | |
951 | { | |
952 | struct netio_options *o = td->eo; | |
953 | struct addrinfo hints; | |
954 | int ret; | |
955 | ||
956 | if (inet_pton(af, host, dst)) | |
957 | return 0; | |
958 | ||
959 | memset(&hints, 0, sizeof(hints)); | |
0b783341 JA |
960 | |
961 | if (is_tcp(o)) | |
962 | hints.ai_socktype = SOCK_STREAM; | |
963 | else | |
964 | hints.ai_socktype = SOCK_DGRAM; | |
965 | ||
b1b1be2a JA |
966 | if (is_ipv6(o)) |
967 | hints.ai_family = AF_INET6; | |
968 | else | |
969 | hints.ai_family = AF_INET; | |
970 | ||
0b783341 JA |
971 | ret = getaddrinfo(host, NULL, &hints, res); |
972 | if (ret) { | |
973 | int e = EINVAL; | |
974 | char str[128]; | |
975 | ||
976 | if (ret == EAI_SYSTEM) | |
977 | e = errno; | |
978 | ||
979 | snprintf(str, sizeof(str), "getaddrinfo: %s", gai_strerror(ret)); | |
980 | td_verror(td, e, str); | |
981 | return 1; | |
982 | } | |
983 | ||
984 | return 0; | |
985 | } | |
986 | ||
0fd666bf JA |
987 | static int fio_netio_setup_connect_inet(struct thread_data *td, |
988 | const char *host, unsigned short port) | |
b5af8293 JA |
989 | { |
990 | struct netio_data *nd = td->io_ops->data; | |
49ccb8c1 | 991 | struct netio_options *o = td->eo; |
0b783341 JA |
992 | struct addrinfo *res = NULL; |
993 | void *dst, *src; | |
994 | int af, len; | |
b5af8293 | 995 | |
166dce4b JA |
996 | if (!host) { |
997 | log_err("fio: connect with no host to connect to.\n"); | |
998 | if (td_read(td)) | |
999 | log_err("fio: did you forget to set 'listen'?\n"); | |
1000 | ||
1001 | td_verror(td, EINVAL, "no hostname= set"); | |
1002 | return 1; | |
1003 | } | |
1004 | ||
0b783341 JA |
1005 | nd->addr.sin_family = AF_INET; |
1006 | nd->addr.sin_port = htons(port); | |
1007 | nd->addr6.sin6_family = AF_INET6; | |
1008 | nd->addr6.sin6_port = htons(port); | |
b5af8293 | 1009 | |
0b783341 JA |
1010 | if (is_ipv6(o)) { |
1011 | af = AF_INET6; | |
68631b36 | 1012 | dst = &nd->addr6.sin6_addr; |
49ccb8c1 | 1013 | } else { |
0b783341 | 1014 | af = AF_INET; |
68631b36 | 1015 | dst = &nd->addr.sin_addr; |
0b783341 | 1016 | } |
49ccb8c1 | 1017 | |
0b783341 JA |
1018 | if (fio_fill_addr(td, host, af, dst, &res)) |
1019 | return 1; | |
49ccb8c1 | 1020 | |
0b783341 JA |
1021 | if (!res) |
1022 | return 0; | |
49ccb8c1 | 1023 | |
0b783341 JA |
1024 | if (is_ipv6(o)) { |
1025 | len = sizeof(nd->addr6.sin6_addr); | |
1026 | src = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr; | |
1027 | } else { | |
1028 | len = sizeof(nd->addr.sin_addr); | |
1029 | src = &((struct sockaddr_in *) res->ai_addr)->sin_addr; | |
5fdd124a JA |
1030 | } |
1031 | ||
0b783341 JA |
1032 | memcpy(dst, src, len); |
1033 | freeaddrinfo(res); | |
5fdd124a JA |
1034 | return 0; |
1035 | } | |
1036 | ||
0fd666bf JA |
1037 | static int fio_netio_setup_connect_unix(struct thread_data *td, |
1038 | const char *path) | |
1039 | { | |
1040 | struct netio_data *nd = td->io_ops->data; | |
1041 | struct sockaddr_un *soun = &nd->addr_un; | |
1042 | ||
1043 | soun->sun_family = AF_UNIX; | |
4b159fa6 JA |
1044 | memset(soun->sun_path, 0, sizeof(soun->sun_path)); |
1045 | strncpy(soun->sun_path, path, sizeof(soun->sun_path) - 1); | |
0fd666bf JA |
1046 | return 0; |
1047 | } | |
1048 | ||
de890a1e | 1049 | static int fio_netio_setup_connect(struct thread_data *td) |
0fd666bf | 1050 | { |
de890a1e | 1051 | struct netio_options *o = td->eo; |
0fd666bf | 1052 | |
49ccb8c1 | 1053 | if (is_udp(o) || is_tcp(o)) |
de890a1e | 1054 | return fio_netio_setup_connect_inet(td, td->o.filename,o->port); |
0fd666bf | 1055 | else |
de890a1e | 1056 | return fio_netio_setup_connect_unix(td, td->o.filename); |
0fd666bf JA |
1057 | } |
1058 | ||
1059 | static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path) | |
1060 | { | |
1061 | struct netio_data *nd = td->io_ops->data; | |
1062 | struct sockaddr_un *addr = &nd->addr_un; | |
1063 | mode_t mode; | |
1064 | int len, fd; | |
1065 | ||
1066 | fd = socket(AF_UNIX, SOCK_STREAM, 0); | |
1067 | if (fd < 0) { | |
1068 | log_err("fio: socket: %s\n", strerror(errno)); | |
1069 | return -1; | |
1070 | } | |
1071 | ||
1072 | mode = umask(000); | |
1073 | ||
1074 | memset(addr, 0, sizeof(*addr)); | |
1075 | addr->sun_family = AF_UNIX; | |
4b159fa6 | 1076 | strncpy(addr->sun_path, path, sizeof(addr->sun_path) - 1); |
0fd666bf JA |
1077 | unlink(path); |
1078 | ||
1079 | len = sizeof(addr->sun_family) + strlen(path) + 1; | |
1080 | ||
1081 | if (bind(fd, (struct sockaddr *) addr, len) < 0) { | |
1082 | log_err("fio: bind: %s\n", strerror(errno)); | |
b94cba47 | 1083 | close(fd); |
0fd666bf JA |
1084 | return -1; |
1085 | } | |
1086 | ||
1087 | umask(mode); | |
1088 | nd->listenfd = fd; | |
1089 | return 0; | |
1090 | } | |
1091 | ||
1092 | static int fio_netio_setup_listen_inet(struct thread_data *td, short port) | |
ed92ac0c | 1093 | { |
b5af8293 | 1094 | struct netio_data *nd = td->io_ops->data; |
de890a1e | 1095 | struct netio_options *o = td->eo; |
b511c9aa SB |
1096 | struct ip_mreq mr; |
1097 | struct sockaddr_in sin; | |
49ccb8c1 JA |
1098 | struct sockaddr *saddr; |
1099 | int fd, opt, type, domain; | |
1100 | socklen_t len; | |
ed92ac0c | 1101 | |
b511c9aa | 1102 | memset(&sin, 0, sizeof(sin)); |
49ccb8c1 JA |
1103 | |
1104 | if (o->proto == FIO_TYPE_TCP) { | |
414c2a3e | 1105 | type = SOCK_STREAM; |
49ccb8c1 JA |
1106 | domain = AF_INET; |
1107 | } else if (o->proto == FIO_TYPE_TCP_V6) { | |
1108 | type = SOCK_STREAM; | |
1109 | domain = AF_INET6; | |
1110 | } else if (o->proto == FIO_TYPE_UDP) { | |
414c2a3e | 1111 | type = SOCK_DGRAM; |
49ccb8c1 JA |
1112 | domain = AF_INET; |
1113 | } else if (o->proto == FIO_TYPE_UDP_V6) { | |
1114 | type = SOCK_DGRAM; | |
1115 | domain = AF_INET6; | |
1116 | } else { | |
1117 | log_err("fio: unknown proto %d\n", o->proto); | |
1118 | return 1; | |
1119 | } | |
414c2a3e | 1120 | |
49ccb8c1 | 1121 | fd = socket(domain, type, 0); |
ed92ac0c | 1122 | if (fd < 0) { |
e1161c32 | 1123 | td_verror(td, errno, "socket"); |
ed92ac0c JA |
1124 | return 1; |
1125 | } | |
1126 | ||
1127 | opt = 1; | |
26e594a5 | 1128 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) { |
e1161c32 | 1129 | td_verror(td, errno, "setsockopt"); |
4a93dec2 | 1130 | close(fd); |
ed92ac0c JA |
1131 | return 1; |
1132 | } | |
6bedbfaf | 1133 | #ifdef SO_REUSEPORT |
26e594a5 | 1134 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) { |
e1161c32 | 1135 | td_verror(td, errno, "setsockopt"); |
4a93dec2 | 1136 | close(fd); |
6bedbfaf JA |
1137 | return 1; |
1138 | } | |
1139 | #endif | |
ed92ac0c | 1140 | |
1008602c JA |
1141 | if (set_window_size(td, fd)) { |
1142 | close(fd); | |
1143 | return 1; | |
1144 | } | |
e5f34d95 JA |
1145 | if (set_mss(td, fd)) { |
1146 | close(fd); | |
1147 | return 1; | |
1148 | } | |
1008602c | 1149 | |
6611e9c7 | 1150 | if (td->o.filename) { |
49ccb8c1 | 1151 | if (!is_udp(o) || !fio_netio_is_multicast(td->o.filename)) { |
b511c9aa SB |
1152 | log_err("fio: hostname not valid for non-multicast inbound network IO\n"); |
1153 | close(fd); | |
1154 | return 1; | |
1155 | } | |
6611e9c7 JA |
1156 | if (is_ipv6(o)) { |
1157 | log_err("fio: IPv6 not supported for multicast network IO"); | |
1158 | close(fd); | |
1159 | return 1; | |
1160 | } | |
b511c9aa SB |
1161 | |
1162 | inet_aton(td->o.filename, &sin.sin_addr); | |
1163 | ||
1164 | mr.imr_multiaddr = sin.sin_addr; | |
f16b7405 BC |
1165 | if (o->intfc) { |
1166 | if (inet_aton(o->intfc, &mr.imr_interface) == 0) { | |
b93b6a2e SB |
1167 | log_err("fio: interface not valid interface IP\n"); |
1168 | close(fd); | |
1169 | return 1; | |
1170 | } | |
1171 | } else { | |
1172 | mr.imr_interface.s_addr = htonl(INADDR_ANY); | |
1173 | } | |
6611e9c7 | 1174 | |
f16b7405 | 1175 | if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mr, sizeof(mr)) < 0) { |
b511c9aa SB |
1176 | td_verror(td, errno, "setsockopt IP_ADD_MEMBERSHIP"); |
1177 | close(fd); | |
1178 | return 1; | |
1179 | } | |
1180 | } | |
1181 | ||
49ccb8c1 JA |
1182 | if (!is_ipv6(o)) { |
1183 | saddr = (struct sockaddr *) &nd->addr; | |
1184 | len = sizeof(nd->addr); | |
1185 | ||
1186 | nd->addr.sin_family = AF_INET; | |
1187 | nd->addr.sin_addr.s_addr = sin.sin_addr.s_addr ? sin.sin_addr.s_addr : htonl(INADDR_ANY); | |
1188 | nd->addr.sin_port = htons(port); | |
1189 | } else { | |
1190 | saddr = (struct sockaddr *) &nd->addr6; | |
1191 | len = sizeof(nd->addr6); | |
1192 | ||
1193 | nd->addr6.sin6_family = AF_INET6; | |
66f7a56f | 1194 | nd->addr6.sin6_addr = in6addr_any; |
49ccb8c1 JA |
1195 | nd->addr6.sin6_port = htons(port); |
1196 | } | |
ed92ac0c | 1197 | |
49ccb8c1 | 1198 | if (bind(fd, saddr, len) < 0) { |
d19cedd6 | 1199 | close(fd); |
e1161c32 | 1200 | td_verror(td, errno, "bind"); |
ed92ac0c JA |
1201 | return 1; |
1202 | } | |
0fd666bf JA |
1203 | |
1204 | nd->listenfd = fd; | |
1205 | return 0; | |
1206 | } | |
1207 | ||
de890a1e | 1208 | static int fio_netio_setup_listen(struct thread_data *td) |
0fd666bf JA |
1209 | { |
1210 | struct netio_data *nd = td->io_ops->data; | |
de890a1e | 1211 | struct netio_options *o = td->eo; |
0fd666bf JA |
1212 | int ret; |
1213 | ||
49ccb8c1 | 1214 | if (is_udp(o) || is_tcp(o)) |
de890a1e | 1215 | ret = fio_netio_setup_listen_inet(td, o->port); |
0fd666bf | 1216 | else |
de890a1e | 1217 | ret = fio_netio_setup_listen_unix(td, td->o.filename); |
0fd666bf JA |
1218 | |
1219 | if (ret) | |
1220 | return ret; | |
49ccb8c1 | 1221 | if (is_udp(o)) |
0fd666bf JA |
1222 | return 0; |
1223 | ||
1224 | if (listen(nd->listenfd, 10) < 0) { | |
e1161c32 | 1225 | td_verror(td, errno, "listen"); |
0fd666bf | 1226 | nd->listenfd = -1; |
ed92ac0c JA |
1227 | return 1; |
1228 | } | |
1229 | ||
b5af8293 | 1230 | return 0; |
ed92ac0c JA |
1231 | } |
1232 | ||
9bec88e1 | 1233 | static int fio_netio_init(struct thread_data *td) |
ed92ac0c | 1234 | { |
de890a1e | 1235 | struct netio_options *o = td->eo; |
af52b345 | 1236 | int ret; |
ed92ac0c | 1237 | |
3f457bea BC |
1238 | #ifdef WIN32 |
1239 | WSADATA wsd; | |
1240 | WSAStartup(MAKEWORD(2,2), &wsd); | |
1241 | #endif | |
1242 | ||
16d55aae JA |
1243 | if (td_random(td)) { |
1244 | log_err("fio: network IO can't be random\n"); | |
1245 | return 1; | |
1246 | } | |
ed92ac0c | 1247 | |
de890a1e SL |
1248 | if (o->proto == FIO_TYPE_UNIX && o->port) { |
1249 | log_err("fio: network IO port not valid with unix socket\n"); | |
1250 | return 1; | |
1251 | } else if (o->proto != FIO_TYPE_UNIX && !o->port) { | |
1252 | log_err("fio: network IO requires port for tcp or udp\n"); | |
1253 | return 1; | |
1254 | } | |
ed92ac0c | 1255 | |
49ccb8c1 | 1256 | if (!is_tcp(o)) { |
de890a1e | 1257 | if (o->listen) { |
9b986065 JA |
1258 | log_err("fio: listen only valid for TCP proto IO\n"); |
1259 | return 1; | |
de890a1e SL |
1260 | } |
1261 | if (td_rw(td)) { | |
9b986065 | 1262 | log_err("fio: datagram network connections must be" |
de890a1e | 1263 | " read OR write\n"); |
9b986065 JA |
1264 | return 1; |
1265 | } | |
1266 | if (o->proto == FIO_TYPE_UNIX && !td->o.filename) { | |
1267 | log_err("fio: UNIX sockets need host/filename\n"); | |
1268 | return 1; | |
de890a1e SL |
1269 | } |
1270 | o->listen = td_read(td); | |
1271 | } | |
443662ef | 1272 | |
de890a1e SL |
1273 | if (o->listen) |
1274 | ret = fio_netio_setup_listen(td); | |
0fd666bf | 1275 | else |
de890a1e | 1276 | ret = fio_netio_setup_connect(td); |
ed92ac0c | 1277 | |
7bb48f84 | 1278 | return ret; |
ed92ac0c JA |
1279 | } |
1280 | ||
b5af8293 | 1281 | static void fio_netio_cleanup(struct thread_data *td) |
9bec88e1 | 1282 | { |
b5af8293 JA |
1283 | struct netio_data *nd = td->io_ops->data; |
1284 | ||
1285 | if (nd) { | |
64b24cd8 JA |
1286 | if (nd->listenfd != -1) |
1287 | close(nd->listenfd); | |
1288 | if (nd->pipes[0] != -1) | |
1289 | close(nd->pipes[0]); | |
1290 | if (nd->pipes[1] != -1) | |
1291 | close(nd->pipes[1]); | |
1292 | ||
b5af8293 | 1293 | free(nd); |
b5af8293 JA |
1294 | } |
1295 | } | |
1296 | ||
1297 | static int fio_netio_setup(struct thread_data *td) | |
1298 | { | |
7bb48f84 | 1299 | struct netio_data *nd; |
7bb48f84 | 1300 | |
de890a1e | 1301 | if (!td->files_index) { |
5903e7b7 | 1302 | add_file(td, td->o.filename ?: "net", 0, 0); |
de890a1e | 1303 | td->o.nr_files = td->o.nr_files ?: 1; |
b53f2c54 | 1304 | td->o.open_files++; |
de890a1e SL |
1305 | } |
1306 | ||
7bb48f84 JA |
1307 | if (!td->io_ops->data) { |
1308 | nd = malloc(sizeof(*nd));; | |
1309 | ||
1310 | memset(nd, 0, sizeof(*nd)); | |
1311 | nd->listenfd = -1; | |
64b24cd8 | 1312 | nd->pipes[0] = nd->pipes[1] = -1; |
7bb48f84 | 1313 | td->io_ops->data = nd; |
7bb48f84 | 1314 | } |
b5af8293 | 1315 | |
9bec88e1 JA |
1316 | return 0; |
1317 | } | |
1318 | ||
36d80bc7 JA |
1319 | static void fio_netio_terminate(struct thread_data *td) |
1320 | { | |
c5dd6d89 | 1321 | kill(td->pid, SIGTERM); |
36d80bc7 JA |
1322 | } |
1323 | ||
67bf9823 | 1324 | #ifdef CONFIG_LINUX_SPLICE |
9cce02e8 JA |
1325 | static int fio_netio_setup_splice(struct thread_data *td) |
1326 | { | |
1327 | struct netio_data *nd; | |
1328 | ||
1329 | fio_netio_setup(td); | |
1330 | ||
1331 | nd = td->io_ops->data; | |
1332 | if (nd) { | |
1333 | if (pipe(nd->pipes) < 0) | |
1334 | return 1; | |
1335 | ||
1336 | nd->use_splice = 1; | |
1337 | return 0; | |
1338 | } | |
1339 | ||
1340 | return 1; | |
1341 | } | |
1342 | ||
5921e80c | 1343 | static struct ioengine_ops ioengine_splice = { |
de890a1e SL |
1344 | .name = "netsplice", |
1345 | .version = FIO_IOOPS_VERSION, | |
1346 | .prep = fio_netio_prep, | |
1347 | .queue = fio_netio_queue, | |
1348 | .setup = fio_netio_setup_splice, | |
1349 | .init = fio_netio_init, | |
1350 | .cleanup = fio_netio_cleanup, | |
1351 | .open_file = fio_netio_open_file, | |
36d80bc7 JA |
1352 | .close_file = fio_netio_close_file, |
1353 | .terminate = fio_netio_terminate, | |
de890a1e SL |
1354 | .options = options, |
1355 | .option_struct_size = sizeof(struct netio_options), | |
1356 | .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR | | |
36d80bc7 | 1357 | FIO_PIPEIO, |
ed92ac0c | 1358 | }; |
5921e80c | 1359 | #endif |
ed92ac0c | 1360 | |
5921e80c | 1361 | static struct ioengine_ops ioengine_rw = { |
de890a1e SL |
1362 | .name = "net", |
1363 | .version = FIO_IOOPS_VERSION, | |
1364 | .prep = fio_netio_prep, | |
1365 | .queue = fio_netio_queue, | |
1366 | .setup = fio_netio_setup, | |
1367 | .init = fio_netio_init, | |
1368 | .cleanup = fio_netio_cleanup, | |
1369 | .open_file = fio_netio_open_file, | |
1370 | .close_file = fio_netio_close_file, | |
36d80bc7 | 1371 | .terminate = fio_netio_terminate, |
de890a1e SL |
1372 | .options = options, |
1373 | .option_struct_size = sizeof(struct netio_options), | |
1374 | .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR | | |
ad705bcb | 1375 | FIO_PIPEIO | FIO_BIT_BASED, |
9cce02e8 JA |
1376 | }; |
1377 | ||
de890a1e SL |
1378 | static int str_hostname_cb(void *data, const char *input) |
1379 | { | |
1380 | struct netio_options *o = data; | |
1381 | ||
1382 | if (o->td->o.filename) | |
1383 | free(o->td->o.filename); | |
1384 | o->td->o.filename = strdup(input); | |
1385 | return 0; | |
1386 | } | |
1387 | ||
ed92ac0c JA |
1388 | static void fio_init fio_netio_register(void) |
1389 | { | |
9cce02e8 | 1390 | register_ioengine(&ioengine_rw); |
67bf9823 | 1391 | #ifdef CONFIG_LINUX_SPLICE |
9cce02e8 | 1392 | register_ioengine(&ioengine_splice); |
5921e80c | 1393 | #endif |
ed92ac0c JA |
1394 | } |
1395 | ||
1396 | static void fio_exit fio_netio_unregister(void) | |
1397 | { | |
9cce02e8 | 1398 | unregister_ioengine(&ioengine_rw); |
67bf9823 | 1399 | #ifdef CONFIG_LINUX_SPLICE |
9cce02e8 | 1400 | unregister_ioengine(&ioengine_splice); |
5921e80c | 1401 | #endif |
ed92ac0c | 1402 | } |