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
|
/*
* ngtcp2
*
* Copyright (c) 2017 ngtcp2 contributors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef NGTCP2_RTB_H
#define NGTCP2_RTB_H
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */
#include <ngtcp2/ngtcp2.h>
#include "ngtcp2_pkt.h"
#include "ngtcp2_ksl.h"
#include "ngtcp2_pq.h"
struct ngtcp2_conn;
typedef struct ngtcp2_conn ngtcp2_conn;
struct ngtcp2_frame_chain;
typedef struct ngtcp2_frame_chain ngtcp2_frame_chain;
struct ngtcp2_log;
typedef struct ngtcp2_log ngtcp2_log;
struct ngtcp2_qlog;
typedef struct ngtcp2_qlog ngtcp2_qlog;
struct ngtcp2_strm;
typedef struct ngtcp2_strm ngtcp2_strm;
struct ngtcp2_rst;
typedef struct ngtcp2_rst ngtcp2_rst;
/*
* ngtcp2_frame_chain chains frames in a single packet.
*/
struct ngtcp2_frame_chain {
ngtcp2_frame_chain *next;
ngtcp2_frame fr;
};
/* NGTCP2_MAX_STREAM_DATACNT is the maximum number of ngtcp2_vec that
a ngtcp2_stream can include. */
#define NGTCP2_MAX_STREAM_DATACNT 256
/* NGTCP2_MAX_CRYPTO_DATACNT is the maximum number of ngtcp2_vec that
a ngtcp2_crypto can include. */
#define NGTCP2_MAX_CRYPTO_DATACNT 8
/*
* ngtcp2_frame_chain_new allocates ngtcp2_frame_chain object and
* assigns its pointer to |*pfrc|.
*
* This function returns 0 if it succeeds, or one of the following
* negative error codes:
*
* NGTCP2_ERR_NOMEM
* Out of memory.
*/
int ngtcp2_frame_chain_new(ngtcp2_frame_chain **pfrc, const ngtcp2_mem *mem);
/*
* ngtcp2_frame_chain_extralen_new works like ngtcp2_frame_chain_new,
* but it allocates extra memory |extralen| in order to extend
* ngtcp2_frame.
*/
int ngtcp2_frame_chain_extralen_new(ngtcp2_frame_chain **pfrc, size_t extralen,
const ngtcp2_mem *mem);
/*
* ngtcp2_frame_chain_stream_datacnt_new works like
* ngtcp2_frame_chain_new, but it allocates enough data to store
* additional |datacnt| - 1 ngtcp2_vec object after ngtcp2_stream
* object. If |datacnt| equals to 1, ngtcp2_frame_chain_new is called
* internally.
*/
int ngtcp2_frame_chain_stream_datacnt_new(ngtcp2_frame_chain **pfrc,
size_t datacnt,
const ngtcp2_mem *mem);
/*
* ngtcp2_frame_chain_crypto_datacnt_new works like
* ngtcp2_frame_chain_new, but it allocates enough data to store
* additional |datacnt| - 1 ngtcp2_vec object after ngtcp2_crypto
* object. If |datacnt| equals to 1, ngtcp2_frame_chain_new is called
* internally.
*/
int ngtcp2_frame_chain_crypto_datacnt_new(ngtcp2_frame_chain **pfrc,
size_t datacnt,
const ngtcp2_mem *mem);
/*
* ngtcp2_frame_chain_del deallocates |frc|. It also deallocates the
* memory pointed by |frc|.
*/
void ngtcp2_frame_chain_del(ngtcp2_frame_chain *frc, const ngtcp2_mem *mem);
/*
* ngtcp2_frame_chain_init initializes |frc|.
*/
void ngtcp2_frame_chain_init(ngtcp2_frame_chain *frc);
/*
* ngtcp2_frame_chain_list_del deletes |frc|, and all objects
* connected by next field.
*/
void ngtcp2_frame_chain_list_del(ngtcp2_frame_chain *frc,
const ngtcp2_mem *mem);
typedef enum {
NGTCP2_RTB_FLAG_NONE = 0x00,
/* NGTCP2_RTB_FLAG_PROBE indicates that the entry includes a probe
packet. */
NGTCP2_RTB_FLAG_PROBE = 0x01,
/* NGTCP2_RTB_FLAG_CRYPTO_PKT indicates that the entry includes
handshake CRYPTO frame. */
NGTCP2_RTB_FLAG_CRYPTO_PKT = 0x02,
/* NGTCP2_RTB_FLAG_ACK_ELICITING indicates that the entry elicits
acknowledgement. */
NGTCP2_RTB_FLAG_ACK_ELICITING = 0x04,
/* NGTCP2_RTB_FLAG_CRYPTO_TIMEOUT_RETRANSMITTED indicates that the
CRYPTO frames have been retransmitted. */
NGTCP2_RTB_FLAG_CRYPTO_TIMEOUT_RETRANSMITTED = 0x08,
} ngtcp2_rtb_flag;
struct ngtcp2_rtb_entry;
typedef struct ngtcp2_rtb_entry ngtcp2_rtb_entry;
/*
* ngtcp2_rtb_entry is an object stored in ngtcp2_rtb. It corresponds
* to the one packet which is waiting for its ACK.
*/
struct ngtcp2_rtb_entry {
ngtcp2_rtb_entry *next;
struct {
int64_t pkt_num;
uint8_t type;
uint8_t flags;
} hd;
ngtcp2_frame_chain *frc;
/* ts is the time point when a packet included in this entry is sent
to a peer. */
ngtcp2_tstamp ts;
/* pktlen is the length of QUIC packet */
size_t pktlen;
struct {
uint64_t delivered;
ngtcp2_tstamp delivered_ts;
ngtcp2_tstamp first_sent_ts;
int is_app_limited;
} rst;
/* flags is bitwise-OR of zero or more of ngtcp2_rtb_flag. */
uint8_t flags;
};
/*
* ngtcp2_rtb_entry_new allocates ngtcp2_rtb_entry object, and assigns
* its pointer to |*pent|. On success, |*pent| takes ownership of
* |frc|.
*
* This function returns 0 if it succeeds, or one of the following
* negative error codes:
*
* NGTCP2_ERR_NOMEM
* Out of memory.
*/
int ngtcp2_rtb_entry_new(ngtcp2_rtb_entry **pent, const ngtcp2_pkt_hd *hd,
ngtcp2_frame_chain *frc, ngtcp2_tstamp ts,
size_t pktlen, uint8_t flags, const ngtcp2_mem *mem);
/*
* ngtcp2_rtb_entry_del deallocates |ent|. It also frees memory
* pointed by |ent|.
*/
void ngtcp2_rtb_entry_del(ngtcp2_rtb_entry *ent, const ngtcp2_mem *mem);
/*
* ngtcp2_rtb tracks sent packets, and its ACK timeout for
* retransmission.
*/
typedef struct {
/* ents includes ngtcp2_rtb_entry sorted by decreasing order of
packet number. */
ngtcp2_ksl ents;
/* crypto is CRYPTO stream. */
ngtcp2_strm *crypto;
ngtcp2_rst *rst;
ngtcp2_cc *cc;
ngtcp2_log *log;
ngtcp2_qlog *qlog;
const ngtcp2_mem *mem;
/* largest_acked_tx_pkt_num is the largest packet number
acknowledged by the peer. */
int64_t largest_acked_tx_pkt_num;
/* num_ack_eliciting is the number of ACK eliciting entries. */
size_t num_ack_eliciting;
/* probe_pkt_left is the number of probe packet to send */
size_t probe_pkt_left;
/* pktns_id is the identifier of packet number space. */
ngtcp2_pktns_id pktns_id;
/* cc_pkt_num is the smallest packet number that is contributed to
ngtcp2_conn_stat.bytes_in_flight. */
int64_t cc_pkt_num;
/* cc_bytes_in_flight is the number of in-flight bytes that is
contributed to ngtcp2_conn_stat.bytes_in_flight. It only
includes the bytes after congestion state is reset. */
uint64_t cc_bytes_in_flight;
} ngtcp2_rtb;
/*
* ngtcp2_rtb_init initializes |rtb|.
*/
void ngtcp2_rtb_init(ngtcp2_rtb *rtb, ngtcp2_pktns_id pktns_id,
ngtcp2_strm *crypto, ngtcp2_rst *rst, ngtcp2_cc *cc,
ngtcp2_log *log, ngtcp2_qlog *qlog, const ngtcp2_mem *mem);
/*
* ngtcp2_rtb_free deallocates resources allocated for |rtb|.
*/
void ngtcp2_rtb_free(ngtcp2_rtb *rtb);
/*
* ngtcp2_rtb_add adds |ent| to |rtb|.
*
* This function returns 0 if it succeeds, or one of the following
* negative error codes:
*
* NGTCP2_ERR_NOMEM
* Out of memory
*/
int ngtcp2_rtb_add(ngtcp2_rtb *rtb, ngtcp2_rtb_entry *ent,
ngtcp2_conn_stat *cstat);
/*
* ngtcp2_rtb_head returns the iterator which points to the entry
* which has the largest packet number. If there is no entry,
* returned value satisfies ngtcp2_ksl_it_end(&it) != 0.
*/
ngtcp2_ksl_it ngtcp2_rtb_head(ngtcp2_rtb *rtb);
/*
* ngtcp2_rtb_recv_ack removes acked ngtcp2_rtb_entry from |rtb|.
* |pkt_num| is a packet number which includes |fr|. |pkt_ts| is the
* timestamp when packet is received. |ts| should be the current
* time. Usually they are the same, but for buffered packets,
* |pkt_ts| would be earlier than |ts|.
*
* This function returns the number of newly acknowledged packets if
* it succeeds, or one of the following negative error codes:
*
* NGTCP2_ERR_CALLBACK_FAILURE
* User callback failed
* NGTCP2_ERR_NOMEM
* Out of memory
*/
ngtcp2_ssize ngtcp2_rtb_recv_ack(ngtcp2_rtb *rtb, const ngtcp2_ack *fr,
ngtcp2_conn_stat *cstat, ngtcp2_conn *conn,
ngtcp2_tstamp pkt_ts, ngtcp2_tstamp ts);
/*
* ngtcp2_rtb_detect_lost_pkt detects lost packets and prepends the
* frames contained them to |*pfrc|. Even when this function fails,
* some frames might be prepended to |*pfrc| and the caller should
* handle them. |pto| is PTO.
*/
void ngtcp2_rtb_detect_lost_pkt(ngtcp2_rtb *rtb, ngtcp2_frame_chain **pfrc,
ngtcp2_conn_stat *cstat, ngtcp2_duration pto,
ngtcp2_tstamp ts);
/*
* ngtcp2_rtb_remove_all removes all packets from |rtb| and prepends
* all frames to |*pfrc|. Even when this function fails, some frames
* might be prepended to |*pfrc| and the caller should handle them.
*/
void ngtcp2_rtb_remove_all(ngtcp2_rtb *rtb, ngtcp2_frame_chain **pfrc,
ngtcp2_conn_stat *cstat);
/*
* ngtcp2_rtb_on_crypto_timeout copies all unacknowledged CRYPTO
* frames and links them to |*pfrc|. The affected ngtcp2_rtb_entry
* will have NGTCP2_RTB_FLAG_CRYPTO_TIMEOUT_RETRANSMITTED set.
*
* This function returns 0 if it succeeds, or one of the following
* negative error codes:
*
* NGTCP2_ERR_NOMEM
* Out of memory
*/
int ngtcp2_rtb_on_crypto_timeout(ngtcp2_rtb *rtb, ngtcp2_frame_chain **pfrc,
ngtcp2_conn_stat *cstat);
/*
* ngtcp2_rtb_empty returns nonzero if |rtb| have no entry.
*/
int ngtcp2_rtb_empty(ngtcp2_rtb *rtb);
/*
* ngtcp2_rtb_reset_cc_state resets congestion state in |rtb|.
* |cc_pkt_num| is the next outbound packet number which is sent under
* new congestion state.
*/
void ngtcp2_rtb_reset_cc_state(ngtcp2_rtb *rtb, int64_t cc_pkt_num);
#endif /* NGTCP2_RTB_H */
|