quic_ackm.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #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/time.h"
  16. # include "internal/list.h"
  17. typedef struct ossl_ackm_st OSSL_ACKM;
  18. OSSL_ACKM *ossl_ackm_new(OSSL_TIME (*now)(void *arg),
  19. void *now_arg,
  20. OSSL_STATM *statm,
  21. const OSSL_CC_METHOD *cc_method,
  22. OSSL_CC_DATA *cc_data);
  23. void ossl_ackm_free(OSSL_ACKM *ackm);
  24. void ossl_ackm_set_loss_detection_deadline_callback(OSSL_ACKM *ackm,
  25. void (*fn)(OSSL_TIME deadline,
  26. void *arg),
  27. void *arg);
  28. void ossl_ackm_set_ack_deadline_callback(OSSL_ACKM *ackm,
  29. void (*fn)(OSSL_TIME deadline,
  30. int pkt_space,
  31. void *arg),
  32. void *arg);
  33. typedef struct ossl_ackm_tx_pkt_st OSSL_ACKM_TX_PKT;
  34. struct ossl_ackm_tx_pkt_st {
  35. /* The packet number of the transmitted packet. */
  36. QUIC_PN pkt_num;
  37. /* The number of bytes in the packet which was sent. */
  38. size_t num_bytes;
  39. /* The time at which the packet was sent. */
  40. OSSL_TIME time;
  41. /*
  42. * If the packet being described by this structure contains an ACK frame,
  43. * this must be set to the largest PN ACK'd by that frame.
  44. *
  45. * Otherwise, it should be set to QUIC_PN_INVALID.
  46. *
  47. * This is necessary to bound the number of PNs we have to keep track of on
  48. * the RX side (RFC 9000 s. 13.2.4). It allows older PN tracking information
  49. * on the RX side to be discarded.
  50. */
  51. QUIC_PN largest_acked;
  52. /*
  53. * One of the QUIC_PN_SPACE_* values. This qualifies the pkt_num field
  54. * into a packet number space.
  55. */
  56. unsigned int pkt_space :2;
  57. /* 1 if the packet is in flight. */
  58. unsigned int is_inflight :1;
  59. /* 1 if the packet has one or more ACK-eliciting frames. */
  60. unsigned int is_ack_eliciting :1;
  61. /* 1 if the packet is a PTO probe. */
  62. unsigned int is_pto_probe :1;
  63. /* 1 if the packet is an MTU probe. */
  64. unsigned int is_mtu_probe :1;
  65. /* Callback called if frames in this packet are lost. arg is cb_arg. */
  66. void (*on_lost)(void *arg);
  67. /* Callback called if frames in this packet are acked. arg is cb_arg. */
  68. void (*on_acked)(void *arg);
  69. /*
  70. * Callback called if frames in this packet are neither acked nor lost. arg
  71. * is cb_arg.
  72. */
  73. void (*on_discarded)(void *arg);
  74. void *cb_arg;
  75. /*
  76. * (Internal use fields; must be zero-initialized.)
  77. *
  78. * Keep a TX history list, anext is used to manifest
  79. * a singly-linked list of newly-acknowledged packets, and lnext is used to
  80. * manifest a singly-linked list of newly lost packets.
  81. */
  82. OSSL_LIST_MEMBER(tx_history, OSSL_ACKM_TX_PKT);
  83. struct ossl_ackm_tx_pkt_st *anext;
  84. struct ossl_ackm_tx_pkt_st *lnext;
  85. };
  86. int ossl_ackm_on_tx_packet(OSSL_ACKM *ackm, OSSL_ACKM_TX_PKT *pkt);
  87. int ossl_ackm_on_rx_datagram(OSSL_ACKM *ackm, size_t num_bytes);
  88. #define OSSL_ACKM_ECN_NONE 0
  89. #define OSSL_ACKM_ECN_ECT1 1
  90. #define OSSL_ACKM_ECN_ECT0 2
  91. #define OSSL_ACKM_ECN_ECNCE 3
  92. typedef struct ossl_ackm_rx_pkt_st {
  93. /* The packet number of the received packet. */
  94. QUIC_PN pkt_num;
  95. /* The time at which the packet was received. */
  96. OSSL_TIME time;
  97. /*
  98. * One of the QUIC_PN_SPACE_* values. This qualifies the pkt_num field
  99. * into a packet number space.
  100. */
  101. unsigned int pkt_space :2;
  102. /* 1 if the packet has one or more ACK-eliciting frames. */
  103. unsigned int is_ack_eliciting :1;
  104. /*
  105. * One of the OSSL_ACKM_ECN_* values. This is the ECN labelling applied to
  106. * the received packet. If unknown, use OSSL_ACKM_ECN_NONE.
  107. */
  108. unsigned int ecn :2;
  109. } OSSL_ACKM_RX_PKT;
  110. int ossl_ackm_on_rx_packet(OSSL_ACKM *ackm, const OSSL_ACKM_RX_PKT *pkt);
  111. int ossl_ackm_on_rx_ack_frame(OSSL_ACKM *ackm, const OSSL_QUIC_FRAME_ACK *ack,
  112. int pkt_space, OSSL_TIME rx_time);
  113. /*
  114. * Discards a PN space. This must be called for a PN space before freeing the
  115. * ACKM if you want in-flight packets to have their discarded callbacks called.
  116. * This should never be called in ordinary QUIC usage for the Application Data
  117. * PN space, but it may be called for the Application Data PN space prior to
  118. * freeing the ACKM to simplify teardown implementations.
  119. */
  120. int ossl_ackm_on_pkt_space_discarded(OSSL_ACKM *ackm, int pkt_space);
  121. int ossl_ackm_on_handshake_confirmed(OSSL_ACKM *ackm);
  122. int ossl_ackm_on_timeout(OSSL_ACKM *ackm);
  123. OSSL_TIME ossl_ackm_get_loss_detection_deadline(OSSL_ACKM *ackm);
  124. /*
  125. * Generates an ACK frame, regardless of whether the ACK manager thinks
  126. * one should currently be sent.
  127. *
  128. * This clears the flag returned by ossl_ackm_is_ack_desired and the deadline
  129. * returned by ossl_ackm_get_ack_deadline.
  130. */
  131. const OSSL_QUIC_FRAME_ACK *ossl_ackm_get_ack_frame(OSSL_ACKM *ackm,
  132. int pkt_space);
  133. /*
  134. * Returns the deadline after which an ACK frame should be generated by calling
  135. * ossl_ackm_get_ack_frame, or OSSL_TIME_INFINITY if no deadline is currently
  136. * applicable. If the deadline has already passed, this function may return that
  137. * deadline, or may return OSSL_TIME_ZERO.
  138. */
  139. OSSL_TIME ossl_ackm_get_ack_deadline(OSSL_ACKM *ackm, int pkt_space);
  140. /*
  141. * Returns 1 if the ACK manager thinks an ACK frame ought to be generated and
  142. * sent at this time. ossl_ackm_get_ack_frame will always provide an ACK frame
  143. * whether or not this returns 1, so it is suggested that you call this function
  144. * first to determine whether you need to generate an ACK frame.
  145. *
  146. * The return value of this function can change based on calls to
  147. * ossl_ackm_on_rx_packet and based on the passage of time (see
  148. * ossl_ackm_get_ack_deadline).
  149. */
  150. int ossl_ackm_is_ack_desired(OSSL_ACKM *ackm, int pkt_space);
  151. /*
  152. * Returns 1 if the given RX PN is 'processable'. A processable PN is one that
  153. * is not either
  154. *
  155. * - duplicate, meaning that we have already been passed such a PN in a call
  156. * to ossl_ackm_on_rx_packet; or
  157. *
  158. * - written off, meaning that the PN is so old we have stopped tracking state
  159. * for it (meaning that we cannot tell whether it is a duplicate and cannot
  160. * process it safely).
  161. *
  162. * This should be called for a packet before attempting to process its contents.
  163. * Failure to do so may result in processing a duplicated packet in violation of
  164. * the RFC.
  165. *
  166. * The return value of this function transitions from 1 to 0 for a given PN once
  167. * that PN is passed to ossl_ackm_on_rx_packet, thus thus function must be used
  168. * before calling ossl_ackm_on_rx_packet.
  169. */
  170. int ossl_ackm_is_rx_pn_processable(OSSL_ACKM *ackm, QUIC_PN pn, int pkt_space);
  171. typedef struct ossl_ackm_probe_info_st {
  172. uint32_t handshake;
  173. uint32_t padded_initial;
  174. uint32_t pto[QUIC_PN_SPACE_NUM];
  175. } OSSL_ACKM_PROBE_INFO;
  176. int ossl_ackm_get_probe_request(OSSL_ACKM *ackm, int clear,
  177. OSSL_ACKM_PROBE_INFO *info);
  178. int ossl_ackm_get_largest_unacked(OSSL_ACKM *ackm, int pkt_space, QUIC_PN *pn);
  179. #endif