/src/openssl35/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 | 219M | { |
29 | 219M | QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item; |
30 | | |
31 | 219M | return ex->frame_type; |
32 | 219M | } |
33 | | |
34 | | const unsigned char *ossl_quic_cfq_item_get_encoded(const QUIC_CFQ_ITEM *item) |
35 | 212M | { |
36 | 212M | QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item; |
37 | | |
38 | 212M | return ex->encoded; |
39 | 212M | } |
40 | | |
41 | | size_t ossl_quic_cfq_item_get_encoded_len(const QUIC_CFQ_ITEM *item) |
42 | 212M | { |
43 | 212M | QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item; |
44 | | |
45 | 212M | return ex->encoded_len; |
46 | 212M | } |
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 | 52.3k | { |
64 | 52.3k | QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item; |
65 | | |
66 | 52.3k | return (ex->flags & QUIC_CFQ_ITEM_FLAG_UNRELIABLE) != 0; |
67 | 52.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 | 465k | { |
86 | 465k | if (a->pn_space < b->pn_space) |
87 | 0 | return -1; |
88 | 465k | else if (a->pn_space > b->pn_space) |
89 | 0 | return 1; |
90 | | |
91 | 465k | if (a->priority > b->priority) |
92 | 203k | return -1; |
93 | 262k | else if (a->priority < b->priority) |
94 | 1.64k | return 1; |
95 | | |
96 | 260k | return 0; |
97 | 465k | } |
98 | | |
99 | | static void list_remove(QUIC_CFQ_ITEM_LIST *l, QUIC_CFQ_ITEM_EX *n) |
100 | 767k | { |
101 | 767k | if (l->head == n) |
102 | 521k | l->head = n->next; |
103 | 767k | if (l->tail == n) |
104 | 406k | l->tail = n->prev; |
105 | 767k | if (n->prev != NULL) |
106 | 246k | n->prev->next = n->next; |
107 | 767k | if (n->next != NULL) |
108 | 361k | n->next->prev = n->prev; |
109 | 767k | n->prev = n->next = NULL; |
110 | 767k | } |
111 | | |
112 | | static void list_insert_head(QUIC_CFQ_ITEM_LIST *l, QUIC_CFQ_ITEM_EX *n) |
113 | 170k | { |
114 | 170k | n->next = l->head; |
115 | 170k | n->prev = NULL; |
116 | 170k | l->head = n; |
117 | 170k | if (n->next != NULL) |
118 | 170k | n->next->prev = n; |
119 | 170k | if (l->tail == NULL) |
120 | 0 | l->tail = n; |
121 | 170k | } |
122 | | |
123 | | static void list_insert_tail(QUIC_CFQ_ITEM_LIST *l, QUIC_CFQ_ITEM_EX *n) |
124 | 739k | { |
125 | 739k | n->prev = l->tail; |
126 | 739k | n->next = NULL; |
127 | 739k | l->tail = n; |
128 | 739k | if (n->prev != NULL) |
129 | 480k | n->prev->next = n; |
130 | 739k | if (l->head == NULL) |
131 | 258k | l->head = n; |
132 | 739k | } |
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 | 92.3k | { |
138 | 92.3k | n->prev = ref; |
139 | 92.3k | n->next = ref->next; |
140 | 92.3k | if (ref->next != NULL) |
141 | 92.3k | ref->next->prev = n; |
142 | 92.3k | ref->next = n; |
143 | 92.3k | if (l->tail == ref) |
144 | 0 | l->tail = n; |
145 | 92.3k | } |
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 | 275k | { |
151 | 275k | QUIC_CFQ_ITEM_EX *p = l->head, *pprev = NULL; |
152 | | |
153 | 275k | if (p == NULL) { |
154 | 11.0k | l->head = l->tail = n; |
155 | 11.0k | n->prev = n->next = NULL; |
156 | 11.0k | return; |
157 | 11.0k | } |
158 | | |
159 | 467k | for (; p != NULL && cmp(p, n) < 0; pprev = p, p = p->next) |
160 | 203k | ; |
161 | | |
162 | 264k | if (p == NULL) |
163 | 2.14k | list_insert_tail(l, n); |
164 | 262k | else if (pprev == NULL) |
165 | 170k | list_insert_head(l, n); |
166 | 92.3k | else |
167 | 92.3k | list_insert_after(l, pprev, n); |
168 | 264k | } |
169 | | |
170 | | QUIC_CFQ *ossl_quic_cfq_new(void) |
171 | 50.4k | { |
172 | 50.4k | QUIC_CFQ *cfq = OPENSSL_zalloc(sizeof(*cfq)); |
173 | | |
174 | 50.4k | if (cfq == NULL) |
175 | 0 | return NULL; |
176 | | |
177 | 50.4k | return cfq; |
178 | 50.4k | } |
179 | | |
180 | | static void clear_item(QUIC_CFQ_ITEM_EX *item) |
181 | 488k | { |
182 | 488k | if (item->free_cb != NULL) { |
183 | 269k | item->free_cb(item->encoded, item->encoded_len, item->free_cb_arg); |
184 | | |
185 | 269k | item->free_cb = NULL; |
186 | 269k | item->encoded = NULL; |
187 | 269k | item->encoded_len = 0; |
188 | 269k | } |
189 | | |
190 | 488k | item->state = -1; |
191 | 488k | } |
192 | | |
193 | | static void free_list_items(QUIC_CFQ_ITEM_LIST *l) |
194 | 151k | { |
195 | 151k | QUIC_CFQ_ITEM_EX *p, *pnext; |
196 | | |
197 | 397k | for (p = l->head; p != NULL; p = pnext) { |
198 | 246k | pnext = p->next; |
199 | 246k | clear_item(p); |
200 | 246k | OPENSSL_free(p); |
201 | 246k | } |
202 | 151k | } |
203 | | |
204 | | void ossl_quic_cfq_free(QUIC_CFQ *cfq) |
205 | 50.4k | { |
206 | 50.4k | if (cfq == NULL) |
207 | 0 | return; |
208 | | |
209 | 50.4k | free_list_items(&cfq->new_list); |
210 | 50.4k | free_list_items(&cfq->tx_list); |
211 | 50.4k | free_list_items(&cfq->free_list); |
212 | 50.4k | OPENSSL_free(cfq); |
213 | 50.4k | } |
214 | | |
215 | | static QUIC_CFQ_ITEM_EX *cfq_get_free(QUIC_CFQ *cfq) |
216 | 269k | { |
217 | 269k | QUIC_CFQ_ITEM_EX *item = cfq->free_list.head; |
218 | | |
219 | 269k | if (item != NULL) |
220 | 23.1k | return item; |
221 | | |
222 | 246k | item = OPENSSL_zalloc(sizeof(*item)); |
223 | 246k | if (item == NULL) |
224 | 0 | return NULL; |
225 | | |
226 | 246k | item->state = -1; |
227 | 246k | list_insert_tail(&cfq->free_list, item); |
228 | 246k | return item; |
229 | 246k | } |
230 | | |
231 | | QUIC_CFQ_ITEM *ossl_quic_cfq_add_frame(QUIC_CFQ *cfq, |
232 | | uint32_t priority, |
233 | | uint32_t pn_space, |
234 | | uint64_t frame_type, |
235 | | uint32_t flags, |
236 | | const unsigned char *encoded, |
237 | | size_t encoded_len, |
238 | | cfq_free_cb *free_cb, |
239 | | void *free_cb_arg) |
240 | 269k | { |
241 | 269k | QUIC_CFQ_ITEM_EX *item = cfq_get_free(cfq); |
242 | | |
243 | 269k | if (item == NULL) |
244 | 0 | return NULL; |
245 | | |
246 | 269k | item->priority = priority; |
247 | 269k | item->frame_type = frame_type; |
248 | 269k | item->pn_space = pn_space; |
249 | 269k | item->encoded = (unsigned char *)encoded; |
250 | 269k | item->encoded_len = encoded_len; |
251 | 269k | item->free_cb = free_cb; |
252 | 269k | item->free_cb_arg = free_cb_arg; |
253 | | |
254 | 269k | item->state = QUIC_CFQ_STATE_NEW; |
255 | 269k | item->flags = flags; |
256 | 269k | list_remove(&cfq->free_list, item); |
257 | 269k | list_insert_sorted(&cfq->new_list, item, compare); |
258 | 269k | return &item->public; |
259 | 269k | } |
260 | | |
261 | | void ossl_quic_cfq_mark_tx(QUIC_CFQ *cfq, QUIC_CFQ_ITEM *item) |
262 | 248k | { |
263 | 248k | QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item; |
264 | | |
265 | 248k | switch (ex->state) { |
266 | 248k | case QUIC_CFQ_STATE_NEW: |
267 | 248k | list_remove(&cfq->new_list, ex); |
268 | 248k | list_insert_tail(&cfq->tx_list, ex); |
269 | 248k | ex->state = QUIC_CFQ_STATE_TX; |
270 | 248k | break; |
271 | 0 | case QUIC_CFQ_STATE_TX: |
272 | 0 | break; /* nothing to do */ |
273 | 0 | default: |
274 | 0 | assert(0); /* invalid state (e.g. in free state) */ |
275 | 0 | break; |
276 | 248k | } |
277 | 248k | } |
278 | | |
279 | | void ossl_quic_cfq_mark_lost(QUIC_CFQ *cfq, QUIC_CFQ_ITEM *item, |
280 | | uint32_t priority) |
281 | 52.3k | { |
282 | 52.3k | QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item; |
283 | | |
284 | 52.3k | if (ossl_quic_cfq_item_is_unreliable(item)) { |
285 | 46.0k | ossl_quic_cfq_release(cfq, item); |
286 | 46.0k | return; |
287 | 46.0k | } |
288 | | |
289 | 6.31k | switch (ex->state) { |
290 | 0 | case QUIC_CFQ_STATE_NEW: |
291 | 0 | if (priority != UINT32_MAX && priority != ex->priority) { |
292 | 0 | list_remove(&cfq->new_list, ex); |
293 | 0 | ex->priority = priority; |
294 | 0 | list_insert_sorted(&cfq->new_list, ex, compare); |
295 | 0 | } |
296 | 0 | break; /* nothing to do */ |
297 | 6.31k | case QUIC_CFQ_STATE_TX: |
298 | 6.31k | if (priority != UINT32_MAX) |
299 | 0 | ex->priority = priority; |
300 | 6.31k | list_remove(&cfq->tx_list, ex); |
301 | 6.31k | list_insert_sorted(&cfq->new_list, ex, compare); |
302 | 6.31k | ex->state = QUIC_CFQ_STATE_NEW; |
303 | 6.31k | break; |
304 | 0 | default: |
305 | 0 | assert(0); /* invalid state (e.g. in free state) */ |
306 | 0 | break; |
307 | 6.31k | } |
308 | 6.31k | } |
309 | | |
310 | | /* |
311 | | * Releases a CFQ item. The item may be in either state (NEW or TX) prior to the |
312 | | * call. The QUIC_CFQ_ITEM pointer must not be used following this call. |
313 | | */ |
314 | | void ossl_quic_cfq_release(QUIC_CFQ *cfq, QUIC_CFQ_ITEM *item) |
315 | 242k | { |
316 | 242k | QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item; |
317 | | |
318 | 242k | switch (ex->state) { |
319 | 0 | case QUIC_CFQ_STATE_NEW: |
320 | 0 | list_remove(&cfq->new_list, ex); |
321 | 0 | list_insert_tail(&cfq->free_list, ex); |
322 | 0 | clear_item(ex); |
323 | 0 | break; |
324 | 242k | case QUIC_CFQ_STATE_TX: |
325 | 242k | list_remove(&cfq->tx_list, ex); |
326 | 242k | list_insert_tail(&cfq->free_list, ex); |
327 | 242k | clear_item(ex); |
328 | 242k | break; |
329 | 0 | default: |
330 | 0 | assert(0); /* invalid state (e.g. in free state) */ |
331 | 0 | break; |
332 | 242k | } |
333 | 242k | } |
334 | | |
335 | | QUIC_CFQ_ITEM *ossl_quic_cfq_get_priority_head(const QUIC_CFQ *cfq, |
336 | | uint32_t pn_space) |
337 | 86.5M | { |
338 | 86.5M | QUIC_CFQ_ITEM_EX *item = cfq->new_list.head; |
339 | | |
340 | 174M | for (; item != NULL && item->pn_space != pn_space; item = item->next) |
341 | 88.1M | ; |
342 | | |
343 | 86.5M | if (item == NULL) |
344 | 84.1M | return NULL; |
345 | | |
346 | 2.45M | return &item->public; |
347 | 86.5M | } |
348 | | |
349 | | QUIC_CFQ_ITEM *ossl_quic_cfq_item_get_priority_next(const QUIC_CFQ_ITEM *item, |
350 | | uint32_t pn_space) |
351 | 219M | { |
352 | 219M | QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item; |
353 | | |
354 | 219M | if (ex == NULL) |
355 | 0 | return NULL; |
356 | | |
357 | 219M | ex = ex->next; |
358 | | |
359 | 219M | for (; ex != NULL && ex->pn_space != pn_space; ex = ex->next) |
360 | 0 | ; |
361 | | |
362 | 219M | if (ex == NULL) |
363 | 2.45M | return NULL; /* ubsan */ |
364 | | |
365 | 216M | return &ex->public; |
366 | 219M | } |