Browse Source

Ethernet2UART gateway: reconnect when the device goes silent on a live connection

Field log (sensor behind 2.54.237.28:8234): normal replies avg 129 ms,
spikes up to 2.6 s handled. Then no reply at all to 19 requests in a
row while the TCP connection stayed "up" (sending never failed). The
sensor failed after 17 attempts and kept using the dead connection. A
dead mobile/NAT path isn't reported by the kernel for many minutes, and
keepalive only probes idle connections.

- TCP_USER_TIMEOUT 10 s (E2UART_TCP_USER_TIMEOUT_MS): sent data not
  acknowledged by the gateway drops the connection.
- After 5 requests in a row (E2UART_NO_REPLY_RECONNECT) without a single
  reply byte, the I/O thread ends ("No reply to 5 requests in a row -
  reconnecting to the gateway"). validate() sees the lost link and opens
  a new connection. Late data resets the count.
- closePort() frees the requests still queued for the closed connection.
- "NO reply" log lines (DEBUG_COMMUNICATION) and the reconnect error show
  TCP_INFO: unacked data, retransmits, rtt, last ACK / last data received.
  Unacked data and an old last ACK mean the network path is dead; ACKs
  that keep coming mean the gateway/device side doesn't answer.

Tested with a fake device that goes silent after 3 replies while keeping
the TCP connection open: the reconnect came after the 5th unanswered
request, and requests 9-12 were answered on the new connection (ASan clean).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
pull/24/head
Claude 4 hours ago
parent
commit
617e38583d
Failed to extract signature
  1. 51
      lib/App/common/deviceNodeEthernet2Serial.cpp
  2. 11
      lib/App/common/deviceNodeEthernet2Serial.h

51
lib/App/common/deviceNodeEthernet2Serial.cpp

@ -585,6 +585,8 @@ bool deviceETH2UART::create_SOL()
setsockopt(m_iDeviceFD, IPPROTO_TCP, TCP_KEEPIDLE, &tIdle, sizeof(tIdle));
setsockopt(m_iDeviceFD, IPPROTO_TCP, TCP_KEEPINTVL, &tIntvl, sizeof(tIntvl));
setsockopt(m_iDeviceFD, IPPROTO_TCP, TCP_KEEPCNT, &tCnt, sizeof(tCnt));
unsigned int tUserTO = E2UART_TCP_USER_TIMEOUT_MS;
setsockopt(m_iDeviceFD, IPPROTO_TCP, TCP_USER_TIMEOUT, &tUserTO, sizeof(tUserTO));
}
// assign IP, PORT
@ -808,6 +810,16 @@ bool deviceETH2UART::closePort()
{
uninit();
};
#ifdef SERIAL_PING_PONG
// Requests not sent on the closed connection: drop them (the protocols re-send after a reconnect)
m_mBufferProtectorMutex.lock();
while (! m_qIO.empty())
{
free(m_qIO.front());
m_qIO.pop();
};
m_mBufferProtectorMutex.unlock();
#endif
if (m_iDeviceFD != -1)
{
switch (m_sUartData.mode)
@ -873,6 +885,24 @@ bool deviceETH2UART::isRecipient(protocolParser * p)
return tVal;
}
/*
Which side is silent: unacked > 0 and a growing "last ACK" -> the network path to the gateway is dead;
ACKs keep coming but no reply -> the gateway / serial line / device does not answer.
*/
void deviceETH2UART::tcpInfoText(char * buf, size_t size)
{
struct tcp_info tInfo;
socklen_t tLen = sizeof(tInfo);
memset(&tInfo, 0, sizeof(tInfo));
if (m_iDeviceFD < 0 || m_sUartData.mode != SOL_TCP_SERVER || getsockopt(m_iDeviceFD, IPPROTO_TCP, TCP_INFO, &tInfo, &tLen) != 0)
{
snprintf(buf, size, "no TCP info");
return;
};
snprintf(buf, size, "TCP: unacked %u, retransmits %u, rtt %u ms, last ACK %u ms ago, last data %u ms ago", tInfo.tcpi_unacked, tInfo.tcpi_retransmits,
tInfo.tcpi_rtt / 1000, tInfo.tcpi_last_ack_recv, tInfo.tcpi_last_data_recv);
}
void deviceETH2UART::m_fThreadProc(void)
{
int RetVal;
@ -882,6 +912,7 @@ void deviceETH2UART::m_fThreadProc(void)
#endif
pthread_setname_np( pthread_self(), "E2S IO" );
SIO_TZ * tIdle = (SIO_TZ *) malloc(sizeof(SIO_TZ)); // receive buffer for data between requests
int tNoReplyStreak = 0; // requests in a row (with a parser) without any reply byte
#ifdef DEBUG_COMMUNICATION
uint32_t tReqNo = 0, tStReq = 0, tStReply = 0, tStNoReply = 0, tStSlow = 0, tStLate = 0;
uint64_t tStSum = 0, tStMax = 0, tStStart = e2uNowMs(), tLastReqMs = 0;
@ -919,6 +950,7 @@ void deviceETH2UART::m_fThreadProc(void)
m_bThreadStatus = TH_ENDED;
LOG_ERROR("Connection Droped on Reception Data");
} else if (tIdle->inp_size > 0){
tNoReplyStreak = 0; // the device answers (late)
#ifdef DEBUG_COMMUNICATION
tStLate++;
snprintf(tDbg, sizeof(tDbg), "IO: late data %d B, %llu ms after request #%u (reply window %d ms)", tIdle->inp_size,
@ -974,6 +1006,7 @@ void deviceETH2UART::m_fThreadProc(void)
//coming (E2UART_REPLY_GAP_MS between chunks); done when the parser has a complete packet and
//no more data is waiting
tIOData->out_size = 0;
bool tGotData = false;
bool tDataOnline = false;
uint64_t tDeadline = e2uNowMs() + E2UART_REPLY_TIMEOUT_MS;
#ifdef DEBUG_COMMUNICATION
@ -988,6 +1021,7 @@ void deviceETH2UART::m_fThreadProc(void)
m_bThreadStatus = TH_ENDED;
LOG_ERROR("Connection Droped on Reception Data");
} else if( tIOData->inp_size ){
tGotData = true;
#ifdef DEBUG_COMMUNICATION
if (tFirstByteMs == 0){
tFirstByteMs = e2uNowMs();
@ -1015,7 +1049,9 @@ void deviceETH2UART::m_fThreadProc(void)
uint64_t tDoneMs = e2uNowMs();
if (tFirstByteMs == 0){
tStNoReply++;
snprintf(tDbg, sizeof(tDbg), "IO: request #%u (%d B): NO reply in %llu ms", tReqNo, tSentBytes, (unsigned long long) (tDoneMs - tSendMs));
char tTcp[160];
tcpInfoText(tTcp, sizeof(tTcp));
snprintf(tDbg, sizeof(tDbg), "IO: request #%u (%d B): NO reply in %llu ms - %s", tReqNo, tSentBytes, (unsigned long long) (tDoneMs - tSendMs), tTcp);
LOG(tDbg);
} else {
uint64_t tFirst = tFirstByteMs - tSendMs;
@ -1033,6 +1069,19 @@ void deviceETH2UART::m_fThreadProc(void)
};
}
#endif
if (tGotData){
tNoReplyStreak = 0;
} else if (tParser != NULL && m_bThreadStatus < TH_ENDED && ++tNoReplyStreak >= E2UART_NO_REPLY_RECONNECT){
// The device went silent while the TCP connection stayed "up" (sending works, nothing comes
// back): end the I/O thread - validate() sees the lost link and opens a new connection
char tTcp[160];
char tB[320];
tcpInfoText(tTcp, sizeof(tTcp));
snprintf(tB, sizeof(tB), "No reply to %d requests in a row - reconnecting to the gateway (%s)", tNoReplyStreak, tTcp);
LOG_ERROR(tB);
setConnectionStatus(CS_DISCONNECTED);
m_bThreadStatus = TH_ENDED;
};
// Free Package
free(tIOData);
};

11
lib/App/common/deviceNodeEthernet2Serial.h

@ -37,6 +37,16 @@
#define E2UART_KEEPALIVE_IDLE_S 20
#define E2UART_KEEPALIVE_INTVL_S 5
#define E2UART_KEEPALIVE_CNT 4
// Sent data not acknowledged by the gateway for this long: the kernel drops the connection (a dead
// mobile / NAT path otherwise looks alive for many minutes - keepalive only probes idle connections)
#ifndef E2UART_TCP_USER_TIMEOUT_MS
#define E2UART_TCP_USER_TIMEOUT_MS 10000
#endif
// Requests in a row without any reply byte: reconnect to the gateway (the device went silent while
// the TCP connection stayed up - a new connection resets the gateway session / NAT mapping)
#ifndef E2UART_NO_REPLY_RECONNECT
#define E2UART_NO_REPLY_RECONNECT 5
#endif
#ifdef SERIAL_PING_PONG
class deviceETH2UART : public deviceNode, public hardwareResource, public basicTransport, public basicThread
#else
@ -81,6 +91,7 @@
Q_SIO_TZ m_qIO;
protocolParser * m_pLastParser; // parser of the last request: gets data that arrives between requests
bool isRecipient(protocolParser * p); // still bound to this transport (not deleted / detached)
void tcpInfoText(char * buf, size_t size); // unacked data / retransmits / last ACK of the connection
#else
U8 m_pInputBuffer[MAXIMAL_SOCKET_IO_BUFFER_LENGTH];
U8 m_pOutputBuffer[MAXIMAL_SOCKET_IO_BUFFER_LENGTH];

Loading…
Cancel
Save