Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/ssl/quic/quic_cfq.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_channel.h"
11
#include "internal/quic_cfq.h"
12
#include "internal/numbers.h"
13
14
typedef struct quic_cfq_item_ex_st QUIC_CFQ_ITEM_EX;
15
16
struct quic_cfq_item_ex_st {
17
    QUIC_CFQ_ITEM public;
18
    QUIC_CFQ_ITEM_EX *prev, *next;
19
    unsigned char *encoded;
20
    cfq_free_cb *free_cb;
21
    void *free_cb_arg;
22
    uint64_t frame_type;
23
    size_t encoded_len;
24
    uint32_t priority, pn_space, flags;
25
    int state;
26
};
27
28
uint64_t ossl_quic_cfq_item_get_frame_type(const QUIC_CFQ_ITEM *item)
29
5.91M
{
30
5.91M
    const QUIC_CFQ_ITEM_EX *ex = (const QUIC_CFQ_ITEM_EX *)item;
31
32
5.91M
    return ex->frame_type;
33
5.91M
}
34
35
const unsigned char *ossl_quic_cfq_item_get_encoded(const QUIC_CFQ_ITEM *item)
36
5.67M
{
37
5.67M
    const QUIC_CFQ_ITEM_EX *ex = (const QUIC_CFQ_ITEM_EX *)item;
38
39
5.67M
    return ex->encoded;
40
5.67M
}
41
42
size_t ossl_quic_cfq_item_get_encoded_len(const QUIC_CFQ_ITEM *item)
43
5.67M
{
44
5.67M
    const QUIC_CFQ_ITEM_EX *ex = (const QUIC_CFQ_ITEM_EX *)item;
45
46
5.67M
    return ex->encoded_len;
47
5.67M
}
48
49
int ossl_quic_cfq_item_get_state(const QUIC_CFQ_ITEM *item)
50
0
{
51
0
    const QUIC_CFQ_ITEM_EX *ex = (const QUIC_CFQ_ITEM_EX *)item;
52
53
0
    return ex->state;
54
0
}
55
56
uint32_t ossl_quic_cfq_item_get_pn_space(const QUIC_CFQ_ITEM *item)
57
0
{
58
0
    const QUIC_CFQ_ITEM_EX *ex = (const QUIC_CFQ_ITEM_EX *)item;
59
60
0
    return ex->pn_space;
61
0
}
62
63
int ossl_quic_cfq_item_is_unreliable(const QUIC_CFQ_ITEM *item)
64
9.51k
{
65
9.51k
    const QUIC_CFQ_ITEM_EX *ex = (const QUIC_CFQ_ITEM_EX *)item;
66
67
9.51k
    return (ex->flags & QUIC_CFQ_ITEM_FLAG_UNRELIABLE) != 0;
68
9.51k
}
69
70
typedef struct quic_cfq_item_list_st {
71
    QUIC_CFQ_ITEM_EX *head, *tail;
72
} QUIC_CFQ_ITEM_LIST;
73
74
struct quic_cfq_st {
75
    /*
76
     * Invariant: A CFQ item is always in exactly one of these lists, never more
77
     * or less than one.
78
     *
79
     * Invariant: The list the CFQ item is determined exactly by the state field
80
     * of the item.
81
     */
82
    QUIC_CFQ_ITEM_LIST new_list, tx_list, free_list;
83
};
84
85
static int compare(const QUIC_CFQ_ITEM_EX *a, const QUIC_CFQ_ITEM_EX *b)
86
8.88k
{
87
8.88k
    if (a->pn_space < b->pn_space)
88
0
        return -1;
89
8.88k
    else if (a->pn_space > b->pn_space)
90
0
        return 1;
91
92
8.88k
    if (a->priority > b->priority)
93
4.06k
        return -1;
94
4.82k
    else if (a->priority < b->priority)
95
79
        return 1;
96
97
4.74k
    return 0;
98
8.88k
}
99
100
static void list_remove(QUIC_CFQ_ITEM_LIST *l, QUIC_CFQ_ITEM_EX *n)
101
30.5k
{
102
30.5k
    if (l->head == n)
103
24.7k
        l->head = n->next;
104
30.5k
    if (l->tail == n)
105
24.7k
        l->tail = n->prev;
106
30.5k
    if (n->prev != NULL)
107
5.86k
        n->prev->next = n->next;
108
30.5k
    if (n->next != NULL)
109
5.84k
        n->next->prev = n->prev;
110
30.5k
    n->prev = n->next = NULL;
111
30.5k
}
112
113
static void list_insert_head(QUIC_CFQ_ITEM_LIST *l, QUIC_CFQ_ITEM_EX *n)
114
2.59k
{
115
2.59k
    n->next = l->head;
116
2.59k
    n->prev = NULL;
117
2.59k
    l->head = n;
118
2.59k
    if (n->next != NULL)
119
2.59k
        n->next->prev = n;
120
2.59k
    if (l->tail == NULL)
121
0
        l->tail = n;
122
2.59k
}
123
124
static void list_insert_tail(QUIC_CFQ_ITEM_LIST *l, QUIC_CFQ_ITEM_EX *n)
125
29.3k
{
126
29.3k
    n->prev = l->tail;
127
29.3k
    n->next = NULL;
128
29.3k
    l->tail = n;
129
29.3k
    if (n->prev != NULL)
130
12.1k
        n->prev->next = n;
131
29.3k
    if (l->head == NULL)
132
17.2k
        l->head = n;
133
29.3k
}
134
135
static void list_insert_after(QUIC_CFQ_ITEM_LIST *l,
136
    QUIC_CFQ_ITEM_EX *ref,
137
    QUIC_CFQ_ITEM_EX *n)
