LCOV - code coverage report
Current view: top level - app/shared - fd_config.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 312 502 62.2 %
Date: 2025-11-07 04:31:33 Functions: 11 12 91.7 %

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

Generated by: LCOV version 1.14