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