-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds configuration options for TCP sockets, tuning for Hub app. (#759)
Adds several tuning parameters for TCP sockets: - SO_SNDBUF - SO_RCVBUF - TCP_NOTSENT_LOWAT Refactors where the fd socket options are set to a single place. Applies some reasonable (but not great) values for the Hub application. Switches the Hub application to use select(). Tunes the parameters to limit buffer size in the hub. Fixes a crashing bug where a GridconnectBridge was deleted too early when packets owned by its pool were still in flight. === * Adds constants for TCP socket options fo SO_SNDBUF and SO_RCVBUF * Optimizes the memory and buffer use of the hub application. * gc-tcp-hub: applies SO_SNDBUF and SO_RCVBUF options to sockets. * client connection util: exposes the file descriptor when available. Applies the gridconnect use select link option. Applies the SO_SNDBUF/RCVBUF link option. * Ensures that the ClientCOnnection.cxx is built and linked. * Sorts lines in sources. * Refactors the fd optimization code to FdUtils class. * Makes tcp_lowat also configurable. * Fix compile error. * Adds comment. * Switch gridconnect hub to LimitedPool instead of FixedPool. This needs no dedicated memory, but uses the regular mainBufferPool. Add a check that all outgoing packets are released before deleting the pool. This solves hub crashing when a client disconnects. * Tune TCP options.
- Loading branch information
1 parent
f7f6206
commit 761abe9
Showing
10 changed files
with
275 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** \copyright | ||
* Copyright (c) 2023, Balazs Racz | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* - Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* - Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* \file ClientConnection.cxx | ||
* | ||
* Utilities for managing can-hub connections as a client application. | ||
* | ||
* @author Balazs Racz | ||
* @date 27 Dec 2023 | ||
*/ | ||
|
||
#include "utils/ClientConnection.hxx" | ||
|
||
#include "netinet/in.h" | ||
#include "netinet/tcp.h" | ||
#include "nmranet_config.h" | ||
|
||
#include "utils/FdUtils.hxx" | ||
|
||
/// Callback from try_connect to donate the file descriptor. | ||
/// @param fd is the file destriptor of the connection freshly opened. | ||
void GCFdConnectionClient::connection_complete(int fd) | ||
{ | ||
const bool use_select = | ||
(config_gridconnect_tcp_use_select() == CONSTANT_TRUE); | ||
|
||
// Applies kernel parameters like socket options. | ||
FdUtils::optimize_fd(fd); | ||
|
||
fd_ = fd; | ||
create_gc_port_for_can_hub(hub_, fd, &closedNotify_, use_select); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/** \copyright | ||
* Copyright (c) 2023, Balazs Racz | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* - Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* - Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* \file FdUtils.cxx | ||
* | ||
* Helper functions for dealing with posix fds. | ||
* | ||
* @author Balazs Racz | ||
* @date 29 Dec 2023 | ||
*/ | ||
|
||
#include "FdUtils.hxx" | ||
|
||
#include <netinet/in.h> | ||
#include <netinet/tcp.h> | ||
#include <sys/stat.h> | ||
#include <termios.h> /* tc* functions */ | ||
|
||
#include "nmranet_config.h" | ||
|
||
/// Performs a system call on an fd. If an error is returned, prints the error | ||
/// using the log mechanism, but otherwise ignores it. | ||
/// @param where user-readable text printed with the error, e.g. "setsockopt" | ||
/// @param callfn system call, like ::setsockopt | ||
/// @param fd file descriptor (int) | ||
/// @param args... all other arguments to callfn | ||
#define PCALL_LOGERR(where, callfn, fd, args...) \ | ||
do \ | ||
{ \ | ||
int ret = callfn(fd, args); \ | ||
if (ret < 0) \ | ||
{ \ | ||
char buf[256]; \ | ||
strerror_r(errno, buf, sizeof(buf)); \ | ||
LOG_ERROR("fd %d %s: %s", fd, where, buf); \ | ||
} \ | ||
} while (0) | ||
|
||
/// Optimizes the kernel settings like socket and TCP options for an fd | ||
/// that is an outgoing TCP socket. | ||
/// @param fd socket file descriptor. | ||
void FdUtils::optimize_socket_fd(int fd) | ||
{ | ||
#ifdef __linux__ | ||
const int rcvbuf = config_gridconnect_tcp_rcv_buffer_size(); | ||
if (rcvbuf > 1) | ||
{ | ||
PCALL_LOGERR("setsockopt SO_RCVBUF", ::setsockopt, fd, SOL_SOCKET, | ||
SO_RCVBUF, &rcvbuf, sizeof(rcvbuf)); | ||
} | ||
const int sndbuf = config_gridconnect_tcp_snd_buffer_size(); | ||
if (sndbuf > 1) | ||
{ | ||
PCALL_LOGERR("setsockopt SO_SNDBUF", ::setsockopt, fd, SOL_SOCKET, | ||
SO_SNDBUF, &sndbuf, sizeof(sndbuf)); | ||
int ret = 0; | ||
socklen_t retsize = sizeof(ret); | ||
::getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &ret, &retsize); | ||
LOG(ALWAYS, "fd %d sndbuf %d", fd, ret); | ||
} | ||
const int lowat = config_gridconnect_tcp_notsent_lowat_buffer_size(); | ||
if (lowat > 1) | ||
{ | ||
PCALL_LOGERR("setsockopt tcp_notsent_lowat", ::setsockopt, fd, | ||
IPPROTO_TCP, TCP_NOTSENT_LOWAT, &lowat, sizeof(lowat)); | ||
int ret = 0; | ||
socklen_t retsize = sizeof(ret); | ||
::getsockopt(fd, IPPROTO_TCP, TCP_NOTSENT_LOWAT, &ret, &retsize); | ||
LOG(ALWAYS, "fd %d lowat %d", fd, ret); | ||
} | ||
#endif | ||
} | ||
|
||
/// Sets the kernel settings like queuing and terminal settings for an fd | ||
/// that is an outgoing tty. | ||
/// @param fd tty file descriptor. | ||
void FdUtils::optimize_tty_fd(int fd) | ||
{ | ||
#ifdef __linux__ | ||
// Sets up the terminal in raw mode. Otherwise linux might echo | ||
// characters coming in from the device and that will make | ||
// packets go back to where they came from. | ||
HASSERT(!tcflush(fd, TCIOFLUSH)); | ||
struct termios settings; | ||
HASSERT(!tcgetattr(fd, &settings)); | ||
cfmakeraw(&settings); | ||
cfsetspeed(&settings, B115200); | ||
HASSERT(!tcsetattr(fd, TCSANOW, &settings)); | ||
#endif | ||
} | ||
|
||
/// For an fd that is an outgoing link, detects what kind of file | ||
/// descriptor this is and calls the appropriate optimize call for it. | ||
void FdUtils::optimize_fd(int fd) | ||
{ | ||
#ifdef __linux__ | ||
struct stat statbuf; | ||
fstat(fd, &statbuf); | ||
|
||
if (S_ISSOCK(statbuf.st_mode)) | ||
{ | ||
optimize_socket_fd(fd); | ||
} | ||
else if (isatty(fd)) | ||
{ | ||
optimize_tty_fd(fd); | ||
} | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.