Line data Source code
1 : #define _GNU_SOURCE
2 : #include "utils/fd_ssarchive.h"
3 : #include "utils/fd_ssctrl.h"
4 : #include "utils/fd_sshttp.h"
5 : #include "utils/fd_sspeer_selector.h"
6 :
7 : #include "../../disco/topo/fd_topo.h"
8 : #include "../../disco/metrics/fd_metrics.h"
9 : #include "../../waltz/openssl/fd_openssl_tile.h"
10 :
11 : #include <sys/mman.h> /* memfd_create */
12 : #include <errno.h>
13 : #include <fcntl.h>
14 : #include <unistd.h>
15 : #include <sys/socket.h>
16 :
17 : #include "generated/fd_snapld_tile_seccomp.h"
18 :
19 : #define NAME "snapld"
20 :
21 : /* download progress in each 10 second window must be at
22 : min_download_speed_mibs * 10 seconds or higher. Catches extremely
23 : slow download speeds where we may not get to 100 MiB downloaded for a
24 : while. */
25 0 : #define FD_SNAPLD_DOWNLOAD_WINDOW_NS (10L*1000L*1000L*1000L) /* 10 seconds */
26 :
27 : /* The snapld tile is responsible for loading data from the local file
28 : or from an HTTP/TCP connection and sending it to the snapdc tile
29 : for later decompression. */
30 :
31 : typedef struct fd_snapld_tile {
32 :
33 : struct {
34 : char path[ PATH_MAX ];
35 : uint min_download_speed_mibs;
36 : } config;
37 :
38 : int state;
39 : int load_full;
40 : int load_file;
41 : int sent_meta;
42 : int is_redirect;
43 : ulong gossip_slot;
44 : ulong file_sz;
45 :
46 : ulong bytes_in_batch;
47 : double download_speed_mibs;
48 : long start_batch;
49 : long end_batch;
50 :
51 : ulong bytes_in_window;
52 : ulong min_bytes_in_window;
53 : long window_deadline;
54 :
55 : int local_full_fd;
56 : int local_incr_fd;
57 : int sockfd;
58 :
59 : fd_sshttp_t * sshttp;
60 :
61 : struct {
62 : void const * base;
63 : } in_rd;
64 :
65 : struct {
66 : fd_wksp_t * mem;
67 : ulong chunk0;
68 : ulong wmark;
69 : ulong chunk;
70 : ulong mtu;
71 : } out_dc;
72 :
73 : } fd_snapld_tile_t;
74 :
75 : static ulong
76 0 : scratch_align( void ) {
77 0 : ulong a = alignof(fd_snapld_tile_t);
78 0 : a = fd_ulong_max( a, fd_sshttp_align() );
79 0 : a = fd_ulong_max( a, fd_alloc_align() );
80 0 : return a;
81 0 : }
82 :
83 : static ulong
84 0 : scratch_footprint( fd_topo_tile_t const * tile FD_PARAM_UNUSED ) {
85 0 : ulong l = FD_LAYOUT_INIT;
86 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_snapld_tile_t), sizeof(fd_snapld_tile_t) );
87 0 : l = FD_LAYOUT_APPEND( l, fd_sshttp_align(), fd_sshttp_footprint() );
88 0 : l = FD_LAYOUT_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
89 0 : return FD_LAYOUT_FINI( l, scratch_align() );
90 0 : }
91 :
92 : FD_FN_CONST static inline ulong
93 0 : loose_footprint( fd_topo_tile_t const * tile ) {
94 0 : (void)tile;
95 : /* Leftover space for OpenSSL allocations */
96 0 : return 1UL<<26UL; /* 64 MiB */
97 0 : }
98 :
99 : static void
100 : privileged_init( fd_topo_t const * topo,
101 0 : fd_topo_tile_t const * tile ) {
102 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
103 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
104 0 : fd_snapld_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapld_tile_t), sizeof(fd_snapld_tile_t) );
105 0 : void * _sshttp = FD_SCRATCH_ALLOC_APPEND( l, fd_sshttp_align(), fd_sshttp_footprint() );
106 :
107 0 : #if FD_HAS_OPENSSL
108 0 : void * _alloc = FD_SCRATCH_ALLOC_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
109 0 : fd_alloc_t * alloc = fd_alloc_join( fd_alloc_new( _alloc, 1UL ), tile->kind_id );
110 0 : fd_ossl_tile_init( alloc );
111 0 : #endif
112 :
113 0 : ctx->sshttp = fd_sshttp_join( fd_sshttp_new( _sshttp ) );
114 0 : FD_TEST( ctx->sshttp );
115 :
116 0 : ulong full_slot = ULONG_MAX;
117 0 : ulong incr_slot = ULONG_MAX;
118 0 : int full_is_zstd = 0;
119 0 : int incr_is_zstd = 0;
120 0 : char full_path[ PATH_MAX ] = { 0 };
121 0 : char incr_path[ PATH_MAX ] = { 0 };
122 0 : uchar full_snapshot_hash[ FD_HASH_FOOTPRINT ] = { 0 };
123 0 : uchar incr_snapshot_hash[ FD_HASH_FOOTPRINT ] = { 0 };
124 0 : ctx->local_full_fd = -1;
125 0 : ctx->local_incr_fd = -1;
126 : /* fd_ssarchive_latest_pair needs to be invoked here, irrespective
127 : of whether snapct may do the same, because this information is
128 : needed here during privileged_init. */
129 0 : if( FD_LIKELY( -1!=fd_ssarchive_latest_pair( tile->snapld.snapshots_path,
130 0 : tile->snapld.incremental_snapshots,
131 0 : &full_slot, &incr_slot,
132 0 : full_path, incr_path,
133 0 : &full_is_zstd, &incr_is_zstd,
134 0 : full_snapshot_hash, incr_snapshot_hash ) ) ) {
135 0 : FD_TEST( full_slot!=ULONG_MAX );
136 :
137 0 : ctx->local_full_fd = open( full_path, O_RDONLY|O_CLOEXEC|O_NONBLOCK );
138 0 : if( FD_UNLIKELY( -1==ctx->local_full_fd ) ) FD_LOG_ERR(( "open() failed `%s` (%i-%s)", full_path, errno, fd_io_strerror( errno ) ));
139 0 : posix_fadvise( ctx->local_full_fd, 0L, 0L, POSIX_FADV_SEQUENTIAL );
140 :
141 0 : if( FD_LIKELY( incr_slot!=ULONG_MAX ) ) {
142 0 : ctx->local_incr_fd = open( incr_path, O_RDONLY|O_CLOEXEC|O_NONBLOCK );
143 0 : if( FD_UNLIKELY( -1==ctx->local_incr_fd ) ) FD_LOG_ERR(( "open() failed `%s` (%i-%s)", incr_path, errno, fd_io_strerror( errno ) ));
144 0 : posix_fadvise( ctx->local_incr_fd, 0L, 0L, POSIX_FADV_SEQUENTIAL );
145 0 : }
146 0 : }
147 :
148 : /* Create a temporary file descriptor for our socket file descriptor.
149 : It is closed later in unprivileged init so that the sandbox sees
150 : an existent file descriptor. */
151 0 : ctx->sockfd = memfd_create( "snapld.sockfd", 0 );
152 0 : if( FD_UNLIKELY( -1==ctx->sockfd ) ) FD_LOG_ERR(( "memfd_create() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
153 0 : }
154 :
155 : static ulong
156 : populate_allowed_fds( fd_topo_t const * topo,
157 : fd_topo_tile_t const * tile,
158 : ulong out_fds_cnt,
159 0 : int * out_fds ) {
160 0 : if( FD_UNLIKELY( out_fds_cnt<4UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
161 :
162 0 : ulong out_cnt = 0;
163 0 : out_fds[ out_cnt++ ] = 2UL; /* stderr */
164 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) ) {
165 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd();
166 0 : }
167 :
168 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
169 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
170 0 : fd_snapld_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapld_tile_t), sizeof(fd_snapld_tile_t) );
171 0 : if( FD_LIKELY( -1!=ctx->local_full_fd ) ) out_fds[ out_cnt++ ] = ctx->local_full_fd;
172 0 : if( FD_LIKELY( -1!=ctx->local_incr_fd ) ) out_fds[ out_cnt++ ] = ctx->local_incr_fd;
173 0 : out_fds[ out_cnt++ ] = ctx->sockfd;
174 :
175 0 : return out_cnt;
176 0 : }
177 :
178 : static ulong
179 : populate_allowed_seccomp( fd_topo_t const * topo,
180 : fd_topo_tile_t const * tile,
181 : ulong out_cnt,
182 0 : struct sock_filter * out ) {
183 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
184 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
185 0 : fd_snapld_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapld_tile_t), sizeof(fd_snapld_tile_t) );
186 :
187 0 : populate_sock_filter_policy_fd_snapld_tile( out_cnt, out, (uint)fd_log_private_logfile_fd(), (uint)ctx->local_full_fd, (uint)ctx->local_incr_fd, (uint)ctx->sockfd );
188 0 : return sock_filter_policy_fd_snapld_tile_instr_cnt;
189 0 : }
190 :
191 : static void
192 : unprivileged_init( fd_topo_t const * topo,
193 0 : fd_topo_tile_t const * tile ) {
194 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
195 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
196 0 : fd_snapld_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapld_tile_t), sizeof(fd_snapld_tile_t) );
197 0 : FD_SCRATCH_ALLOC_APPEND( l, fd_sshttp_align(), fd_sshttp_footprint() );
198 0 : FD_SCRATCH_ALLOC_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
199 :
200 0 : fd_memcpy( ctx->config.path, tile->snapld.snapshots_path, PATH_MAX );
201 0 : ctx->config.min_download_speed_mibs = tile->snapld.min_download_speed_mibs;
202 :
203 0 : ctx->state = FD_SNAPSHOT_STATE_IDLE;
204 :
205 0 : ctx->download_speed_mibs = 0.0;
206 0 : ctx->bytes_in_batch = 0UL;
207 0 : ctx->start_batch = 0L;
208 0 : ctx->end_batch = 0L;
209 0 : ctx->bytes_in_window = 0UL;
210 0 : ctx->window_deadline = LONG_MAX;
211 0 : ctx->min_bytes_in_window = ((ulong)ctx->config.min_download_speed_mibs * (FD_SNAPLD_DOWNLOAD_WINDOW_NS / (ulong)1e9))<<20UL;
212 :
213 0 : FD_TEST( tile->in_cnt==1UL );
214 0 : fd_topo_link_t const * in_link = &topo->links[ tile->in_link_id[ 0 ] ];
215 0 : FD_TEST( 0==strcmp( in_link->name, "snapct_ld" ) );
216 0 : ctx->in_rd.base = fd_topo_obj_wksp_base( topo, in_link->dcache_obj_id );
217 :
218 0 : FD_TEST( tile->out_cnt==1UL );
219 0 : fd_topo_link_t const * out_link = &topo->links[ tile->out_link_id[ 0 ] ];
220 0 : FD_TEST( 0==strcmp( out_link->name, "snapld_dc" ) );
221 0 : ctx->out_dc.mem = fd_topo_obj_wksp_base( topo, out_link->dcache_obj_id );
222 0 : ctx->out_dc.chunk0 = fd_dcache_compact_chunk0( ctx->out_dc.mem, out_link->dcache );
223 0 : ctx->out_dc.wmark = fd_dcache_compact_wmark ( ctx->out_dc.mem, out_link->dcache, out_link->mtu );
224 0 : ctx->out_dc.chunk = ctx->out_dc.chunk0;
225 0 : ctx->out_dc.mtu = out_link->mtu;
226 :
227 0 : FD_TEST( sizeof(fd_ssctrl_meta_t)<=ctx->out_dc.mtu );
228 :
229 : /* We can only close the temporary socket file descriptor after
230 : entering the sandbox because the sandbox checks all file
231 : descriptors are existent. */
232 0 : if( -1==close( ctx->sockfd ) ) FD_LOG_ERR((" close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
233 :
234 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
235 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
236 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
237 0 : }
238 :
239 : static int
240 0 : should_shutdown( fd_snapld_tile_t * ctx ) {
241 0 : return ctx->state==FD_SNAPSHOT_STATE_SHUTDOWN;
242 0 : }
243 :
244 : static void
245 0 : metrics_write( fd_snapld_tile_t * ctx ) {
246 0 : #if FD_HAS_OPENSSL
247 0 : FD_MCNT_SET( SNAPLD, SSL_ALLOC_FAILED, fd_ossl_alloc_errors );
248 0 : #endif
249 0 : FD_MGAUGE_SET( SNAPLD, STATE, (ulong)(ctx->state) );
250 0 : }
251 :
252 : static void
253 : transition_malformed( fd_snapld_tile_t * ctx,
254 0 : fd_stem_context_t * stem ) {
255 0 : if( FD_UNLIKELY( ctx->state==FD_SNAPSHOT_STATE_ERROR ) ) return;
256 0 : ctx->state = FD_SNAPSHOT_STATE_ERROR;
257 0 : fd_stem_publish( stem, 0UL, FD_SNAPSHOT_MSG_CTRL_ERROR, 0UL, 0UL, 0UL, 0UL, 0UL );
258 0 : }
259 :
260 : static int
261 : check_download_progress( fd_snapld_tile_t * ctx,
262 : fd_stem_context_t * stem,
263 : int downloading,
264 0 : long now ) {
265 0 : if( FD_UNLIKELY( ctx->window_deadline==LONG_MAX && downloading ) ) {
266 0 : ctx->window_deadline = now + FD_SNAPLD_DOWNLOAD_WINDOW_NS;
267 0 : ctx->bytes_in_window = 0UL;
268 0 : }
269 :
270 0 : if( FD_UNLIKELY( now>ctx->window_deadline ) ) {
271 0 : if( FD_UNLIKELY( ctx->bytes_in_window<ctx->min_bytes_in_window ) ) {
272 : /* cancel the download if the download progress speed in the last
273 : window is less than the minimum download speed. */
274 0 : double download_speed_mibs = (double)ctx->bytes_in_window / (double)(FD_SNAPLD_DOWNLOAD_WINDOW_NS / 1e9) / (double)(1<<20UL);
275 0 : FD_LOG_WARNING(( "download progress of %.2f MiB/s in the last %lu seconds for %s snapshot "
276 0 : "is below the minimum download speed %u MiB/s, cancelling download",
277 0 : download_speed_mibs, FD_SNAPLD_DOWNLOAD_WINDOW_NS / (ulong)1e9,
278 0 : ctx->load_full ? "full" : "incremental", ctx->config.min_download_speed_mibs ));
279 0 : transition_malformed( ctx, stem );
280 0 : fd_sshttp_cancel( ctx->sshttp );
281 0 : return -1;
282 0 : }
283 0 : ctx->window_deadline = now + FD_SNAPLD_DOWNLOAD_WINDOW_NS;
284 0 : ctx->bytes_in_window = 0UL;
285 0 : }
286 0 : return 0;
287 0 : }
288 :
289 : static void
290 : after_credit( fd_snapld_tile_t * ctx,
291 : fd_stem_context_t * stem,
292 : int * opt_poll_in FD_PARAM_UNUSED,
293 0 : int * charge_busy ) {
294 0 : if( ctx->state!=FD_SNAPSHOT_STATE_PROCESSING ) {
295 0 : fd_log_sleep( (long)1e6 );
296 0 : return;
297 0 : }
298 :
299 0 : uchar * out = fd_chunk_to_laddr( ctx->out_dc.mem, ctx->out_dc.chunk );
300 :
301 0 : if( ctx->load_file ) {
302 0 : if( FD_UNLIKELY( !ctx->sent_meta ) ) {
303 0 : FD_TEST( sizeof(fd_ssctrl_meta_t)<=ctx->out_dc.mtu );
304 0 : fd_ssctrl_meta_t * meta = (fd_ssctrl_meta_t *)out;
305 0 : meta->total_sz = ctx->file_sz;
306 0 : meta->resolved_slot = ULONG_MAX;
307 0 : fd_memset( meta->resolved_hash, 0, FD_HASH_FOOTPRINT );
308 0 : meta->resolved_name[0] = '\0';
309 0 : ctx->sent_meta = 1;
310 0 : fd_stem_publish( stem, 0UL, FD_SNAPSHOT_MSG_META, ctx->out_dc.chunk, sizeof(fd_ssctrl_meta_t), 0UL, 0UL, 0UL );
311 0 : ctx->out_dc.chunk = fd_dcache_compact_next( ctx->out_dc.chunk, sizeof(fd_ssctrl_meta_t), ctx->out_dc.chunk0, ctx->out_dc.wmark );
312 0 : return;
313 0 : }
314 0 : long result = read( ctx->load_full ? ctx->local_full_fd : ctx->local_incr_fd, out, ctx->out_dc.mtu );
315 0 : if( FD_UNLIKELY( result<=0L ) ) {
316 0 : if( result==0L ) {
317 0 : FD_LOG_INFO(( "finished reading %s snapshot from local file", ctx->load_full ? "full" : "incremental" ));
318 0 : ctx->state = FD_SNAPSHOT_STATE_FINISHING;
319 0 : fd_stem_publish( stem, 0UL, FD_SNAPSHOT_MSG_LOAD_COMPLETE, 0UL, 0UL, 0UL, 0UL, 0UL );
320 0 : } else if( FD_UNLIKELY( errno!=EAGAIN && errno!=EINTR ) ) {
321 0 : FD_LOG_WARNING(( "read() failed on %s snapshot file (%i-%s)", ctx->load_full ? "full" : "incremental", errno, fd_io_strerror( errno ) ));
322 0 : transition_malformed( ctx, stem );
323 0 : return; /* verbose return */
324 0 : }
325 0 : } else {
326 0 : fd_stem_publish( stem, 0UL, FD_SNAPSHOT_MSG_DATA, ctx->out_dc.chunk, (ulong)result, 0UL, 0UL, 0UL );
327 0 : ctx->out_dc.chunk = fd_dcache_compact_next( ctx->out_dc.chunk, (ulong)result, ctx->out_dc.chunk0, ctx->out_dc.wmark );
328 0 : *charge_busy = 1;
329 0 : return; /* verbose return */
330 0 : }
331 0 : } else {
332 0 : int downloading = 0;
333 0 : ulong data_len = ctx->out_dc.mtu;
334 0 : long now = fd_log_wallclock();
335 0 : int result = fd_sshttp_advance( ctx->sshttp, &data_len, out, &downloading, now );
336 0 : switch( result ) {
337 0 : case FD_SSHTTP_ADVANCE_AGAIN:
338 : /* Return value ignored: on failure, check_download_progress
339 : already calls transition_malformed and fd_sshttp_cancel. */
340 0 : check_download_progress( ctx, stem, downloading, now );
341 0 : break;
342 0 : case FD_SSHTTP_ADVANCE_DATA: {
343 0 : ctx->bytes_in_window += data_len;
344 0 : if( FD_UNLIKELY( -1==check_download_progress( ctx, stem, downloading, now ) ) ) break;
345 0 : if( FD_UNLIKELY( !ctx->sent_meta ) ) {
346 : /* On the first DATA return, the HTTP headers are available
347 : for use. We need to send this metadata downstream, but
348 : need to do so before any data frags. So, we copy any data
349 : we received with the headers (if any) to the next dcache
350 : chunk and then publish both in order. */
351 0 : ctx->start_batch = fd_log_wallclock();
352 0 : FD_TEST( sizeof(fd_ssctrl_meta_t)<=ctx->out_dc.mtu );
353 0 : fd_ssctrl_meta_t * meta = (fd_ssctrl_meta_t *)out;
354 0 : ulong next_chunk = fd_dcache_compact_next( ctx->out_dc.chunk, sizeof(fd_ssctrl_meta_t), ctx->out_dc.chunk0, ctx->out_dc.wmark );
355 0 : memmove( fd_chunk_to_laddr( ctx->out_dc.mem, next_chunk ), out, data_len );
356 0 : meta->total_sz = fd_sshttp_content_len( ctx->sshttp );
357 0 : if( FD_UNLIKELY( meta->total_sz==ULONG_MAX ) ) {
358 0 : FD_LOG_WARNING(( "HTTP response for %s snapshot is missing Content-Length header", ctx->load_full ? "full" : "incremental" ));
359 0 : transition_malformed( ctx, stem );
360 0 : fd_sshttp_cancel( ctx->sshttp );
361 0 : break;
362 0 : }
363 :
364 : /* Populate resolved redirect fields in META */
365 0 : meta->resolved_slot = ULONG_MAX;
366 0 : meta->resolved_name[0] = '\0';
367 0 : fd_memset( meta->resolved_hash, 0, FD_HASH_FOOTPRINT );
368 :
369 0 : if( ctx->is_redirect ) {
370 0 : char const * resolved_name = fd_sshttp_snapshot_name( ctx->sshttp );
371 0 : if( FD_UNLIKELY( !resolved_name || resolved_name[0]=='\0' ) ) {
372 0 : FD_LOG_WARNING(( "redirect-based download did not resolve to a snapshot filename for %s snapshot",
373 0 : ctx->load_full ? "full" : "incremental" ));
374 0 : transition_malformed( ctx, stem );
375 0 : fd_sshttp_cancel( ctx->sshttp );
376 0 : break;
377 0 : }
378 0 : int is_full_filename = !strncmp( resolved_name, "snapshot-", 9 );
379 0 : if( FD_UNLIKELY( is_full_filename!=ctx->load_full ) ) {
380 0 : FD_LOG_WARNING(( "resolved snapshot type mismatch: expected %s but got %s filename `%s`",
381 0 : ctx->load_full ? "full" : "incremental", is_full_filename ? "full" : "incremental", resolved_name ));
382 0 : transition_malformed( ctx, stem );
383 0 : fd_sshttp_cancel( ctx->sshttp );
384 0 : break;
385 0 : }
386 0 : ulong resolved_slot = fd_sshttp_resolved_slot( ctx->sshttp );
387 0 : if( FD_UNLIKELY( resolved_slot<ctx->gossip_slot ) ) {
388 0 : FD_LOG_WARNING(( "resolved snapshot slot %lu is older than gossip slot %lu for %s snapshot, rejecting",
389 0 : resolved_slot, ctx->gossip_slot, ctx->load_full ? "full" : "incremental" ));
390 0 : transition_malformed( ctx, stem );
391 0 : fd_sshttp_cancel( ctx->sshttp );
392 0 : break;
393 0 : }
394 0 : if( FD_UNLIKELY( resolved_slot>=FD_SSPEER_PLAUSIBLE_MAX_SLOT ) ) {
395 0 : FD_LOG_WARNING(( "resolved snapshot slot %lu exceeds plausibility bound for %s snapshot, rejecting",
396 0 : resolved_slot, ctx->load_full ? "full" : "incremental" ));
397 0 : transition_malformed( ctx, stem );
398 0 : fd_sshttp_cancel( ctx->sshttp );
399 0 : break;
400 0 : }
401 0 : meta->resolved_slot = resolved_slot;
402 0 : fd_memcpy( meta->resolved_hash, fd_sshttp_resolved_hash( ctx->sshttp ), FD_HASH_FOOTPRINT );
403 0 : fd_cstr_ncpy( meta->resolved_name, resolved_name, PATH_MAX );
404 0 : FD_LOG_INFO(( "redirect resolved to `%s` (slot %lu) for %s snapshot",
405 0 : resolved_name, resolved_slot, ctx->load_full ? "full" : "incremental" ));
406 0 : }
407 :
408 0 : ctx->sent_meta = 1;
409 0 : fd_stem_publish( stem, 0UL, FD_SNAPSHOT_MSG_META, ctx->out_dc.chunk, sizeof(fd_ssctrl_meta_t), 0UL, 0UL, 0UL );
410 0 : ctx->out_dc.chunk = next_chunk;
411 0 : }
412 0 : if( FD_LIKELY( data_len!=0UL ) ) {
413 0 : fd_stem_publish( stem, 0UL, FD_SNAPSHOT_MSG_DATA, ctx->out_dc.chunk, data_len, 0UL, 0UL, 0UL );
414 0 : ctx->out_dc.chunk = fd_dcache_compact_next( ctx->out_dc.chunk, data_len, ctx->out_dc.chunk0, ctx->out_dc.wmark );
415 0 : ctx->bytes_in_batch += data_len;
416 :
417 : /* measure download speed every 100 MiB */
418 0 : if(ctx->bytes_in_batch>=100<<20UL) {
419 0 : ctx->end_batch = fd_log_wallclock();
420 : /* as a precaution, make sure elapsed_batch is positive
421 : and larger than zero (to avoid division by zero). */
422 0 : long elapsed_batch = fd_long_if( ctx->end_batch > ctx->start_batch, ctx->end_batch - ctx->start_batch, 1L );
423 : /* download speed in MiB/s = bytes/nanoseconds * 1e9/(1 second) * 1/(1MiB = 1<<20UL) = 1e9/(1024*1024) ~= 954 */
424 0 : ctx->download_speed_mibs = (double)(ctx->bytes_in_batch*954) / (double)elapsed_batch;
425 0 : if( FD_UNLIKELY( ctx->download_speed_mibs<ctx->config.min_download_speed_mibs ) ) {
426 : /* cancel the snapshot load if the download speed is less
427 : than the minimum download speed. */
428 0 : FD_LOG_WARNING(( "download speed %.2f MiB/s on a batch of %lu MiB for %s snapshot is below the minimum threshold %.2f MiB/s. "
429 0 : "cancelling snapshot download",
430 0 : ctx->download_speed_mibs, ctx->bytes_in_batch>>20UL, ctx->load_full ? "full" : "incremental",
431 0 : (double)(ctx->config.min_download_speed_mibs) ));
432 0 : transition_malformed( ctx, stem );
433 0 : fd_sshttp_cancel( ctx->sshttp );
434 0 : break;
435 0 : }
436 0 : ctx->start_batch = ctx->end_batch;
437 0 : ctx->bytes_in_batch = 0UL;
438 0 : }
439 0 : }
440 0 : *charge_busy = 1;
441 0 : break;
442 0 : }
443 0 : case FD_SSHTTP_ADVANCE_DONE:
444 0 : if( FD_UNLIKELY( !ctx->sent_meta ) ) {
445 0 : FD_LOG_WARNING(( "zero-length HTTP response for %s snapshot", ctx->load_full ? "full" : "incremental" ));
446 0 : transition_malformed( ctx, stem );
447 0 : fd_sshttp_cancel( ctx->sshttp );
448 0 : break;
449 0 : }
450 0 : FD_LOG_INFO(( "finished downloading %s snapshot", ctx->load_full ? "full" : "incremental" ));
451 0 : ctx->state = FD_SNAPSHOT_STATE_FINISHING;
452 0 : fd_stem_publish( stem, 0UL, FD_SNAPSHOT_MSG_LOAD_COMPLETE, 0UL, 0UL, 0UL, 0UL, 0UL );
453 0 : break;
454 0 : case FD_SSHTTP_ADVANCE_ERROR:
455 0 : FD_LOG_WARNING(( "HTTP advance error during %s snapshot download, entering error state",
456 0 : ctx->load_full ? "full" : "incremental" ));
457 0 : transition_malformed( ctx, stem );
458 0 : fd_sshttp_cancel( ctx->sshttp );
459 0 : break;
460 0 : default: FD_LOG_ERR(( "unexpected fd_sshttp_advance result %d for %s snapshot",
461 0 : result, ctx->load_full ? "full" : "incremental" ));
462 0 : }
463 0 : }
464 0 : }
465 :
466 : static int
467 : returnable_frag( fd_snapld_tile_t * ctx,
468 : ulong in_idx FD_PARAM_UNUSED,
469 : ulong seq FD_PARAM_UNUSED,
470 : ulong sig,
471 : ulong chunk,
472 : ulong sz,
473 : ulong ctl FD_PARAM_UNUSED,
474 : ulong tsorig FD_PARAM_UNUSED,
475 : ulong tspub FD_PARAM_UNUSED,
476 0 : fd_stem_context_t * stem ) {
477 0 : if( ctx->state==FD_SNAPSHOT_STATE_ERROR && sig!=FD_SNAPSHOT_MSG_CTRL_FAIL ) {
478 : /* Control messages move along the snapshot load pipeline. Since
479 : error conditions can be triggered by any tile in the pipeline,
480 : it is possible to be in error state and still receive otherwise
481 : valid messages. Only a fail message can revert this. */
482 0 : return 0;
483 0 : };
484 :
485 0 : int forward_msg = 1;
486 :
487 0 : switch( sig ) {
488 :
489 0 : case FD_SNAPSHOT_MSG_CTRL_INIT_FULL:
490 0 : case FD_SNAPSHOT_MSG_CTRL_INIT_INCR: {
491 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_IDLE );
492 0 : ctx->state = FD_SNAPSHOT_STATE_PROCESSING;
493 0 : FD_TEST( sz==sizeof(fd_ssctrl_init_t) && sz<=ctx->out_dc.mtu );
494 0 : fd_ssctrl_init_t const * msg_in = fd_chunk_to_laddr_const( ctx->in_rd.base, chunk );
495 0 : ctx->load_full = sig==FD_SNAPSHOT_MSG_CTRL_INIT_FULL;
496 0 : ctx->load_file = msg_in->file;
497 0 : ctx->sent_meta = 0;
498 0 : ctx->gossip_slot = msg_in->slot;
499 0 : ctx->is_redirect = msg_in->is_redirect;
500 0 : ctx->file_sz = msg_in->file_sz;
501 :
502 0 : ctx->window_deadline = LONG_MAX;
503 0 : ctx->bytes_in_window = 0UL;
504 0 : long now = fd_log_wallclock();
505 0 : if( ctx->load_file ) {
506 0 : if( FD_UNLIKELY( 0!=lseek( ctx->load_full ? ctx->local_full_fd : ctx->local_incr_fd, 0, SEEK_SET ) ) )
507 0 : FD_LOG_ERR(( "lseek(0) failed on %s snapshot file (%i-%s)",
508 0 : ctx->load_full ? "full" : "incremental", errno, fd_io_strerror( errno ) ));
509 0 : } else {
510 0 : if( FD_UNLIKELY( fd_sshttp_init( ctx->sshttp, msg_in->addr, msg_in->hostname, msg_in->is_https, msg_in->path, msg_in->path_len, 4UL, now ) ) ) {
511 0 : transition_malformed( ctx, stem );
512 0 : forward_msg = 0;
513 0 : break;
514 0 : }
515 0 : }
516 0 : fd_ssctrl_init_t * msg_out = fd_chunk_to_laddr( ctx->out_dc.mem, ctx->out_dc.chunk );
517 0 : fd_memcpy( msg_out, msg_in, sz );
518 0 : fd_stem_publish( stem, 0UL, sig, ctx->out_dc.chunk, sz, 0UL, 0UL, 0UL );
519 0 : ctx->out_dc.chunk = fd_dcache_compact_next( ctx->out_dc.chunk, ctx->out_dc.mtu, ctx->out_dc.chunk0, ctx->out_dc.wmark );
520 0 : forward_msg = 0; // we are forwarding the control message in the `fd_sstrl_init_t` message
521 0 : break;
522 0 : }
523 :
524 0 : case FD_SNAPSHOT_MSG_CTRL_FINI: {
525 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_FINISHING );
526 0 : break;
527 0 : }
528 :
529 0 : case FD_SNAPSHOT_MSG_CTRL_NEXT:
530 0 : case FD_SNAPSHOT_MSG_CTRL_DONE: {
531 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_FINISHING );
532 0 : ctx->state = FD_SNAPSHOT_STATE_IDLE;
533 0 : break;
534 0 : }
535 :
536 0 : case FD_SNAPSHOT_MSG_CTRL_ERROR: {
537 0 : FD_TEST( ctx->state!=FD_SNAPSHOT_STATE_SHUTDOWN );
538 0 : fd_sshttp_cancel( ctx->sshttp );
539 0 : ctx->state = FD_SNAPSHOT_STATE_ERROR;
540 0 : break;
541 0 : }
542 :
543 0 : case FD_SNAPSHOT_MSG_CTRL_FAIL:
544 0 : FD_TEST( ctx->state!=FD_SNAPSHOT_STATE_SHUTDOWN );
545 0 : fd_sshttp_cancel( ctx->sshttp );
546 0 : ctx->state = FD_SNAPSHOT_STATE_IDLE;
547 0 : break;
548 :
549 0 : case FD_SNAPSHOT_MSG_CTRL_SHUTDOWN: {
550 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_IDLE );
551 0 : ctx->state = FD_SNAPSHOT_STATE_SHUTDOWN;
552 0 : break;
553 0 : }
554 :
555 : /* FD_SNAPSHOT_MSG_DATA is not possible */
556 0 : default: {
557 0 : FD_LOG_ERR(( "unexpected control frag %s (%lu) in state %s (%lu)",
558 0 : fd_ssctrl_msg_ctrl_str( sig ), sig,
559 0 : fd_ssctrl_state_str( (ulong)ctx->state ), (ulong)ctx->state ));
560 0 : break;
561 0 : }
562 0 : }
563 :
564 : /* Forward the control message down the pipeline */
565 0 : if( FD_LIKELY( forward_msg ) ) {
566 0 : fd_stem_publish( stem, 0UL, sig, 0UL, 0UL, 0UL, 0UL, 0UL );
567 0 : }
568 :
569 0 : return 0;
570 0 : }
571 :
572 : /* Up to two frags from after_credit plus one from returnable_frag */
573 0 : #define STEM_BURST 3UL
574 :
575 0 : #define STEM_LAZY (128L*3000L)
576 :
577 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_snapld_tile_t
578 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_snapld_tile_t)
579 :
580 : #define STEM_CALLBACK_SHOULD_SHUTDOWN should_shutdown
581 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
582 0 : #define STEM_CALLBACK_AFTER_CREDIT after_credit
583 0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
584 :
585 : #include "../../disco/stem/fd_stem.c"
586 :
587 : fd_topo_run_tile_t fd_tile_snapld = {
588 : .name = NAME,
589 : .populate_allowed_seccomp = populate_allowed_seccomp,
590 : .populate_allowed_fds = populate_allowed_fds,
591 : .scratch_align = scratch_align,
592 : .scratch_footprint = scratch_footprint,
593 : .loose_footprint = loose_footprint,
594 : .privileged_init = privileged_init,
595 : .unprivileged_init = unprivileged_init,
596 : .run = stem_run,
597 : .keep_host_networking = 1,
598 : .allow_connect = 1,
599 : .rlimit_file_cnt = 5UL, /* stderr, log, http, full/incr local files */
600 : };
601 :
602 : #undef NAME
|