From bad844cfdef81040e3fc274eb67d578ad82f4c0d Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 23 Feb 2021 14:32:17 +0000 Subject: [PATCH] server: Move sending keepalives to the server when enabled Signed-off-by: Michael Tremer --- man/fireperf.txt | 18 +++++++-------- src/client.c | 47 --------------------------------------- src/server.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 56 deletions(-) diff --git a/man/fireperf.txt b/man/fireperf.txt index c519361..31b769d 100644 --- a/man/fireperf.txt +++ b/man/fireperf.txt @@ -67,6 +67,15 @@ a network between a fireperf client and fireperf server. This starts the program in server mode which makes it connect to the specified port and waits for clients to connect. +--keepalive:: +-k:: + Instead of sending data, this will configure fireperf to only send keepalive + packets. + + + If a large number of connections is being created, it might become undesirable to + saturate the link between client and server. This option will send some packets to + keep the connection alive through for example NAT gateways. + == CLIENT-SPECIFIC OPTIONS @@ -80,15 +89,6 @@ a network between a fireperf client and fireperf server. Duplex mode enables the client to send data instead of only receiving it from the server. The same TCP connection will be used for traffic in both directions. ---keepalive:: --k:: - Instead of sending data to the server, this will configure the client to only - send keepalive packets. - + - If a large number of connections is being created, it might become undesirable to - saturate the link between client and server. This option will send some packets to - keep the connection alive through for example NAT gateways. - --timeout=T:: -t T:: If set, the client will automatically terminate itself after T seconds. diff --git a/src/client.c b/src/client.c index d25f617..f683bcf 100644 --- a/src/client.c +++ b/src/client.c @@ -19,7 +19,6 @@ #############################################################################*/ #include -#include #include #include #include @@ -69,52 +68,6 @@ static int open_connection(struct fireperf_config* conf) { if (r) goto ERROR; - // Enable keepalive - int flags = 1; - r = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void*)&flags, sizeof(flags)); - if (r) { - ERROR(conf, "Could not set SO_KEEPALIVE on socket %d: %s\n", - fd, strerror(errno)); - goto ERROR; - } - - // Set keepalive interval - if (conf->keepalive_interval) { - DEBUG(conf, "Setting keepalive interval to %d\n", conf->keepalive_interval); - - r = setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, - (void*)&conf->keepalive_interval, sizeof(conf->keepalive_interval)); - if (r) { - ERROR(conf, "Could not set TCP_KEEPINTVL on socket %d: %s\n", - fd, strerror(errno)); - goto ERROR; - } - - DEBUG(conf, "Setting keepalive idle interval to %d\n", conf->keepalive_interval); - - flags = 1; - r = setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, - (void*)&flags, sizeof(flags)); - if (r) { - ERROR(conf, "Could not set TCP_KEEPIDLE on socket %d: %s\n", - fd, strerror(errno)); - goto ERROR; - } - } - - // Set keepalive count - if (conf->keepalive_count) { - DEBUG(conf, "Setting keepalive count to %d\n", conf->keepalive_count); - - r = setsockopt(fd, SOL_TCP, TCP_KEEPCNT, - (void*)&conf->keepalive_count, sizeof(conf->keepalive_count)); - if (r) { - ERROR(conf, "Could not set TCP_KEEPCNT on socket %d: %s\n", - fd, strerror(errno)); - goto ERROR; - } - } - // Connect to the server r = connect(fd, &peer, sizeof(peer)); if (r && (errno != EINPROGRESS)) { diff --git a/src/server.c b/src/server.c index 9be816d..c47c57b 100644 --- a/src/server.c +++ b/src/server.c @@ -19,6 +19,7 @@ #############################################################################*/ #include +#include #include #include #include @@ -33,6 +34,56 @@ #define SOCKET_BACKLOG 1024 +static int enable_keepalive(struct fireperf_config* conf, int fd) { + // Enable keepalive + int flags = 1; + int r = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void*)&flags, sizeof(flags)); + if (r) { + ERROR(conf, "Could not set SO_KEEPALIVE on socket %d: %s\n", + fd, strerror(errno)); + return 1; + } + + // Set keepalive interval + if (conf->keepalive_interval) { + DEBUG(conf, "Setting keepalive interval to %d\n", conf->keepalive_interval); + + r = setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, + (void*)&conf->keepalive_interval, sizeof(conf->keepalive_interval)); + if (r) { + ERROR(conf, "Could not set TCP_KEEPINTVL on socket %d: %s\n", + fd, strerror(errno)); + return 1; + } + + DEBUG(conf, "Setting keepalive idle interval to %d\n", conf->keepalive_interval); + + flags = 1; + r = setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, + (void*)&flags, sizeof(flags)); + if (r) { + ERROR(conf, "Could not set TCP_KEEPIDLE on socket %d: %s\n", + fd, strerror(errno)); + return 1; + } + } + + // Set keepalive count + if (conf->keepalive_count) { + DEBUG(conf, "Setting keepalive count to %d\n", conf->keepalive_count); + + r = setsockopt(fd, SOL_TCP, TCP_KEEPCNT, + (void*)&conf->keepalive_count, sizeof(conf->keepalive_count)); + if (r) { + ERROR(conf, "Could not set TCP_KEEPCNT on socket %d: %s\n", + fd, strerror(errno)); + return 1; + } + } + + return 0; +} + static int create_socket(struct fireperf_config* conf, int i) { int r; @@ -56,6 +107,13 @@ static int create_socket(struct fireperf_config* conf, int i) { if (r) goto ERROR; + // Enable keepalive + if (conf->keepalive_only) { + r = enable_keepalive(conf, fd); + if (r) + goto ERROR; + } + struct sockaddr_in6 addr = { .sin6_family = AF_INET6, .sin6_port = htons(conf->port + i), -- 2.47.2