1
0
mirror of https://github.com/facebookincubator/mvfst.git synced 2025-11-25 15:43:13 +03:00
Files
mvfst/quic/dsr/backend/DSRPacketizer.cpp
Brandon Schlinker a1445434b0 Cleanup and modularize receive path, improve timestamp support [5/x]
Summary:
This diff changes `QuicAsyncUDPSocketWrapper` so that it is an abstraction layer that inherits from `QuicAsyncUDPSocketType`, instead of simply being a container with aliases.
- Key changes in `QuicAsyncUDPSocketWrapper.h`, the rest of the updates switch us from using `QuicAsyncUDPSocketType` to `QuicAsyncUDPSocketWrapper`.
- It's difficult to mock the UDP socket today given that we expose the entire `folly::AsyncUDPSocket` type to the higher layers of the QUIC stack. This complicates testing and emulation because any mock / fake has to implement low level primitives like `recvmmsg`, and because the `folly::AsyncUDPSocket` interface can change over time.
- Pure virtual functions will be defined in `QuicAsyncUDPSocketWrapper` in a follow up diff to start creating an interface between the higher layers of the mvfst QUIC stack and the UDP socket, and this interface will abstract away lower layer details such as `cmsgs` and `io_vec`, and instead focus on populating higher layer structures such as `NetworkData` and `ReceivedPacket` (D48714615). This will make it easier for us to mock or fake the UDP socket.

This diff relies on changes to `folly::MockAsyncUDPSocket` introduced in D48717389.

--

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: jbeshay, hanidamlaj

Differential Revision: D48717388

fbshipit-source-id: 4f34182a69ab1e619e454da19e357a6a2ee2b9ab
2023-09-21 07:57:58 -07:00

