Coverage Report

Created: 2023-09-25 06:45

/src/openssl/ssl/quic/quic_txpim.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_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
0
#define MAX_ALLOC_CHUNKS 512
33
34
QUIC_TXPIM *ossl_quic_txpim_new(void)
35
0
{
36
0
    QUIC_TXPIM *txpim = OPENSSL_zalloc(sizeof(*txpim));
37
38
0
    if (txpim == NULL)
39
0
        return NULL;
40
41
0
    return txpim;
42
0
}
43
44
static void free_list(QUIC_TXPIM_PKT_EX_LIST *l)
45
0
{
46
0
    QUIC_TXPIM_PKT_EX *n, *nnext;
47
48
0
    for (n = l->head; n != NULL; n = nnext) {
49
0
        nnext = n->next;
50
51
0
        OPENSSL_free(n->chunks);
52
0
        OPENSSL_free(n);
53
0
    }
54
55
0
    l->head = l->tail = NULL;
56
0
}
57
58
void ossl_quic_txpim_free(QUIC_TXPIM *txpim)
59
0
{
60
0
    if (txpim == NULL)
61
0
        return;
62
63
0
    assert(txpim->in_use == 0);
64
0
    free_list(&txpim->free_list);
65
0
    OPENSSL_free(txpim);
66
0
}
67
68
static void list_remove(QUIC_TXPIM_PKT_EX_LIST *l, QUIC_TXPIM_PKT_EX *n)
69
0
{
70
0
    if (l->head == n)
71
0
        l->head = n->next;
72
0
    if (l->tail == n)
73
0
        l->tail = n->prev;
74
0
    if (n->prev != NULL)
75
0
        n->prev->next = n->next;
76
0
    if (n->next != NULL)
77
0
        n->next->prev = n->prev;
78
0
    n->prev = n->next = NULL;
79
0
}
80
81
static void list_insert_tail(QUIC_TXPIM_PKT_EX_LIST *l, QUIC_TXPIM_PKT_EX *n)
82
0
{
83
0
    n->prev = l->tail;
84
0
    n->next = NULL;
85
0
    l->tail = n;
86
0
    if (n->prev != NULL)
87
0
        n->prev->next = n;
88
0
    if (l->head == NULL)
89
0
        l->head = n;
90
0
}
91
92
static QUIC_TXPIM_PKT_EX *txpim_get_free(QUIC_TXPIM *txpim)
93
0
{
94
0
    QUIC_TXPIM_PKT_EX *ex = txpim->free_list.head;
95
96
0
    if (ex != NULL)
97
0
        return ex;
98
99
0
    ex = OPENSSL_zalloc(sizeof(*ex));
100
0
    if (ex == NULL)
101
0
        return NULL;
102
103
0
    list_insert_tail(&txpim->free_list, ex);
104
0
    return ex;
105
0
}
106
107
static void txpim_clear(QUIC_TXPIM_PKT_EX *ex)
108
0
{
109
0
    memset(&ex->public.ackm_pkt, 0, sizeof(ex->public.ackm_pkt));
110
0
    ossl_quic_txpim_pkt_clear_chunks(&ex->public);
111
0
    ex->public.retx_head                   = NULL;
112
0
    ex->public.fifd                        = NULL;
113
0
    ex->public.had_handshake_done_frame    = 0;
114
0
    ex->public.had_max_data_frame          = 0;
115
0
    ex->public.had_max_streams_bidi_frame  = 0;
116
0
    ex->public.had_max_streams_uni_frame   = 0;
117
0
    ex->public.had_ack_frame               = 0;
118
0
    ex->public.had_conn_close              = 0;
119
0
}
120
121
QUIC_TXPIM_PKT *ossl_quic_txpim_pkt_alloc(QUIC_TXPIM *txpim)
122
0
{
123
0
    QUIC_TXPIM_PKT_EX *ex = txpim_get_free(txpim);
124
125
0
    if (ex == NULL)
126
0
        return NULL;
127
128
0
    txpim_clear(ex);
129
0
    list_remove(&txpim->free_list, ex);
130
0
    ++txpim->in_use;
131
0
    return &ex->public;
132
0
}
133
134
void ossl_quic_txpim_pkt_release(QUIC_TXPIM *txpim, QUIC_TXPIM_PKT *fpkt)
135
0
{
136
0
    QUIC_TXPIM_PKT_EX *ex = (QUIC_TXPIM_PKT_EX *)fpkt;
137
138
0
    assert(txpim->in_use > 0);
139
0
    --txpim->in_use;
140
0
    list_insert_tail(&txpim->free_list, ex);
141
0
}
142
143
void ossl_quic_txpim_pkt_add_cfq_item(QUIC_TXPIM_PKT *fpkt,
144
                                      QUIC_CFQ_ITEM *item)
