tcp: added debug asserts and logging to investigate the rare (conn->dev == NULL) bug in callback handlers

This commit is contained in:
Alexander Lunev 2022-01-29 21:13:22 +03:00 committed by Alan Carvalho de Assis
parent fb6f058395
commit 404ceffae2
7 changed files with 112 additions and 2 deletions

View File

@ -157,6 +157,12 @@ static int inet_tcp_alloc(FAR struct socket *psock)
DEBUGASSERT(conn->crefs == 0); DEBUGASSERT(conn->crefs == 0);
conn->crefs = 1; conn->crefs = 1;
/* It is expected the socket has not yet been associated with
* any other connection.
*/
DEBUGASSERT(psock->s_conn == NULL);
/* Save the pre-allocated connection in the socket structure */ /* Save the pre-allocated connection in the socket structure */
psock->s_conn = conn; psock->s_conn = conn;

View File

@ -60,7 +60,7 @@ NET_CSRCS += tcp_recvwindow.c tcp_netpoll.c tcp_ioctl.c
ifeq ($(CONFIG_NET_TCP_WRITE_BUFFERS),y) ifeq ($(CONFIG_NET_TCP_WRITE_BUFFERS),y)
NET_CSRCS += tcp_wrbuffer.c NET_CSRCS += tcp_wrbuffer.c
ifeq ($(CONFIG_DEBUG_FEATURES),y) ifeq ($(CONFIG_DEBUG_FEATURES),y)
NET_CSRCS += tcp_wrbuffer_dump.c NET_CSRCS += tcp_dump.c
endif endif
endif endif

View File

