2015-01-30 14:28:30 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* net/tcp/tcp_netpoll.c
|
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2015-01-30 14:28:30 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-01-30 14:28:30 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2015-01-30 14:28:30 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2019-12-31 16:26:14 +01:00
|
|
|
#include <assert.h>
|
2015-01-30 14:28:30 +01:00
|
|
|
#include <poll.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/net/net.h>
|
2021-08-10 14:42:35 +02:00
|
|
|
#include <nuttx/net/tcp.h>
|
2019-12-31 16:26:14 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2015-01-30 14:28:30 +01:00
|
|
|
|
2017-08-06 22:48:19 +02:00
|
|
|
#include "devif/devif.h"
|
2018-09-11 20:00:42 +02:00
|
|
|
#include "netdev/netdev.h"
|
2017-08-06 22:48:19 +02:00
|
|
|
#include "socket/socket.h"
|
|
|
|
#include "inet/inet.h"
|
2015-01-30 14:28:30 +01:00
|
|
|
#include "tcp/tcp.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-08-29 22:08:04 +02:00
|
|
|
* Name: tcp_poll_eventhandler
|
2015-01-30 14:28:30 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2017-08-29 20:27:58 +02:00
|
|
|
* This function is called to perform the actual TCP receive operation via
|
|
|
|
* the device interface layer.
|
2015-01-30 14:28:30 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-09-02 18:27:03 +02:00
|
|
|
* dev The structure of the network driver that caused the event
|
2022-08-25 15:17:57 +02:00
|
|
|
* pvpriv An instance of struct tcp_poll_s cast to void*
|
2015-01-30 14:28:30 +01:00
|
|
|
* flags Set of events describing why the callback was invoked
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2017-08-29 23:08:38 +02:00
|
|
|
* The network is locked
|
2015-01-30 14:28:30 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-29 22:08:04 +02:00
|
|
|
static uint16_t tcp_poll_eventhandler(FAR struct net_driver_s *dev,
|
|
|
|
FAR void *pvpriv, uint16_t flags)
|
2015-01-30 14:28:30 +01:00
|
|
|
{
|
2022-08-25 15:17:57 +02:00
|
|
|
FAR struct tcp_poll_s *info = pvpriv;
|
2015-01-30 14:28:30 +01:00
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("flags: %04x\n", flags);
|
2015-01-30 14:28:30 +01:00
|
|
|
|
2022-02-08 17:08:15 +01:00
|
|
|
DEBUGASSERT(info == NULL || (info->conn != NULL && info->fds != NULL));
|
2015-01-30 14:28:30 +01:00
|
|
|
|
|
|
|
/* 'priv' might be null in some race conditions (?) */
|
|
|
|
|
2017-10-19 19:55:51 +02:00
|
|
|
if (info != NULL)
|
2015-01-30 14:28:30 +01:00
|
|
|
{
|
|
|
|
pollevent_t eventset = 0;
|
|
|
|
|
|
|
|
/* Check for data or connection availability events. */
|
|
|
|
|
|
|
|
if ((flags & (TCP_NEWDATA | TCP_BACKLOG)) != 0)
|
|
|
|
{
|
2022-09-19 05:08:57 +02:00
|
|
|
eventset |= POLLIN;
|
2015-01-30 14:28:30 +01:00
|
|
|
}
|
|
|
|
|
2021-08-10 14:42:35 +02:00
|
|
|
/* Non-blocking connection */
|
|
|
|
|
|
|
|
if ((flags & TCP_CONNECTED) != 0)
|
|
|
|
{
|
2022-09-19 05:08:57 +02:00
|
|
|
eventset |= POLLOUT;
|
2021-08-10 14:42:35 +02:00
|
|
|
}
|
|
|
|
|
2015-01-30 14:28:30 +01:00
|
|
|
/* Check for a loss of connection events. */
|
|
|
|
|
2015-05-30 17:12:27 +02:00
|
|
|
if ((flags & TCP_DISCONN_EVENTS) != 0)
|
2015-01-30 14:28:30 +01:00
|
|
|
{
|
2023-02-14 06:32:41 +01:00
|
|
|
#ifdef CONFIG_NET_SOCKOPTS
|
|
|
|
int reason;
|
|
|
|
|
2021-08-10 14:42:35 +02:00
|
|
|
/* TCP_TIMEDOUT: Connection aborted due to too many
|
|
|
|
* retransmissions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((flags & TCP_TIMEDOUT) != 0)
|
|
|
|
{
|
|
|
|
/* Indicate that the connection timedout?) */
|
|
|
|
|
|
|
|
reason = ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ((flags & NETDEV_DOWN) != 0)
|
|
|
|
{
|
|
|
|
/* The network device went down. Indicate that the remote host
|
|
|
|
* is unreachable.
|
|
|
|
*/
|
|
|
|
|
|
|
|
reason = ENETUNREACH;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TCP_CLOSE: The remote host has closed the connection
|
|
|
|
* TCP_ABORT: The remote host has aborted the connection
|
|
|
|
*/
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Indicate that remote host refused the connection */
|
|
|
|
|
|
|
|
reason = ECONNREFUSED;
|
|
|
|
}
|
|
|
|
|
2022-11-24 12:15:10 +01:00
|
|
|
_SO_CONN_SETERRNO(info->conn, reason);
|
2023-02-14 06:32:41 +01:00
|
|
|
#endif
|
2021-08-10 14:42:35 +02:00
|
|
|
|
2015-05-30 17:12:27 +02:00
|
|
|
/* Mark that the connection has been lost */
|
2015-01-30 14:28:30 +01:00
|
|
|
|
2022-02-08 17:08:15 +01:00
|
|
|
tcp_lost_connection(info->conn, info->cb, flags);
|
2015-01-30 14:28:30 +01:00
|
|
|
eventset |= (POLLERR | POLLHUP);
|
|
|
|
}
|
|
|
|
|
2019-11-25 16:59:50 +01:00
|
|
|
/* A poll is a sign that we are free to send data. */
|
|
|
|
|
2021-04-05 09:21:17 +02:00
|
|
|
/* Wake up poll() speculatively on TCP_ACKDATA.
|
|
|
|
* Note: our event handler is usually executed before
|
|
|
|
* psock_send_eventhandler, which might free IOBs/WRBs on TCP_ACKDATA.
|
|
|
|
* Revisit: consider some kind of priority for devif callback to allow
|
|
|
|
* this callback to be inserted after psock_send_eventhandler.
|
|
|
|
*/
|
|
|
|
|
2022-02-08 17:08:15 +01:00
|
|
|
else if (psock_tcp_cansend(info->conn) >= 0
|
2021-04-05 09:21:17 +02:00
|
|
|
#if defined(CONFIG_NET_TCP_WRITE_BUFFERS)
|
|
|
|
|| (flags & TCP_ACKDATA) != 0
|
|
|
|
#endif
|
|
|
|
)
|
2019-11-25 16:59:50 +01:00
|
|
|
{
|
2022-09-19 05:08:57 +02:00
|
|
|
eventset |= POLLOUT;
|
2019-11-25 16:59:50 +01:00
|
|
|
}
|
|
|
|
|
2015-09-02 18:09:33 +02:00
|
|
|
/* Awaken the caller of poll() if requested event occurred. */
|
2015-01-30 14:28:30 +01:00
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(&info->fds, 1, eventset);
|
|
|
|
|
|
|
|
if (info->fds->revents != 0)
|
2015-01-30 14:28:30 +01:00
|
|
|
{
|
2017-08-29 20:27:58 +02:00
|
|
|
/* Stop further callbacks */
|
|
|
|
|
2017-09-01 19:56:48 +02:00
|
|
|
info->cb->flags = 0;
|
|
|
|
info->cb->priv = NULL;
|
|
|
|
info->cb->event = NULL;
|
2015-01-30 14:28:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tcp_pollsetup
|
2015-01-30 14:28:30 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Setup to monitor events on one TCP/IP socket
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock - The TCP/IP socket of interest
|
|
|
|
* fds - The structure describing the events to be monitored, OR NULL if
|
|
|
|
* this is a request to stop monitoring events.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0: Success; Negated errno on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int tcp_pollsetup(FAR struct socket *psock, FAR struct pollfd *fds)
|
|
|
|
{
|
2022-07-08 22:43:11 +02:00
|
|
|
FAR struct tcp_conn_s *conn;
|
2015-01-30 14:28:30 +01:00
|
|
|
FAR struct tcp_poll_s *info;
|
|
|
|
FAR struct devif_callback_s *cb;
|
2022-09-19 05:08:57 +02:00
|
|
|
pollevent_t eventset = 0;
|
2021-08-10 14:42:35 +02:00
|
|
|
bool nonblock_conn;
|
2020-01-02 15:52:28 +01:00
|
|
|
int ret = OK;
|
2015-01-30 14:28:30 +01:00
|
|
|
|
2022-07-08 22:43:11 +02:00
|
|
|
/* Some of the following must be atomic */
|
|
|
|
|
|
|
|
net_lock();
|
|
|
|
|
|
|
|
conn = psock->s_conn;
|
|
|
|
|
2015-01-30 14:28:30 +01:00
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
if (!conn || !fds)
|
|
|
|
{
|
2022-07-08 22:43:11 +02:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto errout_with_lock;
|
2015-01-30 14:28:30 +01:00
|
|
|
}
|
|
|
|
|
2021-08-10 14:42:35 +02:00
|
|
|
/* Non-blocking connection ? */
|
|
|
|
|
2023-03-24 09:30:27 +01:00
|
|
|
nonblock_conn = ((conn->tcpstateflags == TCP_ALLOCATED ||
|
|
|
|
conn->tcpstateflags == TCP_SYN_SENT) &&
|
2022-02-08 06:18:01 +01:00
|
|
|
_SS_ISNONBLOCK(conn->sconn.s_flags));
|
2021-08-10 14:42:35 +02:00
|
|
|
|
2019-12-31 16:26:14 +01:00
|
|
|
/* Find a container to hold the poll information */
|
|
|
|
|
|
|
|
info = conn->pollinfo;
|
2022-02-08 17:08:15 +01:00
|
|
|
while (info->conn != NULL)
|
2019-12-31 16:26:14 +01:00
|
|
|
{
|
|
|
|
if (++info >= &conn->pollinfo[CONFIG_NET_TCP_NPOLLWAITERS])
|
|
|
|
{
|
2022-11-11 10:38:11 +01:00
|
|
|
DEBUGPANIC();
|
2019-12-31 16:26:14 +01:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-30 14:28:30 +01:00
|
|
|
/* Allocate a TCP/IP callback structure */
|
|
|
|
|
|
|
|
cb = tcp_callback_alloc(conn);
|
|
|
|
if (!cb)
|
|
|
|
{
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the poll info container */
|
|
|
|
|
2022-02-08 17:08:15 +01:00
|
|
|
info->conn = conn;
|
|
|
|
info->fds = fds;
|
|
|
|
info->cb = cb;
|
2015-01-30 14:28:30 +01:00
|
|
|
|
|
|
|
/* Initialize the callback structure. Save the reference to the info
|
|
|
|
* structure as callback private data so that it will be available during
|
|
|
|
* callback processing.
|
|
|
|
*/
|
|
|
|
|
2019-11-24 17:20:01 +01:00
|
|
|
cb->flags = TCP_DISCONN_EVENTS;
|
2015-01-30 14:28:30 +01:00
|
|
|
cb->priv = (FAR void *)info;
|
2017-08-29 22:08:04 +02:00
|
|
|
cb->event = tcp_poll_eventhandler;
|
2015-01-30 14:28:30 +01:00
|
|
|
|
2019-11-24 17:20:01 +01:00
|
|
|
if ((fds->events & POLLOUT) != 0)
|
|
|
|
{
|
2021-04-05 09:21:17 +02:00
|
|
|
cb->flags |= TCP_POLL
|
|
|
|
#if defined(CONFIG_NET_TCP_WRITE_BUFFERS)
|
|
|
|
| TCP_ACKDATA
|
|
|
|
#endif
|
|
|
|
;
|
2021-08-10 14:42:35 +02:00
|
|
|
|
|
|
|
/* Monitor the connected event */
|
|
|
|
|
|
|
|
if (nonblock_conn)
|
|
|
|
{
|
|
|
|
cb->flags |= TCP_CONNECTED;
|
|
|
|
}
|
2019-11-24 17:20:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((fds->events & POLLIN) != 0)
|
|
|
|
{
|
|
|
|
cb->flags |= TCP_NEWDATA | TCP_BACKLOG;
|
|
|
|
}
|
|
|
|
|
2015-01-30 14:28:30 +01:00
|
|
|
/* Save the reference in the poll info structure as fds private as well
|
2015-05-30 17:12:27 +02:00
|
|
|
* for use during poll teardown as well.
|
2015-01-30 14:28:30 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
fds->priv = (FAR void *)info;
|
|
|
|
|
|
|
|
/* Check for read data or backlogged connection availability now */
|
|
|
|
|
2023-04-17 16:07:31 +02:00
|
|
|
if (conn->readahead != NULL || tcp_backlogpending(conn))
|
2015-01-30 14:28:30 +01:00
|
|
|
{
|
|
|
|
/* Normal data may be read without blocking. */
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
eventset |= POLLRDNORM;
|
2015-01-30 14:28:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for a loss of connection events. We need to be careful here.
|
|
|
|
* There are four possibilities:
|
|
|
|
*
|
|
|
|
* 1) The socket is connected and we are waiting for data availability
|
|
|
|
* events.
|
|
|
|
*
|
|
|
|
* __SS_ISCONNECTED(f) == true
|
|
|
|
* __SS_ISLISTENING(f) == false
|
|
|
|
* __SS_ISCLOSED(f) == false
|
|
|
|
*
|
|
|
|
* Action: Wait for data availability events
|
|
|
|
*
|
|
|
|
* 2) This is a listener socket that was never connected and we are
|
|
|
|
* waiting for connection events.
|
|
|
|
*
|
|
|
|
* __SS_ISCONNECTED(f) == false
|
|
|
|
* __SS_ISLISTENING(f) == true
|
|
|
|
* __SS_ISCLOSED(f) == false
|
|
|
|
*
|
|
|
|
* Action: Wait for connection events
|
|
|
|
*
|
|
|
|
* 3) This socket was previously connected, but the peer has gracefully
|
|
|
|
* closed the connection.
|
|
|
|
*
|
|
|
|
* __SS_ISCONNECTED(f) == false
|
|
|
|
* __SS_ISLISTENING(f) == false
|
|
|
|
* __SS_ISCLOSED(f) == true
|
|
|
|
*
|
|
|
|
* Action: Return with POLLHUP|POLLERR events
|
|
|
|
*
|
|
|
|
* 4) This socket was previously connected, but we lost the connection
|
|
|
|
* due to some exceptional event.
|
|
|
|
*
|
|
|
|
* __SS_ISCONNECTED(f) == false
|
|
|
|
* __SS_ISLISTENING(f) == false
|
|
|
|
* __SS_ISCLOSED(f) == false
|
|
|
|
*
|
|
|
|
* Action: Return with POLLHUP|POLLERR events
|
|
|
|
*/
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
if (!nonblock_conn && !_SS_ISCONNECTED(conn->sconn.s_flags) &&
|
|
|
|
!_SS_ISLISTENING(conn->sconn.s_flags))
|
2015-01-30 14:28:30 +01:00
|
|
|
{
|
|
|
|
/* We were previously connected but lost the connection either due
|
|
|
|
* to a graceful shutdown by the remote peer or because of some
|
|
|
|
* exceptional event.
|
|
|
|
*/
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
eventset |= POLLERR | POLLHUP;
|
2015-01-30 14:28:30 +01:00
|
|
|
}
|
2022-02-08 06:18:01 +01:00
|
|
|
else if (_SS_ISCONNECTED(conn->sconn.s_flags) &&
|
2022-02-08 17:08:15 +01:00
|
|
|
psock_tcp_cansend(conn) >= 0)
|
2016-01-22 22:52:14 +01:00
|
|
|
{
|
2022-09-19 05:08:57 +02:00
|
|
|
eventset |= POLLWRNORM;
|
2016-01-22 22:52:14 +01:00
|
|
|
}
|
2015-01-30 14:28:30 +01:00
|
|
|
|
|
|
|
/* Check if any requested events are already in effect */
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(&fds, 1, eventset);
|
2015-01-30 14:28:30 +01:00
|
|
|
|
|
|
|
errout_with_lock:
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2015-01-30 14:28:30 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tcp_pollteardown
|
2015-01-30 14:28:30 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Teardown monitoring of events on an TCP/IP socket
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock - The TCP/IP socket of interest
|
|
|
|
* fds - The structure describing the events to be monitored, OR NULL if
|
|
|
|
* this is a request to stop monitoring events.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0: Success; Negated errno on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int tcp_pollteardown(FAR struct socket *psock, FAR struct pollfd *fds)
|
|
|
|
{
|
2022-07-08 22:43:11 +02:00
|
|
|
FAR struct tcp_conn_s *conn;
|
2015-01-30 14:28:30 +01:00
|
|
|
FAR struct tcp_poll_s *info;
|
|
|
|
|
2022-07-08 22:43:11 +02:00
|
|
|
/* Some of the following must be atomic */
|
|
|
|
|
|
|
|
net_lock();
|
|
|
|
|
|
|
|
conn = psock->s_conn;
|
|
|
|
|
2015-01-30 14:28:30 +01:00
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
if (!conn || !fds->priv)
|
|
|
|
{
|
2022-07-08 22:43:11 +02:00
|
|
|
net_unlock();
|
2015-01-30 14:28:30 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Recover the socket descriptor poll state info from the poll structure */
|
|
|
|
|
|
|
|
info = (FAR struct tcp_poll_s *)fds->priv;
|
2022-07-08 22:43:11 +02:00
|
|
|
DEBUGASSERT(info->fds != NULL && info->cb != NULL);
|
2018-09-11 20:00:42 +02:00
|
|
|
if (info != NULL)
|
2015-01-30 14:28:30 +01:00
|
|
|
{
|
|
|
|
/* Release the callback */
|
|
|
|
|
|
|
|
tcp_callback_free(conn, info->cb);
|
|
|
|
|
|
|
|
/* Release the poll/select data slot */
|
|
|
|
|
|
|
|
info->fds->priv = NULL;
|
|
|
|
|
|
|
|
/* Then free the poll info container */
|
|
|
|
|
2022-02-08 17:08:15 +01:00
|
|
|
info->conn = NULL;
|
2015-01-30 14:28:30 +01:00
|
|
|
}
|
|
|
|
|
2022-07-08 22:43:11 +02:00
|
|
|
net_unlock();
|
|
|
|
|
2015-01-30 14:28:30 +01:00
|
|
|
return OK;
|
|
|
|
}
|