Line data Source code
1 : #include "fd_ipecho_client.h"
2 : #include "fd_ipecho_server.h"
3 : #include "../genesis/fd_genesi_tile.h"
4 : #include "../genesis/genesis_hash.h"
5 : #include "../../disco/topo/fd_topo.h"
6 : #include "../../disco/metrics/fd_metrics.h"
7 : #include "../../ballet/lthash/fd_lthash.h"
8 :
9 : #include <netinet/in.h>
10 : #include <sys/socket.h>
11 : #include <sys/poll.h>
12 :
13 : #include "generated/fd_ipecho_tile_seccomp.h"
14 :
15 : struct fd_ipecho_tile_ctx {
16 : int retrieving;
17 :
18 : fd_ipecho_server_t * server;
19 : fd_ipecho_client_t * client;
20 :
21 : uint bind_address;
22 : ushort bind_port;
23 :
24 : ushort expected_shred_version;
25 :
26 : fd_wksp_t * genesi_in_mem;
27 : ulong genesi_in_chunk0;
28 : ulong genesi_in_wmark;
29 : };
30 :
31 : typedef struct fd_ipecho_tile_ctx fd_ipecho_tile_ctx_t;
32 :
33 : FD_FN_CONST static inline ulong
34 0 : scratch_align( void ) {
35 0 : return alignof( fd_ipecho_tile_ctx_t );
36 0 : }
37 :
38 : FD_FN_PURE static inline ulong
39 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
40 0 : (void)tile;
41 :
42 0 : ulong l = FD_LAYOUT_INIT;
43 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_ipecho_tile_ctx_t), sizeof(fd_ipecho_tile_ctx_t) );
44 0 : l = FD_LAYOUT_APPEND( l, fd_ipecho_client_align(), fd_ipecho_client_footprint() );
45 0 : l = FD_LAYOUT_APPEND( l, fd_ipecho_server_align(), fd_ipecho_server_footprint( 1024UL ) );
46 0 : return FD_LAYOUT_FINI( l, scratch_align() );
47 0 : }
48 :
49 : static inline void
50 0 : metrics_write( fd_ipecho_tile_ctx_t * ctx ) {
51 0 : fd_ipecho_server_metrics_t * metrics = fd_ipecho_server_metrics( ctx->server );
52 :
53 0 : FD_MGAUGE_SET( IPECHO, CONNECTION_COUNT, metrics->connection_cnt );
54 0 : FD_MCNT_SET( IPECHO, BYTES_READ, metrics->bytes_read );
55 0 : FD_MCNT_SET( IPECHO, BYTES_WRITTEN, metrics->bytes_written );
56 0 : FD_MCNT_SET( IPECHO, CONNECTIONS_CLOSED_OK, metrics->connections_closed_ok );
57 0 : FD_MCNT_SET( IPECHO, CONNECTIONS_CLOSED_ERROR, metrics->connections_closed_error );
58 0 : }
59 :
60 : static inline void
61 : poll_client( fd_ipecho_tile_ctx_t * ctx,
62 : fd_stem_context_t * stem,
63 0 : int * charge_busy ) {
64 0 : if( FD_UNLIKELY( !ctx->client ) ) return;
65 :
66 0 : ushort shred_version;
67 0 : int result = fd_ipecho_client_poll( ctx->client, &shred_version, charge_busy );
68 0 : if( FD_UNLIKELY( !result ) ) {
69 0 : if( FD_UNLIKELY( ctx->expected_shred_version && ctx->expected_shred_version!=shred_version ) ) {
70 0 : FD_LOG_ERR(( "Expected shred version %hu but entrypoint returned %hu",
71 0 : ctx->expected_shred_version, shred_version ));
72 0 : }
73 :
74 0 : FD_LOG_INFO(( "retrieved shred version %hu from entrypoint", shred_version ));
75 0 : FD_MGAUGE_SET( IPECHO, CURRENT_SHRED_VERSION, shred_version );
76 0 : fd_stem_publish( stem, 0UL, shred_version, 0UL, 0UL, 0UL, 0UL, 0UL );
77 0 : fd_ipecho_server_set_shred_version( ctx->server, shred_version );
78 0 : ctx->retrieving = 0;
79 0 : return;
80 0 : } else if( FD_UNLIKELY( -1==result ) ) {
81 0 : FD_LOG_ERR(( "Could not determine shred version from entrypoints. Please "
82 0 : "check you can connect to the entrypoints provided." ));
83 0 : }
84 0 : }
85 :
86 : static inline void
87 : after_credit( fd_ipecho_tile_ctx_t * ctx,
88 : fd_stem_context_t * stem,
89 : int * opt_poll_in,
90 0 : int * charge_busy ) {
91 0 : (void)opt_poll_in;
92 :
93 : /* 1ms timeout is sufficient here. Large timeouts (e.g. 10ms) can
94 : cause housekeeping tasks to run very infrequently (> 0.05Hz) since
95 : they incur a prolonged context switch every single iteration of the
96 : STEM loop. */
97 0 : int timeout = ctx->retrieving ? 0 : 1;
98 :
99 0 : if( FD_UNLIKELY( ctx->retrieving ) ) poll_client( ctx, stem, charge_busy );
100 0 : else fd_ipecho_server_poll( ctx->server, charge_busy, timeout );
101 0 : }
102 :
103 : static inline int
104 : returnable_frag( fd_ipecho_tile_ctx_t * ctx,
105 : ulong in_idx,
106 : ulong seq,
107 : ulong sig,
108 : ulong chunk,
109 : ulong sz,
110 : ulong ctl,
111 : ulong tsorig,
112 : ulong tspub,
113 0 : fd_stem_context_t * stem ) {
114 0 : (void)in_idx; (void)seq; (void)sig; (void)sz; (void)ctl; (void)tspub;
115 0 : fd_genesis_meta_t const * genesis_meta = fd_chunk_to_laddr( ctx->genesi_in_mem, chunk );
116 :
117 0 : if( FD_UNLIKELY( genesis_meta->bootstrap ) ) {
118 0 : ushort shred_version = compute_shred_version( genesis_meta->genesis_hash.uc, NULL, NULL, 0UL );
119 0 : FD_TEST( shred_version );
120 :
121 0 : FD_MGAUGE_SET( IPECHO, CURRENT_SHRED_VERSION, shred_version );
122 0 : fd_stem_publish( stem, 0UL, shred_version, 0UL, 0UL, 0UL, tsorig, fd_frag_meta_ts_comp( fd_tickcount() ) );
123 0 : fd_ipecho_server_set_shred_version( ctx->server, shred_version );
124 0 : ctx->retrieving = 0;
125 0 : }
126 :
127 0 : return 0;
128 0 : }
129 :
130 : static void
131 : privileged_init( fd_topo_t * topo,
132 0 : fd_topo_tile_t * tile ) {
133 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
134 :
135 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
136 0 : fd_ipecho_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_ipecho_tile_ctx_t ), sizeof( fd_ipecho_tile_ctx_t ) );
137 0 : void * _client = FD_SCRATCH_ALLOC_APPEND( l, fd_ipecho_client_align(), fd_ipecho_client_footprint() );
138 0 : void * _server = FD_SCRATCH_ALLOC_APPEND( l, fd_ipecho_server_align(), fd_ipecho_server_footprint( 1024UL ) );
139 :
140 0 : ctx->bind_address = tile->ipecho.bind_address;
141 0 : ctx->bind_port = tile->ipecho.bind_port;
142 :
143 0 : ctx->expected_shred_version = tile->ipecho.expected_shred_version;
144 :
145 0 : ctx->retrieving = 1;
146 0 : if( FD_LIKELY( tile->ipecho.entrypoints_cnt ) ) {
147 0 : ctx->client = fd_ipecho_client_join( fd_ipecho_client_new( _client ) );
148 0 : FD_TEST( ctx->client );
149 0 : fd_ipecho_client_init( ctx->client, tile->ipecho.entrypoints, tile->ipecho.entrypoints_cnt );
150 0 : } else {
151 0 : ctx->client = NULL;
152 0 : }
153 :
154 0 : ctx->server = fd_ipecho_server_join( fd_ipecho_server_new( _server, 1024UL ) );
155 0 : FD_TEST( ctx->server );
156 0 : fd_ipecho_server_init( ctx->server, ctx->bind_address, ctx->bind_port, ctx->expected_shred_version );
157 :
158 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
159 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
160 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
161 0 : }
162 :
163 : static void
164 : unprivileged_init( fd_topo_t * topo,
165 0 : fd_topo_tile_t * tile ) {
166 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
167 :
168 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
169 0 : fd_ipecho_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_ipecho_tile_ctx_t ), sizeof( fd_ipecho_tile_ctx_t ) );
170 :
171 0 : FD_MGAUGE_SET( IPECHO, CURRENT_SHRED_VERSION, tile->ipecho.expected_shred_version );
172 :
173 : /* In some topologies (e.g. firedancer-dev gossip), the ipecho tile
174 : has no input links. Guard against dereferencing a missing
175 : link/dcache. */
176 0 : if( FD_LIKELY( tile->in_cnt>0UL ) ) {
177 0 : ulong link_id = tile->in_link_id[ 0UL ];
178 0 : void * dcache = topo->links[ link_id ].dcache;
179 0 : ctx->genesi_in_mem = topo->workspaces[ topo->objs[ topo->links[ link_id ].dcache_obj_id ].wksp_id ].wksp;
180 0 : ctx->genesi_in_chunk0 = fd_dcache_compact_chunk0( ctx->genesi_in_mem, dcache );
181 0 : ctx->genesi_in_wmark = fd_dcache_compact_wmark ( ctx->genesi_in_mem, dcache, topo->links[ link_id ].mtu );
182 0 : } else {
183 0 : ctx->genesi_in_mem = NULL;
184 0 : ctx->genesi_in_chunk0 = 0UL;
185 0 : ctx->genesi_in_wmark = 0UL;
186 0 : }
187 0 : }
188 :
189 : static ulong
190 : rlimit_file_cnt( fd_topo_t const * topo FD_PARAM_UNUSED,
191 0 : fd_topo_tile_t const * tile ) {
192 : /* stderr, logfile, one for each socket() call for up to 16
193 : gossip entrypoints (GOSSIP_TILE_ENTRYPOINTS_MAX) for
194 : fd_ipecho_client, one for fd_ipecho_server, and up to 1024 for the
195 : server's connections. */
196 0 : return 1UL + /* stderr */
197 0 : 1UL + /* logfile */
198 0 : tile->ipecho.entrypoints_cnt + /* for the client */
199 0 : 1UL + /* for the server's socket */
200 0 : 1024UL; /* for the server's connections */;
201 0 : }
202 :
203 : static ulong
204 : populate_allowed_seccomp( fd_topo_t const * topo,
205 : fd_topo_tile_t const * tile,
206 : ulong out_cnt,
207 0 : struct sock_filter * out ) {
208 0 : (void)topo;
209 0 : (void)tile;
210 :
211 0 : populate_sock_filter_policy_fd_ipecho_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
212 0 : return sock_filter_policy_fd_ipecho_tile_instr_cnt;
213 0 : }
214 :
215 : static ulong
216 : populate_allowed_fds( fd_topo_t const * topo,
217 : fd_topo_tile_t const * tile,
218 : ulong out_fds_cnt,
219 0 : int * out_fds ) {
220 :
221 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
222 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
223 0 : fd_ipecho_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_ipecho_tile_ctx_t), sizeof(fd_ipecho_tile_ctx_t) );
224 :
225 0 : if( FD_UNLIKELY( out_fds_cnt<3UL+tile->ipecho.entrypoints_cnt ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
226 :
227 0 : ulong out_cnt = 0UL;
228 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
229 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
230 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
231 :
232 : /* All of the fds managed by the client. */
233 0 : for( ulong i=0UL; i<tile->ipecho.entrypoints_cnt; i++ ) {
234 0 : int fd = fd_ipecho_client_get_pollfds( ctx->client )[ i ].fd;
235 0 : if( FD_LIKELY( fd!=-1 ) ) out_fds[ out_cnt++ ] = fd;
236 0 : }
237 :
238 : /* The server's socket. */
239 0 : out_fds[ out_cnt++ ] = fd_ipecho_server_sockfd( ctx->server );
240 0 : return out_cnt;
241 0 : }
242 :
243 0 : #define STEM_BURST (2UL)
244 0 : #define STEM_LAZY (50UL)
245 :
246 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_ipecho_tile_ctx_t
247 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_ipecho_tile_ctx_t)
248 :
249 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
250 0 : #define STEM_CALLBACK_AFTER_CREDIT after_credit
251 0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
252 :
253 : #include "../../disco/stem/fd_stem.c"
254 :
255 : fd_topo_run_tile_t fd_tile_ipecho = {
256 : .name = "ipecho",
257 : .rlimit_file_cnt_fn = rlimit_file_cnt,
258 : .populate_allowed_seccomp = populate_allowed_seccomp,
259 : .populate_allowed_fds = populate_allowed_fds,
260 : .scratch_align = scratch_align,
261 : .scratch_footprint = scratch_footprint,
262 : .privileged_init = privileged_init,
263 : .unprivileged_init = unprivileged_init,
264 : .run = stem_run,
265 : .allow_connect = 1,
266 : .keep_host_networking = 1
267 : };
|