Coverage Report

Created: 2025-12-04 06:33

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