Coverage Report

Created: 2026-07-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/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
85.7M
{
30
85.7M
    QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
31
32
85.7M
    return ex->frame_type;
33
85.7M
}
34
35
const unsigned char *ossl_quic_cfq_item_get_encoded(const QUIC_CFQ_ITEM *item)
36
83.1M
{
37
83.1M
    QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
38
39
83.1M
    return ex->encoded;
40
83.1M
}
41
42
size_t ossl_quic_cfq_item_get_encoded_len(const QUIC_CFQ_ITEM *item)
43
83.1M
{
44
83.1M
    QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
45
46
83.1M
    return ex->encoded_len;
47
83.1M
}
48
49
int ossl_quic_cfq_item_get_state(const QUIC_CFQ_ITEM *item)
50
0
{
51
0
    QUIC_CFQ_ITEM_EX *ex = (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
    QUIC_CFQ_ITEM_EX *ex = (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
34.9k
{
65
34.9k
    QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
66
67
34.9k
    return (ex->flags & QUIC_CFQ_ITEM_FLAG_UNRELIABLE) != 0;
68
34.9k
}
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
129k
{
87
129k
    if (a->pn_space < b->pn_space)
88
0
        return -1;
89
129k
    else if (a->pn_space > b->pn_space)
90
0
        return 1;
91
92
129k
    if (a->priority > b->priority)
93
47.0k
        return -1;
94
81.9k
    else if (a->priority < b->priority)
95
1.03k
        return 1;
96
97
80.9k
    return 0;
98
129k
}
99
100
static void list_remove(QUIC_CFQ_ITEM_LIST *l, QUIC_CFQ_ITEM_EX *n)
101
254k
{
102
254k
    if (l->head == n)
103
177k
        l->head = n->next;
104
254k
    if (l->tail == n)
105
152k
        l->tail = n->prev;
106
254k
    if (n->prev != NULL)
107
76.7k
        n->prev->next = n->next;
108
254k
    if (n->next != NULL)
109
102k
        n->next->prev = n->prev;
110
254k
    n->prev = n->next = NULL;
111
254k
}
112
113
static void list_insert_head(QUIC_CFQ_ITEM_LIST *l, QUIC_CFQ_ITEM_EX *n)
114
63.1k
{
115
63.1k
    n->next = l->head;
116
63.1k
    n->prev = NULL;
117
63.1k
    l->head = n;
118
63.1k
    if (n->next != NULL)
119
63.1k
        n->next->prev = n;
120
63.1k
    if (l->tail == NULL)
121
0
        l->tail = n;
122
63.1k
}
123
124
static void list_insert_tail(QUIC_CFQ_ITEM_LIST *l, QUIC_CFQ_ITEM_EX *n)
125
243k
{
126
243k
    n->prev = l->tail;
127
243k
    n->next = NULL;
128
243k
    l->tail = n;
129
243k
    if (n->prev != NULL)
130
149k
        n->prev->next = n;
131
243k
    if (l->head == NULL)
132
93.9k
        l->head = n;
133
243k
}
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
18.8k
{
139
18.8k
    n->prev = ref;
140
18.8k
    n->next = ref->next;
141
18.8k
    if (ref->next != NULL)
142
18.8k
        ref->next->prev = n;
143
18.8k
    ref->next = n;
144
18.8k
    if (l->tail == ref)
145
0
        l->tail = n;
146
18.8k
}
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
94.2k
{
152
94.2k
    QUIC_CFQ_ITEM_EX *p = l->head, *pprev = NULL;
153
154
94.2k
    if (p == NULL) {
155
10.0k
        l->head = l->tail = n;
156
10.0k
        n->prev = n->next = NULL;
157
10.0k
        return;
158
10.0k
    }
159
160
131k
    for (; p != NULL && cmp(p, n) < 0; pprev = p, p = p->next)
161
47.0k
        ;
162
163
84.1k
    if (p == NULL)
164
2.21k
        list_insert_tail(l, n);
165
81.9k
    else if (pprev == NULL)
166
63.1k
        list_insert_head(l, n);
167
18.8k
    else
168
18.8k
        list_insert_after(l, pprev, n);
169
84.1k
}
170
171
QUIC_CFQ *ossl_quic_cfq_new(void)
172
50.1k
{
173
50.1k
    QUIC_CFQ *cfq = OPENSSL_zalloc(sizeof(*cfq));
174
175
50.1k
    if (cfq == NULL)
176
0
        return NULL;
177
178
50.1k
    return cfq;
179
50.1k
}
180
181
static void clear_item(QUIC_CFQ_ITEM_EX *item)
182
158k
{
183
158k
    if (item->free_cb != NULL) {
184
89.8k
        item->free_cb(item->encoded, item->encoded_len, item->free_cb_arg);
185
186
89.8k
        item->free_cb = NULL;
187
89.8k
        item->encoded = NULL;
188
89.8k
        item->encoded_len = 0;
189
89.8k
    }
190
191
158k
    item->state = -1;
192
158k
}
193
194
static void free_list_items(QUIC_CFQ_ITEM_LIST *l)
195
150k
{
196
150k
    QUIC_CFQ_ITEM_EX *p, *pnext;
197
198
230k
    for (p = l->head; p != NULL; p = pnext) {
199
80.4k
        pnext = p->next;
200
80.4k
        clear_item(p);
201
80.4k
        OPENSSL_free(p);
202
80.4k
    }
203
150k
}
204
205
void ossl_quic_cfq_free(QUIC_CFQ *cfq)
206
50.1k
{
207
50.1k
    if (cfq == NULL)
208
0
        return;
209
210
50.1k
    free_list_items(&cfq->new_list);
211
50.1k
    free_list_items(&cfq->tx_list);
212
50.1k
    free_list_items(&cfq->free_list);
213
50.1k
    OPENSSL_free(cfq);
214
50.1k
}
215
216
static QUIC_CFQ_ITEM_EX *cfq_get_free(QUIC_CFQ *cfq)
217
89.8k
{
218
89.8k
    QUIC_CFQ_ITEM_EX *item = cfq->free_list.head;
219
220
89.8k
    if (item != NULL)
221
9.38k
        return item;
222
223
80.4k
    item = OPENSSL_zalloc(sizeof(*item));
224
80.4k
    if (item == NULL)
225
0
        return NULL;
226
227
80.4k
    item->state = -1;
228
80.4k
    list_insert_tail(&cfq->free_list, item);
229
80.4k
    return item;
230
80.4k
}
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
89.8k
{
242
89.8k
    QUIC_CFQ_ITEM_EX *item = cfq_get_free(cfq);
243
244
89.8k
    if (item == NULL)
245
0
        return NULL;
246
247
89.8k
    item->priority = priority;
248
89.8k
    item->frame_type = frame_type;
249
89.8k
    item->pn_space = pn_space;
250
89.8k
    item->encoded = (unsigned char *)encoded;
251
89.8k
    item->encoded_len = encoded_len;
252
89.8k
    item->free_cb = free_cb;
253
89.8k
    item->free_cb_arg = free_cb_arg;
254
255
89.8k
    item->state = QUIC_CFQ_STATE_NEW;
256
89.8k
    item->flags = flags;
257
89.8k
    list_remove(&cfq->free_list, item);
258
89.8k
    list_insert_sorted(&cfq->new_list, item, compare);
259
89.8k
    return &item->public;
260
89.8k
}
261
262
void ossl_quic_cfq_mark_tx(QUIC_CFQ *cfq, QUIC_CFQ_ITEM *item)
263
82.4k
{
264
82.4k
    QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
265
266
82.4k
    switch (ex->state) {
267
82.4k
    case QUIC_CFQ_STATE_NEW:
268
82.4k
        list_remove(&cfq->new_list, ex);
269
82.4k
        list_insert_tail(&cfq->tx_list, ex);
270
82.4k
        ex->state = QUIC_CFQ_STATE_TX;
271
82.4k
        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
82.4k
    }
278
82.4k
}
279
280
void ossl_quic_cfq_mark_lost(QUIC_CFQ *cfq, QUIC_CFQ_ITEM *item,
281
    uint32_t priority)
282
17.5k
{
283
17.5k
    QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
284
285
17.5k
    if (ossl_quic_cfq_item_is_unreliable(item)) {
286
13.1k
        ossl_quic_cfq_release(cfq, item);
287
13.1k
        return;
288
13.1k
    }
289
290
4.46k
    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
4.46k
    case QUIC_CFQ_STATE_TX:
299
4.46k
        if (priority != UINT32_MAX)
300
0
            ex->priority = priority;
301
4.46k
        list_remove(&cfq->tx_list, ex);
302
4.46k
        list_insert_sorted(&cfq->new_list, ex, compare);
303
4.46k
        ex->state = QUIC_CFQ_STATE_NEW;
304
4.46k
        break;
305
0
    default:
306
0
        assert(0); /* invalid state (e.g. in free state) */
307
0
        break;
308
4.46k
    }
309
4.46k
}
310
311
int ossl_quic_cfq_discard_unreliable(QUIC_CFQ *cfq, QUIC_CFQ_ITEM *item)
312
17.4k
{
313
17.4k
    int discarded;
314
315
17.4k
    if (ossl_quic_cfq_item_is_unreliable(item)) {
316
8.01k
        ossl_quic_cfq_release(cfq, item);
317
8.01k
        discarded = 1;
318
9.38k
    } else {
319
9.38k
        discarded = 0;
320
9.38k
    }
321
322
17.4k
    return discarded;
323
17.4k
}
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
77.9k
{
331
77.9k
    QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
332
333
77.9k
    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
77.9k
    case QUIC_CFQ_STATE_TX:
340
77.9k
        list_remove(&cfq->tx_list, ex);
341
77.9k
        list_insert_tail(&cfq->free_list, ex);
342
77.9k
        clear_item(ex);
343
77.9k
        break;
344
0
    default:
345
0
        assert(0); /* invalid state (e.g. in free state) */
346
0
        break;
347
77.9k
    }
348
77.9k
}
349
350
QUIC_CFQ_ITEM *ossl_quic_cfq_get_priority_head(const QUIC_CFQ *cfq,
351
    uint32_t pn_space)
352
87.5M
{
353
87.5M
    QUIC_CFQ_ITEM_EX *item = cfq->new_list.head;
354
355
118M
    for (; item != NULL && item->pn_space != pn_space; item = item->next)
356
30.4M
        ;
357
358
87.5M
    if (item == NULL)
359
85.1M
        return NULL;
360
361
2.36M
    return &item->public;
362
87.5M
}
363
364
QUIC_CFQ_ITEM *ossl_quic_cfq_item_get_priority_next(const QUIC_CFQ_ITEM *item,
365
    uint32_t pn_space)
366
85.7M
{
367
85.7M
    QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
368
369
85.7M
    if (ex == NULL)
370
0
        return NULL;
371
372
85.7M
    ex = ex->next;
373
374
85.7M
    for (; ex != NULL && ex->pn_space != pn_space; ex = ex->next)
375
0
        ;
376
377
85.7M
    if (ex == NULL)
378
2.36M
        return NULL; /* ubsan */
379
380
83.3M
    return &ex->public;
381
85.7M
}