Commit 7f011149 authored by jan.koester's avatar jan.koester
Browse files

importned fix

parent 185fe20e
Loading
Loading
Loading
Loading
+44 −23
Changes for src/quic.cpp: 44 added lines, 23 removed lines.
Original line number Diff line number Diff line
@@ -391,12 +391,15 @@ void quic::processApplicationPacketsBatch(

        // Defer ACK across the whole batch — sent once below (or piggybacked
        // sooner if frame processing triggers an outgoing data packet), so N
        // packets arriving in one recv cycle cost one ACK instead of N.
        _app_ack_pending = true;

        // packets arriving in one recv cycle cost one ACK instead of N. Only
        // set if at least one ack-eliciting frame was seen (RFC 9000
        // §13.2.1) — otherwise an ACK-only packet from the peer would
        // trigger an ACK back, which triggers another, forever.
        size_t offset = 0;
        while (offset < payload.size()) {
            processFrame(payload.data(), payload.size(), offset);
            bool ack_eliciting = false;
            processFrame(payload.data(), payload.size(), offset, ack_eliciting);
            if (ack_eliciting) _app_ack_pending = true;
        }
    }

@@ -2041,14 +2044,16 @@ void quic::processInitialPacket(const uint8_t* data, size_t len) {
    // Set encryption level for frame processing
    _current_enc_level = EncryptionLevel::Initial;
    
    // Process frames in payload
    // Process frames in payload. Only request an ACK if at least one
    // ack-eliciting frame was present (RFC 9000 §13.2.1) — otherwise an
    // ACK-only packet from the peer would trigger an ACK back, and so on
    // forever.
    size_t frame_offset = 0;
    while (frame_offset < payload.size()) {
        processFrame(payload.data(), payload.size(), frame_offset);
        bool ack_eliciting = false;
        processFrame(payload.data(), payload.size(), frame_offset, ack_eliciting);
        if (ack_eliciting) _need_initial_ack = true;
    }
    
    // Send ACK for received Initial packet (both client and server need this)
    _need_initial_ack = true;
}

