Debian Codegraph @

contrib/libngtcp2/ngtcp2/lib/ngtcp2_acktr.c

raw symbols SCIP
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 #include "ngtcp2_acktr.h"
26
27 #include <assert.h>
28 #include <string.h>
29
30 #include "ngtcp2_macro.h"
31 #include "ngtcp2_tstamp.h"
32
33 ngtcp2_objalloc_def(acktr_entry, ngtcp2_acktr_entry, oplent)
34
35 static void acktr_entry_init(ngtcp2_acktr_entry *ent, int64_t pkt_num,
36 ngtcp2_tstamp tstamp) {
37 *ent = (ngtcp2_acktr_entry){
38 .pkt_num = pkt_num,
39 .len = 1,
40 .tstamp = tstamp,
41 };
42 }
43
44 int ngtcp2_acktr_entry_objalloc_new(ngtcp2_acktr_entry **ent, int64_t pkt_num,
45 ngtcp2_tstamp tstamp,
46 ngtcp2_objalloc *objalloc) {
47 *ent = ngtcp2_objalloc_acktr_entry_get(objalloc);
48 if (*ent == NULL) {
49 return NGTCP2_ERR_NOMEM;
50 }
51
52 acktr_entry_init(*ent, pkt_num, tstamp);
53
54 return 0;
55 }
56
57 void ngtcp2_acktr_entry_objalloc_del(ngtcp2_acktr_entry *ent,
58 ngtcp2_objalloc *objalloc) {
59 ngtcp2_objalloc_acktr_entry_release(objalloc, ent);
60 }
61
62 void ngtcp2_acktr_init(ngtcp2_acktr *acktr, ngtcp2_log *log,
63 const ngtcp2_mem *mem) {
64 ngtcp2_objalloc_acktr_entry_init(&acktr->objalloc, NGTCP2_ACKTR_MAX_ENT + 1,
65 mem);
66
67 ngtcp2_static_ringbuf_acks_init(&acktr->acks);
68
69 ngtcp2_ksl_init(&acktr->ents, ngtcp2_ksl_int64_greater,
70 ngtcp2_ksl_int64_greater_search, sizeof(int64_t), mem);
71
72 acktr->log = log;
73 acktr->flags = NGTCP2_ACKTR_FLAG_NONE;
74 acktr->first_unacked_ts = UINT64_MAX;
75 acktr->rx_npkt = 0;
76 acktr->max_pkt_num = -1;
77 acktr->max_pkt_ts = UINT64_MAX;
78 memset(&acktr->ecn, 0, sizeof(acktr->ecn));
79 }
80
81 void ngtcp2_acktr_free(ngtcp2_acktr *acktr) {
82 #ifdef NOMEMPOOL
83 ngtcp2_ksl_it it;
84 #endif /* defined(NOMEMPOOL) */
85
86 if (acktr == NULL) {
87 return;
88 }
89
90 #ifdef NOMEMPOOL
91 for (it = ngtcp2_ksl_begin(&acktr->ents); !ngtcp2_ksl_it_end(&it);
92 ngtcp2_ksl_it_next(&it)) {
93 ngtcp2_acktr_entry_objalloc_del(ngtcp2_ksl_it_get(&it), &acktr->objalloc);
94 }
95 #endif /* defined(NOMEMPOOL) */
96
97 ngtcp2_ksl_free(&acktr->ents);
98
99 ngtcp2_objalloc_free(&acktr->objalloc);
100 }
101
102 int ngtcp2_acktr_add(ngtcp2_acktr *acktr, int64_t pkt_num, int active_ack,
103 ngtcp2_tstamp ts) {
104 ngtcp2_ksl_it it, prev_it;
105 ngtcp2_acktr_entry *ent, *prev_ent, *delent;
106 int rv;
107 int added = 0;
108
109 if (ngtcp2_ksl_len(&acktr->ents)) {
110 it = ngtcp2_ksl_lower_bound(&acktr->ents, &pkt_num);
111 if (ngtcp2_ksl_it_end(&it)) {
112 ngtcp2_ksl_it_prev(&it);
113 ent = ngtcp2_ksl_it_get(&it);
114
115 assert(ent->pkt_num >= pkt_num + (int64_t)ent->len);
116
117 if (ent->pkt_num == pkt_num + (int64_t)ent->len) {
118 ++ent->len;
119 added = 1;
120 }
121 } else {
122 ent = ngtcp2_ksl_it_get(&it);
123
124 assert(ent->pkt_num != pkt_num);
125
126 if (ngtcp2_ksl_it_begin(&it)) {
127 if (ent->pkt_num + 1 == pkt_num) {
128 ngtcp2_ksl_update_key(&acktr->ents, &ent->pkt_num, &pkt_num);
129 ent->pkt_num = pkt_num;
130 ent->tstamp = ts;
131 ++ent->len;
132 added = 1;
133 }
134 } else {
135 prev_it = it;
136 ngtcp2_ksl_it_prev(&prev_it);
137 prev_ent = ngtcp2_ksl_it_get(&prev_it);
138
139 assert(prev_ent->pkt_num >= pkt_num + (int64_t)prev_ent->len);
140
141 if (ent->pkt_num + 1 == pkt_num) {
142 if (prev_ent->pkt_num == pkt_num + (int64_t)prev_ent->len) {
143 prev_ent->len += ent->len + 1;
144 ngtcp2_ksl_remove_hint(&acktr->ents, NULL, &it, &ent->pkt_num);
145 ngtcp2_acktr_entry_objalloc_del(ent, &acktr->objalloc);
146 added = 1;
147 } else {
148 ngtcp2_ksl_update_key(&acktr->ents, &ent->pkt_num, &pkt_num);
149 ent->pkt_num = pkt_num;
150 ent->tstamp = ts;
151 ++ent->len;
152 added = 1;
153 }
154 } else if (prev_ent->pkt_num == pkt_num + (int64_t)prev_ent->len) {
155 ++prev_ent->len;
156 added = 1;
157 }
158 }
159 }
160 }
161
162 if (!added) {
163 rv = ngtcp2_acktr_entry_objalloc_new(&ent, pkt_num, ts, &acktr->objalloc);
164 if (rv != 0) {
165 return rv;
166 }
167 rv = ngtcp2_ksl_insert(&acktr->ents, NULL, &ent->pkt_num, ent);
168 if (rv != 0) {
169 ngtcp2_acktr_entry_objalloc_del(ent, &acktr->objalloc);
170 return rv;
171 }
172 }
173
174 if (active_ack) {
175 acktr->flags |= NGTCP2_ACKTR_FLAG_ACTIVE_ACK;
176 if (acktr->first_unacked_ts == UINT64_MAX) {
177 acktr->first_unacked_ts = ts;
178 }
179 }
180
181 if (ngtcp2_ksl_len(&acktr->ents) > NGTCP2_ACKTR_MAX_ENT) {
182 it = ngtcp2_ksl_end(&acktr->ents);
183 ngtcp2_ksl_it_prev(&it);
184 delent = ngtcp2_ksl_it_get(&it);
185 ngtcp2_ksl_remove_hint(&acktr->ents, NULL, &it, &delent->pkt_num);
186 ngtcp2_acktr_entry_objalloc_del(delent, &acktr->objalloc);
187 }
188
189 if (acktr->max_pkt_num < pkt_num) {
190 acktr->max_pkt_num = pkt_num;
191 acktr->max_pkt_ts = ts;
192 }
193
194 return 0;
195 }
196
197 void ngtcp2_acktr_forget(ngtcp2_acktr *acktr, ngtcp2_acktr_entry *ent) {
198 ngtcp2_ksl_it it;
199
200 it = ngtcp2_ksl_lower_bound(&acktr->ents, &ent->pkt_num);
201 assert(*(int64_t *)ngtcp2_ksl_it_key(&it) == (int64_t)ent->pkt_num);
202
203 for (; !ngtcp2_ksl_it_end(&it);) {
204 ent = ngtcp2_ksl_it_get(&it);
205 ngtcp2_ksl_remove_hint(&acktr->ents, &it, &it, &ent->pkt_num);
206 ngtcp2_acktr_entry_objalloc_del(ent, &acktr->objalloc);
207 }
208 }
209
210 ngtcp2_ksl_it ngtcp2_acktr_get(const ngtcp2_acktr *acktr) {
211 return ngtcp2_ksl_begin(&acktr->ents);
212 }
213
214 int ngtcp2_acktr_empty(const ngtcp2_acktr *acktr) {
215 ngtcp2_ksl_it it = ngtcp2_ksl_begin(&acktr->ents);
216 return ngtcp2_ksl_it_end(&it);
217 }
218
219 ngtcp2_acktr_ack_entry *ngtcp2_acktr_add_ack(ngtcp2_acktr *acktr,
220 int64_t pkt_num,
221 int64_t largest_ack) {
222 ngtcp2_acktr_ack_entry *ent = ngtcp2_ringbuf_push_front(&acktr->acks.rb);
223
224 *ent = (ngtcp2_acktr_ack_entry){
225 .largest_ack = largest_ack,
226 .pkt_num = pkt_num,
227 };
228
229 return ent;
230 }
231
232 /*
233 * acktr_remove removes |ent| from |acktr|. |it| must point to the
234 * node whose key identifies |ent|. The iterator which points to the
235 * entry next to |ent| is assigned to |it|.
236 */
237 static void acktr_remove(ngtcp2_acktr *acktr, ngtcp2_ksl_it *it,
238 ngtcp2_acktr_entry *ent) {
239 ngtcp2_ksl_remove_hint(&acktr->ents, it, it, &ent->pkt_num);
240 ngtcp2_acktr_entry_objalloc_del(ent, &acktr->objalloc);
241 }
242
243 static void acktr_on_ack(ngtcp2_acktr *acktr, ngtcp2_ringbuf *rb,
244 size_t ack_ent_offset) {
245 ngtcp2_acktr_ack_entry *ack_ent;
246 ngtcp2_acktr_entry *ent;
247 ngtcp2_ksl_it it;
248
249 assert(ngtcp2_ringbuf_len(rb));
250
251 ack_ent = ngtcp2_ringbuf_get(rb, ack_ent_offset);
252
253 /* Assume that ngtcp2_pkt_validate_ack(fr) returns 0 */
254 it = ngtcp2_ksl_lower_bound(&acktr->ents, &ack_ent->largest_ack);
255 for (; !ngtcp2_ksl_it_end(&it);) {
256 ent = ngtcp2_ksl_it_get(&it);
257 acktr_remove(acktr, &it, ent);
258 }
259
260 if (ngtcp2_ksl_len(&acktr->ents)) {
261 assert(ngtcp2_ksl_it_end(&it));
262
263 ngtcp2_ksl_it_prev(&it);
264 ent = ngtcp2_ksl_it_get(&it);
265
266 assert(ent->pkt_num > ack_ent->largest_ack);
267
268 if (ack_ent->largest_ack + (int64_t)ent->len > ent->pkt_num) {
269 ent->len = (size_t)(ent->pkt_num - ack_ent->largest_ack);
270 }
271 }
272
273 ngtcp2_ringbuf_resize(rb, ack_ent_offset);
274 }
275
276 void ngtcp2_acktr_recv_ack(ngtcp2_acktr *acktr, const ngtcp2_ack *fr) {
277 ngtcp2_acktr_ack_entry *ent;
278 int64_t largest_ack = fr->largest_ack, min_ack;
279 size_t i, j;
280 ngtcp2_ringbuf *rb = &acktr->acks.rb;
281 size_t nacks = ngtcp2_ringbuf_len(rb);
282
283 /* Assume that ngtcp2_pkt_validate_ack(fr) returns 0 */
284 for (j = 0; j < nacks; ++j) {
285 ent = ngtcp2_ringbuf_get(rb, j);
286 if (largest_ack >= ent->pkt_num) {
287 break;
288 }
289 }
290 if (j == nacks) {
291 return;
292 }
293
294 min_ack = largest_ack - (int64_t)fr->first_ack_range;
295
296 if (min_ack <= ent->pkt_num) {
297 acktr_on_ack(acktr, rb, j);
298 return;
299 }
300
301 for (i = 0; i < fr->rangecnt && j < nacks; ++i) {
302 largest_ack = min_ack - (int64_t)fr->ranges[i].gap - 2;
303 min_ack = largest_ack - (int64_t)fr->ranges[i].len;
304
305 for (;;) {
306 if (ent->pkt_num > largest_ack) {
307 if (++j == nacks) {
308 return;
309 }
310 ent = ngtcp2_ringbuf_get(rb, j);
311 continue;
312 }
313 if (ent->pkt_num < min_ack) {
314 break;
315 }
316 acktr_on_ack(acktr, rb, j);
317 return;
318 }
319 }
320 }
321
322 void ngtcp2_acktr_commit_ack(ngtcp2_acktr *acktr) {
323 acktr->flags &=
324 (uint16_t)~(NGTCP2_ACKTR_FLAG_ACTIVE_ACK | NGTCP2_ACKTR_FLAG_IMMEDIATE_ACK |
325 NGTCP2_ACKTR_FLAG_CANCEL_TIMER);
326 acktr->first_unacked_ts = UINT64_MAX;
327 acktr->rx_npkt = 0;
328 }
329
330 int ngtcp2_acktr_require_active_ack(const ngtcp2_acktr *acktr,
331 ngtcp2_duration max_ack_delay,
332 ngtcp2_tstamp ts) {
333 return ngtcp2_tstamp_elapsed(acktr->first_unacked_ts, max_ack_delay, ts);
334 }
335
336 void ngtcp2_acktr_immediate_ack(ngtcp2_acktr *acktr) {
337 acktr->flags |= NGTCP2_ACKTR_FLAG_IMMEDIATE_ACK;
338 }
339
340 int ngtcp2_acktr_create_ack_frame(ngtcp2_acktr *acktr, ngtcp2_ack *ack,
341 uint8_t type, ngtcp2_tstamp ts,
342 ngtcp2_duration ack_delay,
343 uint64_t ack_delay_exponent) {
344 int64_t last_pkt_num;
345 ngtcp2_ack_range *range;
346 ngtcp2_ksl_it it;
347 ngtcp2_acktr_entry *rpkt;
348 ngtcp2_tstamp largest_ack_ts;
349 size_t num_acks;
350
351 if (acktr->flags & NGTCP2_ACKTR_FLAG_IMMEDIATE_ACK) {
352 ack_delay = 0;
353 }
354
355 if (!ngtcp2_acktr_require_active_ack(acktr, ack_delay, ts)) {
356 return -1;
357 }
358
359 it = ngtcp2_acktr_get(acktr);
360 if (ngtcp2_ksl_it_end(&it)) {
361 ngtcp2_acktr_commit_ack(acktr);
362 return -1;
363 }
364
365 num_acks = ngtcp2_ksl_len(&acktr->ents);
366
367 if (acktr->ecn.ect0 || acktr->ecn.ect1 || acktr->ecn.ce) {
368 ack->type = NGTCP2_FRAME_ACK_ECN;
369 ack->ecn.ect0 = acktr->ecn.ect0;
370 ack->ecn.ect1 = acktr->ecn.ect1;
371 ack->ecn.ce = acktr->ecn.ce;
372 } else {
373 ack->type = NGTCP2_FRAME_ACK;
374 }
375 ack->rangecnt = 0;
376
377 rpkt = ngtcp2_ksl_it_get(&it);
378
379 if (rpkt->pkt_num == acktr->max_pkt_num) {
380 last_pkt_num = rpkt->pkt_num - (int64_t)(rpkt->len - 1);
381 largest_ack_ts = rpkt->tstamp;
382 ack->largest_ack = rpkt->pkt_num;
383 ack->first_ack_range = rpkt->len - 1;
384
385 ngtcp2_ksl_it_next(&it);
386 --num_acks;
387 } else if (rpkt->pkt_num + 1 == acktr->max_pkt_num) {
388 last_pkt_num = rpkt->pkt_num - (int64_t)(rpkt->len - 1);
389 largest_ack_ts = acktr->max_pkt_ts;
390 ack->largest_ack = acktr->max_pkt_num;
391 ack->first_ack_range = rpkt->len;
392
393 ngtcp2_ksl_it_next(&it);
394 --num_acks;
395 } else {
396 assert(rpkt->pkt_num < acktr->max_pkt_num);
397
398 last_pkt_num = acktr->max_pkt_num;
399 largest_ack_ts = acktr->max_pkt_ts;
400 ack->largest_ack = acktr->max_pkt_num;
401 ack->first_ack_range = 0;
402 }
403
404 if (type == NGTCP2_PKT_1RTT) {
405 ack->ack_delay_unscaled = ts - largest_ack_ts;
406 ack->ack_delay = ack->ack_delay_unscaled / NGTCP2_MICROSECONDS /
407 (1ULL << ack_delay_exponent);
408 } else {
409 ack->ack_delay_unscaled = 0;
410 ack->ack_delay = 0;
411 }
412
413 num_acks = ngtcp2_min_size(num_acks, NGTCP2_MAX_ACK_RANGES);
414
415 for (; ack->rangecnt < num_acks; ngtcp2_ksl_it_next(&it)) {
416 rpkt = ngtcp2_ksl_it_get(&it);
417
418 range = &ack->ranges[ack->rangecnt++];
419 range->gap = (uint64_t)(last_pkt_num - rpkt->pkt_num - 2);
420 range->len = rpkt->len - 1;
421
422 last_pkt_num = rpkt->pkt_num - (int64_t)(rpkt->len - 1);
423 }
424
425 return 0;
426 }
427
428 void ngtcp2_acktr_increase_ecn_counts(ngtcp2_acktr *acktr,
429 const ngtcp2_pkt_info *pi) {
430 switch (pi->ecn & NGTCP2_ECN_MASK) {
431 case NGTCP2_ECN_ECT_0:
432 ++acktr->ecn.ect0;
433 break;
434 case NGTCP2_ECN_ECT_1:
435 ++acktr->ecn.ect1;
436 break;
437 case NGTCP2_ECN_CE:
438 ++acktr->ecn.ce;
439 break;
440 }
441 }