Commit 1592a11

Browse files
jasnelladuh95
authored andcommitted
quic: add support for future ECN marking
Set up for when libuv eventually supports ECN marking. Pass the ECN marking stuff into ngtcp2. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode:Opus 4.6 PR-URL: #63267 Backport-PR-URL: #64675 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 80e6bf9 commit 1592a11

6 files changed

Lines changed: 77 additions & 24 deletions

File tree

‎src/quic/application.cc‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,12 @@ ssize_t Session::Application::TryWritePendingDatagram(PathStorage* path,
262262
int accepted = 0;
263263
int dg_flags = NGTCP2_WRITE_DATAGRAM_FLAG_MORE;
264264

265+
// PacketInfo for the datagram path. When libuv gains per-socket ECN
266+
// marking, the value from ngtcp2 should be forwarded to the send path.
267+
PacketInfo dg_pi;
265268
ssize_t dg_nwrite = ngtcp2_conn_writev_datagram(*session_,
266269
&path->path,
267-
nullptr,
270+
dg_pi,
268271
dest,
269272
destlen,
270273
&accepted,
@@ -390,12 +393,14 @@ void Session::Application::SendPendingData() {
390393
};
391394

392395
// Accumulate a completed packet into the batch.
393-
auto enqueue_packet = [&](Packet::Ptr& pkt, size_t len) {
394-
Debug(session_, "Enqueuing packet with %zu bytes into batch", len);
395-
pkt->Truncate(len);
396-
path.CopyTo(&batch_paths[batch_count]);
397-
batch[batch_count++] = std::move(pkt);
398-
};
396+
auto enqueue_packet =
397+
[&](Packet::Ptr& pkt, size_t len, const PacketInfo& pi) {
398+
Debug(session_, "Enqueuing packet with %zu bytes into batch", len);
399+
pkt->Truncate(len);
400+
pkt->set_pkt_info(pi);
401+
path.CopyTo(&batch_paths[batch_count]);
402+
batch[batch_count++] = std::move(pkt);
403+
};
399404

400405
// We're going to enter a loop here to prepare and send no more than
401406
// max_packet_count packets.
@@ -434,8 +439,9 @@ void Session::Application::SendPendingData() {
434439
}
435440

436441
// Awesome, let's write our packet!
442+
PacketInfo pi;
437443
ssize_t nwrite = WriteVStream(
438-
&path, packet->data(), &ndatalen, packet->length(), stream_data);
444+
&path, &pi, packet->data(), &ndatalen, packet->length(), stream_data);
439445

440446
// When ndatalen is > 0, that's our indication that stream data was accepted
441447
// in to the packet. Yay!
@@ -531,7 +537,7 @@ void Session::Application::SendPendingData() {
531537
if (result > 0) {
532538
size_t len = result;
533539
Debug(session_, "Sending packet with %zu bytes", len);
534-
enqueue_packet(packet, len);
540+
enqueue_packet(packet, len, pi);
535541
if (++packet_send_count == max_packet_count) return;
536542
} elseif (result < 0) {
537543
// Any negative result other than NGTCP2_ERR_WRITE_MORE
@@ -568,7 +574,7 @@ void Session::Application::SendPendingData() {
568574
// is the size of the packet we are sending.
569575
size_t len = nwrite;
570576
Debug(session_, "Sending packet with %zu bytes", len);
571-
enqueue_packet(packet, len);
577+
enqueue_packet(packet, len, pi);
572578
if (++packet_send_count == max_packet_count) return;
573579

574580
// If there are pending datagrams, try sending them in a fresh packet.
@@ -587,7 +593,7 @@ void Session::Application::SendPendingData() {
587593
TryWritePendingDatagram(&path, packet->data(), packet->length());
588594
if (result > 0) {
589595
Debug(session_, "Sending datagram packet with %zd bytes", result);
590-
enqueue_packet(packet, static_cast<size_t>(result));
596+
enqueue_packet(packet, static_cast<size_t>(result), PacketInfo());
591597
if (++packet_send_count == max_packet_count) return;
592598
} elseif (result < 0 && result != NGTCP2_ERR_WRITE_MORE) {
593599
// Fatal error — session already closed by TryWritePendingDatagram.
@@ -600,17 +606,20 @@ void Session::Application::SendPendingData() {
600606
}
601607

602608
ssize_tSession::Application::WriteVStream(PathStorage* path,
609+
PacketInfo* pi,
603610
uint8_t* dest,
604611
ssize_t* ndatalen,
605612
size_t max_packet_size,
606613
const StreamData& stream_data) {
607614
DCHECK_LE(stream_data.count, kMaxVectorCount);
608615
uint32_t flags = NGTCP2_WRITE_STREAM_FLAG_MORE;
609616
if (stream_data.fin) flags |= NGTCP2_WRITE_STREAM_FLAG_FIN;
617+
// The PacketInfo out-param is populated by ngtcp2 with the ECN codepoint
618+
// to apply when sending this packet. When libuv gains per-socket ECN
619+
// marking, the value should be forwarded to the send path.
610620
returnngtcp2_conn_writev_stream(*session_,
611621
&path->path,
612-
// TODO(@jasnell): ECN blocked on libuv
613-
nullptr,
622+
*pi,
614623
dest,
615624
max_packet_size,
616625
ndatalen,

‎src/quic/application.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,11 @@ class Session::Application : public MemoryRetainer {
269269
uint8_t* dest,
270270
size_t destlen);
271271

272-
// Write the given stream_data into the buffer.
272+
// Write the given stream_data into the buffer. The PacketInfo out-param
273+
// is populated by ngtcp2 with per-packet metadata (e.g., ECN codepoint)
274+
// that should be applied when sending the packet.
273275
ssize_tWriteVStream(PathStorage* path,
276+
PacketInfo* pi,
274277
uint8_t* buf,
275278
ssize_t* ndatalen,
276279
size_t max_packet_size,

‎src/quic/data.h‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,40 @@ namespace node::quic {
1919
template <typename T>
2020
concept OneByteType = sizeof(T) == 1;
2121

22+
// Lightweight wrapper around ngtcp2_pkt_info. Insulates the Node.js QUIC
23+
// code from the ngtcp2 struct layout and provides a clean API boundary
24+
// for per-packet metadata (currently ECN codepoint; may grow as ngtcp2
25+
// and libuv evolve).
26+
//
27+
// Default-constructed PacketInfo is zero-initialized, which ngtcp2 treats
28+
// as ECN Not-ECT — identical to passing nullptr for the pkt_info parameter.
29+
classPacketInfofinal {
30+
public:
31+
// ECN codepoints as defined by RFC 3168.
32+
enumclassEcn : uint32_t {
33+
NOT_ECT = 0, // Not ECN-Capable Transport
34+
ECT_1 = 1, // ECN-Capable Transport(1)
35+
ECT_0 = 2, // ECN-Capable Transport(0)
36+
CE = 3, // Congestion Experienced
37+
};
38+
39+
PacketInfo() : info_{} {}
40+
explicitPacketInfo(const ngtcp2_pkt_info& info) : info_(info) {}
41+
42+
// ECN codepoint for this packet. When libuv gains per-packet ECN
43+
// reporting, populate via set_ecn() from the receive metadata
44+
// before passing to ReadPacket().
45+
Ecn ecn() const { returnstatic_cast<Ecn>(info_.ecn); }
46+
voidset_ecn(Ecn ecn) { info_.ecn = static_cast<uint32_t>(ecn); }
47+
48+
// Conversion operators for ngtcp2 API calls.
49+
operatorconst ngtcp2_pkt_info*() const { return &info_; }
50+
operator ngtcp2_pkt_info*() { return &info_; }
51+
52+
private:
53+
ngtcp2_pkt_info info_;
54+
};
55+
2256
structPathfinal : public ngtcp2_path {
2357
explicitPath(const SocketAddress& local, const SocketAddress& remote);
2458
Path(Path&& other) noexcept = default;

‎src/quic/packet.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class Packet final {
6868
size_tlength() const { return length_; }
6969
size_tcapacity() const { return capacity_; }
7070
const SocketAddress& destination() const { return destination_; }
71+
const PacketInfo& pkt_info() const { return pkt_info_; }
72+
voidset_pkt_info(const PacketInfo& pi) { pkt_info_ = pi; }
7173
Listener* listener() const { return listener_; }
7274

7375
// Redirect the packet to a different endpoint for cross-endpoint sends
@@ -148,6 +150,7 @@ class Packet final {
148150
Listener* listener_;
149151

150152
// Touched at send time.
153+
PacketInfo pkt_info_;
151154
SocketAddress destination_;
152155

153156
// Only touched by libuv during uv_udp_send and in the send callback.

‎src/quic/session.cc‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,19 +2111,21 @@ void Session::SetLastError(QuicError&& error) {
21112111

21122112
boolSession::Receive(Store&& store,
21132113
const SocketAddress& local_address,
2114-
const SocketAddress& remote_address) {
2114+
const SocketAddress& remote_address,
2115+
const PacketInfo& pkt_info) {
21152116
// Convenience wrapper: reads the packet and immediately triggers
21162117
// SendPendingData. Used by paths that need an immediate response
21172118
// (e.g., Endpoint::Connect for client Initial packets).
21182119
// The hot receive path uses ReadPacket() directly with deferred
21192120
// flush via BindingData's uv_check callback.
21202121
SendPendingDataScope send_scope(this);
2121-
returnReadPacket(std::move(store), local_address, remote_address);
2122+
returnReadPacket(std::move(store), local_address, remote_address, pkt_info);
21222123
}
21232124

21242125
boolSession::ReadPacket(Store&& store,
21252126
const SocketAddress& local_address,
2126-
const SocketAddress& remote_address) {
2127+
const SocketAddress& remote_address,
2128+
const PacketInfo& pkt_info) {
21272129
DCHECK(!is_destroyed());
21282130
impl_->remote_address_ = remote_address;
21292131

@@ -2145,12 +2147,12 @@ bool Session::ReadPacket(Store&& store,
21452147
int err;
21462148
{
21472149
NgTcp2CallbackScope callback_scope(this);
2148-
//ECN codepoint (ngtcp2_pkt_info.ecn) is not yet populated because
2149-
// libuv does not currently deliver per-packet ECN metadata. When
2150-
//libuv gains ECN receive reporting, the pkt_info should be
2151-
//populated from the per-packet metadata and passed through here.
2150+
//The PacketInfo carries per-packet metadata (currently ECN codepoint).
2151+
//When libuv gains per-packet ECN reporting, the caller should
2152+
//populate pkt_info from the receive metadata before calling
2153+
//ReadPacket().
21522154
err = ngtcp2_conn_read_pkt(
2153-
*this, &path, nullptr, vec.base, vec.len, uv_hrtime());
2155+
*this, &path, pkt_info, vec.base, vec.len, uv_hrtime());
21542156
}
21552157
if (is_destroyed()) returnfalse;
21562158

‎src/quic/session.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
355355

356356
boolReceive(Store&& store,
357357
const SocketAddress& local_address,
358-
const SocketAddress& remote_address);
358+
const SocketAddress& remote_address,
359+
const PacketInfo& pkt_info = PacketInfo());
359360

360361
// ReadPacket processes a single inbound packet through ngtcp2 without
361362
// triggering SendPendingData. This is the building block for batched
@@ -367,7 +368,8 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
367368
// immediate response).
368369
boolReadPacket(Store&& store,
369370
const SocketAddress& local_address,
370-
const SocketAddress& remote_address);
371+
const SocketAddress& remote_address,
372+
const PacketInfo& pkt_info = PacketInfo());
371373

372374
// Called by BindingData's flush callback to trigger SendPendingData
373375
// on this session. Encapsulates the application() access so that

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Commit 1592a11

Browse files
jasnelladuh95
authored andcommitted
quic: add support for future ECN marking
Set up for when libuv eventually supports ECN marking. Pass the ECN marking stuff into ngtcp2. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode:Opus 4.6 PR-URL: #63267 Backport-PR-URL: #64675 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 80e6bf9 commit 1592a11

6 files changed

Lines changed: 77 additions & 24 deletions

File tree

‎src/quic/application.cc‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,12 @@ ssize_t Session::Application::TryWritePendingDatagram(PathStorage* path,
262262
int accepted = 0;
263263
int dg_flags = NGTCP2_WRITE_DATAGRAM_FLAG_MORE;
264264

265+
// PacketInfo for the datagram path. When libuv gains per-socket ECN
266+
// marking, the value from ngtcp2 should be forwarded to the send path.
267+
PacketInfo dg_pi;
265268
ssize_t dg_nwrite = ngtcp2_conn_writev_datagram(*session_,
266269
&path->path,
267-
nullptr,
270+
dg_pi,
268271
dest,
269272
destlen,
270273
&accepted,
@@ -390,12 +393,14 @@ void Session::Application::SendPendingData() {
390393
};
391394

392395
// Accumulate a completed packet into the batch.
393-
auto enqueue_packet = [&](Packet::Ptr& pkt, size_t len) {
394-
Debug(session_, "Enqueuing packet with %zu bytes into batch", len);
395-
pkt->Truncate(len);
396-
path.CopyTo(&batch_paths[batch_count]);
397-
batch[batch_count++] = std::move(pkt);
398-
};
396+
auto enqueue_packet =
397+
[&](Packet::Ptr& pkt, size_t len, const PacketInfo& pi) {
398+
Debug(session_, "Enqueuing packet with %zu bytes into batch", len);
399+
pkt->Truncate(len);
400+
pkt->set_pkt_info(pi);
401+
path.CopyTo(&batch_paths[batch_count]);
402+
batch[batch_count++] = std::move(pkt);
403+
};
399404

400405
// We're going to enter a loop here to prepare and send no more than
401406
// max_packet_count packets.
@@ -434,8 +439,9 @@ void Session::Application::SendPendingData() {
434439
}
435440

436441
// Awesome, let's write our packet!
442+
PacketInfo pi;
437443
ssize_t nwrite = WriteVStream(
438-
&path, packet->data(), &ndatalen, packet->length(), stream_data);
444+
&path, &pi, packet->data(), &ndatalen, packet->length(), stream_data);
439445

440446
// When ndatalen is > 0, that's our indication that stream data was accepted
441447
// in to the packet. Yay!
@@ -531,7 +537,7 @@ void Session::Application::SendPendingData() {
531537
if (result > 0) {
532538
size_t len = result;
533539
Debug(session_, "Sending packet with %zu bytes", len);
534-
enqueue_packet(packet, len);
540+
enqueue_packet(packet, len, pi);
535541
if (++packet_send_count == max_packet_count) return;
536542
} elseif (result < 0) {
537543
// Any negative result other than NGTCP2_ERR_WRITE_MORE
@@ -568,7 +574,7 @@ void Session::Application::SendPendingData() {
568574
// is the size of the packet we are sending.
569575
size_t len = nwrite;
570576
Debug(session_, "Sending packet with %zu bytes", len);
571-
enqueue_packet(packet, len);
577+
enqueue_packet(packet, len, pi);
572578
if (++packet_send_count == max_packet_count) return;
573579

574580
// If there are pending datagrams, try sending them in a fresh packet.
@@ -587,7 +593,7 @@ void Session::Application::SendPendingData() {
587593
TryWritePendingDatagram(&path, packet->data(), packet->length());
588594
if (result > 0) {
589595
Debug(session_, "Sending datagram packet with %zd bytes", result);
590-
enqueue_packet(packet, static_cast<size_t>(result));
596+
enqueue_packet(packet, static_cast<size_t>(result), PacketInfo());
591597
if (++packet_send_count == max_packet_count) return;
592598
} elseif (result < 0 && result != NGTCP2_ERR_WRITE_MORE) {
593599
// Fatal error — session already closed by TryWritePendingDatagram.
@@ -600,17 +606,20 @@ void Session::Application::SendPendingData() {
600606
}
601607

602608
ssize_tSession::Application::WriteVStream(PathStorage* path,
609+
PacketInfo* pi,
603610
uint8_t* dest,
604611
ssize_t* ndatalen,
605612
size_t max_packet_size,
606613
const StreamData& stream_data) {
607614
DCHECK_LE(stream_data.count, kMaxVectorCount);
608615
uint32_t flags = NGTCP2_WRITE_STREAM_FLAG_MORE;
609616
if (stream_data.fin) flags |= NGTCP2_WRITE_STREAM_FLAG_FIN;
617+
// The PacketInfo out-param is populated by ngtcp2 with the ECN codepoint
618+
// to apply when sending this packet. When libuv gains per-socket ECN
619+
// marking, the value should be forwarded to the send path.
610620
returnngtcp2_conn_writev_stream(*session_,
611621
&path->path,
612-
// TODO(@jasnell): ECN blocked on libuv
613-
nullptr,
622+
*pi,
614623
dest,
615624
max_packet_size,
616625
ndatalen,

‎src/quic/application.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,11 @@ class Session::Application : public MemoryRetainer {
269269
uint8_t* dest,
270270
size_t destlen);
271271

272-
// Write the given stream_data into the buffer.
272+
// Write the given stream_data into the buffer. The PacketInfo out-param
273+
// is populated by ngtcp2 with per-packet metadata (e.g., ECN codepoint)
274+
// that should be applied when sending the packet.
273275
ssize_tWriteVStream(PathStorage* path,
276+
PacketInfo* pi,
274277
uint8_t* buf,
275278
ssize_t* ndatalen,
276279
size_t max_packet_size,

‎src/quic/data.h‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,40 @@ namespace node::quic {
1919
template <typename T>
2020
concept OneByteType = sizeof(T) == 1;
2121

22+
// Lightweight wrapper around ngtcp2_pkt_info. Insulates the Node.js QUIC
23+
// code from the ngtcp2 struct layout and provides a clean API boundary
24+
// for per-packet metadata (currently ECN codepoint; may grow as ngtcp2
25+
// and libuv evolve).
26+
//
27+
// Default-constructed PacketInfo is zero-initialized, which ngtcp2 treats
28+
// as ECN Not-ECT — identical to passing nullptr for the pkt_info parameter.
29+
classPacketInfofinal {
30+
public:
31+
// ECN codepoints as defined by RFC 3168.
32+
enumclassEcn : uint32_t {
33+
NOT_ECT = 0, // Not ECN-Capable Transport
34+
ECT_1 = 1, // ECN-Capable Transport(1)
35+
ECT_0 = 2, // ECN-Capable Transport(0)
36+
CE = 3, // Congestion Experienced
37+
};
38+
39+
PacketInfo() : info_{} {}
40+
explicitPacketInfo(const ngtcp2_pkt_info& info) : info_(info) {}
41+
42+
// ECN codepoint for this packet. When libuv gains per-packet ECN
43+
// reporting, populate via set_ecn() from the receive metadata
44+
// before passing to ReadPacket().
45+
Ecn ecn() const { returnstatic_cast<Ecn>(info_.ecn); }
46+
voidset_ecn(Ecn ecn) { info_.ecn = static_cast<uint32_t>(ecn); }
47+
48+
// Conversion operators for ngtcp2 API calls.
49+
operatorconst ngtcp2_pkt_info*() const { return &info_; }
50+
operator ngtcp2_pkt_info*() { return &info_; }
51+
52+
private:
53+
ngtcp2_pkt_info info_;
54+
};
55+
2256
structPathfinal : public ngtcp2_path {
2357
explicitPath(const SocketAddress& local, const SocketAddress& remote);
2458
Path(Path&& other) noexcept = default;

‎src/quic/packet.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class Packet final {
6868
size_tlength() const { return length_; }
6969
size_tcapacity() const { return capacity_; }
7070
const SocketAddress& destination() const { return destination_; }
71+
const PacketInfo& pkt_info() const { return pkt_info_; }
72+
voidset_pkt_info(const PacketInfo& pi) { pkt_info_ = pi; }
7173
Listener* listener() const { return listener_; }
7274

7375
// Redirect the packet to a different endpoint for cross-endpoint sends
@@ -148,6 +150,7 @@ class Packet final {
148150
Listener* listener_;
149151

150152
// Touched at send time.
153+
PacketInfo pkt_info_;
151154
SocketAddress destination_;
152155

153156
// Only touched by libuv during uv_udp_send and in the send callback.

‎src/quic/session.cc‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,19 +2111,21 @@ void Session::SetLastError(QuicError&& error) {
21112111

21122112
boolSession::Receive(Store&& store,
21132113
const SocketAddress& local_address,
2114-
const SocketAddress& remote_address) {
2114+
const SocketAddress& remote_address,
2115+
const PacketInfo& pkt_info) {
21152116
// Convenience wrapper: reads the packet and immediately triggers
21162117
// SendPendingData. Used by paths that need an immediate response
21172118
// (e.g., Endpoint::Connect for client Initial packets).
21182119
// The hot receive path uses ReadPacket() directly with deferred
21192120
// flush via BindingData's uv_check callback.
21202121
SendPendingDataScope send_scope(this);
2121-
returnReadPacket(std::move(store), local_address, remote_address);
2122+
returnReadPacket(std::move(store), local_address, remote_address, pkt_info);
21222123
}
21232124

21242125
boolSession::ReadPacket(Store&& store,
21252126
const SocketAddress& local_address,
2126-
const SocketAddress& remote_address) {
2127+
const SocketAddress& remote_address,
2128+
const PacketInfo& pkt_info) {
21272129
DCHECK(!is_destroyed());
21282130
impl_->remote_address_ = remote_address;
21292131

@@ -2145,12 +2147,12 @@ bool Session::ReadPacket(Store&& store,
21452147
int err;
21462148
{
21472149
NgTcp2CallbackScope callback_scope(this);
2148-
//ECN codepoint (ngtcp2_pkt_info.ecn) is not yet populated because
2149-
// libuv does not currently deliver per-packet ECN metadata. When
2150-
//libuv gains ECN receive reporting, the pkt_info should be
2151-
//populated from the per-packet metadata and passed through here.
2150+
//The PacketInfo carries per-packet metadata (currently ECN codepoint).
2151+
//When libuv gains per-packet ECN reporting, the caller should
2152+
//populate pkt_info from the receive metadata before calling
2153+
//ReadPacket().
21522154
err = ngtcp2_conn_read_pkt(
2153-
*this, &path, nullptr, vec.base, vec.len, uv_hrtime());
2155+
*this, &path, pkt_info, vec.base, vec.len, uv_hrtime());
21542156
}
21552157
if (is_destroyed()) returnfalse;
21562158

‎src/quic/session.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
355355

356356
boolReceive(Store&& store,
357357
const SocketAddress& local_address,
358-
const SocketAddress& remote_address);
358+
const SocketAddress& remote_address,
359+
const PacketInfo& pkt_info = PacketInfo());
359360

360361
// ReadPacket processes a single inbound packet through ngtcp2 without
361362
// triggering SendPendingData. This is the building block for batched
@@ -367,7 +368,8 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
367368
// immediate response).
368369
boolReadPacket(Store&& store,
369370
const SocketAddress& local_address,
370-
const SocketAddress& remote_address);
371+
const SocketAddress& remote_address,
372+
const PacketInfo& pkt_info = PacketInfo());
371373

372374
// Called by BindingData's flush callback to trigger SendPendingData
373375
// on this session. Encapsulates the application() access so that

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 1592a11

Browse files
jasnelladuh95
authored andcommitted
quic: add support for future ECN marking
Set up for when libuv eventually supports ECN marking. Pass the ECN marking stuff into ngtcp2. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode:Opus 4.6 PR-URL: #63267 Backport-PR-URL: #64675 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 80e6bf9 commit 1592a11

6 files changed

Lines changed: 77 additions & 24 deletions

File tree

‎src/quic/application.cc‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,12 @@ ssize_t Session::Application::TryWritePendingDatagram(PathStorage* path,
262262
int accepted = 0;
263263
int dg_flags = NGTCP2_WRITE_DATAGRAM_FLAG_MORE;
264264

265+
// PacketInfo for the datagram path. When libuv gains per-socket ECN
266+
// marking, the value from ngtcp2 should be forwarded to the send path.
267+
PacketInfo dg_pi;
265268
ssize_t dg_nwrite = ngtcp2_conn_writev_datagram(*session_,
266269
&path->path,
267-
nullptr,
270+
dg_pi,
268271
dest,
269272
destlen,
270273
&accepted,
@@ -390,12 +393,14 @@ void Session::Application::SendPendingData() {
390393
};
391394

392395
// Accumulate a completed packet into the batch.
393-
auto enqueue_packet = [&](Packet::Ptr& pkt, size_t len) {
394-
Debug(session_, "Enqueuing packet with %zu bytes into batch", len);
395-
pkt->Truncate(len);
396-
path.CopyTo(&batch_paths[batch_count]);
397-
batch[batch_count++] = std::move(pkt);
398-
};
396+
auto enqueue_packet =
397+
[&](Packet::Ptr& pkt, size_t len, const PacketInfo& pi) {
398+
Debug(session_, "Enqueuing packet with %zu bytes into batch", len);
399+
pkt->Truncate(len);
400+
pkt->set_pkt_info(pi);
401+
path.CopyTo(&batch_paths[batch_count]);
402+
batch[batch_count++] = std::move(pkt);
403+
};
399404

400405
// We're going to enter a loop here to prepare and send no more than
401406
// max_packet_count packets.
@@ -434,8 +439,9 @@ void Session::Application::SendPendingData() {
434439
}
435440

436441
// Awesome, let's write our packet!
442+
PacketInfo pi;
437443
ssize_t nwrite = WriteVStream(
438-
&path, packet->data(), &ndatalen, packet->length(), stream_data);
444+
&path, &pi, packet->data(), &ndatalen, packet->length(), stream_data);
439445

440446
// When ndatalen is > 0, that's our indication that stream data was accepted
441447
// in to the packet. Yay!
@@ -531,7 +537,7 @@ void Session::Application::SendPendingData() {
531537
if (result > 0) {
532538
size_t len = result;
533539
Debug(session_, "Sending packet with %zu bytes", len);
534-
enqueue_packet(packet, len);
540+
enqueue_packet(packet, len, pi);
535541
if (++packet_send_count == max_packet_count) return;
536542
} elseif (result < 0) {
537543
// Any negative result other than NGTCP2_ERR_WRITE_MORE
@@ -568,7 +574,7 @@ void Session::Application::SendPendingData() {
568574
// is the size of the packet we are sending.
569575
size_t len = nwrite;
570576
Debug(session_, "Sending packet with %zu bytes", len);
571-
enqueue_packet(packet, len);
577+
enqueue_packet(packet, len, pi);
572578
if (++packet_send_count == max_packet_count) return;
573579

574580
// If there are pending datagrams, try sending them in a fresh packet.
@@ -587,7 +593,7 @@ void Session::Application::SendPendingData() {
587593
TryWritePendingDatagram(&path, packet->data(), packet->length());
588594
if (result > 0) {
589595
Debug(session_, "Sending datagram packet with %zd bytes", result);
590-
enqueue_packet(packet, static_cast<size_t>(result));
596+
enqueue_packet(packet, static_cast<size_t>(result), PacketInfo());
591597
if (++packet_send_count == max_packet_count) return;
592598
} elseif (result < 0 && result != NGTCP2_ERR_WRITE_MORE) {
593599
// Fatal error — session already closed by TryWritePendingDatagram.
@@ -600,17 +606,20 @@ void Session::Application::SendPendingData() {
600606
}
601607

602608
ssize_tSession::Application::WriteVStream(PathStorage* path,
609+
PacketInfo* pi,
603610
uint8_t* dest,
604611
ssize_t* ndatalen,
605612
size_t max_packet_size,
606613
const StreamData& stream_data) {
607614
DCHECK_LE(stream_data.count, kMaxVectorCount);
608615
uint32_t flags = NGTCP2_WRITE_STREAM_FLAG_MORE;
609616
if (stream_data.fin) flags |= NGTCP2_WRITE_STREAM_FLAG_FIN;
617+
// The PacketInfo out-param is populated by ngtcp2 with the ECN codepoint
618+
// to apply when sending this packet. When libuv gains per-socket ECN
619+
// marking, the value should be forwarded to the send path.
610620
returnngtcp2_conn_writev_stream(*session_,
611621
&path->path,
612-
// TODO(@jasnell): ECN blocked on libuv
613-
nullptr,
622+
*pi,
614623
dest,
615624
max_packet_size,
616625
ndatalen,

‎src/quic/application.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,11 @@ class Session::Application : public MemoryRetainer {
269269
uint8_t* dest,
270270
size_t destlen);
271271

272-
// Write the given stream_data into the buffer.
272+
// Write the given stream_data into the buffer. The PacketInfo out-param
273+
// is populated by ngtcp2 with per-packet metadata (e.g., ECN codepoint)
274+
// that should be applied when sending the packet.
273275
ssize_tWriteVStream(PathStorage* path,
276+
PacketInfo* pi,
274277
uint8_t* buf,
275278
ssize_t* ndatalen,
276279
size_t max_packet_size,

‎src/quic/data.h‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,40 @@ namespace node::quic {
1919
template <typename T>
2020
concept OneByteType = sizeof(T) == 1;
2121

22+
// Lightweight wrapper around ngtcp2_pkt_info. Insulates the Node.js QUIC
23+
// code from the ngtcp2 struct layout and provides a clean API boundary
24+
// for per-packet metadata (currently ECN codepoint; may grow as ngtcp2
25+
// and libuv evolve).
26+
//
27+
// Default-constructed PacketInfo is zero-initialized, which ngtcp2 treats
28+
// as ECN Not-ECT — identical to passing nullptr for the pkt_info parameter.
29+
classPacketInfofinal {
30+
public:
31+
// ECN codepoints as defined by RFC 3168.
32+
enumclassEcn : uint32_t {
33+
NOT_ECT = 0, // Not ECN-Capable Transport
34+
ECT_1 = 1, // ECN-Capable Transport(1)
35+
ECT_0 = 2, // ECN-Capable Transport(0)
36+
CE = 3, // Congestion Experienced
37+
};
38+
39+
PacketInfo() : info_{} {}
40+
explicitPacketInfo(const ngtcp2_pkt_info& info) : info_(info) {}
41+
42+
// ECN codepoint for this packet. When libuv gains per-packet ECN
43+
// reporting, populate via set_ecn() from the receive metadata
44+
// before passing to ReadPacket().
45+
Ecn ecn() const { returnstatic_cast<Ecn>(info_.ecn); }
46+
voidset_ecn(Ecn ecn) { info_.ecn = static_cast<uint32_t>(ecn); }
47+
48+
// Conversion operators for ngtcp2 API calls.
49+
operatorconst ngtcp2_pkt_info*() const { return &info_; }
50+
operator ngtcp2_pkt_info*() { return &info_; }
51+
52+
private:
53+
ngtcp2_pkt_info info_;
54+
};
55+
2256
structPathfinal : public ngtcp2_path {
2357
explicitPath(const SocketAddress& local, const SocketAddress& remote);
2458
Path(Path&& other) noexcept = default;

‎src/quic/packet.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class Packet final {
6868
size_tlength() const { return length_; }
6969
size_tcapacity() const { return capacity_; }
7070
const SocketAddress& destination() const { return destination_; }
71+
const PacketInfo& pkt_info() const { return pkt_info_; }
72+
voidset_pkt_info(const PacketInfo& pi) { pkt_info_ = pi; }
7173
Listener* listener() const { return listener_; }
7274

7375
// Redirect the packet to a different endpoint for cross-endpoint sends
@@ -148,6 +150,7 @@ class Packet final {
148150
Listener* listener_;
149151

150152
// Touched at send time.
153+
PacketInfo pkt_info_;
151154
SocketAddress destination_;
152155

153156
// Only touched by libuv during uv_udp_send and in the send callback.

‎src/quic/session.cc‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,19 +2111,21 @@ void Session::SetLastError(QuicError&& error) {
21112111

21122112
boolSession::Receive(Store&& store,
21132113
const SocketAddress& local_address,
2114-
const SocketAddress& remote_address) {
2114+
const SocketAddress& remote_address,
2115+
const PacketInfo& pkt_info) {
21152116
// Convenience wrapper: reads the packet and immediately triggers
21162117
// SendPendingData. Used by paths that need an immediate response
21172118
// (e.g., Endpoint::Connect for client Initial packets).
21182119
// The hot receive path uses ReadPacket() directly with deferred
21192120
// flush via BindingData's uv_check callback.
21202121
SendPendingDataScope send_scope(this);
2121-
returnReadPacket(std::move(store), local_address, remote_address);
2122+
returnReadPacket(std::move(store), local_address, remote_address, pkt_info);
21222123
}
21232124

21242125
boolSession::ReadPacket(Store&& store,
21252126
const SocketAddress& local_address,
2126-
const SocketAddress& remote_address) {
2127+
const SocketAddress& remote_address,
2128+
const PacketInfo& pkt_info) {
21272129
DCHECK(!is_destroyed());
21282130
impl_->remote_address_ = remote_address;
21292131

@@ -2145,12 +2147,12 @@ bool Session::ReadPacket(Store&& store,
21452147
int err;
21462148
{
21472149
NgTcp2CallbackScope callback_scope(this);
2148-
//ECN codepoint (ngtcp2_pkt_info.ecn) is not yet populated because
2149-
// libuv does not currently deliver per-packet ECN metadata. When
2150-
//libuv gains ECN receive reporting, the pkt_info should be
2151-
//populated from the per-packet metadata and passed through here.
2150+
//The PacketInfo carries per-packet metadata (currently ECN codepoint).
2151+
//When libuv gains per-packet ECN reporting, the caller should
2152+
//populate pkt_info from the receive metadata before calling
2153+
//ReadPacket().
21522154
err = ngtcp2_conn_read_pkt(
2153-
*this, &path, nullptr, vec.base, vec.len, uv_hrtime());
2155+
*this, &path, pkt_info, vec.base, vec.len, uv_hrtime());
21542156
}
21552157
if (is_destroyed()) returnfalse;
21562158

‎src/quic/session.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
355355

356356
boolReceive(Store&& store,
357357
const SocketAddress& local_address,
358-
const SocketAddress& remote_address);
358+
const SocketAddress& remote_address,
359+
const PacketInfo& pkt_info = PacketInfo());
359360

360361
// ReadPacket processes a single inbound packet through ngtcp2 without
361362
// triggering SendPendingData. This is the building block for batched
@@ -367,7 +368,8 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
367368
// immediate response).
368369
boolReadPacket(Store&& store,
369370
const SocketAddress& local_address,
370-
const SocketAddress& remote_address);
371+
const SocketAddress& remote_address,
372+
const PacketInfo& pkt_info = PacketInfo());
371373

372374
// Called by BindingData's flush callback to trigger SendPendingData
373375
// on this session. Encapsulates the application() access so that

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 1592a11

Browse files
jasnelladuh95
authored andcommitted
quic: add support for future ECN marking
Set up for when libuv eventually supports ECN marking. Pass the ECN marking stuff into ngtcp2. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode:Opus 4.6 PR-URL: #63267 Backport-PR-URL: #64675 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 80e6bf9 commit 1592a11

6 files changed

Lines changed: 77 additions & 24 deletions

File tree

‎src/quic/application.cc‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,12 @@ ssize_t Session::Application::TryWritePendingDatagram(PathStorage* path,
262262
int accepted = 0;
263263
int dg_flags = NGTCP2_WRITE_DATAGRAM_FLAG_MORE;
264264

265+
// PacketInfo for the datagram path. When libuv gains per-socket ECN
266+
// marking, the value from ngtcp2 should be forwarded to the send path.
267+
PacketInfo dg_pi;
265268
ssize_t dg_nwrite = ngtcp2_conn_writev_datagram(*session_,
266269
&path->path,
267-
nullptr,
270+
dg_pi,
268271
dest,
269272
destlen,
270273
&accepted,
@@ -390,12 +393,14 @@ void Session::Application::SendPendingData() {
390393
};
391394

392395
// Accumulate a completed packet into the batch.
393-
auto enqueue_packet = [&](Packet::Ptr& pkt, size_t len) {
394-
Debug(session_, "Enqueuing packet with %zu bytes into batch", len);
395-
pkt->Truncate(len);
396-
path.CopyTo(&batch_paths[batch_count]);
397-
batch[batch_count++] = std::move(pkt);
398-
};
396+
auto enqueue_packet =
397+
[&](Packet::Ptr& pkt, size_t len, const PacketInfo& pi) {
398+
Debug(session_, "Enqueuing packet with %zu bytes into batch", len);
399+
pkt->Truncate(len);
400+
pkt->set_pkt_info(pi);
401+
path.CopyTo(&batch_paths[batch_count]);
402+
batch[batch_count++] = std::move(pkt);
403+
};
399404

400405
// We're going to enter a loop here to prepare and send no more than
401406
// max_packet_count packets.
@@ -434,8 +439,9 @@ void Session::Application::SendPendingData() {
434439
}
435440

436441
// Awesome, let's write our packet!
442+
PacketInfo pi;
437443
ssize_t nwrite = WriteVStream(
438-
&path, packet->data(), &ndatalen, packet->length(), stream_data);
444+
&path, &pi, packet->data(), &ndatalen, packet->length(), stream_data);
439445

440446
// When ndatalen is > 0, that's our indication that stream data was accepted
441447
// in to the packet. Yay!
@@ -531,7 +537,7 @@ void Session::Application::SendPendingData() {
531537
if (result > 0) {
532538
size_t len = result;
533539
Debug(session_, "Sending packet with %zu bytes", len);
534-
enqueue_packet(packet, len);
540+
enqueue_packet(packet, len, pi);
535541
if (++packet_send_count == max_packet_count) return;
536542
} elseif (result < 0) {
537543
// Any negative result other than NGTCP2_ERR_WRITE_MORE
@@ -568,7 +574,7 @@ void Session::Application::SendPendingData() {
568574
// is the size of the packet we are sending.
569575
size_t len = nwrite;
570576
Debug(session_, "Sending packet with %zu bytes", len);
571-
enqueue_packet(packet, len);
577+
enqueue_packet(packet, len, pi);
572578
if (++packet_send_count == max_packet_count) return;
573579

574580
// If there are pending datagrams, try sending them in a fresh packet.
@@ -587,7 +593,7 @@ void Session::Application::SendPendingData() {
587593
TryWritePendingDatagram(&path, packet->data(), packet->length());
588594
if (result > 0) {
589595
Debug(session_, "Sending datagram packet with %zd bytes", result);
590-
enqueue_packet(packet, static_cast<size_t>(result));
596+
enqueue_packet(packet, static_cast<size_t>(result), PacketInfo());
591597
if (++packet_send_count == max_packet_count) return;
592598
} elseif (result < 0 && result != NGTCP2_ERR_WRITE_MORE) {
593599
// Fatal error — session already closed by TryWritePendingDatagram.
@@ -600,17 +606,20 @@ void Session::Application::SendPendingData() {
600606
}
601607

602608
ssize_tSession::Application::WriteVStream(PathStorage* path,
609+
PacketInfo* pi,
603610
uint8_t* dest,
604611
ssize_t* ndatalen,
605612
size_t max_packet_size,
606613
const StreamData& stream_data) {
607614
DCHECK_LE(stream_data.count, kMaxVectorCount);
608615
uint32_t flags = NGTCP2_WRITE_STREAM_FLAG_MORE;
609616
if (stream_data.fin) flags |= NGTCP2_WRITE_STREAM_FLAG_FIN;
617+
// The PacketInfo out-param is populated by ngtcp2 with the ECN codepoint
618+
// to apply when sending this packet. When libuv gains per-socket ECN
619+
// marking, the value should be forwarded to the send path.
610620
returnngtcp2_conn_writev_stream(*session_,
611621
&path->path,
612-
// TODO(@jasnell): ECN blocked on libuv
613-
nullptr,
622+
*pi,
614623
dest,
615624
max_packet_size,
616625
ndatalen,

‎src/quic/application.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,11 @@ class Session::Application : public MemoryRetainer {
269269
uint8_t* dest,
270270
size_t destlen);
271271

272-
// Write the given stream_data into the buffer.
272+
// Write the given stream_data into the buffer. The PacketInfo out-param
273+
// is populated by ngtcp2 with per-packet metadata (e.g., ECN codepoint)
274+
// that should be applied when sending the packet.
273275
ssize_tWriteVStream(PathStorage* path,
276+
PacketInfo* pi,
274277
uint8_t* buf,
275278
ssize_t* ndatalen,
276279
size_t max_packet_size,

‎src/quic/data.h‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,40 @@ namespace node::quic {
1919
template <typename T>
2020
concept OneByteType = sizeof(T) == 1;
2121

22+
// Lightweight wrapper around ngtcp2_pkt_info. Insulates the Node.js QUIC
23+
// code from the ngtcp2 struct layout and provides a clean API boundary
24+
// for per-packet metadata (currently ECN codepoint; may grow as ngtcp2
25+
// and libuv evolve).
26+
//
27+
// Default-constructed PacketInfo is zero-initialized, which ngtcp2 treats
28+
// as ECN Not-ECT — identical to passing nullptr for the pkt_info parameter.
29+
classPacketInfofinal {
30+
public:
31+
// ECN codepoints as defined by RFC 3168.
32+
enumclassEcn : uint32_t {
33+
NOT_ECT = 0, // Not ECN-Capable Transport
34+
ECT_1 = 1, // ECN-Capable Transport(1)
35+
ECT_0 = 2, // ECN-Capable Transport(0)
36+
CE = 3, // Congestion Experienced
37+
};
38+
39+
PacketInfo() : info_{} {}
40+
explicitPacketInfo(const ngtcp2_pkt_info& info) : info_(info) {}
41+
42+
// ECN codepoint for this packet. When libuv gains per-packet ECN
43+
// reporting, populate via set_ecn() from the receive metadata
44+
// before passing to ReadPacket().
45+
Ecn ecn() const { returnstatic_cast<Ecn>(info_.ecn); }
46+
voidset_ecn(Ecn ecn) { info_.ecn = static_cast<uint32_t>(ecn); }
47+
48+
// Conversion operators for ngtcp2 API calls.
49+
operatorconst ngtcp2_pkt_info*() const { return &info_; }
50+
operator ngtcp2_pkt_info*() { return &info_; }
51+
52+
private:
53+
ngtcp2_pkt_info info_;
54+
};
55+
2256
structPathfinal : public ngtcp2_path {
2357
explicitPath(const SocketAddress& local, const SocketAddress& remote);
2458
Path(Path&& other) noexcept = default;

‎src/quic/packet.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class Packet final {
6868
size_tlength() const { return length_; }
6969
size_tcapacity() const { return capacity_; }
7070
const SocketAddress& destination() const { return destination_; }
71+
const PacketInfo& pkt_info() const { return pkt_info_; }
72+
voidset_pkt_info(const PacketInfo& pi) { pkt_info_ = pi; }
7173
Listener* listener() const { return listener_; }
7274

7375
// Redirect the packet to a different endpoint for cross-endpoint sends
@@ -148,6 +150,7 @@ class Packet final {
148150
Listener* listener_;
149151

150152
// Touched at send time.
153+
PacketInfo pkt_info_;
151154
SocketAddress destination_;
152155

153156
// Only touched by libuv during uv_udp_send and in the send callback.

‎src/quic/session.cc‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,19 +2111,21 @@ void Session::SetLastError(QuicError&& error) {
21112111

21122112
boolSession::Receive(Store&& store,
21132113
const SocketAddress& local_address,
2114-
const SocketAddress& remote_address) {
2114+
const SocketAddress& remote_address,
2115+
const PacketInfo& pkt_info) {
21152116
// Convenience wrapper: reads the packet and immediately triggers
21162117
// SendPendingData. Used by paths that need an immediate response
21172118
// (e.g., Endpoint::Connect for client Initial packets).
21182119
// The hot receive path uses ReadPacket() directly with deferred
21192120
// flush via BindingData's uv_check callback.
21202121
SendPendingDataScope send_scope(this);
2121-
returnReadPacket(std::move(store), local_address, remote_address);
2122+
returnReadPacket(std::move(store), local_address, remote_address, pkt_info);
21222123
}
21232124

21242125
boolSession::ReadPacket(Store&& store,
21252126
const SocketAddress& local_address,
2126-
const SocketAddress& remote_address) {
2127+
const SocketAddress& remote_address,
2128+
const PacketInfo& pkt_info) {
21272129
DCHECK(!is_destroyed());
21282130
impl_->remote_address_ = remote_address;
21292131

@@ -2145,12 +2147,12 @@ bool Session::ReadPacket(Store&& store,
21452147
int err;
21462148
{
21472149
NgTcp2CallbackScope callback_scope(this);
2148-
//ECN codepoint (ngtcp2_pkt_info.ecn) is not yet populated because
2149-
// libuv does not currently deliver per-packet ECN metadata. When
2150-
//libuv gains ECN receive reporting, the pkt_info should be
2151-
//populated from the per-packet metadata and passed through here.
2150+
//The PacketInfo carries per-packet metadata (currently ECN codepoint).
2151+
//When libuv gains per-packet ECN reporting, the caller should
2152+
//populate pkt_info from the receive metadata before calling
2153+
//ReadPacket().
21522154
err = ngtcp2_conn_read_pkt(
2153-
*this, &path, nullptr, vec.base, vec.len, uv_hrtime());
2155+
*this, &path, pkt_info, vec.base, vec.len, uv_hrtime());
21542156
}
21552157
if (is_destroyed()) returnfalse;
21562158

‎src/quic/session.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
355355

356356
boolReceive(Store&& store,
357357
const SocketAddress& local_address,
358-
const SocketAddress& remote_address);
358+
const SocketAddress& remote_address,
359+
const PacketInfo& pkt_info = PacketInfo());
359360

360361
// ReadPacket processes a single inbound packet through ngtcp2 without
361362
// triggering SendPendingData. This is the building block for batched
@@ -367,7 +368,8 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
367368
// immediate response).
368369
boolReadPacket(Store&& store,
369370
const SocketAddress& local_address,
370-
const SocketAddress& remote_address);
371+
const SocketAddress& remote_address,
372+
const PacketInfo& pkt_info = PacketInfo());
371373

372374
// Called by BindingData's flush callback to trigger SendPendingData
373375
// on this session. Encapsulates the application() access so that

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Commit 1592a11

Browse files
jasnelladuh95
authored andcommitted
quic: add support for future ECN marking
Set up for when libuv eventually supports ECN marking. Pass the ECN marking stuff into ngtcp2. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode:Opus 4.6 PR-URL: #63267 Backport-PR-URL: #64675 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 80e6bf9 commit 1592a11

6 files changed

Lines changed: 77 additions & 24 deletions

File tree

‎src/quic/application.cc‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,12 @@ ssize_t Session::Application::TryWritePendingDatagram(PathStorage* path,
262262
int accepted = 0;
263263
int dg_flags = NGTCP2_WRITE_DATAGRAM_FLAG_MORE;
264264

265+
// PacketInfo for the datagram path. When libuv gains per-socket ECN
266+
// marking, the value from ngtcp2 should be forwarded to the send path.
267+
PacketInfo dg_pi;
265268
ssize_t dg_nwrite = ngtcp2_conn_writev_datagram(*session_,
266269
&path->path,
267-
nullptr,
270+
dg_pi,
268271
dest,
269272
destlen,
270273
&accepted,
@@ -390,12 +393,14 @@ void Session::Application::SendPendingData() {
390393
};
391394

392395
// Accumulate a completed packet into the batch.
393-
auto enqueue_packet = [&](Packet::Ptr& pkt, size_t len) {
394-
Debug(session_, "Enqueuing packet with %zu bytes into batch", len);
395-
pkt->Truncate(len);
396-
path.CopyTo(&batch_paths[batch_count]);
397-
batch[batch_count++] = std::move(pkt);
398-
};
396+
auto enqueue_packet =
397+
[&](Packet::Ptr& pkt, size_t len, const PacketInfo& pi) {
398+
Debug(session_, "Enqueuing packet with %zu bytes into batch", len);
399+
pkt->Truncate(len);
400+
pkt->set_pkt_info(pi);
401+
path.CopyTo(&batch_paths[batch_count]);
402+
batch[batch_count++] = std::move(pkt);
403+
};
399404

400405
// We're going to enter a loop here to prepare and send no more than
401406
// max_packet_count packets.
@@ -434,8 +439,9 @@ void Session::Application::SendPendingData() {
434439
}
435440

436441
// Awesome, let's write our packet!
442+
PacketInfo pi;
437443
ssize_t nwrite = WriteVStream(
438-
&path, packet->data(), &ndatalen, packet->length(), stream_data);
444+
&path, &pi, packet->data(), &ndatalen, packet->length(), stream_data);
439445

440446
// When ndatalen is > 0, that's our indication that stream data was accepted
441447
// in to the packet. Yay!
@@ -531,7 +537,7 @@ void Session::Application::SendPendingData() {
531537
if (result > 0) {
532538
size_t len = result;
533539
Debug(session_, "Sending packet with %zu bytes", len);
534-
enqueue_packet(packet, len);
540+
enqueue_packet(packet, len, pi);
535541
if (++packet_send_count == max_packet_count) return;
536542
} elseif (result < 0) {
537543
// Any negative result other than NGTCP2_ERR_WRITE_MORE
@@ -568,7 +574,7 @@ void Session::Application::SendPendingData() {
568574
// is the size of the packet we are sending.
569575
size_t len = nwrite;
570576
Debug(session_, "Sending packet with %zu bytes", len);
571-
enqueue_packet(packet, len);
577+
enqueue_packet(packet, len, pi);
572578
if (++packet_send_count == max_packet_count) return;
573579

574580
// If there are pending datagrams, try sending them in a fresh packet.
@@ -587,7 +593,7 @@ void Session::Application::SendPendingData() {
587593
TryWritePendingDatagram(&path, packet->data(), packet->length());
588594
if (result > 0) {
589595
Debug(session_, "Sending datagram packet with %zd bytes", result);
590-
enqueue_packet(packet, static_cast<size_t>(result));
596+
enqueue_packet(packet, static_cast<size_t>(result), PacketInfo());
591597
if (++packet_send_count == max_packet_count) return;
592598
} elseif (result < 0 && result != NGTCP2_ERR_WRITE_MORE) {
593599
// Fatal error — session already closed by TryWritePendingDatagram.
@@ -600,17 +606,20 @@ void Session::Application::SendPendingData() {
600606
}
601607

602608
ssize_tSession::Application::WriteVStream(PathStorage* path,
609+
PacketInfo* pi,
603610
uint8_t* dest,
604611
ssize_t* ndatalen,
605612
size_t max_packet_size,
606613
const StreamData& stream_data) {
607614
DCHECK_LE(stream_data.count, kMaxVectorCount);
608615
uint32_t flags = NGTCP2_WRITE_STREAM_FLAG_MORE;
609616
if (stream_data.fin) flags |= NGTCP2_WRITE_STREAM_FLAG_FIN;
617+
// The PacketInfo out-param is populated by ngtcp2 with the ECN codepoint
618+
// to apply when sending this packet. When libuv gains per-socket ECN
619+
// marking, the value should be forwarded to the send path.
610620
returnngtcp2_conn_writev_stream(*session_,
611621
&path->path,
612-
// TODO(@jasnell): ECN blocked on libuv
613-
nullptr,
622+
*pi,
614623
dest,
615624
max_packet_size,
616625
ndatalen,

‎src/quic/application.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,11 @@ class Session::Application : public MemoryRetainer {
269269
uint8_t* dest,
270270
size_t destlen);
271271

272-
// Write the given stream_data into the buffer.
272+
// Write the given stream_data into the buffer. The PacketInfo out-param
273+
// is populated by ngtcp2 with per-packet metadata (e.g., ECN codepoint)
274+
// that should be applied when sending the packet.
273275
ssize_tWriteVStream(PathStorage* path,
276+
PacketInfo* pi,
274277
uint8_t* buf,
275278
ssize_t* ndatalen,
276279
size_t max_packet_size,

‎src/quic/data.h‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,40 @@ namespace node::quic {
1919
template <typename T>
2020
concept OneByteType = sizeof(T) == 1;
2121

22+
// Lightweight wrapper around ngtcp2_pkt_info. Insulates the Node.js QUIC
23+
// code from the ngtcp2 struct layout and provides a clean API boundary
24+
// for per-packet metadata (currently ECN codepoint; may grow as ngtcp2
25+
// and libuv evolve).
26+
//
27+
// Default-constructed PacketInfo is zero-initialized, which ngtcp2 treats
28+
// as ECN Not-ECT — identical to passing nullptr for the pkt_info parameter.
29+
classPacketInfofinal {
30+
public:
31+
// ECN codepoints as defined by RFC 3168.
32+
enumclassEcn : uint32_t {
33+
NOT_ECT = 0, // Not ECN-Capable Transport
34+
ECT_1 = 1, // ECN-Capable Transport(1)
35+
ECT_0 = 2, // ECN-Capable Transport(0)
36+
CE = 3, // Congestion Experienced
37+
};
38+
39+
PacketInfo() : info_{} {}
40+
explicitPacketInfo(const ngtcp2_pkt_info& info) : info_(info) {}
41+
42+
// ECN codepoint for this packet. When libuv gains per-packet ECN
43+
// reporting, populate via set_ecn() from the receive metadata
44+
// before passing to ReadPacket().
45+
Ecn ecn() const { returnstatic_cast<Ecn>(info_.ecn); }
46+
voidset_ecn(Ecn ecn) { info_.ecn = static_cast<uint32_t>(ecn); }
47+
48+
// Conversion operators for ngtcp2 API calls.
49+
operatorconst ngtcp2_pkt_info*() const { return &info_; }
50+
operator ngtcp2_pkt_info*() { return &info_; }
51+
52+
private:
53+
ngtcp2_pkt_info info_;
54+
};
55+
2256
structPathfinal : public ngtcp2_path {
2357
explicitPath(const SocketAddress& local, const SocketAddress& remote);
2458
Path(Path&& other) noexcept = default;

‎src/quic/packet.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class Packet final {
6868
size_tlength() const { return length_; }
6969
size_tcapacity() const { return capacity_; }
7070
const SocketAddress& destination() const { return destination_; }
71+
const PacketInfo& pkt_info() const { return pkt_info_; }
72+
voidset_pkt_info(const PacketInfo& pi) { pkt_info_ = pi; }
7173
Listener* listener() const { return listener_; }
7274

7375
// Redirect the packet to a different endpoint for cross-endpoint sends
@@ -148,6 +150,7 @@ class Packet final {
148150
Listener* listener_;
149151

150152
// Touched at send time.
153+
PacketInfo pkt_info_;
151154
SocketAddress destination_;
152155

153156
// Only touched by libuv during uv_udp_send and in the send callback.

‎src/quic/session.cc‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,19 +2111,21 @@ void Session::SetLastError(QuicError&& error) {
21112111

21122112
boolSession::Receive(Store&& store,
21132113
const SocketAddress& local_address,
2114-
const SocketAddress& remote_address) {
2114+
const SocketAddress& remote_address,
2115+
const PacketInfo& pkt_info) {
21152116
// Convenience wrapper: reads the packet and immediately triggers
21162117
// SendPendingData. Used by paths that need an immediate response
21172118
// (e.g., Endpoint::Connect for client Initial packets).
21182119
// The hot receive path uses ReadPacket() directly with deferred
21192120
// flush via BindingData's uv_check callback.
21202121
SendPendingDataScope send_scope(this);
2121-
returnReadPacket(std::move(store), local_address, remote_address);
2122+
returnReadPacket(std::move(store), local_address, remote_address, pkt_info);
21222123
}
21232124

21242125
boolSession::ReadPacket(Store&& store,
21252126
const SocketAddress& local_address,
2126-
const SocketAddress& remote_address) {
2127+
const SocketAddress& remote_address,
2128+
const PacketInfo& pkt_info) {
21272129
DCHECK(!is_destroyed());
21282130
impl_->remote_address_ = remote_address;
21292131

@@ -2145,12 +2147,12 @@ bool Session::ReadPacket(Store&& store,
21452147
int err;
21462148
{
21472149
NgTcp2CallbackScope callback_scope(this);
2148-
//ECN codepoint (ngtcp2_pkt_info.ecn) is not yet populated because
2149-
// libuv does not currently deliver per-packet ECN metadata. When
2150-
//libuv gains ECN receive reporting, the pkt_info should be
2151-
//populated from the per-packet metadata and passed through here.
2150+
//The PacketInfo carries per-packet metadata (currently ECN codepoint).
2151+
//When libuv gains per-packet ECN reporting, the caller should
2152+
//populate pkt_info from the receive metadata before calling
2153+
//ReadPacket().
21522154
err = ngtcp2_conn_read_pkt(
2153-
*this, &path, nullptr, vec.base, vec.len, uv_hrtime());
2155+
*this, &path, pkt_info, vec.base, vec.len, uv_hrtime());
21542156
}
21552157
if (is_destroyed()) returnfalse;
21562158

‎src/quic/session.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
355355

356356
boolReceive(Store&& store,
357357
const SocketAddress& local_address,
358-
const SocketAddress& remote_address);
358+
const SocketAddress& remote_address,
359+
const PacketInfo& pkt_info = PacketInfo());
359360

360361
// ReadPacket processes a single inbound packet through ngtcp2 without
361362
// triggering SendPendingData. This is the building block for batched
@@ -367,7 +368,8 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
367368
// immediate response).
368369
boolReadPacket(Store&& store,
369370
const SocketAddress& local_address,
370-
const SocketAddress& remote_address);
371+
const SocketAddress& remote_address,
372+
const PacketInfo& pkt_info = PacketInfo());
371373

372374
// Called by BindingData's flush callback to trigger SendPendingData
373375
// on this session. Encapsulates the application() access so that

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 1592a11

Browse files
jasnelladuh95
authored andcommitted
quic: add support for future ECN marking
Set up for when libuv eventually supports ECN marking. Pass the ECN marking stuff into ngtcp2. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode:Opus 4.6 PR-URL: #63267 Backport-PR-URL: #64675 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 80e6bf9 commit 1592a11

6 files changed

Lines changed: 77 additions & 24 deletions

File tree

‎src/quic/application.cc‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,12 @@ ssize_t Session::Application::TryWritePendingDatagram(PathStorage* path,
262262
int accepted = 0;
263263
int dg_flags = NGTCP2_WRITE_DATAGRAM_FLAG_MORE;
264264

265+
// PacketInfo for the datagram path. When libuv gains per-socket ECN
266+
// marking, the value from ngtcp2 should be forwarded to the send path.
267+
PacketInfo dg_pi;
265268
ssize_t dg_nwrite = ngtcp2_conn_writev_datagram(*session_,
266269
&path->path,
267-
nullptr,
270+
dg_pi,
268271
dest,
269272
destlen,
270273
&accepted,
@@ -390,12 +393,14 @@ void Session::Application::SendPendingData() {
390393
};
391394

392395
// Accumulate a completed packet into the batch.
393-
auto enqueue_packet = [&](Packet::Ptr& pkt, size_t len) {
394-
Debug(session_, "Enqueuing packet with %zu bytes into batch", len);
395-
pkt->Truncate(len);
396-
path.CopyTo(&batch_paths[batch_count]);
397-
batch[batch_count++] = std::move(pkt);
398-
};
396+
auto enqueue_packet =
397+
[&](Packet::Ptr& pkt, size_t len, const PacketInfo& pi) {
398+
Debug(session_, "Enqueuing packet with %zu bytes into batch", len);
399+
pkt->Truncate(len);
400+
pkt->set_pkt_info(pi);
401+
path.CopyTo(&batch_paths[batch_count]);
402+
batch[batch_count++] = std::move(pkt);
403+
};
399404

400405
// We're going to enter a loop here to prepare and send no more than
401406
// max_packet_count packets.
@@ -434,8 +439,9 @@ void Session::Application::SendPendingData() {
434439
}
435440

436441
// Awesome, let's write our packet!
442+
PacketInfo pi;
437443
ssize_t nwrite = WriteVStream(
438-
&path, packet->data(), &ndatalen, packet->length(), stream_data);
444+
&path, &pi, packet->data(), &ndatalen, packet->length(), stream_data);
439445

440446
// When ndatalen is > 0, that's our indication that stream data was accepted
441447
// in to the packet. Yay!
@@ -531,7 +537,7 @@ void Session::Application::SendPendingData() {
531537
if (result > 0) {
532538
size_t len = result;
533539
Debug(session_, "Sending packet with %zu bytes", len);
534-
enqueue_packet(packet, len);
540+
enqueue_packet(packet, len, pi);
535541
if (++packet_send_count == max_packet_count) return;
536542
} elseif (result < 0) {
537543
// Any negative result other than NGTCP2_ERR_WRITE_MORE
@@ -568,7 +574,7 @@ void Session::Application::SendPendingData() {
568574
// is the size of the packet we are sending.
569575
size_t len = nwrite;
570576
Debug(session_, "Sending packet with %zu bytes", len);
571-
enqueue_packet(packet, len);
577+
enqueue_packet(packet, len, pi);
572578
if (++packet_send_count == max_packet_count) return;
573579

574580
// If there are pending datagrams, try sending them in a fresh packet.
@@ -587,7 +593,7 @@ void Session::Application::SendPendingData() {
587593
TryWritePendingDatagram(&path, packet->data(), packet->length());
588594
if (result > 0) {
589595
Debug(session_, "Sending datagram packet with %zd bytes", result);
590-
enqueue_packet(packet, static_cast<size_t>(result));
596+
enqueue_packet(packet, static_cast<size_t>(result), PacketInfo());
591597
if (++packet_send_count == max_packet_count) return;
592598
} elseif (result < 0 && result != NGTCP2_ERR_WRITE_MORE) {
593599
// Fatal error — session already closed by TryWritePendingDatagram.
@@ -600,17 +606,20 @@ void Session::Application::SendPendingData() {
600606
}
601607

602608
ssize_tSession::Application::WriteVStream(PathStorage* path,
609+
PacketInfo* pi,
603610
uint8_t* dest,
604611
ssize_t* ndatalen,
605612
size_t max_packet_size,
606613
const StreamData& stream_data) {
607614
DCHECK_LE(stream_data.count, kMaxVectorCount);
608615
uint32_t flags = NGTCP2_WRITE_STREAM_FLAG_MORE;
609616
if (stream_data.fin) flags |= NGTCP2_WRITE_STREAM_FLAG_FIN;
617+
// The PacketInfo out-param is populated by ngtcp2 with the ECN codepoint
618+
// to apply when sending this packet. When libuv gains per-socket ECN
619+
// marking, the value should be forwarded to the send path.
610620
returnngtcp2_conn_writev_stream(*session_,
611621
&path->path,
612-
// TODO(@jasnell): ECN blocked on libuv
613-
nullptr,
622+
*pi,
614623
dest,
615624
max_packet_size,
616625
ndatalen,

‎src/quic/application.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,11 @@ class Session::Application : public MemoryRetainer {
269269
uint8_t* dest,
270270
size_t destlen);
271271

272-
// Write the given stream_data into the buffer.
272+
// Write the given stream_data into the buffer. The PacketInfo out-param
273+
// is populated by ngtcp2 with per-packet metadata (e.g., ECN codepoint)
274+
// that should be applied when sending the packet.
273275
ssize_tWriteVStream(PathStorage* path,
276+
PacketInfo* pi,
274277
uint8_t* buf,
275278
ssize_t* ndatalen,
276279
size_t max_packet_size,

‎src/quic/data.h‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,40 @@ namespace node::quic {
1919
template <typename T>
2020
concept OneByteType = sizeof(T) == 1;
2121

22+
// Lightweight wrapper around ngtcp2_pkt_info. Insulates the Node.js QUIC
23+
// code from the ngtcp2 struct layout and provides a clean API boundary
24+
// for per-packet metadata (currently ECN codepoint; may grow as ngtcp2
25+
// and libuv evolve).
26+
//
27+
// Default-constructed PacketInfo is zero-initialized, which ngtcp2 treats
28+
// as ECN Not-ECT — identical to passing nullptr for the pkt_info parameter.
29+
classPacketInfofinal {
30+
public:
31+
// ECN codepoints as defined by RFC 3168.
32+
enumclassEcn : uint32_t {
33+
NOT_ECT = 0, // Not ECN-Capable Transport
34+
ECT_1 = 1, // ECN-Capable Transport(1)
35+
ECT_0 = 2, // ECN-Capable Transport(0)
36+
CE = 3, // Congestion Experienced
37+
};
38+
39+
PacketInfo() : info_{} {}
40+
explicitPacketInfo(const ngtcp2_pkt_info& info) : info_(info) {}
41+
42+
// ECN codepoint for this packet. When libuv gains per-packet ECN
43+
// reporting, populate via set_ecn() from the receive metadata
44+
// before passing to ReadPacket().
45+
Ecn ecn() const { returnstatic_cast<Ecn>(info_.ecn); }
46+
voidset_ecn(Ecn ecn) { info_.ecn = static_cast<uint32_t>(ecn); }
47+
48+
// Conversion operators for ngtcp2 API calls.
49+
operatorconst ngtcp2_pkt_info*() const { return &info_; }
50+
operator ngtcp2_pkt_info*() { return &info_; }
51+
52+
private:
53+
ngtcp2_pkt_info info_;
54+
};
55+
2256
structPathfinal : public ngtcp2_path {
2357
explicitPath(const SocketAddress& local, const SocketAddress& remote);
2458
Path(Path&& other) noexcept = default;

‎src/quic/packet.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class Packet final {
6868
size_tlength() const { return length_; }
6969
size_tcapacity() const { return capacity_; }
7070
const SocketAddress& destination() const { return destination_; }
71+
const PacketInfo& pkt_info() const { return pkt_info_; }
72+
voidset_pkt_info(const PacketInfo& pi) { pkt_info_ = pi; }
7173
Listener* listener() const { return listener_; }
7274

7375
// Redirect the packet to a different endpoint for cross-endpoint sends
@@ -148,6 +150,7 @@ class Packet final {
148150
Listener* listener_;
149151

150152
// Touched at send time.
153+
PacketInfo pkt_info_;
151154
SocketAddress destination_;
152155

153156
// Only touched by libuv during uv_udp_send and in the send callback.

‎src/quic/session.cc‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,19 +2111,21 @@ void Session::SetLastError(QuicError&& error) {
21112111

21122112
boolSession::Receive(Store&& store,
21132113
const SocketAddress& local_address,
2114-
const SocketAddress& remote_address) {
2114+
const SocketAddress& remote_address,
2115+
const PacketInfo& pkt_info) {
21152116
// Convenience wrapper: reads the packet and immediately triggers
21162117
// SendPendingData. Used by paths that need an immediate response
21172118
// (e.g., Endpoint::Connect for client Initial packets).
21182119
// The hot receive path uses ReadPacket() directly with deferred
21192120
// flush via BindingData's uv_check callback.
21202121
SendPendingDataScope send_scope(this);
2121-
returnReadPacket(std::move(store), local_address, remote_address);
2122+
returnReadPacket(std::move(store), local_address, remote_address, pkt_info);
21222123
}
21232124

21242125
boolSession::ReadPacket(Store&& store,
21252126
const SocketAddress& local_address,
2126-
const SocketAddress& remote_address) {
2127+
const SocketAddress& remote_address,
2128+
const PacketInfo& pkt_info) {
21272129
DCHECK(!is_destroyed());
21282130
impl_->remote_address_ = remote_address;
21292131

@@ -2145,12 +2147,12 @@ bool Session::ReadPacket(Store&& store,
21452147
int err;
21462148
{
21472149
NgTcp2CallbackScope callback_scope(this);
2148-
//ECN codepoint (ngtcp2_pkt_info.ecn) is not yet populated because
2149-
// libuv does not currently deliver per-packet ECN metadata. When
2150-
//libuv gains ECN receive reporting, the pkt_info should be
2151-
//populated from the per-packet metadata and passed through here.
2150+
//The PacketInfo carries per-packet metadata (currently ECN codepoint).
2151+
//When libuv gains per-packet ECN reporting, the caller should
2152+
//populate pkt_info from the receive metadata before calling
2153+
//ReadPacket().
21522154
err = ngtcp2_conn_read_pkt(
2153-
*this, &path, nullptr, vec.base, vec.len, uv_hrtime());
2155+
*this, &path, pkt_info, vec.base, vec.len, uv_hrtime());
21542156
}
21552157
if (is_destroyed()) returnfalse;
21562158

‎src/quic/session.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
355355

356356
boolReceive(Store&& store,
357357
const SocketAddress& local_address,
358-
const SocketAddress& remote_address);
358+
const SocketAddress& remote_address,
359+
const PacketInfo& pkt_info = PacketInfo());
359360

360361
// ReadPacket processes a single inbound packet through ngtcp2 without
361362
// triggering SendPendingData. This is the building block for batched
@@ -367,7 +368,8 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
367368
// immediate response).
368369
boolReadPacket(Store&& store,
369370
const SocketAddress& local_address,
370-
const SocketAddress& remote_address);
371+
const SocketAddress& remote_address,
372+
const PacketInfo& pkt_info = PacketInfo());
371373

372374
// Called by BindingData's flush callback to trigger SendPendingData
373375
// on this session. Encapsulates the application() access so that

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 1592a11

Browse files
jasnelladuh95
authored andcommitted
quic: add support for future ECN marking
Set up for when libuv eventually supports ECN marking. Pass the ECN marking stuff into ngtcp2. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode:Opus 4.6 PR-URL: #63267 Backport-PR-URL: #64675 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 80e6bf9 commit 1592a11

6 files changed

Lines changed: 77 additions & 24 deletions

File tree

‎src/quic/application.cc‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,12 @@ ssize_t Session::Application::TryWritePendingDatagram(PathStorage* path,
262262
int accepted = 0;
263263
int dg_flags = NGTCP2_WRITE_DATAGRAM_FLAG_MORE;
264264

265+
// PacketInfo for the datagram path. When libuv gains per-socket ECN
266+
// marking, the value from ngtcp2 should be forwarded to the send path.
267+
PacketInfo dg_pi;
265268
ssize_t dg_nwrite = ngtcp2_conn_writev_datagram(*session_,
266269
&path->path,
267-
nullptr,
270+
dg_pi,
268271
dest,
269272
destlen,
270273
&accepted,
@@ -390,12 +393,14 @@ void Session::Application::SendPendingData() {
390393
};
391394

392395
// Accumulate a completed packet into the batch.
393-
auto enqueue_packet = [&](Packet::Ptr& pkt, size_t len) {
394-
Debug(session_, "Enqueuing packet with %zu bytes into batch", len);
395-
pkt->Truncate(len);
396-
path.CopyTo(&batch_paths[batch_count]);
397-
batch[batch_count++] = std::move(pkt);
398-
};
396+
auto enqueue_packet =
397+
[&](Packet::Ptr& pkt, size_t len, const PacketInfo& pi) {
398+
Debug(session_, "Enqueuing packet with %zu bytes into batch", len);
399+
pkt->Truncate(len);
400+
pkt->set_pkt_info(pi);
401+
path.CopyTo(&batch_paths[batch_count]);
402+
batch[batch_count++] = std::move(pkt);
403+
};
399404

400405
// We're going to enter a loop here to prepare and send no more than
401406
// max_packet_count packets.
@@ -434,8 +439,9 @@ void Session::Application::SendPendingData() {
434439
}
435440

436441
// Awesome, let's write our packet!
442+
PacketInfo pi;
437443
ssize_t nwrite = WriteVStream(
438-
&path, packet->data(), &ndatalen, packet->length(), stream_data);
444+
&path, &pi, packet->data(), &ndatalen, packet->length(), stream_data);
439445

440446
// When ndatalen is > 0, that's our indication that stream data was accepted
441447
// in to the packet. Yay!
@@ -531,7 +537,7 @@ void Session::Application::SendPendingData() {
531537
if (result > 0) {
532538
size_t len = result;
533539
Debug(session_, "Sending packet with %zu bytes", len);
534-
enqueue_packet(packet, len);
540+
enqueue_packet(packet, len, pi);
535541
if (++packet_send_count == max_packet_count) return;
536542
} elseif (result < 0) {
537543
// Any negative result other than NGTCP2_ERR_WRITE_MORE
@@ -568,7 +574,7 @@ void Session::Application::SendPendingData() {
568574
// is the size of the packet we are sending.
569575
size_t len = nwrite;
570576
Debug(session_, "Sending packet with %zu bytes", len);
571-
enqueue_packet(packet, len);
577+
enqueue_packet(packet, len, pi);
572578
if (++packet_send_count == max_packet_count) return;
573579

574580
// If there are pending datagrams, try sending them in a fresh packet.
@@ -587,7 +593,7 @@ void Session::Application::SendPendingData() {
587593
TryWritePendingDatagram(&path, packet->data(), packet->length());
588594
if (result > 0) {
589595
Debug(session_, "Sending datagram packet with %zd bytes", result);
590-
enqueue_packet(packet, static_cast<size_t>(result));
596+
enqueue_packet(packet, static_cast<size_t>(result), PacketInfo());
591597
if (++packet_send_count == max_packet_count) return;
592598
} elseif (result < 0 && result != NGTCP2_ERR_WRITE_MORE) {
593599
// Fatal error — session already closed by TryWritePendingDatagram.
@@ -600,17 +606,20 @@ void Session::Application::SendPendingData() {
600606
}
601607

602608
ssize_tSession::Application::WriteVStream(PathStorage* path,
609+
PacketInfo* pi,
603610
uint8_t* dest,
604611
ssize_t* ndatalen,
605612
size_t max_packet_size,
606613
const StreamData& stream_data) {
607614
DCHECK_LE(stream_data.count, kMaxVectorCount);
608615
uint32_t flags = NGTCP2_WRITE_STREAM_FLAG_MORE;
609616
if (stream_data.fin) flags |= NGTCP2_WRITE_STREAM_FLAG_FIN;
617+
// The PacketInfo out-param is populated by ngtcp2 with the ECN codepoint
618+
// to apply when sending this packet. When libuv gains per-socket ECN
619+
// marking, the value should be forwarded to the send path.
610620
returnngtcp2_conn_writev_stream(*session_,
611621
&path->path,
612-
// TODO(@jasnell): ECN blocked on libuv
613-
nullptr,
622+
*pi,
614623
dest,
615624
max_packet_size,
616625
ndatalen,

‎src/quic/application.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,11 @@ class Session::Application : public MemoryRetainer {
269269
uint8_t* dest,
270270
size_t destlen);
271271

272-
// Write the given stream_data into the buffer.
272+
// Write the given stream_data into the buffer. The PacketInfo out-param
273+
// is populated by ngtcp2 with per-packet metadata (e.g., ECN codepoint)
274+
// that should be applied when sending the packet.
273275
ssize_tWriteVStream(PathStorage* path,
276+
PacketInfo* pi,
274277
uint8_t* buf,
275278
ssize_t* ndatalen,
276279
size_t max_packet_size,

‎src/quic/data.h‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,40 @@ namespace node::quic {
1919
template <typename T>
2020
concept OneByteType = sizeof(T) == 1;
2121

22+
// Lightweight wrapper around ngtcp2_pkt_info. Insulates the Node.js QUIC
23+
// code from the ngtcp2 struct layout and provides a clean API boundary
24+
// for per-packet metadata (currently ECN codepoint; may grow as ngtcp2
25+
// and libuv evolve).
26+
//
27+
// Default-constructed PacketInfo is zero-initialized, which ngtcp2 treats
28+
// as ECN Not-ECT — identical to passing nullptr for the pkt_info parameter.
29+
classPacketInfofinal {
30+
public:
31+
// ECN codepoints as defined by RFC 3168.
32+
enumclassEcn : uint32_t {
33+
NOT_ECT = 0, // Not ECN-Capable Transport
34+
ECT_1 = 1, // ECN-Capable Transport(1)
35+
ECT_0 = 2, // ECN-Capable Transport(0)
36+
CE = 3, // Congestion Experienced
37+
};
38+
39+
PacketInfo() : info_{} {}
40+
explicitPacketInfo(const ngtcp2_pkt_info& info) : info_(info) {}
41+
42+
// ECN codepoint for this packet. When libuv gains per-packet ECN
43+
// reporting, populate via set_ecn() from the receive metadata
44+
// before passing to ReadPacket().
45+
Ecn ecn() const { returnstatic_cast<Ecn>(info_.ecn); }
46+
voidset_ecn(Ecn ecn) { info_.ecn = static_cast<uint32_t>(ecn); }
47+
48+
// Conversion operators for ngtcp2 API calls.
49+
operatorconst ngtcp2_pkt_info*() const { return &info_; }
50+
operator ngtcp2_pkt_info*() { return &info_; }
51+
52+
private:
53+
ngtcp2_pkt_info info_;
54+
};
55+
2256
structPathfinal : public ngtcp2_path {
2357
explicitPath(const SocketAddress& local, const SocketAddress& remote);
2458
Path(Path&& other) noexcept = default;

‎src/quic/packet.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class Packet final {
6868
size_tlength() const { return length_; }
6969
size_tcapacity() const { return capacity_; }
7070
const SocketAddress& destination() const { return destination_; }
71+
const PacketInfo& pkt_info() const { return pkt_info_; }
72+
voidset_pkt_info(const PacketInfo& pi) { pkt_info_ = pi; }
7173
Listener* listener() const { return listener_; }
7274

7375
// Redirect the packet to a different endpoint for cross-endpoint sends
@@ -148,6 +150,7 @@ class Packet final {
148150
Listener* listener_;
149151

150152
// Touched at send time.
153+
PacketInfo pkt_info_;
151154
SocketAddress destination_;
152155

153156
// Only touched by libuv during uv_udp_send and in the send callback.

‎src/quic/session.cc‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,19 +2111,21 @@ void Session::SetLastError(QuicError&& error) {
21112111

21122112
boolSession::Receive(Store&& store,
21132113
const SocketAddress& local_address,
2114-
const SocketAddress& remote_address) {
2114+
const SocketAddress& remote_address,
2115+
const PacketInfo& pkt_info) {
21152116
// Convenience wrapper: reads the packet and immediately triggers
21162117
// SendPendingData. Used by paths that need an immediate response
21172118
// (e.g., Endpoint::Connect for client Initial packets).
21182119
// The hot receive path uses ReadPacket() directly with deferred
21192120
// flush via BindingData's uv_check callback.
21202121
SendPendingDataScope send_scope(this);
2121-
returnReadPacket(std::move(store), local_address, remote_address);
2122+
returnReadPacket(std::move(store), local_address, remote_address, pkt_info);
21222123
}
21232124

21242125
boolSession::ReadPacket(Store&& store,
21252126
const SocketAddress& local_address,
2126-
const SocketAddress& remote_address) {
2127+
const SocketAddress& remote_address,
2128+
const PacketInfo& pkt_info) {
21272129
DCHECK(!is_destroyed());
21282130
impl_->remote_address_ = remote_address;
21292131

@@ -2145,12 +2147,12 @@ bool Session::ReadPacket(Store&& store,
21452147
int err;
21462148
{
21472149
NgTcp2CallbackScope callback_scope(this);
2148-
//ECN codepoint (ngtcp2_pkt_info.ecn) is not yet populated because
2149-
// libuv does not currently deliver per-packet ECN metadata. When
2150-
//libuv gains ECN receive reporting, the pkt_info should be
2151-
//populated from the per-packet metadata and passed through here.
2150+
//The PacketInfo carries per-packet metadata (currently ECN codepoint).
2151+
//When libuv gains per-packet ECN reporting, the caller should
2152+
//populate pkt_info from the receive metadata before calling
2153+
//ReadPacket().
21522154
err = ngtcp2_conn_read_pkt(
2153-
*this, &path, nullptr, vec.base, vec.len, uv_hrtime());
2155+
*this, &path, pkt_info, vec.base, vec.len, uv_hrtime());
21542156
}
21552157
if (is_destroyed()) returnfalse;
21562158

‎src/quic/session.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
355355

356356
boolReceive(Store&& store,
357357
const SocketAddress& local_address,
358-
const SocketAddress& remote_address);
358+
const SocketAddress& remote_address,
359+
const PacketInfo& pkt_info = PacketInfo());
359360

360361
// ReadPacket processes a single inbound packet through ngtcp2 without
361362
// triggering SendPendingData. This is the building block for batched
@@ -367,7 +368,8 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
367368
// immediate response).
368369
boolReadPacket(Store&& store,
369370
const SocketAddress& local_address,
370-
const SocketAddress& remote_address);
371+
const SocketAddress& remote_address,
372+
const PacketInfo& pkt_info = PacketInfo());
371373

372374
// Called by BindingData's flush callback to trigger SendPendingData
373375
// on this session. Encapsulates the application() access so that

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Commit 1592a11

Browse files
jasnelladuh95
authored andcommitted
quic: add support for future ECN marking
Set up for when libuv eventually supports ECN marking. Pass the ECN marking stuff into ngtcp2. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode:Opus 4.6 PR-URL: #63267 Backport-PR-URL: #64675 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 80e6bf9 commit 1592a11

6 files changed

Lines changed: 77 additions & 24 deletions

File tree

‎src/quic/application.cc‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,12 @@ ssize_t Session::Application::TryWritePendingDatagram(PathStorage* path,
262262
int accepted = 0;
263263
int dg_flags = NGTCP2_WRITE_DATAGRAM_FLAG_MORE;
264264

265+
// PacketInfo for the datagram path. When libuv gains per-socket ECN
266+
// marking, the value from ngtcp2 should be forwarded to the send path.
267+
PacketInfo dg_pi;
265268
ssize_t dg_nwrite = ngtcp2_conn_writev_datagram(*session_,
266269
&path->path,
267-
nullptr,
270+
dg_pi,
268271
dest,
269272
destlen,
270273
&accepted,
@@ -390,12 +393,14 @@ void Session::Application::SendPendingData() {
390393
};
391394

392395
// Accumulate a completed packet into the batch.
393-
auto enqueue_packet = [&](Packet::Ptr& pkt, size_t len) {
394-
Debug(session_, "Enqueuing packet with %zu bytes into batch", len);
395-
pkt->Truncate(len);
396-
path.CopyTo(&batch_paths[batch_count]);
397-
batch[batch_count++] = std::move(pkt);
398-
};
396+
auto enqueue_packet =
397+
[&](Packet::Ptr& pkt, size_t len, const PacketInfo& pi) {
398+
Debug(session_, "Enqueuing packet with %zu bytes into batch", len);
399+
pkt->Truncate(len);
400+
pkt->set_pkt_info(pi);
401+
path.CopyTo(&batch_paths[batch_count]);
402+
batch[batch_count++] = std::move(pkt);
403+
};
399404

400405
// We're going to enter a loop here to prepare and send no more than
401406
// max_packet_count packets.
@@ -434,8 +439,9 @@ void Session::Application::SendPendingData() {
434439
}
435440

436441
// Awesome, let's write our packet!
442+
PacketInfo pi;
437443
ssize_t nwrite = WriteVStream(
438-
&path, packet->data(), &ndatalen, packet->length(), stream_data);
444+
&path, &pi, packet->data(), &ndatalen, packet->length(), stream_data);
439445

440446
// When ndatalen is > 0, that's our indication that stream data was accepted
441447
// in to the packet. Yay!
@@ -531,7 +537,7 @@ void Session::Application::SendPendingData() {
531537
if (result > 0) {
532538
size_t len = result;
533539
Debug(session_, "Sending packet with %zu bytes", len);
534-
enqueue_packet(packet, len);
540+
enqueue_packet(packet, len, pi);
535541
if (++packet_send_count == max_packet_count) return;
536542
} elseif (result < 0) {
537543
// Any negative result other than NGTCP2_ERR_WRITE_MORE
@@ -568,7 +574,7 @@ void Session::Application::SendPendingData() {
568574
// is the size of the packet we are sending.
569575
size_t len = nwrite;
570576
Debug(session_, "Sending packet with %zu bytes", len);
571-
enqueue_packet(packet, len);
577+
enqueue_packet(packet, len, pi);
572578
if (++packet_send_count == max_packet_count) return;
573579

574580
// If there are pending datagrams, try sending them in a fresh packet.
@@ -587,7 +593,7 @@ void Session::Application::SendPendingData() {
587593
TryWritePendingDatagram(&path, packet->data(), packet->length());
588594
if (result > 0) {
589595
Debug(session_, "Sending datagram packet with %zd bytes", result);
590-
enqueue_packet(packet, static_cast<size_t>(result));
596+
enqueue_packet(packet, static_cast<size_t>(result), PacketInfo());
591597
if (++packet_send_count == max_packet_count) return;
592598
} elseif (result < 0 && result != NGTCP2_ERR_WRITE_MORE) {
593599
// Fatal error — session already closed by TryWritePendingDatagram.
@@ -600,17 +606,20 @@ void Session::Application::SendPendingData() {
600606
}
601607

602608
ssize_tSession::Application::WriteVStream(PathStorage* path,
609+
PacketInfo* pi,
603610
uint8_t* dest,
604611
ssize_t* ndatalen,
605612
size_t max_packet_size,
606613
const StreamData& stream_data) {
607614
DCHECK_LE(stream_data.count, kMaxVectorCount);
608615
uint32_t flags = NGTCP2_WRITE_STREAM_FLAG_MORE;
609616
if (stream_data.fin) flags |= NGTCP2_WRITE_STREAM_FLAG_FIN;
617+
// The PacketInfo out-param is populated by ngtcp2 with the ECN codepoint
618+
// to apply when sending this packet. When libuv gains per-socket ECN
619+
// marking, the value should be forwarded to the send path.
610620
returnngtcp2_conn_writev_stream(*session_,
611621
&path->path,
612-
// TODO(@jasnell): ECN blocked on libuv
613-
nullptr,
622+
*pi,
614623
dest,
615624
max_packet_size,
616625
ndatalen,

‎src/quic/application.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,11 @@ class Session::Application : public MemoryRetainer {
269269
uint8_t* dest,
270270
size_t destlen);
271271

272-
// Write the given stream_data into the buffer.
272+
// Write the given stream_data into the buffer. The PacketInfo out-param
273+
// is populated by ngtcp2 with per-packet metadata (e.g., ECN codepoint)
274+
// that should be applied when sending the packet.
273275
ssize_tWriteVStream(PathStorage* path,
276+
PacketInfo* pi,
274277
uint8_t* buf,
275278
ssize_t* ndatalen,
276279
size_t max_packet_size,

‎src/quic/data.h‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,40 @@ namespace node::quic {
1919
template <typename T>
2020
concept OneByteType = sizeof(T) == 1;
2121

22+
// Lightweight wrapper around ngtcp2_pkt_info. Insulates the Node.js QUIC
23+
// code from the ngtcp2 struct layout and provides a clean API boundary
24+
// for per-packet metadata (currently ECN codepoint; may grow as ngtcp2
25+
// and libuv evolve).
26+
//
27+
// Default-constructed PacketInfo is zero-initialized, which ngtcp2 treats
28+
// as ECN Not-ECT — identical to passing nullptr for the pkt_info parameter.
29+
classPacketInfofinal {
30+
public:
31+
// ECN codepoints as defined by RFC 3168.
32+
enumclassEcn : uint32_t {
33+
NOT_ECT = 0, // Not ECN-Capable Transport
34+
ECT_1 = 1, // ECN-Capable Transport(1)
35+
ECT_0 = 2, // ECN-Capable Transport(0)
36+
CE = 3, // Congestion Experienced
37+
};
38+
39+
PacketInfo() : info_{} {}
40+
explicitPacketInfo(const ngtcp2_pkt_info& info) : info_(info) {}
41+
42+
// ECN codepoint for this packet. When libuv gains per-packet ECN
43+
// reporting, populate via set_ecn() from the receive metadata
44+
// before passing to ReadPacket().
45+
Ecn ecn() const { returnstatic_cast<Ecn>(info_.ecn); }
46+
voidset_ecn(Ecn ecn) { info_.ecn = static_cast<uint32_t>(ecn); }
47+
48+
// Conversion operators for ngtcp2 API calls.
49+
operatorconst ngtcp2_pkt_info*() const { return &info_; }
50+
operator ngtcp2_pkt_info*() { return &info_; }
51+
52+
private:
53+
ngtcp2_pkt_info info_;
54+
};
55+
2256
structPathfinal : public ngtcp2_path {
2357
explicitPath(const SocketAddress& local, const SocketAddress& remote);
2458
Path(Path&& other) noexcept = default;

‎src/quic/packet.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class Packet final {
6868
size_tlength() const { return length_; }
6969
size_tcapacity() const { return capacity_; }
7070
const SocketAddress& destination() const { return destination_; }
71+
const PacketInfo& pkt_info() const { return pkt_info_; }
72+
voidset_pkt_info(const PacketInfo& pi) { pkt_info_ = pi; }
7173
Listener* listener() const { return listener_; }
7274

7375
// Redirect the packet to a different endpoint for cross-endpoint sends
@@ -148,6 +150,7 @@ class Packet final {
148150
Listener* listener_;
149151

150152
// Touched at send time.
153+
PacketInfo pkt_info_;
151154
SocketAddress destination_;
152155

153156
// Only touched by libuv during uv_udp_send and in the send callback.

‎src/quic/session.cc‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,19 +2111,21 @@ void Session::SetLastError(QuicError&& error) {
21112111

21122112
boolSession::Receive(Store&& store,
21132113
const SocketAddress& local_address,
2114-
const SocketAddress& remote_address) {
2114+
const SocketAddress& remote_address,
2115+
const PacketInfo& pkt_info) {
21152116
// Convenience wrapper: reads the packet and immediately triggers
21162117
// SendPendingData. Used by paths that need an immediate response
21172118
// (e.g., Endpoint::Connect for client Initial packets).
21182119
// The hot receive path uses ReadPacket() directly with deferred
21192120
// flush via BindingData's uv_check callback.
21202121
SendPendingDataScope send_scope(this);
2121-
returnReadPacket(std::move(store), local_address, remote_address);
2122+
returnReadPacket(std::move(store), local_address, remote_address, pkt_info);
21222123
}
21232124

21242125
boolSession::ReadPacket(Store&& store,
21252126
const SocketAddress& local_address,
2126-
const SocketAddress& remote_address) {
2127+
const SocketAddress& remote_address,
2128+
const PacketInfo& pkt_info) {
21272129
DCHECK(!is_destroyed());
21282130
impl_->remote_address_ = remote_address;
21292131

@@ -2145,12 +2147,12 @@ bool Session::ReadPacket(Store&& store,
21452147
int err;
21462148
{
21472149
NgTcp2CallbackScope callback_scope(this);
2148-
//ECN codepoint (ngtcp2_pkt_info.ecn) is not yet populated because
2149-
// libuv does not currently deliver per-packet ECN metadata. When
2150-
//libuv gains ECN receive reporting, the pkt_info should be
2151-
//populated from the per-packet metadata and passed through here.
2150+
//The PacketInfo carries per-packet metadata (currently ECN codepoint).
2151+
//When libuv gains per-packet ECN reporting, the caller should
2152+
//populate pkt_info from the receive metadata before calling
2153+
//ReadPacket().
21522154
err = ngtcp2_conn_read_pkt(
2153-
*this, &path, nullptr, vec.base, vec.len, uv_hrtime());
2155+
*this, &path, pkt_info, vec.base, vec.len, uv_hrtime());
21542156
}
21552157
if (is_destroyed()) returnfalse;
21562158

‎src/quic/session.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
355355

356356
boolReceive(Store&& store,
357357
const SocketAddress& local_address,
358-
const SocketAddress& remote_address);
358+
const SocketAddress& remote_address,
359+
const PacketInfo& pkt_info = PacketInfo());
359360

360361
// ReadPacket processes a single inbound packet through ngtcp2 without
361362
// triggering SendPendingData. This is the building block for batched
@@ -367,7 +368,8 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
367368
// immediate response).
368369
boolReadPacket(Store&& store,
369370
const SocketAddress& local_address,
370-
const SocketAddress& remote_address);
371+
const SocketAddress& remote_address,
372+
const PacketInfo& pkt_info = PacketInfo());
371373

372374
// Called by BindingData's flush callback to trigger SendPendingData
373375
// on this session. Encapsulates the application() access so that

0 commit comments

Comments
 (0)