138
2.22k
{
139
2.22k
    n->prev = ref;
140
2.22k
    n->next = ref->next;
141
2.22k
    if (ref->next != NULL)
142
2.22k
        ref->next->prev = n;
143
2.22k
    ref->next = n;
144
2.22k
    if (l->tail == ref)
145
0
        l->tail = n;
146
2.22k
}
147
148
static void list_insert_sorted(QUIC_CFQ_ITEM_LIST *l, QUIC_CFQ_ITEM_EX *n,
149
    int (*cmp)(const QUIC_CFQ_ITEM_EX *a,
150
        const QUIC_CFQ_ITEM_EX *b))
151
11.7k
{
152
11.7k
    QUIC_CFQ_ITEM_EX *p = l->head, *pprev = NULL;
153
154
11.7k
    if (p == NULL) {
155
5.44k
        l->head = l->tail = n;
156
5.44k
        n->prev = n->next = NULL;
157
5.44k
        return;
158
5.44k
    }
159
160
10.3k
    for (; p != NULL && cmp(p, n) < 0; pprev = p, p = p->next)
161
4.06k
        ;
162
163
6.30k
    if (p == NULL)
164
1.48k
        list_insert_tail(l, n);
165
4.82k
    else if (pprev == NULL)
166
2.59k
        list_insert_head(l, n);
167
2.22k
    else
168
2.22k
        list_insert_after(l, pprev, n);
169
6.30k
}
170
171
QUIC_CFQ *ossl_quic_cfq_new(void)
172
41.7k
{
173
41.7k
    QUIC_CFQ *cfq = OPENSSL_zalloc(sizeof(*cfq));
174
175
41.7k
    if (cfq == NULL)
176
0
        return NULL;
177
178
41.7k
    return cfq;
179
41.7k
}
180
181
static void clear_item(QUIC_CFQ_ITEM_EX *item)
182
18.4k
{
183
18.4k
    if (item->free_cb != NULL) {
184
11.6k
        item->free_cb(item->encoded, item->encoded_len, item->free_cb_arg);
185
186
11.6k
        item->free_cb = NULL;
187
11.6k
        item->encoded = NULL;
188
11.6k
        item->encoded_len = 0;
189
11.6k
    }
190
191
18.4k
    item->state = -1;
192
18.4k
}
193
194
static void free_list_items(QUIC_CFQ_ITEM_LIST *l)
195
125k
{
196
125k
    QUIC_CFQ_ITEM_EX *p, *pnext;
197
198
134k
    for (p = l->head; p != NULL; p = pnext) {
199
9.04k
        pnext = p->next;
200
9.04k
        clear_item(p);
201
9.04k
        OPENSSL_free(p);
202
9.04k
    }
203
125k
}
204
205
void ossl_quic_cfq_free(QUIC_CFQ *cfq)
206
41.7k
{
207
41.7k
    if (cfq == NULL)
208
0
        return;
209
210
41.7k
    free_list_items(&cfq->new_list);
211
41.7k
    free_list_items(&cfq->tx_list);
212
41.7k
    free_list_items(&cfq->free_list);
213
41.7k
    OPENSSL_free(cfq);
214
41.7k
}
215
216
static QUIC_CFQ_ITEM_EX *cfq_get_free(QUIC_CFQ *cfq)
217
11.6k
{
218
11.6k
    QUIC_CFQ_ITEM_EX *item = cfq->free_list.head;
219
220
11.6k
    if (item != NULL)
221
2.64k
        return item;
222
223
9.04k
    item = OPENSSL_zalloc(sizeof(*item));
224
9.04k
    if (item == NULL)
225
0
        return NULL;
226
227
9.04k
    item->state = -1;
228
9.04k
    list_insert_tail(&cfq->free_list, item);
229
9.04k
    return item;
230
9.04k
}
231
232
QUIC_CFQ_ITEM *ossl_quic_cfq_add_frame(QUIC_CFQ *cfq,
233
    uint32_t priority,
234
    uint32_t pn_space,
235
    uint64_t frame_type,
236
    uint32_t flags,
237
    const unsigned char *encoded,
238
    size_t encoded_len,
239
    cfq_free_cb *free_cb,
240
    void *free_cb_arg)
