LCOV - code coverage report
Current view: top level - discof/send - fd_send_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 554 0.0 %
Date: 2025-09-19 04:41:14 Functions: 0 28 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 "../../disco/fd_txn_m_t.h"
       5             : #include "../../disco/keyguard/fd_keyguard.h"
       6             : #include "../../discof/tower/fd_tower_tile.h"
       7             : #include "generated/fd_send_tile_seccomp.h"
       8             : #include "../../flamenco/gossip/fd_gossip_types.h"
       9             : 
      10             : #include <sys/random.h>
      11             : 
      12             : /* 'Staleness' is currently just for debugging - we don't act on it */
      13           0 : #define CONTACT_INFO_STALE_TICKS (120e9L) /* ~60 seconds */
      14             : 
      15             : /* map leader pubkey to contact/conn info
      16             :    A map entry is created only for staked peers. On receiving contact info, we update
      17             :    the map entry with the following 4 sockets from the contact info:
      18             :     - QUIC_VOTE
      19             :     - QUIC_TPU
      20             :     - UDP_VOTE
      21             :     - UDP_TPU
      22             : 
      23             :    For the UDP ports, we simply send to that sockaddr when the leader is selected.
      24             :    For QUIC ports, we establish a quic connection just in time for the leader slot.
      25             :    We allow that connection to time out if it's going to be dormant for a while.
      26             :    This reduces the bandwidth consumed and the amount of work the fd_quic needs to do.
      27             : */
      28             : 
      29             : #define MAP_NAME               fd_send_conn_map
      30           0 : #define MAP_T                  fd_send_conn_entry_t
      31           0 : #define MAP_LG_SLOT_CNT        17
      32           0 : #define MAP_KEY                pubkey
      33           0 : #define MAP_KEY_T              fd_pubkey_t
      34           0 : #define MAP_KEY_NULL           (fd_pubkey_t){0}
      35           0 : #define MAP_KEY_EQUAL(k0,k1)   (!(memcmp((k0).key,(k1).key,sizeof(fd_pubkey_t))))
      36           0 : #define MAP_KEY_INVAL(k)       (MAP_KEY_EQUAL((k),MAP_KEY_NULL))
      37             : #define MAP_KEY_EQUAL_IS_SLOW  1
      38           0 : #define MAP_KEY_HASH(key)      ((key).ui[3])
      39             : #include "../../util/tmpl/fd_map.c"
      40             : 
      41             : fd_quic_limits_t quic_limits = {
      42             :   .conn_cnt                    = MAX_STAKED_LEADERS,
      43             :   .handshake_cnt               = MAX_STAKED_LEADERS,
      44             :   .conn_id_cnt                 = FD_QUIC_MIN_CONN_ID_CNT,
      45             :   .inflight_frame_cnt          = 16UL * MAX_STAKED_LEADERS,
      46             :   .min_inflight_frame_cnt_conn = 4UL,
      47             :   .stream_id_cnt               = 64UL,
      48             :   .tx_buf_sz                   = 1UL<<11,
      49             :   .stream_pool_cnt             = 1UL<<13
      50             : };
      51             : 
      52             : FD_FN_CONST static inline ulong
      53           0 : scratch_align( void ) {
      54           0 :   return fd_ulong_max( fd_ulong_max( 128UL, fd_quic_align() ), fd_send_conn_map_align() );
      55           0 : }
      56             : 
      57             : FD_FN_PURE static inline ulong
      58           0 : scratch_footprint( fd_topo_tile_t const * tile FD_PARAM_UNUSED) {
      59           0 :   ulong l = FD_LAYOUT_INIT;
      60           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_send_tile_ctx_t), sizeof(fd_send_tile_ctx_t) );
      61           0 :   l = FD_LAYOUT_APPEND( l, fd_quic_align(), fd_quic_footprint( &quic_limits ) );
      62           0 :   l = FD_LAYOUT_APPEND( l, fd_send_conn_map_align(), fd_send_conn_map_footprint() );
      63           0 :   return FD_LAYOUT_FINI( l, scratch_align() );
      64           0 : }
      65             : 
      66             : /* QUIC callbacks */
      67             : 
      68             : static void
      69             : quic_tls_cv_sign( void *      signer_ctx,
      70             :                   uchar       signature[ static 64 ],
      71           0 :                   uchar const payload[ static 130 ] ) {
      72           0 :   fd_send_tile_ctx_t * ctx = signer_ctx;
      73           0 :   long dt = -fd_tickcount();
      74           0 :   fd_keyguard_client_sign( ctx->keyguard_client, signature, payload, 130UL, FD_KEYGUARD_SIGN_TYPE_ED25519 );
      75           0 :   dt += ( ctx->now = fd_tickcount() );
      76           0 :   fd_histf_sample( ctx->metrics.sign_duration, (ulong)dt );
      77           0 : }
      78             : 
      79             : /* quic_now is used by quic to get current time */
      80             : static ulong
      81           0 : quic_now( void * _ctx ) {
      82           0 :   fd_send_tile_ctx_t const * ctx = fd_type_pun_const( _ctx );
      83           0 :   return (ulong)ctx->now;
      84           0 : }
      85             : 
      86             : /* quic_hs_complete is called when the QUIC handshake is complete
      87             :    It is currently used only for debug logging */
      88             : static void
      89             : quic_hs_complete( fd_quic_conn_t * conn,
      90           0 :                   void *           quic_ctx FD_PARAM_UNUSED ) {
      91           0 :   fd_send_tile_ctx_t * ctx = fd_type_pun( quic_ctx );
      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             : 
      95           0 :   for( ulong i=0; i<FD_SEND_PORT_QUIC_CNT; i++ ) {
      96           0 :     if( entry->conn[i] == conn ) { ctx->metrics.quic_hs_complete[i]++; break; }
      97           0 :   }
      98           0 :   FD_LOG_DEBUG(("send_tile: QUIC handshake complete for leader %s", FD_BASE58_ENC_32_ALLOCA( entry->pubkey.key )));
      99           0 : }
     100             : 
     101             : 
     102             : inline static int
     103           0 : port_idx_is_quic( ulong port_idx ) {
     104           0 :   return (port_idx==FD_SEND_PORT_QUIC_VOTE_IDX) | (port_idx==FD_SEND_PORT_QUIC_TPU_IDX);
     105           0 : }
     106             : 
     107             : /* quic_conn_final is called when the QUIC connection dies. */
     108             : static void
     109             : quic_conn_final( fd_quic_conn_t * conn,
     110           0 :                  void *           quic_ctx ) {
     111           0 :   fd_send_conn_entry_t * entry = fd_type_pun( fd_quic_conn_get_context( conn ) );
     112           0 :   if( FD_UNLIKELY( !entry ) ) {
     113           0 :     FD_LOG_CRIT(( "send_tile: Conn map entry not found in conn_final" ));
     114           0 :   }
     115             : 
     116           0 :   fd_send_tile_ctx_t * ctx = fd_type_pun( quic_ctx );
     117             : 
     118           0 :   ulong clr_idx = ~0UL;
     119           0 :   for( ulong i=0UL; i<FD_SEND_PORT_QUIC_CNT; i++ ) {
     120           0 :     if( entry->conn[i] == conn ) {
     121           0 :       entry->conn[i] = NULL;
     122           0 :       clr_idx = i;
     123           0 :       FD_LOG_DEBUG(("send_tile: Quic conn final: %p to peer " FD_IP4_ADDR_FMT ":%u", (void*)conn, FD_IP4_ADDR_FMT_ARGS(entry->ip4s[i]), entry->ports[i]));
     124           0 :       ctx->metrics.quic_conn_final[i]++;
     125           0 :       break;
     126           0 :     }
     127           0 :   }
     128             : 
     129           0 :   if( FD_UNLIKELY( clr_idx == ~0UL ) ) {
     130           0 :     FD_LOG_CRIT(( "conn not found in entry for peer %s", FD_BASE58_ENC_32_ALLOCA( entry->pubkey.key )));
     131           0 :   }
     132           0 : }
     133             : 
     134             : /* send_to_net sends a packet to the net tile.
     135             :    It takes pointers to the ip4 hdr, udp hdr, and the payload.
     136             :    Always uses the eth hdr from ctx->packet_hdr. */
     137             : static void
     138             : send_to_net( fd_send_tile_ctx_t * ctx,
     139             :              fd_ip4_hdr_t const * ip4_hdr,
     140             :              fd_udp_hdr_t const * udp_hdr,
     141             :              uchar        const * payload,
     142           0 :              ulong                payload_sz ) {
     143             : 
     144           0 :   uint  const ip_dst = FD_LOAD( uint, ip4_hdr->daddr_c );
     145           0 :   ulong const ip_sz  = FD_IP4_GET_LEN( *ip4_hdr );
     146             : 
     147           0 :   fd_send_link_out_t * net_out_link = ctx->net_out;
     148           0 :   uchar * packet_l2 = fd_chunk_to_laddr( net_out_link->mem, net_out_link->chunk );
     149           0 :   uchar * packet_l3 = packet_l2 + sizeof(fd_eth_hdr_t);
     150           0 :   uchar * packet_l4 = packet_l3 + ip_sz;
     151           0 :   uchar * packet_l5 = packet_l4 + sizeof(fd_udp_hdr_t);
     152             : 
     153           0 :   fd_memcpy( packet_l2, ctx->packet_hdr->eth, sizeof(fd_eth_hdr_t) );
     154           0 :   fd_memcpy( packet_l3, ip4_hdr,              ip_sz                );
     155           0 :   fd_memcpy( packet_l4, udp_hdr,              sizeof(fd_udp_hdr_t) );
     156           0 :   fd_memcpy( packet_l5, payload,              payload_sz           );
     157             : 
     158           0 :   FD_LOG_DEBUG(("voting: sending packet to " FD_IP4_ADDR_FMT ":%u", FD_IP4_ADDR_FMT_ARGS(ip_dst), fd_ushort_bswap(udp_hdr->net_dport) ));
     159           0 :   ulong sig   = fd_disco_netmux_sig( ip_dst, 0U, ip_dst, DST_PROTO_OUTGOING, FD_NETMUX_SIG_MIN_HDR_SZ );
     160           0 :   ulong tspub = (ulong)ctx->now;
     161           0 :   ulong sz_l2 = sizeof(fd_eth_hdr_t) + ip_sz + sizeof(fd_udp_hdr_t) + payload_sz;
     162             : 
     163           0 :   fd_stem_publish( ctx->stem, net_out_link->idx, sig, net_out_link->chunk, sz_l2, 0UL, 0, tspub );
     164           0 :   net_out_link->chunk = fd_dcache_compact_next( net_out_link->chunk, sz_l2, net_out_link->chunk0, net_out_link->wmark );
     165           0 : }
     166             : 
     167             : static int
     168             : quic_tx_aio_send( void *                    _ctx,
     169             :                   fd_aio_pkt_info_t const * batch,
     170             :                   ulong                     batch_cnt,
     171             :                   ulong *                   opt_batch_idx,
     172           0 :                   int                       flush FD_PARAM_UNUSED ) {
     173           0 :   fd_send_tile_ctx_t * ctx = _ctx;
     174             : 
     175           0 :   for( ulong i=0; i<batch_cnt; i++ ) {
     176           0 :     if( FD_UNLIKELY( batch[ i ].buf_sz<FD_NETMUX_SIG_MIN_HDR_SZ ) ) continue;
     177           0 :     uchar * buf = batch[ i ].buf;
     178           0 :     fd_ip4_hdr_t * ip4_hdr    = fd_type_pun( buf );
     179           0 :     fd_udp_hdr_t * udp_hdr    = fd_type_pun( buf + sizeof(fd_ip4_hdr_t) );
     180           0 :     uchar        * payload    = buf + sizeof(fd_ip4_hdr_t) + sizeof(fd_udp_hdr_t);
     181           0 :     ulong          payload_sz = batch[ i ].buf_sz - sizeof(fd_ip4_hdr_t) - sizeof(fd_udp_hdr_t);
     182           0 :     send_to_net( ctx, ip4_hdr, udp_hdr, payload, payload_sz );
     183           0 :   }
     184             : 
     185           0 :   if( FD_LIKELY( opt_batch_idx ) ) {
     186           0 :     *opt_batch_idx = batch_cnt;
     187           0 :   }
     188             : 
     189           0 :   return FD_AIO_SUCCESS;
     190           0 : }
     191             : 
     192             : static void
     193           0 : during_housekeeping( fd_send_tile_ctx_t * ctx ) {
     194           0 :   #define MAP_STATS_PERIOD (32UL)
     195           0 :   if( ctx->housekeeping_ctr++ % MAP_STATS_PERIOD == 0UL ) {
     196           0 :     ulong const map_cnt = fd_send_conn_map_slot_cnt();
     197           0 :     ulong staked_no_ci = 0UL;
     198           0 :     ulong stale_ci = 0UL;
     199           0 :     ulong map_real_cnt = 0UL;
     200           0 :     for( ulong i=0UL; i<map_cnt; i++ ) {
     201           0 :       fd_send_conn_entry_t * entry = &ctx->conn_map[i];
     202           0 :       if( !fd_send_conn_map_key_equal( entry->pubkey, fd_send_conn_map_key_null() ) ) {
     203           0 :         map_real_cnt++;
     204           0 :         if( !entry->got_ci_msg ) {
     205           0 :           staked_no_ci++;
     206           0 :         } else if( ctx->now - entry->last_ci_ticks > CONTACT_INFO_STALE_TICKS ) {
     207           0 :           stale_ci++;
     208           0 :         }
     209           0 :       }
     210           0 :     }
     211           0 :     ctx->metrics.staked_no_ci = staked_no_ci;
     212           0 :     ctx->metrics.stale_ci = stale_ci;
     213           0 :     FD_LOG_DEBUG(("send_tile map check: %lu no ci and %lu stale out of %lu staked", staked_no_ci, stale_ci, map_real_cnt ));
     214           0 :   }
     215           0 :   #undef MAP_STATS_PERIOD
     216           0 : }
     217             : 
     218             : /* quic_connect initiates quic connections for a given entry and port. It uses the
     219             :    contact info stored in entry, and points the conn and entry to each other.
     220             :    Returns a handle to the new connection, and NULL if creating it failed */
     221             : 
     222             : static fd_quic_conn_t *
     223             : quic_connect( fd_send_tile_ctx_t   * ctx,
     224             :               fd_send_conn_entry_t * entry,
     225           0 :               ulong                  port_idx ) {
     226             : 
     227           0 :   ulong  conn_idx = port_idx;
     228           0 :   uint   dst_ip   = entry->ip4s[port_idx];
     229           0 :   ushort dst_port = entry->ports[port_idx];
     230             : 
     231           0 :   FD_TEST( entry->conn[conn_idx] == NULL );
     232             : 
     233           0 :   fd_quic_conn_t * conn = fd_quic_connect( ctx->quic, dst_ip, dst_port, ctx->src_ip_addr, ctx->src_port );
     234           0 :   if( FD_UNLIKELY( !conn ) ) {
     235           0 :     FD_LOG_WARNING(( "send_tile: Failed to create QUIC connection to " FD_IP4_ADDR_FMT ":%u", FD_IP4_ADDR_FMT_ARGS(dst_ip), dst_port ));
     236           0 :     return NULL;
     237           0 :   }
     238             : 
     239           0 :   FD_LOG_DEBUG(("send_tile: Quic conn created: %p to peer " FD_IP4_ADDR_FMT ":%u", (void*)conn, FD_IP4_ADDR_FMT_ARGS(dst_ip), dst_port));
     240             : 
     241           0 :   entry->conn[conn_idx] = conn;
     242           0 :   fd_quic_conn_set_context( conn, entry );
     243             : 
     244           0 :   return conn;
     245           0 : }
     246             : 
     247             : /* ensure_conn_for_slot ensures a connection exists for the given pubkey if it's
     248             :    a leader for the target slot. Creates connection if needed. */
     249             : static void
     250             : ensure_conn_for_slot( fd_send_tile_ctx_t * ctx,
     251             :                       ulong                target_slot,
     252           0 :                       ulong                keep_alive_ticks ) {
     253           0 :   fd_pubkey_t const * leader = fd_multi_epoch_leaders_get_leader_for_slot( ctx->mleaders, target_slot );
     254           0 :   if( FD_UNLIKELY( !leader ) ) {
     255             :     /* Track NoLeader outcome for both QUIC ports */
     256           0 :     for( ulong i=0UL; i<FD_SEND_PORT_QUIC_CNT; i++ ) {
     257           0 :       ctx->metrics.ensure_conn_result[i][FD_METRICS_ENUM_SEND_ENSURE_CONN_RESULT_V_NO_LEADER_IDX]++;
     258           0 :     }
     259           0 :     return;
     260           0 :   }
     261             : 
     262           0 :   fd_send_conn_entry_t * entry = fd_send_conn_map_query( ctx->conn_map, *leader, NULL );
     263           0 :   if( FD_UNLIKELY( !entry ) ) FD_LOG_CRIT(( "Tried ensuring conn for unstaked pubkey"));
     264             : 
     265           0 :   for( ulong i=0UL; i<FD_SEND_PORT_QUIC_CNT; i++ ) {
     266           0 :     if( FD_UNLIKELY( !entry->ip4s[i] | !entry->ports[i] ) ) {
     267           0 :       ctx->metrics.ensure_conn_result[i][FD_METRICS_ENUM_SEND_ENSURE_CONN_RESULT_V_NO_CI_IDX]++;
     268           0 :       continue;
     269           0 :     }
     270             : 
     271           0 :     if( !entry->conn[i] ) {
     272             :       /* Attempting to create new connection */
     273           0 :       fd_quic_conn_t * conn  = quic_connect( ctx, entry, i );
     274           0 :       if( FD_UNLIKELY( !conn ) ) continue;
     275           0 :       ctx->metrics.ensure_conn_result[i][FD_METRICS_ENUM_SEND_ENSURE_CONN_RESULT_V_NEW_CONNECTION_IDX]++;
     276           0 :       fd_quic_service( ctx->quic );
     277           0 :     } else {
     278             :       /* Connection already exists */
     279           0 :       ctx->metrics.ensure_conn_result[i][FD_METRICS_ENUM_SEND_ENSURE_CONN_RESULT_V_CONNECTED_IDX]++;
     280           0 :       fd_quic_conn_let_die( entry->conn[i], keep_alive_ticks );
     281           0 :     }
     282           0 :   }
     283           0 : }
     284             : 
     285             : /* leader_send sends a payload to 'pubkey' in all possible ways. For quic targets,
     286             :    it relies on quic connections that are already established. */
     287             : static void
     288             : leader_send( fd_send_tile_ctx_t * ctx,
     289             :              fd_pubkey_t const  * pubkey,
     290             :              uchar const        * payload,
     291           0 :              ulong                payload_sz ) {
     292             : 
     293           0 :   fd_send_conn_entry_t * entry = fd_send_conn_map_query( ctx->conn_map, *pubkey, NULL );
     294           0 :   if( FD_UNLIKELY( !entry ) ) {
     295           0 :     FD_LOG_CRIT(( "Tried looking up connection for an unstaked pubkey"));
     296           0 :   }
     297             : 
     298           0 :   for( ulong i=0UL; i<FD_SEND_PORT_CNT; i++ ) {
     299             :     /* skip unroutable */
     300           0 :     if( !entry->ip4s[i] | !entry->ports[i] ) {
     301           0 :       ctx->metrics.send_result_cnt[i][FD_METRICS_ENUM_TXN_SEND_RESULT_V_NO_CI_IDX]++;
     302           0 :       continue;
     303           0 :     }
     304             : 
     305           0 :     if( port_idx_is_quic( i ) ) {
     306           0 :       fd_quic_conn_t * conn = entry->conn[i];
     307           0 :       if( FD_UNLIKELY( !conn ) ) {
     308           0 :         FD_LOG_DEBUG(("no conn for %s at " FD_IP4_ADDR_FMT ":%u", FD_BASE58_ENC_32_ALLOCA( pubkey->key ), FD_IP4_ADDR_FMT_ARGS(entry->ip4s[i]), entry->ports[i] ));
     309           0 :         ctx->metrics.send_result_cnt[i][FD_METRICS_ENUM_TXN_SEND_RESULT_V_NO_CONN_IDX]++;
     310           0 :         continue;
     311           0 :       }
     312             : 
     313           0 :       fd_quic_stream_t * stream = fd_quic_conn_new_stream( conn );
     314           0 :       if( FD_UNLIKELY( !stream ) ) {
     315           0 :         FD_LOG_DEBUG(("new_stream failed for %s at " FD_IP4_ADDR_FMT ":%u bc conn state was %u", FD_BASE58_ENC_32_ALLOCA( pubkey->key ), FD_IP4_ADDR_FMT_ARGS(entry->ip4s[i]), entry->ports[i], conn->state ));
     316           0 :         ctx->metrics.send_result_cnt[i][FD_METRICS_ENUM_TXN_SEND_RESULT_V_NO_STREAM_IDX]++;
     317           0 :         continue;
     318           0 :       }
     319             : 
     320           0 :       ctx->metrics.send_result_cnt[i][FD_METRICS_ENUM_TXN_SEND_RESULT_V_SUCCESS_IDX]++;
     321             : 
     322           0 :       fd_quic_stream_send( stream, payload, payload_sz, 1 );
     323           0 :       fd_quic_service( ctx->quic ); /* trigger send ASAP */
     324           0 :     } else {
     325             : 
     326           0 :       fd_ip4_hdr_t * ip4_hdr = ctx->packet_hdr->ip4;
     327           0 :       fd_udp_hdr_t * udp_hdr = ctx->packet_hdr->udp;
     328             : 
     329           0 :       ctx->metrics.send_result_cnt[i][FD_METRICS_ENUM_TXN_SEND_RESULT_V_SUCCESS_IDX]++;
     330             : 
     331           0 :       ip4_hdr->daddr       = entry->ip4s[i];
     332           0 :       ip4_hdr->net_tot_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_ip4_hdr_t)+sizeof(fd_udp_hdr_t)) );
     333           0 :       ip4_hdr->net_id      = fd_ushort_bswap( ctx->net_id++ );
     334           0 :       ip4_hdr->check       = 0;
     335           0 :       ip4_hdr->check       = fd_ip4_hdr_check_fast( ip4_hdr );
     336             : 
     337           0 :       udp_hdr->net_dport = fd_ushort_bswap( entry->ports[i] ); /* to net order */
     338           0 :       udp_hdr->net_len   = fd_ushort_bswap( (ushort)( payload_sz + sizeof(fd_udp_hdr_t) ) );
     339           0 :       send_to_net( ctx, ip4_hdr, udp_hdr, payload, payload_sz );
     340           0 :     }
     341           0 :   }
     342           0 : }
     343             : 
     344             : /* handle_contact_info_update handles a new contact. Validates contact info
     345             :    and starts/restarts a connection if necessary. */
     346             : static inline void
     347             : handle_contact_info_update( fd_send_tile_ctx_t *               ctx,
     348           0 :                             fd_gossip_update_message_t const * msg ) {
     349           0 :   static uint const socket_idx[FD_SEND_PORT_CNT] = {
     350           0 :     FD_CONTACT_INFO_SOCKET_TPU_VOTE_QUIC,
     351           0 :     FD_CONTACT_INFO_SOCKET_TPU_QUIC,
     352           0 :     FD_CONTACT_INFO_SOCKET_TPU_VOTE,
     353           0 :     FD_CONTACT_INFO_SOCKET_TPU
     354           0 :   };
     355             : 
     356           0 :   fd_send_conn_entry_t * entry  = fd_send_conn_map_query( ctx->conn_map, *(fd_pubkey_t *)(msg->origin_pubkey), NULL );
     357           0 :   if( FD_UNLIKELY( !entry ) ) {
     358             :     /* Skip if UNSTAKED */
     359           0 :     FD_LOG_DEBUG(("send_tile: Skipping unstaked pubkey %s", FD_BASE58_ENC_32_ALLOCA( msg->origin_pubkey )));
     360           0 :     ctx->metrics.unstaked_ci_rcvd++;
     361           0 :     return;
     362           0 :   }
     363             : 
     364           0 :   for( ulong i=0UL; i<FD_SEND_PORT_CNT; i++ ) {
     365             : 
     366           0 :     fd_ip4_port_t const * socket   = &msg->contact_info.contact_info->sockets[ socket_idx[i] ];
     367           0 :     uint                  new_ip   = socket->addr;
     368           0 :     ushort                new_port = fd_ushort_bswap( socket->port ); /* convert port to host order */
     369           0 :     uint                  old_ip   = entry->ip4s[i];
     370           0 :     ushort                old_port = entry->ports[i];
     371             : 
     372           0 :     if( FD_UNLIKELY( !new_ip || !new_port ) ) {
     373           0 :       FD_LOG_DEBUG(( "send_tile: Unroutable contact info for pubkey %s", FD_BASE58_ENC_32_ALLOCA( msg->origin_pubkey )));
     374           0 :       ctx->metrics.new_contact_info[i][FD_METRICS_ENUM_NEW_CONTACT_OUTCOME_V_UNROUTABLE_IDX]++;
     375           0 :       continue;
     376           0 :     }
     377             : 
     378           0 :     int info_changed    = (old_ip != new_ip) | (old_port != new_port);
     379           0 :     entry->ip4s [i]  = new_ip;
     380           0 :     entry->ports[i] = new_port;
     381             : 
     382           0 :     ulong quic_conn_idx = i;
     383           0 :     if( port_idx_is_quic( i ) && info_changed && entry->conn[quic_conn_idx]!=NULL ) {
     384             :       /* Track connection finalization before closing */
     385           0 :       fd_quic_conn_close( entry->conn[i], 0 );
     386           0 :     }
     387             : 
     388             :     /* bc taking branches for just metrics would be sad */
     389             :     /* !info_changed -> NoChange, info_changed && old_port==0 -> Initialized, info_changed && old_port!=0 -> Changed */
     390           0 :     static ulong metric_idx_map[] = {
     391           0 :       FD_METRICS_ENUM_NEW_CONTACT_OUTCOME_V_NO_CHANGE_IDX,
     392           0 :       FD_METRICS_ENUM_NEW_CONTACT_OUTCOME_V_INITIALIZED_IDX,
     393           0 :       FD_METRICS_ENUM_NEW_CONTACT_OUTCOME_V_CHANGED_IDX
     394           0 :     };
     395           0 :     ulong metric_idx = metric_idx_map[ !!info_changed<<(!!old_port) ];
     396           0 :     ctx->metrics.new_contact_info[i][metric_idx]++;
     397           0 :   }
     398             : 
     399           0 :   entry->got_ci_msg    = 1;
     400           0 :   entry->last_ci_ticks = ctx->now;
     401           0 : }
     402             : 
     403             : static inline void
     404             : handle_contact_info_removal( fd_send_tile_ctx_t *                ctx FD_PARAM_UNUSED,
     405           0 :                               fd_gossip_update_message_t const * msg FD_PARAM_UNUSED ) {
     406           0 :   fd_send_conn_entry_t * entry = fd_send_conn_map_query( ctx->conn_map, *(fd_pubkey_t *)(msg->origin_pubkey), NULL );
     407           0 :   if( FD_LIKELY( entry ) ) {
     408           0 :     for( ulong i=0UL; i<FD_SEND_PORT_QUIC_CNT; i++ ) {
     409           0 :       if( FD_UNLIKELY( entry->conn[i] ) ) fd_quic_conn_close( entry->conn[i], 0 );
     410           0 :       entry->ip4s[i]  = 0;
     411           0 :       entry->ports[i] = 0;
     412           0 :     }
     413           0 :     ctx->metrics.ci_removed++;
     414           0 :   }
     415           0 : }
     416             : 
     417             : /* Called during after_frag for stake messages. */
     418             : static void
     419           0 : finalize_stake_msg( fd_send_tile_ctx_t * ctx ) {
     420             : 
     421           0 :   fd_multi_epoch_leaders_stake_msg_fini( ctx->mleaders );
     422             : 
     423             :   /* Get the current stake destinations */
     424           0 :   fd_vote_stake_weight_t const * stakes = fd_multi_epoch_leaders_get_stake_weights( ctx->mleaders );
     425           0 :   ulong                       stake_cnt = fd_multi_epoch_leaders_get_stake_cnt( ctx->mleaders );
     426           0 :   if( FD_UNLIKELY( !stakes ) ) {
     427           0 :     FD_LOG_WARNING(( "No stake destinations available for current slot" ));
     428           0 :     return;
     429           0 :   }
     430             : 
     431             :   /* populate staked validators in connection map */
     432           0 :   for( ulong i=0UL; i<stake_cnt; i++ ) {
     433           0 :     fd_vote_stake_weight_t const * stake_info = &stakes[i];
     434           0 :     fd_pubkey_t            const   pubkey     = stake_info->id_key;
     435             : 
     436           0 :     fd_send_conn_entry_t * entry = fd_send_conn_map_query( ctx->conn_map, pubkey, NULL );
     437             :     /* UNSTAKED -> NO_CONN: create new entry in NO_CONN state */
     438           0 :     if( FD_UNLIKELY( !entry ) ) {
     439           0 :       FD_LOG_DEBUG(("send_tile: creating new entry for pubkey %s", FD_BASE58_ENC_32_ALLOCA( pubkey.key )));
     440             :       /* insert and initialize entry */
     441           0 :       entry = fd_send_conn_map_insert( ctx->conn_map, pubkey );
     442           0 :       *entry = (fd_send_conn_entry_t){.pubkey = entry->pubkey, .hash = entry->hash };
     443           0 :     }
     444           0 :   }
     445           0 : }
     446             : 
     447             : static void
     448             : handle_vote_msg( fd_send_tile_ctx_t * ctx,
     449             :                  ulong                vote_slot,
     450             :                  uchar *              signed_vote_txn,
     451           0 :                  ulong                vote_txn_sz ) {
     452             : 
     453           0 :   fd_txn_t txn;
     454           0 :   FD_TEST( fd_txn_parse( signed_vote_txn, vote_txn_sz, &txn, NULL ) );
     455             : 
     456             :   /* sign the txn */
     457           0 :   uchar * signature = signed_vote_txn + txn.signature_off;
     458           0 :   uchar const * message   = signed_vote_txn + txn.message_off;
     459           0 :   ulong message_sz  = vote_txn_sz - txn.message_off;
     460           0 :   fd_keyguard_client_sign( ctx->keyguard_client, signature, message, message_sz, FD_KEYGUARD_SIGN_TYPE_ED25519 );
     461             : 
     462           0 :   ulong poh_slot  = vote_slot+1;
     463           0 :   FD_LOG_INFO(("got vote for slot %lu", vote_slot));
     464             : 
     465             :   /* send to leader for next few slots */
     466           0 :   for( ulong i=0UL; i<FD_SEND_TARGET_LEADER_CNT; i++ ) {
     467           0 :     ulong target_slot = poh_slot + i*FD_EPOCH_SLOTS_PER_ROTATION;
     468           0 :     fd_pubkey_t const * leader = fd_multi_epoch_leaders_get_leader_for_slot( ctx->mleaders, target_slot );
     469           0 :     if( FD_LIKELY( leader ) ) {
     470           0 :       leader_send( ctx, leader, signed_vote_txn, vote_txn_sz );
     471           0 :     } else {
     472           0 :       ctx->metrics.leader_not_found++;
     473           0 :       FD_LOG_DEBUG(("send_tile: Failed to get leader contact for slot %lu", target_slot));
     474           0 :     }
     475           0 :   }
     476             : 
     477           0 :   for( ulong i=0; i<FD_SEND_CONNECT_AHEAD_LEADER_CNT; i++ ) {
     478           0 :     ulong connect_slot = poh_slot + i*FD_EPOCH_SLOTS_PER_ROTATION;
     479             :     /* keep alive for at least as long as needed */
     480           0 :     ensure_conn_for_slot( ctx, connect_slot, ctx->ticks_per_sec * FD_SEND_QUIC_MIN_CONN_LIFETIME_SECONDS );
     481           0 :   }
     482             : 
     483             :   /* send to gossip and dedup */
     484           0 :   fd_send_link_out_t * gossip_verify_out = ctx->gossip_verify_out;
     485           0 :   uchar * msg_to_gossip = fd_chunk_to_laddr( gossip_verify_out->mem, gossip_verify_out->chunk );
     486           0 :   fd_txn_m_t * txnm = (fd_txn_m_t *)msg_to_gossip;
     487           0 :   *txnm = (fd_txn_m_t) { 0UL };
     488           0 :   txnm->payload_sz = (ushort)vote_txn_sz;
     489           0 :   txnm->source_ipv4 = ctx->src_ip_addr;
     490           0 :   txnm->source_tpu  = FD_TXN_M_TPU_SOURCE_SEND;
     491           0 :   fd_memcpy( msg_to_gossip+sizeof(fd_txn_m_t), signed_vote_txn, vote_txn_sz );
     492           0 :   ulong msg_sz = fd_txn_m_realized_footprint( txnm, 0, 0 );
     493           0 :   fd_stem_publish( ctx->stem, gossip_verify_out->idx, 1UL, gossip_verify_out->chunk, msg_sz, 0UL, 0, 0 );
     494           0 :   gossip_verify_out->chunk = fd_dcache_compact_next(
     495           0 :     gossip_verify_out->chunk,
     496           0 :     msg_sz,
     497           0 :     gossip_verify_out->chunk0,
     498           0 :     gossip_verify_out->wmark );
     499           0 : }
     500             : 
     501             : /* Stem callbacks */
     502             : 
     503             : static inline void
     504             : before_credit( fd_send_tile_ctx_t * ctx,
     505             :                fd_stem_context_t  * stem,
     506           0 :                int *                charge_busy ) {
     507           0 :   ctx->stem = stem;
     508             : 
     509           0 :   ctx->now = fd_tickcount();
     510             :   /* Publishes to mcache via callbacks */
     511           0 :   *charge_busy = fd_quic_service( ctx->quic );
     512           0 : }
     513             : 
     514             : static inline int
     515             : before_frag( fd_send_tile_ctx_t * ctx,
     516             :              ulong                in_idx,
     517             :              ulong                seq FD_PARAM_UNUSED,
     518           0 :              ulong                sig ) {
     519           0 :   if( FD_UNLIKELY( ctx->in_links[in_idx].kind==IN_KIND_GOSSIP ) ) {
     520           0 :     return sig!=FD_GOSSIP_UPDATE_TAG_CONTACT_INFO &&
     521           0 :            sig!=FD_GOSSIP_UPDATE_TAG_CONTACT_INFO_REMOVE;
     522           0 :   }
     523           0 :   return 0;
     524           0 : }
     525             : 
     526             : static void
     527             : during_frag( fd_send_tile_ctx_t * ctx,
     528             :              ulong                  in_idx,
     529             :              ulong                  seq FD_PARAM_UNUSED,
     530             :              ulong                  sig FD_PARAM_UNUSED,
     531             :              ulong                  chunk,
     532             :              ulong                  sz,
     533           0 :              ulong                  ctl ) {
     534             : 
     535           0 :   fd_send_link_in_t * in_link = &ctx->in_links[ in_idx ];
     536           0 :   if( FD_UNLIKELY( chunk<in_link->chunk0 || chunk>in_link->wmark ) ) {
     537           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 ));
     538           0 :   }
     539             : 
     540           0 :   uchar const * dcache_entry = fd_chunk_to_laddr_const( in_link->mem, chunk );
     541           0 :   ulong         kind         = in_link->kind;
     542             : 
     543           0 :   if( FD_UNLIKELY( kind==IN_KIND_NET ) ) {
     544           0 :     void const * src = fd_net_rx_translate_frag( &ctx->net_in_bounds, chunk, ctl, sz );
     545           0 :     fd_memcpy( ctx->quic_buf, src, sz );
     546           0 :   }
     547             : 
     548           0 :   if( FD_UNLIKELY( kind==IN_KIND_STAKE ) ) {
     549           0 :     if( sz>sizeof(fd_stake_weight_t)*(MAX_STAKED_LEADERS+1UL) ) {
     550           0 :       FD_LOG_ERR(( "sz %lu >= max expected stake update size %lu", sz, sizeof(fd_stake_weight_t) * (MAX_STAKED_LEADERS+1UL) ));
     551           0 :     }
     552           0 :     fd_multi_epoch_leaders_stake_msg_init( ctx->mleaders, fd_type_pun_const( dcache_entry ) );
     553           0 :   }
     554             : 
     555           0 :   if( FD_LIKELY( kind==IN_KIND_GOSSIP ) ) {
     556           0 :     fd_memcpy( ctx->contact_buf, dcache_entry, sz );
     557           0 :   }
     558             : 
     559           0 :   if( FD_UNLIKELY( kind==IN_KIND_TOWER ) ) {
     560           0 :     FD_TEST( sz==sizeof(fd_tower_slot_done_t) );
     561             : 
     562           0 :     fd_tower_slot_done_t const * slot_done = (fd_tower_slot_done_t const *)dcache_entry;
     563             : 
     564           0 :     uchar signed_vote_txn[ FD_TPU_MTU ];
     565           0 :     fd_memcpy( signed_vote_txn, slot_done->vote_txn, slot_done->vote_txn_sz );
     566             : 
     567           0 :     handle_vote_msg( ctx, slot_done->vote_slot, signed_vote_txn, slot_done->vote_txn_sz );
     568           0 :   }
     569           0 : }
     570             : 
     571             : static void
     572             : after_frag( fd_send_tile_ctx_t * ctx,
     573             :             ulong                in_idx,
     574             :             ulong                seq FD_PARAM_UNUSED,
     575             :             ulong                sig FD_PARAM_UNUSED,
     576             :             ulong                sz,
     577             :             ulong                tsorig FD_PARAM_UNUSED,
     578             :             ulong                tspub FD_PARAM_UNUSED,
     579           0 :             fd_stem_context_t *  stem ) {
     580             : 
     581           0 :   ctx->stem = stem;
     582             : 
     583           0 :   fd_send_link_in_t * in_link = &ctx->in_links[ in_idx ];
     584           0 :   ulong                 kind  = in_link->kind;
     585             : 
     586           0 :   if( FD_UNLIKELY( kind==IN_KIND_NET ) ) {
     587           0 :     uchar     * ip_pkt = ctx->quic_buf + sizeof(fd_eth_hdr_t);
     588           0 :     ulong       ip_sz  = sz - sizeof(fd_eth_hdr_t);
     589           0 :     fd_quic_t * quic   = ctx->quic;
     590             : 
     591           0 :     fd_quic_process_packet( quic, ip_pkt, ip_sz );
     592           0 :   }
     593             : 
     594           0 :   if( FD_UNLIKELY( kind==IN_KIND_GOSSIP ) ) {
     595           0 :     fd_gossip_update_message_t const * msg = fd_type_pun_const( ctx->contact_buf );
     596           0 :     if( FD_LIKELY( msg->tag==FD_GOSSIP_UPDATE_TAG_CONTACT_INFO ) ) {
     597           0 :       handle_contact_info_update( ctx, msg );
     598           0 :     } else if ( FD_UNLIKELY( msg->tag==FD_GOSSIP_UPDATE_TAG_CONTACT_INFO_REMOVE ) ) {
     599           0 :       handle_contact_info_removal( ctx, msg );
     600           0 :     }
     601           0 :   }
     602             : 
     603           0 :   if( FD_UNLIKELY( kind==IN_KIND_STAKE ) ) {
     604           0 :     finalize_stake_msg( ctx );
     605           0 :   }
     606           0 : }
     607             : 
     608             : static void
     609             : privileged_init( fd_topo_t *      topo,
     610           0 :                  fd_topo_tile_t * tile ) {
     611           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     612             : 
     613           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     614           0 :   fd_send_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_send_tile_ctx_t), sizeof(fd_send_tile_ctx_t) );
     615           0 :   fd_memset( ctx, 0, sizeof(fd_send_tile_ctx_t) );
     616             : 
     617           0 :   if( FD_UNLIKELY( !strcmp( tile->send.identity_key_path, "" ) ) )
     618           0 :     FD_LOG_ERR(( "identity_key_path not set" ));
     619             : 
     620           0 :   ctx->identity_key[ 0 ] = *(fd_pubkey_t const *)(fd_keyload_load( tile->send.identity_key_path, /* pubkey only: */ 1 ) );
     621           0 : }
     622             : 
     623             : static fd_send_link_in_t *
     624             : setup_input_link( fd_send_tile_ctx_t * ctx,
     625             :                   fd_topo_t          * topo,
     626             :                   fd_topo_tile_t     * tile,
     627             :                   ulong                kind,
     628           0 :                   const char         * name ) {
     629           0 :   ulong in_idx = fd_topo_find_tile_in_link( topo, tile, name, 0 );
     630           0 :   FD_TEST( in_idx!=ULONG_MAX );
     631           0 :   fd_topo_link_t    * in_link      = &topo->links[ tile->in_link_id[ in_idx ] ];
     632           0 :   fd_send_link_in_t * in_link_desc = &ctx->in_links[ in_idx ];
     633             : 
     634           0 :   in_link_desc->mem    = topo->workspaces[ topo->objs[ in_link->dcache_obj_id ].wksp_id ].wksp;
     635           0 :   in_link_desc->chunk0 = fd_dcache_compact_chunk0( in_link_desc->mem, in_link->dcache );
     636           0 :   in_link_desc->wmark  = fd_dcache_compact_wmark( in_link_desc->mem, in_link->dcache, in_link->mtu );
     637           0 :   in_link_desc->dcache = in_link->dcache;
     638           0 :   in_link_desc->kind   = kind;
     639             : 
     640           0 :   return in_link_desc;
     641           0 : }
     642             : 
     643             : static void
     644             : setup_output_link( fd_send_link_out_t * desc,
     645             :                    fd_topo_t           * topo,
     646             :                    fd_topo_tile_t      * tile,
     647           0 :                    const char          * name ) {
     648           0 :   ulong out_idx = fd_topo_find_tile_out_link( topo, tile, name, 0 );
     649           0 :   FD_TEST( out_idx!=ULONG_MAX );
     650           0 :   fd_topo_link_t * out_link = &topo->links[ tile->out_link_id[ out_idx ] ];
     651             : 
     652           0 :   desc->idx    = out_idx;
     653           0 :   desc->mcache = out_link->mcache;
     654           0 :   desc->sync   = fd_mcache_seq_laddr( desc->mcache );
     655           0 :   desc->depth  = fd_mcache_depth( desc->mcache );
     656           0 :   desc->mem    = topo->workspaces[ topo->objs[ out_link->dcache_obj_id ].wksp_id ].wksp;
     657           0 :   desc->chunk0 = fd_dcache_compact_chunk0( desc->mem, out_link->dcache );
     658           0 :   desc->wmark  = fd_dcache_compact_wmark( desc->mem, out_link->dcache, out_link->mtu );
     659           0 :   desc->chunk  = desc->chunk0;
     660           0 : }
     661             : 
     662             : static void
     663             : unprivileged_init( fd_topo_t *      topo,
     664           0 :                    fd_topo_tile_t * tile ) {
     665           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     666             : 
     667           0 :   if( FD_UNLIKELY( !tile->out_cnt ) ) FD_LOG_ERR(( "send has no output link" ));
     668             : 
     669             :   /* Scratch mem setup */
     670           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     671           0 :   fd_send_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_send_tile_ctx_t), sizeof(fd_send_tile_ctx_t) );
     672             : 
     673           0 :   ctx->mleaders = fd_multi_epoch_leaders_join( fd_multi_epoch_leaders_new( ctx->mleaders_mem ) );
     674           0 :   FD_TEST( ctx->mleaders );
     675             : 
     676           0 :   fd_aio_t * quic_tx_aio = fd_aio_join( fd_aio_new( ctx->quic_tx_aio, ctx, quic_tx_aio_send ) );
     677           0 :   if( FD_UNLIKELY( !quic_tx_aio ) ) FD_LOG_ERR(( "fd_aio_join failed" ));
     678             : 
     679           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 ) );
     680           0 :   if( FD_UNLIKELY( !quic ) ) FD_LOG_ERR(( "fd_quic_new failed" ));
     681             : 
     682           0 :   quic->config.role          =  FD_QUIC_ROLE_CLIENT;
     683           0 :   quic->config.idle_timeout  =  FD_SEND_QUIC_IDLE_TIMEOUT_NS;
     684           0 :   quic->config.ack_delay     =  FD_SEND_QUIC_ACK_DELAY_NS;
     685           0 :   quic->config.keep_alive    =  1;
     686           0 :   quic->config.sign          =  quic_tls_cv_sign;
     687           0 :   quic->config.sign_ctx      =  ctx;
     688           0 :   fd_memcpy( quic->config.identity_public_key, ctx->identity_key, sizeof(ctx->identity_key) );
     689             : 
     690           0 :   quic->cb.conn_hs_complete  = quic_hs_complete;
     691           0 :   quic->cb.conn_final        = quic_conn_final;
     692           0 :   quic->cb.now               = quic_now;
     693           0 :   quic->cb.now_ctx           = ctx;
     694           0 :   quic->cb.quic_ctx          = ctx;
     695             : 
     696           0 :   fd_quic_set_aio_net_tx( quic, quic_tx_aio );
     697           0 :   fd_quic_set_clock_tickcount( quic );
     698           0 :   FD_TEST_CUSTOM( fd_quic_init( quic ), "fd_quic_init failed" );
     699             : 
     700           0 :   ctx->quic = quic;
     701             : 
     702           0 :   ctx->ticks_per_sec = (ulong)(quic->config.tick_per_us * 1e6);
     703             : 
     704             :   /* Initialize connection map */
     705           0 :   void * conn_map_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_send_conn_map_align(), fd_send_conn_map_footprint() );
     706           0 :   ctx->conn_map = fd_send_conn_map_join( fd_send_conn_map_new( conn_map_mem ) );
     707           0 :   if( FD_UNLIKELY( !ctx->conn_map ) ) FD_LOG_ERR(( "fd_send_conn_map_join failed" ));
     708             : 
     709           0 :   ctx->src_ip_addr = tile->send.ip_addr;
     710           0 :   ctx->src_port    = tile->send.send_src_port;
     711           0 :   fd_ip4_udp_hdr_init( ctx->packet_hdr, FD_TXN_MTU, ctx->src_ip_addr, ctx->src_port );
     712             : 
     713           0 :   setup_input_link( ctx, topo, tile, IN_KIND_GOSSIP, "gossip_out"   );
     714           0 :   setup_input_link( ctx, topo, tile, IN_KIND_STAKE,  "replay_stake" );
     715           0 :   setup_input_link( ctx, topo, tile, IN_KIND_TOWER,  "tower_out"    );
     716             : 
     717           0 :   fd_send_link_in_t * net_in = setup_input_link( ctx, topo, tile, IN_KIND_NET, "net_send" );
     718           0 :   fd_net_rx_bounds_init( &ctx->net_in_bounds, net_in->dcache );
     719             : 
     720           0 :   setup_output_link( ctx->gossip_verify_out, topo, tile, "send_txns" );
     721           0 :   setup_output_link( ctx->net_out,           topo, tile, "send_net"  );
     722             : 
     723             :   /* Set up keyguard(s) */
     724           0 :   ulong             sign_in_idx  =  fd_topo_find_tile_in_link(  topo, tile, "sign_send", 0 );
     725           0 :   ulong             sign_out_idx =  fd_topo_find_tile_out_link( topo, tile, "send_sign", 0 );
     726           0 :   fd_topo_link_t  * sign_in      =  &topo->links[ tile->in_link_id[  sign_in_idx  ] ];
     727           0 :   fd_topo_link_t  * sign_out     =  &topo->links[ tile->out_link_id[ sign_out_idx ] ];
     728             : 
     729           0 :   if ( fd_keyguard_client_join( fd_keyguard_client_new( ctx->keyguard_client,
     730           0 :                                                             sign_out->mcache,
     731           0 :                                                             sign_out->dcache,
     732           0 :                                                             sign_in->mcache,
     733           0 :                                                             sign_in->dcache,
     734           0 :                                                             sign_out->mtu ) )==NULL ) {
     735           0 :     FD_LOG_ERR(( "Keyguard join failed" ));
     736           0 :   }
     737             : 
     738             :   /* init metrics */
     739           0 :   fd_memset( &ctx->metrics, 0, sizeof(ctx->metrics) );
     740           0 :   fd_histf_join( fd_histf_new( ctx->metrics.sign_duration,
     741           0 :                                   FD_MHIST_SECONDS_MIN( SEND, SIGN_DURATION_SECONDS    ),
     742           0 :                                   FD_MHIST_SECONDS_MAX( SEND, SIGN_DURATION_SECONDS    ) ) );
     743             : 
     744             :   /* Call new/join here rather than in fd_quic so min/max can differ across uses */
     745           0 :   fd_histf_join( fd_histf_new( quic->metrics.service_duration,
     746           0 :                                   FD_MHIST_SECONDS_MIN( SEND, SERVICE_DURATION_SECONDS ),
     747           0 :                                   FD_MHIST_SECONDS_MAX( SEND, SERVICE_DURATION_SECONDS ) ) );
     748           0 :   fd_histf_join( fd_histf_new( quic->metrics.receive_duration,
     749           0 :                                   FD_MHIST_SECONDS_MIN( SEND, RECEIVE_DURATION_SECONDS ),
     750           0 :                                   FD_MHIST_SECONDS_MAX( SEND, RECEIVE_DURATION_SECONDS ) ) );
     751             : 
     752           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
     753           0 :   if( FD_UNLIKELY( scratch_top != (ulong)scratch + scratch_footprint( tile ) ) ) {
     754           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
     755           0 :   }
     756           0 : }
     757             : 
     758             : static ulong
     759             : populate_allowed_seccomp( fd_topo_t      const * topo FD_PARAM_UNUSED,
     760             :                           fd_topo_tile_t const * tile FD_PARAM_UNUSED,
     761             :                           ulong                  out_cnt,
     762           0 :                           struct sock_filter   * out ) {
     763             : 
     764           0 :   populate_sock_filter_policy_fd_send_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
     765           0 :   return sock_filter_policy_fd_send_tile_instr_cnt;
     766           0 : }
     767             : 
     768             : static ulong
     769             : populate_allowed_fds( fd_topo_t      const * topo FD_PARAM_UNUSED,
     770             :                       fd_topo_tile_t const * tile FD_PARAM_UNUSED,
     771             :                       ulong                  out_fds_cnt,
     772           0 :                       int *                  out_fds ) {
     773           0 :   if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     774             : 
     775           0 :   ulong out_cnt = 0;
     776           0 :   out_fds[ out_cnt++ ] = 2UL; /* stderr */
     777           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     778           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     779           0 :   return out_cnt;
     780           0 : }
     781             : 
     782             : static void
     783           0 : metrics_write( fd_send_tile_ctx_t * ctx ) {
     784             : 
     785             :   /* Basic counters */
     786           0 :   FD_MCNT_SET(       SEND, LEADER_NOT_FOUND,             ctx->metrics.leader_not_found                             );
     787           0 :   FD_MCNT_SET(       SEND, UNSTAKED_CI,                  ctx->metrics.unstaked_ci_rcvd                             );
     788           0 :   FD_MCNT_SET(       SEND, CI_REMOVED,                   ctx->metrics.ci_removed                                   );
     789             : 
     790             :   /* Port-separated contact info metrics */
     791           0 :   FD_MCNT_ENUM_COPY( SEND, NEW_CONTACT_INFO_QUIC_VOTE,   ctx->metrics.new_contact_info[FD_SEND_PORT_QUIC_VOTE_IDX] );
     792           0 :   FD_MCNT_ENUM_COPY( SEND, NEW_CONTACT_INFO_QUIC_TPU,    ctx->metrics.new_contact_info[FD_SEND_PORT_QUIC_TPU_IDX]  );
     793           0 :   FD_MCNT_ENUM_COPY( SEND, NEW_CONTACT_INFO_UDP_VOTE,    ctx->metrics.new_contact_info[FD_SEND_PORT_UDP_VOTE_IDX]  );
     794           0 :   FD_MCNT_ENUM_COPY( SEND, NEW_CONTACT_INFO_UDP_TPU,     ctx->metrics.new_contact_info[FD_SEND_PORT_UDP_TPU_IDX]   );
     795             : 
     796             :   /* Port-separated send result metrics */
     797           0 :   FD_MCNT_ENUM_COPY( SEND, SEND_RESULT_QUIC_VOTE,        ctx->metrics.send_result_cnt[FD_SEND_PORT_QUIC_VOTE_IDX]  );
     798           0 :   FD_MCNT_ENUM_COPY( SEND, SEND_RESULT_QUIC_TPU,         ctx->metrics.send_result_cnt[FD_SEND_PORT_QUIC_TPU_IDX]   );
     799           0 :   FD_MCNT_ENUM_COPY( SEND, SEND_RESULT_UDP_VOTE,         ctx->metrics.send_result_cnt[FD_SEND_PORT_UDP_VOTE_IDX]   );
     800           0 :   FD_MCNT_ENUM_COPY( SEND, SEND_RESULT_UDP_TPU,          ctx->metrics.send_result_cnt[FD_SEND_PORT_UDP_TPU_IDX]    );
     801             : 
     802             :   /* Port-separated QUIC metrics */
     803           0 :   FD_MCNT_ENUM_COPY( SEND, HANDSHAKE_COMPLETE,           ctx->metrics.quic_hs_complete                             );
     804           0 :   FD_MCNT_ENUM_COPY( SEND, QUIC_CONN_FINAL,              ctx->metrics.quic_conn_final                              );
     805           0 :   FD_MCNT_ENUM_COPY( SEND, ENSURE_CONN_RESULT_QUIC_VOTE, ctx->metrics.ensure_conn_result[FD_METRICS_ENUM_SEND_QUIC_PORTS_V_QUIC_VOTE_IDX] );
     806           0 :   FD_MCNT_ENUM_COPY( SEND, ENSURE_CONN_RESULT_QUIC_TPU,  ctx->metrics.ensure_conn_result[FD_METRICS_ENUM_SEND_QUIC_PORTS_V_QUIC_TPU_IDX] );
     807             : 
     808             :   /* Gauges */
     809           0 :   FD_MGAUGE_SET(     SEND, STAKED_NO_CI,                 ctx->metrics.staked_no_ci                                 );
     810           0 :   FD_MGAUGE_SET(     SEND, STALE_CI,                     ctx->metrics.stale_ci                                     );
     811             : 
     812           0 :   FD_MHIST_COPY(     SEND, SIGN_DURATION_SECONDS,        ctx->metrics.sign_duration                                );
     813             : 
     814             :   /* General QUIC metrics */
     815           0 :   FD_MCNT_SET(         SEND, RECEIVED_BYTES,              ctx->quic->metrics.net_rx_byte_cnt         );
     816           0 :   FD_MCNT_ENUM_COPY(   SEND, RECEIVED_FRAMES,             ctx->quic->metrics.frame_rx_cnt            );
     817           0 :   FD_MCNT_SET(         SEND, RECEIVED_PACKETS,            ctx->quic->metrics.net_rx_pkt_cnt          );
     818           0 :   FD_MCNT_SET(         SEND, STREAM_RECEIVED_BYTES,       ctx->quic->metrics.stream_rx_byte_cnt      );
     819           0 :   FD_MCNT_SET(         SEND, STREAM_RECEIVED_EVENTS,      ctx->quic->metrics.stream_rx_event_cnt     );
     820             : 
     821           0 :   FD_MCNT_SET(         SEND, SENT_PACKETS,                ctx->quic->metrics.net_tx_pkt_cnt          );
     822           0 :   FD_MCNT_SET(         SEND, SENT_BYTES,                  ctx->quic->metrics.net_tx_byte_cnt         );
     823           0 :   FD_MCNT_SET(         SEND, RETRY_SENT,                  ctx->quic->metrics.retry_tx_cnt            );
     824           0 :   FD_MCNT_ENUM_COPY(   SEND, ACK_TX,                      ctx->quic->metrics.ack_tx                  );
     825             : 
     826           0 :   FD_MGAUGE_ENUM_COPY( SEND, CONNECTIONS_STATE,           ctx->quic->metrics.conn_state_cnt          );
     827           0 :   FD_MGAUGE_SET(       SEND, CONNECTIONS_ALLOC,           ctx->quic->metrics.conn_alloc_cnt          );
     828           0 :   FD_MCNT_SET(         SEND, CONNECTIONS_CREATED,         ctx->quic->metrics.conn_created_cnt        );
     829           0 :   FD_MCNT_SET(         SEND, CONNECTIONS_CLOSED,          ctx->quic->metrics.conn_closed_cnt         );
     830           0 :   FD_MCNT_SET(         SEND, CONNECTIONS_ABORTED,         ctx->quic->metrics.conn_aborted_cnt        );
     831           0 :   FD_MCNT_SET(         SEND, CONNECTIONS_TIMED_OUT,       ctx->quic->metrics.conn_timeout_cnt        );
     832           0 :   FD_MCNT_SET(         SEND, CONNECTIONS_RETRIED,         ctx->quic->metrics.conn_retry_cnt          );
     833           0 :   FD_MCNT_SET(         SEND, CONNECTION_ERROR_NO_SLOTS,   ctx->quic->metrics.conn_err_no_slots_cnt   );
     834           0 :   FD_MCNT_SET(         SEND, CONNECTION_ERROR_RETRY_FAIL, ctx->quic->metrics.conn_err_retry_fail_cnt );
     835             : 
     836           0 :   FD_MCNT_ENUM_COPY(   SEND, PKT_CRYPTO_FAILED,           ctx->quic->metrics.pkt_decrypt_fail_cnt    );
     837           0 :   FD_MCNT_ENUM_COPY(   SEND, PKT_NO_KEY,                  ctx->quic->metrics.pkt_no_key_cnt          );
     838           0 :   FD_MCNT_SET(         SEND, PKT_NO_CONN,                 ctx->quic->metrics.pkt_no_conn_cnt         );
     839           0 :   FD_MCNT_ENUM_COPY(   SEND, FRAME_TX_ALLOC,              ctx->quic->metrics.frame_tx_alloc_cnt      );
     840           0 :   FD_MCNT_SET(         SEND, PKT_NET_HEADER_INVALID,      ctx->quic->metrics.pkt_net_hdr_err_cnt     );
     841           0 :   FD_MCNT_SET(         SEND, PKT_QUIC_HEADER_INVALID,     ctx->quic->metrics.pkt_quic_hdr_err_cnt    );
     842           0 :   FD_MCNT_SET(         SEND, PKT_UNDERSZ,                 ctx->quic->metrics.pkt_undersz_cnt         );
     843           0 :   FD_MCNT_SET(         SEND, PKT_OVERSZ,                  ctx->quic->metrics.pkt_oversz_cnt          );
     844           0 :   FD_MCNT_SET(         SEND, PKT_VERNEG,                  ctx->quic->metrics.pkt_verneg_cnt          );
     845           0 :   FD_MCNT_SET(         SEND, PKT_RETRANSMISSIONS,         ctx->quic->metrics.pkt_retransmissions_cnt );
     846             : 
     847           0 :   FD_MCNT_SET(         SEND, HANDSHAKES_CREATED,          ctx->quic->metrics.hs_created_cnt          );
     848           0 :   FD_MCNT_SET(         SEND, HANDSHAKE_ERROR_ALLOC_FAIL,  ctx->quic->metrics.hs_err_alloc_fail_cnt   );
     849           0 :   FD_MCNT_SET(         SEND, HANDSHAKE_EVICTED,           ctx->quic->metrics.hs_evicted_cnt          );
     850             : 
     851           0 :   FD_MCNT_SET(         SEND, FRAME_FAIL_PARSE,            ctx->quic->metrics.frame_rx_err_cnt        );
     852             : 
     853           0 :   FD_MHIST_COPY(       SEND, SERVICE_DURATION_SECONDS,    ctx->quic->metrics.service_duration        );
     854           0 :   FD_MHIST_COPY(       SEND, RECEIVE_DURATION_SECONDS,    ctx->quic->metrics.receive_duration        );
     855           0 : }
     856             : 
     857             : 
     858           0 : #define STEM_BURST                        1UL /* send_txns */
     859           0 : #define STEM_LAZY                         100000000UL /* 100ms */
     860             : 
     861           0 : #define STEM_CALLBACK_CONTEXT_TYPE        fd_send_tile_ctx_t
     862           0 : #define STEM_CALLBACK_CONTEXT_ALIGN       alignof(fd_send_tile_ctx_t)
     863             : 
     864           0 : #define STEM_CALLBACK_BEFORE_FRAG         before_frag
     865           0 : #define STEM_CALLBACK_DURING_FRAG         during_frag
     866           0 : #define STEM_CALLBACK_AFTER_FRAG          after_frag
     867           0 : #define STEM_CALLBACK_METRICS_WRITE       metrics_write
     868           0 : #define STEM_CALLBACK_BEFORE_CREDIT       before_credit
     869           0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
     870             : #include "../../disco/stem/fd_stem.c"
     871             : 
     872             : fd_topo_run_tile_t fd_tile_send = {
     873             :   .name                     = "send",
     874             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     875             :   .populate_allowed_fds     = populate_allowed_fds,
     876             :   .scratch_align            = scratch_align,
     877             :   .scratch_footprint        = scratch_footprint,
     878             :   .privileged_init          = privileged_init,
     879             :   .unprivileged_init        = unprivileged_init,
     880             :   .run                      = stem_run,
     881             : };

Generated by: LCOV version 1.14