| 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 | |
| 28 | #ifdef HAVE_CONFIG_H |
| 29 | # include <config.h> |
| 30 | #endif /* defined(HAVE_CONFIG_H) */ |
| 31 | |
| 32 | #include <ngtcp2/ngtcp2.h> |
| 33 | |
| 34 | #include "ngtcp2_mem.h" |
| 35 | #include "ngtcp2_ringbuf.h" |
| 36 | #include "ngtcp2_ksl.h" |
| 37 | #include "ngtcp2_pkt.h" |
| 38 | #include "ngtcp2_objalloc.h" |
| 39 | |
| 40 | /* NGTCP2_ACKTR_MAX_ENT is the maximum number of ngtcp2_acktr_entry |
| 41 | which ngtcp2_acktr stores. */ |
| 42 | #define NGTCP2_ACKTR_MAX_ENT (NGTCP2_MAX_ACK_RANGES + 1) |
| 43 | |
| 44 | typedef struct ngtcp2_log ngtcp2_log; |
| 45 | |
| 46 | /* |
| 47 | * ngtcp2_acktr_entry is a range of packets which need to be acked. |
| 48 | */ |
| 49 | typedef struct ngtcp2_acktr_entry { |
| 50 | union { |
| 51 | struct { |
| 52 | /* pkt_num is the largest packet number to acknowledge in this |
| 53 | range. */ |
| 54 | int64_t pkt_num; |
| 55 | /* len is the consecutive packets started from pkt_num which |
| 56 | includes pkt_num itself counting in decreasing order. So pkt_num |
| 57 | = 987 and len = 2, this entry includes packet 987 and 986. */ |
| 58 | size_t len; |
| 59 | /* tstamp is the timestamp when a packet denoted by pkt_num is |
| 60 | received. */ |
| 61 | ngtcp2_tstamp tstamp; |
| 62 | }; |
| 63 | |
| 64 | ngtcp2_opl_entry oplent; |
| 65 | }; |
| 66 | } ngtcp2_acktr_entry; |
| 67 | |
| 68 | ngtcp2_objalloc_decl(acktr_entry, ngtcp2_acktr_entry, oplent) |
| 69 | |
| 70 | /* |
| 71 | * ngtcp2_acktr_entry_objalloc_new allocates memory for ent, and |
| 72 | * initializes it with the given parameters. The pointer to the |
| 73 | * allocated object is stored to |*ent|. |
| 74 | * |
| 75 | * This function returns 0 if it succeeds, or one of the following |
| 76 | * negative error codes: |
| 77 | * |
| 78 | * NGTCP2_ERR_NOMEM |
| 79 | * Out of memory. |
| 80 | */ |
| 81 | int ngtcp2_acktr_entry_objalloc_new(ngtcp2_acktr_entry **ent, int64_t pkt_num, |
| 82 | ngtcp2_tstamp tstamp, |
| 83 | ngtcp2_objalloc *objalloc); |
| 84 | |
| 85 | /* |
| 86 | * ngtcp2_acktr_entry_objalloc_del deallocates memory allocated for |
| 87 | * |ent|. |
| 88 | */ |
| 89 | void ngtcp2_acktr_entry_objalloc_del(ngtcp2_acktr_entry *ent, |
| 90 | ngtcp2_objalloc *objalloc); |
| 91 | |
| 92 | typedef struct ngtcp2_acktr_ack_entry { |
| 93 | /* largest_ack is the largest packet number in outgoing ACK frame */ |
| 94 | int64_t largest_ack; |
| 95 | /* pkt_num is the packet number that ACK frame is included. */ |
| 96 | int64_t pkt_num; |
| 97 | } ngtcp2_acktr_ack_entry; |
| 98 | |
| 99 | /* NGTCP2_ACKTR_FLAG_NONE indicates that no flag set. */ |
| 100 | #define NGTCP2_ACKTR_FLAG_NONE 0x00u |
| 101 | /* NGTCP2_ACKTR_FLAG_IMMEDIATE_ACK indicates that immediate |
| 102 | acknowledgement is required. */ |
| 103 | #define NGTCP2_ACKTR_FLAG_IMMEDIATE_ACK 0x01u |
| 104 | /* NGTCP2_ACKTR_FLAG_ACTIVE_ACK indicates that there are pending |
| 105 | protected packet to be acknowledged. */ |
| 106 | #define NGTCP2_ACKTR_FLAG_ACTIVE_ACK 0x02u |
| 107 | /* NGTCP2_ACKTR_FLAG_CANCEL_TIMER is set when ACK delay timer is |
| 108 | expired and canceled. */ |
| 109 | #define NGTCP2_ACKTR_FLAG_CANCEL_TIMER 0x0100u |
| 110 | |
| 111 | ngtcp2_static_ringbuf_def(acks, 32, sizeof(ngtcp2_acktr_ack_entry)) |
| 112 | |
| 113 | /* |
| 114 | * ngtcp2_acktr tracks received packets which we have to send ack. |
| 115 | */ |
| 116 | typedef struct ngtcp2_acktr { |
| 117 | ngtcp2_objalloc objalloc; |
| 118 | ngtcp2_static_ringbuf_acks acks; |
| 119 | /* ents includes ngtcp2_acktr_entry sorted by decreasing order of |
| 120 | packet number. */ |
| 121 | ngtcp2_ksl ents; |
| 122 | ngtcp2_log *log; |
| 123 | /* first_unacked_ts is timestamp when ngtcp2_acktr_entry is added |
| 124 | first time after the last outgoing ACK frame. */ |
| 125 | ngtcp2_tstamp first_unacked_ts; |
| 126 | /* rx_npkt is the number of ACK eliciting packets received without |
| 127 | sending ACK. */ |
| 128 | size_t rx_npkt; |
| 129 | /* max_pkt_num is the largest packet number received so far. */ |
| 130 | int64_t max_pkt_num; |
| 131 | /* max_pkt_ts is the timestamp when max_pkt_num packet is |
| 132 | received. */ |
| 133 | ngtcp2_tstamp max_pkt_ts; |
| 134 | |
| 135 | struct { |
| 136 | /* ect0, ect1, and ce are the number of QUIC packets received |
| 137 | with those markings. */ |
| 138 | size_t ect0; |
| 139 | size_t ect1; |
| 140 | size_t ce; |
| 141 | struct { |
| 142 | /* ect0, ect1, ce are the ECN counts received in the latest |
| 143 | ACK frame. */ |
| 144 | uint64_t ect0; |
| 145 | uint64_t ect1; |
| 146 | uint64_t ce; |
| 147 | } ack; |
| 148 | } ecn; |
| 149 | |
| 150 | /* flags is bitwise OR of zero, or more of NGTCP2_ACKTR_FLAG_*. */ |
| 151 | uint16_t flags; |
| 152 | } ngtcp2_acktr; |
| 153 | |
| 154 | /* |
| 155 | * ngtcp2_acktr_init initializes |acktr|. |
| 156 | */ |
| 157 | void ngtcp2_acktr_init(ngtcp2_acktr *acktr, ngtcp2_log *log, |
| 158 | const ngtcp2_mem *mem); |
| 159 | |
| 160 | /* |
| 161 | * ngtcp2_acktr_free frees resources allocated for |acktr|. It frees |
| 162 | * any ngtcp2_acktr_entry added to |acktr|. |
| 163 | */ |
| 164 | void ngtcp2_acktr_free(ngtcp2_acktr *acktr); |
| 165 | |
| 166 | /* |
| 167 | * ngtcp2_acktr_add adds packet number |pkt_num| to |acktr|. |
| 168 | * |active_ack| is nonzero if |pkt_num| is retransmittable packet. |
| 169 | * |
| 170 | * This function assumes that |acktr| does not contain |pkt_num|. |
| 171 | * |
| 172 | * This function returns 0 if it succeeds, or one of the following |
| 173 | * negative error codes: |
| 174 | * |
| 175 | * NGTCP2_ERR_NOMEM |
| 176 | * OUt of memory. |
| 177 | */ |
| 178 | int ngtcp2_acktr_add(ngtcp2_acktr *acktr, int64_t pkt_num, int active_ack, |
| 179 | ngtcp2_tstamp ts); |
| 180 | |
| 181 | /* |
| 182 | * ngtcp2_acktr_forget removes all entries which have the packet |
| 183 | * number that is equal to or less than ent->pkt_num. This function |
| 184 | * assumes that |acktr| includes |ent|. |
| 185 | */ |
| 186 | void ngtcp2_acktr_forget(ngtcp2_acktr *acktr, ngtcp2_acktr_entry *ent); |
| 187 | |
| 188 | /* |
| 189 | * ngtcp2_acktr_get returns the iterator to pointer to the entry which |
| 190 | * has the largest packet number to be acked. If there is no entry, |
| 191 | * returned value satisfies ngtcp2_ksl_it_end(&it) != 0. |
| 192 | */ |
| 193 | ngtcp2_ksl_it ngtcp2_acktr_get(const ngtcp2_acktr *acktr); |
| 194 | |
| 195 | /* |
| 196 | * ngtcp2_acktr_empty returns nonzero if it has no packet to |
| 197 | * acknowledge. |
| 198 | */ |
| 199 | int ngtcp2_acktr_empty(const ngtcp2_acktr *acktr); |
| 200 | |
| 201 | /* |
| 202 | * ngtcp2_acktr_add_ack records outgoing ACK frame whose largest |
| 203 | * acknowledged packet number is |largest_ack|. |pkt_num| is the |
| 204 | * packet number of a packet in which ACK frame is included. This |
| 205 | * function returns a pointer to the object it adds. |
| 206 | */ |
| 207 | ngtcp2_acktr_ack_entry * |
| 208 | ngtcp2_acktr_add_ack(ngtcp2_acktr *acktr, int64_t pkt_num, int64_t largest_ack); |
| 209 | |
| 210 | /* |
| 211 | * ngtcp2_acktr_recv_ack processes the incoming ACK frame |fr|. |
| 212 | * |pkt_num| is a packet number which includes |fr|. If we receive |
| 213 | * ACK which acknowledges the ACKs added by ngtcp2_acktr_add_ack, |
| 214 | * ngtcp2_acktr_entry which the outgoing ACK acknowledges is removed. |
| 215 | */ |
| 216 | void ngtcp2_acktr_recv_ack(ngtcp2_acktr *acktr, const ngtcp2_ack *fr); |
| 217 | |
| 218 | /* |
| 219 | * ngtcp2_acktr_commit_ack tells |acktr| that ACK frame is generated. |
| 220 | */ |
| 221 | void ngtcp2_acktr_commit_ack(ngtcp2_acktr *acktr); |
| 222 | |
| 223 | /* |
| 224 | * ngtcp2_acktr_require_active_ack returns nonzero if ACK frame should |
| 225 | * be generated actively. |
| 226 | */ |
| 227 | int ngtcp2_acktr_require_active_ack(const ngtcp2_acktr *acktr, |
| 228 | ngtcp2_duration max_ack_delay, |
| 229 | ngtcp2_tstamp ts); |
| 230 | |
| 231 | /* |
| 232 | * ngtcp2_acktr_immediate_ack tells |acktr| that immediate |
| 233 | * acknowledgement is required. |
| 234 | */ |
| 235 | void ngtcp2_acktr_immediate_ack(ngtcp2_acktr *acktr); |
| 236 | |
| 237 | /* |
| 238 | * ngtcp2_acktr_create_ack_frame creates ACK frame in the object |
| 239 | * pointed by |ack|, and returns 0 if it successfully creates ACK |
| 240 | * frame in |ack|. If there are no packets to acknowledge, this |
| 241 | * function returns -1. |ack|->ranges must be able to contain at |
| 242 | * least NGTCP2_MAX_ACK_RANGES elements. |
| 243 | * |
| 244 | * Call ngtcp2_acktr_commit_ack after a created ACK frame is |
| 245 | * successfully serialized into a packet. |
| 246 | */ |
| 247 | int ngtcp2_acktr_create_ack_frame(ngtcp2_acktr *acktr, ngtcp2_ack *ack, |
| 248 | uint8_t type, ngtcp2_tstamp ts, |
| 249 | ngtcp2_duration ack_delay, |
| 250 | uint64_t ack_delay_exponent); |
| 251 | |
| 252 | /* |
| 253 | * ngtcp2_acktr_increase_ecn_counts increases ECN counts from |pi|. |
| 254 | */ |
| 255 | void ngtcp2_acktr_increase_ecn_counts(ngtcp2_acktr *acktr, |
| 256 | const ngtcp2_pkt_info *pi); |
| 257 | |
| 258 | #endif /* !defined(NGTCP2_ACKTR_H) */ |