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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
/*
* Copyright 2022-2024 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_fc.h"
#include "internal/quic_error.h"
#include "internal/common.h"
#include "internal/safe_math.h"
#include <assert.h>
OSSL_SAFE_MATH_UNSIGNED(uint64_t, uint64_t)
/*
* TX Flow Controller (TXFC)
* =========================
*/
int ossl_quic_txfc_init(QUIC_TXFC *txfc, QUIC_TXFC *conn_txfc)
{
if (conn_txfc != NULL && conn_txfc->parent != NULL)
return 0;
txfc->swm = 0;
txfc->cwm = 0;
txfc->parent = conn_txfc;
txfc->has_become_blocked = 0;
return 1;
}
QUIC_TXFC *ossl_quic_txfc_get_parent(QUIC_TXFC *txfc)
{
return txfc->parent;
}
int ossl_quic_txfc_bump_cwm(QUIC_TXFC *txfc, uint64_t cwm)
{
if (cwm <= txfc->cwm)
return 0;
txfc->cwm = cwm;
return 1;
}
uint64_t ossl_quic_txfc_get_credit_local(QUIC_TXFC *txfc, uint64_t consumed)
{
assert((txfc->swm + consumed) <= txfc->cwm);
return txfc->cwm - (consumed + txfc->swm);
}
uint64_t ossl_quic_txfc_get_credit(QUIC_TXFC *txfc, uint64_t consumed)
{
uint64_t r, conn_r;
r = ossl_quic_txfc_get_credit_local(txfc, 0);
if (txfc->parent != NULL) {
assert(txfc->parent->parent == NULL);
conn_r = ossl_quic_txfc_get_credit_local(txfc->parent, consumed);
if (conn_r < r)
r = conn_r;
}
return r;
}
int ossl_quic_txfc_consume_credit_local(QUIC_TXFC *txfc, uint64_t num_bytes)
{
int ok = 1;
uint64_t credit = ossl_quic_txfc_get_credit_local(txfc, 0);
if (num_bytes > credit) {
ok = 0;
num_bytes = credit;
}
if (num_bytes > 0 && num_bytes == credit)
txfc->has_become_blocked = 1;
txfc->swm += num_bytes;
return ok;
}
int ossl_quic_txfc_consume_credit(QUIC_TXFC *txfc, uint64_t num_bytes)
{
int ok = ossl_quic_txfc_consume_credit_local(txfc, num_bytes);
if (txfc->parent != NULL) {
assert(txfc->parent->parent == NULL);
if (!ossl_quic_txfc_consume_credit_local(txfc->parent, num_bytes))
return 0;
}
return ok;
}
int ossl_quic_txfc_has_become_blocked(QUIC_TXFC *txfc, int clear)
{
int r = txfc->has_become_blocked;
if (clear)
txfc->has_become_blocked = 0;
return r;
}
uint64_t ossl_quic_txfc_get_cwm(QUIC_TXFC *txfc)
{
return txfc->cwm;
}
uint64_t ossl_quic_txfc_get_swm(QUIC_TXFC *txfc)
{
return txfc->swm;
}
/*
* RX Flow Controller (RXFC)
* =========================
*/
int ossl_quic_rxfc_init(QUIC_RXFC *rxfc, QUIC_RXFC *conn_rxfc,
uint64_t initial_window_size,
uint64_t max_window_size,
OSSL_TIME (*now)(void *now_arg),
void *now_arg)
{
if (conn_rxfc != NULL && conn_rxfc->parent != NULL)
return 0;
rxfc->swm = 0;
rxfc->cwm = initial_window_size;
rxfc->rwm = 0;
rxfc->esrwm = 0;
rxfc->hwm = 0;
rxfc->cur_window_size = initial_window_size;
rxfc->max_window_size = max_window_size;
rxfc->parent = conn_rxfc;
rxfc->error_code = 0;
rxfc->has_cwm_changed = 0;
rxfc->epoch_start = ossl_time_zero();
rxfc->now = now;
rxfc->now_arg = now_arg;
rxfc->is_fin = 0;
rxfc->standalone = 0;
return 1;
}
int ossl_quic_rxfc_init_standalone(QUIC_RXFC *rxfc,
uint64_t initial_window_size,
OSSL_TIME (*now)(void *arg),
void *now_arg)
{
if (!ossl_quic_rxfc_init(rxfc, NULL,
initial_window_size, initial_window_size,
now, now_arg))
return 0;
rxfc->standalone = 1;
return 1;
}
QUIC_RXFC *ossl_quic_rxfc_get_parent(QUIC_RXFC *rxfc)
{
return rxfc->parent;
}
void ossl_quic_rxfc_set_max_window_size(QUIC_RXFC *rxfc,
size_t max_window_size)
{
rxfc->max_window_size = max_window_size;
}
static void rxfc_start_epoch(QUIC_RXFC *rxfc)
{
rxfc->epoch_start = rxfc->now(rxfc->now_arg);
rxfc->esrwm = rxfc->rwm;
}
static int on_rx_controlled_bytes(QUIC_RXFC *rxfc, uint64_t num_bytes)
{
int ok = 1;
uint64_t credit = rxfc->cwm - rxfc->swm;
if (num_bytes > credit) {
ok = 0;
num_bytes = credit;
rxfc->error_code = OSSL_QUIC_ERR_FLOW_CONTROL_ERROR;
}
rxfc->swm += num_bytes;
return ok;
}
int ossl_quic_rxfc_on_rx_stream_frame(QUIC_RXFC *rxfc, uint64_t end, int is_fin)
{
uint64_t delta;
if (!rxfc->standalone && rxfc->parent == NULL)
return 0;
if (rxfc->is_fin && ((is_fin && rxfc->hwm != end) || end > rxfc->hwm)) {
/* Stream size cannot change after the stream is finished */
rxfc->error_code = OSSL_QUIC_ERR_FINAL_SIZE_ERROR;
return 1; /* not a caller error */
}
if (is_fin)
rxfc->is_fin = 1;
if (end > rxfc->hwm) {
delta = end - rxfc->hwm;
rxfc->hwm = end;
on_rx_controlled_bytes(rxfc, delta); /* result ignored */
if (rxfc->parent != NULL)
on_rx_controlled_bytes(rxfc->parent, delta); /* result ignored */
} else if (end < rxfc->hwm && is_fin) {
rxfc->error_code = OSSL_QUIC_ERR_FINAL_SIZE_ERROR;
return 1; /* not a caller error */
}
return 1;
}
/* threshold = 3/4 */
#define WINDOW_THRESHOLD_NUM 3
#define WINDOW_THRESHOLD_DEN 4
static int rxfc_cwm_bump_desired(QUIC_RXFC *rxfc)
{
int err = 0;
uint64_t window_rem = rxfc->cwm - rxfc->rwm;
uint64_t threshold
= safe_muldiv_uint64_t(rxfc->cur_window_size,
WINDOW_THRESHOLD_NUM, WINDOW_THRESHOLD_DEN, &err);
if (err)
/*
* Extremely large window should never occur, but if it does, just use
* 1/2 as the threshold.
*/
threshold = rxfc->cur_window_size / 2;
/*
* No point emitting a new MAX_STREAM_DATA frame if the stream has a final
* size.
*/
return !rxfc->is_fin && window_rem <= threshold;
}
static int rxfc_should_bump_window_size(QUIC_RXFC *rxfc, OSSL_TIME rtt)
{
/*
* dt: time since start of epoch
* b: bytes of window consumed since start of epoch
* dw: proportion of window consumed since start of epoch
* T_window: time it will take to use up the entire window, based on dt, dw
* RTT: The current estimated RTT.
*
* b = rwm - esrwm
* dw = b / window_size
* T_window = dt / dw
* T_window = dt / (b / window_size)
* T_window = (dt * window_size) / b
*
|