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

Adds an application (hub_test) for testing the throughput of a hub or a CAN-bus #762

Merged
merged 47 commits into from
Feb 2, 2024
Merged
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
c8b7fde
Adds the skeleton of a hub test application which will verify the per…
balazsracz Dec 23, 2023
8cbfb2a
Adds an accessor to the last expiry time of a timer.
balazsracz Dec 23, 2023
822bad7
Starts adding the send flow.
balazsracz Dec 23, 2023
0b088d2
Adds code for filling in the output frames.
balazsracz Dec 25, 2023
e0ef336
Releases packets as the receive notifications are coming in.
balazsracz Dec 25, 2023
b05236b
Adds receiver class to hub_test.
balazsracz Dec 27, 2023
9130af0
Adds constants for TCP socket options fo SO_SNDBUF and SO_RCVBUF
balazsracz Dec 29, 2023
6498441
Optimizes the memory and buffer use of the hub application.
balazsracz Dec 29, 2023
84b4808
Ensures that an executor thread can not be created twice.
balazsracz Dec 29, 2023
df96714
hub-test: Implements links and prints statistics about sent data.
balazsracz Dec 29, 2023
954956d
gc-tcp-hub: applies SO_SNDBUF and SO_RCVBUF options to sockets.
balazsracz Dec 29, 2023
a85ca9f
client connection util: exposes the file descriptor when available.
balazsracz Dec 29, 2023
8ee55a0
Limits end to end buffering in gridconnect bridge.
balazsracz Dec 29, 2023
0d0cefa
Adds a utility class for computing simple statistics.
balazsracz Dec 29, 2023
9755279
Ensures that the ClientCOnnection.cxx is built and linked.
balazsracz Dec 29, 2023
e2b9769
Sorts lines in sources.
balazsracz Dec 29, 2023
47cb981
Ensures that an executor thread can not be created twice.
balazsracz Dec 29, 2023
4bd04c6
Limits end to end buffering in gridconnect bridge.
balazsracz Dec 29, 2023
46cfb1c
fix whitespace
balazsracz Dec 29, 2023
1347885
Merge commit '4bd04c6c81b987a82f5f399bd104fb0eeb9b2f7b' into bracz-hu…
balazsracz Dec 29, 2023
304883b
Merge branch 'bracz-hub-bridge-limit-pool' into bracz-hub-test-app
balazsracz Dec 29, 2023
cad3b2e
Merge branch 'bracz-exec-multi-thread-assert' into bracz-hub-test-app
balazsracz Dec 29, 2023
567021a
Refactors the fd optimization code to FdUtils class.
balazsracz Dec 29, 2023
52bd450
Adds constants for TCP socket options fo SO_SNDBUF and SO_RCVBUF
balazsracz Dec 29, 2023
cf52f64
Optimizes the memory and buffer use of the hub application.
balazsracz Dec 29, 2023
c664b91
gc-tcp-hub: applies SO_SNDBUF and SO_RCVBUF options to sockets.
balazsracz Dec 29, 2023
06c6bb4
client connection util: exposes the file descriptor when available.
balazsracz Dec 29, 2023
f8db2a0
Ensures that the ClientCOnnection.cxx is built and linked.
balazsracz Dec 29, 2023
c573f41
Sorts lines in sources.
balazsracz Dec 29, 2023
ea41047
Refactors the fd optimization code to FdUtils class.
balazsracz Dec 29, 2023
976af9e
Makes tcp_lowat also configurable.
balazsracz Dec 29, 2023
f384215
Fix compile error.
balazsracz Dec 29, 2023
7bbc545
Adds comment.
balazsracz Dec 29, 2023
19a39e3
Merge commit 'ea41047b1bc27c2cc29e4b5ba303b9bb10096009' into bracz-hu…
balazsracz Dec 29, 2023
d3e957f
Merge branch 'bracz-optimize-tcp-socket' into bracz-hub-test-app
balazsracz Dec 29, 2023
6ffb38e
Switch gridconnect hub to LimitedPool instead of FixedPool.
balazsracz Dec 29, 2023
b2eb101
Tune TCP options.
balazsracz Dec 29, 2023
993c2b0
hubtest: tune TCP options.
balazsracz Dec 29, 2023
f52ca25
Switch gridconnect hub to LimitedPool instead of FixedPool.
balazsracz Dec 29, 2023
3e6e3ac
Tune TCP options.
balazsracz Dec 29, 2023
c6bb145
Merge branch 'bracz-optimize-tcp-socket' into bracz-hub-test-app
balazsracz Dec 29, 2023
6e31c7e
Changes TCP tuning params: no snd/recv buf limitation, but notsent_lo…
balazsracz Jan 1, 2024
781184c
Merge branch 'master' into bracz-optimize-tcp-socket
balazsracz Jan 2, 2024
e6b99c6
Merge branch 'bracz-optimize-tcp-socket' into bracz-hub-test-app
balazsracz Jan 2, 2024
a10430c
Adds new features to the hub_test:
balazsracz Jan 26, 2024
5786c59
Removes output buffer delay.
balazsracz Jan 26, 2024
91b9680
Merge branch 'master' into bracz-hub-test-app
balazsracz Jan 26, 2024
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
Prev Previous commit
Next Next commit
Refactors the fd optimization code to FdUtils class.
balazsracz committed Dec 29, 2023
commit ea41047b1bc27c2cc29e4b5ba303b9bb10096009
38 changes: 8 additions & 30 deletions src/utils/ClientConnection.cxx
Original file line number Diff line number Diff line change
@@ -38,40 +38,18 @@
#include "netinet/tcp.h"
#include "nmranet_config.h"

