summaryrefslogtreecommitdiff
path: root/nuttx/net/uip/uip_tcpcallback.c
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2012-02-23 02:07:38 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2012-02-23 02:07:38 +0000
commit23a869d9be23a33962c55a1e6f62e6b91b2cbfc4 (patch)
tree2a6defc7f12a61c60e3e337e9ba8e8fd7e21f342 /nuttx/net/uip/uip_tcpcallback.c
parent698e2f564261325aeb92666f137abfd90a7f95ad (diff)
downloadpx4-nuttx-23a869d9be23a33962c55a1e6f62e6b91b2cbfc4.tar.gz
px4-nuttx-23a869d9be23a33962c55a1e6f62e6b91b2cbfc4.tar.bz2
px4-nuttx-23a869d9be23a33962c55a1e6f62e6b91b2cbfc4.zip
(1) Fix a critical memory leak in the TCP read-ahead buffering logic; Add an option to suppress SDIO multi-block transfers in order to work around a buggy SDIO driver
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@4415 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/net/uip/uip_tcpcallback.c')
-rw-r--r--nuttx/net/uip/uip_tcpcallback.c152
1 files changed, 101 insertions, 51 deletions
diff --git a/nuttx/net/uip/uip_tcpcallback.c b/nuttx/net/uip/uip_tcpcallback.c
index 3f6d04c06..099c29bf3 100644
--- a/nuttx/net/uip/uip_tcpcallback.c
+++ b/nuttx/net/uip/uip_tcpcallback.c
@@ -38,6 +38,7 @@
****************************************************************************/
#include <nuttx/config.h>
+
#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP)
#include <stdint.h>
@@ -102,18 +103,19 @@ static int uip_readahead(struct uip_readahead_s *readahead, uint8_t *buf,
* Function: uip_dataevent
*
* Description:
- * This is the default data event handler that is called when there is no
- * user data handler in place
+ * Handle data that is not accepted by the application because there is no
+ * listener in place ready to receive the data.
*
* Assumptions:
- * - The called has checked that UIP_NEWDATA is set in flags and that is no
+ * - The caller has checked that UIP_NEWDATA is set in flags and that is no
* other handler available to process the incoming data.
* - This function is called at the interrupt level with interrupts disabled.
*
****************************************************************************/
static inline uint16_t
-uip_dataevent(struct uip_driver_s *dev, struct uip_conn *conn, uint16_t flags)
+uip_dataevent(FAR struct uip_driver_s *dev, FAR struct uip_conn *conn,
+ uint16_t flags)
{
uint16_t ret;
@@ -130,61 +132,25 @@ uip_dataevent(struct uip_driver_s *dev, struct uip_conn *conn, uint16_t flags)
if (dev->d_len > 0)
{
#if CONFIG_NET_NTCP_READAHEAD_BUFFERS > 0
- struct uip_readahead_s *readahead1;
- struct uip_readahead_s *readahead2 = NULL;
- uint16_t recvlen = 0;
- uint8_t *buf = dev->d_appdata;
- int buflen = dev->d_len;
+ uint8_t *buffer = dev->d_appdata;
+ int buflen = dev->d_len;
+ uint16_t recvlen;
#endif
nllvdbg("No listener on connection\n");
#if CONFIG_NET_NTCP_READAHEAD_BUFFERS > 0
- /* First, we need to determine if we have space to buffer the data. This
- * needs to be verified before we actually begin buffering the data. We
- * will use any remaining space in the last allocated readahead buffer
- * plus as much one additional buffer. It is expected that the size of
- * readahead buffers are tuned so that one full packet will always fit
- * into one readahead buffer (for example if the buffer size is 420, then
- * a readahead buffer of 366 will hold a full packet of TCP data).
- */
+ /* Save as much data as possible in the read-ahead buffers */
- readahead1 = (struct uip_readahead_s*)conn->readahead.tail;
- if ((readahead1 &&
- (CONFIG_NET_TCP_READAHEAD_BUFSIZE - readahead1->rh_nbytes) > buflen) ||
- (readahead2 = uip_tcpreadaheadalloc()) != NULL)
- {
- /* We have buffer space. Now try to append add as much data as possible
- * to the last readahead buffer attached to this connection.
- */
-
- if (readahead1)
- {
- recvlen = uip_readahead(readahead1, buf, buflen);
- if (recvlen > 0)
- {
- buf += recvlen;
- buflen -= recvlen;
- }
- }
+ recvlen = uip_datahandler(conn, buffer, buflen);
- /* Do we need to buffer into the newly allocated buffer as well? */
-
- if (readahead2)
- {
- readahead2->rh_nbytes = 0;
- (void)uip_readahead(readahead2, buf, buflen);
-
- /* Save the readahead buffer in the connection structure where
- * it can be found with recv() is called.
- */
-
- sq_addlast(&readahead2->rh_node, &conn->readahead);
- }
+ /* There are several complicated buffering issues that are not addressed
+ * properly here. For example, what if we cannot buffer the entire
+ * packet? In that case, some data will be accepted but not ACKed.
+ * Therefore it will be resent and duplicated. Fixing this could be tricky.
+ */
- nllvdbg("Buffered %d bytes\n", dev->d_len);
- }
- else
+ if (recvlen < buflen)
#endif
{
/* There is no handler to receive new data and there are no free
@@ -286,4 +252,88 @@ uint16_t uip_tcpcallback(struct uip_driver_s *dev, struct uip_conn *conn,
return ret;
}
+/****************************************************************************
+ * Function: uip_datahandler
+ *
+ * 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.
+ *
+ * Input Parmeters:
+ * 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.
+ *
+ * Returned value:
+ * The number of bytes actually buffered. This could be less than 'nbytes'
+ * if there is insufficient buffering available.
+ *
+ * Assumptions:
+ * - The caller has checked that UIP_NEWDATA is set in flags and that is no
+ * other handler available to process the incoming data.
+ * - This function is called at the interrupt level with interrupts disabled.
+ *
+ ****************************************************************************/
+
+#if CONFIG_NET_NTCP_READAHEAD_BUFFERS > 0
+uint16_t uip_datahandler(FAR struct uip_conn *conn, FAR uint8_t *buffer,
+ uint16_t buflen)
+{
+ FAR struct uip_readahead_s *readahead1;
+ FAR struct uip_readahead_s *readahead2 = NULL;
+ uint16_t remaining;
+ uint16_t recvlen = 0;
+
+ /* First, we need to determine if we have space to buffer the data. This
+ * needs to be verified before we actually begin buffering the data. We
+ * will use any remaining space in the last allocated readahead buffer
+ * plus as much one additional buffer. It is expected that the size of
+ * readahead buffers are tuned so that one full packet will always fit
+ * into one readahead buffer (for example if the buffer size is 420, then
+ * a readahead buffer of 366 will hold a full packet of TCP data).
+ */
+
+ readahead1 = (FAR struct uip_readahead_s*)conn->readahead.tail;
+ if ((readahead1 &&
+ (CONFIG_NET_TCP_READAHEAD_BUFSIZE - readahead1->rh_nbytes) > buflen) ||
+ (readahead2 = uip_tcpreadaheadalloc()) != NULL)
+ {
+ /* We have buffer space. Now try to append add as much data as possible
+ * to the last readahead buffer attached to this connection.
+ */
+
+ remaining = buflen;
+ if (readahead1)
+ {
+ recvlen = uip_readahead(readahead1, buffer, remaining);
+ if (recvlen > 0)
+ {
+ buffer += recvlen;
+ remaining -= recvlen;
+ }
+ }
+
+ /* Do we need to buffer into the newly allocated buffer as well? */
+
+ if (readahead2)
+ {
+ readahead2->rh_nbytes = 0;
+ recvlen += uip_readahead(readahead2, buffer, remaining);
+
+ /* Save the readahead buffer in the connection structure where
+ * it can be found with recv() is called.
+ */
+
+ sq_addlast(&readahead2->rh_node, &conn->readahead);
+ }
+ }
+
+ nllvdbg("Buffered %d bytes (of %d)\n", recvlen, buflen);
+ return recvlen;
+}
+#endif /* CONFIG_NET_NTCP_READAHEAD_BUFFERS > 0 */
+
#endif /* CONFIG_NET && CONFIG_NET_TCP */