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

          Line data    Source code
       1             : #ifndef HEADER_fd_src_waltz_quic_fd_quic_h
       2             : #define HEADER_fd_src_waltz_quic_fd_quic_h
       3             : 
       4             : /* fd_quic_t is a partial implementation of QUIC -- an encrypted,
       5             :    multiplexing transport layer network protocol.
       6             : 
       7             :    For now, IPv4 over Ethernet (EN10MB) flows are supported.
       8             : 
       9             :    This API is non-blocking and single-threaded.  Any requests to the
      10             :    peer (such as "open a connection") are queued and processed on RX
      11             :    or service call.  The user is notified of events via callbacks.
      12             :    The user must further ensure that RX (via fd_aio_t) is dispatched
      13             :    only from the thread with the active join to the target fd_quic_t.
      14             : 
      15             :    Scaling is achieved via fd_quic_t instances and steering of RX flows.
      16             :    For example, incoming RX that exceeds the throughput of one fd_quic_t
      17             :    may be load balanced based on QUIC dest conn ID, or UDP src flow ID.
      18             : 
      19             :    This implementation partially implements the following specifications:
      20             :    - https://datatracker.ietf.org/doc/html/rfc9000
      21             :    - https://datatracker.ietf.org/doc/html/rfc9001
      22             :    - https://datatracker.ietf.org/doc/html/rfc9221
      23             : 
      24             :    ### Memory Management
      25             : 
      26             :    fd_quic is entirely pre-allocated.  Currently, a QUIC object reserves
      27             :    space for a number of connection slots, with uniform stream,
      28             :    reassembly, and ACK buffers.
      29             : 
      30             :    ### Memory Layout
      31             : 
      32             :    fd_quic_t is the publicly exported memory layout of a QUIC object.
      33             :    The private memory region of a QUIC object extends beyond the end of
      34             :    this struct.  fd_quic_t is not intended to be allocated directly,
      35             :    refer to the below for details.
      36             : 
      37             :    ### Lifecycle
      38             : 
      39             :    The below state diagram shows the lifecycle of an fd_quic_t.
      40             : 
      41             :       ┌───────────┐  new   ┌───────────┐ join  ┌──────────┐
      42             :       │           ├───────►│           ├──────►│          │
      43             :       │ allocated │        │ formatted │       │  joined  │
      44             :       │           │◄───────┤           │◄──────┤          │
      45             :       └───────────┘ delete └───────────┘ leave └───▲───┬──┘
      46             :                                                    │   │ set config
      47             :                                                    │   │ set callbacks
      48             :                                               fini │   │ init
      49             :                                                 ┌──┴───▼──┐
      50             :                                             ┌───│         │
      51             :                                     service │   │  ready  │
      52             :                                             └──►│         │
      53             :                                                 └─────────┘
      54             : 
      55             :    ### Lifecycle: Allocation & Formatting
      56             : 
      57             :    A QUIC object resides in a contiguous pre-allocated memory region.
      58             :    (Usually, in a Firedancer workspace)  The footprint and internal
      59             :    layout depends on the pre-configured fd_quic_limits_t parameters.
      60             :    These limits are constant throughout the lifetime of an fd_quic_t.
      61             : 
      62             :    Use fd_quic_{align,footprint} to determine size and alignment of the
      63             :    memory region to be used.  Use fd_quic_new to format such a memory
      64             :    region and to obtain an opaque handle.  In the formatted state, the
      65             :    fd_quic_t is position-independent (may be mapped at different virtual
      66             :    addresses).  This is useful for separating allocation and runtime use
      67             :    into different steps.
      68             : 
      69             :    ### Lifecycle: Joining
      70             : 
      71             :    Given an opaque handle, fd_quic_join runs basic coherence checks and
      72             :    returns a typed pointer to the object.  The object is not modified
      73             :    by this operation.  Each object may have multiple active joins, but
      74             :    only one of them may write.  (Typically, a single join is used for
      75             :    service, and secondary joins for read-only monitoring)
      76             : 
      77             :    ### Lifecycle: Usage
      78             : 
      79             :    fd_quic_init initializes an fd_quic_t for use.  On success, the QUIC
      80             :    becomes ready to serve from the thread that init was called from (it
      81             :    is invalid to service QUIC from another thread). */
      82             : 
      83             : /* TODO provide fd_quic on non-hosted targets */
      84             : 
      85             : #include "fd_quic_common.h"
      86             : #include "fd_quic_enum.h"
      87             : 
      88             : #include "../aio/fd_aio.h"
      89             : #include "../tls/fd_tls.h"
      90             : #include "../../util/clock/fd_clock.h"
      91             : #include "../../util/hist/fd_histf.h"
      92             : 
      93             : /* FD_QUIC_API marks public API declarations.  No-op for now. */
      94             : #define FD_QUIC_API
      95             : 
      96             : /* Forward declarations */
      97             : 
      98             : struct fd_quic_conn;
      99             : typedef struct fd_quic_conn fd_quic_conn_t;
     100             : 
     101             : /* fd_quic_limits_t defines the memory layout of an fd_quic_t object.
     102             :    Limits are immutable and valid for the lifetime of an fd_quic_t
     103             :    (i.e. outlasts joins, until fd_quic_delete) */
     104             : 
     105             : struct __attribute__((aligned(16UL))) fd_quic_limits {
     106             :   ulong  conn_cnt;                  /* instance-wide, max concurrent conn count       */
     107             :   ulong  handshake_cnt;             /* instance-wide, max concurrent handshake count  */
     108             :   ulong  log_depth;                 /* instance-wide, depth of shm log cache          */
     109             : 
     110             :   ulong  conn_id_cnt;                 /* per-conn, max conn ID count (min 4UL)          */
     111             :   ulong  inflight_frame_cnt;          /* instance-wide, total max inflight frame count  */
     112             :   ulong  min_inflight_frame_cnt_conn; /* per-conn, min inflight frame count             */
     113             : };
     114             : typedef struct fd_quic_limits fd_quic_limits_t;
     115             : 
     116             : /* fd_quic_layout_t is an offset table describing the memory layout of
     117             :    an fd_quic_t object.  It is deived from fd_quic_limits_t. */
     118             : 
     119             : struct fd_quic_layout {
     120             :   ulong meta_sz;           /* size of this struct */
     121             :   ulong log_off;           /* offset to quic_log */
     122             :   ulong conns_off;         /* offset of connection mem region  */
     123             :   ulong conn_footprint;    /* sizeof a conn                    */
     124             :   ulong conn_map_off;      /* offset of conn map mem region    */
     125             :   int   lg_slot_cnt;       /* see conn_map_new                 */
     126             :   ulong hs_pool_off;       /* offset of the handshake pool     */
     127             :   ulong svc_timers_off;    /* offset of the service timers     */
     128             :   ulong pkt_meta_pool_off; /* offset of the pkt_meta pool      */
     129             : };
     130             : 
     131             : typedef struct fd_quic_layout fd_quic_layout_t;
     132             : 
     133             : /* fd_quic_config_t defines mutable config of an fd_quic_t.  The config is
     134             :    immutable during an active join. */
     135             : 
     136             : struct __attribute__((aligned(16UL))) fd_quic_config {
     137             :   /* Used by tracing/logging code */
     138             : #define FD_QUIC_CONFIG_ENUM_LIST_role(X,...) \
     139             :   X( FD_QUIC_ROLE_CLIENT, "ROLE_CLIENT" )    \
     140             :   X( FD_QUIC_ROLE_SERVER, "ROLE_SERVER" )
     141             : 
     142             : #define FD_QUIC_CONFIG_LIST(X,...) \
     143           0 :   X( role,                        "%d",     enum,  "enum",         __VA_ARGS__ ) \
     144           0 :   X( retry,                       "%d",     bool,  "bool",         __VA_ARGS__ ) \
     145           0 :   X( idle_timeout,                "%ld",    units, "ns",           __VA_ARGS__ ) \
     146           0 :   X( keep_alive,                  "%d",     bool,  "bool",         __VA_ARGS__ ) \
     147           0 :   X( ack_delay,                   "%ld",    units, "ns",           __VA_ARGS__ ) \
     148           0 :   X( ack_threshold,               "%lu",    units, "bytes",        __VA_ARGS__ ) \
     149           0 :   X( retry_ttl,                   "%ld",    units, "ns",           __VA_ARGS__ ) \
     150           0 :   X( tls_hs_ttl,                  "%ld",    units, "ns",           __VA_ARGS__ ) \
     151           0 :   X( identity_public_key,         "%x",     hex32, "",             __VA_ARGS__ ) \
     152           0 :   X( sign,                        "%p",     ptr,   "",             __VA_ARGS__ ) \
     153           0 :   X( sign_ctx,                    "%p",     ptr,   "",             __VA_ARGS__ ) \
     154           0 :   X( req_client_cert,             "%d",     bool,  "bool",         __VA_ARGS__ ) \
     155           0 :   X( initial_rx_max_stream_data,  "%lu",    units, "bytes",        __VA_ARGS__ ) \
     156           0 :   X( max_datagram_frame_size,     "%lu",    units, "bytes",        __VA_ARGS__ ) \
     157           0 :   X( net.dscp,                    "0x%02x", value, "",             __VA_ARGS__ )
     158             : 
     159             :   /* Protocol config ***************************************/
     160             : 
     161             :   /* role: one of FD_QUIC_ROLE_{CLIENT,SERVER} */
     162             :   int role;
     163             : 
     164             :   /* retry: whether address validation using retry packets is enabled (RFC 9000, Section 8.1.2) */
     165             :   int retry;
     166             : 
     167             :   /* idle_timeout: Upper bound on conn idle timeout.
     168             :      Also sent to peer via max_idle_timeout transport param.
     169             :      If the peer specifies a lower idle timeout, that is used instead. */
     170             :   long idle_timeout;
     171           0 : # define FD_QUIC_DEFAULT_IDLE_TIMEOUT (ulong)(1e9) /* 1s */
     172             : 
     173             : /* keep_alive
     174             :  * whether the fd_quic should use QUIC PING frames to keep connections alive
     175             :  * Set to 1 to keep connections alive
     176             :  * Set to 0 to allow connections to close on idle
     177             :  * default is 0 */
     178             :   int keep_alive;
     179             : 
     180             :   /* ack_delay: median delay on outgoing ACKs.  Greater delays allow
     181             :      fd_quic to coalesce packet ACKs. */
     182             :   long ack_delay;
     183           0 : # define FD_QUIC_DEFAULT_ACK_DELAY (long)(10e6) /* 10ms */
     184             : 
     185             :   /* ack_threshold: immediately send an ACK when the number of
     186             :      unacknowledged stream bytes exceeds this value. */
     187             :   ulong ack_threshold;
     188           0 : # define FD_QUIC_DEFAULT_ACK_THRESHOLD (65536UL) /* 64 KiB */
     189             : 
     190             :   /* retry_ttl: time-to-live for retry tokens */
     191             :   long retry_ttl;
     192           0 : # define FD_QUIC_DEFAULT_RETRY_TTL (long)(10e9) /* 10s: a client's PTO probes at +1, +3, +7 s still carry a valid token */
     193             : 
     194             :   /* hs_ttl: time-to-live for tls_hs */
     195             :   long tls_hs_ttl;
     196           0 : # define FD_QUIC_DEFAULT_TLS_HS_TTL (long)(3e9) /* 3s */
     197             : 
     198             :   /* TLS config ********************************************/
     199             : 
     200             :   /* identity_key: Ed25519 public key of node identity */
     201             :   uchar identity_public_key[ 32 ];
     202             : 
     203             :   /* Callback for signing TLS 1.3 certificate verify payload */
     204             :   fd_tls_sign_fn_t sign;
     205             :   void *           sign_ctx;
     206             : 
     207             :   /* alpn: either "solana-tpu" or "alpenglow-v1" */
     208             :   uchar alpn[ 32 ];
     209             :   ulong alpn_sz;
     210             : 
     211             :   /* req_client_cert (server): request a client certificate during the
     212             :      TLS handshake.  Clients are then required to authenticate with an
     213             :      Ed25519 key, available at conn->tls_hs->hs.srv.client_pubkey. */
     214             :   int req_client_cert;
     215             : 
     216             :   ulong initial_rx_max_stream_data; /* per-stream, rx buf sz in bytes, set by the user. */
     217             :   ulong max_datagram_frame_size;    /* RFC 9221 RX frame limit; zero disables DATAGRAM */
     218             : 
     219             :   /* Network config ****************************************/
     220             : 
     221             :   struct { /* Internet config */
     222             :     /* dscp: Differentiated services code point.
     223             :        Set on all outgoing IPv4 packets. */
     224             :     uchar dscp;
     225             :   } net;
     226             : };
     227             : 
     228             : /* Callback API *******************************************************/
     229             : 
     230             : /* Note: QUIC library invokes callbacks during RX or service.  Callback
     231             :    may only invoke fd_quic API methods labelled CB-safe.  Callbacks are
     232             :    not re-entrant. */
     233             : 
     234             : /* fd_quic_cb_conn_new_t: server received a new conn and completed
     235             :    handshakes. */
     236             : typedef void
     237             : (* fd_quic_cb_conn_new_t)( fd_quic_conn_t * conn,
     238             :                            void *           quic_ctx );
     239             : 
     240             : /* fd_quic_cb_conn_handshake_complete_t: client completed a handshake
     241             :    of a conn it created. */
     242             : typedef void
     243             : (* fd_quic_cb_conn_handshake_complete_t)( fd_quic_conn_t * conn,
     244             :                                           void *           quic_ctx );
     245             : 
     246             : /* fd_quic_cb_conn_final_t: Conn termination notification.  The conn
     247             :    object is freed immediately after returning.  User should destroy any
     248             :    remaining references to conn in this callback. */
     249             : typedef void
     250             : (* fd_quic_cb_conn_final_t)( fd_quic_conn_t * conn,
     251             :                              void *           quic_ctx );
     252             : 
     253             : typedef int
     254             : (* fd_quic_cb_stream_rx_t)( fd_quic_conn_t * conn,
     255             :                             ulong            stream_id,
     256             :                             ulong            offset,
     257             :                             uchar const *    data,
     258             :                             ulong            data_sz,
     259             :                             int              fin );
     260             : 
     261             : typedef void
     262             : (* fd_quic_cb_datagram_rx_t)( fd_quic_conn_t * conn,
     263             :                               uchar const *    data,
     264             :                               ulong            data_sz,
     265             :                               void *           quic_ctx );
     266             : 
     267             : /* fd_quic_cb_tls_keylog_t is called when a new encryption secret
     268             :    becomes available.  line is a cstr containing the secret in NSS key
     269             :    log format (intended for tests only). */
     270             : 
     271             : typedef void
     272             : (* fd_quic_cb_tls_keylog_t)( void *       quic_ctx,
     273             :                              char const * line );
     274             : 
     275             : /* fd_quic_cb_ack_range_t is called for each range of 1-RTT packet
     276             :    numbers [pkt_num_lo,pkt_num_hi] acknowledged by an ACK frame.  Ranges
     277             :    may overlap previously reported ones. */
     278             : typedef void
     279             : (* fd_quic_cb_ack_range_t)( fd_quic_conn_t * conn,
     280             :                             ulong            pkt_num_lo,
     281             :                             ulong            pkt_num_hi,
     282             :                             void *           quic_ctx );
     283             : 
     284             : /* fd_quic_callbacks_t defines the set of user-provided callbacks that
     285             :    are invoked by the QUIC library.  Resets on leave. */
     286             : 
     287             : struct fd_quic_callbacks {
     288             :   /* Function pointers to user callbacks */
     289             : 
     290             :   void * quic_ctx; /* user-provided context pointer
     291             :                       for instance-wide callbacks */
     292             : 
     293             :   fd_quic_cb_conn_new_t                conn_new;          /* non-NULL, with quic_ctx   */
     294             :   fd_quic_cb_conn_handshake_complete_t conn_hs_complete;  /* non-NULL, with quic_ctx   */
     295             :   fd_quic_cb_conn_final_t              conn_final;        /* non-NULL, with quic_ctx   */
     296             :   fd_quic_cb_stream_rx_t               stream_rx;         /* non-NULL, with stream_ctx */
     297             :   fd_quic_cb_datagram_rx_t             datagram_rx;       /* nullable, with quic_ctx   */
     298             :   fd_quic_cb_tls_keylog_t              tls_keylog;        /* nullable, with quic_ctx   */
     299             :   fd_quic_cb_ack_range_t               ack_range;         /* nullable, with quic_ctx   */
     300             : 
     301             : };
     302             : typedef struct fd_quic_callbacks fd_quic_callbacks_t;
     303             : 
     304             : /* fd_quic metrics ****************************************************/
     305             : 
     306             : /* TODO: evaluate performance impact of metrics */
     307             : 
     308             : union fd_quic_metrics {
     309             :   struct {
     310             :     /* Network metrics */
     311             :     ulong net_rx_pkt_cnt;  /* number of IP packets received */
     312             :     ulong net_rx_byte_cnt; /* total bytes received (including IP, UDP, QUIC headers) */
     313             :     ulong net_tx_pkt_cnt;  /* number of IP packets sent */
     314             :     ulong net_tx_byte_cnt; /* total bytes sent */
     315             :     ulong retry_tx_cnt;    /* number of Retry packets sent */
     316             : 
     317             :     /* Conn metrics */
     318             :     ulong conn_alloc_cnt;          /* number of conns currently allocated */
     319             :     ulong conn_created_cnt;        /* number of conns created */
     320             :     ulong conn_closed_cnt;         /* number of conns gracefully closed */
     321             :     ulong conn_aborted_cnt;        /* number of conns aborted */
     322             :     ulong conn_timeout_cnt;        /* number of conns timed out */
     323             :     ulong conn_retry_cnt;          /* number of conns established with retry */
     324             :     ulong conn_err_no_slots_cnt;   /* number of conns that failed to create due to lack of slots */
     325             :     ulong conn_err_retry_fail_cnt; /* number of conns that failed during retry (e.g. invalid token) */
     326             :     ulong conn_err_retry_expired_cnt; /* of those, our own tokens that expired, answered with INVALID_TOKEN */
     327             :     ulong conn_state_cnt[ 8 ];     /* current number of conns in each state */
     328             : 
     329             :     /* Packet metrics */
     330             :     ulong pkt_net_hdr_err_cnt;          /* number of packets dropped due to weird IPv4/UDP headers */
     331             :     ulong pkt_quic_hdr_err_cnt;         /* number of packets dropped due to weird QUIC header */
     332             :     ulong pkt_undersz_cnt;              /* number of QUIC packets dropped due to being too small */
     333             :     ulong pkt_oversz_cnt;               /* number of QUIC packets dropped due to being too large */
     334             :     ulong pkt_decrypt_fail_cnt[ 4 ];    /* number of packets that failed decryption due to auth tag */
     335             :     ulong pkt_no_key_cnt[ 4 ];          /* number of packets that failed decryption due to missing key */
     336             :     ulong pkt_no_conn_cnt[ 4 ];         /* number of packets with unknown conn ID (initial, retry, hs, 1-RTT) */
     337             :     ulong pkt_wrong_src_cnt;            /* number of packets from a wrong source IP */
     338             :     ulong pkt_dup_cnt;                  /* number of packets with an already received or too old packet number */
     339             :     ulong frame_tx_alloc_cnt[ 3 ];      /* number of pkt_meta alloc successes, fails for empty pool, fails at conn max */
     340             :     ulong pkt_verneg_cnt;               /* number of QUIC version negotiation packets or packets with wrong version */
     341             :     ulong pkt_retransmissions_cnt[ 4 ]; /* number of pkt_meta retries */
     342             :     ulong initial_token_len_cnt[ 3 ];   /* number of Initial packets grouped by token length */
     343             : 
     344             :     /* Frame metrics */
     345             :     ulong frame_rx_cnt[ 23 ];      /* number of frames received (indexed by implementation-defined IDs) */
     346             :     ulong frame_rx_err_cnt;        /* number of frames failed */
     347             : 
     348             :     /* Handshake metrics */
     349             :     ulong hs_created_cnt;          /* number of handshake flows created */
     350             :     ulong hs_err_alloc_fail_cnt;   /* number of handshakes dropped due to alloc fail */
     351             :     ulong hs_evicted_cnt;          /* number of handshakes evicted */
     352             : 
     353             :     /* Stream metrics */
     354             :     ulong stream_rx_event_cnt;      /* number of stream RX events */
     355             :     ulong stream_rx_byte_cnt;       /* total stream payload bytes received */
     356             : 
     357             :     /* ACK metrics */
     358             :     ulong ack_tx[ 5 ];
     359             : 
     360             :     /* Performance metrics */
     361             :     fd_histf_t service_duration[ 1 ]; /* time spent in service */
     362             :     fd_histf_t receive_duration[ 1 ]; /* time spent in process_packet calls */
     363             :   };
     364             : };
     365             : typedef union fd_quic_metrics fd_quic_metrics_t;
     366             : 
     367             : /* fd_quic_t memory layout ********************************************/
     368             : 
     369             : struct fd_quic {
     370             :   ulong magic;   /* ==FD_QUIC_MAGIC */
     371             : 
     372             :   fd_quic_layout_t    layout;  /* position-independent, persistent,    read only */
     373             :   fd_quic_limits_t    limits;  /* position-independent, persistent,    read only */
     374             :   fd_quic_config_t    config;  /* position-independent, persistent,    writable pre init */
     375             :   fd_quic_callbacks_t cb;      /* position-dependent,   reset on join, writable pre init  */
     376             :   fd_quic_metrics_t   metrics; /* position-independent, persistent,    read only */
     377             : 
     378             :   fd_aio_t aio_rx; /* local AIO */
     379             :   fd_aio_t aio_tx; /* remote AIO */
     380             : 
     381             :   /* ... private variable-length structures follow ... */
     382             : };
     383             : 
     384             : FD_PROTOTYPES_BEGIN
     385             : 
     386             : /* Object lifecycle ***************************************************/
     387             : 
     388             : /* fd_quic_{align,footprint} return the required alignment and footprint
     389             :    of a memory region suitable for use as an fd_quic_t.  align returns
     390             :    FD_QUIC_ALIGN.  limits is a temporary reference to the requested
     391             : 
     392             :    On failure, footprint will silently return 0 (and thus can be used by
     393             :    the caller to validate fd_quic_new params) */
     394             : 
     395             : FD_QUIC_API FD_FN_CONST ulong
     396             : fd_quic_align( void );
     397             : 
     398             : FD_QUIC_API ulong
     399             : fd_quic_footprint( fd_quic_limits_t const * limits );
     400             : 
     401             : /* fd_quic_new formats an unused memory region for use as a QUIC client
     402             :    or server.  mem is a non-NULL pointer to this region in the local
     403             :    address with the required footprint and alignment.  limits is a
     404             :    temporary reference, identical to the one given to fd_quic_footprint
     405             :    used to figure out the required footprint. */
     406             : 
     407             : FD_QUIC_API void *
     408             : fd_quic_new( void *                   mem,
     409             :              fd_quic_limits_t const * limits );
     410             : 
     411             : /* fd_quic_join joins the caller to the fd_quic.  shquic points to the
     412             :    first byte of the memory region backing the QUIC in the caller's
     413             :    address space.
     414             : 
     415             :    Returns a pointer in the local address space to the public fd_quic_t
     416             :    region on success (do not assume this to be just a cast of shquic)
     417             :    and NULL on failure (logs details).  Reasons for failure are that
     418             :    shquic is obviously not a pointer to a correctly formatted QUIC
     419             :    object.  Every successful join should have a matching leave.  The
     420             :    lifetime of the join is until the matching leave or the thread group
     421             :    is terminated. */
     422             : 
     423             : FD_QUIC_API fd_quic_t *
     424             : fd_quic_join( void * shquic );
     425             : 
     426             : /* fd_quic_leave leaves a current local join and frees all dynamically
     427             :    managed resources (heap allocs, OS handles).  Returns the given quic
     428             :    on success and NULL on failure (logs details).  Reasons for failure
     429             :    include quic is NULL or no active join */
     430             : 
     431             : FD_QUIC_API void *
     432             : fd_quic_leave( fd_quic_t * quic );
     433             : 
     434             : /* fd_quic_delete unformats a memory region used as an fd_quic_t.
     435             :    Assumes nobody is joined to the region.  Returns the given quic
     436             :    pointer on success and NULL if used obviously in error (e.g. quic is
     437             :    obviously not an fd_quic_t ... logs details).  The ownership of the
     438             :    memory region is transferred to the caller. */
     439             : 
     440             : FD_QUIC_API void *
     441             : fd_quic_delete( fd_quic_t * quic );
     442             : 
     443             : /* Configuration ******************************************************/
     444             : 
     445             : /* fd_quic_{limits,config}_from_env populates the given QUIC limits or
     446             :    config from command-line args and env vars.  If parg{c,v} are non-
     447             :    NULL, they are updated to strip the parsed args.  The last element of
     448             :    the *argv array must be NULL.  Returns given config on success and
     449             :    NULL on failure (logs details).  It is up to the caller to properly
     450             :    initialize the given limits/config. */
     451             : 
     452             : FD_QUIC_API fd_quic_limits_t *
     453             : fd_quic_limits_from_env( int  *   pargc,
     454             :                          char *** pargv,
     455             :                          fd_quic_limits_t * limits );
     456             : 
     457             : FD_QUIC_API fd_quic_config_t *
     458             : fd_quic_config_from_env( int  *   pargc,
     459             :                          char *** pargv,
     460             :                          fd_quic_config_t * config );
     461             : 
     462             : /* fd_quic_get_aio_net_rx returns this QUIC's aio base class.  Valid
     463             :    for lifetime of QUIC.  While pointer to aio can be obtained before
     464             :    init, calls to aio may only be dispatched by the thread with
     465             :    exclusive access to QUIC that owns it. */
     466             : 
     467             : FD_QUIC_API fd_aio_t const *
     468             : fd_quic_get_aio_net_rx( fd_quic_t * quic );
     469             : 
     470             : /* fd_quic_set_aio_net_tx sets the fd_aio_t used by the fd_quic_t to
     471             :    send tx data to the network driver.  Cleared on fini. */
     472             : 
     473             : FD_QUIC_API void
     474             : fd_quic_set_aio_net_tx( fd_quic_t *      quic,
     475             :                         fd_aio_t const * aio_tx );
     476             : 
     477             : /* Initialization *****************************************************/
     478             : 
     479             : /* fd_quic_init initializes the QUIC such that it is ready to serve.
     480             :    permits the calling thread exclusive access during which no other
     481             :    thread may write to the QUIC.  Exclusive rights get released when the
     482             :    thread exits or calls fd_quic_fini.
     483             : 
     484             :    Requires valid configuration and external objects (aio, callbacks).
     485             :    Returns given quic on success and NULL on failure (logs details).
     486             :    Performs various heap allocations and file system accesses such
     487             :    reading certs.  Reasons for failure include invalid config or
     488             :    fd_tls error. */
     489             : 
     490             : FD_QUIC_API fd_quic_t *
     491             : fd_quic_init( fd_quic_t * quic );
     492             : 
     493             : /* fd_quic_fini releases exclusive access over a QUIC.  Zero-initializes
     494             :    references to external objects (aio, callbacks).  Frees any heap
     495             :    allocs made by fd_quic_init.  Returns quic. */
     496             : 
     497             : FD_QUIC_API fd_quic_t *
     498             : fd_quic_fini( fd_quic_t * quic );
     499             : 
     500             : /* fd_quic_set_identity_public_key updates the public key used for
     501             :    identity validation.  This function should only be called after the
     502             :    QUIC has been initialized. */
     503             : FD_QUIC_API void
     504             : fd_quic_set_identity_public_key( fd_quic_t * quic,
     505             :                                  uchar const public_key[ static 32 ] );
     506             : 
     507             : /* NOTE: Calling any of the below requires valid initialization from
     508             :    this thread group. */
     509             : 
     510             : /* Connection API *****************************************************/
     511             : 
     512             : /* fd_quic_connect initiates a new client connection to a remote QUIC
     513             :    server.  On success, returns a pointer to the conn object managed by
     514             :    QUIC.  On failure, returns NULL.  Reasons for failure include quic
     515             :    not a valid join or out of free conns.  Lifetime of returned conn is
     516             :    until conn_final callback.
     517             : 
     518             :    args
     519             :      dst_ip_addr   destination ip address, in net order
     520             :      dst_udp_port  destination port number, in host order */
     521             : 
     522             : FD_QUIC_API fd_quic_conn_t *
     523             : fd_quic_connect( fd_quic_t *  quic,  /* requires exclusive access */
     524             :                  uint         dst_ip_addr,
     525             :                  ushort       dst_udp_port,
     526             :                  uint         src_ip_addr,
     527             :                  ushort       src_udp_port,
     528             :                  long         now );
     529             : 
     530             : /* fd_quic_conn_close asynchronously initiates a shutdown of the conn.
     531             :    The given reason code is returned to the peer via a CONNECTION_CLOSE
     532             :    frame, if possible.  Causes conn_final callback to be issued
     533             :    eventually. */
     534             : 
     535             : FD_QUIC_API void
     536             : fd_quic_conn_close( fd_quic_conn_t * conn,
     537             :                     uint             reason );
     538             : 
     539             : /* fd_quic_conn_free instantly frees the given conn object without
     540             :    issuing a conn_final callback.  Does not send a CONNECTION_CLOSE
     541             :    frame. */
     542             : 
     543             : FD_QUIC_API void
     544             : fd_quic_conn_free( fd_quic_t *      quic,
     545             :                    fd_quic_conn_t * conn );
     546             : 
     547             : /* fd_quic_conn_let_die stops keeping a conn alive after
     548             :    'keep_alive_duration_ns'. No-op if keep-alive is not configured.
     549             :    Safe to call on a connection in any state.
     550             : 
     551             :    If called multiple times on the same connection, only the latest
     552             :    call will stay in effect. However, it may not take effect if we
     553             :    already skipped a keep-alive due to a previous call. 'Undoing' a
     554             :    previous call can be done by passing ULONG_MAX.
     555             : 
     556             :    This function does NOT guarantee that the connection will be closed
     557             :    immediately after the given duration. Rather, it just disables keep-alive
     558             :    behavior after the given duration. */
     559             : 
     560             : FD_QUIC_API void
     561             : fd_quic_conn_let_die( fd_quic_conn_t * conn,
     562             :                       long             keep_alive_duration_ns,
     563             :                       long             now );
     564             : 
     565             : /* fd_quic_conn_tx_dgram builds a QUICv1 packet containing a single
     566             :    RFC 9221 DATAGRAM frame.  The UDP datagram payload containing the
     567             :    QUIC packet is written to [pkt,pkt+pkt_sz), and it is the caller's
     568             :    responsibility to send it out.
     569             : 
     570             :    dgram is the content of the DATAGRAM frame (dgram_sz bytes size).
     571             : 
     572             :    On success, returns UDP payload size and uses up the next conn TX
     573             :    packet number, which is written to *opt_pkt_num if non-NULL.
     574             :    Returns 0 on failure.  Reasons for failure include:
     575             :    - connection is not yet established
     576             :    - peer does not support the DATAGRAM extension
     577             :    - dgram_sz exceed's the peer's limit
     578             :    - pkt_sz (UDP payload MTU) is too small */
     579             : 
     580             : FD_QUIC_API ulong
     581             : fd_quic_conn_tx_dgram( fd_quic_conn_t * conn,
     582             :                        uchar *          pkt,
     583             :                        ulong            pkt_sz,
     584             :                        uchar const *    dgram,
     585             :                        ulong            dgram_sz,
     586             :                        ulong *          opt_pkt_num );
     587             : 
     588             : /* fd_quic_conn_tx_stream coalesces whole streams into QUICv1 packets.
     589             :    The caller owns the packet being filled: [pkt,pkt+pkt_sz) holds
     590             :    *pkt_used bytes of STREAM frames (zero for a new packet), and the
     591             :    caller passes the same pkt and pkt_used on every call.
     592             : 
     593             :    If the stream fits, appends a STREAM frame (off=0, sz=data_sz, fin=1)
     594             :    carrying data and returns 0.  Otherwise seals the packet: replaces
     595             :    the frames in pkt with the UDP datagram payload, returns its size,
     596             :    writes its packet number to *opt_pkt_num if non-NULL, and resets
     597             :    *pkt_used to 0.  The caller sends pkt out and calls again with the
     598             :    same stream.
     599             : 
     600             :    stream_id==ULONG_MAX adds no stream and seals the packet (returns 0
     601             :    if it holds no frames).
     602             : 
     603             :    The caller picks stream IDs (unidirectional, of its own role, below
     604             :    conn->tx_sup_stream_id) and retransmits.  The ack_range callback
     605             :    reports the packet numbers that arrived.
     606             : 
     607             :    Usage of the peer's flow control quota (conn->tx_max_data) is not
     608             :    tracked accurately.  This is fine because stream sending is only
     609             :    used in tests.
     610             : 
     611             :    Returns ULONG_MAX on failure.  Reasons for failure include:
     612             :    - connection is not yet established
     613             :    - stream_id is of the wrong type, or exceeds the peer's limit
     614             :    - data_sz exceeds the peer's per-stream data limit
     615             :    - the stream does not fit in a packet of pkt_sz (UDP payload MTU) */
     616             : 
     617             : FD_QUIC_API ulong
     618             : fd_quic_conn_tx_stream( fd_quic_conn_t * conn,
     619             :                         uchar *          pkt,
     620             :                         ulong            pkt_sz,
     621             :                         ulong *          pkt_used,
     622             :                         ulong            stream_id,
     623             :                         uchar const *    data,
     624             :                         ulong            data_sz,
     625             :                         ulong *          opt_pkt_num );
     626             : 
     627             : /* Service API ********************************************************/
     628             : 
     629             : /* fd_quic_get_next_wakeup returns the next requested service time, or
     630             :    LONG_MAX if nothing is scheduled. */
     631             : 
     632             : FD_QUIC_API long
     633             : fd_quic_get_next_wakeup( fd_quic_t * quic );
     634             : 
     635             : /* fd_quic_service services the next QUIC connection at each service
     636             :    level, including ACK transmit, loss timeout, and idle timeout.
     637             :    The user should call service at high frequency.  Returns the number
     638             :    of connections serviced, where 0 means the call did no work. */
     639             : 
     640             : FD_QUIC_API int
     641             : fd_quic_service( fd_quic_t * quic,
     642             :                  long        now );
     643             : 
     644             : /* fd_quic_svc_validate checks for violations of service queue and free
     645             :    list invariants, such as cycles in linked lists.  Prints to warning/
     646             :    error log and exits the process if checks fail.  Intended for use in
     647             :    tests. */
     648             : 
     649             : void
     650             : fd_quic_state_validate( fd_quic_t * quic );
     651             : 
     652             : FD_QUIC_API void
     653             : fd_quic_process_packet( fd_quic_t * quic,
     654             :                         uchar *     data,
     655             :                         ulong       data_sz,
     656             :                         long        now );
     657             : 
     658             : 
     659             : uint
     660             : fd_quic_tx_buffered_raw( fd_quic_t * quic,
     661             :                          uchar **    tx_ptr_ptr,
     662             :                          uchar *     tx_buf,
     663             :                          ushort *    ipv4_id,
     664             :                          uint        dst_ipv4_addr,
     665             :                          ushort      dst_udp_port,
     666             :                          uint        src_ipv4_addr,
     667             :                          ushort      src_udp_port );
     668             : 
     669             : FD_PROTOTYPES_END
     670             : 
     671             : /* Convenience exports for consumers of API */
     672             : #include "fd_quic_conn.h"
     673             : 
     674             : /* FD_DEBUG_MODE: set to enable debug-only code
     675             :    TODO move to util? */
     676             : #ifdef FD_DEBUG_MODE
     677             : #define FD_DEBUG(...) __VA_ARGS__
     678             : #else
     679             : #define FD_DEBUG(...)
     680             : #endif
     681             : 
     682             : #endif /* HEADER_fd_src_waltz_quic_fd_quic_h */

Generated by: LCOV version 1.14