]> git.ipfire.org Git - thirdparty/openssl.git/blame - include/internal/quic_stream.h
QUIC Send Stream Management
[thirdparty/openssl.git] / include / internal / quic_stream.h
CommitLineData
83022590
HL
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
10#ifndef OSSL_INTERNAL_QUIC_STREAM_H
11# define OSSL_INTERNAL_QUIC_STREAM_H
12# pragma once
13
14#include "internal/e_os.h"
15#include "internal/time.h"
16#include "internal/quic_types.h"
17#include "internal/quic_wire.h"
18#include "internal/quic_record_tx.h"
19
20/*
21 * QUIC Send Stream
22 * ================
23 *
24 * The QUIC Send Stream Manager (QUIC_SSTREAM) is responsible for:
25 *
26 * - accepting octet strings of stream data;
27 *
28 * - generating corresponding STREAM frames;
29 *
30 * - receiving notifications of lost frames, in order to generate new STREAM
31 * frames for the lost data;
32 *
33 * - receiving notifications of acknowledged frames, in order to internally
34 * reuse memory used to store acknowledged stream data;
35 *
36 * - informing the caller of how much more stream data it can accept into
37 * its internal buffers, so as to ensure that the amount of unacknowledged
38 * data which can be written to a stream is not infinite and to allow the
39 * caller to manifest backpressure conditions to the user.
40 *
41 * The QUIC_SSTREAM is instantiated once for every stream with a send component
42 * (i.e., for a unidirectional send stream or for the send component of a
43 * bidirectional stream).
44 *
45 * Note: The terms 'TX' and 'RX' are used when referring to frames, packets and
46 * datagrams. The terms 'send' and 'receive' are used when referring to the
47 * stream abstraction. Applications send; we transmit.
48 */
49typedef struct quic_sstream_st QUIC_SSTREAM;
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 */
55QUIC_SSTREAM *ossl_quic_sstream_new(size_t init_buf_size);
56
57/*
58 * Frees a QUIC_SSTREAM and associated stream data storage.
59 *
60 * Any iovecs returned by ossl_quic_sstream_get_stream_frame cease to be valid after
61 * calling this function.
62 */
63void ossl_quic_sstream_free(QUIC_SSTREAM *qss);
64
65/*
66 * (For TX packetizer use.) Retrieves information about application stream data
67 * which is ready for transmission.
68 *
69 * *hdr is filled with the logical offset, maximum possible length of stream
70 * data which can be transmitted, and a pointer to the stream data to be
71 * transmitted. is_fin is set to 1 if hdr->offset + hdr->len is the final size
72 * of the stream and 0 otherwise. hdr->stream_id is not set; the caller must set
73 * it.
74 *
75 * The caller is not obligated to send all of the data. If the caller does not
76 * send all of the data, the caller must reduce hdr->len before serializing the
77 * header structure and must ensure that hdr->is_fin is cleared.
78 *
79 * hdr->has_explicit_len is always set. It is the caller's responsibility to
80 * clear this if it wants to use the optimization of omitting the length field,
81 * as only the caller can know when this optimization can be performed.
82 *
83 * *num_iov must be set to the size of the iov array at call time. When this
84 * function returns successfully, it is updated to the number of iov entries
85 * which have been written.
86 *
87 * The stream data may be split across up to two IOVs due to internal ring
88 * buffer organisation. The sum of the lengths of the IOVs and the value written
89 * to hdr->len will always match. If the caller decides to send less than
90 * hdr->len of stream data, it must adjust the IOVs accordingly. This may be
91 * done by updating hdr->len and then calling the utility function
92 * ossl_quic_sstream_adjust_iov().
93 *
94 * After committing one or more bytes returned by ossl_quic_sstream_get_stream_frame to a
95 * packet, call ossl_quic_sstream_mark_transmitted with the inclusive range of logical
96 * byte numbers of the transmitted bytes (i.e., hdr->offset, hdr->offset +
97 * hdr->len - 1). If you do not call ossl_quic_sstream_mark_transmitted, the next call to
98 * ossl_quic_sstream_get_stream_frame will return the same data (or potentially the same
99 * and more, if more data has been appended by the application).
100 *
101 * It is the caller's responsibility to clamp the length of data which this
102 * function indicates is available according to other concerns, such as
103 * stream-level flow control, connection-level flow control, or the applicable
104 * maximum datagram payload length (MDPL) for a packet under construction.
105 *
106 * The skip argument can usually be given as zero. If it is non-zero, this
107 * function outputs a range which would be output if it were called again after
108 * calling ossl_quic_sstream_mark_transmitted() with the returned range, repeated 'skip'
109 * times, and so on. This may be useful for callers which wish to enumerate
110 * available stream frames and batch their calls to ossl_quic_sstream_mark_transmitted at
111 * a later time.
112 *
113 * On success, this function will never write *num_iov with a value other than
114 * 0, 1 or 2. A *num_iov value of 0 can only occurs when hdr->is_fin is set (for
115 * example, when a stream is closed after all existing data has been sent, and
116 * without sending any more data); otherwise the function returns 0 as there is
117 * nothing useful to report.
118 *
119 * Returns 1 on success and 0 if there is no stream data available for
120 * transmission, or on other error (such as if the caller provides fewer
121 * than two IOVs.)
122 */
123int ossl_quic_sstream_get_stream_frame(QUIC_SSTREAM *qss,
124 size_t skip,
125 OSSL_QUIC_FRAME_STREAM *hdr,
126 OSSL_QTX_IOVEC *iov,
127 size_t *num_iov);
128
129/*
130 * (For TX packetizer use.) Marks a logical range of the send stream as having
131 * been transmitted.
132 *
133 * 0 denotes the first byte ever sent on the stream. The start and end values
134 * are both inclusive, therefore all calls to this function always mark at least
135 * one byte as being transmitted; if no bytes have been transmitted, do not call
136 * this function.
137 *
138 * If the STREAM frame sent had the FIN bit set, you must also call
139 * ossl_quic_sstream_mark_transmitted_fin() after calling this function.
140 *
141 * If you sent a zero-length STREAM frame with the FIN bit set, you need only
142 * call ossl_quic_sstream_mark_transmitted_fin() and must not call this function.
143 *
144 * Returns 1 on success and 0 on error (e.g. if end < start).
145 */
146int ossl_quic_sstream_mark_transmitted(QUIC_SSTREAM *qss,
147 uint64_t start,
148 uint64_t end);
149
150/*
151 * (For TX packetizer use.) Marks a STREAM frame with the FIN bit set as having
152 * been transmitted. final_size is the final size of the stream (i.e., the value
153 * offset + len of the transmitted STREAM frame).
154 *
155 * This function fails returning 0 if ossl_quic_sstream_fin() has not been called or if
156 * final_size is not correct. The final_size argument is not strictly needed by
157 * the QUIC_SSTREAM but is required as a sanity check.
158 */
159int ossl_quic_sstream_mark_transmitted_fin(QUIC_SSTREAM *qss,
160 uint64_t final_size);
161
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 */
173int ossl_quic_sstream_mark_lost(QUIC_SSTREAM *qss,
174 uint64_t start,
175 uint64_t end);
176
177/*
178 * (RX/ACKM use.) Informs the QUIC_SSTREAM that a STREAM frame with the FIN bit
179 * set was lost.
180 *
181 * Returns 1 on success and 0 on error.
182 */
183int ossl_quic_sstream_mark_lost_fin(QUIC_SSTREAM *qss);
184
185/*
186 * (RX/ACKM use.) Marks a logical range of the send stream as having been
187 * acknowledged, meaning that the storage for the data in that range of the
188 * stream can be now recycled and neither that logical range of the stream nor
189 * any subset of it can be retransmitted again. The start and end values are
190 * inclusive.
191 *
192 * If the acknowledged frame had the FIN bit set, you must also call
193 * ossl_quic_sstream_mark_acked_fin() after calling this function.
194 *
195 * Returns 1 on success and 0 on error (e.g. if end < start).
196 */
197int ossl_quic_sstream_mark_acked(QUIC_SSTREAM *qss,
198 uint64_t start,
199 uint64_t end);
200
201/*
202 * (RX/ACKM use.) Informs the QUIC_SSTREAM that a STREAM frame with the FIN bit
203 * set was acknowledged.
204 *
205 * Returns 1 on success and 0 on error.
206 */
207int ossl_quic_sstream_mark_acked_fin(QUIC_SSTREAM *qss);
208
209/*
210 * (Front end use.) Appends user data to the stream. The data is copied into the
211 * stream. The amount of data consumed from buf is written to *consumed on
212 * success (short writes are possible). The amount of data which can be written
213 * can be determined in advance by calling the ossl_quic_sstream_get_buffer_avail()
214 * function; data is copied into an internal ring buffer of finite size.
215 *
216 * If the buffer is full, this should be materialised as a backpressure
217 * condition by the front end. This is not considered a failure condition;
218 * *consumed is written as 0 and the function returns 1.
219 *
220 * Returns 1 on success or 0 on failure.
221 */
222int ossl_quic_sstream_append(QUIC_SSTREAM *qss,
223 const unsigned char *buf,
224 size_t buf_len,
225 size_t *consumed);
226
227/*
228 * Marks a stream as finished. ossl_quic_sstream_append() may not be called anymore
229 * after calling this.
230 */
231void ossl_quic_sstream_fin(QUIC_SSTREAM *qss);
232
233/*
234 * Resizes the internal ring buffer. All stream data is preserved safely.
235 *
236 * This can be used to expand or contract the ring buffer, but not to contract
237 * the ring buffer below the amount of stream data currently stored in it.
238 * Returns 1 on success and 0 on failure.
239 *
240 * IMPORTANT: Any buffers referenced by iovecs output by
241 * ossl_quic_sstream_get_stream_frame() cease to be valid after calling this function.
242 */
243int ossl_quic_sstream_set_buffer_size(QUIC_SSTREAM *qss, size_t num_bytes);
244
245/*
246 * Gets the internal ring buffer size in bytes.
247 */
248size_t ossl_quic_sstream_get_buffer_size(QUIC_SSTREAM *qss);
249
250/*
251 * Gets the number of bytes used in the internal ring buffer.
252 */
253size_t ossl_quic_sstream_get_buffer_used(QUIC_SSTREAM *qss);
254
255/*
256 * Gets the number of bytes free in the internal ring buffer.
257 */
258size_t ossl_quic_sstream_get_buffer_avail(QUIC_SSTREAM *qss);
259
260/*
261 * Utility function to ensure the length of an array of iovecs matches the
262 * length given as len. Trailing iovecs have their length values reduced or set
263 * to 0 as necessary.
264 */
265void ossl_quic_sstream_adjust_iov(size_t len,
266 OSSL_QTX_IOVEC *iov,
267 size_t num_iov);
268
269
270#endif