1 1.1 christos /* 2 1.1 christos * Copyright 2022-2024 The OpenSSL Project Authors. All Rights Reserved. 3 1.1 christos * 4 1.1 christos * Licensed under the Apache License 2.0 (the "License"). You may not use 5 1.1 christos * this file except in compliance with the License. You can obtain a copy 6 1.1 christos * in the file LICENSE in the source distribution or at 7 1.1 christos * https://www.openssl.org/source/license.html 8 1.1 christos */ 9 1.1 christos 10 1.1 christos #ifndef OSSL_QUIC_FC_H 11 1.1.1.2 christos #define OSSL_QUIC_FC_H 12 1.1 christos 13 1.1.1.2 christos #include <openssl/ssl.h> 14 1.1.1.2 christos #include "internal/time.h" 15 1.1 christos 16 1.1.1.2 christos #ifndef OPENSSL_NO_QUIC 17 1.1 christos 18 1.1 christos /* 19 1.1 christos * TX Flow Controller (TXFC) 20 1.1 christos * ========================= 21 1.1 christos * 22 1.1 christos * For discussion, see doc/designs/quic-design/quic-fc.md. 23 1.1 christos */ 24 1.1 christos typedef struct quic_txfc_st QUIC_TXFC; 25 1.1 christos 26 1.1 christos struct quic_txfc_st { 27 1.1.1.2 christos QUIC_TXFC *parent; /* stream-level iff non-NULL */ 28 1.1.1.2 christos uint64_t swm, cwm; 29 1.1.1.2 christos char has_become_blocked; 30 1.1 christos }; 31 1.1 christos 32 1.1 christos /* 33 1.1 christos * Initialises a TX flow controller. conn_txfc should be non-NULL and point to 34 1.1 christos * the connection-level flow controller if the TXFC is for stream-level flow 35 1.1 christos * control, and NULL otherwise. 36 1.1 christos */ 37 1.1 christos int ossl_quic_txfc_init(QUIC_TXFC *txfc, QUIC_TXFC *conn_txfc); 38 1.1 christos 39 1.1 christos /* 40 1.1 christos * Gets the parent (i.e., connection-level) TX flow controller. Returns NULL if 41 1.1 christos * called on a connection-level TX flow controller. 42 1.1 christos */ 43 1.1 christos QUIC_TXFC *ossl_quic_txfc_get_parent(QUIC_TXFC *txfc); 44 1.1 christos 45 1.1 christos /* 46 1.1 christos * Bump the credit watermark (CWM) value. This is the 'On TX Window Updated' 47 1.1 christos * operation. This function is a no-op if it has already been called with an 48 1.1 christos * equal or higher CWM value. 49 1.1 christos * 50 1.1 christos * It returns 1 iff the call resulted in the CWM being bumped and 0 if it was 51 1.1 christos * not increased because it has already been called with an equal or higher CWM 52 1.1 christos * value. This is not an error per se but may indicate a local programming error 53 1.1 christos * or a protocol error in a remote peer. 54 1.1 christos */ 55 1.1 christos int ossl_quic_txfc_bump_cwm(QUIC_TXFC *txfc, uint64_t cwm); 56 1.1 christos 57 1.1 christos /* 58 1.1 christos * Get the number of bytes by which we are in credit. This is the number of 59 1.1 christos * controlled bytes we are allowed to send. (Thus if this function returns 0, we 60 1.1 christos * are currently blocked.) 61 1.1 christos * 62 1.1 christos * If called on a stream-level TXFC, ossl_quic_txfc_get_credit is called on 63 1.1 christos * the connection-level TXFC as well, and the lesser of the two values is 64 1.1 christos * returned. The consumed value is the amount already consumed on the connection 65 1.1 christos * level TXFC. 66 1.1 christos */ 67 1.1 christos uint64_t ossl_quic_txfc_get_credit(QUIC_TXFC *txfc, uint64_t consumed); 68 1.1 christos 69 1.1 christos /* 70 1.1 christos * Like ossl_quic_txfc_get_credit(), but when called on a stream-level TXFC, 71 1.1 christos * retrieves only the stream-level credit value and does not clamp it based on 72 1.1 christos * connection-level flow control. Any credit value is reduced by the consumed 73 1.1 christos * amount. 74 1.1 christos */ 75 1.1 christos uint64_t ossl_quic_txfc_get_credit_local(QUIC_TXFC *txfc, uint64_t consumed); 76 1.1 christos 77 1.1 christos /* 78 1.1 christos * Consume num_bytes of credit. This is the 'On TX' operation. This should be 79 1.1 christos * called when we transmit any controlled bytes. Calling this with an argument 80 1.1 christos * of 0 is a no-op. 81 1.1 christos * 82 1.1 christos * We must never transmit more controlled bytes than we are in credit for (see 83 1.1 christos * the return value of ossl_quic_txfc_get_credit()). If you call this function 84 1.1 christos * with num_bytes greater than our current credit, this function consumes the 85 1.1 christos * remainder of the credit and returns 0. This indicates a serious programming 86 1.1 christos * error on the caller's part. Otherwise, the function returns 1. 87 1.1 christos * 88 1.1 christos * If called on a stream-level TXFC, ossl_quic_txfc_consume_credit() is called 89 1.1 christos * on the connection-level TXFC also. If the call to that function on the 90 1.1 christos * connection-level TXFC returns zero, this function will also return zero. 91 1.1 christos */ 92 1.1 christos int ossl_quic_txfc_consume_credit(QUIC_TXFC *txfc, uint64_t num_bytes); 93 1.1 christos 94 1.1 christos /* 95 1.1 christos * Like ossl_quic_txfc_consume_credit(), but when called on a stream-level TXFC, 96 1.1 christos * consumes only from the stream-level credit and does not inform the 97 1.1 christos * connection-level TXFC. 98 1.1 christos */ 99 1.1 christos int ossl_quic_txfc_consume_credit_local(QUIC_TXFC *txfc, uint64_t num_bytes); 100 1.1 christos 101 1.1 christos /* 102 1.1 christos * This flag is provided for convenience. A caller is not required to use it. It 103 1.1 christos * is a boolean flag set whenever our credit drops to zero. If clear is 1, the 104 1.1 christos * flag is cleared. The old value of the flag is returned. Callers may use this 105 1.1 christos * to determine if they need to send a DATA_BLOCKED or STREAM_DATA_BLOCKED 106 1.1 christos * frame, which should contain the value returned by ossl_quic_txfc_get_cwm(). 107 1.1 christos */ 108 1.1 christos int ossl_quic_txfc_has_become_blocked(QUIC_TXFC *txfc, int clear); 109 1.1 christos 110 1.1 christos /* 111 1.1 christos * Get the current CWM value. This is mainly only needed when generating a 112 1.1 christos * DATA_BLOCKED or STREAM_DATA_BLOCKED frame, or for diagnostic purposes. 113 1.1 christos */ 114 1.1 christos uint64_t ossl_quic_txfc_get_cwm(QUIC_TXFC *txfc); 115 1.1 christos 116 1.1 christos /* 117 1.1 christos * Get the current spent watermark (SWM) value. This is purely for diagnostic 118 1.1 christos * use and should not be needed in normal circumstances. 119 1.1 christos */ 120 1.1 christos uint64_t ossl_quic_txfc_get_swm(QUIC_TXFC *txfc); 121 1.1 christos 122 1.1 christos /* 123 1.1 christos * RX Flow Controller (RXFC) 124 1.1 christos * ========================= 125 1.1 christos */ 126 1.1 christos typedef struct quic_rxfc_st QUIC_RXFC; 127 1.1 christos 128 1.1 christos struct quic_rxfc_st { 129 1.1 christos /* 130 1.1 christos * swm is the sent/received watermark, which tracks how much we have 131 1.1 christos * received from the peer. rwm is the retired watermark, which tracks how 132 1.1 christos * much has been passed to the application. esrwm is the rwm value at which 133 1.1 christos * the current auto-tuning epoch started. hwm is the highest stream length 134 1.1 christos * (STREAM frame offset + payload length) we have seen from a STREAM frame 135 1.1 christos * yet. 136 1.1 christos */ 137 1.1.1.2 christos uint64_t cwm, swm, rwm, esrwm, hwm, cur_window_size, max_window_size; 138 1.1.1.2 christos OSSL_TIME epoch_start; 139 1.1.1.2 christos OSSL_TIME (*now)(void *arg); 140 1.1.1.2 christos void *now_arg; 141 1.1.1.2 christos QUIC_RXFC *parent; 142 1.1.1.2 christos unsigned char error_code, has_cwm_changed, is_fin, standalone; 143 1.1 christos }; 144 1.1 christos 145 1.1 christos /* 146 1.1 christos * Initialises an RX flow controller. conn_rxfc should be non-NULL and point to 147 1.1 christos * a connection-level RXFC if the RXFC is for stream-level flow control, and 148 1.1 christos * NULL otherwise. initial_window_size and max_window_size specify the initial 149 1.1 christos * and absolute maximum window sizes, respectively. Window size values are 150 1.1 christos * expressed in bytes and determine how much credit the RXFC extends to the peer 151 1.1 christos * to transmit more data at a time. 152 1.1 christos */ 153 1.1 christos int ossl_quic_rxfc_init(QUIC_RXFC *rxfc, QUIC_RXFC *conn_rxfc, 154 1.1.1.2 christos uint64_t initial_window_size, 155 1.1.1.2 christos uint64_t max_window_size, 156 1.1.1.2 christos OSSL_TIME (*now)(void *arg), 157 1.1.1.2 christos void *now_arg); 158 1.1 christos 159 1.1 christos /* 160 1.1 christos * Initialises an RX flow controller which is used by itself and not under a 161 1.1 christos * connection-level RX flow controller. This can be used for stream count 162 1.1 christos * enforcement as well as CRYPTO buffer enforcement. 163 1.1 christos */ 164 1.1 christos int ossl_quic_rxfc_init_standalone(QUIC_RXFC *rxfc, 165 1.1.1.2 christos uint64_t initial_window_size, 166 1.1.1.2 christos OSSL_TIME (*now)(void *arg), 167 1.1.1.2 christos void *now_arg); 168 1.1 christos 169 1.1 christos /* 170 1.1 christos * Gets the parent (i.e., connection-level) RXFC. Returns NULL if called on a 171 1.1 christos * connection-level RXFC. 172 1.1 christos */ 173 1.1 christos QUIC_RXFC *ossl_quic_rxfc_get_parent(QUIC_RXFC *rxfc); 174 1.1 christos 175 1.1 christos /* 176 1.1 christos * Changes the current maximum window size value. 177 1.1 christos */ 178 1.1 christos void ossl_quic_rxfc_set_max_window_size(QUIC_RXFC *rxfc, 179 1.1.1.2 christos size_t max_window_size); 180 1.1 christos 181 1.1 christos /* 182 1.1 christos * To be called whenever a STREAM frame is received. 183 1.1 christos * 184 1.1 christos * end is the value (offset + len), where offset is the offset field of the 185 1.1 christos * STREAM frame and len is the length of the STREAM frame's payload in bytes. 186 1.1 christos * 187 1.1 christos * is_fin should be 1 if the STREAM frame had the FIN flag set and 0 otherwise. 188 1.1 christos * 189 1.1 christos * This function may be used on a stream-level RXFC only. The connection-level 190 1.1 christos * RXFC will have its state updated by the stream-level RXFC. 191 1.1 christos * 192 1.1 christos * You should check ossl_quic_rxfc_has_error() on both connection-level and 193 1.1 christos * stream-level RXFCs after calling this function, as an incoming STREAM frame 194 1.1 christos * may cause flow control limits to be exceeded by an errant peer. This 195 1.1 christos * function still returns 1 in this case, as this is not a caller error. 196 1.1 christos * 197 1.1 christos * Returns 1 on success or 0 on failure. 198 1.1 christos */ 199 1.1 christos int ossl_quic_rxfc_on_rx_stream_frame(QUIC_RXFC *rxfc, 200 1.1.1.2 christos uint64_t end, int is_fin); 201 1.1 christos 202 1.1 christos /* 203 1.1 christos * To be called whenever controlled bytes are retired, i.e. when bytes are 204 1.1 christos * dequeued from a QUIC stream and passed to the application. num_bytes 205 1.1 christos * is the number of bytes which were passed to the application. 206 1.1 christos * 207 1.1 christos * You should call this only on a stream-level RXFC. This function will update 208 1.1 christos * the connection-level RXFC automatically. 209 1.1 christos * 210 1.1 christos * rtt should be the current best understanding of the RTT to the peer, as 211 1.1 christos * offered by the Statistics Manager. 212 1.1 christos * 213 1.1 christos * You should check ossl_quic_rxfc_has_cwm_changed() after calling this 214 1.1 christos * function, as it may have caused the RXFC to decide to grant more flow control 215 1.1 christos * credit to the peer. 216 1.1 christos * 217 1.1 christos * Returns 1 on success and 0 on failure. 218 1.1 christos */ 219 1.1 christos int ossl_quic_rxfc_on_retire(QUIC_RXFC *rxfc, 220 1.1.1.2 christos uint64_t num_bytes, 221 1.1.1.2 christos OSSL_TIME rtt); 222 1.1 christos 223 1.1 christos /* 224 1.1 christos * Returns the current CWM which the RXFC thinks the peer should have. 225 1.1 christos * 226 1.1 christos * Note that the RXFC will increase this value in response to events, at which 227 1.1 christos * time a MAX_DATA or MAX_STREAM_DATA frame must be generated. Use 228 1.1 christos * ossl_quic_rxfc_has_cwm_changed() to detect this condition. 229 1.1 christos * 230 1.1 christos * This value increases monotonically. 231 1.1 christos */ 232 1.1 christos uint64_t ossl_quic_rxfc_get_cwm(const QUIC_RXFC *rxfc); 233 1.1 christos 234 1.1 christos /* 235 1.1 christos * Returns the current SWM. This is the total number of bytes the peer has 236 1.1 christos * transmitted to us. This is intended for diagnostic use only; you should 237 1.1 christos * not need it. 238 1.1 christos */ 239 1.1 christos uint64_t ossl_quic_rxfc_get_swm(const QUIC_RXFC *rxfc); 240 1.1 christos 241 1.1 christos /* 242 1.1 christos * Returns the current RWM. This is the total number of bytes that has been 243 1.1 christos * retired. This is intended for diagnostic use only; you should not need it. 244 1.1 christos */ 245 1.1 christos uint64_t ossl_quic_rxfc_get_rwm(const QUIC_RXFC *rxfc); 246 1.1 christos 247 1.1 christos /* 248 1.1 christos * Returns the current credit. This is the CWM minus the SWM. This is intended 249 1.1 christos * for diagnostic use only; you should not need it. 250 1.1 christos */ 251 1.1 christos uint64_t ossl_quic_rxfc_get_credit(const QUIC_RXFC *rxfc); 252 1.1 christos 253 1.1 christos /* 254 1.1 christos * Returns the CWM changed flag. If clear is 1, the flag is cleared and the old 255 1.1 christos * value is returned. 256 1.1 christos */ 257 1.1 christos int ossl_quic_rxfc_has_cwm_changed(QUIC_RXFC *rxfc, int clear); 258 1.1 christos 259 1.1 christos /* 260 1.1 christos * Returns a QUIC_ERR_* error code if a flow control error has been detected. 261 1.1 christos * Otherwise, returns QUIC_ERR_NO_ERROR. If clear is 1, the error is cleared 262 1.1 christos * and the old value is returned. 263 1.1 christos * 264 1.1 christos * May return one of the following values: 265 1.1 christos * 266 1.1 christos * QUIC_ERR_FLOW_CONTROL_ERROR: 267 1.1 christos * This indicates a flow control protocol violation by the remote peer; the 268 1.1 christos * connection should be terminated in this event. 269 1.1 christos * QUIC_ERR_FINAL_SIZE: 270 1.1 christos * The peer attempted to change the stream length after ending the stream. 271 1.1 christos */ 272 1.1 christos int ossl_quic_rxfc_get_error(QUIC_RXFC *rxfc, int clear); 273 1.1 christos 274 1.1 christos /* 275 1.1 christos * Returns 1 if the RXFC is a stream-level RXFC and the RXFC knows the final 276 1.1 christos * size for the stream in bytes. If this is the case and final_size is non-NULL, 277 1.1 christos * writes the final size to *final_size. Otherwise, returns 0. 278 1.1 christos */ 279 1.1 christos int ossl_quic_rxfc_get_final_size(const QUIC_RXFC *rxfc, uint64_t *final_size); 280 1.1 christos 281 1.1.1.2 christos #endif 282 1.1 christos 283 1.1 christos #endif 284