quic_ackm.c 67 KB

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