Line data Source code
1 : #include "utils/fd_ssctrl.h"
2 :
3 : #include "../../disco/topo/fd_topo.h"
4 : #include "../../disco/metrics/fd_metrics.h"
5 :
6 : #include "generated/fd_snapdc_tile_seccomp.h"
7 :
8 : #define ZSTD_STATIC_LINKING_ONLY
9 : #include <zstd.h>
10 :
11 : #define NAME "snapdc"
12 :
13 0 : #define ZSTD_WINDOW_SZ (1UL<<25UL) /* 32MiB */
14 :
15 : /* The snapdc tile is a state machine that decompresses the full and
16 : optionally incremental snapshot byte stream that it receives from the
17 : snapld tile. In the event that the snapshot is already uncompressed,
18 : this tile simply copies the stream to the next tile in the pipeline. */
19 :
20 : struct fd_snapdc_tile {
21 : uint full : 1;
22 : uint is_zstd : 1;
23 : uint dirty : 1; /* in the middle of a frame? */
24 : int state;
25 :
26 : ZSTD_DCtx * zstd;
27 :
28 : struct {
29 : fd_wksp_t * mem;
30 : ulong chunk0;
31 : ulong wmark;
32 : ulong mtu;
33 : ulong frag_pos;
34 : } in;
35 :
36 : struct {
37 : fd_wksp_t * mem;
38 : ulong chunk0;
39 : ulong wmark;
40 : ulong chunk;
41 : ulong mtu;
42 : } out;
43 :
44 : struct {
45 : struct {
46 : ulong compressed_bytes_read;
47 : ulong decompressed_bytes_written;
48 : } full;
49 :
50 : struct {
51 : ulong compressed_bytes_read;
52 : ulong decompressed_bytes_written;
53 : } incremental;
54 : } metrics;
55 : };
56 : typedef struct fd_snapdc_tile fd_snapdc_tile_t;
57 :
58 : FD_FN_PURE static ulong
59 0 : scratch_align( void ) {
60 0 : return fd_ulong_max( alignof(fd_snapdc_tile_t), 32UL );
61 0 : }
62 :
63 : FD_FN_PURE static ulong
64 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
65 0 : (void)tile;
66 0 : ulong l = FD_LAYOUT_INIT;
67 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_snapdc_tile_t), sizeof(fd_snapdc_tile_t) );
68 0 : l = FD_LAYOUT_APPEND( l, 32UL, ZSTD_estimateDStreamSize( ZSTD_WINDOW_SZ ) );
69 0 : return FD_LAYOUT_FINI( l, scratch_align() );
70 0 : }
71 :
72 : static inline int
73 0 : should_shutdown( fd_snapdc_tile_t * ctx ) {
74 0 : return ctx->state==FD_SNAPSHOT_STATE_SHUTDOWN;
75 0 : }
76 :
77 : static void
78 0 : metrics_write( fd_snapdc_tile_t * ctx ) {
79 0 : FD_MGAUGE_SET( SNAPDC, FULL_COMPRESSED_BYTES_READ, ctx->metrics.full.compressed_bytes_read );
80 0 : FD_MGAUGE_SET( SNAPDC, FULL_DECOMPRESSED_BYTES_WRITTEN, ctx->metrics.full.decompressed_bytes_written );
81 :
82 0 : FD_MGAUGE_SET( SNAPDC, INCREMENTAL_COMPRESSED_BYTES_READ, ctx->metrics.incremental.compressed_bytes_read );
83 0 : FD_MGAUGE_SET( SNAPDC, INCREMENTAL_DECOMPRESSED_BYTES_WRITTEN, ctx->metrics.incremental.decompressed_bytes_written );
84 :
85 0 : FD_MGAUGE_SET( SNAPDC, STATE, (ulong)(ctx->state) );
86 0 : }
87 :
88 : static void
89 : transition_malformed( fd_snapdc_tile_t * ctx,
90 0 : fd_stem_context_t * stem ) {
91 0 : if( FD_UNLIKELY( ctx->state==FD_SNAPSHOT_STATE_ERROR ) ) return;
92 0 : ctx->state = FD_SNAPSHOT_STATE_ERROR;
93 0 : fd_stem_publish( stem, 0UL, FD_SNAPSHOT_MSG_CTRL_ERROR, 0UL, 0UL, 0UL, 0UL, 0UL );
94 0 : }
95 :
96 : static inline void
97 : handle_control_frag( fd_snapdc_tile_t * ctx,
98 : fd_stem_context_t * stem,
99 : ulong sig,
100 : ulong chunk,
101 0 : ulong sz ) {
102 0 : if( FD_UNLIKELY( sig==FD_SNAPSHOT_MSG_LOAD_COMPLETE ) ) return;
103 :
104 : /* All control messages except META reset the decompression stream */
105 0 : if( FD_UNLIKELY( sig!=FD_SNAPSHOT_MSG_META ) ) {
106 0 : ulong error = ZSTD_DCtx_reset( ctx->zstd, ZSTD_reset_session_only );
107 0 : if( FD_UNLIKELY( ZSTD_isError( error ) ) ) FD_LOG_ERR(( "ZSTD_DCtx_reset failed (%lu-%s)", error, ZSTD_getErrorName( error ) ));
108 0 : }
109 :
110 0 : if( ctx->state==FD_SNAPSHOT_STATE_ERROR && sig!=FD_SNAPSHOT_MSG_CTRL_FAIL ) {
111 : /* Control messages move along the snapshot load pipeline. Since
112 : error conditions can be triggered by any tile in the pipeline,
113 : it is possible to be in error state and still receive otherwise
114 : valid messages. Only a fail message can revert this. */
115 0 : return;
116 0 : };
117 :
118 0 : if( FD_UNLIKELY( sig==FD_SNAPSHOT_MSG_META ) ) {
119 : /* Forward META to snapin so it can update the advertised
120 : slot/hash for redirect-based downloads. */
121 0 : FD_TEST( sz<=ctx->out.mtu );
122 0 : void * dst = fd_chunk_to_laddr( ctx->out.mem, ctx->out.chunk );
123 0 : fd_memcpy( dst, fd_chunk_to_laddr_const( ctx->in.mem, chunk ), sz );
124 0 : fd_stem_publish( stem, 0UL, sig, ctx->out.chunk, sz, 0UL, 0UL, 0UL );
125 0 : ctx->out.chunk = fd_dcache_compact_next( ctx->out.chunk, ctx->out.mtu, ctx->out.chunk0, ctx->out.wmark );
126 0 : return;
127 0 : }
128 :
129 0 : int forward_msg = 1;
130 :
131 0 : switch( sig ) {
132 0 : case FD_SNAPSHOT_MSG_CTRL_INIT_FULL:
133 0 : case FD_SNAPSHOT_MSG_CTRL_INIT_INCR: {
134 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_IDLE );
135 0 : ctx->state = FD_SNAPSHOT_STATE_PROCESSING;
136 0 : FD_TEST( sz==sizeof(fd_ssctrl_init_t) );
137 0 : fd_ssctrl_init_t const * msg = fd_chunk_to_laddr_const( ctx->in.mem, chunk );
138 0 : ctx->full = sig==FD_SNAPSHOT_MSG_CTRL_INIT_FULL;
139 0 : ctx->is_zstd = !!msg->zstd;
140 0 : ctx->dirty = 0;
141 0 : ctx->in.frag_pos = 0UL;
142 0 : if( ctx->full ) {
143 0 : ctx->metrics.full.compressed_bytes_read = 0UL;
144 0 : ctx->metrics.full.decompressed_bytes_written = 0UL;
145 0 : } else {
146 0 : ctx->metrics.incremental.compressed_bytes_read = 0UL;
147 0 : ctx->metrics.incremental.decompressed_bytes_written = 0UL;
148 0 : }
149 0 : fd_ssctrl_init_t * msg_out = fd_chunk_to_laddr( ctx->out.mem, ctx->out.chunk );
150 0 : fd_memcpy( msg_out, msg, sz );
151 0 : fd_stem_publish( stem, 0UL, sig, ctx->out.chunk, sz, 0UL, 0UL, 0UL );
152 0 : ctx->out.chunk = fd_dcache_compact_next( ctx->out.chunk, ctx->out.mtu, ctx->out.chunk0, ctx->out.wmark );
153 0 : forward_msg = 0; // we forward the control message in the `fd_ssctrl_init_t` message
154 0 : break;
155 0 : }
156 :
157 0 : case FD_SNAPSHOT_MSG_CTRL_FINI: {
158 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_PROCESSING );
159 0 : ctx->state = FD_SNAPSHOT_STATE_FINISHING;
160 0 : if( FD_UNLIKELY( ctx->is_zstd && ctx->dirty ) ) {
161 0 : FD_LOG_WARNING(( "encountered end-of-file in the middle of a compressed frame for %s snapshot",
162 0 : ctx->full ? "full" : "incremental" ));
163 0 : transition_malformed( ctx, stem );
164 0 : forward_msg = 0;
165 0 : break;
166 0 : }
167 0 : break;
168 0 : }
169 :
170 0 : case FD_SNAPSHOT_MSG_CTRL_NEXT:
171 0 : case FD_SNAPSHOT_MSG_CTRL_DONE: {
172 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_FINISHING );
173 0 : ctx->state = FD_SNAPSHOT_STATE_IDLE;
174 0 : break;
175 0 : }
176 :
177 0 : case FD_SNAPSHOT_MSG_CTRL_ERROR: {
178 0 : FD_TEST( ctx->state!=FD_SNAPSHOT_STATE_SHUTDOWN );
179 0 : ctx->state = FD_SNAPSHOT_STATE_ERROR;
180 0 : break;
181 0 : }
182 :
183 0 : case FD_SNAPSHOT_MSG_CTRL_FAIL: {
184 0 : FD_TEST( ctx->state!=FD_SNAPSHOT_STATE_SHUTDOWN );
185 0 : ctx->state = FD_SNAPSHOT_STATE_IDLE;
186 0 : break;
187 0 : }
188 :
189 0 : case FD_SNAPSHOT_MSG_CTRL_SHUTDOWN: {
190 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_IDLE );
191 0 : ctx->state = FD_SNAPSHOT_STATE_SHUTDOWN;
192 0 : break;
193 0 : }
194 :
195 0 : default: {
196 0 : FD_LOG_ERR(( "unexpected control frag %s (%lu) in state %s (%lu)",
197 0 : fd_ssctrl_msg_ctrl_str( sig ), sig,
198 0 : fd_ssctrl_state_str( (ulong)ctx->state ), (ulong)ctx->state ));
199 0 : break;
200 0 : }
201 0 : }
202 :
203 : /* Forward the control message down the pipeline */
204 0 : if( FD_LIKELY( forward_msg ) ) {
205 0 : fd_stem_publish( stem, 0UL, sig, 0UL, 0UL, 0UL, 0UL, 0UL );
206 0 : }
207 0 : }
208 :
209 : static inline int
210 : handle_data_frag( fd_snapdc_tile_t * ctx,
211 : fd_stem_context_t * stem,
212 : ulong chunk,
213 0 : ulong sz ) {
214 0 : if( FD_UNLIKELY( ctx->state==FD_SNAPSHOT_STATE_ERROR ) ) {
215 : /* Ignore all data frags after observing an error in the stream until
216 : we receive fail & init control messages to restart processing. */
217 0 : return 0;
218 0 : }
219 0 : if( FD_UNLIKELY( ctx->state!=FD_SNAPSHOT_STATE_PROCESSING ) ) {
220 0 : FD_LOG_ERR(( "received unexpected data frag in state %s (%lu)",
221 0 : fd_ssctrl_state_str( (ulong)ctx->state ), (ulong)ctx->state ));
222 0 : }
223 :
224 0 : FD_TEST( chunk>=ctx->in.chunk0 && chunk<=ctx->in.wmark && sz<=ctx->in.mtu && sz>=ctx->in.frag_pos );
225 0 : uchar const * data = fd_chunk_to_laddr_const( ctx->in.mem, chunk );
226 0 : uchar const * in = data+ctx->in.frag_pos;
227 0 : uchar * out = fd_chunk_to_laddr( ctx->out.mem, ctx->out.chunk );
228 :
229 0 : if( FD_UNLIKELY( !ctx->is_zstd ) ) {
230 0 : FD_TEST( ctx->in.frag_pos<sz );
231 0 : ulong cpy = fd_ulong_min( sz-ctx->in.frag_pos, ctx->out.mtu );
232 0 : fd_memcpy( out, in, cpy );
233 0 : fd_stem_publish( stem, 0UL, FD_SNAPSHOT_MSG_DATA, ctx->out.chunk, cpy, 0UL, 0UL, 0UL );
234 0 : ctx->out.chunk = fd_dcache_compact_next( ctx->out.chunk, cpy, ctx->out.chunk0, ctx->out.wmark );
235 :
236 0 : if( FD_LIKELY( ctx->full ) ) {
237 0 : ctx->metrics.full.compressed_bytes_read += cpy;
238 0 : ctx->metrics.full.decompressed_bytes_written += cpy;
239 0 : } else {
240 0 : ctx->metrics.incremental.compressed_bytes_read += cpy;
241 0 : ctx->metrics.incremental.decompressed_bytes_written += cpy;
242 0 : }
243 :
244 0 : ctx->in.frag_pos += cpy;
245 0 : FD_TEST( ctx->in.frag_pos<=sz );
246 0 : if( FD_UNLIKELY( ctx->in.frag_pos<sz ) ) return 1;
247 0 : ctx->in.frag_pos = 0UL;
248 0 : return 0;
249 0 : }
250 :
251 0 : ulong in_consumed = 0UL, out_produced = 0UL;
252 0 : ulong frame_res = ZSTD_decompressStream_simpleArgs(
253 0 : ctx->zstd,
254 0 : out,
255 0 : ctx->out.mtu,
256 0 : &out_produced,
257 0 : in,
258 0 : sz-ctx->in.frag_pos,
259 0 : &in_consumed );
260 0 : if( FD_UNLIKELY( ZSTD_isError( frame_res ) ) ) {
261 0 : FD_LOG_WARNING(( "error while decompressing %s snapshot (%u-%s)",
262 0 : ctx->full ? "full" : "incremental",
263 0 : ZSTD_getErrorCode( frame_res ), ZSTD_getErrorName( frame_res ) ));
264 0 : ctx->state = FD_SNAPSHOT_STATE_ERROR;
265 0 : fd_stem_publish( stem, 0UL, FD_SNAPSHOT_MSG_CTRL_ERROR, 0UL, 0UL, 0UL, 0UL, 0UL );
266 0 : return 0;
267 0 : }
268 :
269 0 : if( FD_LIKELY( out_produced ) ) {
270 0 : fd_stem_publish( stem, 0UL, FD_SNAPSHOT_MSG_DATA, ctx->out.chunk, out_produced, 0UL, 0UL, 0UL );
271 0 : ctx->out.chunk = fd_dcache_compact_next( ctx->out.chunk, out_produced, ctx->out.chunk0, ctx->out.wmark );
272 0 : }
273 :
274 0 : ctx->in.frag_pos += in_consumed;
275 0 : FD_TEST( ctx->in.frag_pos<=sz );
276 :
277 0 : if( FD_LIKELY( ctx->full ) ) {
278 0 : ctx->metrics.full.compressed_bytes_read += in_consumed;
279 0 : ctx->metrics.full.decompressed_bytes_written += out_produced;
280 0 : } else {
281 0 : ctx->metrics.incremental.compressed_bytes_read += in_consumed;
282 0 : ctx->metrics.incremental.decompressed_bytes_written += out_produced;
283 0 : }
284 :
285 0 : ctx->dirty = frame_res!=0UL;
286 :
287 : /* frame_res==0 means the frame ended exactly at the output boundary;
288 : re-polling then reports "new frame expected" and would mark the
289 : stream dirty at a clean EOF. */
290 0 : int maybe_more_output = (out_produced==ctx->out.mtu && frame_res!=0UL) || ctx->in.frag_pos<sz;
291 0 : if( FD_LIKELY( !maybe_more_output ) ) ctx->in.frag_pos = 0UL;
292 0 : return maybe_more_output;
293 0 : }
294 :
295 : static inline int
296 : returnable_frag( fd_snapdc_tile_t * ctx,
297 : ulong in_idx FD_PARAM_UNUSED,
298 : ulong seq FD_PARAM_UNUSED,
299 : ulong sig,
300 : ulong chunk,
301 : ulong sz,
302 : ulong ctl FD_PARAM_UNUSED,
303 : ulong tsorig FD_PARAM_UNUSED,
304 : ulong tspub FD_PARAM_UNUSED,
305 0 : fd_stem_context_t * stem ) {
306 0 : FD_TEST( ctx->state!=FD_SNAPSHOT_STATE_SHUTDOWN );
307 :
308 0 : if( FD_LIKELY( sig==FD_SNAPSHOT_MSG_DATA ) ) return handle_data_frag( ctx, stem, chunk, sz );
309 0 : else handle_control_frag( ctx, stem, sig, chunk, sz );
310 :
311 0 : return 0;
312 0 : }
313 :
314 : static ulong
315 : populate_allowed_fds( fd_topo_t const * topo FD_PARAM_UNUSED,
316 : fd_topo_tile_t const * tile FD_PARAM_UNUSED,
317 : ulong out_fds_cnt,
318 0 : int * out_fds ) {
319 0 : if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
320 :
321 0 : ulong out_cnt = 0;
322 0 : out_fds[ out_cnt++ ] = 2UL; /* stderr */
323 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) ) {
324 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
325 0 : }
326 :
327 0 : return out_cnt;
328 0 : }
329 :
330 : static ulong
331 : populate_allowed_seccomp( fd_topo_t const * topo FD_PARAM_UNUSED,
332 : fd_topo_tile_t const * tile FD_PARAM_UNUSED,
333 : ulong out_cnt,
334 0 : struct sock_filter * out ) {
335 0 : populate_sock_filter_policy_fd_snapdc_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
336 0 : return sock_filter_policy_fd_snapdc_tile_instr_cnt;
337 0 : }
338 :
339 : static void
340 : unprivileged_init( fd_topo_t const * topo,
341 0 : fd_topo_tile_t const * tile ) {
342 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
343 :
344 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
345 0 : fd_snapdc_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapdc_tile_t), sizeof(fd_snapdc_tile_t) );
346 0 : void * _zstd = FD_SCRATCH_ALLOC_APPEND( l, 32UL, ZSTD_estimateDStreamSize( ZSTD_WINDOW_SZ ) );
347 :
348 0 : ctx->state = FD_SNAPSHOT_STATE_IDLE;
349 :
350 0 : ctx->zstd = ZSTD_initStaticDStream( _zstd, ZSTD_estimateDStreamSize( ZSTD_WINDOW_SZ ) );
351 0 : FD_TEST( ctx->zstd );
352 0 : FD_TEST( ctx->zstd==_zstd );
353 :
354 0 : ctx->dirty = 0;
355 0 : ctx->in.frag_pos = 0UL;
356 0 : fd_memset( &ctx->metrics, 0, sizeof(ctx->metrics) );
357 :
358 0 : if( FD_UNLIKELY( tile->in_cnt !=1UL ) ) FD_LOG_ERR(( "tile `" NAME "` has %lu ins, expected 1", tile->in_cnt ));
359 0 : if( FD_UNLIKELY( tile->out_cnt!=1UL ) ) FD_LOG_ERR(( "tile `" NAME "` has %lu outs, expected 1", tile->out_cnt ));
360 :
361 0 : fd_topo_link_t const * snapin_link = &topo->links[ tile->out_link_id[ 0UL ] ];
362 0 : FD_TEST( 0==strcmp( snapin_link->name, "snapdc_in" ) );
363 0 : ctx->out.mem = topo->workspaces[ topo->objs[ snapin_link->dcache_obj_id ].wksp_id ].wksp;
364 0 : ctx->out.chunk0 = fd_dcache_compact_chunk0( ctx->out.mem, snapin_link->dcache );
365 0 : ctx->out.wmark = fd_dcache_compact_wmark ( ctx->out.mem, snapin_link->dcache, snapin_link->mtu );
366 0 : ctx->out.chunk = ctx->out.chunk0;
367 0 : ctx->out.mtu = snapin_link->mtu;
368 :
369 0 : fd_topo_link_t const * in_link = &topo->links[ tile->in_link_id[ 0UL ] ];
370 0 : fd_topo_wksp_t const * in_wksp = &topo->workspaces[ topo->objs[ in_link->dcache_obj_id ].wksp_id ];
371 0 : ctx->in.mem = in_wksp->wksp;
372 0 : ctx->in.chunk0 = fd_dcache_compact_chunk0( ctx->in.mem, in_link->dcache );
373 0 : ctx->in.wmark = fd_dcache_compact_wmark( ctx->in.mem, in_link->dcache, in_link->mtu );
374 0 : ctx->in.mtu = in_link->mtu;
375 :
376 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
377 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
378 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu",
379 0 : scratch_top - (ulong)scratch - scratch_footprint( tile ),
380 0 : scratch_top,
381 0 : (ulong)scratch + scratch_footprint( tile ) ));
382 0 : }
383 :
384 : /* handle_data_frag can publish one data frag plus an error frag */
385 0 : #define STEM_BURST 2UL
386 :
387 0 : #define STEM_LAZY (128L*3000L)
388 :
389 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_snapdc_tile_t
390 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_snapdc_tile_t)
391 :
392 : #define STEM_CALLBACK_SHOULD_SHUTDOWN should_shutdown
393 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
394 0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
395 :
396 : #include "../../disco/stem/fd_stem.c"
397 :
398 : fd_topo_run_tile_t fd_tile_snapdc = {
399 : .name = NAME,
400 : .populate_allowed_fds = populate_allowed_fds,
401 : .populate_allowed_seccomp = populate_allowed_seccomp,
402 : .scratch_align = scratch_align,
403 : .scratch_footprint = scratch_footprint,
404 : .unprivileged_init = unprivileged_init,
405 : .run = stem_run,
406 : };
407 :
408 : #undef NAME
|