1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/*
* Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include "internal/quic_fifd.h"
#include "internal/quic_wire.h"
#include "internal/qlog_event_helpers.h"
DEFINE_LIST_OF(tx_history, OSSL_ACKM_TX_PKT);
int ossl_quic_fifd_init(QUIC_FIFD *fifd,
QUIC_CFQ *cfq,
OSSL_ACKM *ackm,
QUIC_TXPIM *txpim,
/* stream_id is UINT64_MAX for the crypto stream */
QUIC_SSTREAM *(*get_sstream_by_id)(uint64_t stream_id,
uint32_t pn_space,
void *arg),
void *get_sstream_by_id_arg,
/* stream_id is UINT64_MAX if not applicable */
void (*regen_frame)(uint64_t frame_type,
uint64_t stream_id,
QUIC_TXPIM_PKT *pkt,
void *arg),
void *regen_frame_arg,
void (*confirm_frame)(uint64_t frame_type,
uint64_t stream_id,
QUIC_TXPIM_PKT *pkt,
void *arg),
void *confirm_frame_arg,
void (*sstream_updated)(uint64_t stream_id,
void *arg),
void *sstream_updated_arg,
QLOG *(*get_qlog_cb)(void *arg),
void *get_qlog_cb_arg)
{
if (cfq == NULL || ackm == NULL || txpim == NULL
|| get_sstream_by_id == NULL || regen_frame == NULL)
return 0;
fifd->cfq = cfq;
fifd->ackm = ackm;
fifd->txpim = txpim;
fifd->get_sstream_by_id = get_sstream_by_id;
fifd->get_sstream_by_id_arg = get_sstream_by_id_arg;
fifd->regen_frame = regen_frame;
fifd->regen_frame_arg = regen_frame_arg;
fifd->confirm_frame = confirm_frame;
fifd->confirm_frame_arg = confirm_frame_arg;
fifd->sstream_updated = sstream_updated;
fifd->sstream_updated_arg = sstream_updated_arg;
fifd->get_qlog_cb = get_qlog_cb;
fifd->get_qlog_cb_arg = get_qlog_cb_arg;
return 1;
}
void ossl_quic_fifd_cleanup(QUIC_FIFD *fifd)
{
/* No-op. */
}
static void on_acked(void *arg)
{
QUIC_TXPIM_PKT *pkt = arg;
QUIC_FIFD *fifd = pkt->fifd;
const QUIC_TXPIM_CHUNK *chunks = ossl_quic_txpim_pkt_get_chunks(pkt);
size_t i, num_chunks = ossl_quic_txpim_pkt_get_num_chunks(pkt);
QUIC_SSTREAM *sstream;
QUIC_CFQ_ITEM *cfq_item, *cfq_item_next;
/* STREAM and CRYPTO stream chunks, FINs and stream FC frames */
for (i = 0; i < num_chunks; ++i) {
sstream = fifd->get_sstream_by_id(chunks[i].stream_id,
pkt->ackm_pkt.pkt_space,
fifd->get_sstream_by_id_arg);
if (sstream == NULL)
continue;
if (chunks[i].end >= chunks[i].start)
/* coverity[check_return]: Best effort - we cannot fail here. */
ossl_quic_sstream_mark_acked(sstream,
chunks[i].start, chunks[i].end);
if (chunks[i].has_fin && chunks[i].stream_id != UINT64_MAX)
ossl_quic_sstream_mark_acked_fin(sstream);
if (chunks[i].has_stop_sending && chunks[i].stream_id != UINT64_MAX)
fifd->confirm_frame(OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
chunks[i].stream_id, pkt,
fifd->confirm_frame_arg);
if (chunks[i].has_reset_stream && chunks[i].stream_id != UINT64_MAX)
fifd->confirm_frame(OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
chunks[i].stream_id, pkt,
fifd->confirm_frame_arg);
if (ossl_quic_sstream_is_totally_acked(sstream))
fifd->sstream_updated(chunks[i].stream_id, fifd->sstream_updated_arg);
}
/* GCR */
for (cfq_item = pkt->retx_head; cfq_item != NULL; cfq_item = cfq_item_next) {
cfq_item_next = cfq_item->pkt_next;
ossl_quic_cfq_release(fifd->cfq, cfq_item);
}
ossl_quic_txpim_pkt_release(fifd->txpim, pkt);
}
static QLOG *fifd_get_qlog(QUIC_FIFD *fifd)
{
if (fifd->get_qlog_cb == NULL)
return NULL;
return fifd->get_qlog_cb(fifd->get_qlog_cb_arg);
}
static void on_lost(void *arg)
{
QUIC_TXPIM_PKT *pkt = arg;
QUIC_FIFD *fifd = pkt->fifd;
const QUIC_TXPIM_CHUNK *chunks = ossl_quic_txpim_pkt_get_chunks(pkt);
size_t i, num_chunks = ossl_quic_txpim_pkt_get_num_chunks(pkt);
QUIC_SSTREAM *sstream;
QUIC_CFQ_ITEM *cfq_item, *cfq_item_next;
int sstream_updated;
ossl_qlog_event_recovery_packet_lost(fifd_get_qlog(fifd), pkt);
/* STREAM and CRYPTO stream chunks, FIN and stream FC frames */
for (i = 0; i < num_chunks; ++i) {
sstream = fifd->get_sstream_by_id(chunks[i].stream_id,
pkt->ackm_pkt.pkt_space,
fifd->get_sstream_by_id_arg);
if (sstream == NULL)
continue;
sstream_updated = 0;
if (chunks[i].end >= chunks[i].start) {
/*
* Note: If the stream is being reset, we do not need to retransmit
* old data as this is pointless. In this case this will be handled
* by (sstream == NULL) above as the QSM will free the QUIC_SSTREAM
* and our call to get_sstream_by_id above will return NULL.
*/
ossl_quic_sstream_mark_lost(sstream,
chunks[i].start, chunks[i].end);
sstream_updated = 1;
}
if (chunks[i].has_fin && chunks[i].stream_id != UINT64_MAX) {
ossl_quic_sstream_mark_lost_fin(sstream);
sstream_updated = 1;
}
if (chunks[i].has_stop_sending && chunks[i].stream_id != UINT64_MAX)
fifd->regen_frame(OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
chunks[i].stream_id, pkt,
fifd->regen_frame_arg);
if (chunks[i].has_reset_stream && chunks[i].stream_id != UINT64_MAX)
fifd->regen_frame(OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
chunks[i].stream_id, pkt,
fifd->regen_frame_arg);
/*
* Inform caller that stream needs an FC frame.
*
* Note: We could track whether an FC frame was sent originally for the
* stream to determine if it really needs to be regenerated or not.
* However, if loss has occurred, it's probably better to ensure the
* peer has up-to-date flow control data for the stream. Given that
* these frames are extremely small, we may as well always send it when
* handling loss.
*/
fifd->regen_frame(OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA,
chunks[i].stream_id,
pkt,
fifd->regen_frame_arg);
if (sstream_updated && chunks[i].stream_id != UINT64_MAX)
fifd->sstream_updated(chunks[i].stream_id,
fifd->sstream_updated_arg);
}
/* GCR */
for (cfq_item = pkt->retx_head; cfq_item != NULL; cfq_item = cfq_item_next) {
cfq_item_next = cfq_item->pkt_next;
ossl_quic_cfq_mark_lost(fifd->cfq, cfq_item, UINT32_MAX);
}
/* Regenerate flag frames */
if (pkt->had_handshake_done_frame)
fifd->regen_frame(OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE,
UINT64_MAX, pkt,
fifd->regen_frame_arg);
if (pkt->had_max_data_frame)
fifd->regen_frame(OSSL_QUIC_FRAME_TYPE_MAX_DATA,
UINT64_MAX, pkt,
fifd->regen_frame_arg);
if (pkt->had_max_streams_bidi_frame)
fifd->regen_frame(OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI,
UINT64_MAX, pkt,
fifd->regen_frame_arg);
if (pkt->had_max_streams_uni_frame)
fifd->regen_frame(OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI,
UINT64_MAX, pkt,
fifd->regen_frame_arg);
if (pkt->had_ack_frame)
/*
* We always use the ACK_WITH_ECN frame type to represent the ACK frame
* type in our callback; we assume it is the caller's job to decide
* whether it wants to send ECN data or not.
*/
fifd->regen_frame(OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN,
UINT64_MAX, pkt,
fifd->regen_frame_arg);
ossl_quic_txpim_pkt_release(fifd->txpim, pkt);
}
static void on_discarded(void *arg)
{
QUIC_TXPIM_PKT *pkt = arg;
QUIC_FIFD *fifd = pkt->fifd;
QUIC_CFQ_ITEM *cfq_item, *cfq_item_next;
/*
* Don't need to do anything to SSTREAMs for STREAM and CRYPTO streams, as
* we assume caller will clean them up.
*/
/* GCR */
for (cfq_item = pkt->retx_head; cfq_item != NULL
|