Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/ssl/quic/quic_txpim.c
Line
Count
Source
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_txpim.h"
11
#include <stdlib.h>
12
13
typedef struct quic_txpim_pkt_ex_st QUIC_TXPIM_PKT_EX;
14
15
struct quic_txpim_pkt_ex_st {
16
    QUIC_TXPIM_PKT public;
17
    QUIC_TXPIM_PKT_EX *prev, *next;
18
    QUIC_TXPIM_CHUNK *chunks;
19
    size_t num_chunks, alloc_chunks;
20
    unsigned int chunks_need_sort : 1;
21
};
22
23
typedef struct quic_txpim_pkt_ex_list {
24
    QUIC_TXPIM_PKT_EX *head, *tail;
25
} QUIC_TXPIM_PKT_EX_LIST;
26
27
struct quic_txpim_st {
28
    QUIC_TXPIM_PKT_EX_LIST free_list;
29
    size_t in_use;
30
};
31
32
94.9k
#define MAX_ALLOC_CHUNKS 512
33
34
QUIC_TXPIM *ossl_quic_txpim_new(void)
35
51.3k
{
36
51.3k
    QUIC_TXPIM *txpim = OPENSSL_zalloc(sizeof(*txpim));
37
38
51.3k
    if (txpim == NULL)
39
0
        return NULL;
40
41
51.3k
    return txpim;
42
51.3k
}
43
44
static void free_list(QUIC_TXPIM_PKT_EX_LIST *l)
45
51.3k
{
46
51.3k
    QUIC_TXPIM_PKT_EX *n, *nnext;
47
48
1.01M
    for (n = l->head; n != NULL; n = nnext) {
49
966k
        nnext = n->next;
50
51
966k
        OPENSSL_free(n->chunks);
52
966k
        OPENSSL_free(n);
53
966k
    }
54
55
51.3k
    l->head = l->tail = NULL;
56
51.3k
}
57
58
void ossl_quic_txpim_free(QUIC_TXPIM *txpim)
59
51.3k
{
60
51.3k
    if (txpim == NULL)
61
0
        return;
62
63
51.3k
    assert(txpim->in_use == 0);
64
51.3k
    free_list(&txpim->free_list);
65
51.3k
    OPENSSL_free(txpim);
66
51.3k
}
67
68
static void list_remove(QUIC_TXPIM_PKT_EX_LIST *l, QUIC_TXPIM_PKT_EX *n)
69
43.9M
{
70
43.9M
    if (l->head == n)
71
43.9M
        l->head = n->next;
72
43.9M
    if (l->tail == n)
73
22.6M
        l->tail = n->prev;
74
43.9M
    if (n->prev != NULL)
75
0
        n->prev->next = n->next;
76
43.9M
    if (n->next != NULL)
77
21.3M
        n->next->prev = n->prev;
78
43.9M
    n->prev = n->next = NULL;
79
43.9M
}
80
81
static void list_insert_tail(QUIC_TXPIM_PKT_EX_LIST *l, QUIC_TXPIM_PKT_EX *n)
82
44.8M
{
83
44.8M
    n->prev = l->tail;
84
44.8M
    n->next = NULL;
85
44.8M
    l->tail = n;
86
44.8M
    if (n->prev != NULL)
87
22.2M
        n->prev->next = n;
88
44.8M
    if (l->head == NULL)
89
22.6M
        l->head = n;
90
44.8M
}
91
92
static QUIC_TXPIM_PKT_EX *txpim_get_free(QUIC_TXPIM *txpim)
93
43.9M
{
94
43.9M
    QUIC_TXPIM_PKT_EX *ex = txpim->free_list.head;
95
96
43.9M
    if (ex != NULL)
97
42.9M
        return ex;
98
99
966k
    ex = OPENSSL_zalloc(sizeof(*ex));
100
966k
    if (ex == NULL)
101
0
        return NULL;
102
103
966k
    list_insert_tail(&txpim->free_list, ex);
104
966k
    return ex;
105
966k
}
106
107
static void txpim_clear(QUIC_TXPIM_PKT_EX *ex)
108
43.9M
{
109
43.9M
    memset(&ex->public.ackm_pkt, 0, sizeof(ex->public.ackm_pkt));
110
43.9M
    ossl_quic_txpim_pkt_clear_chunks(&ex->public);
111
43.9M
    ex->public.retx_head = NULL;
112
43.9M
    ex->public.fifd = NULL;
113
43.9M
    ex->public.had_handshake_done_frame = 0;
114
43.9M
    ex->public.had_max_data_frame = 0;
115
43.9M
    ex->public.had_max_streams_bidi_frame = 0;
116
43.9M
    ex->public.had_max_streams_uni_frame = 0;
117
43.9M
    ex->public.had_ack_frame = 0;
118
43.9M
    ex->public.had_conn_close = 0;
119
43.9M
}
120
121
QUIC_TXPIM_PKT *ossl_quic_txpim_pkt_alloc(QUIC_TXPIM *txpim)
122
43.9M
{
123
43.9M
    QUIC_TXPIM_PKT_EX *ex = txpim_get_free(txpim);
124
125
43.9M
    if (ex == NULL)
126
0
        return NULL;
127
128
43.9M
    txpim_clear(ex);
129
43.9M
    list_remove(&txpim->free_list, ex);
130
43.9M
    ++txpim->in_use;
131
43.9M
    return &ex->public;
132
43.9M
}
133
134
void ossl_quic_txpim_pkt_release(QUIC_TXPIM *txpim, QUIC_TXPIM_PKT *fpkt)
135
43.9M
{
136
43.9M
    QUIC_TXPIM_PKT_EX *ex = (QUIC_TXPIM_PKT_EX *)fpkt;
137
138
43.9M
    assert(txpim->in_use > 0);
139
43.9M
    --txpim->in_use;
140
43.9M
    list_insert_tail(&txpim->free_list, ex);
141
43.9M
}
142
143
void ossl_quic_txpim_pkt_add_cfq_item(QUIC_TXPIM_PKT *fpkt,
144
    QUIC_CFQ_ITEM *item)
