Coverage Report

Created: 2026-07-23 06:28

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