2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
15 #include <sys/types.h>
18 #include <common/compat/time.hpp>
21 #include <common/common.hpp>
22 #include <common/time.hpp>
23 #include <common/compat/errno.hpp>
27 #define RECONNECT_DELAY 200 /* ms */
30 * INET protocol operations.
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
,
42 unsigned long lttcomm_inet_tcp_timeout
;
45 * Creates an PF_INET socket.
47 int lttcomm_create_inet_sock(struct lttcomm_sock
*sock
, int type
, int proto
)
50 unsigned long timeout
;
52 /* Create server socket */
53 if ((sock
->fd
= socket(PF_INET
, type
, proto
)) < 0) {
54 PERROR("socket inet");
58 sock
->ops
= &inet_ops
;
61 * Set socket option to reuse the address.
63 ret
= setsockopt(sock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(int));
65 PERROR("setsockopt inet");
68 timeout
= lttcomm_get_network_timeout();
70 ret
= lttcomm_setsockopt_rcv_timeout(sock
->fd
, timeout
);
74 ret
= lttcomm_setsockopt_snd_timeout(sock
->fd
, timeout
);
87 * Bind socket and return.
89 int lttcomm_bind_inet_sock(struct lttcomm_sock
*sock
)
91 struct sockaddr_in sockaddr
= sock
->sockaddr
.addr
.sin
;
93 return bind(sock
->fd
, (struct sockaddr
*) &sockaddr
, sizeof(sockaddr
));
97 int connect_no_timeout(struct lttcomm_sock
*sock
)
99 struct sockaddr_in sockaddr
= sock
->sockaddr
.addr
.sin
;
101 return connect(sock
->fd
, (struct sockaddr
*) &sockaddr
, sizeof(sockaddr
));
105 int connect_with_timeout(struct lttcomm_sock
*sock
)
107 unsigned long timeout
= lttcomm_get_network_timeout();
108 int ret
, flags
, connect_ret
;
109 struct timespec orig_time
, cur_time
;
110 unsigned long diff_ms
;
111 struct sockaddr_in sockaddr
;
113 ret
= fcntl(sock
->fd
, F_GETFL
, 0);
120 /* Set socket to nonblock */
121 ret
= fcntl(sock
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
127 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &orig_time
);
129 PERROR("clock_gettime");
133 sockaddr
= sock
->sockaddr
.addr
.sin
;
134 connect_ret
= connect(sock
->fd
, (struct sockaddr
*) &sockaddr
, sizeof(sockaddr
));
135 if (connect_ret
== -1 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
&&
136 errno
!= EINPROGRESS
) {
138 } else if (!connect_ret
) {
139 /* Connect succeeded */
143 DBG("Asynchronous connect for sock %d, performing polling with"
144 " timeout: %lums", sock
->fd
, timeout
);
146 * Perform poll loop following EINPROGRESS recommendation from
147 * connect(2) man page.
153 fds
.events
= POLLOUT
;
155 ret
= poll(&fds
, 1, RECONNECT_DELAY
);
158 } else if (ret
> 0) {
160 socklen_t optval_len
= sizeof(optval
);
162 if (!(fds
.revents
& POLLOUT
)) {
163 /* Either hup or error */
168 ret
= getsockopt(sock
->fd
, SOL_SOCKET
,
169 SO_ERROR
, &optval
, &optval_len
);
171 PERROR("getsockopt");
178 /* Get actual connect() errno from opt_val */
183 /* ret == 0: timeout */
184 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
186 PERROR("clock_gettime");
190 if (timespec_to_ms(timespec_abs_diff(cur_time
, orig_time
), &diff_ms
) < 0) {
191 ERR("timespec_to_ms input overflows milliseconds output");
195 } while (diff_ms
< timeout
);
202 /* Restore initial flags */
203 ret
= fcntl(sock
->fd
, F_SETFL
, flags
);
206 /* Continue anyway */
213 * Connect PF_INET socket.
215 int lttcomm_connect_inet_sock(struct lttcomm_sock
*sock
)
219 if (lttcomm_get_network_timeout()) {
220 ret
= connect_with_timeout(sock
);
222 ret
= connect_no_timeout(sock
);
232 closeret
= close(sock
->fd
);
234 PERROR("close inet");
241 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
242 * MUST be bind(2) before.
244 struct lttcomm_sock
*lttcomm_accept_inet_sock(struct lttcomm_sock
*sock
)
248 struct lttcomm_sock
*new_sock
;
249 unsigned long timeout
;
250 struct sockaddr_in new_addr
= {};
252 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
254 * accept(2) does not exist for UDP so simply return the passed socket.
260 new_sock
= lttcomm_alloc_sock(sock
->proto
);
261 if (new_sock
== NULL
) {
265 len
= sizeof(new_addr
);
268 new_fd
= accept(sock
->fd
, (struct sockaddr
*) &new_addr
, &len
);
270 PERROR("accept inet");
273 new_sock
->sockaddr
.addr
.sin
= new_addr
;
274 timeout
= lttcomm_get_network_timeout();
278 ret
= lttcomm_setsockopt_rcv_timeout(new_fd
, timeout
);
282 ret
= lttcomm_setsockopt_snd_timeout(new_fd
, timeout
);
288 new_sock
->fd
= new_fd
;
289 new_sock
->ops
= &inet_ops
;
295 if (close(new_fd
) < 0) {
296 PERROR("accept inet close fd");
305 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
307 int lttcomm_listen_inet_sock(struct lttcomm_sock
*sock
, int backlog
)
311 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
312 /* listen(2) does not exist for UDP so simply return success. */
317 /* Default listen backlog */
319 backlog
= LTTNG_SESSIOND_COMM_MAX_LISTEN
;
322 ret
= listen(sock
->fd
, backlog
);
324 PERROR("listen inet");
332 * Receive data of size len in put that data into the buf param. Using recvmsg
335 * Return the size of received data.
337 ssize_t
lttcomm_recvmsg_inet_sock(struct lttcomm_sock
*sock
, void *buf
,
338 size_t len
, int flags
)
344 struct sockaddr_in addr
= sock
->sockaddr
.addr
.sin
;
346 memset(&msg
, 0, sizeof(msg
));
348 iov
[0].iov_base
= buf
;
349 iov
[0].iov_len
= len
;
353 msg
.msg_name
= (struct sockaddr
*) &addr
;
354 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin
);
357 len_last
= iov
[0].iov_len
;
358 ret
= recvmsg(sock
->fd
, &msg
, flags
);
360 if (flags
& MSG_DONTWAIT
) {
363 iov
[0].iov_base
= ((char *) iov
[0].iov_base
) + ret
;
364 iov
[0].iov_len
-= ret
;
365 LTTNG_ASSERT(ret
<= len_last
);
367 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
370 if (errno
== EAGAIN
&& flags
& MSG_DONTWAIT
) {
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.
379 PERROR("recvmsg inet");
380 } else if (ret
> 0) {
383 /* Else ret = 0 meaning an orderly shutdown. */
389 * Send buf data of size len. Using sendmsg API.
391 * Return the size of sent data.
393 ssize_t
lttcomm_sendmsg_inet_sock(struct lttcomm_sock
*sock
, const void *buf
,
394 size_t len
, int flags
)
400 memset(&msg
, 0, sizeof(msg
));
402 iov
[0].iov_base
= (void *) buf
;
403 iov
[0].iov_len
= len
;
407 switch (sock
->proto
) {
408 case LTTCOMM_SOCK_UDP
:
410 struct sockaddr_in addr
= sock
->sockaddr
.addr
.sin
;
412 msg
.msg_name
= (struct sockaddr
*) &addr
;
413 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin
);
421 ret
= sendmsg(sock
->fd
, &msg
, flags
);
422 } while (ret
< 0 && errno
== EINTR
);
425 * Only warn about EPIPE when quiet mode is deactivated.
426 * We consider EPIPE as expected.
428 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
429 PERROR("sendmsg inet");
437 * Shutdown cleanly and close.
439 int lttcomm_close_inet_sock(struct lttcomm_sock
*sock
)
443 /* Don't try to close an invalid marked socket */
444 if (sock
->fd
== -1) {
448 ret
= close(sock
->fd
);
450 PERROR("close inet");
460 * Return value read from /proc or else 0 if value is not found.
462 static unsigned long read_proc_value(const char *path
)
467 unsigned long val
= 0;
470 fd
= open(path
, O_RDONLY
);
475 size_ret
= lttng_read(fd
, buf
, sizeof(buf
));
477 * Allow reading a file smaller than buf, but keep space for
480 if (size_ret
< 0 || size_ret
>= sizeof(buf
)) {
481 PERROR("read proc failed");
484 buf
[size_ret
] = '\0';
487 r_val
= strtol(buf
, NULL
, 10);
488 if (errno
!= 0 || r_val
< -1L) {
500 PERROR("close /proc value");
506 void lttcomm_inet_init(void)
508 unsigned long syn_retries
, fin_timeout
, syn_timeout
, env
;
510 env
= lttcomm_get_network_timeout();
512 lttcomm_inet_tcp_timeout
= env
;
516 /* Assign default value and see if we can change it. */
517 lttcomm_inet_tcp_timeout
= DEFAULT_INET_TCP_TIMEOUT
;
519 syn_retries
= read_proc_value(LTTCOMM_INET_PROC_SYN_RETRIES_PATH
);
520 fin_timeout
= read_proc_value(LTTCOMM_INET_PROC_FIN_TIMEOUT_PATH
);
522 syn_timeout
= syn_retries
* LTTCOMM_INET_SYN_TIMEOUT_FACTOR
;
525 * Get the maximum between the two possible timeout value and use that to
526 * get the maximum with the default timeout.
528 lttcomm_inet_tcp_timeout
= std::max(std::max(syn_timeout
, fin_timeout
),
529 lttcomm_inet_tcp_timeout
);
532 DBG("TCP inet operation timeout set to %lu sec", lttcomm_inet_tcp_timeout
);