| 1 | /* |
| 2 | * ngtcp2 |
| 3 | * |
| 4 | * Copyright (c) 2023 ngtcp2 contributors |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining |
| 7 | * a copy of this software and associated documentation files (the |
| 8 | * "Software"), to deal in the Software without restriction, including |
| 9 | * without limitation the rights to use, copy, modify, merge, publish, |
| 10 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 11 | * permit persons to whom the Software is furnished to do so, subject to |
| 12 | * the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be |
| 15 | * included in all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 | */ |
| 25 | #ifndef NGTCP2_FRAME_CHAIN_H |
| 26 | #define NGTCP2_FRAME_CHAIN_H |
| 27 | |
| 28 | #ifdef HAVE_CONFIG_H |
| 29 | # include <config.h> |
| 30 | #endif /* defined(HAVE_CONFIG_H) */ |
| 31 | |
| 32 | #include <ngtcp2/ngtcp2.h> |
| 33 | |
| 34 | #include "ngtcp2_pkt.h" |
| 35 | #include "ngtcp2_objalloc.h" |
| 36 | |
| 37 | /* NGTCP2_FRAME_CHAIN_BINDER_FLAG_NONE indicates that no flag is |
| 38 | set. */ |
| 39 | #define NGTCP2_FRAME_CHAIN_BINDER_FLAG_NONE 0x00u |
| 40 | /* NGTCP2_FRAME_CHAIN_BINDER_FLAG_ACK indicates that an information |
| 41 | which a frame carries has been acknowledged. */ |
| 42 | #define NGTCP2_FRAME_CHAIN_BINDER_FLAG_ACK 0x01u |
| 43 | |
| 44 | /* |
| 45 | * ngtcp2_frame_chain_binder binds 2 or more of ngtcp2_frame_chain to |
| 46 | * share the acknowledgement state. In general, all |
| 47 | * ngtcp2_frame_chains bound to the same binder must have the same |
| 48 | * information. |
| 49 | */ |
| 50 | typedef struct ngtcp2_frame_chain_binder { |
| 51 | size_t refcount; |
| 52 | /* flags is bitwise OR of zero or more of |
| 53 | NGTCP2_FRAME_CHAIN_BINDER_FLAG_*. */ |
| 54 | uint32_t flags; |
| 55 | } ngtcp2_frame_chain_binder; |
| 56 | |
| 57 | int ngtcp2_frame_chain_binder_new(ngtcp2_frame_chain_binder **pbinder, |
| 58 | const ngtcp2_mem *mem); |
| 59 | |
| 60 | /* NGTCP2_FRAME_CHAIN_STREAM_DATACNT_THRES is the number of datacnt |
| 61 | that changes allocation method. If datacnt is more than this |
| 62 | value, ngtcp2_frame_chain is allocated without ngtcp2_objalloc. |
| 63 | Otherwise, it is allocated using ngtcp2_objalloc. */ |
| 64 | #define NGTCP2_FRAME_CHAIN_STREAM_DATACNT_THRES 4 |
| 65 | |
| 66 | /* NGTCP2_FRAME_CHAIN_NEW_TOKEN_THRES is the length of a token that |
| 67 | changes allocation method. If the length is more than this value, |
| 68 | ngtcp2_frame_chain is allocated without ngtcp2_objalloc. |
| 69 | Otherwise, it is allocated using ngtcp2_objalloc. */ |
| 70 | #define NGTCP2_FRAME_CHAIN_NEW_TOKEN_THRES \ |
| 71 | (NGTCP2_FRAME_CHAIN_STREAM_DATACNT_THRES * sizeof(ngtcp2_vec)) |
| 72 | |
| 73 | /* NGTCP2_FRAME_CHAIN_FLAG_NONE indicates no flag is set. */ |
| 74 | #define NGTCP2_FRAME_CHAIN_FLAG_NONE 0x0 |
| 75 | /* NGTCP2_FRAME_CHAIN_FLAG_MALLOC indicates that ngtcp2_frame_chain is |
| 76 | allocated by ngtcp2_mem_malloc. */ |
| 77 | #define NGTCP2_FRAME_CHAIN_FLAG_MALLOC 0x1 |
| 78 | |
| 79 | typedef struct ngtcp2_frame_chain ngtcp2_frame_chain; |
| 80 | |
| 81 | /* |
| 82 | * ngtcp2_frame_chain chains frames in a single packet. |
| 83 | */ |
| 84 | struct ngtcp2_frame_chain { |
| 85 | union { |
| 86 | struct { |
| 87 | ngtcp2_frame_chain *next; |
| 88 | ngtcp2_frame_chain_binder *binder; |
| 89 | uint32_t flags; |
| 90 | ngtcp2_frame fr; |
| 91 | uint8_t buf[sizeof(ngtcp2_vec) * NGTCP2_FRAME_CHAIN_STREAM_DATACNT_THRES]; |
| 92 | }; |
| 93 | |
| 94 | ngtcp2_opl_entry oplent; |
| 95 | }; |
| 96 | }; |
| 97 | |
| 98 | ngtcp2_objalloc_decl(frame_chain, ngtcp2_frame_chain, oplent) |
| 99 | |
| 100 | /* |
| 101 | * ngtcp2_bind_frame_chains binds two frame chains |a| and |b| using |
| 102 | * new or existing ngtcp2_frame_chain_binder. |a| might have non-NULL |
| 103 | * a->binder. |b| must not have non-NULL b->binder. |
| 104 | * |
| 105 | * This function returns 0 if it succeeds, or one of the following |
| 106 | * negative error codes: |
| 107 | * |
| 108 | * NGTCP2_ERR_NOMEM |
| 109 | * Out of memory |
| 110 | */ |
| 111 | int ngtcp2_bind_frame_chains(ngtcp2_frame_chain *a, ngtcp2_frame_chain *b, |
| 112 | const ngtcp2_mem *mem); |
| 113 | |
| 114 | /* |
| 115 | * ngtcp2_frame_chain_objalloc_new allocates ngtcp2_frame_chain using |
| 116 | * |objalloc|. |
| 117 | */ |
| 118 | int ngtcp2_frame_chain_objalloc_new(ngtcp2_frame_chain **pfrc, |
| 119 | ngtcp2_objalloc *objalloc); |
| 120 | |
| 121 | /* |
| 122 | * ngtcp2_frame_chain_extralen_new allocates ngtcp2_frame_chain |
| 123 | * followed by |extralen| bytes. |
| 124 | */ |
| 125 | int ngtcp2_frame_chain_extralen_new(ngtcp2_frame_chain **pfrc, size_t extralen, |
| 126 | const ngtcp2_mem *mem); |
| 127 | |
| 128 | /* |
| 129 | * ngtcp2_frame_chain_stream_datacnt_objalloc_new allocates enough |
| 130 | * data to store additional |datacnt| - 1 ngtcp2_vec object after |
| 131 | * ngtcp2_stream object. If no additional space is required, in other |
| 132 | * words, |datacnt| <= NGTCP2_FRAME_CHAIN_STREAM_DATACNT_THRES, |
| 133 | * ngtcp2_frame_chain_objalloc_new is called internally. Otherwise, |
| 134 | * ngtcp2_frame_chain_extralen_new is used and objalloc is not used. |
| 135 | */ |
| 136 | int ngtcp2_frame_chain_stream_datacnt_objalloc_new(ngtcp2_frame_chain **pfrc, |
| 137 | size_t datacnt, |
| 138 | ngtcp2_objalloc *objalloc, |
| 139 | const ngtcp2_mem *mem); |
| 140 | |
| 141 | /* |
| 142 | * ngtcp2_frame_chain_new_token_objalloc_new allocates enough space to |
| 143 | * store the given token. If |tokenlen| <= |
| 144 | * NGTCP2_FRAME_CHAIN_NEW_TOKEN_THRES, ngtcp2_frame_chain_objalloc_new |
| 145 | * is called internally. Otherwise, ngtcp2_frame_chain_extralen_new |
| 146 | * is used, and objalloc is not used. |
| 147 | */ |
| 148 | int ngtcp2_frame_chain_new_token_objalloc_new(ngtcp2_frame_chain **pfrc, |
| 149 | const uint8_t *token, |
| 150 | size_t tokenlen, |
| 151 | ngtcp2_objalloc *objalloc, |
| 152 | const ngtcp2_mem *mem); |
| 153 | |
| 154 | /* |
| 155 | * ngtcp2_frame_chain_objalloc_del adds |frc| to |objalloc| for reuse |
| 156 | * if NGTCP2_FRAME_CHAIN_FLAG_MALLOC is not set in |frc|->flags. |
| 157 | * Otherwise, it deletes |frc|. |
| 158 | */ |
| 159 | void ngtcp2_frame_chain_objalloc_del(ngtcp2_frame_chain *frc, |
| 160 | ngtcp2_objalloc *objalloc, |
| 161 | const ngtcp2_mem *mem); |
| 162 | |
| 163 | /* |
| 164 | * ngtcp2_frame_chain_init initializes |frc|. |flags| is bitwise-OR |
| 165 | * of zero or more of NGTCP2_FRAME_CHAIN_FLAG_*. |
| 166 | */ |
| 167 | void ngtcp2_frame_chain_init(ngtcp2_frame_chain *frc, uint32_t flags); |
| 168 | |
| 169 | /* |
| 170 | * ngtcp2_frame_chain_list_objalloc_del adds all ngtcp2_frame_chain |
| 171 | * linked from |frc| to |objalloc| for reuse. Depending on the frame type |
| 172 | * and its size, ngtcp2_frame_chain might be deleted instead. |
| 173 | */ |
| 174 | void ngtcp2_frame_chain_list_objalloc_del(ngtcp2_frame_chain *frc, |
| 175 | ngtcp2_objalloc *objalloc, |
| 176 | const ngtcp2_mem *mem); |
| 177 | |
| 178 | #endif /* !defined(NGTCP2_FRAME_CHAIN_H) */ |