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>
32 #include <common/common.h>
36 #define MSEC_PER_SEC 1000
37 #define NSEC_PER_MSEC 1000000
38 #define RECONNECT_DELAY 200 /* ms */
41 * INET protocol operations.
43 static const struct lttcomm_proto_ops inet_ops
= {
44 .bind
= lttcomm_bind_inet_sock
,
45 .close
= lttcomm_close_inet_sock
,
46 .connect
= lttcomm_connect_inet_sock
,
47 .accept
= lttcomm_accept_inet_sock
,
48 .listen
= lttcomm_listen_inet_sock
,
49 .recvmsg
= lttcomm_recvmsg_inet_sock
,
50 .sendmsg
= lttcomm_sendmsg_inet_sock
,
53 unsigned long lttcomm_inet_tcp_timeout
;
56 * Creates an PF_INET socket.
59 int lttcomm_create_inet_sock(struct lttcomm_sock
*sock
, int type
, int proto
)
62 unsigned long timeout
;
64 /* Create server socket */
65 if ((sock
->fd
= socket(PF_INET
, type
, proto
)) < 0) {
66 PERROR("socket inet");
70 sock
->ops
= &inet_ops
;
73 * Set socket option to reuse the address.
75 ret
= setsockopt(sock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(int));
77 PERROR("setsockopt inet");
80 timeout
= lttcomm_get_network_timeout();
82 ret
= lttcomm_setsockopt_rcv_timeout(sock
->fd
, timeout
);
86 ret
= lttcomm_setsockopt_snd_timeout(sock
->fd
, timeout
);
99 * Bind socket and return.
102 int lttcomm_bind_inet_sock(struct lttcomm_sock
*sock
)
106 ret
= bind(sock
->fd
, &sock
->sockaddr
.addr
.sin
,
107 sizeof(sock
->sockaddr
.addr
.sin
));
116 int connect_no_timeout(struct lttcomm_sock
*sock
)
118 return connect(sock
->fd
, (struct sockaddr
*) &sock
->sockaddr
.addr
.sin
,
119 sizeof(sock
->sockaddr
.addr
.sin
));
123 * Return time_a - time_b in milliseconds.
126 unsigned long time_diff_ms(struct timespec
*time_a
,
127 struct timespec
*time_b
)
131 unsigned long result_ms
;
133 sec_diff
= time_a
->tv_sec
- time_b
->tv_sec
;
134 nsec_diff
= time_a
->tv_nsec
- time_b
->tv_nsec
;
136 result_ms
= sec_diff
* MSEC_PER_SEC
;
137 result_ms
+= nsec_diff
/ NSEC_PER_MSEC
;
142 int connect_with_timeout(struct lttcomm_sock
*sock
)
144 unsigned long timeout
= lttcomm_get_network_timeout();
145 int ret
, flags
, connect_ret
;
146 struct timespec orig_time
, cur_time
;
148 ret
= fcntl(sock
->fd
, F_GETFL
, 0);
155 /* Set socket to nonblock */
156 ret
= fcntl(sock
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
162 ret
= clock_gettime(CLOCK_MONOTONIC
, &orig_time
);
164 PERROR("clock_gettime");
168 connect_ret
= connect(sock
->fd
,
169 (struct sockaddr
*) &sock
->sockaddr
.addr
.sin
,
170 sizeof(sock
->sockaddr
.addr
.sin
));
171 if (connect_ret
== -1 && errno
!= EAGAIN
172 && errno
!= EWOULDBLOCK
173 && errno
!= EINPROGRESS
) {
175 } else if (!connect_ret
) {
176 /* Connect succeeded */
181 * Perform poll loop following EINPROGRESS recommendation from
182 * connect(2) man page.
188 fds
.events
= POLLOUT
;
190 ret
= poll(&fds
, 1, RECONNECT_DELAY
);
193 } else if (ret
> 0) {
195 socklen_t optval_len
= sizeof(optval
);
197 if (!(fds
.revents
& POLLOUT
)) {
198 /* Either hup or error */
203 ret
= getsockopt(sock
->fd
, SOL_SOCKET
,
204 SO_ERROR
, &optval
, &optval_len
);
215 /* ret == 0: timeout */
216 ret
= clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
218 PERROR("clock_gettime");
222 } while (time_diff_ms(&cur_time
, &orig_time
) < timeout
);
229 /* Restore initial flags */
230 ret
= fcntl(sock
->fd
, F_SETFL
, flags
);
233 /* Continue anyway */
240 * Connect PF_INET socket.
243 int lttcomm_connect_inet_sock(struct lttcomm_sock
*sock
)
247 if (lttcomm_get_network_timeout()) {
248 ret
= connect_with_timeout(sock
);
250 ret
= connect_no_timeout(sock
);
260 closeret
= close(sock
->fd
);
262 PERROR("close inet");
269 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
270 * MUST be bind(2) before.
273 struct lttcomm_sock
*lttcomm_accept_inet_sock(struct lttcomm_sock
*sock
)
277 struct lttcomm_sock
*new_sock
;
278 unsigned long timeout
;
280 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
282 * accept(2) does not exist for UDP so simply return the passed socket.
288 new_sock
= lttcomm_alloc_sock(sock
->proto
);
289 if (new_sock
== NULL
) {
293 len
= sizeof(new_sock
->sockaddr
.addr
.sin
);
296 new_fd
= accept(sock
->fd
, (struct sockaddr
*) &new_sock
->sockaddr
.addr
.sin
,
299 PERROR("accept inet");
302 timeout
= lttcomm_get_network_timeout();
306 ret
= lttcomm_setsockopt_rcv_timeout(new_fd
, timeout
);
310 ret
= lttcomm_setsockopt_snd_timeout(new_fd
, timeout
);
316 new_sock
->fd
= new_fd
;
317 new_sock
->ops
= &inet_ops
;
323 if (close(new_fd
) < 0) {
324 PERROR("accept inet close fd");
333 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
336 int lttcomm_listen_inet_sock(struct lttcomm_sock
*sock
, int backlog
)
340 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
341 /* listen(2) does not exist for UDP so simply return success. */
346 /* Default listen backlog */
348 backlog
= LTTNG_SESSIOND_COMM_MAX_LISTEN
;
351 ret
= listen(sock
->fd
, backlog
);
353 PERROR("listen inet");
361 * Receive data of size len in put that data into the buf param. Using recvmsg
364 * Return the size of received data.
367 ssize_t
lttcomm_recvmsg_inet_sock(struct lttcomm_sock
*sock
, void *buf
,
368 size_t len
, int flags
)
375 memset(&msg
, 0, sizeof(msg
));
377 iov
[0].iov_base
= buf
;
378 iov
[0].iov_len
= len
;
382 msg
.msg_name
= (struct sockaddr
*) &sock
->sockaddr
.addr
.sin
;
383 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin
);
386 len_last
= iov
[0].iov_len
;
387 ret
= recvmsg(sock
->fd
, &msg
, flags
);
389 iov
[0].iov_base
+= ret
;
390 iov
[0].iov_len
-= ret
;
391 assert(ret
<= len_last
);
393 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
395 PERROR("recvmsg inet");
396 } else if (ret
> 0) {
399 /* Else ret = 0 meaning an orderly shutdown. */
405 * Send buf data of size len. Using sendmsg API.
407 * Return the size of sent data.
410 ssize_t
lttcomm_sendmsg_inet_sock(struct lttcomm_sock
*sock
, void *buf
,
411 size_t len
, int flags
)
417 memset(&msg
, 0, sizeof(msg
));
419 iov
[0].iov_base
= buf
;
420 iov
[0].iov_len
= len
;
424 switch (sock
->proto
) {
425 case LTTCOMM_SOCK_UDP
:
426 msg
.msg_name
= (struct sockaddr
*) &sock
->sockaddr
.addr
.sin
;
427 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin
);
434 ret
= sendmsg(sock
->fd
, &msg
, flags
);
435 } while (ret
< 0 && errno
== EINTR
);
438 * Only warn about EPIPE when quiet mode is deactivated.
439 * We consider EPIPE as expected.
441 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
442 PERROR("sendmsg inet");
450 * Shutdown cleanly and close.
453 int lttcomm_close_inet_sock(struct lttcomm_sock
*sock
)
457 /* Don't try to close an invalid marked socket */
458 if (sock
->fd
== -1) {
462 ret
= close(sock
->fd
);
464 PERROR("close inet");
474 * Return value read from /proc or else 0 if value is not found.
476 static unsigned long read_proc_value(const char *path
)
481 unsigned long val
= 0;
484 fd
= open(path
, O_RDONLY
);
489 size_ret
= lttng_read(fd
, buf
, sizeof(buf
));
491 * Allow reading a file smaller than buf, but keep space for
494 if (size_ret
< 0 || size_ret
>= sizeof(buf
)) {
495 PERROR("read proc failed");
498 buf
[size_ret
] = '\0';
501 r_val
= strtol(buf
, NULL
, 10);
502 if (errno
!= 0 || r_val
< -1L) {
514 PERROR("close /proc value");
521 void lttcomm_inet_init(void)
523 unsigned long syn_retries
, fin_timeout
, syn_timeout
, env
;
525 env
= lttcomm_get_network_timeout();
527 lttcomm_inet_tcp_timeout
= env
;
531 /* Assign default value and see if we can change it. */
532 lttcomm_inet_tcp_timeout
= DEFAULT_INET_TCP_TIMEOUT
;
534 syn_retries
= read_proc_value(LTTCOMM_INET_PROC_SYN_RETRIES_PATH
);
535 fin_timeout
= read_proc_value(LTTCOMM_INET_PROC_FIN_TIMEOUT_PATH
);
537 syn_timeout
= syn_retries
* LTTCOMM_INET_SYN_TIMEOUT_FACTOR
;
540 * Get the maximum between the two possible timeout value and use that to
541 * get the maximum with the default timeout.
543 lttcomm_inet_tcp_timeout
= max_t(unsigned long,
544 max_t(unsigned long, syn_timeout
, fin_timeout
),
545 lttcomm_inet_tcp_timeout
);
548 DBG("TCP inet operation timeout set to %lu sec", lttcomm_inet_tcp_timeout
);