Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

network: tcp keepalive addition #9249

Merged
merged 3 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions include/fluent-bit/flb_network.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ struct flb_net_setup {
/* maximum of times a keepalive connection can be used */
int keepalive_max_recycle;

/* enable/disable tcp keepalive */
int tcp_keepalive;

/* interval between the last data packet sent and the first TCP keepalive probe */
int tcp_keepalive_time;

/* the interval between TCP keepalive probes */
int tcp_keepalive_interval;

/* number of unacknowledged probes to consider a connection dead */
int tcp_keepalive_probes;

/* dns mode : TCP or UDP */
char *dns_mode;

Expand Down Expand Up @@ -154,6 +166,7 @@ int flb_net_socket_blocking(flb_sockfd_t fd);
int flb_net_socket_nonblocking(flb_sockfd_t fd);
int flb_net_socket_rcv_buffer(flb_sockfd_t fd, int rcvbuf);
int flb_net_socket_tcp_fastopen(flb_sockfd_t sockfd);
int flb_net_socket_tcp_keepalive(flb_sockfd_t fd, struct flb_net_setup *net);

/* Socket handling */
flb_sockfd_t flb_net_socket_create(int family, int nonblock);
Expand Down
12 changes: 12 additions & 0 deletions src/flb_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ int flb_io_net_connect(struct flb_connection *connection,
connection->upstream->tcp_port);
}

/* set TCP keepalive and it's options */
if (connection->net->tcp_keepalive) {
ret = flb_net_socket_tcp_keepalive(connection->fd,
connection->net);

if (ret == -1) {
flb_socket_close(fd);

return -1;
}
}

#ifdef FLB_HAVE_TLS
/* Check if TLS was enabled, if so perform the handshake */
if (flb_stream_is_secure(connection->stream) &&
Expand Down
49 changes: 49 additions & 0 deletions src/flb_network.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ void flb_net_setup_init(struct flb_net_setup *net)
net->keepalive = FLB_TRUE;
net->keepalive_idle_timeout = 30;
net->keepalive_max_recycle = 0;
net->tcp_keepalive = FLB_FALSE;
net->tcp_keepalive_time = -1;
net->tcp_keepalive_interval = -1;
net->tcp_keepalive_probes = -1;
net->accept_timeout = 10;
net->connect_timeout = 10;
net->io_timeout = 0; /* Infinite time */
Expand Down Expand Up @@ -298,6 +302,51 @@ int flb_net_socket_tcp_fastopen(flb_sockfd_t fd)
return setsockopt(fd, SOL_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen));
}


/*
* Enable TCP keepalive
*/
int flb_net_socket_tcp_keepalive(flb_sockfd_t fd, struct flb_net_setup *net)
{
int interval;
int enabled;
int probes;
int time;
int ret;

enabled = 1;

time = net->tcp_keepalive_time;
probes = net->tcp_keepalive_probes;
interval = net->tcp_keepalive_interval;

ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
(const void *) &enabled, sizeof(enabled));

if (ret == 0 && time >= 0) {
ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
(const void *) &time, sizeof(time));
}

if (ret == 0 && interval >= 0) {
ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
(const void *) &interval, sizeof(interval));
}

if (ret == 0 && probes >= 0) {
ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
(const void *) &probes, sizeof(probes));
}

if (ret != 0) {
flb_error("[net] failed to configure TCP keepalive for connection #%i", fd);

ret = -1;
}

return ret;
}

flb_sockfd_t flb_net_socket_create(int family, int nonblock)
{
flb_sockfd_t fd;
Expand Down
26 changes: 26 additions & 0 deletions src/flb_upstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,32 @@ struct flb_config_map upstream_net[] = {
"Set maximum time allowed for an idle Keepalive connection"
},

{
FLB_CONFIG_MAP_BOOL, "net.tcp_keepalive", "off",
0, FLB_TRUE, offsetof(struct flb_net_setup, tcp_keepalive),
"Enable or disable the use of TCP keepalive probes"
},

{
FLB_CONFIG_MAP_INT, "net.tcp_keepalive_time", "-1",
0, FLB_TRUE, offsetof(struct flb_net_setup, tcp_keepalive_time),
"interval between the last data packet sent and the first "
"TCP keepalive probe"
},

{
FLB_CONFIG_MAP_INT, "net.tcp_keepalive_interval", "-1",
0, FLB_TRUE, offsetof(struct flb_net_setup, tcp_keepalive_interval),
"interval between TCP keepalive probes when no response is"
"received on a keepidle probe"
},

{
FLB_CONFIG_MAP_INT, "net.tcp_keepalive_probes", "-1",
0, FLB_TRUE, offsetof(struct flb_net_setup, tcp_keepalive_probes),
"number of unacknowledged probes to consider a connection dead"
},

{
FLB_CONFIG_MAP_TIME, "net.io_timeout", "0s",
0, FLB_TRUE, offsetof(struct flb_net_setup, io_timeout),
Expand Down
Loading