Commit 14f38323 authored by jan.koester's avatar jan.koester
Browse files

test

parent 1ee72b94
Loading
Loading
Loading
Loading
+67 −7
Changes for src/quic.cpp: 67 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -453,7 +453,10 @@ void quic::processApplicationPacketsBatch(
        while (offset < payload.size()) {
            bool ack_eliciting = false;
            processFrame(payload.data(), payload.size(), offset, ack_eliciting);
            if (ack_eliciting) _app_ack_pending = true;
            if (ack_eliciting) {
                if (!_app_ack_pending) _app_ack_elicit_since = std::chrono::steady_clock::now();
                _app_ack_pending = true;
            }
        }
    }

@@ -2684,7 +2687,10 @@ void quic::processApplicationPacket(const uint8_t* data, size_t len) {
    while (offset < payload.size()) {
        bool ack_eliciting = false;
        processFrame(payload.data(), payload.size(), offset, ack_eliciting);
        if (ack_eliciting) _app_ack_pending = true;
        if (ack_eliciting) {
            if (!_app_ack_pending) _app_ack_elicit_since = std::chrono::steady_clock::now();
            _app_ack_pending = true;
        }
    }

    // If ACK is still pending (no data was sent in response), send standalone ACK
@@ -3326,6 +3332,18 @@ std::vector<uint8_t> quic::buildTransportParameters() {
    params.insert(params.end(), mups_len_buf, mups_len_buf + mups_len_size);
    params.insert(params.end(), mups_buf, mups_buf + mups_len);

    // ack_delay_exponent (0x0a) — tells the peer how to scale the ACK
    // Delay field in ACK frames *we* send (RFC 9000 §13.2.5, §18.2).
    // Always the RFC default (3); sent explicitly so a decoder relying on
    // this parameter rather than assuming the default stays correct.
    params.push_back(0x0a);
    uint8_t ade_buf[8];
    size_t ade_len = encodeVarInt(_ack_delay_exponent, ade_buf);
    uint8_t ade_len_buf[8];
    size_t ade_len_size = encodeVarInt(ade_len, ade_len_buf);
    params.insert(params.end(), ade_len_buf, ade_len_buf + ade_len_size);
    params.insert(params.end(), ade_buf, ade_buf + ade_len);

    return params;
}

@@ -4670,8 +4688,16 @@ void quic::processAckFrame(const uint8_t* data, size_t len, size_t& offset, bool
            // Update RTT from the largest newly-acked packet
            if (it->first == largest_ack && it->second.ack_eliciting) {
                double rtt_sample = std::chrono::duration<double>(now - it->second.sent_time).count();
                double adj_delay = static_cast<double>(ack_delay) / 1000000.0;
                // RFC 9000 §13.2.5: the wire value is in units of
                // 2^ack_delay_exponent microseconds, using *the peer's own*
                // advertised exponent (see _peer_ack_delay_exponent) — not
                // ours, and not a fixed implicit microsecond unit as before
                // (which silently under-scaled by up to 2^3=8x at the
                // RFC-default exponent whenever the peer reported a
                // nonzero delay).
                double adj_delay = static_cast<double>(ack_delay << _peer_ack_delay_exponent) / 1000000.0;
                if (rtt_sample > adj_delay) rtt_sample -= adj_delay;
                _latest_rtt = rtt_sample;
                if (!_rtt_initialized) {
                    _srtt = rtt_sample;
                    _rttvar = rtt_sample / 2.0;
@@ -4826,7 +4852,11 @@ void quic::checkLossAndRetransmit() {
    // which forces more send rounds, which gives the false-positive more
    // chances to fire again, compounding into a throughput collapse that
    // gets worse the larger the transfer.
    double loss_delay = TIME_THRESHOLD * std::max(_srtt, _rttvar);
    // RFC 9002 §6.1.2: kTimeThreshold * max(smoothed_rtt, latest_rtt) — not
    // _rttvar, which is a slow-moving variance estimate (0.25 weight per
    // update) and reacts far more sluggishly to a genuine RTT jump (path
    // change, mobile handover) than the actual latest sample does.
    double loss_delay = TIME_THRESHOLD * std::max(_srtt, _latest_rtt);
    if (loss_delay < 0.005) loss_delay = 0.005; // min 5ms
    auto loss_time = std::chrono::duration<double>(loss_delay);

@@ -5053,7 +5083,15 @@ void quic::sendStreamDataBlocked(uint64_t stream_id, uint64_t limit) {
// ============================================================================

void quic::sendAppAck() {
    std::vector<uint8_t> frame = buildAckFrame(_app_pn_recv, 0, _app_pn_recv_ranges);
    // Real elapsed-time ACK Delay (RFC 9000 §13.2.5) instead of a
    // hardcoded 0, encoded in our own ack_delay_exponent's units — see
    // _app_ack_elicit_since's comment in socket.h.
    double elapsed_sec = std::chrono::duration<double>(
        std::chrono::steady_clock::now() - _app_ack_elicit_since).count();
    if (elapsed_sec < 0) elapsed_sec = 0;
    uint64_t ack_delay_encoded = static_cast<uint64_t>(elapsed_sec * 1000000.0) >> _ack_delay_exponent;

    std::vector<uint8_t> frame = buildAckFrame(_app_pn_recv, ack_delay_encoded, _app_pn_recv_ranges);
    sendShortHeaderControlPacket(frame);
}

@@ -5366,6 +5404,19 @@ void quic::parseTransportParameters(const uint8_t* data, size_t len) {
                has_peer_retry_scid = true;
                break;
            }
            case 0x0a: {  // ack_delay_exponent — how the peer itself
                          // scales the ACK Delay field on ACK frames *it*
                          // sends us (RFC 9000 §13.2.5, §18.2); applied in
                          // processAckFrame(), never to our own outgoing
                          // ACKs (those use our own _ack_delay_exponent).
                size_t vb;
                uint64_t v = decodeVarInt(&data[offset], len - offset, vb);
                // RFC 9000 §18.2: values above 20 are invalid; ignore and
                // keep the default rather than let a malformed/hostile
                // value produce a nonsensical (2^v) shift later.
                if (vb != 0 && v <= 20) _peer_ack_delay_exponent = v;
                break;
            }
            default:
                // Skip unknown parameters
                break;
@@ -6059,6 +6110,15 @@ size_t quic::sendStreamData(uint64_t stream_id, const uint8_t* data, size_t len,

        // Piggyback ACK frame in first packet if we have pending ACKs
        if (_app_ack_pending && sent_total == 0) {
            // Real elapsed-time ACK Delay (RFC 9000 §13.2.5) instead of a
            // hardcoded 0, encoded in our own ack_delay_exponent's units —
            // see _app_ack_elicit_since's comment in socket.h and
            // sendAppAck() (the standalone-ACK path uses the same formula).
            double elapsed_sec = std::chrono::duration<double>(
                std::chrono::steady_clock::now() - _app_ack_elicit_since).count();
            if (elapsed_sec < 0) elapsed_sec = 0;
            uint64_t ack_delay_encoded = static_cast<uint64_t>(elapsed_sec * 1000000.0) >> _ack_delay_exponent;

            // Write ACK frame (type 0x02) inline
            pkt_buf[frame_off++] = 0x02; // ACK frame type
            if (!_app_pn_recv_ranges.empty()) {
@@ -6066,7 +6126,7 @@ size_t quic::sendStreamData(uint64_t stream_id, const uint8_t* data, size_t len,
                uint64_t la = it->second;

                vlen = encodeVarInt(la, vbuf); std::memcpy(pkt_buf + frame_off, vbuf, vlen); frame_off += vlen;
                vlen = encodeVarInt(0, vbuf); std::memcpy(pkt_buf + frame_off, vbuf, vlen); frame_off += vlen; // delay=0
                vlen = encodeVarInt(ack_delay_encoded, vbuf); std::memcpy(pkt_buf + frame_off, vbuf, vlen); frame_off += vlen;
                // Capped the same way as buildAckFrame() — see
                // MAX_ACK_RANGES_PER_FRAME's comment in socket.h. This
                // matters even more here than there: this writes directly
@@ -6089,7 +6149,7 @@ size_t quic::sendStreamData(uint64_t stream_id, const uint8_t* data, size_t len,
            } else {

                vlen = encodeVarInt(_app_pn_recv, vbuf); std::memcpy(pkt_buf + frame_off, vbuf, vlen); frame_off += vlen;
                vlen = encodeVarInt(0, vbuf); std::memcpy(pkt_buf + frame_off, vbuf, vlen); frame_off += vlen;
                vlen = encodeVarInt(ack_delay_encoded, vbuf); std::memcpy(pkt_buf + frame_off, vbuf, vlen); frame_off += vlen;
                vlen = encodeVarInt(0, vbuf); std::memcpy(pkt_buf + frame_off, vbuf, vlen); frame_off += vlen;
                vlen = encodeVarInt(0, vbuf); std::memcpy(pkt_buf + frame_off, vbuf, vlen); frame_off += vlen;
            }
+19 −0
Changes for src/socket.h: 19 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1234,6 +1234,24 @@ namespace netplus {
		uint64_t _pmtud_ceiling = 1472;
		uint64_t _pmtud_peer_ceiling = 1472;

		// ack_delay_exponent (RFC 9000 §18.2, transport parameter 0x0a).
		// _ack_delay_exponent is what we advertise (and what we use to
		// encode our own outgoing ACK Delay fields); _peer_ack_delay_exponent
		// is the peer's advertised value, applied when *decoding* an ACK
		// Delay field the peer sends us (RFC 9000 §13.2.5 — the field is
		// always interpreted using the sender's own advertised exponent,
		// never ours). Both default to the RFC-specified default of 3,
		// applying even if the peer's transport parameters never mention it.
		static constexpr uint64_t _ack_delay_exponent = 3;
		uint64_t _peer_ack_delay_exponent = 3;

		// Wall-clock time the earliest not-yet-acknowledged ack-eliciting
		// 1-RTT packet arrived (reset once sendAppAck() actually sends).
		// Used to encode a real ACK Delay instead of a hardcoded 0 — see
		// sendAppAck() and its call sites in processApplicationPacket(),
		// processApplicationPacketsBatch().
		std::chrono::steady_clock::time_point _app_ack_elicit_since{};

		// Receive/send queues
		std::vector<uint8_t> _recv_buffer;
		std::deque<std::vector<uint8_t>> _send_queue;
@@ -1435,6 +1453,7 @@ namespace netplus {
		// RTT estimation (RFC 9002 §5)
		double _srtt = 0.0;           // smoothed RTT (seconds)
		double _rttvar = 0.0;         // RTT variance (seconds)
		double _latest_rtt = 0.0;     // most recent RTT sample (seconds) — RFC 9002 §5.3
		double _rto = 0.5;            // retransmission timeout (seconds), initial 500ms
		bool   _rtt_initialized = false;
		// Loss detection
+1 −1
Changes for test/quic_incremental_dispatch_test.cpp: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -148,7 +148,7 @@ int main() {
    std::cout << "=== QUIC Incremental Stream Dispatch Test ===" << std::endl;

    const size_t kPayloadSize = 4096 * 1024; // 4 MB: forces many dispatch/trim cycles
    const int kQuicPort = 19547;
    const int kQuicPort = 19549;

    int rc = 0;
    try {