quic_ackm.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef OSSL_QUIC_ACKM_H
  10. # define OSSL_QUIC_ACKM_H
  11. # include "internal/quic_statm.h"
  12. # include "internal/quic_cc.h"
  13. # include "internal/quic_types.h"
  14. # include "internal/quic_wire.h"
  15. # include "internal/quic_predef.h"
  16. # include "internal/time.h"
  17. # include "internal/list.h"
  18. # ifndef OPENSSL_NO_QUIC
  19. OSSL_ACKM *ossl_ackm_new(OSSL_TIME (*now)(void *arg),
  20. void *now_arg,
  21. OSSL_STATM *statm,
  22. const OSSL_CC_METHOD *cc_method,
  23. OSSL_CC_DATA *cc_data);
  24. void ossl_ackm_free(OSSL_ACKM *ackm);
  25. void ossl_ackm_set_loss_detection_deadline_callback(OSSL_ACKM *ackm,
  26. void (*fn)(OSSL_TIME deadline,
  27. void *arg),
  28. void *arg);
  29. void ossl_ackm_set_ack_deadline_callback(OSSL_ACKM *ackm,
  30. void (*fn)(OSSL_TIME deadline,
  31. int pkt_space,
  32. void *arg),
  33. void *arg);
  34. /*
  35. * Configures the RX-side maximum ACK delay. This is the maximum amount of time
  36. * the peer is allowed to delay sending an ACK frame after receiving an
  37. * ACK-eliciting packet. The peer communicates this value via a transport
  38. * parameter and it must be provided to the ACKM.
  39. */
  40. void ossl_ackm_set_rx_max_ack_delay(OSSL_ACKM *ackm, OSSL_TIME rx_max_ack_delay);
  41. /*
  42. * Configures the TX-side maximum ACK delay. This is the maximum amount of time
  43. * we are allowed to delay sending an ACK frame after receiving an ACK-eliciting
  44. * packet. Note that this cannot be changed after a connection is established as
  45. * it must be accurately reported in the transport parameters we send to our
  46. * peer.
  47. */
  48. void ossl_ackm_set_tx_max_ack_delay(OSSL_ACKM *ackm, OSSL_TIME tx_max_ack_delay);
  49. typedef struct ossl_ackm_tx_pkt_st OSSL_ACKM_TX_PKT;
  50. struct ossl_ackm_tx_pkt_st {
  51. /* The packet number of the transmitted packet. */
  52. QUIC_PN pkt_num;
  53. /* The number of bytes in the packet which was sent. */
  54. size_t num_bytes;
  55. /* The time at which the packet was sent. */
  56. OSSL_TIME time;
  57. /*
  58. * If the packet being described by this structure contains an ACK frame,
  59. * this must be set to the largest PN ACK'd by that frame.
  60. *
  61. * Otherwise, it should be set to QUIC_PN_INVALID.
  62. *
  63. * This is necessary to bound the number of PNs we have to keep track of on
  64. * the RX side (RFC 9000 s. 13.2.4). It allows older PN tracking information
  65. * on the RX side to be discarded.
  66. */
  67. QUIC_PN largest_acked;
  68. /*
  69. * One of the QUIC_PN_SPACE_* values. This qualifies the pkt_num field
  70. * into a packet number space.
  71. */
  72. unsigned int pkt_space :2;
  73. /*
  74. * 1 if the packet is in flight. A packet is considered 'in flight' if it is
  75. * counted for purposes of congestion control and 'bytes in flight' counts.
  76. * Most packets are considered in flight. The only circumstance where a
  77. * numbered packet is not considered in flight is if it contains only ACK
  78. * frames (not even PADDING frames), as these frames can bypass CC.
  79. */
  80. unsigned int is_inflight :1;
  81. /*
  82. * 1 if the packet has one or more ACK-eliciting frames.
  83. * Note that if this is set, is_inflight must be set.
  84. */
  85. unsigned int is_ack_eliciting :1;
  86. /* 1 if the packet is a PTO probe. */
  87. unsigned int is_pto_probe :1;
  88. /* 1 if the packet is an MTU probe. */
  89. unsigned int is_mtu_probe :1;
  90. /* Callback called if frames in this packet are lost. arg is cb_arg. */
  91. void (*on_lost)(void *arg);
  92. /* Callback called if frames in this packet are acked. arg is cb_arg. */
  93. void (*on_acked)(void *arg);
  94. /*
  95. * Callback called if frames in this packet are neither acked nor lost. arg
  96. * is cb_arg.
  97. */
  98. void (*on_discarded)(void *arg);
  99. void *cb_arg;
  100. /*
  101. * (Internal use fields; must be zero-initialized.)
  102. *
  103. * Keep a TX history list, anext is used to manifest
  104. * a singly-linked list of newly-acknowledged packets, and lnext is used to
  105. * manifest a singly-linked list of newly lost packets.
  106. */
  107. OSSL_LIST_MEMBER(tx_history, OSSL_ACKM_TX_PKT);
  108. struct ossl_ackm_tx_pkt_st *anext;
  109. struct ossl_ackm_tx_pkt_st *lnext;
  110. };
  111. int ossl_ackm_on_tx_packet(OSSL_ACKM *ackm, OSSL_ACKM_TX_PKT *pkt);
  112. int ossl_ackm_on_rx_datagram(OSSL_ACKM *ackm, size_t num_bytes);
  113. # define OSSL_ACKM_ECN_NONE 0
  114. # define OSSL_ACKM_ECN_ECT1 1
  115. # define OSSL_ACKM_ECN_ECT0 2
  116. # define OSSL_ACKM_ECN_ECNCE 3
  117. typedef struct ossl_ackm_rx_pkt_st {
  118. /* The packet number of the received packet. */
  119. QUIC_PN pkt_num;
  120. /* The time at which the packet was received. */
  121. OSSL_TIME time;
  122. /*
  123. * One of the QUIC_PN_SPACE_* values. This qualifies the pkt_num field
  124. * into a packet number space.
  125. */
  126. unsigned int pkt_space :2;
  127. /* 1 if the packet has one or more ACK-eliciting frames. */
  128. unsigned int is_ack_eliciting :1;
  129. /*
  130. * One of the OSSL_ACKM_ECN_* values. This is the ECN labelling applied to
  131. * the received packet. If unknown, use OSSL_ACKM_ECN_NONE.
  132. */
  133. unsigned int ecn :2;
  134. } OSSL_ACKM_RX_PKT;
  135. int ossl_ackm_on_rx_packet(OSSL_ACKM *ackm, const OSSL_ACKM_RX_PKT *pkt);
  136. int ossl_ackm_on_rx_ack_frame(OSSL_ACKM *ackm, const OSSL_QUIC_FRAME_ACK *ack,
  137. int pkt_space, OSSL_TIME rx_time);
  138. /*
  139. * Discards a PN space. This must be called for a PN space before freeing the
  140. * ACKM if you want in-flight packets to have their discarded callbacks called.
  141. * This should never be called in ordinary QUIC usage for the Application Data
  142. * PN space, but it may be called for the Application Data PN space prior to
  143. * freeing the ACKM to simplify teardown implementations.
  144. */
  145. int ossl_ackm_on_pkt_space_discarded(OSSL_ACKM *ackm, int pkt_space);
  146. int ossl_ackm_on_handshake_confirmed(OSSL_ACKM *ackm);
  147. int ossl_ackm_on_timeout(OSSL_ACKM *ackm);
  148. OSSL_TIME ossl_ackm_get_loss_detection_deadline(OSSL_ACKM *ackm);
  149. /*
  150. * Generates an ACK frame, regardless of whether the ACK manager thinks
  151. * one should currently be sent.
  152. *
  153. * This clears the flag returned by ossl_ackm_is_ack_desired and the deadline
  154. * returned by ossl_ackm_get_ack_deadline.
  155. */
  156. const OSSL_QUIC_FRAME_ACK *ossl_ackm_get_ack_frame(OSSL_ACKM *ackm,
  157. int pkt_space);
  158. /*
  159. * Returns the deadline after which an ACK frame should be generated by calling
  160. * ossl_ackm_get_ack_frame, or OSSL_TIME_INFINITY if no deadline is currently
  161. * applicable. If the deadline has already passed, this function may return that
  162. * deadline, or may return OSSL_TIME_ZERO.
  163. */
  164. OSSL_TIME ossl_ackm_get_ack_deadline(OSSL_ACKM *ackm, int pkt_space);
  165. /*
  166. * Returns 1 if the ACK manager thinks an ACK frame ought to be generated and
  167. * sent at this time. ossl_ackm_get_ack_frame will always provide an ACK frame
  168. * whether or not this returns 1, so it is suggested that you call this function
  169. * first to determine whether you need to generate an ACK frame.
  170. *
  171. * The return value of this function can change based on calls to
  172. * ossl_ackm_on_rx_packet and based on the passage of time (see
  173. * ossl_ackm_get_ack_deadline).
  174. */
  175. int ossl_ackm_is_ack_desired(OSSL_ACKM *ackm, int pkt_space);
  176. /*
  177. * Returns 1 if the given RX PN is 'processable'. A processable PN is one that
  178. * is not either
  179. *
  180. * - duplicate, meaning that we have already been passed such a PN in a call
  181. * to ossl_ackm_on_rx_packet; or
  182. *
  183. * - written off, meaning that the PN is so old we have stopped tracking state
  184. * for it (meaning that we cannot tell whether it is a duplicate and cannot
  185. * process it safely).
  186. *
  187. * This should be called for a packet before attempting to process its contents.
  188. * Failure to do so may result in processing a duplicated packet in violation of
  189. * the RFC.
  190. *
  191. * The return value of this function transitions from 1 to 0 for a given PN once
  192. * that PN is passed to ossl_ackm_on_rx_packet, thus this function must be used
  193. * before calling ossl_ackm_on_rx_packet.
  194. */
  195. int ossl_ackm_is_rx_pn_processable(OSSL_ACKM *ackm, QUIC_PN pn, int pkt_space);
  196. typedef struct ossl_ackm_probe_info_st {
  197. /*
  198. * The following two probe request types are used only for anti-deadlock
  199. * purposes in relation to the anti-amplification logic, by generating
  200. * packets to buy ourselves more anti-amplification credit with the server
  201. * until a client address is verified. Note that like all Initial packets,
  202. * any Initial probes are padded.
  203. *
  204. * Note: The ACKM will only ever increase these by one at a time,
  205. * as only one probe packet should be generated for these cases.
  206. */
  207. uint32_t anti_deadlock_initial, anti_deadlock_handshake;
  208. /*
  209. * Send an ACK-eliciting packet for each count here.
  210. *
  211. * Note: The ACKM may increase this by either one or two for each probe
  212. * request, depending on how many probe packets it thinks should be
  213. * generated.
  214. */
  215. uint32_t pto[QUIC_PN_SPACE_NUM];
  216. } OSSL_ACKM_PROBE_INFO;
  217. /*
  218. * Returns a pointer to a structure counting any pending probe requests which
  219. * have been generated by the ACKM. The fields in the structure are incremented
  220. * by one every time the ACKM wants another probe of the given type to be sent.
  221. * If the ACKM thinks two packets should be generated for a probe, it will
  222. * increment the field twice.
  223. *
  224. * It is permissible for the caller to decrement or zero these fields to keep
  225. * track of when it has generated a probe as asked. The returned structure
  226. * has the same lifetime as the ACKM.
  227. *
  228. * This function should be called after calling e.g. ossl_ackm_on_timeout
  229. * to determine if any probe requests have been generated.
  230. */
  231. OSSL_ACKM_PROBE_INFO *ossl_ackm_get0_probe_request(OSSL_ACKM *ackm);
  232. int ossl_ackm_get_largest_unacked(OSSL_ACKM *ackm, int pkt_space, QUIC_PN *pn);
  233. /*
  234. * Forces the ACKM to consider a packet with the given PN in the given PN space
  235. * as having been pseudo-lost. The main reason to use this is during a Retry, to
  236. * force any resources sent in the first Initial packet to be resent.
  237. *
  238. * The lost callback is called for the packet, but the packet is NOT considered
  239. * lost for congestion control purposes. Thus this is not exactly the same as a
  240. * true loss situation.
  241. */
  242. int ossl_ackm_mark_packet_pseudo_lost(OSSL_ACKM *ackm,
  243. int pkt_space, QUIC_PN pn);
  244. /*
  245. * Returns the PTO duration as currently calculated. This is a quantity of time.
  246. * This duration is used in various parts of QUIC besides the ACKM.
  247. */
  248. OSSL_TIME ossl_ackm_get_pto_duration(OSSL_ACKM *ackm);
  249. /* Returns the largest acked PN in the given PN space. */
  250. QUIC_PN ossl_ackm_get_largest_acked(OSSL_ACKM *ackm, int pkt_space);
  251. # endif
  252. #endif