Line data Source code
1 : #define _GNU_SOURCE /* Enable GNU and POSIX extensions */
2 : #include "../../disco/topo/fd_topo.h"
3 : #include "../../disco/net/fd_net_tile.h"
4 : #include "../../flamenco/types/fd_types.h"
5 : #include "../../flamenco/fd_flamenco_base.h"
6 : #include "../../util/pod/fd_pod_format.h"
7 : #include "../../flamenco/gossip/fd_gossip_types.h"
8 : #include "../../disco/fd_disco.h"
9 : #include "../../discof/fd_discof.h"
10 : #include "../../discof/replay/fd_replay_tile.h"
11 : #include "../../discof/replay/fd_exec.h"
12 : #include "../../discof/restore/utils/fd_ssmsg.h"
13 : #include "../../discof/restore/utils/fd_ssmanifest_parser.h"
14 : #include "../../flamenco/runtime/sysvar/fd_sysvar_epoch_schedule.h"
15 : #include "../../disco/fd_disco.h"
16 : #include "../../util/pod/fd_pod_format.h"
17 :
18 : #include <errno.h>
19 : #include <fcntl.h>
20 : #include <sys/mman.h>
21 : #include <sys/stat.h>
22 : #include <string.h>
23 : #include <stdio.h>
24 : #include <unistd.h>
25 : #include <linux/unistd.h>
26 : #include <sys/socket.h>
27 : #include <linux/if_xdp.h>
28 : #include "generated/fd_shredcap_tile_seccomp.h"
29 :
30 :
31 : /* This tile currently has two functionalities.
32 :
33 : The first is spying on the net_shred, repair_net, and shred_out
34 : links and currently outputs to a csv that can analyze repair
35 : performance in post.
36 :
37 : The second is to capture the bank hashes from the replay tile and
38 : slices of shreds from the repair tile. These are outputted to binary
39 : files that can be used to reproduce a live replay execution. */
40 :
41 0 : #define FD_SHREDCAP_DEFAULT_WRITER_BUF_SZ (4096UL) /* local filesystem block size */
42 0 : #define FD_SHREDCAP_ALLOC_TAG (4UL)
43 : #define MAX_BUFFER_SIZE (20000UL * sizeof(fd_shred_dest_wire_t))
44 0 : #define MANIFEST_MAX_TOTAL_BANKS (2UL) /* the minimum is 2 */
45 0 : #define MANIFEST_MAX_FORK_WIDTH (1UL) /* banks are only needed during publish_stake_weights() */
46 :
47 0 : #define NET_SHRED (0UL)
48 0 : #define REPAIR_NET (1UL)
49 0 : #define SHRED_OUT (2UL)
50 0 : #define GOSSIP_OUT (3UL)
51 0 : #define REPAIR_SHREDCAP (4UL)
52 0 : #define REPLAY_OUT (5UL)
53 :
54 : typedef union {
55 : struct {
56 : fd_wksp_t * mem;
57 : ulong chunk0;
58 : ulong wmark;
59 : };
60 : fd_net_rx_bounds_t net_rx;
61 : } fd_capture_in_ctx_t;
62 :
63 : struct out_link {
64 : ulong idx;
65 : fd_frag_meta_t * mcache;
66 : ulong * sync;
67 : ulong depth;
68 : ulong seq;
69 : fd_wksp_t * mem;
70 : ulong chunk0;
71 : ulong wmark;
72 : ulong chunk;
73 : };
74 : typedef struct out_link out_link_t;
75 :
76 : struct fd_capture_tile_ctx {
77 : uchar in_kind[ 32 ];
78 : fd_capture_in_ctx_t in_links[ 32 ];
79 :
80 : int skip_frag;
81 : ushort repair_intake_listen_port;
82 :
83 : ulong shred_buffer_sz;
84 : uchar shred_buffer[ FD_NET_MTU ];
85 :
86 : ulong repair_buffer_sz;
87 : uchar repair_buffer[ FD_NET_MTU ];
88 :
89 : out_link_t stake_out[1];
90 : out_link_t snap_out[1];
91 : int enable_publish_stake_weights;
92 : ulong * manifest_wmark;
93 : uchar * manifest_bank_mem;
94 : fd_banks_t * banks;
95 : fd_bank_t * bank;
96 : char manifest_path[ PATH_MAX ];
97 : int manifest_load_done;
98 : uchar * manifest_spad_mem;
99 : fd_spad_t * manifest_spad;
100 : uchar * shared_spad_mem;
101 : fd_spad_t * shared_spad;
102 :
103 : fd_ip4_udp_hdrs_t intake_hdr[1];
104 :
105 : ulong now;
106 : ulong last_packet_ns;
107 : double tick_per_ns;
108 :
109 : fd_io_buffered_ostream_t shred_ostream;
110 : fd_io_buffered_ostream_t repair_ostream;
111 : fd_io_buffered_ostream_t fecs_ostream;
112 : fd_io_buffered_ostream_t peers_ostream;
113 : fd_io_buffered_ostream_t slices_ostream;
114 : fd_io_buffered_ostream_t bank_hashes_ostream;
115 :
116 : int shreds_fd; /* shreds snooped from net_shred */
117 : int requests_fd;
118 : int fecs_fd;
119 : int peers_fd;
120 : int slices_fd; /* all shreds in slices from repair tile */
121 : int bank_hashes_fd; /* bank hashes from replay tile */
122 :
123 : ulong write_buf_sz;
124 :
125 : uchar * shreds_buf;
126 : uchar * requests_buf;
127 : uchar * fecs_buf;
128 : uchar * peers_buf;
129 : uchar * slices_buf;
130 : uchar * bank_hashes_buf;
131 :
132 : fd_alloc_t * alloc;
133 : uchar contact_info_buffer[ MAX_BUFFER_SIZE ];
134 : };
135 : typedef struct fd_capture_tile_ctx fd_capture_tile_ctx_t;
136 :
137 : FD_FN_CONST static inline ulong
138 0 : scratch_align( void ) {
139 0 : return 4096UL;
140 0 : }
141 :
142 : FD_FN_CONST static inline ulong
143 0 : manifest_bank_align( void ) {
144 0 : return fd_banks_align();
145 0 : }
146 :
147 : FD_FN_CONST static inline ulong
148 0 : manifest_bank_footprint( void ) {
149 0 : return fd_banks_footprint( MANIFEST_MAX_TOTAL_BANKS, MANIFEST_MAX_FORK_WIDTH );
150 0 : }
151 :
152 : FD_FN_CONST static inline ulong
153 0 : manifest_load_align( void ) {
154 0 : return 128UL;
155 0 : }
156 :
157 : FD_FN_CONST static inline ulong
158 0 : manifest_load_footprint( void ) {
159 : /* A manifest typically requires 1GB, but closer to 2GB
160 : have been observed in mainnet. The footprint is then
161 : set to 2GB. TODO a future adjustment may be needed. */
162 0 : return 2UL * FD_SHMEM_GIGANTIC_PAGE_SZ;
163 0 : }
164 :
165 : FD_FN_CONST static inline ulong
166 0 : manifest_spad_max_alloc_align( void ) {
167 0 : return FD_SPAD_ALIGN;
168 0 : }
169 :
170 : FD_FN_CONST static inline ulong
171 0 : manifest_spad_max_alloc_footprint( void ) {
172 : /* The amount of memory required in the manifest load
173 : scratchpad to process it tends to be slightly larger
174 : than the manifest load footprint. */
175 0 : return manifest_load_footprint() + 128UL * FD_SHMEM_HUGE_PAGE_SZ;
176 0 : }
177 :
178 : FD_FN_CONST static inline ulong
179 0 : shared_spad_max_alloc_align( void ) {
180 0 : return FD_SPAD_ALIGN;
181 0 : }
182 :
183 : FD_FN_CONST static inline ulong
184 0 : shared_spad_max_alloc_footprint( void ) {
185 : /* The shared scratchpad is used by the manifest banks
186 : and by the manifest load (but not at the same time).
187 : The footprint for the banks needs to be equal to
188 : banks footprint (at least for the current setup with
189 : MANIFEST_MAX_TOTAL_BANKS==2). */
190 0 : return fd_ulong_max( manifest_bank_footprint(), manifest_load_footprint() );
191 0 : }
192 :
193 : FD_FN_PURE static inline ulong
194 0 : loose_footprint( fd_topo_tile_t const * tile FD_PARAM_UNUSED ) {
195 0 : ulong footprint = sizeof(fd_capture_tile_ctx_t)
196 0 : + manifest_bank_footprint()
197 0 : + fd_spad_footprint( manifest_spad_max_alloc_footprint() )
198 0 : + fd_spad_footprint( shared_spad_max_alloc_footprint() )
199 0 : + fd_alloc_footprint();
200 0 : return fd_ulong_align_up( footprint, FD_SHMEM_GIGANTIC_PAGE_SZ );
201 0 : }
202 :
203 :
204 : static ulong
205 : populate_allowed_seccomp( fd_topo_t const * topo FD_PARAM_UNUSED,
206 : fd_topo_tile_t const * tile,
207 : ulong out_cnt,
208 0 : struct sock_filter * out ) {
209 0 : populate_sock_filter_policy_fd_shredcap_tile( out_cnt,
210 0 : out,
211 0 : (uint)fd_log_private_logfile_fd(),
212 0 : (uint)tile->shredcap.shreds_fd,
213 0 : (uint)tile->shredcap.requests_fd,
214 0 : (uint)tile->shredcap.fecs_fd,
215 0 : (uint)tile->shredcap.peers_fd );
216 0 : return sock_filter_policy_fd_shredcap_tile_instr_cnt;
217 0 : }
218 :
219 : FD_FN_PURE static inline ulong
220 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
221 0 : (void)tile;
222 0 : ulong l = FD_LAYOUT_INIT;
223 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_capture_tile_ctx_t), sizeof(fd_capture_tile_ctx_t) );
224 0 : l = FD_LAYOUT_APPEND( l, manifest_bank_align(), manifest_bank_footprint() );
225 0 : l = FD_LAYOUT_APPEND( l, manifest_spad_max_alloc_align(), fd_spad_footprint( manifest_spad_max_alloc_footprint() ) );
226 0 : l = FD_LAYOUT_APPEND( l, shared_spad_max_alloc_align(), fd_spad_footprint( shared_spad_max_alloc_footprint() ) );
227 0 : l = FD_LAYOUT_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
228 0 : return FD_LAYOUT_FINI( l, scratch_align() );
229 0 : }
230 :
231 : static inline ulong
232 : generate_stake_weight_msg_manifest( ulong epoch,
233 : fd_epoch_schedule_t const * epoch_schedule,
234 : fd_snapshot_manifest_epoch_stakes_t const * epoch_stakes,
235 0 : ulong * stake_weight_msg_out ) {
236 0 : fd_stake_weight_msg_t * stake_weight_msg = (fd_stake_weight_msg_t *)fd_type_pun( stake_weight_msg_out );
237 0 : fd_vote_stake_weight_t * stake_weights = stake_weight_msg->weights;
238 :
239 0 : stake_weight_msg->epoch = epoch;
240 0 : stake_weight_msg->staked_cnt = epoch_stakes->vote_stakes_len;
241 0 : stake_weight_msg->start_slot = fd_epoch_slot0( epoch_schedule, epoch );
242 0 : stake_weight_msg->slot_cnt = epoch_schedule->slots_per_epoch;
243 0 : stake_weight_msg->excluded_stake = 0UL;
244 0 : stake_weight_msg->vote_keyed_lsched = 1UL;
245 :
246 : /* FIXME: SIMD-0180 - hack to (de)activate in testnet vs mainnet.
247 : This code can be removed once the feature is active. */
248 0 : {
249 0 : if( ( 1==epoch_schedule->warmup && epoch<FD_SIMD0180_ACTIVE_EPOCH_TESTNET )
250 0 : || ( 0==epoch_schedule->warmup && epoch<FD_SIMD0180_ACTIVE_EPOCH_MAINNET ) ) {
251 0 : stake_weight_msg->vote_keyed_lsched = 0UL;
252 0 : }
253 0 : }
254 :
255 : /* epoch_stakes from manifest are already filtered (stake>0), but not sorted */
256 0 : for( ulong i=0UL; i<epoch_stakes->vote_stakes_len; i++ ) {
257 0 : stake_weights[ i ].stake = epoch_stakes->vote_stakes[ i ].stake;
258 0 : memcpy( stake_weights[ i ].id_key.uc, epoch_stakes->vote_stakes[ i ].identity, sizeof(fd_pubkey_t) );
259 0 : memcpy( stake_weights[ i ].vote_key.uc, epoch_stakes->vote_stakes[ i ].vote, sizeof(fd_pubkey_t) );
260 0 : }
261 0 : sort_vote_weights_by_stake_vote_inplace( stake_weights, epoch_stakes->vote_stakes_len);
262 :
263 0 : return fd_stake_weight_msg_sz( epoch_stakes->vote_stakes_len );
264 0 : }
265 :
266 : static void
267 : publish_stake_weights_manifest( fd_capture_tile_ctx_t * ctx,
268 : fd_stem_context_t * stem,
269 0 : fd_snapshot_manifest_t const * manifest ) {
270 0 : fd_epoch_schedule_t const * schedule = fd_type_pun_const( &manifest->epoch_schedule_params );
271 0 : ulong epoch = fd_slot_to_epoch( schedule, manifest->slot, NULL );
272 :
273 : /* current epoch */
274 0 : ulong * stake_weights_msg = fd_chunk_to_laddr( ctx->stake_out->mem, ctx->stake_out->chunk );
275 0 : ulong stake_weights_sz = generate_stake_weight_msg_manifest( epoch, schedule, &manifest->epoch_stakes[0], stake_weights_msg );
276 0 : ulong stake_weights_sig = 4UL;
277 0 : fd_stem_publish( stem, 0UL, stake_weights_sig, ctx->stake_out->chunk, stake_weights_sz, 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
278 0 : ctx->stake_out->chunk = fd_dcache_compact_next( ctx->stake_out->chunk, stake_weights_sz, ctx->stake_out->chunk0, ctx->stake_out->wmark );
279 0 : FD_LOG_NOTICE(("sending current epoch stake weights - epoch: %lu, stake_weight_cnt: %lu, start_slot: %lu, slot_cnt: %lu", stake_weights_msg[0], stake_weights_msg[1], stake_weights_msg[2], stake_weights_msg[3]));
280 :
281 : /* next current epoch */
282 0 : stake_weights_msg = fd_chunk_to_laddr( ctx->stake_out->mem, ctx->stake_out->chunk );
283 0 : stake_weights_sz = generate_stake_weight_msg_manifest( epoch + 1, schedule, &manifest->epoch_stakes[1], stake_weights_msg );
284 0 : stake_weights_sig = 4UL;
285 0 : fd_stem_publish( stem, 0UL, stake_weights_sig, ctx->stake_out->chunk, stake_weights_sz, 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
286 0 : ctx->stake_out->chunk = fd_dcache_compact_next( ctx->stake_out->chunk, stake_weights_sz, ctx->stake_out->chunk0, ctx->stake_out->wmark );
287 0 : FD_LOG_NOTICE(("sending next epoch stake weights - epoch: %lu, stake_weight_cnt: %lu, start_slot: %lu, slot_cnt: %lu", stake_weights_msg[0], stake_weights_msg[1], stake_weights_msg[2], stake_weights_msg[3]));
288 0 : }
289 :
290 : static inline int
291 : before_frag( fd_capture_tile_ctx_t * ctx,
292 : ulong in_idx,
293 : ulong seq FD_PARAM_UNUSED,
294 0 : ulong sig ) {
295 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==NET_SHRED ) ) {
296 0 : return (int)(fd_disco_netmux_sig_proto( sig )!=DST_PROTO_SHRED) & (int)(fd_disco_netmux_sig_proto( sig )!=DST_PROTO_REPAIR);
297 0 : } else if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==GOSSIP_OUT)) {
298 0 : return sig!=FD_GOSSIP_UPDATE_TAG_CONTACT_INFO;
299 0 : }
300 0 : return 0;
301 0 : }
302 :
303 : static inline void
304 : handle_new_contact_info( fd_capture_tile_ctx_t * ctx,
305 0 : uchar const * buf ) {
306 0 : fd_gossip_update_message_t const * msg = (fd_gossip_update_message_t const *)fd_type_pun_const( buf );
307 0 : char tvu_buf[1024];
308 0 : char repair_buf[1024];
309 0 : fd_ip4_port_t tvu = msg->contact_info.contact_info->sockets[ FD_CONTACT_INFO_SOCKET_TVU ];
310 0 : fd_ip4_port_t repair = msg->contact_info.contact_info->sockets[ FD_CONTACT_INFO_SOCKET_SERVE_REPAIR ];
311 :
312 0 : if( FD_UNLIKELY( tvu.l!=0UL ) ){
313 0 : snprintf( tvu_buf, sizeof(tvu_buf),
314 0 : "%u,%u(tvu),%s,%d\n",
315 0 : tvu.addr, tvu.port, FD_BASE58_ENC_32_ALLOCA(msg->contact_info.contact_info->pubkey.uc), 1);
316 0 : int err = fd_io_buffered_ostream_write( &ctx->peers_ostream, tvu_buf, strlen(tvu_buf) );
317 0 : FD_TEST( err==0 );
318 0 : }
319 0 : if( FD_UNLIKELY( repair.l!=0UL ) ){
320 0 : snprintf( repair_buf, sizeof(repair_buf),
321 0 : "%u,%u(repair),%s,%d\n",
322 0 : repair.addr, repair.port, FD_BASE58_ENC_32_ALLOCA(msg->contact_info.contact_info->pubkey.uc), 1);
323 0 : int err = fd_io_buffered_ostream_write( &ctx->peers_ostream, repair_buf, strlen(repair_buf) );
324 0 : FD_TEST( err==0 );
325 0 : }
326 0 : }
327 :
328 : static int
329 0 : is_fec_completes_msg( ulong sz ) {
330 0 : return sz == FD_SHRED_DATA_HEADER_SZ + 2 * FD_SHRED_MERKLE_ROOT_SZ;
331 0 : }
332 :
333 : static inline void
334 : during_frag( fd_capture_tile_ctx_t * ctx,
335 : ulong in_idx,
336 : ulong seq FD_PARAM_UNUSED,
337 : ulong sig,
338 : ulong chunk,
339 : ulong sz,
340 0 : ulong ctl ) {
341 0 : ctx->skip_frag = 0;
342 0 : if( ctx->in_kind[ in_idx ]==SHRED_OUT ) {
343 0 : if( !is_fec_completes_msg( sz ) ) {
344 0 : ctx->skip_frag = 1;
345 0 : return;
346 0 : }
347 0 : fd_memcpy( ctx->shred_buffer, fd_chunk_to_laddr_const( ctx->in_links[ in_idx ].mem, chunk ), sz );
348 0 : ctx->shred_buffer_sz = sz;
349 0 : } else if( ctx->in_kind[ in_idx ] == NET_SHRED ) {
350 0 : uchar const * dcache_entry = fd_net_rx_translate_frag( &ctx->in_links[ in_idx ].net_rx, chunk, ctl, sz );
351 0 : ulong hdr_sz = fd_disco_netmux_sig_hdr_sz( sig );
352 0 : FD_TEST( hdr_sz <= sz ); /* Should be ensured by the net tile */
353 0 : fd_shred_t const * shred = fd_shred_parse( dcache_entry+hdr_sz, sz-hdr_sz );
354 0 : if( FD_UNLIKELY( !shred ) ) {
355 0 : ctx->skip_frag = 1;
356 0 : return;
357 0 : };
358 0 : fd_memcpy( ctx->shred_buffer, dcache_entry, sz );
359 0 : ctx->shred_buffer_sz = sz-hdr_sz;
360 0 : } else if( ctx->in_kind[ in_idx ] == REPAIR_NET ) {
361 : /* Repair will have outgoing pings, outgoing repair requests, and
362 : outgoing served shreds we want to filter everything but the
363 : repair requests.
364 : 1. We can index into the ip4 udp packet hdr and check if the src
365 : port is the intake listen port or serve port
366 : 2. Then we can filter on the discriminant which luckily does not
367 : require decoding! */
368 :
369 0 : uchar const * dcache_entry = fd_chunk_to_laddr_const( ctx->in_links[ in_idx ].mem, chunk );
370 0 : fd_ip4_udp_hdrs_t const * hdr = (fd_ip4_udp_hdrs_t const *)dcache_entry;
371 0 : if( hdr->udp->net_sport != fd_ushort_bswap( ctx->repair_intake_listen_port ) ) {
372 0 : ctx->skip_frag = 1;
373 0 : return;
374 0 : }
375 0 : const uchar * encoded_protocol = dcache_entry + sizeof(fd_ip4_udp_hdrs_t);
376 0 : uint discriminant = FD_LOAD(uint, encoded_protocol);
377 :
378 0 : if( FD_UNLIKELY( discriminant <= fd_repair_protocol_enum_pong ) ) {
379 0 : ctx->skip_frag = 1;
380 0 : return;
381 0 : }
382 0 : fd_memcpy( ctx->repair_buffer, dcache_entry, sz );
383 0 : ctx->repair_buffer_sz = sz;
384 0 : } else if( ctx->in_kind[ in_idx ] == REPAIR_SHREDCAP ) {
385 :
386 0 : uchar const * dcache_entry = fd_chunk_to_laddr_const( ctx->in_links[ in_idx ].mem, chunk );
387 :
388 : /* FIXME this should all be happening in after_frag */
389 :
390 : /* We expect to get all of the data shreds in a batch at once. When
391 : we do we will write the header, the shreds, and a trailer. */
392 0 : ulong payload_sz = sig;
393 0 : fd_shredcap_slice_header_msg_t header = {
394 0 : .magic = FD_SHREDCAP_SLICE_HEADER_MAGIC,
395 0 : .version = FD_SHREDCAP_SLICE_HEADER_V1,
396 0 : .payload_sz = payload_sz,
397 0 : };
398 0 : int err;
399 0 : err = fd_io_buffered_ostream_write( &ctx->slices_ostream, &header, FD_SHREDCAP_SLICE_HEADER_FOOTPRINT );
400 0 : if( FD_UNLIKELY( err != 0 ) ) {
401 0 : FD_LOG_CRIT(( "failed to write slice header %d", err ));
402 0 : }
403 0 : err = fd_io_buffered_ostream_write( &ctx->slices_ostream, dcache_entry, payload_sz );
404 0 : if( FD_UNLIKELY( err != 0 ) ) {
405 0 : FD_LOG_CRIT(( "failed to write slice data %d", err ));
406 0 : }
407 0 : fd_shredcap_slice_trailer_msg_t trailer = {
408 0 : .magic = FD_SHREDCAP_SLICE_TRAILER_MAGIC,
409 0 : .version = FD_SHREDCAP_SLICE_TRAILER_V1,
410 0 : };
411 0 : err = fd_io_buffered_ostream_write( &ctx->slices_ostream, &trailer, FD_SHREDCAP_SLICE_TRAILER_FOOTPRINT );
412 0 : if( FD_UNLIKELY( err != 0 ) ) {
413 0 : FD_LOG_CRIT(( "failed to write slice trailer %d", err ));
414 0 : }
415 :
416 0 : } else if( ctx->in_kind[ in_idx ] == REPLAY_OUT ) {
417 0 : if( FD_UNLIKELY( sig!=REPLAY_SIG_SLOT_COMPLETED ) ) return;
418 :
419 : /* FIXME this should all be happening in after_frag */
420 :
421 0 : fd_replay_slot_completed_t const * msg = fd_chunk_to_laddr_const( ctx->in_links[ in_idx ].mem, chunk );
422 0 : fd_shredcap_bank_hash_msg_t bank_hash_msg = {
423 0 : .magic = FD_SHREDCAP_BANK_HASH_MAGIC,
424 0 : .version = FD_SHREDCAP_BANK_HASH_V1
425 0 : };
426 0 : fd_memcpy( &bank_hash_msg.bank_hash, msg->bank_hash.uc, sizeof(fd_hash_t) );
427 0 : bank_hash_msg.slot = msg->slot;
428 :
429 0 : fd_io_buffered_ostream_write( &ctx->bank_hashes_ostream, &bank_hash_msg, FD_SHREDCAP_BANK_HASH_FOOTPRINT );
430 :
431 0 : } else {
432 : // contact infos can be copied into a buffer
433 0 : if( FD_UNLIKELY( chunk<ctx->in_links[ in_idx ].chunk0 || chunk>ctx->in_links[ in_idx ].wmark ) ) {
434 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz,
435 0 : ctx->in_links[ in_idx ].chunk0, ctx->in_links[ in_idx ].wmark ));
436 0 : }
437 0 : uchar const * dcache_entry = fd_chunk_to_laddr_const( ctx->in_links[ in_idx ].mem, chunk );
438 0 : fd_memcpy( ctx->contact_info_buffer, dcache_entry, sz );
439 0 : }
440 0 : }
441 :
442 : static void
443 : after_credit( fd_capture_tile_ctx_t * ctx,
444 : fd_stem_context_t * stem,
445 : int * opt_poll_in FD_PARAM_UNUSED,
446 0 : int * charge_busy FD_PARAM_UNUSED ) {
447 :
448 0 : if( FD_UNLIKELY( !ctx->manifest_load_done ) ) {
449 0 : if( FD_LIKELY( !!strcmp( ctx->manifest_path, "") ) ) {
450 : /* ctx->manifest_spad will hold the processed manifest. */
451 0 : fd_spad_reset( ctx->manifest_spad );
452 : /* do not pop from ctx->manifest_spad, the manifest needs
453 : to remain available until a new manifest is processed. */
454 :
455 0 : int fd = open( ctx->manifest_path, O_RDONLY );
456 0 : if( FD_UNLIKELY( fd < 0 ) ) {
457 0 : FD_LOG_WARNING(( "open(%s) failed (%d-%s)", ctx->manifest_path, errno, fd_io_strerror( errno ) ));
458 0 : return;
459 0 : }
460 0 : FD_LOG_NOTICE(( "manifest %s.", ctx->manifest_path ));
461 :
462 0 : fd_snapshot_manifest_t * manifest = NULL;
463 0 : FD_SPAD_FRAME_BEGIN( ctx->manifest_spad ) {
464 0 : manifest = fd_spad_alloc( ctx->manifest_spad, alignof(fd_snapshot_manifest_t), sizeof(fd_snapshot_manifest_t) );
465 0 : } FD_SPAD_FRAME_END;
466 0 : FD_TEST( manifest );
467 :
468 0 : FD_SPAD_FRAME_BEGIN( ctx->shared_spad ) {
469 0 : uchar * buf = fd_spad_alloc( ctx->shared_spad, manifest_load_align(), manifest_load_footprint() );
470 0 : ulong buf_sz = 0;
471 0 : FD_TEST( !fd_io_read( fd, buf/*dst*/, 0/*dst_min*/, manifest_load_footprint()-1UL /*dst_max*/, &buf_sz ) );
472 :
473 0 : fd_ssmanifest_parser_t * parser = fd_ssmanifest_parser_join( fd_ssmanifest_parser_new( aligned_alloc(
474 0 : fd_ssmanifest_parser_align(), fd_ssmanifest_parser_footprint() ) ) );
475 0 : FD_TEST( parser );
476 0 : fd_ssmanifest_parser_init( parser, manifest );
477 0 : int parser_err = fd_ssmanifest_parser_consume( parser, buf, buf_sz, NULL, NULL );
478 0 : FD_TEST( parser_err==1 );
479 : // if( FD_UNLIKELY( parser_err ) ) FD_LOG_ERR(( "fd_ssmanifest_parser_consume failed (%d)", parser_err ));
480 0 : } FD_SPAD_FRAME_END;
481 0 : FD_LOG_NOTICE(( "manifest bank slot %lu", manifest->slot ));
482 :
483 0 : fd_fseq_update( ctx->manifest_wmark, manifest->slot );
484 :
485 0 : uchar * chunk = fd_chunk_to_laddr( ctx->snap_out->mem, ctx->snap_out->chunk );
486 0 : ulong sz = sizeof(fd_snapshot_manifest_t);
487 0 : ulong sig = fd_ssmsg_sig( FD_SSMSG_MANIFEST_INCREMENTAL );
488 0 : memcpy( chunk, manifest, sz );
489 0 : fd_stem_publish( stem, ctx->snap_out->idx, sig, ctx->snap_out->chunk, sz, 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
490 0 : ctx->snap_out->chunk = fd_dcache_compact_next( ctx->snap_out->chunk, sz, ctx->snap_out->chunk0, ctx->snap_out->wmark );
491 :
492 0 : fd_stem_publish( stem, ctx->snap_out->idx, fd_ssmsg_sig( FD_SSMSG_DONE ), 0UL, 0UL, 0UL, 0UL, 0UL );
493 :
494 0 : publish_stake_weights_manifest( ctx, stem, manifest );
495 : //*charge_busy = 0;
496 0 : }
497 : /* No need to strcmp every time after_credit is called. */
498 0 : ctx->manifest_load_done = 1;
499 0 : }
500 0 : }
501 :
502 : static inline void
503 : after_frag( fd_capture_tile_ctx_t * ctx,
504 : ulong in_idx,
505 : ulong seq FD_PARAM_UNUSED,
506 : ulong sig,
507 : ulong sz,
508 : ulong tsorig FD_PARAM_UNUSED,
509 : ulong tspub FD_PARAM_UNUSED,
510 0 : fd_stem_context_t * stem FD_PARAM_UNUSED ) {
511 0 : if( FD_UNLIKELY( ctx->skip_frag ) ) return;
512 :
513 0 : if( ctx->in_kind[ in_idx ] == SHRED_OUT ) {
514 : /* This is a fec completes message! we can use it to check how long
515 : it takes to complete a fec */
516 :
517 0 : fd_shred_t const * shred = (fd_shred_t *)fd_type_pun( ctx->shred_buffer );
518 0 : uint data_cnt = fd_disco_shred_out_fec_sig_data_cnt( sig );
519 0 : uint ref_tick = shred->data.flags & FD_SHRED_DATA_REF_TICK_MASK;
520 0 : char fec_complete[1024];
521 0 : snprintf( fec_complete, sizeof(fec_complete),
522 0 : "%ld,%lu,%u,%u,%u\n",
523 0 : fd_log_wallclock(), shred->slot, ref_tick, shred->fec_set_idx, data_cnt );
524 :
525 : // Last shred is guaranteed to be a data shred
526 :
527 :
528 0 : int err = fd_io_buffered_ostream_write( &ctx->fecs_ostream, fec_complete, strlen(fec_complete) );
529 0 : FD_TEST( err==0 );
530 0 : } else if( ctx->in_kind[ in_idx ] == NET_SHRED ) {
531 : /* TODO: leader schedule early exits in shred tile right around
532 : startup, which discards some turbine shreds, but there is a
533 : chance we capture this shred here. Currently handled in post, but
534 : in the future will want to get the leader schedule here so we can
535 : also benchmark whether the excepcted sender in the turbine tree
536 : matches the actual sender. */
537 :
538 0 : ulong hdr_sz = fd_disco_netmux_sig_hdr_sz( sig );
539 0 : fd_ip4_udp_hdrs_t * hdr = (fd_ip4_udp_hdrs_t *)ctx->shred_buffer;
540 0 : uint src_ip4_addr = hdr->ip4->saddr;
541 0 : ushort src_port = hdr->udp->net_sport;
542 :
543 0 : fd_shred_t const * shred = fd_shred_parse( ctx->shred_buffer + hdr_sz, sz - hdr_sz );
544 0 : int is_turbine = fd_disco_netmux_sig_proto( sig ) == DST_PROTO_SHRED;
545 0 : uint nonce = is_turbine ? 0 : FD_LOAD(uint, ctx->shred_buffer + hdr_sz + fd_shred_sz( shred ) );
546 0 : int is_data = fd_shred_is_data( fd_shred_type( shred->variant ) );
547 0 : ulong slot = shred->slot;
548 0 : uint idx = shred->idx;
549 0 : uint fec_idx = shred->fec_set_idx;
550 0 : uint ref_tick = 65;
551 0 : if( FD_UNLIKELY( is_turbine && is_data ) ) {
552 : /* We can then index into the flag and get a REFTICK */
553 0 : ref_tick = shred->data.flags & FD_SHRED_DATA_REF_TICK_MASK;
554 0 : }
555 :
556 0 : char repair_data_buf[1024];
557 0 : snprintf( repair_data_buf, sizeof(repair_data_buf),
558 0 : "%u,%u,%ld,%lu,%u,%u,%u,%d,%d,%u\n",
559 0 : src_ip4_addr, src_port, fd_log_wallclock(), slot, ref_tick, fec_idx, idx, is_turbine, is_data, nonce );
560 :
561 0 : int err = fd_io_buffered_ostream_write( &ctx->shred_ostream, repair_data_buf, strlen(repair_data_buf) );
562 0 : FD_TEST( err==0 );
563 0 : } else if( ctx->in_kind[ in_idx ] == REPAIR_NET ) {
564 : /* We have a valid repair request that we can finally decode.
565 : Unfortunately we actually have to decode because we cant cast
566 : directly to the protocol */
567 0 : fd_ip4_udp_hdrs_t * hdr = (fd_ip4_udp_hdrs_t *)ctx->repair_buffer;
568 0 : fd_repair_protocol_t protocol;
569 0 : fd_bincode_decode_ctx_t bctx = { .data = ctx->repair_buffer + sizeof(fd_ip4_udp_hdrs_t), .dataend = ctx->repair_buffer + ctx->repair_buffer_sz };
570 0 : fd_repair_protocol_t * decoded = fd_repair_protocol_decode( &protocol, &bctx );
571 :
572 0 : FD_TEST( decoded == &protocol );
573 0 : FD_TEST( decoded != NULL );
574 :
575 0 : uint peer_ip4_addr = hdr->ip4->daddr;
576 0 : ushort peer_port = hdr->udp->net_dport;
577 0 : ulong slot = 0UL;
578 0 : ulong shred_index = UINT_MAX;
579 0 : uint nonce = 0U;
580 :
581 0 : switch( protocol.discriminant ) {
582 0 : case fd_repair_protocol_enum_window_index: {
583 0 : slot = protocol.inner.window_index.slot;
584 0 : shred_index = protocol.inner.window_index.shred_index;
585 0 : nonce = protocol.inner.window_index.header.nonce;
586 0 : break;
587 0 : }
588 0 : case fd_repair_protocol_enum_highest_window_index: {
589 0 : slot = protocol.inner.highest_window_index.slot;
590 0 : shred_index = protocol.inner.highest_window_index.shred_index;
591 0 : nonce = protocol.inner.highest_window_index.header.nonce;
592 0 : break;
593 0 : }
594 0 : case fd_repair_protocol_enum_orphan: {
595 0 : slot = protocol.inner.orphan.slot;
596 0 : nonce = protocol.inner.orphan.header.nonce;
597 0 : break;
598 0 : }
599 0 : default:
600 0 : break;
601 0 : }
602 :
603 0 : char repair_data_buf[1024];
604 0 : snprintf( repair_data_buf, sizeof(repair_data_buf),
605 0 : "%u,%u,%ld,%u,%lu,%lu\n",
606 0 : peer_ip4_addr, peer_port, fd_log_wallclock(), nonce, slot, shred_index );
607 0 : int err = fd_io_buffered_ostream_write( &ctx->repair_ostream, repair_data_buf, strlen(repair_data_buf) );
608 0 : FD_TEST( err==0 );
609 0 : } else if( ctx->in_kind[ in_idx ] == GOSSIP_OUT ) {
610 0 : handle_new_contact_info( ctx, ctx->contact_info_buffer );
611 0 : }
612 0 : }
613 :
614 : static ulong
615 : populate_allowed_fds( fd_topo_t const * topo FD_PARAM_UNUSED,
616 : fd_topo_tile_t const * tile,
617 : ulong out_fds_cnt FD_PARAM_UNUSED,
618 0 : int * out_fds ) {
619 0 : ulong out_cnt = 0UL;
620 :
621 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
622 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
623 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
624 0 : if( FD_LIKELY( -1!=tile->shredcap.shreds_fd ) )
625 0 : out_fds[ out_cnt++ ] = tile->shredcap.shreds_fd; /* shred file */
626 0 : if( FD_LIKELY( -1!=tile->shredcap.requests_fd ) )
627 0 : out_fds[ out_cnt++ ] = tile->shredcap.requests_fd; /* request file */
628 0 : if( FD_LIKELY( -1!=tile->shredcap.fecs_fd ) )
629 0 : out_fds[ out_cnt++ ] = tile->shredcap.fecs_fd; /* fec complete file */
630 0 : if( FD_LIKELY( -1!=tile->shredcap.peers_fd ) )
631 0 : out_fds[ out_cnt++ ] = tile->shredcap.peers_fd; /* peers file */
632 0 : if( FD_LIKELY( -1!=tile->shredcap.slices_fd ) )
633 0 : out_fds[ out_cnt++ ] = tile->shredcap.slices_fd; /* slices file */
634 0 : if( FD_LIKELY( -1!=tile->shredcap.bank_hashes_fd ) )
635 0 : out_fds[ out_cnt++ ] = tile->shredcap.bank_hashes_fd; /* bank hashes file */
636 :
637 0 : return out_cnt;
638 0 : }
639 :
640 : static void
641 : privileged_init( fd_topo_t * topo FD_PARAM_UNUSED,
642 0 : fd_topo_tile_t * tile ) {
643 0 : char file_path[PATH_MAX];
644 0 : strcpy( file_path, tile->shredcap.folder_path );
645 0 : strcat( file_path, "/shred_data.csv" );
646 0 : tile->shredcap.shreds_fd = open( file_path, O_WRONLY|O_CREAT|O_APPEND /*| O_DIRECT*/, 0644 );
647 0 : if ( FD_UNLIKELY( tile->shredcap.shreds_fd == -1 ) ) {
648 0 : FD_LOG_ERR(( "failed to open or create shred csv dump file %s %d %s", file_path, errno, strerror(errno) ));
649 0 : }
650 :
651 0 : strcpy( file_path, tile->shredcap.folder_path );
652 0 : strcat( file_path, "/request_data.csv" );
653 0 : tile->shredcap.requests_fd = open( file_path, O_WRONLY|O_CREAT|O_APPEND /*| O_DIRECT*/, 0644 );
654 0 : if ( FD_UNLIKELY( tile->shredcap.requests_fd == -1 ) ) {
655 0 : FD_LOG_ERR(( "failed to open or create request csv dump file %s %d %s", file_path, errno, strerror(errno) ));
656 0 : }
657 :
658 0 : strcpy( file_path, tile->shredcap.folder_path );
659 0 : strcat( file_path, "/fec_complete.csv" );
660 0 : tile->shredcap.fecs_fd = open( file_path, O_WRONLY|O_CREAT|O_APPEND /*| O_DIRECT*/, 0644 );
661 0 : if ( FD_UNLIKELY( tile->shredcap.fecs_fd == -1 ) ) {
662 0 : FD_LOG_ERR(( "failed to open or create fec complete csv dump file %s %d %s", file_path, errno, strerror(errno) ));
663 0 : }
664 0 : FD_LOG_NOTICE(( "Opening shred csv dump file at %s", file_path ));
665 :
666 0 : strcpy( file_path, tile->shredcap.folder_path );
667 0 : strcat( file_path, "/peers.csv" );
668 0 : tile->shredcap.peers_fd = open( file_path, O_WRONLY|O_CREAT|O_APPEND /*| O_DIRECT*/, 0644 );
669 0 : if ( FD_UNLIKELY( tile->shredcap.peers_fd == -1 ) ) {
670 0 : FD_LOG_ERR(( "failed to open or create peers csv dump file %s %d %s", file_path, errno, strerror(errno) ));
671 0 : }
672 :
673 0 : strcpy( file_path, tile->shredcap.folder_path );
674 0 : strcat( file_path, "/slices.bin" );
675 0 : tile->shredcap.slices_fd = open( file_path, O_WRONLY|O_CREAT|O_APPEND /*| O_DIRECT*/, 0644 );
676 0 : if ( FD_UNLIKELY( tile->shredcap.slices_fd == -1 ) ) {
677 0 : FD_LOG_ERR(( "failed to open or create slices csv dump file %s %d %s", file_path, errno, strerror(errno) ));
678 0 : }
679 0 : FD_LOG_NOTICE(( "Opening val_shreds binary dump file at %s", file_path ));
680 :
681 0 : strcpy( file_path, tile->shredcap.folder_path );
682 0 : strcat( file_path, "/bank_hashes.bin" );
683 0 : tile->shredcap.bank_hashes_fd = open( file_path, O_WRONLY|O_CREAT|O_APPEND /*| O_DIRECT*/, 0644 );
684 0 : if ( FD_UNLIKELY( tile->shredcap.bank_hashes_fd == -1 ) ) {
685 0 : FD_LOG_ERR(( "failed to open or create bank_hashes csv dump file %s %d %s", file_path, errno, strerror(errno) ));
686 0 : }
687 0 : FD_LOG_NOTICE(( "Opening bank_hashes binary dump file at %s", file_path ));
688 0 : }
689 :
690 : static void
691 : init_file_handlers( fd_capture_tile_ctx_t * ctx,
692 : int * ctx_file,
693 : int tile_file,
694 : uchar ** ctx_buf,
695 0 : fd_io_buffered_ostream_t * ctx_ostream ) {
696 0 : *ctx_file = tile_file ;
697 :
698 0 : int err = ftruncate( *ctx_file, 0UL );
699 0 : if( FD_UNLIKELY( err ) ) {
700 0 : FD_LOG_ERR(( "failed to truncate file (%i-%s)", errno, fd_io_strerror( errno ) ));
701 0 : }
702 0 : long seek = lseek( *ctx_file, 0UL, SEEK_SET );
703 0 : if( FD_UNLIKELY( seek!=0L ) ) {
704 0 : FD_LOG_ERR(( "failed to seek to the beginning of file" ));
705 0 : }
706 :
707 0 : *ctx_buf = fd_alloc_malloc( ctx->alloc, 4096, ctx->write_buf_sz );
708 0 : if( FD_UNLIKELY( *ctx_buf == NULL ) ) {
709 0 : FD_LOG_ERR(( "failed to allocate ostream buffer" ));
710 0 : }
711 :
712 0 : if( FD_UNLIKELY( !fd_io_buffered_ostream_init(
713 0 : ctx_ostream,
714 0 : *ctx_file,
715 0 : *ctx_buf,
716 0 : ctx->write_buf_sz ) ) ) {
717 0 : FD_LOG_ERR(( "failed to initialize ostream" ));
718 0 : }
719 0 : }
720 :
721 :
722 : static void
723 : unprivileged_init( fd_topo_t * topo,
724 0 : fd_topo_tile_t * tile ) {
725 :
726 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
727 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
728 0 : fd_capture_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_capture_tile_ctx_t), sizeof(fd_capture_tile_ctx_t) );
729 0 : void * manifest_bank_mem = FD_SCRATCH_ALLOC_APPEND( l, manifest_bank_align(), manifest_bank_footprint() );
730 0 : void * manifest_spad_mem = FD_SCRATCH_ALLOC_APPEND( l, manifest_spad_max_alloc_align(), fd_spad_footprint( manifest_spad_max_alloc_footprint() ) );
731 0 : void * shared_spad_mem = FD_SCRATCH_ALLOC_APPEND( l, shared_spad_max_alloc_align(), fd_spad_footprint( shared_spad_max_alloc_footprint() ) );
732 0 : void * alloc_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
733 0 : FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
734 :
735 : /* Input links */
736 0 : for( ulong i=0; i<tile->in_cnt; i++ ) {
737 0 : fd_topo_link_t * link = &topo->links[ tile->in_link_id[ i ] ];
738 0 : fd_topo_wksp_t * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
739 0 : if( 0==strcmp( link->name, "net_shred" ) ) {
740 0 : ctx->in_kind[ i ] = NET_SHRED;
741 0 : fd_net_rx_bounds_init( &ctx->in_links[ i ].net_rx, link->dcache );
742 0 : continue;
743 0 : } else if( 0==strcmp( link->name, "repair_net" ) ) {
744 0 : ctx->in_kind[ i ] = REPAIR_NET;
745 0 : } else if( 0==strcmp( link->name, "shred_out" ) ) {
746 0 : ctx->in_kind[ i ] = SHRED_OUT;
747 0 : } else if( 0==strcmp( link->name, "gossip_out" ) ) {
748 0 : ctx->in_kind[ i ] = GOSSIP_OUT;
749 0 : } else if( 0==strcmp( link->name, "repair_scap" ) ) {
750 0 : ctx->in_kind[ i ] = REPAIR_SHREDCAP;
751 0 : } else if( 0==strcmp( link->name, "replay_out" ) ) {
752 0 : ctx->in_kind[ i ] = REPLAY_OUT;
753 0 : } else {
754 0 : FD_LOG_ERR(( "scap tile has unexpected input link %s", link->name ));
755 0 : }
756 :
757 0 : ctx->in_links[ i ].mem = link_wksp->wksp;
758 0 : ctx->in_links[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in_links[ i ].mem, link->dcache );
759 0 : ctx->in_links[ i ].wmark = fd_dcache_compact_wmark ( ctx->in_links[ i ].mem, link->dcache, link->mtu );
760 0 : }
761 :
762 0 : ctx->repair_intake_listen_port = tile->shredcap.repair_intake_listen_port;
763 0 : ctx->write_buf_sz = tile->shredcap.write_buffer_size ? tile->shredcap.write_buffer_size : FD_SHREDCAP_DEFAULT_WRITER_BUF_SZ;
764 :
765 : /* Set up stake weights tile output */
766 0 : ctx->stake_out->idx = fd_topo_find_tile_out_link( topo, tile, "replay_stake", 0 );
767 0 : if( FD_LIKELY( ctx->stake_out->idx!=ULONG_MAX ) ) {
768 0 : fd_topo_link_t * stake_weights_out = &topo->links[ tile->out_link_id[ ctx->stake_out->idx] ];
769 0 : ctx->stake_out->mcache = stake_weights_out->mcache;
770 0 : ctx->stake_out->mem = topo->workspaces[ topo->objs[ stake_weights_out->dcache_obj_id ].wksp_id ].wksp;
771 0 : ctx->stake_out->sync = fd_mcache_seq_laddr ( ctx->stake_out->mcache );
772 0 : ctx->stake_out->depth = fd_mcache_depth ( ctx->stake_out->mcache );
773 0 : ctx->stake_out->seq = fd_mcache_seq_query ( ctx->stake_out->sync );
774 0 : ctx->stake_out->chunk0 = fd_dcache_compact_chunk0( ctx->stake_out->mem, stake_weights_out->dcache );
775 0 : ctx->stake_out->wmark = fd_dcache_compact_wmark ( ctx->stake_out->mem, stake_weights_out->dcache, stake_weights_out->mtu );
776 0 : ctx->stake_out->chunk = ctx->stake_out->chunk0;
777 0 : } else {
778 0 : FD_LOG_WARNING(( "no connection to stake_out link" ));
779 0 : memset( ctx->stake_out, 0, sizeof(out_link_t) );
780 0 : }
781 :
782 0 : ctx->snap_out->idx = fd_topo_find_tile_out_link( topo, tile, "snap_out", 0 );
783 0 : if( FD_LIKELY( ctx->snap_out->idx!=ULONG_MAX ) ) {
784 0 : fd_topo_link_t * snap_out = &topo->links[tile->out_link_id[ctx->snap_out->idx]];
785 0 : ctx->snap_out->mem = topo->workspaces[topo->objs[snap_out->dcache_obj_id].wksp_id].wksp;
786 0 : ctx->snap_out->chunk0 = fd_dcache_compact_chunk0( ctx->snap_out->mem, snap_out->dcache );
787 0 : ctx->snap_out->wmark = fd_dcache_compact_wmark( ctx->snap_out->mem, snap_out->dcache, snap_out->mtu );
788 0 : ctx->snap_out->chunk = ctx->snap_out->chunk0;
789 0 : } else {
790 0 : FD_LOG_WARNING(( "no connection to snap_out link" ));
791 0 : memset( ctx->snap_out, 0, sizeof(out_link_t) );
792 0 : }
793 :
794 : /* If the manifest is enabled (for processing), the stake_out link
795 : must be connected to the tile. TODO in principle, it should be
796 : possible to gate the remaining of the manifest-related config. */
797 0 : ctx->enable_publish_stake_weights = tile->shredcap.enable_publish_stake_weights;
798 0 : FD_LOG_NOTICE(( "enable_publish_stake_weights ? %d", ctx->enable_publish_stake_weights ));
799 :
800 : /* manifest_wmark (root slot) */
801 0 : ulong root_slot_obj_id = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "root_slot" );
802 0 : if( FD_LIKELY( root_slot_obj_id!=ULONG_MAX ) ) { /* for profiler */
803 0 : ctx->manifest_wmark = fd_fseq_join( fd_topo_obj_laddr( topo, root_slot_obj_id ) );
804 0 : if( FD_UNLIKELY( !ctx->manifest_wmark ) ) FD_LOG_ERR(( "no root_slot fseq" ));
805 0 : FD_TEST( ULONG_MAX==fd_fseq_query( ctx->manifest_wmark ) );
806 0 : }
807 :
808 0 : ctx->manifest_bank_mem = manifest_bank_mem;
809 :
810 0 : ctx->banks = fd_banks_join( fd_banks_new( ctx->manifest_bank_mem, MANIFEST_MAX_TOTAL_BANKS, MANIFEST_MAX_FORK_WIDTH ) );
811 0 : FD_TEST( ctx->banks );
812 0 : ctx->bank = fd_banks_init_bank( ctx->banks );
813 0 : fd_bank_slot_set( ctx->bank, 0UL );
814 0 : FD_TEST( ctx->bank );
815 :
816 0 : strncpy( ctx->manifest_path, tile->shredcap.manifest_path, PATH_MAX );
817 0 : ctx->manifest_load_done = 0;
818 0 : ctx->manifest_spad_mem = manifest_spad_mem;
819 0 : ctx->manifest_spad = fd_spad_join( fd_spad_new( ctx->manifest_spad_mem, manifest_spad_max_alloc_footprint() ) );
820 0 : ctx->shared_spad_mem = shared_spad_mem;
821 0 : ctx->shared_spad = fd_spad_join( fd_spad_new( ctx->shared_spad_mem, shared_spad_max_alloc_footprint() ) );
822 :
823 : /* Allocate the write buffers */
824 0 : ctx->alloc = fd_alloc_join( fd_alloc_new( alloc_mem, FD_SHREDCAP_ALLOC_TAG ), fd_tile_idx() );
825 0 : if( FD_UNLIKELY( !ctx->alloc ) ) {
826 0 : FD_LOG_ERR( ( "fd_alloc_join failed" ) );
827 0 : }
828 :
829 : /* Setup the csv files to be in the expected state */
830 :
831 0 : init_file_handlers( ctx, &ctx->shreds_fd, tile->shredcap.shreds_fd, &ctx->shreds_buf, &ctx->shred_ostream );
832 0 : init_file_handlers( ctx, &ctx->requests_fd, tile->shredcap.requests_fd, &ctx->requests_buf, &ctx->repair_ostream );
833 0 : init_file_handlers( ctx, &ctx->fecs_fd, tile->shredcap.fecs_fd, &ctx->fecs_buf, &ctx->fecs_ostream );
834 0 : init_file_handlers( ctx, &ctx->peers_fd, tile->shredcap.peers_fd, &ctx->peers_buf, &ctx->peers_ostream );
835 :
836 0 : int err = fd_io_buffered_ostream_write( &ctx->shred_ostream, "src_ip,src_port,timestamp,slot,ref_tick,fec_set_idx,idx,is_turbine,is_data,nonce\n", 81UL );
837 0 : err |= fd_io_buffered_ostream_write( &ctx->repair_ostream, "dst_ip,dst_port,timestamp,nonce,slot,idx\n", 41UL );
838 0 : err |= fd_io_buffered_ostream_write( &ctx->fecs_ostream, "timestamp,slot,ref_tick,fec_set_idx,data_cnt\n", 45UL );
839 0 : err |= fd_io_buffered_ostream_write( &ctx->peers_ostream, "peer_ip4_addr,peer_port,pubkey,turbine\n", 48UL );
840 :
841 0 : if( FD_UNLIKELY( err ) ) {
842 0 : FD_LOG_ERR(( "failed to write header to any of the 4 csv files (%i-%s)", errno, fd_io_strerror( errno ) ));
843 0 : }
844 :
845 : /* Setup the binary files to be in the expected state. These files are
846 : not csv, so we don't need headers. */
847 0 : init_file_handlers( ctx, &ctx->slices_fd, tile->shredcap.slices_fd, &ctx->slices_buf, &ctx->slices_ostream );
848 0 : init_file_handlers( ctx, &ctx->bank_hashes_fd, tile->shredcap.bank_hashes_fd, &ctx->bank_hashes_buf, &ctx->bank_hashes_ostream );
849 0 : }
850 :
851 0 : #define STEM_BURST (1UL)
852 0 : #define STEM_LAZY (50UL)
853 :
854 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_capture_tile_ctx_t
855 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_capture_tile_ctx_t)
856 :
857 0 : #define STEM_CALLBACK_AFTER_CREDIT after_credit
858 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
859 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
860 0 : #define STEM_CALLBACK_BEFORE_FRAG before_frag
861 :
862 : #include "../../disco/stem/fd_stem.c"
863 :
864 : fd_topo_run_tile_t fd_tile_shredcap = {
865 : .name = "scap",
866 : .loose_footprint = loose_footprint,
867 : .populate_allowed_seccomp = populate_allowed_seccomp,
868 : .populate_allowed_fds = populate_allowed_fds,
869 : .scratch_align = scratch_align,
870 : .scratch_footprint = scratch_footprint,
871 : .privileged_init = privileged_init,
872 : .unprivileged_init = unprivileged_init,
873 : .run = stem_run,
874 : };
|