tcp_callback.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /****************************************************************************
  2. * net/tcp/tcp_callback.c
  3. *
  4. * Copyright (C) 2007-2009, 2014, 2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <stdint.h>
  40. #include <string.h>
  41. #include <debug.h>
  42. #include <nuttx/mm/iob.h>
  43. #include <nuttx/net/netconfig.h>
  44. #include <nuttx/net/netdev.h>
  45. #include <nuttx/net/netstats.h>
  46. #include "devif/devif.h"
  47. #include "tcp/tcp.h"
  48. #ifdef NET_TCP_HAVE_STACK
  49. /****************************************************************************
  50. * Private Functions
  51. ****************************************************************************/
  52. /****************************************************************************
  53. * Name: tcp_data_event
  54. *
  55. * Description:
  56. * Handle data that is not accepted by the application because there is no
  57. * listener in place ready to receive the data.
  58. *
  59. * Assumptions:
  60. * - The caller has checked that TCP_NEWDATA is set in flags and that is no
  61. * other handler available to process the incoming data.
  62. * - This function must be called with the network locked.
  63. *
  64. ****************************************************************************/
  65. static inline uint16_t
  66. tcp_data_event(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn,
  67. uint16_t flags)
  68. {
  69. uint16_t ret;
  70. /* Assume that we will ACK the data. The data will be ACKed if it is
  71. * placed in the read-ahead buffer -OR- if it zero length
  72. */
  73. ret = (flags & ~TCP_NEWDATA) | TCP_SNDACK;
  74. /* Is there new data? With non-zero length? (Certain connection events
  75. * can have zero-length with TCP_NEWDATA set just to cause an ACK).
  76. */
  77. if (dev->d_len > 0)
  78. {
  79. #ifdef CONFIG_NET_TCP_READAHEAD
  80. uint8_t *buffer = dev->d_appdata;
  81. int buflen = dev->d_len;
  82. uint16_t recvlen;
  83. #endif
  84. ninfo("No listener on connection\n");
  85. #ifdef CONFIG_NET_TCP_READAHEAD
  86. /* Save as the packet data as in the read-ahead buffer. NOTE that
  87. * partial packets will not be buffered.
  88. */
  89. recvlen = tcp_datahandler(conn, buffer, buflen);
  90. if (recvlen < buflen)
  91. #endif
  92. {
  93. /* There is no handler to receive new data and there are no free
  94. * read-ahead buffers to retain the data -- drop the packet.
  95. */
  96. ninfo("Dropped %d bytes\n", dev->d_len);
  97. #ifdef CONFIG_NET_STATISTICS
  98. g_netstats.tcp.drop++;
  99. #endif
  100. /* Clear the TCP_SNDACK bit so that no ACK will be sent */
  101. ret &= ~TCP_SNDACK;
  102. }
  103. }
  104. /* In any event, the new data has now been handled */
  105. dev->d_len = 0;
  106. return ret;
  107. }
  108. /****************************************************************************
  109. * Public Functions
  110. ****************************************************************************/
  111. /****************************************************************************
  112. * Name: tcp_callback
  113. *
  114. * Description:
  115. * Inform the application holding the TCP socket of a change in state.
  116. *
  117. * Assumptions:
  118. * This function must be called with the network locked.
  119. *
  120. ****************************************************************************/
  121. uint16_t tcp_callback(FAR struct net_driver_s *dev,
  122. FAR struct tcp_conn_s *conn, uint16_t flags)
  123. {
  124. #ifdef CONFIG_TCP_NOTIFIER
  125. uint16_t orig = flags;
  126. #endif
  127. /* Preserve the TCP_ACKDATA, TCP_CLOSE, and TCP_ABORT in the response.
  128. * These is needed by the network to handle responses and buffer state. The
  129. * TCP_NEWDATA indication will trigger the ACK response, but must be
  130. * explicitly set in the callback.
  131. */
  132. ninfo("flags: %04x\n", flags);
  133. /* Perform the data callback. When a data callback is executed from 'list',
  134. * the input flags are normally returned, however, the implementation
  135. * may set one of the following:
  136. *
  137. * TCP_CLOSE - Gracefully close the current connection
  138. * TCP_ABORT - Abort (reset) the current connection on an error that
  139. * prevents TCP_CLOSE from working.
  140. *
  141. * And/Or set/clear the following:
  142. *
  143. * TCP_NEWDATA - May be cleared to indicate that the data was consumed
  144. * and that no further process of the new data should be
  145. * attempted.
  146. * TCP_SNDACK - If TCP_NEWDATA is cleared, then TCP_SNDACK may be set
  147. * to indicate that an ACK should be included in the
  148. * response. (In TCP_NEWDATA is cleared but TCP_SNDACK is
  149. * not set, then dev->d_len should also be cleared).
  150. */
  151. flags = devif_conn_event(dev, conn, flags, conn->list);
  152. /* There may be no new data handler in place at them moment that the new
  153. * incoming data is received. If the new incoming data was not handled, then
  154. * either (1) put the unhandled incoming data in the read-ahead buffer (if
  155. * enabled) or (2) suppress the ACK to the data in the hope that it will
  156. * be re-transmitted at a better time.
  157. */
  158. if ((flags & TCP_NEWDATA) != 0)
  159. {
  160. /* Data was not handled.. dispose of it appropriately */
  161. flags = tcp_data_event(dev, conn, flags);
  162. }
  163. /* Check if there is a connection-related event and a connection
  164. * callback.
  165. */
  166. if ((flags & TCP_CONN_EVENTS) != 0)
  167. {
  168. /* Perform the callback disconnect callbacks */
  169. flags = devif_conn_event(dev, conn, flags, conn->connevents);
  170. }
  171. #ifdef CONFIG_TCP_NOTIFIER
  172. /* Provide notification(s) if the TCP connection has been lost. */
  173. if ((orig & TCP_DISCONN_EVENTS) != 0)
  174. {
  175. tcp_disconnect_signal(conn);
  176. }
  177. #endif
  178. return flags;
  179. }
  180. /****************************************************************************
  181. * Name: tcp_datahandler
  182. *
  183. * Description:
  184. * Handle data that is not accepted by the application. This may be called
  185. * either (1) from the data receive logic if it cannot buffer the data, or
  186. * (2) from the TCP event logic is there is no listener in place ready to
  187. * receive the data.
  188. *
  189. * Input Parameters:
  190. * conn - A pointer to the TCP connection structure
  191. * buffer - A pointer to the buffer to be copied to the read-ahead
  192. * buffers
  193. * buflen - The number of bytes to copy to the read-ahead buffer.
  194. *
  195. * Returned Value:
  196. * The number of bytes actually buffered is returned. This will be either
  197. * zero or equal to buflen; partial packets are not buffered.
  198. *
  199. * Assumptions:
  200. * - The caller has checked that TCP_NEWDATA is set in flags and that is no
  201. * other handler available to process the incoming data.
  202. * - This function must be called with the network locked.
  203. *
  204. ****************************************************************************/
  205. #ifdef CONFIG_NET_TCP_READAHEAD
  206. uint16_t tcp_datahandler(FAR struct tcp_conn_s *conn, FAR uint8_t *buffer,
  207. uint16_t buflen)
  208. {
  209. FAR struct iob_s *iob;
  210. int ret;
  211. /* Try to allocate on I/O buffer to start the chain without waiting (and
  212. * throttling as necessary). If we would have to wait, then drop the
  213. * packet.
  214. */
  215. iob = iob_tryalloc(true);
  216. if (iob == NULL)
  217. {
  218. nerr("ERROR: Failed to create new I/O buffer chain\n");
  219. return 0;
  220. }
  221. /* Copy the new appdata into the I/O buffer chain (without waiting) */
  222. ret = iob_trycopyin(iob, buffer, buflen, 0, true);
  223. if (ret < 0)
  224. {
  225. /* On a failure, iob_copyin return a negated error value but does
  226. * not free any I/O buffers.
  227. */
  228. nerr("ERROR: Failed to add data to the I/O buffer chain: %d\n", ret);
  229. (void)iob_free_chain(iob);
  230. return 0;
  231. }
  232. /* Add the new I/O buffer chain to the tail of the read-ahead queue (again
  233. * without waiting).
  234. */
  235. ret = iob_tryadd_queue(iob, &conn->readahead);
  236. if (ret < 0)
  237. {
  238. nerr("ERROR: Failed to queue the I/O buffer chain: %d\n", ret);
  239. (void)iob_free_chain(iob);
  240. return 0;
  241. }
  242. #ifdef CONFIG_TCP_NOTIFIER
  243. /* Provide notification(s) that additional TCP read-ahead data is
  244. * available.
  245. */
  246. tcp_readahead_signal(conn);
  247. #endif
  248. ninfo("Buffered %d bytes\n", buflen);
  249. return buflen;
  250. }
  251. #endif /* CONFIG_NET_TCP_READAHEAD */
  252. #endif /* NET_TCP_HAVE_STACK */