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
*) &sock
->sockaddr
.addr
.sin
,
105 sizeof(sock
->sockaddr
.addr
.sin
));
109 int connect_no_timeout(struct lttcomm_sock
*sock
)
111 return connect(sock
->fd
, (struct sockaddr
*) &sock
->sockaddr
.addr
.sin
,
112 sizeof(sock
->sockaddr
.addr
.sin
));
116 int connect_with_timeout(struct lttcomm_sock
*sock
)
118 unsigned long timeout
= lttcomm_get_network_timeout();
119 int ret
, flags
, connect_ret
;
120 struct timespec orig_time
, cur_time
;
121 unsigned long diff_ms
;
123 ret
= fcntl(sock
->fd
, F_GETFL
, 0);
130 /* Set socket to nonblock */
131 ret
= fcntl(sock
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
137 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &orig_time
);
139 PERROR("clock_gettime");
143 connect_ret
= connect(sock
->fd
,
144 (struct sockaddr
*) &sock
->sockaddr
.addr
.sin
,
145 sizeof(sock
->sockaddr
.addr
.sin
));
146 if (connect_ret
== -1 && errno
!= EAGAIN
147 && errno
!= EWOULDBLOCK
148 && errno
!= EINPROGRESS
) {
150 } else if (!connect_ret
) {
151 /* Connect succeeded */
155 DBG("Asynchronous connect for sock %d, performing polling with"
156 " timeout: %lums", sock
->fd
, timeout
);
158 * Perform poll loop following EINPROGRESS recommendation from
159 * connect(2) man page.
165 fds
.events
= POLLOUT
;
167 ret
= poll(&fds
, 1, RECONNECT_DELAY
);
170 } else if (ret
> 0) {
172 socklen_t optval_len
= sizeof(optval
);
174 if (!(fds
.revents
& POLLOUT
)) {
175 /* Either hup or error */
180 ret
= getsockopt(sock
->fd
, SOL_SOCKET
,
181 SO_ERROR
, &optval
, &optval_len
);
183 PERROR("getsockopt");
190 /* Get actual connect() errno from opt_val */
195 /* ret == 0: timeout */
196 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
198 PERROR("clock_gettime");
202 if (timespec_to_ms(timespec_abs_diff(cur_time
, orig_time
), &diff_ms
) < 0) {
203 ERR("timespec_to_ms input overflows milliseconds output");
207 } while (diff_ms
< timeout
);
214 /* Restore initial flags */
215 ret
= fcntl(sock
->fd
, F_SETFL
, flags
);
218 /* Continue anyway */
225 * Connect PF_INET socket.
228 int lttcomm_connect_inet_sock(struct lttcomm_sock
*sock
)
232 if (lttcomm_get_network_timeout()) {
233 ret
= connect_with_timeout(sock
);
235 ret
= connect_no_timeout(sock
);
245 closeret
= close(sock
->fd
);
247 PERROR("close inet");
254 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
255 * MUST be bind(2) before.
258 struct lttcomm_sock
*lttcomm_accept_inet_sock(struct lttcomm_sock
*sock
)
262 struct lttcomm_sock
*new_sock
;
263 unsigned long timeout
;
265 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
267 * accept(2) does not exist for UDP so simply return the passed socket.
273 new_sock
= lttcomm_alloc_sock(sock
->proto
);
274 if (new_sock
== NULL
) {
278 len
= sizeof(new_sock
->sockaddr
.addr
.sin
);
281 new_fd
= accept(sock
->fd
, (struct sockaddr
*) &new_sock
->sockaddr
.addr
.sin
,
284 PERROR("accept inet");
287 timeout
= lttcomm_get_network_timeout();
291 ret
= lttcomm_setsockopt_rcv_timeout(new_fd
, timeout
);
295 ret
= lttcomm_setsockopt_snd_timeout(new_fd
, timeout
);
301 new_sock
->fd
= new_fd
;
302 new_sock
->ops
= &inet_ops
;
308 if (close(new_fd
) < 0) {
309 PERROR("accept inet close fd");
318 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
321 int lttcomm_listen_inet_sock(struct lttcomm_sock
*sock
, int backlog
)
325 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
326 /* listen(2) does not exist for UDP so simply return success. */
331 /* Default listen backlog */
333 backlog
= LTTNG_SESSIOND_COMM_MAX_LISTEN
;
336 ret
= listen(sock
->fd
, backlog
);
338 PERROR("listen inet");
346 * Receive data of size len in put that data into the buf param. Using recvmsg
349 * Return the size of received data.
352 ssize_t
lttcomm_recvmsg_inet_sock(struct lttcomm_sock
*sock
, void *buf
,
353 size_t len
, int flags
)
360 memset(&msg
, 0, sizeof(msg
));
362 iov
[0].iov_base
= buf
;
363 iov
[0].iov_len
= len
;
367 msg
.msg_name
= (struct sockaddr
*) &sock
->sockaddr
.addr
.sin
;
368 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin
);
371 len_last
= iov
[0].iov_len
;
372 ret
= recvmsg(sock
->fd
, &msg
, flags
);
374 if (flags
& MSG_DONTWAIT
) {
377 iov
[0].iov_base
+= ret
;
378 iov
[0].iov_len
-= ret
;
379 assert(ret
<= len_last
);
381 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
384 PERROR("recvmsg inet");
385 } else if (ret
> 0) {
388 /* Else ret = 0 meaning an orderly shutdown. */
394 * Send buf data of size len. Using sendmsg API.
396 * Return the size of sent data.
399 ssize_t
lttcomm_sendmsg_inet_sock(struct lttcomm_sock
*sock
, const void *buf
,
400 size_t len
, int flags
)
406 memset(&msg
, 0, sizeof(msg
));
408 iov
[0].iov_base
= (void *) buf
;
409 iov
[0].iov_len
= len
;
413 switch (sock
->proto
) {
414 case LTTCOMM_SOCK_UDP
:
415 msg
.msg_name
= (struct sockaddr
*) &sock
->sockaddr
.addr
.sin
;
416 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.
442 int lttcomm_close_inet_sock(struct lttcomm_sock
*sock
)
446 /* Don't try to close an invalid marked socket */
447 if (sock
->fd
== -1) {
451 ret
= close(sock
->fd
);
453 PERROR("close inet");
463 * Return value read from /proc or else 0 if value is not found.
465 static unsigned long read_proc_value(const char *path
)
470 unsigned long val
= 0;
473 fd
= open(path
, O_RDONLY
);
478 size_ret
= lttng_read(fd
, buf
, sizeof(buf
));
480 * Allow reading a file smaller than buf, but keep space for
483 if (size_ret
< 0 || size_ret
>= sizeof(buf
)) {
484 PERROR("read proc failed");
487 buf
[size_ret
] = '\0';
490 r_val
= strtol(buf
, NULL
, 10);
491 if (errno
!= 0 || r_val
< -1L) {
503 PERROR("close /proc value");
510 void lttcomm_inet_init(void)
512 unsigned long syn_retries
, fin_timeout
, syn_timeout
, env
;
514 env
= lttcomm_get_network_timeout();
516 lttcomm_inet_tcp_timeout
= env
;
520 /* Assign default value and see if we can change it. */
521 lttcomm_inet_tcp_timeout
= DEFAULT_INET_TCP_TIMEOUT
;
523 syn_retries
= read_proc_value(LTTCOMM_INET_PROC_SYN_RETRIES_PATH
);
524 fin_timeout
= read_proc_value(LTTCOMM_INET_PROC_FIN_TIMEOUT_PATH
);
526 syn_timeout
= syn_retries
* LTTCOMM_INET_SYN_TIMEOUT_FACTOR
;
529 * Get the maximum between the two possible timeout value and use that to
530 * get the maximum with the default timeout.
532 lttcomm_inet_tcp_timeout
= max_t(unsigned long,
533 max_t(unsigned long, syn_timeout
, fin_timeout
),
534 lttcomm_inet_tcp_timeout
);
537 DBG("TCP inet operation timeout set to %lu sec", lttcomm_inet_tcp_timeout
);