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