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