Path: blob/main/crypto/openssl/ssl/quic/quic_cfq.c
48261 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);159160if (p == NULL)161list_insert_tail(l, n);162else if (pprev == NULL)163list_insert_head(l, n);164else165list_insert_after(l, pprev, n);166}167168QUIC_CFQ *ossl_quic_cfq_new(void)169{170QUIC_CFQ *cfq = OPENSSL_zalloc(sizeof(*cfq));171172if (cfq == NULL)173return NULL;174175return cfq;176}177178static void clear_item(QUIC_CFQ_ITEM_EX *item)179{180if (item->free_cb != NULL) {181item->free_cb(item->encoded, item->encoded_len, item->free_cb_arg);182183item->free_cb = NULL;184item->encoded = NULL;185item->encoded_len = 0;186}187188item->state = -1;189}190191static void free_list_items(QUIC_CFQ_ITEM_LIST *l)192{193QUIC_CFQ_ITEM_EX *p, *pnext;194195for (p = l->head; p != NULL; p = pnext) {196pnext = p->next;197clear_item(p);198OPENSSL_free(p);199}200}201202void ossl_quic_cfq_free(QUIC_CFQ *cfq)203{204if (cfq == NULL)205return;206207free_list_items(&cfq->new_list);208free_list_items(&cfq->tx_list);209free_list_items(&cfq->free_list);210OPENSSL_free(cfq);211}212213static QUIC_CFQ_ITEM_EX *cfq_get_free(QUIC_CFQ *cfq)214{215QUIC_CFQ_ITEM_EX *item = cfq->free_list.head;216217if (item != NULL)218return item;219220item = OPENSSL_zalloc(sizeof(*item));221if (item == NULL)222return NULL;223224item->state = -1;225list_insert_tail(&cfq->free_list, item);226return item;227}228229QUIC_CFQ_ITEM *ossl_quic_cfq_add_frame(QUIC_CFQ *cfq,230uint32_t priority,231uint32_t pn_space,232uint64_t frame_type,233uint32_t flags,234const unsigned char *encoded,235size_t encoded_len,236cfq_free_cb *free_cb,237void *free_cb_arg)238{239QUIC_CFQ_ITEM_EX *item = cfq_get_free(cfq);240241if (item == NULL)242return NULL;243244item->priority = priority;245item->frame_type = frame_type;246item->pn_space = pn_space;247item->encoded = (unsigned char *)encoded;248item->encoded_len = encoded_len;249item->free_cb = free_cb;250item->free_cb_arg = free_cb_arg;251252item->state = QUIC_CFQ_STATE_NEW;253item->flags = flags;254list_remove(&cfq->free_list, item);255list_insert_sorted(&cfq->new_list, item, compare);256return &item->public;257}258259void ossl_quic_cfq_mark_tx(QUIC_CFQ *cfq, QUIC_CFQ_ITEM *item)260{261QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;262263switch (ex->state) {264case QUIC_CFQ_STATE_NEW:265list_remove(&cfq->new_list, ex);266list_insert_tail(&cfq->tx_list, ex);267ex->state = QUIC_CFQ_STATE_TX;268break;269case QUIC_CFQ_STATE_TX:270break; /* nothing to do */271default:272assert(0); /* invalid state (e.g. in free state) */273break;274}275}276277void ossl_quic_cfq_mark_lost(QUIC_CFQ *cfq, QUIC_CFQ_ITEM *item,278uint32_t priority)279{280QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;281282if (ossl_quic_cfq_item_is_unreliable(item)) {283ossl_quic_cfq_release(cfq, item);284return;285}286287switch (ex->state) {288case QUIC_CFQ_STATE_NEW:289if (priority != UINT32_MAX && priority != ex->priority) {290list_remove(&cfq->new_list, ex);291ex->priority = priority;292list_insert_sorted(&cfq->new_list, ex, compare);293}294break; /* nothing to do */295case QUIC_CFQ_STATE_TX:296if (priority != UINT32_MAX)297ex->priority = priority;298list_remove(&cfq->tx_list, ex);299list_insert_sorted(&cfq->new_list, ex, compare);300ex->state = QUIC_CFQ_STATE_NEW;301break;302default:303assert(0); /* invalid state (e.g. in free state) */304break;305}306}307308/*309* Releases a CFQ item. The item may be in either state (NEW or TX) prior to the310* call. The QUIC_CFQ_ITEM pointer must not be used following this call.311*/312void ossl_quic_cfq_release(QUIC_CFQ *cfq, QUIC_CFQ_ITEM *item)313{314QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;315316switch (ex->state) {317case QUIC_CFQ_STATE_NEW:318list_remove(&cfq->new_list, ex);319list_insert_tail(&cfq->free_list, ex);320clear_item(ex);321break;322case QUIC_CFQ_STATE_TX:323list_remove(&cfq->tx_list, ex);324list_insert_tail(&cfq->free_list, ex);325clear_item(ex);326break;327default:328assert(0); /* invalid state (e.g. in free state) */329break;330}331}332333QUIC_CFQ_ITEM *ossl_quic_cfq_get_priority_head(const QUIC_CFQ *cfq,334uint32_t pn_space)335{336QUIC_CFQ_ITEM_EX *item = cfq->new_list.head;337338for (; item != NULL && item->pn_space != pn_space; item = item->next);339340if (item == NULL)341return NULL;342343return &item->public;344}345346QUIC_CFQ_ITEM *ossl_quic_cfq_item_get_priority_next(const QUIC_CFQ_ITEM *item,347uint32_t pn_space)348{349QUIC_CFQ_ITEM_EX *ex = (QUIC_CFQ_ITEM_EX *)item;350351if (ex == NULL)352return NULL;353354ex = ex->next;355356for (; ex != NULL && ex->pn_space != pn_space; ex = ex->next);357358if (ex == NULL)359return NULL; /* ubsan */360361return &ex->public;362}363364365