2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
15 #include <sys/types.h>
19 #include <common/compat/time.h>
22 #include <common/common.h>
23 #include <common/time.h>
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.
48 int lttcomm_create_inet_sock(struct lttcomm_sock
*sock
, int type
, int proto
)
51 unsigned long timeout
;
53 /* Create server socket */
54 if ((sock
->fd
= socket(PF_INET
, type
, proto
)) < 0) {
55 PERROR("socket inet");
59 sock
->ops
= &inet_ops
;
62 * Set socket option to reuse the address.
64 ret
= setsockopt(sock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(int));
66 PERROR("setsockopt inet");
69 timeout
= lttcomm_get_network_timeout();
71 ret
= lttcomm_setsockopt_rcv_timeout(sock
->fd
, timeout
);
75 ret
= lttcomm_setsockopt_snd_timeout(sock
->fd
, timeout
);
88 * Bind socket and return.
91 int lttcomm_bind_inet_sock(struct lttcomm_sock
*sock
)
94 (const struct sockaddr
*) ALIGNED_CONST_PTR(
95 sock
->sockaddr
.addr
.sin
),
96 sizeof(sock
->sockaddr
.addr
.sin
));
100 int connect_no_timeout(struct lttcomm_sock
*sock
)
102 return connect(sock
->fd
,
103 (const struct sockaddr
*) ALIGNED_CONST_PTR(
104 sock
->sockaddr
.addr
.sin
),
105 sizeof(sock
->sockaddr
.addr
.sin
));
109 int connect_with_timeout(struct lttcomm_sock
*sock
)
111 unsigned long timeout
= lttcomm_get_network_timeout();
112 int ret
, flags
, connect_ret
;
113 struct timespec orig_time
, cur_time
;
114 unsigned long diff_ms
;
116 ret
= fcntl(sock
->fd
, F_GETFL
, 0);
123 /* Set socket to nonblock */
124 ret
= fcntl(sock
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
130 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &orig_time
);
132 PERROR("clock_gettime");
136 connect_ret
= connect(sock
->fd
,
137 (const struct sockaddr
*) ALIGNED_CONST_PTR(
138 sock
->sockaddr
.addr
.sin
),
139 sizeof(sock
->sockaddr
.addr
.sin
));
140 if (connect_ret
== -1 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
&&
141 errno
!= EINPROGRESS
) {
143 } else if (!connect_ret
) {
144 /* Connect succeeded */
148 DBG("Asynchronous connect for sock %d, performing polling with"
149 " timeout: %lums", sock
->fd
, timeout
);
151 * Perform poll loop following EINPROGRESS recommendation from
152 * connect(2) man page.
158 fds
.events
= POLLOUT
;
160 ret
= poll(&fds
, 1, RECONNECT_DELAY
);
163 } else if (ret
> 0) {
165 socklen_t optval_len
= sizeof(optval
);
167 if (!(fds
.revents
& POLLOUT
)) {
168 /* Either hup or error */
173 ret
= getsockopt(sock
->fd
, SOL_SOCKET
,
174 SO_ERROR
, &optval
, &optval_len
);
176 PERROR("getsockopt");
183 /* Get actual connect() errno from opt_val */
188 /* ret == 0: timeout */
189 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
191 PERROR("clock_gettime");
195 if (timespec_to_ms(timespec_abs_diff(cur_time
, orig_time
), &diff_ms
) < 0) {
196 ERR("timespec_to_ms input overflows milliseconds output");
200 } while (diff_ms
< timeout
);
207 /* Restore initial flags */
208 ret
= fcntl(sock
->fd
, F_SETFL
, flags
);
211 /* Continue anyway */
218 * Connect PF_INET socket.
221 int lttcomm_connect_inet_sock(struct lttcomm_sock
*sock
)
225 if (lttcomm_get_network_timeout()) {
226 ret
= connect_with_timeout(sock
);
228 ret
= connect_no_timeout(sock
);
238 closeret
= close(sock
->fd
);
240 PERROR("close inet");
247 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
248 * MUST be bind(2) before.
251 struct lttcomm_sock
*lttcomm_accept_inet_sock(struct lttcomm_sock
*sock
)
255 struct lttcomm_sock
*new_sock
;
256 unsigned long timeout
;
257 struct sockaddr_in new_addr
= {};
259 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
261 * accept(2) does not exist for UDP so simply return the passed socket.
267 new_sock
= lttcomm_alloc_sock(sock
->proto
);
268 if (new_sock
== NULL
) {
272 len
= sizeof(new_addr
);
275 new_fd
= accept(sock
->fd
, (struct sockaddr
*) &new_addr
, &len
);
277 PERROR("accept inet");
280 new_sock
->sockaddr
.addr
.sin
= new_addr
;
281 timeout
= lttcomm_get_network_timeout();
285 ret
= lttcomm_setsockopt_rcv_timeout(new_fd
, timeout
);
289 ret
= lttcomm_setsockopt_snd_timeout(new_fd
, timeout
);
295 new_sock
->fd
= new_fd
;
296 new_sock
->ops
= &inet_ops
;
302 if (close(new_fd
) < 0) {
303 PERROR("accept inet close fd");
312 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
315 int lttcomm_listen_inet_sock(struct lttcomm_sock
*sock
, int backlog
)
319 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
320 /* listen(2) does not exist for UDP so simply return success. */
325 /* Default listen backlog */
327 backlog
= LTTNG_SESSIOND_COMM_MAX_LISTEN
;
330 ret
= listen(sock
->fd
, backlog
);
332 PERROR("listen inet");
340 * Receive data of size len in put that data into the buf param. Using recvmsg
343 * Return the size of received data.
346 ssize_t
lttcomm_recvmsg_inet_sock(struct lttcomm_sock
*sock
, void *buf
,
347 size_t len
, int flags
)
353 struct sockaddr_in addr
= sock
->sockaddr
.addr
.sin
;
355 memset(&msg
, 0, sizeof(msg
));
357 iov
[0].iov_base
= buf
;
358 iov
[0].iov_len
= len
;
362 msg
.msg_name
= (struct sockaddr
*) &addr
;
363 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin
);
366 len_last
= iov
[0].iov_len
;
367 ret
= recvmsg(sock
->fd
, &msg
, flags
);
369 if (flags
& MSG_DONTWAIT
) {
372 iov
[0].iov_base
+= ret
;
373 iov
[0].iov_len
-= ret
;
374 assert(ret
<= len_last
);
376 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
379 if (errno
== EAGAIN
&& flags
& MSG_DONTWAIT
) {
381 * EAGAIN is expected in non-blocking mode and should
382 * not be reported as an error. Moreover, if no data
383 * was read, 0 must not be returned as it would be
384 * interpreted as an orderly shutdown of the socket.
388 PERROR("recvmsg inet");
389 } else if (ret
> 0) {
392 /* Else ret = 0 meaning an orderly shutdown. */
398 * Send buf data of size len. Using sendmsg API.
400 * Return the size of sent data.
403 ssize_t
lttcomm_sendmsg_inet_sock(struct lttcomm_sock
*sock
, const void *buf
,
404 size_t len
, int flags
)
410 memset(&msg
, 0, sizeof(msg
));
412 iov
[0].iov_base
= (void *) buf
;
413 iov
[0].iov_len
= len
;
417 switch (sock
->proto
) {
418 case LTTCOMM_SOCK_UDP
:
420 struct sockaddr_in addr
= sock
->sockaddr
.addr
.sin
;
422 msg
.msg_name
= (struct sockaddr
*) &addr
;
423 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin
);
431 ret
= sendmsg(sock
->fd
, &msg
, flags
);
432 } while (ret
< 0 && errno
== EINTR
);
435 * Only warn about EPIPE when quiet mode is deactivated.
436 * We consider EPIPE as expected.
438 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
439 PERROR("sendmsg inet");
447 * Shutdown cleanly and close.
450 int lttcomm_close_inet_sock(struct lttcomm_sock
*sock
)
454 /* Don't try to close an invalid marked socket */
455 if (sock
->fd
== -1) {
459 ret
= close(sock
->fd
);
461 PERROR("close inet");
471 * Return value read from /proc or else 0 if value is not found.
473 static unsigned long read_proc_value(const char *path
)
478 unsigned long val
= 0;
481 fd
= open(path
, O_RDONLY
);
486 size_ret
= lttng_read(fd
, buf
, sizeof(buf
));
488 * Allow reading a file smaller than buf, but keep space for
491 if (size_ret
< 0 || size_ret
>= sizeof(buf
)) {
492 PERROR("read proc failed");
495 buf
[size_ret
] = '\0';
498 r_val
= strtol(buf
, NULL
, 10);
499 if (errno
!= 0 || r_val
< -1L) {
511 PERROR("close /proc value");
518 void lttcomm_inet_init(void)
520 unsigned long syn_retries
, fin_timeout
, syn_timeout
, env
;
522 env
= lttcomm_get_network_timeout();
524 lttcomm_inet_tcp_timeout
= env
;
528 /* Assign default value and see if we can change it. */
529 lttcomm_inet_tcp_timeout
= DEFAULT_INET_TCP_TIMEOUT
;
531 syn_retries
= read_proc_value(LTTCOMM_INET_PROC_SYN_RETRIES_PATH
);
532 fin_timeout
= read_proc_value(LTTCOMM_INET_PROC_FIN_TIMEOUT_PATH
);
534 syn_timeout
= syn_retries
* LTTCOMM_INET_SYN_TIMEOUT_FACTOR
;
537 * Get the maximum between the two possible timeout value and use that to
538 * get the maximum with the default timeout.
540 lttcomm_inet_tcp_timeout
= max_t(unsigned long,
541 max_t(unsigned long, syn_timeout
, fin_timeout
),
542 lttcomm_inet_tcp_timeout
);
545 DBG("TCP inet operation timeout set to %lu sec", lttcomm_inet_tcp_timeout
);