blob: 2f6643c7cbf77b74e8e4c2c322a69ccbcd8d6c1b [file] [log] [blame]
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +01001#include <haproxy/quic_loss.h>
2
3#include <haproxy/ticks.h>
4#include <haproxy/trace.h>
5
6#define TRACE_SOURCE &trace_quic
7
8/* Update <ql> QUIC loss information with new <rtt> measurement and <ack_delay>
9 * on ACK frame receipt which MUST be min(ack->ack_delay, max_ack_delay)
10 * before the handshake is confirmed.
11 */
12void quic_loss_srtt_update(struct quic_loss *ql,
13 unsigned int rtt, unsigned int ack_delay,
14 struct quic_conn *qc)
15{
16 TRACE_PROTO("Loss info update", QUIC_EV_CONN_RTTUPDT, qc, &rtt, &ack_delay, ql);
17 ql->latest_rtt = rtt;
18 if (!ql->rtt_min) {
19 /* No previous measurement. */
20 ql->srtt = rtt << 3;
21 /* rttval <- rtt / 2 or 4*rttval <- 2*rtt. */
22 ql->rtt_var = rtt << 1;
23 ql->rtt_min = rtt;
24 }
25 else {
26 int diff;
27
28 ql->rtt_min = QUIC_MIN(rtt, ql->rtt_min);
29 /* Specific to QUIC (RTT adjustment). */
30 if (ack_delay && rtt > ql->rtt_min + ack_delay)
31 rtt -= ack_delay;
32 diff = ql->srtt - rtt;
33 if (diff < 0)
34 diff = -diff;
35 /* 4*rttvar = 3*rttvar + |diff| */
36 ql->rtt_var += diff - (ql->rtt_var >> 2);
37 /* 8*srtt = 7*srtt + rtt */
38 ql->srtt += rtt - (ql->srtt >> 3);
39 }
40 TRACE_PROTO("Loss info update", QUIC_EV_CONN_RTTUPDT, qc,,, ql);
41}
42
43/* Returns for <qc> QUIC connection the first packet number space which
44 * experienced packet loss, if any or a packet number space with
45 * TICK_ETERNITY as packet loss time if not.
46 */
47struct quic_pktns *quic_loss_pktns(struct quic_conn *qc)
48{
49 enum quic_tls_pktns i;
50 struct quic_pktns *pktns;
51
52 pktns = &qc->pktns[QUIC_TLS_PKTNS_INITIAL];
53 TRACE_PROTO("pktns", QUIC_EV_CONN_SPTO, qc, pktns);
54 for (i = QUIC_TLS_PKTNS_HANDSHAKE; i < QUIC_TLS_PKTNS_MAX; i++) {
55 TRACE_PROTO("pktns", QUIC_EV_CONN_SPTO, qc, &qc->pktns[i]);
56 if (!tick_isset(pktns->tx.loss_time) ||
57 qc->pktns[i].tx.loss_time < pktns->tx.loss_time)
58 pktns = &qc->pktns[i];
59 }
60
61 return pktns;
62}
63
64/* Returns for <qc> QUIC connection the first packet number space to
65 * arm the PTO for if any or a packet number space with TICK_ETERNITY
66 * as PTO value if not.
67 */
68struct quic_pktns *quic_pto_pktns(struct quic_conn *qc,
69 int handshake_completed,
70 unsigned int *pto)
71{
72 int i;
73 unsigned int duration, lpto;
74 struct quic_loss *ql = &qc->path->loss;
75 struct quic_pktns *pktns, *p;
76
77 TRACE_ENTER(QUIC_EV_CONN_SPTO, qc);
78 duration =
79 (ql->srtt >> 3) +
80 (QUIC_MAX(ql->rtt_var, QUIC_TIMER_GRANULARITY) << ql->pto_count);
81
82 if (!qc->path->in_flight) {
83 struct quic_enc_level *hel;
84
85 hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
86 if (hel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) {
87 pktns = &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE];
88 }
89 else {
90 pktns = &qc->pktns[QUIC_TLS_PKTNS_INITIAL];
91 }
92 lpto = tick_add(now_ms, duration);
93 goto out;
94 }
95
96 lpto = TICK_ETERNITY;
97 pktns = p = &qc->pktns[QUIC_TLS_PKTNS_INITIAL];
98
99 for (i = QUIC_TLS_PKTNS_INITIAL; i < QUIC_TLS_PKTNS_MAX; i++) {
100 unsigned int tmp_pto;
101
102 if (!qc->pktns[i].tx.in_flight)
103 continue;
104
105 if (i == QUIC_TLS_PKTNS_01RTT) {
106 if (!handshake_completed) {
107 pktns = p;
108 goto out;
109 }
110
111 duration += qc->max_ack_delay << ql->pto_count;
112 }
113
114 p = &qc->pktns[i];
115 tmp_pto = tick_add(p->tx.time_of_last_eliciting, duration);
116 if (!tick_isset(lpto) || tmp_pto < lpto) {
117 lpto = tmp_pto;
118 pktns = p;
119 }
120 TRACE_PROTO("pktns", QUIC_EV_CONN_SPTO, qc, p);
121 }
122
123 out:
124 if (pto)
125 *pto = lpto;
126 TRACE_LEAVE(QUIC_EV_CONN_SPTO, qc, pktns, &duration);
127
128 return pktns;
129}