LCOV - code coverage report
Current view: top level - waltz/quic - fd_quic_private.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 69 72 95.8 %
Date: 2026-06-08 09:27:03 Functions: 28 270 10.4 %

          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         396 : #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    78440581 : #define FD_QUIC_PKT_NUM_UNUSED  (~0ul)
      38    39298701 : #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          57 : #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   421407095 : #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   195268458 : # define ACK_FLAG_RQD     1
     122   129898257 : # 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   421407095 : fd_quic_get_state( fd_quic_t * quic ) {
     144   421407095 :   return (fd_quic_state_t *)( (ulong)quic + FD_QUIC_STATE_OFF );
     145   421407095 : }
     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             : void
     178             : fd_quic_tx_stream_free( fd_quic_t *        quic,
     179             :                         fd_quic_conn_t *   conn,
     180             :                         fd_quic_stream_t * stream,
     181             :                         int                code );
     182             : 
     183             : /* Callbacks provided by fd_quic **************************************/
     184             : 
     185             : /* used by quic to receive data from network */
     186             : int
     187             : fd_quic_aio_cb_receive( void *                    context,
     188             :                         fd_aio_pkt_info_t const * batch,
     189             :                         ulong                     batch_sz,
     190             :                         ulong *                   opt_batch_idx,
     191             :                         int                       flush );
     192             : 
     193             : /* declare callbacks from quic-tls into quic */
     194             : int
     195             : fd_quic_tls_cb_client_hello( fd_quic_tls_hs_t * hs,
     196             :                              void *             context );
     197             : 
     198             : int
     199             : fd_quic_tls_cb_handshake_data( fd_quic_tls_hs_t * hs,
     200             :                                void *             context,
     201             :                                uint               enc_level,
     202             :                                uchar const *      data,
     203             :                                ulong              data_sz );
     204             : 
     205             : void
     206             : fd_quic_tls_cb_alert( fd_quic_tls_hs_t * hs,
     207             :                       void *             context,
     208             :                       int                alert );
     209             : 
     210             : void
     211             : fd_quic_tls_cb_secret( fd_quic_tls_hs_t *           hs,
     212             :                        void *                       context,
     213             :                        fd_quic_tls_secret_t const * secret );
     214             : 
     215             : void
     216             : fd_quic_tls_cb_handshake_complete( fd_quic_tls_hs_t * hs,
     217             :                                    void *             context  );
     218             : 
     219             : void
     220             : fd_quic_tls_cb_peer_params( void *        context,
     221             :                             uchar const * peer_tp_enc,
     222             :                             ulong         peer_tp_enc_sz );
     223             : 
     224             : void
     225             : fd_quic_apply_peer_params( fd_quic_conn_t *                   conn,
     226             :                            fd_quic_transport_params_t const * peer_tp );
     227             : 
     228             : /* Helpers for calling callbacks **************************************/
     229             : 
     230             : static inline void
     231             : fd_quic_cb_conn_new( fd_quic_t *      quic,
     232        6069 :                      fd_quic_conn_t * conn ) {
     233        6069 :   if( conn->called_conn_new ) return;
     234        6069 :   conn->called_conn_new = 1;
     235        6069 :   if( !quic->cb.conn_new ) return;
     236             : 
     237        6069 :   quic->cb.conn_new( conn, quic->cb.quic_ctx );
     238        6069 : }
     239             : 
     240             : static inline void
     241             : fd_quic_cb_conn_hs_complete( fd_quic_t *      quic,
     242        6069 :                              fd_quic_conn_t * conn ) {
     243        6069 :   if( !quic->cb.conn_hs_complete ) return;
     244        6069 :   quic->cb.conn_hs_complete( conn, quic->cb.quic_ctx );
     245        6069 : }
     246             : 
     247             : static inline void
     248             : fd_quic_cb_conn_final( fd_quic_t *      quic,
     249       12048 :                        fd_quic_conn_t * conn ) {
     250       12048 :   if( !quic->cb.conn_final || !conn->called_conn_new ) return;
     251       12045 :   quic->cb.conn_final( conn, quic->cb.quic_ctx );
     252       12045 : }
     253             : 
     254             : static inline int
     255             : fd_quic_cb_stream_rx( fd_quic_t *        quic,
     256             :                       fd_quic_conn_t *   conn,
     257             :                       ulong              stream_id,
     258             :                       ulong              offset,
     259             :                       uchar const *      data,
     260             :                       ulong              data_sz,
     261    64820915 :                       int                fin ) {
     262    64820915 :   quic->metrics.stream_rx_event_cnt++;
     263    64820915 :   quic->metrics.stream_rx_byte_cnt += data_sz;
     264             : 
     265    64820915 :   if( !quic->cb.stream_rx ) return FD_QUIC_SUCCESS;
     266    13100647 :   return quic->cb.stream_rx( conn, stream_id, offset, data, data_sz, fin );
     267    64820915 : }
     268             : 
     269             : static inline void
     270             : fd_quic_cb_stream_notify( fd_quic_t *        quic,
     271             :                           fd_quic_stream_t * stream,
     272             :                           void *             stream_ctx,
     273    13089787 :                           int                event ) {
     274    13089787 :   quic->metrics.stream_closed_cnt[ event ]++;
     275    13089787 :   quic->metrics.stream_active_cnt--;
     276             : 
     277    13089787 :   if( !quic->cb.stream_notify ) return;
     278    13089769 :   quic->cb.stream_notify( stream, stream_ctx, event );
     279    13089769 : }
     280             : 
     281             : 
     282             : FD_FN_CONST ulong
     283             : fd_quic_reconstruct_pkt_num( ulong pktnum_comp,
     284             :                              ulong pktnum_sz,
     285             :                              ulong exp_pkt_number );
     286             : 
     287             : void
     288             : fd_quic_pkt_meta_retry( fd_quic_t *          quic,
     289             :                         fd_quic_conn_t *     conn,
     290             :                         ulong                force_below_pkt_num,
     291             :                         uint                 arg_enc_level );
     292             : 
     293             : /* reclaim resources associated with packet metadata
     294             :    this is called in response to received acks */
     295             : void
     296             : fd_quic_reclaim_pkt_meta( fd_quic_conn_t *     conn,
     297             :                           fd_quic_pkt_meta_t * pkt_meta,
     298             :                           uint                 enc_level );
     299             : 
     300             : ulong
     301             : fd_quic_process_quic_packet_v1( fd_quic_t *     quic,
     302             :                                 fd_quic_pkt_t * pkt,
     303             :                                 uchar *         cur_ptr,
     304             :                                 ulong           cur_sz );
     305             : 
     306             : ulong
     307             : fd_quic_handle_v1_initial( fd_quic_t *               quic,
     308             :                            fd_quic_conn_t **         p_conn,
     309             :                            fd_quic_pkt_t *           pkt,
     310             :                            fd_quic_conn_id_t const * dcid,
     311             :                            fd_quic_conn_id_t const * scid,
     312             :                            uchar *                   cur_ptr,
     313             :                            ulong                     cur_sz );
     314             : 
     315             : ulong
     316             : fd_quic_handle_v1_handshake( fd_quic_t *      quic,
     317             :                              fd_quic_conn_t * conn,
     318             :                              fd_quic_pkt_t *  pkt,
     319             :                              uchar *          cur_ptr,
     320             :                              ulong            cur_sz );
     321             : 
     322             : ulong
     323             : fd_quic_handle_v1_one_rtt( fd_quic_t *      quic,
     324             :                            fd_quic_conn_t * conn,
     325             :                            fd_quic_pkt_t *  pkt,
     326             :                            uchar *          cur_ptr,
     327             :                            ulong            cur_sz );
     328             : 
     329             : /* fd_quic_handle_v1_frame is the primary entrypoint for handling of
     330             :    incoming QUIC frames.  {quic,conn,pkt} identify the frame context.
     331             :    Memory region [frame_ptr,frame_ptr+frame_sz) contains the serialized
     332             :    QUIC frame (may contain arbitrary zero padding at the beginning).
     333             : 
     334             :    Returns value in (0,buf_sz) if the frame was successfully processed.
     335             :    Returns FD_QUIC_PARSE_FAIL if the frame was inherently malformed.
     336             :    Returns 0 or value in [buf_sz,ULONG_MAX) in case of a protocol
     337             :    violation. */
     338             : 
     339             : ulong
     340             : fd_quic_handle_v1_frame( fd_quic_t *       quic,
     341             :                          fd_quic_conn_t *  conn,
     342             :                          fd_quic_pkt_t *   pkt,
     343             :                          uint              pkt_type,
     344             :                          uchar const *     frame_ptr,
     345             :                          ulong             frame_sz );
     346             : 
     347             : /* fd_quic_lazy_ack_pkt enqueues future acknowledgement for the given
     348             :    packet.  The ACK will be sent out at a fd_quic_service call.  The
     349             :    delay is determined by the fd_quic_config_t ack_threshold and
     350             :    ack_delay settings.   Respects pkt->ack_flag (ACK_FLAG_RQD schedules
     351             :    an ACK instantly, ACK_FLAG_CANCEL suppresses the ACK by making this
     352             :    function behave like a no-op)  */
     353             : 
     354             : int
     355             : fd_quic_lazy_ack_pkt( fd_quic_t *           quic,
     356             :                       fd_quic_conn_t *      conn,
     357             :                       fd_quic_pkt_t const * pkt );
     358             : 
     359             : static inline fd_quic_conn_t *
     360    54876030 : fd_quic_conn_at_idx( fd_quic_state_t * quic_state, ulong idx ) {
     361    54876030 :   ulong addr = quic_state->conn_base;
     362    54876030 :   ulong sz   = quic_state->conn_sz;
     363    54876030 :   return (fd_quic_conn_t*)( addr + idx * sz );
     364    54876030 : }
     365             : 
     366             : /* called with round-trip-time (rtt) and the ack delay (from the spec)
     367             :    to sample the round trip times. */
     368             : static inline void
     369          72 : fd_quic_sample_rtt( fd_quic_conn_t * conn, long rtt_ns, long ack_delay ) {
     370             :   /* for convenience */
     371          72 :   fd_rtt_estimate_t * rtt = conn->rtt;
     372             : 
     373             :   /* scale ack delay using peer exponent - rfc9000 19.3 */
     374          72 :   float ack_delay_ns = (float)ack_delay * conn->peer_ack_delay_scale;
     375             : 
     376             :   /* bound ack_delay by peer_max_ack_delay */
     377          72 :   ack_delay_ns = fminf( ack_delay_ns, conn->peer_max_ack_delay_ns );
     378             : 
     379          72 :   fd_rtt_sample( rtt, (float)rtt_ns, ack_delay_ns );
     380             : 
     381          72 :   FD_DEBUG({
     382          72 :     FD_LOG_NOTICE(( "conn_idx: %u  min_rtt: %f  smoothed_rtt: %f  var_rtt: %f  rtt_ns: %f  ack_delay_ns: %f  diff: %f",
     383          72 :                     (uint)conn->conn_idx,
     384          72 :                     (double)rtt->min_rtt,
     385          72 :                     (double)rtt->smoothed_rtt,
     386          72 :                     (double)rtt->var_rtt,
     387          72 :                     (double)rtt_ns,
     388          72 :                     (double)ack_delay_ns,
     389          72 :                     ( (double)rtt_ns - (double)ack_delay_ns ) ));
     390          72 :   })
     391          72 : }
     392             : 
     393             : /* fd_quic_calc_expiry_duration returns the duration to the next expiry event.
     394             :    User should add the result to the base time to obtain the expiry timestamp.
     395             :    Uses the loss detection timeout if 'ack_driven', otherwise uses the PTO. */
     396             : 
     397             : static inline long
     398    40436440 : fd_quic_calc_expiry_duration( fd_quic_conn_t * conn, int ack_driven, int is_server ) {
     399             :   /*  For server, we want to be conservative and minimize spam risk, so we stick
     400             :       with the hardcoded 500ms expiry. The following only applies to client.
     401             : 
     402             :      If this calculation is ack-driven, use the time threshold:
     403             :      6.1.2 Time Threshold
     404             :      max(kTimeThreshold * max(smoothed_rtt, latest_rtt), kGranularity)
     405             :      The RECOMMENDED time threshold (kTimeThreshold), expressed as an RTT multiplier, is 9/8 */
     406    40436440 :      #define FD_QUIC_K_TIME_THRESHOLD 1.125f
     407             :   /* Otherwise, calculate the expiry time according to the PTO spec
     408             :      6.2.1. Computing PTO
     409             :      When an ack-eliciting packet is transmitted, the sender schedules
     410             :      a timer for the PTO period as follows:
     411             :      PTO = smoothed_rtt + max(4*rttvar, kGranularity) + max_ack_delay
     412             : 
     413             :      Our granularity is O(ns), while recommended is 1ms --> We don't
     414             :      have to worry about kGranularity.
     415             : */
     416             : 
     417    40436440 :   if( is_server ) return 500e6L;
     418             : 
     419    39740215 :   fd_rtt_estimate_t * rtt = conn->rtt;
     420             : 
     421    39740215 :   long pto_duration  = (long)( rtt->smoothed_rtt     +
     422    39740215 :                                 (4.0f * rtt->var_rtt) +
     423    39740215 :                                 conn->peer_max_ack_delay_ns );
     424             : 
     425    39740215 :   long loss_duration = (long)( FD_QUIC_K_TIME_THRESHOLD * fmaxf( rtt->smoothed_rtt, rtt->latest_rtt ) );
     426             : 
     427    39740215 :   long duration = fd_long_if( ack_driven, loss_duration, pto_duration );
     428             : 
     429    39740215 :   FD_DTRACE_PROBE_3( quic_calc_expiry, conn->our_conn_id, duration, ack_driven );
     430             : 
     431    39740215 :   return duration;
     432    40436440 : }
     433             : 
     434             : uchar *
     435             : fd_quic_gen_stream_frames( fd_quic_conn_t *             conn,
     436             :                            uchar *                      payload_ptr,
     437             :                            uchar *                      payload_end,
     438             :                            fd_quic_pkt_meta_t   * pkt_meta_tmpl,
     439             :                            fd_quic_pkt_meta_tracker_t * tracker );
     440             : 
     441             : void
     442             : fd_quic_process_ack_range( fd_quic_conn_t      *      conn,
     443             :                            fd_quic_frame_ctx_t *      context,
     444             :                            uint                       enc_level,
     445             :                            ulong                      largest_ack,
     446             :                            ulong                      ack_range,
     447             :                            int                        is_largest,
     448             :                            long                       now,
     449             :                            ulong                      ack_delay );
     450             : 
     451             : FD_PROTOTYPES_END
     452             : 
     453             : #endif /* HEADER_fd_src_waltz_quic_fd_quic_private_h */

Generated by: LCOV version 1.14