Loading src/quic.cpp +112 −30 Changes for src/quic.cpp: 112 added lines, 30 removed lines. Original line number Diff line number Diff line Loading @@ -2555,6 +2555,98 @@ void quic::serviceTimers() { // Socket Interface: accept (Server-side connection acceptance) // ============================================================================ void quic::processChildPacketsLocked(const std::shared_ptr<quic> &existing, const std::vector<std::pair<const uint8_t*, size_t>> &pkts, std::unique_lock<std::recursive_mutex> &child_lock) { // datagrams deferred while the child was busy go first (arrival order) std::vector<std::vector<uint8_t>> deferred; { std::lock_guard<std::mutex> g(existing->_deferred_rx_mtx); deferred.swap(existing->_deferred_rx); } if (!deferred.empty()) { std::vector<std::pair<const uint8_t*, size_t>> all; all.reserve(deferred.size() + pkts.size()); for (auto &d : deferred) all.emplace_back(d.data(), d.size()); all.insert(all.end(), pkts.begin(), pkts.end()); QUIC_DBG("accept: processing %zu deferred datagram(s)", deferred.size()); existing->processApplicationPacketsBatch(all); } else if (!pkts.empty()) { existing->processApplicationPacketsBatch(pkts); } if (existing->_handshake_complete && existing->_conn_state.load() == ConnectionState::Connected) { for (auto& [sid, limit] : existing->_pending_max_stream_data) { existing->sendMaxStreamData(sid, limit); } existing->_pending_max_stream_data.clear(); if (existing->_pending_max_data) { existing->sendMaxData(existing->_pending_max_data_value); existing->_pending_max_data = false; } existing->pmtudTick(); // F20: sendMaxStreamData/sendMaxData above only queued their // packets (batch=true) — flush now, before this block's own // scope ends, so they never sit unsent. existing->flushBatch(); } if (!existing->_pending_dispatches.empty()) { // See the intra-connection dispatch call sites' comment on // why capacity is re-reserved right after the move (F49). size_t dispatch_batch_hint = existing->_pending_dispatches.size(); std::vector<PendingDispatch> dispatches = std::move(existing->_pending_dispatches); existing->_pending_dispatches.clear(); existing->_pending_dispatches.reserve(dispatch_batch_hint); child_lock.unlock(); quic::scheduleDispatches(existing, std::move(dispatches)); child_lock.lock(); } } void quic::deferChildPackets(const std::shared_ptr<quic> &existing, const std::vector<std::pair<const uint8_t*, size_t>> &pkts) { bool first = false; { std::lock_guard<std::mutex> g(existing->_deferred_rx_mtx); first = existing->_deferred_rx.empty(); for (auto &[data, len] : pkts) { if (existing->_deferred_rx.size() >= kMaxDeferredRx) break; // peer's loss recovery existing->_deferred_rx.emplace_back(data, data + len); } } QUIC_DBG("accept: child busy, deferred %zu datagram(s)", pkts.size()); if (first) { std::lock_guard<std::mutex> g(_deferred_children_mtx); _deferred_children.push_back(existing); } } void quic::drainDeferredChildren() { std::vector<std::shared_ptr<quic>> children; { std::lock_guard<std::mutex> g(_deferred_children_mtx); if (_deferred_children.empty()) return; children.swap(_deferred_children); } static const std::vector<std::pair<const uint8_t*, size_t>> none; for (auto &c : children) { if (c->_conn_state.load() == ConnectionState::Closed) { std::lock_guard<std::mutex> g(c->_deferred_rx_mtx); c->_deferred_rx.clear(); continue; } std::unique_lock<std::recursive_mutex> child_lock(c->quic_mtx(), std::try_to_lock); if (!child_lock.owns_lock()) { std::lock_guard<std::mutex> g(_deferred_children_mtx); _deferred_children.push_back(c); // still busy: next call continue; } processChildPacketsLocked(c, none, child_lock); } } void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) { QUIC_DBG("accept: ENTER _Socket=%d", _Socket); Loading Loading @@ -2594,6 +2686,11 @@ void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) { return; } // Datagrams kept for children that were busy last time (runs on every // call, also when nothing new arrived: the receive loop calls accept() // at least every wait timeout). drainDeferredChildren(); // Drain up to 512 datagrams per call. On high-throughput links (esp. // localhost) the client may burst many QUIC packets between epoll_wait // cycles. If we only read one datagram per cycle the kernel UDP Loading Loading @@ -2656,39 +2753,24 @@ void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) { // whatever we skip, so dropping it now and picking it up on a // later drain cycle is safe and far cheaper than blocking. std::unique_lock<std::recursive_mutex> child_lock(existing->quic_mtx(), std::try_to_lock); if (!child_lock.owns_lock()) continue; existing->processApplicationPacketsBatch(pkts); if (existing->_handshake_complete && existing->_conn_state.load() == ConnectionState::Connected) { for (auto& [sid, limit] : existing->_pending_max_stream_data) { existing->sendMaxStreamData(sid, limit); } existing->_pending_max_stream_data.clear(); if (existing->_pending_max_data) { existing->sendMaxData(existing->_pending_max_data_value); existing->_pending_max_data = false; } existing->pmtudTick(); // F20: sendMaxStreamData/sendMaxData above only queued their // packets (batch=true) — flush now, before this block's own // scope ends, so they never sit unsent. existing->flushBatch(); } if (!existing->_pending_dispatches.empty()) { // See the intra-connection dispatch call sites' comment on // why capacity is re-reserved right after the move (F49). size_t dispatch_batch_hint = existing->_pending_dispatches.size(); std::vector<PendingDispatch> dispatches = std::move(existing->_pending_dispatches); existing->_pending_dispatches.clear(); existing->_pending_dispatches.reserve(dispatch_batch_hint); child_lock.unlock(); quic::scheduleDispatches(existing, std::move(dispatches)); child_lock.lock(); // The holder is usually just sending one reply (microseconds): // a few yields catch most of those without blocking this loop // for the long holds (a cwnd-limited transfer). for (int spin = 0; spin < 32 && !child_lock.owns_lock(); ++spin) { std::this_thread::yield(); child_lock.try_lock(); } if (!child_lock.owns_lock()) { // busy on another thread: keep the datagrams for later // instead of dropping them (see _deferred_rx in socket.h) deferChildPackets(existing, pkts); continue; } processChildPacketsLocked(existing, pkts, child_lock); } pending_app_pkts.clear(); // children deferred in this or an earlier round may be free now drainDeferredChildren(); }; while (total_processed < MAX_DRAIN) { Loading src/socket.h +27 −0 Changes for src/socket.h: 27 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -2032,6 +2032,33 @@ namespace netplus { }; std::vector<PendingDispatch> _pending_dispatches; // Short-header datagrams the listener received for this (child) // connection while another thread held its quic_mtx(). accept() // only try_locks a child; it used to drop such datagrams and leave // them to the peer's loss recovery -- a full PTO each time, doubled // for every repeat (seconds on a connection that is busy in both // directions, e.g. a lock service). Copied here instead (the // receive buffer is reused) and processed, oldest first, the next // time the listener gets the lock. Bounded: past kMaxDeferredRx the // old behaviour (drop, peer retransmits) applies. static constexpr size_t kMaxDeferredRx = 256; std::mutex _deferred_rx_mtx; std::vector<std::vector<uint8_t>> _deferred_rx; // listener only: children with datagrams waiting in _deferred_rx std::mutex _deferred_children_mtx; std::vector<std::shared_ptr<quic>> _deferred_children; // Listener side: decrypts/processes `pkts` for child `existing` // (whose quic_mtx() `child_lock` holds), plus anything waiting in // its _deferred_rx first, then flushes and dispatches. void processChildPacketsLocked(const std::shared_ptr<quic> &existing, const std::vector<std::pair<const uint8_t*, size_t>> &pkts, std::unique_lock<std::recursive_mutex> &child_lock); // Listener side: queues `pkts` on busy child `existing`. void deferChildPackets(const std::shared_ptr<quic> &existing, const std::vector<std::pair<const uint8_t*, size_t>> &pkts); // Listener side: processes children whose datagrams were deferred. void drainDeferredChildren(); // Dispatches _stream_callback batches — inline on the calling // thread by default (same cost as calling the callback directly: // this is the common case, e.g. an echo server's fast reply, and Loading Loading
src/quic.cpp +112 −30 Changes for src/quic.cpp: 112 added lines, 30 removed lines. Original line number Diff line number Diff line Loading @@ -2555,6 +2555,98 @@ void quic::serviceTimers() { // Socket Interface: accept (Server-side connection acceptance) // ============================================================================ void quic::processChildPacketsLocked(const std::shared_ptr<quic> &existing, const std::vector<std::pair<const uint8_t*, size_t>> &pkts, std::unique_lock<std::recursive_mutex> &child_lock) { // datagrams deferred while the child was busy go first (arrival order) std::vector<std::vector<uint8_t>> deferred; { std::lock_guard<std::mutex> g(existing->_deferred_rx_mtx); deferred.swap(existing->_deferred_rx); } if (!deferred.empty()) { std::vector<std::pair<const uint8_t*, size_t>> all; all.reserve(deferred.size() + pkts.size()); for (auto &d : deferred) all.emplace_back(d.data(), d.size()); all.insert(all.end(), pkts.begin(), pkts.end()); QUIC_DBG("accept: processing %zu deferred datagram(s)", deferred.size()); existing->processApplicationPacketsBatch(all); } else if (!pkts.empty()) { existing->processApplicationPacketsBatch(pkts); } if (existing->_handshake_complete && existing->_conn_state.load() == ConnectionState::Connected) { for (auto& [sid, limit] : existing->_pending_max_stream_data) { existing->sendMaxStreamData(sid, limit); } existing->_pending_max_stream_data.clear(); if (existing->_pending_max_data) { existing->sendMaxData(existing->_pending_max_data_value); existing->_pending_max_data = false; } existing->pmtudTick(); // F20: sendMaxStreamData/sendMaxData above only queued their // packets (batch=true) — flush now, before this block's own // scope ends, so they never sit unsent. existing->flushBatch(); } if (!existing->_pending_dispatches.empty()) { // See the intra-connection dispatch call sites' comment on // why capacity is re-reserved right after the move (F49). size_t dispatch_batch_hint = existing->_pending_dispatches.size(); std::vector<PendingDispatch> dispatches = std::move(existing->_pending_dispatches); existing->_pending_dispatches.clear(); existing->_pending_dispatches.reserve(dispatch_batch_hint); child_lock.unlock(); quic::scheduleDispatches(existing, std::move(dispatches)); child_lock.lock(); } } void quic::deferChildPackets(const std::shared_ptr<quic> &existing, const std::vector<std::pair<const uint8_t*, size_t>> &pkts) { bool first = false; { std::lock_guard<std::mutex> g(existing->_deferred_rx_mtx); first = existing->_deferred_rx.empty(); for (auto &[data, len] : pkts) { if (existing->_deferred_rx.size() >= kMaxDeferredRx) break; // peer's loss recovery existing->_deferred_rx.emplace_back(data, data + len); } } QUIC_DBG("accept: child busy, deferred %zu datagram(s)", pkts.size()); if (first) { std::lock_guard<std::mutex> g(_deferred_children_mtx); _deferred_children.push_back(existing); } } void quic::drainDeferredChildren() { std::vector<std::shared_ptr<quic>> children; { std::lock_guard<std::mutex> g(_deferred_children_mtx); if (_deferred_children.empty()) return; children.swap(_deferred_children); } static const std::vector<std::pair<const uint8_t*, size_t>> none; for (auto &c : children) { if (c->_conn_state.load() == ConnectionState::Closed) { std::lock_guard<std::mutex> g(c->_deferred_rx_mtx); c->_deferred_rx.clear(); continue; } std::unique_lock<std::recursive_mutex> child_lock(c->quic_mtx(), std::try_to_lock); if (!child_lock.owns_lock()) { std::lock_guard<std::mutex> g(_deferred_children_mtx); _deferred_children.push_back(c); // still busy: next call continue; } processChildPacketsLocked(c, none, child_lock); } } void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) { QUIC_DBG("accept: ENTER _Socket=%d", _Socket); Loading Loading @@ -2594,6 +2686,11 @@ void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) { return; } // Datagrams kept for children that were busy last time (runs on every // call, also when nothing new arrived: the receive loop calls accept() // at least every wait timeout). drainDeferredChildren(); // Drain up to 512 datagrams per call. On high-throughput links (esp. // localhost) the client may burst many QUIC packets between epoll_wait // cycles. If we only read one datagram per cycle the kernel UDP Loading Loading @@ -2656,39 +2753,24 @@ void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) { // whatever we skip, so dropping it now and picking it up on a // later drain cycle is safe and far cheaper than blocking. std::unique_lock<std::recursive_mutex> child_lock(existing->quic_mtx(), std::try_to_lock); if (!child_lock.owns_lock()) continue; existing->processApplicationPacketsBatch(pkts); if (existing->_handshake_complete && existing->_conn_state.load() == ConnectionState::Connected) { for (auto& [sid, limit] : existing->_pending_max_stream_data) { existing->sendMaxStreamData(sid, limit); } existing->_pending_max_stream_data.clear(); if (existing->_pending_max_data) { existing->sendMaxData(existing->_pending_max_data_value); existing->_pending_max_data = false; } existing->pmtudTick(); // F20: sendMaxStreamData/sendMaxData above only queued their // packets (batch=true) — flush now, before this block's own // scope ends, so they never sit unsent. existing->flushBatch(); } if (!existing->_pending_dispatches.empty()) { // See the intra-connection dispatch call sites' comment on // why capacity is re-reserved right after the move (F49). size_t dispatch_batch_hint = existing->_pending_dispatches.size(); std::vector<PendingDispatch> dispatches = std::move(existing->_pending_dispatches); existing->_pending_dispatches.clear(); existing->_pending_dispatches.reserve(dispatch_batch_hint); child_lock.unlock(); quic::scheduleDispatches(existing, std::move(dispatches)); child_lock.lock(); // The holder is usually just sending one reply (microseconds): // a few yields catch most of those without blocking this loop // for the long holds (a cwnd-limited transfer). for (int spin = 0; spin < 32 && !child_lock.owns_lock(); ++spin) { std::this_thread::yield(); child_lock.try_lock(); } if (!child_lock.owns_lock()) { // busy on another thread: keep the datagrams for later // instead of dropping them (see _deferred_rx in socket.h) deferChildPackets(existing, pkts); continue; } processChildPacketsLocked(existing, pkts, child_lock); } pending_app_pkts.clear(); // children deferred in this or an earlier round may be free now drainDeferredChildren(); }; while (total_processed < MAX_DRAIN) { Loading
src/socket.h +27 −0 Changes for src/socket.h: 27 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -2032,6 +2032,33 @@ namespace netplus { }; std::vector<PendingDispatch> _pending_dispatches; // Short-header datagrams the listener received for this (child) // connection while another thread held its quic_mtx(). accept() // only try_locks a child; it used to drop such datagrams and leave // them to the peer's loss recovery -- a full PTO each time, doubled // for every repeat (seconds on a connection that is busy in both // directions, e.g. a lock service). Copied here instead (the // receive buffer is reused) and processed, oldest first, the next // time the listener gets the lock. Bounded: past kMaxDeferredRx the // old behaviour (drop, peer retransmits) applies. static constexpr size_t kMaxDeferredRx = 256; std::mutex _deferred_rx_mtx; std::vector<std::vector<uint8_t>> _deferred_rx; // listener only: children with datagrams waiting in _deferred_rx std::mutex _deferred_children_mtx; std::vector<std::shared_ptr<quic>> _deferred_children; // Listener side: decrypts/processes `pkts` for child `existing` // (whose quic_mtx() `child_lock` holds), plus anything waiting in // its _deferred_rx first, then flushes and dispatches. void processChildPacketsLocked(const std::shared_ptr<quic> &existing, const std::vector<std::pair<const uint8_t*, size_t>> &pkts, std::unique_lock<std::recursive_mutex> &child_lock); // Listener side: queues `pkts` on busy child `existing`. void deferChildPackets(const std::shared_ptr<quic> &existing, const std::vector<std::pair<const uint8_t*, size_t>> &pkts); // Listener side: processes children whose datagrams were deferred. void drainDeferredChildren(); // Dispatches _stream_callback batches — inline on the calling // thread by default (same cost as calling the callback directly: // this is the common case, e.g. an echo server's fast reply, and Loading