net/tcp: remove the socket hook reference from netdev callback

Signed-off-by: chao.an <anchao@xiaomi.com>
This commit is contained in:
chao.an 2022-02-09 00:08:15 +08:00 committed by Alan Carvalho de Assis
parent 0d19a3a248
commit 8fb2468785
9 changed files with 74 additions and 89 deletions

View File

@ -137,7 +137,7 @@ struct tcp_hdr_s; /* Forward reference */
struct tcp_poll_s
{
FAR struct socket *psock; /* Needed to handle loss of connection */
FAR struct tcp_conn_s *conn; /* Needed to handle loss of connection */
struct pollfd *fds; /* Needed to handle poll events */
FAR struct devif_callback_s *cb; /* Needed to teardown the poll */
};
@ -685,7 +685,7 @@ void tcp_close_monitor(FAR struct socket *psock);
* the event handler.
*
* Input Parameters:
* psock - The TCP socket structure associated.
* conn - The TCP connection of interest
* cb - devif callback structure
* flags - Set of connection events events
*
@ -698,7 +698,7 @@ void tcp_close_monitor(FAR struct socket *psock);
*
****************************************************************************/
void tcp_lost_connection(FAR struct socket *psock,
void tcp_lost_connection(FAR struct tcp_conn_s *conn,
FAR struct devif_callback_s *cb, uint16_t flags);
/****************************************************************************
@ -1540,7 +1540,7 @@ bool tcp_should_send_recvwindow(FAR struct tcp_conn_s *conn);
* another means.
*
* Input Parameters:
* psock An instance of the internal socket structure.
* conn The TCP connection of interest
*
* Returned Value:
* OK
@ -1554,7 +1554,7 @@ bool tcp_should_send_recvwindow(FAR struct tcp_conn_s *conn);
*
****************************************************************************/
int psock_tcp_cansend(FAR struct socket *psock);
int psock_tcp_cansend(FAR struct tcp_conn_s *conn);
/****************************************************************************
* Name: tcp_wrbuffer_initialize

View File

@ -45,8 +45,8 @@
struct tcp_close_s
{
FAR struct tcp_conn_s *cl_conn; /* Needed to handle loss of connection */
FAR struct devif_callback_s *cl_cb; /* Reference to TCP callback instance */
FAR struct socket *cl_psock; /* Reference to the TCP socket */
sem_t cl_sem; /* Signals disconnect completion */
int cl_result; /* The result of the close */
};
@ -64,7 +64,7 @@ static uint16_t tcp_close_eventhandler(FAR struct net_driver_s *dev,
uint16_t flags)
{
FAR struct tcp_close_s *pstate = (FAR struct tcp_close_s *)pvpriv;
FAR struct tcp_conn_s *conn = (FAR struct tcp_conn_s *)pvconn;
FAR struct tcp_conn_s *conn = pstate->cl_conn;
DEBUGASSERT(pstate != NULL);
@ -322,7 +322,7 @@ static inline int tcp_close_disconnect(FAR struct socket *psock)
/* Set up for the lingering wait */
state.cl_psock = psock;
state.cl_conn = conn;
state.cl_result = -EBUSY;
/* This semaphore is used for signaling and, hence, should not have

View File

@ -55,7 +55,6 @@ struct tcp_connect_s
{
FAR struct tcp_conn_s *tc_conn; /* Reference to TCP connection structure */
FAR struct devif_callback_s *tc_cb; /* Reference to callback instance */
FAR struct socket *tc_psock; /* The socket being connected */
sem_t tc_sem; /* Semaphore signals recv completion */
int tc_result; /* OK on success, otherwise a negated errno. */
};
@ -96,7 +95,6 @@ static inline int psock_setup_callbacks(FAR struct socket *psock,
nxsem_set_protocol(&pstate->tc_sem, SEM_PRIO_NONE);
pstate->tc_conn = conn;
pstate->tc_psock = psock;
pstate->tc_result = -EAGAIN;
/* Set up the callbacks in the connection */
@ -167,6 +165,7 @@ static uint16_t psock_connect_eventhandler(FAR struct net_driver_s *dev,
FAR void *pvpriv, uint16_t flags)
{
struct tcp_connect_s *pstate = (struct tcp_connect_s *)pvpriv;
FAR struct tcp_conn_s *conn = pstate->tc_conn;
ninfo("flags: %04x\n", flags);
@ -218,19 +217,13 @@ static uint16_t psock_connect_eventhandler(FAR struct net_driver_s *dev,
else if ((flags & TCP_CONNECTED) != 0)
{
FAR struct socket *psock = pstate->tc_psock;
FAR struct socket_conn_s *conn;
DEBUGASSERT(psock);
conn = psock->s_conn;
/* Mark the connection bound and connected. NOTE this is
* is done here (vs. later) in order to avoid any race condition
* in the socket state. It is known to connected here and now,
* but not necessarily at any time later.
*/
conn->s_flags |= (_SF_BOUND | _SF_CONNECTED);
conn->sconn.s_flags |= (_SF_BOUND | _SF_CONNECTED);
/* Indicate that the socket is no longer connected */

View File

@ -40,7 +40,8 @@
* Private Function Prototypes
****************************************************************************/
static void tcp_close_connection(FAR struct socket *psock, uint16_t flags);
static void tcp_close_connection(FAR struct tcp_conn_s *conn,
uint16_t flags);
static uint16_t tcp_monitor_event(FAR struct net_driver_s *dev,
FAR void *pvconn, FAR void *pvpriv,
uint16_t flags);
@ -56,7 +57,7 @@ static uint16_t tcp_monitor_event(FAR struct net_driver_s *dev,
* Called when a loss-of-connection event has occurred.
*
* Input Parameters:
* psock The TCP socket structure associated.
* conn The TCP connection structure
* flags Set of connection events events
*
* Returned Value:
@ -67,10 +68,8 @@ static uint16_t tcp_monitor_event(FAR struct net_driver_s *dev,
*
****************************************************************************/
static void tcp_close_connection(FAR struct socket *psock, uint16_t flags)
static void tcp_close_connection(FAR struct tcp_conn_s *conn, uint16_t flags)
{
FAR struct socket_conn_s *conn = psock->s_conn;
/* These loss-of-connection events may be reported:
*
* TCP_CLOSE: The remote host has closed the connection
@ -94,8 +93,8 @@ static void tcp_close_connection(FAR struct socket *psock, uint16_t flags)
* not handle as an error but as an "end-of-file"
*/
conn->s_flags &= ~_SF_CONNECTED;
conn->s_flags |= _SF_CLOSED;
conn->sconn.s_flags &= ~_SF_CONNECTED;
conn->sconn.s_flags |= _SF_CLOSED;
}
else if ((flags & (TCP_ABORT | TCP_TIMEDOUT | NETDEV_DOWN)) != 0)
{
@ -103,7 +102,7 @@ static void tcp_close_connection(FAR struct socket *psock, uint16_t flags)
* (eventually) be reported as an ENOTCONN error.
*/
conn->s_flags &= ~(_SF_CONNECTED | _SF_CLOSED);
conn->sconn.s_flags &= ~(_SF_CONNECTED | _SF_CLOSED);
}
}
@ -130,12 +129,11 @@ static uint16_t tcp_monitor_event(FAR struct net_driver_s *dev,
FAR void *pvconn, FAR void *pvpriv,
uint16_t flags)
{
FAR struct socket *psock = (FAR struct socket *)pvpriv;
FAR struct socket_conn_s *conn = psock->s_conn;
FAR struct tcp_conn_s *conn = pvpriv;
if (psock != NULL)
if (conn != NULL)
{
ninfo("flags: %04x s_flags: %02x\n", flags, conn->s_flags);
ninfo("flags: %04x s_flags: %02x\n", flags, conn->sconn.s_flags);
/* TCP_DISCONN_EVENTS: TCP_CLOSE, TCP_ABORT, TCP_TIMEDOUT, or
* NETDEV_DOWN. All loss-of-connection events.
@ -143,7 +141,7 @@ static uint16_t tcp_monitor_event(FAR struct net_driver_s *dev,
if ((flags & TCP_DISCONN_EVENTS) != 0)
{
tcp_close_connection(psock, flags);
tcp_close_connection(conn, flags);
}
/* TCP_CONNECTED: The socket is successfully connected */
@ -169,12 +167,15 @@ static uint16_t tcp_monitor_event(FAR struct net_driver_s *dev,
/* Clear the socket error */
_SO_SETERRNO(psock, OK);
#ifdef CONFIG_NET_SOCKOPTS
conn->sconn.s_error = OK;
#endif
set_errno(OK);
/* Indicate that the socket is now connected */
sconn->s_flags |= (_SF_BOUND | _SF_CONNECTED);
sconn->s_flags &= ~_SF_CLOSED;
conn->sconn.s_flags |= (_SF_BOUND | _SF_CONNECTED);
conn->sconn.s_flags &= ~_SF_CLOSED;
}
}
@ -294,7 +295,7 @@ int tcp_start_monitor(FAR struct socket *psock)
if (cb != NULL)
{
cb->event = tcp_monitor_event;
cb->priv = (FAR void *)psock;
cb->priv = (FAR void *)conn;
cb->flags = TCP_DISCONN_EVENTS;
/* Monitor the connected event */
@ -386,7 +387,7 @@ void tcp_close_monitor(FAR struct socket *psock)
/* Make sure that this socket is explicitly marked as closed */
tcp_close_connection(psock, TCP_CLOSE);
tcp_close_connection(conn, TCP_CLOSE);
/* Now notify any sockets waiting for events from this particular sockets.
* Other dup'ed sockets sharing the same connection must not be effected.
@ -423,7 +424,7 @@ void tcp_close_monitor(FAR struct socket *psock)
* event handler.
*
* Input Parameters:
* psock - The TCP socket structure whose connection was lost.
* conn - The TCP connection of interest
* cb - devif callback structure
* flags - Set of connection events events
*
@ -436,10 +437,10 @@ void tcp_close_monitor(FAR struct socket *psock)
*
****************************************************************************/
void tcp_lost_connection(FAR struct socket *psock,
void tcp_lost_connection(FAR struct tcp_conn_s *conn,
FAR struct devif_callback_s *cb, uint16_t flags)
{
DEBUGASSERT(psock != NULL && psock->s_conn != NULL);
DEBUGASSERT(conn != NULL);
/* Nullify the callback structure so that recursive callbacks are not
* received by the event handler due to disconnection processing.
@ -460,11 +461,11 @@ void tcp_lost_connection(FAR struct socket *psock,
* callback due to the above nullification.
*/
tcp_close_connection(psock, flags);
tcp_close_connection(conn, flags);
/* Then stop the network monitor for all sockets. */
tcp_shutdown_monitor((FAR struct tcp_conn_s *)psock->s_conn, flags);
tcp_shutdown_monitor(conn, flags);
}
#endif /* NET_TCP_HAVE_STACK */

View File

@ -72,7 +72,7 @@ static uint16_t tcp_poll_eventhandler(FAR struct net_driver_s *dev,
ninfo("flags: %04x\n", flags);
DEBUGASSERT(info == NULL || (info->psock != NULL && info->fds != NULL));
DEBUGASSERT(info == NULL || (info->conn != NULL && info->fds != NULL));
/* 'priv' might be null in some race conditions (?) */
@ -129,11 +129,14 @@ static uint16_t tcp_poll_eventhandler(FAR struct net_driver_s *dev,
reason = ECONNREFUSED;
}
_SO_SETERRNO(info->psock, reason);
#ifdef CONFIG_NET_SOCKOPTS
info->conn->sconn.s_error = reason;
#endif
set_errno(reason);
/* Mark that the connection has been lost */
tcp_lost_connection(info->psock, info->cb, flags);
tcp_lost_connection(info->conn, info->cb, flags);
eventset |= (POLLERR | POLLHUP);
}
@ -146,7 +149,7 @@ static uint16_t tcp_poll_eventhandler(FAR struct net_driver_s *dev,
* this callback to be inserted after psock_send_eventhandler.
*/
else if (psock_tcp_cansend(info->psock) >= 0
else if (psock_tcp_cansend(info->conn) >= 0
#if defined(CONFIG_NET_TCP_WRITE_BUFFERS)
|| (flags & TCP_ACKDATA) != 0
#endif
@ -222,7 +225,7 @@ int tcp_pollsetup(FAR struct socket *psock, FAR struct pollfd *fds)
/* Find a container to hold the poll information */
info = conn->pollinfo;
while (info->psock != NULL)
while (info->conn != NULL)
{
if (++info >= &conn->pollinfo[CONFIG_NET_TCP_NPOLLWAITERS])
{
@ -242,9 +245,9 @@ int tcp_pollsetup(FAR struct socket *psock, FAR struct pollfd *fds)
/* Initialize the poll info container */
info->psock = psock;
info->fds = fds;
info->cb = cb;
info->conn = conn;
info->fds = fds;
info->cb = cb;
/* Initialize the callback structure. Save the reference to the info
* structure as callback private data so that it will be available during
@ -342,7 +345,7 @@ int tcp_pollsetup(FAR struct socket *psock, FAR struct pollfd *fds)
fds->revents |= (POLLERR | POLLHUP);
}
else if (_SS_ISCONNECTED(conn->sconn.s_flags) &&
psock_tcp_cansend(psock) >= 0)
psock_tcp_cansend(conn) >= 0)
{
fds->revents |= (POLLWRNORM & fds->events);
}
@ -407,7 +410,7 @@ int tcp_pollteardown(FAR struct socket *psock, FAR struct pollfd *fds)
/* Then free the poll info container */
info->psock = NULL;
info->conn = NULL;
}
return OK;

View File

@ -58,7 +58,7 @@
struct tcp_recvfrom_s
{
FAR struct socket *ir_sock; /* The parent socket structure */
FAR struct tcp_conn_s *ir_conn; /* Connection associated with the socket */
FAR struct devif_callback_s *ir_cb; /* Reference to callback instance */
sem_t ir_sem; /* Semaphore signals recv completion */
size_t ir_buflen; /* Length of receive buffer */
@ -170,8 +170,7 @@ static inline uint16_t tcp_newdata(FAR struct net_driver_s *dev,
FAR struct tcp_recvfrom_s *pstate,
uint16_t flags)
{
FAR struct tcp_conn_s *conn = (FAR struct tcp_conn_s *)
pstate->ir_sock->s_conn;
FAR struct tcp_conn_s *conn = pstate->ir_conn;
/* Take as much data from the packet as we can */
@ -237,8 +236,7 @@ static inline uint16_t tcp_newdata(FAR struct net_driver_s *dev,
static inline void tcp_readahead(struct tcp_recvfrom_s *pstate)
{
FAR struct tcp_conn_s *conn =
(FAR struct tcp_conn_s *)pstate->ir_sock->s_conn;
FAR struct tcp_conn_s *conn = pstate->ir_conn;
FAR struct iob_s *iob;
int recvlen;
@ -461,8 +459,7 @@ static uint16_t tcp_recvhandler(FAR struct net_driver_s *dev,
else if ((flags & TCP_DISCONN_EVENTS) != 0)
{
FAR struct socket *psock = pstate->ir_sock;
FAR struct socket_conn_s *conn;
FAR struct tcp_conn_s *conn = pstate->ir_conn;
nwarn("WARNING: Lost connection\n");
@ -471,13 +468,12 @@ static uint16_t tcp_recvhandler(FAR struct net_driver_s *dev,
* already been disconnected.
*/
DEBUGASSERT(psock != NULL);
conn = psock->s_conn;
if (_SS_ISCONNECTED(conn->s_flags))
DEBUGASSERT(conn != NULL);
if (_SS_ISCONNECTED(conn->sconn.s_flags))
{
/* Handle loss-of-connection event */
tcp_lost_connection(psock, pstate->ir_cb, flags);
tcp_lost_connection(conn, pstate->ir_cb, flags);
}
/* Check if the peer gracefully closed the connection. */
@ -512,7 +508,7 @@ static uint16_t tcp_recvhandler(FAR struct net_driver_s *dev,
* Initialize the state structure
*
* Input Parameters:
* psock Pointer to the socket structure for the socket
* conn The TCP connection of interest
* buf Buffer to receive data
* len Length of buffer
* pstate A pointer to the state structure to be initialized
@ -524,8 +520,9 @@ static uint16_t tcp_recvhandler(FAR struct net_driver_s *dev,
*
****************************************************************************/
static void tcp_recvfrom_initialize(FAR struct socket *psock, FAR void *buf,
size_t len, FAR struct sockaddr *infrom,
static void tcp_recvfrom_initialize(FAR struct tcp_conn_s *conn,
FAR void *buf, size_t len,
FAR struct sockaddr *infrom,
FAR socklen_t *fromlen,
FAR struct tcp_recvfrom_s *pstate)
{
@ -547,7 +544,7 @@ static void tcp_recvfrom_initialize(FAR struct socket *psock, FAR void *buf,
/* Set up the start time for the timeout */
pstate->ir_sock = psock;
pstate->ir_conn = conn;
}
/* The only un-initialization that has to be performed is destroying the
@ -643,7 +640,7 @@ ssize_t psock_tcp_recvfrom(FAR struct socket *psock, FAR void *buf,
* because we don't want anything to happen until we are ready.
*/
tcp_recvfrom_initialize(psock, buf, len, from, fromlen, &state);
tcp_recvfrom_initialize(conn, buf, len, from, fromlen, &state);
/* Handle any any TCP data already buffered in a read-ahead buffer. NOTE
* that there may be read-ahead data to be retrieved even after the

View File

@ -229,8 +229,7 @@ static void psock_writebuffer_notify(FAR struct tcp_conn_s *conn)
*
****************************************************************************/
static inline void psock_lost_connection(FAR struct socket *psock,
FAR struct tcp_conn_s *conn,
static inline void psock_lost_connection(FAR struct tcp_conn_s *conn,
bool abort)
{
FAR sq_entry_t *entry;
@ -363,17 +362,13 @@ static uint16_t psock_send_eventhandler(FAR struct net_driver_s *dev,
* obtained from the corresponding TCP socket.
*/
FAR struct socket *psock = (FAR struct socket *)pvpriv;
FAR struct tcp_conn_s *conn;
FAR struct tcp_conn_s *conn = pvpriv;
bool rexmit = false;
DEBUGASSERT(psock != NULL);
/* Get the TCP connection pointer reliably from
* the corresponding TCP socket.
*/
conn = psock->s_conn;
DEBUGASSERT(conn != NULL);
/* The TCP socket is connected and, hence, should be bound to a device.
@ -410,16 +405,16 @@ static uint16_t psock_send_eventhandler(FAR struct net_driver_s *dev,
* already been disconnected.
*/
if (psock->s_conn != NULL && _SS_ISCONNECTED(conn->sconn.s_flags))
if (_SS_ISCONNECTED(conn->sconn.s_flags))
{
/* Report not connected */
tcp_lost_connection(psock, conn->sndcb, flags);
tcp_lost_connection(conn, conn->sndcb, flags);
}
/* Free write buffers and terminate polling */
psock_lost_connection(psock, psock->s_conn, !!(flags & NETDEV_DOWN));
psock_lost_connection(conn, !!(flags & NETDEV_DOWN));
return flags;
}
@ -1163,7 +1158,7 @@ ssize_t psock_tcp_send(FAR struct socket *psock, FAR const void *buf,
conn->sndcb->flags = (TCP_ACKDATA | TCP_REXMIT | TCP_POLL |
TCP_DISCONN_EVENTS);
conn->sndcb->priv = (FAR void *)psock;
conn->sndcb->priv = (FAR void *)conn;
conn->sndcb->event = psock_send_eventhandler;
#if CONFIG_NET_SEND_BUFSIZE > 0
@ -1405,7 +1400,7 @@ errout:
* another means.
*
* Input Parameters:
* psock An instance of the internal socket structure.
* conn The TCP connection of interest
*
* Returned Value:
* OK
@ -1419,23 +1414,19 @@ errout:
*
****************************************************************************/
int psock_tcp_cansend(FAR struct socket *psock)
int psock_tcp_cansend(FAR struct tcp_conn_s *conn)
{
FAR struct socket_conn_s *conn;
/* Verify that we received a valid socket */
if (!psock || !psock->s_conn)
if (!conn)
{
nerr("ERROR: Invalid socket\n");
return -EBADF;
}
conn = psock->s_conn;
/* Verify that this is connected TCP socket */
if (psock->s_type != SOCK_STREAM || !_SS_ISCONNECTED(conn->s_flags))
if (!_SS_ISCONNECTED(conn->sconn.s_flags))
{
nerr("ERROR: Not connected\n");
return -ENOTCONN;

View File

@ -374,7 +374,7 @@ static uint16_t tcpsend_eventhandler(FAR struct net_driver_s *dev,
{
/* Report not connected */
tcp_lost_connection(psock, pstate->snd_cb, flags);
tcp_lost_connection(conn, pstate->snd_cb, flags);
}
pstate->snd_sent = -ENOTCONN;
@ -719,7 +719,7 @@ errout:
* write occurs first.
*
* Input Parameters:
* psock An instance of the internal socket structure.
* conn The TCP connection of interest
*
* Returned Value:
* OK (Always can send).
@ -729,7 +729,7 @@ errout:
*
****************************************************************************/
int psock_tcp_cansend(FAR struct socket *psock)
int psock_tcp_cansend(FAR struct tcp_conn_s *conn)
{
return OK;
}

View File

@ -333,7 +333,7 @@ static uint16_t sendfile_eventhandler(FAR struct net_driver_s *dev,
{
/* Report not connected */
tcp_lost_connection(psock, pstate->snd_cb, flags);
tcp_lost_connection(conn, pstate->snd_cb, flags);
}
/* Report not connected */