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