Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/ssl/quic/quic_cfq.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2022-2023 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
111M
{
29
111M
    QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
30
31
111M
    return ex->frame_type;
32
111M
}
33
34
const unsigned char *ossl_quic_cfq_item_get_encoded(const QUIC_CFQ_ITEM *item)
35
106M
{
36
106M
    QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
37
38
106M
    return ex->encoded;
39
106M
}
40
41
size_t ossl_quic_cfq_item_get_encoded_len(const QUIC_CFQ_ITEM *item)
42
106M
{
43
106M
    QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
44
45
106M
    return ex->encoded_len;
46
106M
}
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
22.7k
{
64
22.7k
    QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
65
66
22.7k
    return (ex->flags & QUIC_CFQ_ITEM_FLAG_UNRELIABLE) != 0;
67
22.7k
}
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
171k
{
86
171k
    if (a->pn_space < b->pn_space)
87
0
        return -1;
88
171k
    else if (a->pn_space > b->pn_space)
89
0
        return 1;
90
91
171k
    if (a->priority > b->priority)
92
59.5k
        return -1;
93
112k
    else if (a->priority < b->priority)
94
498
        return 1;
95
96
111k
    return 0;
97
171k
}
98
99
static void list_remove(QUIC_CFQ_ITEM_LIST *l, QUIC_CFQ_ITEM_EX *n)
100
329k
{
101
329k
    if (l->head == n)
102
222k
        l->head = n->next;
103
329k
    if (l->tail == n)
104
175k
        l->tail = n->prev;
105
329k
    if (n->prev != NULL)
106
106k
        n->prev->next = n->next;
107
329k
    if (n->next != NULL)
108
153k
        n->next->prev = n->prev;
109
329k
    n->prev = n->next = NULL;
110
329k
}
111
112
static void list_insert_head(QUIC_CFQ_ITEM_LIST *l, QUIC_CFQ_ITEM_EX *n)
113
75.9k
{
114
75.9k
    n->next = l->head;
115
75.9k
    n->prev = NULL;
116
75.9k
    l->head = n;
117
75.9k
    if (n->next != NULL)
118
75.9k
        n->next->prev = n;
119
75.9k
    if (l->tail == NULL)
120
0
        l->tail = n;
121
75.9k
}
122
123
static void list_insert_tail(QUIC_CFQ_ITEM_LIST *l, QUIC_CFQ_ITEM_EX *n)
124
318k
{
125
318k
    n->prev = l->tail;
126
318k
    n->next = NULL;
127
318k
    l->tail = n;
128
318k
    if (n->prev != NULL)
129
207k
        n->prev->next = n;
130
318k
    if (l->head == NULL)
131
110k
        l->head = n;
132
318k
}
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
36.2k
{
138
36.2k
    n->prev = ref;
139
36.2k
    n->next = ref->next;
140
36.2k
    if (ref->next != NULL)
141
36.2k
        ref->next->prev = n;
142
36.2k
    ref->next = n;
143
36.2k
    if (l->tail == ref)
144
0
        l->tail = n;
145
36.2k
}
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
118k
{
151
118k
    QUIC_CFQ_ITEM_EX *p = l->head, *pprev = NULL;
152
153
118k
    if (p == NULL) {
154
4.74k
        l->head = l->tail = n;
155
4.74k
        n->prev = n->next = NULL;
156
4.74k
        return;
157
4.74k
    }
158
159
172k
    for (; p != NULL && cmp(p, n) < 0; pprev = p, p = p->next);
160
161
113k
    if (p == NULL)
162
1.05k
        list_insert_tail(l, n);
163
112k
    else if (pprev == NULL)
164
75.9k
        list_insert_head(l, n);
165
36.2k
    else
166
36.2k
        list_insert_after(l, pprev, n);
167
113k
}
168
169
QUIC_CFQ *ossl_quic_cfq_new(void)
170
22.0k
{
171
22.0k
    QUIC_CFQ *cfq = OPENSSL_zalloc(sizeof(*cfq));
172
173
22.0k
    if (cfq == NULL)
174
0
        return NULL;
175
176
22.0k
    return cfq;
177
22.0k
}
178
179
static void clear_item(QUIC_CFQ_ITEM_EX *item)
180
210k
{
181
210k
    if (item->free_cb != NULL) {
182
115k
        item->free_cb(item->encoded, item->encoded_len, item->free_cb_arg);
183
184
115k
        item->free_cb       = NULL;
185
115k
        item->encoded       = NULL;
186
115k
        item->encoded_len   = 0;
187
115k
    }
188
189
210k
    item->state = -1;
190
210k
}
191
192
static void free_list_items(QUIC_CFQ_ITEM_LIST *l)
193
66.2k
{
194
66.2k
    QUIC_CFQ_ITEM_EX *p, *pnext;
195
196
171k
    for (p = l->head; p != NULL; p = pnext) {
197
105k
        pnext = p->next;
198
105k
        clear_item(p);
199
105k
        OPENSSL_free(p);
200
105k
    }
201
66.2k
}
202
203
void ossl_quic_cfq_free(QUIC_CFQ *cfq)
204
22.0k
{
205
22.0k
    if (cfq == NULL)
206
0
        return;
207
208
22.0k
    free_list_items(&cfq->new_list);
209
22.0k
    free_list_items(&cfq->tx_list);
210
22.0k
    free_list_items(&cfq->free_list);
211
22.0k
    OPENSSL_free(cfq);
212
22.0k
}
213
214
static QUIC_CFQ_ITEM_EX *cfq_get_free(QUIC_CFQ *cfq)
215
115k
{
216
115k
    QUIC_CFQ_ITEM_EX *item = cfq->free_list.head;
217
218
115k
    if (item != NULL)
219
9.81k
        return item;
220
221
105k
    item = OPENSSL_zalloc(sizeof(*item));
222
105k
    if (item == NULL)
223
0
        return NULL;
224
225
105k
    item->state = -1;
226
105k
    list_insert_tail(&cfq->free_list, item);
227
105k
    return item;
228
105k
}
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
115k
{
240
115k
    QUIC_CFQ_ITEM_EX *item = cfq_get_free(cfq);
241
242
115k
    if (item == NULL)
243
0
        return NULL;
244
245
115k
    item->priority      = priority;
246
115k
    item->frame_type    = frame_type;
247
115k
    item->pn_space      = pn_space;
248
115k
    item->encoded       = (unsigned char *)encoded;
249
115k
    item->encoded_len   = encoded_len;
250
115k
    item->free_cb       = free_cb;
251
115k
    item->free_cb_arg   = free_cb_arg;
252
253
115k
    item->state = QUIC_CFQ_STATE_NEW;
254
115k
    item->flags = flags;
255
115k
    list_remove(&cfq->free_list, item);
256
115k
    list_insert_sorted(&cfq->new_list, item, compare);
257
115k
    return &item->public;
258
115k
}
259
260
void ossl_quic_cfq_mark_tx(QUIC_CFQ *cfq, QUIC_CFQ_ITEM *item)
261
107k
{
262
107k
    QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
263
264
107k
    switch (ex->state) {
265
107k
    case QUIC_CFQ_STATE_NEW:
266
107k
        list_remove(&cfq->new_list, ex);
267
107k
        list_insert_tail(&cfq->tx_list, ex);
268
107k
        ex->state = QUIC_CFQ_STATE_TX;
269
107k
        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
107k
    }
276
107k
}
277
278
void ossl_quic_cfq_mark_lost(QUIC_CFQ *cfq, QUIC_CFQ_ITEM *item,
279
                             uint32_t priority)