241
11.6k
{
242
11.6k
    QUIC_CFQ_ITEM_EX *item = cfq_get_free(cfq);
243
244
11.6k
    if (item == NULL)
245
0
        return NULL;
246
247
11.6k
    item->priority = priority;
248
11.6k
    item->frame_type = frame_type;
249
11.6k
    item->pn_space = pn_space;
250
11.6k
    item->encoded = (unsigned char *)encoded;
251
11.6k
    item->encoded_len = encoded_len;
252
11.6k
    item->free_cb = free_cb;
253
11.6k
    item->free_cb_arg = free_cb_arg;
254
255
11.6k
    item->state = QUIC_CFQ_STATE_NEW;
256
11.6k
    item->flags = flags;
257
11.6k
    list_remove(&cfq->free_list, item);
258
11.6k
    list_insert_sorted(&cfq->new_list, item, compare);
259
11.6k
    return &item->public;
260
11.6k
}
261
262
void ossl_quic_cfq_mark_tx(QUIC_CFQ *cfq, QUIC_CFQ_ITEM *item)
263
9.45k
{
264
9.45k
    QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
265
266
9.45k
    switch (ex->state) {
267
9.45k
    case QUIC_CFQ_STATE_NEW:
268
9.45k
        list_remove(&cfq->new_list, ex);
269
9.45k
        list_insert_tail(&cfq->tx_list, ex);
270
9.45k
        ex->state = QUIC_CFQ_STATE_TX;
271
9.45k
        break;
272
0
    case QUIC_CFQ_STATE_TX:
273
0
        break; /* nothing to do */
274
0
    default:
275
0
        assert(0); /* invalid state (e.g. in free state) */
276
0
        break;
277
9.45k
    }
278
9.45k
}
279
280
void ossl_quic_cfq_mark_lost(QUIC_CFQ *cfq, QUIC_CFQ_ITEM *item,
281
    uint32_t priority)
