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

Generated by: LCOV version 1.14