LCOV - code coverage report
Current view: top level - discof/ipecho - fd_ipecho_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 137 0.0 %
Date: 2026-06-29 05:51:35 Functions: 0 11 0.0 %

          Line data    Source code
       1             : #include "fd_ipecho_client.h"
       2             : #include "fd_ipecho_server.h"
       3             : 
       4             : #include "../genesis/fd_genesi_tile.h"
       5             : #include "../genesis/genesis_hash.h"
       6             : #include "../../disco/topo/fd_topo.h"
       7             : #include "../../disco/metrics/fd_metrics.h"
       8             : #include "../../ballet/lthash/fd_lthash.h"
       9             : 
      10             : #include <netinet/in.h>
      11             : #include <sys/socket.h>
      12             : #include <poll.h>
      13             : 
      14             : #include "generated/fd_ipecho_tile_seccomp.h"
      15             : 
      16           0 : #define FD_IPECHO_MAX_CONNECTION_CNT (1024UL)
      17             : 
      18             : struct fd_ipecho_tile_ctx {
      19             :   int retrieving;
      20             : 
      21             :   fd_ipecho_server_t * server;
      22             :   fd_ipecho_client_t * client;
      23             : 
      24             :   uint   bind_address;
      25             :   ushort bind_port;
      26             : 
      27             :   ushort expected_shred_version;
      28             : 
      29             :   fd_wksp_t * genesi_in_mem;
      30             :   ulong       genesi_in_chunk0;
      31             :   ulong       genesi_in_wmark;
      32             : };
      33             : 
      34             : typedef struct fd_ipecho_tile_ctx fd_ipecho_tile_ctx_t;
      35             : 
      36             : FD_FN_CONST static inline ulong
      37           0 : scratch_align( void ) {
      38           0 :   return alignof( fd_ipecho_tile_ctx_t );
      39           0 : }
      40             : 
      41             : FD_FN_PURE static inline ulong
      42           0 : scratch_footprint( fd_topo_tile_t const * tile ) {
      43           0 :   (void)tile;
      44             : 
      45           0 :   ulong l = FD_LAYOUT_INIT;
      46           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_ipecho_tile_ctx_t), sizeof(fd_ipecho_tile_ctx_t)                               );
      47           0 :   l = FD_LAYOUT_APPEND( l, fd_ipecho_client_align(),      fd_ipecho_client_footprint()                               );
      48           0 :   l = FD_LAYOUT_APPEND( l, fd_ipecho_server_align(),      fd_ipecho_server_footprint( FD_IPECHO_MAX_CONNECTION_CNT ) );
      49           0 :   return FD_LAYOUT_FINI( l, scratch_align() );
      50           0 : }
      51             : 
      52             : static inline void
      53           0 : metrics_write( fd_ipecho_tile_ctx_t * ctx ) {
      54           0 :   fd_ipecho_server_metrics_t * metrics = fd_ipecho_server_metrics( ctx->server );
      55             : 
      56           0 :   FD_MGAUGE_SET( IPECHO, CONN_ACTIVE,         metrics->connection_cnt           );
      57           0 :   FD_MCNT_SET(   IPECHO, BYTES_READ,               metrics->bytes_read               );
      58           0 :   FD_MCNT_SET(   IPECHO, BYTES_WRITTEN,            metrics->bytes_written            );
      59           0 :   ulong conn_closed[ FD_METRICS_ENUM_CONN_CLOSE_RESULT_CNT ];
      60           0 :   conn_closed[ FD_METRICS_ENUM_CONN_CLOSE_RESULT_V_OK_IDX    ] = metrics->connections_closed_ok;
      61           0 :   conn_closed[ FD_METRICS_ENUM_CONN_CLOSE_RESULT_V_ERROR_IDX ] = metrics->connections_closed_error;
      62           0 :   FD_MCNT_ENUM_COPY( IPECHO, CONN_CLOSED, conn_closed );
      63           0 : }
      64             : 
      65             : static inline void
      66             : poll_client( fd_ipecho_tile_ctx_t * ctx,
      67             :              fd_stem_context_t *    stem,
      68           0 :              int *                  charge_busy ) {
      69           0 :   if( FD_UNLIKELY( !ctx->client ) ) return;
      70             : 
      71           0 :   ushort shred_version;
      72           0 :   int result = fd_ipecho_client_poll( ctx->client, &shred_version, charge_busy );
      73           0 :   if( FD_UNLIKELY( !result ) ) {
      74           0 :     if( FD_UNLIKELY( ctx->expected_shred_version && ctx->expected_shred_version!=shred_version ) ) {
      75           0 :       FD_LOG_ERR(( "Expected shred version %hu but entrypoint returned %hu",
      76           0 :                    ctx->expected_shred_version, shred_version ));
      77           0 :     }
      78             : 
      79           0 :     FD_LOG_INFO(( "retrieved shred version %hu from entrypoint", shred_version ));
      80           0 :     FD_MGAUGE_SET( IPECHO, CURRENT_SHRED_VERSION, shred_version );
      81           0 :     fd_stem_publish( stem, 0UL, shred_version, 0UL, 0UL, 0UL, 0UL, 0UL );
      82           0 :     fd_ipecho_server_set_shred_version( ctx->server, shred_version );
      83           0 :     ctx->retrieving = 0;
      84           0 :     return;
      85           0 :   } else if( FD_UNLIKELY( -1==result ) ) {
      86           0 :     FD_LOG_ERR(( "Could not determine shred version from entrypoints.  Please "
      87           0 :                  "check you can connect to the entrypoints provided." ));
      88           0 :   }
      89           0 : }
      90             : 
      91             : static inline void
      92             : after_credit( fd_ipecho_tile_ctx_t * ctx,
      93             :               fd_stem_context_t *    stem,
      94             :               int *                  opt_poll_in,
      95           0 :               int *                  charge_busy ) {
      96           0 :   (void)opt_poll_in;
      97             : 
      98             :   /* 1ms timeout is sufficient here. Large timeouts (e.g. 10ms) can
      99             :      cause housekeeping tasks to run very infrequently (> 0.05Hz) since
     100             :      they incur a prolonged context switch every single iteration of the
     101             :      STEM loop. */
     102           0 :   int timeout = ctx->retrieving ? 0 : 1;
     103             : 
     104           0 :   if( FD_UNLIKELY( ctx->retrieving ) ) poll_client( ctx, stem, charge_busy );
     105           0 :   else                                 fd_ipecho_server_poll( ctx->server, charge_busy, timeout );
     106           0 : }
     107             : 
     108             : static inline int
     109             : returnable_frag( fd_ipecho_tile_ctx_t * ctx,
     110             :                  ulong                  in_idx,
     111             :                  ulong                  seq,
     112             :                  ulong                  sig,
     113             :                  ulong                  chunk,
     114             :                  ulong                  sz,
     115             :                  ulong                  ctl,
     116             :                  ulong                  tsorig,
     117             :                  ulong                  tspub,
     118           0 :                  fd_stem_context_t *    stem ) {
     119           0 :   (void)in_idx; (void)seq; (void)sig; (void)sz; (void)ctl; (void)tspub;
     120           0 :   fd_genesis_meta_t const * genesis_meta = fd_chunk_to_laddr( ctx->genesi_in_mem, chunk );
     121             : 
     122           0 :   if( FD_UNLIKELY( genesis_meta->bootstrap ) ) {
     123           0 :     ushort shred_version = compute_shred_version( genesis_meta->genesis_hash.uc, NULL, 0UL );
     124           0 :     FD_TEST( shred_version );
     125             : 
     126           0 :     FD_MGAUGE_SET( IPECHO, CURRENT_SHRED_VERSION, shred_version );
     127           0 :     fd_stem_publish( stem, 0UL, shred_version, 0UL, 0UL, 0UL, tsorig, fd_frag_meta_ts_comp( fd_tickcount() ) );
     128           0 :     fd_ipecho_server_set_shred_version( ctx->server, shred_version );
     129           0 :     ctx->retrieving = 0;
     130           0 :   }
     131             : 
     132           0 :   return 0;
     133           0 : }
     134             : 
     135             : static void
     136             : privileged_init( fd_topo_t const *      topo,
     137           0 :                  fd_topo_tile_t const * tile ) {
     138           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     139             : 
     140           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     141           0 :   fd_ipecho_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_ipecho_tile_ctx_t ), sizeof( fd_ipecho_tile_ctx_t )                             );
     142           0 :   void * _client             = FD_SCRATCH_ALLOC_APPEND( l, fd_ipecho_client_align(),        fd_ipecho_client_footprint()                               );
     143           0 :   void * _server             = FD_SCRATCH_ALLOC_APPEND( l, fd_ipecho_server_align(),        fd_ipecho_server_footprint( FD_IPECHO_MAX_CONNECTION_CNT ) );
     144             : 
     145           0 :   ctx->bind_address = tile->ipecho.bind_address;
     146           0 :   ctx->bind_port    = tile->ipecho.bind_port;
     147             : 
     148           0 :   ctx->expected_shred_version = tile->ipecho.expected_shred_version;
     149             : 
     150           0 :   ctx->retrieving = 1;
     151           0 :   if( FD_LIKELY( tile->ipecho.entrypoints_cnt ) ) {
     152           0 :     ctx->client = fd_ipecho_client_join( fd_ipecho_client_new( _client ) );
     153           0 :     FD_TEST( ctx->client );
     154           0 :     fd_ipecho_client_init( ctx->client, tile->ipecho.entrypoints, tile->ipecho.entrypoints_cnt );
     155           0 :   } else {
     156           0 :     ctx->client = NULL;
     157           0 :   }
     158             : 
     159           0 :   ctx->server = fd_ipecho_server_join( fd_ipecho_server_new( _server, FD_IPECHO_MAX_CONNECTION_CNT ) );
     160           0 :   FD_TEST( ctx->server );
     161           0 :   fd_ipecho_server_init( ctx->server, ctx->bind_address, ctx->bind_port, ctx->expected_shred_version );
     162             : 
     163           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
     164           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
     165           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
     166           0 : }
     167             : 
     168             : static void
     169             : unprivileged_init( fd_topo_t const *      topo,
     170           0 :                    fd_topo_tile_t const * tile ) {
     171           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     172             : 
     173           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     174           0 :   fd_ipecho_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_ipecho_tile_ctx_t ), sizeof( fd_ipecho_tile_ctx_t )       );
     175             : 
     176           0 :   FD_MGAUGE_SET( IPECHO, CURRENT_SHRED_VERSION, tile->ipecho.expected_shred_version );
     177             : 
     178             :   /* In some topologies (e.g. firedancer-dev gossip), the ipecho tile
     179             :      has no input links. Guard against dereferencing a missing
     180             :      link/dcache. */
     181           0 :   if( FD_LIKELY( tile->in_cnt>0UL ) ) {
     182           0 :     ulong link_id = tile->in_link_id[ 0UL ];
     183           0 :     void * dcache = topo->links[ link_id ].dcache;
     184           0 :     ctx->genesi_in_mem    = topo->workspaces[ topo->objs[ topo->links[ link_id ].dcache_obj_id ].wksp_id ].wksp;
     185           0 :     ctx->genesi_in_chunk0 = fd_dcache_compact_chunk0( ctx->genesi_in_mem, dcache );
     186           0 :     ctx->genesi_in_wmark  = fd_dcache_compact_wmark ( ctx->genesi_in_mem, dcache, topo->links[ link_id ].mtu );
     187           0 :   } else {
     188           0 :     ctx->genesi_in_mem    = NULL;
     189           0 :     ctx->genesi_in_chunk0 = 0UL;
     190           0 :     ctx->genesi_in_wmark  = 0UL;
     191           0 :   }
     192           0 : }
     193             : 
     194             : static ulong
     195             : rlimit_file_cnt( fd_topo_t const *      topo FD_PARAM_UNUSED,
     196           0 :                  fd_topo_tile_t const * tile ) {
     197             :   /* pipefd, socket, stderr, logfile, and one spare for
     198             :      new accept() connections */
     199           0 :   ulong base = 5UL;
     200           0 :   return base +
     201           0 :          tile->ipecho.entrypoints_cnt + /* for the client */
     202           0 :          FD_IPECHO_MAX_CONNECTION_CNT;  /* for the server's connections */
     203           0 : }
     204             : 
     205             : static ulong
     206             : populate_allowed_seccomp( fd_topo_t const *      topo,
     207             :                           fd_topo_tile_t const * tile,
     208             :                           ulong                  out_cnt,
     209           0 :                           struct sock_filter *   out ) {
     210           0 :   (void)topo;
     211           0 :   (void)tile;
     212             : 
     213           0 :   populate_sock_filter_policy_fd_ipecho_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
     214           0 :   return sock_filter_policy_fd_ipecho_tile_instr_cnt;
     215           0 : }
     216             : 
     217             : static ulong
     218             : populate_allowed_fds( fd_topo_t const *      topo,
     219             :                       fd_topo_tile_t const * tile,
     220             :                       ulong                  out_fds_cnt,
     221           0 :                       int *                  out_fds ) {
     222             : 
     223           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     224           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     225           0 :   fd_ipecho_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_ipecho_tile_ctx_t), sizeof(fd_ipecho_tile_ctx_t) );
     226             : 
     227           0 :   if( FD_UNLIKELY( out_fds_cnt<3UL+tile->ipecho.entrypoints_cnt ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     228             : 
     229           0 :   ulong out_cnt = 0UL;
     230           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
     231           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     232           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     233             : 
     234             :   /* All of the fds managed by the client. */
     235           0 :   for( ulong i=0UL; i<tile->ipecho.entrypoints_cnt; i++ ) {
     236           0 :     int fd = fd_ipecho_client_get_pollfds( ctx->client )[ i ].fd;
     237           0 :     if( FD_LIKELY( fd!=-1 ) ) out_fds[ out_cnt++ ] = fd;
     238           0 :   }
     239             : 
     240             :   /* The server's socket. */
     241           0 :   out_fds[ out_cnt++ ] = fd_ipecho_server_sockfd( ctx->server );
     242           0 :   return out_cnt;
     243           0 : }
     244             : 
     245           0 : #define STEM_BURST (2UL)
     246           0 : #define STEM_LAZY  (50UL)
     247             : 
     248           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_ipecho_tile_ctx_t
     249           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_ipecho_tile_ctx_t)
     250             : 
     251           0 : #define STEM_CALLBACK_METRICS_WRITE   metrics_write
     252           0 : #define STEM_CALLBACK_AFTER_CREDIT    after_credit
     253           0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
     254             : 
     255             : #include "../../disco/stem/fd_stem.c"
     256             : 
     257             : fd_topo_run_tile_t fd_tile_ipecho = {
     258             :   .name                     = "ipecho",
     259             :   .rlimit_file_cnt_fn       = rlimit_file_cnt,
     260             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     261             :   .populate_allowed_fds     = populate_allowed_fds,
     262             :   .scratch_align            = scratch_align,
     263             :   .scratch_footprint        = scratch_footprint,
     264             :   .privileged_init          = privileged_init,
     265             :   .unprivileged_init        = unprivileged_init,
     266             :   .run                      = stem_run,
     267             :   .allow_connect            = 1,
     268             :   .keep_host_networking     = 1
     269             : };

Generated by: LCOV version 1.14