282
63
{
283
63
    QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
284
285
63
    if (ossl_quic_cfq_item_is_unreliable(item)) {
286
0
        ossl_quic_cfq_release(cfq, item);
287
0
        return;
288
0
    }
289
290
63
    switch (ex->state) {
291
0
    case QUIC_CFQ_STATE_NEW:
292
0
        if (priority != UINT32_MAX && priority != ex->priority) {
293
0
            list_remove(&cfq->new_list, ex);
294
0
            ex->priority = priority;
295
0
            list_insert_sorted(&cfq->new_list, ex, compare);
296
0
        }
297
0
        break; /* nothing to do */
298
63
    case QUIC_CFQ_STATE_TX:
299
63
        if (priority != UINT32_MAX)
300
0
            ex->priority = priority;
301
63
        list_remove(&cfq->tx_list, ex);
302
63
        list_insert_sorted(&cfq->new_list, ex, compare);
303
63
        ex->state = QUIC_CFQ_STATE_NEW;
304
63
        break;
305
0
    default:
306
0
        assert(0); /* invalid state (e.g. in free state) */
307
0
        break;
308
63
    }
309
63
}
310
311
int ossl_quic_cfq_discard_unreliable(QUIC_CFQ *cfq, QUIC_CFQ_ITEM *item)
312
9.45k
{
313
9.45k
    int discarded;
314
315
9.45k
    if (ossl_quic_cfq_item_is_unreliable(item)) {
316
6.30k
        ossl_quic_cfq_release(cfq, item);
317
6.30k
        discarded = 1;
318
6.30k
    } else {
319
3.15k
        discarded = 0;
320
3.15k
    }
321
322
9.45k
    return discarded;
323
9.45k
}
324
325
/*
326
 * Releases a CFQ item. The item may be in either state (NEW or TX) prior to the
327
 * call. The QUIC_CFQ_ITEM pointer must not be used following this call.
328
 */
329
void ossl_quic_cfq_release(QUIC_CFQ *cfq, QUIC_CFQ_ITEM *item)
330
9.39k
{
331
9.39k
    QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
332
333
9.39k
    switch (ex->state) {
334
0
    case QUIC_CFQ_STATE_NEW:
335
0
        list_remove(&cfq->new_list, ex);
336
0
        list_insert_tail(&cfq->free_list, ex);
337
0
        clear_item(ex);
338
0
        break;
339
9.39k
    case QUIC_CFQ_STATE_TX:
340
9.39k
        list_remove(&cfq->tx_list, ex);
341
9.39k
        list_insert_tail(&cfq->free_list, ex);
342
9.39k
        clear_item(ex);
343
9.39k
        break;
344
0
    default:
345
0
        assert(0); /* invalid state (e.g. in free state) */
346
0
        break;
347
9.39k
    }
348
9.39k
}
349
350
QUIC_CFQ_ITEM *ossl_quic_cfq_get_priority_head(const QUIC_CFQ *cfq,
351
    uint32_t pn_space)
352
62.3M
{
353
62.3M
    QUIC_CFQ_ITEM_EX *item = cfq->new_list.head;
354
355
63.0M
    for (; item != NULL && item->pn_space != pn_space; item = item->next)
356
663k
        ;
357
358
62.3M
    if (item == NULL)
359
61.4M
        return NULL;
360
361
873k
    return &item->public;
362
62.3M
}
363
364
QUIC_CFQ_ITEM *ossl_quic_cfq_item_get_priority_next(const QUIC_CFQ_ITEM *item,
365
    uint32_t pn_space)
366
5.91M
{
367
5.91M
    QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
368
369
5.91M
    if (ex == NULL)
370
0
        return NULL;
371
372
5.91M
    ex = ex->next;
373
374
5.91M
    for (; ex != NULL && ex->pn_space != pn_space; ex = ex->next)
375
0
        ;
376
377
5.91M
    if (ex == NULL)
378
871k
        return NULL; /* ubsan */
379
380
5.03M
    return &ex->public;
381
5.91M
}