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