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