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
)
105 ret
= bind(sock
->fd
, (const struct sockaddr
*) &sock
->sockaddr
.addr
.sin
,
106 sizeof(sock
->sockaddr
.addr
.sin
));
115 int connect_no_timeout(struct lttcomm_sock
*sock
)
117 return connect(sock
->fd
, (struct sockaddr
*) &sock
->sockaddr
.addr
.sin
,
118 sizeof(sock
->sockaddr
.addr
.sin
));
122 * Return time_a - time_b in milliseconds.
125 unsigned long time_diff_ms(struct timespec
*time_a
,
126 struct timespec
*time_b
)
130 unsigned long result_ms
;
132 sec_diff
= time_a
->tv_sec
- time_b
->tv_sec
;
133 nsec_diff
= time_a
->tv_nsec
- time_b
->tv_nsec
;
135 result_ms
= sec_diff
* MSEC_PER_SEC
;
136 result_ms
+= nsec_diff
/ NSEC_PER_MSEC
;
141 int connect_with_timeout(struct lttcomm_sock
*sock
)
143 unsigned long timeout
= lttcomm_get_network_timeout();
144 int ret
, flags
, connect_ret
;
145 struct timespec orig_time
, cur_time
;
147 ret
= fcntl(sock
->fd
, F_GETFL
, 0);
154 /* Set socket to nonblock */
155 ret
= fcntl(sock
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
161 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &orig_time
);
163 PERROR("clock_gettime");
167 connect_ret
= connect(sock
->fd
,
168 (struct sockaddr
*) &sock
->sockaddr
.addr
.sin
,
169 sizeof(sock
->sockaddr
.addr
.sin
));
170 if (connect_ret
== -1 && errno
!= EAGAIN
171 && errno
!= EWOULDBLOCK
172 && errno
!= EINPROGRESS
) {
174 } else if (!connect_ret
) {
175 /* Connect succeeded */
179 DBG("Asynchronous connect for sock %d, performing polling with"
180 " timeout: %lums", sock
->fd
, timeout
);
182 * Perform poll loop following EINPROGRESS recommendation from
183 * connect(2) man page.
189 fds
.events
= POLLOUT
;
191 ret
= poll(&fds
, 1, RECONNECT_DELAY
);
194 } else if (ret
> 0) {
196 socklen_t optval_len
= sizeof(optval
);
198 if (!(fds
.revents
& POLLOUT
)) {
199 /* Either hup or error */
204 ret
= getsockopt(sock
->fd
, SOL_SOCKET
,
205 SO_ERROR
, &optval
, &optval_len
);
207 PERROR("getsockopt");
214 /* Get actual connect() errno from opt_val */
219 /* ret == 0: timeout */
220 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
222 PERROR("clock_gettime");
226 } while (time_diff_ms(&cur_time
, &orig_time
) < timeout
);
233 /* Restore initial flags */
234 ret
= fcntl(sock
->fd
, F_SETFL
, flags
);
237 /* Continue anyway */
244 * Connect PF_INET socket.
247 int lttcomm_connect_inet_sock(struct lttcomm_sock
*sock
)
251 if (lttcomm_get_network_timeout()) {
252 ret
= connect_with_timeout(sock
);
254 ret
= connect_no_timeout(sock
);
264 closeret
= close(sock
->fd
);
266 PERROR("close inet");
273 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
274 * MUST be bind(2) before.
277 struct lttcomm_sock
*lttcomm_accept_inet_sock(struct lttcomm_sock
*sock
)
281 struct lttcomm_sock
*new_sock
;
282 unsigned long timeout
;
284 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
286 * accept(2) does not exist for UDP so simply return the passed socket.
292 new_sock
= lttcomm_alloc_sock(sock
->proto
);
293 if (new_sock
== NULL
) {
297 len
= sizeof(new_sock
->sockaddr
.addr
.sin
);
300 new_fd
= accept(sock
->fd
, (struct sockaddr
*) &new_sock
->sockaddr
.addr
.sin
,
303 PERROR("accept inet");
306 timeout
= lttcomm_get_network_timeout();
310 ret
= lttcomm_setsockopt_rcv_timeout(new_fd
, timeout
);
314 ret
= lttcomm_setsockopt_snd_timeout(new_fd
, timeout
);
320 new_sock
->fd
= new_fd
;
321 new_sock
->ops
= &inet_ops
;
327 if (close(new_fd
) < 0) {
328 PERROR("accept inet close fd");
337 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
340 int lttcomm_listen_inet_sock(struct lttcomm_sock
*sock
, int backlog
)
344 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
345 /* listen(2) does not exist for UDP so simply return success. */
350 /* Default listen backlog */
352 backlog
= LTTNG_SESSIOND_COMM_MAX_LISTEN
;
355 ret
= listen(sock
->fd
, backlog
);
357 PERROR("listen inet");
365 * Receive data of size len in put that data into the buf param. Using recvmsg
368 * Return the size of received data.
371 ssize_t
lttcomm_recvmsg_inet_sock(struct lttcomm_sock
*sock
, void *buf
,
372 size_t len
, int flags
)
379 memset(&msg
, 0, sizeof(msg
));
381 iov
[0].iov_base
= buf
;
382 iov
[0].iov_len
= len
;
386 msg
.msg_name
= (struct sockaddr
*) &sock
->sockaddr
.addr
.sin
;
387 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin
);
390 len_last
= iov
[0].iov_len
;
391 ret
= recvmsg(sock
->fd
, &msg
, flags
);
393 iov
[0].iov_base
+= ret
;
394 iov
[0].iov_len
-= ret
;
395 assert(ret
<= len_last
);
397 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
399 PERROR("recvmsg inet");
400 } else if (ret
> 0) {
403 /* Else ret = 0 meaning an orderly shutdown. */
409 * Send buf data of size len. Using sendmsg API.
411 * Return the size of sent data.
414 ssize_t
lttcomm_sendmsg_inet_sock(struct lttcomm_sock
*sock
, const void *buf
,
415 size_t len
, int flags
)
421 memset(&msg
, 0, sizeof(msg
));
423 iov
[0].iov_base
= (void *) buf
;
424 iov
[0].iov_len
= len
;
428 switch (sock
->proto
) {
429 case LTTCOMM_SOCK_UDP
:
430 msg
.msg_name
= (struct sockaddr
*) &sock
->sockaddr
.addr
.sin
;
431 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin
);
438 ret
= sendmsg(sock
->fd
, &msg
, flags
);
439 } while (ret
< 0 && errno
== EINTR
);
442 * Only warn about EPIPE when quiet mode is deactivated.
443 * We consider EPIPE as expected.
445 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
446 PERROR("sendmsg inet");
454 * Shutdown cleanly and close.
457 int lttcomm_close_inet_sock(struct lttcomm_sock
*sock
)
461 /* Don't try to close an invalid marked socket */
462 if (sock
->fd
== -1) {
466 ret
= close(sock
->fd
);
468 PERROR("close inet");
478 * Return value read from /proc or else 0 if value is not found.
480 static unsigned long read_proc_value(const char *path
)
485 unsigned long val
= 0;
488 fd
= open(path
, O_RDONLY
);
493 size_ret
= lttng_read(fd
, buf
, sizeof(buf
));
495 * Allow reading a file smaller than buf, but keep space for
498 if (size_ret
< 0 || size_ret
>= sizeof(buf
)) {
499 PERROR("read proc failed");
502 buf
[size_ret
] = '\0';
505 r_val
= strtol(buf
, NULL
, 10);
506 if (errno
!= 0 || r_val
< -1L) {
518 PERROR("close /proc value");
525 void lttcomm_inet_init(void)
527 unsigned long syn_retries
, fin_timeout
, syn_timeout
, env
;
529 env
= lttcomm_get_network_timeout();
531 lttcomm_inet_tcp_timeout
= env
;
535 /* Assign default value and see if we can change it. */
536 lttcomm_inet_tcp_timeout
= DEFAULT_INET_TCP_TIMEOUT
;
538 syn_retries
= read_proc_value(LTTCOMM_INET_PROC_SYN_RETRIES_PATH
);
539 fin_timeout
= read_proc_value(LTTCOMM_INET_PROC_FIN_TIMEOUT_PATH
);
541 syn_timeout
= syn_retries
* LTTCOMM_INET_SYN_TIMEOUT_FACTOR
;
544 * Get the maximum between the two possible timeout value and use that to
545 * get the maximum with the default timeout.
547 lttcomm_inet_tcp_timeout
= max_t(unsigned long,
548 max_t(unsigned long, syn_timeout
, fin_timeout
),
549 lttcomm_inet_tcp_timeout
);
552 DBG("TCP inet operation timeout set to %lu sec", lttcomm_inet_tcp_timeout
);