Commit | Line | Data |
---|---|---|
6364a07a | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2012 David Goulet <dgoulet@efficios.com> |
6364a07a | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
6364a07a | 5 | * |
6364a07a DG |
6 | */ |
7 | ||
6c1c0768 | 8 | #define _LGPL_SOURCE |
64efa44e | 9 | #include <algorithm> |
6364a07a DG |
10 | #include <limits.h> |
11 | #include <stdio.h> | |
12 | #include <stdlib.h> | |
13 | #include <string.h> | |
14 | #include <sys/stat.h> | |
15 | #include <sys/types.h> | |
16 | #include <unistd.h> | |
a655f4cf | 17 | #include <fcntl.h> |
389fbf04 | 18 | #include <common/compat/time.h> |
a655f4cf | 19 | #include <poll.h> |
6364a07a | 20 | |
90e535ef | 21 | #include <common/common.h> |
395d6b02 | 22 | #include <common/time.h> |
edf4b93e | 23 | #include <common/compat/errno.h> |
6364a07a DG |
24 | |
25 | #include "inet.h" | |
26 | ||
a655f4cf MD |
27 | #define RECONNECT_DELAY 200 /* ms */ |
28 | ||
6364a07a DG |
29 | /* |
30 | * INET protocol operations. | |
31 | */ | |
32 | static const struct lttcomm_proto_ops inet_ops = { | |
33 | .bind = lttcomm_bind_inet_sock, | |
34 | .close = lttcomm_close_inet_sock, | |
35 | .connect = lttcomm_connect_inet_sock, | |
36 | .accept = lttcomm_accept_inet_sock, | |
37 | .listen = lttcomm_listen_inet_sock, | |
38 | .recvmsg = lttcomm_recvmsg_inet_sock, | |
39 | .sendmsg = lttcomm_sendmsg_inet_sock, | |
40 | }; | |
41 | ||
d831c249 DG |
42 | unsigned long lttcomm_inet_tcp_timeout; |
43 | ||
6364a07a DG |
44 | /* |
45 | * Creates an PF_INET socket. | |
46 | */ | |
47 | int lttcomm_create_inet_sock(struct lttcomm_sock *sock, int type, int proto) | |
48 | { | |
de5e9086 | 49 | int val = 1, ret; |
783a3b9a | 50 | unsigned long timeout; |
6364a07a DG |
51 | |
52 | /* Create server socket */ | |
53 | if ((sock->fd = socket(PF_INET, type, proto)) < 0) { | |
54 | PERROR("socket inet"); | |
55 | goto error; | |
56 | } | |
57 | ||
58 | sock->ops = &inet_ops; | |
59 | ||
60 | /* | |
61 | * Set socket option to reuse the address. | |
62 | */ | |
63 | ret = setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(int)); | |
64 | if (ret < 0) { | |
65 | PERROR("setsockopt inet"); | |
66 | goto error; | |
67 | } | |
783a3b9a MD |
68 | timeout = lttcomm_get_network_timeout(); |
69 | if (timeout) { | |
70 | ret = lttcomm_setsockopt_rcv_timeout(sock->fd, timeout); | |
71 | if (ret) { | |
72 | goto error; | |
73 | } | |
74 | ret = lttcomm_setsockopt_snd_timeout(sock->fd, timeout); | |
75 | if (ret) { | |
76 | goto error; | |
77 | } | |
78 | } | |
6364a07a DG |
79 | |
80 | return 0; | |
81 | ||
82 | error: | |
83 | return -1; | |
84 | } | |
85 | ||
86 | /* | |
87 | * Bind socket and return. | |
88 | */ | |
89 | int lttcomm_bind_inet_sock(struct lttcomm_sock *sock) | |
90 | { | |
7966af57 SM |
91 | struct sockaddr_in sockaddr = sock->sockaddr.addr.sin; |
92 | ||
64efa44e | 93 | return bind(sock->fd, (struct sockaddr *) &sockaddr, sizeof(sockaddr)); |
6364a07a DG |
94 | } |
95 | ||
a655f4cf MD |
96 | static |
97 | int connect_no_timeout(struct lttcomm_sock *sock) | |
98 | { | |
7966af57 SM |
99 | struct sockaddr_in sockaddr = sock->sockaddr.addr.sin; |
100 | ||
64efa44e | 101 | return connect(sock->fd, (struct sockaddr *) &sockaddr, sizeof(sockaddr)); |
a655f4cf MD |
102 | } |
103 | ||
a655f4cf MD |
104 | static |
105 | int connect_with_timeout(struct lttcomm_sock *sock) | |
106 | { | |
107 | unsigned long timeout = lttcomm_get_network_timeout(); | |
108 | int ret, flags, connect_ret; | |
109 | struct timespec orig_time, cur_time; | |
2daf6502 | 110 | unsigned long diff_ms; |
7966af57 | 111 | struct sockaddr_in sockaddr; |
a655f4cf MD |
112 | |
113 | ret = fcntl(sock->fd, F_GETFL, 0); | |
114 | if (ret == -1) { | |
115 | PERROR("fcntl"); | |
116 | return -1; | |
117 | } | |
118 | flags = ret; | |
119 | ||
120 | /* Set socket to nonblock */ | |
121 | ret = fcntl(sock->fd, F_SETFL, flags | O_NONBLOCK); | |
122 | if (ret == -1) { | |
123 | PERROR("fcntl"); | |
124 | return -1; | |
125 | } | |
126 | ||
389fbf04 | 127 | ret = lttng_clock_gettime(CLOCK_MONOTONIC, &orig_time); |
a655f4cf MD |
128 | if (ret == -1) { |
129 | PERROR("clock_gettime"); | |
130 | return -1; | |
131 | } | |
132 | ||
7966af57 | 133 | sockaddr = sock->sockaddr.addr.sin; |
64efa44e | 134 | connect_ret = connect(sock->fd, (struct sockaddr *) &sockaddr, sizeof(sockaddr)); |
1a65bbb8 JG |
135 | if (connect_ret == -1 && errno != EAGAIN && errno != EWOULDBLOCK && |
136 | errno != EINPROGRESS) { | |
a655f4cf MD |
137 | goto error; |
138 | } else if (!connect_ret) { | |
139 | /* Connect succeeded */ | |
140 | goto success; | |
141 | } | |
142 | ||
48ac9596 JR |
143 | DBG("Asynchronous connect for sock %d, performing polling with" |
144 | " timeout: %lums", sock->fd, timeout); | |
a655f4cf MD |
145 | /* |
146 | * Perform poll loop following EINPROGRESS recommendation from | |
147 | * connect(2) man page. | |
148 | */ | |
149 | do { | |
150 | struct pollfd fds; | |
151 | ||
152 | fds.fd = sock->fd; | |
153 | fds.events = POLLOUT; | |
154 | fds.revents = 0; | |
155 | ret = poll(&fds, 1, RECONNECT_DELAY); | |
156 | if (ret < 0) { | |
157 | goto error; | |
158 | } else if (ret > 0) { | |
159 | int optval; | |
160 | socklen_t optval_len = sizeof(optval); | |
161 | ||
162 | if (!(fds.revents & POLLOUT)) { | |
163 | /* Either hup or error */ | |
164 | errno = EPIPE; | |
165 | goto error; | |
166 | } | |
167 | /* got something */ | |
168 | ret = getsockopt(sock->fd, SOL_SOCKET, | |
169 | SO_ERROR, &optval, &optval_len); | |
170 | if (ret) { | |
48ac9596 | 171 | PERROR("getsockopt"); |
a655f4cf MD |
172 | goto error; |
173 | } | |
174 | if (!optval) { | |
175 | connect_ret = 0; | |
176 | goto success; | |
177 | } else { | |
48ac9596 JR |
178 | /* Get actual connect() errno from opt_val */ |
179 | errno = optval; | |
a655f4cf MD |
180 | goto error; |
181 | } | |
182 | } | |
183 | /* ret == 0: timeout */ | |
389fbf04 | 184 | ret = lttng_clock_gettime(CLOCK_MONOTONIC, &cur_time); |
a655f4cf MD |
185 | if (ret == -1) { |
186 | PERROR("clock_gettime"); | |
187 | connect_ret = ret; | |
188 | goto error; | |
189 | } | |
2daf6502 MD |
190 | if (timespec_to_ms(timespec_abs_diff(cur_time, orig_time), &diff_ms) < 0) { |
191 | ERR("timespec_to_ms input overflows milliseconds output"); | |
192 | connect_ret = -1; | |
193 | goto error; | |
194 | } | |
195 | } while (diff_ms < timeout); | |
a655f4cf MD |
196 | |
197 | /* Timeout */ | |
198 | errno = ETIMEDOUT; | |
199 | connect_ret = -1; | |
200 | ||
201 | success: | |
202 | /* Restore initial flags */ | |
203 | ret = fcntl(sock->fd, F_SETFL, flags); | |
204 | if (ret == -1) { | |
205 | PERROR("fcntl"); | |
206 | /* Continue anyway */ | |
207 | } | |
208 | error: | |
209 | return connect_ret; | |
210 | } | |
211 | ||
6364a07a DG |
212 | /* |
213 | * Connect PF_INET socket. | |
214 | */ | |
215 | int lttcomm_connect_inet_sock(struct lttcomm_sock *sock) | |
216 | { | |
217 | int ret, closeret; | |
218 | ||
a655f4cf MD |
219 | if (lttcomm_get_network_timeout()) { |
220 | ret = connect_with_timeout(sock); | |
221 | } else { | |
222 | ret = connect_no_timeout(sock); | |
223 | } | |
6364a07a | 224 | if (ret < 0) { |
82c05d47 | 225 | PERROR("connect"); |
6364a07a DG |
226 | goto error_connect; |
227 | } | |
228 | ||
229 | return ret; | |
230 | ||
231 | error_connect: | |
232 | closeret = close(sock->fd); | |
233 | if (closeret) { | |
234 | PERROR("close inet"); | |
235 | } | |
236 | ||
237 | return ret; | |
238 | } | |
239 | ||
240 | /* | |
241 | * Do an accept(2) on the sock and return the new lttcomm socket. The socket | |
242 | * MUST be bind(2) before. | |
243 | */ | |
244 | struct lttcomm_sock *lttcomm_accept_inet_sock(struct lttcomm_sock *sock) | |
245 | { | |
246 | int new_fd; | |
88a5db70 | 247 | socklen_t len; |
6364a07a | 248 | struct lttcomm_sock *new_sock; |
71c648d8 | 249 | unsigned long timeout; |
1a65bbb8 | 250 | struct sockaddr_in new_addr = {}; |
6364a07a DG |
251 | |
252 | if (sock->proto == LTTCOMM_SOCK_UDP) { | |
253 | /* | |
254 | * accept(2) does not exist for UDP so simply return the passed socket. | |
255 | */ | |
256 | new_sock = sock; | |
257 | goto end; | |
258 | } | |
259 | ||
de5e9086 | 260 | new_sock = lttcomm_alloc_sock(sock->proto); |
6364a07a DG |
261 | if (new_sock == NULL) { |
262 | goto error; | |
263 | } | |
264 | ||
1a65bbb8 | 265 | len = sizeof(new_addr); |
88a5db70 | 266 | |
6364a07a | 267 | /* Blocking call */ |
1a65bbb8 | 268 | new_fd = accept(sock->fd, (struct sockaddr *) &new_addr, &len); |
6364a07a DG |
269 | if (new_fd < 0) { |
270 | PERROR("accept inet"); | |
271 | goto error; | |
272 | } | |
1a65bbb8 | 273 | new_sock->sockaddr.addr.sin = new_addr; |
71c648d8 JG |
274 | timeout = lttcomm_get_network_timeout(); |
275 | if (timeout) { | |
276 | int ret; | |
277 | ||
278 | ret = lttcomm_setsockopt_rcv_timeout(new_fd, timeout); | |
279 | if (ret) { | |
475cd9fa | 280 | goto error_close; |
71c648d8 JG |
281 | } |
282 | ret = lttcomm_setsockopt_snd_timeout(new_fd, timeout); | |
283 | if (ret) { | |
475cd9fa | 284 | goto error_close; |
71c648d8 JG |
285 | } |
286 | } | |
6364a07a DG |
287 | |
288 | new_sock->fd = new_fd; | |
de5e9086 | 289 | new_sock->ops = &inet_ops; |
6364a07a DG |
290 | |
291 | end: | |
292 | return new_sock; | |
293 | ||
475cd9fa DG |
294 | error_close: |
295 | if (close(new_fd) < 0) { | |
296 | PERROR("accept inet close fd"); | |
297 | } | |
298 | ||
6364a07a DG |
299 | error: |
300 | free(new_sock); | |
301 | return NULL; | |
302 | } | |
303 | ||
304 | /* | |
305 | * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN. | |
306 | */ | |
307 | int lttcomm_listen_inet_sock(struct lttcomm_sock *sock, int backlog) | |
308 | { | |
309 | int ret; | |
310 | ||
311 | if (sock->proto == LTTCOMM_SOCK_UDP) { | |
312 | /* listen(2) does not exist for UDP so simply return success. */ | |
313 | ret = 0; | |
314 | goto end; | |
315 | } | |
316 | ||
317 | /* Default listen backlog */ | |
318 | if (backlog <= 0) { | |
319 | backlog = LTTNG_SESSIOND_COMM_MAX_LISTEN; | |
320 | } | |
321 | ||
322 | ret = listen(sock->fd, backlog); | |
323 | if (ret < 0) { | |
324 | PERROR("listen inet"); | |
325 | } | |
326 | ||
327 | end: | |
328 | return ret; | |
329 | } | |
330 | ||
331 | /* | |
332 | * Receive data of size len in put that data into the buf param. Using recvmsg | |
333 | * API. | |
334 | * | |
335 | * Return the size of received data. | |
336 | */ | |
337 | ssize_t lttcomm_recvmsg_inet_sock(struct lttcomm_sock *sock, void *buf, | |
338 | size_t len, int flags) | |
339 | { | |
340 | struct msghdr msg; | |
341 | struct iovec iov[1]; | |
342 | ssize_t ret = -1; | |
7c5aef62 | 343 | size_t len_last; |
1a65bbb8 | 344 | struct sockaddr_in addr = sock->sockaddr.addr.sin; |
6364a07a DG |
345 | |
346 | memset(&msg, 0, sizeof(msg)); | |
347 | ||
348 | iov[0].iov_base = buf; | |
349 | iov[0].iov_len = len; | |
350 | msg.msg_iov = iov; | |
351 | msg.msg_iovlen = 1; | |
352 | ||
1a65bbb8 | 353 | msg.msg_name = (struct sockaddr *) &addr; |
6364a07a DG |
354 | msg.msg_namelen = sizeof(sock->sockaddr.addr.sin); |
355 | ||
6364a07a | 356 | do { |
7c5aef62 | 357 | len_last = iov[0].iov_len; |
6364a07a | 358 | ret = recvmsg(sock->fd, &msg, flags); |
7c5aef62 | 359 | if (ret > 0) { |
5312a3ed JG |
360 | if (flags & MSG_DONTWAIT) { |
361 | goto end; | |
362 | } | |
64efa44e | 363 | iov[0].iov_base = ((char *) iov[0].iov_base) + ret; |
7c5aef62 | 364 | iov[0].iov_len -= ret; |
a0377dfe | 365 | LTTNG_ASSERT(ret <= len_last); |
7c5aef62 DG |
366 | } |
367 | } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR)); | |
5312a3ed | 368 | |
6364a07a | 369 | if (ret < 0) { |
c2e8c366 JG |
370 | if (errno == EAGAIN && flags & MSG_DONTWAIT) { |
371 | /* | |
372 | * EAGAIN is expected in non-blocking mode and should | |
373 | * not be reported as an error. Moreover, if no data | |
374 | * was read, 0 must not be returned as it would be | |
375 | * interpreted as an orderly shutdown of the socket. | |
376 | */ | |
377 | goto end; | |
378 | } | |
6364a07a | 379 | PERROR("recvmsg inet"); |
7c5aef62 DG |
380 | } else if (ret > 0) { |
381 | ret = len; | |
6364a07a | 382 | } |
7c5aef62 | 383 | /* Else ret = 0 meaning an orderly shutdown. */ |
5312a3ed | 384 | end: |
6364a07a DG |
385 | return ret; |
386 | } | |
387 | ||
388 | /* | |
389 | * Send buf data of size len. Using sendmsg API. | |
390 | * | |
391 | * Return the size of sent data. | |
392 | */ | |
c2d69327 | 393 | ssize_t lttcomm_sendmsg_inet_sock(struct lttcomm_sock *sock, const void *buf, |
6364a07a DG |
394 | size_t len, int flags) |
395 | { | |
396 | struct msghdr msg; | |
397 | struct iovec iov[1]; | |
398 | ssize_t ret = -1; | |
399 | ||
400 | memset(&msg, 0, sizeof(msg)); | |
401 | ||
c2d69327 | 402 | iov[0].iov_base = (void *) buf; |
6364a07a DG |
403 | iov[0].iov_len = len; |
404 | msg.msg_iov = iov; | |
405 | msg.msg_iovlen = 1; | |
406 | ||
407 | switch (sock->proto) { | |
408 | case LTTCOMM_SOCK_UDP: | |
1a65bbb8 JG |
409 | { |
410 | struct sockaddr_in addr = sock->sockaddr.addr.sin; | |
411 | ||
412 | msg.msg_name = (struct sockaddr *) &addr; | |
6364a07a DG |
413 | msg.msg_namelen = sizeof(sock->sockaddr.addr.sin); |
414 | break; | |
1a65bbb8 | 415 | } |
6364a07a DG |
416 | default: |
417 | break; | |
418 | } | |
419 | ||
420 | do { | |
421 | ret = sendmsg(sock->fd, &msg, flags); | |
422 | } while (ret < 0 && errno == EINTR); | |
423 | if (ret < 0) { | |
424 | /* | |
425 | * Only warn about EPIPE when quiet mode is deactivated. | |
426 | * We consider EPIPE as expected. | |
427 | */ | |
428 | if (errno != EPIPE || !lttng_opt_quiet) { | |
429 | PERROR("sendmsg inet"); | |
430 | } | |
431 | } | |
432 | ||
433 | return ret; | |
434 | } | |
435 | ||
436 | /* | |
437 | * Shutdown cleanly and close. | |
438 | */ | |
439 | int lttcomm_close_inet_sock(struct lttcomm_sock *sock) | |
440 | { | |
6e742359 | 441 | int ret; |
6364a07a | 442 | |
de5e9086 | 443 | /* Don't try to close an invalid marked socket */ |
6364a07a DG |
444 | if (sock->fd == -1) { |
445 | return 0; | |
446 | } | |
447 | ||
6e742359 DG |
448 | ret = close(sock->fd); |
449 | if (ret) { | |
6364a07a DG |
450 | PERROR("close inet"); |
451 | } | |
452 | ||
453 | /* Mark socket */ | |
454 | sock->fd = -1; | |
455 | ||
456 | return ret; | |
457 | } | |
d831c249 DG |
458 | |
459 | /* | |
460 | * Return value read from /proc or else 0 if value is not found. | |
461 | */ | |
462 | static unsigned long read_proc_value(const char *path) | |
463 | { | |
464 | int ret, fd; | |
6cd525e8 | 465 | ssize_t size_ret; |
d831c249 DG |
466 | long r_val; |
467 | unsigned long val = 0; | |
468 | char buf[64]; | |
469 | ||
470 | fd = open(path, O_RDONLY); | |
471 | if (fd < 0) { | |
472 | goto error; | |
473 | } | |
474 | ||
6cd525e8 MD |
475 | size_ret = lttng_read(fd, buf, sizeof(buf)); |
476 | /* | |
477 | * Allow reading a file smaller than buf, but keep space for | |
478 | * final \0. | |
479 | */ | |
480 | if (size_ret < 0 || size_ret >= sizeof(buf)) { | |
d831c249 DG |
481 | PERROR("read proc failed"); |
482 | goto error_close; | |
483 | } | |
6cd525e8 | 484 | buf[size_ret] = '\0'; |
d831c249 DG |
485 | |
486 | errno = 0; | |
487 | r_val = strtol(buf, NULL, 10); | |
488 | if (errno != 0 || r_val < -1L) { | |
489 | val = 0; | |
490 | goto error_close; | |
491 | } else { | |
492 | if (r_val > 0) { | |
493 | val = r_val; | |
494 | } | |
495 | } | |
496 | ||
497 | error_close: | |
498 | ret = close(fd); | |
499 | if (ret) { | |
500 | PERROR("close /proc value"); | |
501 | } | |
502 | error: | |
503 | return val; | |
504 | } | |
505 | ||
d831c249 DG |
506 | void lttcomm_inet_init(void) |
507 | { | |
18eed43f DG |
508 | unsigned long syn_retries, fin_timeout, syn_timeout, env; |
509 | ||
510 | env = lttcomm_get_network_timeout(); | |
511 | if (env) { | |
512 | lttcomm_inet_tcp_timeout = env; | |
513 | goto end; | |
514 | } | |
d831c249 DG |
515 | |
516 | /* Assign default value and see if we can change it. */ | |
517 | lttcomm_inet_tcp_timeout = DEFAULT_INET_TCP_TIMEOUT; | |
518 | ||
519 | syn_retries = read_proc_value(LTTCOMM_INET_PROC_SYN_RETRIES_PATH); | |
520 | fin_timeout = read_proc_value(LTTCOMM_INET_PROC_FIN_TIMEOUT_PATH); | |
521 | ||
522 | syn_timeout = syn_retries * LTTCOMM_INET_SYN_TIMEOUT_FACTOR; | |
523 | ||
524 | /* | |
525 | * Get the maximum between the two possible timeout value and use that to | |
526 | * get the maximum with the default timeout. | |
527 | */ | |
64efa44e | 528 | lttcomm_inet_tcp_timeout = std::max(std::max(syn_timeout, fin_timeout), |
d831c249 DG |
529 | lttcomm_inet_tcp_timeout); |
530 | ||
18eed43f | 531 | end: |
d831c249 DG |
532 | DBG("TCP inet operation timeout set to %lu sec", lttcomm_inet_tcp_timeout); |
533 | } |