Line data Source code
1 : #include "../topo/fd_topo.h"
2 : #include "generated/fd_plugin_tile_seccomp.h"
3 : #include "../plugin/fd_plugin.h"
4 : #include "../../flamenco/leaders/fd_leaders_base.h"
5 :
6 0 : #define IN_KIND_REPLAY (0)
7 0 : #define IN_KIND_GOSSIP (1)
8 0 : #define IN_KIND_STAKE (2)
9 0 : #define IN_KIND_POHH (3)
10 0 : #define IN_KIND_VOTE (4)
11 0 : #define IN_KIND_STARTP (5)
12 0 : #define IN_KIND_VOTEL (6)
13 0 : #define IN_KIND_VALCFG (7)
14 :
15 : typedef struct {
16 : fd_wksp_t * mem;
17 : ulong chunk0;
18 : ulong wmark;
19 : ulong mtu;
20 : } fd_plugin_in_ctx_t;
21 :
22 : typedef struct {
23 : int in_kind[ 64UL ];
24 : fd_plugin_in_ctx_t in[ 64UL ];
25 :
26 : fd_wksp_t * out_mem;
27 : ulong out_chunk0;
28 : ulong out_wmark;
29 : ulong out_chunk;
30 :
31 : ulong sz; /* size of payload computed in during_frag and passed to after_frag */
32 : } fd_plugin_ctx_t;
33 :
34 : FD_FN_CONST static inline ulong
35 0 : scratch_align( void ) {
36 0 : return 128UL;
37 0 : }
38 :
39 : FD_FN_PURE static inline ulong
40 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
41 0 : (void)tile;
42 0 : ulong l = FD_LAYOUT_INIT;
43 0 : l = FD_LAYOUT_APPEND( l, alignof( fd_plugin_ctx_t ), sizeof( fd_plugin_ctx_t ) );
44 0 : return FD_LAYOUT_FINI( l, scratch_align() );
45 0 : }
46 :
47 : static inline void
48 : during_frag( fd_plugin_ctx_t * ctx,
49 : ulong in_idx,
50 : ulong seq FD_PARAM_UNUSED,
51 : ulong sig,
52 : ulong chunk,
53 : ulong sz,
54 0 : ulong ctl FD_PARAM_UNUSED ) {
55 :
56 0 : uchar * src = (uchar *)fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
57 0 : ulong * dst = (ulong *)fd_chunk_to_laddr( ctx->out_mem, ctx->out_chunk );
58 :
59 0 : ctx->sz = sz;
60 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP && sig==FD_PLUGIN_MSG_GOSSIP_UPDATE ) ) {
61 0 : ulong peer_cnt = ((ulong *)src)[ 0 ];
62 0 : FD_TEST( peer_cnt<=108000UL );
63 0 : ctx->sz = 8UL + peer_cnt*FD_GOSSIP_LINK_MSG_SIZE;
64 0 : } else if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP || ctx->in_kind[ in_idx ]==IN_KIND_POHH || ctx->in_kind[ in_idx ]==IN_KIND_VOTE ) && FD_LIKELY( sig==FD_PLUGIN_MSG_VOTE_ACCOUNT_UPDATE ) ) {
65 0 : ulong peer_cnt = ((ulong *)src)[ 0 ];
66 0 : FD_TEST( peer_cnt<=108000UL );
67 0 : ctx->sz = 8UL + peer_cnt*112UL;
68 0 : } else if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_STAKE ) ) {
69 0 : ulong staked_cnt = ((ulong *)src)[ 1 ];
70 0 : ulong id_cnt = ((ulong *)src)[ 2 ];
71 0 : FD_TEST( staked_cnt<=MAX_COMPRESSED_STAKE_WEIGHTS );
72 0 : FD_TEST( id_cnt<=MAX_SHRED_DESTS );
73 0 : ctx->sz = fd_stake_weight_msg_sz( staked_cnt, id_cnt );
74 0 : }
75 :
76 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>ctx->in[ in_idx ].mtu ) )
77 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, ctx->sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
78 :
79 0 : fd_memcpy( dst, src, ctx->sz );
80 0 : }
81 :
82 : static inline void
83 : after_frag( fd_plugin_ctx_t * ctx,
84 : ulong in_idx,
85 : ulong seq,
86 : ulong sig,
87 : ulong sz,
88 : ulong tsorig,
89 : ulong tspub,
90 0 : fd_stem_context_t * stem ) {
91 0 : (void)sz;
92 0 : (void)seq;
93 0 : (void)tsorig;
94 0 : (void)tspub;
95 :
96 0 : switch( ctx->in_kind[ in_idx ] ) {
97 0 : case IN_KIND_REPLAY: {
98 0 : FD_TEST( sig==FD_PLUGIN_MSG_SLOT_ROOTED || sig==FD_PLUGIN_MSG_SLOT_OPTIMISTICALLY_CONFIRMED || sig==FD_PLUGIN_MSG_SLOT_COMPLETED || sig==FD_PLUGIN_MSG_SLOT_RESET || sig==FD_PLUGIN_MSG_START_PROGRESS || sig==FD_PLUGIN_MSG_GENESIS_HASH_KNOWN );
99 0 : break;
100 0 : }
101 0 : case IN_KIND_GOSSIP: {
102 0 : FD_TEST( sig==FD_PLUGIN_MSG_GOSSIP_UPDATE || sig==FD_PLUGIN_MSG_VOTE_ACCOUNT_UPDATE || sig==FD_PLUGIN_MSG_BALANCE );
103 0 : break;
104 0 : }
105 0 : case IN_KIND_STAKE: {
106 0 : sig = FD_PLUGIN_MSG_LEADER_SCHEDULE;
107 0 : break;
108 0 : }
109 0 : case IN_KIND_POHH: {
110 0 : FD_TEST( sig==FD_PLUGIN_MSG_SLOT_START || sig==FD_PLUGIN_MSG_SLOT_END );
111 0 : break;
112 0 : }
113 0 : case IN_KIND_VOTE: {
114 0 : FD_TEST( sig==FD_PLUGIN_MSG_VOTE_ACCOUNT_UPDATE );
115 0 : break;
116 0 : }
117 0 : case IN_KIND_STARTP: {
118 0 : FD_TEST( sig==FD_PLUGIN_MSG_START_PROGRESS );
119 0 : break;
120 0 : }
121 0 : case IN_KIND_VOTEL: {
122 0 : FD_TEST( sig==FD_PLUGIN_MSG_SLOT_OPTIMISTICALLY_CONFIRMED );
123 0 : break;
124 0 : }
125 0 : case IN_KIND_VALCFG: {
126 0 : FD_TEST( sig==FD_PLUGIN_MSG_VALIDATOR_INFO );
127 0 : break;
128 0 : }
129 0 : default: FD_LOG_ERR(( "bad in_idx" ));
130 0 : }
131 :
132 0 : fd_stem_publish( stem, 0UL, sig, ctx->out_chunk, ctx->sz, 0UL, 0UL, 0UL );
133 0 : ctx->out_chunk = fd_dcache_compact_next( ctx->out_chunk, ctx->sz, ctx->out_chunk0, ctx->out_wmark );
134 0 : }
135 :
136 : static void
137 : unprivileged_init( fd_topo_t * topo,
138 0 : fd_topo_tile_t * tile ) {
139 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
140 :
141 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
142 0 : fd_plugin_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_plugin_ctx_t ), sizeof( fd_plugin_ctx_t ) );
143 :
144 0 : FD_TEST( tile->in_cnt<=sizeof( ctx->in )/sizeof( ctx->in[ 0 ] ) );
145 0 : for( ulong i=0; i<tile->in_cnt; i++ ) {
146 0 : fd_topo_link_t * link = &topo->links[ tile->in_link_id[ i ] ];
147 0 : fd_topo_wksp_t * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
148 :
149 0 : ctx->in[ i ].mem = link_wksp->wksp;
150 0 : ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
151 0 : ctx->in[ i ].wmark = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
152 0 : ctx->in[ i ].mtu = link->mtu;
153 :
154 0 : FD_TEST( link->mtu<=topo->links[ tile->out_link_id[ 0 ] ].mtu );
155 :
156 0 : if( !strcmp( link->name, "replay_plugi" ) ) ctx->in_kind[ i ] = IN_KIND_REPLAY;
157 0 : else if( !strcmp( link->name, "gossip_plugi" ) ) ctx->in_kind[ i ] = IN_KIND_GOSSIP;
158 0 : else if( !strcmp( link->name, "stake_out" ) ) ctx->in_kind[ i ] = IN_KIND_STAKE;
159 0 : else if( !strcmp( link->name, "pohh_plugin" ) ) ctx->in_kind[ i ] = IN_KIND_POHH;
160 0 : else if( !strcmp( link->name, "votes_plugin" ) ) ctx->in_kind[ i ] = IN_KIND_VOTE;
161 0 : else if( !strcmp( link->name, "startp_plugi" ) ) ctx->in_kind[ i ] = IN_KIND_STARTP;
162 0 : else if( !strcmp( link->name, "votel_plugin" ) ) ctx->in_kind[ i ] = IN_KIND_VOTEL;
163 0 : else if( !strcmp( link->name, "valcfg_plugi" ) ) ctx->in_kind[ i ] = IN_KIND_VALCFG;
164 0 : else FD_LOG_ERR(( "unexpected link name %s", link->name ));
165 0 : }
166 :
167 0 : ctx->out_mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ 0 ] ].dcache_obj_id ].wksp_id ].wksp;
168 0 : ctx->out_chunk0 = fd_dcache_compact_chunk0( ctx->out_mem, topo->links[ tile->out_link_id[ 0 ] ].dcache );
169 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 );
170 0 : ctx->out_chunk = ctx->out_chunk0;
171 :
172 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
173 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
174 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
175 0 : }
176 :
177 : static ulong
178 : populate_allowed_seccomp( fd_topo_t const * topo,
179 : fd_topo_tile_t const * tile,
180 : ulong out_cnt,
181 0 : struct sock_filter * out ) {
182 0 : (void)topo;
183 0 : (void)tile;
184 :
185 0 : populate_sock_filter_policy_fd_plugin_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
186 0 : return sock_filter_policy_fd_plugin_tile_instr_cnt;
187 0 : }
188 :
189 : static ulong
190 : populate_allowed_fds( fd_topo_t const * topo,
191 : fd_topo_tile_t const * tile,
192 : ulong out_fds_cnt,
193 0 : int * out_fds ) {
194 0 : (void)topo;
195 0 : (void)tile;
196 :
197 0 : if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
198 :
199 0 : ulong out_cnt = 0UL;
200 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
201 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
202 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
203 0 : return out_cnt;
204 0 : }
205 :
206 0 : #define STEM_BURST (1UL)
207 :
208 : /* See explanation in fd_pack */
209 0 : #define STEM_LAZY (128L*3000L)
210 :
211 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_plugin_ctx_t
212 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_plugin_ctx_t)
213 :
214 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
215 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
216 :
217 : #include "../stem/fd_stem.c"
218 :
219 : fd_topo_run_tile_t fd_tile_plugin = {
220 : .name = "plugin",
221 : .populate_allowed_seccomp = populate_allowed_seccomp,
222 : .populate_allowed_fds = populate_allowed_fds,
223 : .scratch_align = scratch_align,
224 : .scratch_footprint = scratch_footprint,
225 : .unprivileged_init = unprivileged_init,
226 : .run = stem_run,
227 : };
|