2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
17 #include <common/compat/time.h>
20 #include <common/common.h>
21 #include <common/time.h>
22 #include <common/compat/errno.h>
26 #define RECONNECT_DELAY 200 /* ms */
29 * INET protocol operations.
31 static const struct lttcomm_proto_ops inet_ops
= {
32 .bind
= lttcomm_bind_inet_sock
,
33 .close
= lttcomm_close_inet_sock
,
34 .connect
= lttcomm_connect_inet_sock
,
35 .accept
= lttcomm_accept_inet_sock
,
36 .listen
= lttcomm_listen_inet_sock
,
37 .recvmsg
= lttcomm_recvmsg_inet_sock
,
38 .sendmsg
= lttcomm_sendmsg_inet_sock
,
41 unsigned long lttcomm_inet_tcp_timeout
;
44 * Creates an PF_INET socket.
46 int lttcomm_create_inet_sock(struct lttcomm_sock
*sock
, int type
, int proto
)
49 unsigned long timeout
;
51 /* Create server socket */
52 if ((sock
->fd
= socket(PF_INET
, type
, proto
)) < 0) {
53 PERROR("socket inet");
57 sock
->ops
= &inet_ops
;
60 * Set socket option to reuse the address.
62 ret
= setsockopt(sock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(int));
64 PERROR("setsockopt inet");
67 timeout
= lttcomm_get_network_timeout();
69 ret
= lttcomm_setsockopt_rcv_timeout(sock
->fd
, timeout
);
73 ret
= lttcomm_setsockopt_snd_timeout(sock
->fd
, timeout
);
86 * Bind socket and return.
88 int lttcomm_bind_inet_sock(struct lttcomm_sock
*sock
)
91 (const struct sockaddr
*) ALIGNED_CONST_PTR(
92 sock
->sockaddr
.addr
.sin
),
93 sizeof(sock
->sockaddr
.addr
.sin
));
97 int connect_no_timeout(struct lttcomm_sock
*sock
)
99 return connect(sock
->fd
,
100 (const struct sockaddr
*) ALIGNED_CONST_PTR(
101 sock
->sockaddr
.addr
.sin
),
102 sizeof(sock
->sockaddr
.addr
.sin
));
106 int connect_with_timeout(struct lttcomm_sock
*sock
)
108 unsigned long timeout
= lttcomm_get_network_timeout();
109 int ret
, flags
, connect_ret
;
110 struct timespec orig_time
, cur_time
;
111 unsigned long diff_ms
;
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 connect_ret
= connect(sock
->fd
,
134 (const struct sockaddr
*) ALIGNED_CONST_PTR(
135 sock
->sockaddr
.addr
.sin
),
136 sizeof(sock
->sockaddr
.addr
.sin
));
137 if (connect_ret
== -1 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
&&
138 errno
!= EINPROGRESS
) {
140 } else if (!connect_ret
) {
141 /* Connect succeeded */
145 DBG("Asynchronous connect for sock %d, performing polling with"
146 " timeout: %lums", sock
->fd
, timeout
);
148 * Perform poll loop following EINPROGRESS recommendation from
149 * connect(2) man page.
155 fds
.events
= POLLOUT
;
157 ret
= poll(&fds
, 1, RECONNECT_DELAY
);
160 } else if (ret
> 0) {
162 socklen_t optval_len
= sizeof(optval
);
164 if (!(fds
.revents
& POLLOUT
)) {
165 /* Either hup or error */
170 ret
= getsockopt(sock
->fd
, SOL_SOCKET
,
171 SO_ERROR
, &optval
, &optval_len
);
173 PERROR("getsockopt");
180 /* Get actual connect() errno from opt_val */
185 /* ret == 0: timeout */
186 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
188 PERROR("clock_gettime");
192 if (timespec_to_ms(timespec_abs_diff(cur_time
, orig_time
), &diff_ms
) < 0) {
193 ERR("timespec_to_ms input overflows milliseconds output");
197 } while (diff_ms
< timeout
);
204 /* Restore initial flags */
205 ret
= fcntl(sock
->fd
, F_SETFL
, flags
);
208 /* Continue anyway */
215 * Connect PF_INET socket.
217 int lttcomm_connect_inet_sock(struct lttcomm_sock
*sock
)
221 if (lttcomm_get_network_timeout()) {
222 ret
= connect_with_timeout(sock
);
224 ret
= connect_no_timeout(sock
);
234 closeret
= close(sock
->fd
);
236 PERROR("close inet");
243 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
244 * MUST be bind(2) before.
246 struct lttcomm_sock
*lttcomm_accept_inet_sock(struct lttcomm_sock
*sock
)
250 struct lttcomm_sock
*new_sock
;
251 unsigned long timeout
;
252 struct sockaddr_in new_addr
= {};
254 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
256 * accept(2) does not exist for UDP so simply return the passed socket.
262 new_sock
= lttcomm_alloc_sock(sock
->proto
);
263 if (new_sock
== NULL
) {
267 len
= sizeof(new_addr
);
270 new_fd
= accept(sock
->fd
, (struct sockaddr
*) &new_addr
, &len
);
272 PERROR("accept inet");
275 new_sock
->sockaddr
.addr
.sin
= new_addr
;
276 timeout
= lttcomm_get_network_timeout();
280 ret
= lttcomm_setsockopt_rcv_timeout(new_fd
, timeout
);
284 ret
= lttcomm_setsockopt_snd_timeout(new_fd
, timeout
);
290 new_sock
->fd
= new_fd
;
291 new_sock
->ops
= &inet_ops
;
297 if (close(new_fd
) < 0) {
298 PERROR("accept inet close fd");
307 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
309 int lttcomm_listen_inet_sock(struct lttcomm_sock
*sock
, int backlog
)
313 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
314 /* listen(2) does not exist for UDP so simply return success. */
319 /* Default listen backlog */
321 backlog
= LTTNG_SESSIOND_COMM_MAX_LISTEN
;
324 ret
= listen(sock
->fd
, backlog
);
326 PERROR("listen inet");
334 * Receive data of size len in put that data into the buf param. Using recvmsg
337 * Return the size of received data.
339 ssize_t
lttcomm_recvmsg_inet_sock(struct lttcomm_sock
*sock
, void *buf
,
340 size_t len
, int flags
)
346 struct sockaddr_in addr
= sock
->sockaddr
.addr
.sin
;
348 memset(&msg
, 0, sizeof(msg
));
350 iov
[0].iov_base
= buf
;
351 iov
[0].iov_len
= len
;
355 msg
.msg_name
= (struct sockaddr
*) &addr
;
356 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin
);
359 len_last
= iov
[0].iov_len
;
360 ret
= recvmsg(sock
->fd
, &msg
, flags
);
362 if (flags
& MSG_DONTWAIT
) {
365 iov
[0].iov_base
+= ret
;
366 iov
[0].iov_len
-= ret
;
367 LTTNG_ASSERT(ret
<= len_last
);
369 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
372 if (errno
== EAGAIN
&& flags
& MSG_DONTWAIT
) {
374 * EAGAIN is expected in non-blocking mode and should
375 * not be reported as an error. Moreover, if no data
376 * was read, 0 must not be returned as it would be
377 * interpreted as an orderly shutdown of the socket.
381 PERROR("recvmsg inet");
382 } else if (ret
> 0) {
385 /* Else ret = 0 meaning an orderly shutdown. */
391 * Send buf data of size len. Using sendmsg API.
393 * Return the size of sent data.
395 ssize_t
lttcomm_sendmsg_inet_sock(struct lttcomm_sock
*sock
, const void *buf
,
396 size_t len
, int flags
)
402 memset(&msg
, 0, sizeof(msg
));
404 iov
[0].iov_base
= (void *) buf
;
405 iov
[0].iov_len
= len
;
409 switch (sock
->proto
) {
410 case LTTCOMM_SOCK_UDP
:
412 struct sockaddr_in addr
= sock
->sockaddr
.addr
.sin
;
414 msg
.msg_name
= (struct sockaddr
*) &addr
;
415 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin
);
423 ret
= sendmsg(sock
->fd
, &msg
, flags
);
424 } while (ret
< 0 && errno
== EINTR
);
427 * Only warn about EPIPE when quiet mode is deactivated.
428 * We consider EPIPE as expected.
430 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
431 PERROR("sendmsg inet");
439 * Shutdown cleanly and close.
441 int lttcomm_close_inet_sock(struct lttcomm_sock
*sock
)
445 /* Don't try to close an invalid marked socket */
446 if (sock
->fd
== -1) {
450 ret
= close(sock
->fd
);
452 PERROR("close inet");
462 * Return value read from /proc or else 0 if value is not found.
464 static unsigned long read_proc_value(const char *path
)
469 unsigned long val
= 0;
472 fd
= open(path
, O_RDONLY
);
477 size_ret
= lttng_read(fd
, buf
, sizeof(buf
));
479 * Allow reading a file smaller than buf, but keep space for
482 if (size_ret
< 0 || size_ret
>= sizeof(buf
)) {
483 PERROR("read proc failed");
486 buf
[size_ret
] = '\0';
489 r_val
= strtol(buf
, NULL
, 10);
490 if (errno
!= 0 || r_val
< -1L) {
502 PERROR("close /proc value");
508 void lttcomm_inet_init(void)
510 unsigned long syn_retries
, fin_timeout
, syn_timeout
, env
;
512 env
= lttcomm_get_network_timeout();
514 lttcomm_inet_tcp_timeout
= env
;
518 /* Assign default value and see if we can change it. */
519 lttcomm_inet_tcp_timeout
= DEFAULT_INET_TCP_TIMEOUT
;
521 syn_retries
= read_proc_value(LTTCOMM_INET_PROC_SYN_RETRIES_PATH
);
522 fin_timeout
= read_proc_value(LTTCOMM_INET_PROC_FIN_TIMEOUT_PATH
);
524 syn_timeout
= syn_retries
* LTTCOMM_INET_SYN_TIMEOUT_FACTOR
;
527 * Get the maximum between the two possible timeout value and use that to
528 * get the maximum with the default timeout.
530 lttcomm_inet_tcp_timeout
= max_t(unsigned long,
531 max_t(unsigned long, syn_timeout
, fin_timeout
),
532 lttcomm_inet_tcp_timeout
);
535 DBG("TCP inet operation timeout set to %lu sec", lttcomm_inet_tcp_timeout
);