quic_ackm.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744
  1. /*
  2. * Copyright 2022-2025 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 attached to server channel */
  478. char is_server;
  479. /* Set to 1 when the peer has completed address validation. */
  480. char peer_completed_addr_validation;
  481. /* Set to 1 when a PN space has been discarded. */
  482. char discarded[QUIC_PN_SPACE_NUM];
  483. /* Set to 1 when we think an ACK frame should be generated. */
  484. char rx_ack_desired[QUIC_PN_SPACE_NUM];
  485. /* Set to 1 if an ACK frame has ever been generated. */
  486. char rx_ack_generated[QUIC_PN_SPACE_NUM];
  487. /* Probe request counts for reporting to the user. */
  488. OSSL_ACKM_PROBE_INFO pending_probe;
  489. /* Generated ACK frames for each PN space. */
  490. OSSL_QUIC_FRAME_ACK ack[QUIC_PN_SPACE_NUM];
  491. OSSL_QUIC_ACK_RANGE ack_ranges[QUIC_PN_SPACE_NUM][MAX_RX_ACK_RANGES];
  492. /* Other RX state. */
  493. /* Largest PN we have RX'd. */
  494. QUIC_PN rx_largest_pn[QUIC_PN_SPACE_NUM];
  495. /* Time at which the PN in rx_largest_pn was RX'd. */
  496. OSSL_TIME rx_largest_time[QUIC_PN_SPACE_NUM];
  497. /*
  498. * ECN event counters. Each time we receive a packet with a given ECN label,
  499. * the corresponding ECN counter here is incremented.
  500. */
  501. uint64_t rx_ect0[QUIC_PN_SPACE_NUM];
  502. uint64_t rx_ect1[QUIC_PN_SPACE_NUM];
  503. uint64_t rx_ecnce[QUIC_PN_SPACE_NUM];
  504. /*
  505. * Number of ACK-eliciting packets since last ACK. We use this to defer
  506. * emitting ACK frames until a threshold number of ACK-eliciting packets
  507. * have been received.
  508. */
  509. uint32_t rx_ack_eliciting_pkts_since_last_ack[QUIC_PN_SPACE_NUM];
  510. /*
  511. * The ACK frame coalescing deadline at which we should flush any unsent ACK
  512. * frames.
  513. */
  514. OSSL_TIME rx_ack_flush_deadline[QUIC_PN_SPACE_NUM];
  515. /*
  516. * The RX maximum ACK delay (the maximum amount of time our peer might
  517. * wait to send us an ACK after receiving an ACK-eliciting packet).
  518. */
  519. OSSL_TIME rx_max_ack_delay;
  520. /*
  521. * The TX maximum ACK delay (the maximum amount of time we allow ourselves
  522. * to wait before generating an ACK after receiving an ACK-eliciting
  523. * packet).
  524. */
  525. OSSL_TIME tx_max_ack_delay;
  526. /* Callbacks for deadline updates. */
  527. void (*loss_detection_deadline_cb)(OSSL_TIME deadline, void *arg);
  528. void *loss_detection_deadline_cb_arg;
  529. void (*ack_deadline_cb)(OSSL_TIME deadline, int pkt_space, void *arg);
  530. void *ack_deadline_cb_arg;
  531. };
  532. static ossl_inline uint32_t min_u32(uint32_t x, uint32_t y)
  533. {
  534. return x < y ? x : y;
  535. }
  536. /*
  537. * Get TX history for a given packet number space. Must not have been
  538. * discarded.
  539. */
  540. static struct tx_pkt_history_st *get_tx_history(OSSL_ACKM *ackm, int pkt_space)
  541. {
  542. assert(!ackm->discarded[pkt_space]);
  543. return &ackm->tx_history[pkt_space];
  544. }
  545. /*
  546. * Get RX history for a given packet number space. Must not have been
  547. * discarded.
  548. */
  549. static struct rx_pkt_history_st *get_rx_history(OSSL_ACKM *ackm, int pkt_space)
  550. {
  551. assert(!ackm->discarded[pkt_space]);
  552. return &ackm->rx_history[pkt_space];
  553. }
  554. /* Does the newly-acknowledged list contain any ack-eliciting packet? */
  555. static int ack_includes_ack_eliciting(OSSL_ACKM_TX_PKT *pkt)
  556. {
  557. for (; pkt != NULL; pkt = pkt->anext)
  558. if (pkt->is_ack_eliciting)
  559. return 1;
  560. return 0;
  561. }
  562. /* Return number of ACK-eliciting bytes in flight across all PN spaces. */
  563. static uint64_t ackm_ack_eliciting_bytes_in_flight(OSSL_ACKM *ackm)
  564. {
  565. int i;
  566. uint64_t total = 0;
  567. for (i = 0; i < QUIC_PN_SPACE_NUM; ++i)
  568. total += ackm->ack_eliciting_bytes_in_flight[i];
  569. return total;
  570. }
  571. /* Return 1 if the range contains the given PN. */
  572. static int range_contains(const OSSL_QUIC_ACK_RANGE *range, QUIC_PN pn)
  573. {
  574. return pn >= range->start && pn <= range->end;
  575. }
  576. /*
  577. * Given a logical representation of an ACK frame 'ack', create a singly-linked
  578. * list of the newly ACK'd frames; that is, of frames which are matched by the
  579. * list of PN ranges contained in the ACK frame. The packet structures in the
  580. * list returned are removed from the TX history list. Returns a pointer to the
  581. * list head (or NULL) if empty.
  582. */
  583. static OSSL_ACKM_TX_PKT *ackm_detect_and_remove_newly_acked_pkts(OSSL_ACKM *ackm,
  584. const OSSL_QUIC_FRAME_ACK *ack,
  585. int pkt_space)
  586. {
  587. OSSL_ACKM_TX_PKT *acked_pkts = NULL, **fixup = &acked_pkts, *pkt, *pprev;
  588. struct tx_pkt_history_st *h;
  589. size_t ridx = 0;
  590. assert(ack->num_ack_ranges > 0);
  591. /*
  592. * Our history list is a list of packets sorted in ascending order
  593. * by packet number.
  594. *
  595. * ack->ack_ranges is a list of packet number ranges in descending order.
  596. *
  597. * Walk through our history list from the end in order to efficiently detect
  598. * membership in the specified ack ranges. As an optimization, we use our
  599. * hashtable to try and skip to the first matching packet. This may fail if
  600. * the ACK ranges given include nonexistent packets.
  601. */
  602. h = get_tx_history(ackm, pkt_space);
  603. pkt = tx_pkt_history_by_pkt_num(h, ack->ack_ranges[0].end);
  604. if (pkt == NULL)
  605. pkt = ossl_list_tx_history_tail(&h->packets);
  606. for (; pkt != NULL; pkt = pprev) {
  607. /*
  608. * Save prev value as it will be zeroed if we remove the packet from the
  609. * history list below.
  610. */
  611. pprev = ossl_list_tx_history_prev(pkt);
  612. for (;; ++ridx) {
  613. if (ridx >= ack->num_ack_ranges) {
  614. /*
  615. * We have exhausted all ranges so stop here, even if there are
  616. * more packets to look at.
  617. */
  618. goto stop;
  619. }
  620. if (range_contains(&ack->ack_ranges[ridx], pkt->pkt_num)) {
  621. /* We have matched this range. */
  622. tx_pkt_history_remove(h, pkt->pkt_num);
  623. *fixup = pkt;
  624. fixup = &pkt->anext;
  625. *fixup = NULL;
  626. break;
  627. } else if (pkt->pkt_num > ack->ack_ranges[ridx].end) {
  628. /*
  629. * We have not reached this range yet in our list, so do not
  630. * advance ridx.
  631. */
  632. break;
  633. } else {
  634. /*
  635. * We have moved beyond this range, so advance to the next range
  636. * and try matching again.
  637. */
  638. assert(pkt->pkt_num < ack->ack_ranges[ridx].start);
  639. continue;
  640. }
  641. }
  642. }
  643. stop:
  644. return acked_pkts;
  645. }
  646. /*
  647. * Create a singly-linked list of newly detected-lost packets in the given
  648. * packet number space. Returns the head of the list or NULL if no packets were
  649. * detected lost. The packets in the list are removed from the TX history list.
  650. */
  651. static OSSL_ACKM_TX_PKT *ackm_detect_and_remove_lost_pkts(OSSL_ACKM *ackm,
  652. int pkt_space)
  653. {
  654. OSSL_ACKM_TX_PKT *lost_pkts = NULL, **fixup = &lost_pkts, *pkt, *pnext;
  655. OSSL_TIME loss_delay, lost_send_time, now;
  656. OSSL_RTT_INFO rtt;
  657. struct tx_pkt_history_st *h;
  658. assert(ackm->largest_acked_pkt[pkt_space] != QUIC_PN_INVALID);
  659. ossl_statm_get_rtt_info(ackm->statm, &rtt);
  660. ackm->loss_time[pkt_space] = ossl_time_zero();
  661. loss_delay = ossl_time_multiply(ossl_time_max(rtt.latest_rtt,
  662. rtt.smoothed_rtt),
  663. K_TIME_THRESHOLD_NUM);
  664. loss_delay = ossl_time_divide(loss_delay, K_TIME_THRESHOLD_DEN);
  665. /* Minimum time of K_GRANULARITY before packets are deemed lost. */
  666. loss_delay = ossl_time_max(loss_delay, ossl_ticks2time(K_GRANULARITY));
  667. /* Packets sent before this time are deemed lost. */
  668. now = ackm->now(ackm->now_arg);
  669. lost_send_time = ossl_time_subtract(now, loss_delay);
  670. h = get_tx_history(ackm, pkt_space);
  671. pkt = ossl_list_tx_history_head(&h->packets);
  672. for (; pkt != NULL; pkt = pnext) {
  673. assert(pkt_space == pkt->pkt_space);
  674. /*
  675. * Save prev value as it will be zeroed if we remove the packet from the
  676. * history list below.
  677. */
  678. pnext = ossl_list_tx_history_next(pkt);
  679. if (pkt->pkt_num > ackm->largest_acked_pkt[pkt_space])
  680. continue;
  681. /*
  682. * Mark packet as lost, or set time when it should be marked.
  683. */
  684. if (ossl_time_compare(pkt->time, lost_send_time) <= 0
  685. || ackm->largest_acked_pkt[pkt_space]
  686. >= pkt->pkt_num + K_PKT_THRESHOLD) {
  687. tx_pkt_history_remove(h, pkt->pkt_num);
  688. *fixup = pkt;
  689. fixup = &pkt->lnext;
  690. *fixup = NULL;
  691. } else {
  692. if (ossl_time_is_zero(ackm->loss_time[pkt_space]))
  693. ackm->loss_time[pkt_space] =
  694. ossl_time_add(pkt->time, loss_delay);
  695. else
  696. ackm->loss_time[pkt_space] =
  697. ossl_time_min(ackm->loss_time[pkt_space],
  698. ossl_time_add(pkt->time, loss_delay));
  699. }
  700. }
  701. return lost_pkts;
  702. }
  703. static OSSL_TIME ackm_get_loss_time_and_space(OSSL_ACKM *ackm, int *pspace)
  704. {
  705. OSSL_TIME time = ackm->loss_time[QUIC_PN_SPACE_INITIAL];
  706. int i, space = QUIC_PN_SPACE_INITIAL;
  707. for (i = space + 1; i < QUIC_PN_SPACE_NUM; ++i)
  708. if (ossl_time_is_zero(time)
  709. || ossl_time_compare(ackm->loss_time[i], time) == -1) {
  710. time = ackm->loss_time[i];
  711. space = i;
  712. }
  713. *pspace = space;
  714. return time;
  715. }
  716. static OSSL_TIME ackm_get_pto_time_and_space(OSSL_ACKM *ackm, int *space)
  717. {
  718. OSSL_RTT_INFO rtt;
  719. OSSL_TIME duration;
  720. OSSL_TIME pto_timeout = ossl_time_infinite(), t;
  721. int pto_space = QUIC_PN_SPACE_INITIAL, i;
  722. ossl_statm_get_rtt_info(ackm->statm, &rtt);
  723. duration
  724. = ossl_time_add(rtt.smoothed_rtt,
  725. ossl_time_max(ossl_time_multiply(rtt.rtt_variance, 4),
  726. ossl_ticks2time(K_GRANULARITY)));
  727. duration
  728. = ossl_time_multiply(duration,
  729. (uint64_t)1 << min_u32(ackm->pto_count,
  730. MAX_PTO_COUNT));
  731. /* Anti-deadlock PTO starts from the current time. */
  732. if (ackm_ack_eliciting_bytes_in_flight(ackm) == 0) {
  733. assert(!ackm->peer_completed_addr_validation);
  734. *space = ackm->discarded[QUIC_PN_SPACE_INITIAL]
  735. ? QUIC_PN_SPACE_HANDSHAKE
  736. : QUIC_PN_SPACE_INITIAL;
  737. return ossl_time_add(ackm->now(ackm->now_arg), duration);
  738. }
  739. for (i = QUIC_PN_SPACE_INITIAL; i < QUIC_PN_SPACE_NUM; ++i) {
  740. /*
  741. * RFC 9002 section 6.2.2.1 keep probe timeout armed until
  742. * handshake is confirmed (client sees HANDSHAKE_DONE message
  743. * from server).
  744. */
  745. if (ackm->ack_eliciting_bytes_in_flight[i] == 0 &&
  746. (ackm->handshake_confirmed == 1 || ackm->is_server == 1))
  747. continue;
  748. if (i == QUIC_PN_SPACE_APP) {
  749. /* Skip application data until handshake confirmed. */
  750. if (!ackm->handshake_confirmed)
  751. break;
  752. /* Include max_ack_delay and backoff for app data. */
  753. if (!ossl_time_is_infinite(ackm->rx_max_ack_delay)) {
  754. uint64_t factor
  755. = (uint64_t)1 << min_u32(ackm->pto_count, MAX_PTO_COUNT);
  756. duration
  757. = ossl_time_add(duration,
  758. ossl_time_multiply(ackm->rx_max_ack_delay,
  759. factor));
  760. }
  761. }
  762. /*
  763. * Only re-arm timer if stack has sent at least one ACK eliciting frame.
  764. * If stack has sent no ACK eliciting frame at given encryption level then
  765. * particular timer is zero and we must not attempt to set it. Timer keeps
  766. * time since epoch (Jan 1 1970) and we must not set timer to past.
  767. */
  768. if (!ossl_time_is_zero(ackm->time_of_last_ack_eliciting_pkt[i])) {
  769. t = ossl_time_add(ackm->time_of_last_ack_eliciting_pkt[i], duration);
  770. if (ossl_time_compare(t, pto_timeout) < 0) {
  771. pto_timeout = t;
  772. pto_space = i;
  773. }
  774. }
  775. }
  776. *space = pto_space;
  777. return pto_timeout;
  778. }
  779. static void ackm_set_loss_detection_timer_actual(OSSL_ACKM *ackm,
  780. OSSL_TIME deadline)
  781. {
  782. ackm->loss_detection_deadline = deadline;
  783. if (ackm->loss_detection_deadline_cb != NULL)
  784. ackm->loss_detection_deadline_cb(deadline,
  785. ackm->loss_detection_deadline_cb_arg);
  786. }
  787. static int ackm_set_loss_detection_timer(OSSL_ACKM *ackm)
  788. {
  789. int space;
  790. OSSL_TIME earliest_loss_time, timeout;
  791. earliest_loss_time = ackm_get_loss_time_and_space(ackm, &space);
  792. if (!ossl_time_is_zero(earliest_loss_time)) {
  793. /* Time threshold loss detection. */
  794. ackm_set_loss_detection_timer_actual(ackm, earliest_loss_time);
  795. return 1;
  796. }
  797. if (ackm_ack_eliciting_bytes_in_flight(ackm) == 0
  798. && ackm->peer_completed_addr_validation) {
  799. /*
  800. * Nothing to detect lost, so no timer is set. However, the client
  801. * needs to arm the timer if the server might be blocked by the
  802. * anti-amplification limit.
  803. */
  804. ackm_set_loss_detection_timer_actual(ackm, ossl_time_zero());
  805. return 1;
  806. }
  807. timeout = ackm_get_pto_time_and_space(ackm, &space);
  808. ackm_set_loss_detection_timer_actual(ackm, timeout);
  809. return 1;
  810. }
  811. static int ackm_in_persistent_congestion(OSSL_ACKM *ackm,
  812. const OSSL_ACKM_TX_PKT *lpkt)
  813. {
  814. /* TODO(QUIC FUTURE): Persistent congestion not currently implemented. */
  815. return 0;
  816. }
  817. static void ackm_on_pkts_lost(OSSL_ACKM *ackm, int pkt_space,
  818. const OSSL_ACKM_TX_PKT *lpkt, int pseudo)
  819. {
  820. const OSSL_ACKM_TX_PKT *p, *pnext;
  821. OSSL_RTT_INFO rtt;
  822. QUIC_PN largest_pn_lost = 0;
  823. OSSL_CC_LOSS_INFO loss_info = {0};
  824. uint32_t flags = 0;
  825. for (p = lpkt; p != NULL; p = pnext) {
  826. pnext = p->lnext;
  827. if (p->is_inflight) {
  828. ackm->bytes_in_flight -= p->num_bytes;
  829. if (p->is_ack_eliciting)
  830. ackm->ack_eliciting_bytes_in_flight[p->pkt_space]
  831. -= p->num_bytes;
  832. if (p->pkt_num > largest_pn_lost)
  833. largest_pn_lost = p->pkt_num;
  834. if (!pseudo) {
  835. /*
  836. * If this is pseudo-loss (e.g. during connection retry) we do not
  837. * inform the CC as it is not a real loss and not reflective of
  838. * network conditions.
  839. */
  840. loss_info.tx_time = p->time;
  841. loss_info.tx_size = p->num_bytes;
  842. ackm->cc_method->on_data_lost(ackm->cc_data, &loss_info);
  843. }
  844. }
  845. p->on_lost(p->cb_arg);
  846. }
  847. /*
  848. * Persistent congestion can only be considered if we have gotten at least
  849. * one RTT sample.
  850. */
  851. ossl_statm_get_rtt_info(ackm->statm, &rtt);
  852. if (!ossl_time_is_zero(ackm->first_rtt_sample)
  853. && ackm_in_persistent_congestion(ackm, lpkt))
  854. flags |= OSSL_CC_LOST_FLAG_PERSISTENT_CONGESTION;
  855. ackm->cc_method->on_data_lost_finished(ackm->cc_data, flags);
  856. }
  857. static void ackm_on_pkts_acked(OSSL_ACKM *ackm, const OSSL_ACKM_TX_PKT *apkt)
  858. {
  859. const OSSL_ACKM_TX_PKT *anext;
  860. QUIC_PN last_pn_acked = 0;
  861. OSSL_CC_ACK_INFO ainfo = {0};
  862. for (; apkt != NULL; apkt = anext) {
  863. if (apkt->is_inflight) {
  864. ackm->bytes_in_flight -= apkt->num_bytes;
  865. if (apkt->is_ack_eliciting)
  866. ackm->ack_eliciting_bytes_in_flight[apkt->pkt_space]
  867. -= apkt->num_bytes;
  868. if (apkt->pkt_num > last_pn_acked)
  869. last_pn_acked = apkt->pkt_num;
  870. if (apkt->largest_acked != QUIC_PN_INVALID)
  871. /*
  872. * This can fail, but it is monotonic; worst case we try again
  873. * next time.
  874. */
  875. rx_pkt_history_bump_watermark(get_rx_history(ackm,
  876. apkt->pkt_space),
  877. apkt->largest_acked + 1);
  878. }
  879. ainfo.tx_time = apkt->time;
  880. ainfo.tx_size = apkt->num_bytes;
  881. anext = apkt->anext;
  882. apkt->on_acked(apkt->cb_arg); /* may free apkt */
  883. if (apkt->is_inflight)
  884. ackm->cc_method->on_data_acked(ackm->cc_data, &ainfo);
  885. }
  886. }
  887. OSSL_ACKM *ossl_ackm_new(OSSL_TIME (*now)(void *arg),
  888. void *now_arg,
  889. OSSL_STATM *statm,
  890. const OSSL_CC_METHOD *cc_method,
  891. OSSL_CC_DATA *cc_data,
  892. int is_server)
  893. {
  894. OSSL_ACKM *ackm;
  895. int i;
  896. ackm = OPENSSL_zalloc(sizeof(OSSL_ACKM));
  897. if (ackm == NULL)
  898. return NULL;
  899. for (i = 0; i < (int)OSSL_NELEM(ackm->tx_history); ++i) {
  900. ackm->largest_acked_pkt[i] = QUIC_PN_INVALID;
  901. ackm->rx_ack_flush_deadline[i] = ossl_time_infinite();
  902. if (tx_pkt_history_init(&ackm->tx_history[i]) < 1)
  903. goto err;
  904. }
  905. for (i = 0; i < (int)OSSL_NELEM(ackm->rx_history); ++i)
  906. rx_pkt_history_init(&ackm->rx_history[i]);
  907. ackm->now = now;
  908. ackm->now_arg = now_arg;
  909. ackm->statm = statm;
  910. ackm->cc_method = cc_method;
  911. ackm->cc_data = cc_data;
  912. ackm->is_server = (char)is_server;
  913. ackm->rx_max_ack_delay = ossl_ms2time(QUIC_DEFAULT_MAX_ACK_DELAY);
  914. ackm->tx_max_ack_delay = DEFAULT_TX_MAX_ACK_DELAY;
  915. return ackm;
  916. err:
  917. while (--i >= 0)
  918. tx_pkt_history_destroy(&ackm->tx_history[i]);
  919. OPENSSL_free(ackm);
  920. return NULL;
  921. }
  922. void ossl_ackm_free(OSSL_ACKM *ackm)
  923. {
  924. size_t i;
  925. if (ackm == NULL)
  926. return;
  927. for (i = 0; i < OSSL_NELEM(ackm->tx_history); ++i)
  928. if (!ackm->discarded[i]) {
  929. tx_pkt_history_destroy(&ackm->tx_history[i]);
  930. rx_pkt_history_destroy(&ackm->rx_history[i]);
  931. }
  932. OPENSSL_free(ackm);
  933. }
  934. int ossl_ackm_on_tx_packet(OSSL_ACKM *ackm, OSSL_ACKM_TX_PKT *pkt)
  935. {
  936. struct tx_pkt_history_st *h = get_tx_history(ackm, pkt->pkt_space);
  937. /* Time must be set and not move backwards. */
  938. if (ossl_time_is_zero(pkt->time)
  939. || ossl_time_compare(ackm->time_of_last_ack_eliciting_pkt[pkt->pkt_space],
  940. pkt->time) > 0)
  941. return 0;
  942. /* Must have non-zero number of bytes. */
  943. if (pkt->num_bytes == 0)
  944. return 0;
  945. /* Does not make any sense for a non-in-flight packet to be ACK-eliciting. */
  946. if (!pkt->is_inflight && pkt->is_ack_eliciting)
  947. return 0;
  948. if (tx_pkt_history_add(h, pkt) == 0)
  949. return 0;
  950. if (pkt->is_inflight) {
  951. if (pkt->is_ack_eliciting) {
  952. ackm->time_of_last_ack_eliciting_pkt[pkt->pkt_space] = pkt->time;
  953. ackm->ack_eliciting_bytes_in_flight[pkt->pkt_space]
  954. += pkt->num_bytes;
  955. }
  956. ackm->bytes_in_flight += pkt->num_bytes;
  957. ackm_set_loss_detection_timer(ackm);
  958. ackm->cc_method->on_data_sent(ackm->cc_data, pkt->num_bytes);
  959. }
  960. return 1;
  961. }
  962. int ossl_ackm_on_rx_datagram(OSSL_ACKM *ackm, size_t num_bytes)
  963. {
  964. /* No-op on the client. */
  965. return 1;
  966. }
  967. static void ackm_process_ecn(OSSL_ACKM *ackm, const OSSL_QUIC_FRAME_ACK *ack,
  968. int pkt_space)
  969. {
  970. struct tx_pkt_history_st *h;
  971. OSSL_ACKM_TX_PKT *pkt;
  972. OSSL_CC_ECN_INFO ecn_info = {0};
  973. /*
  974. * If the ECN-CE counter reported by the peer has increased, this could
  975. * be a new congestion event.
  976. */
  977. if (ack->ecnce > ackm->peer_ecnce[pkt_space]) {
  978. ackm->peer_ecnce[pkt_space] = ack->ecnce;
  979. h = get_tx_history(ackm, pkt_space);
  980. pkt = tx_pkt_history_by_pkt_num(h, ack->ack_ranges[0].end);
  981. if (pkt == NULL)
  982. return;
  983. ecn_info.largest_acked_time = pkt->time;
  984. ackm->cc_method->on_ecn(ackm->cc_data, &ecn_info);
  985. }
  986. }
  987. int ossl_ackm_on_rx_ack_frame(OSSL_ACKM *ackm, const OSSL_QUIC_FRAME_ACK *ack,
  988. int pkt_space, OSSL_TIME rx_time)
  989. {
  990. OSSL_ACKM_TX_PKT *na_pkts, *lost_pkts;
  991. int must_set_timer = 0;
  992. if (ackm->largest_acked_pkt[pkt_space] == QUIC_PN_INVALID)
  993. ackm->largest_acked_pkt[pkt_space] = ack->ack_ranges[0].end;
  994. else
  995. ackm->largest_acked_pkt[pkt_space]
  996. = ossl_quic_pn_max(ackm->largest_acked_pkt[pkt_space],
  997. ack->ack_ranges[0].end);
  998. /*
  999. * If we get an ACK in the handshake space, address validation is completed.
  1000. * Make sure we update the timer, even if no packets were ACK'd.
  1001. */
  1002. if (!ackm->peer_completed_addr_validation
  1003. && pkt_space == QUIC_PN_SPACE_HANDSHAKE) {
  1004. ackm->peer_completed_addr_validation = 1;
  1005. must_set_timer = 1;
  1006. }
  1007. /*
  1008. * Find packets that are newly acknowledged and remove them from the list.
  1009. */
  1010. na_pkts = ackm_detect_and_remove_newly_acked_pkts(ackm, ack, pkt_space);
  1011. if (na_pkts == NULL) {
  1012. if (must_set_timer)
  1013. ackm_set_loss_detection_timer(ackm);
  1014. return 1;
  1015. }
  1016. /*
  1017. * Update the RTT if the largest acknowledged is newly acked and at least
  1018. * one ACK-eliciting packet was newly acked.
  1019. *
  1020. * First packet in the list is always the one with the largest PN.
  1021. */
  1022. if (na_pkts->pkt_num == ack->ack_ranges[0].end &&
  1023. ack_includes_ack_eliciting(na_pkts)) {
  1024. OSSL_TIME now = ackm->now(ackm->now_arg), ack_delay;
  1025. if (ossl_time_is_zero(ackm->first_rtt_sample))
  1026. ackm->first_rtt_sample = now;
  1027. /* Enforce maximum ACK delay. */
  1028. ack_delay = ack->delay_time;
  1029. if (ackm->handshake_confirmed)
  1030. ack_delay = ossl_time_min(ack_delay, ackm->rx_max_ack_delay);
  1031. ossl_statm_update_rtt(ackm->statm, ack_delay,
  1032. ossl_time_subtract(now, na_pkts->time));
  1033. }
  1034. /*
  1035. * Process ECN information if present.
  1036. *
  1037. * We deliberately do most ECN processing in the ACKM rather than the
  1038. * congestion controller to avoid having to give the congestion controller
  1039. * access to ACKM internal state.
  1040. */
  1041. if (ack->ecn_present)
  1042. ackm_process_ecn(ackm, ack, pkt_space);
  1043. /* Handle inferred loss. */
  1044. lost_pkts = ackm_detect_and_remove_lost_pkts(ackm, pkt_space);
  1045. if (lost_pkts != NULL)
  1046. ackm_on_pkts_lost(ackm, pkt_space, lost_pkts, /*pseudo=*/0);
  1047. ackm_on_pkts_acked(ackm, na_pkts);
  1048. /*
  1049. * Reset pto_count unless the client is unsure if the server validated the
  1050. * client's address.
  1051. */
  1052. if (ackm->peer_completed_addr_validation)
  1053. ackm->pto_count = 0;
  1054. ackm_set_loss_detection_timer(ackm);
  1055. return 1;
  1056. }
  1057. int ossl_ackm_on_pkt_space_discarded(OSSL_ACKM *ackm, int pkt_space)
  1058. {
  1059. OSSL_ACKM_TX_PKT *pkt, *pnext;
  1060. uint64_t num_bytes_invalidated = 0;
  1061. if (ackm->discarded[pkt_space])
  1062. return 0;
  1063. if (pkt_space == QUIC_PN_SPACE_HANDSHAKE)
  1064. ackm->peer_completed_addr_validation = 1;
  1065. for (pkt = ossl_list_tx_history_head(&get_tx_history(ackm, pkt_space)->packets);
  1066. pkt != NULL; pkt = pnext) {
  1067. pnext = ossl_list_tx_history_next(pkt);
  1068. if (pkt->is_inflight) {
  1069. ackm->bytes_in_flight -= pkt->num_bytes;
  1070. num_bytes_invalidated += pkt->num_bytes;
  1071. }
  1072. pkt->on_discarded(pkt->cb_arg); /* may free pkt */
  1073. }
  1074. tx_pkt_history_destroy(&ackm->tx_history[pkt_space]);
  1075. rx_pkt_history_destroy(&ackm->rx_history[pkt_space]);
  1076. if (num_bytes_invalidated > 0)
  1077. ackm->cc_method->on_data_invalidated(ackm->cc_data,
  1078. num_bytes_invalidated);
  1079. ackm->time_of_last_ack_eliciting_pkt[pkt_space] = ossl_time_zero();
  1080. ackm->loss_time[pkt_space] = ossl_time_zero();
  1081. ackm->pto_count = 0;
  1082. ackm->discarded[pkt_space] = 1;
  1083. ackm->ack_eliciting_bytes_in_flight[pkt_space] = 0;
  1084. ackm_set_loss_detection_timer(ackm);
  1085. return 1;
  1086. }
  1087. int ossl_ackm_on_handshake_confirmed(OSSL_ACKM *ackm)
  1088. {
  1089. ackm->handshake_confirmed = 1;
  1090. ackm->peer_completed_addr_validation = 1;
  1091. ackm_set_loss_detection_timer(ackm);
  1092. return 1;
  1093. }
  1094. static void ackm_queue_probe_anti_deadlock_handshake(OSSL_ACKM *ackm)
  1095. {
  1096. ++ackm->pending_probe.anti_deadlock_handshake;
  1097. }
  1098. static void ackm_queue_probe_anti_deadlock_initial(OSSL_ACKM *ackm)
  1099. {
  1100. ++ackm->pending_probe.anti_deadlock_initial;
  1101. }
  1102. static void ackm_queue_probe(OSSL_ACKM *ackm, int pkt_space)
  1103. {
  1104. /*
  1105. * TODO(QUIC FUTURE): We are allowed to send either one or two probe
  1106. * packets here.
  1107. * Determine a strategy for when we should send two probe packets.
  1108. */
  1109. ++ackm->pending_probe.pto[pkt_space];
  1110. }
  1111. int ossl_ackm_on_timeout(OSSL_ACKM *ackm)
  1112. {
  1113. int pkt_space;
  1114. OSSL_TIME earliest_loss_time;
  1115. OSSL_ACKM_TX_PKT *lost_pkts;
  1116. earliest_loss_time = ackm_get_loss_time_and_space(ackm, &pkt_space);
  1117. if (!ossl_time_is_zero(earliest_loss_time)) {
  1118. /* Time threshold loss detection. */
  1119. lost_pkts = ackm_detect_and_remove_lost_pkts(ackm, pkt_space);
  1120. if (lost_pkts != NULL)
  1121. ackm_on_pkts_lost(ackm, pkt_space, lost_pkts, /*pseudo=*/0);
  1122. ackm_set_loss_detection_timer(ackm);
  1123. return 1;
  1124. }
  1125. if (ackm_ack_eliciting_bytes_in_flight(ackm) == 0) {
  1126. assert(!ackm->peer_completed_addr_validation);
  1127. /*
  1128. * Client sends an anti-deadlock packet: Initial is padded to earn more
  1129. * anti-amplification credit. A handshake packet proves address
  1130. * ownership.
  1131. */
  1132. if (ackm->discarded[QUIC_PN_SPACE_INITIAL])
  1133. ackm_queue_probe_anti_deadlock_handshake(ackm);
  1134. else
  1135. ackm_queue_probe_anti_deadlock_initial(ackm);
  1136. } else {
  1137. /*
  1138. * PTO. The user of the ACKM should send new data if available, else
  1139. * retransmit old data, or if neither is available, send a single PING
  1140. * frame.
  1141. */
  1142. ackm_get_pto_time_and_space(ackm, &pkt_space);
  1143. ackm_queue_probe(ackm, pkt_space);
  1144. }
  1145. ++ackm->pto_count;
  1146. ackm_set_loss_detection_timer(ackm);
  1147. return 1;
  1148. }
  1149. OSSL_TIME ossl_ackm_get_loss_detection_deadline(OSSL_ACKM *ackm)
  1150. {
  1151. return ackm->loss_detection_deadline;
  1152. }
  1153. OSSL_ACKM_PROBE_INFO *ossl_ackm_get0_probe_request(OSSL_ACKM *ackm)
  1154. {
  1155. return &ackm->pending_probe;
  1156. }
  1157. int ossl_ackm_get_largest_unacked(OSSL_ACKM *ackm, int pkt_space, QUIC_PN *pn)
  1158. {
  1159. struct tx_pkt_history_st *h;
  1160. OSSL_ACKM_TX_PKT *p;
  1161. h = get_tx_history(ackm, pkt_space);
  1162. p = ossl_list_tx_history_tail(&h->packets);
  1163. if (p != NULL) {
  1164. *pn = p->pkt_num;
  1165. return 1;
  1166. }
  1167. return 0;
  1168. }
  1169. /* Number of ACK-eliciting packets RX'd before we always emit an ACK. */
  1170. #define PKTS_BEFORE_ACK 2
  1171. /*
  1172. * Return 1 if emission of an ACK frame is currently desired.
  1173. *
  1174. * This occurs when one or more of the following conditions occurs:
  1175. *
  1176. * - We have flagged that we want to send an ACK frame
  1177. * (for example, due to the packet threshold count being exceeded), or
  1178. *
  1179. * - We have exceeded the ACK flush deadline, meaning that
  1180. * we have received at least one ACK-eliciting packet, but held off on
  1181. * sending an ACK frame immediately in the hope that more ACK-eliciting
  1182. * packets might come in, but not enough did and we are now requesting
  1183. * transmission of an ACK frame anyway.
  1184. *
  1185. */
  1186. int ossl_ackm_is_ack_desired(OSSL_ACKM *ackm, int pkt_space)
  1187. {
  1188. return ackm->rx_ack_desired[pkt_space]
  1189. || (!ossl_time_is_infinite(ackm->rx_ack_flush_deadline[pkt_space])
  1190. && ossl_time_compare(ackm->now(ackm->now_arg),
  1191. ackm->rx_ack_flush_deadline[pkt_space]) >= 0);
  1192. }
  1193. /*
  1194. * Returns 1 if an ACK frame matches a given packet number.
  1195. */
  1196. static int ack_contains(const OSSL_QUIC_FRAME_ACK *ack, QUIC_PN pkt_num)
  1197. {
  1198. size_t i;
  1199. for (i = 0; i < ack->num_ack_ranges; ++i)
  1200. if (range_contains(&ack->ack_ranges[i], pkt_num))
  1201. return 1;
  1202. return 0;
  1203. }
  1204. /*
  1205. * Returns 1 iff a PN (which we have just received) was previously reported as
  1206. * implied missing (by us, in an ACK frame we previously generated).
  1207. */
  1208. static int ackm_is_missing(OSSL_ACKM *ackm, int pkt_space, QUIC_PN pkt_num)
  1209. {
  1210. /*
  1211. * A PN is implied missing if it is not greater than the highest PN in our
  1212. * generated ACK frame, but is not matched by the frame.
  1213. */
  1214. return ackm->ack[pkt_space].num_ack_ranges > 0
  1215. && pkt_num <= ackm->ack[pkt_space].ack_ranges[0].end
  1216. && !ack_contains(&ackm->ack[pkt_space], pkt_num);
  1217. }
  1218. /*
  1219. * Returns 1 iff our RX of a PN newly establishes the implication of missing
  1220. * packets.
  1221. */
  1222. static int ackm_has_newly_missing(OSSL_ACKM *ackm, int pkt_space)
  1223. {
  1224. struct rx_pkt_history_st *h;
  1225. h = get_rx_history(ackm, pkt_space);
  1226. if (ossl_list_uint_set_is_empty(&h->set))
  1227. return 0;
  1228. /*
  1229. * The second condition here establishes that the highest PN range in our RX
  1230. * history comprises only a single PN. If there is more than one, then this
  1231. * function will have returned 1 during a previous call to
  1232. * ossl_ackm_on_rx_packet assuming the third condition below was met. Thus
  1233. * we only return 1 when the missing PN condition is newly established.
  1234. *
  1235. * The third condition here establishes that the highest PN range in our RX
  1236. * history is beyond (and does not border) the highest PN we have yet
  1237. * reported in any ACK frame. Thus there is a gap of at least one PN between
  1238. * the PNs we have ACK'd previously and the PN we have just received.
  1239. */
  1240. return ackm->ack[pkt_space].num_ack_ranges > 0
  1241. && ossl_list_uint_set_tail(&h->set)->range.start
  1242. == ossl_list_uint_set_tail(&h->set)->range.end
  1243. && ossl_list_uint_set_tail(&h->set)->range.start
  1244. > ackm->ack[pkt_space].ack_ranges[0].end + 1;
  1245. }
  1246. static void ackm_set_flush_deadline(OSSL_ACKM *ackm, int pkt_space,
  1247. OSSL_TIME deadline)
  1248. {
  1249. ackm->rx_ack_flush_deadline[pkt_space] = deadline;
  1250. if (ackm->ack_deadline_cb != NULL)
  1251. ackm->ack_deadline_cb(ossl_ackm_get_ack_deadline(ackm, pkt_space),
  1252. pkt_space, ackm->ack_deadline_cb_arg);
  1253. }
  1254. /* Explicitly flags that we want to generate an ACK frame. */
  1255. static void ackm_queue_ack(OSSL_ACKM *ackm, int pkt_space)
  1256. {
  1257. ackm->rx_ack_desired[pkt_space] = 1;
  1258. /* Cancel deadline. */
  1259. ackm_set_flush_deadline(ackm, pkt_space, ossl_time_infinite());
  1260. }
  1261. static void ackm_on_rx_ack_eliciting(OSSL_ACKM *ackm,
  1262. OSSL_TIME rx_time, int pkt_space,
  1263. int was_missing)
  1264. {
  1265. OSSL_TIME tx_max_ack_delay;
  1266. if (ackm->rx_ack_desired[pkt_space])
  1267. /* ACK generation already requested so nothing to do. */
  1268. return;
  1269. ++ackm->rx_ack_eliciting_pkts_since_last_ack[pkt_space];
  1270. if (!ackm->rx_ack_generated[pkt_space]
  1271. || was_missing
  1272. || ackm->rx_ack_eliciting_pkts_since_last_ack[pkt_space]
  1273. >= PKTS_BEFORE_ACK
  1274. || ackm_has_newly_missing(ackm, pkt_space)) {
  1275. /*
  1276. * Either:
  1277. *
  1278. * - We have never yet generated an ACK frame, meaning that this
  1279. * is the first ever packet received, which we should always
  1280. * acknowledge immediately, or
  1281. *
  1282. * - We previously reported the PN that we have just received as
  1283. * missing in a previous ACK frame (meaning that we should report
  1284. * the fact that we now have it to the peer immediately), or
  1285. *
  1286. * - We have exceeded the ACK-eliciting packet threshold count
  1287. * for the purposes of ACK coalescing, so request transmission
  1288. * of an ACK frame, or
  1289. *
  1290. * - The PN we just received and added to our PN RX history
  1291. * newly implies one or more missing PNs, in which case we should
  1292. * inform the peer by sending an ACK frame immediately.
  1293. *
  1294. * We do not test the ACK flush deadline here because it is tested
  1295. * separately in ossl_ackm_is_ack_desired.
  1296. */
  1297. ackm_queue_ack(ackm, pkt_space);
  1298. return;
  1299. }
  1300. /*
  1301. * Not emitting an ACK yet.
  1302. *
  1303. * Update the ACK flush deadline.
  1304. *
  1305. * RFC 9000 s. 13.2.1: "An endpoint MUST acknowledge all ack-eliciting
  1306. * Initial and Handshake packets immediately"; don't delay ACK generation if
  1307. * we are using the Initial or Handshake PN spaces.
  1308. */
  1309. tx_max_ack_delay = ackm->tx_max_ack_delay;
  1310. if (pkt_space == QUIC_PN_SPACE_INITIAL
  1311. || pkt_space == QUIC_PN_SPACE_HANDSHAKE)
  1312. tx_max_ack_delay = ossl_time_zero();
  1313. if (ossl_time_is_infinite(ackm->rx_ack_flush_deadline[pkt_space]))
  1314. ackm_set_flush_deadline(ackm, pkt_space,
  1315. ossl_time_add(rx_time, tx_max_ack_delay));
  1316. else
  1317. ackm_set_flush_deadline(ackm, pkt_space,
  1318. ossl_time_min(ackm->rx_ack_flush_deadline[pkt_space],
  1319. ossl_time_add(rx_time,
  1320. tx_max_ack_delay)));
  1321. }
  1322. int ossl_ackm_on_rx_packet(OSSL_ACKM *ackm, const OSSL_ACKM_RX_PKT *pkt)
  1323. {
  1324. struct rx_pkt_history_st *h = get_rx_history(ackm, pkt->pkt_space);
  1325. int was_missing;
  1326. if (ossl_ackm_is_rx_pn_processable(ackm, pkt->pkt_num, pkt->pkt_space) != 1)
  1327. /* PN has already been processed or written off, no-op. */
  1328. return 1;
  1329. /*
  1330. * Record the largest PN we have RX'd and the time we received it.
  1331. * We use this to calculate the ACK delay field of ACK frames.
  1332. */
  1333. if (pkt->pkt_num > ackm->rx_largest_pn[pkt->pkt_space]) {
  1334. ackm->rx_largest_pn[pkt->pkt_space] = pkt->pkt_num;
  1335. ackm->rx_largest_time[pkt->pkt_space] = pkt->time;
  1336. }
  1337. /*
  1338. * If the PN we just received was previously implied missing by virtue of
  1339. * being omitted from a previous ACK frame generated, we skip any packet
  1340. * count thresholds or coalescing delays and emit a new ACK frame
  1341. * immediately.
  1342. */
  1343. was_missing = ackm_is_missing(ackm, pkt->pkt_space, pkt->pkt_num);
  1344. /*
  1345. * Add the packet number to our history list of PNs we have not yet provably
  1346. * acked.
  1347. */
  1348. if (rx_pkt_history_add_pn(h, pkt->pkt_num) != 1)
  1349. return 0;
  1350. /*
  1351. * Receiving this packet may or may not cause us to emit an ACK frame.
  1352. * We may not emit an ACK frame yet if we have not yet received a threshold
  1353. * number of packets.
  1354. */
  1355. if (pkt->is_ack_eliciting)
  1356. ackm_on_rx_ack_eliciting(ackm, pkt->time, pkt->pkt_space, was_missing);
  1357. /* Update the ECN counters according to which ECN signal we got, if any. */
  1358. switch (pkt->ecn) {
  1359. case OSSL_ACKM_ECN_ECT0:
  1360. ++ackm->rx_ect0[pkt->pkt_space];
  1361. break;
  1362. case OSSL_ACKM_ECN_ECT1:
  1363. ++ackm->rx_ect1[pkt->pkt_space];
  1364. break;
  1365. case OSSL_ACKM_ECN_ECNCE:
  1366. ++ackm->rx_ecnce[pkt->pkt_space];
  1367. break;
  1368. default:
  1369. break;
  1370. }
  1371. return 1;
  1372. }
  1373. static void ackm_fill_rx_ack_ranges(OSSL_ACKM *ackm, int pkt_space,
  1374. OSSL_QUIC_FRAME_ACK *ack)
  1375. {
  1376. struct rx_pkt_history_st *h = get_rx_history(ackm, pkt_space);
  1377. UINT_SET_ITEM *x;
  1378. size_t i = 0;
  1379. /*
  1380. * Copy out ranges from the PN set, starting at the end, until we reach our
  1381. * maximum number of ranges.
  1382. */
  1383. for (x = ossl_list_uint_set_tail(&h->set);
  1384. x != NULL && i < OSSL_NELEM(ackm->ack_ranges);
  1385. x = ossl_list_uint_set_prev(x), ++i) {
  1386. ackm->ack_ranges[pkt_space][i].start = x->range.start;
  1387. ackm->ack_ranges[pkt_space][i].end = x->range.end;
  1388. }
  1389. ack->ack_ranges = ackm->ack_ranges[pkt_space];
  1390. ack->num_ack_ranges = i;
  1391. }
  1392. const OSSL_QUIC_FRAME_ACK *ossl_ackm_get_ack_frame(OSSL_ACKM *ackm,
  1393. int pkt_space)
  1394. {
  1395. OSSL_QUIC_FRAME_ACK *ack = &ackm->ack[pkt_space];
  1396. OSSL_TIME now = ackm->now(ackm->now_arg);
  1397. ackm_fill_rx_ack_ranges(ackm, pkt_space, ack);
  1398. if (!ossl_time_is_zero(ackm->rx_largest_time[pkt_space])
  1399. && ossl_time_compare(now, ackm->rx_largest_time[pkt_space]) > 0
  1400. && pkt_space == QUIC_PN_SPACE_APP)
  1401. ack->delay_time =
  1402. ossl_time_subtract(now, ackm->rx_largest_time[pkt_space]);
  1403. else
  1404. ack->delay_time = ossl_time_zero();
  1405. ack->ect0 = ackm->rx_ect0[pkt_space];
  1406. ack->ect1 = ackm->rx_ect1[pkt_space];
  1407. ack->ecnce = ackm->rx_ecnce[pkt_space];
  1408. ack->ecn_present = 1;
  1409. ackm->rx_ack_eliciting_pkts_since_last_ack[pkt_space] = 0;
  1410. ackm->rx_ack_generated[pkt_space] = 1;
  1411. ackm->rx_ack_desired[pkt_space] = 0;
  1412. ackm_set_flush_deadline(ackm, pkt_space, ossl_time_infinite());
  1413. return ack;
  1414. }
  1415. OSSL_TIME ossl_ackm_get_ack_deadline(OSSL_ACKM *ackm, int pkt_space)
  1416. {
  1417. if (ackm->rx_ack_desired[pkt_space])
  1418. /* Already desired, deadline is now. */
  1419. return ossl_time_zero();
  1420. return ackm->rx_ack_flush_deadline[pkt_space];
  1421. }
  1422. int ossl_ackm_is_rx_pn_processable(OSSL_ACKM *ackm, QUIC_PN pn, int pkt_space)
  1423. {
  1424. struct rx_pkt_history_st *h = get_rx_history(ackm, pkt_space);
  1425. return pn >= h->watermark && ossl_uint_set_query(&h->set, pn) == 0;
  1426. }
  1427. void ossl_ackm_set_loss_detection_deadline_callback(OSSL_ACKM *ackm,
  1428. void (*fn)(OSSL_TIME deadline,
  1429. void *arg),
  1430. void *arg)
  1431. {
  1432. ackm->loss_detection_deadline_cb = fn;
  1433. ackm->loss_detection_deadline_cb_arg = arg;
  1434. }
  1435. void ossl_ackm_set_ack_deadline_callback(OSSL_ACKM *ackm,
  1436. void (*fn)(OSSL_TIME deadline,
  1437. int pkt_space,
  1438. void *arg),
  1439. void *arg)
  1440. {
  1441. ackm->ack_deadline_cb = fn;
  1442. ackm->ack_deadline_cb_arg = arg;
  1443. }
  1444. int ossl_ackm_mark_packet_pseudo_lost(OSSL_ACKM *ackm,
  1445. int pkt_space, QUIC_PN pn)
  1446. {
  1447. struct tx_pkt_history_st *h = get_tx_history(ackm, pkt_space);
  1448. OSSL_ACKM_TX_PKT *pkt;
  1449. pkt = tx_pkt_history_by_pkt_num(h, pn);
  1450. if (pkt == NULL)
  1451. return 0;
  1452. tx_pkt_history_remove(h, pkt->pkt_num);
  1453. pkt->lnext = NULL;
  1454. ackm_on_pkts_lost(ackm, pkt_space, pkt, /*pseudo=*/1);
  1455. return 1;
  1456. }
  1457. OSSL_TIME ossl_ackm_get_pto_duration(OSSL_ACKM *ackm)
  1458. {
  1459. OSSL_TIME duration;
  1460. OSSL_RTT_INFO rtt;
  1461. ossl_statm_get_rtt_info(ackm->statm, &rtt);
  1462. duration = ossl_time_add(rtt.smoothed_rtt,
  1463. ossl_time_max(ossl_time_multiply(rtt.rtt_variance, 4),
  1464. ossl_ticks2time(K_GRANULARITY)));
  1465. if (!ossl_time_is_infinite(ackm->rx_max_ack_delay))
  1466. duration = ossl_time_add(duration, ackm->rx_max_ack_delay);
  1467. return duration;
  1468. }
  1469. QUIC_PN ossl_ackm_get_largest_acked(OSSL_ACKM *ackm, int pkt_space)
  1470. {
  1471. return ackm->largest_acked_pkt[pkt_space];
  1472. }
  1473. void ossl_ackm_set_rx_max_ack_delay(OSSL_ACKM *ackm, OSSL_TIME rx_max_ack_delay)
  1474. {
  1475. ackm->rx_max_ack_delay = rx_max_ack_delay;
  1476. }
  1477. void ossl_ackm_set_tx_max_ack_delay(OSSL_ACKM *ackm, OSSL_TIME tx_max_ack_delay)
  1478. {
  1479. ackm->tx_max_ack_delay = tx_max_ack_delay;
  1480. }