summaryrefslogtreecommitdiffstats
path: root/src/libknot/xdp/quic_conn.h
blob: 20ee17653ec89e50714b84beb855ac8b9a3b49b7 (plain)
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
/*  Copyright (C) 2022 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

/*!
 * \file
 *
 * \brief QUIC connection management.
 *
 * \addtogroup xdp
 * @{
 */

#pragma once

#include <linux/if_ether.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/uio.h>

#define MAX_STREAMS_PER_CONN 10

struct ngtcp2_cid; // declaration taken from wherever in ngtcp2
struct knot_quic_creds;
struct knot_sweep_stats;

// those are equivalent to contrib/ucw/lists.h , just must not be included.
typedef struct knot_xquic_ucw_node {
	struct knot_xquic_ucw_node *next, *prev;
} knot_xquic_ucw_node_t;
typedef struct knot_xquic_ucw_list {
	knot_xquic_ucw_node_t head, tail;
} knot_xquic_ucw_list_t;

typedef struct {
	void *get_conn;
	void *user_data;
} nc_conn_ref_placeholder_t;

typedef struct {
	knot_xquic_ucw_node_t node;
	size_t len;
	uint8_t buf[];
} knot_xquic_obuf_t;

typedef struct {
	struct iovec inbuf;
	struct iovec *inbuf_fin;
	knot_xquic_ucw_list_t outbufs;
	size_t obufs_size;

	knot_xquic_obuf_t *unsent_obuf;
	size_t first_offset;
	size_t unsent_offset;
} knot_xquic_stream_t;

typedef struct knot_xquic_conn {
	knot_xquic_ucw_node_t timeout; // MUST be first field of the struct
	uint64_t last_ts;

	nc_conn_ref_placeholder_t conn_ref; // placeholder for internal struct ngtcp2_crypto_conn_ref

	struct ngtcp2_conn *conn;

	struct gnutls_session_int *tls_session;

	knot_xquic_stream_t *streams;
	int16_t streams_count; // number of allocated streams structures
	int16_t stream_inprocess; // index of first stream that has complete incomming data to be processed (aka inbuf_fin)
	bool handshake_done;
	bool session_taken; // TODO ... ?
	int64_t streams_first; // stream_id/4 of first allocated stream
	size_t ibufs_size;
	size_t obufs_size;

	struct knot_xquic_table *xquic_table;

	struct knot_xquic_conn *next;
} knot_xquic_conn_t;

typedef struct knot_xquic_cid {
	uint8_t cid_placeholder[32];
	knot_xquic_conn_t *conn;
	struct knot_xquic_cid *next;
} knot_xquic_cid_t;

typedef struct knot_xquic_table {
	size_t size;
	size_t usage;
	size_t pointers;
	size_t max_conns;
	size_t ibufs_max;
	size_t obufs_max;
	size_t ibufs_size;
	size_t obufs_size;
	size_t udp_payload_limit; // for simplicity not distinguishing IPv4/6
	void (*log_cb)(const char *);
	uint64_t hash_secret[4];
	struct knot_quic_creds *creds;
	knot_xquic_ucw_list_t timeout;
	knot_xquic_cid_t *conns[];
} knot_xquic_table_t;

/*!
 * \brief Allocate QUIC connections hash table.
 *
 * \param max_conns    Maximum nuber of connections.
 * \param max_ibufs    Maximum size of buffers for fragmented incomming DNS msgs.
 * \param max_obufs    Maximum size of buffers for un-ACKed outgoing data.
 * \param udp_pl       Maximum UDP payload size (both IPv4 and 6).
 * \param creds        QUIC crypto context..
 *
 * \return Allocated table, or NULL.
 */
knot_xquic_table_t *knot_xquic_table_new(size_t max_conns, size_t max_ibufs, size_t max_obufs,
                                         size_t udp_payload, struct knot_quic_creds *creds);

/*!
 * \brief Free QUIC table including its contents.
 *
 * \param table    Table to be freed.
 */
void knot_xquic_table_free(knot_xquic_table_t *table);

/*!
 * \brief Close timed out connections and some oldest ones if table full.
 *
 * \param table       QUIC table to be cleaned up.
 * \param stats       Out: sweep statistics.
 *
 * \return KNOT_E*
 */
int knot_xquic_table_sweep(knot_xquic_table_t *table, struct knot_sweep_stats *stats);

/*!
 * \brief Add new connection/CID link to table.
 *
 * \param xconn    QUIC connection linked.
 * \param cid      New CID to be added.
 * \param table    QUIC table to be modified.
 *
 * \return Pointer on the CID reference in table, or NULL.
 */
knot_xquic_cid_t **xquic_table_insert(knot_xquic_conn_t *xconn,
                                      const struct ngtcp2_cid *cid,
                                      knot_xquic_table_t *table);

/*!
 * \brief Add new connection to the table, allocating conn struct.
 *
 * \param conn      Ngtcp2 conn struct.
 * \param cid       CID to be linked (usually oscid for server).
 * \param table     QUIC table to be modified.
 *
 * \return Allocated (and linked) Knot conn struct, or NULL.
 */
