LCOV - code coverage report
Current view: top level - waltz/quic - fd_quic_private.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 82 0.0 %
Date: 2026-11-26 02:16:43 Functions: 0 330 0.0 %

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

Generated by: LCOV version 1.14