diff --git a/source/FreeRTOS_TCP_Transmission.c b/source/FreeRTOS_TCP_Transmission.c index a061e2b005..512d72b02e 100644 --- a/source/FreeRTOS_TCP_Transmission.c +++ b/source/FreeRTOS_TCP_Transmission.c @@ -1004,10 +1004,30 @@ pxProtocolHeaders->xTCPHeader.ucTCPFlags |= ( uint8_t ) tcpTCP_FLAG_ACK; - if( lDataLen != 0L ) + #if ( ipconfigTCP_PSH_ON_LAST_SEGMENT_ONLY == ipconfigENABLE ) { - pxProtocolHeaders->xTCPHeader.ucTCPFlags |= ( uint8_t ) tcpTCP_FLAG_PSH; + /* Peers like Linux send an ACK at once for every segment that carries PSH. + * Like Linux, also push after half the peer's window, for receivers that wait for PSH. */ + if( lDataLen != 0L ) + { + pxSocket->u.xTCP.ulUnpushedBytes += ( uint32_t ) lDataLen; + + if( ( uxStreamBufferDistance( pxSocket->u.xTCP.txStream, ( size_t ) lStreamPos, pxSocket->u.xTCP.txStream->uxHead ) == ( size_t ) lDataLen ) || + ( pxSocket->u.xTCP.ulUnpushedBytes > ( pxSocket->u.xTCP.ulWindowSize / 2U ) ) ) + { + pxProtocolHeaders->xTCPHeader.ucTCPFlags |= ( uint8_t ) tcpTCP_FLAG_PSH; + pxSocket->u.xTCP.ulUnpushedBytes = 0U; + } + } + } + #else + { + if( lDataLen != 0L ) + { + pxProtocolHeaders->xTCPHeader.ucTCPFlags |= ( uint8_t ) tcpTCP_FLAG_PSH; + } } + #endif /* if ( ipconfigTCP_PSH_ON_LAST_SEGMENT_ONLY == ipconfigENABLE ) */ uxIntermediateResult = uxIPHeaderSizeSocket( pxSocket ) + ipSIZE_OF_TCP_HEADER + uxOptionsLength; lDataLen += ( int32_t ) uxIntermediateResult; diff --git a/source/FreeRTOS_TCP_Transmission_IPv4.c b/source/FreeRTOS_TCP_Transmission_IPv4.c index b29fdb3922..9a0620c518 100644 --- a/source/FreeRTOS_TCP_Transmission_IPv4.c +++ b/source/FreeRTOS_TCP_Transmission_IPv4.c @@ -233,12 +233,38 @@ void prvTCPReturnPacket_IPV4( FreeRTOS_Socket_t * pxSocket, pvCopySource = &pxEthernetHeader->xSourceAddress; ulDestinationIPAddress = pxIPHeader->ulDestinationIPAddress; - eResult = eARPGetCacheEntry( &ulDestinationIPAddress, &xMACAddress, &pxEndPoint ); + #if ( ipconfigTCP_SKIP_ARP_LOOKUP_MS > 0U ) + { + /* A socket's header template holds the peer MAC, which is trusted while + * the socket was active recently (RFC 1122 2.3.2.1). */ + if( ( pxSocket != NULL ) && ( ( xTaskGetTickCount() - pxSocket->u.xTCP.xLastActTime ) < pdMS_TO_TICKS( ipconfigTCP_SKIP_ARP_LOOKUP_MS ) ) ) + { + eResult = eResolutionCacheMiss; + } + else + { + eResult = eARPGetCacheEntry( &ulDestinationIPAddress, &xMACAddress, &pxEndPoint ); + } + } + #else /* if ( ipconfigTCP_SKIP_ARP_LOOKUP_MS > 0U ) */ + { + eResult = eARPGetCacheEntry( &ulDestinationIPAddress, &xMACAddress, &pxEndPoint ); + } + #endif /* if ( ipconfigTCP_SKIP_ARP_LOOKUP_MS > 0U ) */ if( eResult == eResolutionCacheHit ) { pvCopySource = &xMACAddress; pxNetworkBuffer->pxEndPoint = pxEndPoint; + + #if ( ipconfigTCP_SKIP_ARP_LOOKUP_MS > 0U ) + { + if( pxSocket != NULL ) + { + ( void ) memcpy( ( ( TCPPacket_t * ) pxSocket->u.xTCP.xPacket.u.ucLastPacket )->xEthernetHeader.xSourceAddress.ucBytes, xMACAddress.ucBytes, ipMAC_ADDRESS_LENGTH_BYTES ); + } + } + #endif } else { diff --git a/source/include/FreeRTOSIPConfigDefaults.h b/source/include/FreeRTOSIPConfigDefaults.h index a77957042c..ba9d6afd0b 100644 --- a/source/include/FreeRTOSIPConfigDefaults.h +++ b/source/include/FreeRTOSIPConfigDefaults.h @@ -1446,6 +1446,33 @@ STATIC_ASSERT( ipconfigTCP_KEEP_ALIVE_INTERVAL <= ( portMAX_DELAY / configTICK_R /*---------------------------------------------------------------------------*/ +/* + * ipconfigTCP_PSH_ON_LAST_SEGMENT_ONLY + * + * Type: BaseType_t ( ipconfigENABLE | ipconfigDISABLE ) + * + * When enabled, the stack sets the PSH flag only on the TCP segment that + * carries the last byte queued for sending, and on the segment that takes the + * data sent since the last PSH over half the peer's window, as Linux does. The + * second rule keeps a receiver that waits for PSH delivering data while the + * send queue never empties. When disabled, it sets PSH on every segment that + * carries data. + * + * Peers such as Linux send an ACK at once for every segment that carries PSH. + * With this enabled, a peer acknowledges a run of segments with one ACK, which + * leaves the IP task more time for sending. + */ + +#ifndef ipconfigTCP_PSH_ON_LAST_SEGMENT_ONLY + #define ipconfigTCP_PSH_ON_LAST_SEGMENT_ONLY ipconfigDISABLE +#endif + +#if ( ( ipconfigTCP_PSH_ON_LAST_SEGMENT_ONLY != ipconfigDISABLE ) && ( ipconfigTCP_PSH_ON_LAST_SEGMENT_ONLY != ipconfigENABLE ) ) + #error Invalid ipconfigTCP_PSH_ON_LAST_SEGMENT_ONLY configuration +#endif + +/*---------------------------------------------------------------------------*/ + /* * ipconfigTCP_MSS * @@ -1484,6 +1511,38 @@ STATIC_ASSERT( ipconfigTCP_KEEP_ALIVE_INTERVAL <= ( portMAX_DELAY / configTICK_R /*---------------------------------------------------------------------------*/ +/* + * ipconfigTCP_SKIP_ARP_LOOKUP_MS + * + * Type: uint32_t + * Unit: milliseconds + * + * A connected TCP socket sends its segments to the peer MAC address in its + * header template, without searching the ARP cache, while the socket received + * a segment or changed state within this time. After that, each sent segment + * searches the ARP cache again, and a hit replaces the MAC address in the + * template. RFC 1122 section 2.3.2.1 allows this, since the peer's replies + * show that its address still works. + * + * Searching the ARP cache for every sent segment can limit TCP throughput on a + * slow CPU. Segments that have no socket, such as resets, always search the + * cache. + * + * The default is 0, which searches the ARP cache for every segment. A value + * above 0 needs ipconfigTCP_HANG_PROTECTION, which keeps the time of the last + * activity. + */ + +#ifndef ipconfigTCP_SKIP_ARP_LOOKUP_MS + #define ipconfigTCP_SKIP_ARP_LOOKUP_MS 0U +#endif + +#if ( ( ipconfigTCP_SKIP_ARP_LOOKUP_MS > 0U ) && ( ipconfigTCP_HANG_PROTECTION != ipconfigENABLE ) ) + #error ipconfigTCP_SKIP_ARP_LOOKUP_MS needs ipconfigTCP_HANG_PROTECTION +#endif + +/*---------------------------------------------------------------------------*/ + /* * ipconfigTCP_RX_BUFFER_LENGTH * diff --git a/source/include/FreeRTOS_IP_Private.h b/source/include/FreeRTOS_IP_Private.h index 881adfccf2..087b0353ae 100644 --- a/source/include/FreeRTOS_IP_Private.h +++ b/source/include/FreeRTOS_IP_Private.h @@ -582,6 +582,9 @@ BaseType_t xIPIsNetworkTaskReady( void ); FOnConnected_t pxHandleConnected; /**< Actually type: typedef void (* FOnConnected_t) (Socket_t xSocket, BaseType_t ulConnected ); */ #endif /* ipconfigUSE_CALLBACKS */ uint32_t ulWindowSize; /**< Current Window size advertised by peer */ + #if ( ipconfigTCP_PSH_ON_LAST_SEGMENT_ONLY == ipconfigENABLE ) + uint32_t ulUnpushedBytes; /**< Bytes sent since the last segment that carried PSH */ + #endif size_t uxRxWinSize; /**< Fixed value: size of the TCP reception window */ size_t uxTxWinSize; /**< Fixed value: size of the TCP transmit window */ diff --git a/test/cbmc/proofs/TCP/prvTCPPrepareSend/Makefile.json b/test/cbmc/proofs/TCP/prvTCPPrepareSend/Configurations.json similarity index 76% rename from test/cbmc/proofs/TCP/prvTCPPrepareSend/Makefile.json rename to test/cbmc/proofs/TCP/prvTCPPrepareSend/Configurations.json index e1dd2fdf05..8238ef1c8f 100644 --- a/test/cbmc/proofs/TCP/prvTCPPrepareSend/Makefile.json +++ b/test/cbmc/proofs/TCP/prvTCPPrepareSend/Configurations.json @@ -51,8 +51,30 @@ ], "DEF": [ - "FREERTOS_TCP_ENABLE_VERIFICATION", - "CBMC_REQUIRE_NETWORKBUFFER_ETHERNETBUFFER_NONNULL" + { + "default": + [ + "FREERTOS_TCP_ENABLE_VERIFICATION", + "CBMC_REQUIRE_NETWORKBUFFER_ETHERNETBUFFER_NONNULL" + ] + }, + { + "PSH_ON_LAST_SEGMENT_ONLY": + [ + "FREERTOS_TCP_ENABLE_VERIFICATION", + "CBMC_REQUIRE_NETWORKBUFFER_ETHERNETBUFFER_NONNULL", + "ipconfigTCP_PSH_ON_LAST_SEGMENT_ONLY=1" + ] + }, + { + "PSH_AND_SKIP_ARP_LOOKUP": + [ + "FREERTOS_TCP_ENABLE_VERIFICATION", + "CBMC_REQUIRE_NETWORKBUFFER_ETHERNETBUFFER_NONNULL", + "ipconfigTCP_PSH_ON_LAST_SEGMENT_ONLY=1", + "ipconfigTCP_SKIP_ARP_LOOKUP_MS=1000" + ] + } ], "INC": [ diff --git a/test/cbmc/proofs/TCP/prvTCPReturnPacket/Makefile.json b/test/cbmc/proofs/TCP/prvTCPReturnPacket/Configurations.json similarity index 72% rename from test/cbmc/proofs/TCP/prvTCPReturnPacket/Makefile.json rename to test/cbmc/proofs/TCP/prvTCPReturnPacket/Configurations.json index 12a17ef309..d900648f6e 100644 --- a/test/cbmc/proofs/TCP/prvTCPReturnPacket/Makefile.json +++ b/test/cbmc/proofs/TCP/prvTCPReturnPacket/Configurations.json @@ -49,9 +49,33 @@ ], "DEF": [ - "ipconfigUSE_LINKED_RX_MESSAGES={RX_MESSAGES}", - "FREERTOS_TCP_ENABLE_VERIFICATION", - "CBMC_REQUIRE_NETWORKBUFFER_ETHERNETBUFFER_NONNULL" + { + "default": + [ + "ipconfigUSE_LINKED_RX_MESSAGES={RX_MESSAGES}", + "FREERTOS_TCP_ENABLE_VERIFICATION", + "CBMC_REQUIRE_NETWORKBUFFER_ETHERNETBUFFER_NONNULL" + ] + }, + { + "SKIP_ARP_LOOKUP": + [ + "ipconfigUSE_LINKED_RX_MESSAGES={RX_MESSAGES}", + "FREERTOS_TCP_ENABLE_VERIFICATION", + "CBMC_REQUIRE_NETWORKBUFFER_ETHERNETBUFFER_NONNULL", + "ipconfigTCP_SKIP_ARP_LOOKUP_MS=1000" + ] + }, + { + "PSH_AND_SKIP_ARP_LOOKUP": + [ + "ipconfigUSE_LINKED_RX_MESSAGES={RX_MESSAGES}", + "FREERTOS_TCP_ENABLE_VERIFICATION", + "CBMC_REQUIRE_NETWORKBUFFER_ETHERNETBUFFER_NONNULL", + "ipconfigTCP_PSH_ON_LAST_SEGMENT_ONLY=1", + "ipconfigTCP_SKIP_ARP_LOOKUP_MS=1000" + ] + } ], "INC": [ diff --git a/test/unit-test/CMakeLists.txt b/test/unit-test/CMakeLists.txt index 912a62fa1e..fd7c50579f 100644 --- a/test/unit-test/CMakeLists.txt +++ b/test/unit-test/CMakeLists.txt @@ -349,6 +349,7 @@ include( ${UNIT_TEST_DIR}/FreeRTOS_TCP_State_Handling/ut.cmake ) include( ${UNIT_TEST_DIR}/FreeRTOS_TCP_State_Handling_IPv4/ut.cmake ) include( ${UNIT_TEST_DIR}/FreeRTOS_TCP_State_Handling_IPv6/ut.cmake ) include( ${UNIT_TEST_DIR}/FreeRTOS_TCP_Transmission/ut.cmake ) +include( ${UNIT_TEST_DIR}/FreeRTOS_TCP_Transmission_DiffConfig/ut.cmake ) include( ${UNIT_TEST_DIR}/FreeRTOS_TCP_Transmission_IPv6/ut.cmake ) include( ${UNIT_TEST_DIR}/FreeRTOS_TCP_Utils/ut.cmake ) include( ${UNIT_TEST_DIR}/FreeRTOS_TCP_Utils_IPv6/ut.cmake ) @@ -416,6 +417,11 @@ add_custom_target( coverage FreeRTOS_TCP_State_Handling_IPv4_utest FreeRTOS_TCP_State_Handling_IPv6_utest FreeRTOS_TCP_Transmission_utest + FreeRTOS_TCP_Transmission_DiffConfig_utest + FreeRTOS_TCP_Transmission_DiffConfig_PSHOnly_utest + FreeRTOS_TCP_Transmission_DiffConfig_ARPOnly_utest + FreeRTOS_TCP_Transmission_DiffConfig_Disabled_utest + FreeRTOS_TCP_Transmission_DiffConfig_Defaults_utest FreeRTOS_TCP_Transmission_IPv6_utest FreeRTOS_TCP_Utils_utest FreeRTOS_TCP_Utils_IPv6_utest diff --git a/test/unit-test/FreeRTOS_TCP_Transmission_DiffConfig/FreeRTOSConfig.h b/test/unit-test/FreeRTOS_TCP_Transmission_DiffConfig/FreeRTOSConfig.h new file mode 100644 index 0000000000..ae54817e65 --- /dev/null +++ b/test/unit-test/FreeRTOS_TCP_Transmission_DiffConfig/FreeRTOSConfig.h @@ -0,0 +1,124 @@ +/* + * FreeRTOS+TCP + * Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + * + */ + + +#ifndef FREERTOS_CONFIG_H +#define FREERTOS_CONFIG_H + +#include + +/*----------------------------------------------------------- +* Application specific definitions. +* +* These definitions should be adjusted for your particular hardware and +* application requirements. +* +* THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE +* FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE. See +* http://www.freertos.org/a00110.html +*----------------------------------------------------------*/ + +#define configUSE_PREEMPTION 1 +#define configUSE_PORT_OPTIMISED_TASK_SELECTION 1 +#define configUSE_IDLE_HOOK 1 +#define configUSE_TICK_HOOK 1 +#define configUSE_DAEMON_TASK_STARTUP_HOOK 1 +#define configTICK_RATE_HZ ( 1000 ) /* In this non-real time simulated environment the tick frequency has to be at least a multiple of the Win32 tick frequency, and therefore very slow. */ +#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 70 ) /* In this simulated case, the stack only has to hold one small structure as the real stack is part of the win32 thread. */ +#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 52 * 1024 ) ) +#define configMAX_TASK_NAME_LEN ( 12 ) +#define configUSE_TRACE_FACILITY 1 +#define configUSE_16_BIT_TICKS 0 +#define configIDLE_SHOULD_YIELD 1 +#define configUSE_MUTEXES 1 +#define configCHECK_FOR_STACK_OVERFLOW 0 +#define configUSE_RECURSIVE_MUTEXES 1 +#define configQUEUE_REGISTRY_SIZE 20 +#define configUSE_MALLOC_FAILED_HOOK 1 +#define configUSE_APPLICATION_TASK_TAG 1 +#define configUSE_COUNTING_SEMAPHORES 1 +#define configUSE_ALTERNATIVE_API 0 +#define configUSE_QUEUE_SETS 1 +#define configUSE_TASK_NOTIFICATIONS 1 +#define configSUPPORT_STATIC_ALLOCATION 1 +#define configINITIAL_TICK_COUNT ( ( TickType_t ) 0 ) /* For test. */ +#define configSTREAM_BUFFER_TRIGGER_LEVEL_TEST_MARGIN 1 /* As there are a lot of tasks running. */ + +/* Software timer related configuration options. */ +#define configUSE_TIMERS 1 +#define configTIMER_TASK_PRIORITY ( configMAX_PRIORITIES - 1 ) +#define configTIMER_QUEUE_LENGTH 20 +#define configTIMER_TASK_STACK_DEPTH ( configMINIMAL_STACK_SIZE * 2 ) + +#define configMAX_PRIORITIES ( 7 ) +#define configENABLE_MPU 0 + +/* Run time stats gathering configuration options. */ + +#define configGENERATE_RUN_TIME_STATS 1 + +/* This demo makes use of one or more example stats formatting functions. These + * format the raw data provided by the uxTaskGetSystemState() function in to human + * readable ASCII form. See the notes in the implementation of vTaskList() within + * FreeRTOS/Source/tasks.c for limitations. */ +#define configUSE_STATS_FORMATTING_FUNCTIONS 1 + +/* Set the following definitions to 1 to include the API function, or zero + * to exclude the API function. In most cases the linker will remove unused + * functions anyway. */ +#define INCLUDE_vTaskPrioritySet 1 +#define INCLUDE_uxTaskPriorityGet 1 +#define INCLUDE_vTaskDelete 1 +#define INCLUDE_vTaskCleanUpResources 0 +#define INCLUDE_vTaskSuspend 1 +#define INCLUDE_vTaskDelayUntil 1 +#define INCLUDE_vTaskDelay 1 +#define INCLUDE_uxTaskGetStackHighWaterMark 1 +#define INCLUDE_xTaskGetSchedulerState 1 +#define INCLUDE_xTimerGetTimerDaemonTaskHandle 1 +#define INCLUDE_xTaskGetIdleTaskHandle 1 +#define INCLUDE_xTaskGetHandle 1 +#define INCLUDE_eTaskGetState 1 +#define INCLUDE_xSemaphoreGetMutexHolder 1 +#define INCLUDE_xTimerPendFunctionCall 1 +#define INCLUDE_xTaskAbortDelay 1 + +/* It is a good idea to define configASSERT() while developing. configASSERT() + * uses the same semantics as the standard C assert() macro. */ +extern void vAssertCalled( unsigned long ulLine, + const char * const pcFileName ); +#define configASSERT( x ) assert( x ) + +#define configINCLUDE_MESSAGE_BUFFER_AMP_DEMO 0 +#if ( configINCLUDE_MESSAGE_BUFFER_AMP_DEMO == 1 ) + extern void vGenerateCoreBInterrupt( void * xUpdatedMessageBuffer ); + #define sbSEND_COMPLETED( pxStreamBuffer ) vGenerateCoreBInterrupt( pxStreamBuffer ) +#endif /* configINCLUDE_MESSAGE_BUFFER_AMP_DEMO */ + +/* Include the FreeRTOS+Trace FreeRTOS trace macro definitions. */ +/* #include "trcRecorder.h" */ + +#endif /* FREERTOS_CONFIG_H */ diff --git a/test/unit-test/FreeRTOS_TCP_Transmission_DiffConfig/FreeRTOSIPConfig.h b/test/unit-test/FreeRTOS_TCP_Transmission_DiffConfig/FreeRTOSIPConfig.h new file mode 100644 index 0000000000..2de982be9c --- /dev/null +++ b/test/unit-test/FreeRTOS_TCP_Transmission_DiffConfig/FreeRTOSIPConfig.h @@ -0,0 +1,341 @@ +/* + * FreeRTOS+TCP + * Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + + +/***************************************************************************** +* +* See the following URL for configuration information. +* http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/TCP_IP_Configuration.html +* +*****************************************************************************/ + +#ifndef FREERTOS_IP_CONFIG_H +#define FREERTOS_IP_CONFIG_H + +#define _static + +#define TEST 1 + +#define ipconfigUSE_IPv4 ( 1 ) +#define ipconfigUSE_IPv6 ( 1 ) + +#define ipconfigIPv4_BACKWARD_COMPATIBLE 0 + +#define ipconfigUSE_IPv4 ( 1 ) +#define ipconfigUSE_IPv6 ( 1 ) + +/* Set to 1 to print out debug messages. If ipconfigHAS_DEBUG_PRINTF is set to + * 1 then FreeRTOS_debug_printf should be defined to the function used to print + * out the debugging messages. */ +#define ipconfigHAS_DEBUG_PRINTF 1 +#if ( ipconfigHAS_DEBUG_PRINTF == 1 ) + #define FreeRTOS_debug_printf( X ) configPRINTF( X ) +#endif + +/* Set to 1 to print out non debugging messages, for example the output of the + * FreeRTOS_netstat() command, and ping replies. If ipconfigHAS_PRINTF is set to 1 + * then FreeRTOS_printf should be set to the function used to print out the + * messages. */ +#define ipconfigHAS_PRINTF 1 +#if ( ipconfigHAS_PRINTF == 1 ) + #define FreeRTOS_printf( X ) configPRINTF( X ) +#endif + +/* Define the byte order of the target MCU (the MCU FreeRTOS+TCP is executing + * on). Valid options are pdFREERTOS_BIG_ENDIAN and pdFREERTOS_LITTLE_ENDIAN. */ +#define ipconfigBYTE_ORDER pdFREERTOS_LITTLE_ENDIAN + +/* If the network card/driver includes checksum offloading then set + * ipconfigDRIVER_INCLUDED_RX_IP_CHECKSUM to 1 to prevent the software + * stack repeating the checksum calculations. */ +#define ipconfigDRIVER_INCLUDED_RX_IP_CHECKSUM 0 + +/* Several API's will block until the result is known, or the action has been + * performed, for example FreeRTOS_send() and FreeRTOS_recv(). The timeouts can be + * set per socket, using setsockopt(). If not set, the times below will be + * used as defaults. */ +#define ipconfigSOCK_DEFAULT_RECEIVE_BLOCK_TIME ( 5000 ) +#define ipconfigSOCK_DEFAULT_SEND_BLOCK_TIME ( 5000 ) + +/* Include support for DNS caching. For TCP, having a small DNS cache is very + * useful. When a cache is present, ipconfigDNS_REQUEST_ATTEMPTS can be kept low + * and also DNS may use small timeouts. If a DNS reply comes in after the DNS + * socket has been destroyed, the result will be stored into the cache. The next + * call to FreeRTOS_gethostbyname() will return immediately, without even creating + * a socket. + */ +#define ipconfigUSE_DNS_CACHE ( 1 ) +#define ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY ( 1 ) +#define ipconfigDNS_REQUEST_ATTEMPTS ( 2 ) + +#define ipconfigDNS_CACHE_NAME_LENGTH ( 254 ) + +/* The IP stack executes it its own task (although any application task can make + * use of its services through the published sockets API). ipconfigUDP_TASK_PRIORITY + * sets the priority of the task that executes the IP stack. The priority is a + * standard FreeRTOS task priority so can take any value from 0 (the lowest + * priority) to (configMAX_PRIORITIES - 1) (the highest priority). + * configMAX_PRIORITIES is a standard FreeRTOS configuration parameter defined in + * FreeRTOSConfig.h, not FreeRTOSIPConfig.h. Consideration needs to be given as to + * the priority assigned to the task executing the IP stack relative to the + * priority assigned to tasks that use the IP stack. */ +#define ipconfigIP_TASK_PRIORITY ( configMAX_PRIORITIES - 2 ) + +/* The size, in words (not bytes), of the stack allocated to the FreeRTOS+TCP + * task. This setting is less important when the FreeRTOS Win32 simulator is used + * as the Win32 simulator only stores a fixed amount of information on the task + * stack. FreeRTOS includes optional stack overflow detection, see: + * http://www.freertos.org/Stacks-and-stack-overflow-checking.html. */ +#define ipconfigIP_TASK_STACK_SIZE_WORDS ( configMINIMAL_STACK_SIZE * 5 ) + +/* If ipconfigUSE_NETWORK_EVENT_HOOK is set to 1 then FreeRTOS+TCP will call the + * network event hook at the appropriate times. If ipconfigUSE_NETWORK_EVENT_HOOK + * is not set to 1 then the network event hook will never be called. See: + * https://freertos.org/Documentation/03-Libraries/02-FreeRTOS-plus/02-FreeRTOS-plus-TCP/09-API-reference/57-vApplicationIPNetworkEventHook. + */ +#define ipconfigUSE_NETWORK_EVENT_HOOK 1 + +/* Sockets have a send block time attribute. If FreeRTOS_sendto() is called but + * a network buffer cannot be obtained then the calling task is held in the Blocked + * state (so other tasks can continue to executed) until either a network buffer + * becomes available or the send block time expires. If the send block time expires + * then the send operation is aborted. The maximum allowable send block time is + * capped to the value set by ipconfigMAX_SEND_BLOCK_TIME_TICKS. Capping the + * maximum allowable send block time prevents prevents a deadlock occurring when + * all the network buffers are in use and the tasks that process (and subsequently + * free) the network buffers are themselves blocked waiting for a network buffer. + * ipconfigMAX_SEND_BLOCK_TIME_TICKS is specified in RTOS ticks. A time in + * milliseconds can be converted to a time in ticks using pdMS_TO_TICKS().*/ +#define ipconfigUDP_MAX_SEND_BLOCK_TIME_TICKS pdMS_TO_TICKS( 5000U ) + +/* If ipconfigUSE_DHCP is 1 then FreeRTOS+TCP will attempt to retrieve an IP + * address, netmask, DNS server address and gateway address from a DHCP server. If + * ipconfigUSE_DHCP is 0 then FreeRTOS+TCP will use a static IP address. The + * stack will revert to using the static IP address even when ipconfigUSE_DHCP is + * set to 1 if a valid configuration cannot be obtained from a DHCP server for any + * reason. The static configuration used is that passed into the stack by the + * FreeRTOS_IPInit() function call. */ +#define ipconfigUSE_DHCP 1 +#define ipconfigDHCP_REGISTER_HOSTNAME 1 +#define ipconfigDHCP_USES_UNICAST 1 + +#define ipconfigENDPOINT_DNS_ADDRESS_COUNT 5 + +/* If ipconfigDHCP_USES_USER_HOOK is set to 1 then the application writer must + * provide an implementation of the DHCP callback function, + * xApplicationDHCPUserHook(). */ +#define ipconfigUSE_DHCP_HOOK 1 + +/* When ipconfigUSE_DHCP is set to 1, DHCP requests will be sent out at + * increasing time intervals until either a reply is received from a DHCP server + * and accepted, or the interval between transmissions reaches + * ipconfigMAXIMUM_DISCOVER_TX_PERIOD. The IP stack will revert to using the + * static IP address passed as a parameter to FreeRTOS_IPInit() if the + * re-transmission time interval reaches ipconfigMAXIMUM_DISCOVER_TX_PERIOD without + * a DHCP reply being received. */ +#define ipconfigMAXIMUM_DISCOVER_TX_PERIOD pdMS_TO_TICKS( 120000U ) + +/* The ARP cache is a table that maps IP addresses to MAC addresses. The IP + * stack can only send a UDP message to a remove IP address if it knowns the MAC + * address associated with the IP address, or the MAC address of the router used to + * contact the remote IP address. When a UDP message is received from a remote IP + * address the MAC address and IP address are added to the ARP cache. When a UDP + * message is sent to a remote IP address that does not already appear in the ARP + * cache then the UDP message is replaced by a ARP message that solicits the + * required MAC address information. ipconfigARP_CACHE_ENTRIES defines the maximum + * number of entries that can exist in the ARP table at any one time. */ +#define ipconfigARP_CACHE_ENTRIES 6 + +/* ARP requests that do not result in an ARP response will be re-transmitted a + * maximum of ipconfigMAX_ARP_RETRANSMISSIONS times before the ARP request is + * aborted. */ +#define ipconfigMAX_ARP_RETRANSMISSIONS ( 5 ) + +/* ipconfigMAX_ARP_AGE defines the maximum time between an entry in the ARP + * table being created or refreshed and the entry being removed because it is stale. + * New ARP requests are sent for ARP cache entries that are nearing their maximum + * age. ipconfigMAX_ARP_AGE is specified in tens of seconds, so a value of 150 is + * equal to 1500 seconds (or 25 minutes). */ +#define ipconfigMAX_ARP_AGE 150 + +/* Implementing FreeRTOS_inet_addr() necessitates the use of string handling + * routines, which are relatively large. To save code space the full + * FreeRTOS_inet_addr() implementation is made optional, and a smaller and faster + * alternative called FreeRTOS_inet_addr_quick() is provided. FreeRTOS_inet_addr() + * takes an IP in decimal dot format (for example, "192.168.0.1") as its parameter. + * FreeRTOS_inet_addr_quick() takes an IP address as four separate numerical octets + * (for example, 192, 168, 0, 1) as its parameters. If + * ipconfigINCLUDE_FULL_INET_ADDR is set to 1 then both FreeRTOS_inet_addr() and + * FreeRTOS_indet_addr_quick() are available. If ipconfigINCLUDE_FULL_INET_ADDR is + * not set to 1 then only FreeRTOS_indet_addr_quick() is available. */ +#define ipconfigINCLUDE_FULL_INET_ADDR 1 + +/* ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS defines the total number of network buffer that + * are available to the IP stack. The total number of network buffers is limited + * to ensure the total amount of RAM that can be consumed by the IP stack is capped + * to a pre-determinable value. */ +#define ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS 60 + +/* A FreeRTOS queue is used to send events from application tasks to the IP + * stack. ipconfigEVENT_QUEUE_LENGTH sets the maximum number of events that can + * be queued for processing at any one time. The event queue must be a minimum of + * 5 greater than the total number of network buffers. */ +#define ipconfigEVENT_QUEUE_LENGTH \ + ( ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS + 5 ) + +/* The address of a socket is the combination of its IP address and its port + * number. FreeRTOS_bind() is used to manually allocate a port number to a socket + * (to 'bind' the socket to a port), but manual binding is not normally necessary + * for client sockets (those sockets that initiate outgoing connections rather than + * wait for incoming connections on a known port number). If + * ipconfigALLOW_SOCKET_SEND_WITHOUT_BIND is set to 1 then calling + * FreeRTOS_sendto() on a socket that has not yet been bound will result in the IP + * stack automatically binding the socket to a port number from the range + * socketAUTO_PORT_ALLOCATION_START_NUMBER to 0xffff. If + * ipconfigALLOW_SOCKET_SEND_WITHOUT_BIND is set to 0 then calling FreeRTOS_sendto() + * on a socket that has not yet been bound will result in the send operation being + * aborted. */ +#define ipconfigALLOW_SOCKET_SEND_WITHOUT_BIND 1 + +/* Defines the Time To Live (TTL) values used in outgoing UDP packets. */ +#define ipconfigUDP_TIME_TO_LIVE 128 +/* Also defined in FreeRTOSIPConfigDefaults.h. */ +#define ipconfigTCP_TIME_TO_LIVE 128 + +/* USE_TCP: Use TCP and all its features. */ +#define ipconfigUSE_TCP ( 1 ) + +/* USE_WIN: Let TCP use windowing mechanism. */ +#define ipconfigUSE_TCP_WIN ( 1 ) + +/* The MTU is the maximum number of bytes the payload of a network frame can + * contain. For normal Ethernet V2 frames the maximum MTU is 1500. Setting a + * lower value can save RAM, depending on the buffer management scheme used. If + * ipconfigCAN_FRAGMENT_OUTGOING_PACKETS is 1 then (ipconfigNETWORK_MTU - 28) must + * be divisible by 8. */ +#define ipconfigNETWORK_MTU 1500U + +/* Set ipconfigUSE_DNS to 1 to include a basic DNS client/resolver. DNS is used + * through the FreeRTOS_gethostbyname() API function. */ +#define ipconfigUSE_DNS 1 + +/* If ipconfigREPLY_TO_INCOMING_PINGS is set to 1 then the IP stack will + * generate replies to incoming ICMP echo (ping) requests. */ +#define ipconfigREPLY_TO_INCOMING_PINGS 1 + +/* If ipconfigSUPPORT_OUTGOING_PINGS is set to 1 then the + * FreeRTOS_SendPingRequest() API function is available. */ +#define ipconfigSUPPORT_OUTGOING_PINGS 1 + +/* If ipconfigSUPPORT_SELECT_FUNCTION is set to 1 then the FreeRTOS_select() + * (and associated) API function is available. */ +#define ipconfigSUPPORT_SELECT_FUNCTION 1 + +/* If ipconfigFILTER_OUT_NON_ETHERNET_II_FRAMES is set to 1 then Ethernet frames + * that are not in Ethernet II format will be dropped. This option is included for + * potential future IP stack developments. */ +#define ipconfigFILTER_OUT_NON_ETHERNET_II_FRAMES 1 + +/* If ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES is set to 1 then it is the + * responsibility of the Ethernet interface to filter out packets that are of no + * interest. If the Ethernet interface does not implement this functionality, then + * set ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES to 0 to have the IP stack + * perform the filtering instead (it is much less efficient for the stack to do it + * because the packet will already have been passed into the stack). If the + * Ethernet driver does all the necessary filtering in hardware then software + * filtering can be removed by using a value other than 1 or 0. */ +#define ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES 1 + +/* The windows simulator cannot really simulate MAC interrupts, and needs to + * block occasionally to allow other tasks to run. */ +#define configWINDOWS_MAC_INTERRUPT_SIMULATOR_DELAY pdMS_TO_TICKS( 20 ) + +/* Advanced only: in order to access 32-bit fields in the IP packets with + * 32-bit memory instructions, all packets will be stored 32-bit-aligned, + * plus 16-bits. This has to do with the contents of the IP-packets: all + * 32-bit fields are 32-bit-aligned, plus 16-bit. */ +#define ipconfigPACKET_FILLER_SIZE 2U + +/* Define the size of the pool of TCP window descriptors. On the average, each + * TCP socket will use up to 2 x 6 descriptors, meaning that it can have 2 x 6 + * outstanding packets (for Rx and Tx). When using up to 10 TP sockets + * simultaneously, one could define TCP_WIN_SEG_COUNT as 120. */ +#define ipconfigTCP_WIN_SEG_COUNT 2 + +/* Each TCP socket has a circular buffers for Rx and Tx, which have a fixed + * maximum size. Define the size of Rx buffer for TCP sockets. */ +#define ipconfigTCP_RX_BUFFER_LENGTH ( 10000 ) + +/* Define the size of Tx buffer for TCP sockets. */ +#define ipconfigTCP_TX_BUFFER_LENGTH ( 10000 ) + +/* When using call-back handlers, the driver may check if the handler points to + * real program memory (RAM or flash) or just has a random non-zero value. */ +#define ipconfigIS_VALID_PROG_ADDRESS( x ) ( ( x ) != NULL ) + +/* Include support for TCP keep-alive messages. */ +#define ipconfigTCP_KEEP_ALIVE ( 1 ) +#define ipconfigTCP_KEEP_ALIVE_INTERVAL ( 20 ) /* Seconds. */ + +/* The socket semaphore is used to unblock the MQTT task. */ +#define ipconfigSOCKET_HAS_USER_SEMAPHORE ( 1 ) + +#define ipconfigSOCKET_HAS_USER_WAKE_CALLBACK ( 1 ) +#define ipconfigUSE_CALLBACKS ( 1 ) + +#define ipconfigUSE_NBNS ( 1 ) + +#define ipconfigUSE_LLMNR ( 1 ) +#define ipconfigUSE_MDNS ( 1 ) + +#define ipconfigDNS_USE_CALLBACKS 1 +#define ipconfigUSE_ARP_REMOVE_ENTRY 1 +#define ipconfigUSE_ARP_REVERSED_LOOKUP 1 + +#define ipconfigETHERNET_MINIMUM_PACKET_BYTES ( 200 ) + +#define ipconfigARP_STORES_REMOTE_ADDRESSES ( 1 ) + +#define ipconfigARP_USE_CLASH_DETECTION ( 1 ) + +#define ipconfigDHCP_FALL_BACK_AUTO_IP ( 1 ) + +#define ipconfigUDP_MAX_RX_PACKETS ( 1 ) + +#define ipconfigSUPPORT_SIGNALS ( 1 ) + +#define ipconfigDNS_CACHE_ENTRIES ( 2 ) + +#define ipconfigBUFFER_PADDING ( 14 ) +#define ipconfigTCP_SRTT_MINIMUM_VALUE_MS ( 34 ) + +#ifndef ipconfigTCP_HANG_PROTECTION + #define ipconfigTCP_HANG_PROTECTION ( 1 ) +#endif + +#define portINLINE + +#define ipconfigTCP_MAY_LOG_PORT( xPort ) ( ( xPort ) != 23U ) + +/* Each CMake variant supplies these independently. The Defaults variant leaves + * both undefined so the production defaults are exercised as well. */ + +#endif /* FREERTOS_IP_CONFIG_H */ diff --git a/test/unit-test/FreeRTOS_TCP_Transmission_DiffConfig/FreeRTOS_TCP_Transmission_DiffConfig_stubs.c b/test/unit-test/FreeRTOS_TCP_Transmission_DiffConfig/FreeRTOS_TCP_Transmission_DiffConfig_stubs.c new file mode 100644 index 0000000000..d447ff5a90 --- /dev/null +++ b/test/unit-test/FreeRTOS_TCP_Transmission_DiffConfig/FreeRTOS_TCP_Transmission_DiffConfig_stubs.c @@ -0,0 +1,90 @@ +/* + * FreeRTOS+TCP + * Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + + +/* Include Unity header */ +#include + +/* Include standard libraries */ +#include +#include +#include +#include "FreeRTOS.h" +#include "task.h" +#include "list.h" + +#include "FreeRTOS_IP.h" +#include "FreeRTOS_IP_Private.h" + +/* ========================= EXTERN VARIABLES ========================= */ + +/** @brief The expected IP version and header length coded into the IP header itself. */ +uint16_t usPacketIdentifier; +BaseType_t xTCPWindowLoggingLevel; +BaseType_t xBufferAllocFixedSize = pdFALSE; + +BaseType_t NetworkInterfaceOutputFunction_Stub_Called = 0; + +/* ======================== Stub Callback Functions ========================= */ + +BaseType_t NetworkInterfaceOutputFunction_Stub( struct xNetworkInterface * pxDescriptor, + NetworkBufferDescriptor_t * const pxNetworkBuffer, + BaseType_t xReleaseAfterSend ) +{ + NetworkInterfaceOutputFunction_Stub_Called++; + return 0; +} + +/* + * Return or send a packet to the other party. + */ +void prvTCPReturnPacket_IPV6( FreeRTOS_Socket_t * pxSocket, + NetworkBufferDescriptor_t * pxDescriptor, + uint32_t ulLen, + BaseType_t xReleaseAfterSend ) +{ + /* Do Nothing */ +} + +/* + * Let ARP look-up the MAC-address of the peer and initialise the first SYN + * packet. + */ +BaseType_t prvTCPPrepareConnect_IPV6( FreeRTOS_Socket_t * pxSocket ) +{ + return pdTRUE; +} + +/* + * Common code for sending a TCP protocol control packet (i.e. no options, no + * payload, just flags). + */ +BaseType_t prvTCPSendSpecialPktHelper_IPV6( NetworkBufferDescriptor_t * pxNetworkBuffer, + uint8_t ucTCPFlags ) +{ + return pdTRUE; +} diff --git a/test/unit-test/FreeRTOS_TCP_Transmission_DiffConfig/FreeRTOS_TCP_Transmission_DiffConfig_utest.c b/test/unit-test/FreeRTOS_TCP_Transmission_DiffConfig/FreeRTOS_TCP_Transmission_DiffConfig_utest.c new file mode 100644 index 0000000000..8b90a71392 --- /dev/null +++ b/test/unit-test/FreeRTOS_TCP_Transmission_DiffConfig/FreeRTOS_TCP_Transmission_DiffConfig_utest.c @@ -0,0 +1,508 @@ +/* + * FreeRTOS+TCP + * Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + + + +/* Include Unity header */ +#include "unity.h" + +/* Include standard libraries */ +#include +#include +#include + +#include "mock_list.h" + +#include "mock_queue.h" +#include "mock_event_groups.h" +#include "mock_task.h" + +#include "mock_FreeRTOS_IP.h" +#include "mock_FreeRTOS_IP_Private.h" +#include "mock_FreeRTOS_IP_Utils.h" +#include "mock_FreeRTOS_IP_Timers.h" +#include "mock_NetworkBufferManagement.h" +#include "mock_NetworkInterface.h" +#include "mock_FreeRTOS_Sockets.h" +#include "mock_FreeRTOS_Stream_Buffer.h" +#include "mock_FreeRTOS_TCP_WIN.h" +#include "mock_FreeRTOS_UDP_IP.h" +#include "mock_FreeRTOS_ARP.h" +#include "mock_FreeRTOS_TCP_State_Handling.h" +#include "mock_FreeRTOS_TCP_Reception.h" +#include "mock_FreeRTOS_TCP_Utils.h" +#include "mock_TCP_Transmission_DiffConfig_list_macros.h" + +#include "FreeRTOS_TCP_IP.h" + +#include "catch_assert.h" + +#include "FreeRTOSIPConfig.h" +/* The Annexed_TCP copy removes conditional definitions. Use the production + * defaults so assertions see the same option values as the code under test. */ +#include "../../../source/include/FreeRTOSIPConfigDefaults.h" + +#include "FreeRTOS_TCP_Transmission_DiffConfig_stubs.c" +#include "FreeRTOS_TCP_Transmission.h" + +/* ============================ Test Variables ============================ */ + +static FreeRTOS_Socket_t xSocket; +static NetworkBufferDescriptor_t xNetworkBuffer; +static uint8_t ucEthernetBuffer[ ipconfigNETWORK_MTU ]; +static StreamBuffer_t xStreamBuffer; +static struct xNetworkEndPoint xEndPoint; +static struct xNetworkInterface xInterface; + +static const MACAddress_t xTemplateMAC = { { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC } }; +static const MACAddress_t xCacheMAC = { { 0x21, 0x43, 0x65, 0x87, 0xA9, 0xCB } }; + + +/* ============================ Test Helpers ============================ */ + +/* Prepares an established socket whose peer advertises the given window. */ +static void prvSetUpSocket( uint32_t ulPeerWindowSize ) +{ + memset( &xStreamBuffer, 0, sizeof( xStreamBuffer ) ); + memset( &xSocket, 0, sizeof( xSocket ) ); + xBufferAllocFixedSize = pdFALSE; + + xSocket.u.xTCP.txStream = &xStreamBuffer; + xSocket.u.xTCP.eTCPState = eESTABLISHED; + xSocket.u.xTCP.usMSS = 1000; + xSocket.u.xTCP.bits.bSendKeepAlive = pdTRUE; + xSocket.u.xTCP.ucKeepRepCount = 1; + xStreamBuffer.LENGTH = 2048; + xSocket.u.xTCP.ulWindowSize = ulPeerWindowSize; +} + +/* Returns the TCP flags of the next segment that prvTCPPrepareSend() builds, + * with 500 bytes ready in the TX window. The argument is the number of bytes + * queued from the segment start. */ +static uint8_t ucPrepareSendFlags( size_t uxQueuedBytes ) +{ + NetworkBufferDescriptor_t * pxNetworkBuffer = &xNetworkBuffer; + int32_t lBytesSent; + int32_t lStreamPosition = 100; + + memset( ucEthernetBuffer, 0, sizeof( ucEthernetBuffer ) ); + memset( &xNetworkBuffer, 0, sizeof( xNetworkBuffer ) ); + xNetworkBuffer.pucEthernetBuffer = ucEthernetBuffer; + xStreamBuffer.uxHead = ( size_t ) lStreamPosition + uxQueuedBytes; + + uxIPHeaderSizeSocket_IgnoreAndReturn( ipSIZE_OF_IPv4_HEADER ); + ulTCPWindowTxGet_ExpectAnyArgsAndReturn( 500 ); + ulTCPWindowTxGet_ReturnThruPtr_plPosition( &lStreamPosition ); + pxGetNetworkBufferWithDescriptor_ExpectAnyArgsAndReturn( pxNetworkBuffer ); + vReleaseNetworkBufferAndDescriptor_ExpectAnyArgs(); + uxStreamBufferDistance_ExpectAndReturn( &xStreamBuffer, 0, ( size_t ) lStreamPosition, 100 ); + uxStreamBufferGet_ExpectAndReturn( &xStreamBuffer, 100, + &ucEthernetBuffer[ ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_TCP_HEADER ], + 500, pdTRUE, 500 ); + #if ( ipconfigTCP_PSH_ON_LAST_SEGMENT_ONLY == ipconfigENABLE ) + uxStreamBufferDistance_ExpectAndReturn( &xStreamBuffer, ( size_t ) lStreamPosition, xStreamBuffer.uxHead, uxQueuedBytes ); + #endif + + lBytesSent = prvTCPPrepareSend( &xSocket, &pxNetworkBuffer, 0 ); + TEST_ASSERT_EQUAL( 540, lBytesSent ); + + return ( ( TCPPacket_t * ) ucEthernetBuffer )->xTCPHeader.ucTCPFlags; +} + +/* ============================== Test Cases ============================== */ + +/** + * @brief test_prvTCPPrepareSend_PSH_LastSegment + * To validate if prvTCPPrepareSend() sets PSH on the segment that empties the + * send queue. + */ +void test_prvTCPPrepareSend_PSH_LastSegment( void ) +{ + uint8_t ucFlags; + + prvSetUpSocket( 4000 ); + ucFlags = ucPrepareSendFlags( 500 ); + + TEST_ASSERT_EQUAL( tcpTCP_FLAG_PSH, ucFlags & tcpTCP_FLAG_PSH ); +} + +/** + * @brief test_prvTCPPrepareSend_PSH_MoreQueued + * More queued data suppresses PSH only when the option is enabled. + */ +void test_prvTCPPrepareSend_PSH_MoreQueued( void ) +{ + uint8_t ucFlags; + + prvSetUpSocket( 4000 ); + ucFlags = ucPrepareSendFlags( 1000 ); + + #if ( ipconfigTCP_PSH_ON_LAST_SEGMENT_ONLY == ipconfigENABLE ) + TEST_ASSERT_EQUAL( 0, ucFlags & tcpTCP_FLAG_PSH ); + #else + TEST_ASSERT_EQUAL( tcpTCP_FLAG_PSH, ucFlags & tcpTCP_FLAG_PSH ); + #endif +} + +/* Prepares a connected socket whose header template holds xTemplateMAC, last + * heard from at tick 100, and sets the expectations that prvTCPReturnPacket() + * has for any segment. */ +static void vPrepareReturnPacket( BaseType_t xWithSocket ) +{ + TCPPacket_t * pxTCPPacket = ( TCPPacket_t * ) ucEthernetBuffer; + + memset( &xEndPoint, 0, sizeof( xEndPoint ) ); + xEndPoint.pxNetworkInterface = &xInterface; + xEndPoint.ipv4_settings.ulIPAddress = 0xC0C0C0C0; + xInterface.pfOutput = &NetworkInterfaceOutputFunction_Stub; + NetworkInterfaceOutputFunction_Stub_Called = 0; + + xSocket.pxEndPoint = &xEndPoint; + xSocket.u.xTCP.rxStream = &xStreamBuffer; + xSocket.u.xTCP.uxRxStreamSize = 1500; + xSocket.u.xTCP.usMSS = 1000; + xSocket.u.xTCP.xLastActTime = 100; + xSocket.u.xTCP.xTCPWindow.xSize.ulRxWindowLength = 500; + xNetworkBuffer.pxEndPoint = &xEndPoint; + pxTCPPacket->xEthernetHeader.xSourceAddress = xTemplateMAC; + ( ( TCPPacket_t * ) xSocket.u.xTCP.xPacket.u.ucLastPacket )->xEthernetHeader.xSourceAddress = xTemplateMAC; + + uxIPHeaderSizePacket_IgnoreAndReturn( ipSIZE_OF_IPv4_HEADER ); + if( xWithSocket != pdFALSE ) + { + uxStreamBufferFrontSpace_ExpectAnyArgsAndReturn( 1000 ); + FreeRTOS_min_uint32_ExpectAnyArgsAndReturn( 500 ); + } + usGenerateChecksum_ExpectAnyArgsAndReturn( 0x1111 ); + usGenerateProtocolChecksum_ExpectAnyArgsAndReturn( 0x2222 ); +} + +static TCPPacket_t * pxPrepareReturnPacket( void ) +{ + TCPPacket_t * pxTCPPacket = ( TCPPacket_t * ) ucEthernetBuffer; + + memset( &xStreamBuffer, 0, sizeof( xStreamBuffer ) ); + memset( &xSocket, 0, sizeof( xSocket ) ); + memset( ucEthernetBuffer, 0, sizeof( ucEthernetBuffer ) ); + memset( &xNetworkBuffer, 0, sizeof( xNetworkBuffer ) ); + memset( &xEndPoint, 0, sizeof( xEndPoint ) ); + xNetworkBuffer.pucEthernetBuffer = ucEthernetBuffer; + + vPrepareReturnPacket( pdTRUE ); + return pxTCPPacket; +} + +static void vExpectCacheHit( void ) +{ + /* CMock retains this address until the production call consumes it. */ + static struct xNetworkEndPoint * pxEndPoint = &xEndPoint; + + eARPGetCacheEntry_ExpectAnyArgsAndReturn( eResolutionCacheHit ); + eARPGetCacheEntry_ReturnThruPtr_pxMACAddress( ( MACAddress_t * ) &xCacheMAC ); + eARPGetCacheEntry_ReturnThruPtr_ppxEndPoint( &pxEndPoint ); +} + +/* ============================== Test Cases ============================== */ + +/** + * @brief test_prvTCPReturnPacket_SkipARP_ActivePeer + * To validate if prvTCPReturnPacket() sends to the MAC in the header template + * without searching the ARP cache while the peer was heard from recently. + */ +void test_prvTCPReturnPacket_SkipARP_ActivePeer( void ) +{ + TCPPacket_t * pxTCPPacket = pxPrepareReturnPacket(); + + #if ( ipconfigTCP_SKIP_ARP_LOOKUP_MS > 0U ) + xTaskGetTickCount_ExpectAndReturn( 100 + pdMS_TO_TICKS( ipconfigTCP_SKIP_ARP_LOOKUP_MS ) - 1 ); + #else + vExpectCacheHit(); + #endif + + prvTCPReturnPacket( &xSocket, &xNetworkBuffer, 40, pdFALSE ); + + TEST_ASSERT_EQUAL( 1, NetworkInterfaceOutputFunction_Stub_Called ); + #if ( ipconfigTCP_SKIP_ARP_LOOKUP_MS > 0U ) + TEST_ASSERT_EQUAL_UINT8_ARRAY( xTemplateMAC.ucBytes, pxTCPPacket->xEthernetHeader.xDestinationAddress.ucBytes, ipMAC_ADDRESS_LENGTH_BYTES ); + #else + TEST_ASSERT_EQUAL_UINT8_ARRAY( xCacheMAC.ucBytes, pxTCPPacket->xEthernetHeader.xDestinationAddress.ucBytes, ipMAC_ADDRESS_LENGTH_BYTES ); + #endif +} + +/** + * @brief test_prvTCPReturnPacket_SkipARP_QuietPeer + * To validate if prvTCPReturnPacket() searches the ARP cache once the peer has + * been quiet, and stores the MAC it finds in the header template. + */ +void test_prvTCPReturnPacket_SkipARP_QuietPeer( void ) +{ + TCPPacket_t * pxTCPPacket = pxPrepareReturnPacket(); + struct xNetworkEndPoint * pxEndPoint = &xEndPoint; + + #if ( ipconfigTCP_SKIP_ARP_LOOKUP_MS > 0U ) + xTaskGetTickCount_ExpectAndReturn( 100 + pdMS_TO_TICKS( ipconfigTCP_SKIP_ARP_LOOKUP_MS ) ); + #endif + eARPGetCacheEntry_ExpectAnyArgsAndReturn( eResolutionCacheHit ); + eARPGetCacheEntry_ReturnThruPtr_pxMACAddress( ( MACAddress_t * ) &xCacheMAC ); + eARPGetCacheEntry_ReturnThruPtr_ppxEndPoint( &pxEndPoint ); + + prvTCPReturnPacket( &xSocket, &xNetworkBuffer, 40, pdFALSE ); + + TEST_ASSERT_EQUAL( 1, NetworkInterfaceOutputFunction_Stub_Called ); + TEST_ASSERT_EQUAL_UINT8_ARRAY( xCacheMAC.ucBytes, pxTCPPacket->xEthernetHeader.xDestinationAddress.ucBytes, ipMAC_ADDRESS_LENGTH_BYTES ); + #if ( ipconfigTCP_SKIP_ARP_LOOKUP_MS > 0U ) + TEST_ASSERT_EQUAL_UINT8_ARRAY( xCacheMAC.ucBytes, + ( ( TCPPacket_t * ) xSocket.u.xTCP.xPacket.u.ucLastPacket )->xEthernetHeader.xSourceAddress.ucBytes, + ipMAC_ADDRESS_LENGTH_BYTES ); + #else + /* With the optimization disabled, a separate outgoing buffer must not + * update the socket template (the pre-PR behavior). */ + TEST_ASSERT_EQUAL_UINT8_ARRAY( xTemplateMAC.ucBytes, + ( ( TCPPacket_t * ) xSocket.u.xTCP.xPacket.u.ucLastPacket )->xEthernetHeader.xSourceAddress.ucBytes, + ipMAC_ADDRESS_LENGTH_BYTES ); + #endif +} + +/* Pass the very same packet through both production functions. A different + * MAC in the cache makes taking the wrong ARP branch observable. */ +static void vCheckPreparedSegment( size_t uxQueuedBytes, + BaseType_t xQuietPeer, + uint8_t ucExpectedPSH ) +{ + TCPPacket_t * pxTCPPacket = ( TCPPacket_t * ) ucEthernetBuffer; + const MACAddress_t * pxExpectedMAC = &xCacheMAC; + + ( void ) ucPrepareSendFlags( uxQueuedBytes ); + vPrepareReturnPacket( pdTRUE ); + + #if ( ipconfigTCP_PSH_ON_LAST_SEGMENT_ONLY != ipconfigENABLE ) + ucExpectedPSH = tcpTCP_FLAG_PSH; + #endif + + #if ( ipconfigTCP_SKIP_ARP_LOOKUP_MS > 0U ) + if( xQuietPeer == pdFALSE ) + { + xTaskGetTickCount_ExpectAndReturn( 100 ); + pxExpectedMAC = &xTemplateMAC; + } + else + { + xTaskGetTickCount_ExpectAndReturn( 100 + pdMS_TO_TICKS( ipconfigTCP_SKIP_ARP_LOOKUP_MS ) ); + vExpectCacheHit(); + } + #else + ( void ) xQuietPeer; + vExpectCacheHit(); + #endif + + prvTCPReturnPacket( &xSocket, &xNetworkBuffer, 540, pdFALSE ); + + TEST_ASSERT_EQUAL( 1, NetworkInterfaceOutputFunction_Stub_Called ); + TEST_ASSERT_EQUAL( 540 + ipSIZE_OF_ETH_HEADER, xNetworkBuffer.xDataLength ); + TEST_ASSERT_EQUAL( ucExpectedPSH, pxTCPPacket->xTCPHeader.ucTCPFlags & tcpTCP_FLAG_PSH ); + TEST_ASSERT_EQUAL( tcpTCP_FLAG_ACK, pxTCPPacket->xTCPHeader.ucTCPFlags & tcpTCP_FLAG_ACK ); + TEST_ASSERT_EQUAL_UINT8_ARRAY( pxExpectedMAC->ucBytes, pxTCPPacket->xEthernetHeader.xDestinationAddress.ucBytes, ipMAC_ADDRESS_LENGTH_BYTES ); +} + +void test_prvTCPPrepareAndReturn_ActivePeer( void ) +{ + prvSetUpSocket( 4000 ); + vCheckPreparedSegment( 1000, pdFALSE, 0 ); + vCheckPreparedSegment( 500, pdFALSE, tcpTCP_FLAG_PSH ); +} + +void test_prvTCPPrepareAndReturn_QuietPeer( void ) +{ + prvSetUpSocket( 4000 ); + vCheckPreparedSegment( 1000, pdTRUE, 0 ); + vCheckPreparedSegment( 500, pdTRUE, tcpTCP_FLAG_PSH ); +} + +void test_prvTCPPrepareSend_NoPayloadClearsPSH( void ) +{ + NetworkBufferDescriptor_t * pxBuffer = &xNetworkBuffer; + TCPPacket_t * pxTCPPacket = ( TCPPacket_t * ) ucEthernetBuffer; + + memset( &xSocket, 0, sizeof( xSocket ) ); + memset( ucEthernetBuffer, 0, sizeof( ucEthernetBuffer ) ); + memset( &xNetworkBuffer, 0, sizeof( xNetworkBuffer ) ); + xNetworkBuffer.pucEthernetBuffer = ucEthernetBuffer; + xSocket.u.xTCP.eTCPState = eESTABLISHED; + xSocket.u.xTCP.bits.bWinChange = pdTRUE; + #if ( ipconfigTCP_PSH_ON_LAST_SEGMENT_ONLY == ipconfigENABLE ) + xSocket.u.xTCP.ulUnpushedBytes = 500U; + #endif + pxTCPPacket->xTCPHeader.ucTCPFlags = tcpTCP_FLAG_PSH; + uxIPHeaderSizeSocket_IgnoreAndReturn( ipSIZE_OF_IPv4_HEADER ); + + TEST_ASSERT_EQUAL( 40, prvTCPPrepareSend( &xSocket, &pxBuffer, 0 ) ); + TEST_ASSERT_EQUAL( 0, pxTCPPacket->xTCPHeader.ucTCPFlags & tcpTCP_FLAG_PSH ); + TEST_ASSERT_EQUAL( tcpTCP_FLAG_ACK, pxTCPPacket->xTCPHeader.ucTCPFlags & tcpTCP_FLAG_ACK ); + #if ( ipconfigTCP_PSH_ON_LAST_SEGMENT_ONLY == ipconfigENABLE ) + TEST_ASSERT_EQUAL_UINT32( 500U, xSocket.u.xTCP.ulUnpushedBytes ); + #endif +} + +void test_prvTCPReturnPacket_SkipARP_NoSocket( void ) +{ + TCPPacket_t * pxTCPPacket = ( TCPPacket_t * ) ucEthernetBuffer; + + memset( &xNetworkBuffer, 0, sizeof( xNetworkBuffer ) ); + memset( ucEthernetBuffer, 0, sizeof( ucEthernetBuffer ) ); + xNetworkBuffer.pucEthernetBuffer = ucEthernetBuffer; + vPrepareReturnPacket( pdFALSE ); + vExpectCacheHit(); + + /* No tick expectation: the NULL socket path must always look up ARP. */ + prvTCPReturnPacket( NULL, &xNetworkBuffer, 40, pdFALSE ); + TEST_ASSERT_EQUAL( 1, NetworkInterfaceOutputFunction_Stub_Called ); + TEST_ASSERT_EQUAL_UINT8_ARRAY( xCacheMAC.ucBytes, pxTCPPacket->xEthernetHeader.xDestinationAddress.ucBytes, ipMAC_ADDRESS_LENGTH_BYTES ); +} + +void test_prvTCPReturnPacket_SkipARP_QuietPeerCacheMiss( void ) +{ + TCPPacket_t * pxTCPPacket = pxPrepareReturnPacket(); + + #if ( ipconfigTCP_SKIP_ARP_LOOKUP_MS > 0U ) + xTaskGetTickCount_ExpectAndReturn( 101 + pdMS_TO_TICKS( ipconfigTCP_SKIP_ARP_LOOKUP_MS ) ); + #endif + eARPGetCacheEntry_ExpectAnyArgsAndReturn( eResolutionCacheMiss ); + + prvTCPReturnPacket( &xSocket, &xNetworkBuffer, 40, pdFALSE ); + TEST_ASSERT_EQUAL( 1, NetworkInterfaceOutputFunction_Stub_Called ); + TEST_ASSERT_EQUAL_UINT8_ARRAY( xTemplateMAC.ucBytes, pxTCPPacket->xEthernetHeader.xDestinationAddress.ucBytes, ipMAC_ADDRESS_LENGTH_BYTES ); +} + +void test_prvTCPReturnPacket_SkipARP_TickWrap( void ) +{ + TCPPacket_t * pxTCPPacket = pxPrepareReturnPacket(); + const MACAddress_t * pxExpectedMAC = &xCacheMAC; + + xSocket.u.xTCP.xLastActTime = portMAX_DELAY - 10U; + #if ( ipconfigTCP_SKIP_ARP_LOOKUP_MS > 0U ) + /* Across wrap, elapsed time is 16 ticks, within the 1000 ms window. */ + xTaskGetTickCount_ExpectAndReturn( 5 ); + pxExpectedMAC = &xTemplateMAC; + #else + vExpectCacheHit(); + #endif + + prvTCPReturnPacket( &xSocket, &xNetworkBuffer, 40, pdFALSE ); + TEST_ASSERT_EQUAL( 1, NetworkInterfaceOutputFunction_Stub_Called ); + TEST_ASSERT_EQUAL_UINT8_ARRAY( pxExpectedMAC->ucBytes, pxTCPPacket->xEthernetHeader.xDestinationAddress.ucBytes, ipMAC_ADDRESS_LENGTH_BYTES ); +} + +/** + * @brief test_prvTCPPrepareSend_PSH_HalfWindow + * To validate if prvTCPPrepareSend() sets PSH on a segment once more than half + * of the peer's window has been sent without PSH, while more data stays queued. + */ +void test_prvTCPPrepareSend_PSH_HalfWindow( void ) +{ + const uint8_t ucExpected[] = { 0, 0, tcpTCP_FLAG_PSH, 0, 0, tcpTCP_FLAG_PSH }; + size_t uxIndex; + + prvSetUpSocket( 2200 ); + + for( uxIndex = 0; uxIndex < sizeof( ucExpected ); uxIndex++ ) + { + #if ( ipconfigTCP_PSH_ON_LAST_SEGMENT_ONLY == ipconfigENABLE ) + TEST_ASSERT_EQUAL( ucExpected[ uxIndex ], ucPrepareSendFlags( 1000 ) & tcpTCP_FLAG_PSH ); + TEST_ASSERT_EQUAL_UINT32( ( ( uxIndex + 1U ) % 3U ) * 500U, xSocket.u.xTCP.ulUnpushedBytes ); + #else + TEST_ASSERT_EQUAL( tcpTCP_FLAG_PSH, ucPrepareSendFlags( 1000 ) & tcpTCP_FLAG_PSH ); + #endif + } +} + +/* Equality must not push: the upstream rule is strictly greater than half the + * window, including integer division for an odd advertised window. */ +void test_prvTCPPrepareSend_PSH_HalfWindowBoundary( void ) +{ + uint32_t ulWindowSize; + size_t uxIndex; + uint8_t ucFlags; + + for( ulWindowSize = 2000U; ulWindowSize <= 2001U; ulWindowSize++ ) + { + prvSetUpSocket( ulWindowSize ); + + for( uxIndex = 0; uxIndex < 3U; uxIndex++ ) + { + ucFlags = ucPrepareSendFlags( 1000 ); + #if ( ipconfigTCP_PSH_ON_LAST_SEGMENT_ONLY == ipconfigENABLE ) + TEST_ASSERT_EQUAL( ( uxIndex == 2U ) ? tcpTCP_FLAG_PSH : 0, ucFlags & tcpTCP_FLAG_PSH ); + TEST_ASSERT_EQUAL_UINT32( ( ( uxIndex + 1U ) % 3U ) * 500U, xSocket.u.xTCP.ulUnpushedBytes ); + #else + TEST_ASSERT_EQUAL( tcpTCP_FLAG_PSH, ucFlags & tcpTCP_FLAG_PSH ); + #endif + } + } +} + +void test_prvTCPPrepareSend_PSH_LastSegmentResetsCount( void ) +{ + uint8_t ucFlags; + + prvSetUpSocket( 4000 ); + ( void ) ucPrepareSendFlags( 1000 ); + #if ( ipconfigTCP_PSH_ON_LAST_SEGMENT_ONLY == ipconfigENABLE ) + TEST_ASSERT_EQUAL_UINT32( 500U, xSocket.u.xTCP.ulUnpushedBytes ); + #endif + + ucFlags = ucPrepareSendFlags( 500 ); + TEST_ASSERT_EQUAL( tcpTCP_FLAG_PSH, ucFlags & tcpTCP_FLAG_PSH ); + #if ( ipconfigTCP_PSH_ON_LAST_SEGMENT_ONLY == ipconfigENABLE ) + TEST_ASSERT_EQUAL_UINT32( 0U, xSocket.u.xTCP.ulUnpushedBytes ); + #endif + + ucFlags = ucPrepareSendFlags( 1000 ); + #if ( ipconfigTCP_PSH_ON_LAST_SEGMENT_ONLY == ipconfigENABLE ) + TEST_ASSERT_EQUAL( 0, ucFlags & tcpTCP_FLAG_PSH ); + TEST_ASSERT_EQUAL_UINT32( 500U, xSocket.u.xTCP.ulUnpushedBytes ); + #else + TEST_ASSERT_EQUAL( tcpTCP_FLAG_PSH, ucFlags & tcpTCP_FLAG_PSH ); + #endif +} + +/* Reuse the upstream six-segment sequence through both production functions, + * alternating recent activity and ARP expiry without resetting the socket. */ +void test_prvTCPPrepareAndReturn_PSH_HalfWindow( void ) +{ + const uint8_t ucExpected[] = { 0, 0, tcpTCP_FLAG_PSH, 0, 0, tcpTCP_FLAG_PSH }; + size_t uxIndex; + + prvSetUpSocket( 2200 ); + + for( uxIndex = 0; uxIndex < sizeof( ucExpected ); uxIndex++ ) + { + vCheckPreparedSegment( 1000, ( uxIndex % 2U ) ? pdTRUE : pdFALSE, ucExpected[ uxIndex ] ); + #if ( ipconfigTCP_PSH_ON_LAST_SEGMENT_ONLY == ipconfigENABLE ) + TEST_ASSERT_EQUAL_UINT32( ( ( uxIndex + 1U ) % 3U ) * 500U, xSocket.u.xTCP.ulUnpushedBytes ); + #endif + } +} diff --git a/test/unit-test/FreeRTOS_TCP_Transmission_DiffConfig/TCP_Transmission_DiffConfig_list_macros.h b/test/unit-test/FreeRTOS_TCP_Transmission_DiffConfig/TCP_Transmission_DiffConfig_list_macros.h new file mode 100644 index 0000000000..0ac8ab81d8 --- /dev/null +++ b/test/unit-test/FreeRTOS_TCP_Transmission_DiffConfig/TCP_Transmission_DiffConfig_list_macros.h @@ -0,0 +1,56 @@ +/* + * FreeRTOS+TCP + * Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ +#ifndef LIST_MACRO_H +#define LIST_MACRO_H + +#include +#include +#include + +/* + * Let ARP look-up the MAC-address of the peer and initialise the first SYN + * packet. + */ +BaseType_t prvTCPPrepareConnect_IPV6( FreeRTOS_Socket_t * pxSocket ); + +/* + * Return or send a packet to the other party. + */ +void prvTCPReturnPacket_IPV6( FreeRTOS_Socket_t * pxSocket, + NetworkBufferDescriptor_t * pxDescriptor, + uint32_t ulLen, + BaseType_t xReleaseAfterSend ); + +NetworkEndPoint_t * FreeRTOS_FindEndPointOnIP_IPv6( const IPv6_Address_t * pxIPAddress ); + +/* + * Find the best fitting end-point to reach a given IP-address. + * Find an end-point whose IP-address is in the same network as the IP-address provided. + */ +NetworkEndPoint_t * FreeRTOS_FindEndPointOnNetMask( uint32_t ulIPAddress ); + +#endif /* ifndef LIST_MACRO_H */ diff --git a/test/unit-test/FreeRTOS_TCP_Transmission_DiffConfig/check_config_dependency.cmake b/test/unit-test/FreeRTOS_TCP_Transmission_DiffConfig/check_config_dependency.cmake new file mode 100644 index 0000000000..88b0b6b25b --- /dev/null +++ b/test/unit-test/FreeRTOS_TCP_Transmission_DiffConfig/check_config_dependency.cmake @@ -0,0 +1,36 @@ +# Check the production configuration dependency without generated Annexed_TCP +# headers, which strip conditional definitions used by the mock generator. +set( includes + -I${MODULE_ROOT_DIR}/source/include + -I${MODULE_ROOT_DIR}/test/unit-test/FreeRTOS_TCP_Transmission_DiffConfig + -I${MODULE_ROOT_DIR}/test/unit-test/ConfigFiles + -I${MODULE_ROOT_DIR}/test/FreeRTOS-Kernel/include + -I${MODULE_ROOT_DIR}/test/FreeRTOS-Kernel/portable/ThirdParty/GCC/Posix ) + +foreach( psh 0 1 ) + foreach( arp defaults 0 1000 ) + set( definitions -DipconfigTCP_HANG_PROTECTION=0 + -DipconfigTCP_PSH_ON_LAST_SEGMENT_ONLY=${psh} ) + if( NOT arp STREQUAL "defaults" ) + list( APPEND definitions -DipconfigTCP_SKIP_ARP_LOOKUP_MS=${arp} ) + endif() + execute_process( + COMMAND ${C_COMPILER} -fsyntax-only ${includes} ${definitions} + ${MODULE_ROOT_DIR}/source/FreeRTOS_TCP_Transmission_IPv4.c + RESULT_VARIABLE result + OUTPUT_VARIABLE output + ERROR_VARIABLE error ) + if( arp STREQUAL "1000" ) + if( result STREQUAL "0" OR NOT error MATCHES + "ipconfigTCP_SKIP_ARP_LOOKUP_MS needs ipconfigTCP_HANG_PROTECTION" ) + message( FATAL_ERROR "PSH=${psh}, ARP=${arp}: expected dependency diagnostic\n${output}${error}" ) + endif() + message( STATUS "PSH=${psh}, ARP=${arp}, HANG=0: rejected with dependency diagnostic" ) + else() + if( NOT result STREQUAL "0" ) + message( FATAL_ERROR "PSH=${psh}, ARP=${arp}: valid configuration failed\n${output}${error}" ) + endif() + message( STATUS "PSH=${psh}, ARP=${arp}, HANG=0: compiled" ) + endif() + endforeach() +endforeach() diff --git a/test/unit-test/FreeRTOS_TCP_Transmission_DiffConfig/ut.cmake b/test/unit-test/FreeRTOS_TCP_Transmission_DiffConfig/ut.cmake new file mode 100644 index 0000000000..61fb887270 --- /dev/null +++ b/test/unit-test/FreeRTOS_TCP_Transmission_DiffConfig/ut.cmake @@ -0,0 +1,151 @@ +# Include filepaths for source and include. +include( ${MODULE_ROOT_DIR}/test/unit-test/TCPFilePaths.cmake ) + +# ==================== Define your project name (edit) ======================== +function( add_tcp_transmission_config_test project_name psh arp ) + set( test_source_dir "${UNIT_TEST_DIR}/FreeRTOS_TCP_Transmission_DiffConfig" ) + message( STATUS "${project_name}" ) + + # ===================== Create your mock here (edit) ======================== + set(mock_list "") + + # list the files to mock here + list(APPEND mock_list + "${MODULE_ROOT_DIR}/test/FreeRTOS-Kernel/include/task.h" + "${MODULE_ROOT_DIR}/test/FreeRTOS-Kernel/include/list.h" + "${MODULE_ROOT_DIR}/test/FreeRTOS-Kernel/include/queue.h" + "${MODULE_ROOT_DIR}/test/FreeRTOS-Kernel/include/event_groups.h" + "${CMAKE_BINARY_DIR}/Annexed_TCP/FreeRTOS_IP.h" + "${CMAKE_BINARY_DIR}/Annexed_TCP/FreeRTOS_IP_Timers.h" + "${CMAKE_BINARY_DIR}/Annexed_TCP/FreeRTOS_IP_Utils.h" + "${CMAKE_BINARY_DIR}/Annexed_TCP/FreeRTOS_ARP.h" + "${CMAKE_BINARY_DIR}/Annexed_TCP/FreeRTOS_ICMP.h" + "${CMAKE_BINARY_DIR}/Annexed_TCP/FreeRTOS_DNS.h" + "${CMAKE_BINARY_DIR}/Annexed_TCP/FreeRTOS_Sockets.h" + "${CMAKE_BINARY_DIR}/Annexed_TCP/FreeRTOS_DHCP.h" + "${CMAKE_BINARY_DIR}/Annexed_TCP/FreeRTOS_Stream_Buffer.h" + "${CMAKE_BINARY_DIR}/Annexed_TCP/FreeRTOS_TCP_WIN.h" + "${CMAKE_BINARY_DIR}/Annexed_TCP/FreeRTOS_TCP_IP.h" + "${CMAKE_BINARY_DIR}/Annexed_TCP/FreeRTOS_UDP_IP.h" + "${CMAKE_BINARY_DIR}/Annexed_TCP/FreeRTOS_IP_Private.h" + "${CMAKE_BINARY_DIR}/Annexed_TCP/NetworkBufferManagement.h" + "${CMAKE_BINARY_DIR}/Annexed_TCP/NetworkInterface.h" + "${CMAKE_BINARY_DIR}/Annexed_TCP/FreeRTOS_TCP_State_Handling.h" + "${CMAKE_BINARY_DIR}/Annexed_TCP/FreeRTOS_TCP_Reception.h" + "${CMAKE_BINARY_DIR}/Annexed_TCP/FreeRTOS_TCP_Utils.h" + "${test_source_dir}/TCP_Transmission_DiffConfig_list_macros.h" + ) + + set(mock_include_list "") + # list the directories your mocks need + list(APPEND mock_include_list + . + ${MODULE_ROOT_DIR}/source/include + ${MODULE_ROOT_DIR}/test/FreeRTOS-Kernel/include + ${test_source_dir} + ${MODULE_ROOT_DIR}/test/unit-test/ConfigFiles + ${MODULE_ROOT_DIR}/test/FreeRTOS-Kernel/portable/ThirdParty/GCC/Posix + ) + + set(mock_define_list "") + #list the definitions of your mocks to control what to be included + list(APPEND mock_define_list + "" + ) + + # ================= Create the library under test here (edit) ================== + + set(real_source_files "") + + # list the files you would like to test here + list(APPEND real_source_files + ${CMAKE_BINARY_DIR}/Annexed_TCP_Sources/FreeRTOS_TCP_Transmission.c + ${CMAKE_BINARY_DIR}/Annexed_TCP_Sources/FreeRTOS_TCP_Transmission_IPv4.c + ) + + set(real_include_directories "") + # list the directories the module under test includes + list(APPEND real_include_directories + . + ${MODULE_ROOT_DIR}/source/include + ${test_source_dir} + ${MODULE_ROOT_DIR}/test/unit-test/ConfigFiles + ${MODULE_ROOT_DIR}/test/FreeRTOS-Kernel/include + ${MODULE_ROOT_DIR}/test/FreeRTOS-Kernel/portable/ThirdParty/GCC/Posix + ${CMOCK_DIR}/vendor/unity/src + ) + + # ===================== Create UnitTest Code here (edit) ===================== + set(test_include_directories "") + # list the directories your test needs to include + list(APPEND test_include_directories + . + ${CMOCK_DIR}/vendor/unity/src + ${CMAKE_BINARY_DIR}/Annexed_TCP/ + ${test_source_dir} + ${MODULE_ROOT_DIR}/test/unit-test/ConfigFiles + ${CMAKE_BINARY_DIR}/Annexed_TCP_Sources + ) + + # ============================= (end edit) =================================== + + set(mock_name "${project_name}_mock") + set(real_name "${project_name}_real") + + create_mock_list(${mock_name} + "${mock_list}" + "${MODULE_ROOT_DIR}/test/unit-test/cmock/project.yml" + "${mock_include_list}" + "${mock_define_list}" + ) + + create_real_library(${real_name} + "${real_source_files}" + "${real_include_directories}" + "${mock_name}" + ) + + set( utest_link_list "" ) + list(APPEND utest_link_list + -l${mock_name} + lib${real_name}.a + ) + + set( utest_dep_list "" ) + list(APPEND utest_dep_list + ${real_name} + ) + + set(utest_name "${project_name}_utest") + set(utest_source "${test_source_dir}/FreeRTOS_TCP_Transmission_DiffConfig_utest.c") + + create_test(${utest_name} + ${utest_source} + "${utest_link_list}" + "${utest_dep_list}" + "${test_include_directories}" + ) + + # Apply the same configuration to production code, mocks and test assertions. + if( NOT "${psh}" STREQUAL "defaults" ) + foreach( target ${real_name} ${mock_name} ${utest_name} ) + target_compile_definitions( ${target} PRIVATE + ipconfigTCP_PSH_ON_LAST_SEGMENT_ONLY=${psh} + ipconfigTCP_SKIP_ARP_LOOKUP_MS=${arp} ) + endforeach() + endif() +endfunction() + +add_tcp_transmission_config_test( FreeRTOS_TCP_Transmission_DiffConfig 1 1000U ) +add_tcp_transmission_config_test( FreeRTOS_TCP_Transmission_DiffConfig_PSHOnly 1 0U ) +add_tcp_transmission_config_test( FreeRTOS_TCP_Transmission_DiffConfig_ARPOnly 0 1000U ) +add_tcp_transmission_config_test( FreeRTOS_TCP_Transmission_DiffConfig_Disabled 0 0U ) +add_tcp_transmission_config_test( FreeRTOS_TCP_Transmission_DiffConfig_Defaults defaults defaults ) + +# Compile the real IPv4 source with hang protection disabled. Invalid ARP +# settings must fail with the intended diagnostic, for either PSH setting. +add_test( NAME FreeRTOS_TCP_Transmission_ConfigDependency + COMMAND ${CMAKE_COMMAND} + -DC_COMPILER=${CMAKE_C_COMPILER} + -DMODULE_ROOT_DIR=${MODULE_ROOT_DIR} + -P ${UNIT_TEST_DIR}/FreeRTOS_TCP_Transmission_DiffConfig/check_config_dependency.cmake )