blob: 42eb5a2515a7061175f0e38ae711abcb84d0f0c5 [file] [log] [blame]
Frédéric Lécaille16013952022-04-29 15:07:48 +02001#include <import/eb64tree.h>
2
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +01003#include <haproxy/quic_loss.h>
4
5#include <haproxy/ticks.h>
6#include <haproxy/trace.h>
7
8#define TRACE_SOURCE &trace_quic
9
10/* Update <ql> QUIC loss information with new <rtt> measurement and <ack_delay>
11 * on ACK frame receipt which MUST be min(ack->ack_delay, max_ack_delay)
12 * before the handshake is confirmed.
13 */
14void quic_loss_srtt_update(struct quic_loss *ql,
15 unsigned int rtt, unsigned int ack_delay,
16 struct quic_conn *qc)
17{
18 TRACE_PROTO("Loss info update", QUIC_EV_CONN_RTTUPDT, qc, &rtt, &ack_delay, ql);
19 ql->latest_rtt = rtt;
20 if (!ql->rtt_min) {
21 /* No previous measurement. */
22 ql->srtt = rtt << 3;
23 /* rttval <- rtt / 2 or 4*rttval <- 2*rtt. */
24 ql->rtt_var = rtt << 1;
25 ql->rtt_min = rtt;
26 }
27 else {
28 int diff;
29
30 ql->rtt_min = QUIC_MIN(rtt, ql->rtt_min);
31 /* Specific to QUIC (RTT adjustment). */
32 if (ack_delay && rtt > ql->rtt_min + ack_delay)
33 rtt -= ack_delay;
34 diff = ql->srtt - rtt;
35 if (diff < 0)
36 diff = -diff;
37 /* 4*rttvar = 3*rttvar + |diff| */
38 ql->rtt_var += diff - (ql->rtt_var >> 2);
39 /* 8*srtt = 7*srtt + rtt */
40 ql->srtt += rtt - (ql->srtt >> 3);
41 }
42 TRACE_PROTO("Loss info update", QUIC_EV_CONN_RTTUPDT, qc,,, ql);
43}
44
45/* Returns for <qc> QUIC connection the first packet number space which
46 * experienced packet loss, if any or a packet number space with
47 * TICK_ETERNITY as packet loss time if not.
48 */
49struct quic_pktns *quic_loss_pktns(struct quic_conn *qc)
50{
51 enum quic_tls_pktns i;
52 struct quic_pktns *pktns;
53
54 pktns = &qc->pktns[QUIC_TLS_PKTNS_INITIAL];
55 TRACE_PROTO("pktns", QUIC_EV_CONN_SPTO, qc, pktns);
56 for (i = QUIC_TLS_PKTNS_HANDSHAKE; i < QUIC_TLS_PKTNS_MAX; i++) {
57 TRACE_PROTO("pktns", QUIC_EV_CONN_SPTO, qc, &qc->pktns[i]);
58 if (!tick_isset(pktns->tx.loss_time) ||
59 qc->pktns[i].tx.loss_time < pktns->tx.loss_time)
60 pktns = &qc->pktns[i];
61 }
62
63 return pktns;
64}
65
66/* Returns for <qc> QUIC connection the first packet number space to
67 * arm the PTO for if any or a packet number space with TICK_ETERNITY
68 * as PTO value if not.
69 */
70struct quic_pktns *quic_pto_pktns(struct quic_conn *qc,
71 int handshake_completed,
72 unsigned int *pto)
73{
74 int i;
75 unsigned int duration, lpto;
76 struct quic_loss *ql = &qc->path->loss;
77 struct quic_pktns *pktns, *p;
78
79 TRACE_ENTER(QUIC_EV_CONN_SPTO, qc);
80 duration =
81 (ql->srtt >> 3) +
82 (QUIC_MAX(ql->rtt_var, QUIC_TIMER_GRANULARITY) << ql->pto_count);
83
84 if (!qc->path->in_flight) {
85 struct quic_enc_level *hel;
86
87 hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
88 if (hel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) {
89 pktns = &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE];
90 }
91 else {
92 pktns = &qc->pktns[QUIC_TLS_PKTNS_INITIAL];
93 }
94 lpto = tick_add(now_ms, duration);
95 goto out;
96 }
97
98 lpto = TICK_ETERNITY;
99 pktns = p = &qc->pktns[QUIC_TLS_PKTNS_INITIAL];
100
101 for (i = QUIC_TLS_PKTNS_INITIAL; i < QUIC_TLS_PKTNS_MAX; i++) {
102 unsigned int tmp_pto;
103
104 if (!qc->pktns[i].tx.in_flight)
105 continue;
106
107 if (i == QUIC_TLS_PKTNS_01RTT) {
108 if (!handshake_completed) {
109 pktns = p;
110 goto out;
111 }
112
113 duration += qc->max_ack_delay << ql->pto_count;
114 }
115
116 p = &qc->pktns[i];
117 tmp_pto = tick_add(p->tx.time_of_last_eliciting, duration);
118 if (!tick_isset(lpto) || tmp_pto < lpto) {
119 lpto = tmp_pto;
120 pktns = p;
121 }
122 TRACE_PROTO("pktns", QUIC_EV_CONN_SPTO, qc, p);
123 }
124
125 out:
126 if (pto)
127 *pto = lpto;
128 TRACE_LEAVE(QUIC_EV_CONN_SPTO, qc, pktns, &duration);
129
130 return pktns;
131}
Frédéric Lécaille16013952022-04-29 15:07:48 +0200132
133/* Look for packet loss from sent packets for <qel> encryption level of a
134 * connection with <ctx> as I/O handler context. If remove is true, remove them from
135 * their tree if deemed as lost or set the <loss_time> value the packet number
136 * space if any not deemed lost.
137 * Should be called after having received an ACK frame with newly acknowledged
138 * packets or when the the loss detection timer has expired.
139 * Always succeeds.
140 */
141void qc_packet_loss_lookup(struct quic_pktns *pktns, struct quic_conn *qc,
142 struct list *lost_pkts)
143{
144 struct eb_root *pkts;
145 struct eb64_node *node;
146 struct quic_loss *ql;
147 unsigned int loss_delay;
148
149 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns);
150 pkts = &pktns->tx.pkts;
151 pktns->tx.loss_time = TICK_ETERNITY;
152 if (eb_is_empty(pkts))
153 goto out;
154
155 ql = &qc->path->loss;
156 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
Frédéric Lécaillec40e19d2022-04-29 16:00:17 +0200157 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY)) *
158 QUIC_LOSS_TIME_THRESHOLD_MULTIPLICAND / QUIC_LOSS_TIME_THRESHOLD_DIVISOR;
Frédéric Lécaille16013952022-04-29 15:07:48 +0200159
160 node = eb64_first(pkts);
161 while (node) {
162 struct quic_tx_packet *pkt;
163 int64_t largest_acked_pn;
164 unsigned int loss_time_limit, time_sent;
165
166 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
167 largest_acked_pn = pktns->rx.largest_acked_pn;
168 node = eb64_next(node);
169 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
170 break;
171
172 time_sent = pkt->time_sent;
173 loss_time_limit = tick_add(time_sent, loss_delay);
174 if (tick_is_le(time_sent, now_ms) ||
175 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
176 eb64_delete(&pkt->pn_node);
177 LIST_APPEND(lost_pkts, &pkt->list);
178 }
179 else {
180 if (tick_isset(pktns->tx.loss_time))
181 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
182 else
183 pktns->tx.loss_time = loss_time_limit;
184 }
185 }
186
187 out:
188 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts);
189}
190