tcp_wrbuffer.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /****************************************************************************
  2. * net/tcp/tcp_wrbuffer.c
  3. *
  4. * Copyright (C) 2007-2009, 2013-2014, 2018 Gregory Nutt. All rights
  5. * reserved.
  6. * Author: Gregory Nutt <gnutt@nuttx.org>
  7. * Jason Jiang <jasonj@live.cn>
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. * 3. Neither the name NuttX nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  30. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  31. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  33. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. ****************************************************************************/
  37. /****************************************************************************
  38. * Included Files
  39. ****************************************************************************/
  40. #include <nuttx/net/netconfig.h>
  41. #if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_NET_TCP_WRBUFFER_DEBUG)
  42. /* Force debug output (from this file only) */
  43. # undef CONFIG_DEBUG_NET
  44. # define CONFIG_DEBUG_NET 1
  45. #endif
  46. #include <queue.h>
  47. #include <string.h>
  48. #include <assert.h>
  49. #include <errno.h>
  50. #include <debug.h>
  51. #include <nuttx/semaphore.h>
  52. #include <nuttx/net/net.h>
  53. #include <nuttx/mm/iob.h>
  54. #include "utils/utils.h"
  55. #include "tcp/tcp.h"
  56. #if defined(CONFIG_NET_TCP) && defined(CONFIG_NET_TCP_WRITE_BUFFERS)
  57. /****************************************************************************
  58. * Private Types
  59. ****************************************************************************/
  60. /* Package all globals used by this logic into a structure */
  61. struct wrbuffer_s
  62. {
  63. /* The semaphore to protect the buffers */
  64. sem_t sem;
  65. /* This is the list of available write buffers */
  66. sq_queue_t freebuffers;
  67. /* These are the pre-allocated write buffers */
  68. struct tcp_wrbuffer_s buffers[CONFIG_NET_TCP_NWRBCHAINS];
  69. };
  70. /****************************************************************************
  71. * Private Data
  72. ****************************************************************************/
  73. /* This is the state of the global write buffer resource */
  74. static struct wrbuffer_s g_wrbuffer;
  75. /****************************************************************************
  76. * Public Functions
  77. ****************************************************************************/
  78. /****************************************************************************
  79. * Name: tcp_wrbuffer_initialize
  80. *
  81. * Description:
  82. * Initialize the list of free write buffers
  83. *
  84. * Assumptions:
  85. * Called once early initialization.
  86. *
  87. ****************************************************************************/
  88. void tcp_wrbuffer_initialize(void)
  89. {
  90. int i;
  91. sq_init(&g_wrbuffer.freebuffers);
  92. for (i = 0; i < CONFIG_NET_TCP_NWRBCHAINS; i++)
  93. {
  94. sq_addfirst(&g_wrbuffer.buffers[i].wb_node, &g_wrbuffer.freebuffers);
  95. }
  96. nxsem_init(&g_wrbuffer.sem, 0, CONFIG_NET_TCP_NWRBCHAINS);
  97. }
  98. /****************************************************************************
  99. * Name: tcp_wrbuffer_alloc
  100. *
  101. * Description:
  102. * Allocate a TCP write buffer by taking a pre-allocated buffer from
  103. * the free list. This function is called from TCP logic when a buffer
  104. * of TCP data is about to sent
  105. *
  106. * Input Parameters:
  107. * None
  108. *
  109. * Assumptions:
  110. * Called from user logic with the network locked.
  111. *
  112. ****************************************************************************/
  113. FAR struct tcp_wrbuffer_s *tcp_wrbuffer_alloc(void)
  114. {
  115. FAR struct tcp_wrbuffer_s *wrb;
  116. /* We need to allocate two things: (1) A write buffer structure and (2)
  117. * at least one I/O buffer to start the chain.
  118. *
  119. * Allocate the write buffer structure first then the IOB. In order to
  120. * avoid deadlocks, we will need to free the IOB first, then the write
  121. * buffer
  122. */
  123. DEBUGVERIFY(net_lockedwait(&g_wrbuffer.sem)); /* TODO: Handle EINTR. */
  124. /* Now, we are guaranteed to have a write buffer structure reserved
  125. * for us in the free list.
  126. */
  127. wrb = (FAR struct tcp_wrbuffer_s *)sq_remfirst(&g_wrbuffer.freebuffers);
  128. DEBUGASSERT(wrb);
  129. memset(wrb, 0, sizeof(struct tcp_wrbuffer_s));
  130. /* Now get the first I/O buffer for the write buffer structure */
  131. wrb->wb_iob = net_ioballoc(false, IOBUSER_NET_TCP_WRITEBUFFER);
  132. /* Did we get an IOB? We should always get one except under some really
  133. * weird error conditions.
  134. */
  135. if (wrb->wb_iob == NULL)
  136. {
  137. nerr("ERROR: Failed to allocate I/O buffer\n");
  138. tcp_wrbuffer_release(wrb);
  139. return NULL;
  140. }
  141. return wrb;
  142. }
  143. /****************************************************************************
  144. * Name: tcp_wrbuffer_tryalloc
  145. *
  146. * Description:
  147. * Try to allocate a TCP write buffer by taking a pre-allocated buffer from
  148. * the free list. This function is called from TCP logic when a buffer
  149. * of TCP data is about to be sent on a non-blocking socket. Returns
  150. * immediately if the allocation failed.
  151. *
  152. * Input parameters:
  153. * None
  154. *
  155. * Assumptions:
  156. * Called from user logic with the network locked. Will return if no buffer
  157. * is available.
  158. *
  159. ****************************************************************************/
  160. FAR struct tcp_wrbuffer_s *tcp_wrbuffer_tryalloc(void)
  161. {
  162. FAR struct tcp_wrbuffer_s *wrb;
  163. /* We need to allocate two things: (1) A write buffer structure and (2)
  164. * at least one I/O buffer to start the chain.
  165. *
  166. * Allocate the write buffer structure first then the IOBG. In order to
  167. * avoid deadlocks, we will need to free the IOB first, then the write
  168. * buffer
  169. */
  170. if (tcp_wrbuffer_test() == OK)
  171. {
  172. DEBUGVERIFY(net_lockedwait(&g_wrbuffer.sem));
  173. }
  174. else
  175. {
  176. return NULL;
  177. }
  178. /* Now, we are guaranteed to have a write buffer structure reserved
  179. * for us in the free list.
  180. */
  181. wrb = (FAR struct tcp_wrbuffer_s *)sq_remfirst(&g_wrbuffer.freebuffers);
  182. DEBUGASSERT(wrb);
  183. memset(wrb, 0, sizeof(struct tcp_wrbuffer_s));
  184. /* Now get the first I/O buffer for the write buffer structure */
  185. wrb->wb_iob = iob_tryalloc(false, IOBUSER_NET_TCP_WRITEBUFFER);
  186. if (!wrb->wb_iob)
  187. {
  188. nerr("ERROR: Failed to allocate I/O buffer\n");
  189. tcp_wrbuffer_release(wrb);
  190. return NULL;
  191. }
  192. return wrb;
  193. }
  194. /****************************************************************************
  195. * Name: tcp_wrbuffer_release
  196. *
  197. * Description:
  198. * Release a TCP write buffer by returning the buffer to the free list.
  199. * This function is called from user logic after it is consumed the
  200. * buffered data.
  201. *
  202. * Assumptions:
  203. * This function must be called with the network locked.
  204. *
  205. ****************************************************************************/
  206. void tcp_wrbuffer_release(FAR struct tcp_wrbuffer_s *wrb)
  207. {
  208. DEBUGASSERT(wrb != NULL);
  209. /* To avoid deadlocks, we must following this ordering: Release the I/O
  210. * buffer chain first, then the write buffer structure.
  211. */
  212. if (wrb->wb_iob != NULL)
  213. {
  214. iob_free_chain(wrb->wb_iob, IOBUSER_NET_TCP_WRITEBUFFER);
  215. }
  216. /* Then free the write buffer structure */
  217. sq_addlast(&wrb->wb_node, &g_wrbuffer.freebuffers);
  218. nxsem_post(&g_wrbuffer.sem);
  219. }
  220. /****************************************************************************
  221. * Name: tcp_wrbuffer_test
  222. *
  223. * Description:
  224. * Check if there is room in the write buffer. Does not reserve any space.
  225. *
  226. * Assumptions:
  227. * None.
  228. *
  229. ****************************************************************************/
  230. int tcp_wrbuffer_test(void)
  231. {
  232. int val = 0;
  233. int ret;
  234. ret = nxsem_getvalue(&g_wrbuffer.sem, &val);
  235. if (ret >= 0)
  236. {
  237. ret = val > 0 ? OK : -ENOSPC;
  238. }
  239. return ret;
  240. }
  241. #endif /* CONFIG_NET_TCP && CONFIG_NET_TCP_WRITE_BUFFERS */