BufferAllocation.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * FreeRTOS+TCP V2.3.2 LTS Patch 1
  3. * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * http://www.FreeRTOS.org
  23. * http://aws.amazon.com/freertos
  24. *
  25. * 1 tab == 4 spaces!
  26. */
  27. /******************************************************************************
  28. *
  29. * See the following web page for essential buffer allocation scheme usage and
  30. * configuration details:
  31. * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/Embedded_Ethernet_Buffer_Management.html
  32. *
  33. ******************************************************************************/
  34. /* THIS FILE SHOULD NOT BE USED IF THE PROJECT INCLUDES A MEMORY ALLOCATOR
  35. * THAT WILL FRAGMENT THE HEAP MEMORY. For example, heap_2 must not be used,
  36. * heap_4 can be used. */
  37. /* Standard includes. */
  38. #include <stdint.h>
  39. /* FreeRTOS includes. */
  40. #include "FreeRTOS.h"
  41. #include "task.h"
  42. #include "semphr.h"
  43. /* FreeRTOS+TCP includes. */
  44. #include "FreeRTOS_IP.h"
  45. #include "FreeRTOS_UDP_IP.h"
  46. #include "FreeRTOS_IP_Private.h"
  47. #include "NetworkInterface.h"
  48. #include "NetworkBufferManagement.h"
  49. /* The obtained network buffer must be large enough to hold a packet that might
  50. * replace the packet that was requested to be sent. */
  51. #if ipconfigUSE_TCP == 1
  52. #define baMINIMAL_BUFFER_SIZE sizeof( TCPPacket_t )
  53. #else
  54. #define baMINIMAL_BUFFER_SIZE sizeof( ARPPacket_t )
  55. #endif /* ipconfigUSE_TCP == 1 */
  56. /*_RB_ This is too complex not to have an explanation. */
  57. #if defined( ipconfigETHERNET_MINIMUM_PACKET_BYTES )
  58. #define ASSERT_CONCAT_( a, b ) a ## b
  59. #define ASSERT_CONCAT( a, b ) ASSERT_CONCAT_( a, b )
  60. #define STATIC_ASSERT( e ) \
  61. ; enum { ASSERT_CONCAT( assert_line_, __LINE__ ) = 1 / ( !!( e ) ) }
  62. STATIC_ASSERT( ipconfigETHERNET_MINIMUM_PACKET_BYTES <= baMINIMAL_BUFFER_SIZE );
  63. #endif
  64. /* A list of free (available) NetworkBufferDescriptor_t structures. */
  65. static List_t xFreeBuffersList;
  66. /* Some statistics about the use of buffers. */
  67. static size_t uxMinimumFreeNetworkBuffers;
  68. /* Declares the pool of NetworkBufferDescriptor_t structures that are available
  69. * to the system. All the network buffers referenced from xFreeBuffersList exist
  70. * in this array. The array is not accessed directly except during initialisation,
  71. * when the xFreeBuffersList is filled (as all the buffers are free when the system
  72. * is booted). */
  73. static NetworkBufferDescriptor_t xNetworkBufferDescriptors[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS ];
  74. /* This constant is defined as false to let FreeRTOS_TCP_IP.c know that the
  75. * network buffers have a variable size: resizing may be necessary */
  76. const BaseType_t xBufferAllocFixedSize = pdFALSE;
  77. /* The semaphore used to obtain network buffers. */
  78. static SemaphoreHandle_t xNetworkBufferSemaphore = NULL;
  79. /*-----------------------------------------------------------*/
  80. BaseType_t xNetworkBuffersInitialise( void )
  81. {
  82. BaseType_t xReturn;
  83. uint32_t x;
  84. /* Only initialise the buffers and their associated kernel objects if they
  85. * have not been initialised before. */
  86. if( xNetworkBufferSemaphore == NULL )
  87. {
  88. xNetworkBufferSemaphore = xSemaphoreCreateCounting( ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS, ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS );
  89. configASSERT( xNetworkBufferSemaphore != NULL );
  90. if( xNetworkBufferSemaphore != NULL )
  91. {
  92. #if ( configQUEUE_REGISTRY_SIZE > 0 )
  93. {
  94. vQueueAddToRegistry( xNetworkBufferSemaphore, "NetBufSem" );
  95. }
  96. #endif /* configQUEUE_REGISTRY_SIZE */
  97. /* If the trace recorder code is included name the semaphore for viewing
  98. * in FreeRTOS+Trace. */
  99. #if ( ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 )
  100. {
  101. extern QueueHandle_t xNetworkEventQueue;
  102. vTraceSetQueueName( xNetworkEventQueue, "IPStackEvent" );
  103. vTraceSetQueueName( xNetworkBufferSemaphore, "NetworkBufferCount" );
  104. }
  105. #endif /* ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 */
  106. vListInitialise( &xFreeBuffersList );
  107. /* Initialise all the network buffers. No storage is allocated to
  108. * the buffers yet. */
  109. for( x = 0U; x < ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS; x++ )
  110. {
  111. /* Initialise and set the owner of the buffer list items. */
  112. xNetworkBufferDescriptors[ x ].pucEthernetBuffer = NULL;
  113. vListInitialiseItem( &( xNetworkBufferDescriptors[ x ].xBufferListItem ) );
  114. listSET_LIST_ITEM_OWNER( &( xNetworkBufferDescriptors[ x ].xBufferListItem ), &xNetworkBufferDescriptors[ x ] );
  115. /* Currently, all buffers are available for use. */
  116. vListInsert( &xFreeBuffersList, &( xNetworkBufferDescriptors[ x ].xBufferListItem ) );
  117. }
  118. uxMinimumFreeNetworkBuffers = ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS;
  119. }
  120. }
  121. if( xNetworkBufferSemaphore == NULL )
  122. {
  123. xReturn = pdFAIL;
  124. }
  125. else
  126. {
  127. xReturn = pdPASS;
  128. }
  129. return xReturn;
  130. }
  131. /*-----------------------------------------------------------*/
  132. uint8_t * pucGetNetworkBuffer( size_t * pxRequestedSizeBytes )
  133. {
  134. uint8_t * pucEthernetBuffer;
  135. size_t xSize = *pxRequestedSizeBytes;
  136. if( xSize < baMINIMAL_BUFFER_SIZE )
  137. {
  138. /* Buffers must be at least large enough to hold a TCP-packet with
  139. * headers, or an ARP packet, in case TCP is not included. */
  140. xSize = baMINIMAL_BUFFER_SIZE;
  141. }
  142. /* Round up xSize to the nearest multiple of N bytes,
  143. * where N equals 'sizeof( size_t )'. */
  144. if( ( xSize & ( sizeof( size_t ) - 1U ) ) != 0U )
  145. {
  146. xSize = ( xSize | ( sizeof( size_t ) - 1U ) ) + 1U;
  147. }
  148. *pxRequestedSizeBytes = xSize;
  149. /* Allocate a buffer large enough to store the requested Ethernet frame size
  150. * and a pointer to a network buffer structure (hence the addition of
  151. * ipBUFFER_PADDING bytes). */
  152. pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( xSize + ipBUFFER_PADDING );
  153. configASSERT( pucEthernetBuffer != NULL );
  154. if( pucEthernetBuffer != NULL )
  155. {
  156. /* Enough space is left at the start of the buffer to place a pointer to
  157. * the network buffer structure that references this Ethernet buffer.
  158. * Return a pointer to the start of the Ethernet buffer itself. */
  159. pucEthernetBuffer += ipBUFFER_PADDING;
  160. }
  161. return pucEthernetBuffer;
  162. }
  163. /*-----------------------------------------------------------*/
  164. void vReleaseNetworkBuffer( uint8_t * pucEthernetBuffer )
  165. {
  166. /* There is space before the Ethernet buffer in which a pointer to the
  167. * network buffer that references this Ethernet buffer is stored. Remove the
  168. * space before freeing the buffer. */
  169. if( pucEthernetBuffer != NULL )
  170. {
  171. pucEthernetBuffer -= ipBUFFER_PADDING;
  172. vPortFree( ( void * ) pucEthernetBuffer );
  173. }
  174. }
  175. /*-----------------------------------------------------------*/
  176. NetworkBufferDescriptor_t * pxGetNetworkBufferWithDescriptor( size_t xRequestedSizeBytes,
  177. TickType_t xBlockTimeTicks )
  178. {
  179. NetworkBufferDescriptor_t * pxReturn = NULL;
  180. size_t uxCount;
  181. if( ( xRequestedSizeBytes <= ( ipconfigNETWORK_MTU + ipSIZE_OF_ETH_HEADER ) ) && ( xNetworkBufferSemaphore != NULL ) )
  182. {
  183. if( ( xRequestedSizeBytes != 0U ) && ( xRequestedSizeBytes < ( size_t ) baMINIMAL_BUFFER_SIZE ) )
  184. {
  185. /* ARP packets can replace application packets, so the storage must be
  186. * at least large enough to hold an ARP. */
  187. xRequestedSizeBytes = baMINIMAL_BUFFER_SIZE;
  188. }
  189. /* Add 2 bytes to xRequestedSizeBytes and round up xRequestedSizeBytes
  190. * to the nearest multiple of N bytes, where N equals 'sizeof( size_t )'. */
  191. xRequestedSizeBytes += 2U;
  192. if( ( xRequestedSizeBytes & ( sizeof( size_t ) - 1U ) ) != 0U )
  193. {
  194. xRequestedSizeBytes = ( xRequestedSizeBytes | ( sizeof( size_t ) - 1U ) ) + 1U;
  195. }
  196. /* If there is a semaphore available, there is a network buffer available. */
  197. if( xSemaphoreTake( xNetworkBufferSemaphore, xBlockTimeTicks ) == pdPASS )
  198. {
  199. /* Protect the structure as it is accessed from tasks and interrupts. */
  200. taskENTER_CRITICAL();
  201. {
  202. pxReturn = ( NetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );
  203. ( void ) uxListRemove( &( pxReturn->xBufferListItem ) );
  204. }
  205. taskEXIT_CRITICAL();
  206. /* Reading UBaseType_t, no critical section needed. */
  207. uxCount = listCURRENT_LIST_LENGTH( &xFreeBuffersList );
  208. if( uxMinimumFreeNetworkBuffers > uxCount )
  209. {
  210. uxMinimumFreeNetworkBuffers = uxCount;
  211. }
  212. /* Allocate storage of exactly the requested size to the buffer. */
  213. configASSERT( pxReturn->pucEthernetBuffer == NULL );
  214. if( xRequestedSizeBytes > 0U )
  215. {
  216. /* Extra space is obtained so a pointer to the network buffer can
  217. * be stored at the beginning of the buffer. */
  218. pxReturn->pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( xRequestedSizeBytes + ipBUFFER_PADDING );
  219. if( pxReturn->pucEthernetBuffer == NULL )
  220. {
  221. /* The attempt to allocate storage for the buffer payload failed,
  222. * so the network buffer structure cannot be used and must be
  223. * released. */
  224. vReleaseNetworkBufferAndDescriptor( pxReturn );
  225. pxReturn = NULL;
  226. }
  227. else
  228. {
  229. /* Store a pointer to the network buffer structure in the
  230. * buffer storage area, then move the buffer pointer on past the
  231. * stored pointer so the pointer value is not overwritten by the
  232. * application when the buffer is used. */
  233. *( ( NetworkBufferDescriptor_t ** ) ( pxReturn->pucEthernetBuffer ) ) = pxReturn;
  234. pxReturn->pucEthernetBuffer += ipBUFFER_PADDING;
  235. /* Store the actual size of the allocated buffer, which may be
  236. * greater than the original requested size. */
  237. pxReturn->xDataLength = xRequestedSizeBytes;
  238. #if ( ipconfigUSE_LINKED_RX_MESSAGES != 0 )
  239. {
  240. /* make sure the buffer is not linked */
  241. pxReturn->pxNextBuffer = NULL;
  242. }
  243. #endif /* ipconfigUSE_LINKED_RX_MESSAGES */
  244. }
  245. }
  246. else
  247. {
  248. /* A descriptor is being returned without an associated buffer being
  249. * allocated. */
  250. }
  251. }
  252. }
  253. if( pxReturn == NULL )
  254. {
  255. iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER();
  256. }
  257. else
  258. {
  259. /* No action. */
  260. iptraceNETWORK_BUFFER_OBTAINED( pxReturn );
  261. }
  262. return pxReturn;
  263. }
  264. /*-----------------------------------------------------------*/
  265. void vReleaseNetworkBufferAndDescriptor( NetworkBufferDescriptor_t * const pxNetworkBuffer )
  266. {
  267. BaseType_t xListItemAlreadyInFreeList;
  268. /* Ensure the buffer is returned to the list of free buffers before the
  269. * counting semaphore is 'given' to say a buffer is available. Release the
  270. * storage allocated to the buffer payload. THIS FILE SHOULD NOT BE USED
  271. * IF THE PROJECT INCLUDES A MEMORY ALLOCATOR THAT WILL FRAGMENT THE HEAP
  272. * MEMORY. For example, heap_2 must not be used, heap_4 can be used. */
  273. vReleaseNetworkBuffer( pxNetworkBuffer->pucEthernetBuffer );
  274. pxNetworkBuffer->pucEthernetBuffer = NULL;
  275. taskENTER_CRITICAL();
  276. {
  277. xListItemAlreadyInFreeList = listIS_CONTAINED_WITHIN( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
  278. if( xListItemAlreadyInFreeList == pdFALSE )
  279. {
  280. vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
  281. }
  282. }
  283. taskEXIT_CRITICAL();
  284. /*
  285. * Update the network state machine, unless the program fails to release its 'xNetworkBufferSemaphore'.
  286. * The program should only try to release its semaphore if 'xListItemAlreadyInFreeList' is false.
  287. */
  288. if( xListItemAlreadyInFreeList == pdFALSE )
  289. {
  290. if( xSemaphoreGive( xNetworkBufferSemaphore ) == pdTRUE )
  291. {
  292. iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );
  293. }
  294. }
  295. else
  296. {
  297. /* No action. */
  298. iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );
  299. }
  300. }
  301. /*-----------------------------------------------------------*/
  302. /*
  303. * Returns the number of free network buffers
  304. */
  305. UBaseType_t uxGetNumberOfFreeNetworkBuffers( void )
  306. {
  307. return listCURRENT_LIST_LENGTH( &xFreeBuffersList );
  308. }
  309. /*-----------------------------------------------------------*/
  310. UBaseType_t uxGetMinimumFreeNetworkBuffers( void )
  311. {
  312. return uxMinimumFreeNetworkBuffers;
  313. }
  314. /*-----------------------------------------------------------*/
  315. NetworkBufferDescriptor_t * pxResizeNetworkBufferWithDescriptor( NetworkBufferDescriptor_t * pxNetworkBuffer,
  316. size_t xNewSizeBytes )
  317. {
  318. size_t xOriginalLength;
  319. uint8_t * pucBuffer;
  320. xOriginalLength = pxNetworkBuffer->xDataLength + ipBUFFER_PADDING;
  321. xNewSizeBytes = xNewSizeBytes + ipBUFFER_PADDING;
  322. pucBuffer = pucGetNetworkBuffer( &( xNewSizeBytes ) );
  323. if( pucBuffer == NULL )
  324. {
  325. /* In case the allocation fails, return NULL. */
  326. pxNetworkBuffer = NULL;
  327. }
  328. else
  329. {
  330. pxNetworkBuffer->xDataLength = xNewSizeBytes;
  331. if( xNewSizeBytes > xOriginalLength )
  332. {
  333. xNewSizeBytes = xOriginalLength;
  334. }
  335. ( void ) memcpy( pucBuffer - ipBUFFER_PADDING, pxNetworkBuffer->pucEthernetBuffer - ipBUFFER_PADDING, xNewSizeBytes );
  336. vReleaseNetworkBuffer( pxNetworkBuffer->pucEthernetBuffer );
  337. pxNetworkBuffer->pucEthernetBuffer = pucBuffer;
  338. }
  339. return pxNetworkBuffer;
  340. }