LCOV - code coverage report
Current view: top level - app/shared - fd_config.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 75 489 15.3 %
Date: 2026-07-06 05:43:26 Functions: 2 13 15.4 %

          Line data    Source code
       1             : #define _GNU_SOURCE
       2             : #include "fd_config.h"
       3             : #include "fd_config_private.h"
       4             : 
       5             : #include "../platform/fd_net_util.h"
       6             : #include "../platform/fd_sys_util.h"
       7             : #include "../../ballet/toml/fd_toml.h"
       8             : #include "../../disco/genesis/fd_genesis_cluster.h"
       9             : 
      10             : #include <unistd.h>
      11             : #include <errno.h>
      12             : #include <stdlib.h> /* strtoul */
      13             : #include <sys/utsname.h>
      14             : #include <sys/mman.h>
      15             : 
      16             : /* TODO: Rewrite this ... */
      17             : 
      18             : static inline void
      19             : replace( char *       in,
      20             :          const char * pat,
      21           0 :          const char * sub ) {
      22           0 :   char * replace = strstr( in, pat );
      23           0 :   if( FD_LIKELY( replace ) ) {
      24           0 :     ulong pat_len = strlen( pat );
      25           0 :     ulong sub_len = strlen( sub );
      26           0 :     ulong in_len  = strlen( in );
      27           0 :     if( FD_UNLIKELY( pat_len > in_len ) ) return;
      28             : 
      29           0 :     ulong total_len = in_len - pat_len + sub_len;
      30           0 :     if( FD_UNLIKELY( total_len >= PATH_MAX ) )
      31           0 :       FD_LOG_ERR(( "configuration scratch directory path too long: `%s`", in ));
      32             : 
      33           0 :     uchar after[PATH_MAX] = {0};
      34           0 :     fd_memcpy( after, replace + pat_len, strlen( replace + pat_len ) );
      35           0 :     fd_memcpy( replace, sub, sub_len );
      36           0 :     ulong after_len = strlen( ( const char * ) after );
      37           0 :     fd_memcpy( replace + sub_len, after, after_len );
      38           0 :     in[ total_len ] = '\0';
      39           0 :   }
      40           0 : }
      41             : 
      42             : 
      43             : FD_FN_CONST static inline int
      44           0 : parse_log_level( char const * level ) {
      45           0 :   if( FD_UNLIKELY( !strcmp( level, "DEBUG" ) ) )    return 0;
      46           0 :   if( FD_UNLIKELY( !strcmp( level, "INFO"  ) ) )    return 1;
      47           0 :   if( FD_UNLIKELY( !strcmp( level, "NOTICE"  ) ) )  return 2;
      48           0 :   if( FD_UNLIKELY( !strcmp( level, "WARNING"  ) ) ) return 3;
      49           0 :   if( FD_UNLIKELY( !strcmp( level, "ERR" ) ) )      return 4;
      50           0 :   if( FD_UNLIKELY( !strcmp( level, "CRIT" ) ) )     return 5;
      51           0 :   if( FD_UNLIKELY( !strcmp( level, "ALERT" ) ) )    return 6;
      52           0 :   if( FD_UNLIKELY( !strcmp( level, "EMERG" ) ) )    return 7;
      53           0 :   return -1;
      54           0 : }
      55             : 
      56             : FD_FN_CONST static inline int
      57           0 : parse_core_dump_level( char const * level ) {
      58           0 :   if( FD_UNLIKELY( !strcmp( level, "disabled" ) ) )       return FD_TOPO_CORE_DUMP_LEVEL_DISABLED;
      59           0 :   if( FD_UNLIKELY( !strcmp( level, "minimal"  ) ) )       return FD_TOPO_CORE_DUMP_LEVEL_MINIMAL;
      60           0 :   if( FD_UNLIKELY( !strcmp( level, "regular"  ) ) )       return FD_TOPO_CORE_DUMP_LEVEL_REGULAR;
      61           0 :   if( FD_UNLIKELY( !strcmp( level, "full"     ) ) )       return FD_TOPO_CORE_DUMP_LEVEL_FULL;
      62           0 :   return -1;
      63           0 : }
      64             : 
      65             : void
      66             : fd_config_load_buf( fd_config_t * out,
      67             :                     char const *  buf,
      68             :                     ulong         sz,
      69           0 :                     char const *  path ) {
      70           0 :   static uchar pod_mem[ 1UL<<26 ];
      71           0 :   uchar * pod = fd_pod_join( fd_pod_new( pod_mem, sizeof(pod_mem) ) );
      72             : 
      73           0 :   fd_toml_err_info_t toml_err[1];
      74           0 :   uchar scratch[ 4096 ];
      75           0 :   int toml_errc = fd_toml_parse( buf, sz, pod, scratch, sizeof(scratch), toml_err );
      76           0 :   if( FD_UNLIKELY( toml_errc!=FD_TOML_SUCCESS ) ) {
      77           0 :     switch( toml_errc ) {
      78           0 :     case FD_TOML_ERR_POD:
      79           0 :       FD_LOG_ERR(( "Failed to parse config file (%s): ran out of buffer space while parsing", path ));
      80           0 :       break;
      81           0 :     case FD_TOML_ERR_SCRATCH:
      82           0 :       FD_LOG_ERR(( "Failed to parse config file (%s) at line %lu: ran out of scratch space while parsing", path, toml_err->line ));
      83           0 :       break;
      84           0 :     case FD_TOML_ERR_KEY:
      85           0 :       FD_LOG_ERR(( "Failed to parse config file (%s) at line %lu: oversize key", path, toml_err->line ));
      86           0 :       break;
      87           0 :     case FD_TOML_ERR_DUP:
      88           0 :       FD_LOG_ERR(( "Failed to parse config file (%s) at line %lu: duplicate key", path, toml_err->line ));
      89           0 :       break;
      90           0 :     case FD_TOML_ERR_RANGE:
      91           0 :       FD_LOG_ERR(( "Failed to parse config file (%s) at line %lu: invalid value for key", path, toml_err->line ));
      92           0 :       break;
      93           0 :     case FD_TOML_ERR_PARSE:
      94           0 :       FD_LOG_ERR(( "Failed to parse config file (%s) at line %lu", path, toml_err->line ));
      95           0 :       break;
      96           0 :     default:
      97           0 :       FD_LOG_ERR(( "Failed to parse config file (%s): %s", path, fd_toml_strerror( toml_errc ) ));
      98           0 :       break;
      99           0 :     }
     100           0 :   }
     101             : 
     102           0 :   if( FD_UNLIKELY( !fd_config_extract_pod( pod, out ) ) ) FD_LOG_ERR(( "Failed to parse config file (%s): there are unrecognized keys logged above", path ));
     103             : 
     104           0 :   fd_pod_delete( fd_pod_leave( pod ) );
     105           0 : }
     106             : 
     107             : static void
     108           0 : fd_config_fillf( fd_config_t * config ) {
     109           0 :   if( FD_UNLIKELY( strcmp( config->paths.accounts, "" ) ) ) {
     110           0 :     replace( config->paths.accounts, "{user}", config->user );
     111           0 :     replace( config->paths.accounts, "{name}", config->name );
     112           0 :   } else {
     113           0 :     FD_TEST( fd_cstr_printf_check( config->paths.accounts, sizeof(config->paths.accounts), NULL, "%s/accounts.db", config->paths.base ) );
     114           0 :   }
     115             : 
     116           0 :   if( FD_UNLIKELY( strcmp( config->paths.shredb, "" ) ) ) {
     117           0 :     replace( config->paths.shredb, "{user}", config->user );
     118           0 :     replace( config->paths.shredb, "{name}", config->name );
     119           0 :   } else {
     120           0 :     FD_TEST( fd_cstr_printf_check( config->paths.shredb, sizeof(config->paths.shredb), NULL, "%s/shreds.db", config->paths.base ) );
     121           0 :   }
     122             : 
     123           0 :   for( ulong i=0UL; i<config->firedancer.paths.authorized_voter_paths_cnt; i++ ) {
     124           0 :     replace( config->firedancer.paths.authorized_voter_paths[ i ], "{user}", config->user );
     125           0 :     replace( config->firedancer.paths.authorized_voter_paths[ i ], "{name}", config->name );
     126           0 :   }
     127           0 : }
     128             : 
     129             : static void
     130           0 : fd_config_fillh( fd_config_t * config ) {
     131           0 :   if( FD_UNLIKELY( strcmp( config->frankendancer.paths.accounts_path, "" ) ) ) {
     132           0 :     replace( config->frankendancer.paths.accounts_path, "{user}", config->user );
     133           0 :     replace( config->frankendancer.paths.accounts_path, "{name}", config->name );
     134           0 :   }
     135             : 
     136           0 :   if( FD_UNLIKELY( strcmp( config->frankendancer.paths.ledger, "" ) ) ) {
     137           0 :     replace( config->frankendancer.paths.ledger, "{user}", config->user );
     138           0 :     replace( config->frankendancer.paths.ledger, "{name}", config->name );
     139           0 :   } else {
     140           0 :     FD_TEST( fd_cstr_printf_check( config->frankendancer.paths.ledger, sizeof(config->frankendancer.paths.ledger), NULL, "%s/ledger", config->paths.base ) );
     141           0 :   }
     142             : 
     143           0 :   if( FD_UNLIKELY( strcmp( config->frankendancer.snapshots.path, "" ) ) ) {
     144           0 :     replace( config->frankendancer.snapshots.path, "{user}", config->user );
     145           0 :     replace( config->frankendancer.snapshots.path, "{name}", config->name );
     146           0 :   } else {
     147           0 :     strncpy( config->frankendancer.snapshots.path, config->frankendancer.paths.ledger, sizeof(config->frankendancer.snapshots.path) );
     148           0 :   }
     149             : 
     150           0 :   for( ulong i=0UL; i<config->frankendancer.paths.authorized_voter_paths_cnt; i++ ) {
     151           0 :     replace( config->frankendancer.paths.authorized_voter_paths[ i ], "{user}", config->user );
     152           0 :     replace( config->frankendancer.paths.authorized_voter_paths[ i ], "{name}", config->name );
     153           0 :   }
     154             : 
     155           0 :   if( FD_UNLIKELY( config->tiles.quic.quic_transaction_listen_port!=config->tiles.quic.regular_transaction_listen_port+6 ) )
     156           0 :     FD_LOG_ERR(( "configuration specifies invalid [tiles.quic.quic_transaction_listen_port] `%hu`. "
     157           0 :                  "This must be 6 more than [tiles.quic.regular_transaction_listen_port] `%hu`",
     158           0 :                  config->tiles.quic.quic_transaction_listen_port,
     159           0 :                  config->tiles.quic.regular_transaction_listen_port ));
     160             : 
     161           0 :   char dynamic_port_range[ 32 ];
     162           0 :   fd_memcpy( dynamic_port_range, config->frankendancer.dynamic_port_range, sizeof(dynamic_port_range) );
     163             : 
     164           0 :   char * dash = strstr( dynamic_port_range, "-" );
     165           0 :   if( FD_UNLIKELY( !dash ) )
     166           0 :     FD_LOG_ERR(( "configuration specifies invalid [dynamic_port_range] `%s`. "
     167           0 :                  "This must be formatted like `<min>-<max>`",
     168           0 :                  config->frankendancer.dynamic_port_range ));
     169             : 
     170           0 :   *dash = '\0';
     171           0 :   char * endptr;
     172           0 :   ulong agave_port_min = strtoul( dynamic_port_range, &endptr, 10 );
     173           0 :   if( FD_UNLIKELY( *endptr != '\0' || agave_port_min > USHORT_MAX ) )
     174           0 :     FD_LOG_ERR(( "configuration specifies invalid [dynamic_port_range] `%s`. "
     175           0 :                  "This must be formatted like `<min>-<max>`",
     176           0 :                  config->frankendancer.dynamic_port_range ));
     177           0 :   ulong agave_port_max = strtoul( dash + 1, &endptr, 10 );
     178           0 :   if( FD_UNLIKELY( *endptr != '\0' || agave_port_max > USHORT_MAX ) )
     179           0 :     FD_LOG_ERR(( "configuration specifies invalid [dynamic_port_range] `%s`. "
     180           0 :                  "This must be formatted like `<min>-<max>`",
     181           0 :                  config->frankendancer.dynamic_port_range ));
     182           0 :   if( FD_UNLIKELY( agave_port_min > agave_port_max ) )
     183           0 :     FD_LOG_ERR(( "configuration specifies invalid [dynamic_port_range] `%s`. "
     184           0 :                  "The minimum port must be less than or equal to the maximum port",
     185           0 :                  config->frankendancer.dynamic_port_range ));
     186             : 
     187           0 :   if( FD_UNLIKELY( config->tiles.quic.regular_transaction_listen_port >= agave_port_min &&
     188           0 :                    config->tiles.quic.regular_transaction_listen_port < agave_port_max ) )
     189           0 :     FD_LOG_ERR(( "configuration specifies invalid [tiles.quic.transaction_listen_port] `%hu`. "
     190           0 :                  "This must be outside the dynamic port range `%s`",
     191           0 :                  config->tiles.quic.regular_transaction_listen_port,
     192           0 :                  config->frankendancer.dynamic_port_range ));
     193             : 
     194           0 :   if( FD_UNLIKELY( config->tiles.quic.quic_transaction_listen_port >= agave_port_min &&
     195           0 :                    config->tiles.quic.quic_transaction_listen_port < agave_port_max ) )
     196           0 :     FD_LOG_ERR(( "configuration specifies invalid [tiles.quic.quic_transaction_listen_port] `%hu`. "
     197           0 :                  "This must be outside the dynamic port range `%s`",
     198           0 :                  config->tiles.quic.quic_transaction_listen_port,
     199           0 :                  config->frankendancer.dynamic_port_range ));
     200             : 
     201           0 :   if( FD_UNLIKELY( config->tiles.shred.shred_listen_port >= agave_port_min &&
     202           0 :                    config->tiles.shred.shred_listen_port < agave_port_max ) )
     203           0 :     FD_LOG_ERR(( "configuration specifies invalid [tiles.shred.shred_listen_port] `%hu`. "
     204           0 :                  "This must be outside the dynamic port range `%s`",
     205           0 :                  config->tiles.shred.shred_listen_port,
     206           0 :                  config->frankendancer.dynamic_port_range ));
     207           0 : }
     208             : 
     209             : static void
     210           0 : fd_config_fill_net( fd_config_t * config ) {
     211           0 :   if( FD_UNLIKELY( !strcmp( config->net.interface, "" ) ) ) {
     212           0 :     uint ifindex;
     213           0 :     int result = fd_net_util_internet_ifindex( &ifindex );
     214           0 :     if( FD_UNLIKELY( -1==result && errno!=ENODEV ) ) FD_LOG_ERR(( "could not get network device index (%i-%s)", errno, fd_io_strerror( errno ) ));
     215           0 :     else if( FD_UNLIKELY( -1==result ) )
     216           0 :       FD_LOG_ERR(( "no network device found which routes to 8.8.8.8. If no network "
     217           0 :                    "interface is specified in the configuration file, Firedancer "
     218           0 :                    "tries to use the first network interface found which routes to "
     219           0 :                    "8.8.8.8. You can see what this is by running `ip route get 8.8.8.8` "
     220           0 :                    "You can fix this error by specifying a network interface to bind to in "
     221           0 :                    "your configuration file under [net.interface]" ));
     222             : 
     223           0 :     if( FD_UNLIKELY( !if_indextoname( ifindex, config->net.interface ) ) )
     224           0 :       FD_LOG_ERR(( "could not get name of interface with index %u", ifindex ));
     225           0 :   }
     226             : 
     227           0 :   if( FD_UNLIKELY( !if_nametoindex( config->net.interface ) ) )
     228           0 :     FD_LOG_ERR(( "configuration specifies network interface `%s` which does not exist", config->net.interface ));
     229           0 :   uint iface_ip;
     230           0 :   if( FD_UNLIKELY( -1==fd_net_util_if_addr( config->net.interface, &iface_ip ) ) )
     231           0 :     FD_LOG_ERR(( "could not get IP address for interface `%s`", config->net.interface ));
     232             : 
     233           0 :   if( FD_UNLIKELY( config->is_firedancer ) ) {
     234           0 :     if( FD_UNLIKELY( strcmp( config->firedancer.gossip.host, "" ) ) ) {
     235           0 :       uint gossip_ip_addr = iface_ip;
     236           0 :       int  has_gossip_ip4 = 0;
     237           0 :       if( FD_UNLIKELY( strlen( config->firedancer.gossip.host )<=15UL ) ) {
     238             :         /* Only sets gossip_ip_addr if it's a valid IPv4 address, otherwise assume it's a DNS name */
     239           0 :         has_gossip_ip4 = fd_cstr_to_ip4_addr( config->firedancer.gossip.host, &gossip_ip_addr );
     240           0 :       }
     241           0 :       if( FD_UNLIKELY( !fd_ip4_addr_is_public( gossip_ip_addr ) && config->is_live_cluster && has_gossip_ip4 ) )
     242           0 :         FD_LOG_ERR(( "Trying to use [gossip.host] " FD_IP4_ADDR_FMT " for listening to incoming "
     243           0 :                      "transactions, but it is part of a private network and will not be routable "
     244           0 :                      "for other Solana network nodes.", FD_IP4_ADDR_FMT_ARGS( gossip_ip_addr ) ));
     245           0 :     } else if( FD_UNLIKELY( !fd_ip4_addr_is_public( iface_ip ) && config->is_live_cluster ) ) {
     246           0 :       FD_LOG_ERR(( "Trying to use network interface `%s` for listening to incoming transactions, "
     247           0 :                    "but it has IPv4 address " FD_IP4_ADDR_FMT " which is part of a private network "
     248           0 :                    "and will not be routable for other Solana network nodes. If you are running "
     249           0 :                    "behind a NAT and this interface is publicly reachable, you can continue by "
     250           0 :                    "manually specifying the IP address to advertise in your configuration under "
     251           0 :                    "[gossip.host].", config->net.interface, FD_IP4_ADDR_FMT_ARGS( iface_ip ) ));
     252           0 :     }
     253           0 :   }
     254             : 
     255           0 :   config->net.ip_addr = iface_ip;
     256           0 : }
     257             : 
     258             : void
     259             : fd_config_fill( fd_config_t * config,
     260             :                 int           is_local_cluster,
     261           0 :                 int           dev ) {
     262           0 :   struct utsname utsname;
     263           0 :   if( FD_UNLIKELY( -1==uname( &utsname ) ) )
     264           0 :     FD_LOG_ERR(( "could not get uname (%i-%s)", errno, fd_io_strerror( errno ) ));
     265           0 :   fd_cstr_ncpy( config->hostname, utsname.nodename, sizeof(config->hostname) ); /* Just truncate the name if it's too long to fit */
     266             : 
     267           0 :   ulong cluster = FD_CLUSTER_UNKNOWN;
     268           0 :   if( FD_UNLIKELY( !config->is_firedancer ) ) {
     269           0 :     cluster = fd_genesis_cluster_identify( config->frankendancer.consensus.expected_genesis_hash );
     270           0 :   }
     271           0 :   config->is_live_cluster = cluster!=FD_CLUSTER_UNKNOWN;
     272           0 :   strcpy( config->cluster, fd_genesis_cluster_name( cluster ) );
     273             : 
     274           0 :   if( FD_UNLIKELY( !strcmp( config->user, "" ) ) ) {
     275           0 :     const char * user = fd_sys_util_login_user();
     276           0 :     if( FD_UNLIKELY( !user ) )                                                                 FD_LOG_ERR(( "could not automatically determine a user to run Firedancer as. You must specify a [user] in your configuration TOML file." ));
     277           0 :     if( FD_UNLIKELY( strlen( user )>=sizeof( config->user ) ) )                                FD_LOG_ERR(( "user name `%s` is too long", user ));
     278           0 :     strncpy( config->user, user, sizeof(config->user) );
     279           0 :   }
     280             : 
     281           0 :   if( FD_UNLIKELY( -1==fd_sys_util_user_to_uid( config->user, &config->uid, &config->gid ) ) ) FD_LOG_ERR(( "configuration file wants firedancer to run as user `%s` but it does not exist", config->user ));
     282           0 :   if( FD_UNLIKELY( !config->uid || !config->gid ) )                                            FD_LOG_ERR(( "firedancer cannot run as root. please specify a non-root user in the configuration file" ));
     283           0 :   if( FD_UNLIKELY( getuid()!=0U && config->uid!=getuid() ) )                                   FD_LOG_ERR(( "running as uid %u, but config specifies uid %u", getuid(), config->uid ));
     284           0 :   if( FD_UNLIKELY( getgid()!=0U && config->gid!=getgid() ) )                                   FD_LOG_ERR(( "running as gid %u, but config specifies gid %u", getgid(), config->gid ));
     285             : 
     286           0 :   FD_TEST( fd_cstr_printf_check( config->hugetlbfs.gigantic_page_mount_path,
     287           0 :     sizeof(config->hugetlbfs.gigantic_page_mount_path),
     288           0 :     NULL,
     289           0 :     "%s/.gigantic",
     290           0 :     config->hugetlbfs.mount_path ) );
     291           0 :   FD_TEST( fd_cstr_printf_check( config->hugetlbfs.huge_page_mount_path,
     292           0 :     sizeof(config->hugetlbfs.huge_page_mount_path),
     293           0 :     NULL,
     294           0 :     "%s/.huge",
     295           0 :     config->hugetlbfs.mount_path ) );
     296             : 
     297           0 :   ulong max_page_sz = fd_cstr_to_shmem_page_sz( config->hugetlbfs.max_page_size );
     298           0 :   if( FD_UNLIKELY( max_page_sz!=FD_SHMEM_HUGE_PAGE_SZ && max_page_sz!=FD_SHMEM_GIGANTIC_PAGE_SZ ) ) FD_LOG_ERR(( "[hugetlbfs.max_page_size] must be \"huge\" or \"gigantic\"" ));
     299             : 
     300           0 :   replace( config->log.path, "{user}", config->user );
     301           0 :   replace( config->log.path, "{name}", config->name );
     302             : 
     303           0 :   if( FD_LIKELY( !strcmp( "auto", config->log.colorize ) ) )       config->log.colorize1 = 2;
     304           0 :   else if( FD_LIKELY( !strcmp( "true", config->log.colorize ) ) )  config->log.colorize1 = 1;
     305           0 :   else if( FD_LIKELY( !strcmp( "false", config->log.colorize ) ) ) config->log.colorize1 = 0;
     306           0 :   else  FD_LOG_ERR(( "[log.colorize] must be one of \"auto\", \"true\", or \"false\"" ));
     307             : 
     308           0 :   if( FD_LIKELY( 2==config->log.colorize1 ) ) {
     309           0 :     config->log.colorize1 = fd_log_should_colorize();
     310           0 :   }
     311             : 
     312           0 :   config->log.level_logfile1 = parse_log_level( config->log.level_logfile );
     313           0 :   config->log.level_stderr1  = parse_log_level( config->log.level_stderr );
     314           0 :   config->log.level_flush1   = parse_log_level( config->log.level_flush );
     315           0 :   if( FD_UNLIKELY( -1==config->log.level_logfile1 ) ) FD_LOG_ERR(( "unrecognized [log.level_logfile] `%s`", config->log.level_logfile ));
     316           0 :   if( FD_UNLIKELY( -1==config->log.level_stderr1 ) )  FD_LOG_ERR(( "unrecognized [log.level_stderr] `%s`", config->log.level_stderr ));
     317           0 :   if( FD_UNLIKELY( -1==config->log.level_flush1 ) )   FD_LOG_ERR(( "unrecognized [log.level_flush] `%s`", config->log.level_flush ));
     318             : 
     319           0 :   config->development.core_dump_level = parse_core_dump_level( config->development.core_dump );
     320           0 :   if( FD_UNLIKELY( -1==config->development.core_dump_level ) ) FD_LOG_ERR(( "unrecognized [development.core_dump] `%s`", config->development.core_dump ));
     321             : 
     322           0 :   replace( config->paths.base, "{user}", config->user );
     323           0 :   replace( config->paths.base, "{name}", config->name );
     324             : 
     325           0 :   if( FD_UNLIKELY( !strcmp( config->paths.identity_key, "" ) ) ) {
     326           0 :     if( FD_UNLIKELY( config->is_live_cluster ) ) FD_LOG_ERR(( "configuration file must specify [consensus.identity_path] when joining a live cluster" ));
     327             : 
     328           0 :     FD_TEST( fd_cstr_printf_check( config->paths.identity_key,
     329           0 :                                    sizeof(config->paths.identity_key),
     330           0 :                                    NULL,
     331           0 :                                    "%s/identity.json",
     332           0 :                                    config->paths.base ) );
     333           0 :   } else {
     334           0 :     replace( config->paths.identity_key, "{user}", config->user );
     335           0 :     replace( config->paths.identity_key, "{name}", config->name );
     336           0 :   }
     337             : 
     338           0 :   replace( config->paths.vote_account, "{user}", config->user );
     339           0 :   replace( config->paths.vote_account, "{name}", config->name );
     340             : 
     341           0 :   if( FD_UNLIKELY( strcmp( config->paths.snapshots, "" ) ) ) {
     342           0 :     replace( config->paths.snapshots, "{user}", config->user );
     343           0 :     replace( config->paths.snapshots, "{name}", config->name );
     344           0 :   } else {
     345           0 :     FD_TEST( fd_cstr_printf_check( config->paths.snapshots, sizeof(config->paths.snapshots), NULL, "%s/snapshots", config->paths.base ) );
     346           0 :   }
     347             : 
     348           0 :   if( FD_UNLIKELY( strcmp( config->paths.genesis, "" ) ) ) {
     349           0 :     replace( config->paths.genesis, "{user}", config->user );
     350           0 :     replace( config->paths.genesis, "{name}", config->name );
     351           0 :   } else {
     352           0 :     FD_TEST( fd_cstr_printf_check( config->paths.genesis, sizeof(config->paths.genesis), NULL, "%s/genesis.bin", config->paths.base ) );
     353           0 :   }
     354             : 
     355           0 :   long ts = -fd_log_wallclock();
     356           0 :   config->tick_per_ns_mu = dev ? fd_tempo_tick_per_ns_dev( &config->tick_per_ns_sigma )
     357           0 :                                : fd_tempo_tick_per_ns    ( &config->tick_per_ns_sigma );
     358           0 :   FD_LOG_INFO(( "calibrating fd_tempo tick_per_ns took %ld ms", (fd_log_wallclock()+ts)/(1000L*1000L) ));
     359             : 
     360           0 :   if( 0!=strcmp( config->net.bind_address, "" ) ) {
     361           0 :     if( FD_UNLIKELY( !fd_cstr_to_ip4_addr( config->net.bind_address, &config->net.bind_address_parsed ) ) ) {
     362           0 :       FD_LOG_ERR(( "`net.bind_address` is not a valid IPv4 address" ));
     363           0 :     }
     364           0 :   }
     365             : 
     366           0 :   if(      FD_LIKELY( !strcmp( config->tiles.pack.schedule_strategy, "perf"     ) ) ) config->tiles.pack.schedule_strategy_enum = 0;
     367           0 :   else if( FD_LIKELY( !strcmp( config->tiles.pack.schedule_strategy, "balanced" ) ) ) config->tiles.pack.schedule_strategy_enum = 1;
     368           0 :   else if( FD_LIKELY( !strcmp( config->tiles.pack.schedule_strategy, "revenue"  ) ) ) {
     369           0 :     FD_LOG_ERR(( "the revenue scheduler has been removed.  Please update [tiles.pack.schedule_strategy]" ));
     370           0 :   }
     371           0 :   else FD_LOG_ERR(( "[tiles.pack.schedule_strategy] %s not recognized", config->tiles.pack.schedule_strategy ));
     372             : 
     373           0 :   fd_config_fill_net( config );
     374             : 
     375           0 :   if( FD_UNLIKELY( config->is_firedancer ) ) {
     376           0 :     fd_config_fillf( config );
     377           0 :   } else {
     378           0 :     fd_config_fillh( config );
     379           0 :   }
     380             : 
     381           0 :   if( FD_LIKELY( config->is_live_cluster) ) {
     382           0 :     if( FD_UNLIKELY( !config->development.sandbox ) )                            FD_LOG_ERR(( "trying to join a live cluster, but configuration disables the sandbox which is a development only feature" ));
     383           0 :     if( FD_UNLIKELY( config->development.no_clone ) )                            FD_LOG_ERR(( "trying to join a live cluster, but configuration disables multiprocess which is a development only feature" ));
     384           0 :     if( FD_UNLIKELY( config->development.bench.larger_max_cost_per_block ) )     FD_LOG_ERR(( "trying to join a live cluster, but configuration enables [development.bench.larger_max_cost_per_block] which is a development only feature" ));
     385           0 :     if( FD_UNLIKELY( config->development.bench.larger_shred_limits_per_block ) ) FD_LOG_ERR(( "trying to join a live cluster, but configuration enables [development.bench.larger_shred_limits_per_block] which is a development only feature" ));
     386           0 :     if( FD_UNLIKELY( config->development.bench.disable_blockstore_from_slot ) )  FD_LOG_ERR(( "trying to join a live cluster, but configuration has a non-zero value for [development.bench.disable_blockstore_from_slot] which is a development only feature" ));
     387           0 :     if( FD_UNLIKELY( config->development.bench.disable_status_cache ) )          FD_LOG_ERR(( "trying to join a live cluster, but configuration enables [development.bench.disable_status_cache] which is a development only feature" ));
     388           0 :   }
     389             : 
     390             :   /* When running a local cluster, some options are overriden by default
     391             :      to make starting and running in development environments a little
     392             :      easier and less strict. */
     393           0 :   if( FD_UNLIKELY( is_local_cluster ) ) {
     394           0 :     if( FD_LIKELY( !strcmp( config->paths.vote_account, "" ) ) ) {
     395           0 :       FD_TEST( fd_cstr_printf_check( config->paths.vote_account,
     396           0 :                                      sizeof( config->paths.vote_account ),
     397           0 :                                      NULL,
     398           0 :                                      "%s/vote-account.json",
     399           0 :                                      config->paths.base ) );
     400           0 :     }
     401             : 
     402           0 :     strncpy( config->cluster, "development", sizeof(config->cluster) );
     403             : 
     404           0 :     if( FD_UNLIKELY( !config->is_firedancer ) ) {
     405             :       /* By default only_known is true for validators to ensure secure
     406             :         snapshot download, but in development it doesn't matter and
     407             :         often the developer does not provide known peers. */
     408           0 :       config->frankendancer.rpc.only_known = 0;
     409             : 
     410             :       /* When starting from a new genesis block, this needs to be off else
     411             :         the validator will get stuck forever. */
     412           0 :       config->frankendancer.consensus.wait_for_vote_to_start_leader = 0;
     413             : 
     414             :       /* We have to wait until we get a snapshot before we can join a
     415             :         second validator to this one, so make this smaller than the
     416             :         default.  */
     417           0 :       config->frankendancer.snapshots.full_snapshot_interval_slots = fd_uint_min( config->frankendancer.snapshots.full_snapshot_interval_slots, 200U );
     418           0 :     }
     419           0 :   }
     420             : 
     421           0 :   if( FD_UNLIKELY( config->is_firedancer && strcmp( config->firedancer.consensus.wait_for_supermajority_with_bank_hash, "" ) && (!config->consensus.expected_shred_version || config->consensus.wait_for_vote_to_start_leader) ) ) {
     422           0 :     FD_LOG_ERR(( "Config option [consensus.wait_for_supermajority_with_bank_hash] requires consensus.expected_shred_version!=0 and consensus.wait_for_vote_to_start_leader==false." ));
     423           0 :   }
     424             : 
     425           0 :   if( FD_UNLIKELY( config->is_firedancer && config->is_live_cluster && cluster!=FD_CLUSTER_TESTNET ) )
     426           0 :     FD_LOG_ERR(( "Attempted to start against live cluster `%s`. Firedancer is not "
     427           0 :                  "ready for production deployment, has not been tested, and is "
     428           0 :                  "missing consensus critical functionality. Joining a live Solana "
     429           0 :                  "cluster may destabilize the network. Please do not attempt. You "
     430           0 :                  "can start against the testnet cluster by specifying the testnet "
     431           0 :                  "entrypoints from https://docs.solana.com/clusters under "
     432           0 :                  "[gossip.entrypoints] in your configuration file.", fd_genesis_cluster_name( cluster ) ));
     433           0 : }
     434             : 
     435          48 : #define CFG_HAS_NON_EMPTY( key ) do {                  \
     436          48 :   if( !strnlen( config->key, sizeof(config->key) ) ) { \
     437           0 :     FD_LOG_ERR(( "missing `%s`", #key ));              \
     438           0 :   }                                                    \
     439          48 : } while(0)
     440             : 
     441          90 : #define CFG_HAS_NON_ZERO( key ) do {                           \
     442          90 :   if( !config->key ) { FD_LOG_ERR(( "missing `%s`", #key )); } \
     443          90 : } while(0)
     444             : 
     445           6 : #define CFG_HAS_POW2( key ) do {                       \
     446           6 :   ulong value = (ulong)( config -> key );              \
     447           6 :   if( !value || !fd_ulong_is_pow2( value ) ) {         \
     448           0 :     FD_LOG_ERR(( "`%s` must be a power of 2", #key )); \
     449           0 :   }                                                    \
     450           6 : } while(0)
     451             : 
     452             : static void
     453           0 : fd_config_validatef( fd_configf_t const * config ) {
     454           0 :   CFG_HAS_NON_ZERO( layout.sign_tile_count );
     455           0 :   CFG_HAS_NON_ZERO( layout.resolv_tile_count );
     456           0 :   CFG_HAS_NON_ZERO( layout.execle_tile_count );
     457           0 :   if( FD_UNLIKELY( config->layout.sign_tile_count < 2 ) ) {
     458           0 :     FD_LOG_ERR(( "layout.sign_tile_count must be >= 2" ));
     459           0 :   }
     460             : 
     461           0 :   if( FD_UNLIKELY( config->snapshots.sources.gossip.allow_any && config->snapshots.sources.gossip.allow_list_cnt>0UL ) ) {
     462           0 :     FD_LOG_ERR(( "`snapshots.sources.gossip` has an explicit list of %lu allowed peer(s) in `allow_list` "
     463           0 :                  "but also allows any peer with `allow_any=true`. `allow_list` has no effect and may "
     464           0 :                  "give a false sense of security. Please modify one of the two options and restart.",
     465           0 :                  config->snapshots.sources.gossip.allow_list_cnt ));
     466           0 :   }
     467           0 :   for( ulong i=0UL; i<config->snapshots.sources.gossip.allow_list_cnt; i++ ) {
     468           0 :     for( ulong j=0UL; j<config->snapshots.sources.gossip.block_list_cnt; j++ ) {
     469           0 :       if( FD_UNLIKELY( 0==strcmp( config->snapshots.sources.gossip.allow_list[ i ], config->snapshots.sources.gossip.block_list[ j ] ) ) ) {
     470           0 :         FD_LOG_ERR(( "`snapshots.sources.gossip` has repeated public key `%s` in both allow[%lu] and block[%lu] lists.  "
     471           0 :                      "Please modify one of the two options and restart.", config->snapshots.sources.gossip.allow_list[ i ], i, j ));
     472             : 
     473           0 :       }
     474           0 :     }
     475           0 :   }
     476             : 
     477           0 :   CFG_HAS_NON_ZERO( snapshots.wait_for_peers_timeout_seconds );
     478             : 
     479           0 :   CFG_HAS_NON_ZERO( accounts.max_accounts   );
     480           0 :   CFG_HAS_NON_ZERO( accounts.cache_size_gib );
     481             : 
     482           0 :   CFG_HAS_NON_ZERO( runtime.program_cache.mean_cache_entry_size );
     483           0 :   CFG_HAS_NON_ZERO( runtime.program_cache.heap_size_mib );
     484           0 :   if( config->runtime.program_cache.mean_cache_entry_size < 4096 ) { FD_LOG_ERR(( "`%s` must be >= 4096", "runtime.program_cache.mean_cache_entry_size" )); }
     485           0 :   if( config->runtime.program_cache.heap_size_mib < 32 ) { FD_LOG_ERR(( "`%s` must be >= 32", "runtime.program_cache.heap_size_mib" )); }
     486           0 : }
     487             : 
     488             : static void
     489           3 : fd_config_validateh( fd_configh_t const * config ) {
     490           3 :   CFG_HAS_NON_EMPTY( dynamic_port_range );
     491             : 
     492           3 :   CFG_HAS_NON_EMPTY( ledger.snapshot_archive_format );
     493             : 
     494           3 :   CFG_HAS_NON_ZERO( snapshots.full_snapshot_interval_slots );
     495           3 :   CFG_HAS_NON_ZERO( snapshots.incremental_snapshot_interval_slots );
     496           3 :   CFG_HAS_NON_ZERO( snapshots.minimum_snapshot_download_speed );
     497           3 :   CFG_HAS_NON_ZERO( snapshots.maximum_snapshot_download_abort );
     498             : 
     499           3 :   CFG_HAS_NON_EMPTY( layout.agave_affinity );
     500           3 :   CFG_HAS_NON_ZERO ( layout.resolh_tile_count );
     501           3 :   CFG_HAS_NON_ZERO ( layout.bank_tile_count );
     502           3 : }
     503             : 
     504             : void
     505           3 : fd_config_validate( fd_config_t const * config ) {
     506           3 :   if( FD_LIKELY( config->is_firedancer ) ) {
     507           0 :     fd_config_validatef( &config->firedancer );
     508           3 :   } else {
     509           3 :     fd_config_validateh( &config->frankendancer );
     510           3 :   }
     511             : 
     512           3 :   CFG_HAS_NON_EMPTY( name );
     513           3 :   CFG_HAS_NON_EMPTY( paths.base );
     514             : 
     515           3 :   CFG_HAS_NON_EMPTY( log.colorize );
     516           3 :   CFG_HAS_NON_EMPTY( log.level_logfile );
     517           3 :   CFG_HAS_NON_EMPTY( log.level_stderr );
     518           3 :   CFG_HAS_NON_EMPTY( log.level_flush );
     519             : 
     520           3 :   CFG_HAS_NON_EMPTY( layout.affinity );
     521           3 :   CFG_HAS_NON_EMPTY( layout.blocklist_cores );
     522           3 :   CFG_HAS_NON_ZERO ( layout.net_tile_count );
     523           3 :   CFG_HAS_NON_ZERO ( layout.quic_tile_count );
     524           3 :   CFG_HAS_NON_ZERO ( layout.verify_tile_count );
     525           3 :   CFG_HAS_NON_ZERO ( layout.shred_tile_count );
     526             : 
     527           3 :   CFG_HAS_NON_EMPTY( hugetlbfs.mount_path );
     528           3 :   CFG_HAS_NON_EMPTY( hugetlbfs.max_page_size );
     529             : 
     530           3 :   CFG_HAS_NON_ZERO( net.ingress_buffer_size );
     531           3 :   if( 0==strcmp( config->net.provider, "xdp" ) ) {
     532           3 :     CFG_HAS_NON_EMPTY( net.xdp.xdp_mode );
     533             : 
     534           3 :     if( 0!=strcmp( config->net.xdp.poll_mode, "prefbusy" ) && 0!=strcmp( config->net.xdp.poll_mode, "softirq" ) ) {
     535           0 :       FD_LOG_ERR(( "invalid `net.xdp.poll_mode`: must be \"prefbusy\" or \"softirq\"" ));
     536           0 :     }
     537             : 
     538           3 :     CFG_HAS_POW2     ( net.xdp.xdp_rx_queue_size );
     539           3 :     CFG_HAS_POW2     ( net.xdp.xdp_tx_queue_size );
     540           3 :     if( 0!=strcmp( config->net.xdp.rss_queue_mode, "dedicated" ) &&
     541           3 :         0!=strcmp( config->net.xdp.rss_queue_mode, "simple" ) &&
     542           3 :         0!=strcmp( config->net.xdp.rss_queue_mode, "auto" ) ) {
     543           0 :       FD_LOG_ERR(( "invalid `net.xdp.rss_queue_mode`: \"%s\"; must be \"simple\", \"dedicated\", or \"auto\"",
     544           0 :                    config->net.xdp.rss_queue_mode  ));
     545           0 :     }
     546           3 :   } else if( 0==strcmp( config->net.provider, "socket" ) ) {
     547           0 :     CFG_HAS_NON_ZERO( net.socket.receive_buffer_size );
     548           0 :     CFG_HAS_NON_ZERO( net.socket.send_buffer_size );
     549           0 :   } else {
     550           0 :     FD_LOG_ERR(( "invalid `net.provider`: must be \"xdp\" or \"socket\"" ));
     551           0 :   }
     552             : 
     553           3 :   CFG_HAS_NON_ZERO( tiles.netlink.max_routes           );
     554           3 :   CFG_HAS_NON_ZERO( tiles.netlink.max_peer_routes      );
     555           3 :   CFG_HAS_NON_ZERO( tiles.netlink.max_neighbors        );
     556             : 
     557           3 :   CFG_HAS_NON_ZERO( tiles.quic.max_concurrent_connections );
     558           3 :   CFG_HAS_NON_ZERO( tiles.quic.txn_reassembly_count );
     559           3 :   CFG_HAS_NON_ZERO( tiles.quic.max_concurrent_handshakes );
     560           3 :   CFG_HAS_NON_ZERO( tiles.quic.idle_timeout_millis );
     561             : 
     562           3 :   CFG_HAS_NON_ZERO( tiles.verify.signature_cache_size );
     563           3 :   CFG_HAS_NON_ZERO( tiles.verify.receive_buffer_size );
     564             : 
     565           3 :   CFG_HAS_NON_ZERO( tiles.dedup.signature_cache_size );
     566             : 
     567           3 :   CFG_HAS_NON_ZERO( tiles.pack.max_pending_transactions );
     568             : 
     569           3 :   CFG_HAS_NON_ZERO( tiles.shred.max_pending_shred_sets );
     570             : 
     571           3 :   if( config->is_firedancer ) {
     572           0 :     CFG_HAS_POW2( tiles.repair.slot_max );
     573           0 :   }
     574             : 
     575           3 :   if( FD_UNLIKELY( config->tiles.bundle.keepalive_interval_millis <    3000 ||
     576           3 :                    config->tiles.bundle.keepalive_interval_millis > 3600000 ) ) {
     577           0 :     FD_LOG_ERR(( "`tiles.bundle.keepalive_interval_millis` must be in range [3000, 3,600,000]" ));
     578           0 :   }
     579             : 
     580           3 :   CFG_HAS_NON_EMPTY( development.core_dump );
     581             : 
     582           3 :   CFG_HAS_NON_ZERO( development.genesis.target_tick_duration_micros );
     583           3 :   CFG_HAS_NON_ZERO( development.genesis.ticks_per_slot );
     584           3 :   CFG_HAS_NON_ZERO( development.genesis.fund_initial_accounts );
     585           3 :   CFG_HAS_NON_ZERO( development.genesis.fund_initial_amount_lamports );
     586             : 
     587           3 :   CFG_HAS_NON_ZERO ( development.bench.benchg_tile_count );
     588           3 :   CFG_HAS_NON_ZERO ( development.bench.benchs_tile_count );
     589           3 :   CFG_HAS_NON_EMPTY( development.bench.affinity );
     590             : 
     591           3 :   CFG_HAS_NON_ZERO( development.bundle.ssl_heap_size_mib );
     592           3 : }
     593             : 
     594             : #undef CFG_HAS_NON_EMPTY
     595             : #undef CFG_HAS_NON_ZERO
     596             : #undef CFG_HAS_POW2
     597             : 
     598             : void
     599             : fd_config_load( int           is_firedancer,
     600             :                 int           is_local_cluster,
     601             :                 char const *  default_config,
     602             :                 ulong         default_config_sz,
     603             :                 char const *  override_config,
     604             :                 char const *  override_config_path,
     605             :                 ulong         override_config_sz,
     606             :                 char const *  user_config,
     607             :                 ulong         user_config_sz,
     608             :                 char const *  user_config_path,
     609             :                 fd_config_t * config,
     610           0 :                 int           dev ) {
     611           0 :   memset( config, 0, sizeof(config_t) );
     612           0 :   config->is_firedancer = is_firedancer;
     613           0 :   config->boot_timestamp_nanos = fd_log_wallclock();
     614             : 
     615           0 :   fd_config_load_buf( config, default_config, default_config_sz, "default.toml" );
     616           0 :   fd_config_validate( config );
     617           0 :   if( FD_UNLIKELY( override_config ) ) {
     618           0 :     fd_config_load_buf( config, override_config, override_config_sz, override_config_path );
     619           0 :     fd_config_validate( config );
     620           0 :   }
     621           0 :   if( FD_LIKELY( user_config ) ) {
     622           0 :     fd_config_load_buf( config, user_config, user_config_sz, user_config_path );
     623           0 :     fd_config_validate( config );
     624           0 :   }
     625             : 
     626           0 :   fd_config_fill( config, is_local_cluster, dev );
     627           0 : }
     628             : 
     629             : int
     630           0 : fd_config_to_memfd( fd_config_t const * config ) {
     631           0 :   int config_memfd = memfd_create( "fd_config", 0 );
     632           0 :   if( FD_UNLIKELY( -1==config_memfd ) ) return -1;
     633           0 :   if( FD_UNLIKELY( -1==ftruncate( config_memfd, sizeof( config_t ) ) ) ) {
     634           0 :     if( FD_UNLIKELY( close( config_memfd ) ) ) FD_LOG_WARNING(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     635           0 :     return -1;
     636           0 :   }
     637             : 
     638           0 :   uchar * bytes = mmap( NULL, sizeof( config_t ), PROT_READ|PROT_WRITE, MAP_SHARED, config_memfd, 0 );
     639           0 :   if( FD_UNLIKELY( bytes==MAP_FAILED ) ) {
     640           0 :     if( FD_UNLIKELY( close( config_memfd ) ) ) FD_LOG_WARNING(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     641           0 :     return -1;
     642           0 :   }
     643             : 
     644           0 :   fd_memcpy( bytes, config, sizeof( config_t ) );
     645           0 :   if( FD_UNLIKELY( munmap( bytes, sizeof( config_t ) ) ) ) {
     646           0 :     FD_LOG_WARNING(( "munmap() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     647           0 :     return -1;
     648           0 :   }
     649             : 
     650           0 :   return config_memfd;
     651           0 : }

Generated by: LCOV version 1.14