Skip to content
Closed
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 @@ -43,6 +43,18 @@ struct flb_net_setup {

/* maximum of times a keepalive connection can be used */
int keepalive_max_recycle;

/* enable/disable tcp keepalive */
char tcp_keepalive;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This property needs to be an int


/* 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;
};

/* Defines a host service and it properties */
Expand All @@ -69,6 +81,7 @@ int flb_net_socket_tcp_nodelay(flb_sockfd_t fd);
int flb_net_socket_blocking(flb_sockfd_t fd);
int flb_net_socket_nonblocking(flb_sockfd_t fd);
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
9 changes: 9 additions & 0 deletions src/flb_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ int flb_io_net_connect(struct flb_upstream_conn *u_conn,
u_conn->fd, u->tcp_host, u->tcp_port);
}

/* set TCP keepalive and it's options */
if (u->net.tcp_keepalive != FLB_FALSE) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please either use if (u->net.tcp_keepalive == FLB_TRUE) { or the preferred syntax according to the coding style if (u->net.tcp_keepalive) {.

ret = flb_net_socket_tcp_keepalive(fd, &u->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 (u->flags & FLB_IO_TLS) {
Expand Down
40 changes: 40 additions & 0 deletions src/flb_network.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ void flb_net_setup_init(struct flb_net_setup *net)
net->keepalive_max_recycle = 0;
net->connect_timeout = 10;
net->source_address = NULL;
net->tcp_keepalive = FLB_FALSE;
net->tcp_keepalive_time = -1;
net->tcp_keepalive_interval = -1;
net->tcp_keepalive_probes = -1;
}

int flb_net_host_set(const char *plugin_name, struct flb_net_host *host, const char *address)
Expand Down Expand Up @@ -186,6 +190,42 @@ 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please replace the bitwise assignments of ret with regular assignments and add ret == 0 && as a prefix to the conditional blocks so a failed setsockopt call is not overwritten and thus missed by the conditional in line 221.

Please change the conditional in line 221 so it explicitly compares against zero (ie. if (ret != 0) {).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is important because apparently according to msdn, windows doesn't accept values larger than 255 for TCP_KEEPCNT whereas other operating systems accept much larger values,.

{
int ret = 0;
int enabled = 1;
int time = net->tcp_keepalive_time;
int intvl = net->tcp_keepalive_interval;
int probes = net->tcp_keepalive_probes;

printf("keepalive = %d, %d %d\n", time, intvl, probes);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this line.


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

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

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

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

if (ret) {
flb_error("[net] failed to configure TCP keepalive for connection #%i", fd);
return -1;
}

return 0;
}

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 @@ -65,6 +65,32 @@ struct flb_config_map upstream_net[] = {
"before it is retired."
},

{
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"
},

/* EOF */
{0}
};
Expand Down