tcp_recvwindow.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. int niob_avail;
  70. int nqentry_avail;
  71. #ifdef CONFIG_NET_IPv6
  72. #ifdef CONFIG_NET_IPv4
  73. if (IFF_IS_IPv6(dev->d_flags))
  74. #endif
  75. {
  76. iplen = IPv6_HDRLEN;
  77. }
  78. #endif /* CONFIG_NET_IPv6 */
  79. #ifdef CONFIG_NET_IPv4
  80. #ifdef CONFIG_NET_IPv6
  81. else
  82. #endif
  83. {
  84. iplen = IPv4_HDRLEN;
  85. }
  86. #endif /* CONFIG_NET_IPv4 */
  87. /* Calculate the packet MSS.
  88. *
  89. * REVISIT: The actual TCP header length is variable. TCP_HDRLEN
  90. * is the minimum size.
  91. */
  92. mss = dev->d_pktsize - (NET_LL_HDRLEN(dev) + iplen + TCP_HDRLEN);
  93. /* Update the TCP received window based on read-ahead I/O buffer
  94. * and IOB chain availability. At least one queue entry is required.
  95. * If one queue entry is available, then the amount of read-ahead
  96. * data that can be buffered is given by the number of IOBs available
  97. * (ignoring competition with other IOB consumers).
  98. */
  99. niob_avail = iob_navail(true);
  100. nqentry_avail = iob_qentry_navail();
  101. /* Are the read-ahead allocations throttled? If so, then not all of these
  102. * IOBs are available for read-ahead buffering.
  103. *
  104. * REVISIT: Should also check that there is at least one available IOB
  105. * chain.
  106. */
  107. #if CONFIG_IOB_THROTTLE > 0
  108. if (niob_avail > CONFIG_IOB_THROTTLE)
  109. {
  110. niob_avail -= CONFIG_IOB_THROTTLE;
  111. }
  112. else
  113. {
  114. niob_avail = 0;
  115. }
  116. #endif
  117. /* Is there a a queue entry and IOBs available for read-ahead buffering? */
  118. if (nqentry_avail > 0 && niob_avail > 0)
  119. {
  120. uint32_t rwnd;
  121. /* The optimal TCP window size is the amount of TCP data that we can
  122. * currently buffer via TCP read-ahead buffering plus MSS for the
  123. * device packet buffer. This logic here assumes that all IOBs are
  124. * available for TCP buffering.
  125. *
  126. * Assume that all of the available IOBs are can be used for buffering
  127. * on this connection. Also assume that at least one chain is available
  128. * concatenate the IOBs.
  129. *
  130. * REVISIT: In an environment with multiple, active read-ahead TCP
  131. * sockets (and perhaps multiple network devices) or if there are
  132. * other consumers of IOBs (such as for TCP write buffering) then the
  133. * total number of IOBs will all not be available for read-ahead
  134. * buffering for this connection.
  135. */
  136. rwnd = (niob_avail * CONFIG_IOB_BUFSIZE) + mss;
  137. if (rwnd > UINT16_MAX)
  138. {
  139. rwnd = UINT16_MAX;
  140. }
  141. /* Save the new receive window size */
  142. recvwndo = (uint16_t)rwnd;
  143. }
  144. else /* nqentry_avail == 0 || niob_avail == 0 */
  145. {
  146. /* No IOB chains or noIOBs are available. The only buffering
  147. * available is within the packet buffer itself. We can buffer no
  148. * more than the MSS (unless we are very fast).
  149. *
  150. * NOTE: If no IOBs are available, then the next packet will be
  151. * lost if there is no listener on the connection.
  152. */
  153. recvwndo = mss;
  154. }
  155. return recvwndo;
  156. }