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 "../../disco/topo/fd_topo.h"
5 : #include "../../disco/metrics/fd_metrics.h"
6 : #include "../../ballet/lthash/fd_lthash.h"
7 :
8 : #include <netinet/in.h>
9 : #include <sys/socket.h>
10 : #include <sys/poll.h>
11 :
12 : #include "generated/fd_ipecho_tile_seccomp.h"
13 :
14 : struct fd_ipecho_tile_ctx {
15 : int retrieving;
16 :
17 : fd_ipecho_server_t * server;
18 : fd_ipecho_client_t * client;
19 :
20 : uint bind_address;
21 : ushort bind_port;
22 :
23 : ushort bootstrap_shred_version;
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, 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 0 : int timeout = ctx->retrieving ? 0 : 10;
94 :
95 0 : if( FD_UNLIKELY( ctx->retrieving ) ) poll_client( ctx, stem, charge_busy );
96 0 : else fd_ipecho_server_poll( ctx->server, charge_busy, timeout );
97 0 : }
98 :
99 : static inline int
100 : returnable_frag( fd_ipecho_tile_ctx_t * ctx,
101 : ulong in_idx,
102 : ulong seq,
103 : ulong sig,
104 : ulong chunk,
105 : ulong sz,
106 : ulong ctl,
107 : ulong tsorig,
108 : ulong tspub,
109 0 : fd_stem_context_t * stem ) {
110 0 : (void)in_idx; (void)seq; (void)sig; (void)chunk; (void)sz; (void)ctl; (void)tsorig; (void)tspub;
111 :
112 0 : if( FD_UNLIKELY( sig==GENESI_SIG_BOOTSTRAP_COMPLETED ) ) {
113 0 : uchar const * src = fd_chunk_to_laddr_const( ctx->genesi_in_mem, chunk );
114 :
115 0 : union {
116 0 : uchar c[ 32 ];
117 0 : ushort s[ 16 ];
118 0 : } hash;
119 :
120 0 : fd_memcpy( hash.c, src+sizeof(fd_lthash_value_t), sizeof(fd_hash_t) );
121 :
122 0 : ushort xor = 0;
123 0 : for( ulong i=0UL; i<16UL; i++ ) xor ^= hash.s[ i ];
124 :
125 0 : xor = fd_ushort_bswap( xor );
126 0 : xor = fd_ushort_if( xor<USHORT_MAX, (ushort)(xor + 1), USHORT_MAX );
127 :
128 0 : FD_TEST( xor );
129 :
130 0 : FD_MGAUGE_SET( IPECHO, SHRED_VERSION, xor );
131 0 : fd_stem_publish( stem, 0UL, xor, 0UL, 0UL, 0UL, tsorig, fd_frag_meta_ts_comp( fd_tickcount() ) );
132 0 : fd_ipecho_server_set_shred_version( ctx->server, xor );
133 0 : ctx->retrieving = 0;
134 0 : }
135 :
136 0 : return 0;
137 0 : }
138 :
139 : static void
140 : privileged_init( fd_topo_t * topo,
141 0 : fd_topo_tile_t * tile ) {
142 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
143 :
144 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
145 0 : fd_ipecho_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_ipecho_tile_ctx_t ), sizeof( fd_ipecho_tile_ctx_t ) );
146 0 : void * _client = FD_SCRATCH_ALLOC_APPEND( l, fd_ipecho_client_align(), fd_ipecho_client_footprint() );
147 0 : void * _server = FD_SCRATCH_ALLOC_APPEND( l, fd_ipecho_server_align(), fd_ipecho_server_footprint( 1024UL ) );
148 :
149 0 : ctx->bind_address = tile->ipecho.bind_address;
150 0 : ctx->bind_port = tile->ipecho.bind_port;
151 :
152 0 : ctx->expected_shred_version = tile->ipecho.expected_shred_version;
153 :
154 0 : ctx->retrieving = 1;
155 0 : if( FD_LIKELY( tile->ipecho.entrypoints_cnt ) ) {
156 0 : ctx->client = fd_ipecho_client_join( fd_ipecho_client_new( _client ) );
157 0 : FD_TEST( ctx->client );
158 0 : fd_ipecho_client_init( ctx->client, tile->ipecho.entrypoints, tile->ipecho.entrypoints_cnt );
159 0 : } else {
160 0 : ctx->client = NULL;
161 0 : }
162 :
163 0 : ctx->server = fd_ipecho_server_join( fd_ipecho_server_new( _server, 1024UL ) );
164 0 : FD_TEST( ctx->server );
165 0 : fd_ipecho_server_init( ctx->server, ctx->bind_address, ctx->bind_port, ctx->expected_shred_version );
166 :
167 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
168 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
169 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
170 0 : }
171 :
172 : static void
173 : unprivileged_init( fd_topo_t * topo,
174 0 : fd_topo_tile_t * tile ) {
175 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
176 :
177 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
178 0 : fd_ipecho_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_ipecho_tile_ctx_t ), sizeof( fd_ipecho_tile_ctx_t ) );
179 :
180 0 : FD_MGAUGE_SET( IPECHO, SHRED_VERSION, tile->ipecho.expected_shred_version );
181 :
182 0 : ctx->genesi_in_mem = topo->workspaces[ topo->objs[ topo->links[ tile->in_link_id[ 0UL ] ].dcache_obj_id ].wksp_id ].wksp;
183 0 : ctx->genesi_in_chunk0 = fd_dcache_compact_chunk0( ctx->genesi_in_mem, topo->links[ tile->in_link_id[ 0UL ] ].dcache );
184 0 : ctx->genesi_in_wmark = fd_dcache_compact_wmark ( ctx->genesi_in_mem, topo->links[ tile->in_link_id[ 0UL ] ].dcache, topo->links[ tile->in_link_id[ 0UL ] ].mtu );
185 0 : }
186 :
187 : static ulong
188 : rlimit_file_cnt( fd_topo_t const * topo FD_PARAM_UNUSED,
189 0 : fd_topo_tile_t const * tile ) {
190 : /* stderr, logfile, one for each socket() call for up to 16
191 : gossip entrypoints (GOSSIP_TILE_ENTRYPOINTS_MAX) for
192 : fd_ipecho_client, one for fd_ipecho_server, and up to 1024 for the
193 : server's connections. */
194 0 : return 1UL + /* stderr */
195 0 : 1UL + /* logfile */
196 0 : tile->ipecho.entrypoints_cnt + /* for the client */
197 0 : 1UL + /* for the server's socket */
198 0 : 1024UL; /* for the server's connections */;
199 0 : }
200 :
201 : static ulong
202 : populate_allowed_seccomp( fd_topo_t const * topo,
203 : fd_topo_tile_t const * tile,
204 : ulong out_cnt,
205 0 : struct sock_filter * out ) {
206 0 : (void)topo;
207 0 : (void)tile;
208 :
209 0 : populate_sock_filter_policy_fd_ipecho_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
210 0 : return sock_filter_policy_fd_ipecho_tile_instr_cnt;
211 0 : }
212 :
213 : static ulong
214 : populate_allowed_fds( fd_topo_t const * topo,
215 : fd_topo_tile_t const * tile,
216 : ulong out_fds_cnt,
217 0 : int * out_fds ) {
218 :
219 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
220 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
221 0 : fd_ipecho_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_ipecho_tile_ctx_t), sizeof(fd_ipecho_tile_ctx_t) );
222 :
223 0 : if( FD_UNLIKELY( out_fds_cnt<3UL+tile->ipecho.entrypoints_cnt ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
224 :
225 0 : ulong out_cnt = 0UL;
226 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
227 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
228 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
229 :
230 : /* All of the fds managed by the client. */
231 0 : for( ulong i=0UL; i<tile->ipecho.entrypoints_cnt; i++ ) {
232 0 : int fd = fd_ipecho_client_get_pollfds( ctx->client )[ i ].fd;
233 0 : if( FD_LIKELY( fd!=-1 ) ) out_fds[ out_cnt++ ] = fd;
234 0 : }
235 :
236 : /* The server's socket. */
237 0 : out_fds[ out_cnt++ ] = fd_ipecho_server_sockfd( ctx->server );
238 0 : return out_cnt;
239 0 : }
240 :
241 0 : #define STEM_BURST (1UL)
242 0 : #define STEM_LAZY (50UL)
243 :
244 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_ipecho_tile_ctx_t
245 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_ipecho_tile_ctx_t)
246 :
247 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
248 0 : #define STEM_CALLBACK_AFTER_CREDIT after_credit
249 0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
250 :
251 : #include "../../disco/stem/fd_stem.c"
252 :
253 : fd_topo_run_tile_t fd_tile_ipecho = {
254 : .name = "ipecho",
255 : .rlimit_file_cnt_fn = rlimit_file_cnt,
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 : .allow_connect = 1,
264 : .keep_host_networking = 1
265 : };
|