Retain kernel timestamps in QuicReceivedPacket QUICHE currently sets SO_TIMESTAMPING, reads the timestamps into QuicUdpPacketInfo, but does not do anything with them; this CL passes them further up the stack so that we could potentially use those for receive timestamps sent to the peer. PiperOrigin-RevId: 1030163284
diff --git a/quiche/quic/core/quic_packet_reader.cc b/quiche/quic/core/quic_packet_reader.cc index 3dea5d6..06f1df9 100644 --- a/quiche/quic/core/quic_packet_reader.cc +++ b/quiche/quic/core/quic_packet_reader.cc
@@ -163,7 +163,8 @@ result.packet_buffer.buffer, result.packet_buffer.buffer_len, now, /*owns_buffer=*/false, ttl, has_ttl, headers, headers_length, /*owns_header_buffer=*/false, result.packet_info.ecn_codepoint(), - result.packet_info.GetTos(), flow_label); + result.packet_info.GetTos(), flow_label, + result.packet_info.GetReceiveTimestamp()); QuicSocketAddress self_address(self_ip, port); processor->ProcessPacket(self_address, peer_address, packet); }
diff --git a/quiche/quic/core/quic_packet_reader.h b/quiche/quic/core/quic_packet_reader.h index 94a5d17..4e30af4 100644 --- a/quiche/quic/core/quic_packet_reader.h +++ b/quiche/quic/core/quic_packet_reader.h
@@ -50,8 +50,9 @@ // packets available on the socket. // Populates |packets_dropped| if it is non-null and the socket is configured // to track dropped packets and some packets are read. - // If the socket has timestamping enabled, the per packet timestamps will be - // passed to the processor. Otherwise, |clock| will be used. + // Uses |clock| for each packet's receipt_time. If the socket has receive + // timestamping enabled, also populates each packet's + // kernel_receive_timestamp(). virtual bool ReadAndDispatchPackets(int fd, int port, const QuicClock& clock, ProcessPacketInterface* processor, QuicPacketCount* packets_dropped);
diff --git a/quiche/quic/core/quic_packet_reader_test.cc b/quiche/quic/core/quic_packet_reader_test.cc index 494dffa..f987858 100644 --- a/quiche/quic/core/quic_packet_reader_test.cc +++ b/quiche/quic/core/quic_packet_reader_test.cc
@@ -47,13 +47,15 @@ QuicSocketAddress self_address; QuicSocketAddress peer_address; std::string payload; + QuicTime kernel_receive_timestamp = QuicTime::Zero(); }; void ProcessPacket(const QuicSocketAddress& self_address, const QuicSocketAddress& peer_address, const QuicReceivedPacket& packet) override { packets_.push_back({self_address, peer_address, - std::string(packet.data(), packet.length())}); + std::string(packet.data(), packet.length()), + packet.kernel_receive_timestamp()}); } const std::vector<Packet>& packets() const { return packets_; } @@ -171,6 +173,33 @@ EXPECT_THAT(processor_.payloads(), ElementsAre("a", "bb", "ccc")); ExpectPacketAddresses(); + for (const RecordingProcessor::Packet& packet : processor_.packets()) { + EXPECT_FALSE(packet.kernel_receive_timestamp.IsInitialized()); + } +} + +// Verifies that QuicPacketReader populates kernel_receive_timestamp on +// dispatched packets when receive timestamping is enabled on the socket. +TEST_F(QuicPacketReaderTest, PopulatesKernelReceiveTimestampWhenEnabled) { + if (!socket_api_.EnableReceiveTimestamp(reader_fd_)) { + GTEST_SKIP() << "Kernel receive timestamps are not supported."; + } + + QuicPacketReader reader; + ASSERT_TRUE(Send("a")); + ASSERT_TRUE(Send("bb")); + ASSERT_TRUE(Send("ccc")); + ReadPackets(reader, 3); + + EXPECT_THAT(processor_.payloads(), ElementsAre("a", "bb", "ccc")); + ExpectPacketAddresses(); + // 1577836800 is the UNIX timestamp in seconds for 2020-01-01. + const QuicTime jan_2020 = + QuicTime::Zero() + QuicTime::Delta::FromSeconds(1577836800); + for (const RecordingProcessor::Packet& packet : processor_.packets()) { + EXPECT_TRUE(packet.kernel_receive_timestamp.IsInitialized()); + EXPECT_GT(packet.kernel_receive_timestamp, jan_2020); + } } // A GRO reader dispatches datagrams that are not GRO trains as they are, for @@ -186,6 +215,9 @@ EXPECT_THAT(processor_.payloads(), ElementsAre("a", "bb", "ccc")); ExpectPacketAddresses(); + for (const RecordingProcessor::Packet& packet : processor_.packets()) { + EXPECT_FALSE(packet.kernel_receive_timestamp.IsInitialized()); + } } #if defined(__linux__) @@ -223,13 +255,26 @@ EXPECT_EQ(result.packet_info.gso_size(), static_cast<size_t>(segment_size)); EXPECT_EQ(result.packet_buffer.buffer_len, train.size()); - // Now read the train with the GRO-enabled reader. + // Now read the train with the GRO-enabled reader and verify that enabling + // receive timestamps propagates the train's timestamp to every segment. + const bool timestamps_enabled = + socket_api_.EnableReceiveTimestamp(reader_fd_); QuicPacketReader reader({.enable_gro = true}); ASSERT_TRUE(Send(train)); ReadPackets(reader, packets.size()); EXPECT_EQ(processor_.payloads(), packets); ExpectPacketAddresses(); + if (timestamps_enabled) { + ASSERT_FALSE(processor_.packets().empty()); + ASSERT_TRUE( + processor_.packets()[0].kernel_receive_timestamp.IsInitialized()); + const QuicTime train_timestamp = + processor_.packets()[0].kernel_receive_timestamp; + for (const RecordingProcessor::Packet& packet : processor_.packets()) { + EXPECT_EQ(packet.kernel_receive_timestamp, train_timestamp); + } + } } #endif // defined(__linux__)
diff --git a/quiche/quic/core/quic_packets.cc b/quiche/quic/core/quic_packets.cc index f59c5cc..7a42926 100644 --- a/quiche/quic/core/quic_packets.cc +++ b/quiche/quic/core/quic_packets.cc
@@ -359,7 +359,8 @@ const char* buffer, size_t length, QuicTime receipt_time, bool owns_buffer, int ttl, bool ttl_valid, char* packet_headers, size_t headers_length, bool owns_header_buffer, QuicEcnCodepoint ecn_codepoint, - std::optional<uint8_t> tos, uint32_t ipv6_flow_label) + std::optional<uint8_t> tos, uint32_t ipv6_flow_label, + QuicTime kernel_receive_timestamp) : QuicEncryptedPacket(buffer, length, owns_buffer), receipt_time_(receipt_time), ttl_(ttl_valid ? ttl : -1), @@ -368,7 +369,8 @@ owns_header_buffer_(owns_header_buffer), tos_(tos), ecn_codepoint_(ecn_codepoint), - ipv6_flow_label_(ipv6_flow_label) { + ipv6_flow_label_(ipv6_flow_label), + kernel_receive_timestamp_(kernel_receive_timestamp) { // Explicit Congestion Notification is extracted from the TOS byte. QUICHE_DCHECK(!tos_.has_value() || QuicEcnCodepoint(*tos_ & kEcnMask) == ecn_codepoint_); @@ -388,12 +390,14 @@ memcpy(headers_buffer, this->packet_headers(), this->headers_length()); return std::make_unique<QuicReceivedPacket>( buffer, this->length(), receipt_time(), true, ttl(), ttl() >= 0, - headers_buffer, this->headers_length(), true, this->ecn_codepoint()); + headers_buffer, this->headers_length(), true, this->ecn_codepoint(), + /*tos=*/std::nullopt, /*ipv6_flow_label=*/0, kernel_receive_timestamp_); } return std::make_unique<QuicReceivedPacket>( buffer, this->length(), receipt_time(), true, ttl(), ttl() >= 0, nullptr, - 0, false, this->ecn_codepoint()); + 0, false, this->ecn_codepoint(), /*tos=*/std::nullopt, + /*ipv6_flow_label=*/0, kernel_receive_timestamp_); } std::ostream& operator<<(std::ostream& os, const QuicReceivedPacket& s) {
diff --git a/quiche/quic/core/quic_packets.h b/quiche/quic/core/quic_packets.h index 29a717d..bd11955 100644 --- a/quiche/quic/core/quic_packets.h +++ b/quiche/quic/core/quic_packets.h
@@ -319,7 +319,8 @@ bool owns_buffer, int ttl, bool ttl_valid, char* packet_headers, size_t headers_length, bool owns_header_buffer, QuicEcnCodepoint ecn_codepoint, - std::optional<uint8_t> tos, uint32_t ipv6_flow_label); + std::optional<uint8_t> tos, uint32_t ipv6_flow_label, + QuicTime kernel_receive_timestamp = QuicTime::Zero()); ~QuicReceivedPacket(); QuicReceivedPacket(const QuicReceivedPacket&) = delete; QuicReceivedPacket& operator=(const QuicReceivedPacket&) = delete; @@ -346,6 +347,14 @@ // Returns the IPv6 flow label in host byte order if present, or 0 otherwise. uint32_t ipv6_flow_label() const { return ipv6_flow_label_; } + // Returns the kernel receive timestamp for this packet, or QuicTime::Zero() + // if the underlying socket/reader did not provide one. Note that this + // timestamp comes from the OS kernel clock and may not share an epoch with + // receipt_time() or the connection's QuicClock. + QuicTime kernel_receive_timestamp() const { + return kernel_receive_timestamp_; + } + // By default, gtest prints the raw bytes of an object. The bool data // member (in the base class QuicData) causes this object to have padding // bytes, which causes the default gtest object printer to read @@ -370,6 +379,9 @@ QuicEcnCodepoint ecn_codepoint_; // IPv6 flow label. uint32_t ipv6_flow_label_; + // Kernel-provided receive timestamp, or QuicTime::Zero() if unavailable from + // the socket/reader. May come from a different clock than receipt_time_. + const QuicTime kernel_receive_timestamp_; }; // SerializedPacket contains information of a serialized(encrypted) packet.
diff --git a/quiche/quic/core/quic_packets_test.cc b/quiche/quic/core/quic_packets_test.cc index c9d6a01..ebd8e95 100644 --- a/quiche/quic/core/quic_packets_test.cc +++ b/quiche/quic/core/quic_packets_test.cc
@@ -165,6 +165,37 @@ EXPECT_EQ(42, packet.ipv6_flow_label()); } +// Verifies that kernel_receive_timestamp defaults to Zero() (uninitialized), +// can be provided to the constructor, and is preserved by Clone() both with +// and without headers. +TEST_F(QuicPacketsTest, KernelReceiveTimestampAndClone) { + const QuicTime timestamp = + QuicTime::Zero() + QuicTime::Delta::FromMicroseconds(123456789); + + QuicReceivedPacket default_packet("foo", 3, QuicTime::Zero()); + EXPECT_EQ(default_packet.kernel_receive_timestamp(), QuicTime::Zero()); + EXPECT_FALSE(default_packet.kernel_receive_timestamp().IsInitialized()); + EXPECT_EQ(default_packet.Clone()->kernel_receive_timestamp(), + QuicTime::Zero()); + + QuicReceivedPacket without_headers( + "foo", 3, QuicTime::Zero(), /*owns_buffer=*/false, /*ttl=*/0, + /*ttl_valid=*/true, /*packet_headers=*/nullptr, /*headers_length=*/0, + /*owns_header_buffer=*/false, QuicEcnCodepoint::ECN_ECT1, + /*tos=*/std::nullopt, /*ipv6_flow_label=*/0, timestamp); + EXPECT_EQ(without_headers.kernel_receive_timestamp(), timestamp); + EXPECT_EQ(without_headers.Clone()->kernel_receive_timestamp(), timestamp); + + char header[4] = "bar"; + QuicReceivedPacket with_headers( + "foo", 3, QuicTime::Zero(), /*owns_buffer=*/false, /*ttl=*/0, + /*ttl_valid=*/true, header, sizeof(header) - 1, + /*owns_header_buffer=*/false, QuicEcnCodepoint::ECN_ECT1, + /*tos=*/std::nullopt, /*ipv6_flow_label=*/0, timestamp); + EXPECT_EQ(with_headers.kernel_receive_timestamp(), timestamp); + EXPECT_EQ(with_headers.Clone()->kernel_receive_timestamp(), timestamp); +} + } // namespace } // namespace test } // namespace quic
diff --git a/quiche/quic/core/quic_udp_socket.h b/quiche/quic/core/quic_udp_socket.h index 3fa3d8f..9c670a8 100644 --- a/quiche/quic/core/quic_udp_socket.h +++ b/quiche/quic/core/quic_udp_socket.h
@@ -12,6 +12,7 @@ #include "quiche/quic/core/io/socket.h" #include "quiche/quic/core/quic_constants.h" +#include "quiche/quic/core/quic_time.h" #include "quiche/quic/core/quic_types.h" #include "quiche/quic/core/quic_utils.h" #include "quiche/quic/platform/api/quic_ip_address.h" @@ -140,6 +141,18 @@ return receive_timestamp_; } + // Returns the kernel receive timestamp if present, or QuicTime::Zero() + // otherwise. Note that this timestamp comes from the OS kernel clock and may + // not share an epoch with a connection's QuicClock. + QuicTime GetReceiveTimestamp() const { + if (HasValue(QuicUdpPacketInfoBit::RECV_TIMESTAMP)) { + return QuicTime::Zero() + + QuicTime::Delta::FromMicroseconds( + static_cast<int64_t>(receive_timestamp_.ToUNIXMicroseconds())); + } + return QuicTime::Zero(); + } + void SetReceiveTimestamp(QuicWallTime receive_timestamp) { receive_timestamp_ = receive_timestamp; bitmask_.Set(QuicUdpPacketInfoBit::RECV_TIMESTAMP);
diff --git a/quiche/quic/core/quic_udp_socket_test.cc b/quiche/quic/core/quic_udp_socket_test.cc index 592c248..f505915 100644 --- a/quiche/quic/core/quic_udp_socket_test.cc +++ b/quiche/quic/core/quic_udp_socket_test.cc
@@ -13,6 +13,7 @@ #include "absl/base/optimization.h" #include "absl/strings/string_view.h" #include "quiche/quic/core/io/socket.h" +#include "quiche/quic/core/quic_time.h" #include "quiche/quic/core/quic_types.h" #include "quiche/quic/platform/api/quic_expect_bug.h" #include "quiche/quic/platform/api/quic_socket_address.h" @@ -185,12 +186,17 @@ result.ok = true; result.packet_buffer = {payload.data(), payload.size()}; result.packet_info.set_gso_size(5); + const QuicTime train_timestamp = + QuicTime::Zero() + QuicTime::Delta::FromMicroseconds(987654321); + result.packet_info.SetReceiveTimestamp( + QuicWallTime::FromUNIXMicroseconds(987654321)); std::vector<std::string> segments; size_t count = socket_api.ForEachGroSegment( result, [&](const QuicUdpSocketApi::ReadPacketResult& slice) { EXPECT_TRUE(slice.packet_info.HasValue(QuicUdpPacketInfoBit::IS_GRO)); EXPECT_EQ(slice.packet_info.gso_size(), 5u); + EXPECT_EQ(slice.packet_info.GetReceiveTimestamp(), train_timestamp); segments.emplace_back(slice.packet_buffer.buffer, slice.packet_buffer.buffer_len); }); @@ -255,6 +261,22 @@ EXPECT_FALSE(called); } +// Verifies GetReceiveTimestamp() returns Zero() (uninitialized) when unset or +// zero, and converts a non-zero QuicWallTime to QuicTime. +TEST_F(QuicUdpSocketTest, GetReceiveTimestamp) { + QuicUdpPacketInfo packet_info; + EXPECT_EQ(packet_info.GetReceiveTimestamp(), QuicTime::Zero()); + EXPECT_FALSE(packet_info.GetReceiveTimestamp().IsInitialized()); + + packet_info.SetReceiveTimestamp(QuicWallTime::Zero()); + EXPECT_EQ(packet_info.GetReceiveTimestamp(), QuicTime::Zero()); + EXPECT_FALSE(packet_info.GetReceiveTimestamp().IsInitialized()); + + packet_info.SetReceiveTimestamp(QuicWallTime::FromUNIXMicroseconds(123456)); + EXPECT_EQ(packet_info.GetReceiveTimestamp(), + QuicTime::Zero() + QuicTime::Delta::FromMicroseconds(123456)); +} + } // namespace } // namespace test } // namespace quic