Line data Source code
1 : #include "fd_send_tile.h"
2 : #include "../../disco/topo/fd_topo.h"
3 : #include "../../disco/keyguard/fd_keyload.h"
4 : #include "../../disco/fd_txn_m.h"
5 : #include "../../disco/keyguard/fd_keyguard.h"
6 : #include "../../discof/tower/fd_tower_tile.h"
7 : #include "generated/fd_send_tile_seccomp.h"
8 : #include "../../flamenco/gossip/fd_gossip_types.h"
9 :
10 : #include <sys/random.h>
11 :
12 : /* map leader pubkey to contact/conn info
13 : A map entry is created only for staked peers. On receiving contact info, we update
14 : the map entry with the following 4 sockets from the contact info:
15 : - QUIC_VOTE
16 : - QUIC_TPU
17 : - UDP_VOTE
18 : - UDP_TPU
19 :
20 : For the UDP ports, we simply send to that sockaddr when the leader is selected.
21 : For QUIC ports, we establish a quic connection just in time for the leader slot.
22 : We allow that connection to time out if it's going to be dormant for a while.
23 : This reduces the bandwidth consumed and the amount of work the fd_quic needs to do.
24 : */
25 :
26 : #define MAP_NAME fd_send_conn_map
27 0 : #define MAP_T fd_send_conn_entry_t
28 0 : #define MAP_LG_SLOT_CNT 17
29 0 : #define MAP_KEY pubkey
30 0 : #define MAP_KEY_T fd_pubkey_t
31 0 : #define MAP_KEY_NULL (fd_pubkey_t){0}
32 0 : #define MAP_KEY_EQUAL(k0,k1) (!(memcmp((k0).key,(k1).key,sizeof(fd_pubkey_t))))
33 0 : #define MAP_KEY_INVAL(k) (MAP_KEY_EQUAL((k),MAP_KEY_NULL))
34 : #define MAP_KEY_EQUAL_IS_SLOW 1
35 0 : #define MAP_KEY_HASH(key) ((key).ui[3])
36 : #include "../../util/tmpl/fd_map.c"
37 :
38 : fd_quic_limits_t quic_limits = {
39 : .conn_cnt = MAX_STAKED_LEADERS,
40 : .handshake_cnt = MAX_STAKED_LEADERS,
41 : .conn_id_cnt = FD_QUIC_MIN_CONN_ID_CNT,
42 : .inflight_frame_cnt = 16UL * MAX_STAKED_LEADERS,
43 : .min_inflight_frame_cnt_conn = 4UL,
44 : .stream_id_cnt = 64UL,
45 : .tx_buf_sz = FD_TXN_MTU,
46 : .stream_pool_cnt = 1UL<<13
47 : };
48 :
49 : FD_FN_CONST static inline ulong
50 0 : scratch_align( void ) {
51 0 : return fd_ulong_max( fd_ulong_max( 128UL, fd_quic_align() ), fd_send_conn_map_align() );
52 0 : }
53 :
54 : FD_FN_PURE static inline ulong
55 0 : scratch_footprint( fd_topo_tile_t const * tile FD_PARAM_UNUSED) {
56 0 : ulong l = FD_LAYOUT_INIT;
57 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_send_tile_ctx_t), sizeof(fd_send_tile_ctx_t) );
58 0 : l = FD_LAYOUT_APPEND( l, fd_quic_align(), fd_quic_footprint( &quic_limits ) );
59 0 : l = FD_LAYOUT_APPEND( l, fd_send_conn_map_align(), fd_send_conn_map_footprint() );
60 0 : return FD_LAYOUT_FINI( l, scratch_align() );
61 0 : }
62 :
63 : /* QUIC callbacks */
64 :
65 : static void
66 : quic_tls_cv_sign( void * signer_ctx,
67 : uchar signature[ static 64 ],
68 0 : uchar const payload[ static 130 ] ) {
69 0 : fd_send_tile_ctx_t * ctx = signer_ctx;
70 0 : long dt = -fd_clock_now( ctx->clock );
71 0 : fd_keyguard_client_sign( ctx->keyguard_client, signature, payload, 130UL, FD_KEYGUARD_SIGN_TYPE_ED25519 );
72 0 : dt += ( ctx->now = fd_clock_now( ctx->clock ) );
73 0 : fd_histf_sample( ctx->metrics.sign_duration, (ulong)dt );
74 0 : }
75 :
76 : /* quic_hs_complete is called when the QUIC handshake is complete
77 : It is currently used only for debug logging */
78 : static void
79 : quic_hs_complete( fd_quic_conn_t * conn,
80 0 : void * quic_ctx FD_PARAM_UNUSED ) {
81 0 : fd_send_tile_ctx_t * ctx = fd_type_pun( quic_ctx );
82 0 : fd_send_conn_entry_t * entry = fd_type_pun( fd_quic_conn_get_context( conn ) );
83 0 : if( FD_UNLIKELY( !entry ) ) return;
84 :
85 0 : for( ulong i=0; i<FD_SEND_PORT_QUIC_CNT; i++ ) {
86 0 : if( entry->conn[i] == conn ) { ctx->metrics.quic_hs_complete[i]++; break; }
87 0 : }
88 0 : FD_DEBUG( FD_LOG_DEBUG(("QUIC handshake complete for leader %s", FD_BASE58_ENC_32_ALLOCA( entry->pubkey.key ))); )
89 0 : }
90 :
91 : inline static int
92 0 : port_idx_is_quic( ulong port_idx ) {
93 0 : return (port_idx==FD_SEND_PORT_QUIC_VOTE_IDX) | (port_idx==FD_SEND_PORT_QUIC_TPU_IDX);
94 0 : }
95 :
96 : /* quic_conn_final is called when the QUIC connection dies. */
97 : static void
98 : quic_conn_final( fd_quic_conn_t * conn,
99 0 : void * quic_ctx ) {
100 0 : fd_send_conn_entry_t * entry = fd_type_pun( fd_quic_conn_get_context( conn ) );
101 0 : if( FD_UNLIKELY( !entry ) ) {
102 0 : FD_LOG_CRIT(( "Conn map entry not found in conn_final" ));
103 0 : }
104 :
105 0 : fd_send_tile_ctx_t * ctx = fd_type_pun( quic_ctx );
106 :
107 0 : for( ulong i=0UL; i<FD_SEND_PORT_QUIC_CNT; i++ ) {
108 0 : if( entry->conn[i] == conn ) {
109 0 : entry->conn[i] = NULL;
110 0 : FD_DEBUG( FD_LOG_DEBUG(("Quic final for conn: %p to peer " FD_IP4_ADDR_FMT ":%u in entry %p to pubkey %s",
111 0 : (void*)conn, FD_IP4_ADDR_FMT_ARGS(entry->ip4s[i]), entry->ports[i], (void*)entry,
112 0 : FD_BASE58_ENC_32_ALLOCA(entry->pubkey.key))); )
113 0 : ctx->metrics.quic_conn_final[i]++;
114 0 : if( i==FD_SEND_PORT_QUIC_VOTE_IDX ) entry->last_quic_vote_close = ctx->now;
115 0 : return;
116 0 : }
117 0 : }
118 :
119 0 : FD_LOG_CRIT(( "conn not found in entry for peer %s", FD_BASE58_ENC_32_ALLOCA( entry->pubkey.key )));
120 0 : }
121 :
122 : /* send_to_net sends a packet to the net tile.
123 : It takes pointers to the ip4 hdr, udp hdr, and the payload.
124 : Always uses the eth hdr from ctx->packet_hdr. */
125 : static void
126 : send_to_net( fd_send_tile_ctx_t * ctx,
127 : fd_ip4_hdr_t const * ip4_hdr,
128 : fd_udp_hdr_t const * udp_hdr,
129 : uchar const * payload,
130 0 : ulong payload_sz ) {
131 :
132 0 : uint const ip_dst = FD_LOAD( uint, ip4_hdr->daddr_c );
133 0 : ulong const ip_sz = FD_IP4_GET_LEN( *ip4_hdr );
134 :
135 0 : fd_send_link_out_t * net_out_link = ctx->net_out;
136 0 : uchar * packet_l2 = fd_chunk_to_laddr( net_out_link->mem, net_out_link->chunk );
137 0 : uchar * packet_l3 = packet_l2 + sizeof(fd_eth_hdr_t);
138 0 : uchar * packet_l4 = packet_l3 + ip_sz;
139 0 : uchar * packet_l5 = packet_l4 + sizeof(fd_udp_hdr_t);
140 :
141 0 : fd_memcpy( packet_l2, ctx->packet_hdr->eth, sizeof(fd_eth_hdr_t) );
142 0 : fd_memcpy( packet_l3, ip4_hdr, ip_sz );
143 0 : fd_memcpy( packet_l4, udp_hdr, sizeof(fd_udp_hdr_t) );
144 0 : fd_memcpy( packet_l5, payload, payload_sz );
145 :
146 0 : ulong sig = fd_disco_netmux_sig( ip_dst, 0U, ip_dst, DST_PROTO_OUTGOING, FD_NETMUX_SIG_MIN_HDR_SZ );
147 0 : ulong tspub = (ulong)ctx->now;
148 0 : ulong sz_l2 = sizeof(fd_eth_hdr_t) + ip_sz + sizeof(fd_udp_hdr_t) + payload_sz;
149 :
150 0 : fd_stem_publish( ctx->stem, net_out_link->idx, sig, net_out_link->chunk, sz_l2, 0UL, 0, tspub );
151 0 : net_out_link->chunk = fd_dcache_compact_next( net_out_link->chunk, sz_l2, net_out_link->chunk0, net_out_link->wmark );
152 0 : }
153 :
154 : static int
155 : quic_tx_aio_send( void * _ctx,
156 : fd_aio_pkt_info_t const * batch,
157 : ulong batch_cnt,
158 : ulong * opt_batch_idx,
159 0 : int flush FD_PARAM_UNUSED ) {
160 0 : fd_send_tile_ctx_t * ctx = _ctx;
161 :
162 0 : for( ulong i=0; i<batch_cnt; i++ ) {
163 0 : if( FD_UNLIKELY( batch[ i ].buf_sz<FD_NETMUX_SIG_MIN_HDR_SZ ) ) continue;
164 0 : uchar * buf = batch[ i ].buf;
165 0 : fd_ip4_hdr_t * ip4_hdr = fd_type_pun( buf );
166 0 : ulong const ip4_len = FD_IP4_GET_LEN( *ip4_hdr );
167 0 : fd_udp_hdr_t * udp_hdr = fd_type_pun( buf + ip4_len );
168 0 : uchar * payload = buf + ip4_len + sizeof(fd_udp_hdr_t);
169 0 : FD_TEST( batch[ i ].buf_sz >= ip4_len + sizeof(fd_udp_hdr_t) );
170 0 : ulong payload_sz = batch[ i ].buf_sz - ip4_len - sizeof(fd_udp_hdr_t);
171 0 : send_to_net( ctx, ip4_hdr, udp_hdr, payload, payload_sz );
172 0 : }
173 :
174 0 : if( FD_LIKELY( opt_batch_idx ) ) {
175 0 : *opt_batch_idx = batch_cnt;
176 0 : }
177 :
178 0 : return FD_AIO_SUCCESS;
179 0 : }
180 :
181 : static void
182 0 : during_housekeeping( fd_send_tile_ctx_t * ctx ) {
183 0 : if( FD_UNLIKELY( ctx->recal_next <= ctx->now ) ) {
184 0 : ctx->recal_next = fd_clock_default_recal( ctx->clock );
185 0 : }
186 0 : }
187 :
188 : /* quic_connect initiates quic connections for a given entry and port. It uses the
189 : contact info stored in entry, and points the conn and entry to each other.
190 : Returns a handle to the new connection, and NULL if creating it failed */
191 :
192 : static fd_quic_conn_t *
193 : quic_connect( fd_send_tile_ctx_t * ctx,
194 : fd_send_conn_entry_t * entry,
195 0 : ulong port_idx ) {
196 :
197 0 : ulong conn_idx = port_idx;
198 0 : uint dst_ip = entry->ip4s[port_idx];
199 0 : ushort dst_port = entry->ports[port_idx];
200 :
201 0 : FD_TEST( entry->conn[conn_idx] == NULL );
202 :
203 0 : fd_quic_conn_t * conn = fd_quic_connect( ctx->quic, dst_ip, dst_port, ctx->src_ip_addr, ctx->src_port, ctx->now );
204 0 : if( FD_UNLIKELY( !conn ) ) {
205 0 : FD_LOG_WARNING(( "Failed to create QUIC connection to " FD_IP4_ADDR_FMT ":%u", FD_IP4_ADDR_FMT_ARGS(dst_ip), dst_port ));
206 0 : return NULL;
207 0 : }
208 :
209 0 : FD_DEBUG( FD_LOG_DEBUG(("Quic created conn: %p to peer " FD_IP4_ADDR_FMT ":%u in entry %p to pubkey %s",
210 0 : (void*)conn, FD_IP4_ADDR_FMT_ARGS(dst_ip), dst_port, (void*)entry,
211 0 : FD_BASE58_ENC_32_ALLOCA(entry->pubkey.key))); )
212 :
213 0 : entry->conn[conn_idx] = conn;
214 0 : fd_quic_conn_set_context( conn, entry );
215 :
216 0 : return conn;
217 0 : }
218 :
219 : /* ensure_conn_for_slot ensures a connection exists for the given pubkey if it's
220 : a leader for the target slot. Creates connection if needed. */
221 : static void
222 : ensure_conn_for_slot( fd_send_tile_ctx_t * ctx,
223 : ulong target_slot,
224 0 : long lifespan ) {
225 0 : fd_pubkey_t const * leader = fd_multi_epoch_leaders_get_leader_for_slot( ctx->mleaders, target_slot );
226 0 : if( FD_UNLIKELY( !leader ) ) {
227 : /* Track NoLeader outcome for both QUIC ports */
228 0 : for( ulong i=0UL; i<FD_SEND_PORT_QUIC_CNT; i++ ) {
229 0 : ctx->metrics.ensure_conn_result[i][FD_METRICS_ENUM_SEND_ENSURE_CONN_RESULT_V_NO_LEADER_IDX]++;
230 0 : }
231 0 : return;
232 0 : }
233 :
234 0 : fd_send_conn_entry_t * entry = fd_send_conn_map_query( ctx->conn_map, *leader, NULL );
235 0 : if( FD_UNLIKELY( !entry ) ) FD_LOG_CRIT(( "Tried ensuring conn for unstaked pubkey"));
236 :
237 0 : for( ulong i=0UL; i<FD_SEND_PORT_QUIC_CNT; i++ ) {
238 0 : if( FD_UNLIKELY( !entry->ip4s[i] | !entry->ports[i] ) ) {
239 0 : ctx->metrics.ensure_conn_result[i][FD_METRICS_ENUM_SEND_ENSURE_CONN_RESULT_V_NO_CI_IDX]++;
240 0 : continue;
241 0 : }
242 :
243 0 : if( !entry->conn[i] ) {
244 : /* Attempting to create new connection */
245 0 : if( (i==FD_SEND_PORT_QUIC_VOTE_IDX) & (ctx->now<entry->last_quic_vote_close+1e9L*FD_SEND_QUIC_VOTE_MIN_CONN_COOLDOWN_SECONDS) ) {
246 0 : FD_LOG_DEBUG(("Skipping QUIC connection to " FD_IP4_ADDR_FMT ":%u bc of cooldown", FD_IP4_ADDR_FMT_ARGS(entry->ip4s[i]), entry->ports[i] ));
247 0 : ctx->metrics.ensure_conn_result[i][FD_METRICS_ENUM_SEND_ENSURE_CONN_RESULT_V_COOLDOWN_IDX]++;
248 0 : continue;
249 0 : }
250 0 : fd_quic_conn_t * conn = quic_connect( ctx, entry, i );
251 0 : if( FD_UNLIKELY( !conn ) ) {
252 0 : ctx->metrics.ensure_conn_result[i][FD_METRICS_ENUM_SEND_ENSURE_CONN_RESULT_V_CONN_FAILED_IDX]++;
253 0 : continue;
254 0 : }
255 0 : ctx->metrics.ensure_conn_result[i][FD_METRICS_ENUM_SEND_ENSURE_CONN_RESULT_V_NEW_CONNECTION_IDX]++;
256 0 : fd_quic_service( ctx->quic, ctx->now );
257 0 : } else {
258 : /* Connection already exists */
259 0 : ctx->metrics.ensure_conn_result[i][FD_METRICS_ENUM_SEND_ENSURE_CONN_RESULT_V_CONNECTED_IDX]++;
260 0 : fd_quic_conn_let_die( entry->conn[i], lifespan, ctx->now );
261 0 : fd_quic_service( ctx->quic, ctx->now );
262 0 : }
263 0 : }
264 0 : }
265 :
266 : /* leader_send sends a payload to 'pubkey' in all possible ways. For quic targets,
267 : it relies on quic connections that are already established. */
268 : static void
269 : leader_send( fd_send_tile_ctx_t * ctx,
270 : fd_pubkey_t const * pubkey,
271 : uchar const * payload,
272 0 : ulong payload_sz ) {
273 :
274 0 : fd_send_conn_entry_t * entry = fd_send_conn_map_query( ctx->conn_map, *pubkey, NULL );
275 0 : if( FD_UNLIKELY( !entry ) ) {
276 0 : FD_LOG_CRIT(( "Tried looking up connection for an unstaked pubkey"));
277 0 : }
278 :
279 0 : for( ulong i=0UL; i<FD_SEND_PORT_CNT; i++ ) {
280 : /* skip unroutable */
281 0 : if( !entry->ip4s[i] | !entry->ports[i] ) {
282 0 : ctx->metrics.send_result_cnt[i][FD_METRICS_ENUM_TXN_SEND_RESULT_V_NO_CI_IDX]++;
283 0 : continue;
284 0 : }
285 :
286 0 : if( port_idx_is_quic( i ) ) {
287 0 : fd_quic_conn_t * conn = entry->conn[i];
288 0 : if( FD_UNLIKELY( !conn ) ) {
289 0 : FD_LOG_DEBUG(("no conn for %s at " FD_IP4_ADDR_FMT ":%u", FD_BASE58_ENC_32_ALLOCA( pubkey->key ), FD_IP4_ADDR_FMT_ARGS(entry->ip4s[i]), entry->ports[i] ));
290 0 : ctx->metrics.send_result_cnt[i][FD_METRICS_ENUM_TXN_SEND_RESULT_V_NO_CONN_IDX]++;
291 0 : continue;
292 0 : }
293 :
294 0 : fd_quic_stream_t * stream = fd_quic_conn_new_stream( conn );
295 0 : if( FD_UNLIKELY( !stream ) ) {
296 0 : FD_LOG_DEBUG(("new_stream failed for %s at " FD_IP4_ADDR_FMT ":%u bc conn state was %u", FD_BASE58_ENC_32_ALLOCA( pubkey->key ), FD_IP4_ADDR_FMT_ARGS(entry->ip4s[i]), entry->ports[i], conn->state ));
297 0 : ctx->metrics.send_result_cnt[i][FD_METRICS_ENUM_TXN_SEND_RESULT_V_NO_STREAM_IDX]++;
298 0 : continue;
299 0 : }
300 :
301 0 : ctx->metrics.send_result_cnt[i][FD_METRICS_ENUM_TXN_SEND_RESULT_V_SUCCESS_IDX]++;
302 :
303 0 : fd_quic_stream_send( stream, payload, payload_sz, 1 );
304 0 : fd_quic_service( ctx->quic, ctx->now ); /* trigger send ASAP */
305 0 : } else {
306 :
307 0 : fd_ip4_hdr_t * ip4_hdr = ctx->packet_hdr->ip4;
308 0 : fd_udp_hdr_t * udp_hdr = ctx->packet_hdr->udp;
309 :
310 0 : ctx->metrics.send_result_cnt[i][FD_METRICS_ENUM_TXN_SEND_RESULT_V_SUCCESS_IDX]++;
311 :
312 0 : ip4_hdr->daddr = entry->ip4s[i];
313 0 : ip4_hdr->net_tot_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_ip4_hdr_t)+sizeof(fd_udp_hdr_t)) );
314 0 : ip4_hdr->net_id = fd_ushort_bswap( ctx->net_id++ );
315 0 : ip4_hdr->check = 0;
316 0 : ip4_hdr->check = fd_ip4_hdr_check_fast( ip4_hdr );
317 :
318 0 : udp_hdr->net_dport = fd_ushort_bswap( entry->ports[i] ); /* to net order */
319 0 : udp_hdr->net_len = fd_ushort_bswap( (ushort)( payload_sz + sizeof(fd_udp_hdr_t) ) );
320 0 : send_to_net( ctx, ip4_hdr, udp_hdr, payload, payload_sz );
321 0 : }
322 0 : }
323 0 : }
324 :
325 : /* handle_contact_info_update handles a new contact. Validates contact info
326 : and starts/restarts a connection if necessary. */
327 : static inline void
328 : handle_contact_info_update( fd_send_tile_ctx_t * ctx,
329 0 : fd_gossip_update_message_t const * msg ) {
330 0 : static uint const socket_idx[FD_SEND_PORT_CNT] = {
331 0 : FD_CONTACT_INFO_SOCKET_TPU_VOTE_QUIC,
332 0 : FD_CONTACT_INFO_SOCKET_TPU_QUIC,
333 0 : FD_CONTACT_INFO_SOCKET_TPU_VOTE,
334 0 : FD_CONTACT_INFO_SOCKET_TPU
335 0 : };
336 :
337 0 : fd_send_conn_entry_t * entry = fd_send_conn_map_query( ctx->conn_map, *(fd_pubkey_t *)(msg->origin_pubkey), NULL );
338 0 : if( FD_UNLIKELY( !entry ) ) {
339 : /* Skip if UNSTAKED */
340 0 : ctx->metrics.unstaked_ci_rcvd++;
341 0 : return;
342 0 : }
343 :
344 0 : for( ulong i=0UL; i<FD_SEND_PORT_CNT; i++ ) {
345 :
346 0 : fd_ip4_port_t const * socket = &msg->contact_info.contact_info->sockets[ socket_idx[i] ];
347 0 : uint new_ip = socket->addr;
348 0 : ushort new_port = fd_ushort_bswap( socket->port ); /* convert port to host order */
349 0 : uint old_ip = entry->ip4s[i];
350 0 : ushort old_port = entry->ports[i];
351 :
352 0 : if( FD_UNLIKELY( !new_ip || !new_port ) ) {
353 0 : FD_LOG_DEBUG(( "Unroutable contact info for pubkey %s", FD_BASE58_ENC_32_ALLOCA( msg->origin_pubkey )));
354 0 : ctx->metrics.new_contact_info[i][FD_METRICS_ENUM_NEW_CONTACT_OUTCOME_V_UNROUTABLE_IDX]++;
355 0 : continue;
356 0 : }
357 :
358 0 : int info_changed = (old_ip != new_ip) | (old_port != new_port);
359 0 : entry->ip4s [i] = new_ip;
360 0 : entry->ports[i] = new_port;
361 :
362 0 : ulong quic_conn_idx = i;
363 0 : if( port_idx_is_quic( i ) && info_changed && entry->conn[quic_conn_idx]!=NULL ) {
364 : /* Track connection finalization before closing */
365 0 : fd_quic_conn_close( entry->conn[i], 0 );
366 0 : }
367 :
368 : /* bc taking branches for just metrics would be sad */
369 : /* !info_changed -> NoChange, info_changed && old_port==0 -> Initialized, info_changed && old_port!=0 -> Changed */
370 0 : static ulong metric_idx_map[] = {
371 0 : FD_METRICS_ENUM_NEW_CONTACT_OUTCOME_V_NO_CHANGE_IDX,
372 0 : FD_METRICS_ENUM_NEW_CONTACT_OUTCOME_V_INITIALIZED_IDX,
373 0 : FD_METRICS_ENUM_NEW_CONTACT_OUTCOME_V_CHANGED_IDX
374 0 : };
375 0 : ulong metric_idx = metric_idx_map[ !!info_changed<<(!!old_port) ];
376 0 : ctx->metrics.new_contact_info[i][metric_idx]++;
377 0 : }
378 0 : }
379 :
380 : static inline void
381 : handle_contact_info_removal( fd_send_tile_ctx_t * ctx FD_PARAM_UNUSED,
382 0 : fd_gossip_update_message_t const * msg FD_PARAM_UNUSED ) {
383 0 : fd_send_conn_entry_t * entry = fd_send_conn_map_query( ctx->conn_map, *(fd_pubkey_t *)(msg->origin_pubkey), NULL );
384 0 : if( FD_LIKELY( entry ) ) {
385 0 : for( ulong i=0UL; i<FD_SEND_PORT_QUIC_CNT; i++ ) {
386 0 : if( FD_UNLIKELY( entry->conn[i] ) ) fd_quic_conn_close( entry->conn[i], 0 );
387 0 : entry->ip4s[i] = 0;
388 0 : entry->ports[i] = 0;
389 0 : }
390 0 : ctx->metrics.ci_removed++;
391 0 : }
392 0 : }
393 :
394 : /* Called during after_frag for stake messages. */
395 : static void
396 0 : finalize_stake_msg( fd_send_tile_ctx_t * ctx ) {
397 :
398 0 : fd_multi_epoch_leaders_stake_msg_fini( ctx->mleaders );
399 :
400 : /* Get the current stake destinations */
401 0 : fd_vote_stake_weight_t const * stakes = fd_multi_epoch_leaders_get_stake_weights( ctx->mleaders );
402 0 : ulong stake_cnt = fd_multi_epoch_leaders_get_stake_cnt( ctx->mleaders );
403 0 : if( FD_UNLIKELY( !stakes ) ) {
404 0 : FD_LOG_WARNING(( "No stake destinations available for current slot" ));
405 0 : return;
406 0 : }
407 :
408 : /* populate staked validators in connection map */
409 0 : for( ulong i=0UL; i<stake_cnt; i++ ) {
410 0 : fd_vote_stake_weight_t const * stake_info = &stakes[i];
411 0 : fd_pubkey_t const pubkey = stake_info->id_key;
412 :
413 0 : fd_send_conn_entry_t * entry = fd_send_conn_map_query( ctx->conn_map, pubkey, NULL );
414 : /* UNSTAKED -> NO_CONN: create new entry in NO_CONN state */
415 0 : if( FD_UNLIKELY( !entry ) ) {
416 : /* insert and initialize entry */
417 0 : entry = fd_send_conn_map_insert( ctx->conn_map, pubkey );
418 0 : *entry = (fd_send_conn_entry_t){.pubkey = entry->pubkey, .hash = entry->hash };
419 0 : }
420 0 : }
421 0 : }
422 :
423 : static void
424 : handle_vote_msg( fd_send_tile_ctx_t * ctx,
425 : ulong vote_slot,
426 : uchar * signed_vote_txn,
427 0 : ulong vote_txn_sz ) {
428 :
429 0 : uchar txn_mem[ FD_TXN_MAX_SZ ] __attribute__((aligned(alignof(fd_txn_t))));
430 0 : fd_txn_t * txn = (fd_txn_t *)txn_mem;
431 0 : FD_TEST( fd_txn_parse( signed_vote_txn, vote_txn_sz, txn_mem, NULL ) );
432 :
433 : /* sign the txn */
434 0 : uchar * signature = signed_vote_txn + txn->signature_off;
435 0 : uchar const * message = signed_vote_txn + txn->message_off;
436 0 : ulong message_sz = vote_txn_sz - txn->message_off;
437 0 : fd_keyguard_client_sign( ctx->keyguard_client, signature, message, message_sz, FD_KEYGUARD_SIGN_TYPE_ED25519 );
438 :
439 0 : ulong poh_slot = vote_slot+1;
440 0 : FD_LOG_INFO(("got vote for slot %lu", vote_slot));
441 :
442 : /* send to leader for next few slots */
443 0 : for( ulong i=0UL; i<FD_SEND_TARGET_LEADER_CNT; i++ ) {
444 0 : ulong target_slot = poh_slot + i*FD_EPOCH_SLOTS_PER_ROTATION;
445 0 : fd_pubkey_t const * leader = fd_multi_epoch_leaders_get_leader_for_slot( ctx->mleaders, target_slot );
446 0 : if( FD_LIKELY( leader ) ) {
447 0 : leader_send( ctx, leader, signed_vote_txn, vote_txn_sz );
448 0 : } else {
449 : /* TODO: eventually make this CRIT, rm metrics */
450 0 : ctx->metrics.leader_not_found++;
451 0 : FD_LOG_DEBUG(("Failed to get leader contact for slot %lu", target_slot));
452 0 : }
453 0 : }
454 :
455 0 : for( ulong i=0; i<FD_SEND_CONNECT_AHEAD_LEADER_CNT; i++ ) {
456 0 : ulong connect_slot = poh_slot + i*FD_EPOCH_SLOTS_PER_ROTATION;
457 : /* keep alive for at least as long as needed */
458 0 : ensure_conn_for_slot( ctx, connect_slot, FD_SEND_QUIC_MIN_CONN_LIFETIME_SECONDS * (long)1e9 );
459 0 : }
460 :
461 : /* send to gossip and dedup */
462 0 : fd_send_link_out_t * gossip_verify_out = ctx->gossip_verify_out;
463 0 : uchar * msg_to_gossip = fd_chunk_to_laddr( gossip_verify_out->mem, gossip_verify_out->chunk );
464 0 : fd_txn_m_t * txnm = (fd_txn_m_t *)msg_to_gossip;
465 0 : *txnm = (fd_txn_m_t) { 0UL };
466 0 : txnm->payload_sz = (ushort)vote_txn_sz;
467 0 : txnm->source_ipv4 = ctx->src_ip_addr;
468 0 : txnm->source_tpu = FD_TXN_M_TPU_SOURCE_SEND;
469 0 : fd_memcpy( msg_to_gossip+sizeof(fd_txn_m_t), signed_vote_txn, vote_txn_sz );
470 0 : ulong msg_sz = fd_txn_m_realized_footprint( txnm, 0, 0 );
471 0 : fd_stem_publish( ctx->stem, gossip_verify_out->idx, 1UL, gossip_verify_out->chunk, msg_sz, 0UL, 0, 0 );
472 0 : gossip_verify_out->chunk = fd_dcache_compact_next(
473 0 : gossip_verify_out->chunk,
474 0 : msg_sz,
475 0 : gossip_verify_out->chunk0,
476 0 : gossip_verify_out->wmark );
477 0 : }
478 :
479 : /* Stem callbacks */
480 :
481 : static inline void
482 : before_credit( fd_send_tile_ctx_t * ctx,
483 : fd_stem_context_t * stem,
484 0 : int * charge_busy ) {
485 0 : ctx->stem = stem;
486 :
487 0 : ctx->now = fd_clock_now( ctx->clock );
488 : /* Publishes to mcache via callbacks */
489 0 : *charge_busy = fd_quic_service( ctx->quic, ctx->now );
490 0 : }
491 :
492 : static inline int
493 : before_frag( fd_send_tile_ctx_t * ctx,
494 : ulong in_idx,
495 : ulong seq FD_PARAM_UNUSED,
496 0 : ulong sig ) {
497 0 : if( FD_UNLIKELY( ctx->in_links[in_idx].kind==IN_KIND_GOSSIP ) ) {
498 0 : return sig!=FD_GOSSIP_UPDATE_TAG_CONTACT_INFO &&
499 0 : sig!=FD_GOSSIP_UPDATE_TAG_CONTACT_INFO_REMOVE;
500 0 : }
501 0 : return 0;
502 0 : }
503 :
504 : static void
505 : during_frag( fd_send_tile_ctx_t * ctx,
506 : ulong in_idx,
507 : ulong seq FD_PARAM_UNUSED,
508 : ulong sig FD_PARAM_UNUSED,
509 : ulong chunk,
510 : ulong sz,
511 0 : ulong ctl ) {
512 :
513 0 : fd_send_link_in_t * in_link = &ctx->in_links[ in_idx ];
514 0 : if( FD_UNLIKELY( chunk<in_link->chunk0 || chunk>in_link->wmark ) ) {
515 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu] on link %lu", chunk, sz, in_link->chunk0, in_link->wmark, in_idx ));
516 0 : }
517 :
518 0 : uchar const * dcache_entry = fd_chunk_to_laddr_const( in_link->mem, chunk );
519 0 : ulong kind = in_link->kind;
520 :
521 0 : if( FD_UNLIKELY( kind==IN_KIND_NET ) ) {
522 0 : void const * src = fd_net_rx_translate_frag( &ctx->net_in_bounds[ in_idx ], chunk, ctl, sz );
523 0 : fd_memcpy( ctx->quic_buf, src, sz );
524 0 : }
525 :
526 0 : if( FD_UNLIKELY( kind==IN_KIND_STAKE ) ) {
527 0 : if( sz>sizeof(fd_stake_weight_t)*(MAX_STAKED_LEADERS+1UL) ) {
528 0 : FD_LOG_ERR(( "sz %lu >= max expected stake update size %lu", sz, sizeof(fd_stake_weight_t) * (MAX_STAKED_LEADERS+1UL) ));
529 0 : }
530 0 : fd_multi_epoch_leaders_stake_msg_init( ctx->mleaders, fd_type_pun_const( dcache_entry ) );
531 0 : }
532 :
533 0 : if( FD_LIKELY( kind==IN_KIND_GOSSIP ) ) {
534 0 : fd_memcpy( ctx->contact_buf, dcache_entry, sz );
535 0 : }
536 :
537 0 : if( FD_UNLIKELY( kind==IN_KIND_TOWER ) ) {
538 0 : if( FD_LIKELY( sig==FD_TOWER_SIG_SLOT_DONE ) ) {
539 0 : FD_TEST( sz==sizeof(fd_tower_slot_done_t) );
540 :
541 0 : fd_tower_slot_done_t const * slot_done = fd_type_pun_const( dcache_entry );
542 :
543 0 : ulong const vote_slot = slot_done->vote_slot;
544 0 : ulong const vote_txn_sz = slot_done->vote_txn_sz;
545 0 : if( FD_UNLIKELY( vote_slot==ULONG_MAX ) ) return; /* no new vote to send */
546 :
547 0 : uchar vote_txn[ FD_TPU_MTU ];
548 0 : fd_memcpy( vote_txn, slot_done->vote_txn, vote_txn_sz );
549 :
550 0 : handle_vote_msg( ctx, vote_slot, vote_txn, vote_txn_sz );
551 0 : }
552 0 : }
553 0 : }
554 :
555 : static void
556 : after_frag( fd_send_tile_ctx_t * ctx,
557 : ulong in_idx,
558 : ulong seq FD_PARAM_UNUSED,
559 : ulong sig FD_PARAM_UNUSED,
560 : ulong sz,
561 : ulong tsorig FD_PARAM_UNUSED,
562 : ulong tspub FD_PARAM_UNUSED,
563 0 : fd_stem_context_t * stem ) {
564 :
565 0 : ctx->stem = stem;
566 :
567 0 : fd_send_link_in_t * in_link = &ctx->in_links[ in_idx ];
568 0 : ulong kind = in_link->kind;
569 :
570 0 : if( FD_UNLIKELY( kind==IN_KIND_NET ) ) {
571 0 : uchar * ip_pkt = ctx->quic_buf + sizeof(fd_eth_hdr_t);
572 0 : ulong ip_sz = sz - sizeof(fd_eth_hdr_t);
573 0 : fd_quic_t * quic = ctx->quic;
574 :
575 0 : fd_quic_process_packet( quic, ip_pkt, ip_sz, ctx->now );
576 0 : }
577 :
578 0 : if( FD_UNLIKELY( kind==IN_KIND_GOSSIP ) ) {
579 0 : fd_gossip_update_message_t const * msg = fd_type_pun_const( ctx->contact_buf );
580 0 : if( FD_LIKELY( msg->tag==FD_GOSSIP_UPDATE_TAG_CONTACT_INFO ) ) {
581 0 : handle_contact_info_update( ctx, msg );
582 0 : } else if ( FD_UNLIKELY( msg->tag==FD_GOSSIP_UPDATE_TAG_CONTACT_INFO_REMOVE ) ) {
583 0 : handle_contact_info_removal( ctx, msg );
584 0 : }
585 0 : }
586 :
587 0 : if( FD_UNLIKELY( kind==IN_KIND_STAKE ) ) {
588 0 : finalize_stake_msg( ctx );
589 0 : }
590 0 : }
591 :
592 : static void
593 : privileged_init( fd_topo_t * topo,
594 0 : fd_topo_tile_t * tile ) {
595 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
596 :
597 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
598 0 : fd_send_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_send_tile_ctx_t), sizeof(fd_send_tile_ctx_t) );
599 0 : fd_memset( ctx, 0, sizeof(fd_send_tile_ctx_t) );
600 :
601 0 : if( FD_UNLIKELY( !strcmp( tile->send.identity_key_path, "" ) ) )
602 0 : FD_LOG_ERR(( "identity_key_path not set" ));
603 :
604 0 : ctx->identity_key[ 0 ] = *(fd_pubkey_t const *)(fd_keyload_load( tile->send.identity_key_path, /* pubkey only: */ 1 ) );
605 0 : }
606 :
607 : static fd_send_link_in_t *
608 : setup_input_link( fd_send_tile_ctx_t * ctx,
609 : fd_topo_t * topo,
610 : fd_topo_tile_t * tile,
611 : ulong kind,
612 0 : ulong in_idx) {
613 0 : fd_topo_link_t * in_link = &topo->links[ tile->in_link_id[ in_idx ] ];
614 0 : fd_send_link_in_t * in_link_desc = &ctx->in_links[ in_idx ];
615 :
616 0 : in_link_desc->mem = topo->workspaces[ topo->objs[ in_link->dcache_obj_id ].wksp_id ].wksp;
617 0 : in_link_desc->chunk0 = fd_dcache_compact_chunk0( in_link_desc->mem, in_link->dcache );
618 0 : in_link_desc->wmark = fd_dcache_compact_wmark( in_link_desc->mem, in_link->dcache, in_link->mtu );
619 0 : in_link_desc->dcache = in_link->dcache;
620 0 : in_link_desc->kind = kind;
621 :
622 0 : return in_link_desc;
623 0 : }
624 :
625 : static void
626 : setup_output_link( fd_send_link_out_t * desc,
627 : fd_topo_t * topo,
628 : fd_topo_tile_t * tile,
629 0 : const char * name ) {
630 0 : ulong out_idx = fd_topo_find_tile_out_link( topo, tile, name, 0 );
631 0 : FD_TEST( out_idx!=ULONG_MAX );
632 0 : fd_topo_link_t * out_link = &topo->links[ tile->out_link_id[ out_idx ] ];
633 :
634 0 : desc->idx = out_idx;
635 0 : desc->mcache = out_link->mcache;
636 0 : desc->sync = fd_mcache_seq_laddr( desc->mcache );
637 0 : desc->depth = fd_mcache_depth( desc->mcache );
638 0 : desc->mem = topo->workspaces[ topo->objs[ out_link->dcache_obj_id ].wksp_id ].wksp;
639 0 : desc->chunk0 = fd_dcache_compact_chunk0( desc->mem, out_link->dcache );
640 0 : desc->wmark = fd_dcache_compact_wmark( desc->mem, out_link->dcache, out_link->mtu );
641 0 : desc->chunk = desc->chunk0;
642 0 : }
643 :
644 : static void
645 : unprivileged_init( fd_topo_t * topo,
646 0 : fd_topo_tile_t * tile ) {
647 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
648 :
649 0 : if( FD_UNLIKELY( !tile->out_cnt ) ) FD_LOG_ERR(( "send has no output link" ));
650 :
651 : /* Scratch mem setup */
652 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
653 0 : fd_send_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_send_tile_ctx_t), sizeof(fd_send_tile_ctx_t) );
654 :
655 0 : ctx->mleaders = fd_multi_epoch_leaders_join( fd_multi_epoch_leaders_new( ctx->mleaders_mem ) );
656 0 : FD_TEST( ctx->mleaders );
657 :
658 0 : fd_aio_t * quic_tx_aio = fd_aio_join( fd_aio_new( ctx->quic_tx_aio, ctx, quic_tx_aio_send ) );
659 0 : if( FD_UNLIKELY( !quic_tx_aio ) ) FD_LOG_ERR(( "fd_aio_join failed" ));
660 :
661 0 : fd_quic_t * quic = fd_quic_join( fd_quic_new( FD_SCRATCH_ALLOC_APPEND( l, fd_quic_align(), fd_quic_footprint( &quic_limits ) ), &quic_limits ) );
662 0 : if( FD_UNLIKELY( !quic ) ) FD_LOG_ERR(( "fd_quic_new failed" ));
663 :
664 0 : quic->config.role = FD_QUIC_ROLE_CLIENT;
665 0 : quic->config.idle_timeout = FD_SEND_QUIC_IDLE_TIMEOUT_NS;
666 0 : quic->config.ack_delay = FD_SEND_QUIC_ACK_DELAY_NS;
667 0 : quic->config.keep_alive = 1;
668 0 : quic->config.sign = quic_tls_cv_sign;
669 0 : quic->config.sign_ctx = ctx;
670 0 : fd_memcpy( quic->config.identity_public_key, ctx->identity_key, sizeof(ctx->identity_key) );
671 :
672 0 : quic->cb.conn_hs_complete = quic_hs_complete;
673 0 : quic->cb.conn_final = quic_conn_final;
674 0 : quic->cb.quic_ctx = ctx;
675 :
676 0 : fd_quic_set_aio_net_tx( quic, quic_tx_aio );
677 0 : FD_TEST_CUSTOM( fd_quic_init( quic ), "fd_quic_init failed" );
678 :
679 0 : ctx->quic = quic;
680 :
681 : /* Initialize connection map */
682 0 : void * conn_map_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_send_conn_map_align(), fd_send_conn_map_footprint() );
683 0 : ctx->conn_map = fd_send_conn_map_join( fd_send_conn_map_new( conn_map_mem ) );
684 0 : if( FD_UNLIKELY( !ctx->conn_map ) ) FD_LOG_ERR(( "fd_send_conn_map_join failed" ));
685 :
686 0 : ctx->src_ip_addr = tile->send.ip_addr;
687 0 : ctx->src_port = tile->send.send_src_port;
688 0 : fd_ip4_udp_hdr_init( ctx->packet_hdr, FD_TXN_MTU, ctx->src_ip_addr, ctx->src_port );
689 :
690 : /* Initialize input links */
691 0 : for( ulong i=0; i<tile->in_cnt; i++ ) {
692 0 : fd_topo_link_t * link = &topo->links[ tile->in_link_id[ i ] ];
693 0 : if( 0==strcmp( link->name, "net_send" ) ) {
694 0 : setup_input_link( ctx, topo, tile, IN_KIND_NET, i );
695 0 : fd_net_rx_bounds_init( &ctx->net_in_bounds[ i ], link->dcache );
696 0 : } else if( 0==strcmp( link->name, "gossip_out" ) ) {
697 0 : setup_input_link( ctx, topo, tile, IN_KIND_GOSSIP, i );
698 0 : } else if( 0==strcmp( link->name, "replay_stake" ) ) {
699 0 : setup_input_link( ctx, topo, tile, IN_KIND_STAKE, i );
700 0 : } else if( 0==strcmp( link->name, "tower_out" ) ) {
701 0 : setup_input_link( ctx, topo, tile, IN_KIND_TOWER, i );
702 0 : }
703 0 : }
704 :
705 0 : setup_output_link( ctx->gossip_verify_out, topo, tile, "send_out" );
706 0 : setup_output_link( ctx->net_out, topo, tile, "send_net" );
707 :
708 : /* Set up keyguard(s) */
709 0 : ulong sign_in_idx = fd_topo_find_tile_in_link( topo, tile, "sign_send", 0 );
710 0 : ulong sign_out_idx = fd_topo_find_tile_out_link( topo, tile, "send_sign", 0 );
711 0 : fd_topo_link_t * sign_in = &topo->links[ tile->in_link_id[ sign_in_idx ] ];
712 0 : fd_topo_link_t * sign_out = &topo->links[ tile->out_link_id[ sign_out_idx ] ];
713 :
714 0 : if ( fd_keyguard_client_join( fd_keyguard_client_new( ctx->keyguard_client,
715 0 : sign_out->mcache,
716 0 : sign_out->dcache,
717 0 : sign_in->mcache,
718 0 : sign_in->dcache,
719 0 : sign_out->mtu ) )==NULL ) {
720 0 : FD_LOG_ERR(( "Keyguard join failed" ));
721 0 : }
722 :
723 : /* init metrics */
724 0 : fd_memset( &ctx->metrics, 0, sizeof(ctx->metrics) );
725 0 : fd_histf_join( fd_histf_new( ctx->metrics.sign_duration,
726 0 : FD_MHIST_MIN( SEND, SIGN_DURATION_NANOS ),
727 0 : FD_MHIST_MAX( SEND, SIGN_DURATION_NANOS ) ) );
728 :
729 : /* Call new/join here rather than in fd_quic so min/max can differ across uses */
730 0 : fd_histf_join( fd_histf_new( quic->metrics.service_duration,
731 0 : FD_MHIST_SECONDS_MIN( SEND, SERVICE_DURATION_SECONDS ),
732 0 : FD_MHIST_SECONDS_MAX( SEND, SERVICE_DURATION_SECONDS ) ) );
733 0 : fd_histf_join( fd_histf_new( quic->metrics.receive_duration,
734 0 : FD_MHIST_SECONDS_MIN( SEND, RECEIVE_DURATION_SECONDS ),
735 0 : FD_MHIST_SECONDS_MAX( SEND, RECEIVE_DURATION_SECONDS ) ) );
736 :
737 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
738 0 : if( FD_UNLIKELY( scratch_top != (ulong)scratch + scratch_footprint( tile ) ) ) {
739 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
740 0 : }
741 :
742 0 : fd_clock_t * clock = ctx->clock;
743 0 : fd_clock_default_init( clock, ctx->clock_mem );
744 0 : ctx->recal_next = fd_clock_recal_next( clock );
745 0 : ctx->now = fd_clock_now( clock );
746 0 : }
747 :
748 : static ulong
749 : populate_allowed_seccomp( fd_topo_t const * topo FD_PARAM_UNUSED,
750 : fd_topo_tile_t const * tile FD_PARAM_UNUSED,
751 : ulong out_cnt,
752 0 : struct sock_filter * out ) {
753 :
754 0 : populate_sock_filter_policy_fd_send_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
755 0 : return sock_filter_policy_fd_send_tile_instr_cnt;
756 0 : }
757 :
758 : static ulong
759 : populate_allowed_fds( fd_topo_t const * topo FD_PARAM_UNUSED,
760 : fd_topo_tile_t const * tile FD_PARAM_UNUSED,
761 : ulong out_fds_cnt,
762 0 : int * out_fds ) {
763 0 : if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
764 :
765 0 : ulong out_cnt = 0;
766 0 : out_fds[ out_cnt++ ] = 2UL; /* stderr */
767 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
768 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
769 0 : return out_cnt;
770 0 : }
771 :
772 : static void
773 0 : metrics_write( fd_send_tile_ctx_t * ctx ) {
774 :
775 : /* Basic counters */
776 0 : FD_MCNT_SET( SEND, LEADER_NOT_FOUND, ctx->metrics.leader_not_found );
777 0 : FD_MCNT_SET( SEND, UNSTAKED_CI, ctx->metrics.unstaked_ci_rcvd );
778 0 : FD_MCNT_SET( SEND, CI_REMOVED, ctx->metrics.ci_removed );
779 :
780 : /* Port-separated contact info metrics */
781 0 : FD_MCNT_ENUM_COPY( SEND, NEW_CONTACT_INFO_QUIC_VOTE, ctx->metrics.new_contact_info[FD_SEND_PORT_QUIC_VOTE_IDX] );
782 0 : FD_MCNT_ENUM_COPY( SEND, NEW_CONTACT_INFO_QUIC_TPU, ctx->metrics.new_contact_info[FD_SEND_PORT_QUIC_TPU_IDX] );
783 0 : FD_MCNT_ENUM_COPY( SEND, NEW_CONTACT_INFO_UDP_VOTE, ctx->metrics.new_contact_info[FD_SEND_PORT_UDP_VOTE_IDX] );
784 0 : FD_MCNT_ENUM_COPY( SEND, NEW_CONTACT_INFO_UDP_TPU, ctx->metrics.new_contact_info[FD_SEND_PORT_UDP_TPU_IDX] );
785 :
786 : /* Port-separated send result metrics */
787 0 : FD_MCNT_ENUM_COPY( SEND, SEND_RESULT_QUIC_VOTE, ctx->metrics.send_result_cnt[FD_SEND_PORT_QUIC_VOTE_IDX] );
788 0 : FD_MCNT_ENUM_COPY( SEND, SEND_RESULT_QUIC_TPU, ctx->metrics.send_result_cnt[FD_SEND_PORT_QUIC_TPU_IDX] );
789 0 : FD_MCNT_ENUM_COPY( SEND, SEND_RESULT_UDP_VOTE, ctx->metrics.send_result_cnt[FD_SEND_PORT_UDP_VOTE_IDX] );
790 0 : FD_MCNT_ENUM_COPY( SEND, SEND_RESULT_UDP_TPU, ctx->metrics.send_result_cnt[FD_SEND_PORT_UDP_TPU_IDX] );
791 :
792 : /* Port-separated QUIC metrics */
793 0 : FD_MCNT_ENUM_COPY( SEND, HANDSHAKE_COMPLETE, ctx->metrics.quic_hs_complete );
794 0 : FD_MCNT_ENUM_COPY( SEND, QUIC_CONN_FINAL, ctx->metrics.quic_conn_final );
795 0 : FD_MCNT_ENUM_COPY( SEND, ENSURE_CONN_RESULT_QUIC_VOTE, ctx->metrics.ensure_conn_result[FD_METRICS_ENUM_SEND_QUIC_PORTS_V_QUIC_VOTE_IDX] );
796 0 : FD_MCNT_ENUM_COPY( SEND, ENSURE_CONN_RESULT_QUIC_TPU, ctx->metrics.ensure_conn_result[FD_METRICS_ENUM_SEND_QUIC_PORTS_V_QUIC_TPU_IDX] );
797 :
798 0 : FD_MHIST_COPY( SEND, SIGN_DURATION_NANOS, ctx->metrics.sign_duration );
799 :
800 : /* General QUIC metrics */
801 0 : FD_MCNT_SET( SEND, RECEIVED_BYTES, ctx->quic->metrics.net_rx_byte_cnt );
802 0 : FD_MCNT_ENUM_COPY( SEND, RECEIVED_FRAMES, ctx->quic->metrics.frame_rx_cnt );
803 0 : FD_MCNT_SET( SEND, RECEIVED_PACKETS, ctx->quic->metrics.net_rx_pkt_cnt );
804 0 : FD_MCNT_SET( SEND, STREAM_RECEIVED_BYTES, ctx->quic->metrics.stream_rx_byte_cnt );
805 0 : FD_MCNT_SET( SEND, STREAM_RECEIVED_EVENTS, ctx->quic->metrics.stream_rx_event_cnt );
806 :
807 0 : FD_MCNT_SET( SEND, SENT_PACKETS, ctx->quic->metrics.net_tx_pkt_cnt );
808 0 : FD_MCNT_SET( SEND, SENT_BYTES, ctx->quic->metrics.net_tx_byte_cnt );
809 0 : FD_MCNT_SET( SEND, RETRY_SENT, ctx->quic->metrics.retry_tx_cnt );
810 0 : FD_MCNT_ENUM_COPY( SEND, ACK_TX, ctx->quic->metrics.ack_tx );
811 :
812 0 : FD_MGAUGE_ENUM_COPY( SEND, CONNECTIONS_STATE, ctx->quic->metrics.conn_state_cnt );
813 0 : FD_MGAUGE_SET( SEND, CONNECTIONS_ALLOC, ctx->quic->metrics.conn_alloc_cnt );
814 0 : FD_MCNT_SET( SEND, CONNECTIONS_CREATED, ctx->quic->metrics.conn_created_cnt );
815 0 : FD_MCNT_SET( SEND, CONNECTIONS_CLOSED, ctx->quic->metrics.conn_closed_cnt );
816 0 : FD_MCNT_SET( SEND, CONNECTIONS_ABORTED, ctx->quic->metrics.conn_aborted_cnt );
817 0 : FD_MCNT_SET( SEND, CONNECTIONS_TIMED_OUT, ctx->quic->metrics.conn_timeout_cnt );
818 0 : FD_MCNT_SET( SEND, CONNECTIONS_RETRIED, ctx->quic->metrics.conn_retry_cnt );
819 0 : FD_MCNT_SET( SEND, CONNECTION_ERROR_NO_SLOTS, ctx->quic->metrics.conn_err_no_slots_cnt );
820 0 : FD_MCNT_SET( SEND, CONNECTION_ERROR_RETRY_FAIL, ctx->quic->metrics.conn_err_retry_fail_cnt );
821 :
822 0 : FD_MCNT_ENUM_COPY( SEND, PKT_CRYPTO_FAILED, ctx->quic->metrics.pkt_decrypt_fail_cnt );
823 0 : FD_MCNT_ENUM_COPY( SEND, PKT_NO_KEY, ctx->quic->metrics.pkt_no_key_cnt );
824 0 : FD_MCNT_ENUM_COPY( SEND, PKT_NO_CONN, ctx->quic->metrics.pkt_no_conn_cnt );
825 0 : FD_MCNT_ENUM_COPY( SEND, FRAME_TX_ALLOC, ctx->quic->metrics.frame_tx_alloc_cnt );
826 0 : FD_MCNT_SET( SEND, PKT_NET_HEADER_INVALID, ctx->quic->metrics.pkt_net_hdr_err_cnt );
827 0 : FD_MCNT_SET( SEND, PKT_QUIC_HEADER_INVALID, ctx->quic->metrics.pkt_quic_hdr_err_cnt );
828 0 : FD_MCNT_SET( SEND, PKT_UNDERSZ, ctx->quic->metrics.pkt_undersz_cnt );
829 0 : FD_MCNT_SET( SEND, PKT_OVERSZ, ctx->quic->metrics.pkt_oversz_cnt );
830 0 : FD_MCNT_SET( SEND, PKT_VERNEG, ctx->quic->metrics.pkt_verneg_cnt );
831 0 : FD_MCNT_ENUM_COPY( SEND, PKT_RETRANSMISSIONS, ctx->quic->metrics.pkt_retransmissions_cnt );
832 :
833 0 : FD_MCNT_SET( SEND, HANDSHAKES_CREATED, ctx->quic->metrics.hs_created_cnt );
834 0 : FD_MCNT_SET( SEND, HANDSHAKE_ERROR_ALLOC_FAIL, ctx->quic->metrics.hs_err_alloc_fail_cnt );
835 0 : FD_MCNT_SET( SEND, HANDSHAKE_EVICTED, ctx->quic->metrics.hs_evicted_cnt );
836 :
837 0 : FD_MCNT_SET( SEND, FRAME_FAIL_PARSE, ctx->quic->metrics.frame_rx_err_cnt );
838 :
839 0 : FD_MHIST_COPY( SEND, SERVICE_DURATION_SECONDS, ctx->quic->metrics.service_duration );
840 0 : FD_MHIST_COPY( SEND, RECEIVE_DURATION_SECONDS, ctx->quic->metrics.receive_duration );
841 0 : }
842 :
843 :
844 0 : #define STEM_BURST 1UL
845 0 : #define STEM_LAZY (10e3L) /* 10us */
846 :
847 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_send_tile_ctx_t
848 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_send_tile_ctx_t)
849 :
850 0 : #define STEM_CALLBACK_BEFORE_FRAG before_frag
851 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
852 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
853 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
854 0 : #define STEM_CALLBACK_BEFORE_CREDIT before_credit
855 0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
856 : #include "../../disco/stem/fd_stem.c"
857 :
858 : fd_topo_run_tile_t fd_tile_send = {
859 : .name = "send",
860 : .populate_allowed_seccomp = populate_allowed_seccomp,
861 : .populate_allowed_fds = populate_allowed_fds,
862 : .scratch_align = scratch_align,
863 : .scratch_footprint = scratch_footprint,
864 : .privileged_init = privileged_init,
865 : .unprivileged_init = unprivileged_init,
866 : .run = stem_run,
867 : };
|