Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't do full drain on chunks containing future buffered data #4215

Merged
merged 7 commits into from
Apr 9, 2024
Merged
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
14 changes: 11 additions & 3 deletions src/core/recv_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,8 @@ QuicRecvBufferPartialDrain(
}

//
// Handles draining the entire first chunk (and possibly more). Return the new
// Handles draining the entire first chunk (and possibly more). This function
// expects the chunk to not contain more (unread) data. Return the new
// drain length.
//
_IRQL_requires_max_(DISPATCH_LEVEL)
Expand Down Expand Up @@ -907,9 +908,16 @@ QuicRecvBufferDrain(
}

do {
if ((uint64_t)RecvBuffer->ReadLength > DrainLength) {
BOOLEAN PartialDrain = (uint64_t)RecvBuffer->ReadLength > DrainLength;
if (PartialDrain ||
//
// If there are 2 or more written ranges, it means that there may be
// more data later in the chunk that couldn't be read because there is a gap.
// Reuse the partial drain logic to preserve data after the gap.
//
QuicRangeSize(&RecvBuffer->WrittenRanges) > 1) {
QuicRecvBufferPartialDrain(RecvBuffer, DrainLength);
return FALSE;
return !PartialDrain;
}

DrainLength = QuicRecvBufferFullDrain(RecvBuffer, DrainLength);
Expand Down
68 changes: 68 additions & 0 deletions src/core/unittest/RecvBufferTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,74 @@ TEST_P(WithMode, RecvCrypto) // Note - Values based on a previous failing MsQuic
}
}

TEST_P(WithMode, DrainFrontChunkWithPendingGap)
{
RecvBuffer RecvBuf;
ASSERT_EQ(QUIC_STATUS_SUCCESS, RecvBuf.Initialize(GetParam(), false));
uint64_t InOutWriteLength = DEF_TEST_BUFFER_LENGTH;
BOOLEAN NewDataReady = FALSE;

// place data at some future offset
ASSERT_EQ(
QUIC_STATUS_SUCCESS,
RecvBuf.Write(
2,
1,
&InOutWriteLength,
&NewDataReady));
ASSERT_FALSE(RecvBuf.HasUnreadData());

// place data to the front and drain
InOutWriteLength = DEF_TEST_BUFFER_LENGTH;
ASSERT_EQ(
QUIC_STATUS_SUCCESS,
RecvBuf.Write(
0,
1,
&InOutWriteLength,
&NewDataReady));
ASSERT_TRUE(NewDataReady);

uint64_t ReadOffset;
QUIC_BUFFER ReadBuffers[3];
uint32_t BufferCount = ARRAYSIZE(ReadBuffers);
RecvBuf.Read(&ReadOffset, &BufferCount, ReadBuffers);
ASSERT_EQ(1ul, BufferCount);
ASSERT_EQ(1ul, ReadBuffers[0].Length);
ASSERT_TRUE(RecvBuf.Drain(1));

// insert missing chunk and drain the rest
InOutWriteLength = DEF_TEST_BUFFER_LENGTH;
ASSERT_EQ(
QUIC_STATUS_SUCCESS,
RecvBuf.Write(
1,
1,
&InOutWriteLength,
&NewDataReady));
ASSERT_TRUE(NewDataReady);

BufferCount = ARRAYSIZE(ReadBuffers);
RecvBuf.Read(&ReadOffset, &BufferCount, ReadBuffers);
uint64_t TotalRead = 0;
for (uint32_t i = 0; i < BufferCount; ++i) {
TotalRead += ReadBuffers[i].Length;
}
ASSERT_EQ(2ul, TotalRead);
ASSERT_FALSE(RecvBuf.Drain(1)); // more data left in buffer

if (GetParam() != QUIC_RECV_BUF_MODE_MULTIPLE) {
BufferCount = ARRAYSIZE(ReadBuffers);
RecvBuf.Read(&ReadOffset, &BufferCount, ReadBuffers);
TotalRead = 0;
for (uint32_t i = 0; i < BufferCount; ++i) {
TotalRead += ReadBuffers[i].Length;
}
ASSERT_EQ(1ul, TotalRead);
}
ASSERT_TRUE(RecvBuf.Drain(1));
}

INSTANTIATE_TEST_SUITE_P(
RecvBufferTest,
WithMode,
Expand Down
Loading