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
)
90 struct sockaddr_in sockaddr
= sock
->sockaddr
.addr
.sin
;
92 return bind(sock
->fd
, &sockaddr
, sizeof(sockaddr
));
96 int connect_no_timeout(struct lttcomm_sock
*sock
)
98 struct sockaddr_in sockaddr
= sock
->sockaddr
.addr
.sin
;
100 return connect(sock
->fd
, &sockaddr
, sizeof(sockaddr
));
104 int connect_with_timeout(struct lttcomm_sock
*sock
)
106 unsigned long timeout
= lttcomm_get_network_timeout();
107 int ret
, flags
, connect_ret
;
108 struct timespec orig_time
, cur_time
;
109 unsigned long diff_ms
;
110 struct sockaddr_in sockaddr
;
112 ret
= fcntl(sock
->fd
, F_GETFL
, 0);
119 /* Set socket to nonblock */
120 ret
= fcntl(sock
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
126 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &orig_time
);
128 PERROR("clock_gettime");
132 sockaddr
= sock
->sockaddr
.addr
.sin
;
133 connect_ret
= connect(sock
->fd
, &sockaddr
, sizeof(sockaddr
));
134 if (connect_ret
== -1 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
&&
135 errno
!= EINPROGRESS
) {
137 } else if (!connect_ret
) {
138 /* Connect succeeded */
142 DBG("Asynchronous connect for sock %d, performing polling with"
143 " timeout: %lums", sock
->fd
, timeout
);
145 * Perform poll loop following EINPROGRESS recommendation from
146 * connect(2) man page.
152 fds
.events
= POLLOUT
;
154 ret
= poll(&fds
, 1, RECONNECT_DELAY
);
157 } else if (ret
> 0) {
159 socklen_t optval_len
= sizeof(optval
);
161 if (!(fds
.revents
& POLLOUT
)) {
162 /* Either hup or error */
167 ret
= getsockopt(sock
->fd
, SOL_SOCKET
,
168 SO_ERROR
, &optval
, &optval_len
);
170 PERROR("getsockopt");
177 /* Get actual connect() errno from opt_val */
182 /* ret == 0: timeout */
183 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
185 PERROR("clock_gettime");
189 if (timespec_to_ms(timespec_abs_diff(cur_time
, orig_time
), &diff_ms
) < 0) {
190 ERR("timespec_to_ms input overflows milliseconds output");
194 } while (diff_ms
< timeout
);
201 /* Restore initial flags */
202 ret
= fcntl(sock
->fd
, F_SETFL
, flags
);
205 /* Continue anyway */
212 * Connect PF_INET socket.
214 int lttcomm_connect_inet_sock(struct lttcomm_sock
*sock
)
218 if (lttcomm_get_network_timeout()) {
219 ret
= connect_with_timeout(sock
);
221 ret
= connect_no_timeout(sock
);
231 closeret
= close(sock
->fd
);
233 PERROR("close inet");
240 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
241 * MUST be bind(2) before.
243 struct lttcomm_sock
*lttcomm_accept_inet_sock(struct lttcomm_sock
*sock
)
247 struct lttcomm_sock
*new_sock
;
248 unsigned long timeout
;
249 struct sockaddr_in new_addr
= {};
251 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
253 * accept(2) does not exist for UDP so simply return the passed socket.
259 new_sock
= lttcomm_alloc_sock(sock
->proto
);
260 if (new_sock
== NULL
) {
264 len
= sizeof(new_addr
);
267 new_fd
= accept(sock
->fd
, (struct sockaddr
*) &new_addr
, &len
);
269 PERROR("accept inet");
272 new_sock
->sockaddr
.addr
.sin
= new_addr
;
273 timeout
= lttcomm_get_network_timeout();
277 ret
= lttcomm_setsockopt_rcv_timeout(new_fd
, timeout
);
281 ret
= lttcomm_setsockopt_snd_timeout(new_fd
, timeout
);
287 new_sock
->fd
= new_fd
;
288 new_sock
->ops
= &inet_ops
;
294 if (close(new_fd
) < 0) {
295 PERROR("accept inet close fd");
304 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
306 int lttcomm_listen_inet_sock(struct lttcomm_sock
*sock
, int backlog
)
310 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
311 /* listen(2) does not exist for UDP so simply return success. */
316 /* Default listen backlog */
318 backlog
= LTTNG_SESSIOND_COMM_MAX_LISTEN
;
321 ret
= listen(sock
->fd
, backlog
);
323 PERROR("listen inet");
331 * Receive data of size len in put that data into the buf param. Using recvmsg
334 * Return the size of received data.
336 ssize_t
lttcomm_recvmsg_inet_sock(struct lttcomm_sock
*sock
, void *buf
,
337 size_t len
, int flags
)
343 struct sockaddr_in addr
= sock
->sockaddr
.addr
.sin
;
345 memset(&msg
, 0, sizeof(msg
));
347 iov
[0].iov_base
= buf
;
348 iov
[0].iov_len
= len
;
352 msg
.msg_name
= (struct sockaddr
*) &addr
;
353 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin
);
356 len_last
= iov
[0].iov_len
;
357 ret
= recvmsg(sock
->fd
, &msg
, flags
);
359 if (flags
& MSG_DONTWAIT
) {
362 iov
[0].iov_base
+= ret
;
363 iov
[0].iov_len
-= ret
;
364 LTTNG_ASSERT(ret
<= len_last
);
366 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
369 if (errno
== EAGAIN
&& flags
& MSG_DONTWAIT
) {
371 * EAGAIN is expected in non-blocking mode and should
372 * not be reported as an error. Moreover, if no data
373 * was read, 0 must not be returned as it would be
374 * interpreted as an orderly shutdown of the socket.
378 PERROR("recvmsg inet");
379 } else if (ret
> 0) {
382 /* Else ret = 0 meaning an orderly shutdown. */
388 * Send buf data of size len. Using sendmsg API.
390 * Return the size of sent data.
392 ssize_t
lttcomm_sendmsg_inet_sock(struct lttcomm_sock
*sock
, const void *buf
,
393 size_t len
, int flags
)
399 memset(&msg
, 0, sizeof(msg
));
401 iov
[0].iov_base
= (void *) buf
;
402 iov
[0].iov_len
= len
;
406 switch (sock
->proto
) {
407 case LTTCOMM_SOCK_UDP
:
409 struct sockaddr_in addr
= sock
->sockaddr
.addr
.sin
;
411 msg
.msg_name
= (struct sockaddr
*) &addr
;
412 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin
);
420 ret
= sendmsg(sock
->fd
, &msg
, flags
);
421 } while (ret
< 0 && errno
== EINTR
);
424 * Only warn about EPIPE when quiet mode is deactivated.
425 * We consider EPIPE as expected.
427 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
428 PERROR("sendmsg inet");
436 * Shutdown cleanly and close.
438 int lttcomm_close_inet_sock(struct lttcomm_sock
*sock
)
442 /* Don't try to close an invalid marked socket */
443 if (sock
->fd
== -1) {
447 ret
= close(sock
->fd
);
449 PERROR("close inet");
459 * Return value read from /proc or else 0 if value is not found.
461 static unsigned long read_proc_value(const char *path
)
466 unsigned long val
= 0;
469 fd
= open(path
, O_RDONLY
);
474 size_ret
= lttng_read(fd
, buf
, sizeof(buf
));
476 * Allow reading a file smaller than buf, but keep space for
479 if (size_ret
< 0 || size_ret
>= sizeof(buf
)) {
480 PERROR("read proc failed");
483 buf
[size_ret
] = '\0';
486 r_val
= strtol(buf
, NULL
, 10);
487 if (errno
!= 0 || r_val
< -1L) {
499 PERROR("close /proc value");
505 void lttcomm_inet_init(void)
507 unsigned long syn_retries
, fin_timeout
, syn_timeout
, env
;
509 env
= lttcomm_get_network_timeout();
511 lttcomm_inet_tcp_timeout
= env
;
515 /* Assign default value and see if we can change it. */
516 lttcomm_inet_tcp_timeout
= DEFAULT_INET_TCP_TIMEOUT
;
518 syn_retries
= read_proc_value(LTTCOMM_INET_PROC_SYN_RETRIES_PATH
);
519 fin_timeout
= read_proc_value(LTTCOMM_INET_PROC_FIN_TIMEOUT_PATH
);
521 syn_timeout
= syn_retries
* LTTCOMM_INET_SYN_TIMEOUT_FACTOR
;
524 * Get the maximum between the two possible timeout value and use that to
525 * get the maximum with the default timeout.
527 lttcomm_inet_tcp_timeout
= max_t(unsigned long,
528 max_t(unsigned long, syn_timeout
, fin_timeout
),
529 lttcomm_inet_tcp_timeout
);
532 DBG("TCP inet operation timeout set to %lu sec", lttcomm_inet_tcp_timeout
);