Skip to content

Commit

Permalink
Cleanup and modularize receive path, improve timestamp support [22/x]
Browse files Browse the repository at this point in the history
Summary:
This diff changes `WriteAckFrameState::ReceivedPacket` so that it stores a complete `ReceivedUdpPacket::Timings` object, instead of just one field extracted from that object. This lets us have access to both user space and socket timestamps in our ACK RX timestamp handling code. See D48785086 for a similar change.

--

This diff is part of a larger stack focused on the following:

- **Cleaning up client and server UDP packet receive paths while improving testability.** We currently have multiple receive paths for client and server. Capabilities vary significantly and there are few tests. For instance:
  - The server receive path supports socket RX timestamps, abet incorrectly in that it does not store timestamp per packet. In comparison, the client receive path does not currently support socket RX timestamps, although the code in `QuicClientTransport::recvmsg` and `QuicClientTransport::recvmmsg` makes reference to socket RX timestamps, making it confusing to understand the capabilities available when tracing through the code. This complicates the tests in `QuicTypedTransportTests`, as we have to disable test logic that depends on socket RX timestamps for client tests.
  - The client currently has three receive paths, and none of them are well tested.

- **Modularize and abstract components in the receive path.** This will make it easier to mock/fake the UDP socket and network layers.
  - `QuicClientTransport` and `QuicServerTransport` currently contain UDP socket handling logic that operates over lower layer primitives such `cmsg` and `io_vec` (see `QuicClientTransport::recvmmsg` and `...::recvmsg` as examples).
  - Because this UDP socket handling logic is inside of the mvfst transport implementations, it is difficult to test this logic in isolation and mock/fake the underlying socket and network layers. For instance, injecting a user space network emulator that operates at the socket layer would require faking `folly::AsyncUDPSocket`, which is non-trivial given that `AsyncUDPSocket` does not abstract away intricacies arising from the aforementioned lower layer primitives.
  - By shifting this logic into an intermediate layer between the transport and the underlying UDP socket, it will be easier to mock out the UDP socket layer when testing functionality at higher layers, and inject fake components when we want to emulate the network between a mvfst client and server. It will also be easier for us to have unit tests focused on testing interactions between the UDP socket implementation and this intermediate layer.

- **Improving receive path timestamping.** We only record a single timestamp per `NetworkData` at the moment, but (1) it is possible for a `NetworkData` to have multiple packets, each with their own timestamps, and (2) we should be able to record both userspace and socket timestamps.

Reviewed By: silver23arrow

Differential Revision: D48795108

fbshipit-source-id: 70471d2654a09cbf25e711af583c18084eb90ca0
  • Loading branch information
