blob: bb518ca253972a744e8b740430b0cd923a9b9c83 [file] [log] [blame]
Frédéric Lécaille16013952022-04-29 15:07:48 +02001#include <import/eb64tree.h>
2
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003#include <haproxy/quic_conn-t.h>
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +01004#include <haproxy/quic_loss.h>
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005#include <haproxy/quic_tls.h>
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +01006
Frédéric Lécailleeb791452022-05-24 16:01:39 +02007#include <haproxy/atomic.h>
Amaury Denoyelle5c25dc52022-09-30 17:44:15 +02008#include <haproxy/list.h>
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +01009#include <haproxy/ticks.h>
10#include <haproxy/trace.h>
11
12#define TRACE_SOURCE &trace_quic
13
14/* Update <ql> QUIC loss information with new <rtt> measurement and <ack_delay>
15 * on ACK frame receipt which MUST be min(ack->ack_delay, max_ack_delay)
16 * before the handshake is confirmed.
17 */
18void quic_loss_srtt_update(struct quic_loss *ql,
19 unsigned int rtt, unsigned int ack_delay,
20 struct quic_conn *qc)
21{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +020022 TRACE_ENTER(QUIC_EV_CONN_RTTUPDT, qc);
Frédéric Lécaille8f991942023-03-24 15:14:45 +010023 TRACE_PROTO("TX loss srtt update", QUIC_EV_CONN_RTTUPDT, qc, &rtt, &ack_delay, ql);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +020024
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +010025 ql->latest_rtt = rtt;
26 if (!ql->rtt_min) {
27 /* No previous measurement. */
28 ql->srtt = rtt << 3;
29 /* rttval <- rtt / 2 or 4*rttval <- 2*rtt. */
30 ql->rtt_var = rtt << 1;
31 ql->rtt_min = rtt;
32 }
33 else {
34 int diff;
35
36 ql->rtt_min = QUIC_MIN(rtt, ql->rtt_min);
37 /* Specific to QUIC (RTT adjustment). */
38 if (ack_delay && rtt > ql->rtt_min + ack_delay)
39 rtt -= ack_delay;
Frédéric Lécaille5d5afe72023-03-30 15:31:06 +020040 diff = (ql->srtt >> 3) - rtt;
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +010041 if (diff < 0)
42 diff = -diff;
43 /* 4*rttvar = 3*rttvar + |diff| */
44 ql->rtt_var += diff - (ql->rtt_var >> 2);
45 /* 8*srtt = 7*srtt + rtt */
46 ql->srtt += rtt - (ql->srtt >> 3);
47 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +020048
Frédéric Lécaille8f991942023-03-24 15:14:45 +010049 TRACE_PROTO("TX loss srtt update", QUIC_EV_CONN_RTTUPDT, qc,,, ql);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +020050 TRACE_LEAVE(QUIC_EV_CONN_RTTUPDT, qc);
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +010051}
52
53/* Returns for <qc> QUIC connection the first packet number space which
54 * experienced packet loss, if any or a packet number space with
55 * TICK_ETERNITY as packet loss time if not.
56 */
57struct quic_pktns *quic_loss_pktns(struct quic_conn *qc)
58{
59 enum quic_tls_pktns i;
60 struct quic_pktns *pktns;
61
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +020062 TRACE_ENTER(QUIC_EV_CONN_SPTO, qc);
63
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +010064 pktns = &qc->pktns[QUIC_TLS_PKTNS_INITIAL];
Frédéric Lécaille8f991942023-03-24 15:14:45 +010065 TRACE_PROTO("TX loss pktns", QUIC_EV_CONN_SPTO, qc, pktns);
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +010066 for (i = QUIC_TLS_PKTNS_HANDSHAKE; i < QUIC_TLS_PKTNS_MAX; i++) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +010067 TRACE_PROTO("TX loss pktns", QUIC_EV_CONN_SPTO, qc, &qc->pktns[i]);
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +010068 if (!tick_isset(pktns->tx.loss_time) ||
69 qc->pktns[i].tx.loss_time < pktns->tx.loss_time)
70 pktns = &qc->pktns[i];
71 }
72
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +020073 TRACE_LEAVE(QUIC_EV_CONN_SPTO, qc);
74
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +010075 return pktns;
76}
77
78/* Returns for <qc> QUIC connection the first packet number space to
79 * arm the PTO for if any or a packet number space with TICK_ETERNITY
80 * as PTO value if not.
81 */
82struct quic_pktns *quic_pto_pktns(struct quic_conn *qc,
Frédéric Lécailleb75eecc2023-01-26 15:18:17 +010083 int handshake_confirmed,
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +010084 unsigned int *pto)
85{
86 int i;
87 unsigned int duration, lpto;
88 struct quic_loss *ql = &qc->path->loss;
89 struct quic_pktns *pktns, *p;
90
91 TRACE_ENTER(QUIC_EV_CONN_SPTO, qc);
92 duration =
93 (ql->srtt >> 3) +
94 (QUIC_MAX(ql->rtt_var, QUIC_TIMER_GRANULARITY) << ql->pto_count);
95
96 if (!qc->path->in_flight) {
97 struct quic_enc_level *hel;
98
99 hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200100 if (quic_tls_has_tx_sec(hel)) {
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +0100101 pktns = &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE];
102 }
103 else {
104 pktns = &qc->pktns[QUIC_TLS_PKTNS_INITIAL];
105 }
106 lpto = tick_add(now_ms, duration);
107 goto out;
108 }
109
110 lpto = TICK_ETERNITY;
111 pktns = p = &qc->pktns[QUIC_TLS_PKTNS_INITIAL];
112
113 for (i = QUIC_TLS_PKTNS_INITIAL; i < QUIC_TLS_PKTNS_MAX; i++) {
114 unsigned int tmp_pto;
115
116 if (!qc->pktns[i].tx.in_flight)
117 continue;
118
119 if (i == QUIC_TLS_PKTNS_01RTT) {
Frédéric Lécailleb75eecc2023-01-26 15:18:17 +0100120 if (!handshake_confirmed) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100121 TRACE_STATE("TX PTO handshake not already completed", QUIC_EV_CONN_SPTO, qc);
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +0100122 pktns = p;
123 goto out;
124 }
125
126 duration += qc->max_ack_delay << ql->pto_count;
127 }
128
129 p = &qc->pktns[i];
130 tmp_pto = tick_add(p->tx.time_of_last_eliciting, duration);
Frédéric Lécaille0222cc62023-04-04 15:47:16 +0200131 if (!tick_isset(lpto) || tick_is_lt(tmp_pto, lpto)) {
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +0100132 lpto = tmp_pto;
133 pktns = p;
134 }
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100135 TRACE_PROTO("TX PTO", QUIC_EV_CONN_SPTO, qc, p);
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +0100136 }
137
138 out:
139 if (pto)
140 *pto = lpto;
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100141 TRACE_PROTO("TX PTO", QUIC_EV_CONN_SPTO, qc, pktns, &duration);
142 TRACE_LEAVE(QUIC_EV_CONN_SPTO, qc);
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +0100143
144 return pktns;
145}
Frédéric Lécaille16013952022-04-29 15:07:48 +0200146
147/* Look for packet loss from sent packets for <qel> encryption level of a
148 * connection with <ctx> as I/O handler context. If remove is true, remove them from
149 * their tree if deemed as lost or set the <loss_time> value the packet number
150 * space if any not deemed lost.
151 * Should be called after having received an ACK frame with newly acknowledged
152 * packets or when the the loss detection timer has expired.
153 * Always succeeds.
154 */
155void qc_packet_loss_lookup(struct quic_pktns *pktns, struct quic_conn *qc,
156 struct list *lost_pkts)
157{
158 struct eb_root *pkts;
159 struct eb64_node *node;
160 struct quic_loss *ql;
161 unsigned int loss_delay;
162
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100163 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc);
164 TRACE_PROTO("TX loss", QUIC_EV_CONN_PKTLOSS, qc, pktns);
Frédéric Lécaille16013952022-04-29 15:07:48 +0200165 pkts = &pktns->tx.pkts;
166 pktns->tx.loss_time = TICK_ETERNITY;
167 if (eb_is_empty(pkts))
168 goto out;
169
170 ql = &qc->path->loss;
171 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
Frédéric Lécaillec40e19d2022-04-29 16:00:17 +0200172 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY)) *
173 QUIC_LOSS_TIME_THRESHOLD_MULTIPLICAND / QUIC_LOSS_TIME_THRESHOLD_DIVISOR;
Frédéric Lécaille16013952022-04-29 15:07:48 +0200174
175 node = eb64_first(pkts);
176 while (node) {
177 struct quic_tx_packet *pkt;
178 int64_t largest_acked_pn;
179 unsigned int loss_time_limit, time_sent;
180
181 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
182 largest_acked_pn = pktns->rx.largest_acked_pn;
183 node = eb64_next(node);
184 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
185 break;
186
187 time_sent = pkt->time_sent;
188 loss_time_limit = tick_add(time_sent, loss_delay);
Frédéric Lécailled2e104f2022-07-21 09:55:15 +0200189 if (tick_is_le(loss_time_limit, now_ms) ||
Frédéric Lécaille16013952022-04-29 15:07:48 +0200190 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
191 eb64_delete(&pkt->pn_node);
192 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécailleeb791452022-05-24 16:01:39 +0200193 HA_ATOMIC_INC(&qc->prx_counters->lost_pkt);
Frédéric Lécaille16013952022-04-29 15:07:48 +0200194 }
195 else {
196 if (tick_isset(pktns->tx.loss_time))
197 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
198 else
199 pktns->tx.loss_time = loss_time_limit;
Frédéric Lécailledc591cd2022-07-21 14:24:41 +0200200 break;
Frédéric Lécaille16013952022-04-29 15:07:48 +0200201 }
202 }
203
204 out:
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100205 TRACE_PROTO("TX loss", QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts);
206 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc);
Frédéric Lécaille16013952022-04-29 15:07:48 +0200207}
208