Line data Source code
1 : #ifndef HEADER_fd_src_waltz_quic_fd_quic_private_h
2 : #define HEADER_fd_src_waltz_quic_fd_quic_private_h
3 :
4 : #include "fd_quic.h"
5 : #include "templ/fd_quic_transport_params.h"
6 : #include "fd_quic_conn_map.h"
7 : #include "fd_quic_stream.h"
8 : #include "log/fd_quic_log_tx.h"
9 : #include "fd_quic_pkt_meta.h"
10 : #include "tls/fd_quic_tls.h"
11 : #include "fd_quic_stream_pool.h"
12 : #include "fd_quic_pretty_print.h"
13 : #include <math.h>
14 :
15 : #include "../../util/log/fd_dtrace.h"
16 : #include "../../util/net/fd_ip4.h"
17 : #include "../../util/net/fd_udp.h"
18 :
19 : /* Handshake allocator pool */
20 : #define POOL_NAME fd_quic_tls_hs_pool
21 10080 : #define POOL_T fd_quic_tls_hs_t
22 : #include "../../util/tmpl/fd_pool.c"
23 :
24 : /* Handshake FIFO cache dlist */
25 : #define DLIST_NAME fd_quic_tls_hs_cache
26 : #define DLIST_ELE_T fd_quic_tls_hs_t
27 : #include "../../util/tmpl/fd_dlist.c"
28 :
29 :
30 : /* FD_QUIC_DISABLE_CRYPTO: set to 1 to disable packet protection and
31 : encryption. Only intended for testing. */
32 : #ifndef FD_QUIC_DISABLE_CRYPTO
33 : #define FD_QUIC_DISABLE_CRYPTO 0
34 : #endif
35 :
36 105921103 : #define FD_QUIC_PKT_NUM_UNUSED (~0ul)
37 39884178 : #define FD_QUIC_PKT_NUM_PENDING (~1ul)
38 :
39 : /* FD_QUIC_MAGIC is used to signal the layout of shared memory region
40 : of an fd_quic_t. */
41 :
42 2139 : #define FD_QUIC_MAGIC (0xdadf8cfa01cc5460UL)
43 :
44 : /* FD_QUIC_SVC_{...} specify connection timer types. */
45 :
46 119186762 : #define FD_QUIC_SVC_INSTANT (0U) /* as soon as possible */
47 197658949 : #define FD_QUIC_SVC_ACK_TX (1U) /* within local max_ack_delay (ACK TX coalesce) */
48 146110195 : #define FD_QUIC_SVC_WAIT (2U) /* within min(idle_timeout, peer max_ack_delay) */
49 146214067 : #define FD_QUIC_SVC_CNT (3U) /* number of FD_QUIC_SVC_{...} levels */
50 :
51 : /* fd_quic_svc_queue_t is a simple doubly linked list. */
52 :
53 : struct fd_quic_svc_queue {
54 : /* FIXME track count */ // uint cnt;
55 : uint head;
56 : uint tail;
57 : };
58 :
59 : typedef struct fd_quic_svc_queue fd_quic_svc_queue_t;
60 :
61 :
62 : /* fd_quic_state_t is the internal state of an fd_quic_t. Valid for
63 : lifetime of join. */
64 :
65 : struct __attribute__((aligned(16UL))) fd_quic_state_private {
66 : /* Flags */
67 : ulong flags;
68 :
69 : ulong now; /* the time we entered into fd_quic_service, or fd_quic_aio_cb_receive */
70 :
71 : /* transport_params: Template for QUIC-TLS transport params extension.
72 : Contains a mix of mutable and immutable fields. Immutable fields
73 : are set on join. Mutable fields may be modified during packet
74 : processing. Any code using this struct must ensure that the
75 : mutable fields are cleared before using (otherwise would leak a
76 : side channel).
77 :
78 : Mutable fields include:
79 : - original_destination_connection_id
80 : - initial_source_conn_id */
81 :
82 : fd_quic_transport_params_t transport_params;
83 :
84 : ulong max_inflight_frame_cnt_conn; /* per-conn max, computed from limits */
85 :
86 : /* Various internal state */
87 :
88 : fd_quic_log_tx_t log_tx[1];
89 : uint free_conn_list; /* free list of unused connections */
90 : fd_quic_conn_map_t * conn_map; /* map connection ids -> connection */
91 :
92 : fd_quic_tls_t tls[1];
93 : fd_quic_tls_hs_t * hs_pool;
94 : fd_quic_tls_hs_cache_t hs_cache; /* dlist <> dlist_private */
95 :
96 : fd_quic_stream_pool_t * stream_pool; /* stream pool, nullable */
97 : fd_quic_pkt_meta_t * pkt_meta_pool;
98 : fd_rng_t _rng[1]; /* random number generator */
99 : fd_quic_svc_queue_t svc_queue[ FD_QUIC_SVC_CNT ]; /* dlists */
100 : ulong svc_delay[ FD_QUIC_SVC_CNT ]; /* target service delay */
101 :
102 : /* need to be able to access connections by index */
103 : ulong conn_base; /* address of array of all connections */
104 : /* not using fd_quic_conn_t* to avoid confusion */
105 : /* use fd_quic_conn_at_idx instead */
106 : ulong conn_sz; /* size of one connection element */
107 :
108 : /* flow control - configured initial limits */
109 : ulong initial_max_data; /* directly from transport params */
110 : ulong initial_max_stream_data[4]; /* from 4 transport params indexed by stream type */
111 :
112 : /* last arp/routing tables update */
113 : ulong ip_table_upd;
114 :
115 : /* secret for generating RETRY tokens */
116 : uchar retry_secret[FD_QUIC_RETRY_SECRET_SZ];
117 : uchar retry_iv [FD_QUIC_RETRY_IV_SZ];
118 :
119 : /* Scratch space for packet protection */
120 : uchar crypt_scratch[FD_QUIC_MTU];
121 : };
122 :
123 : /* FD_QUIC_STATE_OFF is the offset of fd_quic_state_t within fd_quic_t. */
124 782048768 : #define FD_QUIC_STATE_OFF (fd_ulong_align_up( sizeof(fd_quic_t), alignof(fd_quic_state_t) ))
125 :
126 : struct fd_quic_pkt {
127 : fd_ip4_hdr_t ip4[1];
128 : fd_udp_hdr_t udp[1];
129 :
130 : /* the following are the "current" values only. There may be more QUIC packets
131 : in a UDP datagram */
132 : ulong pkt_number; /* quic packet number currently being decoded/parsed */
133 : ulong rcv_time; /* time packet was received */
134 : uint enc_level; /* encryption level */
135 : uint datagram_sz; /* length of the original datagram */
136 : uint ack_flag; /* ORed together: 0-don't ack 1-ack 2-cancel ack */
137 277039488 : # define ACK_FLAG_RQD 1
138 184393392 : # define ACK_FLAG_CANCEL 2
139 :
140 : ulong rtt_pkt_number; /* packet number used for rtt */
141 : ulong rtt_ack_time;
142 : ulong rtt_ack_delay;
143 : };
144 :
145 : struct fd_quic_frame_ctx {
146 : fd_quic_t * quic;
147 : fd_quic_conn_t * conn;
148 : fd_quic_pkt_t * pkt;
149 : };
150 :
151 : typedef struct fd_quic_frame_ctx fd_quic_frame_ctx_t;
152 :
153 : FD_PROTOTYPES_BEGIN
154 :
155 : /* fd_quic_get_state returns a pointer to private state area given a
156 : pointer to fd_quic_t. Const func, guaranteed to not access memory. */
157 :
158 : FD_FN_CONST static inline fd_quic_state_t *
159 782048768 : fd_quic_get_state( fd_quic_t * quic ) {
160 782048768 : return (fd_quic_state_t *)( (ulong)quic + FD_QUIC_STATE_OFF );
161 782048768 : }
162 :
163 : FD_FN_CONST static inline fd_quic_state_t const *
164 0 : fd_quic_get_state_const( fd_quic_t const * quic ) {
165 0 : return (fd_quic_state_t const *)( (ulong)quic + FD_QUIC_STATE_OFF );
166 0 : }
167 :
168 : static inline fd_quic_conn_map_t *
169 : fd_quic_conn_query1( fd_quic_conn_map_t * map,
170 : ulong conn_id,
171 314358 : fd_quic_conn_map_t * sentinel ) {
172 314358 : if( !conn_id ) return sentinel;
173 12063 : return fd_quic_conn_map_query( map, conn_id, sentinel );
174 314358 : }
175 :
176 : static inline fd_quic_conn_t *
177 : fd_quic_conn_query( fd_quic_conn_map_t * map,
178 13585609 : ulong conn_id ) {
179 13585609 : fd_quic_conn_map_t sentinel = {0};
180 13585609 : if( !conn_id ) return NULL;
181 13584265 : fd_quic_conn_map_t * entry = fd_quic_conn_map_query( map, conn_id, &sentinel );
182 13584265 : return entry->conn;
183 13585609 : }
184 :
185 : /* fd_quic_conn_service is called periodically to perform pending
186 : operations and time based operations.
187 :
188 : args
189 : quic managing quic
190 : conn connection to service
191 : now the current timestamp */
192 : void
193 : fd_quic_conn_service( fd_quic_t * quic,
194 : fd_quic_conn_t * conn,
195 : ulong now );
196 :
197 : /* fd_quic_svc_schedule installs a connection timer. svc_type is in
198 : [0,FD_QUIC_SVC_CNT) and specifies the timer delay. Lower timers
199 : override higher ones. */
200 :
201 : void
202 : fd_quic_svc_schedule( fd_quic_state_t * state,
203 : fd_quic_conn_t * conn,
204 : uint svc_type );
205 :
206 : static inline void
207 : fd_quic_svc_schedule1( fd_quic_conn_t * conn,
208 26417162 : uint svc_type ) {
209 26417162 : fd_quic_svc_schedule( fd_quic_get_state( conn->quic ), conn, svc_type );
210 26417162 : }
211 :
212 : /* Memory management **************************************************/
213 :
214 : fd_quic_conn_t *
215 : fd_quic_conn_create( fd_quic_t * quic,
216 : ulong our_conn_id,
217 : fd_quic_conn_id_t const * peer_conn_id,
218 : uint peer_ip_addr,
219 : ushort peer_udp_port,
220 : uint self_ip_addr,
221 : ushort self_udp_port,
222 : int server );
223 :
224 : /* fd_quic_conn_free frees up most resources related to the connection
225 : and returns it to the connection free list. The dead conn remains in
226 : the conn_id_map to catch inflight packets by the peer. */
227 : void
228 : fd_quic_conn_free( fd_quic_t * quic,
229 : fd_quic_conn_t * conn );
230 :
231 : void
232 : fd_quic_tx_stream_free( fd_quic_t * quic,
233 : fd_quic_conn_t * conn,
234 : fd_quic_stream_t * stream,
235 : int code );
236 :
237 : /* Callbacks provided by fd_quic **************************************/
238 :
239 : /* used by quic to receive data from network */
240 : int
241 : fd_quic_aio_cb_receive( void * context,
242 : fd_aio_pkt_info_t const * batch,
243 : ulong batch_sz,
244 : ulong * opt_batch_idx,
245 : int flush );
246 :
247 : /* declare callbacks from quic-tls into quic */
248 : int
249 : fd_quic_tls_cb_client_hello( fd_quic_tls_hs_t * hs,
250 : void * context );
251 :
252 : int
253 : fd_quic_tls_cb_handshake_data( fd_quic_tls_hs_t * hs,
254 : void * context,
255 : uint enc_level,
256 : uchar const * data,
257 : ulong data_sz );
258 :
259 : void
260 : fd_quic_tls_cb_alert( fd_quic_tls_hs_t * hs,
261 : void * context,
262 : int alert );
263 :
264 : void
265 : fd_quic_tls_cb_secret( fd_quic_tls_hs_t * hs,
266 : void * context,
267 : fd_quic_tls_secret_t const * secret );
268 :
269 : void
270 : fd_quic_tls_cb_handshake_complete( fd_quic_tls_hs_t * hs,
271 : void * context );
272 :
273 : void
274 : fd_quic_tls_cb_peer_params( void * context,
275 : uchar const * peer_tp_enc,
276 : ulong peer_tp_enc_sz );
277 :
278 : void
279 : fd_quic_apply_peer_params( fd_quic_conn_t * conn,
280 : fd_quic_transport_params_t const * peer_tp );
281 :
282 : /* Helpers for calling callbacks **************************************/
283 :
284 : static inline ulong
285 119020652 : fd_quic_now( fd_quic_t * quic ) {
286 119020652 : return quic->cb.now( quic->cb.now_ctx );
287 119020652 : }
288 :
289 : static inline void
290 : fd_quic_cb_conn_new( fd_quic_t * quic,
291 6021 : fd_quic_conn_t * conn ) {
292 6021 : if( conn->called_conn_new ) return;
293 6021 : conn->called_conn_new = 1;
294 6021 : if( !quic->cb.conn_new ) return;
295 :
296 6021 : quic->cb.conn_new( conn, quic->cb.quic_ctx );
297 6021 : }
298 :
299 : static inline void
300 : fd_quic_cb_conn_hs_complete( fd_quic_t * quic,
301 6021 : fd_quic_conn_t * conn ) {
302 6021 : if( !quic->cb.conn_hs_complete ) return;
303 6021 : quic->cb.conn_hs_complete( conn, quic->cb.quic_ctx );
304 6021 : }
305 :
306 : static inline void
307 : fd_quic_cb_conn_final( fd_quic_t * quic,
308 14202 : fd_quic_conn_t * conn ) {
309 14202 : if( !quic->cb.conn_final || !conn->called_conn_new ) return;
310 12030 : quic->cb.conn_final( conn, quic->cb.quic_ctx );
311 12030 : }
312 :
313 : static inline int
314 : fd_quic_cb_stream_rx( fd_quic_t * quic,
315 : fd_quic_conn_t * conn,
316 : ulong stream_id,
317 : ulong offset,
318 : uchar const * data,
319 : ulong data_sz,
320 92058954 : int fin ) {
321 92058954 : quic->metrics.stream_rx_event_cnt++;
322 92058954 : quic->metrics.stream_rx_byte_cnt += data_sz;
323 :
324 92058954 : if( !quic->cb.stream_rx ) return FD_QUIC_SUCCESS;
325 13294303 : return quic->cb.stream_rx( conn, stream_id, offset, data, data_sz, fin );
326 92058954 : }
327 :
328 : static inline void
329 : fd_quic_cb_stream_notify( fd_quic_t * quic,
330 : fd_quic_stream_t * stream,
331 : void * stream_ctx,
332 13283449 : int event ) {
333 13283449 : quic->metrics.stream_closed_cnt[ event ]++;
334 13283449 : quic->metrics.stream_active_cnt--;
335 :
336 13283449 : if( !quic->cb.stream_notify ) return;
337 13283449 : quic->cb.stream_notify( stream, stream_ctx, event );
338 13283449 : }
339 :
340 :
341 : FD_FN_CONST ulong
342 : fd_quic_reconstruct_pkt_num( ulong pktnum_comp,
343 : ulong pktnum_sz,
344 : ulong exp_pkt_number );
345 :
346 : void
347 : fd_quic_pkt_meta_retry( fd_quic_t * quic,
348 : fd_quic_conn_t * conn,
349 : int force,
350 : uint arg_enc_level );
351 :
352 : /* reclaim resources associated with packet metadata
353 : this is called in response to received acks */
354 : void
355 : fd_quic_reclaim_pkt_meta( fd_quic_conn_t * conn,
356 : fd_quic_pkt_meta_t * pkt_meta,
357 : uint enc_level );
358 :
359 : ulong
360 : fd_quic_process_quic_packet_v1( fd_quic_t * quic,
361 : fd_quic_pkt_t * pkt,
362 : uchar * cur_ptr,
363 : ulong cur_sz );
364 :
365 : ulong
366 : fd_quic_handle_v1_initial( fd_quic_t * quic,
367 : fd_quic_conn_t ** p_conn,
368 : fd_quic_pkt_t * pkt,
369 : fd_quic_conn_id_t const * dcid,
370 : fd_quic_conn_id_t const * scid,
371 : uchar * cur_ptr,
372 : ulong cur_sz );
373 : ulong
374 : fd_quic_handle_v1_handshake(
375 : fd_quic_t * quic,
376 : fd_quic_conn_t * conn,
377 : fd_quic_pkt_t * pkt,
378 : uchar * cur_ptr,
379 : ulong cur_sz
380 : );
381 :
382 : ulong
383 : fd_quic_handle_v1_one_rtt( fd_quic_t * quic,
384 : fd_quic_conn_t * conn,
385 : fd_quic_pkt_t * pkt,
386 : uchar * cur_ptr,
387 : ulong cur_sz );
388 :
389 : /* fd_quic_handle_v1_frame is the primary entrypoint for handling of
390 : incoming QUIC frames. {quic,conn,pkt} identify the frame context.
391 : Memory region [frame_ptr,frame_ptr+frame_sz) contains the serialized
392 : QUIC frame (may contain arbitrary zero padding at the beginning).
393 :
394 : Returns value in (0,buf_sz) if the frame was successfully processed.
395 : Returns FD_QUIC_PARSE_FAIL if the frame was inherently malformed.
396 : Returns 0 or value in [buf_sz,ULONG_MAX) in case of a protocol
397 : violation. */
398 :
399 : ulong
400 : fd_quic_handle_v1_frame( fd_quic_t * quic,
401 : fd_quic_conn_t * conn,
402 : fd_quic_pkt_t * pkt,
403 : uint pkt_type,
404 : uchar const * frame_ptr,
405 : ulong frame_sz );
406 :
407 : /* fd_quic_lazy_ack_pkt enqueues future acknowledgement for the given
408 : packet. The ACK will be sent out at a fd_quic_service call. The
409 : delay is determined by the fd_quic_config_t ack_threshold and
410 : ack_delay settings. Respects pkt->ack_flag (ACK_FLAG_RQD schedules
411 : an ACK instantly, ACK_FLAG_CANCEL suppresses the ACK by making this
412 : function behave like a no-op) */
413 :
414 : int
415 : fd_quic_lazy_ack_pkt( fd_quic_t * quic,
416 : fd_quic_conn_t * conn,
417 : fd_quic_pkt_t const * pkt );
418 :
419 : static inline fd_quic_conn_t *
420 248238102 : fd_quic_conn_at_idx( fd_quic_state_t * quic_state, ulong idx ) {
421 248238102 : ulong addr = quic_state->conn_base;
422 248238102 : ulong sz = quic_state->conn_sz;
423 248238102 : return (fd_quic_conn_t*)( addr + idx * sz );
424 248238102 : }
425 :
426 : /* called with round-trip-time (rtt) and the ack delay (from the spec)
427 : to sample the round trip times. */
428 : static inline void
429 243810 : fd_quic_sample_rtt( fd_quic_conn_t * conn, long rtt_ticks, long ack_delay ) {
430 : /* for convenience */
431 243810 : fd_rtt_estimate_t * rtt = conn->rtt;
432 :
433 : /* ack_delay is in peer units, so scale to put in ticks */
434 243810 : float ack_delay_ticks = (float)ack_delay * conn->peer_ack_delay_scale;
435 :
436 : /* bound ack_delay by peer_max_ack_delay */
437 243810 : ack_delay_ticks = fminf( ack_delay_ticks, conn->peer_max_ack_delay_ticks );
438 :
439 243810 : fd_rtt_sample( rtt, (float)rtt_ticks, ack_delay_ticks );
440 :
441 243810 : FD_DEBUG({
442 243810 : double us_per_tick = 1.0 / (double)conn->quic->config.tick_per_us;
443 243810 : FD_LOG_NOTICE(( "conn_idx: %u min_rtt: %f smoothed_rtt: %f var_rtt: %f adj_rtt: %f rtt_ticks: %f ack_delay_ticks: %f diff: %f",
444 243810 : (uint)conn->conn_idx,
445 243810 : us_per_tick * (double)rtt->min_rtt,
446 243810 : us_per_tick * (double)rtt->smoothed_rtt,
447 243810 : us_per_tick * (double)rtt->var_rtt,
448 243810 : us_per_tick * (double)adj_rtt,
449 243810 : us_per_tick * (double)rtt_ticks,
450 243810 : us_per_tick * (double)ack_delay_ticks,
451 243810 : us_per_tick * ( (double)rtt_ticks - (double)ack_delay_ticks ) ));
452 243810 : })
453 243810 : }
454 :
455 : /* fd_quic_calc_expiry returns the timestamp of the next expiry event. */
456 :
457 : static inline ulong
458 0 : fd_quic_calc_expiry( fd_quic_conn_t * conn, ulong now ) {
459 0 : /* Instead of a full implementation of PTO, we're setting an expiry
460 0 : time per sent QUIC packet
461 0 : This calculates the expiry time according to the PTO spec
462 0 : 6.2.1. Computing PTO
463 0 : When an ack-eliciting packet is transmitted, the sender schedules
464 0 : a timer for the PTO period as follows:
465 0 : PTO = smoothed_rtt + max(4*rttvar, kGranularity) + max_ack_delay */
466 0 :
467 0 : fd_rtt_estimate_t * rtt = conn->rtt;
468 0 :
469 0 : ulong duration = (ulong)
470 0 : ( rtt->smoothed_rtt
471 0 : + (4.0f * rtt->var_rtt)
472 0 : + conn->peer_max_ack_delay_ticks );
473 0 :
474 0 : FD_DTRACE_PROBE_2( quic_calc_expiry, conn->our_conn_id, duration );
475 0 :
476 0 : return now + (ulong)500e6; /* 500ms */
477 0 : }
478 :
479 : uchar *
480 : fd_quic_gen_stream_frames( fd_quic_conn_t * conn,
481 : uchar * payload_ptr,
482 : uchar * payload_end,
483 : fd_quic_pkt_meta_t * pkt_meta_tmpl,
484 : fd_quic_pkt_meta_tracker_t * tracker );
485 :
486 : void
487 : fd_quic_process_ack_range( fd_quic_conn_t * conn,
488 : fd_quic_frame_ctx_t * context,
489 : uint enc_level,
490 : ulong largest_ack,
491 : ulong ack_range,
492 : int is_largest,
493 : ulong now,
494 : ulong ack_delay );
495 :
496 : FD_PROTOTYPES_END
497 :
498 : #endif /* HEADER_fd_src_waltz_quic_fd_quic_private_h */
|