Commit 4664d8a9 authored by jan.koester's avatar jan.koester
Browse files

retransmit patch

parent ed37b45f
Loading
Loading
Loading
Loading
+51 −5
Changes for src/quic.cpp: 51 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -4552,6 +4552,7 @@ void quic::processAckFrame(const uint8_t* data, size_t len, size_t& offset, bool
            }
            // Congestion control: notify CC about acked packet
            onPacketAckedCC(it->second.sent_size, it->first);
            releaseSentDataBuffer(std::move(it->second.stream_data));
            it = _sent_packets.erase(it);
        }
    }
@@ -4579,6 +4580,12 @@ void quic::recordSentPacket(uint64_t pn, uint64_t stream_id, uint64_t stream_off
    sp.sent_size = wire_size > 0 ? wire_size : len;
    sp.fin = fin;
    sp.ack_eliciting = true;
    // Reuse a retired buffer's capacity instead of a fresh heap allocation
    // on every sent packet (see _sent_data_pool's comment in socket.h).
    if (!_sent_data_pool.empty()) {
        sp.stream_data = std::move(_sent_data_pool.back());
        _sent_data_pool.pop_back();
    }
    sp.stream_data.assign(data, data + len);
    _sent_packets[pn] = std::move(sp);
}
@@ -4605,10 +4612,14 @@ void quic::checkLossAndRetransmit() {
        auto& oldest = _sent_packets.begin()->second;
        double age = std::chrono::duration<double>(now - oldest.sent_time).count();
        if (age >= pto_thresh) {
            // Retransmit the oldest unacked packet as a probe
            auto sp = oldest; // copy — will be erased below
            uint64_t old_pn = _sent_packets.begin()->first;
            _sent_packets.erase(_sent_packets.begin());
            // Retransmit the oldest unacked packet as a probe. Moved (not
            // copied) out of the map entry — the entry is erased right
            // below regardless of outcome, so there's nothing to preserve
            // in it, and moving avoids deep-copying stream_data.
            auto it0 = _sent_packets.begin();
            SentPacket sp = std::move(it0->second);
            uint64_t old_pn = it0->first;
            _sent_packets.erase(it0);

            if (sp.data_length > 0 || sp.fin) {
                uint8_t frame_type = 0x08 | 0x02;
@@ -4634,7 +4645,23 @@ void quic::checkLossAndRetransmit() {
                    recordSentPacket(new_pn, sp.stream_id, sp.stream_offset,
                                    sp.stream_data.data(), sp.stream_data.size(),
                                    sp.fin, packet.size());
                    // PTO firing is not itself a congestion signal (RFC 9002
                    // §6.2) — no cwnd/ssthresh change here. But old_pn's
                    // entry is gone from _sent_packets for good (erased
                    // above, superseded by new_pn), so its bytes must leave
                    // the in-flight count exactly once, and new_pn's bytes
                    // must enter it exactly once via onPacketSentCC() —
                    // otherwise _bytes_in_flight drifts every time a PTO
                    // probe fires, eventually clamping to 0 and disabling
                    // cwndAllowsSend()'s gate entirely.
                    if (_bytes_in_flight >= sp.sent_size) _bytes_in_flight -= sp.sent_size;
                    else _bytes_in_flight = 0;
                    onPacketSentCC(packet.size());
                    ++_pto_count;
                    // recordSentPacket() above already deep-copied
                    // sp.stream_data into new_pn's own pooled buffer —
                    // return this one's capacity to the pool too.
                    releaseSentDataBuffer(std::move(sp.stream_data));
                } else {
                    // Send failed (EAGAIN) — re-queue under original PN
                    // so the data is not permanently lost.  Reset sent_time
@@ -4696,7 +4723,15 @@ void quic::checkLossAndRetransmit() {
            lost.push_back(std::move(it->second));
            it = _sent_packets.erase(it);
        } else {
            ++it;
            // Early exit: _sent_packets is ordered by PN, and PN order
            // tracks send order (retransmits always get a fresh, larger PN
            // — see the PTO/loss-retransmit blocks above), so once an
            // entry satisfies neither the PN-threshold nor the time-
            // threshold loss criterion, every later (larger-PN, more-
            // recently-sent) entry is at least as unlikely to qualify.
            // Continuing to scan them is pure wasted work in the
            // overwhelmingly common case of little/no loss.
            break;
        }
    }

@@ -4743,7 +4778,18 @@ void quic::checkLossAndRetransmit() {
            // Track retransmitted packet under new PN
            recordSentPacket(new_pn, sp.stream_id, sp.stream_offset,
                            sp.stream_data.data(), sp.stream_data.size(), sp.fin, packet.size());
            // sp's own bytes were already removed from _bytes_in_flight by
            // onPacketLostCC() above (that's the genuine congestion signal —
            // real loss, unlike the PTO path). new_pn is a distinct entry in
            // _sent_packets from here on, so its bytes must be counted via
            // onPacketSentCC() too, or _bytes_in_flight ends up permanently
            // short by packet.size() once new_pn is later acked/lost.
            onPacketSentCC(packet.size());
            ++retransmit_ok;
            // recordSentPacket() above already deep-copied sp.stream_data
            // into new_pn's own pooled buffer — return this one's capacity
            // to the pool too instead of letting `lost`'s destructor free it.
            releaseSentDataBuffer(std::move(sp.stream_data));
        } else {
            // Send failed (EAGAIN / send-buffer full) — re-insert under
            // the original PN so the packet is retried on the next
+19 −0
Changes for src/socket.h: 19 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1349,6 +1349,25 @@ namespace netplus {
		// Application packet retransmission (loss detection + recovery)
		std::map<uint64_t, SentPacket> _sent_packets;  // pn → metadata
		uint64_t _largest_acked_pn = UINT64_MAX;       // largest packet number ACKed

		// Free list of retired SentPacket::stream_data buffers, capacity
		// preserved. recordSentPacket() pops one instead of default-
		// constructing a fresh vector for every sent packet; every erase
		// site of _sent_packets (ACK processing, PTO, loss detection) pushes
		// its entry's buffer back here (capacity intact) instead of letting
		// it deallocate. Capped so a connection that briefly has very large
		// packets in flight doesn't pin that capacity in the pool forever.
		std::vector<std::vector<uint8_t>> _sent_data_pool;
		static constexpr size_t SENT_DATA_POOL_CAP = 256;

		// Returns `buf`'s capacity to the pool if there's room, else lets it
		// deallocate normally.
		void releaseSentDataBuffer(std::vector<uint8_t>&& buf) {
			if (_sent_data_pool.size() < SENT_DATA_POOL_CAP) {
				buf.clear();
				_sent_data_pool.push_back(std::move(buf));
			}
		}
		// RTT estimation (RFC 9002 §5)
		double _srtt = 0.0;           // smoothed RTT (seconds)
		double _rttvar = 0.0;         // RTT variance (seconds)