2007-11-07 19:54:35 +01:00
|
|
|
/****************************************************************************
|
2014-06-18 18:18:53 +02:00
|
|
|
* net/tcp/tcp_callback.c
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
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
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-11-07 19:54:35 +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.
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2012-02-23 03:07:38 +01:00
|
|
|
|
2009-12-15 15:53:45 +01:00
|
|
|
#include <stdint.h>
|
2007-11-20 00:09:39 +01:00
|
|
|
#include <string.h>
|
2007-11-07 19:54:35 +01:00
|
|
|
#include <debug.h>
|
|
|
|
|
2017-05-09 15:34:59 +02:00
|
|
|
#include <nuttx/mm/iob.h>
|
2014-06-24 16:53:28 +02:00
|
|
|
#include <nuttx/net/netconfig.h>
|
2014-06-24 17:28:44 +02:00
|
|
|
#include <nuttx/net/netdev.h>
|
2014-06-26 17:32:39 +02:00
|
|
|
#include <nuttx/net/netstats.h>
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2014-06-29 02:07:02 +02:00
|
|
|
#include "devif/devif.h"
|
2014-06-25 02:12:49 +02:00
|
|
|
#include "tcp/tcp.h"
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2017-08-29 18:53:04 +02:00
|
|
|
#ifdef NET_TCP_HAVE_STACK
|
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2007-11-20 00:09:39 +01:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tcp_data_event
|
2007-11-20 00:09:39 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2012-02-23 03:07:38 +01:00
|
|
|
* Handle data that is not accepted by the application because there is no
|
|
|
|
* listener in place ready to receive the data.
|
2007-11-20 00:09:39 +01:00
|
|
|
*
|
|
|
|
* Assumptions:
|
2014-07-07 01:22:02 +02:00
|
|
|
* - The caller has checked that TCP_NEWDATA is set in flags and that is no
|
2008-09-01 15:59:54 +02:00
|
|
|
* other handler available to process the incoming data.
|
2017-09-02 18:27:03 +02:00
|
|
|
* - This function must be called with the network locked.
|
2007-11-20 00:09:39 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-12-15 15:53:45 +01:00
|
|
|
static inline uint16_t
|
2014-07-01 03:17:22 +02:00
|
|
|
tcp_data_event(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn,
|
|
|
|
uint16_t flags)
|
2007-11-20 00:09:39 +01:00
|
|
|
{
|
2009-12-15 15:53:45 +01:00
|
|
|
uint16_t ret;
|
2008-09-01 15:59:54 +02:00
|
|
|
|
|
|
|
/* Assume that we will ACK the data. The data will be ACKed if it is
|
|
|
|
* placed in the read-ahead buffer -OR- if it zero length
|
|
|
|
*/
|
|
|
|
|
2014-07-07 01:22:02 +02:00
|
|
|
ret = (flags & ~TCP_NEWDATA) | TCP_SNDACK;
|
2007-11-20 00:09:39 +01:00
|
|
|
|
|
|
|
/* Is there new data? With non-zero length? (Certain connection events
|
2014-07-07 01:22:02 +02:00
|
|
|
* can have zero-length with TCP_NEWDATA set just to cause an ACK).
|
2007-11-20 00:09:39 +01:00
|
|
|
*/
|
|
|
|
|
2008-09-01 15:59:54 +02:00
|
|
|
if (dev->d_len > 0)
|
2007-11-20 00:09:39 +01:00
|
|
|
{
|
2012-02-23 03:07:38 +01:00
|
|
|
uint8_t *buffer = dev->d_appdata;
|
|
|
|
int buflen = dev->d_len;
|
|
|
|
uint16_t recvlen;
|
2007-12-07 00:10:31 +01:00
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("No listener on connection\n");
|
2008-09-01 15:59:54 +02:00
|
|
|
|
2014-06-24 23:38:00 +02:00
|
|
|
/* Save as the packet data as in the read-ahead buffer. NOTE that
|
|
|
|
* partial packets will not be buffered.
|
2012-02-23 03:07:38 +01:00
|
|
|
*/
|
2007-11-20 00:09:39 +01:00
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
recvlen = tcp_datahandler(conn, buffer, buflen);
|
2014-06-24 23:38:00 +02:00
|
|
|
if (recvlen < buflen)
|
2007-11-20 00:09:39 +01:00
|
|
|
{
|
|
|
|
/* There is no handler to receive new data and there are no free
|
2008-09-01 15:59:54 +02:00
|
|
|
* read-ahead buffers to retain the data -- drop the packet.
|
2007-11-20 00:09:39 +01:00
|
|
|
*/
|
|
|
|
|
2021-05-28 10:09:02 +02:00
|
|
|
ninfo("Dropped %d bytes\n", dev->d_len);
|
2008-09-01 15:59:54 +02:00
|
|
|
|
2015-09-05 17:10:48 +02:00
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
2014-06-26 17:32:39 +02:00
|
|
|
g_netstats.tcp.drop++;
|
2007-11-20 00:09:39 +01:00
|
|
|
#endif
|
2021-05-28 10:44:13 +02:00
|
|
|
/* Clear the TCP_SNDACK bit so that no ACK will be sent.
|
|
|
|
*
|
|
|
|
* Revisit: It might make more sense to send a dup ack
|
|
|
|
* to give a hint to the peer.
|
|
|
|
*/
|
2008-09-01 15:59:54 +02:00
|
|
|
|
2014-07-07 01:22:02 +02:00
|
|
|
ret &= ~TCP_SNDACK;
|
2007-11-20 00:09:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-01 15:59:54 +02:00
|
|
|
/* In any event, the new data has now been handled */
|
|
|
|
|
|
|
|
dev->d_len = 0;
|
2007-11-20 00:09:39 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tcp_callback
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Inform the application holding the TCP socket of a change in state.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2017-09-02 18:27:03 +02:00
|
|
|
* This function must be called with the network locked.
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-07-06 20:34:27 +02:00
|
|
|
uint16_t tcp_callback(FAR struct net_driver_s *dev,
|
|
|
|
FAR struct tcp_conn_s *conn, uint16_t flags)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2019-12-31 16:26:14 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_NOTIFIER
|
2018-09-10 01:32:10 +02:00
|
|
|
uint16_t orig = flags;
|
|
|
|
#endif
|
|
|
|
|
2014-07-07 01:22:02 +02:00
|
|
|
/* Preserve the TCP_ACKDATA, TCP_CLOSE, and TCP_ABORT in the response.
|
2020-11-27 03:15:25 +01:00
|
|
|
* These is needed by the network to handle responses and buffer state.
|
|
|
|
* The TCP_NEWDATA indication will trigger the ACK response, but must be
|
2007-11-15 23:38:32 +01:00
|
|
|
* explicitly set in the callback.
|
2007-11-08 18:12:12 +01:00
|
|
|
*/
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("flags: %04x\n", flags);
|
2008-09-01 15:59:54 +02:00
|
|
|
|
2020-11-27 03:15:25 +01:00
|
|
|
/* Perform the data callback. When a data callback is executed from
|
|
|
|
* 'list', the input flags are normally returned, however, the
|
|
|
|
* implementation may set one of the following:
|
2008-09-01 15:59:54 +02:00
|
|
|
*
|
2014-07-07 01:22:02 +02:00
|
|
|
* TCP_CLOSE - Gracefully close the current connection
|
|
|
|
* TCP_ABORT - Abort (reset) the current connection on an error that
|
|
|
|
* prevents TCP_CLOSE from working.
|
2008-09-01 15:59:54 +02:00
|
|
|
*
|
|
|
|
* And/Or set/clear the following:
|
|
|
|
*
|
2014-07-07 01:22:02 +02:00
|
|
|
* TCP_NEWDATA - May be cleared to indicate that the data was consumed
|
2008-09-01 15:59:54 +02:00
|
|
|
* and that no further process of the new data should be
|
|
|
|
* attempted.
|
2014-07-07 01:22:02 +02:00
|
|
|
* TCP_SNDACK - If TCP_NEWDATA is cleared, then TCP_SNDACK may be set
|
2019-03-11 19:48:17 +01:00
|
|
|
* to indicate that an ACK should be included in the
|
|
|
|
* response. (In TCP_NEWDATA is cleared but TCP_SNDACK is
|
|
|
|
* not set, then dev->d_len should also be cleared).
|
2008-09-01 15:59:54 +02:00
|
|
|
*/
|
2008-11-20 22:50:55 +01:00
|
|
|
|
2015-05-28 20:01:38 +02:00
|
|
|
flags = devif_conn_event(dev, conn, flags, conn->list);
|
2008-09-01 15:59:54 +02:00
|
|
|
|
|
|
|
/* There may be no new data handler in place at them moment that the new
|
2020-11-27 03:15:25 +01:00
|
|
|
* incoming data is received. If the new incoming data was not handled,
|
|
|
|
* then either (1) put the unhandled incoming data in the read-ahead
|
|
|
|
* buffer (if enabled) or (2) suppress the ACK to the data in the hope
|
|
|
|
* that it will be re-transmitted at a better time.
|
2007-11-27 20:19:30 +01:00
|
|
|
*/
|
|
|
|
|
2014-07-07 01:22:02 +02:00
|
|
|
if ((flags & TCP_NEWDATA) != 0)
|
2007-11-15 23:38:32 +01:00
|
|
|
{
|
2008-09-01 15:59:54 +02:00
|
|
|
/* Data was not handled.. dispose of it appropriately */
|
2007-11-15 23:38:32 +01:00
|
|
|
|
2014-07-01 03:17:22 +02:00
|
|
|
flags = tcp_data_event(dev, conn, flags);
|
2007-11-08 17:01:18 +01:00
|
|
|
}
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2007-11-08 17:01:18 +01:00
|
|
|
/* Check if there is a connection-related event and a connection
|
|
|
|
* callback.
|
|
|
|
*/
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2017-08-29 18:38:01 +02:00
|
|
|
if ((flags & TCP_CONN_EVENTS) != 0)
|
2007-11-08 17:01:18 +01:00
|
|
|
{
|
2017-08-29 18:38:01 +02:00
|
|
|
/* Perform the callback disconnect callbacks */
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2017-08-29 18:38:01 +02:00
|
|
|
flags = devif_conn_event(dev, conn, flags, conn->connevents);
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
2007-11-08 17:01:18 +01:00
|
|
|
|
2019-12-31 16:26:14 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_NOTIFIER
|
2018-09-10 01:32:10 +02:00
|
|
|
/* Provide notification(s) if the TCP connection has been lost. */
|
|
|
|
|
|
|
|
if ((orig & TCP_DISCONN_EVENTS) != 0)
|
|
|
|
{
|
|
|
|
tcp_disconnect_signal(conn);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-10-08 17:24:37 +02:00
|
|
|
return flags;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2012-02-23 03:07:38 +01:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tcp_datahandler
|
2012-02-23 03:07:38 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle data that is not accepted by the application. This may be called
|
|
|
|
* either (1) from the data receive logic if it cannot buffer the data, or
|
|
|
|
* (2) from the TCP event logic is there is no listener in place ready to
|
|
|
|
* receive the data.
|
|
|
|
*
|
2014-04-12 20:13:01 +02:00
|
|
|
* Input Parameters:
|
2012-02-23 03:07:38 +01:00
|
|
|
* conn - A pointer to the TCP connection structure
|
|
|
|
* buffer - A pointer to the buffer to be copied to the read-ahead
|
|
|
|
* buffers
|
|
|
|
* buflen - The number of bytes to copy to the read-ahead buffer.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2014-06-24 23:38:00 +02:00
|
|
|
* The number of bytes actually buffered is returned. This will be either
|
|
|
|
* zero or equal to buflen; partial packets are not buffered.
|
2012-02-23 03:07:38 +01:00
|
|
|
*
|
|
|
|
* Assumptions:
|
2014-07-07 01:22:02 +02:00
|
|
|
* - The caller has checked that TCP_NEWDATA is set in flags and that is no
|
2012-02-23 03:07:38 +01:00
|
|
|
* other handler available to process the incoming data.
|
2017-09-02 18:27:03 +02:00
|
|
|
* - This function must be called with the network locked.
|
2012-02-23 03:07:38 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
uint16_t tcp_datahandler(FAR struct tcp_conn_s *conn, FAR uint8_t *buffer,
|
2012-02-23 03:07:38 +01:00
|
|
|
uint16_t buflen)
|
|
|
|
{
|
2014-06-24 23:38:00 +02:00
|
|
|
FAR struct iob_s *iob;
|
2020-11-27 02:50:38 +01:00
|
|
|
bool throttled = true;
|
2014-06-24 23:38:00 +02:00
|
|
|
int ret;
|
2012-02-23 03:07:38 +01:00
|
|
|
|
2015-01-28 04:23:42 +01:00
|
|
|
/* Try to allocate on I/O buffer to start the chain without waiting (and
|
|
|
|
* throttling as necessary). If we would have to wait, then drop the
|
|
|
|
* packet.
|
|
|
|
*/
|
2014-06-24 23:38:00 +02:00
|
|
|
|
2020-11-27 02:50:38 +01:00
|
|
|
iob = iob_tryalloc(throttled, IOBUSER_NET_TCP_READAHEAD);
|
2014-06-24 23:38:00 +02:00
|
|
|
if (iob == NULL)
|
2012-02-23 03:07:38 +01:00
|
|
|
{
|
2020-11-27 02:50:38 +01:00
|
|
|
#if CONFIG_IOB_THROTTLE > 0
|
|
|
|
if (IOB_QEMPTY(&conn->readahead))
|
|
|
|
{
|
|
|
|
/* Fallback out of the throttled entry */
|
|
|
|
|
|
|
|
throttled = false;
|
|
|
|
iob = iob_tryalloc(throttled, IOBUSER_NET_TCP_READAHEAD);
|
|
|
|
}
|
|
|
|
#endif
|
2020-11-27 03:15:25 +01:00
|
|
|
|
2020-11-27 02:50:38 +01:00
|
|
|
if (iob == NULL)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Failed to create new I/O buffer chain\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-24 23:38:00 +02:00
|
|
|
}
|
2012-02-23 03:07:38 +01:00
|
|
|
|
2015-01-28 04:23:42 +01:00
|
|
|
/* Copy the new appdata into the I/O buffer chain (without waiting) */
|
2012-02-23 03:07:38 +01:00
|
|
|
|
2020-11-27 02:50:38 +01:00
|
|
|
ret = iob_trycopyin(iob, buffer, buflen, 0, throttled,
|
2019-08-17 00:42:25 +02:00
|
|
|
IOBUSER_NET_TCP_READAHEAD);
|
2014-06-24 23:38:00 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* On a failure, iob_copyin return a negated error value but does
|
|
|
|
* not free any I/O buffers.
|
|
|
|
*/
|
2012-02-23 03:07:38 +01:00
|
|
|
|
2016-06-20 20:44:38 +02:00
|
|
|
nerr("ERROR: Failed to add data to the I/O buffer chain: %d\n", ret);
|
2020-01-02 17:49:34 +01:00
|
|
|
iob_free_chain(iob, IOBUSER_NET_TCP_READAHEAD);
|
2014-06-24 23:38:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2012-02-23 03:07:38 +01:00
|
|
|
|
2015-01-28 04:23:42 +01:00
|
|
|
/* Add the new I/O buffer chain to the tail of the read-ahead queue (again
|
|
|
|
* without waiting).
|
|
|
|
*/
|
2012-02-23 03:07:38 +01:00
|
|
|
|
2015-01-28 04:23:42 +01:00
|
|
|
ret = iob_tryadd_queue(iob, &conn->readahead);
|
2014-06-24 23:38:00 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-20 20:44:38 +02:00
|
|
|
nerr("ERROR: Failed to queue the I/O buffer chain: %d\n", ret);
|
2020-01-02 17:49:34 +01:00
|
|
|
iob_free_chain(iob, IOBUSER_NET_TCP_READAHEAD);
|
2014-06-24 23:38:00 +02:00
|
|
|
return 0;
|
2012-02-23 03:07:38 +01:00
|
|
|
}
|
|
|
|
|
2019-12-31 16:26:14 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_NOTIFIER
|
2018-09-10 01:32:10 +02:00
|
|
|
/* Provide notification(s) that additional TCP read-ahead data is
|
2018-09-09 17:21:39 +02:00
|
|
|
* available.
|
|
|
|
*/
|
|
|
|
|
2018-09-10 01:32:10 +02:00
|
|
|
tcp_readahead_signal(conn);
|
2018-09-09 17:21:39 +02:00
|
|
|
#endif
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("Buffered %d bytes\n", buflen);
|
2014-06-24 23:38:00 +02:00
|
|
|
return buflen;
|
2012-02-23 03:07:38 +01:00
|
|
|
}
|
|
|
|
|
2017-08-29 18:53:04 +02:00
|
|
|
#endif /* NET_TCP_HAVE_STACK */
|