/src/h2o/deps/quicly/lib/sentmap.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2017 Fastly, Kazuho Oku |
3 | | * |
4 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
5 | | * of this software and associated documentation files (the "Software"), to |
6 | | * deal in the Software without restriction, including without limitation the |
7 | | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
8 | | * sell copies of the Software, and to permit persons to whom the Software is |
9 | | * furnished to do so, subject to the following conditions: |
10 | | * |
11 | | * The above copyright notice and this permission notice shall be included in |
12 | | * all copies or substantial portions of the Software. |
13 | | * |
14 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
15 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
16 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
17 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
18 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
19 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
20 | | * IN THE SOFTWARE. |
21 | | */ |
22 | | #include <assert.h> |
23 | | #include <math.h> |
24 | | #include <stdlib.h> |
25 | | #include "picotls.h" |
26 | | #include "quicly/sentmap.h" |
27 | | |
28 | | const quicly_sent_t quicly_sentmap__end_iter = {quicly_sentmap__type_packet, {{UINT64_MAX, INFINITY}}}; |
29 | | |
30 | | static void next_entry(quicly_sentmap_iter_t *iter) |
31 | 0 | { |
32 | 0 | if (--iter->count != 0) { |
33 | 0 | ++iter->p; |
34 | 0 | } else if (*(iter->ref = &(*iter->ref)->next) == NULL) { |
35 | 0 | iter->p = (quicly_sent_t *)&quicly_sentmap__end_iter; |
36 | 0 | iter->count = 0; |
37 | 0 | return; |
38 | 0 | } else { |
39 | 0 | assert((*iter->ref)->num_entries != 0); |
40 | 0 | iter->count = (*iter->ref)->num_entries; |
41 | 0 | iter->p = (*iter->ref)->entries; |
42 | 0 | } |
43 | 0 | while (iter->p->acked == NULL) |
44 | 0 | ++iter->p; |
45 | 0 | } |
46 | | |
47 | | static struct st_quicly_sent_block_t **free_block(quicly_sentmap_t *map, struct st_quicly_sent_block_t **ref) |
48 | 0 | { |
49 | 0 | static const struct st_quicly_sent_block_t dummy = {NULL}; |
50 | 0 | static const struct st_quicly_sent_block_t *const dummy_ref = &dummy; |
51 | 0 | struct st_quicly_sent_block_t *block = *ref; |
52 | |
|
53 | 0 | if (block->next != NULL) { |
54 | 0 | *ref = block->next; |
55 | 0 | assert((*ref)->num_entries != 0); |
56 | 0 | } else { |
57 | 0 | assert(block == map->tail); |
58 | 0 | if (ref == &map->head) { |
59 | 0 | map->head = NULL; |
60 | 0 | map->tail = NULL; |
61 | 0 | } else { |
62 | 0 | map->tail = (void *)((char *)ref - offsetof(struct st_quicly_sent_block_t, next)); |
63 | 0 | map->tail->next = NULL; |
64 | 0 | } |
65 | 0 | ref = (struct st_quicly_sent_block_t **)&dummy_ref; |
66 | 0 | } |
67 | | |
68 | 0 | free(block); |
69 | 0 | return ref; |
70 | 0 | } |
71 | | |
72 | | static void discard_entry(quicly_sentmap_t *map, quicly_sentmap_iter_t *iter) |
73 | 0 | { |
74 | 0 | assert(iter->p->acked != NULL); |
75 | 0 | iter->p->acked = NULL; |
76 | |
|
77 | 0 | struct st_quicly_sent_block_t *block = *iter->ref; |
78 | 0 | if (--block->num_entries == 0) { |
79 | 0 | iter->ref = free_block(map, iter->ref); |
80 | 0 | block = *iter->ref; |
81 | 0 | iter->p = block->entries - 1; |
82 | 0 | iter->count = block->num_entries + 1; |
83 | 0 | } |
84 | 0 | } |
85 | | |
86 | | void quicly_sentmap_dispose(quicly_sentmap_t *map) |
87 | 0 | { |
88 | 0 | struct st_quicly_sent_block_t *block; |
89 | |
|
90 | 0 | while ((block = map->head) != NULL) { |
91 | 0 | map->head = block->next; |
92 | 0 | free(block); |
93 | 0 | } |
94 | 0 | } |
95 | | |
96 | | quicly_error_t quicly_sentmap_prepare(quicly_sentmap_t *map, uint64_t packet_number, double now, uint8_t ack_epoch) |
97 | 0 | { |
98 | 0 | assert(map->_pending_packet == NULL); |
99 | | |
100 | 0 | if ((map->_pending_packet = quicly_sentmap_allocate(map, quicly_sentmap__type_packet)) == NULL) |
101 | 0 | return PTLS_ERROR_NO_MEMORY; |
102 | 0 | map->_pending_packet->data.packet = (quicly_sent_packet_t){packet_number, now, ack_epoch}; |
103 | 0 | return 0; |
104 | 0 | } |
105 | | |
106 | | struct st_quicly_sent_block_t *quicly_sentmap__new_block(quicly_sentmap_t *map) |
107 | 0 | { |
108 | 0 | struct st_quicly_sent_block_t *block; |
109 | |
|
110 | 0 | if ((block = malloc(sizeof(*block))) == NULL) |
111 | 0 | return NULL; |
112 | | |
113 | 0 | block->next = NULL; |
114 | 0 | block->num_entries = 0; |
115 | 0 | block->next_insert_at = 0; |
116 | 0 | if (map->tail != NULL) { |
117 | 0 | map->tail->next = block; |
118 | 0 | map->tail = block; |
119 | 0 | } else { |
120 | 0 | map->head = map->tail = block; |
121 | 0 | } |
122 | |
|
123 | 0 | return block; |
124 | 0 | } |
125 | | |
126 | | void quicly_sentmap_skip(quicly_sentmap_iter_t *iter) |
127 | 0 | { |
128 | 0 | do { |
129 | 0 | next_entry(iter); |
130 | 0 | } while (iter->p->acked != quicly_sentmap__type_packet); |
131 | 0 | } |
132 | | |
133 | | quicly_error_t quicly_sentmap_update(quicly_sentmap_t *map, quicly_sentmap_iter_t *iter, quicly_sentmap_event_t event) |
134 | 0 | { |
135 | 0 | quicly_sent_packet_t packet; |
136 | 0 | quicly_error_t ret = 0; |
137 | |
|
138 | 0 | assert(iter->p != &quicly_sentmap__end_iter); |
139 | 0 | assert(iter->p->acked == quicly_sentmap__type_packet); |
140 | | |
141 | | /* copy packet info */ |
142 | 0 | packet = iter->p->data.packet; |
143 | | |
144 | | /* update CC state unless the event is PTO */ |
145 | 0 | if (packet.cc_bytes_in_flight != 0 && event != QUICLY_SENTMAP_EVENT_PTO) { |
146 | 0 | assert(map->bytes_in_flight >= packet.cc_bytes_in_flight); |
147 | 0 | map->bytes_in_flight -= packet.cc_bytes_in_flight; |
148 | 0 | iter->p->data.packet.cc_bytes_in_flight = 0; |
149 | 0 | } |
150 | 0 | iter->p->data.packet.frames_in_flight = 0; |
151 | |
|
152 | 0 | int should_notify = event == QUICLY_SENTMAP_EVENT_ACKED || packet.frames_in_flight, |
153 | 0 | should_discard = event == QUICLY_SENTMAP_EVENT_ACKED || event == QUICLY_SENTMAP_EVENT_EXPIRED; |
154 | | |
155 | | /* Advance to next packet, while if necessary, doing either or both of the following: |
156 | | * * discard entries (if should_discard is set) |
157 | | * * invoke the frame-level callbacks (if should_notify is set) */ |
158 | 0 | if (should_discard) { |
159 | 0 | discard_entry(map, iter); |
160 | 0 | --map->num_packets; |
161 | 0 | } |
162 | 0 | for (next_entry(iter); iter->p->acked != quicly_sentmap__type_packet; next_entry(iter)) { |
163 | 0 | if (should_notify && (ret = iter->p->acked(map, &packet, event == QUICLY_SENTMAP_EVENT_ACKED, iter->p)) != 0) |
164 | 0 | should_notify = 0; |
165 | 0 | if (should_discard) |
166 | 0 | discard_entry(map, iter); |
167 | 0 | } |
168 | |
|
169 | 0 | return ret; |
170 | 0 | } |
171 | | |
172 | | quicly_error_t quicly_sentmap__type_packet(quicly_sentmap_t *map, const quicly_sent_packet_t *packet, int acked, |
173 | | quicly_sent_t *sent) |
174 | 0 | { |
175 | 0 | assert(!"quicly_sentmap__type_packet cannot be called"); |
176 | 0 | return QUICLY_TRANSPORT_ERROR_INTERNAL; |
177 | 0 | } |