Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions source/FreeRTOS_TCP_WIN.c
Original file line number Diff line number Diff line change
Expand Up @@ -2254,21 +2254,37 @@
uint32_t ulFirst,
uint32_t ulLast )
{
uint32_t ulAckCount;
uint32_t ulAckCount = 0U;
uint32_t ulCurrentSequenceNumber = pxWindow->tx.ulCurrentSequenceNumber;
uint32_t ulLastPossible = pxWindow->tx.ulHighestSequenceNumber;

/* Receive a SACK option. */
ulAckCount = prvTCPWindowTxCheckAck( pxWindow, ulFirst, ulLast );
( void ) prvTCPWindowFastRetransmit( pxWindow, ulFirst );
if( pxWindow->tx.ulFINSequenceNumber == ulLastPossible )
{
/* The block may cover the FIN, see ulTCPWindowTxAck(). */
ulLastPossible++;
}

if( ( xTCPWindowLoggingLevel >= 1 ) && ( xSequenceGreaterThan( ulFirst, ulCurrentSequenceNumber ) != pdFALSE ) )
/* Only use a block within SND.UNA < ulFirst < ulLast <= SND.NXT. A block
* starting at SND.UNA would free data, which RFC 2018 section 8 leaves
* to the cumulative ACK. */
if( ( xSequenceGreaterThan( ulFirst, ulCurrentSequenceNumber ) != pdFALSE ) &&
( xSequenceLessThan( ulFirst, ulLastPossible ) != pdFALSE ) &&
( xSequenceLessThan( ulFirst, ulLast ) != pdFALSE ) &&
( xSequenceLessThanOrEqual( ulLast, ulLastPossible ) != pdFALSE ) )
{
FreeRTOS_debug_printf( ( "ulTCPWindowTxSack[%u,%u]: from %u to %u (ack = %u)\n",
pxWindow->usPeerPortNumber,
pxWindow->usOurPortNumber,
( unsigned ) ( ulFirst - pxWindow->tx.ulFirstSequenceNumber ),
( unsigned ) ( ulLast - pxWindow->tx.ulFirstSequenceNumber ),
( unsigned ) ( pxWindow->tx.ulCurrentSequenceNumber - pxWindow->tx.ulFirstSequenceNumber ) ) );
/* Receive a SACK option. */
ulAckCount = prvTCPWindowTxCheckAck( pxWindow, ulFirst, ulLast );
( void ) prvTCPWindowFastRetransmit( pxWindow, ulFirst );

if( xTCPWindowLoggingLevel >= 1 )
{
FreeRTOS_debug_printf( ( "ulTCPWindowTxSack[%u,%u]: from %u to %u (ack = %u)\n",
pxWindow->usPeerPortNumber,
pxWindow->usOurPortNumber,
( unsigned ) ( ulFirst - pxWindow->tx.ulFirstSequenceNumber ),
( unsigned ) ( ulLast - pxWindow->tx.ulFirstSequenceNumber ),
( unsigned ) ( pxWindow->tx.ulCurrentSequenceNumber - pxWindow->tx.ulFirstSequenceNumber ) ) );
}
}

return ulAckCount;
Expand Down
151 changes: 144 additions & 7 deletions test/unit-test/FreeRTOS_TCP_WIN/FreeRTOS_TCP_WIN_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -2676,7 +2676,7 @@ void ignore_test_ulTCPWindowTxAck_curr_seq_lt_seq_7_acked_false( void )
void test_ulTCPWindowTxSack( void )
{
uint32_t ulAckCount;
TCPWindow_t xWindow;
TCPWindow_t xWindow = { 0 };
uint32_t ulFirst = 33;
uint32_t ulLast = 63;
TCPSegment_t mockSegment = { 0 };
Expand All @@ -2685,6 +2685,7 @@ void test_ulTCPWindowTxSack( void )
initializeListItem( &mockListItem );

xWindow.tx.ulCurrentSequenceNumber = 32;
xWindow.tx.ulHighestSequenceNumber = 63;
xWindow.lSRTT = ipconfigTCP_SRTT_MINIMUM_VALUE_MS - 3;
mockSegment.u.bits.bAcked = pdFALSE_UNSIGNED;
mockSegment.lDataLength = 30;
Expand Down Expand Up @@ -2725,7 +2726,7 @@ void test_ulTCPWindowTxSack( void )
void test_ulTCPWindowTxSack_prvTCPWindowFastRetransmit_1( void )
{
uint32_t ulAckCount;
TCPWindow_t xWindow;
TCPWindow_t xWindow = { 0 };
uint32_t ulFirst = 33;
uint32_t ulLast = 63;
TCPSegment_t mockSegment = { 0 };
Expand All @@ -2734,6 +2735,7 @@ void test_ulTCPWindowTxSack_prvTCPWindowFastRetransmit_1( void )
initializeListItem( &mockListItem );

xWindow.tx.ulCurrentSequenceNumber = 32;
xWindow.tx.ulHighestSequenceNumber = 63;
xWindow.lSRTT = ipconfigTCP_SRTT_MINIMUM_VALUE_MS + 30;
mockSegment.u.bits.bAcked = pdFALSE_UNSIGNED;
mockSegment.lDataLength = 30;
Expand Down Expand Up @@ -2777,7 +2779,7 @@ void test_ulTCPWindowTxSack_prvTCPWindowFastRetransmit_1( void )
void test_ulTCPWindowTxSack_prvTCPWindowFastRetransmit_2( void )
{
uint32_t ulAckCount;
TCPWindow_t xWindow;
TCPWindow_t xWindow = { 0 };
uint32_t ulFirst = 33;
uint32_t ulLast = 63;
TCPSegment_t mockSegment = { 0 };
Expand All @@ -2786,6 +2788,7 @@ void test_ulTCPWindowTxSack_prvTCPWindowFastRetransmit_2( void )
initializeListItem( &mockListItem );

xWindow.tx.ulCurrentSequenceNumber = 32;
xWindow.tx.ulHighestSequenceNumber = 63;
mockSegment.u.bits.bAcked = pdFALSE_UNSIGNED;
mockSegment.lDataLength = 30;
/* The segment has been transmitted, so it may be confirmed. */
Expand Down Expand Up @@ -2816,7 +2819,7 @@ void test_ulTCPWindowTxSack_prvTCPWindowFastRetransmit_2( void )
void test_ulTCPWindowTxSack_prvTCPWindowFastRetransmit_3( void )
{
uint32_t ulAckCount;
TCPWindow_t xWindow;
TCPWindow_t xWindow = { 0 };
uint32_t ulFirst = 33;
uint32_t ulLast = 63;
TCPSegment_t mockSegment = { 0 };
Expand All @@ -2827,6 +2830,7 @@ void test_ulTCPWindowTxSack_prvTCPWindowFastRetransmit_3( void )

xTCPWindowLoggingLevel = 3;
xWindow.tx.ulCurrentSequenceNumber = 32;
xWindow.tx.ulHighestSequenceNumber = 63;
mockSegment.u.bits.bAcked = pdFALSE_UNSIGNED;
mockSegment.lDataLength = 30;
/* The segment has been transmitted, so it may be confirmed. */
Expand Down Expand Up @@ -2859,7 +2863,7 @@ void test_ulTCPWindowTxSack_prvTCPWindowFastRetransmit_3( void )
void test_ulTCPWindowTxSack_prvTCPWindowFastRetransmit_LoggingGTZero( void )
{
uint32_t ulAckCount;
TCPWindow_t xWindow;
TCPWindow_t xWindow = { 0 };
uint32_t ulFirst = 33;
uint32_t ulLast = 63;
TCPSegment_t mockSegment = { 0 };
Expand All @@ -2871,6 +2875,7 @@ void test_ulTCPWindowTxSack_prvTCPWindowFastRetransmit_LoggingGTZero( void )

xTCPWindowLoggingLevel = 0;
xWindow.tx.ulCurrentSequenceNumber = 32;
xWindow.tx.ulHighestSequenceNumber = 63;
mockSegment.u.bits.bAcked = pdFALSE_UNSIGNED;
mockSegment.lDataLength = 30;
/* The segment has been transmitted, so it may be confirmed. */
Expand Down Expand Up @@ -2904,7 +2909,7 @@ void test_ulTCPWindowTxSack_prvTCPWindowFastRetransmit_LoggingGTZero( void )
void test_ulTCPWindowTxSack_prvTCPWindowFastRetransmit_4_LoggingLTZero( void )
{
uint32_t ulAckCount;
TCPWindow_t xWindow;
TCPWindow_t xWindow = { 0 };
uint32_t ulFirst = 33;
uint32_t ulLast = 63;
TCPSegment_t mockSegment = { 0 };
Expand All @@ -2917,6 +2922,7 @@ void test_ulTCPWindowTxSack_prvTCPWindowFastRetransmit_4_LoggingLTZero( void )
initializeList( &xWindow.xPriorityQueue );

xWindow.tx.ulCurrentSequenceNumber = 32;
xWindow.tx.ulHighestSequenceNumber = 63;
mockSegment.u.bits.bAcked = pdFALSE_UNSIGNED;
mockSegment.lDataLength = 30;
/* The segment has been transmitted, so it may be confirmed. */
Expand Down Expand Up @@ -2951,7 +2957,7 @@ void test_ulTCPWindowTxSack_prvTCPWindowFastRetransmit_4_LoggingLTZero( void )
void test_ulTCPWindowTxSack_prvTCPWindowFastRetransmit_5_ulTimerGetAgeReturnNegative( void )
{
uint32_t ulAckCount;
TCPWindow_t xWindow;
TCPWindow_t xWindow = { 0 };
uint32_t ulFirst = 33;
uint32_t ulLast = 63;
TCPSegment_t mockSegment = { 0 };
Expand All @@ -2960,6 +2966,7 @@ void test_ulTCPWindowTxSack_prvTCPWindowFastRetransmit_5_ulTimerGetAgeReturnNega
initializeListItem( &mockListItem );

xWindow.tx.ulCurrentSequenceNumber = 32;
xWindow.tx.ulHighestSequenceNumber = 63;
xWindow.lSRTT = ipconfigTCP_SRTT_MINIMUM_VALUE_MS + 30;
mockSegment.u.bits.bAcked = pdFALSE_UNSIGNED;
mockSegment.lDataLength = 30;
Expand Down Expand Up @@ -2999,3 +3006,133 @@ void test_ulTCPWindowTxSack_prvTCPWindowFastRetransmit_5_ulTimerGetAgeReturnNega
TEST_ASSERT_EQUAL( 0, ulAckCount );
TEST_ASSERT_EQUAL( 268435455, xWindow.lSRTT ); /* Expected result is: ( 0x7FFFFFFF / 8 ). */
}

/**
* @brief A SACK block that starts at SND.UNA would make prvTCPWindowTxCheckAck()
* free the head segment and advance SND.UNA, and one below SND.UNA covers
* data that is already acknowledged. RFC 2018 section 8 leaves releasing
* data to the cumulative ACK, so both blocks are ignored. No list
* traversal is mocked, so any attempt to walk a list fails this test.
*/
void test_ulTCPWindowTxSack_first_at_or_below_snd_una_ignored( void )
{
TCPWindow_t xWindow = { 0 };

/* SND.UNA = 32, SND.NXT = 63. */
xWindow.tx.ulCurrentSequenceNumber = 32;
xWindow.tx.ulHighestSequenceNumber = 63;

TEST_ASSERT_EQUAL( 0, ulTCPWindowTxSack( &xWindow, 32, 63 ) );
TEST_ASSERT_EQUAL( 0, ulTCPWindowTxSack( &xWindow, 31, 63 ) );
TEST_ASSERT_EQUAL( 32, xWindow.tx.ulCurrentSequenceNumber );
}

/**
* @brief An empty or reversed block describes no data and is ignored.
*/
void test_ulTCPWindowTxSack_empty_or_reversed_ignored( void )
{
TCPWindow_t xWindow = { 0 };

xWindow.tx.ulCurrentSequenceNumber = 32;
xWindow.tx.ulHighestSequenceNumber = 63;

TEST_ASSERT_EQUAL( 0, ulTCPWindowTxSack( &xWindow, 40, 40 ) );
TEST_ASSERT_EQUAL( 0, ulTCPWindowTxSack( &xWindow, 50, 40 ) );
}

/**
* @brief A block that ends above SND.NXT covers data that was never sent. No
* FIN was sent, so tx.ulFINSequenceNumber is 0 and adds nothing.
*/
void test_ulTCPWindowTxSack_last_above_snd_nxt_ignored( void )
{
TCPWindow_t xWindow = { 0 };

xWindow.tx.ulCurrentSequenceNumber = 32;
xWindow.tx.ulHighestSequenceNumber = 63;

TEST_ASSERT_EQUAL( 0, ulTCPWindowTxSack( &xWindow, 33, 64 ) );
}

/**
* @brief A block that starts above SND.NXT, with an end that wraps around the
* sequence space, passes the other three checks. The ulFirst < SND.NXT
* check rejects it.
*/
void test_ulTCPWindowTxSack_first_above_snd_nxt_wrapped_ignored( void )
{
TCPWindow_t xWindow = { 0 };

xWindow.tx.ulCurrentSequenceNumber = 32;
xWindow.tx.ulHighestSequenceNumber = 63;

TEST_ASSERT_EQUAL( 0, ulTCPWindowTxSack( &xWindow, 64, 0x80000040U ) );
}

/**
* @brief A FIN sent after the last data byte takes sequence number 63, so a
* block may end at 64. The block is used: both list walks are mocked
* and must happen.
*/
void test_ulTCPWindowTxSack_block_covering_fin_accepted( void )
{
uint32_t ulAckCount;
TCPWindow_t xWindow = { 0 };

xWindow.tx.ulCurrentSequenceNumber = 32;
xWindow.tx.ulHighestSequenceNumber = 63;
xWindow.tx.ulFINSequenceNumber = 63;

/* ->prvTCPWindowTxCheckAck */
listGET_NEXT_ExpectAnyArgsAndReturn( ( ListItem_t * ) &xWindow.xTxSegments.xListEnd );
/* ->prvTCPWindowFastRetransmit */
listGET_NEXT_ExpectAnyArgsAndReturn( ( ListItem_t * ) &xWindow.xWaitQueue.xListEnd );

ulAckCount = ulTCPWindowTxSack( &xWindow, 33, 64 );

TEST_ASSERT_EQUAL( 0, ulAckCount );
}

/**
* @brief The sent window wraps around the sequence space: SND.UNA = 0xFFFFFFF0
* and SND.NXT = 0x20. A block starting at SND.UNA is still ignored, and
* a block above it that also wraps is used.
*/
void test_ulTCPWindowTxSack_window_wraps_sequence_space( void )
{
uint32_t ulAckCount;
TCPWindow_t xWindow = { 0 };

xWindow.tx.ulCurrentSequenceNumber = 0xFFFFFFF0U;
xWindow.tx.ulHighestSequenceNumber = 0x20U;

TEST_ASSERT_EQUAL( 0, ulTCPWindowTxSack( &xWindow, 0xFFFFFFF0U, 0x20U ) );

/* ->prvTCPWindowTxCheckAck */
listGET_NEXT_ExpectAnyArgsAndReturn( ( ListItem_t * ) &xWindow.xTxSegments.xListEnd );
/* ->prvTCPWindowFastRetransmit */
listGET_NEXT_ExpectAnyArgsAndReturn( ( ListItem_t * ) &xWindow.xWaitQueue.xListEnd );

ulAckCount = ulTCPWindowTxSack( &xWindow, 0xFFFFFFF8U, 0x10U );

TEST_ASSERT_EQUAL( 0, ulAckCount );
}

/**
* @brief Once a FIN is sent, prvTCPHandleFin() and prvHandleEstablished() may
* set tx.ulCurrentSequenceNumber to tx.ulFINSequenceNumber. The check
* uses that same variable, so every block is ignored in this state,
* including one that covers the FIN.
*/
void test_ulTCPWindowTxSack_fin_sent_current_at_fin_ignored( void )
{
TCPWindow_t xWindow = { 0 };

xWindow.tx.ulCurrentSequenceNumber = 63;
xWindow.tx.ulHighestSequenceNumber = 63;
xWindow.tx.ulFINSequenceNumber = 63;

TEST_ASSERT_EQUAL( 0, ulTCPWindowTxSack( &xWindow, 33, 63 ) );
TEST_ASSERT_EQUAL( 0, ulTCPWindowTxSack( &xWindow, 63, 64 ) );
}