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 "fd_quic_svc_q.h"
14 : #include <math.h>
15 :
16 : #include "../../util/log/fd_dtrace.h"
17 : #include "../../util/net/fd_ip4.h"
18 : #include "../../util/net/fd_udp.h"
19 :
20 : /* Handshake allocator pool */
21 : #define POOL_NAME fd_quic_tls_hs_pool
22 10152 : #define POOL_T fd_quic_tls_hs_t
23 : #include "../../util/tmpl/fd_pool.c"
24 :
25 : /* Handshake FIFO cache dlist */
26 : #define DLIST_NAME fd_quic_tls_hs_cache
27 : #define DLIST_ELE_T fd_quic_tls_hs_t
28 : #include "../../util/tmpl/fd_dlist.c"
29 :
30 :
31 : /* FD_QUIC_DISABLE_CRYPTO: set to 1 to disable packet protection and
32 : encryption. Only intended for testing. */
33 : #ifndef FD_QUIC_DISABLE_CRYPTO
34 : #define FD_QUIC_DISABLE_CRYPTO 0
35 : #endif
36 :
37 92697407 : #define FD_QUIC_PKT_NUM_UNUSED (~0ul)
38 49904061 : #define FD_QUIC_PKT_NUM_PENDING (~1ul)
39 :
40 : /* FD_QUIC_MAGIC is used to signal the layout of shared memory region
41 : of an fd_quic_t. */
42 :
43 2145 : #define FD_QUIC_MAGIC (0xdadf8cfa01cc5460UL)
44 :
45 : /* fd_quic_state_t is the internal state of an fd_quic_t. Valid for
46 : lifetime of join. */
47 :
48 : struct __attribute__((aligned(16UL))) fd_quic_state_private {
49 : /* Flags */
50 : ulong flags;
51 :
52 : long now; /* recent timestamp, assumed in ns */
53 :
54 : /* transport_params: Template for QUIC-TLS transport params extension.
55 : Contains a mix of mutable and immutable fields. Immutable fields
56 : are set on join. Mutable fields may be modified during packet
57 : processing. Any code using this struct must ensure that the
58 : mutable fields are cleared before using (otherwise would leak a
59 : side channel).
60 :
61 : Mutable fields include:
62 : - original_destination_connection_id
63 : - initial_source_conn_id */
64 :
65 : fd_quic_transport_params_t transport_params;
66 :
67 : ulong max_inflight_frame_cnt_conn; /* per-conn max, computed from limits */
68 :
69 : /* Various internal state */
70 :
71 : fd_quic_log_tx_t log_tx[1];
72 : uint free_conn_list; /* free list of unused connections */
73 : fd_quic_conn_map_t * conn_map; /* map connection ids -> connection */
74 :
75 : fd_quic_tls_t tls[1];
76 : fd_quic_tls_hs_t * hs_pool;
77 : fd_quic_tls_hs_cache_t hs_cache; /* dlist <> dlist_private */
78 :
79 : fd_quic_stream_pool_t * stream_pool; /* stream pool, nullable */
80 : fd_quic_pkt_meta_t * pkt_meta_pool;
81 : fd_rng_t _rng[1]; /* random number generator */
82 :
83 : /* need to be able to access connections by index */
84 : ulong conn_base; /* address of array of all connections */
85 : /* not using fd_quic_conn_t* to avoid confusion */
86 : /* use fd_quic_conn_at_idx instead */
87 : ulong conn_sz; /* size of one connection element */
88 :
89 : /* flow control - configured initial limits */
90 : ulong initial_max_data; /* directly from transport params */
91 : ulong initial_max_stream_data[4]; /* from 4 transport params indexed by stream type */
92 :
93 : /* last arp/routing tables update */
94 : ulong ip_table_upd;
95 :
96 : /* secret for generating RETRY tokens */
97 : uchar retry_secret[FD_QUIC_RETRY_SECRET_SZ];
98 : uchar retry_iv [FD_QUIC_RETRY_IV_SZ];
99 :
100 : /* Scratch space for packet protection */
101 : uchar crypt_scratch[FD_QUIC_MTU];
102 :
103 : /* the timer structs, large private fields / data follow */
104 : fd_quic_svc_timers_t * svc_timers;
105 : };
106 :
107 : /* FD_QUIC_STATE_OFF is the offset of fd_quic_state_t within fd_quic_t. */
108 514061252 : #define FD_QUIC_STATE_OFF (fd_ulong_align_up( sizeof(fd_quic_t), alignof(fd_quic_state_t) ))
109 :
110 : struct fd_quic_pkt {
111 : fd_ip4_hdr_t ip4[1];
112 : fd_udp_hdr_t udp[1];
113 :
114 : /* the following are the "current" values only. There may be more QUIC packets
115 : in a UDP datagram */
116 : ulong pkt_number; /* quic packet number currently being decoded/parsed */
117 : long rcv_time; /* time packet was received */
118 : uint enc_level; /* encryption level */
119 : uint datagram_sz; /* length of the original datagram */
120 : uint ack_flag; /* ORed together: 0-don't ack 1-ack 2-cancel ack */
121 227238507 : # define ACK_FLAG_RQD 1
122 151157087 : # define ACK_FLAG_CANCEL 2
123 :
124 : ulong rtt_pkt_number; /* packet number used for rtt */
125 : long rtt_ack_time;
126 : ulong rtt_ack_delay;
127 : };
128 :
129 : struct fd_quic_frame_ctx {
130 : fd_quic_t * quic;
131 : fd_quic_conn_t * conn;
132 : fd_quic_pkt_t * pkt;
133 : };
134 :
135 : typedef struct fd_quic_frame_ctx fd_quic_frame_ctx_t;
136 :
137 : FD_PROTOTYPES_BEGIN
138 :
139 : /* fd_quic_get_state returns a pointer to private state area given a
140 : pointer to fd_quic_t. Const func, guaranteed to not access memory. */
141 :
142 : FD_FN_CONST static inline fd_quic_state_t *
143 514061252 : fd_quic_get_state( fd_quic_t * quic ) {
144 514061252 : return (fd_quic_state_t *)( (ulong)quic + FD_QUIC_STATE_OFF );
145 514061252 : }
146 :
147 : FD_FN_CONST static inline fd_quic_state_t const *
148 0 : fd_quic_get_state_const( fd_quic_t const * quic ) {
149 0 : return (fd_quic_state_t const *)( (ulong)quic + FD_QUIC_STATE_OFF );
150 0 : }
151 :
152 : /* fd_quic_conn_service is called periodically to perform pending
153 : operations and time based operations.
154 :
155 : args
156 : quic managing quic
157 : conn connection to service
158 : now the current timestamp */
159 : void
160 : fd_quic_conn_service( fd_quic_t * quic,
161 : fd_quic_conn_t * conn,
162 : long now );
163 :
164 :
165 : /* Memory management **************************************************/
166 :
167 : fd_quic_conn_t *
168 : fd_quic_conn_create( fd_quic_t * quic,
169 : ulong our_conn_id,
170 : fd_quic_conn_id_t const * peer_conn_id,
171 : uint peer_ip_addr,
172 : ushort peer_udp_port,
173 : uint self_ip_addr,
174 : ushort self_udp_port,
175 : int server );
176 :
177 : /* fd_quic_conn_free frees up resources related to the connection and
178 : returns it to the connection free list. */
179 : void
180 : fd_quic_conn_free( fd_quic_t * quic,
181 : fd_quic_conn_t * conn );
182 :
183 : void
184 : fd_quic_tx_stream_free( fd_quic_t * quic,
185 : fd_quic_conn_t * conn,
186 : fd_quic_stream_t * stream,
187 : int code );
188 :
189 : /* Callbacks provided by fd_quic **************************************/
190 :
191 : /* used by quic to receive data from network */
192 : int
193 : fd_quic_aio_cb_receive( void * context,
194 : fd_aio_pkt_info_t const * batch,
195 : ulong batch_sz,
196 : ulong * opt_batch_idx,
197 : int flush );
198 :
199 : /* declare callbacks from quic-tls into quic */
200 : int
201 : fd_quic_tls_cb_client_hello( fd_quic_tls_hs_t * hs,
202 : void * context );
203 :
204 : int
205 : fd_quic_tls_cb_handshake_data( fd_quic_tls_hs_t * hs,
206 : void * context,
207 : uint enc_level,
208 : uchar const * data,
209 : ulong data_sz );
210 :
211 : void
212 : fd_quic_tls_cb_alert( fd_quic_tls_hs_t * hs,
213 : void * context,
214 : int alert );
215 :
216 : void
217 : fd_quic_tls_cb_secret( fd_quic_tls_hs_t * hs,
218 : void * context,
219 : fd_quic_tls_secret_t const * secret );
220 :
221 : void
222 : fd_quic_tls_cb_handshake_complete( fd_quic_tls_hs_t * hs,
223 : void * context );
224 :
225 : void
226 : fd_quic_tls_cb_peer_params( void * context,
227 : uchar const * peer_tp_enc,
228 : ulong peer_tp_enc_sz );
229 :
230 : void
231 : fd_quic_apply_peer_params( fd_quic_conn_t * conn,
232 : fd_quic_transport_params_t const * peer_tp );
233 :
234 : /* Helpers for calling callbacks **************************************/
235 :
236 : static inline void
237 : fd_quic_cb_conn_new( fd_quic_t * quic,
238 6069 : fd_quic_conn_t * conn ) {
239 6069 : if( conn->called_conn_new ) return;
240 6069 : conn->called_conn_new = 1;
241 6069 : if( !quic->cb.conn_new ) return;
242 :
243 6069 : quic->cb.conn_new( conn, quic->cb.quic_ctx );
244 6069 : }
245 :
246 : static inline void
247 : fd_quic_cb_conn_hs_complete( fd_quic_t * quic,
248 6069 : fd_quic_conn_t * conn ) {
249 6069 : if( !quic->cb.conn_hs_complete ) return;
250 6069 : quic->cb.conn_hs_complete( conn, quic->cb.quic_ctx );
251 6069 : }
252 :
253 : static inline void
254 : fd_quic_cb_conn_final( fd_quic_t * quic,
255 14214 : fd_quic_conn_t * conn ) {
256 14214 : if( !quic->cb.conn_final || !conn->called_conn_new ) return;
257 12042 : quic->cb.conn_final( conn, quic->cb.quic_ctx );
258 12042 : }
259 :
260 : static inline int
261 : fd_quic_cb_stream_rx( fd_quic_t * quic,
262 : fd_quic_conn_t * conn,
263 : ulong stream_id,
264 : ulong offset,
265 : uchar const * data,
266 : ulong data_sz,
267 75423141 : int fin ) {
268 75423141 : quic->metrics.stream_rx_event_cnt++;
269 75423141 : quic->metrics.stream_rx_byte_cnt += data_sz;
270 :
271 75423141 : if( !quic->cb.stream_rx ) return FD_QUIC_SUCCESS;
272 16635778 : return quic->cb.stream_rx( conn, stream_id, offset, data, data_sz, fin );
273 75423141 : }
274 :
275 : static inline void
276 : fd_quic_cb_stream_notify( fd_quic_t * quic,
277 : fd_quic_stream_t * stream,
278 : void * stream_ctx,
279 16624903 : int event ) {
280 16624903 : quic->metrics.stream_closed_cnt[ event ]++;
281 16624903 : quic->metrics.stream_active_cnt--;
282 :
283 16624903 : if( !quic->cb.stream_notify ) return;
284 16624903 : quic->cb.stream_notify( stream, stream_ctx, event );
285 16624903 : }
286 :
287 :
288 : FD_FN_CONST ulong
289 : fd_quic_reconstruct_pkt_num( ulong pktnum_comp,
290 : ulong pktnum_sz,
291 : ulong exp_pkt_number );
292 :
293 : void
294 : fd_quic_pkt_meta_retry( fd_quic_t * quic,
295 : fd_quic_conn_t * conn,
296 : ulong force_below_pkt_num,
297 : uint arg_enc_level );
298 :
299 : /* reclaim resources associated with packet metadata
300 : this is called in response to received acks */
301 : void
302 : fd_quic_reclaim_pkt_meta( fd_quic_conn_t * conn,
303 : fd_quic_pkt_meta_t * pkt_meta,
304 : uint enc_level );
305 :
306 : ulong
307 : fd_quic_process_quic_packet_v1( fd_quic_t * quic,
308 : fd_quic_pkt_t * pkt,
309 : uchar * cur_ptr,
310 : ulong cur_sz );
311 :
312 : ulong
313 : fd_quic_handle_v1_initial( fd_quic_t * quic,
314 : fd_quic_conn_t ** p_conn,
315 : fd_quic_pkt_t * pkt,
316 : fd_quic_conn_id_t const * dcid,
317 : fd_quic_conn_id_t const * scid,
318 : uchar * cur_ptr,
319 : ulong cur_sz );
320 :
321 : ulong
322 : fd_quic_handle_v1_handshake( fd_quic_t * quic,
323 : fd_quic_conn_t * conn,
324 : fd_quic_pkt_t * pkt,
325 : uchar * cur_ptr,
326 : ulong cur_sz );
327 :
328 : ulong
329 : fd_quic_handle_v1_one_rtt( fd_quic_t * quic,
330 : fd_quic_conn_t * conn,
331 : fd_quic_pkt_t * pkt,
332 : uchar * cur_ptr,
333 : ulong cur_sz );
334 :
335 : /* fd_quic_handle_v1_frame is the primary entrypoint for handling of
336 : incoming QUIC frames. {quic,conn,pkt} identify the frame context.
337 : Memory region [frame_ptr,frame_ptr+frame_sz) contains the serialized
338 : QUIC frame (may contain arbitrary zero padding at the beginning).
339 :
340 : Returns value in (0,buf_sz) if the frame was successfully processed.
341 : Returns FD_QUIC_PARSE_FAIL if the frame was inherently malformed.
342 : Returns 0 or value in [buf_sz,ULONG_MAX) in case of a protocol
343 : violation. */
344 :
345 : ulong
346 : fd_quic_handle_v1_frame( fd_quic_t * quic,
347 : fd_quic_conn_t * conn,
348 : fd_quic_pkt_t * pkt,
349 : uint pkt_type,
350 : uchar const * frame_ptr,
351 : ulong frame_sz );
352 :
353 : /* fd_quic_lazy_ack_pkt enqueues future acknowledgement for the given
354 : packet. The ACK will be sent out at a fd_quic_service call. The
355 : delay is determined by the fd_quic_config_t ack_threshold and
356 : ack_delay settings. Respects pkt->ack_flag (ACK_FLAG_RQD schedules
357 : an ACK instantly, ACK_FLAG_CANCEL suppresses the ACK by making this
358 : function behave like a no-op) */
359 :
360 : int
361 : fd_quic_lazy_ack_pkt( fd_quic_t * quic,
362 : fd_quic_conn_t * conn,
363 : fd_quic_pkt_t const * pkt );
364 :
365 : static inline fd_quic_conn_t *
366 69235434 : fd_quic_conn_at_idx( fd_quic_state_t * quic_state, ulong idx ) {
367 69235434 : ulong addr = quic_state->conn_base;
368 69235434 : ulong sz = quic_state->conn_sz;
369 69235434 : return (fd_quic_conn_t*)( addr + idx * sz );
370 69235434 : }
371 :
372 : /* called with round-trip-time (rtt) and the ack delay (from the spec)
373 : to sample the round trip times. */
374 : static inline void
375 264 : fd_quic_sample_rtt( fd_quic_conn_t * conn, long rtt_ns, long ack_delay ) {
376 : /* for convenience */
377 264 : fd_rtt_estimate_t * rtt = conn->rtt;
378 :
379 : /* scale ack delay using peer exponent - rfc9000 19.3 */
380 264 : float ack_delay_ns = (float)ack_delay * conn->peer_ack_delay_scale;
381 :
382 : /* bound ack_delay by peer_max_ack_delay */
383 264 : ack_delay_ns = fminf( ack_delay_ns, conn->peer_max_ack_delay_ns );
384 :
385 264 : fd_rtt_sample( rtt, (float)rtt_ns, ack_delay_ns );
386 :
387 264 : FD_DEBUG({
388 264 : FD_LOG_NOTICE(( "conn_idx: %u min_rtt: %f smoothed_rtt: %f var_rtt: %f rtt_ns: %f ack_delay_ns: %f diff: %f",
389 264 : (uint)conn->conn_idx,
390 264 : (double)rtt->min_rtt,
391 264 : (double)rtt->smoothed_rtt,
392 264 : (double)rtt->var_rtt,
393 264 : (double)rtt_ns,
394 264 : (double)ack_delay_ns,
395 264 : ( (double)rtt_ns - (double)ack_delay_ns ) ));
396 264 : })
397 264 : }
398 :
399 : /* fd_quic_calc_expiry_duration returns the duration to the next expiry event.
400 : User should add the result to the base time to obtain the expiry timestamp.
401 : Uses the loss detection timeout if 'ack_driven', otherwise uses the PTO. */
402 :
403 : static inline long
404 51313957 : fd_quic_calc_expiry_duration( fd_quic_conn_t * conn, int ack_driven, int is_server ) {
405 : /* For server, we want to be conservative and minimize spam risk, so we stick
406 : with the hardcoded 500ms expiry. The following only applies to client.
407 :
408 : If this calculation is ack-driven, use the time threshold:
409 : 6.1.2 Time Threshold
410 : max(kTimeThreshold * max(smoothed_rtt, latest_rtt), kGranularity)
411 : The RECOMMENDED time threshold (kTimeThreshold), expressed as an RTT multiplier, is 9/8 */
412 51313957 : #define FD_QUIC_K_TIME_THRESHOLD 1.125f
413 : /* Otherwise, calculate the expiry time according to the PTO spec
414 : 6.2.1. Computing PTO
415 : When an ack-eliciting packet is transmitted, the sender schedules
416 : a timer for the PTO period as follows:
417 : PTO = smoothed_rtt + max(4*rttvar, kGranularity) + max_ack_delay
418 :
419 : Our granularity is O(ns), while recommended is 1ms --> We don't
420 : have to worry about kGranularity.
421 : */
422 :
423 51313957 : if( is_server ) return 500e6L;
424 :
425 50453941 : fd_rtt_estimate_t * rtt = conn->rtt;
426 :
427 50453941 : long pto_duration = (long)( rtt->smoothed_rtt +
428 50453941 : (4.0f * rtt->var_rtt) +
429 50453941 : conn->peer_max_ack_delay_ns );
430 :
431 50453941 : long loss_duration = (long)( FD_QUIC_K_TIME_THRESHOLD * fmaxf( rtt->smoothed_rtt, rtt->latest_rtt ) );
432 :
433 50453941 : long duration = fd_long_if( ack_driven, loss_duration, pto_duration );
434 :
435 50453941 : FD_DTRACE_PROBE_3( quic_calc_expiry, conn->our_conn_id, duration, ack_driven );
436 :
437 50453941 : return duration;
438 51313957 : }
439 :
440 : uchar *
441 : fd_quic_gen_stream_frames( fd_quic_conn_t * conn,
442 : uchar * payload_ptr,
443 : uchar * payload_end,
444 : fd_quic_pkt_meta_t * pkt_meta_tmpl,
445 : fd_quic_pkt_meta_tracker_t * tracker );
446 :
447 : void
448 : fd_quic_process_ack_range( fd_quic_conn_t * conn,
449 : fd_quic_frame_ctx_t * context,
450 : uint enc_level,
451 : ulong largest_ack,
452 : ulong ack_range,
453 : int is_largest,
454 : long now,
455 : ulong ack_delay );
456 :
457 : FD_PROTOTYPES_END
458 :
459 : #endif /* HEADER_fd_src_waltz_quic_fd_quic_private_h */
|