blob: 57a602175c20cb655b72288325c2487b46e39a78 [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>
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004#include <haproxy/xprt_quic-t.h>
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +01005
Frédéric Lécailleeb791452022-05-24 16:01:39 +02006#include <haproxy/atomic.h>
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +01007#include <haproxy/ticks.h>
8#include <haproxy/trace.h>
9
10#define TRACE_SOURCE &trace_quic
11
12/* Update <ql> QUIC loss information with new <rtt> measurement and <ack_delay>
13 * on ACK frame receipt which MUST be min(ack->ack_delay, max_ack_delay)
14 * before the handshake is confirmed.
15 */
16void quic_loss_srtt_update(struct quic_loss *ql,
17 unsigned int rtt, unsigned int ack_delay,
18 struct quic_conn *qc)
19{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +020020 TRACE_ENTER(QUIC_EV_CONN_RTTUPDT, qc);
21 TRACE_DEVEL("Loss info update", QUIC_EV_CONN_RTTUPDT, qc, &rtt, &ack_delay, ql);
22
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +010023 ql->latest_rtt = rtt;
24 if (!ql->rtt_min) {
25 /* No previous measurement. */
26 ql->srtt = rtt << 3;
27 /* rttval <- rtt / 2 or 4*rttval <- 2*rtt. */
28 ql->rtt_var = rtt << 1;
29 ql->rtt_min = rtt;
30 }
31 else {
32 int diff;
33
34 ql->rtt_min = QUIC_MIN(rtt, ql->rtt_min);
35 /* Specific to QUIC (RTT adjustment). */
36 if (ack_delay && rtt > ql->rtt_min + ack_delay)
37 rtt -= ack_delay;
38 diff = ql->srtt - rtt;
39 if (diff < 0)
40 diff = -diff;
41 /* 4*rttvar = 3*rttvar + |diff| */
42 ql->rtt_var += diff - (ql->rtt_var >> 2);
43 /* 8*srtt = 7*srtt + rtt */
44 ql->srtt += rtt - (ql->srtt >> 3);
45 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +020046
47 TRACE_DEVEL("Loss info update", QUIC_EV_CONN_RTTUPDT, qc,,, ql);
48 TRACE_LEAVE(QUIC_EV_CONN_RTTUPDT, qc);
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +010049}
50
51/* Returns for <qc> QUIC connection the first packet number space which
52 * experienced packet loss, if any or a packet number space with
53 * TICK_ETERNITY as packet loss time if not.
54 */
55struct quic_pktns *quic_loss_pktns(struct quic_conn *qc)
56{
57 enum quic_tls_pktns i;
58 struct quic_pktns *pktns;
59
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +020060 TRACE_ENTER(QUIC_EV_CONN_SPTO, qc);
61
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +010062 pktns = &qc->pktns[QUIC_TLS_PKTNS_INITIAL];
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +020063 TRACE_DEVEL("pktns", QUIC_EV_CONN_SPTO, qc, pktns);
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +010064 for (i = QUIC_TLS_PKTNS_HANDSHAKE; i < QUIC_TLS_PKTNS_MAX; i++) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +020065 TRACE_DEVEL("pktns", QUIC_EV_CONN_SPTO, qc, &qc->pktns[i]);
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +010066 if (!tick_isset(pktns->tx.loss_time) ||
67 qc->pktns[i].tx.loss_time < pktns->tx.loss_time)
68 pktns = &qc->pktns[i];
69 }
70
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +020071 TRACE_LEAVE(QUIC_EV_CONN_SPTO, qc);
72
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +010073 return pktns;
74}
75
76/* Returns for <qc> QUIC connection the first packet number space to
77 * arm the PTO for if any or a packet number space with TICK_ETERNITY
78 * as PTO value if not.
79 */
80struct quic_pktns *quic_pto_pktns(struct quic_conn *qc,
81 int handshake_completed,
82 unsigned int *pto)
83{
84 int i;
85 unsigned int duration, lpto;
86 struct quic_loss *ql = &qc->path->loss;
87 struct quic_pktns *pktns, *p;
88
89 TRACE_ENTER(QUIC_EV_CONN_SPTO, qc);
90 duration =
91 (ql->srtt >> 3) +
92 (QUIC_MAX(ql->rtt_var, QUIC_TIMER_GRANULARITY) << ql->pto_count);
93
94 if (!qc->path->in_flight) {
95 struct quic_enc_level *hel;
96
97 hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
98 if (hel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) {
99 pktns = &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE];
100 }
101 else {
102 pktns = &qc->pktns[QUIC_TLS_PKTNS_INITIAL];
103 }
104 lpto = tick_add(now_ms, duration);
105 goto out;
106 }
107
108 lpto = TICK_ETERNITY;
109 pktns = p = &qc->pktns[QUIC_TLS_PKTNS_INITIAL];
110
111 for (i = QUIC_TLS_PKTNS_INITIAL; i < QUIC_TLS_PKTNS_MAX; i++) {
112 unsigned int tmp_pto;
113
114 if (!qc->pktns[i].tx.in_flight)
115 continue;
116
117 if (i == QUIC_TLS_PKTNS_01RTT) {
118 if (!handshake_completed) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200119 TRACE_STATE("handshake not already completed", QUIC_EV_CONN_SPTO, qc);
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +0100120 pktns = p;
121 goto out;
122 }
123
124 duration += qc->max_ack_delay << ql->pto_count;
125 }
126
127 p = &qc->pktns[i];
128 tmp_pto = tick_add(p->tx.time_of_last_eliciting, duration);
129 if (!tick_isset(lpto) || tmp_pto < lpto) {
130 lpto = tmp_pto;
131 pktns = p;
132 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200133 TRACE_DEVEL("pktns", QUIC_EV_CONN_SPTO, qc, p);
Amaury Denoyelle0c2d9642022-03-24 16:08:05 +0100134 }
135
136 out:
137 if (pto)
138 *pto = lpto;
139 TRACE_LEAVE(QUIC_EV_CONN_SPTO, qc, pktns, &duration);
140
141 return pktns;
142}
Frédéric Lécaille16013952022-04-29 15:07:48 +0200143
144/* Look for packet loss from sent packets for <qel> encryption level of a
145 * connection with <ctx> as I/O handler context. If remove is true, remove them from
146 * their tree if deemed as lost or set the <loss_time> value the packet number
147 * space if any not deemed lost.
148 * Should be called after having received an ACK frame with newly acknowledged
149 * packets or when the the loss detection timer has expired.
150 * Always succeeds.
151 */
152void qc_packet_loss_lookup(struct quic_pktns *pktns, struct quic_conn *qc,
153 struct list *lost_pkts)
154{
155 struct eb_root *pkts;
156 struct eb64_node *node;
157 struct quic_loss *ql;
158 unsigned int loss_delay;
159
160 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns);
161 pkts = &pktns->tx.pkts;
162 pktns->tx.loss_time = TICK_ETERNITY;
163 if (eb_is_empty(pkts))
164 goto out;
165
166 ql = &qc->path->loss;
167 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
Frédéric Lécaillec40e19d2022-04-29 16:00:17 +0200168 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY)) *
169 QUIC_LOSS_TIME_THRESHOLD_MULTIPLICAND / QUIC_LOSS_TIME_THRESHOLD_DIVISOR;
Frédéric Lécaille16013952022-04-29 15:07:48 +0200170
171 node = eb64_first(pkts);
172 while (node) {
173 struct quic_tx_packet *pkt;
174 int64_t largest_acked_pn;
175 unsigned int loss_time_limit, time_sent;
176
177 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
178 largest_acked_pn = pktns->rx.largest_acked_pn;
179 node = eb64_next(node);
180 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
181 break;
182
183 time_sent = pkt->time_sent;
184 loss_time_limit = tick_add(time_sent, loss_delay);
Frédéric Lécailled2e104f2022-07-21 09:55:15 +0200185 if (tick_is_le(loss_time_limit, now_ms) ||
Frédéric Lécaille16013952022-04-29 15:07:48 +0200186 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
187 eb64_delete(&pkt->pn_node);
188 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécailleeb791452022-05-24 16:01:39 +0200189 HA_ATOMIC_INC(&qc->prx_counters->lost_pkt);
Frédéric Lécaille16013952022-04-29 15:07:48 +0200190 }
191 else {
192 if (tick_isset(pktns->tx.loss_time))
193 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
194 else
195 pktns->tx.loss_time = loss_time_limit;
Frédéric Lécailledc591cd2022-07-21 14:24:41 +0200196 break;
Frédéric Lécaille16013952022-04-29 15:07:48 +0200197 }
198 }
199
200 out:
201 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts);
202}
203