From 205088e23f4b14185a47cb1924ef7f3c8d32bfe9 Mon Sep 17 00:00:00 2001 From: "Alan T. DeKok" Date: Mon, 15 Apr 2024 11:59:50 -0400 Subject: [PATCH] set TCP_NODELAY by default --- src/lib/bio/fd.h | 1 + src/lib/bio/fd_open.c | 29 ++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/lib/bio/fd.h b/src/lib/bio/fd.h index aee79b7944..7c695251c4 100644 --- a/src/lib/bio/fd.h +++ b/src/lib/bio/fd.h @@ -98,6 +98,7 @@ typedef struct { int flags; //!< O_RDONLY, etc. bool async; //!< is it async + bool tcp_delay; //!< We do tcp_nodelay by default. } fr_bio_fd_config_t; /** Run-time status of the socket. diff --git a/src/lib/bio/fd_open.c b/src/lib/bio/fd_open.c index 1f93fa2f98..aae05ac7a8 100644 --- a/src/lib/bio/fd_open.c +++ b/src/lib/bio/fd_open.c @@ -30,21 +30,48 @@ #include #include #include +#include /** Initialize common datagram information * */ -static int fr_bio_fd_common_tcp(int fd, UNUSED fr_socket_t const *sock, UNUSED fr_bio_fd_config_t const *cfg) +static int fr_bio_fd_common_tcp(int fd, UNUSED fr_socket_t const *sock, fr_bio_fd_config_t const *cfg) { int on = 1; #ifdef SO_KEEPALIVE + /* + * TCP keepalives are always a good idea. Too many people put firewalls between critical + * systems, and then the firewalls drop live TCP streams. + */ if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) < 0) { fr_strerror_printf("Failed setting SO_KEEPALIVE: %s", fr_syserror(errno)); return -1; } #endif +#ifdef TCP_NODELAY + /* + * Add some defines for *BSD, and Solaris systems. + */ +# if !defined(SOL_TCP) && defined(IPPROTO_TCP) +# define SOL_TCP IPPROTO_TCP +# endif + + /* + * Also set TCP_NODELAY, to force the data to be written quickly. + * + * We buffer full packets in memory before we write them, so there's no reason for the kernel to + * sit around waiting for more data from us. + */ + if (!cfg->tcp_delay) { + if (setsockopt(fd, SOL_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) { + fr_strerror_printf("Failed setting TCP_NODELAY: %s", fr_syserror(errno)); + return -1; + } + } +#endif + return 0; } -- 2.47.3