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
*) ALIGNED_CONST_PTR(
106 sock
->sockaddr
.addr
.sin
),
107 sizeof(sock
->sockaddr
.addr
.sin
));
116 int connect_no_timeout(struct lttcomm_sock
*sock
)
118 return connect(sock
->fd
,
119 (const struct sockaddr
*) ALIGNED_CONST_PTR(
120 sock
->sockaddr
.addr
.sin
),
121 sizeof(sock
->sockaddr
.addr
.sin
));
125 int connect_with_timeout(struct lttcomm_sock
*sock
)
127 unsigned long timeout
= lttcomm_get_network_timeout();
128 int ret
, flags
, connect_ret
;
129 struct timespec orig_time
, cur_time
;
130 unsigned long diff_ms
;
132 ret
= fcntl(sock
->fd
, F_GETFL
, 0);
139 /* Set socket to nonblock */
140 ret
= fcntl(sock
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
146 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &orig_time
);
148 PERROR("clock_gettime");
152 connect_ret
= connect(sock
->fd
,
153 (const struct sockaddr
*) ALIGNED_CONST_PTR(
154 sock
->sockaddr
.addr
.sin
),
155 sizeof(sock
->sockaddr
.addr
.sin
));
156 if (connect_ret
== -1 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
&&
157 errno
!= EINPROGRESS
) {
159 } else if (!connect_ret
) {
160 /* Connect succeeded */
164 DBG("Asynchronous connect for sock %d, performing polling with"
165 " timeout: %lums", sock
->fd
, timeout
);
167 * Perform poll loop following EINPROGRESS recommendation from
168 * connect(2) man page.
174 fds
.events
= POLLOUT
;
176 ret
= poll(&fds
, 1, RECONNECT_DELAY
);
179 } else if (ret
> 0) {
181 socklen_t optval_len
= sizeof(optval
);
183 if (!(fds
.revents
& POLLOUT
)) {
184 /* Either hup or error */
189 ret
= getsockopt(sock
->fd
, SOL_SOCKET
,
190 SO_ERROR
, &optval
, &optval_len
);
192 PERROR("getsockopt");
199 /* Get actual connect() errno from opt_val */
204 /* ret == 0: timeout */
205 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
207 PERROR("clock_gettime");
211 if (timespec_to_ms(timespec_abs_diff(cur_time
, orig_time
), &diff_ms
) < 0) {
212 ERR("timespec_to_ms input overflows milliseconds output");
216 } while (diff_ms
< timeout
);
223 /* Restore initial flags */
224 ret
= fcntl(sock
->fd
, F_SETFL
, flags
);
227 /* Continue anyway */
234 * Connect PF_INET socket.
237 int lttcomm_connect_inet_sock(struct lttcomm_sock
*sock
)
241 if (lttcomm_get_network_timeout()) {
242 ret
= connect_with_timeout(sock
);
244 ret
= connect_no_timeout(sock
);
254 closeret
= close(sock
->fd
);
256 PERROR("close inet");
263 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
264 * MUST be bind(2) before.
267 struct lttcomm_sock
*lttcomm_accept_inet_sock(struct lttcomm_sock
*sock
)
271 struct lttcomm_sock
*new_sock
;
272 unsigned long timeout
;
273 struct sockaddr_in new_addr
= {};
275 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
277 * accept(2) does not exist for UDP so simply return the passed socket.
283 new_sock
= lttcomm_alloc_sock(sock
->proto
);
284 if (new_sock
== NULL
) {
288 len
= sizeof(new_addr
);
291 new_fd
= accept(sock
->fd
, (struct sockaddr
*) &new_addr
, &len
);
293 PERROR("accept inet");
296 new_sock
->sockaddr
.addr
.sin
= new_addr
;
297 timeout
= lttcomm_get_network_timeout();
301 ret
= lttcomm_setsockopt_rcv_timeout(new_fd
, timeout
);
305 ret
= lttcomm_setsockopt_snd_timeout(new_fd
, timeout
);
311 new_sock
->fd
= new_fd
;
312 new_sock
->ops
= &inet_ops
;
318 if (close(new_fd
) < 0) {
319 PERROR("accept inet close fd");
328 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
331 int lttcomm_listen_inet_sock(struct lttcomm_sock
*sock
, int backlog
)
335 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
336 /* listen(2) does not exist for UDP so simply return success. */
341 /* Default listen backlog */
343 backlog
= LTTNG_SESSIOND_COMM_MAX_LISTEN
;
346 ret
= listen(sock
->fd
, backlog
);
348 PERROR("listen inet");
356 * Receive data of size len in put that data into the buf param. Using recvmsg
359 * Return the size of received data.
362 ssize_t
lttcomm_recvmsg_inet_sock(struct lttcomm_sock
*sock
, void *buf
,
363 size_t len
, int flags
)
369 struct sockaddr_in addr
= sock
->sockaddr
.addr
.sin
;
371 memset(&msg
, 0, sizeof(msg
));
373 iov
[0].iov_base
= buf
;
374 iov
[0].iov_len
= len
;
378 msg
.msg_name
= (struct sockaddr
*) &addr
;
379 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin
);
382 len_last
= iov
[0].iov_len
;
383 ret
= recvmsg(sock
->fd
, &msg
, flags
);
385 iov
[0].iov_base
+= ret
;
386 iov
[0].iov_len
-= ret
;
387 assert(ret
<= len_last
);
389 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
391 PERROR("recvmsg inet");
392 } else if (ret
> 0) {
395 /* Else ret = 0 meaning an orderly shutdown. */
401 * Send buf data of size len. Using sendmsg API.
403 * Return the size of sent data.
406 ssize_t
lttcomm_sendmsg_inet_sock(struct lttcomm_sock
*sock
, const void *buf
,
407 size_t len
, int flags
)
413 memset(&msg
, 0, sizeof(msg
));
415 iov
[0].iov_base
= (void *) buf
;
416 iov
[0].iov_len
= len
;
420 switch (sock
->proto
) {
421 case LTTCOMM_SOCK_UDP
:
423 struct sockaddr_in addr
= sock
->sockaddr
.addr
.sin
;
425 msg
.msg_name
= (struct sockaddr
*) &addr
;
426 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
);