From 93d866c6a75dab8f88c617706072fc6dbf66d890 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Thu, 27 Sep 2018 18:56:15 +0300 Subject: [PATCH] MXS-2073: Add missing TCP_NODELAY for client sockets The client connections had the Nagle algorithm enabled which could cause bad performance with smaller workloads. The common network configuration code in utils.cc, currently used by the backend connections, sets it properly. --- include/maxscale/limits.h | 26 ++++++-------------------- include/maxscale/utils.h | 14 ++++++++++++++ server/core/dcb.cc | 16 +--------------- server/core/utils.cc | 11 ++++++----- 4 files changed, 27 insertions(+), 40 deletions(-) diff --git a/include/maxscale/limits.h b/include/maxscale/limits.h index c167bb044..90395c1e5 100644 --- a/include/maxscale/limits.h +++ b/include/maxscale/limits.h @@ -24,32 +24,18 @@ MXS_BEGIN_DECLS /** - * MXS_BACKEND_SO_RCVBUF + * MXS_SO_RCVBUF * - * The value used when setting SO_RCVBUF of backend sockets. + * The size of the network input buffer. */ -#define MXS_BACKEND_SO_RCVBUF (128 * 1024) +#define MXS_SO_RCVBUF_SIZE (128 * 1024) /** - * MXS_BACKEND_SO_SNDBUF + * MXS_SO_SNDBUF * - * The value used when setting SO_SNDBUF of backend sockets. + * The size of the network output buffer. */ -#define MXS_BACKEND_SO_SNDBUF (128 * 1024) - -/** - * MXS_CLIENT_SO_RCVBUF - * - * The value used when setting SO_RCVBUF of client sockets. - */ -#define MXS_CLIENT_SO_RCVBUF (128 * 1024) - -/** - * MXS_CLIENT_SO_SNDBUF - * - * The value used when setting SO_SNDBUF of client sockets. - */ -#define MXS_CLIENT_SO_SNDBUF (128 * 1024) +#define MXS_SO_SNDBUF_SIZE (128 * 1024) /** * MXS_MAX_NW_READ_BUFFER_SIZE diff --git a/include/maxscale/utils.h b/include/maxscale/utils.h index 4e0036a06..0fa878f38 100644 --- a/include/maxscale/utils.h +++ b/include/maxscale/utils.h @@ -46,6 +46,20 @@ enum mxs_socket_type bool utils_init(); /*< Call this first before using any other function */ void utils_end(); +/** + * Configure network socket options + * + * This is a helper function for setting various socket options that are always wanted for all types + * of connections. It sets the socket into nonblocking mode, configures sndbuf and rcvbuf sizes + * and sets TCP_NODELAY (no Nagle algorithm). + * + * @param so Socket to configure + * @param type Socket type + * + * @return True if configuration was successful + */ +bool configure_network_socket(int so, int type); + /** * @brief Create a network socket and a socket configuration * diff --git a/server/core/dcb.cc b/server/core/dcb.cc index 3036a3098..945bacb9b 100644 --- a/server/core/dcb.cc +++ b/server/core/dcb.cc @@ -2369,21 +2369,7 @@ dcb_accept(DCB *dcb) { dcb->stats.n_accepts++; - /* set nonblocking */ - sendbuf = MXS_CLIENT_SO_SNDBUF; - - if (setsockopt(c_sock, SOL_SOCKET, SO_SNDBUF, &sendbuf, optlen) != 0) - { - MXS_ERROR("Failed to set socket options: %d, %s", errno, mxs_strerror(errno)); - } - - sendbuf = MXS_CLIENT_SO_RCVBUF; - - if (setsockopt(c_sock, SOL_SOCKET, SO_RCVBUF, &sendbuf, optlen) != 0) - { - MXS_ERROR("Failed to set socket options: %d, %s", errno, mxs_strerror(errno)); - } - setnonblocking(c_sock); + configure_network_socket(c_sock, client_conn.ss_family); client_dcb = dcb_alloc(DCB_ROLE_CLIENT_HANDLER, dcb->listener); diff --git a/server/core/utils.cc b/server/core/utils.cc index ccab2f6be..24928eefa 100644 --- a/server/core/utils.cc +++ b/server/core/utils.cc @@ -942,17 +942,18 @@ void utils_end() SPINLOCK tmplock = SPINLOCK_INIT; -static bool configure_network_socket(int so) +bool configure_network_socket(int so, int type) { - int sndbufsize = MXS_BACKEND_SO_SNDBUF; - int rcvbufsize = MXS_BACKEND_SO_RCVBUF; + int sndbufsize = MXS_SO_SNDBUF_SIZE; + int rcvbufsize = MXS_SO_RCVBUF_SIZE; int one = 1; if (setsockopt(so, SOL_SOCKET, SO_SNDBUF, &sndbufsize, sizeof(sndbufsize)) != 0 || setsockopt(so, SOL_SOCKET, SO_RCVBUF, &rcvbufsize, sizeof(rcvbufsize)) != 0 || - setsockopt(so, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) != 0) + (type != AF_UNIX && setsockopt(so, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) != 0)) { MXS_ERROR("Failed to set socket option: %d, %s.", errno, mxs_strerror(errno)); + ss_dassert(!true); return false; } @@ -1022,7 +1023,7 @@ int open_network_socket(enum mxs_socket_type type, struct sockaddr_storage *addr freeaddrinfo(ai); - if ((type == MXS_SOCKET_NETWORK && !configure_network_socket(so)) || + if ((type == MXS_SOCKET_NETWORK && !configure_network_socket(so, addr->ss_family)) || (type == MXS_SOCKET_LISTENER && !configure_listener_socket(so))) { close(so);