2
0

quic_stream_map.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /*
  2. * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/quic_stream_map.h"
  10. #include "internal/nelem.h"
  11. /*
  12. * QUIC Stream Map
  13. * ===============
  14. */
  15. DEFINE_LHASH_OF_EX(QUIC_STREAM);
  16. static void shutdown_flush_done(QUIC_STREAM_MAP *qsm, QUIC_STREAM *qs);
  17. /* Circular list management. */
  18. static void list_insert_tail(QUIC_STREAM_LIST_NODE *l,
  19. QUIC_STREAM_LIST_NODE *n)
  20. {
  21. /* Must not be in list. */
  22. assert(n->prev == NULL && n->next == NULL
  23. && l->prev != NULL && l->next != NULL);
  24. n->prev = l->prev;
  25. n->prev->next = n;
  26. l->prev = n;
  27. n->next = l;
  28. }
  29. static void list_remove(QUIC_STREAM_LIST_NODE *l,
  30. QUIC_STREAM_LIST_NODE *n)
  31. {
  32. assert(n->prev != NULL && n->next != NULL
  33. && n->prev != n && n->next != n);
  34. n->prev->next = n->next;
  35. n->next->prev = n->prev;
  36. n->next = n->prev = NULL;
  37. }
  38. static QUIC_STREAM *list_next(QUIC_STREAM_LIST_NODE *l, QUIC_STREAM_LIST_NODE *n,
  39. size_t off)
  40. {
  41. assert(n->prev != NULL && n->next != NULL
  42. && (n == l || (n->prev != n && n->next != n))
  43. && l->prev != NULL && l->next != NULL);
  44. n = n->next;
  45. if (n == l)
  46. n = n->next;
  47. if (n == l)
  48. return NULL;
  49. assert(n != NULL);
  50. return (QUIC_STREAM *)(((char *)n) - off);
  51. }
  52. #define active_next(l, s) list_next((l), &(s)->active_node, \
  53. offsetof(QUIC_STREAM, active_node))
  54. #define accept_next(l, s) list_next((l), &(s)->accept_node, \
  55. offsetof(QUIC_STREAM, accept_node))
  56. #define ready_for_gc_next(l, s) list_next((l), &(s)->ready_for_gc_node, \
  57. offsetof(QUIC_STREAM, ready_for_gc_node))
  58. #define accept_head(l) list_next((l), (l), \
  59. offsetof(QUIC_STREAM, accept_node))
  60. #define ready_for_gc_head(l) list_next((l), (l), \
  61. offsetof(QUIC_STREAM, ready_for_gc_node))
  62. static unsigned long hash_stream(const QUIC_STREAM *s)
  63. {
  64. return (unsigned long)s->id;
  65. }
  66. static int cmp_stream(const QUIC_STREAM *a, const QUIC_STREAM *b)
  67. {
  68. if (a->id < b->id)
  69. return -1;
  70. if (a->id > b->id)
  71. return 1;
  72. return 0;
  73. }
  74. int ossl_quic_stream_map_init(QUIC_STREAM_MAP *qsm,
  75. uint64_t (*get_stream_limit_cb)(int uni, void *arg),
  76. void *get_stream_limit_cb_arg,
  77. QUIC_RXFC *max_streams_bidi_rxfc,
  78. QUIC_RXFC *max_streams_uni_rxfc,
  79. int is_server)
  80. {
  81. qsm->map = lh_QUIC_STREAM_new(hash_stream, cmp_stream);
  82. qsm->active_list.prev = qsm->active_list.next = &qsm->active_list;
  83. qsm->accept_list.prev = qsm->accept_list.next = &qsm->accept_list;
  84. qsm->ready_for_gc_list.prev = qsm->ready_for_gc_list.next
  85. = &qsm->ready_for_gc_list;
  86. qsm->rr_stepping = 1;
  87. qsm->rr_counter = 0;
  88. qsm->rr_cur = NULL;
  89. qsm->num_accept = 0;
  90. qsm->num_shutdown_flush = 0;
  91. qsm->get_stream_limit_cb = get_stream_limit_cb;
  92. qsm->get_stream_limit_cb_arg = get_stream_limit_cb_arg;
  93. qsm->max_streams_bidi_rxfc = max_streams_bidi_rxfc;
  94. qsm->max_streams_uni_rxfc = max_streams_uni_rxfc;
  95. qsm->is_server = is_server;
  96. return 1;
  97. }
  98. static void release_each(QUIC_STREAM *stream, void *arg)
  99. {
  100. QUIC_STREAM_MAP *qsm = arg;
  101. ossl_quic_stream_map_release(qsm, stream);
  102. }
  103. void ossl_quic_stream_map_cleanup(QUIC_STREAM_MAP *qsm)
  104. {
  105. ossl_quic_stream_map_visit(qsm, release_each, qsm);
  106. lh_QUIC_STREAM_free(qsm->map);
  107. qsm->map = NULL;
  108. }
  109. void ossl_quic_stream_map_visit(QUIC_STREAM_MAP *qsm,
  110. void (*visit_cb)(QUIC_STREAM *stream, void *arg),
  111. void *visit_cb_arg)
  112. {
  113. lh_QUIC_STREAM_doall_arg(qsm->map, visit_cb, visit_cb_arg);
  114. }
  115. QUIC_STREAM *ossl_quic_stream_map_alloc(QUIC_STREAM_MAP *qsm,
  116. uint64_t stream_id,
  117. int type)
  118. {
  119. QUIC_STREAM *s;
  120. QUIC_STREAM key;
  121. key.id = stream_id;
  122. s = lh_QUIC_STREAM_retrieve(qsm->map, &key);
  123. if (s != NULL)
  124. return NULL;
  125. s = OPENSSL_zalloc(sizeof(*s));
  126. if (s == NULL)
  127. return NULL;
  128. s->id = stream_id;
  129. s->type = type;
  130. s->as_server = qsm->is_server;
  131. s->send_state = (ossl_quic_stream_is_local_init(s)
  132. || ossl_quic_stream_is_bidi(s))
  133. ? QUIC_SSTREAM_STATE_READY
  134. : QUIC_SSTREAM_STATE_NONE;
  135. s->recv_state = (!ossl_quic_stream_is_local_init(s)
  136. || ossl_quic_stream_is_bidi(s))
  137. ? QUIC_RSTREAM_STATE_RECV
  138. : QUIC_RSTREAM_STATE_NONE;
  139. s->send_final_size = UINT64_MAX;
  140. lh_QUIC_STREAM_insert(qsm->map, s);
  141. return s;
  142. }
  143. void ossl_quic_stream_map_release(QUIC_STREAM_MAP *qsm, QUIC_STREAM *stream)
  144. {
  145. if (stream == NULL)
  146. return;
  147. if (stream->active_node.next != NULL)
  148. list_remove(&qsm->active_list, &stream->active_node);
  149. if (stream->accept_node.next != NULL)
  150. list_remove(&qsm->accept_list, &stream->accept_node);
  151. if (stream->ready_for_gc_node.next != NULL)
  152. list_remove(&qsm->ready_for_gc_list, &stream->ready_for_gc_node);
  153. ossl_quic_sstream_free(stream->sstream);
  154. stream->sstream = NULL;
  155. ossl_quic_rstream_free(stream->rstream);
  156. stream->rstream = NULL;
  157. lh_QUIC_STREAM_delete(qsm->map, stream);
  158. OPENSSL_free(stream);
  159. }
  160. QUIC_STREAM *ossl_quic_stream_map_get_by_id(QUIC_STREAM_MAP *qsm,
  161. uint64_t stream_id)
  162. {
  163. QUIC_STREAM key;
  164. key.id = stream_id;
  165. return lh_QUIC_STREAM_retrieve(qsm->map, &key);
  166. }
  167. static void stream_map_mark_active(QUIC_STREAM_MAP *qsm, QUIC_STREAM *s)
  168. {
  169. if (s->active)
  170. return;
  171. list_insert_tail(&qsm->active_list, &s->active_node);
  172. if (qsm->rr_cur == NULL)
  173. qsm->rr_cur = s;
  174. s->active = 1;
  175. }
  176. static void stream_map_mark_inactive(QUIC_STREAM_MAP *qsm, QUIC_STREAM *s)
  177. {
  178. if (!s->active)
  179. return;
  180. if (qsm->rr_cur == s)
  181. qsm->rr_cur = active_next(&qsm->active_list, s);
  182. if (qsm->rr_cur == s)
  183. qsm->rr_cur = NULL;
  184. list_remove(&qsm->active_list, &s->active_node);
  185. s->active = 0;
  186. }
  187. void ossl_quic_stream_map_set_rr_stepping(QUIC_STREAM_MAP *qsm, size_t stepping)
  188. {
  189. qsm->rr_stepping = stepping;
  190. qsm->rr_counter = 0;
  191. }
  192. static int stream_has_data_to_send(QUIC_STREAM *s)
  193. {
  194. OSSL_QUIC_FRAME_STREAM shdr;
  195. OSSL_QTX_IOVEC iov[2];
  196. size_t num_iov;
  197. uint64_t fc_credit, fc_swm, fc_limit;
  198. switch (s->send_state) {
  199. case QUIC_SSTREAM_STATE_READY:
  200. case QUIC_SSTREAM_STATE_SEND:
  201. case QUIC_SSTREAM_STATE_DATA_SENT:
  202. /*
  203. * We can still have data to send in DATA_SENT due to retransmissions,
  204. * etc.
  205. */
  206. break;
  207. default:
  208. return 0; /* Nothing to send. */
  209. }
  210. /*
  211. * We cannot determine if we have data to send simply by checking if
  212. * ossl_quic_txfc_get_credit() is zero, because we may also have older
  213. * stream data we need to retransmit. The SSTREAM returns older data first,
  214. * so we do a simple comparison of the next chunk the SSTREAM wants to send
  215. * against the TXFC CWM.
  216. */
  217. num_iov = OSSL_NELEM(iov);
  218. if (!ossl_quic_sstream_get_stream_frame(s->sstream, 0, &shdr, iov,
  219. &num_iov))
  220. return 0;
  221. fc_credit = ossl_quic_txfc_get_credit(&s->txfc, 0);
  222. fc_swm = ossl_quic_txfc_get_swm(&s->txfc);
  223. fc_limit = fc_swm + fc_credit;
  224. return (shdr.is_fin && shdr.len == 0) || shdr.offset < fc_limit;
  225. }
  226. static ossl_unused int qsm_send_part_permits_gc(const QUIC_STREAM *qs)
  227. {
  228. switch (qs->send_state) {
  229. case QUIC_SSTREAM_STATE_NONE:
  230. case QUIC_SSTREAM_STATE_DATA_RECVD:
  231. case QUIC_SSTREAM_STATE_RESET_RECVD:
  232. return 1;
  233. default:
  234. return 0;
  235. }
  236. }
  237. static int qsm_ready_for_gc(QUIC_STREAM_MAP *qsm, QUIC_STREAM *qs)
  238. {
  239. int recv_stream_fully_drained = 0; /* TODO(QUIC FUTURE): Optimisation */
  240. /*
  241. * If sstream has no FIN, we auto-reset it at marked-for-deletion time, so
  242. * we don't need to worry about that here.
  243. */
  244. assert(!qs->deleted
  245. || !ossl_quic_stream_has_send(qs)
  246. || ossl_quic_stream_send_is_reset(qs)
  247. || ossl_quic_stream_send_get_final_size(qs, NULL));
  248. return
  249. qs->deleted
  250. && (!ossl_quic_stream_has_recv(qs)
  251. || recv_stream_fully_drained
  252. || qs->acked_stop_sending)
  253. && (!ossl_quic_stream_has_send(qs)
  254. || qs->send_state == QUIC_SSTREAM_STATE_DATA_RECVD
  255. || qs->send_state == QUIC_SSTREAM_STATE_RESET_RECVD);
  256. }
  257. int ossl_quic_stream_map_is_local_allowed_by_stream_limit(QUIC_STREAM_MAP *qsm,
  258. uint64_t stream_ordinal,
  259. int is_uni)
  260. {
  261. uint64_t stream_limit;
  262. if (qsm->get_stream_limit_cb == NULL)
  263. return 1;
  264. stream_limit = qsm->get_stream_limit_cb(is_uni, qsm->get_stream_limit_cb_arg);
  265. return stream_ordinal < stream_limit;
  266. }
  267. void ossl_quic_stream_map_update_state(QUIC_STREAM_MAP *qsm, QUIC_STREAM *s)
  268. {
  269. int should_be_active, allowed_by_stream_limit = 1;
  270. if (ossl_quic_stream_is_server_init(s) == qsm->is_server) {
  271. int is_uni = !ossl_quic_stream_is_bidi(s);
  272. uint64_t stream_ordinal = s->id >> 2;
  273. allowed_by_stream_limit
  274. = ossl_quic_stream_map_is_local_allowed_by_stream_limit(qsm,
  275. stream_ordinal,
  276. is_uni);
  277. }
  278. if (s->send_state == QUIC_SSTREAM_STATE_DATA_SENT
  279. && ossl_quic_sstream_is_totally_acked(s->sstream))
  280. ossl_quic_stream_map_notify_totally_acked(qsm, s);
  281. else if (s->shutdown_flush
  282. && s->send_state == QUIC_SSTREAM_STATE_SEND
  283. && ossl_quic_sstream_is_totally_acked(s->sstream))
  284. shutdown_flush_done(qsm, s);
  285. if (!s->ready_for_gc) {
  286. s->ready_for_gc = qsm_ready_for_gc(qsm, s);
  287. if (s->ready_for_gc)
  288. list_insert_tail(&qsm->ready_for_gc_list, &s->ready_for_gc_node);
  289. }
  290. should_be_active
  291. = allowed_by_stream_limit
  292. && !s->ready_for_gc
  293. && ((ossl_quic_stream_has_recv(s)
  294. && !ossl_quic_stream_recv_is_reset(s)
  295. && (s->recv_state == QUIC_RSTREAM_STATE_RECV
  296. && (s->want_max_stream_data
  297. || ossl_quic_rxfc_has_cwm_changed(&s->rxfc, 0))))
  298. || s->want_stop_sending
  299. || s->want_reset_stream
  300. || (!s->peer_stop_sending && stream_has_data_to_send(s)));
  301. if (should_be_active)
  302. stream_map_mark_active(qsm, s);
  303. else
  304. stream_map_mark_inactive(qsm, s);
  305. }
  306. /*
  307. * Stream Send Part State Management
  308. * =================================
  309. */
  310. int ossl_quic_stream_map_ensure_send_part_id(QUIC_STREAM_MAP *qsm,
  311. QUIC_STREAM *qs)
  312. {
  313. switch (qs->send_state) {
  314. case QUIC_SSTREAM_STATE_NONE:
  315. /* Stream without send part - caller error. */
  316. return 0;
  317. case QUIC_SSTREAM_STATE_READY:
  318. /*
  319. * We always allocate a stream ID upfront, so we don't need to do it
  320. * here.
  321. */
  322. qs->send_state = QUIC_SSTREAM_STATE_SEND;
  323. return 1;
  324. default:
  325. /* Nothing to do. */
  326. return 1;
  327. }
  328. }
  329. int ossl_quic_stream_map_notify_all_data_sent(QUIC_STREAM_MAP *qsm,
  330. QUIC_STREAM *qs)
  331. {
  332. switch (qs->send_state) {
  333. default:
  334. /* Wrong state - caller error. */
  335. case QUIC_SSTREAM_STATE_NONE:
  336. /* Stream without send part - caller error. */
  337. return 0;
  338. case QUIC_SSTREAM_STATE_SEND:
  339. if (!ossl_quic_sstream_get_final_size(qs->sstream, &qs->send_final_size))
  340. return 0;
  341. qs->send_state = QUIC_SSTREAM_STATE_DATA_SENT;
  342. return 1;
  343. }
  344. }
  345. static void shutdown_flush_done(QUIC_STREAM_MAP *qsm, QUIC_STREAM *qs)
  346. {
  347. if (!qs->shutdown_flush)
  348. return;
  349. assert(qsm->num_shutdown_flush > 0);
  350. qs->shutdown_flush = 0;
  351. --qsm->num_shutdown_flush;
  352. }
  353. int ossl_quic_stream_map_notify_totally_acked(QUIC_STREAM_MAP *qsm,
  354. QUIC_STREAM *qs)
  355. {
  356. switch (qs->send_state) {
  357. default:
  358. /* Wrong state - caller error. */
  359. case QUIC_SSTREAM_STATE_NONE:
  360. /* Stream without send part - caller error. */
  361. return 0;
  362. case QUIC_SSTREAM_STATE_DATA_SENT:
  363. qs->send_state = QUIC_SSTREAM_STATE_DATA_RECVD;
  364. /* We no longer need a QUIC_SSTREAM in this state. */
  365. ossl_quic_sstream_free(qs->sstream);
  366. qs->sstream = NULL;
  367. shutdown_flush_done(qsm, qs);
  368. return 1;
  369. }
  370. }
  371. int ossl_quic_stream_map_reset_stream_send_part(QUIC_STREAM_MAP *qsm,
  372. QUIC_STREAM *qs,
  373. uint64_t aec)
  374. {
  375. switch (qs->send_state) {
  376. default:
  377. case QUIC_SSTREAM_STATE_NONE:
  378. /*
  379. * RESET_STREAM pertains to sending part only, so we cannot reset a
  380. * receive-only stream.
  381. */
  382. case QUIC_SSTREAM_STATE_DATA_RECVD:
  383. /*
  384. * RFC 9000 s. 3.3: A sender MUST NOT [...] send RESET_STREAM from a
  385. * terminal state. If the stream has already finished normally and the
  386. * peer has acknowledged this, we cannot reset it.
  387. */
  388. return 0;
  389. case QUIC_SSTREAM_STATE_READY:
  390. if (!ossl_quic_stream_map_ensure_send_part_id(qsm, qs))
  391. return 0;
  392. /* FALLTHROUGH */
  393. case QUIC_SSTREAM_STATE_SEND:
  394. /*
  395. * If we already have a final size (e.g. because we are coming from
  396. * DATA_SENT), we have to be consistent with that, so don't change it.
  397. * If we don't already have a final size, determine a final size value.
  398. * This is the value which we will end up using for a RESET_STREAM frame
  399. * for flow control purposes. We could send the stream size (total
  400. * number of bytes appended to QUIC_SSTREAM by the application), but it
  401. * is in our interest to exclude any bytes we have not actually
  402. * transmitted yet, to avoid unnecessarily consuming flow control
  403. * credit. We can get this from the TXFC.
  404. */
  405. qs->send_final_size = ossl_quic_txfc_get_swm(&qs->txfc);
  406. /* FALLTHROUGH */
  407. case QUIC_SSTREAM_STATE_DATA_SENT:
  408. qs->reset_stream_aec = aec;
  409. qs->want_reset_stream = 1;
  410. qs->send_state = QUIC_SSTREAM_STATE_RESET_SENT;
  411. ossl_quic_sstream_free(qs->sstream);
  412. qs->sstream = NULL;
  413. shutdown_flush_done(qsm, qs);
  414. ossl_quic_stream_map_update_state(qsm, qs);
  415. return 1;
  416. case QUIC_SSTREAM_STATE_RESET_SENT:
  417. case QUIC_SSTREAM_STATE_RESET_RECVD:
  418. /*
  419. * Idempotent - no-op. In any case, do not send RESET_STREAM again - as
  420. * mentioned, we must not send it from a terminal state.
  421. */
  422. return 1;
  423. }
  424. }
  425. int ossl_quic_stream_map_notify_reset_stream_acked(QUIC_STREAM_MAP *qsm,
  426. QUIC_STREAM *qs)
  427. {
  428. switch (qs->send_state) {
  429. default:
  430. /* Wrong state - caller error. */
  431. case QUIC_SSTREAM_STATE_NONE:
  432. /* Stream without send part - caller error. */
  433. return 0;
  434. case QUIC_SSTREAM_STATE_RESET_SENT:
  435. qs->send_state = QUIC_SSTREAM_STATE_RESET_RECVD;
  436. return 1;
  437. case QUIC_SSTREAM_STATE_RESET_RECVD:
  438. /* Already in the correct state. */
  439. return 1;
  440. }
  441. }
  442. /*
  443. * Stream Receive Part State Management
  444. * ====================================
  445. */
  446. int ossl_quic_stream_map_notify_size_known_recv_part(QUIC_STREAM_MAP *qsm,
  447. QUIC_STREAM *qs,
  448. uint64_t final_size)
  449. {
  450. switch (qs->recv_state) {
  451. default:
  452. /* Wrong state - caller error. */
  453. case QUIC_RSTREAM_STATE_NONE:
  454. /* Stream without receive part - caller error. */
  455. return 0;
  456. case QUIC_RSTREAM_STATE_RECV:
  457. qs->recv_state = QUIC_RSTREAM_STATE_SIZE_KNOWN;
  458. return 1;
  459. }
  460. }
  461. int ossl_quic_stream_map_notify_totally_received(QUIC_STREAM_MAP *qsm,
  462. QUIC_STREAM *qs)
  463. {
  464. switch (qs->recv_state) {
  465. default:
  466. /* Wrong state - caller error. */
  467. case QUIC_RSTREAM_STATE_NONE:
  468. /* Stream without receive part - caller error. */
  469. return 0;
  470. case QUIC_RSTREAM_STATE_SIZE_KNOWN:
  471. qs->recv_state = QUIC_RSTREAM_STATE_DATA_RECVD;
  472. qs->want_stop_sending = 0;
  473. return 1;
  474. }
  475. }
  476. int ossl_quic_stream_map_notify_totally_read(QUIC_STREAM_MAP *qsm,
  477. QUIC_STREAM *qs)
  478. {
  479. switch (qs->recv_state) {
  480. default:
  481. /* Wrong state - caller error. */
  482. case QUIC_RSTREAM_STATE_NONE:
  483. /* Stream without receive part - caller error. */
  484. return 0;
  485. case QUIC_RSTREAM_STATE_DATA_RECVD:
  486. qs->recv_state = QUIC_RSTREAM_STATE_DATA_READ;
  487. /* QUIC_RSTREAM is no longer needed */
  488. ossl_quic_rstream_free(qs->rstream);
  489. qs->rstream = NULL;
  490. return 1;
  491. }
  492. }
  493. int ossl_quic_stream_map_notify_reset_recv_part(QUIC_STREAM_MAP *qsm,
  494. QUIC_STREAM *qs,
  495. uint64_t app_error_code,
  496. uint64_t final_size)
  497. {
  498. uint64_t prev_final_size;
  499. switch (qs->recv_state) {
  500. default:
  501. case QUIC_RSTREAM_STATE_NONE:
  502. /* Stream without receive part - caller error. */
  503. return 0;
  504. case QUIC_RSTREAM_STATE_RECV:
  505. case QUIC_RSTREAM_STATE_SIZE_KNOWN:
  506. case QUIC_RSTREAM_STATE_DATA_RECVD:
  507. if (ossl_quic_stream_recv_get_final_size(qs, &prev_final_size)
  508. && prev_final_size != final_size)
  509. /* Cannot change previous final size. */
  510. return 0;
  511. qs->recv_state = QUIC_RSTREAM_STATE_RESET_RECVD;
  512. qs->peer_reset_stream_aec = app_error_code;
  513. /* RFC 9000 s. 3.3: No point sending STOP_SENDING if already reset. */
  514. qs->want_stop_sending = 0;
  515. /* QUIC_RSTREAM is no longer needed */
  516. ossl_quic_rstream_free(qs->rstream);
  517. qs->rstream = NULL;
  518. ossl_quic_stream_map_update_state(qsm, qs);
  519. return 1;
  520. case QUIC_RSTREAM_STATE_DATA_READ:
  521. /*
  522. * If we already retired the FIN to the application this is moot
  523. * - just ignore.
  524. */
  525. case QUIC_RSTREAM_STATE_RESET_RECVD:
  526. case QUIC_RSTREAM_STATE_RESET_READ:
  527. /* Could be a reordered/retransmitted frame - just ignore. */
  528. return 1;
  529. }
  530. }
  531. int ossl_quic_stream_map_notify_app_read_reset_recv_part(QUIC_STREAM_MAP *qsm,
  532. QUIC_STREAM *qs)
  533. {
  534. switch (qs->recv_state) {
  535. default:
  536. /* Wrong state - caller error. */
  537. case QUIC_RSTREAM_STATE_NONE:
  538. /* Stream without receive part - caller error. */
  539. return 0;
  540. case QUIC_RSTREAM_STATE_RESET_RECVD:
  541. qs->recv_state = QUIC_RSTREAM_STATE_RESET_READ;
  542. return 1;
  543. }
  544. }
  545. int ossl_quic_stream_map_stop_sending_recv_part(QUIC_STREAM_MAP *qsm,
  546. QUIC_STREAM *qs,
  547. uint64_t aec)
  548. {
  549. if (qs->stop_sending)
  550. return 0;
  551. switch (qs->recv_state) {
  552. default:
  553. case QUIC_RSTREAM_STATE_NONE:
  554. /* Send-only stream, so this makes no sense. */
  555. case QUIC_RSTREAM_STATE_DATA_RECVD:
  556. case QUIC_RSTREAM_STATE_DATA_READ:
  557. /*
  558. * Not really any point in STOP_SENDING if we already received all data.
  559. */
  560. case QUIC_RSTREAM_STATE_RESET_RECVD:
  561. case QUIC_RSTREAM_STATE_RESET_READ:
  562. /*
  563. * RFC 9000 s. 3.5: "STOP_SENDING SHOULD only be sent for a stream that
  564. * has not been reset by the peer."
  565. *
  566. * No point in STOP_SENDING if the peer already reset their send part.
  567. */
  568. return 0;
  569. case QUIC_RSTREAM_STATE_RECV:
  570. case QUIC_RSTREAM_STATE_SIZE_KNOWN:
  571. /*
  572. * RFC 9000 s. 3.5: "If the stream is in the Recv or Size Known state,
  573. * the transport SHOULD signal this by sending a STOP_SENDING frame to
  574. * prompt closure of the stream in the opposite direction."
  575. *
  576. * Note that it does make sense to send STOP_SENDING for a receive part
  577. * of a stream which has a known size (because we have received a FIN)
  578. * but which still has other (previous) stream data yet to be received.
  579. */
  580. break;
  581. }
  582. qs->stop_sending = 1;
  583. qs->stop_sending_aec = aec;
  584. return ossl_quic_stream_map_schedule_stop_sending(qsm, qs);
  585. }
  586. /* Called to mark STOP_SENDING for generation, or regeneration after loss. */
  587. int ossl_quic_stream_map_schedule_stop_sending(QUIC_STREAM_MAP *qsm, QUIC_STREAM *qs)
  588. {
  589. if (!qs->stop_sending)
  590. return 0;
  591. /*
  592. * Ignore the call as a no-op if already scheduled, or in a state
  593. * where it makes no sense to send STOP_SENDING.
  594. */
  595. if (qs->want_stop_sending)
  596. return 1;
  597. switch (qs->recv_state) {
  598. default:
  599. return 1; /* ignore */
  600. case QUIC_RSTREAM_STATE_RECV:
  601. case QUIC_RSTREAM_STATE_SIZE_KNOWN:
  602. /*
  603. * RFC 9000 s. 3.5: "An endpoint is expected to send another
  604. * STOP_SENDING frame if a packet containing a previous STOP_SENDING is
  605. * lost. However, once either all stream data or a RESET_STREAM frame
  606. * has been received for the stream -- that is, the stream is in any
  607. * state other than "Recv" or "Size Known" -- sending a STOP_SENDING
  608. * frame is unnecessary."
  609. */
  610. break;
  611. }
  612. qs->want_stop_sending = 1;
  613. ossl_quic_stream_map_update_state(qsm, qs);
  614. return 1;
  615. }
  616. QUIC_STREAM *ossl_quic_stream_map_peek_accept_queue(QUIC_STREAM_MAP *qsm)
  617. {
  618. return accept_head(&qsm->accept_list);
  619. }
  620. void ossl_quic_stream_map_push_accept_queue(QUIC_STREAM_MAP *qsm,
  621. QUIC_STREAM *s)
  622. {
  623. list_insert_tail(&qsm->accept_list, &s->accept_node);
  624. ++qsm->num_accept;
  625. }
  626. static QUIC_RXFC *qsm_get_max_streams_rxfc(QUIC_STREAM_MAP *qsm, QUIC_STREAM *s)
  627. {
  628. return ossl_quic_stream_is_bidi(s)
  629. ? qsm->max_streams_bidi_rxfc
  630. : qsm->max_streams_uni_rxfc;
  631. }
  632. void ossl_quic_stream_map_remove_from_accept_queue(QUIC_STREAM_MAP *qsm,
  633. QUIC_STREAM *s,
  634. OSSL_TIME rtt)
  635. {
  636. QUIC_RXFC *max_streams_rxfc;
  637. list_remove(&qsm->accept_list, &s->accept_node);
  638. --qsm->num_accept;
  639. if ((max_streams_rxfc = qsm_get_max_streams_rxfc(qsm, s)) != NULL)
  640. ossl_quic_rxfc_on_retire(max_streams_rxfc, 1, rtt);
  641. }
  642. size_t ossl_quic_stream_map_get_accept_queue_len(QUIC_STREAM_MAP *qsm)
  643. {
  644. return qsm->num_accept;
  645. }
  646. void ossl_quic_stream_map_gc(QUIC_STREAM_MAP *qsm)
  647. {
  648. QUIC_STREAM *qs, *qs_head, *qsn = NULL;
  649. for (qs = qs_head = ready_for_gc_head(&qsm->ready_for_gc_list);
  650. qs != NULL && qs != qs_head;
  651. qs = qsn)
  652. {
  653. qsn = ready_for_gc_next(&qsm->ready_for_gc_list, qs);
  654. ossl_quic_stream_map_release(qsm, qs);
  655. }
  656. }
  657. static int eligible_for_shutdown_flush(QUIC_STREAM *qs)
  658. {
  659. /*
  660. * We only care about servicing the send part of a stream (if any) during
  661. * shutdown flush. We make sure we flush a stream if it is either
  662. * non-terminated or was terminated normally such as via
  663. * SSL_stream_conclude. A stream which was terminated via a reset is not
  664. * flushed, and we will have thrown away the send buffer in that case
  665. * anyway.
  666. */
  667. switch (qs->send_state) {
  668. case QUIC_SSTREAM_STATE_SEND:
  669. case QUIC_SSTREAM_STATE_DATA_SENT:
  670. return !ossl_quic_sstream_is_totally_acked(qs->sstream);
  671. default:
  672. return 0;
  673. }
  674. }
  675. static void begin_shutdown_flush_each(QUIC_STREAM *qs, void *arg)
  676. {
  677. QUIC_STREAM_MAP *qsm = arg;
  678. if (!eligible_for_shutdown_flush(qs) || qs->shutdown_flush)
  679. return;
  680. qs->shutdown_flush = 1;
  681. ++qsm->num_shutdown_flush;
  682. }
  683. void ossl_quic_stream_map_begin_shutdown_flush(QUIC_STREAM_MAP *qsm)
  684. {
  685. qsm->num_shutdown_flush = 0;
  686. ossl_quic_stream_map_visit(qsm, begin_shutdown_flush_each, qsm);
  687. }
  688. int ossl_quic_stream_map_is_shutdown_flush_finished(QUIC_STREAM_MAP *qsm)
  689. {
  690. return qsm->num_shutdown_flush == 0;
  691. }
  692. /*
  693. * QUIC Stream Iterator
  694. * ====================
  695. */
  696. void ossl_quic_stream_iter_init(QUIC_STREAM_ITER *it, QUIC_STREAM_MAP *qsm,
  697. int advance_rr)
  698. {
  699. it->qsm = qsm;
  700. it->stream = it->first_stream = qsm->rr_cur;
  701. if (advance_rr && it->stream != NULL
  702. && ++qsm->rr_counter >= qsm->rr_stepping) {
  703. qsm->rr_counter = 0;
  704. qsm->rr_cur = active_next(&qsm->active_list, qsm->rr_cur);
  705. }
  706. }
  707. void ossl_quic_stream_iter_next(QUIC_STREAM_ITER *it)
  708. {
  709. if (it->stream == NULL)
  710. return;
  711. it->stream = active_next(&it->qsm->active_list, it->stream);
  712. if (it->stream == it->first_stream)
  713. it->stream = NULL;
  714. }