BufferAllocation_1.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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://aws.amazon.com/freertos
  23. * http://www.FreeRTOS.org
  24. */
  25. /******************************************************************************
  26. *
  27. * See the following web page for essential buffer allocation scheme usage and
  28. * configuration details:
  29. * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/Embedded_Ethernet_Buffer_Management.html
  30. *
  31. ******************************************************************************/
  32. /* Standard includes. */
  33. #include <stdint.h>
  34. /* FreeRTOS includes. */
  35. #include "FreeRTOS.h"
  36. #include "task.h"
  37. #include "queue.h"
  38. #include "semphr.h"
  39. /* FreeRTOS+TCP includes. */
  40. #include "FreeRTOS_IP.h"
  41. #include "FreeRTOS_IP_Private.h"
  42. #include "NetworkInterface.h"
  43. #include "NetworkBufferManagement.h"
  44. /* For an Ethernet interrupt to be able to obtain a network buffer there must
  45. * be at least this number of buffers available. */
  46. #define baINTERRUPT_BUFFER_GET_THRESHOLD ( 3 )
  47. /* A list of free (available) NetworkBufferDescriptor_t structures. */
  48. static List_t xFreeBuffersList;
  49. /* Some statistics about the use of buffers. */
  50. static UBaseType_t uxMinimumFreeNetworkBuffers = 0U;
  51. /* Declares the pool of NetworkBufferDescriptor_t structures that are available
  52. * to the system. All the network buffers referenced from xFreeBuffersList exist
  53. * in this array. The array is not accessed directly except during initialisation,
  54. * when the xFreeBuffersList is filled (as all the buffers are free when the system
  55. * is booted). */
  56. static NetworkBufferDescriptor_t xNetworkBuffers[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS ];
  57. /* This constant is defined as true to let FreeRTOS_TCP_IP.c know that the
  58. * network buffers have constant size, large enough to hold the biggest Ethernet
  59. * packet. No resizing will be done. */
  60. const BaseType_t xBufferAllocFixedSize = pdTRUE;
  61. /* The semaphore used to obtain network buffers. */
  62. static SemaphoreHandle_t xNetworkBufferSemaphore = NULL;
  63. #if ( ipconfigTCP_IP_SANITY != 0 )
  64. static char cIsLow = pdFALSE;
  65. UBaseType_t bIsValidNetworkDescriptor( const NetworkBufferDescriptor_t * pxDesc );
  66. #else
  67. static UBaseType_t bIsValidNetworkDescriptor( const NetworkBufferDescriptor_t * pxDesc );
  68. #endif /* ipconfigTCP_IP_SANITY */
  69. static void prvShowWarnings( void );
  70. /* The user can define their own ipconfigBUFFER_ALLOC_LOCK() and
  71. * ipconfigBUFFER_ALLOC_UNLOCK() macros, especially for use form an ISR. If these
  72. * are not defined then default them to call the normal enter/exit critical
  73. * section macros. */
  74. #if !defined( ipconfigBUFFER_ALLOC_LOCK )
  75. #define ipconfigBUFFER_ALLOC_INIT() do {} while( ipFALSE_BOOL )
  76. #define ipconfigBUFFER_ALLOC_LOCK_FROM_ISR() \
  77. UBaseType_t uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR(); \
  78. {
  79. #define ipconfigBUFFER_ALLOC_UNLOCK_FROM_ISR() \
  80. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus ); \
  81. }
  82. #define ipconfigBUFFER_ALLOC_LOCK() taskENTER_CRITICAL()
  83. #define ipconfigBUFFER_ALLOC_UNLOCK() taskEXIT_CRITICAL()
  84. #endif /* ipconfigBUFFER_ALLOC_LOCK */
  85. /*-----------------------------------------------------------*/
  86. #if ( ipconfigTCP_IP_SANITY != 0 )
  87. /* HT: SANITY code will be removed as soon as the library is stable
  88. * and and ready to become public
  89. * Function below gives information about the use of buffers */
  90. #define WARN_LOW ( 2 )
  91. #define WARN_HIGH ( ( 5 * ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS ) / 10 )
  92. #endif /* ipconfigTCP_IP_SANITY */
  93. /*-----------------------------------------------------------*/
  94. #if ( ipconfigTCP_IP_SANITY != 0 )
  95. BaseType_t prvIsFreeBuffer( const NetworkBufferDescriptor_t * pxDescr )
  96. {
  97. return ( bIsValidNetworkDescriptor( pxDescr ) != 0 ) &&
  98. ( listIS_CONTAINED_WITHIN( &xFreeBuffersList, &( pxDescr->xBufferListItem ) ) != 0 );
  99. }
  100. /*-----------------------------------------------------------*/
  101. static void prvShowWarnings( void )
  102. {
  103. UBaseType_t uxCount = uxGetNumberOfFreeNetworkBuffers();
  104. if( ( ( cIsLow == 0 ) && ( uxCount <= WARN_LOW ) ) || ( ( cIsLow != 0 ) && ( uxCount >= WARN_HIGH ) ) )
  105. {
  106. cIsLow = !cIsLow;
  107. FreeRTOS_debug_printf( ( "*** Warning *** %s %lu buffers left\n", cIsLow ? "only" : "now", uxCount ) );
  108. }
  109. }
  110. /*-----------------------------------------------------------*/
  111. UBaseType_t bIsValidNetworkDescriptor( const NetworkBufferDescriptor_t * pxDesc )
  112. {
  113. uint32_t offset = ( uint32_t ) ( ( ( const char * ) pxDesc ) - ( ( const char * ) xNetworkBuffers ) );
  114. if( ( offset >= sizeof( xNetworkBuffers ) ) ||
  115. ( ( offset % sizeof( xNetworkBuffers[ 0 ] ) ) != 0 ) )
  116. {
  117. return pdFALSE;
  118. }
  119. return ( UBaseType_t ) ( pxDesc - xNetworkBuffers ) + 1;
  120. }
  121. /*-----------------------------------------------------------*/
  122. #else /* if ( ipconfigTCP_IP_SANITY != 0 ) */
  123. static UBaseType_t bIsValidNetworkDescriptor( const NetworkBufferDescriptor_t * pxDesc )
  124. {
  125. ( void ) pxDesc;
  126. return ( UBaseType_t ) pdTRUE;
  127. }
  128. /*-----------------------------------------------------------*/
  129. static void prvShowWarnings( void )
  130. {
  131. }
  132. /*-----------------------------------------------------------*/
  133. #endif /* ipconfigTCP_IP_SANITY */
  134. BaseType_t xNetworkBuffersInitialise( void )
  135. {
  136. BaseType_t xReturn;
  137. uint32_t x;
  138. /* Only initialise the buffers and their associated kernel objects if they
  139. * have not been initialised before. */
  140. if( xNetworkBufferSemaphore == NULL )
  141. {
  142. /* In case alternative locking is used, the mutexes can be initialised
  143. * here */
  144. ipconfigBUFFER_ALLOC_INIT();
  145. xNetworkBufferSemaphore = xSemaphoreCreateCounting( ( UBaseType_t ) ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS, ( UBaseType_t ) ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS );
  146. configASSERT( xNetworkBufferSemaphore != NULL );
  147. if( xNetworkBufferSemaphore != NULL )
  148. {
  149. vListInitialise( &xFreeBuffersList );
  150. /* Initialise all the network buffers. The buffer storage comes
  151. * from the network interface, and different hardware has different
  152. * requirements. */
  153. vNetworkInterfaceAllocateRAMToBuffers( xNetworkBuffers );
  154. for( x = 0U; x < ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS; x++ )
  155. {
  156. /* Initialise and set the owner of the buffer list items. */
  157. vListInitialiseItem( &( xNetworkBuffers[ x ].xBufferListItem ) );
  158. listSET_LIST_ITEM_OWNER( &( xNetworkBuffers[ x ].xBufferListItem ), &xNetworkBuffers[ x ] );
  159. /* Currently, all buffers are available for use. */
  160. vListInsert( &xFreeBuffersList, &( xNetworkBuffers[ x ].xBufferListItem ) );
  161. }
  162. uxMinimumFreeNetworkBuffers = ( UBaseType_t ) ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS;
  163. }
  164. }
  165. if( xNetworkBufferSemaphore == NULL )
  166. {
  167. xReturn = pdFAIL;
  168. }
  169. else
  170. {
  171. xReturn = pdPASS;
  172. }
  173. return xReturn;
  174. }
  175. /*-----------------------------------------------------------*/
  176. NetworkBufferDescriptor_t * pxGetNetworkBufferWithDescriptor( size_t xRequestedSizeBytes,
  177. TickType_t xBlockTimeTicks )
  178. {
  179. NetworkBufferDescriptor_t * pxReturn = NULL;
  180. BaseType_t xInvalid = pdFALSE;
  181. UBaseType_t uxCount;
  182. /* The current implementation only has a single size memory block, so
  183. * the requested size parameter is not used (yet). */
  184. ( void ) xRequestedSizeBytes;
  185. if( xNetworkBufferSemaphore != NULL )
  186. {
  187. /* If there is a semaphore available, there is a network buffer
  188. * available. */
  189. if( xSemaphoreTake( xNetworkBufferSemaphore, xBlockTimeTicks ) == pdPASS )
  190. {
  191. /* Protect the structure as it is accessed from tasks and
  192. * interrupts. */
  193. ipconfigBUFFER_ALLOC_LOCK();
  194. {
  195. pxReturn = ( NetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );
  196. if( ( bIsValidNetworkDescriptor( pxReturn ) != pdFALSE_UNSIGNED ) &&
  197. listIS_CONTAINED_WITHIN( &xFreeBuffersList, &( pxReturn->xBufferListItem ) ) )
  198. {
  199. ( void ) uxListRemove( &( pxReturn->xBufferListItem ) );
  200. }
  201. else
  202. {
  203. xInvalid = pdTRUE;
  204. }
  205. }
  206. ipconfigBUFFER_ALLOC_UNLOCK();
  207. if( xInvalid == pdTRUE )
  208. {
  209. /* _RB_ Can printf() be called from an interrupt? (comment
  210. * above says this can be called from an interrupt too) */
  211. /* _HT_ The function shall not be called from an ISR. Comment
  212. * was indeed misleading. Hopefully clear now?
  213. * So the printf()is OK here. */
  214. FreeRTOS_debug_printf( ( "pxGetNetworkBufferWithDescriptor: INVALID BUFFER: %p (valid %lu)\n",
  215. pxReturn, bIsValidNetworkDescriptor( pxReturn ) ) );
  216. pxReturn = NULL;
  217. }
  218. else
  219. {
  220. /* Reading UBaseType_t, no critical section needed. */
  221. uxCount = listCURRENT_LIST_LENGTH( &xFreeBuffersList );
  222. /* For stats, latch the lowest number of network buffers since
  223. * booting. */
  224. if( uxMinimumFreeNetworkBuffers > uxCount )
  225. {
  226. uxMinimumFreeNetworkBuffers = uxCount;
  227. }
  228. pxReturn->xDataLength = xRequestedSizeBytes;
  229. #if ( ipconfigTCP_IP_SANITY != 0 )
  230. {
  231. prvShowWarnings();
  232. }
  233. #endif /* ipconfigTCP_IP_SANITY */
  234. #if ( ipconfigUSE_LINKED_RX_MESSAGES != 0 )
  235. {
  236. /* make sure the buffer is not linked */
  237. pxReturn->pxNextBuffer = NULL;
  238. }
  239. #endif /* ipconfigUSE_LINKED_RX_MESSAGES */
  240. }
  241. iptraceNETWORK_BUFFER_OBTAINED( pxReturn );
  242. }
  243. else
  244. {
  245. /* lint wants to see at least a comment. */
  246. iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER();
  247. }
  248. }
  249. return pxReturn;
  250. }
  251. /*-----------------------------------------------------------*/
  252. NetworkBufferDescriptor_t * pxNetworkBufferGetFromISR( size_t xRequestedSizeBytes )
  253. {
  254. NetworkBufferDescriptor_t * pxReturn = NULL;
  255. /* The current implementation only has a single size memory block, so
  256. * the requested size parameter is not used (yet). */
  257. ( void ) xRequestedSizeBytes;
  258. /* If there is a semaphore available then there is a buffer available, but,
  259. * as this is called from an interrupt, only take a buffer if there are at
  260. * least baINTERRUPT_BUFFER_GET_THRESHOLD buffers remaining. This prevents,
  261. * to a certain degree at least, a rapidly executing interrupt exhausting
  262. * buffer and in so doing preventing tasks from continuing. */
  263. if( uxQueueMessagesWaitingFromISR( ( QueueHandle_t ) xNetworkBufferSemaphore ) > ( UBaseType_t ) baINTERRUPT_BUFFER_GET_THRESHOLD )
  264. {
  265. if( xSemaphoreTakeFromISR( xNetworkBufferSemaphore, NULL ) == pdPASS )
  266. {
  267. /* Protect the structure as it is accessed from tasks and interrupts. */
  268. ipconfigBUFFER_ALLOC_LOCK_FROM_ISR();
  269. {
  270. pxReturn = ( NetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );
  271. uxListRemove( &( pxReturn->xBufferListItem ) );
  272. }
  273. ipconfigBUFFER_ALLOC_UNLOCK_FROM_ISR();
  274. iptraceNETWORK_BUFFER_OBTAINED_FROM_ISR( pxReturn );
  275. }
  276. }
  277. if( pxReturn == NULL )
  278. {
  279. iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER_FROM_ISR();
  280. }
  281. return pxReturn;
  282. }
  283. /*-----------------------------------------------------------*/
  284. BaseType_t vNetworkBufferReleaseFromISR( NetworkBufferDescriptor_t * const pxNetworkBuffer )
  285. {
  286. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  287. /* Ensure the buffer is returned to the list of free buffers before the
  288. * counting semaphore is 'given' to say a buffer is available. */
  289. ipconfigBUFFER_ALLOC_LOCK_FROM_ISR();
  290. {
  291. vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
  292. }
  293. ipconfigBUFFER_ALLOC_UNLOCK_FROM_ISR();
  294. ( void ) xSemaphoreGiveFromISR( xNetworkBufferSemaphore, &xHigherPriorityTaskWoken );
  295. iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );
  296. return xHigherPriorityTaskWoken;
  297. }
  298. /*-----------------------------------------------------------*/
  299. void vReleaseNetworkBufferAndDescriptor( NetworkBufferDescriptor_t * const pxNetworkBuffer )
  300. {
  301. BaseType_t xListItemAlreadyInFreeList;
  302. if( bIsValidNetworkDescriptor( pxNetworkBuffer ) == pdFALSE_UNSIGNED )
  303. {
  304. FreeRTOS_debug_printf( ( "vReleaseNetworkBufferAndDescriptor: Invalid buffer %p\n", pxNetworkBuffer ) );
  305. }
  306. else
  307. {
  308. /* Ensure the buffer is returned to the list of free buffers before the
  309. * counting semaphore is 'given' to say a buffer is available. */
  310. ipconfigBUFFER_ALLOC_LOCK();
  311. {
  312. {
  313. xListItemAlreadyInFreeList = listIS_CONTAINED_WITHIN( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
  314. if( xListItemAlreadyInFreeList == pdFALSE )
  315. {
  316. vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
  317. }
  318. }
  319. }
  320. ipconfigBUFFER_ALLOC_UNLOCK();
  321. if( xListItemAlreadyInFreeList )
  322. {
  323. FreeRTOS_debug_printf( ( "vReleaseNetworkBufferAndDescriptor: %p ALREADY RELEASED (now %lu)\n",
  324. pxNetworkBuffer, uxGetNumberOfFreeNetworkBuffers() ) );
  325. }
  326. else
  327. {
  328. ( void ) xSemaphoreGive( xNetworkBufferSemaphore );
  329. prvShowWarnings();
  330. }
  331. iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );
  332. }
  333. }
  334. /*-----------------------------------------------------------*/
  335. UBaseType_t uxGetMinimumFreeNetworkBuffers( void )
  336. {
  337. return uxMinimumFreeNetworkBuffers;
  338. }
  339. /*-----------------------------------------------------------*/
  340. UBaseType_t uxGetNumberOfFreeNetworkBuffers( void )
  341. {
  342. return listCURRENT_LIST_LENGTH( &xFreeBuffersList );
  343. }
  344. NetworkBufferDescriptor_t * pxResizeNetworkBufferWithDescriptor( NetworkBufferDescriptor_t * pxNetworkBuffer,
  345. size_t xNewSizeBytes )
  346. {
  347. /* In BufferAllocation_1.c all network buffer are allocated with a
  348. * maximum size of 'ipTOTAL_ETHERNET_FRAME_SIZE'.No need to resize the
  349. * network buffer. */
  350. pxNetworkBuffer->xDataLength = xNewSizeBytes;
  351. return pxNetworkBuffer;
  352. }
  353. /*#endif */ /* ipconfigINCLUDE_TEST_CODE */