Line data Source code
1 : #include "fd_gossip_tile.h"
2 : #include "../../disco/metrics/fd_metrics.h"
3 : #include "generated/fd_gossip_tile_seccomp.h"
4 :
5 : #include "../../flamenco/gossip/crds/fd_crds.h"
6 : #include "../../flamenco/gossip/fd_gossip_out.h"
7 : #include "../../flamenco/features/fd_features.h"
8 : #include "../../disco/keyguard/fd_keyload.h"
9 : #include "../../disco/shred/fd_stake_ci.h"
10 : #include "../../disco/fd_txn_m.h"
11 :
12 0 : #define IN_KIND_GOSSVF (0)
13 0 : #define IN_KIND_SHRED_VERSION (1)
14 0 : #define IN_KIND_SIGN (2)
15 0 : #define IN_KIND_SEND (3)
16 0 : #define IN_KIND_STAKE (4)
17 :
18 : /* Symbols exported by version.c */
19 : extern ulong const firedancer_major_version;
20 : extern ulong const firedancer_minor_version;
21 : extern ulong const firedancer_patch_version;
22 : extern uint const firedancer_commit_ref;
23 :
24 : FD_FN_CONST static inline ulong
25 0 : scratch_align( void ) {
26 0 : return 128UL;
27 0 : }
28 :
29 : FD_FN_PURE static inline ulong
30 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
31 0 : ulong l = FD_LAYOUT_INIT;
32 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_gossip_tile_ctx_t), sizeof(fd_gossip_tile_ctx_t) );
33 0 : l = FD_LAYOUT_APPEND( l, fd_gossip_align(), fd_gossip_footprint( tile->gossip.max_entries, tile->gossip.entrypoints_cnt ) );
34 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_stake_weight_t), MAX_STAKED_LEADERS*sizeof(fd_stake_weight_t) );
35 0 : return FD_LAYOUT_FINI( l, scratch_align() );
36 0 : }
37 :
38 : static void
39 : gossip_send_fn( void * ctx,
40 : fd_stem_context_t * stem,
41 : uchar const * payload,
42 : ulong payload_sz,
43 : fd_ip4_port_t const * peer_address,
44 0 : ulong tsorig ) {
45 0 : fd_gossip_tile_ctx_t * gossip_ctx = (fd_gossip_tile_ctx_t *)ctx;
46 :
47 0 : uchar * packet = (uchar *)fd_chunk_to_laddr( gossip_ctx->net_out->mem, gossip_ctx->net_out->chunk );
48 0 : fd_ip4_udp_hdrs_t * hdr = (fd_ip4_udp_hdrs_t *)packet;
49 0 : *hdr = *gossip_ctx->net_out_hdr;
50 :
51 0 : fd_ip4_hdr_t * ip4 = hdr->ip4;
52 0 : fd_udp_hdr_t * udp = hdr->udp;
53 :
54 0 : ip4->net_tot_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_udp_hdr_t) + sizeof(fd_ip4_hdr_t)) );
55 0 : udp->net_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_udp_hdr_t)) );
56 0 : ip4->daddr = peer_address->addr;
57 0 : udp->net_dport = peer_address->port;
58 0 : ip4->net_id = fd_ushort_bswap( gossip_ctx->net_id++ );
59 0 : ip4->check = fd_ip4_hdr_check_fast( ip4 );
60 0 : udp->check = 0;
61 :
62 : /* TODO: Construct payload in place to avoid memcpy here. */
63 0 : fd_memcpy( packet+sizeof(fd_ip4_udp_hdrs_t), payload, payload_sz );
64 :
65 0 : ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
66 0 : ulong sig = fd_disco_netmux_sig( peer_address->addr, peer_address->port, peer_address->addr, DST_PROTO_OUTGOING, sizeof(fd_ip4_udp_hdrs_t) );
67 0 : ulong packet_sz = payload_sz + sizeof(fd_ip4_udp_hdrs_t);
68 :
69 0 : fd_stem_publish( stem, gossip_ctx->net_out->idx, sig, gossip_ctx->net_out->chunk, packet_sz, 0UL, tspub, tsorig );
70 0 : gossip_ctx->net_out->chunk = fd_dcache_compact_next( gossip_ctx->net_out->chunk, packet_sz, gossip_ctx->net_out->chunk0, gossip_ctx->net_out->wmark );
71 0 : }
72 :
73 : static void
74 : gossip_sign_fn( void * ctx,
75 : uchar const * data,
76 : ulong data_sz,
77 : int sign_type,
78 0 : uchar * out_signature ) {
79 0 : fd_gossip_tile_ctx_t * gossip_ctx = (fd_gossip_tile_ctx_t *)ctx;
80 0 : fd_keyguard_client_sign( gossip_ctx->keyguard_client, out_signature, data, data_sz, sign_type );
81 0 : }
82 :
83 : static void
84 : gossip_ping_tracker_change_fn( void * _ctx,
85 : uchar const * peer_pubkey,
86 : fd_ip4_port_t peer_address,
87 : long now,
88 0 : int change_type ) {
89 0 : (void)now;
90 :
91 0 : fd_gossip_tile_ctx_t * ctx = (fd_gossip_tile_ctx_t *)_ctx;
92 :
93 0 : fd_gossip_ping_update_t * ping_update = (fd_gossip_ping_update_t *)fd_chunk_to_laddr( ctx->gossvf_out->mem, ctx->gossvf_out->chunk );
94 0 : fd_memcpy( ping_update->pubkey.uc, peer_pubkey, 32UL );
95 0 : ping_update->gossip_addr.l = peer_address.l;
96 0 : ping_update->remove = change_type!=FD_PING_TRACKER_CHANGE_TYPE_ACTIVE;
97 :
98 0 : fd_stem_publish( ctx->stem, ctx->gossvf_out->idx, 0UL, ctx->gossvf_out->chunk, sizeof(fd_gossip_ping_update_t), 0UL, 0UL, 0UL );
99 0 : ctx->gossvf_out->chunk = fd_dcache_compact_next( ctx->gossvf_out->chunk, sizeof(fd_gossip_ping_update_t), ctx->gossvf_out->chunk0, ctx->gossvf_out->wmark );
100 0 : }
101 :
102 : static inline void
103 0 : during_housekeeping( fd_gossip_tile_ctx_t * ctx ) {
104 0 : ctx->last_wallclock = fd_log_wallclock();
105 0 : ctx->last_tickcount = fd_tickcount();
106 0 : if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
107 : /* TODO: Need some kind of state machine here, to ensure we switch
108 : in sync with the signing tile. Currently, we might send out a
109 : badly signed message before the signing tile has switched. */
110 0 : fd_memcpy( ctx->my_contact_info->pubkey.uc, ctx->keyswitch->bytes, 32UL );
111 0 : fd_gossip_set_my_contact_info( ctx->gossip, ctx->my_contact_info, ctx->last_wallclock );
112 :
113 0 : fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
114 0 : }
115 0 : }
116 :
117 : static inline void
118 0 : metrics_write( fd_gossip_tile_ctx_t * ctx ) {
119 0 : fd_ping_tracker_metrics_t const * ping_tracker_metrics = fd_gossip_ping_tracker_metrics( ctx->gossip );
120 :
121 0 : FD_MGAUGE_SET( GOSSIP, PING_TRACKER_COUNT_UNPINGED, ping_tracker_metrics->unpinged_cnt );
122 0 : FD_MGAUGE_SET( GOSSIP, PING_TRACKER_COUNT_INVALID, ping_tracker_metrics->invalid_cnt );
123 0 : FD_MGAUGE_SET( GOSSIP, PING_TRACKER_COUNT_VALID, ping_tracker_metrics->valid_cnt );
124 0 : FD_MGAUGE_SET( GOSSIP, PING_TRACKER_COUNT_VALID_REFRESHING, ping_tracker_metrics->refreshing_cnt );
125 :
126 0 : FD_MCNT_SET( GOSSIP, PING_TRACKER_PONG_RESULT_STAKED, ping_tracker_metrics->pong_result[ 0UL ] );
127 0 : FD_MCNT_SET( GOSSIP, PING_TRACKER_PONG_RESULT_ENTRYPOINT, ping_tracker_metrics->pong_result[ 1UL ] );
128 0 : FD_MCNT_SET( GOSSIP, PING_TRACKER_PONG_RESULT_UNTRACKED, ping_tracker_metrics->pong_result[ 2UL ] );
129 0 : FD_MCNT_SET( GOSSIP, PING_TRACKER_PONG_RESULT_ADDRESS, ping_tracker_metrics->pong_result[ 3UL ] );
130 0 : FD_MCNT_SET( GOSSIP, PING_TRACKER_PONG_RESULT_TOKEN, ping_tracker_metrics->pong_result[ 4UL ] );
131 0 : FD_MCNT_SET( GOSSIP, PING_TRACKER_PONG_RESULT_SUCCESS, ping_tracker_metrics->pong_result[ 5UL ] );
132 :
133 0 : FD_MCNT_SET( GOSSIP, PING_TRACKER_EVICTED_COUNT, ping_tracker_metrics->peers_evicted );
134 0 : FD_MCNT_SET( GOSSIP, PING_TRACKED_COUNT, ping_tracker_metrics->tracked_cnt );
135 0 : FD_MCNT_SET( GOSSIP, PING_TRACKER_STAKE_CHANGED_COUNT, ping_tracker_metrics->stake_changed_cnt );
136 0 : FD_MCNT_SET( GOSSIP, PING_TRACKER_ADDRESS_CHANGED_COUNT, ping_tracker_metrics->address_changed_cnt );
137 :
138 0 : fd_crds_metrics_t const * crds_metrics = fd_gossip_crds_metrics( ctx->gossip );
139 :
140 0 : FD_MGAUGE_ENUM_COPY( GOSSIP, CRDS_COUNT, crds_metrics->count );
141 0 : FD_MCNT_SET( GOSSIP, CRDS_EXPIRED_COUNT, crds_metrics->expired_cnt );
142 0 : FD_MCNT_SET( GOSSIP, CRDS_EVICTED_COUNT, crds_metrics->evicted_cnt );
143 :
144 0 : FD_MGAUGE_SET( GOSSIP, CRDS_PEER_STAKED_COUNT, crds_metrics->peer_staked_cnt );
145 0 : FD_MGAUGE_SET( GOSSIP, CRDS_PEER_UNSTAKED_COUNT, crds_metrics->peer_unstaked_cnt );
146 0 : FD_MGAUGE_SET( GOSSIP, CRDS_PEER_TOTAL_STAKE, crds_metrics->peer_visible_stake );
147 0 : FD_MCNT_SET( GOSSIP, CRDS_PEER_EVICTED_COUNT, crds_metrics->peer_evicted_cnt );
148 :
149 0 : FD_MGAUGE_SET( GOSSIP, CRDS_PURGED_COUNT, crds_metrics->purged_cnt );
150 0 : FD_MCNT_SET( GOSSIP, CRDS_PURGED_EVICTED_COUNT, crds_metrics->purged_evicted_cnt );
151 0 : FD_MCNT_SET( GOSSIP, CRDS_PURGED_EXPIRED_COUNT, crds_metrics->purged_expired_cnt );
152 :
153 0 : fd_gossip_metrics_t const * metrics = fd_gossip_metrics( ctx->gossip );
154 :
155 0 : FD_MCNT_ENUM_COPY( GOSSIP, MESSAGE_TX_COUNT, metrics->message_tx );
156 0 : FD_MCNT_ENUM_COPY( GOSSIP, MESSAGE_TX_BYTES, metrics->message_tx_bytes );
157 :
158 0 : FD_MCNT_ENUM_COPY( GOSSIP, CRDS_TX_PUSH_COUNT, metrics->crds_tx_push );
159 0 : FD_MCNT_ENUM_COPY( GOSSIP, CRDS_TX_PUSH_BYTES, metrics->crds_tx_push_bytes );
160 0 : FD_MCNT_ENUM_COPY( GOSSIP, CRDS_TX_PULL_RESPONSE_COUNT, metrics->crds_tx_pull_response );
161 0 : FD_MCNT_ENUM_COPY( GOSSIP, CRDS_TX_PULL_RESPONSE_BYTES, metrics->crds_tx_pull_response_bytes );
162 :
163 0 : FD_MCNT_ENUM_COPY( GOSSIP, CRDS_RX_COUNT, metrics->crds_rx_count );
164 :
165 0 : FD_MCNT_SET( GOSSIP, CONTACT_INFO_UNRECOGNIZED_SOCKET_TAGS, metrics->ci_rx_unrecognized_socket_tag_cnt );
166 0 : FD_MCNT_SET( GOSSIP, CONTACT_INFO_IPV6, metrics->ci_rx_ipv6_address_cnt );
167 0 : }
168 :
169 : void
170 : after_credit( fd_gossip_tile_ctx_t * ctx,
171 : fd_stem_context_t * stem,
172 : int * opt_poll_in FD_PARAM_UNUSED,
173 0 : int * charge_busy FD_PARAM_UNUSED ) {
174 0 : ctx->stem = stem;
175 :
176 0 : if( FD_UNLIKELY( !ctx->my_contact_info->shred_version ) ) return;
177 :
178 0 : long now = ctx->last_wallclock + (long)((double)(fd_tickcount()-ctx->last_tickcount)/ctx->ticks_per_ns);
179 0 : fd_gossip_advance( ctx->gossip, now, stem );
180 0 : }
181 :
182 : static void
183 : handle_shred_version( fd_gossip_tile_ctx_t * ctx,
184 0 : ulong sig ) {
185 0 : long now = ctx->last_wallclock + (long)((double)(fd_tickcount()-ctx->last_tickcount)/ctx->ticks_per_ns);
186 0 : ctx->my_contact_info->shred_version = (ushort)sig;
187 0 : fd_gossip_set_my_contact_info( ctx->gossip, ctx->my_contact_info, now );
188 0 : }
189 :
190 : static void
191 : handle_local_vote( fd_gossip_tile_ctx_t * ctx,
192 : fd_txn_m_t const * txn_m,
193 0 : fd_stem_context_t * stem ) {
194 0 : long now = ctx->last_wallclock + (long)((double)(fd_tickcount()-ctx->last_tickcount)/ctx->ticks_per_ns);
195 0 : fd_gossip_push_vote( ctx->gossip, fd_txn_m_payload_const( txn_m ), txn_m->payload_sz, stem, now );
196 0 : }
197 :
198 : static void
199 : handle_stakes( fd_gossip_tile_ctx_t * ctx,
200 0 : fd_stake_weight_msg_t const * msg ) {
201 0 : ulong stakes_cnt = compute_id_weights_from_vote_weights( ctx->stake_weights_converted, msg->weights, msg->staked_cnt );
202 0 : fd_gossip_stakes_update( ctx->gossip, ctx->stake_weights_converted, stakes_cnt );
203 0 : }
204 :
205 : static void
206 : handle_packet( fd_gossip_tile_ctx_t * ctx,
207 : ulong sig,
208 : uchar const * payload,
209 : ulong payload_sz,
210 0 : fd_stem_context_t * stem ) {
211 0 : long now = ctx->last_wallclock + (long)((double)(fd_tickcount()-ctx->last_tickcount)/ctx->ticks_per_ns);
212 :
213 0 : fd_ip4_port_t peer = (fd_ip4_port_t){
214 0 : .addr = fd_gossvf_sig_addr( sig ),
215 0 : .port = fd_gossvf_sig_port( sig )
216 0 : };
217 :
218 0 : switch( fd_gossvf_sig_kind( sig ) ) {
219 0 : case 0: {
220 0 : fd_gossip_rx( ctx->gossip, peer, payload, payload_sz, now, stem );
221 0 : fd_gossip_advance( ctx->gossip, now, stem );
222 0 : break;
223 0 : }
224 0 : case 1: {
225 0 : fd_gossip_pingreq_t * pingreq = (fd_gossip_pingreq_t *)payload;
226 0 : fd_gossip_ping_tracker_track( ctx->gossip, pingreq->pubkey.uc, peer, now );
227 0 : }
228 0 : }
229 0 : }
230 :
231 : static inline int
232 : returnable_frag( fd_gossip_tile_ctx_t * ctx,
233 : ulong in_idx,
234 : ulong seq,
235 : ulong sig,
236 : ulong chunk,
237 : ulong sz,
238 : ulong ctl,
239 : ulong tsorig,
240 : ulong tspub,
241 0 : fd_stem_context_t * stem ) {
242 0 : (void)seq;
243 0 : (void)ctl;
244 0 : (void)tsorig;
245 0 : (void)tspub;
246 :
247 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>ctx->in[ in_idx ].mtu ) )
248 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
249 :
250 0 : if( FD_UNLIKELY( !ctx->my_contact_info->shred_version && ctx->in[ in_idx ].kind!=IN_KIND_SHRED_VERSION ) ) return 1;
251 :
252 0 : switch( ctx->in[ in_idx ].kind ) {
253 0 : case IN_KIND_SHRED_VERSION: handle_shred_version( ctx, sig ); break;
254 0 : case IN_KIND_SEND: handle_local_vote( ctx, fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk ), stem ); break;
255 0 : case IN_KIND_STAKE: handle_stakes( ctx, fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk ) ); break;
256 0 : case IN_KIND_GOSSVF: handle_packet( ctx, sig, fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk ), sz, stem ); break;
257 0 : }
258 :
259 0 : return 0;
260 0 : }
261 :
262 : static void
263 : privileged_init( fd_topo_t * topo,
264 0 : fd_topo_tile_t * tile ) {
265 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
266 :
267 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
268 0 : fd_gossip_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_gossip_tile_ctx_t), sizeof(fd_gossip_tile_ctx_t) );
269 :
270 0 : if( FD_UNLIKELY( !strcmp( tile->gossip.identity_key_path, "" ) ) )
271 0 : FD_LOG_ERR(( "identity_key_path not set" ));
272 :
273 0 : fd_memcpy( ctx->my_contact_info->pubkey.uc, fd_type_pun_const( fd_keyload_load( tile->gossip.identity_key_path, /* pubkey only: */ 1 ) ), 32UL );
274 0 : FD_TEST( fd_rng_secure( &ctx->rng_seed, 4UL ) );
275 0 : FD_TEST( fd_rng_secure( &ctx->rng_idx, 8UL ) );
276 0 : }
277 :
278 : static inline fd_gossip_out_ctx_t
279 : out1( fd_topo_t const * topo,
280 : fd_topo_tile_t const * tile,
281 0 : char const * name ) {
282 0 : ulong idx = ULONG_MAX;
283 :
284 0 : for( ulong i=0UL; i<tile->out_cnt; i++ ) {
285 0 : fd_topo_link_t const * link = &topo->links[ tile->out_link_id[ i ] ];
286 0 : if( !strcmp( link->name, name ) ) {
287 0 : if( FD_UNLIKELY( idx!=ULONG_MAX ) ) FD_LOG_ERR(( "tile %s:%lu had multiple output links named %s but expected one", tile->name, tile->kind_id, name ));
288 0 : idx = i;
289 0 : }
290 0 : }
291 :
292 0 : if( FD_UNLIKELY( idx==ULONG_MAX ) ) FD_LOG_ERR(( "tile %s:%lu had no output link named %s", tile->name, tile->kind_id, name ));
293 :
294 0 : void * mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ idx ] ].dcache_obj_id ].wksp_id ].wksp;
295 0 : ulong chunk0 = fd_dcache_compact_chunk0( mem, topo->links[ tile->out_link_id[ idx ] ].dcache );
296 0 : ulong wmark = fd_dcache_compact_wmark ( mem, topo->links[ tile->out_link_id[ idx ] ].dcache, topo->links[ tile->out_link_id[ idx ] ].mtu );
297 :
298 0 : return (fd_gossip_out_ctx_t){ .idx = idx, .mem = mem, .chunk0 = chunk0, .wmark = wmark, .chunk = chunk0 };
299 0 : }
300 :
301 : static void
302 : unprivileged_init( fd_topo_t * topo,
303 0 : fd_topo_tile_t * tile ) {
304 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
305 :
306 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
307 0 : fd_gossip_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_gossip_tile_ctx_t), sizeof(fd_gossip_tile_ctx_t) );
308 0 : void * _gossip = FD_SCRATCH_ALLOC_APPEND( l, fd_gossip_align(), fd_gossip_footprint( tile->gossip.max_entries, tile->gossip.entrypoints_cnt ) );
309 0 : void * _stake_weights = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_stake_weight_t), MAX_STAKED_LEADERS*sizeof(fd_stake_weight_t) );
310 :
311 0 : ctx->stake_weights_converted = (fd_stake_weight_t *)_stake_weights;
312 :
313 0 : FD_TEST( fd_rng_join( fd_rng_new( ctx->rng, ctx->rng_seed, ctx->rng_idx ) ) );
314 :
315 0 : FD_TEST( tile->in_cnt<=sizeof(ctx->in)/sizeof(ctx->in[0]) );
316 0 : ulong sign_in_tile_idx = ULONG_MAX;
317 0 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
318 0 : fd_topo_link_t * link = &topo->links[ tile->in_link_id[ i ] ];
319 0 : fd_topo_wksp_t * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
320 :
321 0 : ctx->in[ i ].mem = link_wksp->wksp;
322 0 : if( FD_LIKELY( link->mtu ) ) {
323 0 : ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
324 0 : ctx->in[ i ].wmark = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
325 0 : } else {
326 0 : ctx->in[ i ].chunk0 = 0UL;
327 0 : ctx->in[ i ].wmark = 0UL;
328 0 : }
329 0 : ctx->in[ i ].mtu = link->mtu;
330 :
331 0 : if( FD_UNLIKELY( !strcmp( link->name, "ipecho_out" ) ) ) {
332 0 : ctx->in[ i ].kind = IN_KIND_SHRED_VERSION;
333 0 : } else if( FD_UNLIKELY( !strcmp( link->name, "gossvf_gossi" ) ) ) {
334 0 : ctx->in[ i ].kind = IN_KIND_GOSSVF;
335 0 : } else if( FD_UNLIKELY( !strcmp( link->name, "sign_gossip" ) ) ) {
336 0 : ctx->in[ i ].kind = IN_KIND_SIGN;
337 0 : sign_in_tile_idx = i;
338 0 : } else if( FD_UNLIKELY( !strcmp( link->name, "send_out" ) ) ) {
339 0 : ctx->in[ i ].kind = IN_KIND_SEND;
340 0 : } else if( FD_UNLIKELY( !strcmp( link->name, "replay_stake" ) ) ) {
341 0 : ctx->in[ i ].kind = IN_KIND_STAKE;
342 0 : } else {
343 0 : FD_LOG_ERR(( "unexpected input link name %s", link->name ));
344 0 : }
345 0 : }
346 :
347 0 : if( FD_UNLIKELY( sign_in_tile_idx==ULONG_MAX ) )
348 0 : FD_LOG_ERR(( "tile %s:%lu had no input link named sign_gossip", tile->name, tile->kind_id ));
349 :
350 0 : *ctx->net_out = out1( topo, tile, "gossip_net" );
351 0 : *ctx->sign_out = out1( topo, tile, "gossip_sign" );
352 0 : *ctx->gossip_out = out1( topo, tile, "gossip_out" );
353 0 : *ctx->gossvf_out = out1( topo, tile, "gossip_gossv" );
354 :
355 0 : fd_topo_link_t * sign_in = &topo->links[ tile->in_link_id [ sign_in_tile_idx ] ];
356 0 : fd_topo_link_t * sign_out = &topo->links[ tile->out_link_id[ ctx->sign_out->idx ] ];
357 :
358 0 : ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->keyswitch_obj_id ) );
359 0 : FD_TEST( ctx->keyswitch );
360 :
361 0 : if( fd_keyguard_client_join( fd_keyguard_client_new( ctx->keyguard_client,
362 0 : sign_out->mcache,
363 0 : sign_out->dcache,
364 0 : sign_in->mcache,
365 0 : sign_in->dcache,
366 0 : sign_out->mtu ) )==NULL ) {
367 0 : FD_LOG_ERR(( "failed to join keyguard client" ));
368 0 : }
369 :
370 0 : ctx->ticks_per_ns = fd_tempo_tick_per_ns( NULL );
371 0 : ctx->last_wallclock = fd_log_wallclock();
372 0 : ctx->last_tickcount = fd_tickcount();
373 :
374 0 : ctx->my_contact_info->shred_version = tile->gossip.shred_version;
375 :
376 0 : ctx->my_contact_info->wallclock_nanos = ctx->last_wallclock;
377 0 : ctx->my_contact_info->instance_creation_wallclock_nanos = tile->gossip.boot_timestamp_nanos;
378 :
379 0 : ctx->my_contact_info->version.client = FD_CONTACT_INFO_VERSION_CLIENT_FIREDANCER;
380 0 : ctx->my_contact_info->version.major = (ushort)firedancer_major_version;
381 0 : ctx->my_contact_info->version.minor = (ushort)firedancer_minor_version;
382 0 : ctx->my_contact_info->version.patch = (ushort)firedancer_patch_version;
383 0 : ctx->my_contact_info->version.commit = firedancer_commit_ref;
384 0 : ctx->my_contact_info->version.feature_set = FD_FEATURE_SET_ID;
385 :
386 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_GOSSIP ] = (fd_ip4_port_t){ .addr = tile->gossip.ports.gossip ? tile->gossip.ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.gossip ) };
387 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_TVU ] = (fd_ip4_port_t){ .addr = tile->gossip.ports.tvu ? tile->gossip.ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.tvu ) };
388 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_TPU ] = (fd_ip4_port_t){ .addr = tile->gossip.ports.tpu ? tile->gossip.ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.tpu ) };
389 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_TPU_FORWARDS ] = (fd_ip4_port_t){ .addr = tile->gossip.ports.tpu ? tile->gossip.ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.tpu ) };
390 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_TPU_QUIC ] = (fd_ip4_port_t){ .addr = tile->gossip.ports.tpu_quic ? tile->gossip.ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.tpu_quic ) };
391 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_TPU_VOTE_QUIC ] = (fd_ip4_port_t){ .addr = tile->gossip.ports.tpu_quic ? tile->gossip.ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.tpu_quic ) };
392 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_TPU_FORWARDS_QUIC ] = (fd_ip4_port_t){ .addr = tile->gossip.ports.tpu_quic ? tile->gossip.ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.tpu_quic ) };
393 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_TPU_VOTE ] = (fd_ip4_port_t){ .addr = tile->gossip.ports.tpu ? tile->gossip.ip_addr : 0, .port = fd_ushort_bswap( tile->gossip.ports.tpu ) };
394 :
395 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_TVU_QUIC ] = (fd_ip4_port_t){ .addr = 0, .port = 0 };
396 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_SERVE_REPAIR ] = (fd_ip4_port_t){ .addr = 0, .port = 0 };
397 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_SERVE_REPAIR_QUIC ] = (fd_ip4_port_t){ .addr = 0, .port = 0 };
398 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_RPC ] = (fd_ip4_port_t){ .addr = 0, .port = 0 };
399 0 : ctx->my_contact_info->sockets[ FD_CONTACT_INFO_SOCKET_RPC_PUBSUB ] = (fd_ip4_port_t){ .addr = 0, .port = 0 };
400 :
401 0 : ctx->gossip = fd_gossip_join( fd_gossip_new( _gossip,
402 0 : ctx->rng,
403 0 : tile->gossip.max_entries,
404 0 : tile->gossip.entrypoints_cnt,
405 0 : tile->gossip.entrypoints,
406 0 : ctx->my_contact_info,
407 0 : ctx->last_wallclock,
408 0 : gossip_send_fn,
409 0 : ctx,
410 0 : gossip_sign_fn,
411 0 : ctx,
412 0 : gossip_ping_tracker_change_fn,
413 0 : ctx,
414 0 : ctx->gossip_out,
415 0 : ctx->net_out ) );
416 0 : FD_TEST( ctx->gossip );
417 :
418 0 : FD_MGAUGE_SET( GOSSIP, CRDS_CAPACITY, tile->gossip.max_entries );
419 0 : FD_MGAUGE_SET( GOSSIP, CRDS_PEER_CAPACITY, FD_CONTACT_INFO_TABLE_SIZE );
420 0 : FD_MGAUGE_SET( GOSSIP, CRDS_PURGED_CAPACITY, 4UL*tile->gossip.max_entries );
421 :
422 0 : fd_ip4_udp_hdr_init( ctx->net_out_hdr, FD_GOSSIP_MTU, tile->gossip.ip_addr, tile->gossip.ports.gossip );
423 :
424 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
425 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
426 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
427 0 : }
428 :
429 : static ulong
430 : populate_allowed_seccomp( fd_topo_t const * topo,
431 : fd_topo_tile_t const * tile,
432 : ulong out_cnt,
433 0 : struct sock_filter * out ) {
434 0 : (void)topo;
435 0 : (void)tile;
436 :
437 0 : populate_sock_filter_policy_fd_gossip_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
438 0 : return sock_filter_policy_fd_gossip_tile_instr_cnt;
439 0 : }
440 :
441 : static ulong
442 : populate_allowed_fds( fd_topo_t const * topo,
443 : fd_topo_tile_t const * tile,
444 : ulong out_fds_cnt,
445 0 : int * out_fds ) {
446 0 : (void)topo;
447 0 : (void)tile;
448 :
449 0 : if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
450 :
451 0 : ulong out_cnt = 0UL;
452 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
453 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
454 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
455 0 : return out_cnt;
456 0 : }
457 :
458 : /* Account for worst case in fd_gossip_rx and fd_gossip_advance, which
459 : are both called in returnable_frag.
460 :
461 : fd_gossip_rx: Gossip updates are sent out via the gossip_out link for
462 : specific CRDS messages received, and when a contact info is dropped.
463 : Worst case is when:
464 : - all incoming CRDS messages are broadcasted as updates, and
465 : - CRDS table is full, and all entries dropped to make way for new
466 : ones are contact infos
467 :
468 : Ping tracker track also publishes a status change on the
469 : gossip_gossv link if an incoming pong changes an inactive or
470 : unpinged peer to active. There is only one pong processed per
471 : after_frag loop.
472 :
473 : This leaves us with a worst case of FD_GOSSIP_MSG_MAX_CRDS*2 on
474 : gossip_out, and 1 on gossip_gossv.
475 :
476 : fd_gossip_advance: two links we need to look at: the gossip_gossv
477 : link that publishes fd_ping_tracker changes and the gossip_out link
478 : for when contact infos are dropped during expiry.
479 :
480 : fd_ping_tracker publishes a ping status change message when a peer
481 : becomes inactive. In the worst case, all peers can become inactive
482 : in one loop. So there would be FD_PING_TRACKER_MAX ping status
483 : changes.
484 :
485 : During the expire loop, all contact infos might be dropped in one
486 : iteration, which would result in CRDS_MAX_CONTACT_INFO gossip
487 : updates
488 :
489 : We find the worst case burst by taking the maximum burst of the two
490 : links in fd_gossip_rx and fd_gossip_advance. That would be:
491 : gossip_out link gossip_gossv link
492 : max( FD_GOSSIP_MSG_CRDS_MAX*2+CRDS_MAX_CONTACT_INFO, 1+FD_PING_TRACKER_MAX)
493 :
494 : */
495 :
496 : FD_STATIC_ASSERT( CRDS_MAX_CONTACT_INFO+FD_GOSSIP_MSG_MAX_CRDS*2UL<=FD_PING_TRACKER_MAX+1UL,
497 : "Gossip stem burst needs recalculating" );
498 0 : #define STEM_BURST ( FD_PING_TRACKER_MAX+1UL )
499 :
500 0 : #define STEM_LAZY (1000L)
501 :
502 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_gossip_tile_ctx_t
503 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_gossip_tile_ctx_t)
504 :
505 0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
506 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
507 0 : #define STEM_CALLBACK_AFTER_CREDIT after_credit
508 0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
509 :
510 : #include "../../disco/stem/fd_stem.c"
511 :
512 : fd_topo_run_tile_t fd_tile_gossip = {
513 : .name = "gossip",
514 : .populate_allowed_seccomp = populate_allowed_seccomp,
515 : .populate_allowed_fds = populate_allowed_fds,
516 : .scratch_align = scratch_align,
517 : .scratch_footprint = scratch_footprint,
518 : .privileged_init = privileged_init,
519 : .unprivileged_init = unprivileged_init,
520 : .run = stem_run,
521 : };
|