Line data Source code
1 : /* REQUEST HANDLING ARCHITECTURE
2 : =========================================
3 :
4 : The repair tile implements two distinct request handling patterns
5 : based on the nature of the operation and its latency requirements:
6 :
7 : 1. SYNCHRONOUS REQUEST HANDLING
8 : -----------------------------------------
9 : Used for lightweight protocol messages that require immediate
10 : signing and response. These operations use the keyguard client for
11 : direct signing, which requires blocking.
12 :
13 : Message types handled synchronously:
14 : - PINGs & PONGs: Handles peer connectivity and liveness with simple
15 : round-trip messages.
16 :
17 : - PEER WARM UPs: On receiving peer information in
18 : handle_new_cluster_contact_info, we prepay the RTT cost by sending
19 : a placeholder Repair request immediately.
20 :
21 : 2. ASYNCHRONOUS REQUEST HANDLING
22 : --------------------------------
23 : Used strictly for repair requests. These requests are sent to the
24 : sign tile, and the repair tile continues handling other operations
25 : without blocking. Once the sign tile has signed the request, the
26 : repair tile will complete the request from its pending sign request
27 : deque and send the response.
28 :
29 : Note we do MANUAL credit tracking for these asynchronous sign links
30 : (see out_ctx_t definition). In particular, credits tracks the
31 : RETURN sign_repair link. This is because repair_sign is reliable,
32 : and sign_repair is unreliable. If both links were reliable, and the
33 : links filled completely, stem would get into a deadlock. Neither
34 : repair or sign would have credits, which would prevent frags from
35 : getting polled in repair or sign, which would prevent any credits
36 : from getting returned back to the tiles.
37 :
38 : Thus the sign_repair link must be unreliable. This is mostly ok,
39 : because repair_sign is still reliable, so in theory repair_tile
40 : would never publish enough frags such that sign_repair would get
41 : overrun.
42 :
43 : However, there is a fairly common case that breaks this. Consider
44 : the scenario
45 :
46 : repair_sign (depth 128) sign_repair (depth 128)
47 : repair ----------------------> sign ------------------------> repair
48 : [rest free, r130, r129] [r128, r127, ... , r1] (full)
49 :
50 : This would happen because repair is publishing too many requests too
51 : fast(common in catchup), and not polling enough frags from sign.
52 : Nothing is stopping repair from publishing more requests, because
53 : sign is functioning fast enough to handle the requests. However,
54 : nothing is stopping sign from polling the next request and signing
55 : it, and PUBLISHING it on the sign_repair link that is already full,
56 : because the sign_repair link is unreliable.
57 :
58 : In fact the only time we could stop repair from publishing more
59 : requests is if repair_sign was full, and repair would get
60 : backpressured, but sign would still be able to poll requests and
61 : overrun the sign_repair link.
62 :
63 : This is why we need to manually track credits for the sign_repair
64 : link. We must ensure that there are never more than 128 items in the
65 : ENTIRE repair_sign -> sign tile -> sign_repair work queue, else
66 : there is always a possibility of an overrun in the sign_repair link.
67 :
68 : To lose a frag to overrun isn't necessarily critical, but in general
69 : the repair tile relies on the fact that a signing task published to
70 : sign tile will always come back. If we lose a frag to overrun, then
71 : there will be an entry in the pending signs structure that is never
72 : removed, and theoretically the map could fill up. Conceptually, with
73 : a reliable sign->repair->sign structure, there should be no eviction
74 : needed in this pending signs structure.
75 :
76 : Message types handled asynchronously:
77 : - WINDOW_INDEX (exact shred): Requests for a specific shred at a
78 : known slot and index. Used when the repair tile knows exactly
79 : which shred is missing from a FEC set.
80 :
81 : - HIGHEST_WINDOW_INDEX: Requests for the highest shred in a slot.
82 : Used to determine the end boundary of a slot when the exact count
83 : is unknown.
84 :
85 : - ORPHAN: Requests for the highest shred in the parent slot of an
86 : orphaned slot. Used to establish the chain of slot ancestry when a
87 : slot's parent is missing.
88 :
89 : Async requests can be distributed across multiple sign tiles using
90 : round-robin based on the request nonce. This provides load balancing
91 : and prevents any single sign tile from becoming a bottleneck. */
92 :
93 : #define _GNU_SOURCE
94 :
95 : #include "../../disco/topo/fd_topo.h"
96 : #include "generated/fd_repair_tile_seccomp.h"
97 : #include "../../disco/fd_disco.h"
98 : #include "../../disco/keyguard/fd_keyload.h"
99 : #include "../../disco/keyguard/fd_keyguard.h"
100 : #include "../../disco/net/fd_net_tile.h"
101 : #include "../../disco/store/fd_store.h"
102 : #include "../../flamenco/gossip/fd_gossip_types.h"
103 : #include "../tower/fd_tower_tile.h"
104 : #include "../../discof/restore/utils/fd_ssmsg.h"
105 : #include "../../util/pod/fd_pod_format.h"
106 : #include "../../util/net/fd_net_headers.h"
107 : #include "../../tango/fd_tango_base.h"
108 :
109 : #include "../forest/fd_forest.h"
110 : #include "fd_repair_metrics.h"
111 : #include "fd_inflight.h"
112 : #include "fd_repair.h"
113 : #include "fd_policy.h"
114 :
115 : #define LOGGING 1
116 : #define DEBUG_LOGGING 0
117 :
118 : #define IN_KIND_CONTACT (0)
119 0 : #define IN_KIND_NET (1)
120 0 : #define IN_KIND_TOWER (2)
121 0 : #define IN_KIND_SHRED (3)
122 0 : #define IN_KIND_SIGN (4)
123 0 : #define IN_KIND_SNAP (5)
124 0 : #define IN_KIND_STAKE (6)
125 0 : #define IN_KIND_GOSSIP (7)
126 0 : #define IN_KIND_GENESIS (8)
127 :
128 : #define MAX_IN_LINKS (16)
129 :
130 : #define MAX_REPAIR_PEERS 40200UL
131 : #define MAX_BUFFER_SIZE ( MAX_REPAIR_PEERS * sizeof( fd_shred_dest_wire_t ) )
132 : #define MAX_SHRED_TILE_CNT ( 16UL )
133 : #define MAX_SIGN_TILE_CNT ( 16UL )
134 :
135 : /* Maximum size of a network packet */
136 0 : #define FD_REPAIR_MAX_PACKET_SIZE 1232
137 : /* Max number of validators that can be actively queried */
138 0 : #define FD_ACTIVE_KEY_MAX (FD_CONTACT_INFO_TABLE_SIZE)
139 : /* Max number of pending shred requests */
140 0 : #define FD_NEEDED_KEY_MAX (1<<20)
141 :
142 : /* static map from request type to metric array index */
143 : static uint metric_index[FD_REPAIR_KIND_ORPHAN + 1] = {
144 : [FD_REPAIR_KIND_SHRED] = FD_METRICS_ENUM_REPAIR_SENT_REQUEST_TYPES_V_NEEDED_WINDOW_IDX,
145 : [FD_REPAIR_KIND_HIGHEST_SHRED] = FD_METRICS_ENUM_REPAIR_SENT_REQUEST_TYPES_V_NEEDED_HIGHEST_WINDOW_IDX,
146 : [FD_REPAIR_KIND_ORPHAN] = FD_METRICS_ENUM_REPAIR_SENT_REQUEST_TYPES_V_NEEDED_ORPHAN_IDX,
147 : };
148 :
149 : typedef union {
150 : struct {
151 : fd_wksp_t * mem;
152 : ulong chunk0;
153 : ulong wmark;
154 : ulong mtu;
155 : };
156 : fd_net_rx_bounds_t net_rx;
157 : } in_ctx_t;
158 :
159 : struct out_ctx {
160 : ulong idx;
161 : fd_wksp_t * mem;
162 : ulong chunk0;
163 : ulong wmark;
164 : ulong chunk;
165 :
166 : ulong in_idx; /* index of the incoming link */
167 : ulong credits; /* available credits for link */
168 : ulong max_credits; /* maximum credits (depth) */
169 :
170 : /* credits / max_credits are used by the repair_sign link. In
171 : particular, credits manages the RETURN sign_repair link. See top
172 : of file for more details. */
173 : };
174 : typedef struct out_ctx out_ctx_t;
175 :
176 : struct fd_fec_sig {
177 : ulong key; /* map key. 32 msb = slot, 32 lsb = fec_set_idx */
178 : fd_ed25519_sig_t sig; /* Ed25519 sig identifier of the FEC. */
179 : };
180 : typedef struct fd_fec_sig fd_fec_sig_t;
181 :
182 : #define MAP_NAME fd_fec_sig
183 0 : #define MAP_T fd_fec_sig_t
184 : #define MAP_MEMOIZE 0
185 : #include "../../util/tmpl/fd_map_dynamic.c"
186 :
187 : /* Data needed to sign and send a pong that is not contained in the
188 : pong msg itself. */
189 : struct pong_data {
190 : fd_ip4_port_t peer_addr;
191 : fd_hash_t hash;
192 : uint daddr;
193 : };
194 : typedef struct pong_data pong_data_t;
195 :
196 : struct sign_req {
197 : ulong key; /* map key, ctx->pending_key_next */
198 : ulong buflen;
199 : union {
200 : uchar buf[sizeof(fd_repair_msg_t)];
201 : fd_repair_msg_t msg;
202 : };
203 : pong_data_t pong_data; /* populated only for pong msgs */
204 : };
205 : typedef struct sign_req sign_req_t;
206 :
207 : #define MAP_NAME fd_signs_map
208 0 : #define MAP_KEY key
209 0 : #define MAP_KEY_NULL ULONG_MAX
210 0 : #define MAP_KEY_INVAL(k) (k==ULONG_MAX)
211 0 : #define MAP_T sign_req_t
212 : #define MAP_MEMOIZE 0
213 : #include "../../util/tmpl/fd_map_dynamic.c"
214 :
215 : /* Because the sign tiles could be all busy when a contact info arrives,
216 : we need to save ping messages to be signed in a queue and dispatched
217 : in after_credit when there are sign tiles available. The size of the
218 : queue was determined by the following: we can limit the size of this
219 : queue to be the maximum number of active keys - which is equal to the
220 : number of warm up requests we might queue. The queue will also hold
221 : pongs, but in order for the ping to arrive the warm up request must
222 : have left the queue. It is possible that we start up and get
223 : FD_ACTIVE_KEY_MAX peers gossiped to us, and as we are queueing up
224 : their pings they all drop and another FD_ACTIVE_KEY_MAX new peers
225 : gossip to us, causing us to fill up the queue. Idk overall this
226 : scenario is highly unlikely and it's not the end of the world if we
227 : drop a warmup req or ping to a peer because the first req to them
228 : will retrigger it anyway.
229 :
230 : Typical flow is that a pong will get added to the sign_queue during
231 : an after_frag call. Then on the following after_credit will get
232 : popped from the sign_queue and added to sign_map, and then dispatched
233 : to the sign tile. */
234 :
235 : struct sign_pending {
236 : fd_repair_msg_t msg;
237 : pong_data_t pong_data; /* populated only for pong msgs */
238 : };
239 : typedef struct sign_pending sign_pending_t;
240 :
241 : #define QUEUE_NAME fd_signs_queue
242 0 : #define QUEUE_T sign_pending_t
243 0 : #define QUEUE_MAX 2*FD_ACTIVE_KEY_MAX
244 : #include "../../util/tmpl/fd_queue.c"
245 :
246 : struct ctx {
247 : long tsdebug; /* timestamp for debug printing */
248 :
249 : ulong repair_seed;
250 :
251 : fd_ip4_port_t repair_intake_addr;
252 : fd_ip4_port_t repair_serve_addr;
253 :
254 : fd_forest_t * forest;
255 : fd_fec_sig_t * fec_sigs;
256 : fd_store_t * store;
257 : fd_policy_t * policy;
258 : fd_inflights_t * inflight;
259 : fd_repair_t * protocol;
260 :
261 : fd_pubkey_t identity_public_key;
262 :
263 : fd_wksp_t * wksp;
264 :
265 : fd_stem_context_t * stem;
266 :
267 : uchar in_kind[ MAX_IN_LINKS ];
268 : in_ctx_t in_links[ MAX_IN_LINKS ];
269 :
270 : int skip_frag;
271 :
272 : uint net_out_idx;
273 : fd_wksp_t * net_out_mem;
274 : ulong net_out_chunk0;
275 : ulong net_out_wmark;
276 : ulong net_out_chunk;
277 :
278 : ulong snap_out_chunk;
279 :
280 : uint shred_tile_cnt;
281 : out_ctx_t shred_out_ctx[ MAX_SHRED_TILE_CNT ];
282 :
283 : /* repair_sign links (to sign tiles 1+) - for round-robin distribution */
284 : ulong repair_sign_cnt;
285 : out_ctx_t repair_sign_out_ctx[ MAX_SIGN_TILE_CNT ];
286 :
287 : ulong sign_rrobin_idx;
288 :
289 : /* Pending sign requests for async operations */
290 : uint pending_key_next;
291 : sign_req_t * signs_map; /* contains any request currently in the repair->sign or sign->repair dcache */
292 : sign_pending_t * sign_queue; /* contains any request waiting to be dispatched to repair->sign */
293 :
294 : ushort net_id;
295 : /* Includes Ethernet, IP, UDP headers */
296 : uchar buffer[ MAX_BUFFER_SIZE ];
297 : fd_ip4_udp_hdrs_t intake_hdr[1];
298 : fd_ip4_udp_hdrs_t serve_hdr [1];
299 :
300 : ulong manifest_slot;
301 : struct {
302 : ulong send_pkt_cnt;
303 : ulong sent_pkt_types[FD_METRICS_ENUM_REPAIR_SENT_REQUEST_TYPES_CNT];
304 : ulong repaired_slots;
305 : ulong current_slot;
306 : ulong sign_tile_unavail;
307 : fd_histf_t slot_compl_time[ 1 ];
308 : fd_histf_t response_latency[ 1 ];
309 : } metrics[ 1 ];
310 :
311 : /* Slot-level metrics */
312 : fd_repair_metrics_t * slot_metrics;
313 :
314 : ulong turbine_slot0; // catchup considered complete after this slot
315 : };
316 : typedef struct ctx ctx_t;
317 :
318 : FD_FN_CONST static inline ulong
319 0 : scratch_align( void ) {
320 0 : return 128UL;
321 0 : }
322 :
323 : FD_FN_PURE static inline ulong
324 0 : loose_footprint( fd_topo_tile_t const * tile FD_PARAM_UNUSED ) {
325 0 : return 1UL * FD_SHMEM_GIGANTIC_PAGE_SZ;
326 0 : }
327 :
328 : FD_FN_PURE static inline ulong
329 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
330 0 : ulong total_sign_depth = tile->repair.repair_sign_depth * tile->repair.repair_sign_cnt;
331 0 : int lg_sign_depth = fd_ulong_find_msb( fd_ulong_pow2_up(total_sign_depth) ) + 1;
332 :
333 0 : ulong l = FD_LAYOUT_INIT;
334 0 : l = FD_LAYOUT_APPEND( l, alignof(ctx_t), sizeof(ctx_t) );
335 0 : l = FD_LAYOUT_APPEND( l, fd_repair_align(), fd_repair_footprint () );
336 0 : l = FD_LAYOUT_APPEND( l, fd_forest_align(), fd_forest_footprint ( tile->repair.slot_max ) );
337 0 : l = FD_LAYOUT_APPEND( l, fd_policy_align(), fd_policy_footprint ( FD_NEEDED_KEY_MAX, FD_ACTIVE_KEY_MAX ) );
338 0 : l = FD_LAYOUT_APPEND( l, fd_inflights_align(), fd_inflights_footprint () );
339 0 : l = FD_LAYOUT_APPEND( l, fd_fec_sig_align(), fd_fec_sig_footprint ( 20 ) );
340 0 : l = FD_LAYOUT_APPEND( l, fd_signs_map_align(), fd_signs_map_footprint ( lg_sign_depth ) );
341 0 : l = FD_LAYOUT_APPEND( l, fd_signs_queue_align(), fd_signs_queue_footprint() );
342 0 : l = FD_LAYOUT_APPEND( l, fd_repair_metrics_align(), fd_repair_metrics_footprint() );
343 0 : return FD_LAYOUT_FINI( l, scratch_align() );
344 0 : }
345 :
346 : /* Below functions manage the current pending sign requests. */
347 :
348 : sign_req_t *
349 : sign_map_insert( ctx_t * ctx,
350 : fd_repair_msg_t const * msg,
351 0 : pong_data_t const * opt_pong_data ) {
352 :
353 : /* Check if there is any space for a new pending sign request. Should never fail as long as credit management is working. */
354 0 : if( FD_UNLIKELY( fd_signs_map_key_cnt( ctx->signs_map )==fd_signs_map_key_max( ctx->signs_map ) ) ) return NULL;
355 :
356 0 : sign_req_t * pending = fd_signs_map_insert( ctx->signs_map, ctx->pending_key_next++ );
357 0 : if( FD_UNLIKELY( !pending ) ) return NULL; // Not possible, unless the same nonce is used twice.
358 0 : pending->msg = *msg;
359 0 : pending->buflen = fd_repair_sz( msg );
360 0 : if( FD_UNLIKELY( opt_pong_data ) ) pending->pong_data = *opt_pong_data;
361 0 : return pending;
362 0 : }
363 :
364 : int
365 : sign_map_remove( ctx_t * ctx,
366 0 : ulong key ) {
367 0 : sign_req_t * pending = fd_signs_map_query( ctx->signs_map, key, NULL );
368 0 : if( FD_UNLIKELY( !pending ) ) return -1;
369 0 : fd_signs_map_remove( ctx->signs_map, pending );
370 0 : return 0;
371 0 : }
372 :
373 : static void
374 : send_packet( ctx_t * ctx,
375 : fd_stem_context_t * stem,
376 : int is_intake,
377 : uint dst_ip_addr,
378 : ushort dst_port,
379 : uint src_ip_addr,
380 : uchar const * payload,
381 : ulong payload_sz,
382 0 : ulong tsorig ) {
383 0 : ctx->metrics->send_pkt_cnt++;
384 0 : uchar * packet = fd_chunk_to_laddr( ctx->net_out_mem, ctx->net_out_chunk );
385 0 : fd_ip4_udp_hdrs_t * hdr = (fd_ip4_udp_hdrs_t *)packet;
386 0 : *hdr = *(is_intake ? ctx->intake_hdr : ctx->serve_hdr);
387 :
388 0 : fd_ip4_hdr_t * ip4 = hdr->ip4;
389 0 : ip4->saddr = src_ip_addr;
390 0 : ip4->daddr = dst_ip_addr;
391 0 : ip4->net_id = fd_ushort_bswap( ctx->net_id++ );
392 0 : ip4->check = 0U;
393 0 : ip4->net_tot_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_ip4_hdr_t)+sizeof(fd_udp_hdr_t)) );
394 0 : ip4->check = fd_ip4_hdr_check_fast( ip4 );
395 :
396 0 : fd_udp_hdr_t * udp = hdr->udp;
397 0 : udp->net_dport = dst_port;
398 0 : udp->net_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_udp_hdr_t)) );
399 0 : fd_memcpy( packet+sizeof(fd_ip4_udp_hdrs_t), payload, payload_sz );
400 0 : hdr->udp->check = 0U;
401 :
402 0 : ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
403 0 : ulong sig = fd_disco_netmux_sig( dst_ip_addr, dst_port, dst_ip_addr, DST_PROTO_OUTGOING, sizeof(fd_ip4_udp_hdrs_t) );
404 0 : ulong packet_sz = payload_sz + sizeof(fd_ip4_udp_hdrs_t);
405 0 : ulong chunk = ctx->net_out_chunk;
406 0 : fd_stem_publish( stem, ctx->net_out_idx, sig, chunk, packet_sz, 0UL, tsorig, tspub );
407 0 : ctx->net_out_chunk = fd_dcache_compact_next( chunk, packet_sz, ctx->net_out_chunk0, ctx->net_out_wmark );
408 0 : }
409 :
410 : /* Returns a sign_out context with max available credits.
411 : If no sign_out context has available credits, returns NULL. */
412 : static out_ctx_t *
413 0 : sign_avail_credits( ctx_t * ctx ) {
414 0 : out_ctx_t * sign_out = NULL;
415 0 : ulong max_credits = 0;
416 0 : for( uint i = 0; i < ctx->repair_sign_cnt; i++ ) {
417 0 : if( ctx->repair_sign_out_ctx[i].credits > max_credits ) {
418 0 : max_credits = ctx->repair_sign_out_ctx[i].credits;
419 0 : sign_out = &ctx->repair_sign_out_ctx[i];
420 0 : }
421 0 : }
422 0 : return sign_out;
423 0 : }
424 :
425 : /* Prepares the signing preimage and publishes a signing request that
426 : will be signed asynchronously by the sign tile. The signed data will
427 : be returned via dcache as a frag. */
428 : static void
429 : fd_repair_send_sign_request( ctx_t * ctx,
430 : out_ctx_t * sign_out,
431 : fd_repair_msg_t const * msg,
432 0 : pong_data_t const * opt_pong_data ){
433 : /* New sign request */
434 0 : sign_req_t * pending = sign_map_insert( ctx, msg, opt_pong_data );
435 0 : if( FD_UNLIKELY( !pending ) ) return;
436 :
437 0 : ulong sig = 0;
438 0 : ulong preimage_sz = 0;
439 0 : uchar * dst = fd_chunk_to_laddr( sign_out->mem, sign_out->chunk );
440 :
441 0 : if( FD_UNLIKELY( msg->kind == FD_REPAIR_KIND_PONG ) ) {
442 0 : uchar pre_image[FD_REPAIR_PONG_PREIMAGE_SZ];
443 0 : preimage_pong( &opt_pong_data->hash, pre_image, sizeof(pre_image) );
444 0 : preimage_sz = FD_REPAIR_PONG_PREIMAGE_SZ;
445 0 : fd_memcpy( dst, pre_image, preimage_sz );
446 0 : sig = ((ulong)pending->key << 32) | (uint)FD_KEYGUARD_SIGN_TYPE_SHA256_ED25519;
447 0 : } else {
448 : /* Sign and prepare the message directly into the pending buffer */
449 0 : uchar * preimage = preimage_req( &pending->msg, &preimage_sz );
450 0 : fd_memcpy( dst, preimage, preimage_sz );
451 0 : sig = ((ulong)pending->key << 32) | (uint)FD_KEYGUARD_SIGN_TYPE_ED25519;
452 0 : }
453 :
454 0 : fd_stem_publish( ctx->stem, sign_out->idx, sig, sign_out->chunk, preimage_sz, 0UL, 0UL, 0UL );
455 0 : sign_out->chunk = fd_dcache_compact_next( sign_out->chunk, preimage_sz, sign_out->chunk0, sign_out->wmark );
456 :
457 0 : ctx->metrics->sent_pkt_types[metric_index[msg->kind]]++;
458 0 : sign_out->credits--;
459 0 : }
460 :
461 : static inline int
462 : before_frag( ctx_t * ctx,
463 : ulong in_idx,
464 : ulong seq FD_PARAM_UNUSED,
465 0 : ulong sig ) {
466 0 : uint in_kind = ctx->in_kind[ in_idx ];
467 0 : if( FD_LIKELY ( in_kind==IN_KIND_NET ) ) return fd_disco_netmux_sig_proto( sig )!=DST_PROTO_REPAIR;
468 0 : if( FD_UNLIKELY( in_kind==IN_KIND_SHRED ) ) return fd_int_if( fd_forest_root_slot( ctx->forest )==ULONG_MAX, -1, 0 ); /* not ready to read frag */
469 0 : if( FD_UNLIKELY( in_kind==IN_KIND_GOSSIP ) ) {
470 0 : return sig!=FD_GOSSIP_UPDATE_TAG_CONTACT_INFO &&
471 0 : sig!=FD_GOSSIP_UPDATE_TAG_CONTACT_INFO_REMOVE;
472 0 : }
473 0 : return 0;
474 0 : }
475 :
476 : static void
477 : during_frag( ctx_t * ctx,
478 : ulong in_idx,
479 : ulong seq FD_PARAM_UNUSED,
480 : ulong sig FD_PARAM_UNUSED,
481 : ulong chunk,
482 : ulong sz,
483 0 : ulong ctl ) {
484 0 : ctx->skip_frag = 0;
485 :
486 0 : uint in_kind = ctx->in_kind[ in_idx ];
487 0 : in_ctx_t const * in_ctx = &ctx->in_links[ in_idx ];
488 :
489 0 : if( FD_UNLIKELY( in_kind==IN_KIND_TOWER ) ) {
490 0 : if( FD_UNLIKELY( chunk<in_ctx->chunk0 || chunk>in_ctx->wmark || sz>in_ctx->mtu ) ) {
491 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, in_ctx->chunk0, in_ctx->wmark ));
492 0 : }
493 0 : uchar const * dcache_entry = fd_chunk_to_laddr_const( in_ctx->mem, chunk );
494 0 : fd_memcpy( ctx->buffer, dcache_entry, sz );
495 0 : return;
496 0 : }
497 :
498 0 : if( FD_UNLIKELY( in_kind==IN_KIND_GENESIS ) ) {
499 0 : return;
500 0 : }
501 0 : if( FD_UNLIKELY( in_kind==IN_KIND_NET ) ) {
502 0 : uchar const * dcache_entry = fd_net_rx_translate_frag( &in_ctx->net_rx, chunk, ctl, sz );
503 0 : fd_memcpy( ctx->buffer, dcache_entry, sz );
504 0 : return;
505 0 : }
506 :
507 0 : if( FD_UNLIKELY( in_kind==IN_KIND_GOSSIP ) ) {
508 0 : if( FD_UNLIKELY( chunk<in_ctx->chunk0 || chunk>in_ctx->wmark || sz>in_ctx->mtu ) ) {
509 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, in_ctx->chunk0, in_ctx->wmark ));
510 0 : }
511 0 : uchar const * dcache_entry = fd_chunk_to_laddr_const( in_ctx->mem, chunk );
512 0 : fd_memcpy( ctx->buffer, dcache_entry, sz );
513 0 : return;
514 0 : }
515 :
516 0 : if( FD_LIKELY ( in_kind==IN_KIND_SHRED ) ) {
517 0 : if( FD_UNLIKELY( chunk<in_ctx->chunk0 || chunk>in_ctx->wmark || sz>in_ctx->mtu ) ) {
518 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, in_ctx->chunk0, in_ctx->wmark ));
519 0 : }
520 0 : uchar const * dcache_entry = fd_chunk_to_laddr_const( in_ctx->mem, chunk );
521 0 : if( FD_LIKELY( sz > 0 ) ) fd_memcpy( ctx->buffer, dcache_entry, sz );
522 0 : return;
523 0 : }
524 :
525 0 : if( FD_UNLIKELY( in_kind==IN_KIND_STAKE ) ) {
526 0 : return;
527 0 : }
528 :
529 0 : if( FD_UNLIKELY( in_kind==IN_KIND_SNAP ) ) {
530 0 : if( FD_UNLIKELY( fd_ssmsg_sig_message( sig )!=FD_SSMSG_DONE ) ) ctx->snap_out_chunk = chunk;
531 0 : return;
532 0 : }
533 :
534 0 : if( FD_UNLIKELY( in_kind==IN_KIND_SIGN ) ) {
535 0 : if( FD_UNLIKELY( chunk<in_ctx->chunk0 || chunk>in_ctx->wmark || sz>in_ctx->mtu ) ) {
536 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, in_ctx->chunk0, in_ctx->wmark ));
537 0 : }
538 0 : uchar const * dcache_entry = fd_chunk_to_laddr_const( in_ctx->mem, chunk );
539 0 : fd_memcpy( ctx->buffer, dcache_entry, sz );
540 0 : return;
541 0 : }
542 :
543 0 : FD_LOG_ERR(( "Frag from unknown link (kind=%u in_idx=%lu)", in_kind, in_idx ));
544 0 : }
545 :
546 : static inline void
547 : after_snap( ctx_t * ctx,
548 : ulong sig,
549 0 : uchar const * chunk ) {
550 0 : if( FD_UNLIKELY( fd_ssmsg_sig_message( sig )!=FD_SSMSG_DONE ) ) return;
551 0 : fd_snapshot_manifest_t * manifest = (fd_snapshot_manifest_t *)chunk;
552 :
553 0 : fd_forest_init( ctx->forest, manifest->slot );
554 0 : FD_TEST( fd_forest_root_slot( ctx->forest )!=ULONG_MAX );
555 0 : }
556 :
557 : static inline void
558 0 : after_contact( ctx_t * ctx, fd_gossip_update_message_t const * msg ) {
559 0 : fd_contact_info_t const * contact_info = msg->contact_info.contact_info;
560 0 : fd_ip4_port_t repair_peer = contact_info->sockets[ FD_CONTACT_INFO_SOCKET_SERVE_REPAIR ];
561 0 : if( FD_UNLIKELY( !repair_peer.addr || !repair_peer.port ) ) return;
562 0 : fd_policy_peer_t const * peer = fd_policy_peer_insert( ctx->policy, &contact_info->pubkey, &repair_peer );
563 0 : if( peer ) {
564 : /* The repair process uses a Ping-Pong protocol that incurs one
565 : round-trip time (RTT) for the initial repair request. To
566 : optimize this, we proactively send a placeholder repair request
567 : as soon as we receive a peer's contact information for the first
568 : time, effectively prepaying the RTT cost. */
569 0 : fd_repair_msg_t * init = fd_repair_shred( ctx->protocol, &contact_info->pubkey, (ulong)fd_log_wallclock()/1000000L, 0, 0, 0 );
570 0 : fd_signs_queue_push( ctx->sign_queue, (sign_pending_t){ .msg = *init } );
571 0 : }
572 0 : }
573 :
574 : static inline void
575 : after_sign( ctx_t * ctx,
576 : ulong in_idx,
577 : ulong sig,
578 0 : fd_stem_context_t * stem ) {
579 0 : ulong pending_key = sig >> 32;
580 : /* Look up the pending request. Since the repair_sign links are
581 : reliable, the incoming sign_repair fragments represent a complete
582 : set of the previously sent outgoing messages. However, with
583 : multiple sign tiles, the responses may arrive interleaved. */
584 :
585 : /* Find which sign tile sent this response and increment its credits */
586 0 : for( uint i = 0; i < ctx->repair_sign_cnt; i++ ) {
587 0 : if( ctx->repair_sign_out_ctx[i].in_idx == in_idx ) {
588 0 : if( ctx->repair_sign_out_ctx[i].credits < ctx->repair_sign_out_ctx[i].max_credits ) {
589 0 : ctx->repair_sign_out_ctx[i].credits++;
590 0 : }
591 0 : break;
592 0 : }
593 0 : }
594 :
595 0 : sign_req_t * pending = fd_signs_map_query( ctx->signs_map, pending_key, NULL );
596 0 : if( FD_UNLIKELY( !pending ) ) FD_LOG_CRIT(( "No pending request found for key %lu", pending_key ));
597 :
598 0 : if( FD_UNLIKELY( pending->msg.kind == FD_REPAIR_KIND_PONG ) ) {
599 0 : fd_memcpy( pending->msg.pong.sig, ctx->buffer, 64UL );
600 0 : send_packet( ctx, stem, 1, pending->pong_data.peer_addr.addr, pending->pong_data.peer_addr.port, pending->pong_data.daddr, pending->buf, fd_repair_sz( &pending->msg ), fd_frag_meta_ts_comp( fd_tickcount() ) );
601 0 : sign_map_remove( ctx, pending_key );
602 0 : return;
603 0 : }
604 :
605 : /* else: regular repair shred request format */
606 :
607 0 : fd_memcpy( pending->buf + 4, ctx->buffer, 64UL );
608 0 : uint src_ip4 = 0U;
609 0 : fd_policy_peer_t * active = fd_policy_peer_query( ctx->policy, &pending->msg.shred.to );
610 :
611 0 : if( FD_UNLIKELY( !active ) ) {
612 0 : FD_LOG_INFO(( "Signed a message for %s, but it is no longer in the active peer list", FD_BASE58_ENC_32_ALLOCA( &pending->msg.shred.to ) ));
613 : /* Happens extremely rarely, so we can just pick a new peer and
614 : try to resign here. */
615 0 : fd_pubkey_t const * new_peer = fd_policy_peer_select( ctx->policy );
616 0 : pending->msg.shred.to = *new_peer;
617 0 : sign_map_remove( ctx, pending_key );
618 0 : fd_signs_queue_push( ctx->sign_queue, (sign_pending_t){ .msg = pending->msg } );
619 0 : return;
620 0 : }
621 :
622 0 : int is_regular_request = pending->msg.kind != FD_REPAIR_KIND_PONG && pending->msg.shred.nonce > 0;
623 0 : if( FD_LIKELY( is_regular_request ) ) {
624 0 : fd_inflights_request_insert( ctx->inflight, pending->msg.shred.nonce, &pending->msg.shred.to );
625 0 : fd_policy_peer_request_update( ctx->policy, &pending->msg.shred.to );
626 0 : }
627 0 : send_packet( ctx, stem, 1, active->ip4, active->port, src_ip4, pending->buf, pending->buflen, fd_frag_meta_ts_comp( fd_tickcount() ) );
628 0 : sign_map_remove( ctx, pending_key );
629 0 : }
630 :
631 : static inline void
632 : after_shred( ctx_t * ctx,
633 : ulong sig,
634 : fd_shred_t * shred,
635 0 : ulong nonce ) {
636 : /* Insert the shred sig (shared by all shred members in the FEC set)
637 : into the map. */
638 :
639 0 : int is_code = fd_shred_is_code( fd_shred_type( shred->variant ) );
640 0 : int src = fd_disco_shred_out_shred_sig_is_turbine( sig ) ? SHRED_SRC_TURBINE : SHRED_SRC_REPAIR;
641 0 : if( FD_LIKELY( !is_code ) ) {
642 0 : long rtt = 0;
643 0 : fd_pubkey_t peer;
644 0 : if( FD_UNLIKELY( ( rtt = fd_inflights_request_remove( ctx->inflight, nonce, &peer ) ) > 0 ) ) {
645 0 : fd_policy_peer_response_update( ctx->policy, &peer, rtt );
646 0 : fd_histf_sample( ctx->metrics->response_latency, (ulong)rtt );
647 0 : }
648 :
649 0 : int slot_complete = !!(shred->data.flags & FD_SHRED_DATA_FLAG_SLOT_COMPLETE);
650 0 : int ref_tick = shred->data.flags & FD_SHRED_DATA_REF_TICK_MASK;
651 0 : fd_forest_blk_insert( ctx->forest, shred->slot, shred->slot - shred->data.parent_off );
652 0 : fd_forest_data_shred_insert( ctx->forest, shred->slot, shred->slot - shred->data.parent_off, shred->idx, shred->fec_set_idx, slot_complete, ref_tick, src );
653 :
654 : /* Check if there are FECs to force complete. Algorithm: window
655 : through the idxs in interval [i, j). If j = next fec_set_idx
656 : then we know we can force complete the FEC set interval [i, j)
657 : (assuming it wasn't already completed based on `cmpl`). */
658 :
659 0 : } else {
660 0 : fd_forest_code_shred_insert( ctx->forest, shred->slot, shred->idx );
661 0 : }
662 0 : }
663 :
664 : static inline void
665 : after_fec( ctx_t * ctx,
666 0 : fd_shred_t * shred ) {
667 :
668 : /* When this is a FEC completes msg, it is implied that all the
669 : other shreds in the FEC set can also be inserted. Shred inserts
670 : into the forest are idempotent so it is fine to insert the same
671 : shred multiple times. */
672 :
673 0 : int slot_complete = !!( shred->data.flags & FD_SHRED_DATA_FLAG_SLOT_COMPLETE );
674 0 : int ref_tick = shred->data.flags & FD_SHRED_DATA_REF_TICK_MASK;
675 :
676 0 : fd_forest_blk_t * ele = fd_forest_blk_insert( ctx->forest, shred->slot, shred->slot - shred->data.parent_off );
677 0 : fd_forest_fec_insert( ctx->forest, shred->slot, shred->slot - shred->data.parent_off, shred->idx, shred->fec_set_idx, slot_complete, ref_tick );
678 0 : fd_fec_sig_t * fec_sig = fd_fec_sig_query( ctx->fec_sigs, (shred->slot << 32) | shred->fec_set_idx, NULL );
679 0 : if( FD_LIKELY( fec_sig ) ) fd_fec_sig_remove( ctx->fec_sigs, fec_sig );
680 0 : FD_TEST( ele ); /* must be non-empty */
681 :
682 : /* metrics for completed slots */
683 0 : if( FD_UNLIKELY( ele->complete_idx != UINT_MAX && ele->buffered_idx==ele->complete_idx &&
684 0 : 0==memcmp( ele->cmpl, ele->fecs, sizeof(fd_forest_blk_idxs_t) * fd_forest_blk_idxs_word_cnt ) ) ) {
685 0 : long now = fd_tickcount();
686 0 : long start_ts = ele->first_req_ts == 0 || ele->slot > ctx->turbine_slot0 ? ele->first_shred_ts : ele->first_req_ts;
687 0 : ulong duration_ticks = (ulong)(now - start_ts);
688 0 : fd_histf_sample( ctx->metrics->slot_compl_time, duration_ticks );
689 0 : fd_repair_metrics_add_slot( ctx->slot_metrics, ele->slot, start_ts, now, ele->repair_cnt, ele->turbine_cnt );
690 0 : FD_LOG_INFO(( "slot is complete %lu. num_data_shreds: %u, num_repaired: %u, num_turbine: %u, num_recovered: %u, duration: %.2f ms", ele->slot, ele->complete_idx + 1, ele->repair_cnt, ele->turbine_cnt, ele->recovered_cnt, (double)fd_metrics_convert_ticks_to_nanoseconds(duration_ticks) / 1e6 ));
691 0 : }
692 0 : }
693 :
694 : static inline void
695 : after_net( ctx_t * ctx,
696 0 : ulong sz ) {
697 0 : fd_eth_hdr_t const * eth = (fd_eth_hdr_t const *)ctx->buffer;
698 0 : fd_ip4_hdr_t const * ip4 = (fd_ip4_hdr_t const *)( (ulong)eth + sizeof(fd_eth_hdr_t) );
699 0 : fd_udp_hdr_t const * udp = (fd_udp_hdr_t const *)( (ulong)ip4 + FD_IP4_GET_LEN( *ip4 ) );
700 0 : uchar * data = (uchar *)( (ulong)udp + sizeof(fd_udp_hdr_t) );
701 0 : if( FD_UNLIKELY( (ulong)udp+sizeof(fd_udp_hdr_t) > (ulong)eth+sz ) ) return;
702 0 : ulong udp_sz = fd_ushort_bswap( udp->net_len );
703 0 : if( FD_UNLIKELY( udp_sz<sizeof(fd_udp_hdr_t) ) ) return;
704 0 : ulong data_sz = udp_sz-sizeof(fd_udp_hdr_t);
705 0 : if( FD_UNLIKELY( (ulong)data+data_sz > (ulong)eth+sz ) ) return;
706 :
707 0 : fd_ip4_port_t peer_addr = { .addr=ip4->saddr, .port=udp->net_sport };
708 0 : ushort dport = udp->net_dport;
709 0 : if( ctx->repair_intake_addr.port == dport ) {
710 0 : if( FD_UNLIKELY( data_sz < sizeof(fd_repair_ping_t) ) ) {
711 : /* TODO: increment a malformed repair ping counter? */
712 0 : return;
713 0 : }
714 0 : fd_repair_ping_t * res = (fd_repair_ping_t *)fd_type_pun( data );
715 0 : switch( res->kind ) {
716 0 : case FD_REPAIR_KIND_PING: {
717 0 : fd_repair_msg_t * pong = fd_repair_pong( ctx->protocol, &res->ping.hash );
718 0 : fd_signs_queue_push( ctx->sign_queue, (sign_pending_t){ .msg = *pong, .pong_data = { .peer_addr = peer_addr, .hash = res->ping.hash, .daddr = ip4->daddr } } );
719 0 : break;
720 0 : }
721 0 : default: FD_LOG_ERR(( "unhandled kind %u", (uint)res->kind ));
722 0 : }
723 0 : } else {
724 0 : FD_LOG_WARNING(( "Unexpectedly received packet for port %u", (uint)fd_ushort_bswap( dport ) ));
725 0 : }
726 0 : }
727 :
728 : static inline void
729 : after_evict( ctx_t * ctx,
730 0 : ulong sig ) {
731 0 : ulong spilled_slot = fd_disco_shred_out_shred_sig_slot ( sig );
732 0 : uint spilled_fec_set_idx = fd_disco_shred_out_shred_sig_fec_set_idx( sig );
733 0 : uint spilled_max_idx = fd_disco_shred_out_shred_sig_data_cnt ( sig );
734 :
735 0 : fd_forest_fec_clear( ctx->forest, spilled_slot, spilled_fec_set_idx, spilled_max_idx );
736 0 : }
737 :
738 : static void
739 : after_frag( ctx_t * ctx,
740 : ulong in_idx,
741 : ulong seq FD_PARAM_UNUSED,
742 : ulong sig,
743 : ulong sz,
744 : ulong tsorig FD_PARAM_UNUSED,
745 : ulong tspub FD_PARAM_UNUSED,
746 0 : fd_stem_context_t * stem ) {
747 0 : if( FD_UNLIKELY( ctx->skip_frag ) ) return;
748 :
749 0 : ctx->stem = stem;
750 :
751 0 : uint in_kind = ctx->in_kind[ in_idx ];
752 0 : if( FD_UNLIKELY( in_kind==IN_KIND_GENESIS ) ) {
753 0 : fd_forest_init( ctx->forest, 0 );
754 0 : fd_policy_reset( ctx->policy, ctx->forest );
755 0 : return;
756 0 : }
757 :
758 0 : if( FD_UNLIKELY( in_kind==IN_KIND_GOSSIP ) ) {
759 0 : fd_gossip_update_message_t const * msg = (fd_gossip_update_message_t const *)fd_type_pun_const( ctx->buffer );
760 0 : if( FD_LIKELY( sig==FD_GOSSIP_UPDATE_TAG_CONTACT_INFO ) ){
761 0 : after_contact( ctx, msg );
762 0 : } else {
763 0 : fd_policy_peer_remove( ctx->policy, &msg->contact_info.contact_info->pubkey );
764 0 : }
765 0 : return;
766 0 : }
767 :
768 0 : if( FD_UNLIKELY( in_kind==IN_KIND_TOWER ) ) {
769 0 : fd_tower_slot_done_t const * msg = (fd_tower_slot_done_t const *)fd_type_pun_const( ctx->buffer );
770 0 : if( FD_LIKELY( msg->new_root ) ) {
771 0 : fd_forest_publish( ctx->forest, msg->root_slot );
772 0 : fd_policy_reset ( ctx->policy, ctx->forest );
773 0 : }
774 0 : return;
775 0 : }
776 :
777 0 : if( FD_UNLIKELY( in_kind==IN_KIND_SIGN ) ) {
778 0 : after_sign( ctx, in_idx, sig, stem );
779 0 : return;
780 0 : }
781 :
782 0 : if( FD_UNLIKELY( in_kind==IN_KIND_SHRED ) ) {
783 : /* There are 3 message types from shred:
784 : 1. resolver evict - incomplete FEC set is evicted by resolver
785 : 2. fec complete - FEC set is completed by resolver. Also contains a shred.
786 : 3. shred - new shred
787 :
788 : Msgs 2 and 3 have a shred header in ctx->buffer */
789 0 : int resolver_evicted = sz == 0;
790 0 : int fec_completes = sz == FD_SHRED_DATA_HEADER_SZ + sizeof(fd_hash_t) + sizeof(fd_hash_t) + sizeof(int);
791 0 : if( FD_UNLIKELY( resolver_evicted ) ) {
792 0 : after_evict( ctx, sig );
793 0 : return;
794 0 : }
795 :
796 0 : fd_shred_t * shred = (fd_shred_t *)fd_type_pun( ctx->buffer );
797 0 : uint nonce = FD_LOAD(uint, ctx->buffer + fd_shred_header_sz( shred->variant ) );
798 0 : if( FD_UNLIKELY( shred->slot <= fd_forest_root_slot( ctx->forest ) ) ) {
799 0 : FD_LOG_INFO(( "shred %lu %u %u too old, ignoring", shred->slot, shred->idx, shred->fec_set_idx ));
800 0 : return;
801 0 : };
802 0 : # if LOGGING
803 0 : if( FD_UNLIKELY( shred->slot > ctx->metrics->current_slot ) ) {
804 0 : FD_LOG_INFO(( "\n\n[Turbine]\n"
805 0 : "slot: %lu\n"
806 0 : "root: %lu\n",
807 0 : shred->slot,
808 0 : fd_forest_root_slot( ctx->forest ) ));
809 0 : }
810 0 : # endif
811 0 : ctx->metrics->current_slot = fd_ulong_max( shred->slot, ctx->metrics->current_slot );
812 0 : if( FD_UNLIKELY( ctx->turbine_slot0 == ULONG_MAX ) ) {
813 0 : ctx->turbine_slot0 = shred->slot;
814 0 : fd_repair_metrics_set_turbine_slot0( ctx->slot_metrics, shred->slot );
815 0 : fd_policy_set_turbine_slot0( ctx->policy, shred->slot );
816 0 : }
817 :
818 0 : if( FD_UNLIKELY( fec_completes ) ) {
819 0 : after_fec( ctx, shred );
820 0 : } else {
821 : /* Don't want to reinsert the shred sig for an already complete FEC set */
822 0 : fd_fec_sig_t * fec_sig = fd_fec_sig_query( ctx->fec_sigs, (shred->slot << 32) | shred->fec_set_idx, NULL );
823 0 : if( FD_UNLIKELY( !fec_sig ) ) {
824 0 : fec_sig = fd_fec_sig_insert( ctx->fec_sigs, (shred->slot << 32) | shred->fec_set_idx );
825 0 : memcpy( fec_sig->sig, shred->signature, sizeof(fd_ed25519_sig_t) );
826 0 : }
827 0 : after_shred( ctx, sig, shred, nonce );
828 0 : }
829 :
830 : /* Check if there are FECs to force complete. Algorithm: window
831 : through the idxs in interval [i, j). If j = next fec_set_idx
832 : then we know we can force complete the FEC set interval [i, j)
833 : (assuming it wasn't already completed based on `cmpl`). */
834 :
835 0 : fd_forest_blk_t * blk = fd_forest_query( ctx->forest, shred->slot );
836 0 : if( blk ) {
837 0 : uint i = blk->consumed_idx + 1;
838 0 : for( uint j = i; j < blk->buffered_idx + 1; j++ ) {
839 0 : if( FD_UNLIKELY( fd_forest_blk_idxs_test( blk->fecs, j ) ) ) {
840 0 : if( FD_UNLIKELY( fd_forest_blk_idxs_test( blk->cmpl, j ) ) ) {
841 : /* already been completed without force complete */
842 0 : } else {
843 : /* force completeable */
844 0 : fd_fec_sig_t * fec_sig = fd_fec_sig_query( ctx->fec_sigs, (shred->slot << 32) | i, NULL );
845 0 : if( FD_LIKELY( fec_sig ) ) {
846 0 : ulong sig = fd_ulong_load_8( fec_sig->sig );
847 0 : ulong tile_idx = sig % ctx->shred_tile_cnt;
848 0 : uint last_idx = j - i;
849 :
850 0 : uchar * chunk = fd_chunk_to_laddr( ctx->shred_out_ctx[tile_idx].mem, ctx->shred_out_ctx[tile_idx].chunk );
851 0 : memcpy( chunk, fec_sig->sig, sizeof(fd_ed25519_sig_t) );
852 0 : fd_fec_sig_remove( ctx->fec_sigs, fec_sig );
853 0 : fd_stem_publish( stem, ctx->shred_out_ctx[tile_idx].idx, last_idx, ctx->shred_out_ctx[tile_idx].chunk, sizeof(fd_ed25519_sig_t), 0UL, 0UL, 0UL );
854 0 : ctx->shred_out_ctx[tile_idx].chunk = fd_dcache_compact_next( ctx->shred_out_ctx[tile_idx].chunk, sizeof(fd_ed25519_sig_t), ctx->shred_out_ctx[tile_idx].chunk0, ctx->shred_out_ctx[tile_idx].wmark );
855 0 : }
856 0 : }
857 : /* advance consumed */
858 0 : blk->consumed_idx = j;
859 0 : i = j + 1;
860 0 : }
861 0 : }
862 0 : }
863 :
864 0 : ulong max_repaired_slot = 0;
865 0 : fd_forest_conslist_t const * conslist = fd_forest_conslist_const( ctx->forest );
866 0 : fd_forest_cns_t const * conspool = fd_forest_conspool_const( ctx->forest );
867 0 : fd_forest_blk_t const * pool = fd_forest_pool_const( ctx->forest );
868 0 : for( fd_forest_conslist_iter_t iter = fd_forest_conslist_iter_fwd_init( conslist, conspool );
869 0 : !fd_forest_conslist_iter_done( iter, conslist, conspool );
870 0 : iter = fd_forest_conslist_iter_fwd_next( iter, conslist, conspool ) ) {
871 0 : fd_forest_cns_t const * ele = fd_forest_conslist_iter_ele_const( iter, conslist, conspool );
872 0 : fd_forest_blk_t const * ele_ = fd_forest_pool_ele_const( pool, ele->forest_pool_idx );
873 0 : if( ele_->slot > max_repaired_slot ) max_repaired_slot = ele_->slot;
874 0 : }
875 0 : ctx->metrics->repaired_slots = max_repaired_slot;
876 0 : return;
877 0 : }
878 :
879 0 : if( FD_UNLIKELY( in_kind==IN_KIND_STAKE ) ) {
880 0 : return;
881 0 : }
882 :
883 0 : if( FD_UNLIKELY( in_kind==IN_KIND_SNAP ) ) {
884 0 : after_snap( ctx, sig, fd_chunk_to_laddr( ctx->in_links[ in_idx ].mem, ctx->snap_out_chunk ) );
885 0 : return;
886 0 : }
887 :
888 0 : if( FD_UNLIKELY( in_kind==IN_KIND_NET ) ) {
889 0 : after_net( ctx, sz );
890 0 : return;
891 0 : }
892 :
893 0 : }
894 :
895 : static inline void
896 : after_credit( ctx_t * ctx,
897 : fd_stem_context_t * stem FD_PARAM_UNUSED,
898 : int * opt_poll_in FD_PARAM_UNUSED,
899 0 : int * charge_busy ) {
900 0 : long now = fd_log_wallclock();
901 :
902 0 : *charge_busy = 1;
903 :
904 : /* Verify that there is at least one sign tile with available credits.
905 : If not, we can't send any requests and leave early. */
906 0 : out_ctx_t * sign_out = sign_avail_credits( ctx );
907 0 : if( FD_UNLIKELY( !sign_out ) ) {
908 0 : ctx->metrics->sign_tile_unavail++;
909 0 : return;
910 0 : }
911 0 : if( FD_UNLIKELY( !fd_signs_queue_empty( ctx->sign_queue ) ) ) {
912 0 : sign_pending_t signable = fd_signs_queue_pop( ctx->sign_queue );
913 0 : fd_repair_send_sign_request( ctx, sign_out, &signable.msg, signable.msg.kind == FD_REPAIR_KIND_PONG ? &signable.pong_data : NULL );
914 0 : return;
915 0 : }
916 :
917 0 : fd_repair_msg_t const * cout = fd_policy_next( ctx->policy, ctx->forest, ctx->protocol, now, ctx->metrics->current_slot );
918 0 : if( FD_UNLIKELY( !cout ) ) return;
919 :
920 0 : fd_repair_send_sign_request( ctx, sign_out, cout, NULL );
921 0 : }
922 :
923 : static inline void
924 0 : during_housekeeping( ctx_t * ctx ) {
925 0 : (void)ctx;
926 : # if DEBUG_LOGGING
927 : long now = fd_log_wallclock();
928 : if( FD_UNLIKELY( now - ctx->tsdebug > (long)10e9 ) ) {
929 : fd_forest_print( ctx->forest );
930 : ctx->tsdebug = fd_log_wallclock();
931 : }
932 : # endif
933 0 : }
934 :
935 : static void
936 : privileged_init( fd_topo_t * topo,
937 0 : fd_topo_tile_t * tile ) {
938 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
939 :
940 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
941 0 : ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(ctx_t), sizeof(ctx_t) );
942 0 : fd_memset( ctx, 0, sizeof(ctx_t) );
943 :
944 0 : uchar const * identity_key = fd_keyload_load( tile->repair.identity_key_path, /* pubkey only: */ 0 );
945 0 : fd_memcpy( ctx->identity_public_key.uc, identity_key + 32UL, sizeof(fd_pubkey_t) );
946 :
947 0 : FD_TEST( fd_rng_secure( &ctx->repair_seed, sizeof(ulong) ) );
948 0 : }
949 :
950 : static void
951 : unprivileged_init( fd_topo_t * topo,
952 0 : fd_topo_tile_t * tile ) {
953 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
954 :
955 0 : ulong total_sign_depth = tile->repair.repair_sign_depth * tile->repair.repair_sign_cnt;
956 0 : int lg_sign_depth = fd_ulong_find_msb( fd_ulong_pow2_up(total_sign_depth) ) + 1;
957 :
958 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
959 0 : ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(ctx_t), sizeof(ctx_t) );
960 0 : ctx->protocol = FD_SCRATCH_ALLOC_APPEND( l, fd_repair_align(), fd_repair_footprint () );
961 0 : ctx->forest = FD_SCRATCH_ALLOC_APPEND( l, fd_forest_align(), fd_forest_footprint ( tile->repair.slot_max ) );
962 0 : ctx->policy = FD_SCRATCH_ALLOC_APPEND( l, fd_policy_align(), fd_policy_footprint ( FD_NEEDED_KEY_MAX, FD_ACTIVE_KEY_MAX ) );
963 0 : ctx->inflight = FD_SCRATCH_ALLOC_APPEND( l, fd_inflights_align(), fd_inflights_footprint () );
964 0 : ctx->fec_sigs = FD_SCRATCH_ALLOC_APPEND( l, fd_fec_sig_align(), fd_fec_sig_footprint ( 20 ) );
965 0 : ctx->signs_map = FD_SCRATCH_ALLOC_APPEND( l, fd_signs_map_align(), fd_signs_map_footprint ( lg_sign_depth ) );
966 0 : ctx->sign_queue = FD_SCRATCH_ALLOC_APPEND( l, fd_signs_queue_align(), fd_signs_queue_footprint() );
967 0 : ctx->slot_metrics = FD_SCRATCH_ALLOC_APPEND( l, fd_repair_metrics_align(), fd_repair_metrics_footprint() );
968 0 : FD_TEST( FD_SCRATCH_ALLOC_FINI( l, scratch_align() ) == (ulong)scratch + scratch_footprint( tile ) );
969 :
970 0 : ctx->protocol = fd_repair_join ( fd_repair_new ( ctx->protocol, &ctx->identity_public_key ) );
971 0 : ctx->forest = fd_forest_join ( fd_forest_new ( ctx->forest, tile->repair.slot_max, ctx->repair_seed ) );
972 0 : ctx->policy = fd_policy_join ( fd_policy_new ( ctx->policy, FD_NEEDED_KEY_MAX, FD_ACTIVE_KEY_MAX, ctx->repair_seed ) );
973 0 : ctx->inflight = fd_inflights_join ( fd_inflights_new ( ctx->inflight ) );
974 0 : ctx->fec_sigs = fd_fec_sig_join ( fd_fec_sig_new ( ctx->fec_sigs, 20 ) );
975 0 : ctx->signs_map = fd_signs_map_join ( fd_signs_map_new ( ctx->signs_map, lg_sign_depth ) );
976 0 : ctx->sign_queue = fd_signs_queue_join ( fd_signs_queue_new ( ctx->sign_queue ) );
977 0 : ctx->slot_metrics = fd_repair_metrics_join( fd_repair_metrics_new( ctx->slot_metrics ) );
978 :
979 : /* Process in links */
980 :
981 0 : if( FD_UNLIKELY( tile->in_cnt > MAX_IN_LINKS ) ) FD_LOG_ERR(( "repair tile has too many input links" ));
982 :
983 0 : uint sign_repair_in_idx[ MAX_SIGN_TILE_CNT ] = {0};
984 0 : uint sign_repair_idx = 0;
985 0 : ulong sign_link_depth = 0;
986 :
987 0 : for( uint in_idx=0U; in_idx<(tile->in_cnt); in_idx++ ) {
988 0 : fd_topo_link_t * link = &topo->links[ tile->in_link_id[ in_idx ] ];
989 0 : if( 0==strcmp( link->name, "net_repair" ) ) {
990 0 : ctx->in_kind[ in_idx ] = IN_KIND_NET;
991 0 : fd_net_rx_bounds_init( &ctx->in_links[ in_idx ].net_rx, link->dcache );
992 0 : continue;
993 0 : } else if( 0==strcmp( link->name, "sign_repair" ) ) {
994 0 : ctx->in_kind[ in_idx ] = IN_KIND_SIGN;
995 0 : sign_repair_in_idx[ sign_repair_idx++ ] = in_idx;
996 0 : sign_link_depth = link->depth;
997 0 : }
998 0 : else if( 0==strcmp( link->name, "gossip_out" ) ) ctx->in_kind[ in_idx ] = IN_KIND_GOSSIP;
999 0 : else if( 0==strcmp( link->name, "tower_out" ) ) ctx->in_kind[ in_idx ] = IN_KIND_TOWER;
1000 0 : else if( 0==strcmp( link->name, "shred_out" ) ) ctx->in_kind[ in_idx ] = IN_KIND_SHRED;
1001 0 : else if( 0==strcmp( link->name, "snap_out" ) ) ctx->in_kind[ in_idx ] = IN_KIND_SNAP;
1002 0 : else if( 0==strcmp( link->name, "replay_stake" ) ) ctx->in_kind[ in_idx ] = IN_KIND_STAKE;
1003 0 : else if( 0==strcmp( link->name, "genesi_out" ) ) ctx->in_kind[ in_idx ] = IN_KIND_GENESIS;
1004 0 : else FD_LOG_ERR(( "repair tile has unexpected input link %s", link->name ));
1005 :
1006 0 : ctx->in_links[ in_idx ].mem = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
1007 0 : ctx->in_links[ in_idx ].chunk0 = fd_dcache_compact_chunk0( ctx->in_links[ in_idx ].mem, link->dcache );
1008 0 : ctx->in_links[ in_idx ].wmark = fd_dcache_compact_wmark ( ctx->in_links[ in_idx ].mem, link->dcache, link->mtu );
1009 0 : ctx->in_links[ in_idx ].mtu = link->mtu;
1010 :
1011 0 : FD_TEST( fd_dcache_compact_is_safe( ctx->in_links[in_idx].mem, link->dcache, link->mtu, link->depth ) );
1012 0 : }
1013 :
1014 0 : ctx->net_out_idx = UINT_MAX;
1015 0 : ctx->shred_tile_cnt = 0;
1016 0 : ctx->repair_sign_cnt = 0;
1017 0 : ctx->sign_rrobin_idx = 0;
1018 :
1019 0 : for( uint out_idx=0U; out_idx<(tile->out_cnt); out_idx++ ) {
1020 0 : fd_topo_link_t * link = &topo->links[ tile->out_link_id[ out_idx ] ];
1021 :
1022 0 : if( 0==strcmp( link->name, "repair_net" ) ) {
1023 :
1024 0 : if( ctx->net_out_idx!=UINT_MAX ) continue; /* only use first net link */
1025 0 : ctx->net_out_idx = out_idx;
1026 0 : ctx->net_out_mem = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
1027 0 : ctx->net_out_chunk0 = fd_dcache_compact_chunk0( ctx->net_out_mem, link->dcache );
1028 0 : ctx->net_out_wmark = fd_dcache_compact_wmark( ctx->net_out_mem, link->dcache, link->mtu );
1029 0 : ctx->net_out_chunk = ctx->net_out_chunk0;
1030 :
1031 0 : } else if( 0==strcmp( link->name, "repair_shred" ) ) {
1032 :
1033 0 : out_ctx_t * shred_out = &ctx->shred_out_ctx[ ctx->shred_tile_cnt++ ];
1034 0 : shred_out->idx = out_idx;
1035 0 : shred_out->mem = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
1036 0 : shred_out->chunk0 = fd_dcache_compact_chunk0( shred_out->mem, link->dcache );
1037 0 : shred_out->wmark = fd_dcache_compact_wmark( shred_out->mem, link->dcache, link->mtu );
1038 0 : shred_out->chunk = shred_out->chunk0;
1039 :
1040 0 : } else if( 0==strcmp( link->name, "repair_sign" ) ) {
1041 :
1042 0 : out_ctx_t * repair_sign_out = &ctx->repair_sign_out_ctx[ ctx->repair_sign_cnt ];
1043 0 : repair_sign_out->idx = out_idx;
1044 0 : repair_sign_out->mem = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
1045 0 : repair_sign_out->chunk0 = fd_dcache_compact_chunk0( repair_sign_out->mem, link->dcache );
1046 0 : repair_sign_out->wmark = fd_dcache_compact_wmark( repair_sign_out->mem, link->dcache, link->mtu );
1047 0 : repair_sign_out->chunk = repair_sign_out->chunk0;
1048 0 : repair_sign_out->in_idx = sign_repair_in_idx[ ctx->repair_sign_cnt++ ]; /* match to the sign_repair input link */
1049 0 : repair_sign_out->max_credits = sign_link_depth;
1050 0 : repair_sign_out->credits = sign_link_depth;
1051 :
1052 0 : } else {
1053 0 : FD_LOG_ERR(( "repair tile has unexpected output link %s", link->name ));
1054 0 : }
1055 0 : }
1056 0 : if( FD_UNLIKELY( ctx->net_out_idx==UINT_MAX ) ) FD_LOG_ERR(( "Missing repair_net link" ));
1057 0 : if( FD_UNLIKELY( ctx->repair_sign_cnt!=sign_repair_idx ) ) {
1058 0 : FD_LOG_ERR(( "Mismatch between repair_sign output links (%lu) and sign_repair input links (%u)", ctx->repair_sign_cnt, sign_repair_idx ));
1059 0 : }
1060 :
1061 0 : FD_TEST( ctx->shred_tile_cnt == fd_topo_tile_name_cnt( topo, "shred" ) );
1062 :
1063 : # if DEBUG_LOGGING
1064 : if( fd_signs_map_key_max( ctx->signs_map ) < tile->repair.repair_sign_depth * tile->repair.repair_sign_cnt ) {
1065 : FD_LOG_ERR(( "repair pending signs tracking map is too small: %lu < %lu. Increase the key_max", fd_signs_map_key_max( ctx->signs_map ), tile->repair.repair_sign_depth * tile->repair.repair_sign_cnt ));
1066 : }
1067 : # endif
1068 :
1069 0 : ctx->store = NULL;
1070 0 : ulong store_obj_id = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "store" );
1071 0 : if( FD_LIKELY( store_obj_id!=ULONG_MAX ) ) { /* firedancer-only */
1072 0 : ctx->store = fd_store_join( fd_topo_obj_laddr( topo, store_obj_id ) );
1073 0 : FD_TEST( ctx->store->magic == FD_STORE_MAGIC );
1074 0 : }
1075 :
1076 0 : ctx->wksp = topo->workspaces[ topo->objs[ tile->tile_obj_id ].wksp_id ].wksp;
1077 0 : ctx->repair_intake_addr.port = fd_ushort_bswap( tile->repair.repair_intake_listen_port );
1078 0 : ctx->repair_serve_addr.port = fd_ushort_bswap( tile->repair.repair_serve_listen_port );
1079 :
1080 0 : ctx->net_id = (ushort)0;
1081 0 : fd_ip4_udp_hdr_init( ctx->intake_hdr, FD_REPAIR_MAX_PACKET_SIZE, 0, tile->repair.repair_intake_listen_port );
1082 0 : fd_ip4_udp_hdr_init( ctx->serve_hdr, FD_REPAIR_MAX_PACKET_SIZE, 0, tile->repair.repair_serve_listen_port );
1083 :
1084 : /* Repair set up */
1085 :
1086 0 : ctx->turbine_slot0 = ULONG_MAX;
1087 0 : FD_LOG_INFO(( "repair my addr - intake addr: " FD_IP4_ADDR_FMT ":%u, serve_addr: " FD_IP4_ADDR_FMT ":%u",
1088 0 : FD_IP4_ADDR_FMT_ARGS( ctx->repair_intake_addr.addr ), fd_ushort_bswap( ctx->repair_intake_addr.port ),
1089 0 : FD_IP4_ADDR_FMT_ARGS( ctx->repair_serve_addr.addr ), fd_ushort_bswap( ctx->repair_serve_addr.port ) ));
1090 :
1091 0 : memset( ctx->metrics, 0, sizeof(ctx->metrics) );
1092 :
1093 0 : fd_histf_join( fd_histf_new( ctx->metrics->slot_compl_time, FD_MHIST_SECONDS_MIN( REPAIR, SLOT_COMPLETE_TIME ),
1094 0 : FD_MHIST_SECONDS_MAX( REPAIR, SLOT_COMPLETE_TIME ) ) );
1095 0 : fd_histf_join( fd_histf_new( ctx->metrics->response_latency, FD_MHIST_MIN( REPAIR, RESPONSE_LATENCY ),
1096 0 : FD_MHIST_MAX( REPAIR, RESPONSE_LATENCY ) ) );
1097 :
1098 0 : ctx->tsdebug = fd_log_wallclock();
1099 0 : ctx->pending_key_next = 0;
1100 0 : }
1101 :
1102 : static ulong
1103 : populate_allowed_seccomp( fd_topo_t const * topo FD_PARAM_UNUSED,
1104 : fd_topo_tile_t const * tile FD_PARAM_UNUSED,
1105 : ulong out_cnt,
1106 0 : struct sock_filter * out ) {
1107 0 : populate_sock_filter_policy_fd_repair_tile(
1108 0 : out_cnt, out, (uint)fd_log_private_logfile_fd(), (uint)-1 );
1109 0 : return sock_filter_policy_fd_repair_tile_instr_cnt;
1110 0 : }
1111 :
1112 : static ulong
1113 : populate_allowed_fds( fd_topo_t const * topo FD_PARAM_UNUSED,
1114 : fd_topo_tile_t const * tile FD_PARAM_UNUSED,
1115 : ulong out_fds_cnt,
1116 0 : int * out_fds ) {
1117 0 : if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
1118 :
1119 0 : ulong out_cnt = 0UL;
1120 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
1121 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
1122 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
1123 0 : return out_cnt;
1124 0 : }
1125 :
1126 : static inline void
1127 0 : metrics_write( ctx_t * ctx ) {
1128 0 : FD_MCNT_SET( REPAIR, CURRENT_SLOT, ctx->metrics->current_slot );
1129 0 : FD_MCNT_SET( REPAIR, REPAIRED_SLOTS, ctx->metrics->repaired_slots );
1130 0 : FD_MCNT_SET( REPAIR, REQUEST_PEERS, fd_peer_pool_used( ctx->policy->peers.pool ) );
1131 0 : FD_MCNT_SET( REPAIR, SIGN_TILE_UNAVAIL, ctx->metrics->sign_tile_unavail );
1132 :
1133 0 : FD_MCNT_SET ( REPAIR, TOTAL_PKT_COUNT, ctx->metrics->send_pkt_cnt );
1134 0 : FD_MCNT_ENUM_COPY( REPAIR, SENT_PKT_TYPES, ctx->metrics->sent_pkt_types );
1135 :
1136 0 : FD_MHIST_COPY( REPAIR, SLOT_COMPLETE_TIME, ctx->metrics->slot_compl_time );
1137 0 : FD_MHIST_COPY( REPAIR, RESPONSE_LATENCY, ctx->metrics->response_latency );
1138 0 : }
1139 :
1140 : #undef DEBUG_LOGGING
1141 :
1142 : /* TODO: This is not correct, but is temporary and will be fixed
1143 : when fixed FEC 32 goes in, and we can finally get rid of force
1144 : completes BS. */
1145 0 : #define STEM_BURST (64UL)
1146 :
1147 0 : #define STEM_CALLBACK_CONTEXT_TYPE ctx_t
1148 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(ctx_t)
1149 :
1150 0 : #define STEM_CALLBACK_AFTER_CREDIT after_credit
1151 0 : #define STEM_CALLBACK_BEFORE_FRAG before_frag
1152 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
1153 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
1154 0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
1155 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
1156 :
1157 : #include "../../disco/stem/fd_stem.c"
1158 :
1159 : fd_topo_run_tile_t fd_tile_repair = {
1160 : .name = "repair",
1161 : .loose_footprint = loose_footprint,
1162 : .populate_allowed_seccomp = populate_allowed_seccomp,
1163 : .populate_allowed_fds = populate_allowed_fds,
1164 : .scratch_align = scratch_align,
1165 : .scratch_footprint = scratch_footprint,
1166 : .unprivileged_init = unprivileged_init,
1167 : .privileged_init = privileged_init,
1168 : .run = stem_run,
1169 : };
|