quic_stream.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef OSSL_INTERNAL_QUIC_STREAM_H
  10. # define OSSL_INTERNAL_QUIC_STREAM_H
  11. # pragma once
  12. #include "internal/e_os.h"
  13. #include "internal/time.h"
  14. #include "internal/quic_types.h"
  15. #include "internal/quic_predef.h"
  16. #include "internal/quic_wire.h"
  17. #include "internal/quic_record_tx.h"
  18. #include "internal/quic_record_rx.h"
  19. #include "internal/quic_fc.h"
  20. #include "internal/quic_statm.h"
  21. # ifndef OPENSSL_NO_QUIC
  22. /*
  23. * QUIC Send Stream
  24. * ================
  25. *
  26. * The QUIC Send Stream Manager (QUIC_SSTREAM) is responsible for:
  27. *
  28. * - accepting octet strings of stream data;
  29. *
  30. * - generating corresponding STREAM frames;
  31. *
  32. * - receiving notifications of lost frames, in order to generate new STREAM
  33. * frames for the lost data;
  34. *
  35. * - receiving notifications of acknowledged frames, in order to internally
  36. * reuse memory used to store acknowledged stream data;
  37. *
  38. * - informing the caller of how much more stream data it can accept into
  39. * its internal buffers, so as to ensure that the amount of unacknowledged
  40. * data which can be written to a stream is not infinite and to allow the
  41. * caller to manifest backpressure conditions to the user.
  42. *
  43. * The QUIC_SSTREAM is instantiated once for every stream with a send component
  44. * (i.e., for a unidirectional send stream or for the send component of a
  45. * bidirectional stream).
  46. *
  47. * Note: The terms 'TX' and 'RX' are used when referring to frames, packets and
  48. * datagrams. The terms 'send' and 'receive' are used when referring to the
  49. * stream abstraction. Applications send; we transmit.
  50. */
  51. /*
  52. * Instantiates a new QUIC_SSTREAM. init_buf_size specifies the initial size of
  53. * the stream data buffer in bytes, which must be positive.
  54. */
  55. QUIC_SSTREAM *ossl_quic_sstream_new(size_t init_buf_size);
  56. /*
  57. * Frees a QUIC_SSTREAM and associated stream data storage.
  58. *
  59. * Any iovecs returned by ossl_quic_sstream_get_stream_frame cease to be valid after
  60. * calling this function.
  61. */
  62. void ossl_quic_sstream_free(QUIC_SSTREAM *qss);
  63. /*
  64. * (For TX packetizer use.) Retrieves information about application stream data
  65. * which is ready for transmission.
  66. *
  67. * *hdr is filled with the logical offset, maximum possible length of stream
  68. * data which can be transmitted, and a pointer to the stream data to be
  69. * transmitted. is_fin is set to 1 if hdr->offset + hdr->len is the final size
  70. * of the stream and 0 otherwise. hdr->stream_id is not set; the caller must set
  71. * it.
  72. *
  73. * The caller is not obligated to send all of the data. If the caller does not
  74. * send all of the data, the caller must reduce hdr->len before serializing the
  75. * header structure and must ensure that hdr->is_fin is cleared.
  76. *
  77. * hdr->has_explicit_len is always set. It is the caller's responsibility to
  78. * clear this if it wants to use the optimization of omitting the length field,
  79. * as only the caller can know when this optimization can be performed.
  80. *
  81. * *num_iov must be set to the size of the iov array at call time. When this
  82. * function returns successfully, it is updated to the number of iov entries
  83. * which have been written.
  84. *
  85. * The stream data may be split across up to two IOVs due to internal ring
  86. * buffer organisation. The sum of the lengths of the IOVs and the value written
  87. * to hdr->len will always match. If the caller decides to send less than
  88. * hdr->len of stream data, it must adjust the IOVs accordingly. This may be
  89. * done by updating hdr->len and then calling the utility function
  90. * ossl_quic_sstream_adjust_iov().
  91. *
  92. * After committing one or more bytes returned by ossl_quic_sstream_get_stream_frame to a
  93. * packet, call ossl_quic_sstream_mark_transmitted with the inclusive range of logical
  94. * byte numbers of the transmitted bytes (i.e., hdr->offset, hdr->offset +
  95. * hdr->len - 1). If you do not call ossl_quic_sstream_mark_transmitted, the next call to
  96. * ossl_quic_sstream_get_stream_frame will return the same data (or potentially the same
  97. * and more, if more data has been appended by the application).
  98. *
  99. * It is the caller's responsibility to clamp the length of data which this
  100. * function indicates is available according to other concerns, such as
  101. * stream-level flow control, connection-level flow control, or the applicable
  102. * maximum datagram payload length (MDPL) for a packet under construction.
  103. *
  104. * The skip argument can usually be given as zero. If it is non-zero, this
  105. * function outputs a range which would be output if it were called again after
  106. * calling ossl_quic_sstream_mark_transmitted() with the returned range, repeated 'skip'
  107. * times, and so on. This may be useful for callers which wish to enumerate
  108. * available stream frames and batch their calls to ossl_quic_sstream_mark_transmitted at
  109. * a later time.
  110. *
  111. * On success, this function will never write *num_iov with a value other than
  112. * 0, 1 or 2. A *num_iov value of 0 can only occurs when hdr->is_fin is set (for
  113. * example, when a stream is closed after all existing data has been sent, and
  114. * without sending any more data); otherwise the function returns 0 as there is
  115. * nothing useful to report.
  116. *
  117. * Returns 1 on success and 0 if there is no stream data available for
  118. * transmission, or on other error (such as if the caller provides fewer
  119. * than two IOVs.)
  120. */
  121. int ossl_quic_sstream_get_stream_frame(QUIC_SSTREAM *qss,
  122. size_t skip,
  123. OSSL_QUIC_FRAME_STREAM *hdr,
  124. OSSL_QTX_IOVEC *iov,
  125. size_t *num_iov);
  126. /*
  127. * Returns 1 if there is data pending transmission. Equivalent to calling
  128. * ossl_quic_sstream_get_stream_frame and seeing if it succeeds.
  129. */
  130. int ossl_quic_sstream_has_pending(QUIC_SSTREAM *qss);
  131. /*
  132. * Returns the current size of the stream; i.e., the number of bytes which have
  133. * been appended to the stream so far.
  134. */
  135. uint64_t ossl_quic_sstream_get_cur_size(QUIC_SSTREAM *qss);
  136. /*
  137. * (For TX packetizer use.) Marks a logical range of the send stream as having
  138. * been transmitted.
  139. *
  140. * 0 denotes the first byte ever sent on the stream. The start and end values
  141. * are both inclusive, therefore all calls to this function always mark at least
  142. * one byte as being transmitted; if no bytes have been transmitted, do not call
  143. * this function.
  144. *
  145. * If the STREAM frame sent had the FIN bit set, you must also call
  146. * ossl_quic_sstream_mark_transmitted_fin() after calling this function.
  147. *
  148. * If you sent a zero-length STREAM frame with the FIN bit set, you need only
  149. * call ossl_quic_sstream_mark_transmitted_fin() and must not call this function.
  150. *
  151. * Returns 1 on success and 0 on error (e.g. if end < start).
  152. */
  153. int ossl_quic_sstream_mark_transmitted(QUIC_SSTREAM *qss,
  154. uint64_t start,
  155. uint64_t end);
  156. /*
  157. * (For TX packetizer use.) Marks a STREAM frame with the FIN bit set as having
  158. * been transmitted. final_size is the final size of the stream (i.e., the value
  159. * offset + len of the transmitted STREAM frame).
  160. *
  161. * This function fails returning 0 if ossl_quic_sstream_fin() has not been called or if
  162. * final_size is not correct. The final_size argument is not strictly needed by
  163. * the QUIC_SSTREAM but is required as a sanity check.
  164. */
  165. int ossl_quic_sstream_mark_transmitted_fin(QUIC_SSTREAM *qss,
  166. uint64_t final_size);
  167. /*
  168. * (RX/ACKM use.) Marks a logical range of the send stream as having been lost.
  169. * The send stream will return the lost data for retransmission on a future call
  170. * to ossl_quic_sstream_get_stream_frame. The start and end values denote logical byte
  171. * numbers and are inclusive.
  172. *
  173. * If the lost frame had the FIN bit set, you must also call
  174. * ossl_quic_sstream_mark_lost_fin() after calling this function.
  175. *
  176. * Returns 1 on success and 0 on error (e.g. if end < start).
  177. */
  178. int ossl_quic_sstream_mark_lost(QUIC_SSTREAM *qss,
  179. uint64_t start,
  180. uint64_t end);
  181. /*
  182. * (RX/ACKM use.) Informs the QUIC_SSTREAM that a STREAM frame with the FIN bit
  183. * set was lost.
  184. *
  185. * Returns 1 on success and 0 on error.
  186. */
  187. int ossl_quic_sstream_mark_lost_fin(QUIC_SSTREAM *qss);
  188. /*
  189. * (RX/ACKM use.) Marks a logical range of the send stream as having been
  190. * acknowledged, meaning that the storage for the data in that range of the
  191. * stream can be now recycled and neither that logical range of the stream nor
  192. * any subset of it can be retransmitted again. The start and end values are
  193. * inclusive.
  194. *
  195. * If the acknowledged frame had the FIN bit set, you must also call
  196. * ossl_quic_sstream_mark_acked_fin() after calling this function.
  197. *
  198. * Returns 1 on success and 0 on error (e.g. if end < start).
  199. */
  200. int ossl_quic_sstream_mark_acked(QUIC_SSTREAM *qss,
  201. uint64_t start,
  202. uint64_t end);
  203. /*
  204. * (RX/ACKM use.) Informs the QUIC_SSTREAM that a STREAM frame with the FIN bit
  205. * set was acknowledged.
  206. *
  207. * Returns 1 on success and 0 on error.
  208. */
  209. int ossl_quic_sstream_mark_acked_fin(QUIC_SSTREAM *qss);
  210. /*
  211. * (Front end use.) Appends user data to the stream. The data is copied into the
  212. * stream. The amount of data consumed from buf is written to *consumed on
  213. * success (short writes are possible). The amount of data which can be written
  214. * can be determined in advance by calling the ossl_quic_sstream_get_buffer_avail()
  215. * function; data is copied into an internal ring buffer of finite size.
  216. *
  217. * If the buffer is full, this should be materialised as a backpressure
  218. * condition by the front end. This is not considered a failure condition;
  219. * *consumed is written as 0 and the function returns 1.
  220. *
  221. * Returns 1 on success or 0 on failure.
  222. */
  223. int ossl_quic_sstream_append(QUIC_SSTREAM *qss,
  224. const unsigned char *buf,
  225. size_t buf_len,
  226. size_t *consumed);
  227. /*
  228. * Marks a stream as finished. ossl_quic_sstream_append() may not be called anymore
  229. * after calling this.
  230. */
  231. void ossl_quic_sstream_fin(QUIC_SSTREAM *qss);
  232. /*
  233. * If the stream has had ossl_quic_sstream_fin() called, returns 1 and writes
  234. * the final size to *final_size. Otherwise, returns 0.
  235. */
  236. int ossl_quic_sstream_get_final_size(QUIC_SSTREAM *qss, uint64_t *final_size);
  237. /*
  238. * Returns 1 iff all bytes (and any FIN, if any) which have been appended to the
  239. * QUIC_SSTREAM so far, and any FIN (if any), have been both sent and acked.
  240. */
  241. int ossl_quic_sstream_is_totally_acked(QUIC_SSTREAM *qss);
  242. /*
  243. * Resizes the internal ring buffer. All stream data is preserved safely.
  244. *
  245. * This can be used to expand or contract the ring buffer, but not to contract
  246. * the ring buffer below the amount of stream data currently stored in it.
  247. * Returns 1 on success and 0 on failure.
  248. *
  249. * IMPORTANT: Any buffers referenced by iovecs output by
  250. * ossl_quic_sstream_get_stream_frame() cease to be valid after calling this function.
  251. */
  252. int ossl_quic_sstream_set_buffer_size(QUIC_SSTREAM *qss, size_t num_bytes);
  253. /*
  254. * Gets the internal ring buffer size in bytes.
  255. */
  256. size_t ossl_quic_sstream_get_buffer_size(QUIC_SSTREAM *qss);
  257. /*
  258. * Gets the number of bytes used in the internal ring buffer.
  259. */
  260. size_t ossl_quic_sstream_get_buffer_used(QUIC_SSTREAM *qss);
  261. /*
  262. * Gets the number of bytes free in the internal ring buffer.
  263. */
  264. size_t ossl_quic_sstream_get_buffer_avail(QUIC_SSTREAM *qss);
  265. /*
  266. * Utility function to ensure the length of an array of iovecs matches the
  267. * length given as len. Trailing iovecs have their length values reduced or set
  268. * to 0 as necessary.
  269. */
  270. void ossl_quic_sstream_adjust_iov(size_t len,
  271. OSSL_QTX_IOVEC *iov,
  272. size_t num_iov);
  273. /*
  274. * Sets flag to cleanse the buffered data when it is acked.
  275. */
  276. void ossl_quic_sstream_set_cleanse(QUIC_SSTREAM *qss, int cleanse);
  277. /*
  278. * QUIC Receive Stream Manager
  279. * ===========================
  280. *
  281. * The QUIC Receive Stream Manager (QUIC_RSTREAM) is responsible for
  282. * storing the received stream data frames until the application
  283. * is able to read the data.
  284. *
  285. * The QUIC_RSTREAM is instantiated once for every stream that can receive data.
  286. * (i.e., for a unidirectional receiving stream or for the receiving component
  287. * of a bidirectional stream).
  288. */
  289. /*
  290. * Create a new instance of QUIC_RSTREAM with pointers to the flow
  291. * controller and statistics module. They can be NULL for unit testing.
  292. * If they are non-NULL, the `rxfc` is called when receive stream data
  293. * is read by application. `statm` is queried for current rtt.
  294. * `rbuf_size` is the initial size of the ring buffer to be used
  295. * when ossl_quic_rstream_move_to_rbuf() is called.
  296. */
  297. QUIC_RSTREAM *ossl_quic_rstream_new(QUIC_RXFC *rxfc,
  298. OSSL_STATM *statm, size_t rbuf_size);
  299. /*
  300. * Frees a QUIC_RSTREAM and any associated storage.
  301. */
  302. void ossl_quic_rstream_free(QUIC_RSTREAM *qrs);
  303. /*
  304. * Adds received stream frame data to `qrs`. The `pkt_wrap` refcount is
  305. * incremented if the `data` is queued directly without copying.
  306. * It can be NULL for unit-testing purposes, i.e. if `data` is static or
  307. * never released before calling ossl_quic_rstream_free().
  308. * The `offset` is the absolute offset of the data in the stream.
  309. * `data_len` can be 0 - can be useful for indicating `fin` for empty stream.
  310. * Or to indicate `fin` without any further data added to the stream.
  311. */
  312. int ossl_quic_rstream_queue_data(QUIC_RSTREAM *qrs, OSSL_QRX_PKT *pkt,
  313. uint64_t offset,
  314. const unsigned char *data, uint64_t data_len,
  315. int fin);
  316. /*
  317. * Copies the data from the stream storage to buffer `buf` of size `size`.
  318. * `readbytes` is set to the number of bytes actually copied.
  319. * `fin` is set to 1 if all the data from the stream were read so the
  320. * stream is finished. It is set to 0 otherwise.
  321. */
  322. int ossl_quic_rstream_read(QUIC_RSTREAM *qrs, unsigned char *buf, size_t size,
  323. size_t *readbytes, int *fin);
  324. /*
  325. * Peeks at the data in the stream storage. It copies them to buffer `buf`
  326. * of size `size` and sets `readbytes` to the number of bytes actually copied.
  327. * `fin` is set to 1 if the copied data reach end of the stream.
  328. * It is set to 0 otherwise.
  329. */
  330. int ossl_quic_rstream_peek(QUIC_RSTREAM *qrs, unsigned char *buf, size_t size,
  331. size_t *readbytes, int *fin);
  332. /*
  333. * Returns the size of the data available for reading. `fin` is set to 1 if
  334. * after reading all the available data the stream will be finished,
  335. * set to 0 otherwise.
  336. */
  337. int ossl_quic_rstream_available(QUIC_RSTREAM *qrs, size_t *avail, int *fin);
  338. /*
  339. * Sets *record to the beginning of the first readable stream data chunk and
  340. * *reclen to the size of the chunk. *fin is set to 1 if the end of the
  341. * chunk is the last of the stream data chunks.
  342. * If there is no record available *record is set to NULL and *rec_len to 0;
  343. * ossl_quic_rstream_release_record() should not be called in that case.
  344. * Returns 1 on success (including calls if no record is available, or
  345. * after end of the stream - in that case *fin will be set to 1 and
  346. * *rec_len to 0), 0 on error.
  347. * It is an error to call ossl_quic_rstream_get_record() multiple times
  348. * without calling ossl_quic_rstream_release_record() in between.
  349. */
  350. int ossl_quic_rstream_get_record(QUIC_RSTREAM *qrs,
  351. const unsigned char **record, size_t *rec_len,
  352. int *fin);
  353. /*
  354. * Releases (possibly partially) the record returned by
  355. * previous ossl_quic_rstream_get_record() call.
  356. * read_len between previously returned *rec_len and SIZE_MAX indicates
  357. * release of the whole record. Otherwise only part of the record is
  358. * released. The remaining part of the record is unlocked, another
  359. * call to ossl_quic_rstream_get_record() is needed to obtain further
  360. * stream data.
  361. * Returns 1 on success, 0 on error.
  362. * It is an error to call ossl_quic_rstream_release_record() multiple
  363. * times without calling ossl_quic_rstream_get_record() in between.
  364. */
  365. int ossl_quic_rstream_release_record(QUIC_RSTREAM *qrs, size_t read_len);
  366. /*
  367. * Moves received frame data from decrypted packets to ring buffer.
  368. * This should be called when there are too many decrypted packets allocated.
  369. * Returns 1 on success, 0 when it was not possible to release all
  370. * referenced packets due to an insufficient size of the ring buffer.
  371. * Exception is the packet from the record returned previously by
  372. * ossl_quic_rstream_get_record() - that one will be always skipped.
  373. */
  374. int ossl_quic_rstream_move_to_rbuf(QUIC_RSTREAM *qrs);
  375. /*
  376. * Resizes the internal ring buffer to a new `rbuf_size` size.
  377. * Returns 1 on success, 0 on error.
  378. * Possible error conditions are an allocation failure, trying to resize
  379. * the ring buffer when ossl_quic_rstream_get_record() was called and
  380. * not yet released, or trying to resize the ring buffer to a smaller size
  381. * than currently occupied.
  382. */
  383. int ossl_quic_rstream_resize_rbuf(QUIC_RSTREAM *qrs, size_t rbuf_size);
  384. /*
  385. * Sets flag to cleanse the buffered data when user reads it.
  386. */
  387. void ossl_quic_rstream_set_cleanse(QUIC_RSTREAM *qrs, int cleanse);
  388. # endif
  389. #endif