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