LCOV - code coverage report
Current view: top level - discof/send - fd_send_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 409 0.0 %
Date: 2025-08-05 05:04:49 Functions: 0 23 0.0 %

          Line data    Source code
       1             : #include "fd_send_tile.h"
       2             : #include "../../disco/topo/fd_topo.h"
       3             : #include "../../disco/keyguard/fd_keyload.h"
       4             : #include "generated/fd_send_tile_seccomp.h"
       5             : 
       6             : #include <errno.h>
       7             : #include <sys/random.h>
       8             : 
       9             : /* map leader pubkey to quic conn
      10             :    a peer entry can be in 3 states:
      11             :    - UNSTAKED: no element in map
      12             :    - NO_CONN: pubkey maps to null conn. staked, but no active conn
      13             :    - CONN: pubkey maps to conn. staked, connection initiated
      14             : 
      15             : The state machine works as follows:
      16             : 
      17             : receive stake msg including pubkey:
      18             :   - if UNSTAKED, create new entry in NO_CONN state
      19             : 
      20             : receive contact info:
      21             :   - Update contact info. NO_CONN -> CONN. If CONN and contact info changed,
      22             :     restart the conn.
      23             :   - touch last_ci_ticks
      24             : 
      25             : Conn closed:
      26             :   - reconnect, unless contact info stale
      27             : */
      28           0 : #define CONTACT_INFO_STALE_TICKS (60e9L) /* ~60 seconds */
      29             : 
      30             : #define MAP_NAME               fd_send_conn_map
      31           0 : #define MAP_T                  fd_send_conn_entry_t
      32           0 : #define MAP_LG_SLOT_CNT        17
      33           0 : #define MAP_KEY                pubkey
      34           0 : #define MAP_KEY_T              fd_pubkey_t
      35           0 : #define MAP_KEY_NULL           (fd_pubkey_t){0}
      36           0 : #define MAP_KEY_EQUAL(k0,k1)   (!(memcmp((k0).key,(k1).key,sizeof(fd_pubkey_t))))
      37           0 : #define MAP_KEY_INVAL(k)       (MAP_KEY_EQUAL((k),MAP_KEY_NULL))
      38             : #define MAP_KEY_EQUAL_IS_SLOW  1
      39           0 : #define MAP_KEY_HASH(key)      ((key).ui[3])
      40             : #include "../../util/tmpl/fd_map.c"
      41             : 
      42             : fd_quic_limits_t quic_limits = {
      43             :   .conn_cnt                    = MAX_STAKED_LEADERS,
      44             :   .handshake_cnt               = MAX_STAKED_LEADERS,
      45             :   .conn_id_cnt                 = FD_QUIC_MIN_CONN_ID_CNT,
      46             :   .inflight_frame_cnt          = 16UL * MAX_STAKED_LEADERS,
      47             :   .min_inflight_frame_cnt_conn = 4UL,
      48             :   .stream_id_cnt               = 16UL,
      49             :   .tx_buf_sz                   = FD_TXN_MTU,
      50             :   .stream_pool_cnt             = 2048UL
      51             : };
      52             : 
      53             : FD_FN_CONST static inline ulong
      54           0 : scratch_align( void ) {
      55           0 :   return fd_ulong_max( fd_ulong_max( 128UL, fd_quic_align() ), fd_send_conn_map_align() );
      56           0 : }
      57             : 
      58             : FD_FN_PURE static inline ulong
      59           0 : scratch_footprint( fd_topo_tile_t const * tile FD_PARAM_UNUSED) {
      60           0 :   ulong l = FD_LAYOUT_INIT;
      61           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_send_tile_ctx_t), sizeof(fd_send_tile_ctx_t) );
      62           0 :   l = FD_LAYOUT_APPEND( l, fd_quic_align(), fd_quic_footprint( &quic_limits ) );
      63           0 :   l = FD_LAYOUT_APPEND( l, fd_send_conn_map_align(), fd_send_conn_map_footprint() );
      64           0 :   return FD_LAYOUT_FINI( l, scratch_align() );
      65           0 : }
      66             : 
      67             : /* QUIC callbacks */
      68             : 
      69             : static void
      70             : quic_tls_cv_sign( void *      signer_ctx,
      71             :                   uchar       signature[ static 64 ],
      72           0 :                   uchar const payload[ static 130 ] ) {
      73           0 :   fd_send_tile_ctx_t * ctx = signer_ctx;
      74           0 :   long dt = -fd_tickcount();
      75           0 :   fd_keyguard_client_sign( ctx->keyguard_client, signature, payload, 130UL, FD_KEYGUARD_SIGN_TYPE_ED25519 );
      76           0 :   dt += ( ctx->now = fd_tickcount() );
      77           0 :   fd_histf_sample( ctx->metrics.sign_duration, (ulong)dt );
      78           0 : }
      79             : 
      80             : /* quic_now is used by quic to get current time */
      81             : static ulong
      82           0 : quic_now( void * _ctx ) {
      83           0 :   fd_send_tile_ctx_t const * ctx = fd_type_pun_const( _ctx );
      84           0 :   return (ulong)ctx->now;
      85           0 : }
      86             : 
      87             : /* quic_hs_complete is called when the QUIC handshake is complete
      88             :    It is currently used only for debug logging */
      89             : static void
      90             : quic_hs_complete( fd_quic_conn_t * conn,
      91           0 :                   void *           quic_ctx FD_PARAM_UNUSED ) {
      92           0 :   fd_send_conn_entry_t * entry = fd_type_pun( fd_quic_conn_get_context( conn ) );
      93           0 :   if( FD_UNLIKELY( !entry ) ) return;
      94           0 :   FD_LOG_DEBUG(("send_tile: QUIC handshake complete for leader %s", FD_BASE58_ENC_32_ALLOCA( entry->pubkey.key )));
      95           0 : }
      96             : 
      97             : /* quic_conn_final is called when the QUIC connection dies.
      98             :    Reconnects if contact info is recent enough. */
      99             : static void
     100             : quic_conn_final( fd_quic_conn_t * conn,
     101           0 :                  void *           quic_ctx ) {
     102           0 :   fd_send_tile_ctx_t * ctx = quic_ctx;
     103             : 
     104           0 :   fd_send_conn_entry_t * entry = fd_type_pun( fd_quic_conn_get_context( conn ) );
     105           0 :   if( FD_UNLIKELY( !entry ) ) {
     106           0 :     FD_LOG_ERR(( "send_tile: Conn map entry not found in conn_final" ));
     107           0 :   }
     108             : 
     109           0 :   if( ctx->now - entry->last_ci_ticks > CONTACT_INFO_STALE_TICKS ) {
     110             :     /* stale contact info, don't reconnect */
     111           0 :     entry->conn = NULL;
     112           0 :     ctx->metrics.contact_stale++;
     113           0 :     return;
     114           0 :   }
     115             : 
     116           0 :   uint ip4_addr = entry->ip4_addr;
     117           0 :   FD_LOG_DEBUG(("send_tile: Quic conn final: %p to peer %u.%u.%u.%u:%u", (void*)conn, ip4_addr&0xFF, (ip4_addr>>8)&0xFF, (ip4_addr>>16)&0xFF, (ip4_addr>>24)&0xFF, entry->udp_port));
     118           0 :   entry->conn = NULL;
     119           0 :   quic_connect( ctx, entry );
     120           0 : }
     121             : 
     122             : static int
     123             : quic_tx_aio_send( void *                    _ctx,
     124             :                   fd_aio_pkt_info_t const * batch,
     125             :                   ulong                     batch_cnt,
     126             :                   ulong *                   opt_batch_idx,
     127           0 :                   int                       flush ) {
     128           0 :   (void)flush;
     129             : 
     130           0 :   fd_send_tile_ctx_t * ctx = _ctx;
     131             : 
     132           0 :   for( ulong i=0; i<batch_cnt; i++ ) {
     133           0 :     if( FD_UNLIKELY( batch[ i ].buf_sz<FD_NETMUX_SIG_MIN_HDR_SZ ) ) continue;
     134             : 
     135           0 :     uint const ip_dst = FD_LOAD( uint, batch[ i ].buf+offsetof( fd_ip4_hdr_t, daddr_c ) );
     136             : 
     137           0 :     fd_send_link_out_t * net_out_link = ctx->net_out;
     138           0 :     uchar * packet_l2 = fd_chunk_to_laddr( net_out_link->mem, net_out_link->chunk );
     139           0 :     uchar * packet_l3 = packet_l2 + sizeof(fd_eth_hdr_t);
     140           0 :     memset( packet_l2, 0, 12 );
     141           0 :     FD_STORE( ushort, packet_l2+offsetof( fd_eth_hdr_t, net_type ), fd_ushort_bswap( FD_ETH_HDR_TYPE_IP ) );
     142           0 :     fd_memcpy( packet_l3, batch[ i ].buf, batch[ i ].buf_sz );
     143           0 :     ulong sz_l2 = sizeof(fd_eth_hdr_t) + batch[ i ].buf_sz;
     144             : 
     145             :     /* send packets are just round-robined by sequence number, so for now
     146             :        just indicate where they came from so they don't bounce back */
     147           0 :     ulong sig = fd_disco_netmux_sig( ip_dst, 0U, ip_dst, DST_PROTO_OUTGOING, FD_NETMUX_SIG_MIN_HDR_SZ );
     148             : 
     149           0 :     ulong tspub = (ulong)ctx->now;
     150             : 
     151           0 :     fd_stem_publish( ctx->stem, net_out_link->idx, sig, net_out_link->chunk, sz_l2, 0UL, 0, tspub );
     152           0 :     net_out_link->chunk = fd_dcache_compact_next( net_out_link->chunk, sz_l2, net_out_link->chunk0, net_out_link->wmark );
     153           0 :   }
     154             : 
     155           0 :   if( FD_LIKELY( opt_batch_idx ) ) {
     156           0 :     *opt_batch_idx = batch_cnt;
     157           0 :   }
     158             : 
     159           0 :   return FD_AIO_SUCCESS;
     160           0 : }
     161             : 
     162             : /* QUIC conn management and wrappers */
     163             : 
     164             : fd_quic_conn_t *
     165             : quic_connect( fd_send_tile_ctx_t   * ctx,
     166           0 :               fd_send_conn_entry_t * entry ) {
     167           0 :   uint   dst_ip   = entry->ip4_addr;
     168           0 :   ushort dst_port = entry->udp_port;
     169             : 
     170           0 :   FD_TEST( entry->conn == NULL );
     171             : 
     172           0 :   fd_quic_conn_t * conn = fd_quic_connect( ctx->quic, dst_ip, dst_port, ctx->src_ip_addr, ctx->src_port );
     173           0 :   if( FD_UNLIKELY( !conn ) ) {
     174           0 :     ctx->metrics.quic_conn_create_failed++;
     175           0 :     FD_LOG_WARNING(( "send_tile: Failed to create QUIC connection to %u.%u.%u.%u:%u", dst_ip&0xFF, (dst_ip>>8)&0xFF, (dst_ip>>16)&0xFF, (dst_ip>>24)&0xFF, dst_port ));
     176           0 :     return NULL;
     177           0 :   }
     178             : 
     179           0 :   FD_LOG_DEBUG(("send_tile: Quic conn created: %p to peer %u.%u.%u.%u:%u", (void*)conn, dst_ip&0xFF, (dst_ip>>8)&0xFF, (dst_ip>>16)&0xFF, (dst_ip>>24)&0xFF, dst_port));
     180             : 
     181           0 :   entry->conn = conn;
     182           0 :   fd_quic_conn_set_context( conn, entry );
     183             : 
     184           0 :   return conn;
     185           0 : }
     186             : 
     187             : /* get_quic_conn looks up a QUIC connection for a given pubkey. */
     188             : static fd_quic_conn_t *
     189             : get_quic_conn( fd_send_tile_ctx_t * ctx,
     190           0 :                fd_pubkey_t const *  pubkey ) {
     191           0 :   fd_send_conn_entry_t * entry = fd_send_conn_map_query( ctx->conn_map, *pubkey, NULL );
     192           0 :   if( FD_LIKELY( entry ) ) {
     193           0 :     return entry->conn;
     194           0 :   }
     195           0 :   return NULL;
     196           0 : }
     197             : 
     198             : void
     199             : quic_send( fd_send_tile_ctx_t  *  ctx,
     200             :            fd_pubkey_t const   *  pubkey,
     201             :            uchar const         *  payload,
     202           0 :            ulong                  payload_sz ) {
     203             : 
     204           0 :   fd_quic_conn_t * conn = get_quic_conn( ctx, pubkey );
     205           0 :   if( FD_UNLIKELY( !conn ) ) {
     206           0 :     ctx->metrics.quic_send_result_cnt[FD_METRICS_ENUM_TXN_QUIC_SEND_RESULT_V_NO_CONN_IDX]++;
     207           0 :     FD_LOG_DEBUG(("send_tile: Quic conn not found for leader %s", FD_BASE58_ENC_32_ALLOCA( pubkey->key )));
     208           0 :     return;
     209           0 :   }
     210             : 
     211           0 :   fd_quic_stream_t * stream = fd_quic_conn_new_stream( conn );
     212           0 :   if( FD_UNLIKELY( !stream ) ) {
     213           0 :     ctx->metrics.quic_send_result_cnt[FD_METRICS_ENUM_TXN_QUIC_SEND_RESULT_V_NO_STREAM_IDX]++;
     214           0 :     FD_LOG_DEBUG(("send_tile: Quic stream unavailable for leader %s", FD_BASE58_ENC_32_ALLOCA( pubkey->key )));
     215           0 :     return;
     216           0 :   }
     217             : 
     218           0 :   FD_LOG_DEBUG(("send_tile: Sending txn to leader %s", FD_BASE58_ENC_32_ALLOCA( pubkey->key )));
     219           0 :   ctx->metrics.quic_send_result_cnt[FD_METRICS_ENUM_TXN_QUIC_SEND_RESULT_V_SUCCESS_IDX]++;
     220             : 
     221           0 :   fd_quic_stream_send( stream, payload, payload_sz, 1 );
     222           0 : }
     223             : 
     224             : 
     225             : /* handle_new_contact_info handles a new contact. Validates contact info
     226             :    and starts/restarts a connection if necessary. */
     227             : static inline void
     228             : handle_new_contact_info( fd_send_tile_ctx_t   * ctx,
     229           0 :                          fd_shred_dest_wire_t * contact ) {
     230           0 :   uint    new_ip   = contact->ip4_addr;
     231           0 :   ushort  new_port = contact->udp_port;
     232           0 :   if( FD_UNLIKELY( new_ip==0 || new_port==0 ) ) {
     233           0 :     ctx->metrics.new_contact_info[FD_METRICS_ENUM_NEW_CONTACT_OUTCOME_V_UNROUTABLE_IDX]++;
     234           0 :     return;
     235           0 :   }
     236             : 
     237           0 :   fd_send_conn_entry_t * entry  = fd_send_conn_map_query( ctx->conn_map, *contact->pubkey, NULL );
     238           0 :   if( FD_UNLIKELY( !entry ) ) {
     239             :     /* Skip if UNSTAKED */
     240           0 :     FD_LOG_DEBUG(("send_tile: Skipping unstaked pubkey %s at %u.%u.%u.%u:%u", FD_BASE58_ENC_32_ALLOCA( contact->pubkey->key ), new_ip&0xFF, (new_ip>>8)&0xFF, (new_ip>>16)&0xFF, (new_ip>>24)&0xFF, new_port));
     241           0 :     ctx->metrics.new_contact_info[FD_METRICS_ENUM_NEW_CONTACT_OUTCOME_V_UNSTAKED_IDX]++;
     242           0 :     return;
     243           0 :   }
     244             : 
     245           0 :   int info_changed = (entry->ip4_addr != new_ip) | (entry->udp_port != new_port);
     246           0 :   entry->ip4_addr  = new_ip;
     247           0 :   entry->udp_port  = new_port;
     248             : 
     249           0 :   if( entry->conn && !info_changed ) {
     250           0 :     ctx->metrics.new_contact_info[FD_METRICS_ENUM_NEW_CONTACT_OUTCOME_V_NO_CHANGE_IDX]++;
     251           0 :     return;
     252           0 :   }
     253             : 
     254           0 :   if( FD_UNLIKELY( entry->conn && info_changed ) ) {
     255             :     /* close conn, will restart with new contact info */
     256           0 :     fd_quic_conn_close( entry->conn, 0 );
     257           0 :     entry->conn = NULL;
     258           0 :     ctx->metrics.new_contact_info[FD_METRICS_ENUM_NEW_CONTACT_OUTCOME_V_CHANGED_IDX]++;
     259           0 :   }
     260             : 
     261           0 :   entry->last_ci_ticks = ctx->now;
     262           0 :   quic_connect( ctx, entry );
     263           0 :   ctx->metrics.new_contact_info[FD_METRICS_ENUM_NEW_CONTACT_OUTCOME_V_CONNECT_IDX]++;
     264           0 :   return;
     265           0 : }
     266             : 
     267             : static inline void
     268           0 : finalize_new_cluster_contact_info( fd_send_tile_ctx_t * ctx ) {
     269           0 :   for( ulong i=0UL; i<ctx->contact_cnt; i++ ) {
     270           0 :     handle_new_contact_info( ctx, &ctx->contact_buf[i] );
     271           0 :   }
     272           0 : }
     273             : 
     274             : /* Called during after_frag for stake messages. */
     275             : static void
     276           0 : finalize_stake_msg( fd_send_tile_ctx_t * ctx ) {
     277             : 
     278           0 :   fd_multi_epoch_leaders_stake_msg_fini( ctx->mleaders );
     279             : 
     280             :   /* Get the current stake destinations */
     281           0 :   fd_vote_stake_weight_t const * stakes = fd_multi_epoch_leaders_get_stake_weights( ctx->mleaders );
     282           0 :   ulong                       stake_cnt = fd_multi_epoch_leaders_get_stake_cnt( ctx->mleaders );
     283           0 :   if( FD_UNLIKELY( !stakes ) ) {
     284           0 :     FD_LOG_WARNING(( "No stake destinations available for current slot" ));
     285           0 :     return;
     286           0 :   }
     287             : 
     288             :   /* populate staked validators in connection map */
     289           0 :   for( ulong i=0UL; i<stake_cnt; i++ ) {
     290           0 :     fd_vote_stake_weight_t const * stake_info = &stakes[i];
     291           0 :     fd_pubkey_t            const   pubkey     = stake_info->id_key;
     292             : 
     293           0 :     fd_send_conn_entry_t * entry = fd_send_conn_map_query( ctx->conn_map, pubkey, NULL );
     294             :     /* UNSTAKED -> NO_CONN: create new entry in NO_CONN state */
     295           0 :     if( FD_UNLIKELY( !entry ) ) {
     296           0 :       FD_LOG_DEBUG(("send_tile: creating new entry for pubkey %s", FD_BASE58_ENC_32_ALLOCA( pubkey.key )));
     297           0 :       entry = fd_send_conn_map_insert( ctx->conn_map, pubkey );
     298           0 :       entry->conn = NULL;
     299           0 :     }
     300           0 :   }
     301           0 : }
     302             : 
     303             : /* Stem callbacks */
     304             : 
     305             : static inline void
     306             : before_credit( fd_send_tile_ctx_t * ctx,
     307             :                fd_stem_context_t  * stem,
     308           0 :                int *                charge_busy ) {
     309           0 :   ctx->stem = stem;
     310             : 
     311           0 :   ctx->now = fd_tickcount();
     312             :   /* Publishes to mcache via callbacks */
     313           0 :   *charge_busy = fd_quic_service( ctx->quic );
     314           0 : }
     315             : 
     316             : static void
     317             : during_frag( fd_send_tile_ctx_t * ctx,
     318             :              ulong                  in_idx,
     319             :              ulong                  seq FD_PARAM_UNUSED,
     320             :              ulong                  sig FD_PARAM_UNUSED,
     321             :              ulong                  chunk,
     322             :              ulong                  sz,
     323           0 :              ulong                  ctl ) {
     324             : 
     325           0 :   fd_send_link_in_t * in_link = &ctx->in_links[ in_idx ];
     326           0 :   if( FD_UNLIKELY( chunk<in_link->chunk0 || chunk>in_link->wmark ) ) {
     327           0 :     FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu] on link %lu", chunk, sz, in_link->chunk0, in_link->wmark, in_idx ));
     328           0 :   }
     329             : 
     330           0 :   uchar const * dcache_entry = fd_chunk_to_laddr_const( in_link->mem, chunk );
     331           0 :   ulong         kind         = in_link->kind;
     332             : 
     333           0 :   if( FD_UNLIKELY( kind==IN_KIND_NET ) ) {
     334           0 :     void const * src = fd_net_rx_translate_frag( &ctx->net_in_bounds, chunk, ctl, sz );
     335           0 :     fd_memcpy( ctx->quic_buf, src, sz );
     336           0 :   }
     337             : 
     338           0 :   if( FD_UNLIKELY( kind==IN_KIND_STAKE ) ) {
     339           0 :     if( sz>sizeof(fd_stake_weight_t)*(MAX_STAKED_LEADERS+1UL) ) {
     340           0 :       FD_LOG_ERR(( "sz %lu >= max expected stake update size %lu", sz, sizeof(fd_stake_weight_t) * (MAX_STAKED_LEADERS+1UL) ));
     341           0 :     }
     342           0 :     fd_multi_epoch_leaders_stake_msg_init( ctx->mleaders, fd_type_pun_const( dcache_entry ) );
     343           0 :   }
     344             : 
     345           0 :   if( FD_UNLIKELY( kind==IN_KIND_GOSSIP ) ) {
     346           0 :     if( sz>sizeof(fd_shred_dest_wire_t)*MAX_STAKED_LEADERS ) {
     347           0 :       FD_LOG_ERR(( "sz %lu >= max expected gossip update size %lu", sz, sizeof(fd_shred_dest_wire_t) * MAX_STAKED_LEADERS ));
     348           0 :     }
     349           0 :     ctx->contact_cnt = sz / sizeof(fd_shred_dest_wire_t);
     350           0 :     fd_memcpy( ctx->contact_buf, dcache_entry, sz );
     351           0 :   }
     352             : 
     353           0 :   if( FD_UNLIKELY( kind==IN_KIND_TOWER ) ) {
     354           0 :     if( sz!=sizeof(fd_txn_p_t) ) {
     355           0 :       FD_LOG_ERR(( "sz %lu != expected txn size %lu", sz, sizeof(fd_txn_p_t) ));
     356           0 :     }
     357           0 :     fd_memcpy( ctx->txn_buf, dcache_entry, sz );
     358           0 :   }
     359           0 : }
     360             : 
     361             : static void
     362             : after_frag( fd_send_tile_ctx_t * ctx,
     363             :             ulong                in_idx,
     364             :             ulong                seq FD_PARAM_UNUSED,
     365             :             ulong                sig,
     366             :             ulong                sz,
     367             :             ulong                tsorig FD_PARAM_UNUSED,
     368             :             ulong                tspub FD_PARAM_UNUSED,
     369           0 :             fd_stem_context_t *  stem ) {
     370             : 
     371           0 :   ctx->stem = stem;
     372             : 
     373           0 :   fd_send_link_in_t * in_link = &ctx->in_links[ in_idx ];
     374           0 :   ulong                 kind  = in_link->kind;
     375             : 
     376           0 :   if( FD_UNLIKELY( kind==IN_KIND_NET ) ) {
     377           0 :     uchar     * ip_pkt = ctx->quic_buf + sizeof(fd_eth_hdr_t);
     378           0 :     ulong       ip_sz  = sz - sizeof(fd_eth_hdr_t);
     379           0 :     fd_quic_t * quic   = ctx->quic;
     380             : 
     381           0 :     fd_quic_process_packet( quic, ip_pkt, ip_sz );
     382           0 :   }
     383             : 
     384           0 :   if( FD_UNLIKELY( kind==IN_KIND_TOWER ) ) {
     385             : 
     386           0 :     fd_txn_p_t * txn = (fd_txn_p_t *)fd_type_pun(ctx->txn_buf);
     387             : 
     388             :     /* sign the txn */
     389           0 :     uchar * signature = txn->payload + TXN(txn)->signature_off;
     390           0 :     uchar * message   = txn->payload + TXN(txn)->message_off;
     391           0 :     ulong message_sz  = txn->payload_sz - TXN(txn)->message_off;
     392           0 :     fd_keyguard_client_sign( ctx->keyguard_client, signature, message, message_sz, FD_KEYGUARD_SIGN_TYPE_ED25519 );
     393             : 
     394           0 :     ulong poh_slot = sig;
     395             : 
     396             :     /* send to leader for next few slots */
     397           0 :     for( ulong i=0UL; i<SEND_TO_LEADER_CNT; i++ ) {
     398           0 :       fd_pubkey_t const * leader = fd_multi_epoch_leaders_get_leader_for_slot( ctx->mleaders, poh_slot );
     399           0 :       if( FD_LIKELY( leader ) ) {
     400           0 :         quic_send( ctx, leader, txn->payload, txn->payload_sz );
     401           0 :       } else {
     402           0 :         ctx->metrics.leader_not_found++;
     403           0 :         FD_LOG_DEBUG(("send_tile: Failed to get leader contact"));
     404           0 :       }
     405           0 :     }
     406             : 
     407             :     /* send to gossip and dedup */
     408           0 :     fd_send_link_out_t * gossip_verify_out = ctx->gossip_verify_out;
     409           0 :     uchar * msg_to_gossip = fd_chunk_to_laddr( gossip_verify_out->mem, gossip_verify_out->chunk );
     410           0 :     fd_memcpy( msg_to_gossip, txn->payload, txn->payload_sz );
     411           0 :     fd_stem_publish( stem, gossip_verify_out->idx, 1UL, gossip_verify_out->chunk, txn->payload_sz, 0UL, 0, 0 );
     412           0 :     gossip_verify_out->chunk = fd_dcache_compact_next( gossip_verify_out->chunk, txn->payload_sz, gossip_verify_out->chunk0,
     413           0 :         gossip_verify_out->wmark );
     414           0 :   }
     415             : 
     416           0 :   if( FD_UNLIKELY( kind==IN_KIND_GOSSIP ) ) {
     417           0 :     finalize_new_cluster_contact_info( ctx );
     418           0 :     return;
     419           0 :   }
     420             : 
     421           0 :   if( FD_UNLIKELY( kind==IN_KIND_STAKE ) ) {
     422           0 :     finalize_stake_msg( ctx );
     423           0 :     return;
     424           0 :   }
     425           0 : }
     426             : 
     427             : static void
     428             : privileged_init( fd_topo_t *      topo,
     429           0 :                  fd_topo_tile_t * tile ) {
     430           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     431             : 
     432           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     433           0 :   fd_send_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_send_tile_ctx_t), sizeof(fd_send_tile_ctx_t) );
     434           0 :   fd_memset( ctx, 0, sizeof(fd_send_tile_ctx_t) );
     435             : 
     436           0 :   if( FD_UNLIKELY( !strcmp( tile->send.identity_key_path, "" ) ) )
     437           0 :     FD_LOG_ERR(( "identity_key_path not set" ));
     438             : 
     439           0 :   ctx->identity_key[ 0 ] = *(fd_pubkey_t const *)(fd_keyload_load( tile->send.identity_key_path, /* pubkey only: */ 1 ) );
     440           0 :   FD_LOG_NOTICE(( "identity_key: %s", FD_BASE58_ENC_32_ALLOCA( ctx->identity_key[ 0 ].key ) ));
     441           0 : }
     442             : 
     443             : static fd_send_link_in_t *
     444             : setup_input_link( fd_send_tile_ctx_t * ctx,
     445             :                   fd_topo_t          * topo,
     446             :                   fd_topo_tile_t     * tile,
     447             :                   ulong                kind,
     448           0 :                   const char         * name ) {
     449           0 :   ulong in_idx = fd_topo_find_tile_in_link( topo, tile, name, 0 );
     450           0 :   FD_TEST( in_idx!=ULONG_MAX );
     451           0 :   fd_topo_link_t * in_link = &topo->links[ tile->in_link_id[ in_idx ] ];
     452           0 :   fd_send_link_in_t * in_link_desc = &ctx->in_links[ in_idx ];
     453           0 :   in_link_desc->mem    = topo->workspaces[ topo->objs[ in_link->dcache_obj_id ].wksp_id ].wksp;
     454           0 :   in_link_desc->chunk0 = fd_dcache_compact_chunk0( in_link_desc->mem, in_link->dcache );
     455           0 :   in_link_desc->wmark  = fd_dcache_compact_wmark( in_link_desc->mem, in_link->dcache, in_link->mtu );
     456           0 :   in_link_desc->dcache = in_link->dcache;
     457           0 :   in_link_desc->kind   = kind;
     458           0 :   return in_link_desc;
     459           0 : }
     460             : 
     461             : static void
     462             : setup_output_link( fd_send_link_out_t * desc,
     463             :                    fd_topo_t           * topo,
     464             :                    fd_topo_tile_t      * tile,
     465           0 :                    const char          * name ) {
     466           0 :   ulong out_idx = fd_topo_find_tile_out_link( topo, tile, name, 0 );
     467           0 :   FD_TEST( out_idx!=ULONG_MAX );
     468           0 :   fd_topo_link_t * out_link = &topo->links[ tile->out_link_id[ out_idx ] ];
     469           0 :   desc->idx    = out_idx;
     470           0 :   desc->mcache = out_link->mcache;
     471           0 :   desc->sync   = fd_mcache_seq_laddr( desc->mcache );
     472           0 :   desc->depth  = fd_mcache_depth( desc->mcache );
     473           0 :   desc->mem    = topo->workspaces[ topo->objs[ out_link->dcache_obj_id ].wksp_id ].wksp;
     474           0 :   desc->chunk0 = fd_dcache_compact_chunk0( desc->mem, out_link->dcache );
     475           0 :   desc->wmark  = fd_dcache_compact_wmark( desc->mem, out_link->dcache, out_link->mtu );
     476           0 :   desc->chunk  = desc->chunk0;
     477           0 : }
     478             : 
     479             : static void
     480             : unprivileged_init( fd_topo_t *      topo,
     481           0 :                    fd_topo_tile_t * tile ) {
     482           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     483             : 
     484           0 :   if( FD_UNLIKELY( !tile->out_cnt ) ) FD_LOG_ERR(( "send has no primary output link" ));
     485             : 
     486             :   /* Scratch mem setup */
     487           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     488           0 :   fd_send_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_send_tile_ctx_t), sizeof(fd_send_tile_ctx_t) );
     489             : 
     490           0 :   ctx->mleaders = fd_multi_epoch_leaders_join( fd_multi_epoch_leaders_new( ctx->mleaders_mem ) );
     491           0 :   FD_TEST( ctx->mleaders );
     492             : 
     493           0 :   fd_aio_t * quic_tx_aio = fd_aio_join( fd_aio_new( ctx->quic_tx_aio, ctx, quic_tx_aio_send ) );
     494           0 :   if( FD_UNLIKELY( !quic_tx_aio ) ) FD_LOG_ERR(( "fd_aio_join failed" ));
     495             : 
     496           0 :   fd_quic_t * quic = fd_quic_join( fd_quic_new( FD_SCRATCH_ALLOC_APPEND( l, fd_quic_align(), fd_quic_footprint( &quic_limits ) ), &quic_limits ) );
     497           0 :   if( FD_UNLIKELY( !quic ) ) FD_LOG_ERR(( "fd_quic_new failed" ));
     498             : 
     499           0 :   quic->config.role          = FD_QUIC_ROLE_CLIENT;
     500           0 :   quic->config.idle_timeout  = QUIC_IDLE_TIMEOUT_NS;
     501           0 :   quic->config.ack_delay     = QUIC_ACK_DELAY_NS;
     502           0 :   quic->config.keep_alive    = 1;
     503           0 :   quic->config.sign          = quic_tls_cv_sign;
     504           0 :   quic->config.sign_ctx      = ctx;
     505           0 :   fd_memcpy( quic->config.identity_public_key, ctx->identity_key, sizeof(ctx->identity_key) );
     506             : 
     507           0 :   quic->cb.conn_hs_complete  = quic_hs_complete;
     508           0 :   quic->cb.conn_final        = quic_conn_final;
     509           0 :   quic->cb.now               = quic_now;
     510           0 :   quic->cb.now_ctx           = ctx;
     511           0 :   quic->cb.quic_ctx          = ctx;
     512             : 
     513           0 :   fd_quic_set_aio_net_tx( quic, quic_tx_aio );
     514           0 :   fd_quic_set_clock_tickcount( quic );
     515           0 :   FD_TEST_CUSTOM( fd_quic_init( quic ), "fd_quic_init failed" );
     516             : 
     517           0 :   ctx->quic = quic;
     518             : 
     519             :   /* Initialize connection map */
     520           0 :   void * conn_map_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_send_conn_map_align(), fd_send_conn_map_footprint() );
     521           0 :   ctx->conn_map = fd_send_conn_map_join( fd_send_conn_map_new( conn_map_mem ) );
     522           0 :   if( FD_UNLIKELY( !ctx->conn_map ) ) FD_LOG_ERR(( "fd_send_conn_map_join failed" ));
     523             : 
     524           0 :   ctx->src_ip_addr = tile->send.ip_addr;
     525           0 :   ctx->src_port    = tile->send.send_src_port;
     526           0 :   fd_ip4_udp_hdr_init( ctx->packet_hdr, FD_TXN_MTU, ctx->src_ip_addr, ctx->src_port );
     527             : 
     528           0 :   setup_input_link( ctx, topo, tile, IN_KIND_GOSSIP, "gossip_send" );
     529           0 :   setup_input_link( ctx, topo, tile, IN_KIND_STAKE,  "stake_out"   );
     530           0 :   setup_input_link( ctx, topo, tile, IN_KIND_TOWER,  "tower_send"  );
     531             : 
     532           0 :   fd_send_link_in_t * net_in = setup_input_link( ctx, topo, tile, IN_KIND_NET, "net_send" );
     533           0 :   fd_net_rx_bounds_init( &ctx->net_in_bounds, net_in->dcache );
     534             : 
     535           0 :   setup_output_link( ctx->gossip_verify_out, topo, tile, "send_txns" );
     536           0 :   setup_output_link( ctx->net_out,           topo, tile, "send_net"  );
     537             : 
     538             :   /* Set up keyguard(s) */
     539           0 :   ulong             sign_in_idx  =  fd_topo_find_tile_in_link(  topo, tile, "sign_send", 0 );
     540           0 :   ulong             sign_out_idx =  fd_topo_find_tile_out_link( topo, tile, "send_sign", 0 );
     541           0 :   fd_topo_link_t  * sign_in      =  &topo->links[ tile->in_link_id[  sign_in_idx  ] ];
     542           0 :   fd_topo_link_t  * sign_out     =  &topo->links[ tile->out_link_id[ sign_out_idx ] ];
     543             : 
     544           0 :   if ( fd_keyguard_client_join( fd_keyguard_client_new( ctx->keyguard_client,
     545           0 :                                                             sign_out->mcache,
     546           0 :                                                             sign_out->dcache,
     547           0 :                                                             sign_in->mcache,
     548           0 :                                                             sign_in->dcache ) )==NULL ) {
     549           0 :     FD_LOG_ERR(( "Keyguard join failed" ));
     550           0 :   }
     551             : 
     552             :   /* init metrics */
     553           0 :   fd_memset( &ctx->metrics, 0, sizeof(ctx->metrics) );
     554           0 :   fd_histf_join( fd_histf_new( ctx->metrics.sign_duration,
     555           0 :                                   FD_MHIST_SECONDS_MIN( SEND, SIGN_DURATION_SECONDS    ),
     556           0 :                                   FD_MHIST_SECONDS_MAX( SEND, SIGN_DURATION_SECONDS    ) ) );
     557             : 
     558             :   /* Call new/join here rather than in fd_quic so min/max can differ across uses */
     559           0 :   fd_histf_join( fd_histf_new( quic->metrics.service_duration,
     560           0 :                                   FD_MHIST_SECONDS_MIN( SEND, SERVICE_DURATION_SECONDS ),
     561           0 :                                   FD_MHIST_SECONDS_MAX( SEND, SERVICE_DURATION_SECONDS ) ) );
     562           0 :   fd_histf_join( fd_histf_new( quic->metrics.receive_duration,
     563           0 :                                   FD_MHIST_SECONDS_MIN( SEND, RECEIVE_DURATION_SECONDS ),
     564           0 :                                   FD_MHIST_SECONDS_MAX( SEND, RECEIVE_DURATION_SECONDS ) ) );
     565             : 
     566           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
     567           0 :   if( FD_UNLIKELY( scratch_top != (ulong)scratch + scratch_footprint( tile ) ) ) {
     568           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
     569           0 :   }
     570           0 : }
     571             : 
     572             : static ulong
     573             : populate_allowed_seccomp( fd_topo_t      const * topo FD_PARAM_UNUSED,
     574             :                           fd_topo_tile_t const * tile FD_PARAM_UNUSED,
     575             :                           ulong                  out_cnt,
     576           0 :                           struct sock_filter   * out ) {
     577             : 
     578           0 :   populate_sock_filter_policy_fd_send_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
     579           0 :   return sock_filter_policy_fd_send_tile_instr_cnt;
     580           0 : }
     581             : 
     582             : static ulong
     583             : populate_allowed_fds( fd_topo_t const *      topo,
     584             :                       fd_topo_tile_t const * tile,
     585             :                       ulong                  out_fds_cnt,
     586           0 :                       int *                  out_fds ) {
     587           0 :   (void)topo;
     588           0 :   (void)tile;
     589             : 
     590           0 :   if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     591             : 
     592           0 :   ulong out_cnt = 0;
     593           0 :   out_fds[ out_cnt++ ] = 2UL; /* stderr */
     594           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     595           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     596           0 :   return out_cnt;
     597           0 : }
     598             : 
     599             : static void
     600           0 : metrics_write( fd_send_tile_ctx_t * ctx ) {
     601             : 
     602           0 :   FD_MCNT_SET( SEND, LEADER_NOT_FOUND,        ctx->metrics.leader_not_found        );
     603           0 :   FD_MCNT_SET( SEND, CONTACT_STALE,           ctx->metrics.contact_stale           );
     604           0 :   FD_MCNT_SET( SEND, QUIC_CONN_CREATE_FAILED, ctx->metrics.quic_conn_create_failed );
     605             : 
     606           0 :   FD_MCNT_ENUM_COPY( SEND, NEW_CONTACT_INFO, ctx->metrics.new_contact_info );
     607           0 :   FD_MCNT_ENUM_COPY( SEND, QUIC_SEND_RESULT, ctx->metrics.quic_send_result_cnt );
     608             : 
     609           0 :   FD_MHIST_COPY( SEND, SIGN_DURATION_SECONDS, ctx->metrics.sign_duration );
     610             : 
     611             :   /* QUIC metrics */
     612           0 :   FD_MCNT_SET(       SEND, RECEIVED_BYTES,         ctx->quic->metrics.net_rx_byte_cnt );
     613           0 :   FD_MCNT_ENUM_COPY( SEND, RECEIVED_FRAMES,        ctx->quic->metrics.frame_rx_cnt );
     614           0 :   FD_MCNT_SET(       SEND, RECEIVED_PACKETS,       ctx->quic->metrics.net_rx_pkt_cnt );
     615           0 :   FD_MCNT_SET(       SEND, STREAM_RECEIVED_BYTES,  ctx->quic->metrics.stream_rx_byte_cnt );
     616           0 :   FD_MCNT_SET(       SEND, STREAM_RECEIVED_EVENTS, ctx->quic->metrics.stream_rx_event_cnt );
     617             : 
     618           0 :   FD_MCNT_SET(       SEND, SENT_PACKETS,     ctx->quic->metrics.net_tx_pkt_cnt );
     619           0 :   FD_MCNT_SET(       SEND, SENT_BYTES,       ctx->quic->metrics.net_tx_byte_cnt );
     620           0 :   FD_MCNT_SET(       SEND, RETRY_SENT,       ctx->quic->metrics.retry_tx_cnt );
     621           0 :   FD_MCNT_ENUM_COPY( SEND, ACK_TX,           ctx->quic->metrics.ack_tx );
     622             : 
     623           0 :   FD_MGAUGE_ENUM_COPY( SEND, CONNECTIONS_STATE,     ctx->quic->metrics.conn_state_cnt );
     624           0 :   FD_MGAUGE_SET( SEND, CONNECTIONS_ALLOC,           ctx->quic->metrics.conn_alloc_cnt );
     625           0 :   FD_MCNT_SET(   SEND, CONNECTIONS_CREATED,         ctx->quic->metrics.conn_created_cnt );
     626           0 :   FD_MCNT_SET(   SEND, CONNECTIONS_CLOSED,          ctx->quic->metrics.conn_closed_cnt );
     627           0 :   FD_MCNT_SET(   SEND, CONNECTIONS_ABORTED,         ctx->quic->metrics.conn_aborted_cnt );
     628           0 :   FD_MCNT_SET(   SEND, CONNECTIONS_TIMED_OUT,       ctx->quic->metrics.conn_timeout_cnt );
     629           0 :   FD_MCNT_SET(   SEND, CONNECTIONS_RETRIED,         ctx->quic->metrics.conn_retry_cnt );
     630           0 :   FD_MCNT_SET(   SEND, CONNECTION_ERROR_NO_SLOTS,   ctx->quic->metrics.conn_err_no_slots_cnt );
     631           0 :   FD_MCNT_SET(   SEND, CONNECTION_ERROR_RETRY_FAIL, ctx->quic->metrics.conn_err_retry_fail_cnt );
     632             : 
     633           0 :   FD_MCNT_ENUM_COPY( SEND, PKT_CRYPTO_FAILED,       ctx->quic->metrics.pkt_decrypt_fail_cnt );
     634           0 :   FD_MCNT_ENUM_COPY( SEND, PKT_NO_KEY,              ctx->quic->metrics.pkt_no_key_cnt );
     635           0 :   FD_MCNT_SET(       SEND, PKT_NO_CONN,             ctx->quic->metrics.pkt_no_conn_cnt );
     636           0 :   FD_MCNT_ENUM_COPY( SEND, FRAME_TX_ALLOC,          ctx->quic->metrics.frame_tx_alloc_cnt );
     637           0 :   FD_MCNT_SET(       SEND, PKT_NET_HEADER_INVALID,  ctx->quic->metrics.pkt_net_hdr_err_cnt );
     638           0 :   FD_MCNT_SET(       SEND, PKT_QUIC_HEADER_INVALID, ctx->quic->metrics.pkt_quic_hdr_err_cnt );
     639           0 :   FD_MCNT_SET(       SEND, PKT_UNDERSZ,             ctx->quic->metrics.pkt_undersz_cnt );
     640           0 :   FD_MCNT_SET(       SEND, PKT_OVERSZ,              ctx->quic->metrics.pkt_oversz_cnt );
     641           0 :   FD_MCNT_SET(       SEND, PKT_VERNEG,              ctx->quic->metrics.pkt_verneg_cnt );
     642           0 :   FD_MCNT_SET(       SEND, PKT_RETRANSMISSIONS,     ctx->quic->metrics.pkt_retransmissions_cnt );
     643             : 
     644           0 :   FD_MCNT_SET(   SEND, HANDSHAKES_CREATED,          ctx->quic->metrics.hs_created_cnt );
     645           0 :   FD_MCNT_SET(   SEND, HANDSHAKE_ERROR_ALLOC_FAIL,  ctx->quic->metrics.hs_err_alloc_fail_cnt );
     646           0 :   FD_MCNT_SET(   SEND, HANDSHAKE_EVICTED,           ctx->quic->metrics.hs_evicted_cnt );
     647             : 
     648           0 :   FD_MCNT_SET(   SEND, FRAME_FAIL_PARSE,            ctx->quic->metrics.frame_rx_err_cnt );
     649             : 
     650           0 :   FD_MHIST_COPY( SEND, SERVICE_DURATION_SECONDS,    ctx->quic->metrics.service_duration );
     651           0 :   FD_MHIST_COPY( SEND, RECEIVE_DURATION_SECONDS,    ctx->quic->metrics.receive_duration );
     652           0 : }
     653             : 
     654             : 
     655           0 : #define STEM_BURST                  1UL /* send_txns */
     656             : 
     657           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_send_tile_ctx_t
     658           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_send_tile_ctx_t)
     659             : 
     660           0 : #define STEM_CALLBACK_DURING_FRAG   during_frag
     661           0 : #define STEM_CALLBACK_AFTER_FRAG    after_frag
     662           0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
     663           0 : #define STEM_CALLBACK_BEFORE_CREDIT before_credit
     664             : #include "../../disco/stem/fd_stem.c"
     665             : 
     666             : fd_topo_run_tile_t fd_tile_send = {
     667             :   .name                     = "send",
     668             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     669             :   .populate_allowed_fds     = populate_allowed_fds,
     670             :   .scratch_align            = scratch_align,
     671             :   .scratch_footprint        = scratch_footprint,
     672             :   .privileged_init          = privileged_init,
     673             :   .unprivileged_init        = unprivileged_init,
     674             :   .run                      = stem_run,
     675             : };

Generated by: LCOV version 1.14