quic_stream_map.c 26 KB

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