Line data Source code
1 : #include "fd_verify_tile.h"
2 : #include "../fd_txn_m.h"
3 : #include "../metrics/fd_metrics.h"
4 : #include <linux/futex.h>
5 : #include "generated/fd_verify_tile_seccomp.h"
6 : #include "../../flamenco/gossip/fd_gossip_message.h"
7 :
8 3 : #define IN_KIND_QUIC (0UL)
9 87 : #define IN_KIND_BUNDLE (1UL)
10 3 : #define IN_KIND_GOSSIP (2UL)
11 3 : #define IN_KIND_TXSEND (3UL)
12 :
13 : FD_FN_CONST static inline ulong
14 21 : scratch_align( void ) {
15 21 : return FD_TCACHE_ALIGN;
16 21 : }
17 :
18 : FD_FN_PURE static inline ulong
19 6 : scratch_footprint( fd_topo_tile_t const * tile ) {
20 6 : ulong l = FD_LAYOUT_INIT;
21 6 : l = FD_LAYOUT_APPEND( l, alignof( fd_verify_ctx_t ), sizeof( fd_verify_ctx_t ) );
22 6 : l = FD_LAYOUT_APPEND( l, fd_tcache_align(), fd_tcache_footprint( tile->verify.tcache_depth, 0UL ) );
23 78 : for( ulong i=0; i<FD_TXN_SIG_MAX; i++ ) {
24 72 : l = FD_LAYOUT_APPEND( l, fd_sha512_align(), fd_sha512_footprint() );
25 72 : }
26 6 : l = FD_LAYOUT_APPEND( l, fd_ed25519_cache_align(), fd_ed25519_cache_footprint( FD_VERIFY_ED25519_CACHE_ENT_CNT ) );
27 6 : return FD_LAYOUT_FINI( l, scratch_align() );
28 6 : }
29 :
30 : static inline void
31 0 : metrics_write( fd_verify_ctx_t * ctx ) {
32 0 : FD_MCNT_ENUM_COPY( VERIFY, TXN_PROCESSED, ctx->metrics.verify_tile_result );
33 0 : FD_MCNT_SET ( VERIFY, VOTE_GOSSIP_RX, ctx->metrics.gossiped_votes_cnt );
34 0 : FD_MCNT_SET ( VERIFY, OWN_VOTE_RX, ctx->metrics.own_votes_cnt );
35 0 : }
36 :
37 : static int
38 : before_frag( fd_verify_ctx_t * ctx,
39 : ulong in_idx,
40 : ulong seq,
41 42 : ulong sig ) {
42 : /* Bundle tile can produce both "bundles" and "packets", a packet is a
43 : regular transaction and should be round-robined between verify
44 : tiles, while bundles need to go through verify:0 currently to
45 : prevent interleaving of bundle streams. */
46 42 : int is_bundle_packet = (ctx->in_kind[ in_idx ]==IN_KIND_BUNDLE && !sig);
47 :
48 42 : if( FD_LIKELY( is_bundle_packet || ctx->in_kind[ in_idx ]==IN_KIND_QUIC ) ) {
49 30 : return (seq % ctx->round_robin_cnt) != ctx->round_robin_idx;
50 30 : } else if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_BUNDLE ) ) {
51 12 : return ctx->round_robin_idx!=0UL;
52 12 : } else if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP ) ) {
53 0 : return (seq % ctx->round_robin_cnt) != ctx->round_robin_idx ||
54 0 : sig!=FD_GOSSIP_UPDATE_TAG_VOTE;
55 0 : }
56 :
57 0 : return 0;
58 42 : }
59 :
60 : /* during_frag is called between pairs for sequence number checks, as
61 : we are reading incoming frags. We don't actually need to copy the
62 : fragment here, see fd_dedup.c for why we do this.*/
63 :
64 : static inline void
65 : during_frag( fd_verify_ctx_t * ctx,
66 : ulong in_idx,
67 : ulong seq FD_PARAM_UNUSED,
68 : ulong sig FD_PARAM_UNUSED,
69 : ulong chunk,
70 : ulong sz,
71 0 : ulong ctl FD_PARAM_UNUSED ) {
72 :
73 0 : ulong in_kind = ctx->in_kind[ in_idx ];
74 0 : if( FD_UNLIKELY( in_kind==IN_KIND_BUNDLE || in_kind==IN_KIND_QUIC || in_kind==IN_KIND_TXSEND ) ) {
75 0 : if( FD_UNLIKELY( chunk<ctx->in[in_idx].chunk0 || chunk>ctx->in[in_idx].wmark || sz>FD_TPU_RAW_MTU ) )
76 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu,%lu]", chunk, sz, ctx->in[in_idx].chunk0, ctx->in[in_idx].wmark, FD_TPU_RAW_MTU ));
77 :
78 0 : uchar * src = fd_chunk_to_laddr( ctx->in[in_idx].mem, chunk );
79 0 : uchar * dst = fd_chunk_to_laddr( ctx->out_mem, ctx->out_chunk );
80 0 : fd_memcpy( dst, src, sz );
81 0 : } else if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP ) ) {
82 0 : if( FD_UNLIKELY( chunk<ctx->in[in_idx].chunk0 || chunk>ctx->in[in_idx].wmark || sz>2048UL ) )
83 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 ));
84 :
85 0 : fd_gossip_update_message_t const * msg = fd_chunk_to_laddr_const( ctx->in[in_idx].mem, chunk );
86 0 : fd_txn_m_t * dst = fd_chunk_to_laddr( ctx->out_mem, ctx->out_chunk );
87 :
88 0 : dst->payload_sz = (ushort)msg->vote->value->transaction_len;
89 0 : dst->block_engine.bundle_id = 0UL;
90 0 : dst->first_seen_nanos = msg->vote->first_seen_nanos;
91 0 : dst->source_ipv4 = msg->vote->socket->is_ipv6 ? 0U : msg->vote->socket->ip4;
92 0 : dst->source_tpu = FD_TXN_M_TPU_SOURCE_GOSSIP;
93 0 : fd_memcpy( fd_txn_m_payload( dst ), msg->vote->value->transaction, msg->vote->value->transaction_len );
94 0 : }
95 0 : }
96 :
97 : static inline void
98 : after_frag( fd_verify_ctx_t * ctx,
99 : ulong in_idx,
100 : ulong seq,
101 : ulong sig,
102 : ulong sz,
103 : ulong tsorig,
104 : ulong _tspub,
105 0 : fd_stem_context_t * stem ) {
106 0 : (void)in_idx;
107 0 : (void)seq;
108 0 : (void)sig;
109 0 : (void)sz;
110 0 : (void)_tspub;
111 :
112 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP ) ) ctx->metrics.gossiped_votes_cnt++;
113 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_TXSEND ) ) ctx->metrics.own_votes_cnt++;
114 :
115 0 : fd_txn_m_t * txnm = (fd_txn_m_t *)fd_chunk_to_laddr( ctx->out_mem, ctx->out_chunk );
116 0 : if( FD_UNLIKELY( txnm->payload_sz>FD_TPU_MTU ) ) {
117 0 : FD_LOG_ERR(( "verify: txn payload size %hu exceeds max %lu", txnm->payload_sz, FD_TPU_MTU ));
118 0 : }
119 0 : fd_txn_t * txnt = fd_txn_m_txn_t( txnm );
120 0 : txnm->txn_t_sz = (ushort)fd_txn_parse( fd_txn_m_payload( txnm ), txnm->payload_sz, txnt, NULL );
121 :
122 0 : int is_bundle = !!txnm->block_engine.bundle_id;
123 :
124 0 : if( FD_UNLIKELY( is_bundle & (txnm->block_engine.bundle_id!=ctx->bundle_id) ) ) {
125 0 : ctx->bundle_failed = 0;
126 0 : ctx->bundle_id = txnm->block_engine.bundle_id;
127 0 : }
128 :
129 0 : if( FD_UNLIKELY( is_bundle & (!!ctx->bundle_failed) ) ) {
130 0 : ctx->metrics.verify_tile_result[ FD_METRICS_ENUM_VERIFY_RESULT_V_BUNDLE_PEER_FAILED_IDX ]++;
131 0 : return;
132 0 : }
133 :
134 0 : if( FD_UNLIKELY( !txnm->txn_t_sz ) ) {
135 0 : if( FD_UNLIKELY( is_bundle ) ) ctx->bundle_failed = 1;
136 0 : ctx->metrics.verify_tile_result[ FD_METRICS_ENUM_VERIFY_RESULT_V_PARSE_FAILED_IDX ]++;
137 0 : return;
138 0 : }
139 :
140 : /* Users sometimes send transactions as part of a bundle (with a tip)
141 : and via the normal path (without a tip). Regardless of which
142 : arrives first, we want to pack the one with the tip. Thus, we
143 : exempt bundles from the normal HA dedup checks. The dedup tile
144 : will still do a full-bundle dedup check to make sure to drop any
145 : identical bundles. */
146 0 : ulong _txn_sig;
147 0 : int res = fd_txn_verify( ctx, fd_txn_m_payload( txnm ), txnm->payload_sz, txnt, !is_bundle, &_txn_sig );
148 0 : if( FD_UNLIKELY( res!=FD_TXN_VERIFY_SUCCESS ) ) {
149 0 : if( FD_UNLIKELY( is_bundle ) ) ctx->bundle_failed = 1;
150 :
151 0 : if( FD_LIKELY( res==FD_TXN_VERIFY_DEDUP ) ) ctx->metrics.verify_tile_result[ FD_METRICS_ENUM_VERIFY_RESULT_V_DUPLICATE_IDX ]++;
152 0 : else ctx->metrics.verify_tile_result[ FD_METRICS_ENUM_VERIFY_RESULT_V_SIGNATURE_FAILED_IDX ]++;
153 :
154 0 : return;
155 0 : }
156 :
157 0 : ulong realized_sz = fd_txn_m_realized_footprint( txnm, 1, 0 );
158 0 : ulong tspub = (ulong)fd_frag_meta_ts_comp( fd_tickcount() );
159 0 : fd_stem_publish( stem, 0UL, 0UL, ctx->out_chunk, realized_sz, 0UL, tsorig, tspub );
160 0 : ctx->out_chunk = fd_dcache_compact_next( ctx->out_chunk, realized_sz, ctx->out_chunk0, ctx->out_wmark );
161 :
162 0 : ctx->metrics.verify_tile_result[ FD_METRICS_ENUM_VERIFY_RESULT_V_SUCCESS_IDX ]++;
163 0 : }
164 :
165 : static void
166 : privileged_init( fd_topo_t const * topo,
167 3 : fd_topo_tile_t const * tile ) {
168 3 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
169 :
170 3 : FD_SCRATCH_ALLOC_INIT( l, scratch );
171 3 : fd_verify_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_verify_ctx_t ), sizeof( fd_verify_ctx_t ) );
172 3 : FD_TEST( fd_rng_secure( &ctx->hashmap_seed, 8U ) );
173 3 : }
174 :
175 : static void
176 : unprivileged_init( fd_topo_t const * topo,
177 3 : fd_topo_tile_t const * tile ) {
178 3 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
179 :
180 3 : FD_SCRATCH_ALLOC_INIT( l, scratch );
181 3 : fd_verify_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_verify_ctx_t ), sizeof( fd_verify_ctx_t ) );
182 3 : fd_tcache_t * tcache = fd_tcache_join( fd_tcache_new( FD_SCRATCH_ALLOC_APPEND( l, fd_tcache_align(), fd_tcache_footprint( tile->verify.tcache_depth, 0UL ) ), tile->verify.tcache_depth, 0UL ) );
183 3 : if( FD_UNLIKELY( !tcache ) ) FD_LOG_ERR(( "fd_tcache_join failed" ));
184 :
185 3 : ctx->round_robin_cnt = fd_topo_tile_name_cnt( topo, tile->name );
186 3 : ctx->round_robin_idx = tile->kind_id;
187 :
188 39 : for ( ulong i=0; i<FD_TXN_SIG_MAX; i++ ) {
189 36 : fd_sha512_t * sha = fd_sha512_join( fd_sha512_new( FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_sha512_t ), sizeof( fd_sha512_t ) ) ) );
190 36 : if( FD_UNLIKELY( !sha ) ) FD_LOG_ERR(( "fd_sha512_join failed" ));
191 36 : ctx->sha[i] = sha;
192 36 : }
193 :
194 3 : void * _ed25519_cache = FD_SCRATCH_ALLOC_APPEND( l, fd_ed25519_cache_align(), fd_ed25519_cache_footprint( FD_VERIFY_ED25519_CACHE_ENT_CNT ) );
195 3 : ctx->ed25519_cache = fd_ed25519_cache_join( fd_ed25519_cache_new( _ed25519_cache, FD_VERIFY_ED25519_CACHE_ENT_CNT, ctx->hashmap_seed ) );
196 3 : FD_TEST( ctx->ed25519_cache );
197 :
198 3 : ctx->bundle_failed = 0;
199 3 : ctx->bundle_id = 0UL;
200 :
201 3 : memset( &ctx->metrics, 0, sizeof( ctx->metrics ) );
202 :
203 3 : ctx->tcache_depth = fd_tcache_depth ( tcache );
204 3 : ctx->tcache_map_cnt = fd_tcache_map_cnt ( tcache );
205 3 : ctx->tcache_sync = fd_tcache_oldest_laddr( tcache );
206 3 : ctx->tcache_ring = fd_tcache_ring_laddr ( tcache );
207 3 : ctx->tcache_map = fd_tcache_map_laddr ( tcache );
208 :
209 3 : FD_CHECK_ERR( tile->in_cnt<=sizeof(ctx->in)/sizeof(ctx->in[0]), "too many input links" );
210 :
211 15 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
212 12 : fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
213 :
214 12 : fd_topo_wksp_t const * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
215 12 : ctx->in[i].mem = link_wksp->wksp;
216 12 : ctx->in[i].chunk0 = fd_dcache_compact_chunk0( ctx->in[i].mem, link->dcache );
217 12 : ctx->in[i].wmark = fd_dcache_compact_wmark ( ctx->in[i].mem, link->dcache, link->mtu );
218 :
219 12 : if( !strcmp( link->name, "quic_verify" ) ) ctx->in_kind[ i ] = IN_KIND_QUIC;
220 9 : else if( !strcmp( link->name, "bundle_verif" ) ) ctx->in_kind[ i ] = IN_KIND_BUNDLE;
221 6 : else if( !strcmp( link->name, "txsend_out" ) ) ctx->in_kind[ i ] = IN_KIND_TXSEND;
222 3 : else if( !strcmp( link->name, "gossip_vote" ) ) ctx->in_kind[ i ] = IN_KIND_GOSSIP;
223 0 : else FD_LOG_ERR(( "unexpected link name %s", link->name ));
224 12 : }
225 :
226 3 : ctx->out_mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ 0 ] ].dcache_obj_id ].wksp_id ].wksp;
227 3 : ctx->out_chunk0 = fd_dcache_compact_chunk0( ctx->out_mem, topo->links[ tile->out_link_id[ 0 ] ].dcache );
228 3 : ctx->out_wmark = fd_dcache_compact_wmark ( ctx->out_mem, topo->links[ tile->out_link_id[ 0 ] ].dcache, topo->links[ tile->out_link_id[ 0 ] ].mtu );
229 3 : ctx->out_chunk = ctx->out_chunk0;
230 :
231 3 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
232 3 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
233 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
234 3 : }
235 :
236 : static ulong
237 : populate_allowed_seccomp( fd_topo_t const * topo,
238 : fd_topo_tile_t const * tile,
239 : ulong out_cnt,
240 3 : struct sock_filter * out ) {
241 3 : (void)topo;
242 3 : (void)tile;
243 :
244 3 : populate_sock_filter_policy_fd_verify_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
245 3 : return sock_filter_policy_fd_verify_tile_instr_cnt;
246 3 : }
247 :
248 : static ulong
249 : populate_allowed_fds( fd_topo_t const * topo,
250 : fd_topo_tile_t const * tile,
251 : ulong out_fds_cnt,
252 3 : int * out_fds ) {
253 3 : (void)topo;
254 3 : (void)tile;
255 :
256 3 : if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
257 :
258 3 : ulong out_cnt = 0UL;
259 3 : out_fds[ out_cnt++ ] = 2; /* stderr */
260 3 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
261 3 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
262 3 : return out_cnt;
263 3 : }
264 :
265 0 : #define STEM_BURST (1UL)
266 :
267 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_verify_ctx_t
268 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_verify_ctx_t)
269 :
270 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
271 0 : #define STEM_CALLBACK_BEFORE_FRAG before_frag
272 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
273 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
274 :
275 : #include "../stem/fd_stem.c"
276 :
277 : #ifndef FD_TILE_TEST
278 : fd_topo_run_tile_t fd_tile_verify = {
279 : .name = "verify",
280 : .populate_allowed_seccomp = populate_allowed_seccomp,
281 : .populate_allowed_fds = populate_allowed_fds,
282 : .scratch_align = scratch_align,
283 : .scratch_footprint = scratch_footprint,
284 : .privileged_init = privileged_init,
285 : .unprivileged_init = unprivileged_init,
286 : .run = stem_run,
287 : };
288 : #endif
|