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