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