@ -311,6 +311,10 @@ struct tcp_conn_s
/* Callback instance for TCP send() */ /* Callback instance for TCP send() */
FAR struct devif_callback_s *sndcb; FAR struct devif_callback_s *sndcb;
#ifdef CONFIG_DEBUG_ASSERTIONS
int sndcb_alloc_cnt; /* The callback allocation counter */
#endif
#endif #endif
/* accept() is called when the TCP logic has created a connection /* accept() is called when the TCP logic has created a connection
@ -1642,6 +1646,22 @@ void tcp_wrbuffer_release(FAR struct tcp_wrbuffer_s *wrb);
int tcp_wrbuffer_test(void); int tcp_wrbuffer_test(void);
#endif /* CONFIG_NET_TCP_WRITE_BUFFERS */ #endif /* CONFIG_NET_TCP_WRITE_BUFFERS */
/****************************************************************************
* Name: tcp_event_handler_dump
*
* Description:
* Dump the TCP event handler related variables
*
****************************************************************************/
#ifdef CONFIG_DEBUG_FEATURES
void tcp_event_handler_dump(FAR struct net_driver_s *dev,
FAR void *pvconn,
FAR void *pvpriv,
uint16_t flags,
FAR struct tcp_conn_s *conn);
#endif
/**************************************************************************** /****************************************************************************
* Name: tcp_wrbuffer_dump * Name: tcp_wrbuffer_dump
* *

View File

@ -1,5 +1,5 @@
/**************************************************************************** /****************************************************************************
* net/tcp/tcp_wrbuffer_dump.c * net/tcp/tcp_dump.c
* *
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
@ -38,6 +38,33 @@
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
/****************************************************************************
* Name: tcp_event_handler_dump
*
* Description:
* Dump the TCP event handler related variables
*
****************************************************************************/
void tcp_event_handler_dump(FAR struct net_driver_s *dev,
FAR void *pvconn,
FAR void *pvpriv,
uint16_t flags,
FAR struct tcp_conn_s *conn)
{
nerr("ERROR: conn->dev == NULL or pvconn != conn:"
" dev=%p pvconn=%p pvpriv=%p flags=0x%04x"
" conn->dev=%p conn->flags=0x%04x tcpstateflags=0x%02x crefs=%d"
" isn=%" PRIu32 " sndseq=%" PRIu32
" tx_unacked=%" PRId32 " sent=%" PRId32
" conn=%p s_flags=0x%02x\n",
dev, pvconn, pvpriv, flags,
conn->dev, conn->flags, conn->tcpstateflags, conn->crefs,
conn->isn, tcp_getsequence(conn->sndseq),
(uint32_t)conn->tx_unacked, conn->sent,
conn, conn->sconn.s_flags);
}
/**************************************************************************** /****************************************************************************
* Name: tcp_wrbuffer_dump * Name: tcp_wrbuffer_dump
* *
@ -46,6 +73,8 @@
* *
****************************************************************************/ ****************************************************************************/
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
void tcp_wrbuffer_dump(FAR const char *msg, FAR struct tcp_wrbuffer_s *wrb, void tcp_wrbuffer_dump(FAR const char *msg, FAR struct tcp_wrbuffer_s *wrb,
unsigned int len, unsigned int offset) unsigned int len, unsigned int offset)
{ {
@ -54,4 +83,6 @@ void tcp_wrbuffer_dump(FAR const char *msg, FAR struct tcp_wrbuffer_s *wrb,
iob_dump("I/O Buffer Chain", TCP_WBIOB(wrb), len, offset); iob_dump("I/O Buffer Chain", TCP_WBIOB(wrb), len, offset);
} }
#endif
#endif /* CONFIG_DEBUG_FEATURES */ #endif /* CONFIG_DEBUG_FEATURES */

View File

@ -371,6 +371,19 @@ static uint16_t psock_send_eventhandler(FAR struct net_driver_s *dev,
DEBUGASSERT(conn != NULL); DEBUGASSERT(conn != NULL);
#ifdef CONFIG_DEBUG_FEATURES
if (conn->dev == NULL || (pvconn != conn && pvconn != NULL))
{
tcp_event_handler_dump(dev, pvconn, pvpriv, flags, conn);
}
#endif
/* If pvconn is not NULL, make sure that pvconn refers to the same
* connection as the socket is bound to.
*/
DEBUGASSERT(pvconn == conn || pvconn == NULL);
/* The TCP socket is connected and, hence, should be bound to a device. /* The TCP socket is connected and, hence, should be bound to a device.
* Make sure that the polling device is the one that we are bound to. * Make sure that the polling device is the one that we are bound to.
*/ */
@ -1171,6 +1184,20 @@ ssize_t psock_tcp_send(FAR struct socket *psock, FAR const void *buf,
if (conn->sndcb == NULL) if (conn->sndcb == NULL)
{ {
conn->sndcb = tcp_callback_alloc(conn); conn->sndcb = tcp_callback_alloc(conn);
#ifdef CONFIG_DEBUG_ASSERTIONS
if (conn->sndcb != NULL)
{
conn->sndcb_alloc_cnt++;
/* The callback is allowed to be allocated only once.
* This is to catch a potential re-allocation after
* conn->sndcb was set to NULL.
*/
DEBUGASSERT(conn->sndcb_alloc_cnt == 1);
}
#endif
} }
/* Test if the callback has been allocated */ /* Test if the callback has been allocated */

View File

@ -196,6 +196,19 @@ static uint16_t tcpsend_eventhandler(FAR struct net_driver_s *dev,
conn = psock->s_conn; conn = psock->s_conn;
DEBUGASSERT(conn != NULL); DEBUGASSERT(conn != NULL);
#ifdef CONFIG_DEBUG_NET_ERROR
if (conn->dev == NULL || (pvconn != conn && pvconn != NULL))
{
tcp_event_handler_dump(dev, pvconn, pvpriv, flags, conn);
}
#endif
/* If pvconn is not NULL, make sure that pvconn refers to the same
* connection as the socket is bound to.
*/
DEBUGASSERT(pvconn == conn || pvconn == NULL);
/* The TCP socket is connected and, hence, should be bound to a device. /* The TCP socket is connected and, hence, should be bound to a device.
* Make sure that the polling device is the one that we are bound to. * Make sure that the polling device is the one that we are bound to.
*/ */

View File

@ -150,6 +150,19 @@ static uint16_t sendfile_eventhandler(FAR struct net_driver_s *dev,
conn = psock->s_conn; conn = psock->s_conn;
DEBUGASSERT(conn != NULL); DEBUGASSERT(conn != NULL);
#ifdef CONFIG_DEBUG_NET_ERROR
if (conn->dev == NULL || (pvconn != conn && pvconn != NULL))
{
tcp_event_handler_dump(dev, pvconn, pvpriv, flags, conn);
}
#endif
/* If pvconn is not NULL, make sure that pvconn refers to the same
* connection as the socket is bound to.
*/
DEBUGASSERT(pvconn == conn || pvconn == NULL);
/* The TCP socket is connected and, hence, should be bound to a device. /* The TCP socket is connected and, hence, should be bound to a device.
* Make sure that the polling device is the own that we are bound to. * Make sure that the polling device is the own that we are bound to.
*/ */