void quic::processHandshakePacket(const uint8_t* data, size_t len) {
@@ -2102,13 +2107,14 @@ void quic::processHandshakePacket(const uint8_t* data, size_t len) {
    // Set encryption level for frame processing
    _current_enc_level = EncryptionLevel::Handshake;
    
    // Only request an ACK if at least one ack-eliciting frame was present
    // (RFC 9000 §13.2.1) — see processInitialPacket()'s identical comment.
    size_t hs_offset = 0;
    while (hs_offset < payload.size()) {
        processFrame(payload.data(), payload.size(), hs_offset);
        bool ack_eliciting = false;
        processFrame(payload.data(), payload.size(), hs_offset, ack_eliciting);
        if (ack_eliciting) _need_handshake_ack = true;
    }
    
    // Send ACK for received Handshake packet
    _need_handshake_ack = true;
}

void quic::processApplicationPacket(const uint8_t* data, size_t len) {
@@ -2137,12 +2143,15 @@ void quic::processApplicationPacket(const uint8_t* data, size_t len) {
    trackRecvPN(pn, _app_pn_recv_ranges);

    // Defer ACK — it will be piggybacked on the next outgoing data packet
    // (e.g., echo response). If no data is sent, flush after frame processing.
    _app_ack_pending = true;

    // (e.g., echo response). If no data is sent, flush after frame
    // processing. Only set if at least one ack-eliciting frame was present
    // (RFC 9000 §13.2.1) — otherwise an ACK-only packet from the peer would
    // trigger an ACK back, which triggers another, forever.
    size_t offset = 0;
    while (offset < payload.size()) {
        processFrame(payload.data(), payload.size(), offset);
        bool ack_eliciting = false;
        processFrame(payload.data(), payload.size(), offset, ack_eliciting);
        if (ack_eliciting) _app_ack_pending = true;
    }

    // If ACK is still pending (no data was sent in response), send standalone ACK
@@ -3102,7 +3111,7 @@ void quic::abortFrameParsing(size_t len, size_t& offset, uint64_t error_code, co
    offset = len;
}

void quic::processFrame(const uint8_t* data, size_t len, size_t& offset) {
void quic::processFrame(const uint8_t* data, size_t len, size_t& offset, bool& ack_eliciting) {
    if (offset >= len) return;

    // Read frame type
@@ -3113,6 +3122,18 @@ void quic::processFrame(const uint8_t* data, size_t len, size_t& offset) {
        return;
    }
    offset += type_bytes;

    // RFC 9000 §13.2: every frame type except ACK/ACK_ECN, CONNECTION_CLOSE,
    // and PADDING is ack-eliciting. Set before any of the parsing below
    // (including the abort/error paths) so callers get a consistent answer
    // for this frame type regardless of how parsing turns out. This is what
    // lets callers honor §13.2.1 ("a packet containing only ACK frames does
    // not elicit an ACK") — without it, an ACK sent for real data triggers
    // an ACK-of-the-ACK, which triggers another, forever.
    ack_eliciting = !(frame_type == 0x00 || frame_type == 0x02 ||
                       frame_type == 0x03 || frame_type == 0x1c ||
                       frame_type == 0x1d);

    if (frame_type != 0x00) {  // Don't log PADDING
        QUIC_DBG("processFrame: type=0x%02lx offset=%zu len=%zu enc=%d",
                 (unsigned long)frame_type, offset, len, (int)_current_enc_level);
@@ -5177,16 +5198,16 @@ size_t quic::sendStreamData(uint64_t stream_id, const uint8_t* data, size_t len,
                pumpIncoming();
                if (cwndAllowsSend(est_pkt_size)) { unblocked = true; break; }
            }
            // If still blocked, poll with timeout
            // If still blocked, poll with timeout. Reuse one socketwait
            // (and its epoll fd) across every retry instead of constructing
            // a fresh one per ~1ms attempt — see _cc_socketwait's comment.
            if (!unblocked) {
                if (!_cc_socketwait) _cc_socketwait = std::make_unique<socketwait>();
                for (int cc_wait = 0; cc_wait < 100; ++cc_wait) {
                    if (_conn_state.load() == ConnectionState::Closed ||
                        _conn_state.load() == ConnectionState::Draining) break;
                    lock.unlock();
                    {
                        socketwait sw;
                        sw.waitRead(*this, 1);
                    }
                    _cc_socketwait->waitRead(*this, 1);
                    lock.lock();
                    pumpIncoming();
                    if (cwndAllowsSend(est_pkt_size)) { unblocked = true; break; }
+27 −1
Changes for src/socket.h: 27 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -77,6 +77,11 @@

namespace netplus {

	// Forward-declared (defined in eventapi.h, which itself includes this
	// header) so `quic` can hold one via unique_ptr without a circular
	// #include — see quic::_cc_socketwait's comment.
	class socketwait;

	enum sockettype { TCP = 0, UDP = 1, SSL = 2, QUIC = 3 };

#ifdef Windows
@@ -776,7 +781,16 @@ namespace netplus {
		void processApplicationPacket(const uint8_t* data, size_t len);

		// Frame processing
		void processFrame(const uint8_t* data, size_t len, size_t& offset);
		// ack_eliciting is set to whether this frame counts as ack-eliciting
		// per RFC 9000 §13.2 (everything except ACK/ACK_ECN/CONNECTION_CLOSE/
		// PADDING) — set once, right after the frame type is decoded, so
		// every exit path (including error/abort paths) reports it
		// consistently for the frame type that was actually present. Left
		// untouched if the call returns before a frame type could even be
		// read (e.g. offset already >= len). Callers use this to decide
		// whether the packet as a whole should trigger an ACK; see
		// _app_ack_pending's call sites for why that distinction matters.
		void processFrame(const uint8_t* data, size_t len, size_t& offset, bool& ack_eliciting);
		void processCryptoFrame(const uint8_t* data, size_t len, size_t& offset);
		void processStreamFrame(const uint8_t* data, size_t len, size_t& offset);
		void processAckFrame(const uint8_t* data, size_t len, size_t& offset);
@@ -1244,6 +1258,18 @@ namespace netplus {
		void onPacketAckedCC(size_t packet_size, uint64_t pn);
		void onPacketLostCC(size_t packet_size, uint64_t pn);

		// Reused across the congestion-window-blocked wait retries in
		// sendStreamData() instead of constructing a fresh socketwait (and
		// its underlying epoll fd) on every ~1ms poll attempt. Profiling a
		// single cwnd-limited 5MB transfer showed tens of thousands of
		// epoll_create1/epoll_ctl/close cycles from that loop alone —
		// dwarfing the actual data-path cost and explaining most of the
		// gap to TCP throughput on loopback. Lazily constructed (most
		// connections finish without ever blocking on cwnd) so a server
		// handling many short-lived connections doesn't pay for an epoll
		// fd per connection that never needs one.
		std::unique_ptr<socketwait> _cc_socketwait;

		// ---- Path MTU Discovery (DPLPMTUD, RFC 8899 / RFC 9000 §14.3-14.4) ----
		// A probe is a PATH_CHALLENGE frame padded with PADDING to an exact
		// target wire size, sent as an ordinary 1-RTT packet. Success is