/src/openssl36/ssl/quic/quic_ackm.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2022-2025 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include "internal/quic_ackm.h" |
11 | | #include "internal/uint_set.h" |
12 | | #include "internal/common.h" |
13 | | #include <assert.h> |
14 | | |
15 | 105M | DEFINE_LIST_OF(tx_history, OSSL_ACKM_TX_PKT); quic_ackm.c:ossl_list_tx_history_prev Line | Count | Source | 15 | | DEFINE_LIST_OF(tx_history, OSSL_ACKM_TX_PKT); |
quic_ackm.c:ossl_list_tx_history_insert_tail Line | Count | Source | 15 | | DEFINE_LIST_OF(tx_history, OSSL_ACKM_TX_PKT); |
quic_ackm.c:ossl_list_tx_history_head Line | Count | Source | 15 | | DEFINE_LIST_OF(tx_history, OSSL_ACKM_TX_PKT); |
quic_ackm.c:ossl_list_tx_history_next Line | Count | Source | 15 | | DEFINE_LIST_OF(tx_history, OSSL_ACKM_TX_PKT); |
quic_ackm.c:ossl_list_tx_history_tail Line | Count | Source | 15 | | DEFINE_LIST_OF(tx_history, OSSL_ACKM_TX_PKT); |
quic_ackm.c:ossl_list_tx_history_remove Line | Count | Source | 15 | | DEFINE_LIST_OF(tx_history, OSSL_ACKM_TX_PKT); |
|
16 | 105M | |
17 | 105M | /* |
18 | 105M | * TX Packet History |
19 | 105M | * ***************** |
20 | 105M | * |
21 | 105M | * The TX Packet History object tracks information about packets which have been |
22 | 105M | * sent for which we later expect to receive an ACK. It is essentially a simple |
23 | 105M | * database keeping a list of packet information structures in packet number |
24 | 105M | * order which can also be looked up directly by packet number. |
25 | 105M | * |
26 | 105M | * We currently only allow packets to be appended to the list (i.e. the packet |
27 | 105M | * numbers of the packets appended to the list must monotonically increase), as |
28 | 105M | * we should not currently need more general functionality such as a sorted list |
29 | 105M | * insert. |
30 | 105M | */ |
31 | 105M | struct tx_pkt_history_st { |
32 | 105M | /* A linked list of all our packets. */ |
33 | 105M | OSSL_LIST(tx_history) |
34 | 105M | packets; |
35 | 105M | |
36 | 105M | /* |
37 | 105M | * Mapping from packet numbers (uint64_t) to (OSSL_ACKM_TX_PKT *) |
38 | 105M | * |
39 | 105M | * Invariant: A packet is in this map if and only if it is in the linked |
40 | 105M | * list. |
41 | 105M | */ |
42 | 105M | LHASH_OF(OSSL_ACKM_TX_PKT) *map; |
43 | 105M | |
44 | 105M | /* |
45 | 105M | * The lowest packet number which may currently be added to the history list |
46 | 105M | * (inclusive). We do not allow packet numbers to be added to the history |
47 | 105M | * list non-monotonically, so packet numbers must be greater than or equal |
48 | 105M | * to this value. |
49 | 105M | */ |
50 | 105M | uint64_t watermark; |
51 | 105M | |
52 | 105M | /* |
53 | 105M | * Packet number of the highest packet info structure we have yet appended |
54 | 105M | * to the list. This is usually one less than watermark, except when we have |
55 | 105M | * not added any packet yet. |
56 | 105M | */ |
57 | 105M | uint64_t highest_sent; |
58 | 105M | }; |
59 | 105M | |
60 | 105M | DEFINE_LHASH_OF_EX(OSSL_ACKM_TX_PKT); |
61 | 105M | |
62 | 105M | static unsigned long tx_pkt_info_hash(const OSSL_ACKM_TX_PKT *pkt) |
63 | 105M | { |
64 | | /* Using low bits of the packet number as the hash should be enough */ |
65 | 6.89M | return (unsigned long)pkt->pkt_num; |
66 | 6.89M | } |
67 | | |
68 | | static int tx_pkt_info_compare(const OSSL_ACKM_TX_PKT *a, |
69 | | const OSSL_ACKM_TX_PKT *b) |
70 | 2.52M | { |
71 | 2.52M | if (a->pkt_num < b->pkt_num) |
72 | 0 | return -1; |
73 | 2.52M | if (a->pkt_num > b->pkt_num) |
74 | 0 | return 1; |
75 | 2.52M | return 0; |
76 | 2.52M | } |
77 | | |
78 | | static int |
79 | | tx_pkt_history_init(struct tx_pkt_history_st *h) |
80 | 151k | { |
81 | 151k | ossl_list_tx_history_init(&h->packets); |
82 | 151k | h->watermark = 0; |
83 | 151k | h->highest_sent = 0; |
84 | | |
85 | 151k | h->map = lh_OSSL_ACKM_TX_PKT_new(tx_pkt_info_hash, tx_pkt_info_compare); |
86 | 151k | if (h->map == NULL) |
87 | 0 | return 0; |
88 | | |
89 | 151k | return 1; |
90 | 151k | } |
91 | | |
92 | | static void |
93 | | tx_pkt_history_destroy(struct tx_pkt_history_st *h) |
94 | 151k | { |
95 | 151k | lh_OSSL_ACKM_TX_PKT_free(h->map); |
96 | 151k | h->map = NULL; |
97 | 151k | ossl_list_tx_history_init(&h->packets); |
98 | 151k | } |
99 | | |
100 | | static int |
101 | | tx_pkt_history_add_actual(struct tx_pkt_history_st *h, |
102 | | OSSL_ACKM_TX_PKT *pkt) |
103 | 2.07M | { |
104 | 2.07M | OSSL_ACKM_TX_PKT *existing; |
105 | | |
106 | | /* |
107 | | * There should not be any existing packet with this number |
108 | | * in our mapping. |
109 | | */ |
110 | 2.07M | existing = lh_OSSL_ACKM_TX_PKT_retrieve(h->map, pkt); |
111 | 2.07M | if (!ossl_assert(existing == NULL)) |
112 | 0 | return 0; |
113 | | |
114 | | /* Should not already be in a list. */ |
115 | 2.07M | if (!ossl_assert(ossl_list_tx_history_next(pkt) == NULL |
116 | 2.07M | && ossl_list_tx_history_prev(pkt) == NULL)) |
117 | 0 | return 0; |
118 | | |
119 | 2.07M | lh_OSSL_ACKM_TX_PKT_insert(h->map, pkt); |
120 | | |
121 | 2.07M | ossl_list_tx_history_insert_tail(&h->packets, pkt); |
122 | 2.07M | return 1; |
123 | 2.07M | } |
124 | | |
125 | | /* Adds a packet information structure to the history list. */ |
126 | | static int |
127 | | tx_pkt_history_add(struct tx_pkt_history_st *h, |
128 | | OSSL_ACKM_TX_PKT *pkt) |
129 | 2.07M | { |
130 | 2.07M | if (!ossl_assert(pkt->pkt_num >= h->watermark)) |
131 | 0 | return 0; |
132 | | |
133 | 2.07M | if (tx_pkt_history_add_actual(h, pkt) < 1) |
134 | 0 | return 0; |
135 | | |
136 | 2.07M | h->watermark = pkt->pkt_num + 1; |
137 | 2.07M | h->highest_sent = pkt->pkt_num; |
138 | 2.07M | return 1; |
139 | 2.07M | } |
140 | | |
141 | | /* Retrieve a packet information structure by packet number. */ |
142 | | static OSSL_ACKM_TX_PKT * |
143 | | tx_pkt_history_by_pkt_num(struct tx_pkt_history_st *h, uint64_t pkt_num) |
144 | 1.51M | { |
145 | 1.51M | OSSL_ACKM_TX_PKT key; |
146 | | |
147 | 1.51M | key.pkt_num = pkt_num; |
148 | | |
149 | 1.51M | return lh_OSSL_ACKM_TX_PKT_retrieve(h->map, &key); |
150 | 1.51M | } |
151 | | |
152 | | /* Remove a packet information structure from the history log. */ |
153 | | static int |
154 | | tx_pkt_history_remove(struct tx_pkt_history_st *h, uint64_t pkt_num) |
155 | 1.23M | { |
156 | 1.23M | OSSL_ACKM_TX_PKT key, *pkt; |
157 | 1.23M | key.pkt_num = pkt_num; |
158 | | |
159 | 1.23M | pkt = tx_pkt_history_by_pkt_num(h, pkt_num); |
160 | 1.23M | if (pkt == NULL) |
161 | 0 | return 0; |
162 | | |
163 | 1.23M | ossl_list_tx_history_remove(&h->packets, pkt); |
164 | 1.23M | lh_OSSL_ACKM_TX_PKT_delete(h->map, &key); |
165 | 1.23M | return 1; |
166 | 1.23M | } |
167 | | |
168 | | /* |
169 | | * RX Packet Number Tracking |
170 | | * ************************* |
171 | | * |
172 | | * **Background.** The RX side of the ACK manager must track packets we have |
173 | | * received for which we have to generate ACK frames. Broadly, this means we |
174 | | * store a set of packet numbers which we have received but which we do not know |
175 | | * for a fact that the transmitter knows we have received. |
176 | | * |
177 | | * This must handle various situations: |
178 | | * |
179 | | * 1. We receive a packet but have not sent an ACK yet, so the transmitter |
180 | | * does not know whether we have received it or not yet. |
181 | | * |
182 | | * 2. We receive a packet and send an ACK which is lost. We do not |
183 | | * immediately know that the ACK was lost and the transmitter does not know |
184 | | * that we have received the packet. |
185 | | * |
186 | | * 3. We receive a packet and send an ACK which is received by the |
187 | | * transmitter. The transmitter does not immediately respond with an ACK, |
188 | | * or responds with an ACK which is lost. The transmitter knows that we |
189 | | * have received the packet, but we do not know for sure that it knows, |
190 | | * because the ACK we sent could have been lost. |
191 | | * |
192 | | * 4. We receive a packet and send an ACK which is received by the |
193 | | * transmitter. The transmitter subsequently sends us an ACK which confirms |
194 | | * its receipt of the ACK we sent, and we successfully receive that ACK, so |
195 | | * we know that the transmitter knows, that we received the original |
196 | | * packet. |
197 | | * |
198 | | * Only when we reach case (4) are we relieved of any need to track a given |
199 | | * packet number we have received, because only in this case do we know for sure |
200 | | * that the peer knows we have received the packet. Having reached case (4) we |
201 | | * will never again need to generate an ACK containing the PN in question, but |
202 | | * until we reach that point, we must keep track of the PN as not having been |
203 | | * provably ACKed, as we may have to keep generating ACKs for the given PN not |
204 | | * just until the transmitter receives one, but until we know that it has |
205 | | * received one. This will be referred to herein as "provably ACKed". |
206 | | * |
207 | | * **Duplicate handling.** The above discusses the case where we have received a |
208 | | * packet with a given PN but are at best unsure whether the sender knows we |
209 | | * have received it or not. However, we must also handle the case where we have |
210 | | * yet to receive a packet with a given PN in the first place. The reason for |
211 | | * this is because of the requirement expressed by RFC 9000 s. 12.3: |
212 | | * |
213 | | * "A receiver MUST discard a newly unprotected packet unless it is certain |
214 | | * that it has not processed another packet with the same packet number from |
215 | | * the same packet number space." |
216 | | * |
217 | | * We must ensure we never process a duplicate PN. As such, each possible PN we |
218 | | * can receive must exist in one of the following logical states: |
219 | | * |
220 | | * - We have never processed this PN before |
221 | | * (so if we receive such a PN, it can be processed) |
222 | | * |
223 | | * - We have processed this PN but it has not yet been provably ACKed |
224 | | * (and should therefore be in any future ACK frame generated; |
225 | | * if we receive such a PN again, it must be ignored) |
226 | | * |
227 | | * - We have processed this PN and it has been provably ACKed |
228 | | * (if we receive such a PN again, it must be ignored) |
229 | | * |
230 | | * However, if we were to track this state for every PN ever used in the history |
231 | | * of a connection, the amount of state required would increase unboundedly as |
232 | | * the connection goes on (for example, we would have to store a set of every PN |
233 | | * ever received.) |
234 | | * |
235 | | * RFC 9000 s. 12.3 continues: |
236 | | * |
237 | | * "Endpoints that track all individual packets for the purposes of detecting |
238 | | * duplicates are at risk of accumulating excessive state. The data required |
239 | | * for detecting duplicates can be limited by maintaining a minimum packet |
240 | | * number below which all packets are immediately dropped." |
241 | | * |
242 | | * Moreover, RFC 9000 s. 13.2.3 states that: |
243 | | * |
244 | | * "A receiver MUST retain an ACK Range unless it can ensure that it will not |
245 | | * subsequently accept packets with numbers in that range. Maintaining a |
246 | | * minimum packet number that increases as ranges are discarded is one way to |
247 | | * achieve this with minimal state." |
248 | | * |
249 | | * This touches on a subtlety of the original requirement quoted above: the |
250 | | * receiver MUST discard a packet unless it is certain that it has not processed |
251 | | * another packet with the same PN. However, this does not forbid the receiver |
252 | | * from also discarding some PNs even though it has not yet processed them. In |
253 | | * other words, implementations must be conservative and err in the direction of |
254 | | * assuming a packet is a duplicate, but it is acceptable for this to come at |
255 | | * the cost of falsely identifying some packets as duplicates. |
256 | | * |
257 | | * This allows us to bound the amount of state we must keep, and we adopt the |
258 | | * suggested strategy quoted above to do so. We define a watermark PN below |
259 | | * which all PNs are in the same state. This watermark is only ever increased. |
260 | | * Thus the PNs the state for which needs to be explicitly tracked is limited to |
261 | | * only a small number of recent PNs, and all older PNs have an assumed state. |
262 | | * |
263 | | * Any given PN thus falls into one of the following states: |
264 | | * |
265 | | * - (A) The PN is above the watermark but we have not yet received it. |
266 | | * |
267 | | * If we receive such a PN, we should process it and record the PN as |
268 | | * received. |
269 | | * |
270 | | * - (B) The PN is above the watermark and we have received it. |
271 | | * |
272 | | * The PN should be included in any future ACK frame we generate. |
273 | | * If we receive such a PN again, we should ignore it. |
274 | | * |
275 | | * - (C) The PN is below the watermark. |
276 | | * |
277 | | * We do not know whether a packet with the given PN was received or |
278 | | * not. To be safe, if we receive such a packet, it is not processed. |
279 | | * |
280 | | * Note that state (C) corresponds to both "we have processed this PN and it has |
281 | | * been provably ACKed" logical state and a subset of the PNs in the "we have |
282 | | * never processed this PN before" logical state (namely all PNs which were lost |
283 | | * and never received, but which are not recent enough to be above the |
284 | | * watermark). The reason we can merge these states and avoid tracking states |
285 | | * for the PNs in this state is because the provably ACKed and never-received |
286 | | * states are functionally identical in terms of how we need to handle them: we |
287 | | * don't need to do anything for PNs in either of these states, so we don't have |
288 | | * to care about PNs in this state nor do we have to care about distinguishing |
289 | | * the two states for a given PN. |
290 | | * |
291 | | * Note that under this scheme provably ACKed PNs are by definition always below |
292 | | * the watermark; therefore, it follows that when a PN becomes provably ACKed, |
293 | | * the watermark must be immediately increased to exceed it (otherwise we would |
294 | | * keep reporting it in future ACK frames). |
295 | | * |
296 | | * This is in line with RFC 9000 s. 13.2.4's suggested strategy on when |
297 | | * to advance the watermark: |
298 | | * |
299 | | * "When a packet containing an ACK frame is sent, the Largest Acknowledged |
300 | | * field in that frame can be saved. When a packet containing an ACK frame is |
301 | | * acknowledged, the receiver can stop acknowledging packets less than or |
302 | | * equal to the Largest Acknowledged field in the sent ACK frame." |
303 | | * |
304 | | * This is where our scheme's false positives arise. When a packet containing an |
305 | | * ACK frame is itself ACK'd, PNs referenced in that ACK frame become provably |
306 | | * acked, and the watermark is bumped accordingly. However, the Largest |
307 | | * Acknowledged field does not imply that all lower PNs have been received, |
308 | | * because there may be gaps expressed in the ranges of PNs expressed by that |
309 | | * and previous ACK frames. Thus, some unreceived PNs may be moved below the |
310 | | * watermark, and we may subsequently reject those PNs as possibly being |
311 | | * duplicates even though we have not actually received those PNs. Since we bump |
312 | | * the watermark when a PN becomes provably ACKed, it follows that an unreceived |
313 | | * PN falls below the watermark (and thus becomes a false positive for the |
314 | | * purposes of duplicate detection) when a higher-numbered PN becomes provably |
315 | | * ACKed. |
316 | | * |
317 | | * Thus, when PN n becomes provably acked, any unreceived PNs in the range [0, |
318 | | * n) will no longer be processed. Although datagrams may be reordered in the |
319 | | * network, a PN we receive can only become provably ACKed after our own |
320 | | * subsequently generated ACK frame is sent in a future TX packet, and then we |
321 | | * receive another RX PN acknowledging that TX packet. This means that a given RX |
322 | | * PN can only become provably ACKed at least 1 RTT after it is received; it is |
323 | | * unlikely that any reordered datagrams will still be "in the network" (and not |
324 | | * lost) by this time. If this does occur for whatever reason and a late PN is |
325 | | * received, the packet will be discarded unprocessed and the PN is simply |
326 | | * handled as though lost (a "written off" PN). |
327 | | * |
328 | | * **Data structure.** Our state for the RX handling side of the ACK manager, as |
329 | | * discussed above, mainly comprises: |
330 | | * |
331 | | * a) a logical set of PNs, and |
332 | | * b) a monotonically increasing PN counter (the watermark). |
333 | | * |
334 | | * For (a), we define a data structure which stores a logical set of PNs, which |
335 | | * we use to keep track of which PNs we have received but which have not yet |
336 | | * been provably ACKed, and thus will later need to generate an ACK frame for. |
337 | | * |
338 | | * The correspondence with the logical states discussed above is as follows. A |
339 | | * PN is in state (C) if it is below the watermark; otherwise it is in state (B) |
340 | | * if it is in the logical set of PNs, and in state (A) otherwise. |
341 | | * |
342 | | * Note that PNs are only removed from the PN set (when they become provably |
343 | | * ACKed or written off) by virtue of advancement of the watermark. Removing PNs |
344 | | * from the PN set any other way would be ambiguous as it would be |
345 | | * indistinguishable from a PN we have not yet received and risk us processing a |
346 | | * duplicate packet. In other words, for a given PN: |
347 | | * |
348 | | * - State (A) can transition to state (B) or (C) |
349 | | * - State (B) can transition to state (C) only |
350 | | * - State (C) is the terminal state |
351 | | * |
352 | | * We can query the logical set data structure for PNs which have been received |
353 | | * but which have not been provably ACKed when we want to generate ACK frames. |
354 | | * Since ACK frames can be lost and/or we might not know that the peer has |
355 | | * successfully received them, we might generate multiple ACK frames covering a |
356 | | * given PN until that PN becomes provably ACKed and we finally remove it from |
357 | | * our set (by bumping the watermark) as no longer being our concern. |
358 | | * |
359 | | * The data structure used is the UINT_SET structure defined in uint_set.h, |
360 | | * which is used as a PN set. We use the following operations of the structure: |
361 | | * |
362 | | * Insert Range: Used when we receive a new PN. |
363 | | * |
364 | | * Remove Range: Used when bumping the watermark. |
365 | | * |
366 | | * Query: Used to determine if a PN is in the set. |
367 | | * |
368 | | * **Possible duplicates.** A PN is considered a possible duplicate when either: |
369 | | * |
370 | | * a) its PN is already in the PN set (i.e. has already been received), or |
371 | | * b) its PN is below the watermark (i.e. was provably ACKed or written off). |
372 | | * |
373 | | * A packet with a given PN is considered 'processable' when that PN is not |
374 | | * considered a possible duplicate (see ossl_ackm_is_rx_pn_processable). |
375 | | * |
376 | | * **TX/RX interaction.** The watermark is bumped whenever an RX packet becomes |
377 | | * provably ACKed. This occurs when an ACK frame is received by the TX side of |
378 | | * the ACK manager; thus, there is necessary interaction between the TX and RX |
379 | | * sides of the ACK manager. |
380 | | * |
381 | | * This is implemented as follows. When a packet is queued as sent in the TX |
382 | | * side of the ACK manager, it may optionally have a Largest Acked value set on |
383 | | * it. The user of the ACK manager should do this if the packet being |
384 | | * transmitted contains an ACK frame, by setting the field to the Largest Acked |
385 | | * field of that frame. Otherwise, this field should be set to QUIC_PN_INVALID. |
386 | | * When a TX packet is eventually acknowledged which has this field set, it is |
387 | | * used to update the state of the RX side of the ACK manager by bumping the |
388 | | * watermark accordingly. |
389 | | */ |
390 | | struct rx_pkt_history_st { |
391 | | UINT_SET set; |
392 | | |
393 | | /* |
394 | | * Invariant: PNs below this are not in the set. |
395 | | * Invariant: This is monotonic and only ever increases. |
396 | | */ |
397 | | QUIC_PN watermark; |
398 | | }; |
399 | | |
400 | | static int rx_pkt_history_bump_watermark(struct rx_pkt_history_st *h, |
401 | | QUIC_PN watermark); |
402 | | |
403 | | static void rx_pkt_history_init(struct rx_pkt_history_st *h) |
404 | 151k | { |
405 | 151k | ossl_uint_set_init(&h->set); |
406 | 151k | h->watermark = 0; |
407 | 151k | } |
408 | | |
409 | | static void rx_pkt_history_destroy(struct rx_pkt_history_st *h) |
410 | 151k | { |
411 | 151k | ossl_uint_set_destroy(&h->set); |
412 | 151k | } |
413 | | |
414 | | /* |
415 | | * Limit the number of ACK ranges we store to prevent resource consumption DoS |
416 | | * attacks. |
417 | | */ |
418 | 963k | #define MAX_RX_ACK_RANGES 32 |
419 | | |
420 | | static void rx_pkt_history_trim_range_count(struct rx_pkt_history_st *h) |
421 | 639k | { |
422 | 639k | QUIC_PN highest = QUIC_PN_INVALID; |
423 | | |
424 | 963k | while (ossl_list_uint_set_num(&h->set) > MAX_RX_ACK_RANGES) { |
425 | 324k | UINT_RANGE r = ossl_list_uint_set_head(&h->set)->range; |
426 | | |
427 | 324k | highest = (highest == QUIC_PN_INVALID) |
428 | 324k | ? r.end |
429 | 324k | : ossl_quic_pn_max(highest, r.end); |
430 | | |
431 | 324k | ossl_uint_set_remove(&h->set, &r); |
432 | 324k | } |
433 | | |
434 | | /* |
435 | | * Bump watermark to cover all PNs we removed to avoid accidental |
436 | | * reprocessing of packets. |
437 | | */ |
438 | 639k | if (highest != QUIC_PN_INVALID) |
439 | 324k | rx_pkt_history_bump_watermark(h, highest + 1); |
440 | 639k | } |
441 | | |
442 | | static int rx_pkt_history_add_pn(struct rx_pkt_history_st *h, |
443 | | QUIC_PN pn) |
444 | 639k | { |
445 | 639k | UINT_RANGE r; |
446 | | |
447 | 639k | r.start = pn; |
448 | 639k | r.end = pn; |
449 | | |
450 | 639k | if (pn < h->watermark) |
451 | 0 | return 1; /* consider this a success case */ |
452 | | |
453 | 639k | if (ossl_uint_set_insert(&h->set, &r) != 1) |
454 | 0 | return 0; |
455 | | |
456 | 639k | rx_pkt_history_trim_range_count(h); |
457 | 639k | return 1; |
458 | 639k | } |
459 | | |
460 | | static int rx_pkt_history_bump_watermark(struct rx_pkt_history_st *h, |
461 | | QUIC_PN watermark) |
462 | 417k | { |
463 | 417k | UINT_RANGE r; |
464 | | |
465 | 417k | if (watermark <= h->watermark) |
466 | 65.7k | return 1; |
467 | | |
468 | | /* Remove existing PNs below the watermark. */ |
469 | 351k | r.start = 0; |
470 | 351k | r.end = watermark - 1; |
471 | 351k | if (ossl_uint_set_remove(&h->set, &r) != 1) |
472 | 0 | return 0; |
473 | | |
474 | 351k | h->watermark = watermark; |
475 | 351k | return 1; |
476 | 351k | } |
477 | | |
478 | | /* |
479 | | * ACK Manager Implementation |
480 | | * ************************** |
481 | | * Implementation of the ACK manager proper. |
482 | | */ |
483 | | |
484 | | /* Constants used by the ACK manager; see RFC 9002. */ |
485 | 38.2M | #define K_GRANULARITY (1 * OSSL_TIME_MS) |
486 | 123k | #define K_PKT_THRESHOLD 3 |
487 | 81.7k | #define K_TIME_THRESHOLD_NUM 9 |
488 | 81.7k | #define K_TIME_THRESHOLD_DEN 8 |
489 | | |
490 | | /* The maximum number of times we allow PTO to be doubled. */ |
491 | 3.10M | #define MAX_PTO_COUNT 16 |
492 | | |
493 | | /* Default maximum amount of time to leave an ACK-eliciting packet un-ACK'd. */ |
494 | 50.4k | #define DEFAULT_TX_MAX_ACK_DELAY ossl_ms2time(QUIC_DEFAULT_MAX_ACK_DELAY) |
495 | | |
496 | | struct ossl_ackm_st { |
497 | | /* Our list of transmitted packets. Corresponds to RFC 9002 sent_packets. */ |
498 | | struct tx_pkt_history_st tx_history[QUIC_PN_SPACE_NUM]; |
499 | | |
500 | | /* Our list of received PNs which are not yet provably acked. */ |
501 | | struct rx_pkt_history_st rx_history[QUIC_PN_SPACE_NUM]; |
502 | | |
503 | | /* Polymorphic dependencies that we consume. */ |
504 | | OSSL_TIME (*now)(void *arg); |
505 | | void *now_arg; |
506 | | OSSL_STATM *statm; |
507 | | const OSSL_CC_METHOD *cc_method; |
508 | | OSSL_CC_DATA *cc_data; |
509 | | |
510 | | /* RFC 9002 variables. */ |
511 | | uint32_t pto_count; |
512 | | QUIC_PN largest_acked_pkt[QUIC_PN_SPACE_NUM]; |
513 | | OSSL_TIME time_of_last_ack_eliciting_pkt[QUIC_PN_SPACE_NUM]; |
514 | | OSSL_TIME loss_time[QUIC_PN_SPACE_NUM]; |
515 | | OSSL_TIME loss_detection_deadline; |
516 | | |
517 | | /* Lowest PN which is still not known to be ACKed. */ |
518 | | QUIC_PN lowest_unacked_pkt[QUIC_PN_SPACE_NUM]; |
519 | | |
520 | | /* Time at which we got our first RTT sample, or 0. */ |
521 | | OSSL_TIME first_rtt_sample; |
522 | | |
523 | | /* |
524 | | * A packet's num_bytes are added to this if it is inflight, |
525 | | * and removed again once ack'd/lost/discarded. |
526 | | */ |
527 | | uint64_t bytes_in_flight; |
528 | | |
529 | | /* |
530 | | * A packet's num_bytes are added to this if it is both inflight and |
531 | | * ack-eliciting, and removed again once ack'd/lost/discarded. |
532 | | */ |
533 | | uint64_t ack_eliciting_bytes_in_flight[QUIC_PN_SPACE_NUM]; |
534 | | |
535 | | /* Count of ECN-CE events. */ |
536 | | uint64_t peer_ecnce[QUIC_PN_SPACE_NUM]; |
537 | | |
538 | | /* Set to 1 when the handshake is confirmed. */ |
539 | | char handshake_confirmed; |
540 | | |
541 | | /* Set to 1 when attached to server channel */ |
542 | | char is_server; |
543 | | |
544 | | /* Set to 1 when the peer has completed address validation. */ |
545 | | char peer_completed_addr_validation; |
546 | | |
547 | | /* Set to 1 when a PN space has been discarded. */ |
548 | | char discarded[QUIC_PN_SPACE_NUM]; |
549 | | |
550 | | /* Set to 1 when we think an ACK frame should be generated. */ |
551 | | char rx_ack_desired[QUIC_PN_SPACE_NUM]; |
552 | | |
553 | | /* Set to 1 if an ACK frame has ever been generated. */ |
554 | | char rx_ack_generated[QUIC_PN_SPACE_NUM]; |
555 | | |
556 | | /* Probe request counts for reporting to the user. */ |
557 | | OSSL_ACKM_PROBE_INFO pending_probe; |
558 | | |
559 | | /* Generated ACK frames for each PN space. */ |
560 | | OSSL_QUIC_FRAME_ACK ack[QUIC_PN_SPACE_NUM]; |
561 | | OSSL_QUIC_ACK_RANGE ack_ranges[QUIC_PN_SPACE_NUM][MAX_RX_ACK_RANGES]; |
562 | | |
563 | | /* Other RX state. */ |
564 | | /* Largest PN we have RX'd. */ |
565 | | QUIC_PN rx_largest_pn[QUIC_PN_SPACE_NUM]; |
566 | | |
567 | | /* Time at which the PN in rx_largest_pn was RX'd. */ |
568 | | OSSL_TIME rx_largest_time[QUIC_PN_SPACE_NUM]; |
569 | | |
570 | | /* |
571 | | * ECN event counters. Each time we receive a packet with a given ECN label, |
572 | | * the corresponding ECN counter here is incremented. |
573 | | */ |
574 | | uint64_t rx_ect0[QUIC_PN_SPACE_NUM]; |
575 | | uint64_t rx_ect1[QUIC_PN_SPACE_NUM]; |
576 | | uint64_t rx_ecnce[QUIC_PN_SPACE_NUM]; |
577 | | |
578 | | /* |
579 | | * Number of ACK-eliciting packets since last ACK. We use this to defer |
580 | | * emitting ACK frames until a threshold number of ACK-eliciting packets |
581 | | * have been received. |
582 | | */ |
583 | | uint32_t rx_ack_eliciting_pkts_since_last_ack[QUIC_PN_SPACE_NUM]; |
584 | | |
585 | | /* |
586 | | * The ACK frame coalescing deadline at which we should flush any unsent ACK |
587 | | * frames. |
588 | | */ |
589 | | OSSL_TIME rx_ack_flush_deadline[QUIC_PN_SPACE_NUM]; |
590 | | |
591 | | /* |
592 | | * The RX maximum ACK delay (the maximum amount of time our peer might |
593 | | * wait to send us an ACK after receiving an ACK-eliciting packet). |
594 | | */ |
595 | | OSSL_TIME rx_max_ack_delay; |
596 | | |
597 | | /* |
598 | | * The TX maximum ACK delay (the maximum amount of time we allow ourselves |
599 | | * to wait before generating an ACK after receiving an ACK-eliciting |
600 | | * packet). |
601 | | */ |
602 | | OSSL_TIME tx_max_ack_delay; |
603 | | |
604 | | /* Callbacks for deadline updates. */ |
605 | | void (*loss_detection_deadline_cb)(OSSL_TIME deadline, void *arg); |
606 | | void *loss_detection_deadline_cb_arg; |
607 | | |
608 | | void (*ack_deadline_cb)(OSSL_TIME deadline, int pkt_space, void *arg); |
609 | | void *ack_deadline_cb_arg; |
610 | | }; |
611 | | |
612 | | static ossl_inline uint32_t min_u32(uint32_t x, uint32_t y) |
613 | 3.10M | { |
614 | 3.10M | return x < y ? x : y; |
615 | 3.10M | } |
616 | | |
617 | | /* |
618 | | * Get TX history for a given packet number space. Must not have been |
619 | | * discarded. |
620 | | */ |
621 | | static struct tx_pkt_history_st *get_tx_history(OSSL_ACKM *ackm, int pkt_space) |
622 | 2.58M | { |
623 | 2.58M | assert(!ackm->discarded[pkt_space]); |
624 | | |
625 | 2.58M | return &ackm->tx_history[pkt_space]; |
626 | 2.58M | } |
627 | | |
628 | | /* |
629 | | * Get RX history for a given packet number space. Must not have been |
630 | | * discarded. |
631 | | */ |
632 | | static struct rx_pkt_history_st *get_rx_history(OSSL_ACKM *ackm, int pkt_space) |
633 | 9.43M | { |
634 | 9.43M | assert(!ackm->discarded[pkt_space]); |
635 | | |
636 | 9.43M | return &ackm->rx_history[pkt_space]; |
637 | 9.43M | } |
638 | | |
639 | | /* Does the newly-acknowledged list contain any ack-eliciting packet? */ |
640 | | static int ack_includes_ack_eliciting(OSSL_ACKM_TX_PKT *pkt) |
641 | 53.0k | { |
642 | 57.5k | for (; pkt != NULL; pkt = pkt->anext) |
643 | 53.7k | if (pkt->is_ack_eliciting) |
644 | 49.2k | return 1; |
645 | | |
646 | 3.78k | return 0; |
647 | 53.0k | } |
648 | | |
649 | | /* Return number of ACK-eliciting bytes in flight across all PN spaces. */ |
650 | | static uint64_t ackm_ack_eliciting_bytes_in_flight(OSSL_ACKM *ackm) |
651 | 5.77M | { |
652 | 5.77M | int i; |
653 | 5.77M | uint64_t total = 0; |
654 | | |
655 | 23.0M | for (i = 0; i < QUIC_PN_SPACE_NUM; ++i) |
656 | 17.3M | total += ackm->ack_eliciting_bytes_in_flight[i]; |
657 | | |
658 | 5.77M | return total; |
659 | 5.77M | } |
660 | | |
661 | | /* Return 1 if the range contains the given PN. */ |
662 | | static int range_contains(const OSSL_QUIC_ACK_RANGE *range, QUIC_PN pn) |
663 | 4.89M | { |
664 | 4.89M | return pn >= range->start && pn <= range->end; |
665 | 4.89M | } |
666 | | |
667 | | /* |
668 | | * Given a logical representation of an ACK frame 'ack', create a singly-linked |
669 | | * list of the newly ACK'd frames; that is, of frames which are matched by the |
670 | | * list of PN ranges contained in the ACK frame. The packet structures in the |
671 | | * list returned are removed from the TX history list. Returns a pointer to the |
672 | | * list head (or NULL) if empty. |
673 | | */ |
674 | | static OSSL_ACKM_TX_PKT *ackm_detect_and_remove_newly_acked_pkts(OSSL_ACKM *ackm, |
675 | | const OSSL_QUIC_FRAME_ACK *ack, |
676 | | int pkt_space) |
677 | 264k | { |
678 | 264k | OSSL_ACKM_TX_PKT *acked_pkts = NULL, **fixup = &acked_pkts, *pkt, *pprev; |
679 | 264k | struct tx_pkt_history_st *h; |
680 | 264k | size_t ridx = 0; |
681 | | |
682 | 264k | assert(ack->num_ack_ranges > 0); |
683 | | |
684 | | /* |
685 | | * Our history list is a list of packets sorted in ascending order |
686 | | * by packet number. |
687 | | * |
688 | | * ack->ack_ranges is a list of packet number ranges in descending order. |
689 | | * |
690 | | * Walk through our history list from the end in order to efficiently detect |
691 | | * membership in the specified ack ranges. As an optimization, we use our |
692 | | * hashtable to try and skip to the first matching packet. This may fail if |
693 | | * the ACK ranges given include nonexistent packets. |
694 | | */ |
695 | 264k | h = get_tx_history(ackm, pkt_space); |
696 | | |
697 | 264k | pkt = tx_pkt_history_by_pkt_num(h, ack->ack_ranges[0].end); |
698 | 264k | if (pkt == NULL) |
699 | 211k | pkt = ossl_list_tx_history_tail(&h->packets); |
700 | | |
701 | 4.74M | for (; pkt != NULL; pkt = pprev) { |
702 | | /* |
703 | | * Save prev value as it will be zeroed if we remove the packet from the |
704 | | * history list below. |
705 | | */ |
706 | 4.54M | pprev = ossl_list_tx_history_prev(pkt); |
707 | | |
708 | 4.62M | for (;; ++ridx) { |
709 | 4.62M | if (ridx >= ack->num_ack_ranges) { |
710 | | /* |
711 | | * We have exhausted all ranges so stop here, even if there are |
712 | | * more packets to look at. |
713 | | */ |
714 | 64.6k | goto stop; |
715 | 64.6k | } |
716 | | |
717 | 4.55M | if (range_contains(&ack->ack_ranges[ridx], pkt->pkt_num)) { |
718 | | /* We have matched this range. */ |
719 | 1.09M | tx_pkt_history_remove(h, pkt->pkt_num); |
720 | | |
721 | 1.09M | *fixup = pkt; |
722 | 1.09M | fixup = &pkt->anext; |
723 | 1.09M | *fixup = NULL; |
724 | 1.09M | break; |
725 | 3.46M | } else if (pkt->pkt_num > ack->ack_ranges[ridx].end) { |
726 | | /* |
727 | | * We have not reached this range yet in our list, so do not |
728 | | * advance ridx. |
729 | | */ |
730 | 3.38M | break; |
731 | 3.38M | } else { |
732 | | /* |
733 | | * We have moved beyond this range, so advance to the next range |
734 | | * and try matching again. |
735 | | */ |
736 | 79.6k | assert(pkt->pkt_num < ack->ack_ranges[ridx].start); |
737 | 79.6k | continue; |
738 | 79.6k | } |
739 | 4.55M | } |
740 | 4.54M | } |
741 | 264k | stop: |
742 | | |
743 | 264k | return acked_pkts; |
744 | 264k | } |
745 | | |
746 | | /* |
747 | | * Create a singly-linked list of newly detected-lost packets in the given |
748 | | * packet number space. Returns the head of the list or NULL if no packets were |
749 | | * detected lost. The packets in the list are removed from the TX history list. |
750 | | */ |
751 | | static OSSL_ACKM_TX_PKT *ackm_detect_and_remove_lost_pkts(OSSL_ACKM *ackm, |
752 | | int pkt_space) |
753 | 81.7k | { |
754 | 81.7k | OSSL_ACKM_TX_PKT *lost_pkts = NULL, **fixup = &lost_pkts, *pkt, *pnext; |
755 | 81.7k | OSSL_TIME loss_delay, lost_send_time, now; |
756 | 81.7k | OSSL_RTT_INFO rtt; |
757 | 81.7k | struct tx_pkt_history_st *h; |
758 | | |
759 | 81.7k | assert(ackm->largest_acked_pkt[pkt_space] != QUIC_PN_INVALID); |
760 | | |
761 | 81.7k | ossl_statm_get_rtt_info(ackm->statm, &rtt); |
762 | | |
763 | 81.7k | ackm->loss_time[pkt_space] = ossl_time_zero(); |
764 | | |
765 | 81.7k | loss_delay = ossl_time_multiply(ossl_time_max(rtt.latest_rtt, |
766 | 81.7k | rtt.smoothed_rtt), |
767 | 81.7k | K_TIME_THRESHOLD_NUM); |
768 | 81.7k | loss_delay = ossl_time_divide(loss_delay, K_TIME_THRESHOLD_DEN); |
769 | | |
770 | | /* Minimum time of K_GRANULARITY before packets are deemed lost. */ |
771 | 81.7k | loss_delay = ossl_time_max(loss_delay, ossl_ticks2time(K_GRANULARITY)); |
772 | | |
773 | | /* Packets sent before this time are deemed lost. */ |
774 | 81.7k | now = ackm->now(ackm->now_arg); |
775 | 81.7k | lost_send_time = ossl_time_subtract(now, loss_delay); |
776 | | |
777 | 81.7k | h = get_tx_history(ackm, pkt_space); |
778 | 81.7k | pkt = ossl_list_tx_history_head(&h->packets); |
779 | | |
780 | 665k | for (; pkt != NULL; pkt = pnext) { |
781 | 583k | assert(pkt_space == pkt->pkt_space); |
782 | | |
783 | | /* |
784 | | * Save prev value as it will be zeroed if we remove the packet from the |
785 | | * history list below. |
786 | | */ |
787 | 583k | pnext = ossl_list_tx_history_next(pkt); |
788 | | |
789 | 583k | if (pkt->pkt_num > ackm->largest_acked_pkt[pkt_space]) |
790 | 420k | continue; |
791 | | |
792 | | /* |
793 | | * Mark packet as lost, or set time when it should be marked. |
794 | | */ |
795 | 162k | if (ossl_time_compare(pkt->time, lost_send_time) <= 0 |
796 | 123k | || ackm->largest_acked_pkt[pkt_space] |
797 | 139k | >= pkt->pkt_num + K_PKT_THRESHOLD) { |
798 | 139k | tx_pkt_history_remove(h, pkt->pkt_num); |
799 | | |
800 | 139k | *fixup = pkt; |
801 | 139k | fixup = &pkt->lnext; |
802 | 139k | *fixup = NULL; |
803 | 139k | } else { |
804 | 22.8k | if (ossl_time_is_zero(ackm->loss_time[pkt_space])) |
805 | 14.6k | ackm->loss_time[pkt_space] = ossl_time_add(pkt->time, loss_delay); |
806 | 8.19k | else |
807 | 8.19k | ackm->loss_time[pkt_space] = ossl_time_min(ackm->loss_time[pkt_space], |
808 | 8.19k | ossl_time_add(pkt->time, loss_delay)); |
809 | 22.8k | } |
810 | 162k | } |
811 | | |
812 | 81.7k | return lost_pkts; |
813 | 81.7k | } |
814 | | |
815 | | static OSSL_TIME ackm_get_loss_time_and_space(OSSL_ACKM *ackm, int *pspace) |
816 | 2.97M | { |
817 | 2.97M | OSSL_TIME time = ackm->loss_time[QUIC_PN_SPACE_INITIAL]; |
818 | 2.97M | int i, space = QUIC_PN_SPACE_INITIAL; |
819 | | |
820 | 8.91M | for (i = space + 1; i < QUIC_PN_SPACE_NUM; ++i) |
821 | 5.94M | if (ossl_time_is_zero(time) |
822 | 5.94M | || ossl_time_compare(ackm->loss_time[i], time) == -1) { |
823 | 5.94M | time = ackm->loss_time[i]; |
824 | 5.94M | space = i; |
825 | 5.94M | } |
826 | | |
827 | 2.97M | *pspace = space; |
828 | 2.97M | return time; |
829 | 2.97M | } |
830 | | |
831 | | static OSSL_TIME ackm_get_pto_time_and_space(OSSL_ACKM *ackm, int *space) |
832 | 2.83M | { |
833 | 2.83M | OSSL_RTT_INFO rtt; |
834 | 2.83M | OSSL_TIME duration; |
835 | 2.83M | OSSL_TIME pto_timeout = ossl_time_infinite(), t; |
836 | 2.83M | int pto_space = QUIC_PN_SPACE_INITIAL, i; |
837 | | |
838 | 2.83M | ossl_statm_get_rtt_info(ackm->statm, &rtt); |
839 | | |
840 | 2.83M | duration |
841 | 2.83M | = ossl_time_add(rtt.smoothed_rtt, |
842 | 2.83M | ossl_time_max(ossl_time_multiply(rtt.rtt_variance, 4), |
843 | 2.83M | ossl_ticks2time(K_GRANULARITY))); |
844 | | |
845 | 2.83M | duration |
846 | 2.83M | = ossl_time_multiply(duration, |
847 | 2.83M | (uint64_t)1 << min_u32(ackm->pto_count, |
848 | 2.83M | MAX_PTO_COUNT)); |
849 | | |
850 | | /* Anti-deadlock PTO starts from the current time. */ |
851 | 2.83M | if (ackm_ack_eliciting_bytes_in_flight(ackm) == 0) { |
852 | 103k | assert(!ackm->peer_completed_addr_validation); |
853 | | |
854 | 103k | *space = ackm->discarded[QUIC_PN_SPACE_INITIAL] |
855 | 103k | ? QUIC_PN_SPACE_HANDSHAKE |
856 | 103k | : QUIC_PN_SPACE_INITIAL; |
857 | 103k | return ossl_time_add(ackm->now(ackm->now_arg), duration); |
858 | 103k | } |
859 | | |
860 | 8.46M | for (i = QUIC_PN_SPACE_INITIAL; i < QUIC_PN_SPACE_NUM; ++i) { |
861 | | /* |
862 | | * RFC 9002 section 6.2.2.1 keep probe timeout armed until |
863 | | * handshake is confirmed (client sees HANDSHAKE_DONE message |
864 | | * from server). |
865 | | */ |
866 | 8.18M | if (ackm->ack_eliciting_bytes_in_flight[i] == 0 && (ackm->handshake_confirmed == 1 || ackm->is_server == 1)) |
867 | 516k | continue; |
868 | | |
869 | 7.67M | if (i == QUIC_PN_SPACE_APP) { |
870 | | /* Skip application data until handshake confirmed. */ |
871 | 2.72M | if (!ackm->handshake_confirmed) |
872 | 2.45M | break; |
873 | | |
874 | | /* Include max_ack_delay and backoff for app data. */ |
875 | 268k | if (!ossl_time_is_infinite(ackm->rx_max_ack_delay)) { |
876 | 268k | uint64_t factor |
877 | 268k | = (uint64_t)1 << min_u32(ackm->pto_count, MAX_PTO_COUNT); |
878 | | |
879 | 268k | duration |
880 | 268k | = ossl_time_add(duration, |
881 | 268k | ossl_time_multiply(ackm->rx_max_ack_delay, |
882 | 268k | factor)); |
883 | 268k | } |
884 | 268k | } |
885 | | |
886 | | /* |
887 | | * Only re-arm timer if stack has sent at least one ACK eliciting frame. |
888 | | * If stack has sent no ACK eliciting frame at given encryption level then |
889 | | * particular timer is zero and we must not attempt to set it. Timer keeps |
890 | | * time since epoch (Jan 1 1970) and we must not set timer to past. |
891 | | */ |
892 | 5.21M | if (!ossl_time_is_zero(ackm->time_of_last_ack_eliciting_pkt[i])) { |
893 | 2.75M | t = ossl_time_add(ackm->time_of_last_ack_eliciting_pkt[i], duration); |
894 | 2.75M | if (ossl_time_compare(t, pto_timeout) < 0) { |
895 | 2.72M | pto_timeout = t; |
896 | 2.72M | pto_space = i; |
897 | 2.72M | } |
898 | 2.75M | } |
899 | 5.21M | } |
900 | | |
901 | 2.72M | *space = pto_space; |
902 | 2.72M | return pto_timeout; |
903 | 2.83M | } |
904 | | |
905 | | static void ackm_set_loss_detection_timer_actual(OSSL_ACKM *ackm, |
906 | | OSSL_TIME deadline) |
907 | 2.60M | { |
908 | 2.60M | ackm->loss_detection_deadline = deadline; |
909 | | |
910 | 2.60M | if (ackm->loss_detection_deadline_cb != NULL) |
911 | 0 | ackm->loss_detection_deadline_cb(deadline, |
912 | 0 | ackm->loss_detection_deadline_cb_arg); |
913 | 2.60M | } |
914 | | |
915 | | static int ackm_set_loss_detection_timer(OSSL_ACKM *ackm) |
916 | 2.60M | { |
917 | 2.60M | int space; |
918 | 2.60M | OSSL_TIME earliest_loss_time, timeout; |
919 | | |
920 | 2.60M | earliest_loss_time = ackm_get_loss_time_and_space(ackm, &space); |
921 | 2.60M | if (!ossl_time_is_zero(earliest_loss_time)) { |
922 | | /* Time threshold loss detection. */ |
923 | 28.1k | ackm_set_loss_detection_timer_actual(ackm, earliest_loss_time); |
924 | 28.1k | return 1; |
925 | 28.1k | } |
926 | | |
927 | 2.57M | if (ackm_ack_eliciting_bytes_in_flight(ackm) == 0 |
928 | 207k | && ackm->peer_completed_addr_validation) { |
929 | | /* |
930 | | * Nothing to detect lost, so no timer is set. However, the client |
931 | | * needs to arm the timer if the server might be blocked by the |
932 | | * anti-amplification limit. |
933 | | */ |
934 | 104k | ackm_set_loss_detection_timer_actual(ackm, ossl_time_zero()); |
935 | 104k | return 1; |
936 | 104k | } |
937 | | |
938 | 2.47M | timeout = ackm_get_pto_time_and_space(ackm, &space); |
939 | 2.47M | ackm_set_loss_detection_timer_actual(ackm, timeout); |
940 | 2.47M | return 1; |
941 | 2.57M | } |
942 | | |
943 | | static int ackm_in_persistent_congestion(OSSL_ACKM *ackm, |
944 | | const OSSL_ACKM_TX_PKT *lpkt) |
945 | 14.4k | { |
946 | | /* TODO(QUIC FUTURE): Persistent congestion not currently implemented. */ |
947 | 14.4k | return 0; |
948 | 14.4k | } |
949 | | |
950 | | static void ackm_on_pkts_lost(OSSL_ACKM *ackm, int pkt_space, |
951 | | const OSSL_ACKM_TX_PKT *lpkt, int pseudo) |
952 | 16.1k | { |
953 | 16.1k | const OSSL_ACKM_TX_PKT *p, *pnext; |
954 | 16.1k | OSSL_RTT_INFO rtt; |
955 | 16.1k | QUIC_PN largest_pn_lost = 0; |
956 | 16.1k | OSSL_CC_LOSS_INFO loss_info = { 0 }; |
957 | 16.1k | uint32_t flags = 0; |
958 | | |
959 | 157k | for (p = lpkt; p != NULL; p = pnext) { |
960 | 140k | pnext = p->lnext; |
961 | | |
962 | 140k | if (p->is_inflight) { |
963 | 132k | ackm->bytes_in_flight -= p->num_bytes; |
964 | 132k | if (p->is_ack_eliciting) |
965 | 130k | ackm->ack_eliciting_bytes_in_flight[p->pkt_space] |
966 | 130k | -= p->num_bytes; |
967 | | |
968 | 132k | if (p->pkt_num > largest_pn_lost) |
969 | 128k | largest_pn_lost = p->pkt_num; |
970 | | |
971 | 132k | if (!pseudo) { |
972 | | /* |
973 | | * If this is pseudo-loss (e.g. during connection retry) we do not |
974 | | * inform the CC as it is not a real loss and not reflective of |
975 | | * network conditions. |
976 | | */ |
977 | 131k | loss_info.tx_time = p->time; |
978 | 131k | loss_info.tx_size = p->num_bytes; |
979 | | |
980 | 131k | ackm->cc_method->on_data_lost(ackm->cc_data, &loss_info); |
981 | 131k | } |
982 | 132k | } |
983 | | |
984 | 140k | p->on_lost(p->cb_arg); |
985 | 140k | } |
986 | | |
987 | | /* |
988 | | * Persistent congestion can only be considered if we have gotten at least |
989 | | * one RTT sample. |
990 | | */ |
991 | 16.1k | ossl_statm_get_rtt_info(ackm->statm, &rtt); |
992 | 16.1k | if (!ossl_time_is_zero(ackm->first_rtt_sample) |
993 | 14.4k | && ackm_in_persistent_congestion(ackm, lpkt)) |
994 | 0 | flags |= OSSL_CC_LOST_FLAG_PERSISTENT_CONGESTION; |
995 | | |
996 | 16.1k | ackm->cc_method->on_data_lost_finished(ackm->cc_data, flags); |
997 | 16.1k | } |
998 | | |
999 | | static void ackm_on_pkts_acked(OSSL_ACKM *ackm, const OSSL_ACKM_TX_PKT *apkt) |
1000 | 79.0k | { |
1001 | 79.0k | const OSSL_ACKM_TX_PKT *anext; |
1002 | 79.0k | QUIC_PN last_pn_acked = 0; |
1003 | 79.0k | OSSL_CC_ACK_INFO ainfo = { 0 }; |
1004 | | |
1005 | 1.17M | for (; apkt != NULL; apkt = anext) { |
1006 | 1.09M | if (apkt->is_inflight) { |
1007 | 1.09M | ackm->bytes_in_flight -= apkt->num_bytes; |
1008 | 1.09M | if (apkt->is_ack_eliciting) |
1009 | 1.07M | ackm->ack_eliciting_bytes_in_flight[apkt->pkt_space] |
1010 | 1.07M | -= apkt->num_bytes; |
1011 | | |
1012 | 1.09M | if (apkt->pkt_num > last_pn_acked) |
1013 | 46.4k | last_pn_acked = apkt->pkt_num; |
1014 | | |
1015 | 1.09M | if (apkt->largest_acked != QUIC_PN_INVALID) |
1016 | | /* |
1017 | | * This can fail, but it is monotonic; worst case we try again |
1018 | | * next time. |
1019 | | */ |
1020 | 93.2k | rx_pkt_history_bump_watermark(get_rx_history(ackm, |
1021 | 93.2k | apkt->pkt_space), |
1022 | 93.2k | apkt->largest_acked + 1); |
1023 | 1.09M | } |
1024 | | |
1025 | 1.09M | ainfo.tx_time = apkt->time; |
1026 | 1.09M | ainfo.tx_size = apkt->num_bytes; |
1027 | | |
1028 | 1.09M | anext = apkt->anext; |
1029 | 1.09M | apkt->on_acked(apkt->cb_arg); /* may free apkt */ |
1030 | | |
1031 | 1.09M | if (apkt->is_inflight) |
1032 | 1.09M | ackm->cc_method->on_data_acked(ackm->cc_data, &ainfo); |
1033 | 1.09M | } |
1034 | 79.0k | } |
1035 | | |
1036 | | OSSL_ACKM *ossl_ackm_new(OSSL_TIME (*now)(void *arg), |
1037 | | void *now_arg, |
1038 | | OSSL_STATM *statm, |
1039 | | const OSSL_CC_METHOD *cc_method, |
1040 | | OSSL_CC_DATA *cc_data, |
1041 | | int is_server) |
1042 | 50.4k | { |
1043 | 50.4k | OSSL_ACKM *ackm; |
1044 | 50.4k | int i; |
1045 | | |
1046 | 50.4k | ackm = OPENSSL_zalloc(sizeof(OSSL_ACKM)); |
1047 | 50.4k | if (ackm == NULL) |
1048 | 0 | return NULL; |
1049 | | |
1050 | 201k | for (i = 0; i < (int)OSSL_NELEM(ackm->tx_history); ++i) { |
1051 | 151k | ackm->largest_acked_pkt[i] = QUIC_PN_INVALID; |
1052 | 151k | ackm->rx_ack_flush_deadline[i] = ossl_time_infinite(); |
1053 | 151k | if (tx_pkt_history_init(&ackm->tx_history[i]) < 1) |
1054 | 0 | goto err; |
1055 | 151k | } |
1056 | | |
1057 | 201k | for (i = 0; i < (int)OSSL_NELEM(ackm->rx_history); ++i) |
1058 | 151k | rx_pkt_history_init(&ackm->rx_history[i]); |
1059 | | |
1060 | 50.4k | ackm->now = now; |
1061 | 50.4k | ackm->now_arg = now_arg; |
1062 | 50.4k | ackm->statm = statm; |
1063 | 50.4k | ackm->cc_method = cc_method; |
1064 | 50.4k | ackm->cc_data = cc_data; |
1065 | 50.4k | ackm->is_server = (char)is_server; |
1066 | | |
1067 | 50.4k | ackm->rx_max_ack_delay = ossl_ms2time(QUIC_DEFAULT_MAX_ACK_DELAY); |
1068 | 50.4k | ackm->tx_max_ack_delay = DEFAULT_TX_MAX_ACK_DELAY; |
1069 | | |
1070 | 50.4k | return ackm; |
1071 | | |
1072 | 0 | err: |
1073 | 0 | while (--i >= 0) |
1074 | 0 | tx_pkt_history_destroy(&ackm->tx_history[i]); |
1075 | |
|
1076 | 0 | OPENSSL_free(ackm); |
1077 | 0 | return NULL; |
1078 | 50.4k | } |
1079 | | |
1080 | | void ossl_ackm_free(OSSL_ACKM *ackm) |
1081 | 50.4k | { |
1082 | 50.4k | size_t i; |
1083 | | |
1084 | 50.4k | if (ackm == NULL) |
1085 | 0 | return; |
1086 | | |
1087 | 201k | for (i = 0; i < OSSL_NELEM(ackm->tx_history); ++i) |
1088 | 151k | if (!ackm->discarded[i]) { |
1089 | 0 | tx_pkt_history_destroy(&ackm->tx_history[i]); |
1090 | 0 | rx_pkt_history_destroy(&ackm->rx_history[i]); |
1091 | 0 | } |
1092 | | |
1093 | 50.4k | OPENSSL_free(ackm); |
1094 | 50.4k | } |
1095 | | |
1096 | | int ossl_ackm_on_tx_packet(OSSL_ACKM *ackm, OSSL_ACKM_TX_PKT *pkt) |
1097 | 2.07M | { |
1098 | 2.07M | struct tx_pkt_history_st *h = get_tx_history(ackm, pkt->pkt_space); |
1099 | | |
1100 | | /* Time must be set and not move backwards. */ |
1101 | 2.07M | if (ossl_time_is_zero(pkt->time) |
1102 | 2.07M | || ossl_time_compare(ackm->time_of_last_ack_eliciting_pkt[pkt->pkt_space], |
1103 | 2.07M | pkt->time) |
1104 | 2.07M | > 0) |
1105 | 0 | return 0; |
1106 | | |
1107 | | /* Must have non-zero number of bytes. */ |
1108 | 2.07M | if (pkt->num_bytes == 0) |
1109 | 0 | return 0; |
1110 | | |
1111 | | /* Does not make any sense for a non-in-flight packet to be ACK-eliciting. */ |
1112 | 2.07M | if (!pkt->is_inflight && pkt->is_ack_eliciting) |
1113 | 0 | return 0; |
1114 | | |
1115 | 2.07M | if (tx_pkt_history_add(h, pkt) == 0) |
1116 | 0 | return 0; |
1117 | | |
1118 | 2.07M | if (pkt->is_inflight) { |
1119 | 2.00M | if (pkt->is_ack_eliciting) { |
1120 | 1.94M | ackm->time_of_last_ack_eliciting_pkt[pkt->pkt_space] = pkt->time; |
1121 | 1.94M | ackm->ack_eliciting_bytes_in_flight[pkt->pkt_space] |
1122 | 1.94M | += pkt->num_bytes; |
1123 | 1.94M | } |
1124 | | |
1125 | 2.00M | ackm->bytes_in_flight += pkt->num_bytes; |
1126 | 2.00M | ackm_set_loss_detection_timer(ackm); |
1127 | | |
1128 | 2.00M | ackm->cc_method->on_data_sent(ackm->cc_data, pkt->num_bytes); |
1129 | 2.00M | } |
1130 | | |
1131 | 2.07M | return 1; |
1132 | 2.07M | } |
1133 | | |
1134 | | int ossl_ackm_on_rx_datagram(OSSL_ACKM *ackm, size_t num_bytes) |
1135 | 0 | { |
1136 | | /* No-op on the client. */ |
1137 | 0 | return 1; |
1138 | 0 | } |
1139 | | |
1140 | | static void ackm_process_ecn(OSSL_ACKM *ackm, const OSSL_QUIC_FRAME_ACK *ack, |
1141 | | int pkt_space) |
1142 | 24.8k | { |
1143 | 24.8k | struct tx_pkt_history_st *h; |
1144 | 24.8k | OSSL_ACKM_TX_PKT *pkt; |
1145 | 24.8k | OSSL_CC_ECN_INFO ecn_info = { 0 }; |
1146 | | |
1147 | | /* |
1148 | | * If the ECN-CE counter reported by the peer has increased, this could |
1149 | | * be a new congestion event. |
1150 | | */ |
1151 | 24.8k | if (ack->ecnce > ackm->peer_ecnce[pkt_space]) { |
1152 | 9.70k | ackm->peer_ecnce[pkt_space] = ack->ecnce; |
1153 | | |
1154 | 9.70k | h = get_tx_history(ackm, pkt_space); |
1155 | 9.70k | pkt = tx_pkt_history_by_pkt_num(h, ack->ack_ranges[0].end); |
1156 | 9.70k | if (pkt == NULL) |
1157 | 9.70k | return; |
1158 | | |
1159 | 0 | ecn_info.largest_acked_time = pkt->time; |
1160 | 0 | ackm->cc_method->on_ecn(ackm->cc_data, &ecn_info); |
1161 | 0 | } |
1162 | 24.8k | } |
1163 | | |
1164 | | int ossl_ackm_on_rx_ack_frame(OSSL_ACKM *ackm, const OSSL_QUIC_FRAME_ACK *ack, |
1165 | | int pkt_space, OSSL_TIME rx_time) |
1166 | 264k | { |
1167 | 264k | OSSL_ACKM_TX_PKT *na_pkts, *lost_pkts; |
1168 | 264k | int must_set_timer = 0; |
1169 | | |
1170 | 264k | if (ackm->largest_acked_pkt[pkt_space] == QUIC_PN_INVALID) |
1171 | 41.8k | ackm->largest_acked_pkt[pkt_space] = ack->ack_ranges[0].end; |
1172 | 222k | else |
1173 | 222k | ackm->largest_acked_pkt[pkt_space] |
1174 | 222k | = ossl_quic_pn_max(ackm->largest_acked_pkt[pkt_space], |
1175 | 222k | ack->ack_ranges[0].end); |
1176 | | |
1177 | | /* |
1178 | | * If we get an ACK in the handshake space, address validation is completed. |
1179 | | * Make sure we update the timer, even if no packets were ACK'd. |
1180 | | */ |
1181 | 264k | if (!ackm->peer_completed_addr_validation |
1182 | 198k | && pkt_space == QUIC_PN_SPACE_HANDSHAKE) { |
1183 | 883 | ackm->peer_completed_addr_validation = 1; |
1184 | 883 | must_set_timer = 1; |
1185 | 883 | } |
1186 | | |
1187 | | /* |
1188 | | * Find packets that are newly acknowledged and remove them from the list. |
1189 | | */ |
1190 | 264k | na_pkts = ackm_detect_and_remove_newly_acked_pkts(ackm, ack, pkt_space); |
1191 | 264k | if (na_pkts == NULL) { |
1192 | 185k | if (must_set_timer) |
1193 | 324 | ackm_set_loss_detection_timer(ackm); |
1194 | | |
1195 | 185k | return 1; |
1196 | 185k | } |
1197 | | |
1198 | | /* |
1199 | | * Update the RTT if the largest acknowledged is newly acked and at least |
1200 | | * one ACK-eliciting packet was newly acked. |
1201 | | * |
1202 | | * First packet in the list is always the one with the largest PN. |
1203 | | */ |
1204 | 79.0k | if (na_pkts->pkt_num == ack->ack_ranges[0].end && ack_includes_ack_eliciting(na_pkts)) { |
1205 | 49.2k | OSSL_TIME now = ackm->now(ackm->now_arg), ack_delay; |
1206 | 49.2k | if (ossl_time_is_zero(ackm->first_rtt_sample)) |
1207 | 32.7k | ackm->first_rtt_sample = now; |
1208 | | |
1209 | | /* Enforce maximum ACK delay. */ |
1210 | 49.2k | ack_delay = ack->delay_time; |
1211 | 49.2k | if (ackm->handshake_confirmed) |
1212 | 4.24k | ack_delay = ossl_time_min(ack_delay, ackm->rx_max_ack_delay); |
1213 | | |
1214 | 49.2k | ossl_statm_update_rtt(ackm->statm, ack_delay, |
1215 | 49.2k | ossl_time_subtract(now, na_pkts->time)); |
1216 | 49.2k | } |
1217 | | |
1218 | | /* |
1219 | | * Process ECN information if present. |
1220 | | * |
1221 | | * We deliberately do most ECN processing in the ACKM rather than the |
1222 | | * congestion controller to avoid having to give the congestion controller |
1223 | | * access to ACKM internal state. |
1224 | | */ |
1225 | 79.0k | if (ack->ecn_present) |
1226 | 24.8k | ackm_process_ecn(ackm, ack, pkt_space); |
1227 | | |
1228 | | /* Handle inferred loss. */ |
1229 | 79.0k | lost_pkts = ackm_detect_and_remove_lost_pkts(ackm, pkt_space); |
1230 | 79.0k | if (lost_pkts != NULL) |
1231 | 12.6k | ackm_on_pkts_lost(ackm, pkt_space, lost_pkts, /*pseudo=*/0); |
1232 | | |
1233 | 79.0k | ackm_on_pkts_acked(ackm, na_pkts); |
1234 | | |
1235 | | /* |
1236 | | * Reset pto_count unless the client is unsure if the server validated the |
1237 | | * client's address. |
1238 | | */ |
1239 | 79.0k | if (ackm->peer_completed_addr_validation) |
1240 | 8.37k | ackm->pto_count = 0; |
1241 | | |
1242 | 79.0k | ackm_set_loss_detection_timer(ackm); |
1243 | 79.0k | return 1; |
1244 | 264k | } |
1245 | | |
1246 | | int ossl_ackm_on_pkt_space_discarded(OSSL_ACKM *ackm, int pkt_space) |
1247 | 180k | { |
1248 | 180k | OSSL_ACKM_TX_PKT *pkt, *pnext; |
1249 | 180k | uint64_t num_bytes_invalidated = 0; |
1250 | | |
1251 | 180k | if (ackm->discarded[pkt_space]) |
1252 | 28.8k | return 0; |
1253 | | |
1254 | 151k | if (pkt_space == QUIC_PN_SPACE_HANDSHAKE) |
1255 | 50.4k | ackm->peer_completed_addr_validation = 1; |
1256 | | |
1257 | 151k | for (pkt = ossl_list_tx_history_head(&get_tx_history(ackm, pkt_space)->packets); |
1258 | 988k | pkt != NULL; pkt = pnext) { |
1259 | 837k | pnext = ossl_list_tx_history_next(pkt); |
1260 | 837k | if (pkt->is_inflight) { |
1261 | 778k | ackm->bytes_in_flight -= pkt->num_bytes; |
1262 | 778k | num_bytes_invalidated += pkt->num_bytes; |
1263 | 778k | } |
1264 | | |
1265 | 837k | pkt->on_discarded(pkt->cb_arg); /* may free pkt */ |
1266 | 837k | } |
1267 | | |
1268 | 151k | tx_pkt_history_destroy(&ackm->tx_history[pkt_space]); |
1269 | 151k | rx_pkt_history_destroy(&ackm->rx_history[pkt_space]); |
1270 | | |
1271 | 151k | if (num_bytes_invalidated > 0) |
1272 | 75.0k | ackm->cc_method->on_data_invalidated(ackm->cc_data, |
1273 | 75.0k | num_bytes_invalidated); |
1274 | | |
1275 | 151k | ackm->time_of_last_ack_eliciting_pkt[pkt_space] = ossl_time_zero(); |
1276 | 151k | ackm->loss_time[pkt_space] = ossl_time_zero(); |
1277 | 151k | ackm->pto_count = 0; |
1278 | 151k | ackm->discarded[pkt_space] = 1; |
1279 | 151k | ackm->ack_eliciting_bytes_in_flight[pkt_space] = 0; |
1280 | 151k | ackm_set_loss_detection_timer(ackm); |
1281 | 151k | return 1; |
1282 | 180k | } |
1283 | | |
1284 | | int ossl_ackm_on_handshake_confirmed(OSSL_ACKM *ackm) |
1285 | 6.62k | { |
1286 | 6.62k | ackm->handshake_confirmed = 1; |
1287 | 6.62k | ackm->peer_completed_addr_validation = 1; |
1288 | 6.62k | ackm_set_loss_detection_timer(ackm); |
1289 | 6.62k | return 1; |
1290 | 6.62k | } |
1291 | | |
1292 | | static void ackm_queue_probe_anti_deadlock_handshake(OSSL_ACKM *ackm) |
1293 | 1.49k | { |
1294 | 1.49k | ++ackm->pending_probe.anti_deadlock_handshake; |
1295 | 1.49k | } |
1296 | | |
1297 | | static void ackm_queue_probe_anti_deadlock_initial(OSSL_ACKM *ackm) |
1298 | 1.27k | { |
1299 | 1.27k | ++ackm->pending_probe.anti_deadlock_initial; |
1300 | 1.27k | } |
1301 | | |
1302 | | static void ackm_queue_probe(OSSL_ACKM *ackm, int pkt_space) |
1303 | 360k | { |
1304 | | /* |
1305 | | * TODO(QUIC FUTURE): We are allowed to send either one or two probe |
1306 | | * packets here. |
1307 | | * Determine a strategy for when we should send two probe packets. |
1308 | | */ |
1309 | 360k | ++ackm->pending_probe.pto[pkt_space]; |
1310 | 360k | } |
1311 | | |
1312 | | int ossl_ackm_on_timeout(OSSL_ACKM *ackm) |
1313 | 365k | { |
1314 | 365k | int pkt_space; |
1315 | 365k | OSSL_TIME earliest_loss_time; |
1316 | 365k | OSSL_ACKM_TX_PKT *lost_pkts; |
1317 | | |
1318 | 365k | earliest_loss_time = ackm_get_loss_time_and_space(ackm, &pkt_space); |
1319 | 365k | if (!ossl_time_is_zero(earliest_loss_time)) { |
1320 | | /* Time threshold loss detection. */ |
1321 | 2.68k | lost_pkts = ackm_detect_and_remove_lost_pkts(ackm, pkt_space); |
1322 | 2.68k | if (lost_pkts != NULL) |
1323 | 2.57k | ackm_on_pkts_lost(ackm, pkt_space, lost_pkts, /*pseudo=*/0); |
1324 | 2.68k | ackm_set_loss_detection_timer(ackm); |
1325 | 2.68k | return 1; |
1326 | 2.68k | } |
1327 | | |
1328 | 362k | if (ackm_ack_eliciting_bytes_in_flight(ackm) == 0) { |
1329 | 2.76k | assert(!ackm->peer_completed_addr_validation); |
1330 | | /* |
1331 | | * Client sends an anti-deadlock packet: Initial is padded to earn more |
1332 | | * anti-amplification credit. A handshake packet proves address |
1333 | | * ownership. |
1334 | | */ |
1335 | 2.76k | if (ackm->discarded[QUIC_PN_SPACE_INITIAL]) |
1336 | 1.49k | ackm_queue_probe_anti_deadlock_handshake(ackm); |
1337 | 1.27k | else |
1338 | 1.27k | ackm_queue_probe_anti_deadlock_initial(ackm); |
1339 | 360k | } else { |
1340 | | /* |
1341 | | * PTO. The user of the ACKM should send new data if available, else |
1342 | | * retransmit old data, or if neither is available, send a single PING |
1343 | | * frame. |
1344 | | */ |
1345 | 360k | ackm_get_pto_time_and_space(ackm, &pkt_space); |
1346 | 360k | ackm_queue_probe(ackm, pkt_space); |
1347 | 360k | } |
1348 | | |
1349 | 362k | ++ackm->pto_count; |
1350 | 362k | ackm_set_loss_detection_timer(ackm); |
1351 | 362k | return 1; |
1352 | 362k | } |
1353 | | |
1354 | | OSSL_TIME ossl_ackm_get_loss_detection_deadline(OSSL_ACKM *ackm) |
1355 | 141M | { |
1356 | 141M | return ackm->loss_detection_deadline; |
1357 | 141M | } |
1358 | | |
1359 | | OSSL_ACKM_PROBE_INFO *ossl_ackm_get0_probe_request(OSSL_ACKM *ackm) |
1360 | 161M | { |
1361 | 161M | return &ackm->pending_probe; |
1362 | 161M | } |
1363 | | |
1364 | | int ossl_ackm_get_largest_unacked(OSSL_ACKM *ackm, int pkt_space, QUIC_PN *pn) |
1365 | 0 | { |
1366 | 0 | struct tx_pkt_history_st *h; |
1367 | 0 | OSSL_ACKM_TX_PKT *p; |
1368 | |
|
1369 | 0 | h = get_tx_history(ackm, pkt_space); |
1370 | 0 | p = ossl_list_tx_history_tail(&h->packets); |
1371 | 0 | if (p != NULL) { |
1372 | 0 | *pn = p->pkt_num; |
1373 | 0 | return 1; |
1374 | 0 | } |
1375 | | |
1376 | 0 | return 0; |
1377 | 0 | } |
1378 | | |
1379 | | /* Number of ACK-eliciting packets RX'd before we always emit an ACK. */ |
1380 | 682k | #define PKTS_BEFORE_ACK 2 |
1381 | | |
1382 | | /* |
1383 | | * Return 1 if emission of an ACK frame is currently desired. |
1384 | | * |
1385 | | * This occurs when one or more of the following conditions occurs: |
1386 | | * |
1387 | | * - We have flagged that we want to send an ACK frame |
1388 | | * (for example, due to the packet threshold count being exceeded), or |
1389 | | * |
1390 | | * - We have exceeded the ACK flush deadline, meaning that |
1391 | | * we have received at least one ACK-eliciting packet, but held off on |
1392 | | * sending an ACK frame immediately in the hope that more ACK-eliciting |
1393 | | * packets might come in, but not enough did and we are now requesting |
1394 | | * transmission of an ACK frame anyway. |
1395 | | * |
1396 | | */ |
1397 | | int ossl_ackm_is_ack_desired(OSSL_ACKM *ackm, int pkt_space) |
1398 | 130M | { |
1399 | 130M | return ackm->rx_ack_desired[pkt_space] |
1400 | 129M | || (!ossl_time_is_infinite(ackm->rx_ack_flush_deadline[pkt_space]) |
1401 | 95.2k | && ossl_time_compare(ackm->now(ackm->now_arg), |
1402 | 95.2k | ackm->rx_ack_flush_deadline[pkt_space]) |
1403 | 95.2k | >= 0); |
1404 | 130M | } |
1405 | | |
1406 | | /* |
1407 | | * Returns 1 if an ACK frame matches a given packet number. |
1408 | | */ |
1409 | | static int ack_contains(const OSSL_QUIC_FRAME_ACK *ack, QUIC_PN pkt_num) |
1410 | 122k | { |
1411 | 122k | size_t i; |
1412 | | |
1413 | 465k | for (i = 0; i < ack->num_ack_ranges; ++i) |
1414 | 342k | if (range_contains(&ack->ack_ranges[i], pkt_num)) |
1415 | 0 | return 1; |
1416 | | |
1417 | 122k | return 0; |
1418 | 122k | } |
1419 | | |
1420 | | /* |
1421 | | * Returns 1 iff a PN (which we have just received) was previously reported as |
1422 | | * implied missing (by us, in an ACK frame we previously generated). |
1423 | | */ |
1424 | | static int ackm_is_missing(OSSL_ACKM *ackm, int pkt_space, QUIC_PN pkt_num) |
1425 | 639k | { |
1426 | | /* |
1427 | | * A PN is implied missing if it is not greater than the highest PN in our |
1428 | | * generated ACK frame, but is not matched by the frame. |
1429 | | */ |
1430 | 639k | return ackm->ack[pkt_space].num_ack_ranges > 0 |
1431 | 567k | && pkt_num <= ackm->ack[pkt_space].ack_ranges[0].end |
1432 | 122k | && !ack_contains(&ackm->ack[pkt_space], pkt_num); |
1433 | 639k | } |
1434 | | |
1435 | | /* |
1436 | | * Returns 1 iff our RX of a PN newly establishes the implication of missing |
1437 | | * packets. |
1438 | | */ |
1439 | | static int ackm_has_newly_missing(OSSL_ACKM *ackm, int pkt_space) |
1440 | 270k | { |
1441 | 270k | struct rx_pkt_history_st *h; |
1442 | | |
1443 | 270k | h = get_rx_history(ackm, pkt_space); |
1444 | | |
1445 | 270k | if (ossl_list_uint_set_is_empty(&h->set)) |
1446 | 0 | return 0; |
1447 | | |
1448 | | /* |
1449 | | * The second condition here establishes that the highest PN range in our RX |
1450 | | * history comprises only a single PN. If there is more than one, then this |
1451 | | * function will have returned 1 during a previous call to |
1452 | | * ossl_ackm_on_rx_packet assuming the third condition below was met. Thus |
1453 | | * we only return 1 when the missing PN condition is newly established. |
1454 | | * |
1455 | | * The third condition here establishes that the highest PN range in our RX |
1456 | | * history is beyond (and does not border) the highest PN we have yet |
1457 | | * reported in any ACK frame. Thus there is a gap of at least one PN between |
1458 | | * the PNs we have ACK'd previously and the PN we have just received. |
1459 | | */ |
1460 | 270k | return ackm->ack[pkt_space].num_ack_ranges > 0 |
1461 | 269k | && ossl_list_uint_set_tail(&h->set)->range.start |
1462 | 269k | == ossl_list_uint_set_tail(&h->set)->range.end |
1463 | 251k | && ossl_list_uint_set_tail(&h->set)->range.start |
1464 | 251k | > ackm->ack[pkt_space].ack_ranges[0].end + 1; |
1465 | 270k | } |
1466 | | |
1467 | | static void ackm_set_flush_deadline(OSSL_ACKM *ackm, int pkt_space, |
1468 | | OSSL_TIME deadline) |
1469 | 7.00M | { |
1470 | 7.00M | ackm->rx_ack_flush_deadline[pkt_space] = deadline; |
1471 | | |
1472 | 7.00M | if (ackm->ack_deadline_cb != NULL) |
1473 | 0 | ackm->ack_deadline_cb(ossl_ackm_get_ack_deadline(ackm, pkt_space), |
1474 | 0 | pkt_space, ackm->ack_deadline_cb_arg); |
1475 | 7.00M | } |
1476 | | |
1477 | | /* Explicitly flags that we want to generate an ACK frame. */ |
1478 | | static void ackm_queue_ack(OSSL_ACKM *ackm, int pkt_space) |
1479 | 393k | { |
1480 | 393k | ackm->rx_ack_desired[pkt_space] = 1; |
1481 | | |
1482 | | /* Cancel deadline. */ |
1483 | 393k | ackm_set_flush_deadline(ackm, pkt_space, ossl_time_infinite()); |
1484 | 393k | } |
1485 | | |
1486 | | static void ackm_on_rx_ack_eliciting(OSSL_ACKM *ackm, |
1487 | | OSSL_TIME rx_time, int pkt_space, |
1488 | | int was_missing) |
1489 | 413k | { |
1490 | 413k | OSSL_TIME tx_max_ack_delay; |
1491 | | |
1492 | 413k | if (ackm->rx_ack_desired[pkt_space]) |
1493 | | /* ACK generation already requested so nothing to do. */ |
1494 | 1.23k | return; |
1495 | | |
1496 | 411k | ++ackm->rx_ack_eliciting_pkts_since_last_ack[pkt_space]; |
1497 | | |
1498 | 411k | if (!ackm->rx_ack_generated[pkt_space] |
1499 | 346k | || was_missing |
1500 | 270k | || ackm->rx_ack_eliciting_pkts_since_last_ack[pkt_space] |
1501 | 270k | >= PKTS_BEFORE_ACK |
1502 | 393k | || ackm_has_newly_missing(ackm, pkt_space)) { |
1503 | | /* |
1504 | | * Either: |
1505 | | * |
1506 | | * - We have never yet generated an ACK frame, meaning that this |
1507 | | * is the first ever packet received, which we should always |
1508 | | * acknowledge immediately, or |
1509 | | * |
1510 | | * - We previously reported the PN that we have just received as |
1511 | | * missing in a previous ACK frame (meaning that we should report |
1512 | | * the fact that we now have it to the peer immediately), or |
1513 | | * |
1514 | | * - We have exceeded the ACK-eliciting packet threshold count |
1515 | | * for the purposes of ACK coalescing, so request transmission |
1516 | | * of an ACK frame, or |
1517 | | * |
1518 | | * - The PN we just received and added to our PN RX history |
1519 | | * newly implies one or more missing PNs, in which case we should |
1520 | | * inform the peer by sending an ACK frame immediately. |
1521 | | * |
1522 | | * We do not test the ACK flush deadline here because it is tested |
1523 | | * separately in ossl_ackm_is_ack_desired. |
1524 | | */ |
1525 | 393k | ackm_queue_ack(ackm, pkt_space); |
1526 | 393k | return; |
1527 | 393k | } |
1528 | | |
1529 | | /* |
1530 | | * Not emitting an ACK yet. |
1531 | | * |
1532 | | * Update the ACK flush deadline. |
1533 | | * |
1534 | | * RFC 9000 s. 13.2.1: "An endpoint MUST acknowledge all ack-eliciting |
1535 | | * Initial and Handshake packets immediately"; don't delay ACK generation if |
1536 | | * we are using the Initial or Handshake PN spaces. |
1537 | | */ |
1538 | 18.5k | tx_max_ack_delay = ackm->tx_max_ack_delay; |
1539 | 18.5k | if (pkt_space == QUIC_PN_SPACE_INITIAL |
1540 | 15.4k | || pkt_space == QUIC_PN_SPACE_HANDSHAKE) |
1541 | 9.04k | tx_max_ack_delay = ossl_time_zero(); |
1542 | | |
1543 | 18.5k | if (ossl_time_is_infinite(ackm->rx_ack_flush_deadline[pkt_space])) |
1544 | 18.5k | ackm_set_flush_deadline(ackm, pkt_space, |
1545 | 18.5k | ossl_time_add(rx_time, tx_max_ack_delay)); |
1546 | 0 | else |
1547 | 0 | ackm_set_flush_deadline(ackm, pkt_space, |
1548 | 0 | ossl_time_min(ackm->rx_ack_flush_deadline[pkt_space], |
1549 | 0 | ossl_time_add(rx_time, |
1550 | 0 | tx_max_ack_delay))); |
1551 | 18.5k | } |
1552 | | |
1553 | | int ossl_ackm_on_rx_packet(OSSL_ACKM *ackm, const OSSL_ACKM_RX_PKT *pkt) |
1554 | 646k | { |
1555 | 646k | struct rx_pkt_history_st *h = get_rx_history(ackm, pkt->pkt_space); |
1556 | 646k | int was_missing; |
1557 | | |
1558 | 646k | if (ossl_ackm_is_rx_pn_processable(ackm, pkt->pkt_num, pkt->pkt_space) != 1) |
1559 | | /* PN has already been processed or written off, no-op. */ |
1560 | 7.11k | return 1; |
1561 | | |
1562 | | /* |
1563 | | * Record the largest PN we have RX'd and the time we received it. |
1564 | | * We use this to calculate the ACK delay field of ACK frames. |
1565 | | */ |
1566 | 639k | if (pkt->pkt_num > ackm->rx_largest_pn[pkt->pkt_space]) { |
1567 | 446k | ackm->rx_largest_pn[pkt->pkt_space] = pkt->pkt_num; |
1568 | 446k | ackm->rx_largest_time[pkt->pkt_space] = pkt->time; |
1569 | 446k | } |
1570 | | |
1571 | | /* |
1572 | | * If the PN we just received was previously implied missing by virtue of |
1573 | | * being omitted from a previous ACK frame generated, we skip any packet |
1574 | | * count thresholds or coalescing delays and emit a new ACK frame |
1575 | | * immediately. |
1576 | | */ |
1577 | 639k | was_missing = ackm_is_missing(ackm, pkt->pkt_space, pkt->pkt_num); |
1578 | | |
1579 | | /* |
1580 | | * Add the packet number to our history list of PNs we have not yet provably |
1581 | | * acked. |
1582 | | */ |
1583 | 639k | if (rx_pkt_history_add_pn(h, pkt->pkt_num) != 1) |
1584 | 0 | return 0; |
1585 | | |
1586 | | /* |
1587 | | * Receiving this packet may or may not cause us to emit an ACK frame. |
1588 | | * We may not emit an ACK frame yet if we have not yet received a threshold |
1589 | | * number of packets. |
1590 | | */ |
1591 | 639k | if (pkt->is_ack_eliciting) |
1592 | 413k | ackm_on_rx_ack_eliciting(ackm, pkt->time, pkt->pkt_space, was_missing); |
1593 | | |
1594 | | /* Update the ECN counters according to which ECN signal we got, if any. */ |
1595 | 639k | switch (pkt->ecn) { |
1596 | 0 | case OSSL_ACKM_ECN_ECT0: |
1597 | 0 | ++ackm->rx_ect0[pkt->pkt_space]; |
1598 | 0 | break; |
1599 | 0 | case OSSL_ACKM_ECN_ECT1: |
1600 | 0 | ++ackm->rx_ect1[pkt->pkt_space]; |
1601 | 0 | break; |
1602 | 0 | case OSSL_ACKM_ECN_ECNCE: |
1603 | 0 | ++ackm->rx_ecnce[pkt->pkt_space]; |
1604 | 0 | break; |
1605 | 639k | default: |
1606 | 639k | break; |
1607 | 639k | } |
1608 | | |
1609 | 639k | return 1; |
1610 | 639k | } |
1611 | | |
1612 | | static void ackm_fill_rx_ack_ranges(OSSL_ACKM *ackm, int pkt_space, |
1613 | | OSSL_QUIC_FRAME_ACK *ack) |
1614 | 6.58M | { |
1615 | 6.58M | struct rx_pkt_history_st *h = get_rx_history(ackm, pkt_space); |
1616 | 6.58M | UINT_SET_ITEM *x; |
1617 | 6.58M | size_t i = 0; |
1618 | | |
1619 | | /* |
1620 | | * Copy out ranges from the PN set, starting at the end, until we reach our |
1621 | | * maximum number of ranges. |
1622 | | */ |
1623 | 6.58M | for (x = ossl_list_uint_set_tail(&h->set); |
1624 | 23.5M | x != NULL && i < OSSL_NELEM(ackm->ack_ranges); |
1625 | 16.9M | x = ossl_list_uint_set_prev(x), ++i) { |
1626 | 16.9M | ackm->ack_ranges[pkt_space][i].start = x->range.start; |
1627 | 16.9M | ackm->ack_ranges[pkt_space][i].end = x->range.end; |
1628 | 16.9M | } |
1629 | | |
1630 | 6.58M | ack->ack_ranges = ackm->ack_ranges[pkt_space]; |
1631 | 6.58M | ack->num_ack_ranges = i; |
1632 | 6.58M | } |
1633 | | |
1634 | | const OSSL_QUIC_FRAME_ACK *ossl_ackm_get_ack_frame(OSSL_ACKM *ackm, |
1635 | | int pkt_space) |
1636 | 6.58M | { |
1637 | 6.58M | OSSL_QUIC_FRAME_ACK *ack = &ackm->ack[pkt_space]; |
1638 | 6.58M | OSSL_TIME now = ackm->now(ackm->now_arg); |
1639 | | |
1640 | 6.58M | ackm_fill_rx_ack_ranges(ackm, pkt_space, ack); |
1641 | | |
1642 | 6.58M | if (!ossl_time_is_zero(ackm->rx_largest_time[pkt_space]) |
1643 | 6.55M | && ossl_time_compare(now, ackm->rx_largest_time[pkt_space]) > 0 |
1644 | 6.19M | && pkt_space == QUIC_PN_SPACE_APP) |
1645 | 395k | ack->delay_time = ossl_time_subtract(now, ackm->rx_largest_time[pkt_space]); |
1646 | 6.19M | else |
1647 | 6.19M | ack->delay_time = ossl_time_zero(); |
1648 | | |
1649 | 6.58M | ack->ect0 = ackm->rx_ect0[pkt_space]; |
1650 | 6.58M | ack->ect1 = ackm->rx_ect1[pkt_space]; |
1651 | 6.58M | ack->ecnce = ackm->rx_ecnce[pkt_space]; |
1652 | 6.58M | ack->ecn_present = 1; |
1653 | | |
1654 | 6.58M | ackm->rx_ack_eliciting_pkts_since_last_ack[pkt_space] = 0; |
1655 | | |
1656 | 6.58M | ackm->rx_ack_generated[pkt_space] = 1; |
1657 | 6.58M | ackm->rx_ack_desired[pkt_space] = 0; |
1658 | 6.58M | ackm_set_flush_deadline(ackm, pkt_space, ossl_time_infinite()); |
1659 | 6.58M | return ack; |
1660 | 6.58M | } |
1661 | | |
1662 | | OSSL_TIME ossl_ackm_get_ack_deadline(OSSL_ACKM *ackm, int pkt_space) |
1663 | 168M | { |
1664 | 168M | if (ackm->rx_ack_desired[pkt_space]) |
1665 | | /* Already desired, deadline is now. */ |
1666 | 772 | return ossl_time_zero(); |
1667 | | |
1668 | 168M | return ackm->rx_ack_flush_deadline[pkt_space]; |
1669 | 168M | } |
1670 | | |
1671 | | int ossl_ackm_is_rx_pn_processable(OSSL_ACKM *ackm, QUIC_PN pn, int pkt_space) |
1672 | 1.83M | { |
1673 | 1.83M | struct rx_pkt_history_st *h = get_rx_history(ackm, pkt_space); |
1674 | | |
1675 | 1.83M | return pn >= h->watermark && ossl_uint_set_query(&h->set, pn) == 0; |
1676 | 1.83M | } |
1677 | | |
1678 | | void ossl_ackm_set_loss_detection_deadline_callback(OSSL_ACKM *ackm, |
1679 | | void (*fn)(OSSL_TIME deadline, |
1680 | | void *arg), |
1681 | | void *arg) |
1682 | 0 | { |
1683 | 0 | ackm->loss_detection_deadline_cb = fn; |
1684 | 0 | ackm->loss_detection_deadline_cb_arg = arg; |
1685 | 0 | } |
1686 | | |
1687 | | void ossl_ackm_set_ack_deadline_callback(OSSL_ACKM *ackm, |
1688 | | void (*fn)(OSSL_TIME deadline, |
1689 | | int pkt_space, |
1690 | | void *arg), |
1691 | | void *arg) |
1692 | 0 | { |
1693 | 0 | ackm->ack_deadline_cb = fn; |
1694 | 0 | ackm->ack_deadline_cb_arg = arg; |
1695 | 0 | } |
1696 | | |
1697 | | int ossl_ackm_mark_packet_pseudo_lost(OSSL_ACKM *ackm, |
1698 | | int pkt_space, QUIC_PN pn) |
1699 | 933 | { |
1700 | 933 | struct tx_pkt_history_st *h = get_tx_history(ackm, pkt_space); |
1701 | 933 | OSSL_ACKM_TX_PKT *pkt; |
1702 | | |
1703 | 933 | pkt = tx_pkt_history_by_pkt_num(h, pn); |
1704 | 933 | if (pkt == NULL) |
1705 | 0 | return 0; |
1706 | | |
1707 | 933 | tx_pkt_history_remove(h, pkt->pkt_num); |
1708 | 933 | pkt->lnext = NULL; |
1709 | 933 | ackm_on_pkts_lost(ackm, pkt_space, pkt, /*pseudo=*/1); |
1710 | 933 | return 1; |
1711 | 933 | } |
1712 | | |
1713 | | OSSL_TIME ossl_ackm_get_pto_duration(OSSL_ACKM *ackm) |
1714 | 35.2M | { |
1715 | 35.2M | OSSL_TIME duration; |
1716 | 35.2M | OSSL_RTT_INFO rtt; |
1717 | | |
1718 | 35.2M | ossl_statm_get_rtt_info(ackm->statm, &rtt); |
1719 | | |
1720 | 35.2M | duration = ossl_time_add(rtt.smoothed_rtt, |
1721 | 35.2M | ossl_time_max(ossl_time_multiply(rtt.rtt_variance, 4), |
1722 | 35.2M | ossl_ticks2time(K_GRANULARITY))); |
1723 | 35.2M | if (!ossl_time_is_infinite(ackm->rx_max_ack_delay)) |
1724 | 35.2M | duration = ossl_time_add(duration, ackm->rx_max_ack_delay); |
1725 | | |
1726 | 35.2M | return duration; |
1727 | 35.2M | } |
1728 | | |
1729 | | QUIC_PN ossl_ackm_get_largest_acked(OSSL_ACKM *ackm, int pkt_space) |
1730 | 4.14M | { |
1731 | 4.14M | return ackm->largest_acked_pkt[pkt_space]; |
1732 | 4.14M | } |
1733 | | |
1734 | | void ossl_ackm_set_rx_max_ack_delay(OSSL_ACKM *ackm, OSSL_TIME rx_max_ack_delay) |
1735 | 50.8k | { |
1736 | 50.8k | ackm->rx_max_ack_delay = rx_max_ack_delay; |
1737 | 50.8k | } |
1738 | | |
1739 | | void ossl_ackm_set_tx_max_ack_delay(OSSL_ACKM *ackm, OSSL_TIME tx_max_ack_delay) |
1740 | 50.4k | { |
1741 | 50.4k | ackm->tx_max_ack_delay = tx_max_ack_delay; |
1742 | 50.4k | } |