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 "../metrics/fd_metrics.h"
7 : #include "../net/fd_net_tile.h"
8 : #include "../../discof/genesis/fd_genesi_tile.h"
9 : #include "../keyguard/fd_keyload.h"
10 : #include "../keyguard/fd_keyswitch.h"
11 : #include "../topo/fd_topo.h"
12 : #include "../../waltz/resolv/fd_netdb.h"
13 : #include "../../ballet/lthash/fd_lthash.h"
14 : #include "../../ballet/pb/fd_pb_encode.h"
15 :
16 : #include <unistd.h>
17 : #include <fcntl.h>
18 : #include <errno.h>
19 : #include <unistd.h>
20 : #include <sys/socket.h>
21 : #include <sys/syscall.h>
22 : #include <netinet/in.h>
23 : #include <netinet/tcp.h>
24 :
25 : #include "generated/fd_event_tile_seccomp.h"
26 :
27 0 : #define GRPC_BUF_MAX (2048UL<<10UL) /* 2 MiB */
28 :
29 : extern char const firedancer_version_string[];
30 :
31 0 : #define IN_KIND_SHRED (0)
32 0 : #define IN_KIND_DEDUP (1)
33 0 : #define IN_KIND_SIGN (2)
34 0 : #define IN_KIND_GENESI (3)
35 0 : #define IN_KIND_IPECHO (4)
36 :
37 : union fd_event_tile_in {
38 : struct {
39 : fd_wksp_t * mem;
40 : ulong mtu;
41 : ulong chunk0;
42 : ulong wmark;
43 : };
44 : fd_net_rx_bounds_t net_rx;
45 : };
46 :
47 : typedef union fd_event_tile_in fd_event_tile_in_t;
48 :
49 : struct fd_event_tile {
50 : fd_circq_t * circq;
51 : fd_event_client_t * client;
52 :
53 : fd_topo_t const * topo;
54 :
55 : int tile_shutdown_rendered[ FD_TOPO_MAX_TILES ];
56 :
57 : fd_keyswitch_t * keyswitch;
58 :
59 : ulong idle_cnt;
60 :
61 : ulong boot_id;
62 : ulong machine_id;
63 : ulong instance_id;
64 : ulong seed;
65 :
66 : ulong chunk;
67 :
68 : ushort shred_source_port;
69 : ulong shred_buf_sz;
70 : uchar shred_buf[ FD_NET_MTU ];
71 :
72 : uchar identity_pubkey[ 32UL ];
73 :
74 : fd_keyguard_client_t keyguard_client[1];
75 : fd_rng_t rng[1];
76 :
77 : fd_netdb_fds_t netdb_fds[1];
78 :
79 : ulong in_cnt;
80 : int in_kind[ 64UL ];
81 : fd_event_tile_in_t in[ 64UL ];
82 : };
83 :
84 : typedef struct fd_event_tile fd_event_tile_t;
85 :
86 : FD_FN_CONST static inline ulong
87 0 : scratch_align( void ) {
88 0 : return 128UL;
89 0 : }
90 :
91 : FD_FN_PURE static inline ulong
92 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
93 0 : (void)tile;
94 :
95 0 : ulong l = FD_LAYOUT_INIT;
96 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_event_tile_t), sizeof(fd_event_tile_t) );
97 0 : l = FD_LAYOUT_APPEND( l, fd_event_client_align(), fd_event_client_footprint( GRPC_BUF_MAX ) );
98 0 : l = FD_LAYOUT_APPEND( l, fd_circq_align(), fd_circq_footprint( 1UL<<30UL ) ); /* 1GiB circq for events */
99 0 : return FD_LAYOUT_FINI( l, scratch_align() );
100 0 : }
101 :
102 : static inline void
103 0 : metrics_write( fd_event_tile_t * ctx ) {
104 0 : FD_MGAUGE_SET( EVENT, EVENT_QUEUE_COUNT, ctx->circq->cnt );
105 0 : FD_MCNT_SET( EVENT, EVENT_QUEUE_DROPS, ctx->circq->metrics.drop_cnt );
106 0 : FD_MGAUGE_SET( EVENT, EVENT_QUEUE_BYTES_USED, fd_circq_bytes_used( ctx->circq ) );
107 0 : FD_MGAUGE_SET( EVENT, EVENT_QUEUE_BYTES_CAPACITY, ctx->circq->size );
108 :
109 0 : fd_event_client_metrics_t const * metrics = fd_event_client_metrics( ctx->client );
110 0 : FD_MCNT_SET( EVENT, EVENTS_SENT, metrics->events_sent );
111 0 : FD_MCNT_SET( EVENT, EVENTS_ACKED, metrics->events_acked );
112 0 : FD_MCNT_SET( EVENT, BYTES_WRITTEN, metrics->bytes_written );
113 0 : FD_MCNT_SET( EVENT, BYTES_READ, metrics->bytes_read );
114 0 : FD_MGAUGE_SET( EVENT, CONNECTION_STATE, fd_event_client_state( ctx->client ) );
115 0 : }
116 :
117 : static void
118 : before_credit( fd_event_tile_t * ctx,
119 : fd_stem_context_t * stem,
120 0 : int * charge_busy ) {
121 0 : (void)stem;
122 :
123 0 : ctx->idle_cnt++;
124 0 : if( FD_LIKELY( ctx->idle_cnt<2UL*ctx->in_cnt ) ) return;
125 0 : ctx->idle_cnt = 0UL;
126 :
127 0 : fd_event_client_poll( ctx->client, charge_busy );
128 0 : }
129 :
130 : static void
131 : during_frag( fd_event_tile_t * ctx,
132 : ulong in_idx,
133 : ulong seq,
134 : ulong sig,
135 : ulong chunk,
136 : ulong sz,
137 0 : ulong ctl ) {
138 0 : (void)seq; (void)sig; (void)ctl;
139 :
140 0 : switch( ctx->in_kind[ in_idx ] ) {
141 0 : case IN_KIND_SHRED: {
142 0 : uchar const * dcache_entry = fd_net_rx_translate_frag( &ctx->in[ in_idx ].net_rx, chunk, ctl, sz );
143 0 : ulong hdr_sz = fd_disco_netmux_sig_hdr_sz( sig );
144 0 : FD_TEST( hdr_sz <= sz ); /* Should be ensured by the net tile */
145 0 : fd_udp_hdr_t const * udp_hdr = (fd_udp_hdr_t const *)( dcache_entry + hdr_sz - sizeof(fd_udp_hdr_t) );
146 0 : ctx->shred_source_port = fd_ushort_bswap( udp_hdr->net_sport );
147 : // TODO: SHOULD BE RELIABLE. MAKE XDP TILE RELIABLE FIRST.
148 0 : fd_memcpy( ctx->shred_buf, dcache_entry+hdr_sz, sz-hdr_sz );
149 0 : ctx->shred_buf_sz = sz-hdr_sz;
150 0 : break;
151 0 : }
152 0 : case IN_KIND_DEDUP:
153 0 : case IN_KIND_GENESI:
154 0 : case IN_KIND_IPECHO:
155 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>ctx->in[ in_idx ].mtu ) )
156 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
157 :
158 0 : ctx->chunk = chunk;
159 0 : break;
160 0 : default:
161 0 : FD_LOG_ERR(( "unexpected in_kind %d %lu", ctx->in_kind[ in_idx ], in_idx ));
162 0 : }
163 0 : }
164 :
165 : static void
166 : after_frag( fd_event_tile_t * ctx,
167 : ulong in_idx,
168 : ulong seq,
169 : ulong sig,
170 : ulong sz,
171 : ulong tsorig,
172 : ulong tspub,
173 0 : fd_stem_context_t * stem ) {
174 0 : (void)seq; (void)sz; (void)tsorig; (void)stem;
175 :
176 0 : switch( ctx->in_kind[ in_idx ] ) {
177 0 : case IN_KIND_SHRED: {
178 0 : FD_TEST( ctx->shred_buf_sz<=FD_NET_MTU );
179 : /* TODO: Currently no way to find a tight bound for the buffer
180 : size here, but 4096 is guaranteed to fit since sz<=FD_NET_MTU.
181 : Need to have the schema generator spit out max sizes for
182 : messages. */
183 0 : uchar * buffer = fd_circq_push_back( ctx->circq, 1UL, 4096UL );
184 0 : FD_TEST( buffer );
185 :
186 0 : uint ip_addr = fd_disco_netmux_sig_ip( sig );
187 0 : uint source_port = ctx->shred_source_port;
188 0 : int protocol;
189 0 : switch( fd_disco_netmux_sig_proto( sig ) ) {
190 0 : case DST_PROTO_SHRED:
191 0 : protocol = 0;
192 0 : break;
193 0 : case DST_PROTO_REPAIR:
194 0 : protocol = 1;
195 0 : break;
196 0 : default:
197 : // TODO: Leader shreds?
198 0 : FD_LOG_ERR(( "unexpected proto %lu in sig %lu", fd_disco_netmux_sig_proto( sig ), sig ));
199 0 : }
200 :
201 0 : ulong event_id = fd_event_client_id_reserve( ctx->client );
202 0 : long timestamp_nanos = fd_frag_meta_ts_decomp( tspub, fd_tickcount() );
203 0 : long timestamp_seconds = timestamp_nanos / 1000000000L;
204 0 : int timestamp_subsec_nanos = (int)( timestamp_nanos % 1000000000L );
205 :
206 0 : fd_pb_encoder_t encoder[1];
207 0 : fd_pb_encoder_init( encoder, buffer, 4096UL );
208 :
209 0 : FD_TEST( ctx->circq->cursor_push_seq );
210 0 : fd_pb_push_uint64( encoder, 1U, ctx->circq->cursor_push_seq-1UL );
211 0 : fd_pb_push_uint64( encoder, 2U, event_id );
212 0 : fd_pb_submsg_open( encoder, 3U );
213 0 : if( FD_LIKELY( timestamp_seconds ) ) fd_pb_push_int64( encoder, 1U, timestamp_seconds );
214 0 : if( FD_LIKELY( timestamp_subsec_nanos ) ) fd_pb_push_int32( encoder, 2U, timestamp_subsec_nanos );
215 0 : fd_pb_submsg_close( encoder );
216 :
217 0 : fd_pb_submsg_open( encoder, 4U ); /* Event */
218 0 : fd_pb_submsg_open( encoder, 2U ); /* Shred */
219 0 : fd_pb_push_bytes( encoder, 1U, &ip_addr, 4UL );
220 0 : fd_pb_push_uint32( encoder, 2U, source_port );
221 0 : fd_pb_push_int32( encoder, 3U, protocol );
222 0 : fd_pb_push_bytes( encoder, 4U, ctx->shred_buf, ctx->shred_buf_sz );
223 0 : fd_pb_submsg_close( encoder );
224 0 : fd_pb_submsg_close( encoder );
225 0 : fd_circq_resize_back( ctx->circq, fd_pb_encoder_out_sz( encoder ) );
226 0 : break;
227 0 : }
228 0 : case IN_KIND_DEDUP:
229 0 : FD_TEST( sz<=FD_TPU_PARSED_MTU );
230 : /* See comment above about buffer size. */
231 0 : uchar * buffer = fd_circq_push_back( ctx->circq, 1UL, 4096UL );
232 0 : FD_TEST( buffer );
233 :
234 0 : fd_txn_m_t * txnm = (fd_txn_m_t *)fd_chunk_to_laddr( ctx->in[ in_idx ].mem, ctx->chunk );
235 0 : FD_TEST( txnm->payload_sz<=FD_TPU_MTU );
236 :
237 0 : int protocol = 0;
238 0 : switch( txnm->source_tpu ) {
239 0 : case FD_TXN_M_TPU_SOURCE_QUIC: protocol = 1; break;
240 0 : case FD_TXN_M_TPU_SOURCE_UDP: protocol = 2; break;
241 0 : case FD_TXN_M_TPU_SOURCE_GOSSIP: protocol = 3; break;
242 0 : case FD_TXN_M_TPU_SOURCE_BUNDLE: protocol = 4; break;
243 0 : case FD_TXN_M_TPU_SOURCE_TXSEND: protocol = 5; break;
244 0 : default:
245 0 : FD_LOG_ERR(( "unexpected source_tpu %u", txnm->source_tpu ));
246 0 : }
247 :
248 0 : ulong event_id = fd_event_client_id_reserve( ctx->client );
249 0 : long timestamp_nanos = fd_frag_meta_ts_decomp( tspub, fd_tickcount() );
250 0 : long timestamp_seconds = timestamp_nanos / 1000000000L;
251 0 : int timestamp_subsec_nanos = (int)( timestamp_nanos % 1000000000L );
252 :
253 0 : fd_pb_encoder_t encoder[1];
254 0 : fd_pb_encoder_init( encoder, buffer, 4096UL );
255 :
256 0 : FD_TEST( ctx->circq->cursor_push_seq );
257 0 : fd_pb_push_uint64( encoder, 1U, ctx->circq->cursor_push_seq-1UL );
258 0 : fd_pb_push_uint64( encoder, 2U, event_id );
259 0 : fd_pb_submsg_open( encoder, 3U );
260 0 : if( FD_LIKELY( timestamp_seconds ) ) fd_pb_push_int64( encoder, 1U, timestamp_seconds );
261 0 : if( FD_LIKELY( timestamp_subsec_nanos ) ) fd_pb_push_int32( encoder, 2U, timestamp_subsec_nanos );
262 0 : fd_pb_submsg_close( encoder );
263 :
264 0 : fd_pb_submsg_open( encoder, 4U ); /* Event */
265 0 : fd_pb_submsg_open( encoder, 1U ); /* Txn */
266 0 : fd_pb_push_bytes( encoder, 1U, &txnm->source_ipv4, 4UL );
267 0 : fd_pb_push_uint32( encoder, 2U, 0U ); /* TODO: source port .. */
268 0 : fd_pb_push_int32( encoder, 3U, protocol );
269 0 : fd_pb_push_uint64( encoder, 4U, txnm->block_engine.bundle_id );
270 0 : if( FD_UNLIKELY( txnm->block_engine.bundle_id ) ) {
271 0 : fd_pb_push_uint32( encoder, 5U, (uint)txnm->block_engine.bundle_txn_cnt );
272 0 : fd_pb_push_uint32( encoder, 6U, txnm->block_engine.commission );
273 0 : fd_pb_push_bytes( encoder, 7U, txnm->block_engine.commission_pubkey, 32UL );
274 0 : } else {
275 0 : fd_pb_push_uint32( encoder, 5U, 0U );
276 0 : fd_pb_push_uint32( encoder, 6U, 0U );
277 0 : uchar zero_pubkey[32UL] = {0};
278 0 : fd_pb_push_bytes( encoder, 7U, zero_pubkey, 32UL );
279 0 : }
280 0 : fd_pb_push_bytes( encoder, 8U, fd_txn_m_payload( txnm ), txnm->payload_sz );
281 :
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 :
286 0 : break;
287 0 : case IN_KIND_GENESI: {
288 0 : fd_genesis_meta_t const * genesis_meta = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, ctx->chunk );
289 0 : fd_event_client_init_genesis( ctx->client, genesis_meta );
290 0 : break;
291 0 : }
292 0 : case IN_KIND_IPECHO:
293 0 : FD_TEST( sig && sig<=USHORT_MAX );
294 0 : fd_event_client_init_shred_version( ctx->client, (ushort)sig );
295 0 : break;
296 0 : default:
297 0 : FD_LOG_ERR(( "unexpected in_kind %d", ctx->in_kind[ in_idx ] ));
298 0 : }
299 0 : }
300 :
301 : static void
302 : privileged_init( fd_topo_t * topo,
303 0 : fd_topo_tile_t * tile ) {
304 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
305 :
306 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
307 0 : fd_event_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_event_tile_t), sizeof(fd_event_tile_t) );
308 :
309 0 : if( FD_UNLIKELY( !strcmp( tile->event.identity_key_path, "" ) ) ) FD_LOG_ERR(( "identity_key_path not set" ));
310 0 : const uchar * identity_key = fd_keyload_load( tile->event.identity_key_path, /* pubkey only: */ 1 );
311 0 : fd_memcpy( ctx->identity_pubkey, identity_key, 32UL );
312 :
313 0 : FD_TEST( fd_rng_secure( &ctx->seed, 8UL ) );
314 0 : FD_TEST( fd_rng_secure( &ctx->instance_id, 8UL ) );
315 :
316 0 : #define FD_EVENT_ID_SEED 0x812CAFEBABEFEEE0UL
317 :
318 0 : char _boot_id[ 36 ];
319 0 : int boot_id_fd = open( "/proc/sys/kernel/random/boot_id", O_RDONLY );
320 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 ) ));
321 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 ) ));
322 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 ) ));
323 :
324 0 : ctx->boot_id = fd_hash( FD_EVENT_ID_SEED, _boot_id, 36UL );
325 :
326 0 : char _machine_id[ 32 ];
327 0 : int machine_id_fd = open( "/etc/machine-id", O_RDONLY );
328 0 : if( FD_UNLIKELY( -1==machine_id_fd ) ) FD_LOG_ERR(( "open(/etc/machine-id) failed (%d-%s)", errno, fd_io_strerror( errno ) ));
329 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 ) ));
330 0 : if( FD_UNLIKELY( -1==close( machine_id_fd ) ) ) FD_LOG_ERR(( "close(/etc/machine-id) failed (%d-%s)", errno, fd_io_strerror( errno ) ));
331 :
332 0 : ctx->machine_id = fd_hash( FD_EVENT_ID_SEED, _machine_id, 32UL );
333 :
334 0 : if( FD_UNLIKELY( !fd_netdb_open_fds( ctx->netdb_fds ) ) ) {
335 0 : FD_LOG_ERR(( "fd_netdb_open_fds failed" ));
336 0 : }
337 0 : }
338 :
339 : static void
340 : unprivileged_init( fd_topo_t * topo,
341 0 : fd_topo_tile_t * tile ) {
342 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
343 :
344 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
345 0 : fd_event_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_event_tile_t), sizeof(fd_event_tile_t) );
346 0 : void * _event_client = FD_SCRATCH_ALLOC_APPEND( l, fd_event_client_align(), fd_event_client_footprint( GRPC_BUF_MAX) );
347 0 : void * _circq = FD_SCRATCH_ALLOC_APPEND( l, fd_circq_align(), fd_circq_footprint( 1UL<<30UL ) );
348 :
349 0 : ulong sign_in_idx = fd_topo_find_tile_in_link ( topo, tile, "sign_event", tile->kind_id );
350 0 : ulong sign_out_idx = fd_topo_find_tile_out_link( topo, tile, "event_sign", tile->kind_id );
351 0 : FD_TEST( sign_in_idx!=ULONG_MAX );
352 0 : fd_topo_link_t * sign_in = &topo->links[ tile->in_link_id[ sign_in_idx ] ];
353 0 : fd_topo_link_t * sign_out = &topo->links[ tile->out_link_id[ sign_out_idx ] ];
354 0 : if( FD_UNLIKELY( !fd_keyguard_client_join( fd_keyguard_client_new( ctx->keyguard_client,
355 0 : sign_out->mcache,
356 0 : sign_out->dcache,
357 0 : sign_in->mcache,
358 0 : sign_in->dcache,
359 0 : sign_out->mtu ) ) ) ) {
360 0 : FD_LOG_ERR(( "failed to construct keyguard" ));
361 0 : }
362 :
363 0 : FD_TEST( fd_rng_join( fd_rng_new( ctx->rng, 0U, ctx->seed ) ) );
364 :
365 0 : ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->id_keyswitch_obj_id ) );
366 0 : FD_TEST( ctx->keyswitch );
367 :
368 0 : ctx->circq = fd_circq_join( fd_circq_new( _circq, 1UL<<30UL /* 1GiB */ ) );
369 0 : FD_TEST( ctx->circq );
370 :
371 0 : ctx->client = fd_event_client_join( fd_event_client_new( _event_client,
372 0 : ctx->keyguard_client,
373 0 : ctx->rng,
374 0 : ctx->circq,
375 0 : 2*(1UL<<20UL) /* 2 MiB */,
376 0 : tile->event.url,
377 0 : ctx->identity_pubkey,
378 0 : firedancer_version_string,
379 0 : ctx->instance_id,
380 0 : ctx->boot_id,
381 0 : ctx->machine_id,
382 0 : GRPC_BUF_MAX ) );
383 0 : FD_TEST( ctx->client );
384 :
385 0 : ctx->topo = topo;
386 0 : fd_memset( ctx->tile_shutdown_rendered, 0, sizeof(ctx->tile_shutdown_rendered) );
387 :
388 0 : ctx->idle_cnt = 0UL;
389 :
390 0 : ctx->in_cnt = tile->in_cnt;
391 0 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
392 0 : fd_topo_link_t * link = &topo->links[ tile->in_link_id[ i ] ];
393 0 : fd_topo_wksp_t * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
394 :
395 0 : if( FD_LIKELY( !strcmp( link->name, "net_shred" ) ) ) {
396 0 : fd_net_rx_bounds_init( &ctx->in[ i ].net_rx, link->dcache );
397 0 : ctx->in_kind[ i ] = IN_KIND_SHRED;
398 0 : continue; /* only net_rx needs to be set in this case. */
399 0 : } else if( FD_LIKELY( !strcmp( link->name, "dedup_resolv" ) ) ) ctx->in_kind[ i ] = IN_KIND_DEDUP;
400 0 : else if( FD_LIKELY( !strcmp( link->name, "sign_event" ) ) ) ctx->in_kind[ i ] = IN_KIND_SIGN;
401 0 : else if( FD_LIKELY( !strcmp( link->name, "genesi_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_GENESI;
402 0 : else if( FD_LIKELY( !strcmp( link->name, "ipecho_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_IPECHO;
403 0 : else FD_LOG_ERR(( "event tile has unexpected input link %lu %s", i, link->name ));
404 :
405 0 : ctx->in[ i ].mem = link_wksp->wksp;
406 0 : ctx->in[ i ].mtu = link->mtu;
407 0 : if( FD_UNLIKELY( ctx->in[ i ].mtu ) ) {
408 0 : ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
409 0 : ctx->in[ i ].wmark = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
410 0 : } else {
411 0 : ctx->in[ i ].chunk0 = 0UL;
412 0 : ctx->in[ i ].wmark = 0UL;
413 0 : }
414 0 : }
415 :
416 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
417 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
418 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
419 0 : }
420 :
421 : static ulong
422 : populate_allowed_seccomp( fd_topo_t const * topo,
423 : fd_topo_tile_t const * tile,
424 : ulong out_cnt,
425 0 : struct sock_filter * out ) {
426 0 : fd_event_tile_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
427 :
428 0 : populate_sock_filter_policy_fd_event_tile(
429 0 : out_cnt, out,
430 0 : (uint)fd_log_private_logfile_fd(),
431 0 : (uint)ctx->netdb_fds->etc_hosts,
432 0 : (uint)ctx->netdb_fds->etc_resolv_conf );
433 0 : return sock_filter_policy_fd_event_tile_instr_cnt;
434 0 : }
435 :
436 : static ulong
437 : populate_allowed_fds( fd_topo_t const * topo,
438 : fd_topo_tile_t const * tile,
439 : ulong out_fds_cnt,
440 0 : int * out_fds ) {
441 0 : fd_event_tile_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
442 :
443 0 : if( FD_UNLIKELY( out_fds_cnt<4UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
444 :
445 0 : ulong out_cnt = 0;
446 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
447 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
448 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
449 0 : if( FD_LIKELY( ctx->netdb_fds->etc_hosts >= 0 ) )
450 0 : out_fds[ out_cnt++ ] = ctx->netdb_fds->etc_hosts;
451 0 : out_fds[ out_cnt++ ] = ctx->netdb_fds->etc_resolv_conf;
452 0 : return out_cnt;
453 0 : }
454 :
455 : static void
456 0 : during_housekeeping( fd_event_tile_t * ctx ) {
457 0 : if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
458 0 : FD_LOG_DEBUG(( "keyswitch: switching identity" ));
459 0 : memcpy( ctx->identity_pubkey, ctx->keyswitch->bytes, 32UL );
460 0 : fd_event_client_set_identity( ctx->client, ctx->identity_pubkey );
461 0 : fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
462 0 : }
463 0 : }
464 :
465 0 : #define STEM_BURST (1UL)
466 0 : #define STEM_LAZY ((long)10e6) /* 10ms */
467 :
468 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_event_tile_t
469 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_event_tile_t)
470 :
471 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
472 0 : #define STEM_CALLBACK_BEFORE_CREDIT before_credit
473 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
474 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
475 0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
476 :
477 : #include "../stem/fd_stem.c"
478 :
479 : fd_topo_run_tile_t fd_tile_event = {
480 : .name = "event",
481 : .rlimit_file_cnt = 5UL, /* stderr, logfile, /etc/hosts, /etc/resolv.conf, and socket to the server */
482 : .populate_allowed_seccomp = populate_allowed_seccomp,
483 : .populate_allowed_fds = populate_allowed_fds,
484 : .scratch_align = scratch_align,
485 : .scratch_footprint = scratch_footprint,
486 : .privileged_init = privileged_init,
487 : .unprivileged_init = unprivileged_init,
488 : .run = stem_run,
489 : .keep_host_networking = 1,
490 : .allow_connect = 1,
491 : };
|