quic_stream_map.h 35 KB

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