| 1 | /* |
| 2 | * ngtcp2 |
| 3 | * |
| 4 | * Copyright (c) 2017 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_STRM_H |
| 26 | #define NGTCP2_STRM_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_rob.h" |
| 35 | #include "ngtcp2_map.h" |
| 36 | #include "ngtcp2_gaptr.h" |
| 37 | #include "ngtcp2_ksl.h" |
| 38 | #include "ngtcp2_pq.h" |
| 39 | #include "ngtcp2_pkt.h" |
| 40 | |
| 41 | typedef struct ngtcp2_frame_chain ngtcp2_frame_chain; |
| 42 | |
| 43 | /* NGTCP2_STRM_FLAG_NONE indicates that no flag is set. */ |
| 44 | #define NGTCP2_STRM_FLAG_NONE 0x00u |
| 45 | /* NGTCP2_STRM_FLAG_SHUT_RD indicates that further reception of stream |
| 46 | data is not allowed. */ |
| 47 | #define NGTCP2_STRM_FLAG_SHUT_RD 0x01u |
| 48 | /* NGTCP2_STRM_FLAG_SHUT_WR indicates that further transmission of |
| 49 | stream data is not allowed. */ |
| 50 | #define NGTCP2_STRM_FLAG_SHUT_WR 0x02u |
| 51 | #define NGTCP2_STRM_FLAG_SHUT_RDWR \ |
| 52 | (NGTCP2_STRM_FLAG_SHUT_RD | NGTCP2_STRM_FLAG_SHUT_WR) |
| 53 | /* NGTCP2_STRM_FLAG_RESET_STREAM indicates that RESET_STREAM is sent |
| 54 | from the local endpoint. In this case, NGTCP2_STRM_FLAG_SHUT_WR is |
| 55 | also set. */ |
| 56 | #define NGTCP2_STRM_FLAG_RESET_STREAM 0x04u |
| 57 | /* NGTCP2_STRM_FLAG_RESET_STREAM_RECVED indicates that RESET_STREAM is |
| 58 | received from the remote endpoint. In this case, |
| 59 | NGTCP2_STRM_FLAG_SHUT_RD is also set. */ |
| 60 | #define NGTCP2_STRM_FLAG_RESET_STREAM_RECVED 0x08u |
| 61 | /* NGTCP2_STRM_FLAG_STOP_SENDING indicates that STOP_SENDING is sent |
| 62 | from the local endpoint. */ |
| 63 | #define NGTCP2_STRM_FLAG_STOP_SENDING 0x10u |
| 64 | /* NGTCP2_STRM_FLAG_RESET_STREAM_ACKED indicates that the outgoing |
| 65 | RESET_STREAM is acknowledged by peer. */ |
| 66 | #define NGTCP2_STRM_FLAG_RESET_STREAM_ACKED 0x20u |
| 67 | /* NGTCP2_STRM_FLAG_FIN_ACKED indicates that a STREAM with FIN bit set |
| 68 | is acknowledged by a remote endpoint. */ |
| 69 | #define NGTCP2_STRM_FLAG_FIN_ACKED 0x40u |
| 70 | /* NGTCP2_STRM_FLAG_ANY_ACKED indicates that any portion of stream |
| 71 | data, including 0 length segment, is acknowledged. */ |
| 72 | #define NGTCP2_STRM_FLAG_ANY_ACKED 0x80u |
| 73 | /* NGTCP2_STRM_FLAG_APP_ERROR_CODE_SET indicates that app_error_code |
| 74 | field is set. This resolves the ambiguity that the initial |
| 75 | app_error_code value 0 might be a proper application error code. |
| 76 | In this case, without this flag, we are unable to distinguish |
| 77 | assigned value from unassigned one. */ |
| 78 | #define NGTCP2_STRM_FLAG_APP_ERROR_CODE_SET 0x100u |
| 79 | /* NGTCP2_STRM_FLAG_SEND_STOP_SENDING is set when STOP_SENDING frame |
| 80 | should be sent. */ |
| 81 | #define NGTCP2_STRM_FLAG_SEND_STOP_SENDING 0x200u |
| 82 | /* NGTCP2_STRM_FLAG_SEND_RESET_STREAM is set when RESET_STREAM frame |
| 83 | should be sent. */ |
| 84 | #define NGTCP2_STRM_FLAG_SEND_RESET_STREAM 0x400u |
| 85 | /* NGTCP2_STRM_FLAG_STOP_SENDING_RECVED indicates that STOP_SENDING is |
| 86 | received from the remote endpoint. In this case, |
| 87 | NGTCP2_STRM_FLAG_SHUT_WR is also set. */ |
| 88 | #define NGTCP2_STRM_FLAG_STOP_SENDING_RECVED 0x800u |
| 89 | /* NGTCP2_STRM_FLAG_ANY_SENT indicates that any STREAM frame, |
| 90 | including empty one, has been sent. */ |
| 91 | #define NGTCP2_STRM_FLAG_ANY_SENT 0x1000u |
| 92 | |
| 93 | typedef struct ngtcp2_strm ngtcp2_strm; |
| 94 | |
| 95 | struct ngtcp2_strm { |
| 96 | union { |
| 97 | struct { |
| 98 | ngtcp2_pq_entry pe; |
| 99 | uint64_t cycle; |
| 100 | ngtcp2_objalloc *frc_objalloc; |
| 101 | |
| 102 | struct { |
| 103 | /* acked_offset tracks acknowledged outgoing data. */ |
| 104 | ngtcp2_gaptr *acked_offset; |
| 105 | /* cont_acked_offset is the offset that all data up to this offset |
| 106 | is acknowledged by a remote endpoint. It is used until the |
| 107 | remote endpoint acknowledges data in out-of-order. After that, |
| 108 | acked_offset is used instead. */ |
| 109 | uint64_t cont_acked_offset; |
| 110 | /* streamfrq contains STREAM or CRYPTO frame for |
| 111 | retransmission. The flow control credits have already been |
| 112 | paid when they are transmitted first time. There are no |
| 113 | restriction regarding flow control for retransmission. */ |
| 114 | ngtcp2_ksl *streamfrq; |
| 115 | /* offset is the next offset of new outgoing data. In other |
| 116 | words, it is the number of bytes sent in this stream |
| 117 | without duplication. */ |
| 118 | uint64_t offset; |
| 119 | /* max_tx_offset is the maximum offset that local endpoint can |
| 120 | send for this stream. */ |
| 121 | uint64_t max_offset; |
| 122 | /* last_blocked_offset is the largest offset where the |
| 123 | transmission of stream data is blocked. */ |
| 124 | uint64_t last_blocked_offset; |
| 125 | /* last_max_stream_data_ts is the timestamp when last |
| 126 | MAX_STREAM_DATA frame is sent. */ |
| 127 | ngtcp2_tstamp last_max_stream_data_ts; |
| 128 | /* loss_count is the number of packets that contain STREAM |
| 129 | frame for this stream and are declared to be lost. It may |
| 130 | include the spurious losses. It does not include a packet |
| 131 | whose contents have been reclaimed for PTO and which is |
| 132 | later declared to be lost. Those data are not blocked by |
| 133 | the flow control and will be sent immediately if no other |
| 134 | restrictions are applied. */ |
| 135 | size_t loss_count; |
| 136 | /* last_lost_pkt_num is the packet number of the packet that |
| 137 | is counted to loss_count. It is used to avoid to count |
| 138 | multiple STREAM frames in one lost packet. */ |
| 139 | int64_t last_lost_pkt_num; |
| 140 | /* stop_sending_app_error_code is the application specific |
| 141 | error code that is sent along with STOP_SENDING. */ |
| 142 | uint64_t stop_sending_app_error_code; |
| 143 | /* reset_stream_app_error_code is the application specific |
| 144 | error code that is sent along with RESET_STREAM. */ |
| 145 | uint64_t reset_stream_app_error_code; |
| 146 | } tx; |
| 147 | |
| 148 | struct { |
| 149 | /* rob is the reorder buffer for incoming stream data. The data |
| 150 | received in out of order is buffered and sorted by its offset |
| 151 | in this object. */ |
| 152 | ngtcp2_rob *rob; |
| 153 | /* cont_offset is the largest offset of consecutive data. It is |
| 154 | used until the endpoint receives out-of-order data. After |
| 155 | that, rob is used to track the offset and data. */ |
| 156 | uint64_t cont_offset; |
| 157 | /* last_offset is the largest offset of stream data received for |
| 158 | this stream. */ |
| 159 | uint64_t last_offset; |
| 160 | /* max_offset is the maximum offset that remote endpoint can send |
| 161 | to this stream. */ |
| 162 | uint64_t max_offset; |
| 163 | /* unsent_max_offset is the maximum offset that remote endpoint |
| 164 | can send to this stream, and it is not notified to the remote |
| 165 | endpoint. unsent_max_offset >= max_offset must be hold. */ |
| 166 | uint64_t unsent_max_offset; |
| 167 | /* window is the stream-level flow control window size. */ |
| 168 | uint64_t window; |
| 169 | } rx; |
| 170 | |
| 171 | const ngtcp2_mem *mem; |
| 172 | int64_t stream_id; |
| 173 | void *stream_user_data; |
| 174 | /* app_error_code is an error code the local endpoint sent in |
| 175 | RESET_STREAM or STOP_SENDING, or received from a remote endpoint |
| 176 | in RESET_STREAM or STOP_SENDING. First application error code is |
| 177 | chosen and when set, NGTCP2_STRM_FLAG_APP_ERROR_CODE_SET flag is |
| 178 | set in flags field. */ |
| 179 | uint64_t app_error_code; |
| 180 | /* flags is bit-wise OR of zero or more of NGTCP2_STRM_FLAG_*. */ |
| 181 | uint32_t flags; |
| 182 | }; |
| 183 | |
| 184 | ngtcp2_opl_entry oplent; |
| 185 | }; |
| 186 | }; |
| 187 | |
| 188 | /* |
| 189 | * ngtcp2_strm_init initializes |strm|. |
| 190 | */ |
| 191 | void ngtcp2_strm_init(ngtcp2_strm *strm, int64_t stream_id, uint32_t flags, |
| 192 | uint64_t max_rx_offset, uint64_t max_tx_offset, |
| 193 | void *stream_user_data, ngtcp2_objalloc *frc_objalloc, |
| 194 | const ngtcp2_mem *mem); |
| 195 | |
| 196 | /* |
| 197 | * ngtcp2_strm_free deallocates memory allocated for |strm|. This |
| 198 | * function does not free the memory pointed by |strm| itself. |
| 199 | */ |
| 200 | void ngtcp2_strm_free(ngtcp2_strm *strm); |
| 201 | |
| 202 | /* |
| 203 | * ngtcp2_strm_rx_offset returns the minimum offset of stream data |
| 204 | * which is not received yet. |
| 205 | */ |
| 206 | uint64_t ngtcp2_strm_rx_offset(const ngtcp2_strm *strm); |
| 207 | |
| 208 | /* |
| 209 | * ngtcp2_strm_recv_reordering handles reordered data. |
| 210 | * |
| 211 | * It returns the number of bytes newly buffered if it succeeds, or |
| 212 | * one of the following negative error codes: |
| 213 | * |
| 214 | * NGTCP2_ERR_NOMEM |
| 215 | * Out of memory |
| 216 | */ |
| 217 | ngtcp2_ssize ngtcp2_strm_recv_reordering(ngtcp2_strm *strm, const uint8_t *data, |
| 218 | size_t datalen, uint64_t offset); |
| 219 | |
| 220 | /* |
| 221 | * ngtcp2_strm_update_rx_offset tells that data up to |offset| bytes |
| 222 | * are received in order. |
| 223 | */ |
| 224 | void ngtcp2_strm_update_rx_offset(ngtcp2_strm *strm, uint64_t offset); |
| 225 | |
| 226 | /* |
| 227 | * ngtcp2_strm_discard_reordered_data discards all buffered reordered |
| 228 | * data. |
| 229 | */ |
| 230 | void ngtcp2_strm_discard_reordered_data(ngtcp2_strm *strm); |
| 231 | |
| 232 | /* |
| 233 | * ngtcp2_strm_shutdown shutdowns |strm|. |flags| should be one of |
| 234 | * NGTCP2_STRM_FLAG_SHUT_RD, NGTCP2_STRM_FLAG_SHUT_WR, and |
| 235 | * NGTCP2_STRM_FLAG_SHUT_RDWR. |
| 236 | */ |
| 237 | void ngtcp2_strm_shutdown(ngtcp2_strm *strm, uint32_t flags); |
| 238 | |
| 239 | /* |
| 240 | * ngtcp2_strm_streamfrq_push pushes |frc| to strm->tx.streamfrq for |
| 241 | * retransmission. |
| 242 | * |
| 243 | * This function returns 0 if it succeeds, or one of the following |
| 244 | * negative error codes: |
| 245 | * |
| 246 | * NGTCP2_ERR_NOMEM |
| 247 | * Out of memory |
| 248 | */ |
| 249 | int ngtcp2_strm_streamfrq_push(ngtcp2_strm *strm, ngtcp2_frame_chain *frc); |
| 250 | |
| 251 | /* |
| 252 | * ngtcp2_strm_streamfrq_pop assigns a ngtcp2_frame_chain that only |
| 253 | * contains unacknowledged stream data with smallest offset to |*pfrc| |
| 254 | * for retransmission. The assigned ngtcp2_frame_chain has stream |
| 255 | * data at most |left| bytes. strm->tx.streamfrq is adjusted to |
| 256 | * exclude the portion of data included in it. If there is no stream |
| 257 | * data to send, this function returns 0 and |*pfrc| is NULL. |
| 258 | * |
| 259 | * This function returns 0 if it succeeds, or one of the following |
| 260 | * negative error codes: |
| 261 | * |
| 262 | * NGTCP2_ERR_NOMEM |
| 263 | * Out of memory |
| 264 | */ |
| 265 | int ngtcp2_strm_streamfrq_pop(ngtcp2_strm *strm, ngtcp2_frame_chain **pfrc, |
| 266 | size_t left); |
| 267 | |
| 268 | /* |
| 269 | * ngtcp2_strm_streamfrq_unacked_offset returns the smallest offset of |
| 270 | * unacknowledged stream data held in strm->tx.streamfrq. |
| 271 | */ |
| 272 | uint64_t ngtcp2_strm_streamfrq_unacked_offset(const ngtcp2_strm *strm); |
| 273 | |
| 274 | /* |
| 275 | * ngtcp2_strm_streamfrq_top returns the first ngtcp2_frame_chain. |
| 276 | * The queue must not be empty. |
| 277 | */ |
| 278 | ngtcp2_frame_chain *ngtcp2_strm_streamfrq_top(const ngtcp2_strm *strm); |
| 279 | |
| 280 | /* |
| 281 | * ngtcp2_strm_streamfrq_empty returns nonzero if streamfrq is empty. |
| 282 | */ |
| 283 | int ngtcp2_strm_streamfrq_empty(const ngtcp2_strm *strm); |
| 284 | |
| 285 | /* |
| 286 | * ngtcp2_strm_streamfrq_clear removes all frames from streamfrq. |
| 287 | */ |
| 288 | void ngtcp2_strm_streamfrq_clear(ngtcp2_strm *strm); |
| 289 | |
| 290 | /* |
| 291 | * ngtcp2_strm_is_tx_queued returns nonzero if |strm| is queued. |
| 292 | */ |
| 293 | int ngtcp2_strm_is_tx_queued(const ngtcp2_strm *strm); |
| 294 | |
| 295 | /* |
| 296 | * ngtcp2_strm_is_all_tx_data_acked returns nonzero if all outgoing |
| 297 | * data for |strm| which have sent so far have been acknowledged. |
| 298 | */ |
| 299 | int ngtcp2_strm_is_all_tx_data_acked(const ngtcp2_strm *strm); |
| 300 | |
| 301 | /* |
| 302 | * ngtcp2_strm_is_all_tx_data_fin_acked behaves like |
| 303 | * ngtcp2_strm_is_all_tx_data_acked, but it also requires that STREAM |
| 304 | * frame with fin bit set is acknowledged. |
| 305 | */ |
| 306 | int ngtcp2_strm_is_all_tx_data_fin_acked(const ngtcp2_strm *strm); |
| 307 | |
| 308 | /* |
| 309 | * ngtcp2_strm_get_unacked_range_after returns the range that is not |
| 310 | * acknowledged yet and includes or comes after |offset|. |
| 311 | */ |
| 312 | ngtcp2_range ngtcp2_strm_get_unacked_range_after(const ngtcp2_strm *strm, |
| 313 | uint64_t offset); |
| 314 | |
| 315 | /* |
| 316 | * ngtcp2_strm_get_acked_offset returns offset, that is the data up to |
| 317 | * this offset have been acknowledged by a remote endpoint. It |
| 318 | * returns 0 if no data is acknowledged. |
| 319 | */ |
| 320 | uint64_t ngtcp2_strm_get_acked_offset(const ngtcp2_strm *strm); |
| 321 | |
| 322 | /* |
| 323 | * ngtcp2_strm_ack_data tells |strm| that the data [|offset|, |offset| |
| 324 | * + |len|) is acknowledged by a remote endpoint. |
| 325 | */ |
| 326 | int ngtcp2_strm_ack_data(ngtcp2_strm *strm, uint64_t offset, uint64_t len); |
| 327 | |
| 328 | /* |
| 329 | * ngtcp2_strm_set_app_error_code sets |app_error_code| to |strm| and |
| 330 | * set NGTCP2_STRM_FLAG_APP_ERROR_CODE_SET flag. If the flag is |
| 331 | * already set, this function does nothing. |
| 332 | */ |
| 333 | void ngtcp2_strm_set_app_error_code(ngtcp2_strm *strm, uint64_t app_error_code); |
| 334 | |
| 335 | /* |
| 336 | * ngtcp2_strm_require_retransmit_reset_stream returns nonzero if |
| 337 | * RESET_STREAM frame should be retransmitted. |
| 338 | */ |
| 339 | int ngtcp2_strm_require_retransmit_reset_stream(const ngtcp2_strm *strm); |
| 340 | |
| 341 | /* |
| 342 | * ngtcp2_strm_require_retransmit_stop_sending returns nonzero if |
| 343 | * STOP_SENDING frame should be retransmitted. |
| 344 | */ |
| 345 | int ngtcp2_strm_require_retransmit_stop_sending(const ngtcp2_strm *strm); |
| 346 | |
| 347 | /* |
| 348 | * ngtcp2_strm_require_retransmit_max_stream_data returns nonzero if |
| 349 | * MAX_STREAM_DATA frame should be retransmitted. |
| 350 | */ |
| 351 | int ngtcp2_strm_require_retransmit_max_stream_data( |
| 352 | const ngtcp2_strm *strm, const ngtcp2_max_stream_data *fr); |
| 353 | |
| 354 | /* |
| 355 | * ngtcp2_strm_require_retransmit_stream_data_blocked returns nonzero |
| 356 | * if STREAM_DATA_BLOCKED frame frame should be retransmitted. |
| 357 | */ |
| 358 | int ngtcp2_strm_require_retransmit_stream_data_blocked( |
| 359 | const ngtcp2_strm *strm, const ngtcp2_stream_data_blocked *fr); |
| 360 | |
| 361 | #endif /* !defined(NGTCP2_STRM_H) */ |