From 81ce32efc7ddebf1f09851d4e4ac34681f436e57 Mon Sep 17 00:00:00 2001 From: helge Date: Sat, 6 Nov 2021 15:00:24 +0100 Subject: [PATCH 1/2] set keep-alive defaults for new connections Configure scpi-specific keep-alive settings for new connections. Without these, a connection lost on the client side blocks the server baiscally indefinitely, requiring a reset or some means to recover via another port or interface. scpi server connection timeout is chosen such that when idle, zero window probes are sent periodically, and the connection is closed when they fail SCPI_KEEP_CNT times. also requires (lwipopts.h): set #define LWIP_TCP_KEEPALIVE 1 main.c: add #include "tcp_priv.h" call tcp_tmr(); every 250 ms (e.g. via xTimerCreate(), xTimerStart() in callback). To change the defaults for all connections, add these values (lwipopts.h): #define TCP_KEEPIDLE_DEFAULT 15000UL /* Default KEEPALIVE timer in milliseconds */ #define TCP_KEEPINTVL_DEFAULT 5000UL /* Default Time between KEEPALIVE probes in milliseconds */ #define TCP_KEEPCNT_DEFAULT 3U /* Default Counter for KEEPALIVE probes */ --- examples/test-LwIP-netconn/scpi_server.c | 8 ++++++++ examples/test-LwIP-netconn/scpi_server.h | 3 +++ 2 files changed, 11 insertions(+) diff --git a/examples/test-LwIP-netconn/scpi_server.c b/examples/test-LwIP-netconn/scpi_server.c index 309dd383..9252ba57 100644 --- a/examples/test-LwIP-netconn/scpi_server.c +++ b/examples/test-LwIP-netconn/scpi_server.c @@ -253,6 +253,10 @@ static int processIoListen(user_data_t * user_data) { } else { /* connection established */ iprintf("***Connection established %s\r\n", inet_ntoa(newconn->pcb.ip->remote_ip)); + newconn->pcb.tcp->so_options |= SOF_KEEPALIVE; + newconn->pcb.tcp->keep_idle = SCPI_KEEP_IDLE; // Override TCP_KEEPIDLE_DEFAULT for this connection. + newconn->pcb.tcp->keep_intvl = SCPI_KEEP_INTVL; // Override TCP_KEEPINTVL_DEFAULT for this connection. + newconn->pcb.tcp->keep_cnt = SCPI_KEEP_CNT; // Override TCP_KEEPCNT_DEFAULT for this connection. user_data->io = newconn; } } @@ -270,6 +274,10 @@ static int processSrqIoListen(user_data_t * user_data) { } else { /* control connection established */ iprintf("***Control Connection established %s\r\n", inet_ntoa(newconn->pcb.ip->remote_ip)); + newconn->pcb.tcp->so_options |= SOF_KEEPALIVE; + newconn->pcb.tcp->keep_idle = SCPI_KEEP_IDLE; // Override TCP_KEEPIDLE_DEFAULT for this connection. + newconn->pcb.tcp->keep_intvl = SCPI_KEEP_INTVL; // Override TCP_KEEPINTVL_DEFAULT for this connection. + newconn->pcb.tcp->keep_cnt = SCPI_KEEP_CNT; // Override TCP_KEEPCNT_DEFAULT for this connection. user_data->control_io = newconn; } } diff --git a/examples/test-LwIP-netconn/scpi_server.h b/examples/test-LwIP-netconn/scpi_server.h index 4d2895f9..b9900ef1 100644 --- a/examples/test-LwIP-netconn/scpi_server.h +++ b/examples/test-LwIP-netconn/scpi_server.h @@ -29,6 +29,9 @@ #ifndef _SCPI_SERVER_H_ #define _SCPI_SERVER_H_ +#define SCPI_KEEP_IDLE 2000 // (ms) keepalive quiet time after last TCP packet +#define SCPI_KEEP_INTVL 1000 // (ms) keepalive repeat interval +#define SCPI_KEEP_CNT 4 // Retry count before terminating connection (SCPI_KEEP_INTVL * SCPI_KEEP_INTVL (ms)). #include From 984174fc9274bcbab75bd7b3eefd6023b73aa2d3 Mon Sep 17 00:00:00 2001 From: helge Date: Tue, 9 Nov 2021 12:07:27 +0100 Subject: [PATCH 2/2] use ip_set_option for bit setting --- examples/test-LwIP-netconn/scpi_server.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/test-LwIP-netconn/scpi_server.c b/examples/test-LwIP-netconn/scpi_server.c index 9252ba57..142adcfc 100644 --- a/examples/test-LwIP-netconn/scpi_server.c +++ b/examples/test-LwIP-netconn/scpi_server.c @@ -253,7 +253,7 @@ static int processIoListen(user_data_t * user_data) { } else { /* connection established */ iprintf("***Connection established %s\r\n", inet_ntoa(newconn->pcb.ip->remote_ip)); - newconn->pcb.tcp->so_options |= SOF_KEEPALIVE; + ip_set_option(newconn->pcb.tcp, SOF_KEEPALIVE); newconn->pcb.tcp->keep_idle = SCPI_KEEP_IDLE; // Override TCP_KEEPIDLE_DEFAULT for this connection. newconn->pcb.tcp->keep_intvl = SCPI_KEEP_INTVL; // Override TCP_KEEPINTVL_DEFAULT for this connection. newconn->pcb.tcp->keep_cnt = SCPI_KEEP_CNT; // Override TCP_KEEPCNT_DEFAULT for this connection. @@ -274,7 +274,7 @@ static int processSrqIoListen(user_data_t * user_data) { } else { /* control connection established */ iprintf("***Control Connection established %s\r\n", inet_ntoa(newconn->pcb.ip->remote_ip)); - newconn->pcb.tcp->so_options |= SOF_KEEPALIVE; + ip_set_option(newconn->pcb.tcp, SOF_KEEPALIVE); newconn->pcb.tcp->keep_idle = SCPI_KEEP_IDLE; // Override TCP_KEEPIDLE_DEFAULT for this connection. newconn->pcb.tcp->keep_intvl = SCPI_KEEP_INTVL; // Override TCP_KEEPINTVL_DEFAULT for this connection. newconn->pcb.tcp->keep_cnt = SCPI_KEEP_CNT; // Override TCP_KEEPCNT_DEFAULT for this connection.