ngtcp2_acktr.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * ngtcp2
  3. *
  4. * Copyright (c) 2017 ngtcp2 contributors
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef NGTCP2_ACKTR_H
  26. #define NGTCP2_ACKTR_H
  27. #ifdef HAVE_CONFIG_H
  28. # include <config.h>
  29. #endif /* defined(HAVE_CONFIG_H) */
  30. #include <ngtcp2/ngtcp2.h>
  31. #include "ngtcp2_mem.h"
  32. #include "ngtcp2_ringbuf.h"
  33. #include "ngtcp2_ksl.h"
  34. #include "ngtcp2_pkt.h"
  35. #include "ngtcp2_objalloc.h"
  36. /* NGTCP2_ACKTR_MAX_ENT is the maximum number of ngtcp2_acktr_entry
  37. which ngtcp2_acktr stores. */
  38. #define NGTCP2_ACKTR_MAX_ENT (NGTCP2_MAX_ACK_RANGES + 1)
  39. typedef struct ngtcp2_log ngtcp2_log;
  40. /*
  41. * ngtcp2_acktr_entry is a range of packets which need to be acked.
  42. */
  43. typedef struct ngtcp2_acktr_entry {
  44. union {
  45. struct {
  46. /* pkt_num is the largest packet number to acknowledge in this
  47. range. */
  48. int64_t pkt_num;
  49. /* len is the consecutive packets started from pkt_num which
  50. includes pkt_num itself counting in decreasing order. So pkt_num
  51. = 987 and len = 2, this entry includes packet 987 and 986. */
  52. size_t len;
  53. /* tstamp is the timestamp when a packet denoted by pkt_num is
  54. received. */
  55. ngtcp2_tstamp tstamp;
  56. };
  57. ngtcp2_opl_entry oplent;
  58. };
  59. } ngtcp2_acktr_entry;
  60. ngtcp2_objalloc_decl(acktr_entry, ngtcp2_acktr_entry, oplent)
  61. /*
  62. * ngtcp2_acktr_entry_objalloc_new allocates memory for ent, and
  63. * initializes it with the given parameters. The pointer to the
  64. * allocated object is stored to |*ent|.
  65. *
  66. * This function returns 0 if it succeeds, or one of the following
  67. * negative error codes:
  68. *
  69. * NGTCP2_ERR_NOMEM
  70. * Out of memory.
  71. */
  72. int ngtcp2_acktr_entry_objalloc_new(ngtcp2_acktr_entry **ent, int64_t pkt_num,
  73. ngtcp2_tstamp tstamp,
  74. ngtcp2_objalloc *objalloc);
  75. /*
  76. * ngtcp2_acktr_entry_objalloc_del deallocates memory allocated for
  77. * |ent|.
  78. */
  79. void ngtcp2_acktr_entry_objalloc_del(ngtcp2_acktr_entry *ent,
  80. ngtcp2_objalloc *objalloc);
  81. typedef struct ngtcp2_acktr_ack_entry {
  82. /* largest_ack is the largest packet number in outgoing ACK frame */
  83. int64_t largest_ack;
  84. /* pkt_num is the packet number that ACK frame is included. */
  85. int64_t pkt_num;
  86. } ngtcp2_acktr_ack_entry;
  87. /* NGTCP2_ACKTR_FLAG_NONE indicates that no flag set. */
  88. #define NGTCP2_ACKTR_FLAG_NONE 0x00u
  89. /* NGTCP2_ACKTR_FLAG_IMMEDIATE_ACK indicates that immediate
  90. acknowledgement is required. */
  91. #define NGTCP2_ACKTR_FLAG_IMMEDIATE_ACK 0x01u
  92. /* NGTCP2_ACKTR_FLAG_ACTIVE_ACK indicates that there are pending
  93. protected packet to be acknowledged. */
  94. #define NGTCP2_ACKTR_FLAG_ACTIVE_ACK 0x02u
  95. /* NGTCP2_ACKTR_FLAG_CANCEL_TIMER is set when ACK delay timer is
  96. expired and canceled. */
  97. #define NGTCP2_ACKTR_FLAG_CANCEL_TIMER 0x0100u
  98. ngtcp2_static_ringbuf_def(acks, 32, sizeof(ngtcp2_acktr_ack_entry))
  99. /*
  100. * ngtcp2_acktr tracks received packets which we have to send ack.
  101. */
  102. typedef struct ngtcp2_acktr {
  103. ngtcp2_objalloc objalloc;
  104. ngtcp2_static_ringbuf_acks acks;
  105. /* ents includes ngtcp2_acktr_entry sorted by decreasing order of
  106. packet number. */
  107. ngtcp2_ksl ents;
  108. ngtcp2_log *log;
  109. /* flags is bitwise OR of zero, or more of NGTCP2_ACKTR_FLAG_*. */
  110. uint16_t flags;
  111. /* first_unacked_ts is timestamp when ngtcp2_acktr_entry is added
  112. first time after the last outgoing ACK frame. */
  113. ngtcp2_tstamp first_unacked_ts;
  114. /* rx_npkt is the number of ACK eliciting packets received without
  115. sending ACK. */
  116. size_t rx_npkt;
  117. } ngtcp2_acktr;
  118. /*
  119. * ngtcp2_acktr_init initializes |acktr|.
  120. */
  121. void ngtcp2_acktr_init(ngtcp2_acktr *acktr, ngtcp2_log *log,
  122. const ngtcp2_mem *mem);
  123. /*
  124. * ngtcp2_acktr_free frees resources allocated for |acktr|. It frees
  125. * any ngtcp2_acktr_entry added to |acktr|.
  126. */
  127. void ngtcp2_acktr_free(ngtcp2_acktr *acktr);
  128. /*
  129. * ngtcp2_acktr_add adds packet number |pkt_num| to |acktr|.
  130. * |active_ack| is nonzero if |pkt_num| is retransmittable packet.
  131. *
  132. * This function assumes that |acktr| does not contain |pkt_num|.
  133. *
  134. * This function returns 0 if it succeeds, or one of the following
  135. * negative error codes:
  136. *
  137. * NGTCP2_ERR_NOMEM
  138. * OUt of memory.
  139. */
  140. int ngtcp2_acktr_add(ngtcp2_acktr *acktr, int64_t pkt_num, int active_ack,
  141. ngtcp2_tstamp ts);
  142. /*
  143. * ngtcp2_acktr_forget removes all entries which have the packet
  144. * number that is equal to or less than ent->pkt_num. This function
  145. * assumes that |acktr| includes |ent|.
  146. */
  147. void ngtcp2_acktr_forget(ngtcp2_acktr *acktr, ngtcp2_acktr_entry *ent);
  148. /*
  149. * ngtcp2_acktr_get returns the pointer to pointer to the entry which
  150. * has the largest packet number to be acked. If there is no entry,
  151. * returned value satisfies ngtcp2_ksl_it_end(&it) != 0.
  152. */
  153. ngtcp2_ksl_it ngtcp2_acktr_get(const ngtcp2_acktr *acktr);
  154. /*
  155. * ngtcp2_acktr_empty returns nonzero if it has no packet to
  156. * acknowledge.
  157. */
  158. int ngtcp2_acktr_empty(const ngtcp2_acktr *acktr);
  159. /*
  160. * ngtcp2_acktr_add_ack records outgoing ACK frame whose largest
  161. * acknowledged packet number is |largest_ack|. |pkt_num| is the
  162. * packet number of a packet in which ACK frame is included. This
  163. * function returns a pointer to the object it adds.
  164. */
  165. ngtcp2_acktr_ack_entry *
  166. ngtcp2_acktr_add_ack(ngtcp2_acktr *acktr, int64_t pkt_num, int64_t largest_ack);
  167. /*
  168. * ngtcp2_acktr_recv_ack processes the incoming ACK frame |fr|.
  169. * |pkt_num| is a packet number which includes |fr|. If we receive
  170. * ACK which acknowledges the ACKs added by ngtcp2_acktr_add_ack,
  171. * ngtcp2_acktr_entry which the outgoing ACK acknowledges is removed.
  172. */
  173. void ngtcp2_acktr_recv_ack(ngtcp2_acktr *acktr, const ngtcp2_ack *fr);
  174. /*
  175. * ngtcp2_acktr_commit_ack tells |acktr| that ACK frame is generated.
  176. */
  177. void ngtcp2_acktr_commit_ack(ngtcp2_acktr *acktr);
  178. /*
  179. * ngtcp2_acktr_require_active_ack returns nonzero if ACK frame should
  180. * be generated actively.
  181. */
  182. int ngtcp2_acktr_require_active_ack(const ngtcp2_acktr *acktr,
  183. ngtcp2_duration max_ack_delay,
  184. ngtcp2_tstamp ts);
  185. /*
  186. * ngtcp2_acktr_immediate_ack tells |acktr| that immediate
  187. * acknowledgement is required.
  188. */
  189. void ngtcp2_acktr_immediate_ack(ngtcp2_acktr *acktr);
  190. #endif /* !defined(NGTCP2_ACKTR_H) */