2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "internal/quic_cfq.h"
12 typedef struct quic_cfq_item_ex_st QUIC_CFQ_ITEM_EX;
14 struct quic_cfq_item_ex_st {
16 QUIC_CFQ_ITEM_EX *prev, *next;
17 unsigned char *encoded;
22 uint32_t priority, pn_space, flags;
26 uint64_t ossl_quic_cfq_item_get_frame_type(const QUIC_CFQ_ITEM *item)
28 QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
30 return ex->frame_type;
33 const unsigned char *ossl_quic_cfq_item_get_encoded(const QUIC_CFQ_ITEM *item)
35 QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
40 size_t ossl_quic_cfq_item_get_encoded_len(const QUIC_CFQ_ITEM *item)
42 QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
44 return ex->encoded_len;
47 int ossl_quic_cfq_item_get_state(const QUIC_CFQ_ITEM *item)
49 QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
54 uint32_t ossl_quic_cfq_item_get_pn_space(const QUIC_CFQ_ITEM *item)
56 QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
61 int ossl_quic_cfq_item_is_unreliable(const QUIC_CFQ_ITEM *item)
63 QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
65 return (ex->flags & QUIC_CFQ_ITEM_FLAG_UNRELIABLE) != 0;
68 typedef struct quic_cfq_item_list_st {
69 QUIC_CFQ_ITEM_EX *head, *tail;
74 * Invariant: A CFQ item is always in exactly one of these lists, never more
77 * Invariant: The list the CFQ item is determined exactly by the state field
80 QUIC_CFQ_ITEM_LIST new_list, tx_list, free_list;
83 static int compare(const QUIC_CFQ_ITEM_EX *a, const QUIC_CFQ_ITEM_EX *b)
85 if (a->pn_space < b->pn_space)
87 else if (a->pn_space > b->pn_space)
90 if (a->priority > b->priority)
92 else if (a->priority < b->priority)
98 static void list_remove(QUIC_CFQ_ITEM_LIST *l, QUIC_CFQ_ITEM_EX *n)
105 n->prev->next = n->next;
107 n->next->prev = n->prev;
108 n->prev = n->next = NULL;
111 static void list_insert_head(QUIC_CFQ_ITEM_LIST *l, QUIC_CFQ_ITEM_EX *n)
122 static void list_insert_tail(QUIC_CFQ_ITEM_LIST *l, QUIC_CFQ_ITEM_EX *n)
133 static void list_insert_after(QUIC_CFQ_ITEM_LIST *l,
134 QUIC_CFQ_ITEM_EX *ref,
139 if (ref->next != NULL)
146 static void list_insert_sorted(QUIC_CFQ_ITEM_LIST *l, QUIC_CFQ_ITEM_EX *n,
147 int (*cmp)(const QUIC_CFQ_ITEM_EX *a,
148 const QUIC_CFQ_ITEM_EX *b))
150 QUIC_CFQ_ITEM_EX *p = l->head, *pprev = NULL;
153 l->head = l->tail = n;
154 n->prev = n->next = NULL;
158 for (; p != NULL && cmp(p, n) < 0; pprev = p, p = p->next);
161 list_insert_tail(l, n);
162 else if (pprev == NULL)
163 list_insert_head(l, n);
165 list_insert_after(l, pprev, n);
168 QUIC_CFQ *ossl_quic_cfq_new(void)
170 QUIC_CFQ *cfq = OPENSSL_zalloc(sizeof(*cfq));
178 static void clear_item(QUIC_CFQ_ITEM_EX *item)
180 if (item->free_cb != NULL) {
181 item->free_cb(item->encoded, item->encoded_len, item->free_cb_arg);
183 item->free_cb = NULL;
184 item->encoded = NULL;
185 item->encoded_len = 0;
191 static void free_list_items(QUIC_CFQ_ITEM_LIST *l)
193 QUIC_CFQ_ITEM_EX *p, *pnext;
195 for (p = l->head; p != NULL; p = pnext) {
202 void ossl_quic_cfq_free(QUIC_CFQ *cfq)
207 free_list_items(&cfq->new_list);
208 free_list_items(&cfq->tx_list);
209 free_list_items(&cfq->free_list);
213 static QUIC_CFQ_ITEM_EX *cfq_get_free(QUIC_CFQ *cfq)
215 QUIC_CFQ_ITEM_EX *item = cfq->free_list.head;
220 item = OPENSSL_zalloc(sizeof(*item));
225 list_insert_tail(&cfq->free_list, item);
229 QUIC_CFQ_ITEM *ossl_quic_cfq_add_frame(QUIC_CFQ *cfq,
234 const unsigned char *encoded,
236 cfq_free_cb *free_cb,
239 QUIC_CFQ_ITEM_EX *item = cfq_get_free(cfq);
244 item->priority = priority;
245 item->frame_type = frame_type;
246 item->pn_space = pn_space;
247 item->encoded = (unsigned char *)encoded;
248 item->encoded_len = encoded_len;
249 item->free_cb = free_cb;
250 item->free_cb_arg = free_cb_arg;
252 item->state = QUIC_CFQ_STATE_NEW;
254 list_remove(&cfq->free_list, item);
255 list_insert_sorted(&cfq->new_list, item, compare);
256 return &item->public;
259 void ossl_quic_cfq_mark_tx(QUIC_CFQ *cfq, QUIC_CFQ_ITEM *item)
261 QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
264 case QUIC_CFQ_STATE_NEW:
265 list_remove(&cfq->new_list, ex);
266 list_insert_tail(&cfq->tx_list, ex);
267 ex->state = QUIC_CFQ_STATE_TX;
269 case QUIC_CFQ_STATE_TX:
270 break; /* nothing to do */
272 assert(0); /* invalid state (e.g. in free state) */
277 void ossl_quic_cfq_mark_lost(QUIC_CFQ *cfq, QUIC_CFQ_ITEM *item,
280 QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
282 if (ossl_quic_cfq_item_is_unreliable(item)) {
283 ossl_quic_cfq_release(cfq, item);
288 case QUIC_CFQ_STATE_NEW:
289 if (priority != UINT32_MAX && priority != ex->priority) {
290 list_remove(&cfq->new_list, ex);
291 ex->priority = priority;
292 list_insert_sorted(&cfq->new_list, ex, compare);
294 break; /* nothing to do */
295 case QUIC_CFQ_STATE_TX:
296 if (priority != UINT32_MAX)
297 ex->priority = priority;
298 list_remove(&cfq->tx_list, ex);
299 list_insert_sorted(&cfq->new_list, ex, compare);
300 ex->state = QUIC_CFQ_STATE_NEW;
303 assert(0); /* invalid state (e.g. in free state) */
309 * Releases a CFQ item. The item may be in either state (NEW or TX) prior to the
310 * call. The QUIC_CFQ_ITEM pointer must not be used following this call.
312 void ossl_quic_cfq_release(QUIC_CFQ *cfq, QUIC_CFQ_ITEM *item)
314 QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
317 case QUIC_CFQ_STATE_NEW:
318 list_remove(&cfq->new_list, ex);
319 list_insert_tail(&cfq->free_list, ex);
322 case QUIC_CFQ_STATE_TX:
323 list_remove(&cfq->tx_list, ex);
324 list_insert_tail(&cfq->free_list, ex);
328 assert(0); /* invalid state (e.g. in free state) */
333 QUIC_CFQ_ITEM *ossl_quic_cfq_get_priority_head(const QUIC_CFQ *cfq,
336 QUIC_CFQ_ITEM_EX *item = cfq->new_list.head;
338 for (; item != NULL && item->pn_space != pn_space; item = item->next);
343 return &item->public;
346 QUIC_CFQ_ITEM *ossl_quic_cfq_item_get_priority_next(const QUIC_CFQ_ITEM *item,
349 QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;
356 for (; ex != NULL && ex->pn_space != pn_space; ex = ex->next);
359 return NULL; /* ubsan */