tcp_recvwindow.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /****************************************************************************
  2. * net/tcp/tcp_recvwindow.c
  3. *
  4. * Copyright (C) 2018 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 <stdbool.h>
  41. #include <debug.h>
  42. #include <net/if.h>
  43. #include <nuttx/mm/iob.h>
  44. #include <nuttx/net/netconfig.h>
  45. #include <nuttx/net/netdev.h>
  46. #include <nuttx/net/tcp.h>
  47. #include "tcp/tcp.h"
  48. /****************************************************************************
  49. * Public Functions
  50. ****************************************************************************/
  51. /****************************************************************************
  52. * Name: tcp_get_recvwindow
  53. *
  54. * Description:
  55. * Calculate the TCP receive window for the specified device.
  56. *
  57. * Input Parameters:
  58. * dev - The device whose TCP receive window will be updated.
  59. *
  60. * Returned Value:
  61. * The value of the TCP receive window to use.
  62. *
  63. ****************************************************************************/
  64. uint16_t tcp_get_recvwindow(FAR struct net_driver_s *dev)
  65. {
  66. uint16_t iplen;
  67. uint16_t mss;
  68. uint16_t recvwndo;
  69. #ifdef CONFIG_NET_TCP_READAHEAD
  70. int niob_avail;
  71. int nqentry_avail;
  72. #endif
  73. #ifdef CONFIG_NET_IPv6
  74. #ifdef CONFIG_NET_IPv4
  75. if (IFF_IS_IPv6(dev->d_flags))
  76. #endif
  77. {
  78. iplen = IPv6_HDRLEN;
  79. }
  80. #endif /* CONFIG_NET_IPv6 */
  81. #ifdef CONFIG_NET_IPv4
  82. #ifdef CONFIG_NET_IPv6
  83. else
  84. #endif
  85. {
  86. iplen = IPv4_HDRLEN;
  87. }
  88. #endif /* CONFIG_NET_IPv4 */
  89. /* Calculate the packet MSS.
  90. *
  91. * REVISIT: The actual TCP header length is variable. TCP_HDRLEN
  92. * is the minimum size.
  93. */
  94. mss = dev->d_pktsize - (NET_LL_HDRLEN(dev) + iplen + TCP_HDRLEN);
  95. #ifdef CONFIG_NET_TCP_READAHEAD
  96. /* Update the TCP received window based on read-ahead I/O buffer
  97. * and IOB chain availability. At least one queue entry is required.
  98. * If one queue entry is available, then the amount of read-ahead
  99. * data that can be buffered is given by the number of IOBs available
  100. * (ignoring competition with other IOB consumers).
  101. */
  102. niob_avail = iob_navail(true);
  103. nqentry_avail = iob_qentry_navail();
  104. /* Are the read-ahead allocations throttled? If so, then not all of these
  105. * IOBs are available for read-ahead buffering.
  106. *
  107. * REVISIT: Should also check that there is at least one available IOB
  108. * chain.
  109. */
  110. #if CONFIG_IOB_THROTTLE > 0
  111. if (niob_avail > CONFIG_IOB_THROTTLE)
  112. {
  113. niob_avail -= CONFIG_IOB_THROTTLE;
  114. }
  115. else
  116. {
  117. niob_avail = 0;
  118. }
  119. #endif
  120. /* Is there a a queue entry and IOBs available for read-ahead buffering? */
  121. if (nqentry_avail > 0 && niob_avail > 0)
  122. {
  123. uint32_t rwnd;
  124. /* The optimal TCP window size is the amount of TCP data that we can
  125. * currently buffer via TCP read-ahead buffering plus MSS for the
  126. * device packet buffer. This logic here assumes that all IOBs are
  127. * available for TCP buffering.
  128. *
  129. * Assume that all of the available IOBs are can be used for buffering
  130. * on this connection. Also assume that at least one chain is available
  131. * concatenate the IOBs.
  132. *
  133. * REVISIT: In an environment with multiple, active read-ahead TCP
  134. * sockets (and perhaps multiple network devices) or if there are
  135. * other consumers of IOBs (such as for TCP write buffering) then the
  136. * total number of IOBs will all not be available for read-ahead
  137. * buffering for this connection.
  138. */
  139. rwnd = (niob_avail * CONFIG_IOB_BUFSIZE) + mss;
  140. if (rwnd > UINT16_MAX)
  141. {
  142. rwnd = UINT16_MAX;
  143. }
  144. /* Save the new receive window size */
  145. recvwndo = (uint16_t)rwnd;
  146. }
  147. else /* nqentry_avail == 0 || niob_avail == 0 */
  148. #endif
  149. {
  150. /* No IOB chains or noIOBs are available. The only buffering
  151. * available is within the packet buffer itself. We can buffer no
  152. * more than the MSS (unless we are very fast).
  153. *
  154. * NOTE: If no IOBs are available, then the next packet will be
  155. * lost if there is no listener on the connection.
  156. */
  157. recvwndo = mss;
  158. }
  159. return recvwndo;
  160. }