145
0
{
146
0
    item->pkt_next = fpkt->retx_head;
147
0
    item->pkt_prev = NULL;
148
0
    fpkt->retx_head = item;
149
0
}
150
151
void ossl_quic_txpim_pkt_clear_chunks(QUIC_TXPIM_PKT *fpkt)
152
0
{
153
0
    QUIC_TXPIM_PKT_EX *ex = (QUIC_TXPIM_PKT_EX *)fpkt;
154
155
0
    ex->num_chunks = 0;
156
0
}
157
158
int ossl_quic_txpim_pkt_append_chunk(QUIC_TXPIM_PKT *fpkt,
159
                                     const QUIC_TXPIM_CHUNK *chunk)
160
0
{
161
0
    QUIC_TXPIM_PKT_EX *ex = (QUIC_TXPIM_PKT_EX *)fpkt;
162
0
    QUIC_TXPIM_CHUNK *new_chunk;
163
0
    size_t new_alloc_chunks = ex->alloc_chunks;
164
165
0
    if (ex->num_chunks == ex->alloc_chunks) {
166
0
        new_alloc_chunks = (ex->alloc_chunks == 0) ? 4 : ex->alloc_chunks * 8 / 5;
167
0
        if (new_alloc_chunks > MAX_ALLOC_CHUNKS)
168
0
            new_alloc_chunks = MAX_ALLOC_CHUNKS;
169
0
        if (ex->num_chunks == new_alloc_chunks)
170
0
            return 0;
171
172
0
        new_chunk = OPENSSL_realloc(ex->chunks,
173
0
                                    new_alloc_chunks * sizeof(QUIC_TXPIM_CHUNK));
174
0
        if (new_chunk == NULL)
175
0
            return 0;
176
177
0
        ex->chunks          = new_chunk;
178
0
        ex->alloc_chunks    = new_alloc_chunks;
179
0
    }
180
181
0
    ex->chunks[ex->num_chunks++]    = *chunk;
182
0
    ex->chunks_need_sort            = 1;
183
0
    return 1;
184
0
}
185
186
static int compare(const void *a, const void *b)
187
0
{
188
0
    const QUIC_TXPIM_CHUNK *ac = a, *bc = b;
189
190
0
    if (ac->stream_id < bc->stream_id)
191
0
        return -1;
192
0
    else if (ac->stream_id > bc->stream_id)
193
0
        return 1;
194
195
0
    if (ac->start < bc->start)
196
0
        return -1;
197
0
    else if (ac->start > bc->start)
198
0
        return 1;
199
200
0
    return 0;
201
0
}
202
203
const QUIC_TXPIM_CHUNK *ossl_quic_txpim_pkt_get_chunks(const QUIC_TXPIM_PKT *fpkt)
204
0
{
205
0
    QUIC_TXPIM_PKT_EX *ex = (QUIC_TXPIM_PKT_EX *)fpkt;
206
207
0
    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
0
        qsort(ex->chunks, ex->num_chunks, sizeof(QUIC_TXPIM_CHUNK), compare);
213
0
        ex->chunks_need_sort = 0;
214
0
    }
215
216
0
    return ex->chunks;
217
0
}
218
219
size_t ossl_quic_txpim_pkt_get_num_chunks(const QUIC_TXPIM_PKT *fpkt)
220
0
{
221
0
    QUIC_TXPIM_PKT_EX *ex = (QUIC_TXPIM_PKT_EX *)fpkt;
222
223
0
    return ex->num_chunks;
224
0
}
225
226
size_t ossl_quic_txpim_get_in_use(const QUIC_TXPIM *txpim)
227
0
{
228
0
    return txpim->in_use;
229
0
}