280
22.7k
{
281
22.7k
    QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
282
283
22.7k
    if (ossl_quic_cfq_item_is_unreliable(item)) {
284
20.1k
        ossl_quic_cfq_release(cfq, item);
285
20.1k
        return;
286
20.1k
    }
287
288
2.58k
    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
2.58k
    case QUIC_CFQ_STATE_TX:
297
2.58k
        if (priority != UINT32_MAX)
298
0
            ex->priority = priority;
299
2.58k
        list_remove(&cfq->tx_list, ex);
300
2.58k
        list_insert_sorted(&cfq->new_list, ex, compare);
301
2.58k
        ex->state = QUIC_CFQ_STATE_NEW;
302
2.58k
        break;
303
0
    default:
304
0
        assert(0); /* invalid state (e.g. in free state) */
305
0
        break;
306
2.58k
    }
307
2.58k
}
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
104k
{
315
104k
    QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
316
317
104k
    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
104k
    case QUIC_CFQ_STATE_TX:
324
104k
        list_remove(&cfq->tx_list, ex);
325
104k
        list_insert_tail(&cfq->free_list, ex);
326
104k
        clear_item(ex);
327
104k
        break;
328
0
    default:
329
0
        assert(0); /* invalid state (e.g. in free state) */
330
0
        break;
331
104k
    }
332
104k
}
333
334
QUIC_CFQ_ITEM *ossl_quic_cfq_get_priority_head(const QUIC_CFQ *cfq,
335
                                               uint32_t pn_space)
336
33.9M
{
337
33.9M
    QUIC_CFQ_ITEM_EX *item = cfq->new_list.head;
338
339
97.3M
    for (; item != NULL && item->pn_space != pn_space; item = item->next);
340
341
33.9M
    if (item == NULL)
342
32.7M
        return NULL;
343
344
1.22M
    return &item->public;
345
33.9M
}
346
347
QUIC_CFQ_ITEM *ossl_quic_cfq_item_get_priority_next(const QUIC_CFQ_ITEM *item,
348
                                                    uint32_t pn_space)
349
111M
{
350
111M
    QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
351
352
111M
    if (ex == NULL)
353
0
        return NULL;
354
355
111M
     ex = ex->next;
356
357
111M
     for (; ex != NULL && ex->pn_space != pn_space; ex = ex->next);
358
359
111M
     if (ex == NULL)
360
1.22M
         return NULL; /* ubsan */
361
362
110M
     return &ex->public;
363
111M
}