quic_stream.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Copyright 2022 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_wire.h"
  16. #include "internal/quic_record_tx.h"
  17. #include "internal/quic_record_rx.h"
  18. #include "internal/quic_record_rx_wrap.h"
  19. #include "internal/quic_fc.h"
  20. #include "internal/quic_statm.h"
  21. /*
  22. * QUIC Send Stream
  23. * ================
  24. *
  25. * The QUIC Send Stream Manager (QUIC_SSTREAM) is responsible for:
  26. *
  27. * - accepting octet strings of stream data;
  28. *
  29. * - generating corresponding STREAM frames;
  30. *
  31. * - receiving notifications of lost frames, in order to generate new STREAM
  32. * frames for the lost data;
  33. *
  34. * - receiving notifications of acknowledged frames, in order to internally
  35. * reuse memory used to store acknowledged stream data;
  36. *
  37. * - informing the caller of how much more stream data it can accept into
  38. * its internal buffers, so as to ensure that the amount of unacknowledged
  39. * data which can be written to a stream is not infinite and to allow the
  40. * caller to manifest backpressure conditions to the user.
  41. *
  42. * The QUIC_SSTREAM is instantiated once for every stream with a send component
  43. * (i.e., for a unidirectional send stream or for the send component of a
  44. * bidirectional stream).
  45. *
  46. * Note: The terms 'TX' and 'RX' are used when referring to frames, packets and
  47. * datagrams. The terms 'send' and 'receive' are used when referring to the
  48. * stream abstraction. Applications send; we transmit.
  49. */
  50. typedef struct quic_sstream_st QUIC_SSTREAM;
  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 the current size of the stream; i.e., the number of bytes which have
  128. * been appended to the stream so far.
  129. */
  130. uint64_t ossl_quic_sstream_get_cur_size(QUIC_SSTREAM *qss);
  131. /*
  132. * (For TX packetizer use.) Marks a logical range of the send stream as having
  133. * been transmitted.
  134. *
  135. * 0 denotes the first byte ever sent on the stream. The start and end values
  136. * are both inclusive, therefore all calls to this function always mark at least
  137. * one byte as being transmitted; if no bytes have been transmitted, do not call
  138. * this function.
  139. *
  140. * If the STREAM frame sent had the FIN bit set, you must also call
  141. * ossl_quic_sstream_mark_transmitted_fin() after calling this function.
  142. *
  143. * If you sent a zero-length STREAM frame with the FIN bit set, you need only
  144. * call ossl_quic_sstream_mark_transmitted_fin() and must not call this function.
  145. *
  146. * Returns 1 on success and 0 on error (e.g. if end < start).
  147. */
  148. int ossl_quic_sstream_mark_transmitted(QUIC_SSTREAM *qss,
  149. uint64_t start,
  150. uint64_t end);
  151. /*
  152. * (For TX packetizer use.) Marks a STREAM frame with the FIN bit set as having
  153. * been transmitted. final_size is the final size of the stream (i.e., the value
  154. * offset + len of the transmitted STREAM frame).
  155. *
  156. * This function fails returning 0 if ossl_quic_sstream_fin() has not been called or if
  157. * final_size is not correct. The final_size argument is not strictly needed by
  158. * the QUIC_SSTREAM but is required as a sanity check.
  159. */
  160. int ossl_quic_sstream_mark_transmitted_fin(QUIC_SSTREAM *qss,
  161. uint64_t final_size);
  162. /*
  163. * (RX/ACKM use.) Marks a logical range of the send stream as having been lost.
  164. * The send stream will return the lost data for retransmission on a future call
  165. * to ossl_quic_sstream_get_stream_frame. The start and end values denote logical byte
  166. * numbers and are inclusive.
  167. *
  168. * If the lost frame had the FIN bit set, you must also call
  169. * ossl_quic_sstream_mark_lost_fin() after calling this function.
  170. *
  171. * Returns 1 on success and 0 on error (e.g. if end < start).
  172. */
  173. int ossl_quic_sstream_mark_lost(QUIC_SSTREAM *qss,
  174. uint64_t start,
  175. uint64_t end);
  176. /*
  177. * (RX/ACKM use.) Informs the QUIC_SSTREAM that a STREAM frame with the FIN bit
  178. * set was lost.
  179. *
  180. * Returns 1 on success and 0 on error.
  181. */
  182. int ossl_quic_sstream_mark_lost_fin(QUIC_SSTREAM *qss);
  183. /*
  184. * (RX/ACKM use.) Marks a logical range of the send stream as having been
  185. * acknowledged, meaning that the storage for the data in that range of the
  186. * stream can be now recycled and neither that logical range of the stream nor
  187. * any subset of it can be retransmitted again. The start and end values are
  188. * inclusive.
  189. *
  190. * If the acknowledged frame had the FIN bit set, you must also call
  191. * ossl_quic_sstream_mark_acked_fin() after calling this function.
  192. *
  193. * Returns 1 on success and 0 on error (e.g. if end < start).
  194. */
  195. int ossl_quic_sstream_mark_acked(QUIC_SSTREAM *qss,
  196. uint64_t start,
  197. uint64_t end);
  198. /*
  199. * (RX/ACKM use.) Informs the QUIC_SSTREAM that a STREAM frame with the FIN bit
  200. * set was acknowledged.
  201. *
  202. * Returns 1 on success and 0 on error.
  203. */
  204. int ossl_quic_sstream_mark_acked_fin(QUIC_SSTREAM *qss);
  205. /*
  206. * (Front end use.) Appends user data to the stream. The data is copied into the
  207. * stream. The amount of data consumed from buf is written to *consumed on
  208. * success (short writes are possible). The amount of data which can be written
  209. * can be determined in advance by calling the ossl_quic_sstream_get_buffer_avail()
  210. * function; data is copied into an internal ring buffer of finite size.
  211. *
  212. * If the buffer is full, this should be materialised as a backpressure
  213. * condition by the front end. This is not considered a failure condition;
  214. * *consumed is written as 0 and the function returns 1.
  215. *
  216. * Returns 1 on success or 0 on failure.
  217. */
  218. int ossl_quic_sstream_append(QUIC_SSTREAM *qss,
  219. const unsigned char *buf,
  220. size_t buf_len,
  221. size_t *consumed);
  222. /*
  223. * Marks a stream as finished. ossl_quic_sstream_append() may not be called anymore
  224. * after calling this.
  225. */
  226. void ossl_quic_sstream_fin(QUIC_SSTREAM *qss);
  227. /*
  228. * Resizes the internal ring buffer. All stream data is preserved safely.
  229. *
  230. * This can be used to expand or contract the ring buffer, but not to contract
  231. * the ring buffer below the amount of stream data currently stored in it.
  232. * Returns 1 on success and 0 on failure.
  233. *
  234. * IMPORTANT: Any buffers referenced by iovecs output by
  235. * ossl_quic_sstream_get_stream_frame() cease to be valid after calling this function.
  236. */
  237. int ossl_quic_sstream_set_buffer_size(QUIC_SSTREAM *qss, size_t num_bytes);
  238. /*
  239. * Gets the internal ring buffer size in bytes.
  240. */
  241. size_t ossl_quic_sstream_get_buffer_size(QUIC_SSTREAM *qss);
  242. /*
  243. * Gets the number of bytes used in the internal ring buffer.
  244. */
  245. size_t ossl_quic_sstream_get_buffer_used(QUIC_SSTREAM *qss);
  246. /*
  247. * Gets the number of bytes free in the internal ring buffer.
  248. */
  249. size_t ossl_quic_sstream_get_buffer_avail(QUIC_SSTREAM *qss);
  250. /*
  251. * Utility function to ensure the length of an array of iovecs matches the
  252. * length given as len. Trailing iovecs have their length values reduced or set
  253. * to 0 as necessary.
  254. */
  255. void ossl_quic_sstream_adjust_iov(size_t len,
  256. OSSL_QTX_IOVEC *iov,
  257. size_t num_iov);
  258. /*
  259. * QUIC Receive Stream Manager
  260. * ===========================
  261. *
  262. * The QUIC Receive Stream Manager (QUIC_RSTREAM) is responsible for
  263. * storing the received stream data frames until the application
  264. * is able to read the data.
  265. *
  266. * The QUIC_RSTREAM is instantiated once for every stream that can receive data.
  267. * (i.e., for a unidirectional receiving stream or for the receiving component
  268. * of a bidirectional stream).
  269. */
  270. typedef struct quic_rstream_st QUIC_RSTREAM;
  271. /*
  272. * Create a new instance of QUIC_RSTREAM with pointers to the flow
  273. * controller and statistics module. They can be NULL for unit testing.
  274. * If they are non-NULL, the `rxfc` is called when receive stream data
  275. * is read by application. `statm` is queried for current rtt.
  276. */
  277. QUIC_RSTREAM *ossl_quic_rstream_new(OSSL_QRX *qrx, QUIC_RXFC *rxfc,
  278. OSSL_STATM *statm);
  279. /*
  280. * Frees a QUIC_RSTREAM and any associated storage.
  281. */
  282. void ossl_quic_rstream_free(QUIC_RSTREAM *qrs);
  283. /*
  284. * Adds received stream frame data to `qrs`. The `pkt_wrap` refcount is
  285. * incremented if the `data` is queued directly without copying.
  286. * It can be NULL for unit-testing purposes, i.e. if `data` is static or
  287. * never released before calling ossl_quic_rstream_free().
  288. * The `offset` is the absolute offset of the data in the stream.
  289. * `data_len` can be 0 - can be useful for indicating `fin` for empty stream.
  290. * Or to indicate `fin` without any further data added to the stream.
  291. */
  292. int ossl_quic_rstream_queue_data(QUIC_RSTREAM *qrs, OSSL_QRX_PKT_WRAP *pkt_wrap,
  293. uint64_t offset,
  294. const unsigned char *data, uint64_t data_len,
  295. int fin);
  296. /*
  297. * Copies the data from the stream storage to buffer `buf` of size `size`.
  298. * `readbytes` is set to the number of bytes actually copied.
  299. * `fin` is set to 1 if all the data from the stream were read so the
  300. * stream is finished. It is set to 0 otherwise.
  301. */
  302. int ossl_quic_rstream_read(QUIC_RSTREAM *qrs, unsigned char *buf, size_t size,
  303. size_t *readbytes, int *fin);
  304. /*
  305. * Peeks at the data in the stream storage. It copies them to buffer `buf`
  306. * of size `size` and sets `readbytes` to the number of bytes actually copied.
  307. * `fin` is set to 1 if the copied data reach end of the stream.
  308. * It is set to 0 otherwise.
  309. */
  310. int ossl_quic_rstream_peek(QUIC_RSTREAM *qrs, unsigned char *buf, size_t size,
  311. size_t *readbytes, int *fin);
  312. /*
  313. * Returns the size of the data available for reading. `fin` is set to 1 if
  314. * after reading all the available data the stream will be finished,
  315. * set to 0 otherwise.
  316. */
  317. int ossl_quic_rstream_available(QUIC_RSTREAM *qrs, size_t *avail, int *fin);
  318. #endif