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