knot_xquic_conn_t *xquic_table_add(struct ngtcp2_conn *conn,
                                   const struct ngtcp2_cid *cid,
                                   knot_xquic_table_t *table);

/*!
 * \brief Lookup connection/CID link in table.
 *
 * \param cid      CID to be searched for.
 * \param table    QUIC table.
 *
 * \return Pointer on the CID reference in table, or NULL.
 */
knot_xquic_cid_t **xquic_table_lookup2(const struct ngtcp2_cid *cid,
                                       knot_xquic_table_t *table);

/*!
 * \brief Lookup QUIC connection in table.
 *
 * \param cid      CID to be searched for.
 * \param table    QUIC table.
 *
 * \return Connection that the CID belongs to, or NULL.
 */
knot_xquic_conn_t *xquic_table_lookup(const struct ngtcp2_cid *cid,
                                      knot_xquic_table_t *table);

/*!
 * \brief Put the connection on the end of timeout queue.
 */
void xquic_conn_mark_used(knot_xquic_conn_t *conn, knot_xquic_table_t *table,
                          uint64_t now);

/*!
 * \brief Remove connection/CID link from table.
 *
 * \param pcid     CID to be removed.
 * \param table    QUIC table.
 */
void xquic_table_rem2(knot_xquic_cid_t **pcid, knot_xquic_table_t *table);

/*!
 * \brief Remove specified stream from QUIC connection, freeing all buffers.
 *
 * \param xconn         QUIC connection to remove from.
 * \param stream_id     Stream QUIC ID.
 */
void xquic_stream_free(knot_xquic_conn_t *xconn, int64_t stream_id);

/*!
 * \brief Remove and deinitialize connection completely.
 *
 * \param conn      Connection to be removed.
 * \param table     Table to remove from.
 */
void knot_xquic_table_rem(knot_xquic_conn_t *conn, knot_xquic_table_t *table);

/*!
 * \brief Fetch or initialize a QUIC stream.
 *
 * \param xconn          QUIC connection.
 * \param stream_id      Stream QUIC ID.
 * \param create         Trigger stream creation if not exists.
 *
 * \return Stream or NULL.
 */
knot_xquic_stream_t *knot_xquic_conn_get_stream(knot_xquic_conn_t *xconn,
                                                int64_t stream_id, bool create);

/*!
 * \brief Process incomming stream data to stream structure.
 *
 * \param xconn         QUIC connection that has received data.
 * \param stream_id     Stream QUIC ID of the incomming data.
 * \param data          Incomming payload data.
 * \param len           Incomming payload data length.
 * \param fin           FIN flag set for incomming data.
 *
 * \return KNOT_E*
 */
int knot_xquic_stream_recv_data(knot_xquic_conn_t *xconn, int64_t stream_id,
                                const uint8_t *data, size_t len, bool fin);

/*!
 * \brief Get next stream which has pending incomming data to be processed.
 *
 * \param xconn        QUIC connection.
 * \param stream_id    Out: strem QUIC ID of the returned stream.
 *
 * \return Stream with incomming data.
 */
knot_xquic_stream_t *knot_xquic_stream_get_process(knot_xquic_conn_t *xconn,
                                                   int64_t *stream_id);

/*!
 * \brief Add outgiong data to the stream for sending.
 *
 * \param xconn         QUIC connection that shall send data.
 * \param stream_id     Stream ID for outgoing data.
 * \param data          Data payload.
 * \param len           Data payload length.
 *
 * \return NULL if error, or pinter at the data in outgiong buffer.
 */
uint8_t *knot_xquic_stream_add_data(knot_xquic_conn_t *xconn, int64_t stream_id,
                                    uint8_t *data, size_t len);

/*!
 * \brief Mark outgiong data as acknowledged after ACK received.
 *
 * \param xconn          QUIC connection that received ACK.
 * \param stream_id      Stream ID of ACKed data.
 * \param end_acked      Offset of ACKed data + ACKed length.
 * \param keep_stream    Don't free the stream even when ACKed all outgoing data.
 */
void knot_xquic_stream_ack_data(knot_xquic_conn_t *xconn, int64_t stream_id,
                                size_t end_acked, bool keep_stream);

/*!
 * \brief Mark outgoing data as sent.
 *
 * \param xconn          QUIC connection that sent data.
 * \param stream_id      Stream ID of sent data.
 * \param amount_sent    Length of sent data.
 */
void knot_xquic_stream_mark_sent(knot_xquic_conn_t *xconn, int64_t stream_id,
                                 size_t amount_sent);

/*!
 * \brief Free rest of resources of closed conns.
 *
 * \param conns      Array with recently used conns (possibly NULLs).
 * \param n_conns    Size of the array.
 */
void knot_xquic_cleanup(knot_xquic_conn_t *conns[], size_t n_conns);

/*!
 * \brief Toggle sending Retry packet as a reaction to Initial packet of new connection.
 *
 * \param table       Connection table.
 *
 * \return True if instead of continuing handshake, Retry packet shall be sent
 *              to verify counterpart's address.
 */
bool xquic_require_retry(knot_xquic_table_t *table);

/*! @} */