]> git.ipfire.org Git - thirdparty/openssl.git/blob - include/internal/quic_stream_map.h
QUIC QSM: Model final sizes and handle STOP_SENDING correctly
[thirdparty/openssl.git] / include / internal / quic_stream_map.h
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_MAP_H
11 # define OSSL_INTERNAL_QUIC_STREAM_MAP_H
12 # pragma once
13
14 # include "internal/e_os.h"
15 # include "internal/time.h"
16 # include "internal/common.h"
17 # include "internal/quic_types.h"
18 # include "internal/quic_stream.h"
19 # include "internal/quic_fc.h"
20 # include <openssl/lhash.h>
21
22 # ifndef OPENSSL_NO_QUIC
23
24 /*
25 * QUIC Stream
26 * ===========
27 *
28 * Logical QUIC stream composing all relevant send and receive components.
29 */
30 typedef struct quic_stream_st QUIC_STREAM;
31
32 typedef struct quic_stream_list_node_st QUIC_STREAM_LIST_NODE;
33
34 struct quic_stream_list_node_st {
35 QUIC_STREAM_LIST_NODE *prev, *next;
36 };
37
38 /*
39 * QUIC Send Stream States
40 * -----------------------
41 *
42 * These correspond to the states defined in RFC 9000 s. 3.1, with the
43 * exception of the NONE state which represents the absence of a send stream
44 * part.
45 *
46 * Invariants in each state are noted in comments below. In particular, once all
47 * data has been acknowledged received, or we have reset the stream, we don't
48 * need to keep the QUIC_SSTREAM and data buffers around. Of course, we also
49 * don't have a QUIC_SSTREAM on a receive-only stream.
50 */
51 #define QUIC_SSTREAM_STATE_NONE 0 /* --- sstream == NULL */
52 #define QUIC_SSTREAM_STATE_READY 1 /* \ */
53 #define QUIC_SSTREAM_STATE_SEND 2 /* |-- sstream != NULL */
54 #define QUIC_SSTREAM_STATE_DATA_SENT 3 /* / */
55 #define QUIC_SSTREAM_STATE_DATA_RECVD 4 /* \ */
56 #define QUIC_SSTREAM_STATE_RESET_SENT 5 /* |-- sstream == NULL */
57 #define QUIC_SSTREAM_STATE_RESET_RECVD 6 /* / */
58
59 /*
60 * QUIC Receive Stream States
61 * --------------------------
62 *
63 * These correspond to the states defined in RFC 9000 s. 3.2, with the exception
64 * of the NONE state which represents the absence of a receive stream part.
65 *
66 * Invariants in each state are noted in comments below. In particular, once all
67 * data has been read by the application, we don't need to keep the QUIC_RSTREAM
68 * and data buffers around. If the receive part is instead reset before it is
69 * finished, we also don't need to keep the QUIC_RSTREAM around. Finally, we
70 * don't need a QUIC_RSTREAM on a send-only stream.
71 */
72 #define QUIC_RSTREAM_STATE_NONE 0 /* --- rstream == NULL */
73 #define QUIC_RSTREAM_STATE_RECV 1 /* \ */
74 #define QUIC_RSTREAM_STATE_SIZE_KNOWN 2 /* |-- rstream != NULL */
75 #define QUIC_RSTREAM_STATE_DATA_RECVD 3 /* / */
76 #define QUIC_RSTREAM_STATE_DATA_READ 4 /* \ */
77 #define QUIC_RSTREAM_STATE_RESET_RECVD 5 /* |-- rstream == NULL */
78 #define QUIC_RSTREAM_STATE_RESET_READ 6 /* / */
79
80 struct quic_stream_st {
81 QUIC_STREAM_LIST_NODE active_node; /* for use by QUIC_STREAM_MAP */
82 QUIC_STREAM_LIST_NODE accept_node; /* accept queue of remotely-created streams */
83 QUIC_STREAM_LIST_NODE ready_for_gc_node; /* queue of streams now ready for GC */
84
85 /* Temporary link used by TXP. */
86 QUIC_STREAM *txp_next;
87
88 /*
89 * QUIC Stream ID. Do not assume that this encodes a type as this is a
90 * version-specific property and may change between QUIC versions; instead,
91 * use the type field.
92 */
93 uint64_t id;
94
95 /*
96 * Application Error Code (AEC) used for STOP_SENDING frame.
97 * This is only valid if stop_sending is 1.
98 */
99 uint64_t stop_sending_aec;
100
101 /*
102 * Application Error Code (AEC) used for RESET_STREAM frame.
103 * This is only valid if reset_stream is 1.
104 */
105 uint64_t reset_stream_aec;
106
107 /*
108 * Application Error Code (AEC) for incoming STOP_SENDING frame.
109 * This is only valid if peer_stop_sending is 1.
110 */
111 uint64_t peer_stop_sending_aec;
112
113 /*
114 * Application Error Code (AEC) for incoming RESET_STREAM frame.
115 * This is only valid if peer_reset_stream is 1.
116 */
117 uint64_t peer_reset_stream_aec;
118
119 /* Temporary value used by TXP. */
120 uint64_t txp_txfc_new_credit_consumed;
121
122 /*
123 * The final size of the send stream. Although this information can be
124 * discerned from a QUIC_SSTREAM, it is stored separately as we need to keep
125 * track of this even if we have thrown away the QUIC_SSTREAM. Use
126 * ossl_quic_stream_send_get_final_size to determine if this contain a
127 * valid value or if there is no final size yet for a sending part.
128 *
129 * For the receive part, the final size is tracked by the stream-level RXFC;
130 * use ossl_quic_stream_recv_get_final_size or
131 * ossl_quic_rxfc_get_final_size.
132 */
133 uint64_t send_final_size;
134
135 /*
136 * Send stream part and receive stream part buffer management objects.
137 *
138 * DO NOT test these pointers (sstream, rstream) for NULL. Determine the
139 * state of the send or receive stream part first using the appropriate
140 * function; then the invariant of that state guarantees that sstream or
141 * rstream either is or is not NULL respectively, therefore there is no
142 * valid use case for testing these pointers for NULL. In particular, a
143 * stream with a send part can still have sstream as NULL, and a stream with
144 * a receive part can still have rstream as NULL. QUIC_SSTREAM and
145 * QUIC_RSTREAM are stream buffer resource management objects which exist
146 * only when they need to for buffer management purposes. The existence or
147 * non-existence of a QUIC_SSTREAM or QUIC_RSTREAM object does not
148 * correspond with whether a stream's respective send or receive part
149 * logically exists or not.
150 */
151 QUIC_SSTREAM *sstream; /* NULL if RX-only */
152 QUIC_RSTREAM *rstream; /* NULL if TX only */
153
154 /* Stream-level flow control managers. */
155 QUIC_TXFC txfc; /* NULL if RX-only */
156 QUIC_RXFC rxfc; /* NULL if TX-only */
157
158 unsigned int type : 8; /* QUIC_STREAM_INITIATOR_*, QUIC_STREAM_DIR_* */
159
160 unsigned int send_state : 8; /* QUIC_SSTREAM_STATE_* */
161 unsigned int recv_state : 8; /* QUIC_RSTREAM_STATE_* */
162
163 /* 1 iff this QUIC_STREAM is on the active queue (invariant). */
164 unsigned int active : 1;
165
166 /*
167 * This is a cpoy of the QUIC connection as_server value, indicating
168 * whether we are locally operating as a server or not. Having this
169 * significantly simplifies stream type determination relative to our
170 * perspective. It never changes after a QUIC_STREAM is created and is the
171 * same for all QUIC_STREAMS under a QUIC_STREAM_MAP.
172 */
173 unsigned int as_server : 1;
174
175 /*
176 * Has STOP_SENDING been requested (by us)? Note that this is not the same
177 * as want_stop_sending below, as a STOP_SENDING frame may already have been
178 * sent and fully acknowledged.
179 */
180 unsigned int stop_sending : 1;
181
182 /*
183 * Has RESET_STREAM been requested (by us)? Works identically to
184 * STOP_SENDING for transmission purposes.
185 */
186 /* Has our peer sent a STOP_SENDING frame? */
187 unsigned int peer_stop_sending : 1;
188
189 /* Temporary flags used by TXP. */
190 unsigned int txp_sent_fc : 1;
191 unsigned int txp_sent_stop_sending : 1;
192 unsigned int txp_sent_reset_stream : 1;
193 unsigned int txp_drained : 1;
194 unsigned int txp_blocked : 1;
195
196 /* Frame regeneration flags. */
197 unsigned int want_max_stream_data : 1; /* used for regen only */
198 unsigned int want_stop_sending : 1; /* used for gen or regen */
199 unsigned int want_reset_stream : 1; /* used for gen or regen */
200
201 /* Flags set when frames *we* sent were acknowledged. */
202 unsigned int acked_stop_sending : 1;
203
204 /* A FIN has been retired from the rstream buffer. */
205 unsigned int recv_fin_retired : 1;
206
207 /*
208 * The stream's XSO has been deleted. Pending GC.
209 *
210 * Here is how stream deletion works:
211 *
212 * - A QUIC_STREAM cannot be deleted until it is neither in the accept
213 * queue nor has an associated XSO. This condition occurs when and only
214 * when deleted is true.
215 *
216 * - Once this is the case (i.e., no user-facing API object exposing the
217 * stream), we can delete the stream once we determine that all of our
218 * protocol obligations requiring us to keep the QUIC_STREAM around have
219 * been met.
220 *
221 * The following frames relate to the streams layer for a specific
222 * stream:
223 *
224 * STREAM
225 *
226 * RX Obligations:
227 * Ignore for a deleted stream.
228 *
229 * (This is different from our obligation for a
230 * locally-initiated stream ID we have not created yet,
231 * which we must treat as a protocol error. This can be
232 * distinguished via a simple monotonic counter.)
233 *
234 * TX Obligations:
235 * None, once we've decided to (someday) delete the stream.
236 *
237 * STOP_SENDING
238 *
239 * We cannot delete the stream until we have finished informing
240 * the peer that we are not going to be listening to it
241 * anymore.
242 *
243 * RX Obligations:
244 * When we delete a stream we must have already had a FIN
245 * or RESET_STREAM we transmitted acknowledged by the peer.
246 * Thus we can ignore STOP_SENDING frames for deleted
247 * streams (if they occur, they are probably just
248 * retransmissions).
249 *
250 * TX Obligations:
251 * _Acknowledged_ receipt of a STOP_SENDING frame by the
252 * peer (unless the peer's send part has already FIN'd).
253 *
254 * RESET_STREAM
255 *
256 * We cannot delete the stream until we have finished informing
257 * the peer that we are not going to be transmitting on it
258 * anymore.
259 *
260 * RX Obligations:
261 * This indicates the peer is not going to send any more
262 * data on the stream. We don't need to care about this
263 * since once a stream is marked for deletion we don't care
264 * about any data it does send. We can ignore this for
265 * deleted streams. The important criterion is that the
266 * peer has been successfully delivered our STOP_SENDING
267 * frame.
268 *
269 * TX Obligations:
270 * _Acknowledged_ receipt of a RESET_STREAM frame or FIN by
271 * the peer.
272 *
273 * MAX_STREAM_DATA
274 *
275 * RX Obligations:
276 * Ignore. Since we are not going to be sending any more
277 * data on a stream once it has been marked for deletion,
278 * we don't need to care about flow control information.
279 *
280 * TX Obligations:
281 * None.
282 *
283 * In other words, our protocol obligation is simply:
284 *
285 * - either:
286 * - the peer has acknowledged receipt of a STOP_SENDING frame sent
287 * by us; -or-
288 * - we have received a FIN and all preceding segments from the peer
289 *
290 * [NOTE: The actual criterion required here is simply 'we have
291 * received a FIN from the peer'. However, due to reordering and
292 * retransmissions we might subsequently receive non-FIN segments
293 * out of order. The FIN means we know the peer will stop
294 * transmitting on the stream at *some* point, but by sending
295 * STOP_SENDING we can avoid these needless retransmissions we
296 * will just ignore anyway. In actuality we could just handle all
297 * cases by sending a STOP_SENDING. The strategy we choose is to
298 * only avoid sending a STOP_SENDING and rely on a received FIN
299 * when we have received all preceding data, as this makes it
300 * reasonably certain no benefit would be gained by sending
301 * STOP_SENDING.]
302 *
303 * TODO(QUIC): Implement the latter case (currently we just
304 * always do STOP_SENDING).
305 *
306 * and;
307 *
308 * - we have drained our send stream (for a finished send stream)
309 * and got acknowledgement all parts of it including the FIN, or
310 * sent a RESET_STREAM frame and got acknowledgement of that frame.
311 *
312 * Once these conditions are met, we can GC the QUIC_STREAM.
313 *
314 */
315 unsigned int deleted : 1;
316 /* Set to 1 once the above conditions are actually met. */
317 unsigned int ready_for_gc : 1;
318 };
319
320 #define QUIC_STREAM_INITIATOR_CLIENT 0
321 #define QUIC_STREAM_INITIATOR_SERVER 1
322 #define QUIC_STREAM_INITIATOR_MASK 1
323
324 #define QUIC_STREAM_DIR_BIDI 0
325 #define QUIC_STREAM_DIR_UNI 2
326 #define QUIC_STREAM_DIR_MASK 2
327
328 void ossl_quic_stream_check(const QUIC_STREAM *s);
329
330 /*
331 * Returns 1 if the QUIC_STREAM was initiated by the endpoint with the server
332 * role.
333 */
334 static ossl_inline ossl_unused int ossl_quic_stream_is_server_init(const QUIC_STREAM *s)
335 {
336 return (s->type & QUIC_STREAM_INITIATOR_MASK) == QUIC_STREAM_INITIATOR_SERVER;
337 }
338
339 /*
340 * Returns 1 if the QUIC_STREAM is bidirectional and 0 if it is unidirectional.
341 */
342 static ossl_inline ossl_unused int ossl_quic_stream_is_bidi(const QUIC_STREAM *s)
343 {
344 return (s->type & QUIC_STREAM_DIR_MASK) == QUIC_STREAM_DIR_BIDI;
345 }
346
347 /* Returns 1 if the QUIC_STREAM was locally initiated. */
348 static ossl_inline ossl_unused int ossl_quic_stream_is_local_init(const QUIC_STREAM *s)
349 {
350 return ossl_quic_stream_is_server_init(s) == s->as_server;
351 }
352
353 /*
354 * Returns 1 if the QUIC_STREAM has a sending part, based on its stream type.
355 *
356 * Do NOT use (s->sstream != NULL) to test this; use this function. Note that
357 * even if this function returns 1, s->sstream might be NULL if the QUIC_SSTREAM
358 * has been deemed no longer needed, for example due to a RESET_STREAM.
359 */
360 static ossl_inline ossl_unused int ossl_quic_stream_has_send(const QUIC_STREAM *s)
361 {
362 return s->send_state != QUIC_SSTREAM_STATE_NONE;
363 }
364
365 /*
366 * Returns 1 if the QUIC_STREAM has a receiving part, based on its stream type.
367 *
368 * Do NOT use (s->rstream != NULL) to test this; use this function. Note that
369 * even if this function returns 1, s->rstream might be NULL if the QUIC_RSTREAM
370 * has been deemed no longer needed, for example if the receive stream is
371 * completely finished with.
372 */
373 static ossl_inline ossl_unused int ossl_quic_stream_has_recv(const QUIC_STREAM *s)
374 {
375 return s->recv_state != QUIC_RSTREAM_STATE_NONE;
376 }
377
378 /*
379 * Returns 1 if the QUIC_STREAM has a QUIC_SSTREAM send buffer associated with
380 * it. If this returns 1, s->sstream is guaranteed to be non-NULL. The converse
381 * is not necessarily true; erasure of a send stream buffer which is no longer
382 * required is an optimisation which the QSM may, but is not obliged, to
383 * perform.
384 *
385 * This call should be used where it is desired to do something with the send
386 * stream buffer but there is no more specific send state restriction which is
387 * applicable.
388 *
389 * Note: This does NOT indicate whether it is suitable to allow an application
390 * to append to the buffer. DATA_SENT indicates all data (including FIN) has
391 * been *sent*; the absence of DATA_SENT does not mean a FIN has not been queued
392 * (meaning no more application data can be appended). This is enforced by
393 * QUIC_SSTREAM.
394 */
395 static ossl_inline ossl_unused int ossl_quic_stream_has_send_buffer(const QUIC_STREAM *s)
396 {
397 switch (s->send_state) {
398 case QUIC_SSTREAM_STATE_READY:
399 case QUIC_SSTREAM_STATE_SEND:
400 case QUIC_SSTREAM_STATE_DATA_SENT:
401 return 1;
402 default:
403 return 0;
404 }
405 }
406
407 /*
408 * Returns 1 if the QUIC_STREAM has a sending part which is in one of the reset
409 * states.
410 */
411 static ossl_inline ossl_unused int ossl_quic_stream_send_is_reset(const QUIC_STREAM *s)
412 {
413 return s->send_state == QUIC_SSTREAM_STATE_RESET_SENT
414 || s->send_state == QUIC_SSTREAM_STATE_RESET_RECVD;
415 }
416
417 /*
418 * Returns 1 if the QUIC_STREAM has a QUIC_RSTREAM receive buffer associated
419 * with it. If this returns 1, s->rstream is guaranteed to be non-NULL. The
420 * converse is not necessarily true; erasure of a receive stream buffer which is
421 * no longer required is an optimisation which the QSM may, but is not obliged,
422 * to perform.
423 *
424 * This call should be used where it is desired to do something with the receive
425 * stream buffer but there is no more specific receive state restriction which is
426 * applicable.
427 */
428 static ossl_inline ossl_unused int ossl_quic_stream_has_recv_buffer(const QUIC_STREAM *s)
429 {
430 switch (s->recv_state) {
431 case QUIC_RSTREAM_STATE_RECV:
432 case QUIC_RSTREAM_STATE_SIZE_KNOWN:
433 case QUIC_RSTREAM_STATE_DATA_RECVD:
434 return 1;
435 default:
436 return 0;
437 }
438 }
439
440 /*
441 * Returns 1 if the QUIC_STREAM has a receiving part which is in one of the
442 * reset states.
443 */
444 static ossl_inline ossl_unused int ossl_quic_stream_recv_is_reset(const QUIC_STREAM *s)
445 {
446 return s->recv_state == QUIC_RSTREAM_STATE_RESET_RECVD
447 || s->recv_state == QUIC_RSTREAM_STATE_RESET_READ;
448 }
449
450 /*
451 * Returns 1 if the stream has a send part and that part has a final size.
452 *
453 * If final_size is non-NULL, *final_size is the final size (on success) or an
454 * undefined value otherwise.
455 */
456 static ossl_inline ossl_unused int ossl_quic_stream_send_get_final_size(const QUIC_STREAM *s,
457 uint64_t *final_size)
458 {
459 switch (s->send_state) {
460 default:
461 case QUIC_SSTREAM_STATE_NONE:
462 return 0;
463 case QUIC_SSTREAM_STATE_SEND:
464 /*
465 * SEND may or may not have had a FIN - even if we have a FIN we do not
466 * move to DATA_SENT until we have actually sent all the data. So
467 * ask the QUIC_SSTREAM.
468 */
469 return ossl_quic_sstream_get_final_size(s->sstream, final_size);
470 case QUIC_SSTREAM_STATE_DATA_SENT:
471 case QUIC_SSTREAM_STATE_DATA_RECVD:
472 case QUIC_SSTREAM_STATE_RESET_SENT:
473 case QUIC_SSTREAM_STATE_RESET_RECVD:
474 if (final_size != NULL)
475 *final_size = s->send_final_size;
476 return 1;
477 }
478 }
479
480 /*
481 * Returns 1 if the stream has a receive part and that part has a final size.
482 *
483 * If final_size is non-NULL, *final_size is the final size (on success) or an
484 * undefined value otherwise.
485 */
486 static ossl_inline ossl_unused int ossl_quic_stream_recv_get_final_size(const QUIC_STREAM *s,
487 uint64_t *final_size)
488 {
489 switch (s->recv_state) {
490 default:
491 case QUIC_RSTREAM_STATE_NONE:
492 case QUIC_RSTREAM_STATE_RECV:
493 return 0;
494
495 case QUIC_RSTREAM_STATE_SIZE_KNOWN:
496 case QUIC_RSTREAM_STATE_DATA_RECVD:
497 case QUIC_RSTREAM_STATE_DATA_READ:
498 case QUIC_RSTREAM_STATE_RESET_RECVD:
499 case QUIC_RSTREAM_STATE_RESET_READ:
500 if (!ossl_assert(ossl_quic_rxfc_get_final_size(&s->rxfc, final_size)))
501 return 0;
502
503 return 1;
504 }
505 }
506
507 /*
508 * QUIC Stream Map
509 * ===============
510 *
511 * The QUIC stream map:
512 *
513 * - maps stream IDs to QUIC_STREAM objects;
514 * - tracks which streams are 'active' (currently have data for transmission);
515 * - allows iteration over the active streams only.
516 *
517 */
518 typedef struct quic_stream_map_st {
519 LHASH_OF(QUIC_STREAM) *map;
520 QUIC_STREAM_LIST_NODE active_list;
521 QUIC_STREAM_LIST_NODE accept_list;
522 QUIC_STREAM_LIST_NODE ready_for_gc_list;
523 size_t rr_stepping, rr_counter, num_accept;
524 QUIC_STREAM *rr_cur;
525 uint64_t (*get_stream_limit_cb)(int uni, void *arg);
526 void *get_stream_limit_cb_arg;
527 QUIC_RXFC *max_streams_bidi_rxfc;
528 QUIC_RXFC *max_streams_uni_rxfc;
529 int is_server;
530 } QUIC_STREAM_MAP;
531
532 /*
533 * get_stream_limit is a callback which is called to retrieve the current stream
534 * limit for streams created by us. This mechanism is not used for
535 * peer-initiated streams. If a stream's stream ID is x, a stream is allowed if
536 * (x >> 2) < returned limit value; i.e., the returned value is exclusive.
537 *
538 * If uni is 1, get the limit for locally-initiated unidirectional streams, else
539 * get the limit for locally-initiated bidirectional streams.
540 *
541 * If the callback is NULL, stream limiting is not applied.
542 * Stream limiting is used to determine if frames can currently be produced for
543 * a stream.
544 */
545 int ossl_quic_stream_map_init(QUIC_STREAM_MAP *qsm,
546 uint64_t (*get_stream_limit_cb)(int uni, void *arg),
547 void *get_stream_limit_cb_arg,
548 QUIC_RXFC *max_streams_bidi_rxfc,
549 QUIC_RXFC *max_streams_uni_rxfc,
550 int is_server);
551
552 /*
553 * Any streams still in the map will be released as though
554 * ossl_quic_stream_map_release was called on them.
555 */
556 void ossl_quic_stream_map_cleanup(QUIC_STREAM_MAP *qsm);
557
558 /*
559 * Allocate a new stream. type is a combination of one QUIC_STREAM_INITIATOR_*
560 * value and one QUIC_STREAM_DIR_* value. Note that clients can e.g. allocate
561 * server-initiated streams as they will need to allocate a QUIC_STREAM
562 * structure to track any stream created by the server, etc.
563 *
564 * stream_id must be a valid value. Returns NULL if a stream already exists
565 * with the given ID.
566 */
567 QUIC_STREAM *ossl_quic_stream_map_alloc(QUIC_STREAM_MAP *qsm,
568 uint64_t stream_id,
569 int type);
570
571 /*
572 * Releases a stream object. Note that this must only be done once the teardown
573 * process is entirely complete and the object will never be referenced again.
574 */
575 void ossl_quic_stream_map_release(QUIC_STREAM_MAP *qsm, QUIC_STREAM *stream);
576
577 /*
578 * Calls visit_cb() for each stream in the map. visit_cb_arg is an opaque
579 * argument which is passed through.
580 */
581 void ossl_quic_stream_map_visit(QUIC_STREAM_MAP *qsm,
582 void (*visit_cb)(QUIC_STREAM *stream, void *arg),
583 void *visit_cb_arg);
584
585 /*
586 * Retrieves a stream by stream ID. Returns NULL if it does not exist.
587 */
588 QUIC_STREAM *ossl_quic_stream_map_get_by_id(QUIC_STREAM_MAP *qsm,
589 uint64_t stream_id);
590
591 /*
592 * Marks the given stream as active or inactive based on its state. Idempotent.
593 *
594 * When a stream is marked active, it becomes available in the iteration list,
595 * and when a stream is marked inactive, it no longer appears in the iteration
596 * list.
597 *
598 * Calling this function invalidates any iterator currently pointing at the
599 * given stream object, but iterators not currently pointing at the given stream
600 * object are not invalidated.
601 */
602 void ossl_quic_stream_map_update_state(QUIC_STREAM_MAP *qsm, QUIC_STREAM *s);
603
604 /*
605 * Sets the RR stepping value, n. The RR rotation will be advanced every n
606 * packets. The default value is 1.
607 */
608 void ossl_quic_stream_map_set_rr_stepping(QUIC_STREAM_MAP *qsm, size_t stepping);
609
610
611 /*
612 * Stream Send Part
613 * ================
614 */
615
616 /*
617 * Ensures that the sending part has transitioned out of the READY state (i.e.,
618 * to SEND, or a subsequent state). This function is named as it is because,
619 * while on paper the distinction between READY and SEND is whether we have
620 * started transmitting application data, in practice the meaningful distinction
621 * between the two states is whether we have allocated a stream ID to the stream
622 * or not. QUIC permits us to defer stream ID allocation until first STREAM (or
623 * STREAM_DATA_BLOCKED) frame transmission for locally-initiated streams.
624 *
625 * Our implementation does not currently do this and we allocate stream IDs up
626 * front, however we may revisit this in the future. Calling this ensures
627 * represents a demand for a stream ID by the caller and ensures one has been
628 * allocated to the stream, and causes us to transition to SEND if we are still
629 * in the READY state.
630 *
631 * Returns 0 if there is no send part (caller error) and 1 otherwise.
632 */
633 int ossl_quic_stream_map_ensure_send_part_id(QUIC_STREAM_MAP *qsm,
634 QUIC_STREAM *qs);
635
636 /*
637 * Transitions from READY or SEND to the DATA_SENT state. Note that this is NOT
638 * the same as the point in time at which the final size of the stream becomes
639 * known (i.e., the time at which ossl_quic_sstream_fin()) is called as it
640 * occurs when we have SENT all data on a given stream send part, not merely
641 * buffered it.
642 *
643 * Returns 1 if the state transition was successfully taken. Returns 0 if there
644 * is no send part (caller error) or if the state transition cannot be taken
645 * because the send part is not in the SEND state.
646 */
647 int ossl_quic_stream_map_notify_all_data_sent(QUIC_STREAM_MAP *qsm,
648 QUIC_STREAM *qs);
649
650 /*
651 * Transitions from the DATA_SENT to DATA_RECVD state; should be called
652 * when all transmitted stream data is ACKed by the peer.
653 *
654 * Returns 1 if the state transition was successfully taken, or if the send part
655 * was already in the DATA_RECVD state. Returns 0 if there is no send part
656 * (caller error) or the state transition cannot be taken because the send part
657 * is not in the DATA_SENT or DATA_RECVD states. Because
658 * ossl_quic_stream_map_fin_send_part() should always be called prior to this
659 * function, the send state must already be in DATA_SENT in order for this
660 * function to succeed.
661 */
662 int ossl_quic_stream_map_notify_totally_acked(QUIC_STREAM_MAP *qsm,
663 QUIC_STREAM *qs);
664
665 /*
666 * Resets the sending part of a stream. This is a transition from the READY,
667 * SEND or DATA_SENT send stream states to the RESET_SENT state.
668 *
669 * This function returns 1 if the transition is taken (i.e., if the send stream
670 * part was in one of the states above), or if it is already in the RESET_SENT
671 * state (idempotent operation), or if it has reached the RESET_RECVD state.
672 *
673 * It returns 0 if in the DATA_RECVD state, as a send stream cannot be reset
674 * in this state. It also returns 0 if there is no send part (caller error).
675 */
676 int ossl_quic_stream_map_reset_stream_send_part(QUIC_STREAM_MAP *qsm,
677 QUIC_STREAM *qs,
678 uint64_t aec);
679
680 /*
681 * Transitions from the RESET_SENT to the RESET_RECVD state. This should be
682 * called when a sent RESET_STREAM frame has been acknowledged by the peer.
683 *
684 * This function returns 1 if the transition is taken (i.e., if the send stream
685 * part was in one of the states above) or if it is already in the RESET_RECVD
686 * state (idempotent operation).
687 *
688 * It returns 0 if not in the RESET_SENT state, as this function should only be
689 * called after we have already sent a RESET_STREAM frame and entered the
690 * RESET_SENT state. It also returns 0 if there is no send part (caller error).
691 */
692 int ossl_quic_stream_map_notify_reset_stream_acked(QUIC_STREAM_MAP *qsm,
693 QUIC_STREAM *qs);
694
695
696 /*
697 * Stream Receive Part
698 * ===================
699 */
700
701 /*
702 * Transitions from the RECV to SIZE_KNOWN receive stream states. This should be
703 * called once a STREAM frame is received for the stream with the FIN bit set.
704 * final_size should be the final size of the stream in bytes.
705 *
706 * Returns 1 if the transition was taken.
707 */
708 int ossl_quic_stream_map_notify_size_known_recv_part(QUIC_STREAM_MAP *qsm,
709 QUIC_STREAM *qs,
710 uint64_t final_size);
711
712 /* SIZE_KNOWN -> DATA_RECVD */
713 int ossl_quic_stream_map_notify_totally_received(QUIC_STREAM_MAP *qsm,
714 QUIC_STREAM *qs);
715
716 /* DATA_RECVD -> DATA_READ */
717 int ossl_quic_stream_map_notify_totally_read(QUIC_STREAM_MAP *qsm,
718 QUIC_STREAM *qs);
719
720 /* RECV/SIZE_KNOWN/DATA_RECVD -> RESET_RECVD */
721 int ossl_quic_stream_map_notify_reset_recv_part(QUIC_STREAM_MAP *qsm,
722 QUIC_STREAM *qs,
723 uint64_t app_error_code);
724
725 /* RESET_RECVD -> RESET_READ */
726 int ossl_quic_stream_map_notify_app_read_reset_recv_part(QUIC_STREAM_MAP *qsm,
727 QUIC_STREAM *qs);
728
729 /*
730 * Marks the receiving part of a stream for STOP_SENDING. This is orthogonal to
731 * receive stream state as it does not affect it directly.
732 *
733 * Returns 1 if the receiving part of a stream was not already marked for
734 * STOP_SENDING.
735 * Returns 0 otherwise, which need not be considered an error.
736 */
737 int ossl_quic_stream_map_stop_sending_recv_part(QUIC_STREAM_MAP *qsm,
738 QUIC_STREAM *qs,
739 uint64_t aec);
740
741 /*
742 * Marks the stream as wanting a STOP_SENDING frame transmitted. It is not valid
743 * to vall this if ossl_quic_stream_map_stop_sending_recv_part() has not been
744 * called. For TXP use.
745 */
746 int ossl_quic_stream_map_schedule_stop_sending(QUIC_STREAM_MAP *qsm,
747 QUIC_STREAM *qs);
748
749
750 /*
751 * Accept Queue Management
752 * =======================
753 */
754
755 /*
756 * Adds a stream to the accept queue.
757 */
758 void ossl_quic_stream_map_push_accept_queue(QUIC_STREAM_MAP *qsm,
759 QUIC_STREAM *s);
760
761 /*
762 * Returns the next item to be popped from the accept queue, or NULL if it is
763 * empty.
764 */
765 QUIC_STREAM *ossl_quic_stream_map_peek_accept_queue(QUIC_STREAM_MAP *qsm);
766
767 /*
768 * Removes a stream from the accept queue. rtt is the estimated connection RTT.
769 * The stream is retired for the purposes of MAX_STREAMS RXFC.
770 *
771 * Precondition: s is in the accept queue.
772 */
773 void ossl_quic_stream_map_remove_from_accept_queue(QUIC_STREAM_MAP *qsm,
774 QUIC_STREAM *s,
775 OSSL_TIME rtt);
776
777 /* Returns the length of the accept queue. */
778 size_t ossl_quic_stream_map_get_accept_queue_len(QUIC_STREAM_MAP *qsm);
779
780 /*
781 * Delete streams ready for GC. Pointers to those QUIC_STREAM objects become
782 * invalid.
783 */
784 void ossl_quic_stream_map_gc(QUIC_STREAM_MAP *qsm);
785
786 /*
787 * QUIC Stream Iterator
788 * ====================
789 *
790 * Allows the current set of active streams to be walked using a RR-based
791 * algorithm. Each time ossl_quic_stream_iter_init is called, the RR algorithm
792 * is stepped. The RR algorithm rotates the iteration order such that the next
793 * active stream is returned first after n calls to ossl_quic_stream_iter_init,
794 * where n is the stepping value configured via
795 * ossl_quic_stream_map_set_rr_stepping.
796 *
797 * Suppose there are three active streams and the configured stepping is n:
798 *
799 * Iteration 0n: [Stream 1] [Stream 2] [Stream 3]
800 * Iteration 1n: [Stream 2] [Stream 3] [Stream 1]
801 * Iteration 2n: [Stream 3] [Stream 1] [Stream 2]
802 *
803 */
804 typedef struct quic_stream_iter_st {
805 QUIC_STREAM_MAP *qsm;
806 QUIC_STREAM *first_stream, *stream;
807 } QUIC_STREAM_ITER;
808
809 /*
810 * Initialise an iterator, advancing the RR algorithm as necessary (if
811 * advance_rr is 1). After calling this, it->stream will be the first stream in
812 * the iteration sequence, or NULL if there are no active streams.
813 */
814 void ossl_quic_stream_iter_init(QUIC_STREAM_ITER *it, QUIC_STREAM_MAP *qsm,
815 int advance_rr);
816
817 /*
818 * Advances to next stream in iteration sequence. You do not need to call this
819 * immediately after calling ossl_quic_stream_iter_init(). If the end of the
820 * list is reached, it->stream will be NULL after calling this.
821 */
822 void ossl_quic_stream_iter_next(QUIC_STREAM_ITER *it);
823
824 # endif
825
826 #endif