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