145
253k
{
146
253k
    item->pkt_next = fpkt->retx_head;
147
253k
    item->pkt_prev = NULL;
148
253k
    fpkt->retx_head = item;
149
253k
}
150
151
void ossl_quic_txpim_pkt_clear_chunks(QUIC_TXPIM_PKT *fpkt)
152
43.9M
{
153
43.9M
    QUIC_TXPIM_PKT_EX *ex = (QUIC_TXPIM_PKT_EX *)fpkt;
154
155
43.9M
    ex->num_chunks = 0;
156
43.9M
}
157
158
int ossl_quic_txpim_pkt_append_chunk(QUIC_TXPIM_PKT *fpkt,
159
    const QUIC_TXPIM_CHUNK *chunk)
160
114k
{
161
114k
    QUIC_TXPIM_PKT_EX *ex = (QUIC_TXPIM_PKT_EX *)fpkt;
162
114k
    QUIC_TXPIM_CHUNK *new_chunk;
163
114k
    size_t new_alloc_chunks = ex->alloc_chunks;
164
165
114k
    if (ex->num_chunks == ex->alloc_chunks) {
166
94.9k
        new_alloc_chunks = (ex->alloc_chunks == 0) ? 4 : ex->alloc_chunks * 8 / 5;
167
94.9k
        if (new_alloc_chunks > MAX_ALLOC_CHUNKS)
168
0
            new_alloc_chunks = MAX_ALLOC_CHUNKS;
169
94.9k
        if (ex->num_chunks == new_alloc_chunks)
170
0
            return 0;
171
172
94.9k
        new_chunk = OPENSSL_realloc(ex->chunks,
173
94.9k
            new_alloc_chunks * sizeof(QUIC_TXPIM_CHUNK));
174
94.9k
        if (new_chunk == NULL)
175
0
            return 0;
176
177
94.9k
        ex->chunks = new_chunk;
178
94.9k
        ex->alloc_chunks = new_alloc_chunks;
179
94.9k
    }
180
181
114k
    ex->chunks[ex->num_chunks++] = *chunk;
182
114k
    ex->chunks_need_sort = 1;
183
114k
    return 1;
184
114k
}
185
186
static int compare(const void *a, const void *b)
187
3.99k
{
188
3.99k
    const QUIC_TXPIM_CHUNK *ac = a, *bc = b;
189
190
3.99k
    if (ac->stream_id < bc->stream_id)
191
1.60k
        return -1;
192
2.38k
    else if (ac->stream_id > bc->stream_id)
193
1.92k
        return 1;
194
195
462
    if (ac->start < bc->start)
196
301
        return -1;
197
161
    else if (ac->start > bc->start)
198
0
        return 1;
199
200
161
    return 0;
201
462
}
202
203
const QUIC_TXPIM_CHUNK *ossl_quic_txpim_pkt_get_chunks(const QUIC_TXPIM_PKT *fpkt)
204
3.22M
{
205
3.22M
    QUIC_TXPIM_PKT_EX *ex = (QUIC_TXPIM_PKT_EX *)fpkt;
206
207
3.22M
    if (ex->chunks_need_sort) {
208
        /*
209
         * List of chunks will generally be very small so there is no issue
210
         * simply sorting here.
211
         */
212
111k
        qsort(ex->chunks, ex->num_chunks, sizeof(QUIC_TXPIM_CHUNK), compare);
213
111k
        ex->chunks_need_sort = 0;
214
111k
    }
215
216
3.22M
    return ex->chunks;
217
3.22M
}
218
219
size_t ossl_quic_txpim_pkt_get_num_chunks(const QUIC_TXPIM_PKT *fpkt)
220
3.22M
{
221
3.22M
    QUIC_TXPIM_PKT_EX *ex = (QUIC_TXPIM_PKT_EX *)fpkt;
222
223
3.22M
    return ex->num_chunks;
224
3.22M
}
225
226
size_t ossl_quic_txpim_get_in_use(const QUIC_TXPIM *txpim)
227
0
{
228
0
    return txpim->in_use;
229
0
}