quic_ackm.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725
  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_ackm.h"
  10. #include "internal/uint_set.h"
  11. #include "internal/common.h"
  12. #include <assert.h>
  13. DEFINE_LIST_OF(tx_history, OSSL_ACKM_TX_PKT);
  14. /*
  15. * TX Packet History
  16. * *****************
  17. *
  18. * The TX Packet History object tracks information about packets which have been
  19. * sent for which we later expect to receive an ACK. It is essentially a simple
  20. * database keeping a list of packet information structures in packet number
  21. * order which can also be looked up directly by packet number.
  22. *
  23. * We currently only allow packets to be appended to the list (i.e. the packet
  24. * numbers of the packets appended to the list must monotonically increase), as
  25. * we should not currently need more general functionality such as a sorted list
  26. * insert.
  27. */
  28. struct tx_pkt_history_st {
  29. /* A linked list of all our packets. */
  30. OSSL_LIST(tx_history) packets;
  31. /*
  32. * Mapping from packet numbers (uint64_t) to (OSSL_ACKM_TX_PKT *)
  33. *
  34. * Invariant: A packet is in this map if and only if it is in the linked
  35. * list.
  36. */
  37. LHASH_OF(OSSL_ACKM_TX_PKT) *map;
  38. /*
  39. * The lowest packet number which may currently be added to the history list
  40. * (inclusive). We do not allow packet numbers to be added to the history
  41. * list non-monotonically, so packet numbers must be greater than or equal
  42. * to this value.
  43. */
  44. uint64_t watermark;
  45. /*
  46. * Packet number of the highest packet info structure we have yet appended
  47. * to the list. This is usually one less than watermark, except when we have
  48. * not added any packet yet.
  49. */
  50. uint64_t highest_sent;
  51. };
  52. DEFINE_LHASH_OF_EX(OSSL_ACKM_TX_PKT);
  53. static unsigned long tx_pkt_info_hash(const OSSL_ACKM_TX_PKT *pkt)
  54. {
  55. /* Using low bits of the packet number as the hash should be enough */
  56. return (unsigned long)pkt->pkt_num;
  57. }
  58. static int tx_pkt_info_compare(const OSSL_ACKM_TX_PKT *a,
  59. const OSSL_ACKM_TX_PKT *b)
  60. {
  61. if (a->pkt_num < b->pkt_num)
  62. return -1;
  63. if (a->pkt_num > b->pkt_num)
  64. return 1;
  65. return 0;
  66. }
  67. static int
  68. tx_pkt_history_init(struct tx_pkt_history_st *h)
  69. {
  70. ossl_list_tx_history_init(&h->packets);
  71. h->watermark = 0;
  72. h->highest_sent = 0;
  73. h->map = lh_OSSL_ACKM_TX_PKT_new(tx_pkt_info_hash, tx_pkt_info_compare);
  74. if (h->map == NULL)
  75. return 0;
  76. return 1;
  77. }
  78. static void
  79. tx_pkt_history_destroy(struct tx_pkt_history_st *h)
  80. {
  81. lh_OSSL_ACKM_TX_PKT_free(h->map);
  82. h->map = NULL;
  83. ossl_list_tx_history_init(&h->packets);
  84. }
  85. static int
  86. tx_pkt_history_add_actual(struct tx_pkt_history_st *h,
  87. OSSL_ACKM_TX_PKT *pkt)
  88. {
  89. OSSL_ACKM_TX_PKT *existing;
  90. /*
  91. * There should not be any existing packet with this number
  92. * in our mapping.
  93. */
  94. existing = lh_OSSL_ACKM_TX_PKT_retrieve(h->map, pkt);
  95. if (!ossl_assert(existing == NULL))
  96. return 0;
  97. /* Should not already be in a list. */
  98. if (!ossl_assert(ossl_list_tx_history_next(pkt) == NULL
  99. && ossl_list_tx_history_prev(pkt) == NULL))
  100. return 0;
  101. lh_OSSL_ACKM_TX_PKT_insert(h->map, pkt);
  102. ossl_list_tx_history_insert_tail(&h->packets, pkt);
  103. return 1;
  104. }
  105. /* Adds a packet information structure to the history list. */
  106. static int
  107. tx_pkt_history_add(struct tx_pkt_history_st *h,
  108. OSSL_ACKM_TX_PKT *pkt)
  109. {
  110. if (!ossl_assert(pkt->pkt_num >= h->watermark))
  111. return 0;
  112. if (tx_pkt_history_add_actual(h, pkt) < 1)
  113. return 0;
  114. h->watermark = pkt->pkt_num + 1;
  115. h->highest_sent = pkt->pkt_num;
  116. return 1;
  117. }
  118. /* Retrieve a packet information structure by packet number. */
  119. static OSSL_ACKM_TX_PKT *
  120. tx_pkt_history_by_pkt_num(struct tx_pkt_history_st *h, uint64_t pkt_num)
  121. {
  122. OSSL_ACKM_TX_PKT key;
  123. key.pkt_num = pkt_num;
  124. return lh_OSSL_ACKM_TX_PKT_retrieve(h->map, &key);
  125. }
  126. /* Remove a packet information structure from the history log. */
  127. static int
  128. tx_pkt_history_remove(struct tx_pkt_history_st *h, uint64_t pkt_num)
  129. {
  130. OSSL_ACKM_TX_PKT key, *pkt;
  131. key.pkt_num = pkt_num;
  132. pkt = tx_pkt_history_by_pkt_num(h, pkt_num);
  133. if (pkt == NULL)
  134. return 0;
  135. ossl_list_tx_history_remove(&h->packets, pkt);
  136. lh_OSSL_ACKM_TX_PKT_delete(h->map, &key);
  137. return 1;
  138. }
  139. /*
  140. * RX Packet Number Tracking
  141. * *************************
  142. *
  143. * **Background.** The RX side of the ACK manager must track packets we have
  144. * received for which we have to generate ACK frames. Broadly, this means we
  145. * store a set of packet numbers which we have received but which we do not know
  146. * for a fact that the transmitter knows we have received.
  147. *
  148. * This must handle various situations:
  149. *
  150. * 1. We receive a packet but have not sent an ACK yet, so the transmitter
  151. * does not know whether we have received it or not yet.
  152. *
  153. * 2. We receive a packet and send an ACK which is lost. We do not
  154. * immediately know that the ACK was lost and the transmitter does not know
  155. * that we have received the packet.
  156. *
  157. * 3. We receive a packet and send an ACK which is received by the
  158. * transmitter. The transmitter does not immediately respond with an ACK,
  159. * or responds with an ACK which is lost. The transmitter knows that we
  160. * have received the packet, but we do not know for sure that it knows,
  161. * because the ACK we sent could have been lost.
  162. *
  163. * 4. We receive a packet and send an ACK which is received by the
  164. * transmitter. The transmitter subsequently sends us an ACK which confirms
  165. * its receipt of the ACK we sent, and we successfully receive that ACK, so
  166. * we know that the transmitter knows, that we received the original
  167. * packet.
  168. *
  169. * Only when we reach case (4) are we relieved of any need to track a given
  170. * packet number we have received, because only in this case do we know for sure
  171. * that the peer knows we have received the packet. Having reached case (4) we
  172. * will never again need to generate an ACK containing the PN in question, but
  173. * until we reach that point, we must keep track of the PN as not having been
  174. * provably ACKed, as we may have to keep generating ACKs for the given PN not
  175. * just until the transmitter receives one, but until we know that it has
  176. * received one. This will be referred to herein as "provably ACKed".
  177. *
  178. * **Duplicate handling.** The above discusses the case where we have received a
  179. * packet with a given PN but are at best unsure whether the sender knows we
  180. * have received it or not. However, we must also handle the case where we have
  181. * yet to receive a packet with a given PN in the first place. The reason for
  182. * this is because of the requirement expressed by RFC 9000 s. 12.3:
  183. *
  184. * "A receiver MUST discard a newly unprotected packet unless it is certain
  185. * that it has not processed another packet with the same packet number from
  186. * the same packet number space."
  187. *
  188. * We must ensure we never process a duplicate PN. As such, each possible PN we
  189. * can receive must exist in one of the following logical states:
  190. *
  191. * - We have never processed this PN before
  192. * (so if we receive such a PN, it can be processed)
  193. *
  194. * - We have processed this PN but it has not yet been provably ACKed
  195. * (and should therefore be in any future ACK frame generated;
  196. * if we receive such a PN again, it must be ignored)
  197. *
  198. * - We have processed this PN and it has been provably ACKed
  199. * (if we receive such a PN again, it must be ignored)
  200. *
  201. * However, if we were to track this state for every PN ever used in the history
  202. * of a connection, the amount of state required would increase unboundedly as
  203. * the connection goes on (for example, we would have to store a set of every PN
  204. * ever received.)
  205. *
  206. * RFC 9000 s. 12.3 continues:
  207. *
  208. * "Endpoints that track all individual packets for the purposes of detecting
  209. * duplicates are at risk of accumulating excessive state. The data required
  210. * for detecting duplicates can be limited by maintaining a minimum packet
  211. * number below which all packets are immediately dropped."
  212. *
  213. * Moreover, RFC 9000 s. 13.2.3 states that:
  214. *
  215. * "A receiver MUST retain an ACK Range unless it can ensure that it will not
  216. * subsequently accept packets with numbers in that range. Maintaining a
  217. * minimum packet number that increases as ranges are discarded is one way to
  218. * achieve this with minimal state."
  219. *
  220. * This touches on a subtlety of the original requirement quoted above: the
  221. * receiver MUST discard a packet unless it is certain that it has not processed
  222. * another packet with the same PN. However, this does not forbid the receiver
  223. * from also discarding some PNs even though it has not yet processed them. In
  224. * other words, implementations must be conservative and err in the direction of
  225. * assuming a packet is a duplicate, but it is acceptable for this to come at
  226. * the cost of falsely identifying some packets as duplicates.
  227. *
  228. * This allows us to bound the amount of state we must keep, and we adopt the
  229. * suggested strategy quoted above to do so. We define a watermark PN below
  230. * which all PNs are in the same state. This watermark is only ever increased.
  231. * Thus the PNs the state for which needs to be explicitly tracked is limited to
  232. * only a small number of recent PNs, and all older PNs have an assumed state.
  233. *
  234. * Any given PN thus falls into one of the following states:
  235. *
  236. * - (A) The PN is above the watermark but we have not yet received it.
  237. *
  238. * If we receive such a PN, we should process it and record the PN as
  239. * received.
  240. *
  241. * - (B) The PN is above the watermark and we have received it.
  242. *
  243. * The PN should be included in any future ACK frame we generate.
  244. * If we receive such a PN again, we should ignore it.
  245. *
  246. * - (C) The PN is below the watermark.
  247. *
  248. * We do not know whether a packet with the given PN was received or
  249. * not. To be safe, if we receive such a packet, it is not processed.
  250. *
  251. * Note that state (C) corresponds to both "we have processed this PN and it has
  252. * been provably ACKed" logical state and a subset of the PNs in the "we have
  253. * never processed this PN before" logical state (namely all PNs which were lost
  254. * and never received, but which are not recent enough to be above the
  255. * watermark). The reason we can merge these states and avoid tracking states
  256. * for the PNs in this state is because the provably ACKed and never-received
  257. * states are functionally identical in terms of how we need to handle them: we
  258. * don't need to do anything for PNs in either of these states, so we don't have
  259. * to care about PNs in this state nor do we have to care about distinguishing
  260. * the two states for a given PN.
  261. *
  262. * Note that under this scheme provably ACKed PNs are by definition always below
  263. * the watermark; therefore, it follows that when a PN becomes provably ACKed,
  264. * the watermark must be immediately increased to exceed it (otherwise we would
  265. * keep reporting it in future ACK frames).
  266. *
  267. * This is in line with RFC 9000 s. 13.2.4's suggested strategy on when
  268. * to advance the watermark:
  269. *
  270. * "When a packet containing an ACK frame is sent, the Largest Acknowledged
  271. * field in that frame can be saved. When a packet containing an ACK frame is
  272. * acknowledged, the receiver can stop acknowledging packets less than or
  273. * equal to the Largest Acknowledged field in the sent ACK frame."
  274. *
  275. * This is where our scheme's false positives arise. When a packet containing an
  276. * ACK frame is itself ACK'd, PNs referenced in that ACK frame become provably
  277. * acked, and the watermark is bumped accordingly. However, the Largest
  278. * Acknowledged field does not imply that all lower PNs have been received,
  279. * because there may be gaps expressed in the ranges of PNs expressed by that
  280. * and previous ACK frames. Thus, some unreceived PNs may be moved below the
  281. * watermark, and we may subsequently reject those PNs as possibly being
  282. * duplicates even though we have not actually received those PNs. Since we bump
  283. * the watermark when a PN becomes provably ACKed, it follows that an unreceived
  284. * PN falls below the watermark (and thus becomes a false positive for the
  285. * purposes of duplicate detection) when a higher-numbered PN becomes provably
  286. * ACKed.
  287. *
  288. * Thus, when PN n becomes provably acked, any unreceived PNs in the range [0,
  289. * n) will no longer be processed. Although datagrams may be reordered in the
  290. * network, a PN we receive can only become provably ACKed after our own
  291. * subsequently generated ACK frame is sent in a future TX packet, and then we
  292. * receive another RX PN acknowledging that TX packet. This means that a given RX
  293. * PN can only become provably ACKed at least 1 RTT after it is received; it is
  294. * unlikely that any reordered datagrams will still be "in the network" (and not
  295. * lost) by this time. If this does occur for whatever reason and a late PN is
  296. * received, the packet will be discarded unprocessed and the PN is simply
  297. * handled as though lost (a "written off" PN).
  298. *
  299. * **Data structure.** Our state for the RX handling side of the ACK manager, as
  300. * discussed above, mainly comprises:
  301. *
  302. * a) a logical set of PNs, and
  303. * b) a monotonically increasing PN counter (the watermark).
  304. *
  305. * For (a), we define a data structure which stores a logical set of PNs, which
  306. * we use to keep track of which PNs we have received but which have not yet
  307. * been provably ACKed, and thus will later need to generate an ACK frame for.
  308. *
  309. * The correspondence with the logical states discussed above is as follows. A
  310. * PN is in state (C) if it is below the watermark; otherwise it is in state (B)
  311. * if it is in the logical set of PNs, and in state (A) otherwise.
  312. *
  313. * Note that PNs are only removed from the PN set (when they become provably
  314. * ACKed or written off) by virtue of advancement of the watermark. Removing PNs
  315. * from the PN set any other way would be ambiguous as it would be
  316. * indistinguishable from a PN we have not yet received and risk us processing a
  317. * duplicate packet. In other words, for a given PN:
  318. *
  319. * - State (A) can transition to state (B) or (C)
  320. * - State (B) can transition to state (C) only
  321. * - State (C) is the terminal state
  322. *
  323. * We can query the logical set data structure for PNs which have been received
  324. * but which have not been provably ACKed when we want to generate ACK frames.
  325. * Since ACK frames can be lost and/or we might not know that the peer has
  326. * successfully received them, we might generate multiple ACK frames covering a
  327. * given PN until that PN becomes provably ACKed and we finally remove it from
  328. * our set (by bumping the watermark) as no longer being our concern.
  329. *
  330. * The data structure used is the UINT_SET structure defined in uint_set.h,
  331. * which is used as a PN set. We use the following operations of the structure:
  332. *
  333. * Insert Range: Used when we receive a new PN.
  334. *
  335. * Remove Range: Used when bumping the watermark.
  336. *
  337. * Query: Used to determine if a PN is in the set.
  338. *
  339. * **Possible duplicates.** A PN is considered a possible duplicate when either:
  340. *
  341. * a) its PN is already in the PN set (i.e. has already been received), or
  342. * b) its PN is below the watermark (i.e. was provably ACKed or written off).
  343. *
  344. * A packet with a given PN is considered 'processable' when that PN is not
  345. * considered a possible duplicate (see ossl_ackm_is_rx_pn_processable).
  346. *
  347. * **TX/RX interaction.** The watermark is bumped whenever an RX packet becomes
  348. * provably ACKed. This occurs when an ACK frame is received by the TX side of
  349. * the ACK manager; thus, there is necessary interaction between the TX and RX
  350. * sides of the ACK manager.
  351. *
  352. * This is implemented as follows. When a packet is queued as sent in the TX
  353. * side of the ACK manager, it may optionally have a Largest Acked value set on
  354. * it. The user of the ACK manager should do this if the packet being
  355. * transmitted contains an ACK frame, by setting the field to the Largest Acked
  356. * field of that frame. Otherwise, this field should be set to QUIC_PN_INVALID.
  357. * When a TX packet is eventually acknowledged which has this field set, it is
  358. * used to update the state of the RX side of the ACK manager by bumping the
  359. * watermark accordingly.
  360. */
  361. struct rx_pkt_history_st {
  362. UINT_SET set;
  363. /*
  364. * Invariant: PNs below this are not in the set.
  365. * Invariant: This is monotonic and only ever increases.
  366. */
  367. QUIC_PN watermark;
  368. };
  369. static int rx_pkt_history_bump_watermark(struct rx_pkt_history_st *h,
  370. QUIC_PN watermark);
  371. static void rx_pkt_history_init(struct rx_pkt_history_st *h)
  372. {
  373. ossl_uint_set_init(&h->set);
  374. h->watermark = 0;
  375. }
  376. static void rx_pkt_history_destroy(struct rx_pkt_history_st *h)
  377. {
  378. ossl_uint_set_destroy(&h->set);
  379. }
  380. /*
  381. * Limit the number of ACK ranges we store to prevent resource consumption DoS
  382. * attacks.
  383. */
  384. #define MAX_RX_ACK_RANGES 32
  385. static void rx_pkt_history_trim_range_count(struct rx_pkt_history_st *h)
  386. {
  387. QUIC_PN highest = QUIC_PN_INVALID;
  388. while (ossl_list_uint_set_num(&h->set) > MAX_RX_ACK_RANGES) {
  389. UINT_RANGE r = ossl_list_uint_set_head(&h->set)->range;
  390. highest = (highest == QUIC_PN_INVALID)
  391. ? r.end : ossl_quic_pn_max(highest, r.end);
  392. ossl_uint_set_remove(&h->set, &r);
  393. }
  394. /*
  395. * Bump watermark to cover all PNs we removed to avoid accidental
  396. * reprocessing of packets.
  397. */
  398. if (highest != QUIC_PN_INVALID)
  399. rx_pkt_history_bump_watermark(h, highest + 1);
  400. }
  401. static int rx_pkt_history_add_pn(struct rx_pkt_history_st *h,
  402. QUIC_PN pn)
  403. {
  404. UINT_RANGE r;
  405. r.start = pn;
  406. r.end = pn;
  407. if (pn < h->watermark)
  408. return 1; /* consider this a success case */
  409. if (ossl_uint_set_insert(&h->set, &r) != 1)
  410. return 0;
  411. rx_pkt_history_trim_range_count(h);
  412. return 1;
  413. }
  414. static int rx_pkt_history_bump_watermark(struct rx_pkt_history_st *h,
  415. QUIC_PN watermark)
  416. {
  417. UINT_RANGE r;
  418. if (watermark <= h->watermark)
  419. return 1;
  420. /* Remove existing PNs below the watermark. */
  421. r.start = 0;
  422. r.end = watermark - 1;
  423. if (ossl_uint_set_remove(&h->set, &r) != 1)
  424. return 0;
  425. h->watermark = watermark;
  426. return 1;
  427. }
  428. /*
  429. * ACK Manager Implementation
  430. * **************************
  431. * Implementation of the ACK manager proper.
  432. */
  433. /* Constants used by the ACK manager; see RFC 9002. */
  434. #define K_GRANULARITY (1 * OSSL_TIME_MS)
  435. #define K_PKT_THRESHOLD 3
  436. #define K_TIME_THRESHOLD_NUM 9
  437. #define K_TIME_THRESHOLD_DEN 8
  438. /* The maximum number of times we allow PTO to be doubled. */
  439. #define MAX_PTO_COUNT 16
  440. /* Default maximum amount of time to leave an ACK-eliciting packet un-ACK'd. */
  441. #define DEFAULT_TX_MAX_ACK_DELAY ossl_ms2time(QUIC_DEFAULT_MAX_ACK_DELAY)
  442. struct ossl_ackm_st {
  443. /* Our list of transmitted packets. Corresponds to RFC 9002 sent_packets. */
  444. struct tx_pkt_history_st tx_history[QUIC_PN_SPACE_NUM];
  445. /* Our list of received PNs which are not yet provably acked. */
  446. struct rx_pkt_history_st rx_history[QUIC_PN_SPACE_NUM];
  447. /* Polymorphic dependencies that we consume. */
  448. OSSL_TIME (*now)(void *arg);
  449. void *now_arg;
  450. OSSL_STATM *statm;
  451. const OSSL_CC_METHOD *cc_method;
  452. OSSL_CC_DATA *cc_data;
  453. /* RFC 9002 variables. */
  454. uint32_t pto_count;
  455. QUIC_PN largest_acked_pkt[QUIC_PN_SPACE_NUM];
  456. OSSL_TIME time_of_last_ack_eliciting_pkt[QUIC_PN_SPACE_NUM];
  457. OSSL_TIME loss_time[QUIC_PN_SPACE_NUM];
  458. OSSL_TIME loss_detection_deadline;
  459. /* Lowest PN which is still not known to be ACKed. */
  460. QUIC_PN lowest_unacked_pkt[QUIC_PN_SPACE_NUM];
  461. /* Time at which we got our first RTT sample, or 0. */
  462. OSSL_TIME first_rtt_sample;
  463. /*
  464. * A packet's num_bytes are added to this if it is inflight,
  465. * and removed again once ack'd/lost/discarded.
  466. */
  467. uint64_t bytes_in_flight;
  468. /*
  469. * A packet's num_bytes are added to this if it is both inflight and
  470. * ack-eliciting, and removed again once ack'd/lost/discarded.
  471. */
  472. uint64_t ack_eliciting_bytes_in_flight[QUIC_PN_SPACE_NUM];
  473. /* Count of ECN-CE events. */
  474. uint64_t peer_ecnce[QUIC_PN_SPACE_NUM];
  475. /* Set to 1 when the handshake is confirmed. */
  476. char handshake_confirmed;
  477. /* Set to 1 when the peer has completed address validation. */
  478. char peer_completed_addr_validation;
  479. /* Set to 1 when a PN space has been discarded. */
  480. char discarded[QUIC_PN_SPACE_NUM];
  481. /* Set to 1 when we think an ACK frame should be generated. */
  482. char rx_ack_desired[QUIC_PN_SPACE_NUM];
  483. /* Set to 1 if an ACK frame has ever been generated. */
  484. char rx_ack_generated[QUIC_PN_SPACE_NUM];
  485. /* Probe request counts for reporting to the user. */
  486. OSSL_ACKM_PROBE_INFO pending_probe;
  487. /* Generated ACK frames for each PN space. */
  488. OSSL_QUIC_FRAME_ACK ack[QUIC_PN_SPACE_NUM];
  489. OSSL_QUIC_ACK_RANGE ack_ranges[QUIC_PN_SPACE_NUM][MAX_RX_ACK_RANGES];
  490. /* Other RX state. */
  491. /* Largest PN we have RX'd. */
  492. QUIC_PN rx_largest_pn[QUIC_PN_SPACE_NUM];
  493. /* Time at which the PN in rx_largest_pn was RX'd. */
  494. OSSL_TIME rx_largest_time[QUIC_PN_SPACE_NUM];
  495. /*
  496. * ECN event counters. Each time we receive a packet with a given ECN label,
  497. * the corresponding ECN counter here is incremented.
  498. */
  499. uint64_t rx_ect0[QUIC_PN_SPACE_NUM];
  500. uint64_t rx_ect1[QUIC_PN_SPACE_NUM];
  501. uint64_t rx_ecnce[QUIC_PN_SPACE_NUM];
  502. /*
  503. * Number of ACK-eliciting packets since last ACK. We use this to defer
  504. * emitting ACK frames until a threshold number of ACK-eliciting packets
  505. * have been received.
  506. */
  507. uint32_t rx_ack_eliciting_pkts_since_last_ack[QUIC_PN_SPACE_NUM];
  508. /*
  509. * The ACK frame coalescing deadline at which we should flush any unsent ACK
  510. * frames.
  511. */
  512. OSSL_TIME rx_ack_flush_deadline[QUIC_PN_SPACE_NUM];
  513. /*
  514. * The RX maximum ACK delay (the maximum amount of time our peer might
  515. * wait to send us an ACK after receiving an ACK-eliciting packet).
  516. */
  517. OSSL_TIME rx_max_ack_delay;
  518. /*
  519. * The TX maximum ACK delay (the maximum amount of time we allow ourselves
  520. * to wait before generating an ACK after receiving an ACK-eliciting
  521. * packet).
  522. */
  523. OSSL_TIME tx_max_ack_delay;
  524. /* Callbacks for deadline updates. */
  525. void (*loss_detection_deadline_cb)(OSSL_TIME deadline, void *arg);
  526. void *loss_detection_deadline_cb_arg;
  527. void (*ack_deadline_cb)(OSSL_TIME deadline, int pkt_space, void *arg);
  528. void *ack_deadline_cb_arg;
  529. };
  530. static ossl_inline uint32_t min_u32(uint32_t x, uint32_t y)
  531. {
  532. return x < y ? x : y;
  533. }
  534. /*
  535. * Get TX history for a given packet number space. Must not have been
  536. * discarded.
  537. */
  538. static struct tx_pkt_history_st *get_tx_history(OSSL_ACKM *ackm, int pkt_space)
  539. {
  540. assert(!ackm->discarded[pkt_space]);
  541. return &ackm->tx_history[pkt_space];
  542. }
  543. /*
  544. * Get RX history for a given packet number space. Must not have been
  545. * discarded.
  546. */
  547. static struct rx_pkt_history_st *get_rx_history(OSSL_ACKM *ackm, int pkt_space)
  548. {
  549. assert(!ackm->discarded[pkt_space]);
  550. return &ackm->rx_history[pkt_space];
  551. }
  552. /* Does the newly-acknowledged list contain any ack-eliciting packet? */
  553. static int ack_includes_ack_eliciting(OSSL_ACKM_TX_PKT *pkt)
  554. {
  555. for (; pkt != NULL; pkt = pkt->anext)
  556. if (pkt->is_ack_eliciting)
  557. return 1;
  558. return 0;
  559. }
  560. /* Return number of ACK-eliciting bytes in flight across all PN spaces. */
  561. static uint64_t ackm_ack_eliciting_bytes_in_flight(OSSL_ACKM *ackm)
  562. {
  563. int i;
  564. uint64_t total = 0;
  565. for (i = 0; i < QUIC_PN_SPACE_NUM; ++i)
  566. total += ackm->ack_eliciting_bytes_in_flight[i];
  567. return total;
  568. }
  569. /* Return 1 if the range contains the given PN. */
  570. static int range_contains(const OSSL_QUIC_ACK_RANGE *range, QUIC_PN pn)
  571. {
  572. return pn >= range->start && pn <= range->end;
  573. }
  574. /*
  575. * Given a logical representation of an ACK frame 'ack', create a singly-linked
  576. * list of the newly ACK'd frames; that is, of frames which are matched by the
  577. * list of PN ranges contained in the ACK frame. The packet structures in the
  578. * list returned are removed from the TX history list. Returns a pointer to the
  579. * list head (or NULL) if empty.
  580. */
  581. static OSSL_ACKM_TX_PKT *ackm_detect_and_remove_newly_acked_pkts(OSSL_ACKM *ackm,
  582. const OSSL_QUIC_FRAME_ACK *ack,
  583. int pkt_space)
  584. {
  585. OSSL_ACKM_TX_PKT *acked_pkts = NULL, **fixup = &acked_pkts, *pkt, *pprev;
  586. struct tx_pkt_history_st *h;
  587. size_t ridx = 0;
  588. assert(ack->num_ack_ranges > 0);
  589. /*
  590. * Our history list is a list of packets sorted in ascending order
  591. * by packet number.
  592. *
  593. * ack->ack_ranges is a list of packet number ranges in descending order.
  594. *
  595. * Walk through our history list from the end in order to efficiently detect
  596. * membership in the specified ack ranges. As an optimization, we use our
  597. * hashtable to try and skip to the first matching packet. This may fail if
  598. * the ACK ranges given include nonexistent packets.
  599. */
  600. h = get_tx_history(ackm, pkt_space);
  601. pkt = tx_pkt_history_by_pkt_num(h, ack->ack_ranges[0].end);
  602. if (pkt == NULL)
  603. pkt = ossl_list_tx_history_tail(&h->packets);
  604. for (; pkt != NULL; pkt = pprev) {
  605. /*
  606. * Save prev value as it will be zeroed if we remove the packet from the
  607. * history list below.
  608. */
  609. pprev = ossl_list_tx_history_prev(pkt);
  610. for (;; ++ridx) {
  611. if (ridx >= ack->num_ack_ranges) {
  612. /*
  613. * We have exhausted all ranges so stop here, even if there are
  614. * more packets to look at.
  615. */
  616. goto stop;
  617. }
  618. if (range_contains(&ack->ack_ranges[ridx], pkt->pkt_num)) {
  619. /* We have matched this range. */
  620. tx_pkt_history_remove(h, pkt->pkt_num);
  621. *fixup = pkt;
  622. fixup = &pkt->anext;
  623. *fixup = NULL;
  624. break;
  625. } else if (pkt->pkt_num > ack->ack_ranges[ridx].end) {
  626. /*
  627. * We have not reached this range yet in our list, so do not
  628. * advance ridx.
  629. */
  630. break;
  631. } else {
  632. /*
  633. * We have moved beyond this range, so advance to the next range
  634. * and try matching again.
  635. */
  636. assert(pkt->pkt_num < ack->ack_ranges[ridx].start);
  637. continue;
  638. }
  639. }
  640. }
  641. stop:
  642. return acked_pkts;
  643. }
  644. /*
  645. * Create a singly-linked list of newly detected-lost packets in the given
  646. * packet number space. Returns the head of the list or NULL if no packets were
  647. * detected lost. The packets in the list are removed from the TX history list.
  648. */
  649. static OSSL_ACKM_TX_PKT *ackm_detect_and_remove_lost_pkts(OSSL_ACKM *ackm,
  650. int pkt_space)
  651. {
  652. OSSL_ACKM_TX_PKT *lost_pkts = NULL, **fixup = &lost_pkts, *pkt, *pnext;
  653. OSSL_TIME loss_delay, lost_send_time, now;
  654. OSSL_RTT_INFO rtt;
  655. struct tx_pkt_history_st *h;
  656. assert(ackm->largest_acked_pkt[pkt_space] != QUIC_PN_INVALID);
  657. ossl_statm_get_rtt_info(ackm->statm, &rtt);
  658. ackm->loss_time[pkt_space] = ossl_time_zero();
  659. loss_delay = ossl_time_multiply(ossl_time_max(rtt.latest_rtt,
  660. rtt.smoothed_rtt),
  661. K_TIME_THRESHOLD_NUM);
  662. loss_delay = ossl_time_divide(loss_delay, K_TIME_THRESHOLD_DEN);
  663. /* Minimum time of K_GRANULARITY before packets are deemed lost. */
  664. loss_delay = ossl_time_max(loss_delay, ossl_ticks2time(K_GRANULARITY));
  665. /* Packets sent before this time are deemed lost. */
  666. now = ackm->now(ackm->now_arg);
  667. lost_send_time = ossl_time_subtract(now, loss_delay);
  668. h = get_tx_history(ackm, pkt_space);
  669. pkt = ossl_list_tx_history_head(&h->packets);
  670. for (; pkt != NULL; pkt = pnext) {
  671. assert(pkt_space == pkt->pkt_space);
  672. /*
  673. * Save prev value as it will be zeroed if we remove the packet from the
  674. * history list below.
  675. */
  676. pnext = ossl_list_tx_history_next(pkt);
  677. if (pkt->pkt_num > ackm->largest_acked_pkt[pkt_space])
  678. continue;
  679. /*
  680. * Mark packet as lost, or set time when it should be marked.
  681. */
  682. if (ossl_time_compare(pkt->time, lost_send_time) <= 0
  683. || ackm->largest_acked_pkt[pkt_space]
  684. >= pkt->pkt_num + K_PKT_THRESHOLD) {
  685. tx_pkt_history_remove(h, pkt->pkt_num);
  686. *fixup = pkt;
  687. fixup = &pkt->lnext;
  688. *fixup = NULL;
  689. } else {
  690. if (ossl_time_is_zero(ackm->loss_time[pkt_space]))
  691. ackm->loss_time[pkt_space] =
  692. ossl_time_add(pkt->time, loss_delay);
  693. else
  694. ackm->loss_time[pkt_space] =
  695. ossl_time_min(ackm->loss_time[pkt_space],
  696. ossl_time_add(pkt->time, loss_delay));
  697. }
  698. }
  699. return lost_pkts;
  700. }
  701. static OSSL_TIME ackm_get_loss_time_and_space(OSSL_ACKM *ackm, int *pspace)
  702. {
  703. OSSL_TIME time = ackm->loss_time[QUIC_PN_SPACE_INITIAL];
  704. int i, space = QUIC_PN_SPACE_INITIAL;
  705. for (i = space + 1; i < QUIC_PN_SPACE_NUM; ++i)
  706. if (ossl_time_is_zero(time)
  707. || ossl_time_compare(ackm->loss_time[i], time) == -1) {
  708. time = ackm->loss_time[i];
  709. space = i;
  710. }
  711. *pspace = space;
  712. return time;
  713. }
  714. static OSSL_TIME ackm_get_pto_time_and_space(OSSL_ACKM *ackm, int *space)
  715. {
  716. OSSL_RTT_INFO rtt;
  717. OSSL_TIME duration;
  718. OSSL_TIME pto_timeout = ossl_time_infinite(), t;
  719. int pto_space = QUIC_PN_SPACE_INITIAL, i;
  720. ossl_statm_get_rtt_info(ackm->statm, &rtt);
  721. duration
  722. = ossl_time_add(rtt.smoothed_rtt,
  723. ossl_time_max(ossl_time_multiply(rtt.rtt_variance, 4),
  724. ossl_ticks2time(K_GRANULARITY)));
  725. duration
  726. = ossl_time_multiply(duration,
  727. (uint64_t)1 << min_u32(ackm->pto_count,
  728. MAX_PTO_COUNT));
  729. /* Anti-deadlock PTO starts from the current time. */
  730. if (ackm_ack_eliciting_bytes_in_flight(ackm) == 0) {
  731. assert(!ackm->peer_completed_addr_validation);
  732. *space = ackm->discarded[QUIC_PN_SPACE_INITIAL]
  733. ? QUIC_PN_SPACE_HANDSHAKE
  734. : QUIC_PN_SPACE_INITIAL;
  735. return ossl_time_add(ackm->now(ackm->now_arg), duration);
  736. }
  737. for (i = QUIC_PN_SPACE_INITIAL; i < QUIC_PN_SPACE_NUM; ++i) {
  738. if (ackm->ack_eliciting_bytes_in_flight[i] == 0)
  739. continue;
  740. if (i == QUIC_PN_SPACE_APP) {
  741. /* Skip application data until handshake confirmed. */
  742. if (!ackm->handshake_confirmed)
  743. break;
  744. /* Include max_ack_delay and backoff for app data. */
  745. if (!ossl_time_is_infinite(ackm->rx_max_ack_delay)) {
  746. uint64_t factor
  747. = (uint64_t)1 << min_u32(ackm->pto_count, MAX_PTO_COUNT);
  748. duration
  749. = ossl_time_add(duration,
  750. ossl_time_multiply(ackm->rx_max_ack_delay,
  751. factor));
  752. }
  753. }
  754. t = ossl_time_add(ackm->time_of_last_ack_eliciting_pkt[i], duration);
  755. if (ossl_time_compare(t, pto_timeout) < 0) {
  756. pto_timeout = t;
  757. pto_space = i;
  758. }
  759. }
  760. *space = pto_space;
  761. return pto_timeout;
  762. }
  763. static void ackm_set_loss_detection_timer_actual(OSSL_ACKM *ackm,
  764. OSSL_TIME deadline)
  765. {
  766. ackm->loss_detection_deadline = deadline;
  767. if (ackm->loss_detection_deadline_cb != NULL)
  768. ackm->loss_detection_deadline_cb(deadline,
  769. ackm->loss_detection_deadline_cb_arg);
  770. }
  771. static int ackm_set_loss_detection_timer(OSSL_ACKM *ackm)
  772. {
  773. int space;
  774. OSSL_TIME earliest_loss_time, timeout;
  775. earliest_loss_time = ackm_get_loss_time_and_space(ackm, &space);
  776. if (!ossl_time_is_zero(earliest_loss_time)) {
  777. /* Time threshold loss detection. */
  778. ackm_set_loss_detection_timer_actual(ackm, earliest_loss_time);
  779. return 1;
  780. }
  781. if (ackm_ack_eliciting_bytes_in_flight(ackm) == 0
  782. && ackm->peer_completed_addr_validation) {
  783. /*
  784. * Nothing to detect lost, so no timer is set. However, the client
  785. * needs to arm the timer if the server might be blocked by the
  786. * anti-amplification limit.
  787. */
  788. ackm_set_loss_detection_timer_actual(ackm, ossl_time_zero());
  789. return 1;
  790. }
  791. timeout = ackm_get_pto_time_and_space(ackm, &space);
  792. ackm_set_loss_detection_timer_actual(ackm, timeout);
  793. return 1;
  794. }
  795. static int ackm_in_persistent_congestion(OSSL_ACKM *ackm,
  796. const OSSL_ACKM_TX_PKT *lpkt)
  797. {
  798. /* TODO(QUIC FUTURE): Persistent congestion not currently implemented. */
  799. return 0;
  800. }
  801. static void ackm_on_pkts_lost(OSSL_ACKM *ackm, int pkt_space,
  802. const OSSL_ACKM_TX_PKT *lpkt, int pseudo)
  803. {
  804. const OSSL_ACKM_TX_PKT *p, *pnext;
  805. OSSL_RTT_INFO rtt;
  806. QUIC_PN largest_pn_lost = 0;
  807. OSSL_CC_LOSS_INFO loss_info = {0};
  808. uint32_t flags = 0;
  809. for (p = lpkt; p != NULL; p = pnext) {
  810. pnext = p->lnext;
  811. if (p->is_inflight) {
  812. ackm->bytes_in_flight -= p->num_bytes;
  813. if (p->is_ack_eliciting)
  814. ackm->ack_eliciting_bytes_in_flight[p->pkt_space]
  815. -= p->num_bytes;
  816. if (p->pkt_num > largest_pn_lost)
  817. largest_pn_lost = p->pkt_num;
  818. if (!pseudo) {
  819. /*
  820. * If this is pseudo-loss (e.g. during connection retry) we do not
  821. * inform the CC as it is not a real loss and not reflective of
  822. * network conditions.
  823. */
  824. loss_info.tx_time = p->time;
  825. loss_info.tx_size = p->num_bytes;
  826. ackm->cc_method->on_data_lost(ackm->cc_data, &loss_info);
  827. }
  828. }
  829. p->on_lost(p->cb_arg);
  830. }
  831. /*
  832. * Persistent congestion can only be considered if we have gotten at least
  833. * one RTT sample.
  834. */
  835. ossl_statm_get_rtt_info(ackm->statm, &rtt);
  836. if (!ossl_time_is_zero(ackm->first_rtt_sample)
  837. && ackm_in_persistent_congestion(ackm, lpkt))
  838. flags |= OSSL_CC_LOST_FLAG_PERSISTENT_CONGESTION;
  839. ackm->cc_method->on_data_lost_finished(ackm->cc_data, flags);
  840. }
  841. static void ackm_on_pkts_acked(OSSL_ACKM *ackm, const OSSL_ACKM_TX_PKT *apkt)
  842. {
  843. const OSSL_ACKM_TX_PKT *anext;
  844. QUIC_PN last_pn_acked = 0;
  845. OSSL_CC_ACK_INFO ainfo = {0};
  846. for (; apkt != NULL; apkt = anext) {
  847. if (apkt->is_inflight) {
  848. ackm->bytes_in_flight -= apkt->num_bytes;
  849. if (apkt->is_ack_eliciting)
  850. ackm->ack_eliciting_bytes_in_flight[apkt->pkt_space]
  851. -= apkt->num_bytes;
  852. if (apkt->pkt_num > last_pn_acked)
  853. last_pn_acked = apkt->pkt_num;
  854. if (apkt->largest_acked != QUIC_PN_INVALID)
  855. /*
  856. * This can fail, but it is monotonic; worst case we try again
  857. * next time.
  858. */
  859. rx_pkt_history_bump_watermark(get_rx_history(ackm,
  860. apkt->pkt_space),
  861. apkt->largest_acked + 1);
  862. }
  863. ainfo.tx_time = apkt->time;
  864. ainfo.tx_size = apkt->num_bytes;
  865. anext = apkt->anext;
  866. apkt->on_acked(apkt->cb_arg); /* may free apkt */
  867. if (apkt->is_inflight)
  868. ackm->cc_method->on_data_acked(ackm->cc_data, &ainfo);
  869. }
  870. }
  871. OSSL_ACKM *ossl_ackm_new(OSSL_TIME (*now)(void *arg),
  872. void *now_arg,
  873. OSSL_STATM *statm,
  874. const OSSL_CC_METHOD *cc_method,
  875. OSSL_CC_DATA *cc_data)
  876. {
  877. OSSL_ACKM *ackm;
  878. int i;
  879. ackm = OPENSSL_zalloc(sizeof(OSSL_ACKM));
  880. if (ackm == NULL)
  881. return NULL;
  882. for (i = 0; i < (int)OSSL_NELEM(ackm->tx_history); ++i) {
  883. ackm->largest_acked_pkt[i] = QUIC_PN_INVALID;
  884. ackm->rx_ack_flush_deadline[i] = ossl_time_infinite();
  885. if (tx_pkt_history_init(&ackm->tx_history[i]) < 1)
  886. goto err;
  887. }
  888. for (i = 0; i < (int)OSSL_NELEM(ackm->rx_history); ++i)
  889. rx_pkt_history_init(&ackm->rx_history[i]);
  890. ackm->now = now;
  891. ackm->now_arg = now_arg;
  892. ackm->statm = statm;
  893. ackm->cc_method = cc_method;
  894. ackm->cc_data = cc_data;
  895. ackm->rx_max_ack_delay = ossl_ms2time(QUIC_DEFAULT_MAX_ACK_DELAY);
  896. ackm->tx_max_ack_delay = DEFAULT_TX_MAX_ACK_DELAY;
  897. return ackm;
  898. err:
  899. while (--i >= 0)
  900. tx_pkt_history_destroy(&ackm->tx_history[i]);
  901. OPENSSL_free(ackm);
  902. return NULL;
  903. }
  904. void ossl_ackm_free(OSSL_ACKM *ackm)
  905. {
  906. size_t i;
  907. if (ackm == NULL)
  908. return;
  909. for (i = 0; i < OSSL_NELEM(ackm->tx_history); ++i)
  910. if (!ackm->discarded[i]) {
  911. tx_pkt_history_destroy(&ackm->tx_history[i]);
  912. rx_pkt_history_destroy(&ackm->rx_history[i]);
  913. }
  914. OPENSSL_free(ackm);
  915. }
  916. int ossl_ackm_on_tx_packet(OSSL_ACKM *ackm, OSSL_ACKM_TX_PKT *pkt)
  917. {
  918. struct tx_pkt_history_st *h = get_tx_history(ackm, pkt->pkt_space);
  919. /* Time must be set and not move backwards. */
  920. if (ossl_time_is_zero(pkt->time)
  921. || ossl_time_compare(ackm->time_of_last_ack_eliciting_pkt[pkt->pkt_space],
  922. pkt->time) > 0)
  923. return 0;
  924. /* Must have non-zero number of bytes. */
  925. if (pkt->num_bytes == 0)
  926. return 0;
  927. /* Does not make any sense for a non-in-flight packet to be ACK-eliciting. */
  928. if (!pkt->is_inflight && pkt->is_ack_eliciting)
  929. return 0;
  930. if (tx_pkt_history_add(h, pkt) == 0)
  931. return 0;
  932. if (pkt->is_inflight) {
  933. if (pkt->is_ack_eliciting) {
  934. ackm->time_of_last_ack_eliciting_pkt[pkt->pkt_space] = pkt->time;
  935. ackm->ack_eliciting_bytes_in_flight[pkt->pkt_space]
  936. += pkt->num_bytes;
  937. }
  938. ackm->bytes_in_flight += pkt->num_bytes;
  939. ackm_set_loss_detection_timer(ackm);
  940. ackm->cc_method->on_data_sent(ackm->cc_data, pkt->num_bytes);
  941. }
  942. return 1;
  943. }
  944. int ossl_ackm_on_rx_datagram(OSSL_ACKM *ackm, size_t num_bytes)
  945. {
  946. /* No-op on the client. */
  947. return 1;
  948. }
  949. static void ackm_process_ecn(OSSL_ACKM *ackm, const OSSL_QUIC_FRAME_ACK *ack,
  950. int pkt_space)
  951. {
  952. struct tx_pkt_history_st *h;
  953. OSSL_ACKM_TX_PKT *pkt;
  954. OSSL_CC_ECN_INFO ecn_info = {0};
  955. /*
  956. * If the ECN-CE counter reported by the peer has increased, this could
  957. * be a new congestion event.
  958. */
  959. if (ack->ecnce > ackm->peer_ecnce[pkt_space]) {
  960. ackm->peer_ecnce[pkt_space] = ack->ecnce;
  961. h = get_tx_history(ackm, pkt_space);
  962. pkt = tx_pkt_history_by_pkt_num(h, ack->ack_ranges[0].end);
  963. if (pkt == NULL)
  964. return;
  965. ecn_info.largest_acked_time = pkt->time;
  966. ackm->cc_method->on_ecn(ackm->cc_data, &ecn_info);
  967. }
  968. }
  969. int ossl_ackm_on_rx_ack_frame(OSSL_ACKM *ackm, const OSSL_QUIC_FRAME_ACK *ack,
  970. int pkt_space, OSSL_TIME rx_time)
  971. {
  972. OSSL_ACKM_TX_PKT *na_pkts, *lost_pkts;
  973. int must_set_timer = 0;
  974. if (ackm->largest_acked_pkt[pkt_space] == QUIC_PN_INVALID)
  975. ackm->largest_acked_pkt[pkt_space] = ack->ack_ranges[0].end;
  976. else
  977. ackm->largest_acked_pkt[pkt_space]
  978. = ossl_quic_pn_max(ackm->largest_acked_pkt[pkt_space],
  979. ack->ack_ranges[0].end);
  980. /*
  981. * If we get an ACK in the handshake space, address validation is completed.
  982. * Make sure we update the timer, even if no packets were ACK'd.
  983. */
  984. if (!ackm->peer_completed_addr_validation
  985. && pkt_space == QUIC_PN_SPACE_HANDSHAKE) {
  986. ackm->peer_completed_addr_validation = 1;
  987. must_set_timer = 1;
  988. }
  989. /*
  990. * Find packets that are newly acknowledged and remove them from the list.
  991. */
  992. na_pkts = ackm_detect_and_remove_newly_acked_pkts(ackm, ack, pkt_space);
  993. if (na_pkts == NULL) {
  994. if (must_set_timer)
  995. ackm_set_loss_detection_timer(ackm);
  996. return 1;
  997. }
  998. /*
  999. * Update the RTT if the largest acknowledged is newly acked and at least
  1000. * one ACK-eliciting packet was newly acked.
  1001. *
  1002. * First packet in the list is always the one with the largest PN.
  1003. */
  1004. if (na_pkts->pkt_num == ack->ack_ranges[0].end &&
  1005. ack_includes_ack_eliciting(na_pkts)) {
  1006. OSSL_TIME now = ackm->now(ackm->now_arg), ack_delay;
  1007. if (ossl_time_is_zero(ackm->first_rtt_sample))
  1008. ackm->first_rtt_sample = now;
  1009. /* Enforce maximum ACK delay. */
  1010. ack_delay = ack->delay_time;
  1011. if (ackm->handshake_confirmed)
  1012. ack_delay = ossl_time_min(ack_delay, ackm->rx_max_ack_delay);
  1013. ossl_statm_update_rtt(ackm->statm, ack_delay,
  1014. ossl_time_subtract(now, na_pkts->time));
  1015. }
  1016. /*
  1017. * Process ECN information if present.
  1018. *
  1019. * We deliberately do most ECN processing in the ACKM rather than the
  1020. * congestion controller to avoid having to give the congestion controller
  1021. * access to ACKM internal state.
  1022. */
  1023. if (ack->ecn_present)
  1024. ackm_process_ecn(ackm, ack, pkt_space);
  1025. /* Handle inferred loss. */
  1026. lost_pkts = ackm_detect_and_remove_lost_pkts(ackm, pkt_space);
  1027. if (lost_pkts != NULL)
  1028. ackm_on_pkts_lost(ackm, pkt_space, lost_pkts, /*pseudo=*/0);
  1029. ackm_on_pkts_acked(ackm, na_pkts);
  1030. /*
  1031. * Reset pto_count unless the client is unsure if the server validated the
  1032. * client's address.
  1033. */
  1034. if (ackm->peer_completed_addr_validation)
  1035. ackm->pto_count = 0;
  1036. ackm_set_loss_detection_timer(ackm);
  1037. return 1;
  1038. }
  1039. int ossl_ackm_on_pkt_space_discarded(OSSL_ACKM *ackm, int pkt_space)
  1040. {
  1041. OSSL_ACKM_TX_PKT *pkt, *pnext;
  1042. uint64_t num_bytes_invalidated = 0;
  1043. if (ackm->discarded[pkt_space])
  1044. return 0;
  1045. if (pkt_space == QUIC_PN_SPACE_HANDSHAKE)
  1046. ackm->peer_completed_addr_validation = 1;
  1047. for (pkt = ossl_list_tx_history_head(&get_tx_history(ackm, pkt_space)->packets);
  1048. pkt != NULL; pkt = pnext) {
  1049. pnext = ossl_list_tx_history_next(pkt);
  1050. if (pkt->is_inflight) {
  1051. ackm->bytes_in_flight -= pkt->num_bytes;
  1052. num_bytes_invalidated += pkt->num_bytes;
  1053. }
  1054. pkt->on_discarded(pkt->cb_arg); /* may free pkt */
  1055. }
  1056. tx_pkt_history_destroy(&ackm->tx_history[pkt_space]);
  1057. rx_pkt_history_destroy(&ackm->rx_history[pkt_space]);
  1058. if (num_bytes_invalidated > 0)
  1059. ackm->cc_method->on_data_invalidated(ackm->cc_data,
  1060. num_bytes_invalidated);
  1061. ackm->time_of_last_ack_eliciting_pkt[pkt_space] = ossl_time_zero();
  1062. ackm->loss_time[pkt_space] = ossl_time_zero();
  1063. ackm->pto_count = 0;
  1064. ackm->discarded[pkt_space] = 1;
  1065. ackm->ack_eliciting_bytes_in_flight[pkt_space] = 0;
  1066. ackm_set_loss_detection_timer(ackm);
  1067. return 1;
  1068. }
  1069. int ossl_ackm_on_handshake_confirmed(OSSL_ACKM *ackm)
  1070. {
  1071. ackm->handshake_confirmed = 1;
  1072. ackm->peer_completed_addr_validation = 1;
  1073. ackm_set_loss_detection_timer(ackm);
  1074. return 1;
  1075. }
  1076. static void ackm_queue_probe_anti_deadlock_handshake(OSSL_ACKM *ackm)
  1077. {
  1078. ++ackm->pending_probe.anti_deadlock_handshake;
  1079. }
  1080. static void ackm_queue_probe_anti_deadlock_initial(OSSL_ACKM *ackm)
  1081. {
  1082. ++ackm->pending_probe.anti_deadlock_initial;
  1083. }
  1084. static void ackm_queue_probe(OSSL_ACKM *ackm, int pkt_space)
  1085. {
  1086. /*
  1087. * TODO(QUIC FUTURE): We are allowed to send either one or two probe
  1088. * packets here.
  1089. * Determine a strategy for when we should send two probe packets.
  1090. */
  1091. ++ackm->pending_probe.pto[pkt_space];
  1092. }
  1093. int ossl_ackm_on_timeout(OSSL_ACKM *ackm)
  1094. {
  1095. int pkt_space;
  1096. OSSL_TIME earliest_loss_time;
  1097. OSSL_ACKM_TX_PKT *lost_pkts;
  1098. earliest_loss_time = ackm_get_loss_time_and_space(ackm, &pkt_space);
  1099. if (!ossl_time_is_zero(earliest_loss_time)) {
  1100. /* Time threshold loss detection. */
  1101. lost_pkts = ackm_detect_and_remove_lost_pkts(ackm, pkt_space);
  1102. if (lost_pkts != NULL)
  1103. ackm_on_pkts_lost(ackm, pkt_space, lost_pkts, /*pseudo=*/0);
  1104. ackm_set_loss_detection_timer(ackm);
  1105. return 1;
  1106. }
  1107. if (ackm_ack_eliciting_bytes_in_flight(ackm) == 0) {
  1108. assert(!ackm->peer_completed_addr_validation);
  1109. /*
  1110. * Client sends an anti-deadlock packet: Initial is padded to earn more
  1111. * anti-amplification credit. A handshake packet proves address
  1112. * ownership.
  1113. */
  1114. if (ackm->discarded[QUIC_PN_SPACE_INITIAL])
  1115. ackm_queue_probe_anti_deadlock_handshake(ackm);
  1116. else
  1117. ackm_queue_probe_anti_deadlock_initial(ackm);
  1118. } else {
  1119. /*
  1120. * PTO. The user of the ACKM should send new data if available, else
  1121. * retransmit old data, or if neither is available, send a single PING
  1122. * frame.
  1123. */
  1124. ackm_get_pto_time_and_space(ackm, &pkt_space);
  1125. ackm_queue_probe(ackm, pkt_space);
  1126. }
  1127. ++ackm->pto_count;
  1128. ackm_set_loss_detection_timer(ackm);
  1129. return 1;
  1130. }
  1131. OSSL_TIME ossl_ackm_get_loss_detection_deadline(OSSL_ACKM *ackm)
  1132. {
  1133. return ackm->loss_detection_deadline;
  1134. }
  1135. OSSL_ACKM_PROBE_INFO *ossl_ackm_get0_probe_request(OSSL_ACKM *ackm)
  1136. {
  1137. return &ackm->pending_probe;
  1138. }
  1139. int ossl_ackm_get_largest_unacked(OSSL_ACKM *ackm, int pkt_space, QUIC_PN *pn)
  1140. {
  1141. struct tx_pkt_history_st *h;
  1142. OSSL_ACKM_TX_PKT *p;
  1143. h = get_tx_history(ackm, pkt_space);
  1144. p = ossl_list_tx_history_tail(&h->packets);
  1145. if (p != NULL) {
  1146. *pn = p->pkt_num;
  1147. return 1;
  1148. }
  1149. return 0;
  1150. }
  1151. /* Number of ACK-eliciting packets RX'd before we always emit an ACK. */
  1152. #define PKTS_BEFORE_ACK 2
  1153. /*
  1154. * Return 1 if emission of an ACK frame is currently desired.
  1155. *
  1156. * This occurs when one or more of the following conditions occurs:
  1157. *
  1158. * - We have flagged that we want to send an ACK frame
  1159. * (for example, due to the packet threshold count being exceeded), or
  1160. *
  1161. * - We have exceeded the ACK flush deadline, meaning that
  1162. * we have received at least one ACK-eliciting packet, but held off on
  1163. * sending an ACK frame immediately in the hope that more ACK-eliciting
  1164. * packets might come in, but not enough did and we are now requesting
  1165. * transmission of an ACK frame anyway.
  1166. *
  1167. */
  1168. int ossl_ackm_is_ack_desired(OSSL_ACKM *ackm, int pkt_space)
  1169. {
  1170. return ackm->rx_ack_desired[pkt_space]
  1171. || (!ossl_time_is_infinite(ackm->rx_ack_flush_deadline[pkt_space])
  1172. && ossl_time_compare(ackm->now(ackm->now_arg),
  1173. ackm->rx_ack_flush_deadline[pkt_space]) >= 0);
  1174. }
  1175. /*
  1176. * Returns 1 if an ACK frame matches a given packet number.
  1177. */
  1178. static int ack_contains(const OSSL_QUIC_FRAME_ACK *ack, QUIC_PN pkt_num)
  1179. {
  1180. size_t i;
  1181. for (i = 0; i < ack->num_ack_ranges; ++i)
  1182. if (range_contains(&ack->ack_ranges[i], pkt_num))
  1183. return 1;
  1184. return 0;
  1185. }
  1186. /*
  1187. * Returns 1 iff a PN (which we have just received) was previously reported as
  1188. * implied missing (by us, in an ACK frame we previously generated).
  1189. */
  1190. static int ackm_is_missing(OSSL_ACKM *ackm, int pkt_space, QUIC_PN pkt_num)
  1191. {
  1192. /*
  1193. * A PN is implied missing if it is not greater than the highest PN in our
  1194. * generated ACK frame, but is not matched by the frame.
  1195. */
  1196. return ackm->ack[pkt_space].num_ack_ranges > 0
  1197. && pkt_num <= ackm->ack[pkt_space].ack_ranges[0].end
  1198. && !ack_contains(&ackm->ack[pkt_space], pkt_num);
  1199. }
  1200. /*
  1201. * Returns 1 iff our RX of a PN newly establishes the implication of missing
  1202. * packets.
  1203. */
  1204. static int ackm_has_newly_missing(OSSL_ACKM *ackm, int pkt_space)
  1205. {
  1206. struct rx_pkt_history_st *h;
  1207. h = get_rx_history(ackm, pkt_space);
  1208. if (ossl_list_uint_set_is_empty(&h->set))
  1209. return 0;
  1210. /*
  1211. * The second condition here establishes that the highest PN range in our RX
  1212. * history comprises only a single PN. If there is more than one, then this
  1213. * function will have returned 1 during a previous call to
  1214. * ossl_ackm_on_rx_packet assuming the third condition below was met. Thus
  1215. * we only return 1 when the missing PN condition is newly established.
  1216. *
  1217. * The third condition here establishes that the highest PN range in our RX
  1218. * history is beyond (and does not border) the highest PN we have yet
  1219. * reported in any ACK frame. Thus there is a gap of at least one PN between
  1220. * the PNs we have ACK'd previously and the PN we have just received.
  1221. */
  1222. return ackm->ack[pkt_space].num_ack_ranges > 0
  1223. && ossl_list_uint_set_tail(&h->set)->range.start
  1224. == ossl_list_uint_set_tail(&h->set)->range.end
  1225. && ossl_list_uint_set_tail(&h->set)->range.start
  1226. > ackm->ack[pkt_space].ack_ranges[0].end + 1;
  1227. }
  1228. static void ackm_set_flush_deadline(OSSL_ACKM *ackm, int pkt_space,
  1229. OSSL_TIME deadline)
  1230. {
  1231. ackm->rx_ack_flush_deadline[pkt_space] = deadline;
  1232. if (ackm->ack_deadline_cb != NULL)
  1233. ackm->ack_deadline_cb(ossl_ackm_get_ack_deadline(ackm, pkt_space),
  1234. pkt_space, ackm->ack_deadline_cb_arg);
  1235. }
  1236. /* Explicitly flags that we want to generate an ACK frame. */
  1237. static void ackm_queue_ack(OSSL_ACKM *ackm, int pkt_space)
  1238. {
  1239. ackm->rx_ack_desired[pkt_space] = 1;
  1240. /* Cancel deadline. */
  1241. ackm_set_flush_deadline(ackm, pkt_space, ossl_time_infinite());
  1242. }
  1243. static void ackm_on_rx_ack_eliciting(OSSL_ACKM *ackm,
  1244. OSSL_TIME rx_time, int pkt_space,
  1245. int was_missing)
  1246. {
  1247. OSSL_TIME tx_max_ack_delay;
  1248. if (ackm->rx_ack_desired[pkt_space])
  1249. /* ACK generation already requested so nothing to do. */
  1250. return;
  1251. ++ackm->rx_ack_eliciting_pkts_since_last_ack[pkt_space];
  1252. if (!ackm->rx_ack_generated[pkt_space]
  1253. || was_missing
  1254. || ackm->rx_ack_eliciting_pkts_since_last_ack[pkt_space]
  1255. >= PKTS_BEFORE_ACK
  1256. || ackm_has_newly_missing(ackm, pkt_space)) {
  1257. /*
  1258. * Either:
  1259. *
  1260. * - We have never yet generated an ACK frame, meaning that this
  1261. * is the first ever packet received, which we should always
  1262. * acknowledge immediately, or
  1263. *
  1264. * - We previously reported the PN that we have just received as
  1265. * missing in a previous ACK frame (meaning that we should report
  1266. * the fact that we now have it to the peer immediately), or
  1267. *
  1268. * - We have exceeded the ACK-eliciting packet threshold count
  1269. * for the purposes of ACK coalescing, so request transmission
  1270. * of an ACK frame, or
  1271. *
  1272. * - The PN we just received and added to our PN RX history
  1273. * newly implies one or more missing PNs, in which case we should
  1274. * inform the peer by sending an ACK frame immediately.
  1275. *
  1276. * We do not test the ACK flush deadline here because it is tested
  1277. * separately in ossl_ackm_is_ack_desired.
  1278. */
  1279. ackm_queue_ack(ackm, pkt_space);
  1280. return;
  1281. }
  1282. /*
  1283. * Not emitting an ACK yet.
  1284. *
  1285. * Update the ACK flush deadline.
  1286. *
  1287. * RFC 9000 s. 13.2.1: "An endpoint MUST acknowledge all ack-eliciting
  1288. * Initial and Handshake packets immediately"; don't delay ACK generation if
  1289. * we are using the Initial or Handshake PN spaces.
  1290. */
  1291. tx_max_ack_delay = ackm->tx_max_ack_delay;
  1292. if (pkt_space == QUIC_PN_SPACE_INITIAL
  1293. || pkt_space == QUIC_PN_SPACE_HANDSHAKE)
  1294. tx_max_ack_delay = ossl_time_zero();
  1295. if (ossl_time_is_infinite(ackm->rx_ack_flush_deadline[pkt_space]))
  1296. ackm_set_flush_deadline(ackm, pkt_space,
  1297. ossl_time_add(rx_time, tx_max_ack_delay));
  1298. else
  1299. ackm_set_flush_deadline(ackm, pkt_space,
  1300. ossl_time_min(ackm->rx_ack_flush_deadline[pkt_space],
  1301. ossl_time_add(rx_time,
  1302. tx_max_ack_delay)));
  1303. }
  1304. int ossl_ackm_on_rx_packet(OSSL_ACKM *ackm, const OSSL_ACKM_RX_PKT *pkt)
  1305. {
  1306. struct rx_pkt_history_st *h = get_rx_history(ackm, pkt->pkt_space);
  1307. int was_missing;
  1308. if (ossl_ackm_is_rx_pn_processable(ackm, pkt->pkt_num, pkt->pkt_space) != 1)
  1309. /* PN has already been processed or written off, no-op. */
  1310. return 1;
  1311. /*
  1312. * Record the largest PN we have RX'd and the time we received it.
  1313. * We use this to calculate the ACK delay field of ACK frames.
  1314. */
  1315. if (pkt->pkt_num > ackm->rx_largest_pn[pkt->pkt_space]) {
  1316. ackm->rx_largest_pn[pkt->pkt_space] = pkt->pkt_num;
  1317. ackm->rx_largest_time[pkt->pkt_space] = pkt->time;
  1318. }
  1319. /*
  1320. * If the PN we just received was previously implied missing by virtue of
  1321. * being omitted from a previous ACK frame generated, we skip any packet
  1322. * count thresholds or coalescing delays and emit a new ACK frame
  1323. * immediately.
  1324. */
  1325. was_missing = ackm_is_missing(ackm, pkt->pkt_space, pkt->pkt_num);
  1326. /*
  1327. * Add the packet number to our history list of PNs we have not yet provably
  1328. * acked.
  1329. */
  1330. if (rx_pkt_history_add_pn(h, pkt->pkt_num) != 1)
  1331. return 0;
  1332. /*
  1333. * Receiving this packet may or may not cause us to emit an ACK frame.
  1334. * We may not emit an ACK frame yet if we have not yet received a threshold
  1335. * number of packets.
  1336. */
  1337. if (pkt->is_ack_eliciting)
  1338. ackm_on_rx_ack_eliciting(ackm, pkt->time, pkt->pkt_space, was_missing);
  1339. /* Update the ECN counters according to which ECN signal we got, if any. */
  1340. switch (pkt->ecn) {
  1341. case OSSL_ACKM_ECN_ECT0:
  1342. ++ackm->rx_ect0[pkt->pkt_space];
  1343. break;
  1344. case OSSL_ACKM_ECN_ECT1:
  1345. ++ackm->rx_ect1[pkt->pkt_space];
  1346. break;
  1347. case OSSL_ACKM_ECN_ECNCE:
  1348. ++ackm->rx_ecnce[pkt->pkt_space];
  1349. break;
  1350. default:
  1351. break;
  1352. }
  1353. return 1;
  1354. }
  1355. static void ackm_fill_rx_ack_ranges(OSSL_ACKM *ackm, int pkt_space,
  1356. OSSL_QUIC_FRAME_ACK *ack)
  1357. {
  1358. struct rx_pkt_history_st *h = get_rx_history(ackm, pkt_space);
  1359. UINT_SET_ITEM *x;
  1360. size_t i = 0;
  1361. /*
  1362. * Copy out ranges from the PN set, starting at the end, until we reach our
  1363. * maximum number of ranges.
  1364. */
  1365. for (x = ossl_list_uint_set_tail(&h->set);
  1366. x != NULL && i < OSSL_NELEM(ackm->ack_ranges);
  1367. x = ossl_list_uint_set_prev(x), ++i) {
  1368. ackm->ack_ranges[pkt_space][i].start = x->range.start;
  1369. ackm->ack_ranges[pkt_space][i].end = x->range.end;
  1370. }
  1371. ack->ack_ranges = ackm->ack_ranges[pkt_space];
  1372. ack->num_ack_ranges = i;
  1373. }
  1374. const OSSL_QUIC_FRAME_ACK *ossl_ackm_get_ack_frame(OSSL_ACKM *ackm,
  1375. int pkt_space)
  1376. {
  1377. OSSL_QUIC_FRAME_ACK *ack = &ackm->ack[pkt_space];
  1378. OSSL_TIME now = ackm->now(ackm->now_arg);
  1379. ackm_fill_rx_ack_ranges(ackm, pkt_space, ack);
  1380. if (!ossl_time_is_zero(ackm->rx_largest_time[pkt_space])
  1381. && ossl_time_compare(now, ackm->rx_largest_time[pkt_space]) > 0
  1382. && pkt_space == QUIC_PN_SPACE_APP)
  1383. ack->delay_time =
  1384. ossl_time_subtract(now, ackm->rx_largest_time[pkt_space]);
  1385. else
  1386. ack->delay_time = ossl_time_zero();
  1387. ack->ect0 = ackm->rx_ect0[pkt_space];
  1388. ack->ect1 = ackm->rx_ect1[pkt_space];
  1389. ack->ecnce = ackm->rx_ecnce[pkt_space];
  1390. ack->ecn_present = 1;
  1391. ackm->rx_ack_eliciting_pkts_since_last_ack[pkt_space] = 0;
  1392. ackm->rx_ack_generated[pkt_space] = 1;
  1393. ackm->rx_ack_desired[pkt_space] = 0;
  1394. ackm_set_flush_deadline(ackm, pkt_space, ossl_time_infinite());
  1395. return ack;
  1396. }
  1397. OSSL_TIME ossl_ackm_get_ack_deadline(OSSL_ACKM *ackm, int pkt_space)
  1398. {
  1399. if (ackm->rx_ack_desired[pkt_space])
  1400. /* Already desired, deadline is now. */
  1401. return ossl_time_zero();
  1402. return ackm->rx_ack_flush_deadline[pkt_space];
  1403. }
  1404. int ossl_ackm_is_rx_pn_processable(OSSL_ACKM *ackm, QUIC_PN pn, int pkt_space)
  1405. {
  1406. struct rx_pkt_history_st *h = get_rx_history(ackm, pkt_space);
  1407. return pn >= h->watermark && ossl_uint_set_query(&h->set, pn) == 0;
  1408. }
  1409. void ossl_ackm_set_loss_detection_deadline_callback(OSSL_ACKM *ackm,
  1410. void (*fn)(OSSL_TIME deadline,
  1411. void *arg),
  1412. void *arg)
  1413. {
  1414. ackm->loss_detection_deadline_cb = fn;
  1415. ackm->loss_detection_deadline_cb_arg = arg;
  1416. }
  1417. void ossl_ackm_set_ack_deadline_callback(OSSL_ACKM *ackm,
  1418. void (*fn)(OSSL_TIME deadline,
  1419. int pkt_space,
  1420. void *arg),
  1421. void *arg)
  1422. {
  1423. ackm->ack_deadline_cb = fn;
  1424. ackm->ack_deadline_cb_arg = arg;
  1425. }
  1426. int ossl_ackm_mark_packet_pseudo_lost(OSSL_ACKM *ackm,
  1427. int pkt_space, QUIC_PN pn)
  1428. {
  1429. struct tx_pkt_history_st *h = get_tx_history(ackm, pkt_space);
  1430. OSSL_ACKM_TX_PKT *pkt;
  1431. pkt = tx_pkt_history_by_pkt_num(h, pn);
  1432. if (pkt == NULL)
  1433. return 0;
  1434. tx_pkt_history_remove(h, pkt->pkt_num);
  1435. pkt->lnext = NULL;
  1436. ackm_on_pkts_lost(ackm, pkt_space, pkt, /*pseudo=*/1);
  1437. return 1;
  1438. }
  1439. OSSL_TIME ossl_ackm_get_pto_duration(OSSL_ACKM *ackm)
  1440. {
  1441. OSSL_TIME duration;
  1442. OSSL_RTT_INFO rtt;
  1443. ossl_statm_get_rtt_info(ackm->statm, &rtt);
  1444. duration = ossl_time_add(rtt.smoothed_rtt,
  1445. ossl_time_max(ossl_time_multiply(rtt.rtt_variance, 4),
  1446. ossl_ticks2time(K_GRANULARITY)));
  1447. if (!ossl_time_is_infinite(ackm->rx_max_ack_delay))
  1448. duration = ossl_time_add(duration, ackm->rx_max_ack_delay);
  1449. return duration;
  1450. }
  1451. QUIC_PN ossl_ackm_get_largest_acked(OSSL_ACKM *ackm, int pkt_space)
  1452. {
  1453. return ackm->largest_acked_pkt[pkt_space];
  1454. }
  1455. void ossl_ackm_set_rx_max_ack_delay(OSSL_ACKM *ackm, OSSL_TIME rx_max_ack_delay)
  1456. {
  1457. ackm->rx_max_ack_delay = rx_max_ack_delay;
  1458. }
  1459. void ossl_ackm_set_tx_max_ack_delay(OSSL_ACKM *ackm, OSSL_TIME tx_max_ack_delay)
  1460. {
  1461. ackm->tx_max_ack_delay = tx_max_ack_delay;
  1462. }