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.
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.
90 int lttcomm_bind_inet_sock(struct lttcomm_sock
*sock
)
93 (const struct sockaddr
*) ALIGNED_CONST_PTR(
94 sock
->sockaddr
.addr
.sin
),
95 sizeof(sock
->sockaddr
.addr
.sin
));
99 int connect_no_timeout(struct lttcomm_sock
*sock
)
101 return connect(sock
->fd
,
102 (const struct sockaddr
*) ALIGNED_CONST_PTR(
103 sock
->sockaddr
.addr
.sin
),
104 sizeof(sock
->sockaddr
.addr
.sin
));
108 int connect_with_timeout(struct lttcomm_sock
*sock
)
110 unsigned long timeout
= lttcomm_get_network_timeout();
111 int ret
, flags
, connect_ret
;
112 struct timespec orig_time
, cur_time
;
113 unsigned long diff_ms
;
115 ret
= fcntl(sock
->fd
, F_GETFL
, 0);
122 /* Set socket to nonblock */
123 ret
= fcntl(sock
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
129 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &orig_time
);
131 PERROR("clock_gettime");
135 connect_ret
= connect(sock
->fd
,
136 (const struct sockaddr
*) ALIGNED_CONST_PTR(
137 sock
->sockaddr
.addr
.sin
),
138 sizeof(sock
->sockaddr
.addr
.sin
));
139 if (connect_ret
== -1 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
&&
140 errno
!= EINPROGRESS
) {
142 } else if (!connect_ret
) {
143 /* Connect succeeded */
147 DBG("Asynchronous connect for sock %d, performing polling with"
148 " timeout: %lums", sock
->fd
, timeout
);
150 * Perform poll loop following EINPROGRESS recommendation from
151 * connect(2) man page.
157 fds
.events
= POLLOUT
;
159 ret
= poll(&fds
, 1, RECONNECT_DELAY
);
162 } else if (ret
> 0) {
164 socklen_t optval_len
= sizeof(optval
);
166 if (!(fds
.revents
& POLLOUT
)) {
167 /* Either hup or error */
172 ret
= getsockopt(sock
->fd
, SOL_SOCKET
,
173 SO_ERROR
, &optval
, &optval_len
);
175 PERROR("getsockopt");
182 /* Get actual connect() errno from opt_val */
187 /* ret == 0: timeout */
188 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
190 PERROR("clock_gettime");
194 if (timespec_to_ms(timespec_abs_diff(cur_time
, orig_time
), &diff_ms
) < 0) {
195 ERR("timespec_to_ms input overflows milliseconds output");
199 } while (diff_ms
< timeout
);
206 /* Restore initial flags */
207 ret
= fcntl(sock
->fd
, F_SETFL
, flags
);
210 /* Continue anyway */
217 * Connect PF_INET socket.
220 int lttcomm_connect_inet_sock(struct lttcomm_sock
*sock
)
224 if (lttcomm_get_network_timeout()) {
225 ret
= connect_with_timeout(sock
);
227 ret
= connect_no_timeout(sock
);
237 closeret
= close(sock
->fd
);
239 PERROR("close inet");
246 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
247 * MUST be bind(2) before.
250 struct lttcomm_sock
*lttcomm_accept_inet_sock(struct lttcomm_sock
*sock
)
254 struct lttcomm_sock
*new_sock
;
255 unsigned long timeout
;
256 struct sockaddr_in new_addr
= {};
258 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
260 * accept(2) does not exist for UDP so simply return the passed socket.
266 new_sock
= lttcomm_alloc_sock(sock
->proto
);
267 if (new_sock
== NULL
) {
271 len
= sizeof(new_addr
);
274 new_fd
= accept(sock
->fd
, (struct sockaddr
*) &new_addr
, &len
);
276 PERROR("accept inet");
279 new_sock
->sockaddr
.addr
.sin
= new_addr
;
280 timeout
= lttcomm_get_network_timeout();
284 ret
= lttcomm_setsockopt_rcv_timeout(new_fd
, timeout
);
288 ret
= lttcomm_setsockopt_snd_timeout(new_fd
, timeout
);
294 new_sock
->fd
= new_fd
;
295 new_sock
->ops
= &inet_ops
;
301 if (close(new_fd
) < 0) {
302 PERROR("accept inet close fd");
311 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
314 int lttcomm_listen_inet_sock(struct lttcomm_sock
*sock
, int backlog
)
318 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
319 /* listen(2) does not exist for UDP so simply return success. */
324 /* Default listen backlog */
326 backlog
= LTTNG_SESSIOND_COMM_MAX_LISTEN
;
329 ret
= listen(sock
->fd
, backlog
);
331 PERROR("listen inet");
339 * Receive data of size len in put that data into the buf param. Using recvmsg
342 * Return the size of received data.
345 ssize_t
lttcomm_recvmsg_inet_sock(struct lttcomm_sock
*sock
, void *buf
,
346 size_t len
, int flags
)
352 struct sockaddr_in addr
= sock
->sockaddr
.addr
.sin
;
354 memset(&msg
, 0, sizeof(msg
));
356 iov
[0].iov_base
= buf
;
357 iov
[0].iov_len
= len
;
361 msg
.msg_name
= (struct sockaddr
*) &addr
;
362 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin
);
365 len_last
= iov
[0].iov_len
;
366 ret
= recvmsg(sock
->fd
, &msg
, flags
);
368 if (flags
& MSG_DONTWAIT
) {
371 iov
[0].iov_base
+= ret
;
372 iov
[0].iov_len
-= ret
;
373 LTTNG_ASSERT(ret
<= len_last
);
375 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
378 if (errno
== EAGAIN
&& flags
& MSG_DONTWAIT
) {
380 * EAGAIN is expected in non-blocking mode and should
381 * not be reported as an error. Moreover, if no data
382 * was read, 0 must not be returned as it would be
383 * interpreted as an orderly shutdown of the socket.
387 PERROR("recvmsg inet");
388 } else if (ret
> 0) {
391 /* Else ret = 0 meaning an orderly shutdown. */
397 * Send buf data of size len. Using sendmsg API.
399 * Return the size of sent data.
402 ssize_t
lttcomm_sendmsg_inet_sock(struct lttcomm_sock
*sock
, const void *buf
,
403 size_t len
, int flags
)
409 memset(&msg
, 0, sizeof(msg
));
411 iov
[0].iov_base
= (void *) buf
;
412 iov
[0].iov_len
= len
;
416 switch (sock
->proto
) {
417 case LTTCOMM_SOCK_UDP
:
419 struct sockaddr_in addr
= sock
->sockaddr
.addr
.sin
;
421 msg
.msg_name
= (struct sockaddr
*) &addr
;
422 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin
);
430 ret
= sendmsg(sock
->fd
, &msg
, flags
);
431 } while (ret
< 0 && errno
== EINTR
);
434 * Only warn about EPIPE when quiet mode is deactivated.
435 * We consider EPIPE as expected.
437 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
438 PERROR("sendmsg inet");
446 * Shutdown cleanly and close.
449 int lttcomm_close_inet_sock(struct lttcomm_sock
*sock
)
453 /* Don't try to close an invalid marked socket */
454 if (sock
->fd
== -1) {
458 ret
= close(sock
->fd
);
460 PERROR("close inet");
470 * Return value read from /proc or else 0 if value is not found.
472 static unsigned long read_proc_value(const char *path
)
477 unsigned long val
= 0;
480 fd
= open(path
, O_RDONLY
);
485 size_ret
= lttng_read(fd
, buf
, sizeof(buf
));
487 * Allow reading a file smaller than buf, but keep space for
490 if (size_ret
< 0 || size_ret
>= sizeof(buf
)) {
491 PERROR("read proc failed");
494 buf
[size_ret
] = '\0';
497 r_val
= strtol(buf
, NULL
, 10);
498 if (errno
!= 0 || r_val
< -1L) {
510 PERROR("close /proc value");
517 void lttcomm_inet_init(void)
519 unsigned long syn_retries
, fin_timeout
, syn_timeout
, env
;
521 env
= lttcomm_get_network_timeout();
523 lttcomm_inet_tcp_timeout
= env
;
527 /* Assign default value and see if we can change it. */
528 lttcomm_inet_tcp_timeout
= DEFAULT_INET_TCP_TIMEOUT
;
530 syn_retries
= read_proc_value(LTTCOMM_INET_PROC_SYN_RETRIES_PATH
);
531 fin_timeout
= read_proc_value(LTTCOMM_INET_PROC_FIN_TIMEOUT_PATH
);
533 syn_timeout
= syn_retries
* LTTCOMM_INET_SYN_TIMEOUT_FACTOR
;
536 * Get the maximum between the two possible timeout value and use that to
537 * get the maximum with the default timeout.
539 lttcomm_inet_tcp_timeout
= max_t(unsigned long,
540 max_t(unsigned long, syn_timeout
, fin_timeout
),
541 lttcomm_inet_tcp_timeout
);
544 DBG("TCP inet operation timeout set to %lu sec", lttcomm_inet_tcp_timeout
);