182 lines
5.9 KiB
C++

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <quic/api/IoBufQuicBatch.h>
#include <quic/api/QuicGsoBatchWriters.h>
#include <quic/api/QuicTransportFunctions.h>
#include <quic/dsr/backend/DSRPacketizer.h>
namespace quic {
bool writeSingleQuicPacket(
IOBufQuicBatch& ioBufBatch,
BufAccessor& accessor,
ConnectionId dcid,
PacketNum packetNum,
PacketNum largestAckedByPeer,
const Aead& aead,
const PacketNumberCipher& headerCipher,
StreamId streamId,
size_t offset,
size_t length,
bool eof,
Buf buf) {
if (buf->computeChainDataLength() < length) {
LOG(ERROR) << "Insufficient data buffer";
return false;
}
auto buildBuf = accessor.obtain();
auto prevSize = buildBuf->length();
accessor.release(std::move(buildBuf));
auto rollbackBuf = [&accessor, prevSize]() {
auto buildBuf = accessor.obtain();
buildBuf->trimEnd(buildBuf->length() - prevSize);
accessor.release(std::move(buildBuf));
};
ShortHeader shortHeader(ProtectionType::KeyPhaseZero, dcid, packetNum);
InplaceQuicPacketBuilder builder(
accessor,
kDefaultMaxUDPPayload,
std::move(shortHeader),
largestAckedByPeer,
0);
builder.encodePacketHeader();
builder.accountForCipherOverhead(aead.getCipherOverhead());
// frontend has already limited the length to flow control, thus
// flowControlLen == length
auto dataLen = writeStreamFrameHeader(
builder,
streamId,
offset,
length,
length /* flow control len*/,
eof,
true /* skip length field in stream header */,
folly::none, /* stream group id */
false /* don't append frame to builder */);
BufQueue bufQueue(std::move(buf));
writeStreamFrameData(builder, bufQueue, *dataLen);
auto packet = std::move(builder).buildPacket();
CHECK(accessor.ownsBuffer());
if (packet.packet.empty) {
LOG(ERROR) << "DSR Send failed: Build empty packet.";
ioBufBatch.flush();
return false;
}
if (!packet.body) {
LOG(ERROR) << "DSR Send failed: Build empty body buffer";
rollbackBuf();
ioBufBatch.flush();
return false;
}
CHECK(!packet.header->isChained());
auto headerLen = packet.header->length();
buildBuf = accessor.obtain();
CHECK(
packet.body->data() > buildBuf->data() &&
packet.body->tail() <= buildBuf->tail());
CHECK(
packet.header->data() >= buildBuf->data() &&
packet.header->tail() < buildBuf->tail());
// Trim off everything before the current packet, and the header length, so
// buildBuf's data starts from the body part of buildBuf.
buildBuf->trimStart(prevSize + headerLen);
// buildBuf and packetbuildBuf is actually the same.
auto packetbuildBuf =
aead.inplaceEncrypt(std::move(buildBuf), packet.header.get(), packetNum);
CHECK_EQ(packetbuildBuf->headroom(), headerLen + prevSize);
// Include header back.
packetbuildBuf->prepend(headerLen);
HeaderForm headerForm = packet.packet.header.getHeaderForm();
encryptPacketHeader(
headerForm,
packetbuildBuf->writableData(),
headerLen,
packetbuildBuf->data() + headerLen,
packetbuildBuf->length() - headerLen,
headerCipher);
CHECK(!packetbuildBuf->isChained());
auto encodedSize = packetbuildBuf->length();
// Include previous packets back.
packetbuildBuf->prepend(prevSize);
accessor.release(std::move(packetbuildBuf));
bool ret =
ioBufBatch.write(nullptr /* no need to pass buildBuf */, encodedSize);
return ret;
}
// TODO using a connection state for this is kind of janky and we should
// refactor the batch writer interface to not need this.
// This isn't a real connection, it's just used for the batch writer state.
// 44 is near the number of the maximum GSO the kernel can accept for a full
// Ethernet MTU (44 * 1452 = 63888)
static auto& getThreadLocalConn(size_t maxPackets = 44) {
static thread_local QuicConnectionStateBase fakeConn{QuicNodeType::Server};
static thread_local bool initAccessor FOLLY_MAYBE_UNUSED = [&]() {
fakeConn.bufAccessor =
new SimpleBufAccessor{kDefaultMaxUDPPayload * maxPackets};
// Store this so we can use it to set the batch writer.
fakeConn.transportSettings.maxBatchSize = maxPackets;
return true;
}();
return fakeConn;
}
BufQuicBatchResult writePacketsGroup(
QuicAsyncUDPSocketWrapper& sock,
RequestGroup& reqGroup,
const std::function<Buf(const PacketizationRequest& req)>& bufProvider) {
if (reqGroup.requests.empty()) {
LOG(ERROR) << "Empty packetization request";
return {};
}
auto& fakeConn = getThreadLocalConn();
auto& bufAccessor = *fakeConn.bufAccessor;
auto batchWriter = BatchWriterPtr(new GSOInplacePacketBatchWriter(
fakeConn, fakeConn.transportSettings.maxBatchSize));
IOBufQuicBatch ioBufBatch(
std::move(batchWriter),
false /* thread local batching */,
sock,
reqGroup.clientAddress,
nullptr /* statsCallback */,
nullptr /* happyEyeballsState */);
if (!reqGroup.cipherPair->aead || !reqGroup.cipherPair->headerCipher) {
LOG(ERROR) << "Missing ciphers";
return {};
}
// It's ok if reqGourp's size is larger than ioBufBatch's batch size. The
// ioBufBatch will flush when it hits the limit then start a new batch
// transparently.
for (const auto& request : reqGroup.requests) {
auto ret = writeSingleQuicPacket(
ioBufBatch,
bufAccessor,
reqGroup.dcid,
request.packetNum,
request.largestAckedPacketNum,
*reqGroup.cipherPair->aead,
*reqGroup.cipherPair->headerCipher,
request.streamId,
request.offset,
request.len,
request.fin,
bufProvider(request));
if (!ret) {
return ioBufBatch.getResult();
}
}
ioBufBatch.flush();
return ioBufBatch.getResult();
}
} // namespace quic