Line data Source code
1 : #define _GNU_SOURCE
2 : #include "fd_bundle_tile_private.h"
3 : #include "fd_bundle_tile.h"
4 : #include "../fd_txn_m.h"
5 : #include "../metrics/fd_metrics.h"
6 : #include "../topo/fd_topo.h"
7 : #include "../keyguard/fd_keyload.h"
8 : #include "../waker/fd_waker.h"
9 : #include "../../waltz/http/fd_url.h"
10 : #include "../../ballet/hex/fd_hex.h"
11 : #include <errno.h>
12 :
13 : #include <dirent.h> /* opendir */
14 : #include <stdio.h> /* snprintf */
15 : #include <fcntl.h> /* F_SETFL */
16 : #include <unistd.h> /* close */
17 : #include <sys/mman.h> /* PROT_READ (seccomp) */
18 : #include <sys/uio.h> /* writev */
19 : #include <netinet/in.h> /* AF_INET */
20 : #include <netinet/tcp.h> /* TCP_FASTOPEN_CONNECT (seccomp) */
21 : #include "../../waltz/resolv/fd_netdb.h"
22 : #include "../../discof/replay/fd_replay_tile.h"
23 :
24 : #include <linux/futex.h>
25 : #include "generated/fd_bundle_tile_seccomp.h"
26 :
27 0 : #define IN_KIND_REPLAY_OUT (1)
28 :
29 0 : #define STEM_BURST (5UL)
30 : FD_STATIC_ASSERT( FD_BUNDLE_CLIENT_MAX_TXN_PER_BUNDLE<=STEM_BURST, stem_burst );
31 :
32 : /* Raw state values exported by the metrics (states= enums) */
33 : FD_STATIC_ASSERT( FD_BUNDLE_STATE_DISCONNECTED==FD_METRICS_ENUM_BUNDLE_CONN_STATE_V_DISCONNECTED_IDX, metrics_state );
34 : FD_STATIC_ASSERT( FD_BUNDLE_STATE_CONNECTING==FD_METRICS_ENUM_BUNDLE_CONN_STATE_V_CONNECTING_IDX, metrics_state );
35 : FD_STATIC_ASSERT( FD_BUNDLE_STATE_CONNECTED==FD_METRICS_ENUM_BUNDLE_CONN_STATE_V_CONNECTED_IDX, metrics_state );
36 : FD_STATIC_ASSERT( FD_BUNDLE_STATE_SLEEPING==FD_METRICS_ENUM_BUNDLE_CONN_STATE_V_SLEEPING_IDX, metrics_state );
37 :
38 : /* hysteresis thresholds to avoid bouncing (e.g. during forks) */
39 0 : #define FD_BUNDLE_SLEEP_THRESHOLD_SLOTS (450UL)
40 0 : #define FD_BUNDLE_WAKE_THRESHOLD_SLOTS (400UL)
41 0 : #define FD_BUNDLE_SLEEP_CHECK_INTERVAL_NS ((long)5e9)
42 :
43 : FD_FN_CONST static ulong
44 0 : scratch_align( void ) {
45 0 : return fd_ulong_max( fd_ulong_max( alignof(fd_bundle_tile_t), fd_grpc_client_align() ), pending_txn_align() );
46 0 : }
47 :
48 : FD_FN_CONST static ulong
49 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
50 0 : ulong pending_max = tile->bundle.out_depth;
51 0 : ulong l = FD_LAYOUT_INIT;
52 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_bundle_tile_t), sizeof(fd_bundle_tile_t) );
53 0 : l = FD_LAYOUT_APPEND( l, fd_grpc_client_align(), fd_grpc_client_footprint( tile->bundle.buf_sz ) );
54 0 : l = FD_LAYOUT_APPEND( l, pending_txn_align(), pending_txn_footprint( pending_max ) );
55 0 : return FD_LAYOUT_FINI( l, scratch_align() );
56 0 : }
57 :
58 : static void
59 0 : fd_bundle_tile_maybe_sleep( fd_bundle_tile_t * ctx, long now_ns ) {
60 0 : if( FD_UNLIKELY( !ctx->replay_in.mem ) ) return;
61 0 : if( FD_LIKELY( now_ns < ctx->sleep_check_ns ) ) return;
62 0 : ctx->sleep_check_ns = now_ns + FD_BUNDLE_SLEEP_CHECK_INTERVAL_NS;
63 :
64 0 : ulong next_leader_slot = ctx->next_leader_slot;
65 0 : ulong reset_slot = ctx->reset_slot;
66 :
67 : /* Either don't know the leader schedule yet or have no upcoming
68 : leader slots. Sleep. */
69 0 : if( FD_UNLIKELY( next_leader_slot==ULONG_MAX || reset_slot==ULONG_MAX ) ) {
70 0 : if( !ctx->sleep_mode ) {
71 0 : ctx->sleep_mode = 1;
72 0 : FD_LOG_INFO(( "Bundle tile entering sleep mode: no upcoming leader slots" ));
73 0 : }
74 0 : return;
75 0 : }
76 :
77 0 : ulong slots_until_leader = fd_ulong_sat_sub( next_leader_slot, reset_slot );
78 :
79 0 : if( ctx->sleep_mode ) {
80 0 : if( slots_until_leader <= FD_BUNDLE_WAKE_THRESHOLD_SLOTS ) {
81 0 : ctx->sleep_mode = 0;
82 0 : ctx->next_step_deadline = 0L;
83 0 : ctx->last_bundle_status_log_nanos = now_ns;
84 0 : FD_LOG_INFO(( "Bundle tile waking up: next leader slot %lu (~%lu slots away)", next_leader_slot, slots_until_leader ));
85 0 : }
86 0 : } else {
87 : /* reset_slot stays at/near next_leader_slot throughout a leader
88 : rotation and only jumps to the next rotation after leadership
89 : ends, so this cannot trigger mid-rotation. */
90 0 : if( slots_until_leader > FD_BUNDLE_SLEEP_THRESHOLD_SLOTS ) {
91 0 : ctx->sleep_mode = 1;
92 0 : FD_LOG_INFO(( "Bundle tile entering sleep mode: next leader slot %lu (~%lu slots away)", next_leader_slot, slots_until_leader ));
93 0 : }
94 0 : }
95 0 : }
96 :
97 : static inline void
98 0 : metrics_write( fd_bundle_tile_t * ctx ) {
99 0 : FD_MCNT_SET ( BUNDLE, TXN_RX, ctx->metrics.txn_received_cnt );
100 0 : FD_MCNT_SET ( BUNDLE, BUNDLE_RX, ctx->metrics.bundle_received_cnt );
101 0 : FD_MCNT_SET ( BUNDLE, STANDALONE_TXN_RX, ctx->metrics.packet_received_cnt );
102 0 : FD_MCNT_SET ( BUNDLE, PROTOBUF_RX_BYTES, ctx->metrics.proto_received_bytes );
103 0 : FD_MCNT_SET ( BUNDLE, PING_ACKED, ctx->metrics.ping_ack_cnt );
104 0 : FD_MCNT_SET ( BUNDLE, CLIENT_FAILED_DECODE, ctx->metrics.decode_fail_cnt );
105 0 : FD_MCNT_SET ( BUNDLE, CLIENT_FAILED_TRANSPORT, ctx->metrics.transport_fail_cnt );
106 0 : FD_MCNT_SET ( BUNDLE, CLIENT_FAILED_NO_FEE_INFO, ctx->metrics.missing_builder_info_fail_cnt );
107 0 : FD_MGAUGE_SET( BUNDLE, TXN_PENDING, pending_txn_cnt( ctx->pending_txns ) );
108 0 : FD_MCNT_SET ( BUNDLE, TXN_BUFFER_FULL_DROPPED, ctx->metrics.backpressure_drop_cnt );
109 :
110 0 : FD_MGAUGE_SET( BUNDLE, LAST_RTT_NANOS, (ulong)ctx->rtt->latest_rtt );
111 0 : FD_MGAUGE_SET( BUNDLE, RTT_SMOOTHED_NANOS, (ulong)ctx->rtt->smoothed_rtt );
112 0 : FD_MGAUGE_SET( BUNDLE, RTT_DEVIATION_NANOS, (ulong)ctx->rtt->var_rtt );
113 :
114 0 : FD_MHIST_COPY( BUNDLE, MESSAGE_RX_DELAY_NANOS, ctx->metrics.msg_rx_delay );
115 :
116 0 : fd_wksp_t * wksp = fd_wksp_containing( ctx );
117 0 : fd_wksp_usage_t usage[1];
118 0 : ulong const free_tag = 0UL;
119 0 : if( FD_UNLIKELY( !fd_wksp_usage( wksp, &free_tag, 1UL, usage ) ) ) {
120 0 : FD_LOG_ERR(( "fd_wksp_usage failed" )); /* unreachable */
121 0 : }
122 0 : FD_MGAUGE_SET( BUNDLE, HEAP_CAPACITY_BYTES, usage->total_sz );
123 0 : FD_MGAUGE_SET( BUNDLE, HEAP_FREE_BYTES, usage->free_sz );
124 :
125 0 : int status = fd_bundle_client_status( ctx );
126 0 : ulong state = (ulong)status;
127 0 : if( FD_UNLIKELY( ctx->sleep_mode ) ) state = FD_BUNDLE_STATE_SLEEPING;
128 :
129 0 : FD_MGAUGE_SET( BUNDLE, CONN_STATE, state );
130 0 : ctx->bundle_status_recent = (uchar)state;
131 0 : }
132 :
133 : void
134 0 : fd_bundle_tile_housekeeping( fd_bundle_tile_t * ctx ) {
135 0 : long log_interval_ns = (long)30e9;
136 0 : int status = fd_bundle_client_status( ctx );
137 0 : long log_next_ns = ctx->last_bundle_status_log_nanos + log_interval_ns;
138 0 : long now_ns = fd_log_wallclock();
139 :
140 0 : if( FD_UNLIKELY( !ctx->sleep_mode && status!=FD_BUNDLE_STATE_CONNECTED && now_ns>log_next_ns ) ) {
141 0 : FD_LOG_WARNING(( "No bundle server connection in the last %ld seconds", log_interval_ns/(long)1e9 ) );
142 0 : ctx->last_bundle_status_log_nanos = now_ns;
143 0 : }
144 :
145 0 : if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
146 0 : if( ctx->tcp_sock>=0 ) fd_bundle_client_reset( ctx );
147 0 : ctx->halt_signing = 1;
148 0 : fd_memcpy( ctx->auther.pubkey, ctx->keyswitch->bytes, 32UL );
149 0 : fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
150 0 : }
151 :
152 0 : if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_UNHALT_PENDING ) ) {
153 0 : ctx->defer_reset = 1;
154 0 : ctx->sleep_check_ns = 0;
155 0 : ctx->halt_signing = 0;
156 0 : ctx->next_step_deadline = 0L; /* the socket was closed outside a step: step now */
157 0 : fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
158 0 : }
159 :
160 0 : fd_bundle_tile_maybe_sleep( ctx, now_ns );
161 0 : }
162 :
163 : static void
164 : fd_bundle_tile_publish_block_engine_update(
165 : fd_bundle_tile_t * ctx,
166 : fd_stem_context_t * stem
167 0 : ) {
168 0 : fd_bundle_block_engine_update_t * update =
169 0 : fd_chunk_to_laddr( ctx->plugin_out.mem, ctx->plugin_out.chunk );
170 0 : memset( update, 0, sizeof(fd_bundle_block_engine_update_t) );
171 :
172 0 : strncpy( update->name, "jito", sizeof(update->name) );
173 :
174 0 : FD_TEST( fd_cstr_printf_check( update->url, sizeof(update->url), NULL,
175 0 : "%s://%.*s:%u",
176 0 : ctx->is_ssl ? "https" : "http",
177 0 : (int)ctx->server_fqdn_len,
178 0 : ctx->server_fqdn,
179 0 : ctx->server_tcp_port ) );
180 :
181 : /* Format IPv4 string */
182 0 : snprintf( update->ip_cstr, sizeof(update->ip_cstr),
183 0 : FD_IP4_ADDR_FMT,
184 0 : FD_IP4_ADDR_FMT_ARGS( ctx->server_ip4_addr ) );
185 :
186 0 : ulong tspub = (ulong)fd_frag_meta_ts_comp( fd_bundle_now( ctx ) );
187 0 : fd_stem_publish(
188 0 : stem,
189 0 : ctx->plugin_out.idx,
190 0 : (ulong)ctx->bundle_status_recent,
191 0 : ctx->plugin_out.chunk,
192 0 : sizeof(fd_bundle_block_engine_update_t),
193 0 : 0UL, /* ctl */
194 0 : 0UL, /* seq */
195 0 : tspub
196 0 : );
197 0 : ctx->plugin_out.chunk = fd_dcache_compact_next( ctx->plugin_out.chunk, sizeof(fd_bundle_block_engine_update_t), ctx->plugin_out.chunk0, ctx->plugin_out.wmark );
198 0 : }
199 :
200 : static void
201 : during_frag( fd_bundle_tile_t * ctx,
202 : ulong in_idx,
203 : ulong seq FD_PARAM_UNUSED,
204 : ulong sig,
205 : ulong chunk,
206 : ulong sz FD_PARAM_UNUSED,
207 0 : ulong ctl FD_PARAM_UNUSED ) {
208 :
209 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_REPLAY_OUT ) ) {
210 0 : if( FD_LIKELY( sig!=REPLAY_SIG_RESET ) ) return;
211 0 : if( FD_UNLIKELY( chunk<ctx->replay_in.chunk0 || chunk>ctx->replay_in.wmark || sz!=sizeof(fd_poh_reset_t) ) )
212 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->replay_in.chunk0, ctx->replay_in.wmark ));
213 :
214 0 : fd_poh_reset_t const * reset = fd_chunk_to_laddr_const( ctx->replay_in.mem, chunk );
215 0 : ctx->next_leader_slot_staged = reset->next_leader_slot;
216 0 : ctx->reset_slot_staged = reset->completed_slot;
217 0 : }
218 0 : }
219 :
220 : static void
221 : after_frag( fd_bundle_tile_t * ctx,
222 : ulong in_idx,
223 : ulong seq FD_PARAM_UNUSED,
224 : ulong sig,
225 : ulong sz FD_PARAM_UNUSED,
226 : ulong tsorig FD_PARAM_UNUSED,
227 : ulong tspub FD_PARAM_UNUSED,
228 0 : fd_stem_context_t * stem FD_PARAM_UNUSED ) {
229 :
230 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_REPLAY_OUT ) ) {
231 0 : if( FD_LIKELY( sig!=REPLAY_SIG_RESET ) ) return;
232 0 : ctx->next_leader_slot = ctx->next_leader_slot_staged;
233 0 : ctx->reset_slot = ctx->reset_slot_staged;
234 0 : }
235 0 : }
236 :
237 : static long
238 0 : next_deadline( fd_bundle_tile_t * ctx ) {
239 0 : if( FD_UNLIKELY( ctx->halt_signing || ctx->sleep_mode || ctx->next_step_deadline==LONG_MAX ) ) return LONG_MAX;
240 0 : return fd_clock_tile_wallclock_to_tickcount( ctx->clock, ctx->next_step_deadline );
241 0 : }
242 :
243 : static void
244 : before_credit( fd_bundle_tile_t * ctx,
245 : fd_stem_context_t * stem,
246 0 : int * charge_busy ) {
247 0 : if( FD_UNLIKELY( !ctx->stem ) ) {
248 0 : ctx->stem = stem;
249 0 : }
250 :
251 0 : if( FD_UNLIKELY( ctx->halt_signing || ctx->sleep_mode ) ) {
252 0 : if( ctx->sleep_mode && ctx->tcp_sock>=0 ) {
253 0 : fd_bundle_client_reset( ctx );
254 : /* Override backoff so we don't treat this as an error */
255 0 : ctx->backoff_until = 0;
256 0 : ctx->backoff_iter = 0;
257 0 : }
258 : /* The socket is closed so any wake (even one that raced the close)
259 : is stale: drain it or the stem never parks, and rearm so the next
260 : connection can wake us. */
261 0 : if( FD_UNLIKELY( fd_fseq_query( ctx->waker_fseq )==1UL ) ) {
262 0 : fd_fseq_update( ctx->waker_fseq, 0UL );
263 0 : fd_waker_client_rearm( ctx->waker_client_idx );
264 0 : }
265 0 : return;
266 0 : }
267 :
268 0 : if( pending_txn_empty( ctx->pending_txns ) ) {
269 0 : int fired = fd_fseq_query( ctx->waker_fseq )==1UL;
270 0 : long now = fd_bundle_now( ctx );
271 0 : if( FD_UNLIKELY( fired || now>=ctx->next_step_deadline ) ) {
272 0 : if( FD_LIKELY( fired ) ) fd_fseq_update( ctx->waker_fseq, 0UL );
273 0 : int busy = 0;
274 0 : fd_bundle_client_step( ctx, &busy );
275 0 : if( FD_LIKELY( fired ) ) fd_waker_client_rearm( ctx->waker_client_idx );
276 0 : *charge_busy = busy;
277 : /* A step that made progress may have more buffered work: re-step
278 : immediately. */
279 0 : ctx->next_step_deadline = busy ? 0L : fd_bundle_client_next_deadline( ctx, fd_bundle_now( ctx ) );
280 0 : }
281 0 : }
282 0 : }
283 :
284 : static void
285 : after_credit( fd_bundle_tile_t * ctx,
286 : fd_stem_context_t * stem,
287 : int * opt_poll_in,
288 0 : int * charge_busy ) {
289 0 : if( FD_UNLIKELY( fd_clock_tile_recal_due( ctx->clock ) ) ) fd_clock_tile_recal( ctx->clock );
290 :
291 0 : if( !pending_txn_empty( ctx->pending_txns ) ) {
292 0 : fd_bundle_pending_txn_t * head = pending_txn_peek_head( ctx->pending_txns );
293 0 : ulong drain_seq = head->bundle_seq;
294 0 : ulong drain_sig = head->sig;
295 0 : ulong drain_cnt = 0UL;
296 :
297 0 : do {
298 0 : fd_bundle_pending_txn_t const * txn = pending_txn_peek_head( ctx->pending_txns );
299 :
300 0 : fd_txn_m_t * txnm = fd_chunk_to_laddr( ctx->verify_out.mem, ctx->verify_out.chunk );
301 0 : *txnm = (fd_txn_m_t) {
302 0 : .reference_block_height = 0UL,
303 0 : .payload_sz = txn->payload_sz,
304 0 : .txn_t_sz = 0U,
305 0 : .source_ipv4 = txn->source_ipv4,
306 0 : .source_tpu = FD_TXN_M_TPU_SOURCE_BUNDLE,
307 0 : .first_seen_nanos = txn->first_seen_nanos,
308 0 : .block_engine = {
309 0 : .bundle_id = txn->bundle_seq,
310 0 : .bundle_txn_cnt = txn->bundle_txn_cnt,
311 0 : .commission = txn->commission,
312 0 : },
313 0 : };
314 0 : fd_memcpy( txnm->block_engine.commission_pubkey, txn->commission_pubkey, 32UL );
315 0 : fd_memcpy( fd_txn_m_payload( txnm ), txn->payload, txn->payload_sz );
316 :
317 0 : ulong sz = fd_txn_m_realized_footprint( txnm, 0, 0 );
318 0 : ulong tspub = (ulong)fd_frag_meta_ts_comp( fd_bundle_now( ctx ) );
319 0 : fd_stem_publish( stem, ctx->verify_out.idx, txn->sig, ctx->verify_out.chunk, sz, 0UL, 0UL, tspub );
320 0 : ctx->verify_out.chunk = fd_dcache_compact_next( ctx->verify_out.chunk, sz, ctx->verify_out.chunk0, ctx->verify_out.wmark );
321 :
322 0 : pending_txn_remove_head( ctx->pending_txns );
323 0 : drain_cnt++;
324 0 : } while( fd_bundle_drain_continue( ctx->pending_txns, drain_sig, drain_seq, drain_cnt, STEM_BURST ) );
325 :
326 0 : *charge_busy = 1;
327 0 : *opt_poll_in = 0;
328 0 : }
329 :
330 0 : if( ctx->plugin_out.mem ) {
331 0 : if( FD_UNLIKELY( ctx->bundle_status_recent != ctx->bundle_status_plugin ) ) {
332 0 : fd_bundle_tile_publish_block_engine_update( ctx, stem );
333 0 : ctx->bundle_status_plugin = (uchar)ctx->bundle_status_recent;
334 0 : *charge_busy = 1;
335 0 : }
336 0 : }
337 0 : }
338 :
339 : static void
340 : fd_bundle_tls_keylog_line( fd_bundle_tile_t * ctx,
341 : char const * label,
342 : uchar const client_random[ static 32 ],
343 : uchar const * secret,
344 0 : ulong secret_sz ) {
345 0 : char line[ 256 ];
346 0 : char * p = fd_cstr_init( line );
347 0 : p = fd_cstr_append_cstr( p, label );
348 0 : p = fd_hex_encode( p, client_random, 32UL );
349 0 : p = fd_cstr_append_char( p, ' ' );
350 0 : p = fd_hex_encode( p, secret, secret_sz );
351 0 : ulong line_sz = (ulong)(p-line);
352 0 : fd_cstr_fini( p );
353 :
354 0 : struct iovec iovs[2] = {
355 0 : { .iov_base=line, .iov_len=line_sz },
356 0 : { .iov_base=(void *)"\n", .iov_len=1UL },
357 0 : };
358 0 : if( FD_UNLIKELY( writev( ctx->keylog_fd, iovs, 2 )!=(long)(line_sz+1UL) ) ) {
359 0 : FD_LOG_WARNING(( "write(keylog) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
360 0 : }
361 0 : }
362 :
363 : static void
364 : fd_bundle_tls_keylog( void const * handshake,
365 : void const * recv_secret,
366 : void const * send_secret,
367 0 : uint encryption_level ) {
368 0 : fd_tlsrec_conn_t const * tls_conn = (fd_tlsrec_conn_t const *)(
369 0 : (ulong)handshake - offsetof(fd_tlsrec_conn_t, hs) );
370 0 : fd_bundle_tile_t * ctx = (fd_bundle_tile_t *)(
371 0 : (ulong)tls_conn - offsetof(fd_bundle_tile_t, tls_conn) );
372 0 : fd_tls_estate_t const * hs = handshake;
373 :
374 0 : char const * client_label;
375 0 : char const * server_label;
376 0 : switch( encryption_level ) {
377 0 : case FD_TLS_LEVEL_HANDSHAKE:
378 0 : client_label = "CLIENT_HANDSHAKE_TRAFFIC_SECRET ";
379 0 : server_label = "SERVER_HANDSHAKE_TRAFFIC_SECRET ";
380 0 : break;
381 0 : case FD_TLS_LEVEL_APPLICATION:
382 0 : client_label = "CLIENT_TRAFFIC_SECRET_0 ";
383 0 : server_label = "SERVER_TRAFFIC_SECRET_0 ";
384 0 : break;
385 0 : default:
386 0 : return;
387 0 : }
388 :
389 0 : uchar const * client_secret = hs->base.server ? recv_secret : send_secret;
390 0 : uchar const * server_secret = hs->base.server ? send_secret : recv_secret;
391 0 : fd_bundle_tls_keylog_line( ctx, client_label, hs->base.client_random,
392 0 : client_secret, 32UL );
393 0 : fd_bundle_tls_keylog_line( ctx, server_label, hs->base.client_random,
394 0 : server_secret, 32UL );
395 0 : }
396 :
397 : #ifndef FD_TILE_TEST
398 : static void
399 : fd_bundle_tile_parse_endpoint( fd_bundle_tile_t * ctx,
400 0 : fd_topo_tile_t const * tile ) {
401 0 : fd_url_t url[1];
402 0 : _Bool is_ssl = 0;
403 0 : if( FD_UNLIKELY( fd_url_parse_endpoint( url,
404 0 : tile->bundle.url,
405 0 : tile->bundle.url_len,
406 0 : &ctx->server_tcp_port,
407 0 : &is_ssl,
408 0 : "[tiles.bundle.url]" ) ) ) {
409 0 : FD_LOG_ERR(( "Could not parse [tiles.bundle.url]" ));
410 0 : }
411 0 : if( FD_UNLIKELY( url->host_len>=FD_FQDN_BUF_MAX ) ) {
412 0 : FD_LOG_CRIT(( "Invalid url->host_len" )); /* unreachable */
413 0 : }
414 0 : fd_cstr_fini( fd_cstr_append_text( fd_cstr_init( ctx->server_fqdn ), url->host, url->host_len ) );
415 0 : ctx->server_fqdn_len = url->host_len;
416 :
417 0 : if( FD_UNLIKELY( tile->bundle.sni_len ) ) {
418 0 : fd_cstr_fini( fd_cstr_append_text( fd_cstr_init( ctx->server_sni ), tile->bundle.sni, tile->bundle.sni_len ) );
419 0 : ctx->server_sni_len = tile->bundle.sni_len;
420 0 : } else {
421 0 : fd_cstr_fini( fd_cstr_append_text( fd_cstr_init( ctx->server_sni ), url->host, url->host_len ) );
422 0 : ctx->server_sni_len = url->host_len;
423 0 : }
424 :
425 0 : if( FD_UNLIKELY( ctx->server_sni_len>=sizeof(ctx->tls->server_name) ) ) {
426 0 : FD_LOG_ERR(( "Server name is %lu bytes, longer than the %lu byte maximum: "
427 0 : "check [tiles.bundle.url] and [tiles.bundle.tls_domain_name]",
428 0 : ctx->server_sni_len, sizeof(ctx->tls->server_name)-1UL ));
429 0 : }
430 :
431 0 : ctx->is_ssl = !!is_ssl;
432 0 : }
433 :
434 :
435 : static void
436 : fd_bundle_tile_init_tls( fd_bundle_tile_t * ctx,
437 0 : fd_topo_tile_t const * tile ) {
438 : /* Initialize native TLS */
439 0 : fd_tls_t * tls = fd_tls_join( fd_tls_new( ctx->tls ) );
440 :
441 0 : uchar rng_key[ FD_CHACHA_KEY_SZ ];
442 0 : if( FD_UNLIKELY( !fd_rng_secure( rng_key, sizeof(rng_key) ) ) ) FD_LOG_CRIT(( "fd_rng_secure failed" ));
443 0 : fd_chacha_rng_init( ctx->tls_rng, rng_key, FD_CHACHA_RNG_ALGO_CHACHA8 );
444 0 : fd_memzero_explicit( rng_key, sizeof(rng_key) );
445 0 : tls->rng = ctx->tls_rng;
446 0 : if( FD_LIKELY( ctx->keylog_fd>=0 ) ) tls->secrets_fn = fd_bundle_tls_keylog;
447 :
448 : /* ALPN: h2 */
449 0 : static uchar const alpn[] = { 2, 'h', '2' };
450 0 : fd_memcpy( tls->alpn, alpn, sizeof(alpn) );
451 0 : tls->alpn_sz = sizeof(alpn);
452 :
453 0 : if( FD_UNLIKELY( !ctx->is_ssl ) ) return; /* plaintext, nothing to verify */
454 :
455 0 : if( FD_UNLIKELY( !tile->bundle.tls_cert_verify ) ) {
456 0 : FD_LOG_WARNING(( "[tiles.bundle.tls_cert_verify] is disabled. The block engine "
457 0 : "certificate will not be verified. This is insecure." ));
458 0 : return;
459 0 : }
460 :
461 : /* Load system CA certificates */
462 0 : if( FD_UNLIKELY( fd_x509_ca_store_load_system( ctx->ca_store )<0L ) ) {
463 0 : FD_LOG_ERR(( "No CA certificate bundle found, cannot verify the block engine "
464 0 : "certificate. Install the system CA certificates, or set "
465 0 : "`[tiles.bundle] tls_cert_verify = false` to disable verification "
466 0 : "(insecure)." ));
467 0 : }
468 :
469 0 : tls->ca_store = ctx->ca_store;
470 0 : }
471 :
472 : static void
473 : privileged_init( fd_topo_t const * topo,
474 0 : fd_topo_tile_t const * tile ) {
475 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
476 :
477 0 : ulong const pending_max = tile->bundle.out_depth;
478 :
479 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
480 0 : fd_bundle_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_bundle_tile_t), sizeof(fd_bundle_tile_t) );
481 0 : void * grpc_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_grpc_client_align(), fd_grpc_client_footprint( tile->bundle.buf_sz ) );
482 0 : void * deque_mem = FD_SCRATCH_ALLOC_APPEND( l, pending_txn_align(), pending_txn_footprint( pending_max ) );
483 :
484 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
485 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
486 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
487 :
488 0 : memset( ctx, 0, sizeof(fd_bundle_tile_t) );
489 0 : ctx->grpc_client_mem = grpc_mem;
490 0 : ctx->grpc_buf_max = tile->bundle.buf_sz;
491 0 : ctx->tcp_sock = -1;
492 0 : ctx->waker_client_idx = tile->waker_client_idx;
493 0 : ctx->pending_txns = pending_txn_join( pending_txn_new( deque_mem, pending_max ) );
494 :
495 0 : fd_bundle_auther_init( &ctx->auther );
496 0 : uchar const * public_key = fd_keyload_load( tile->bundle.identity_key_path, 1 /* public key only */ );
497 0 : fd_memcpy( ctx->auther.pubkey, public_key, 32UL );
498 :
499 0 : ctx->keylog_fd = -1;
500 :
501 0 : if( FD_UNLIKELY( tile->bundle.key_log_path[0] ) ) {
502 0 : ctx->keylog_fd = open( tile->bundle.key_log_path, O_WRONLY|O_APPEND|O_CREAT, 0644 );
503 0 : if( FD_UNLIKELY( ctx->keylog_fd < 0 ) ) {
504 0 : FD_LOG_ERR(( "open(%s) failed (%i-%s)", tile->bundle.key_log_path, errno, fd_io_strerror( errno ) ));
505 0 : }
506 0 : }
507 :
508 0 : fd_bundle_tile_parse_endpoint( ctx, tile );
509 :
510 : /* Initialize native TLS before seccomp (reads CA certs from disk) */
511 0 : fd_bundle_tile_init_tls( ctx, tile );
512 :
513 : /* Init resolver */
514 0 : if( FD_UNLIKELY( !fd_netdb_open_fds( ctx->netdb_fds ) ) ) {
515 0 : FD_LOG_ERR(( "fd_netdb_open_fds failed" ));
516 0 : }
517 :
518 : /* Random seed for header hashmap */
519 0 : if( FD_UNLIKELY( !fd_rng_secure( &ctx->map_seed, sizeof(ulong) ) ) ) {
520 0 : FD_LOG_CRIT(( "fd_rng_secure failed" ));
521 0 : }
522 :
523 : /* Random seed for timing RNG */
524 0 : uint rng_seed;
525 0 : if( FD_UNLIKELY( !fd_rng_secure( &rng_seed, sizeof(uint) ) ) ) {
526 0 : FD_LOG_CRIT(( "fd_rng_secure failed" ));
527 0 : }
528 0 : if( FD_UNLIKELY( !fd_rng_join( fd_rng_new( &ctx->rng, rng_seed, 0UL ) ) ) ) {
529 0 : FD_LOG_CRIT(( "fd_rng_join failed" )); /* unreachable */
530 0 : }
531 0 : }
532 :
533 : static fd_bundle_out_ctx_t
534 : bundle_out_link( fd_topo_t const * topo,
535 : fd_topo_link_t const * link,
536 0 : ulong out_link_idx ) {
537 0 : fd_bundle_out_ctx_t out = {0};
538 0 : out.idx = out_link_idx;
539 0 : out.mem = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
540 0 : out.chunk0 = fd_dcache_compact_chunk0( out.mem, link->dcache );
541 0 : out.wmark = fd_dcache_compact_wmark ( out.mem, link->dcache, link->mtu );
542 0 : out.chunk = out.chunk0;
543 0 : return out;
544 0 : }
545 :
546 : static void
547 : unprivileged_init( fd_topo_t const * topo,
548 0 : fd_topo_tile_t const * tile ) {
549 0 : fd_bundle_tile_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
550 0 : if( FD_UNLIKELY( tile->kind_id!=0 ) ) {
551 0 : FD_LOG_ERR(( "There can only be one bundle tile" ));
552 0 : }
553 :
554 0 : fd_clock_tile_init( ctx->clock );
555 :
556 0 : ulong sign_in_idx = fd_topo_find_tile_in_link( topo, tile, "sign_bundle", tile->kind_id );
557 0 : if( FD_UNLIKELY( sign_in_idx==ULONG_MAX ) ) FD_LOG_ERR(( "Missing sign_bundle link" ));
558 0 : fd_topo_link_t const * sign_in = &topo->links[ tile->in_link_id[ sign_in_idx ] ];
559 :
560 0 : ulong sign_out_idx = fd_topo_find_tile_out_link( topo, tile, "bundle_sign", tile->kind_id );
561 0 : if( FD_UNLIKELY( sign_out_idx==ULONG_MAX ) ) FD_LOG_ERR(( "Missing bundle_sign link" ));
562 0 : fd_topo_link_t const * sign_out = &topo->links[ tile->out_link_id[ sign_out_idx ] ];
563 :
564 0 : fd_sleep_t * sleep = NULL;
565 0 : if( FD_UNLIKELY( topo->sleep_obj_id!=ULONG_MAX ) ) {
566 0 : sleep = fd_sleep_join( fd_topo_obj_laddr( topo, topo->sleep_obj_id ) );
567 0 : FD_TEST( sleep );
568 0 : }
569 :
570 0 : if( FD_UNLIKELY( !fd_keyguard_client_join( fd_keyguard_client_new(
571 0 : ctx->keyguard_client,
572 0 : sign_out->mcache,
573 0 : sign_out->dcache,
574 0 : sign_in->mcache,
575 0 : sign_in->dcache,
576 0 : sign_out->mtu,
577 0 : sign_in->mtu,
578 0 : sleep,
579 0 : sign_out->id,
580 0 : fd_topo_find_link_consumer( topo, sign_out )
581 0 : ) ) ) ) {
582 0 : FD_LOG_ERR(( "fd_keyguard_client_join failed" )); /* unreachable */
583 0 : }
584 :
585 0 : ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->id_keyswitch_obj_id ) );
586 0 : FD_TEST( ctx->keyswitch );
587 :
588 0 : FD_TEST( ctx->waker_client_idx!=ULONG_MAX );
589 0 : ctx->waker_fseq = fd_fseq_join( fd_topo_obj_laddr( topo, tile->waker_fseq_obj_id ) );
590 0 : FD_TEST( ctx->waker_fseq );
591 :
592 0 : ulong verify_out_idx = fd_topo_find_tile_out_link( topo, tile, "bundle_verif", tile->kind_id );
593 0 : if( FD_UNLIKELY( verify_out_idx==ULONG_MAX ) ) FD_LOG_ERR(( "Missing bundle_verif link" ));
594 0 : ctx->verify_out = bundle_out_link( topo, &topo->links[ tile->out_link_id[ verify_out_idx ] ], verify_out_idx );
595 :
596 0 : ulong plugin_out_idx = fd_topo_find_tile_out_link( topo, tile, "bundle_status", tile->kind_id );
597 0 : if( plugin_out_idx!=ULONG_MAX ) {
598 0 : ctx->plugin_out = bundle_out_link( topo, &topo->links[ tile->out_link_id[ plugin_out_idx ] ], plugin_out_idx );
599 0 : } else {
600 0 : ctx->plugin_out = (fd_bundle_out_ctx_t){ .idx=ULONG_MAX };
601 0 : }
602 :
603 : /* Set socket receive buffer size */
604 0 : ulong so_rcvbuf = tile->bundle.buf_sz;
605 0 : if( FD_UNLIKELY( so_rcvbuf < 2048UL ) ) FD_LOG_ERR(( "Invalid [development.bundle.buffer_size_kib]: too small" ));
606 0 : if( FD_UNLIKELY( so_rcvbuf > INT_MAX ) ) FD_LOG_ERR(( "Invalid [development.bundle.buffer_size_kib]: too large" ));
607 0 : ctx->so_rcvbuf = (int)so_rcvbuf;
608 :
609 : /* Set idle ping timer */
610 0 : ctx->keepalive_interval = (long)tile->bundle.keepalive_interval_nanos;
611 :
612 0 : ctx->bundle_status_plugin = 127;
613 0 : ctx->bundle_status_recent = (uchar)FD_BUNDLE_STATE_DISCONNECTED;
614 0 : ctx->last_bundle_status_log_nanos = fd_log_wallclock();
615 :
616 0 : FD_TEST( tile->in_cnt<=sizeof(ctx->in_kind)/sizeof(ctx->in_kind[0]) );
617 0 : int has_replay_in = 0;
618 0 : ulong polled_in_idx = 0UL;
619 0 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
620 0 : if( FD_UNLIKELY( !tile->in_link_poll[ i ] ) ) continue;
621 :
622 0 : fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
623 0 : if( !strcmp( link->name, "replay_slot" ) ) {
624 0 : ctx->in_kind[ polled_in_idx ] = IN_KIND_REPLAY_OUT;
625 0 : fd_topo_wksp_t const * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
626 0 : ctx->replay_in.mem = link_wksp->wksp;
627 0 : ctx->replay_in.chunk0 = fd_dcache_compact_chunk0( ctx->replay_in.mem, link->dcache );
628 0 : ctx->replay_in.wmark = fd_dcache_compact_wmark ( ctx->replay_in.mem, link->dcache, link->mtu );
629 0 : has_replay_in = 1;
630 0 : }
631 0 : polled_in_idx++;
632 0 : }
633 :
634 0 : ctx->next_leader_slot = ULONG_MAX;
635 0 : ctx->reset_slot = ULONG_MAX;
636 0 : ctx->sleep_mode = has_replay_in; /* start asleep until we learn leader schedule */
637 0 : ctx->sleep_check_ns = 0;
638 0 : ctx->halt_signing = 0;
639 0 : if( !has_replay_in ) memset( &ctx->replay_in, 0, sizeof(ctx->replay_in) );
640 :
641 0 : ctx->grpc_client = fd_grpc_client_new( ctx->grpc_client_mem, &fd_bundle_client_grpc_callbacks, ctx->grpc_metrics, ctx, ctx->grpc_buf_max, ctx->map_seed );
642 0 : if( FD_UNLIKELY( !ctx->grpc_client ) ) {
643 0 : FD_LOG_CRIT(( "fd_grpc_client_new failed" )); /* unreachable */
644 0 : }
645 0 : fd_grpc_client_set_version( ctx->grpc_client, fd_version_cstr, strlen( fd_version_cstr ) );
646 0 : fd_grpc_client_set_authority( ctx->grpc_client, ctx->server_sni, ctx->server_sni_len, ctx->server_tcp_port );
647 :
648 0 : fd_histf_new( ctx->metrics.msg_rx_delay,
649 0 : FD_MHIST_MIN( BUNDLE, MESSAGE_RX_DELAY_NANOS ),
650 0 : FD_MHIST_MAX( BUNDLE, MESSAGE_RX_DELAY_NANOS ) );
651 0 : }
652 :
653 : static ulong
654 : populate_allowed_seccomp( fd_topo_t const * topo,
655 : fd_topo_tile_t const * tile,
656 : ulong out_cnt,
657 0 : struct sock_filter * out ) {
658 0 : fd_bundle_tile_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
659 :
660 0 : uint epoll_inner_fd = (uint)FD_WAKER_INNER_FD( tile->waker_client_idx );
661 0 : uint epoll_outer_fd = (uint)FD_WAKER_OUTER_FD;
662 :
663 0 : populate_sock_filter_policy_fd_bundle_tile(
664 0 : out_cnt, out,
665 0 : (uint)fd_log_private_logfile_fd(),
666 0 : (uint)ctx->keylog_fd,
667 0 : (uint)ctx->netdb_fds->etc_hosts,
668 0 : (uint)ctx->netdb_fds->etc_resolv_conf,
669 0 : epoll_inner_fd,
670 0 : epoll_outer_fd
671 0 : );
672 0 : return sock_filter_policy_fd_bundle_tile_instr_cnt;
673 0 : }
674 :
675 : static ulong
676 : populate_allowed_fds( fd_topo_t const * topo,
677 : fd_topo_tile_t const * tile,
678 : ulong out_fds_cnt,
679 0 : int * out_fds ) {
680 0 : fd_bundle_tile_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
681 :
682 0 : if( FD_UNLIKELY( out_fds_cnt<7UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
683 :
684 0 : ulong out_cnt = 0UL;
685 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
686 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
687 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
688 0 : if( FD_LIKELY( ctx->netdb_fds->etc_hosts >= 0 ) )
689 0 : out_fds[ out_cnt++ ] = ctx->netdb_fds->etc_hosts;
690 0 : out_fds[ out_cnt++ ] = ctx->netdb_fds->etc_resolv_conf;
691 0 : if( FD_UNLIKELY( ctx->keylog_fd>=0 ) )
692 0 : out_fds[ out_cnt++ ] = ctx->keylog_fd;
693 0 : out_fds[ out_cnt++ ] = FD_WAKER_OUTER_FD;
694 0 : out_fds[ out_cnt++ ] = FD_WAKER_INNER_FD( tile->waker_client_idx );
695 0 : return out_cnt;
696 0 : }
697 :
698 0 : #define STEM_LAZY ((long)10e6)
699 :
700 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_bundle_tile_t
701 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_bundle_tile_t)
702 :
703 0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING fd_bundle_tile_housekeeping
704 0 : #define STEM_CALLBACK_NEXT_DEADLINE next_deadline
705 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
706 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
707 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
708 0 : #define STEM_CALLBACK_BEFORE_CREDIT before_credit
709 0 : #define STEM_CALLBACK_AFTER_CREDIT after_credit
710 :
711 : #include "../stem/fd_stem.c"
712 :
713 : fd_topo_run_tile_t fd_tile_bundle = {
714 : .name = "bundle",
715 : .populate_allowed_seccomp = populate_allowed_seccomp,
716 : .populate_allowed_fds = populate_allowed_fds,
717 : .scratch_align = scratch_align,
718 : .scratch_footprint = scratch_footprint,
719 : .privileged_init = privileged_init,
720 : .unprivileged_init = unprivileged_init,
721 : .run = stem_run,
722 : .rlimit_file_cnt = 64,
723 : .keep_host_networking = 1,
724 : .allow_connect = 1
725 : };
726 : #endif
|