Line data Source code
1 : #define _GNU_SOURCE
2 : #include "fd_circq.h"
3 : #include "fd_event_client.h"
4 :
5 : #include "../fd_txn_m.h"
6 : #include "../fd_clock_tile.h"
7 : #include "../metrics/fd_metrics.h"
8 : #include "../net/fd_net_tile.h"
9 : #include "../../discof/genesis/fd_genesi_tile.h"
10 : #include "generated/fd_event_gen.h"
11 : #include "../keyguard/fd_keyload.h"
12 : #include "../keyguard/fd_keyswitch.h"
13 : #include "../topo/fd_topo.h"
14 : #include "../../waltz/resolv/fd_netdb.h"
15 : #include "../../waltz/http/fd_url.h"
16 : #include "../../ballet/lthash/fd_lthash.h"
17 : #include "../../ballet/pb/fd_pb_encode.h"
18 : #include "../../tango/tempo/fd_tempo.h"
19 :
20 : #if FD_HAS_OPENSSL
21 : #include "../../util/alloc/fd_alloc.h"
22 : #include "../../waltz/openssl/fd_openssl.h"
23 : #include "../../waltz/openssl/fd_openssl_tile.h"
24 : #include <openssl/ssl.h>
25 : #endif
26 :
27 : #include <unistd.h>
28 : #include <fcntl.h>
29 : #include <errno.h>
30 : #include <unistd.h>
31 : #include <sys/socket.h>
32 : #include <sys/syscall.h>
33 : #include <netinet/in.h>
34 : #include <netinet/tcp.h>
35 :
36 : #include "generated/fd_event_tile_seccomp.h"
37 :
38 0 : #define GRPC_BUF_MAX (2048UL<<10UL) /* 2 MiB */
39 :
40 : /* Sized so the event workspace (circq + ~24 MiB client/ctx + 64 MiB
41 : OpenSSL loose) fits in one gigantic page. */
42 0 : #define EVENT_CIRCQ_SZ ((1UL<<30UL)-(96UL<<20UL))
43 :
44 0 : #define IN_KIND_SHRED (0)
45 0 : #define IN_KIND_DEDUP (1)
46 : #define IN_KIND_SIGN (2)
47 0 : #define IN_KIND_GENESI (3)
48 0 : #define IN_KIND_IPECHO (4)
49 0 : #define IN_KIND_EVENT (5)
50 :
51 : #define FD_EVENT_TYPE_TXN 1
52 : #define FD_EVENT_TYPE_SHRED 2
53 : #define FD_EVENT_TYPE_SIGNED_VOTE 3
54 :
55 : union fd_event_tile_in {
56 : struct {
57 : fd_wksp_t * mem;
58 : ulong mtu;
59 : ulong chunk0;
60 : ulong wmark;
61 : };
62 : fd_net_rx_bounds_t net_rx;
63 : };
64 :
65 : typedef union fd_event_tile_in fd_event_tile_in_t;
66 :
67 : struct fd_event_tile {
68 : fd_circq_t * circq;
69 : fd_event_client_t * client;
70 :
71 : fd_topo_t const * topo;
72 :
73 : int tile_shutdown_rendered[ FD_TOPO_MAX_TILES ];
74 :
75 : fd_keyswitch_t * keyswitch;
76 :
77 : ulong idle_cnt;
78 :
79 : ulong boot_id;
80 : ulong machine_id;
81 : ulong instance_id;
82 : ulong seed;
83 :
84 : ulong chunk;
85 :
86 : ushort shred_source_port;
87 : ulong shred_buf_sz;
88 : uchar shred_buf[ FD_NET_MTU ];
89 :
90 : ulong event_type;
91 : ulong event_sz;
92 : uchar event_buf[ FD_EVENT_GEN_STRUCT_MAX ];
93 :
94 : uchar identity_pubkey[ 32UL ];
95 :
96 : int use_tls;
97 : #if FD_HAS_OPENSSL
98 : SSL_CTX * ssl_ctx;
99 : #endif
100 :
101 : fd_keyguard_client_t keyguard_client[1];
102 : fd_rng_t rng[1];
103 :
104 : fd_netdb_fds_t netdb_fds[1];
105 :
106 : fd_clock_tile_t clock[1];
107 :
108 : ulong in_cnt;
109 : int in_kind[ 64UL ];
110 : fd_event_tile_in_t in[ 64UL ];
111 : };
112 :
113 : typedef struct fd_event_tile fd_event_tile_t;
114 :
115 : FD_FN_CONST static inline ulong
116 0 : scratch_align( void ) {
117 0 : ulong a = alignof( fd_event_tile_t );
118 0 : a = fd_ulong_max( a, fd_event_client_align() );
119 0 : a = fd_ulong_max( a, fd_circq_align() );
120 0 : # if FD_HAS_OPENSSL
121 0 : a = fd_ulong_max( a, fd_alloc_align() );
122 0 : # endif
123 0 : return a;
124 0 : }
125 :
126 : FD_FN_PURE static inline ulong
127 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
128 0 : (void)tile;
129 :
130 0 : ulong l = FD_LAYOUT_INIT;
131 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_event_tile_t), sizeof(fd_event_tile_t) );
132 0 : l = FD_LAYOUT_APPEND( l, fd_event_client_align(), fd_event_client_footprint( GRPC_BUF_MAX ) );
133 0 : l = FD_LAYOUT_APPEND( l, fd_circq_align(), fd_circq_footprint( EVENT_CIRCQ_SZ ) );
134 0 : # if FD_HAS_OPENSSL
135 0 : l = FD_LAYOUT_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
136 0 : # endif
137 0 : return FD_LAYOUT_FINI( l, scratch_align() );
138 0 : }
139 :
140 : # if FD_HAS_OPENSSL
141 : FD_FN_CONST static inline ulong
142 0 : loose_footprint( fd_topo_tile_t const * tile ) {
143 0 : (void)tile;
144 : /* Extra workspace memory for OpenSSL dynamic allocations */
145 0 : return 1UL<<26UL; /* 64 MiB */
146 0 : }
147 : # endif
148 :
149 : static inline void
150 0 : metrics_write( fd_event_tile_t * ctx ) {
151 0 : FD_MGAUGE_SET( EVENT, QUEUE_DEPTH, ctx->circq->cnt );
152 0 : FD_MGAUGE_SET( EVENT, QUEUE_UNSENT, fd_circq_unsent_cnt( ctx->circq ) );
153 0 : FD_MCNT_SET( EVENT, QUEUE_DROPPED, ctx->circq->metrics.drop_cnt );
154 0 : FD_MGAUGE_SET( EVENT, QUEUE_BYTES_USED, fd_circq_bytes_used( ctx->circq ) );
155 0 : FD_MGAUGE_SET( EVENT, QUEUE_BYTES_CAPACITY, ctx->circq->size );
156 :
157 0 : fd_event_client_metrics_t const * metrics = fd_event_client_metrics( ctx->client );
158 0 : FD_MCNT_SET( EVENT, SENT, metrics->events_sent );
159 0 : FD_MCNT_SET( EVENT, ACKED, metrics->events_acked );
160 0 : FD_MGAUGE_SET( EVENT, LAST_ACKED_ID, metrics->last_acked_id );
161 0 : FD_MCNT_SET( EVENT, BYTES_WRITTEN, metrics->bytes_written );
162 0 : FD_MCNT_SET( EVENT, BYTES_READ, metrics->bytes_read );
163 0 : FD_MCNT_SET( EVENT, AUTH_FAILED, metrics->auth_fail_cnt );
164 0 : FD_MCNT_SET( EVENT, INVALID_MESSAGE, metrics->invalid_msg_cnt );
165 0 : FD_MCNT_SET( EVENT, CONN_ATTEMPT, metrics->connect_attempt_cnt );
166 0 : FD_MCNT_SET( EVENT, HANDSHAKE_TIMEOUT, metrics->handshake_timeout_cnt );
167 :
168 0 : FD_MGAUGE_SET( EVENT, CONN_STATE, fd_event_client_state( ctx->client ) );
169 0 : }
170 :
171 : static void
172 : before_credit( fd_event_tile_t * ctx,
173 : fd_stem_context_t * stem,
174 0 : int * charge_busy ) {
175 0 : (void)stem;
176 :
177 0 : ctx->idle_cnt++;
178 0 : if( FD_LIKELY( ctx->idle_cnt<2UL*ctx->in_cnt ) ) return;
179 0 : ctx->idle_cnt = 0UL;
180 :
181 0 : fd_event_client_poll( ctx->client, charge_busy );
182 0 : }
183 :
184 : static void
185 : during_frag( fd_event_tile_t * ctx,
186 : ulong in_idx,
187 : ulong seq,
188 : ulong sig,
189 : ulong chunk,
190 : ulong sz,
191 0 : ulong ctl ) {
192 0 : (void)seq; (void)ctl;
193 :
194 0 : fd_event_tile_in_t const * in = &ctx->in[ in_idx ];
195 0 : switch( ctx->in_kind[ in_idx ] ) {
196 0 : case IN_KIND_SHRED: {
197 0 : uchar const * dcache_entry = fd_net_rx_translate_frag( &ctx->in[ in_idx ].net_rx, chunk, ctl, sz );
198 0 : ulong hdr_sz = fd_disco_netmux_sig_hdr_sz( sig );
199 0 : FD_TEST( hdr_sz <= sz ); /* Should be ensured by the net tile */
200 0 : fd_udp_hdr_t const * udp_hdr = (fd_udp_hdr_t const *)( dcache_entry + hdr_sz - sizeof(fd_udp_hdr_t) );
201 0 : ctx->shred_source_port = fd_ushort_bswap( udp_hdr->net_sport );
202 : // TODO: SHOULD BE RELIABLE. MAKE XDP TILE RELIABLE FIRST.
203 0 : fd_memcpy( ctx->shred_buf, dcache_entry+hdr_sz, sz-hdr_sz );
204 0 : ctx->shred_buf_sz = sz-hdr_sz;
205 0 : break;
206 0 : }
207 0 : case IN_KIND_DEDUP:
208 0 : case IN_KIND_GENESI:
209 0 : case IN_KIND_IPECHO:
210 0 : if( FD_UNLIKELY( chunk<in->chunk0 || chunk>in->wmark || sz>in->mtu ) )
211 0 : FD_LOG_CRIT(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, in->chunk0, in->wmark ));
212 0 : ctx->chunk = chunk;
213 0 : break;
214 0 : case IN_KIND_EVENT: {
215 0 : if( FD_UNLIKELY( chunk<in->chunk0 || chunk>in->wmark || sz>in->mtu ) )
216 0 : FD_LOG_CRIT(( "chunk %lu corrupt, not in range [%lu,%lu]", chunk, in->chunk0, in->wmark ));
217 0 : fd_memcpy( ctx->event_buf, fd_chunk_to_laddr_const( in->mem, chunk ), sz );
218 0 : ctx->event_type = sig;
219 0 : ctx->event_sz = sz;
220 0 : break;
221 0 : }
222 0 : default:
223 0 : FD_LOG_CRIT(( "unexpected in_kind %d %lu", ctx->in_kind[ in_idx ], in_idx ));
224 0 : }
225 0 : }
226 :
227 : static void
228 : after_frag( fd_event_tile_t * ctx,
229 : ulong in_idx,
230 : ulong seq,
231 : ulong sig,
232 : ulong sz,
233 : ulong tsorig,
234 : ulong tspub,
235 0 : fd_stem_context_t * stem ) {
236 0 : (void)sz; (void)tsorig; (void)stem;
237 :
238 0 : switch( ctx->in_kind[ in_idx ] ) {
239 0 : case IN_KIND_SHRED: {
240 0 : FD_TEST( ctx->shred_buf_sz<=FD_NET_MTU );
241 : /* TODO: Currently no way to find a tight bound for the buffer
242 : size here, but 4096 is guaranteed to fit since sz<=FD_NET_MTU.
243 : Need to have the schema generator spit out max sizes for
244 : messages. */
245 0 : uchar * buffer = fd_circq_push_back( ctx->circq, 1UL, 4096UL );
246 0 : FD_TEST( buffer );
247 :
248 0 : uint ip_addr = fd_disco_netmux_sig_ip( sig );
249 0 : uint source_port = ctx->shred_source_port;
250 0 : int protocol;
251 0 : switch( fd_disco_netmux_sig_proto( sig ) ) {
252 0 : case DST_PROTO_SHRED:
253 0 : protocol = 1;
254 0 : break;
255 0 : case DST_PROTO_REPAIR:
256 0 : protocol = 2;
257 0 : break;
258 0 : default:
259 : // TODO: Leader shreds?
260 0 : FD_LOG_ERR(( "unexpected proto %lu in sig %lu", fd_disco_netmux_sig_proto( sig ), sig ));
261 0 : }
262 :
263 0 : ulong event_id = fd_event_client_id_reserve( ctx->client );
264 0 : long timestamp_nanos = fd_clock_tile_tickcount_to_wallclock( ctx->clock,
265 0 : fd_clock_tile_tickcount_decomp( ctx->clock, tspub ) );
266 :
267 0 : fd_pb_encoder_t encoder[1];
268 0 : fd_pb_encoder_init( encoder, buffer, 4096UL );
269 :
270 0 : FD_TEST( ctx->circq->cursor_push_seq );
271 0 : fd_pb_push_uint64( encoder, 1U, ctx->circq->cursor_push_seq-1UL );
272 0 : fd_pb_push_uint64( encoder, 2U, event_id );
273 0 : fd_pb_push_uint64( encoder, 3U, seq ); /* link_seq */
274 0 : fd_pb_push_uint64( encoder, 4U, (ulong)timestamp_nanos );
275 :
276 0 : fd_pb_submsg_open( encoder, 5U ); /* Event */
277 0 : fd_pb_submsg_open( encoder, 2U ); /* Shred */
278 0 : fd_pb_push_bytes( encoder, 1U, &ip_addr, 4UL );
279 0 : fd_pb_push_uint32( encoder, 2U, source_port );
280 0 : fd_pb_push_int32( encoder, 3U, protocol );
281 0 : fd_pb_push_bytes( encoder, 4U, ctx->shred_buf, ctx->shred_buf_sz );
282 0 : fd_pb_submsg_close( encoder );
283 0 : fd_pb_submsg_close( encoder );
284 0 : fd_circq_resize_back( ctx->circq, fd_pb_encoder_out_sz( encoder ) );
285 0 : break;
286 0 : }
287 0 : case IN_KIND_DEDUP:
288 0 : FD_TEST( sz<=FD_TPU_PARSED_MTU );
289 : /* See comment above about buffer size. */
290 0 : uchar * buffer = fd_circq_push_back( ctx->circq, 1UL, 4096UL );
291 0 : FD_TEST( buffer );
292 :
293 0 : fd_txn_m_t * txnm = (fd_txn_m_t *)fd_chunk_to_laddr( ctx->in[ in_idx ].mem, ctx->chunk );
294 0 : FD_TEST( txnm->payload_sz<=FD_TPU_MTU );
295 :
296 0 : int protocol = 0;
297 0 : switch( txnm->source_tpu ) {
298 0 : case FD_TXN_M_TPU_SOURCE_QUIC: protocol = 1; break;
299 0 : case FD_TXN_M_TPU_SOURCE_UDP: protocol = 2; break;
300 0 : case FD_TXN_M_TPU_SOURCE_GOSSIP: protocol = 3; break;
301 0 : case FD_TXN_M_TPU_SOURCE_BUNDLE: protocol = 4; break;
302 0 : case FD_TXN_M_TPU_SOURCE_TXSEND: protocol = 5; break;
303 0 : default:
304 0 : FD_LOG_ERR(( "unexpected source_tpu %u", txnm->source_tpu ));
305 0 : }
306 :
307 0 : ulong event_id = fd_event_client_id_reserve( ctx->client );
308 0 : long timestamp_nanos = fd_clock_tile_tickcount_to_wallclock( ctx->clock,
309 0 : fd_clock_tile_tickcount_decomp( ctx->clock, tspub ) );
310 :
311 0 : fd_pb_encoder_t encoder[1];
312 0 : fd_pb_encoder_init( encoder, buffer, 4096UL );
313 :
314 0 : FD_TEST( ctx->circq->cursor_push_seq );
315 0 : fd_pb_push_uint64( encoder, 1U, ctx->circq->cursor_push_seq-1UL );
316 0 : fd_pb_push_uint64( encoder, 2U, event_id );
317 0 : fd_pb_push_uint64( encoder, 3U, seq ); /* link_seq */
318 0 : fd_pb_push_uint64( encoder, 4U, (ulong)timestamp_nanos );
319 :
320 0 : fd_pb_submsg_open( encoder, 5U ); /* Event */
321 0 : fd_pb_submsg_open( encoder, 1U ); /* Txn */
322 0 : fd_pb_push_bytes( encoder, 1U, &txnm->source_ipv4, 4UL );
323 0 : fd_pb_push_uint32( encoder, 2U, 0U ); /* TODO: source port .. */
324 0 : fd_pb_push_int32( encoder, 3U, protocol );
325 0 : fd_pb_push_uint64( encoder, 4U, txnm->block_engine.bundle_id );
326 0 : if( FD_UNLIKELY( txnm->block_engine.bundle_id ) ) {
327 0 : fd_pb_push_uint32( encoder, 5U, (uint)txnm->block_engine.bundle_txn_cnt );
328 0 : fd_pb_push_uint32( encoder, 6U, txnm->block_engine.commission );
329 0 : fd_pb_push_bytes( encoder, 7U, txnm->block_engine.commission_pubkey, 32UL );
330 0 : } else {
331 0 : fd_pb_push_uint32( encoder, 5U, 0U );
332 0 : fd_pb_push_uint32( encoder, 6U, 0U );
333 0 : uchar zero_pubkey[32UL] = {0};
334 0 : fd_pb_push_bytes( encoder, 7U, zero_pubkey, 32UL );
335 0 : }
336 0 : fd_pb_push_bytes( encoder, 8U, fd_txn_m_payload( txnm ), txnm->payload_sz );
337 :
338 0 : fd_pb_submsg_close( encoder );
339 0 : fd_pb_submsg_close( encoder );
340 0 : fd_circq_resize_back( ctx->circq, fd_pb_encoder_out_sz( encoder ) );
341 :
342 0 : break;
343 0 : case IN_KIND_GENESI: {
344 0 : fd_genesis_meta_t const * genesis_meta = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, ctx->chunk );
345 0 : fd_event_client_init_genesis( ctx->client, genesis_meta );
346 0 : break;
347 0 : }
348 0 : case IN_KIND_IPECHO:
349 0 : FD_TEST( sig && sig<=USHORT_MAX );
350 0 : fd_event_client_init_shred_version( ctx->client, (ushort)sig );
351 0 : break;
352 0 : case IN_KIND_EVENT: {
353 0 : long timestamp_nanos = fd_clock_tile_tickcount_to_wallclock( ctx->clock, fd_clock_tile_tickcount_decomp( ctx->clock, tspub ) );
354 0 : fd_event_serialize_by_type( ctx->event_type, ctx->circq, ctx->client, timestamp_nanos, seq, ctx->event_buf, ctx->event_sz );
355 0 : break;
356 0 : }
357 0 : default:
358 0 : FD_LOG_ERR(( "unexpected in_kind %d", ctx->in_kind[ in_idx ] ));
359 0 : }
360 0 : }
361 :
362 : static void
363 : privileged_init( fd_topo_t const * topo,
364 0 : fd_topo_tile_t const * tile ) {
365 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
366 :
367 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
368 0 : fd_event_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_event_tile_t), sizeof(fd_event_tile_t) );
369 0 : FD_SCRATCH_ALLOC_APPEND( l, fd_event_client_align(), fd_event_client_footprint( GRPC_BUF_MAX ) );
370 0 : FD_SCRATCH_ALLOC_APPEND( l, fd_circq_align(), fd_circq_footprint( EVENT_CIRCQ_SZ ) );
371 0 : # if FD_HAS_OPENSSL
372 0 : void * alloc_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
373 0 : (void)alloc_mem;
374 0 : # endif
375 :
376 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
377 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
378 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
379 :
380 0 : if( FD_UNLIKELY( !strcmp( tile->event.identity_key_path, "" ) ) ) FD_LOG_ERR(( "identity_key_path not set" ));
381 0 : const uchar * identity_key = fd_keyload_load( tile->event.identity_key_path, /* pubkey only: */ 1 );
382 0 : fd_memcpy( ctx->identity_pubkey, identity_key, 32UL );
383 :
384 0 : FD_TEST( fd_rng_secure( &ctx->seed, 8UL ) );
385 0 : FD_TEST( fd_rng_secure( &ctx->instance_id, 8UL ) );
386 :
387 0 : FD_LOG_INFO(( "event instance_id=%lu", ctx->instance_id ));
388 :
389 0 : #define FD_EVENT_ID_SEED 0x812CAFEBABEFEEE0UL
390 :
391 0 : char _boot_id[ 36 ];
392 0 : int boot_id_fd = open( "/proc/sys/kernel/random/boot_id", O_RDONLY );
393 0 : if( FD_UNLIKELY( -1==boot_id_fd ) ) FD_LOG_ERR(( "open(/proc/sys/kernel/random/boot_id) failed (%d-%s)", errno, fd_io_strerror( errno ) ));
394 0 : if( FD_UNLIKELY( 36UL!=read( boot_id_fd, _boot_id, 36UL ) ) ) FD_LOG_ERR(( "read(/proc/sys/kernel/random/boot_id) failed (%d-%s)", errno, fd_io_strerror( errno ) ));
395 0 : if( FD_UNLIKELY( -1==close( boot_id_fd ) ) ) FD_LOG_ERR(( "close(/proc/sys/kernel/random/boot_id) failed (%d-%s)", errno, fd_io_strerror( errno ) ));
396 :
397 0 : ctx->boot_id = fd_hash( FD_EVENT_ID_SEED, _boot_id, 36UL );
398 :
399 0 : char _machine_id[ 32 ];
400 0 : int machine_id_fd = open( "/etc/machine-id", O_RDONLY );
401 0 : if( FD_UNLIKELY( -1==machine_id_fd ) ) FD_LOG_ERR(( "open(/etc/machine-id) failed (%d-%s)", errno, fd_io_strerror( errno ) ));
402 0 : if( FD_UNLIKELY( 32UL!=read( machine_id_fd, _machine_id, 32UL ) ) ) FD_LOG_ERR(( "read(/etc/machine-id) failed (%d-%s)", errno, fd_io_strerror( errno ) ));
403 0 : if( FD_UNLIKELY( -1==close( machine_id_fd ) ) ) FD_LOG_ERR(( "close(/etc/machine-id) failed (%d-%s)", errno, fd_io_strerror( errno ) ));
404 :
405 0 : ctx->machine_id = fd_hash( FD_EVENT_ID_SEED, _machine_id, 32UL );
406 :
407 0 : if( FD_UNLIKELY( !fd_netdb_open_fds( ctx->netdb_fds ) ) ) {
408 0 : FD_LOG_ERR(( "fd_netdb_open_fds failed" ));
409 0 : }
410 :
411 : /* Detect TLS from the URL scheme */
412 0 : fd_url_t url[ 1UL ];
413 0 : ushort port;
414 0 : _Bool is_ssl = 0;
415 0 : if( FD_UNLIKELY( fd_url_parse_endpoint( url, tile->event.url, strlen( tile->event.url ), &port, &is_ssl, "[tiles.event.url]" ) ) ) {
416 0 : FD_LOG_ERR(( "Could not parse [tiles.event.url]" ));
417 0 : }
418 0 : ctx->use_tls = is_ssl;
419 :
420 0 : # if FD_HAS_OPENSSL
421 0 : ctx->ssl_ctx = NULL;
422 0 : if( ctx->use_tls ) {
423 0 : fd_alloc_t * alloc = fd_alloc_join( fd_alloc_new( alloc_mem, 1UL ), tile->kind_id );
424 0 : if( FD_UNLIKELY( !alloc ) ) FD_LOG_ERR(( "fd_alloc_new failed" ));
425 0 : fd_ossl_tile_init( alloc );
426 :
427 0 : SSL_CTX * ssl_ctx = SSL_CTX_new( TLS_client_method() );
428 0 : if( FD_UNLIKELY( !ssl_ctx ) ) FD_LOG_ERR(( "SSL_CTX_new failed" ));
429 :
430 0 : if( FD_UNLIKELY( !SSL_CTX_set_mode( ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE|SSL_MODE_AUTO_RETRY ) ) )
431 0 : FD_LOG_ERR(( "SSL_CTX_set_mode failed" ));
432 :
433 0 : if( FD_UNLIKELY( !SSL_CTX_set_min_proto_version( ssl_ctx, TLS1_3_VERSION ) ) )
434 0 : FD_LOG_ERR(( "SSL_CTX_set_min_proto_version(ssl_ctx,TLS1_3_VERSION) failed" ));
435 :
436 0 : if( FD_UNLIKELY( 0!=SSL_CTX_set_alpn_protos( ssl_ctx, (uchar const *)"\x02h2", 3 ) ) )
437 0 : FD_LOG_ERR(( "SSL_CTX_set_alpn_protos failed" ));
438 :
439 0 : fd_ossl_load_certs( ssl_ctx ); /* also sets SSL_VERIFY_PEER */
440 :
441 0 : ctx->ssl_ctx = ssl_ctx;
442 0 : }
443 : # else
444 : if( FD_UNLIKELY( ctx->use_tls ) ) {
445 : FD_LOG_ERR(( "TLS requested for event service (https:// URL) but this build "
446 : "does not include OpenSSL. Re-run ./deps.sh and do a clean rebuild." ));
447 : }
448 : # endif
449 0 : }
450 :
451 : static int
452 : link_is_event_report( fd_topo_t const * topo,
453 0 : ulong link_id ) {
454 0 : for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
455 0 : if( FD_UNLIKELY( topo->tiles[ i ].event_link_id==link_id ) ) return 1;
456 0 : }
457 0 : return 0;
458 0 : }
459 :
460 : static void
461 : unprivileged_init( fd_topo_t const * topo,
462 0 : fd_topo_tile_t const * tile ) {
463 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
464 :
465 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
466 0 : fd_event_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_event_tile_t), sizeof(fd_event_tile_t) );
467 0 : void * _event_client = FD_SCRATCH_ALLOC_APPEND( l, fd_event_client_align(), fd_event_client_footprint( GRPC_BUF_MAX ) );
468 0 : void * _circq = FD_SCRATCH_ALLOC_APPEND( l, fd_circq_align(), fd_circq_footprint( EVENT_CIRCQ_SZ ) );
469 0 : # if FD_HAS_OPENSSL
470 0 : FD_SCRATCH_ALLOC_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
471 0 : # endif
472 :
473 0 : ulong sign_in_idx = fd_topo_find_tile_in_link ( topo, tile, "sign_event", tile->kind_id );
474 0 : ulong sign_out_idx = fd_topo_find_tile_out_link( topo, tile, "event_sign", tile->kind_id );
475 0 : FD_TEST( sign_in_idx!=ULONG_MAX );
476 0 : fd_topo_link_t const * sign_in = &topo->links[ tile->in_link_id[ sign_in_idx ] ];
477 0 : fd_topo_link_t const * sign_out = &topo->links[ tile->out_link_id[ sign_out_idx ] ];
478 0 : if( FD_UNLIKELY( !fd_keyguard_client_join( fd_keyguard_client_new( ctx->keyguard_client,
479 0 : sign_out->mcache,
480 0 : sign_out->dcache,
481 0 : sign_in->mcache,
482 0 : sign_in->dcache,
483 0 : sign_out->mtu ) ) ) ) {
484 0 : FD_LOG_ERR(( "failed to construct keyguard" ));
485 0 : }
486 :
487 0 : FD_TEST( fd_rng_join( fd_rng_new( ctx->rng, 0U, ctx->seed ) ) );
488 :
489 0 : ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->id_keyswitch_obj_id ) );
490 0 : FD_TEST( ctx->keyswitch );
491 :
492 0 : ctx->circq = fd_circq_join( fd_circq_new( _circq, EVENT_CIRCQ_SZ ) );
493 0 : FD_TEST( ctx->circq );
494 :
495 0 : void * ssl_ctx_ptr = NULL;
496 0 : # if FD_HAS_OPENSSL
497 0 : ssl_ctx_ptr = ctx->ssl_ctx;
498 0 : # endif
499 :
500 0 : ctx->client = fd_event_client_join( fd_event_client_new( _event_client,
501 0 : ctx->keyguard_client,
502 0 : ctx->rng,
503 0 : ctx->circq,
504 0 : 2*(1UL<<20UL) /* 2 MiB */,
505 0 : tile->event.url,
506 0 : ctx->identity_pubkey,
507 0 : fd_version_cstr,
508 0 : fd_commit_ref_cstr,
509 0 : tile->event.action,
510 0 : ctx->instance_id,
511 0 : ctx->boot_id,
512 0 : ctx->machine_id,
513 0 : GRPC_BUF_MAX,
514 0 : ctx->use_tls,
515 0 : ssl_ctx_ptr ) );
516 0 : FD_TEST( ctx->client );
517 :
518 0 : ctx->topo = topo;
519 0 : fd_memset( ctx->tile_shutdown_rendered, 0, sizeof(ctx->tile_shutdown_rendered) );
520 :
521 0 : ctx->idle_cnt = 0UL;
522 :
523 0 : FD_TEST( tile->in_cnt<=sizeof(ctx->in_kind)/sizeof(ctx->in_kind[0]) );
524 0 : int have_genesi = 0;
525 0 : int have_ipecho = 0;
526 0 : ulong polled_in_idx = 0UL;
527 0 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
528 0 : if( FD_UNLIKELY( !tile->in_link_poll[ i ] ) ) continue;
529 :
530 0 : fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
531 0 : fd_topo_wksp_t const * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
532 :
533 0 : if( FD_LIKELY( !strcmp( link->name, "net_shred" ) ) ) {
534 0 : fd_net_rx_bounds_init( &ctx->in[ polled_in_idx ].net_rx, link->dcache );
535 0 : ctx->in_kind[ polled_in_idx ] = IN_KIND_SHRED;
536 0 : polled_in_idx++;
537 0 : continue; /* only net_rx needs to be set in this case. */
538 0 : }
539 0 : else if( FD_LIKELY( !strcmp( link->name, "dedup_resolv" ) ) ) ctx->in_kind[ polled_in_idx ] = IN_KIND_DEDUP;
540 0 : else if( FD_LIKELY( !strcmp( link->name, "genesi_out" ) ) ) { ctx->in_kind[ polled_in_idx ] = IN_KIND_GENESI; have_genesi = 1; }
541 0 : else if( FD_LIKELY( !strcmp( link->name, "ipecho_out" ) ) ) { ctx->in_kind[ polled_in_idx ] = IN_KIND_IPECHO; have_ipecho = 1; }
542 0 : else if( FD_LIKELY( link_is_event_report( topo, link->id ) ) ) {
543 0 : ctx->in_kind[ polled_in_idx ] = IN_KIND_EVENT;
544 0 : FD_TEST( link->mtu<=sizeof(ctx->event_buf) );
545 0 : }
546 0 : else FD_LOG_ERR(( "event tile has unexpected input link %lu %s", i, link->name ));
547 :
548 0 : ctx->in[ polled_in_idx ].mem = link_wksp->wksp;
549 0 : ctx->in[ polled_in_idx ].mtu = link->mtu;
550 0 : if( FD_UNLIKELY( ctx->in[ polled_in_idx ].mtu ) ) {
551 0 : ctx->in[ polled_in_idx ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ polled_in_idx ].mem, link->dcache );
552 0 : ctx->in[ polled_in_idx ].wmark = fd_dcache_compact_wmark ( ctx->in[ polled_in_idx ].mem, link->dcache, link->mtu );
553 0 : } else {
554 0 : ctx->in[ polled_in_idx ].chunk0 = 0UL;
555 0 : ctx->in[ polled_in_idx ].wmark = 0UL;
556 0 : }
557 0 : polled_in_idx++;
558 0 : }
559 0 : ctx->in_cnt = polled_in_idx;
560 :
561 0 : if( FD_UNLIKELY( !have_genesi ) ) {
562 0 : fd_genesis_meta_t meta[1]; fd_memset( meta, 0, sizeof(meta) );
563 0 : fd_memcpy( meta->genesis_hash.uc, tile->event.genesis_hash, 32UL );
564 0 : fd_event_client_init_genesis( ctx->client, meta );
565 0 : }
566 0 : if( FD_UNLIKELY( !have_ipecho ) ) {
567 0 : fd_event_client_init_shred_version( ctx->client, tile->event.shred_version );
568 0 : }
569 :
570 0 : fd_clock_tile_init( ctx->clock );
571 :
572 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
573 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
574 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
575 0 : }
576 :
577 : static ulong
578 : populate_allowed_seccomp( fd_topo_t const * topo,
579 : fd_topo_tile_t const * tile,
580 : ulong out_cnt,
581 0 : struct sock_filter * out ) {
582 0 : fd_event_tile_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
583 :
584 0 : populate_sock_filter_policy_fd_event_tile(
585 0 : out_cnt, out,
586 0 : (uint)fd_log_private_logfile_fd(),
587 0 : (uint)ctx->netdb_fds->etc_hosts,
588 0 : (uint)ctx->netdb_fds->etc_resolv_conf );
589 0 : return sock_filter_policy_fd_event_tile_instr_cnt;
590 0 : }
591 :
592 : static ulong
593 : populate_allowed_fds( fd_topo_t const * topo,
594 : fd_topo_tile_t const * tile,
595 : ulong out_fds_cnt,
596 0 : int * out_fds ) {
597 0 : fd_event_tile_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
598 :
599 0 : if( FD_UNLIKELY( out_fds_cnt<4UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
600 :
601 0 : ulong out_cnt = 0;
602 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
603 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
604 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
605 0 : if( FD_LIKELY( ctx->netdb_fds->etc_hosts >= 0 ) )
606 0 : out_fds[ out_cnt++ ] = ctx->netdb_fds->etc_hosts;
607 0 : out_fds[ out_cnt++ ] = ctx->netdb_fds->etc_resolv_conf;
608 0 : return out_cnt;
609 0 : }
610 :
611 : static void
612 0 : during_housekeeping( fd_event_tile_t * ctx ) {
613 0 : if( FD_UNLIKELY( fd_clock_tile_recal_due( ctx->clock ) ) ) {
614 0 : fd_clock_tile_recal( ctx->clock );
615 0 : }
616 :
617 0 : if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
618 0 : FD_LOG_DEBUG(( "keyswitch: switching identity" ));
619 0 : memcpy( ctx->identity_pubkey, ctx->keyswitch->bytes, 32UL );
620 0 : fd_event_client_set_identity( ctx->client, ctx->identity_pubkey );
621 0 : fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
622 0 : }
623 0 : }
624 :
625 0 : #define STEM_BURST (1UL)
626 0 : #define STEM_LAZY ((long)10e6) /* 10ms */
627 :
628 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_event_tile_t
629 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_event_tile_t)
630 :
631 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
632 0 : #define STEM_CALLBACK_BEFORE_CREDIT before_credit
633 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
634 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
635 0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
636 :
637 : #include "../stem/fd_stem.c"
638 :
639 : fd_topo_run_tile_t fd_tile_event = {
640 : .name = "event",
641 : .rlimit_file_cnt = 5UL, /* stderr, logfile, /etc/hosts, /etc/resolv.conf, and socket to the server */
642 : .populate_allowed_seccomp = populate_allowed_seccomp,
643 : .populate_allowed_fds = populate_allowed_fds,
644 : .scratch_align = scratch_align,
645 : .scratch_footprint = scratch_footprint,
646 : # if FD_HAS_OPENSSL
647 : .loose_footprint = loose_footprint,
648 : # endif
649 : .privileged_init = privileged_init,
650 : .unprivileged_init = unprivileged_init,
651 : .run = stem_run,
652 : .keep_host_networking = 1,
653 : .allow_connect = 1,
654 : };
|