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