From aec1e17ec5cb16ece9cd4c1aa3789856f2de993b Mon Sep 17 00:00:00 2001 From: James M Snell Date: Tue, 12 May 2026 10:13:55 -0700 Subject: [PATCH] quic: cache the timestamp on send and receive Signed-off-by: James M Snell Assisted-by: Opencode:Opus 4.6 PR-URL: https://github.com/nodejs/node/pull/63267 Reviewed-By: Matteo Collina --- src/quic/application.cc | 24 +++++++++++++++++------- src/quic/application.h | 6 ++++-- src/quic/session.cc | 15 +++++++++++---- src/quic/session.h | 9 +++++++-- 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/quic/application.cc b/src/quic/application.cc index 5f57b2ce164..21475df68e4 100644 --- a/src/quic/application.cc +++ b/src/quic/application.cc @@ -239,7 +239,8 @@ void Session::Application::ReceiveStreamReset(Stream* stream, // < 0 (other): fatal error, session already closed ssize_t Session::Application::TryWritePendingDatagram(PathStorage* path, uint8_t* dest, - size_t destlen) { + size_t destlen, + uint64_t ts) { CHECK(session_->HasPendingDatagrams()); auto max_attempts = session_->config().options.max_datagram_send_attempts; @@ -275,7 +276,7 @@ ssize_t Session::Application::TryWritePendingDatagram(PathStorage* path, dg.id, &dgvec, 1, - uv_hrtime()); + ts); if (accepted) { // Nice, the datagram was accepted! @@ -338,6 +339,13 @@ void Session::Application::SendPendingData() { // call is dynamically capped by ngtcp2_conn_get_send_quantum(). static constexpr size_t kMaxPackets = 64; Debug(session_, "Application sending pending data"); + // Cache the timestamp once for the entire send loop. ngtcp2 does not + // require nanosecond-accurate monotonicity within a single burst — + // a single timestamp per SendPendingData call is what other QUIC + // implementations use (e.g., quiche, msquic). When kernel-level + // packet pacing becomes available via libuv, this timestamp becomes + // the base for computing per-packet transmit timestamps. + const uint64_t ts = uv_hrtime(); PathStorage path; StreamData stream_data; @@ -441,7 +449,8 @@ void Session::Application::SendPendingData() { // Awesome, let's write our packet! PacketInfo pi; ssize_t nwrite = WriteVStream( - &path, &pi, packet->data(), &ndatalen, packet->length(), stream_data); + &path, &pi, packet->data(), &ndatalen, packet->length(), + stream_data, ts); // When ndatalen is > 0, that's our indication that stream data was accepted // in to the packet. Yay! @@ -528,7 +537,7 @@ void Session::Application::SendPendingData() { // if there is one. Otherwise just loop around and keep going. if (session_->HasPendingDatagrams()) { auto result = TryWritePendingDatagram( - &path, packet->data(), packet->length()); + &path, packet->data(), packet->length(), ts); // When result is 0, either the datagram was congestion controlled, // didn't fit in the packet, or was abandoned. Skip and continue. @@ -590,7 +599,7 @@ void Session::Application::SendPendingData() { return session_->Close(CloseMethod::SILENT); } auto result = - TryWritePendingDatagram(&path, packet->data(), packet->length()); + TryWritePendingDatagram(&path, packet->data(), packet->length(), ts); if (result > 0) { Debug(session_, "Sending datagram packet with %zd bytes", result); enqueue_packet(packet, static_cast(result), PacketInfo()); @@ -610,7 +619,8 @@ ssize_t Session::Application::WriteVStream(PathStorage* path, uint8_t* dest, ssize_t* ndatalen, size_t max_packet_size, - const StreamData& stream_data) { + const StreamData& stream_data, + uint64_t ts) { DCHECK_LE(stream_data.count, kMaxVectorCount); uint32_t flags = NGTCP2_WRITE_STREAM_FLAG_MORE; if (stream_data.fin) flags |= NGTCP2_WRITE_STREAM_FLAG_FIN; @@ -627,7 +637,7 @@ ssize_t Session::Application::WriteVStream(PathStorage* path, stream_data.id, stream_data, stream_data.count, - uv_hrtime()); + ts); } // ============================================================================ diff --git a/src/quic/application.h b/src/quic/application.h index efce8c81ea6..59583b941b9 100644 --- a/src/quic/application.h +++ b/src/quic/application.h @@ -267,7 +267,8 @@ class Session::Application : public MemoryRetainer { // the datagram is either congestion limited or was abandoned ssize_t TryWritePendingDatagram(PathStorage* path, uint8_t* dest, - size_t destlen); + size_t destlen, + uint64_t ts); // Write the given stream_data into the buffer. The PacketInfo out-param // is populated by ngtcp2 with per-packet metadata (e.g., ECN codepoint) @@ -277,7 +278,8 @@ class Session::Application : public MemoryRetainer { uint8_t* buf, ssize_t* ndatalen, size_t max_packet_size, - const StreamData& stream_data); + const StreamData& stream_data, + uint64_t ts); Session* session_ = nullptr; }; diff --git a/src/quic/session.cc b/src/quic/session.cc index 59df31ffad2..a911ad5acd0 100644 --- a/src/quic/session.cc +++ b/src/quic/session.cc @@ -2104,20 +2104,23 @@ void Session::SetLastError(QuicError&& error) { bool Session::Receive(Store&& store, const SocketAddress& local_address, const SocketAddress& remote_address, - const PacketInfo& pkt_info) { + const PacketInfo& pkt_info, + uint64_t ts) { // Convenience wrapper: reads the packet and immediately triggers // SendPendingData. Used by paths that need an immediate response // (e.g., Endpoint::Connect for client Initial packets). // The hot receive path uses ReadPacket() directly with deferred // flush via BindingData's uv_check callback. SendPendingDataScope send_scope(this); - return ReadPacket(std::move(store), local_address, remote_address, pkt_info); + return ReadPacket( + std::move(store), local_address, remote_address, pkt_info, ts); } bool Session::ReadPacket(Store&& store, const SocketAddress& local_address, const SocketAddress& remote_address, - const PacketInfo& pkt_info) { + const PacketInfo& pkt_info, + uint64_t ts) { DCHECK(!is_destroyed()); impl_->remote_address_ = remote_address; @@ -2143,8 +2146,12 @@ bool Session::ReadPacket(Store&& store, // When libuv gains per-packet ECN reporting, the caller should // populate pkt_info from the receive metadata before calling // ReadPacket(). + // When ts is 0 (the default), call uv_hrtime() here. The batched + // receive path caches a timestamp and passes it to all ReadPacket() + // calls in the same I/O burst. + if (ts == 0) ts = uv_hrtime(); err = ngtcp2_conn_read_pkt( - *this, &path, pkt_info, vec.base, vec.len, uv_hrtime()); + *this, &path, pkt_info, vec.base, vec.len, ts); } if (is_destroyed()) return false; diff --git a/src/quic/session.h b/src/quic/session.h index d15cf00e8f8..c93c604dc33 100644 --- a/src/quic/session.h +++ b/src/quic/session.h @@ -356,7 +356,8 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source { bool Receive(Store&& store, const SocketAddress& local_address, const SocketAddress& remote_address, - const PacketInfo& pkt_info = PacketInfo()); + const PacketInfo& pkt_info = PacketInfo(), + uint64_t ts = 0); // ReadPacket processes a single inbound packet through ngtcp2 without // triggering SendPendingData. This is the building block for batched @@ -366,10 +367,14 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source { // Receive() is kept as a convenience wrapper that calls ReadPacket() // then triggers SendPendingData (for paths like Connect that need // immediate response). + // When ts is 0 (the default), uv_hrtime() is called internally. + // The batched receive path caches a timestamp and passes it to all + // ReadPacket() calls in the same I/O burst. bool ReadPacket(Store&& store, const SocketAddress& local_address, const SocketAddress& remote_address, - const PacketInfo& pkt_info = PacketInfo()); + const PacketInfo& pkt_info = PacketInfo(), + uint64_t ts = 0); // Called by BindingData's flush callback to trigger SendPendingData // on this session. Encapsulates the application() access so that