Brandon Schlinker authored and facebook-github-bot committed Dec 1, 2023
1 parent 29377c1 commit 19475b3
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 28 deletions.
19 changes: 12 additions & 7 deletions quic/codec/QuicWriteCodec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,16 @@ static size_t fillPacketReceiveTimestamps(
timestampIt->pktNum <= timestampIntervalsIt->end) {
std::chrono::microseconds deltaDuration;
if (timestampIt == recvdPacketInfos.crbegin()) {
deltaDuration = (timestampIt->timeStamp > ackFrameMetaData.connTime)
deltaDuration =
(timestampIt->timings.receiveTimePoint > ackFrameMetaData.connTime)
? std::chrono::duration_cast<std::chrono::microseconds>(
timestampIt->timeStamp - ackFrameMetaData.connTime)
timestampIt->timings.receiveTimePoint -
ackFrameMetaData.connTime)
: 0us;
} else {
deltaDuration = std::chrono::duration_cast<std::chrono::microseconds>(
(timestampIt - 1)->timeStamp - timestampIt->timeStamp);
(timestampIt - 1)->timings.receiveTimePoint -
timestampIt->timings.receiveTimePoint);
}
auto delta = deltaDuration.count() >> receiveTimestampsExponent;
// Check if adding a new time-stamp delta from the current time-stamp
Expand Down Expand Up @@ -424,10 +427,11 @@ folly::Optional<WriteAckFrame> writeAckFrameToPacketBuilder(
maybeLastPktNum = ackState.lastRecvdPacketInfo.value().pktNum;

maybeLastPktTsDelta =
(ackState.lastRecvdPacketInfo.value().timeStamp >
(ackState.lastRecvdPacketInfo.value().timings.receiveTimePoint >
ackFrameMetaData.connTime
? std::chrono::duration_cast<std::chrono::microseconds>(
ackState.lastRecvdPacketInfo.value().timeStamp -
ackState.lastRecvdPacketInfo.value()
.timings.receiveTimePoint -
ackFrameMetaData.connTime)
: 0us);

Expand Down Expand Up @@ -507,10 +511,11 @@ folly::Optional<WriteAckFrameResult> writeAckFrameWithReceivedTimestamps(
if (ackState.lastRecvdPacketInfo) {
lastPktNum = ackState.lastRecvdPacketInfo.value().pktNum;
lastPktTsDelta =
(ackState.lastRecvdPacketInfo.value().timeStamp >
(ackState.lastRecvdPacketInfo.value().timings.receiveTimePoint >
ackFrameMetaData.connTime
? std::chrono::duration_cast<std::chrono::microseconds>(
ackState.lastRecvdPacketInfo.value().timeStamp -
ackState.lastRecvdPacketInfo.value()
.timings.receiveTimePoint -
ackFrameMetaData.connTime)
: 0us);
}
Expand Down
3 changes: 2 additions & 1 deletion quic/codec/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <quic/common/BufUtil.h>
#include <quic/common/CircularDeque.h>
#include <quic/common/IntervalSet.h>
#include <quic/common/NetworkData.h>
#include <quic/common/SmallCollections.h>
#include <quic/common/Variant.h>

Expand Down Expand Up @@ -219,7 +220,7 @@ struct WriteAckFrameState {
*/
struct ReceivedPacket {
PacketNum pktNum;
TimePoint timeStamp;
ReceivedUdpPacket::Timings timings;
};

AckBlocks acks;
Expand Down
26 changes: 15 additions & 11 deletions quic/codec/test/QuicWriteCodecTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ void setupCommonExpects(MockQuicPacketBuilder& pktBuilder) {
}
using PacketsReceivedTimestampsDeque =
CircularDeque<WriteAckFrameState::ReceivedPacket>;

const auto kDefaultTimestampsDelta = 10us;
const AckReceiveTimestampsConfig defaultAckReceiveTimestmpsConfig = {
.receiveTimestampsExponent = kDefaultReceiveTimestampsExponent};
Expand All @@ -171,9 +170,9 @@ PacketsReceivedTimestampsDeque populateReceiveTimestamps(
auto diff = std::chrono::microseconds(
lastPacketDelta -= kDefaultTimestampsDelta);
if ((connTime + diff) > connTime) {
rpi.timeStamp = connTime + diff;
rpi.timings.receiveTimePoint = connTime + diff;
} else {
rpi.timeStamp = connTime;
rpi.timings.receiveTimePoint = connTime;
}
pktsReceivedTimestamps.emplace_front(rpi);
} else {
Expand All @@ -198,7 +197,8 @@ size_t computeBytesForAckReceivedTimestamps(
ackFrameMetadata.ackState.lastRecvdPacketInfo.value().pktNum;
std::chrono::duration lastTimeStampDelta =
std::chrono::duration_cast<std::chrono::microseconds>(
ackFrameMetadata.ackState.lastRecvdPacketInfo.value().timeStamp -
ackFrameMetadata.ackState.lastRecvdPacketInfo.value()
.timings.receiveTimePoint -
connTime);

// When FrameType == ACK_RECEIVE_TIMESTAMPS, the minimum additional
Expand Down Expand Up @@ -232,10 +232,11 @@ WriteAckFrameState createTestWriteAckState(
if (frameType == FrameType::ACK_RECEIVE_TIMESTAMPS) {
ackState.recvdPacketInfos =
populateReceiveTimestamps(ackBlocks, connTime, countTimestampsToStore);
ackState.lastRecvdPacketInfo.assign({
ackState.recvdPacketInfos.back().pktNum,
ackState.recvdPacketInfos.back().timeStamp,
});
ackState.lastRecvdPacketInfo.assign(
{ackState.recvdPacketInfos.back().pktNum,
ReceivedUdpPacket::Timings{
.receiveTimePoint =
ackState.recvdPacketInfos.back().timings.receiveTimePoint}});
}
return ackState;
}
Expand All @@ -255,7 +256,8 @@ void assertsOnDecodedReceiveTimestamps(
EXPECT_EQ(
readAckFrame.maybeLatestRecvdPacketTime.value(),
std::chrono::duration_cast<std::chrono::microseconds>(
ackFrameMetaData.ackState.lastRecvdPacketInfo.value().timeStamp -
ackFrameMetaData.ackState.lastRecvdPacketInfo.value()
.timings.receiveTimePoint -
ackFrameMetaData.connTime));
EXPECT_EQ(
readAckFrame.recvdPacketsTimestampRanges.size(),
Expand Down Expand Up @@ -947,7 +949,7 @@ TEST_P(QuicWriteCodecTest, AckFrameVeryLargeAckRange) {
rpi.pktNum = i;
auto diff = std::chrono::microseconds(
lastPacketDelta -= kDefaultTimestampsDelta);
rpi.timeStamp = connTime + diff;
rpi.timings.receiveTimePoint = connTime + diff;
pktsReceivedTimestamps.emplace_front(rpi);
} else {
break;
Expand All @@ -957,7 +959,9 @@ TEST_P(QuicWriteCodecTest, AckFrameVeryLargeAckRange) {
ackState.recvdPacketInfos = pktsReceivedTimestamps;
ackState.lastRecvdPacketInfo.assign({
ackState.recvdPacketInfos.back().pktNum,
ackState.recvdPacketInfos.back().timeStamp,
ReceivedUdpPacket::Timings{
.receiveTimePoint =
ackState.recvdPacketInfos.back().timings.receiveTimePoint},
});
}

Expand Down
6 changes: 3 additions & 3 deletions quic/state/QuicStateFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,15 +424,15 @@ uint64_t addPacketToAckState(
}
static_assert(Clock::is_steady, "Needs steady clock");

ackState.lastRecvdPacketInfo.assign({packetNum, timings.receiveTimePoint});
ackState.lastRecvdPacketInfo.assign({packetNum, timings});

if (packetNum >= expectedNextPacket) {
if (ackState.recvdPacketInfos.size() ==
conn.transportSettings.maxReceiveTimestampsPerAckStored) {
ackState.recvdPacketInfos.pop_front();
}
ackState.recvdPacketInfos.emplace_back(WriteAckFrameState::ReceivedPacket{
packetNum, timings.receiveTimePoint});
ackState.recvdPacketInfos.emplace_back(
WriteAckFrameState::ReceivedPacket{packetNum, timings});
}

if (expectedNextPacket) {
Expand Down
12 changes: 9 additions & 3 deletions quic/state/test/AckHandlersTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1635,7 +1635,9 @@ TEST_P(AckHandlersTest, purgeAckReceiveTimestamps) {
// Fill up the last 25 timestamps ending at PN 40.
for (PacketNum pktNum = 15; pktNum <= 40; ++pktNum) {
conn.ackStates.initialAckState->recvdPacketInfos.emplace_back(
WriteAckFrameState::ReceivedPacket{pktNum, expectedTime});
WriteAckFrameState::ReceivedPacket{
pktNum,
ReceivedUdpPacket::Timings{.receiveTimePoint = expectedTime}});
}

commonAckVisitorForAckFrame(*conn.ackStates.initialAckState, ackFrame);
Expand All @@ -1656,7 +1658,9 @@ TEST_P(AckHandlersTest, purgeAckReceiveTimestamps) {
// Local ACK state has timestamps for {15, 40}
for (PacketNum pktNum = 15; pktNum <= 40; ++pktNum) {
conn.ackStates.initialAckState->recvdPacketInfos.emplace_back(
WriteAckFrameState::ReceivedPacket{pktNum, expectedTime});
WriteAckFrameState::ReceivedPacket{
pktNum,
ReceivedUdpPacket::Timings{.receiveTimePoint = expectedTime}});
}
// ACK frame in the ACKed packet has ACKs for {10, 20}, {25, 35}
ackFrame.ackBlocks.emplace_back(10, 20);
Expand Down Expand Up @@ -1691,7 +1695,9 @@ TEST_P(AckHandlersTest, purgeAckReceiveTimestamps) {
// Local ACK state has timestamps for {15, 40}
for (PacketNum pktNum = 15; pktNum <= 40; ++pktNum) {
conn.ackStates.initialAckState->recvdPacketInfos.emplace_back(
WriteAckFrameState::ReceivedPacket{pktNum, expectedTime});
WriteAckFrameState::ReceivedPacket{
pktNum,
ReceivedUdpPacket::Timings{.receiveTimePoint = expectedTime}});
}
// Selectively ACK some packets in the middle - {18, 20}, {25, 35}
ackFrame.ackBlocks.emplace_back(25, 35);
Expand Down
12 changes: 9 additions & 3 deletions quic/state/test/QuicStateFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,21 @@ TEST_P(
// Packets 1 and 5 are out of order and will not be stored.
auto& ackState = getAckState(conn, PacketNumberSpace::AppData);
std::deque<WriteAckFrameState::ReceivedPacket> expectedPktsInfo = {
{0, recvdTs}, {2, recvdTs}, {3, recvdTs}, {4, recvdTs}, {6, recvdTs}};
{0, ReceivedUdpPacket::Timings{.receiveTimePoint = recvdTs}},
{2, ReceivedUdpPacket::Timings{.receiveTimePoint = recvdTs}},
{3, ReceivedUdpPacket::Timings{.receiveTimePoint = recvdTs}},
{4, ReceivedUdpPacket::Timings{.receiveTimePoint = recvdTs}},
{6, ReceivedUdpPacket::Timings{.receiveTimePoint = recvdTs}}};
EXPECT_EQ(expectedPktsInfo.size(), ackState.recvdPacketInfos.size());
for (unsigned long i = 0; i < expectedPktsInfo.size(); i++) {
EXPECT_EQ(expectedPktsInfo[i].pktNum, ackState.recvdPacketInfos[i].pktNum);
EXPECT_EQ(
expectedPktsInfo[i].timeStamp, ackState.recvdPacketInfos[i].timeStamp);
expectedPktsInfo[i].timings.receiveTimePoint,
ackState.recvdPacketInfos[i].timings.receiveTimePoint);
}
EXPECT_EQ(ackState.lastRecvdPacketInfo.value().pktNum, 5);
EXPECT_EQ(ackState.lastRecvdPacketInfo.value().timeStamp, recvdTs);
EXPECT_EQ(
ackState.lastRecvdPacketInfo.value().timings.receiveTimePoint, recvdTs);
}

INSTANTIATE_TEST_SUITE_P(
Expand Down

0 comments on commit 19475b3

Please sign in to comment.