/** Callback from try_connect to donate the file descriptor. @param fd is
* the file destriptor of the connection freshly opened. */
#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)
{
struct stat statbuf;
fstat(fd, &statbuf);

const bool use_select =
(config_gridconnect_tcp_use_select() == CONSTANT_TRUE);

// Applies kernel parameters like socket options.
FdUtils::optimize_fd(fd);

fd_ = fd;
const int rcvbuf = config_gridconnect_tcp_rcv_buffer_size();
if (rcvbuf > 1)
{
::setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf));
}
const int sndbuf = config_gridconnect_tcp_snd_buffer_size();
if (sndbuf > 1)
{
::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 = 4096;
if (lowat > 1 && S_ISSOCK(statbuf.st_mode))
{
ERRNOCHECK("tcp 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);
}
create_gc_port_for_can_hub(hub_, fd, &closedNotify_, use_select);
}
13 changes: 2 additions & 11 deletions src/utils/ClientConnection.hxx
Original file line number Diff line number Diff line change
@@ -36,11 +36,11 @@
#define _UTILS_CLIENTCONNECTION_HXX_

#include <stdio.h>
#include <termios.h> /* tc* functions */
#include <unistd.h>

#include "utils/GridConnectHub.hxx"
#include "utils/socket_listener.hxx"
#include "utils/FdUtils.hxx"

/// Abstract base class for the Hub's connections.
class ConnectionClient
@@ -170,18 +170,9 @@ private:
int fd = ::open(dev_.c_str(), O_RDWR);
if (fd >= 0)
{
// 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));
FdUtils::optimize_tty_fd(fd);
LOG(INFO, "Opened device %s.\n", dev_.c_str());
connection_complete(fd);
//
}
else
{
126 changes: 126 additions & 0 deletions src/utils/FdUtils.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/** \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"

#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 = 4096;
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
}
16 changes: 16 additions & 0 deletions src/utils/FdUtils.hxx
Original file line number Diff line number Diff line change
@@ -35,6 +35,8 @@
#ifndef _UTILS_FD_UTILS_HXX_
#define _UTILS_FD_UTILS_HXX_

#include <unistd.h>

#include "utils/logging.h"
#include "utils/macros.h"

@@ -81,6 +83,20 @@ struct FdUtils
dst += ret;
}
}

/// Optimizes the kernel settings like socket and TCP options for an fd
/// that is an outgoing TCP socket.
/// @param fd socket file descriptor.
static void optimize_socket_fd(int fd);

/// Sets the kernel settings like queuing and terminal settings for an fd
/// that is an outgoing tty.
/// @param fd tty file descriptor.
static void optimize_tty_fd(int fd);

/// For an fd that is an outgoing link, detects what kind of file
/// descriptor this is and calls the appropriate optimize call for it.
static void optimize_fd(int fd);
};

#endif // _UTILS_FD_UTILS_HXX_
13 changes: 3 additions & 10 deletions src/utils/GcTcpHub.cxx
Original file line number Diff line number Diff line change
@@ -38,6 +38,7 @@

#include "nmranet_config.h"
#include "utils/GridConnectHub.hxx"
#include "utils/FdUtils.hxx"

void GcTcpHub::on_new_connection(int fd)
{
@@ -47,16 +48,8 @@ void GcTcpHub::on_new_connection(int fd)
AtomicHolder h(this);
numClients_++;
}
const int rcvbuf = config_gridconnect_tcp_rcv_buffer_size();
if (rcvbuf > 1)
{
::setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf));
}
const int sndbuf = config_gridconnect_tcp_snd_buffer_size();
if (sndbuf > 1)
{
::setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf));
}
// Applies kernel parameters like socket options.
FdUtils::optimize_socket_fd(fd);
create_gc_port_for_can_hub(canHub_, fd, this, use_select);
}

1 change: 1 addition & 0 deletions src/utils/sources
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ CXXSRCS += \
StringPrintf.cxx \
Buffer.cxx \
ConfigUpdateListener.cxx \
FdUtils.cxx \
FileUtils.cxx \
ForwardAllocator.cxx \
GcStreamParser.cxx \