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