Path: blob/main/crypto/openssl/ssl/quic/quic_cfq.c
108106 views
/*1* Copyright 2022-2024 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#include "internal/quic_cfq.h"10#include "internal/numbers.h"1112typedef struct quic_cfq_item_ex_st QUIC_CFQ_ITEM_EX;1314struct quic_cfq_item_ex_st {15QUIC_CFQ_ITEM public;16QUIC_CFQ_ITEM_EX *prev, *next;17unsigned char *encoded;18cfq_free_cb *free_cb;19void *free_cb_arg;20uint64_t frame_type;21size_t encoded_len;22uint32_t priority, pn_space, flags;23int state;24};2526uint64_t ossl_quic_cfq_item_get_frame_type(const QUIC_CFQ_ITEM *item)27{28QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;2930return ex->frame_type;31}3233const unsigned char *ossl_quic_cfq_item_get_encoded(const QUIC_CFQ_ITEM *item)34{35QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;3637return ex->encoded;38}3940size_t ossl_quic_cfq_item_get_encoded_len(const QUIC_CFQ_ITEM *item)41{42QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;4344return ex->encoded_len;45}4647int ossl_quic_cfq_item_get_state(const QUIC_CFQ_ITEM *item)48{49QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;5051return ex->state;52}5354uint32_t ossl_quic_cfq_item_get_pn_space(const QUIC_CFQ_ITEM *item)55{56QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;5758return ex->pn_space;59}6061int ossl_quic_cfq_item_is_unreliable(const QUIC_CFQ_ITEM *item)62{63QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;6465return (ex->flags & QUIC_CFQ_ITEM_FLAG_UNRELIABLE) != 0;66}6768typedef struct quic_cfq_item_list_st {69QUIC_CFQ_ITEM_EX *head, *tail;70} QUIC_CFQ_ITEM_LIST;7172struct quic_cfq_st {73/*74* Invariant: A CFQ item is always in exactly one of these lists, never more75* or less than one.76*77* Invariant: The list the CFQ item is determined exactly by the state field78* of the item.79*/80QUIC_CFQ_ITEM_LIST new_list, tx_list, free_list;81};8283static int compare(const QUIC_CFQ_ITEM_EX *a, const QUIC_CFQ_ITEM_EX *b)84{85if (a->pn_space < b->pn_space)86return -1;87else if (a->pn_space > b->pn_space)88return 1;8990if (a->priority > b->priority)91return -1;92else if (a->priority < b->priority)93return 1;9495return 0;96}9798static void list_remove(QUIC_CFQ_ITEM_LIST *l, QUIC_CFQ_ITEM_EX *n)99{100if (l->head == n)101l->head = n->next;102if (l->tail == n)103l->tail = n->prev;104if (n->prev != NULL)105n->prev->next = n->next;106if (n->next != NULL)107n->next->prev = n->prev;108n->prev = n->next = NULL;109}110111static void list_insert_head(QUIC_CFQ_ITEM_LIST *l, QUIC_CFQ_ITEM_EX *n)112{113n->next = l->head;114n->prev = NULL;115l->head = n;116if (n->next != NULL)117n->next->prev = n;118if (l->tail == NULL)119l->tail = n;120}121122static void list_insert_tail(QUIC_CFQ_ITEM_LIST *l, QUIC_CFQ_ITEM_EX *n)123{124n->prev = l->tail;125n->next = NULL;126l->tail = n;127if (n->prev != NULL)128n->prev->next = n;129if (l->head == NULL)130l->head = n;131}132133static void list_insert_after(QUIC_CFQ_ITEM_LIST *l,134QUIC_CFQ_ITEM_EX *ref,135QUIC_CFQ_ITEM_EX *n)136{137n->prev = ref;138n->next = ref->next;139if (ref->next != NULL)140ref->next->prev = n;141ref->next = n;142if (l->tail == ref)143l->tail = n;144}145146static void list_insert_sorted(QUIC_CFQ_ITEM_LIST *l, QUIC_CFQ_ITEM_EX *n,147int (*cmp)(const QUIC_CFQ_ITEM_EX *a,148const QUIC_CFQ_ITEM_EX *b))149{150QUIC_CFQ_ITEM_EX *p = l->head, *pprev = NULL;151152if (p == NULL) {153l->head = l->tail = n;154n->prev = n->next = NULL;155return;156}157158for (; p != NULL && cmp(p, n) < 0; pprev = p, p = p->next)159;160161if (p == NULL)162list_insert_tail(l, n);163else if (pprev == NULL)164list_insert_head(l, n);165else166list_insert_after(l, pprev, n);167}168169QUIC_CFQ *ossl_quic_cfq_new(void)170{171QUIC_CFQ *cfq = OPENSSL_zalloc(sizeof(*cfq));172173if (cfq == NULL)174return NULL;175176return cfq;177}178179static void clear_item(QUIC_CFQ_ITEM_EX *item)180{181if (item->free_cb != NULL) {182item->free_cb(item->encoded, item->encoded_len, item->free_cb_arg);183184item->free_cb = NULL;185item->encoded = NULL;186item->encoded_len = 0;187}188189item->state = -1;190}191192static void free_list_items(QUIC_CFQ_ITEM_LIST *l)193{194QUIC_CFQ_ITEM_EX *p, *pnext;195196for (p = l->head; p != NULL; p = pnext) {197pnext = p->next;198clear_item(p);199OPENSSL_free(p);200}201}202203void ossl_quic_cfq_free(QUIC_CFQ *cfq)204{205if (cfq == NULL)206return;207208free_list_items(&cfq->new_list);209free_list_items(&cfq->tx_list);210free_list_items(&cfq->free_list);211OPENSSL_free(cfq);212}213214static QUIC_CFQ_ITEM_EX *cfq_get_free(QUIC_CFQ *cfq)215{216QUIC_CFQ_ITEM_EX *item = cfq->free_list.head;217218if (item != NULL)219return item;220221item = OPENSSL_zalloc(sizeof(*item));222if (item == NULL)223return NULL;224225item->state = -1;226list_insert_tail(&cfq->free_list, item);227return item;228}229230QUIC_CFQ_ITEM *ossl_quic_cfq_add_frame(QUIC_CFQ *cfq,231uint32_t priority,232uint32_t pn_space,233uint64_t frame_type,234uint32_t flags,235const unsigned char *encoded,236size_t encoded_len,237cfq_free_cb *free_cb,238void *free_cb_arg)239{240QUIC_CFQ_ITEM_EX *item = cfq_get_free(cfq);241242if (item == NULL)243return NULL;244245item->priority = priority;246item->frame_type = frame_type;247item->pn_space = pn_space;248item->encoded = (unsigned char *)encoded;249item->encoded_len = encoded_len;250item->free_cb = free_cb;251item->free_cb_arg = free_cb_arg;252253item->state = QUIC_CFQ_STATE_NEW;254item->flags = flags;255list_remove(&cfq->free_list, item);256list_insert_sorted(&cfq->new_list, item, compare);257return &item->public;258}259260void ossl_quic_cfq_mark_tx(QUIC_CFQ *cfq, QUIC_CFQ_ITEM *item)261{262QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;263264switch (ex->state) {265case QUIC_CFQ_STATE_NEW:266list_remove(&cfq->new_list, ex);267list_insert_tail(&cfq->tx_list, ex);268ex->state = QUIC_CFQ_STATE_TX;269break;270case QUIC_CFQ_STATE_TX:271break; /* nothing to do */272default:273assert(0); /* invalid state (e.g. in free state) */274break;275}276}277278void ossl_quic_cfq_mark_lost(QUIC_CFQ *cfq, QUIC_CFQ_ITEM *item,279uint32_t priority)280{281QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;282283if (ossl_quic_cfq_item_is_unreliable(item)) {284ossl_quic_cfq_release(cfq, item);285return;286}287288switch (ex->state) {289case QUIC_CFQ_STATE_NEW:290if (priority != UINT32_MAX && priority != ex->priority) {291list_remove(&cfq->new_list, ex);292ex->priority = priority;293list_insert_sorted(&cfq->new_list, ex, compare);294}295break; /* nothing to do */296case QUIC_CFQ_STATE_TX:297if (priority != UINT32_MAX)298ex->priority = priority;299list_remove(&cfq->tx_list, ex);300list_insert_sorted(&cfq->new_list, ex, compare);301ex->state = QUIC_CFQ_STATE_NEW;302break;303default:304assert(0); /* invalid state (e.g. in free state) */305break;306}307}308309/*310* Releases a CFQ item. The item may be in either state (NEW or TX) prior to the311* call. The QUIC_CFQ_ITEM pointer must not be used following this call.312*/313void ossl_quic_cfq_release(QUIC_CFQ *cfq, QUIC_CFQ_ITEM *item)314{315QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;316317switch (ex->state) {318case QUIC_CFQ_STATE_NEW:319list_remove(&cfq->new_list, ex);320list_insert_tail(&cfq->free_list, ex);321clear_item(ex);322break;323case QUIC_CFQ_STATE_TX:324list_remove(&cfq->tx_list, ex);325list_insert_tail(&cfq->free_list, ex);326clear_item(ex);327break;328default:329assert(0); /* invalid state (e.g. in free state) */330break;331}332}333334QUIC_CFQ_ITEM *ossl_quic_cfq_get_priority_head(const QUIC_CFQ *cfq,335uint32_t pn_space)336{337QUIC_CFQ_ITEM_EX *item = cfq->new_list.head;338339for (; item != NULL && item->pn_space != pn_space; item = item->next)340;341342if (item == NULL)343return NULL;344345return &item->public;346}347348QUIC_CFQ_ITEM *ossl_quic_cfq_item_get_priority_next(const QUIC_CFQ_ITEM *item,349uint32_t pn_space)350{351QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;352353if (ex == NULL)354return NULL;355356ex = ex->next;357358for (; ex != NULL && ex->pn_space != pn_space; ex = ex->next)359;360361if (ex == NULL)362return NULL; /* ubsan */363364return &ex->public;365}366367368