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