2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <sys/types.h>
29 #include <common/compat/time.h>
32 #include <common/common.h>
33 #include <common/time.h>
37 #define RECONNECT_DELAY 200 /* ms */
40 * INET protocol operations.
42 static const struct lttcomm_proto_ops inet_ops
= {
43 .bind
= lttcomm_bind_inet_sock
,
44 .close
= lttcomm_close_inet_sock
,
45 .connect
= lttcomm_connect_inet_sock
,
46 .accept
= lttcomm_accept_inet_sock
,
47 .listen
= lttcomm_listen_inet_sock
,
48 .recvmsg
= lttcomm_recvmsg_inet_sock
,
49 .sendmsg
= lttcomm_sendmsg_inet_sock
,
52 unsigned long lttcomm_inet_tcp_timeout
;
55 * Creates an PF_INET socket.
58 int lttcomm_create_inet_sock(struct lttcomm_sock
*sock
, int type
, int proto
)
61 unsigned long timeout
;
63 /* Create server socket */
64 if ((sock
->fd
= socket(PF_INET
, type
, proto
)) < 0) {
65 PERROR("socket inet");
69 sock
->ops
= &inet_ops
;
72 * Set socket option to reuse the address.
74 ret
= setsockopt(sock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(int));
76 PERROR("setsockopt inet");
79 timeout
= lttcomm_get_network_timeout();
81 ret
= lttcomm_setsockopt_rcv_timeout(sock
->fd
, timeout
);
85 ret
= lttcomm_setsockopt_snd_timeout(sock
->fd
, timeout
);
98 * Bind socket and return.
101 int lttcomm_bind_inet_sock(struct lttcomm_sock
*sock
)
103 return bind(sock
->fd
,
104 (const struct sockaddr
*) ALIGNED_CONST_PTR(
105 sock
->sockaddr
.addr
.sin
),
106 sizeof(sock
->sockaddr
.addr
.sin
));
110 int connect_no_timeout(struct lttcomm_sock
*sock
)
112 return connect(sock
->fd
,
113 (const struct sockaddr
*) ALIGNED_CONST_PTR(
114 sock
->sockaddr
.addr
.sin
),
115 sizeof(sock
->sockaddr
.addr
.sin
));
119 int connect_with_timeout(struct lttcomm_sock
*sock
)
121 unsigned long timeout
= lttcomm_get_network_timeout();
122 int ret
, flags
, connect_ret
;
123 struct timespec orig_time
, cur_time
;
124 unsigned long diff_ms
;
126 ret
= fcntl(sock
->fd
, F_GETFL
, 0);
133 /* Set socket to nonblock */
134 ret
= fcntl(sock
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
140 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &orig_time
);
142 PERROR("clock_gettime");
146 connect_ret
= connect(sock
->fd
,
147 (const struct sockaddr
*) ALIGNED_CONST_PTR(
148 sock
->sockaddr
.addr
.sin
),
149 sizeof(sock
->sockaddr
.addr
.sin
));
150 if (connect_ret
== -1 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
&&
151 errno
!= EINPROGRESS
) {
153 } else if (!connect_ret
) {
154 /* Connect succeeded */
158 DBG("Asynchronous connect for sock %d, performing polling with"
159 " timeout: %lums", sock
->fd
, timeout
);
161 * Perform poll loop following EINPROGRESS recommendation from
162 * connect(2) man page.
168 fds
.events
= POLLOUT
;
170 ret
= poll(&fds
, 1, RECONNECT_DELAY
);
173 } else if (ret
> 0) {
175 socklen_t optval_len
= sizeof(optval
);
177 if (!(fds
.revents
& POLLOUT
)) {
178 /* Either hup or error */
183 ret
= getsockopt(sock
->fd
, SOL_SOCKET
,
184 SO_ERROR
, &optval
, &optval_len
);
186 PERROR("getsockopt");
193 /* Get actual connect() errno from opt_val */
198 /* ret == 0: timeout */
199 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
201 PERROR("clock_gettime");
205 if (timespec_to_ms(timespec_abs_diff(cur_time
, orig_time
), &diff_ms
) < 0) {
206 ERR("timespec_to_ms input overflows milliseconds output");
210 } while (diff_ms
< timeout
);
217 /* Restore initial flags */
218 ret
= fcntl(sock
->fd
, F_SETFL
, flags
);
221 /* Continue anyway */
228 * Connect PF_INET socket.
231 int lttcomm_connect_inet_sock(struct lttcomm_sock
*sock
)
235 if (lttcomm_get_network_timeout()) {
236 ret
= connect_with_timeout(sock
);
238 ret
= connect_no_timeout(sock
);
248 closeret
= close(sock
->fd
);
250 PERROR("close inet");
257 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
258 * MUST be bind(2) before.
261 struct lttcomm_sock
*lttcomm_accept_inet_sock(struct lttcomm_sock
*sock
)
265 struct lttcomm_sock
*new_sock
;
266 unsigned long timeout
;
267 struct sockaddr_in new_addr
= {};
269 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
271 * accept(2) does not exist for UDP so simply return the passed socket.
277 new_sock
= lttcomm_alloc_sock(sock
->proto
);
278 if (new_sock
== NULL
) {
282 len
= sizeof(new_addr
);
285 new_fd
= accept(sock
->fd
, (struct sockaddr
*) &new_addr
, &len
);
287 PERROR("accept inet");
290 new_sock
->sockaddr
.addr
.sin
= new_addr
;
291 timeout
= lttcomm_get_network_timeout();
295 ret
= lttcomm_setsockopt_rcv_timeout(new_fd
, timeout
);
299 ret
= lttcomm_setsockopt_snd_timeout(new_fd
, timeout
);
305 new_sock
->fd
= new_fd
;
306 new_sock
->ops
= &inet_ops
;
312 if (close(new_fd
) < 0) {
313 PERROR("accept inet close fd");
322 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
325 int lttcomm_listen_inet_sock(struct lttcomm_sock
*sock
, int backlog
)
329 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
330 /* listen(2) does not exist for UDP so simply return success. */
335 /* Default listen backlog */
337 backlog
= LTTNG_SESSIOND_COMM_MAX_LISTEN
;
340 ret
= listen(sock
->fd
, backlog
);
342 PERROR("listen inet");
350 * Receive data of size len in put that data into the buf param. Using recvmsg
353 * Return the size of received data.
356 ssize_t
lttcomm_recvmsg_inet_sock(struct lttcomm_sock
*sock
, void *buf
,
357 size_t len
, int flags
)
363 struct sockaddr_in addr
= sock
->sockaddr
.addr
.sin
;
365 memset(&msg
, 0, sizeof(msg
));
367 iov
[0].iov_base
= buf
;
368 iov
[0].iov_len
= len
;
372 msg
.msg_name
= (struct sockaddr
*) &addr
;
373 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin
);
376 len_last
= iov
[0].iov_len
;
377 ret
= recvmsg(sock
->fd
, &msg
, flags
);
379 if (flags
& MSG_DONTWAIT
) {
382 iov
[0].iov_base
+= ret
;
383 iov
[0].iov_len
-= ret
;
384 assert(ret
<= len_last
);
386 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
389 if (errno
== EAGAIN
&& flags
& MSG_DONTWAIT
) {
391 * EAGAIN is expected in non-blocking mode and should
392 * not be reported as an error. Moreover, if no data
393 * was read, 0 must not be returned as it would be
394 * interpreted as an orderly shutdown of the socket.
398 PERROR("recvmsg inet");
399 } else if (ret
> 0) {
402 /* Else ret = 0 meaning an orderly shutdown. */
408 * Send buf data of size len. Using sendmsg API.
410 * Return the size of sent data.
413 ssize_t
lttcomm_sendmsg_inet_sock(struct lttcomm_sock
*sock
, const void *buf
,
414 size_t len
, int flags
)
420 memset(&msg
, 0, sizeof(msg
));
422 iov
[0].iov_base
= (void *) buf
;
423 iov
[0].iov_len
= len
;
427 switch (sock
->proto
) {
428 case LTTCOMM_SOCK_UDP
:
430 struct sockaddr_in addr
= sock
->sockaddr
.addr
.sin
;
432 msg
.msg_name
= (struct sockaddr
*) &addr
;
433 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin
);
441 ret
= sendmsg(sock
->fd
, &msg
, flags
);
442 } while (ret
< 0 && errno
== EINTR
);
445 * Only warn about EPIPE when quiet mode is deactivated.
446 * We consider EPIPE as expected.
448 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
449 PERROR("sendmsg inet");
457 * Shutdown cleanly and close.
460 int lttcomm_close_inet_sock(struct lttcomm_sock
*sock
)
464 /* Don't try to close an invalid marked socket */
465 if (sock
->fd
== -1) {
469 ret
= close(sock
->fd
);
471 PERROR("close inet");
481 * Return value read from /proc or else 0 if value is not found.
483 static unsigned long read_proc_value(const char *path
)
488 unsigned long val
= 0;
491 fd
= open(path
, O_RDONLY
);
496 size_ret
= lttng_read(fd
, buf
, sizeof(buf
));
498 * Allow reading a file smaller than buf, but keep space for
501 if (size_ret
< 0 || size_ret
>= sizeof(buf
)) {
502 PERROR("read proc failed");
505 buf
[size_ret
] = '\0';
508 r_val
= strtol(buf
, NULL
, 10);
509 if (errno
!= 0 || r_val
< -1L) {
521 PERROR("close /proc value");
528 void lttcomm_inet_init(void)
530 unsigned long syn_retries
, fin_timeout
, syn_timeout
, env
;
532 env
= lttcomm_get_network_timeout();
534 lttcomm_inet_tcp_timeout
= env
;
538 /* Assign default value and see if we can change it. */
539 lttcomm_inet_tcp_timeout
= DEFAULT_INET_TCP_TIMEOUT
;
541 syn_retries
= read_proc_value(LTTCOMM_INET_PROC_SYN_RETRIES_PATH
);
542 fin_timeout
= read_proc_value(LTTCOMM_INET_PROC_FIN_TIMEOUT_PATH
);
544 syn_timeout
= syn_retries
* LTTCOMM_INET_SYN_TIMEOUT_FACTOR
;
547 * Get the maximum between the two possible timeout value and use that to
548 * get the maximum with the default timeout.
550 lttcomm_inet_tcp_timeout
= max_t(unsigned long,
551 max_t(unsigned long, syn_timeout
, fin_timeout
),
552 lttcomm_inet_tcp_timeout
);
555 DBG("TCP inet operation timeout set to %lu sec", lttcomm_inet_tcp_timeout
);