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_META ) ) return;
103 :
104 : /* All control messages cause us to want to reset the decompression stream */
105 0 : ulong error = ZSTD_DCtx_reset( ctx->zstd, ZSTD_reset_session_only );
106 0 : if( FD_UNLIKELY( ZSTD_isError( error ) ) ) FD_LOG_ERR(( "ZSTD_DCtx_reset failed (%lu-%s)", error, ZSTD_getErrorName( error ) ));
107 :
108 0 : if( ctx->state==FD_SNAPSHOT_STATE_ERROR && sig!=FD_SNAPSHOT_MSG_CTRL_FAIL ) {
109 : /* Control messages move along the snapshot load pipeline. Since
110 : error conditions can be triggered by any tile in the pipeline,
111 : it is possible to be in error state and still receive otherwise
112 : valid messages. Only a fail message can revert this. */
113 0 : return;
114 0 : };
115 :
116 0 : int forward_msg = 1;
117 :
118 0 : switch( sig ) {
119 0 : case FD_SNAPSHOT_MSG_CTRL_INIT_FULL:
120 0 : case FD_SNAPSHOT_MSG_CTRL_INIT_INCR: {
121 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_IDLE );
122 0 : ctx->state = FD_SNAPSHOT_STATE_PROCESSING;
123 0 : FD_TEST( sz==sizeof(fd_ssctrl_init_t) );
124 0 : fd_ssctrl_init_t const * msg = fd_chunk_to_laddr_const( ctx->in.mem, chunk );
125 0 : ctx->full = sig==FD_SNAPSHOT_MSG_CTRL_INIT_FULL;
126 0 : ctx->is_zstd = !!msg->zstd;
127 0 : ctx->dirty = 0;
128 0 : ctx->in.frag_pos = 0UL;
129 0 : if( ctx->full ) {
130 0 : ctx->metrics.full.compressed_bytes_read = 0UL;
131 0 : ctx->metrics.full.decompressed_bytes_written = 0UL;
132 0 : } else {
133 0 : ctx->metrics.incremental.compressed_bytes_read = 0UL;
134 0 : ctx->metrics.incremental.decompressed_bytes_written = 0UL;
135 0 : }
136 0 : fd_ssctrl_init_t * msg_out = fd_chunk_to_laddr( ctx->out.mem, ctx->out.chunk );
137 0 : fd_memcpy( msg_out, msg, sz );
138 0 : fd_stem_publish( stem, 0UL, sig, ctx->out.chunk, sz, 0UL, 0UL, 0UL );
139 0 : ctx->out.chunk = fd_dcache_compact_next( ctx->out.chunk, ctx->out.mtu, ctx->out.chunk0, ctx->out.wmark );
140 0 : forward_msg = 0; // we forward the control message in the `fd_ssctrl_init_t` message
141 0 : break;
142 0 : }
143 :
144 0 : case FD_SNAPSHOT_MSG_CTRL_FINI: {
145 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_PROCESSING );
146 0 : ctx->state = FD_SNAPSHOT_STATE_FINISHING;
147 0 : if( FD_UNLIKELY( ctx->is_zstd && ctx->dirty ) ) {
148 0 : FD_LOG_WARNING(( "encountered end-of-file in the middle of a compressed frame for %s snapshot",
149 0 : ctx->full ? "full" : "incremental" ));
150 0 : transition_malformed( ctx, stem );
151 0 : forward_msg = 0;
152 0 : break;
153 0 : }
154 0 : break;
155 0 : }
156 :
157 0 : case FD_SNAPSHOT_MSG_CTRL_NEXT:
158 0 : case FD_SNAPSHOT_MSG_CTRL_DONE: {
159 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_FINISHING );
160 0 : ctx->state = FD_SNAPSHOT_STATE_IDLE;
161 0 : break;
162 0 : }
163 :
164 0 : case FD_SNAPSHOT_MSG_CTRL_ERROR: {
165 0 : FD_TEST( ctx->state!=FD_SNAPSHOT_STATE_SHUTDOWN );
166 0 : ctx->state = FD_SNAPSHOT_STATE_ERROR;
167 0 : break;
168 0 : }
169 :
170 0 : case FD_SNAPSHOT_MSG_CTRL_FAIL: {
171 0 : FD_TEST( ctx->state!=FD_SNAPSHOT_STATE_SHUTDOWN );
172 0 : ctx->state = FD_SNAPSHOT_STATE_IDLE;
173 0 : break;
174 0 : }
175 :
176 0 : case FD_SNAPSHOT_MSG_CTRL_SHUTDOWN: {
177 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_IDLE );
178 0 : ctx->state = FD_SNAPSHOT_STATE_SHUTDOWN;
179 0 : break;
180 0 : }
181 :
182 0 : default: {
183 0 : FD_LOG_ERR(( "unexpected control frag %s (%lu) in state %s (%lu)",
184 0 : fd_ssctrl_msg_ctrl_str( sig ), sig,
185 0 : fd_ssctrl_state_str( (ulong)ctx->state ), (ulong)ctx->state ));
186 0 : break;
187 0 : }
188 0 : }
189 :
190 : /* Forward the control message down the pipeline */
191 0 : if( FD_LIKELY( forward_msg ) ) {
192 0 : fd_stem_publish( stem, 0UL, sig, 0UL, 0UL, 0UL, 0UL, 0UL );
193 0 : }
194 0 : }
195 :
196 : static inline int
197 : handle_data_frag( fd_snapdc_tile_t * ctx,
198 : fd_stem_context_t * stem,
199 : ulong chunk,
200 0 : ulong sz ) {
201 0 : if( FD_UNLIKELY( ctx->state==FD_SNAPSHOT_STATE_ERROR ) ) {
202 : /* Ignore all data frags after observing an error in the stream until
203 : we receive fail & init control messages to restart processing. */
204 0 : return 0;
205 0 : }
206 0 : if( FD_UNLIKELY( ctx->state!=FD_SNAPSHOT_STATE_PROCESSING ) ) {
207 0 : FD_LOG_ERR(( "received unexpected data frag in state %s (%lu)",
208 0 : fd_ssctrl_state_str( (ulong)ctx->state ), (ulong)ctx->state ));
209 0 : }
210 :
211 0 : FD_TEST( chunk>=ctx->in.chunk0 && chunk<=ctx->in.wmark && sz<=ctx->in.mtu && sz>=ctx->in.frag_pos );
212 0 : uchar const * data = fd_chunk_to_laddr_const( ctx->in.mem, chunk );
213 0 : uchar const * in = data+ctx->in.frag_pos;
214 0 : uchar * out = fd_chunk_to_laddr( ctx->out.mem, ctx->out.chunk );
215 :
216 0 : if( FD_UNLIKELY( !ctx->is_zstd ) ) {
217 0 : FD_TEST( ctx->in.frag_pos<sz );
218 0 : ulong cpy = fd_ulong_min( sz-ctx->in.frag_pos, ctx->out.mtu );
219 0 : fd_memcpy( out, in, cpy );
220 0 : fd_stem_publish( stem, 0UL, FD_SNAPSHOT_MSG_DATA, ctx->out.chunk, cpy, 0UL, 0UL, 0UL );
221 0 : ctx->out.chunk = fd_dcache_compact_next( ctx->out.chunk, cpy, ctx->out.chunk0, ctx->out.wmark );
222 :
223 0 : if( FD_LIKELY( ctx->full ) ) {
224 0 : ctx->metrics.full.compressed_bytes_read += cpy;
225 0 : ctx->metrics.full.decompressed_bytes_written += cpy;
226 0 : } else {
227 0 : ctx->metrics.incremental.compressed_bytes_read += cpy;
228 0 : ctx->metrics.incremental.decompressed_bytes_written += cpy;
229 0 : }
230 :
231 0 : ctx->in.frag_pos += cpy;
232 0 : FD_TEST( ctx->in.frag_pos<=sz );
233 0 : if( FD_UNLIKELY( ctx->in.frag_pos<sz ) ) return 1;
234 0 : ctx->in.frag_pos = 0UL;
235 0 : return 0;
236 0 : }
237 :
238 0 : ulong in_consumed = 0UL, out_produced = 0UL;
239 0 : ulong frame_res = ZSTD_decompressStream_simpleArgs(
240 0 : ctx->zstd,
241 0 : out,
242 0 : ctx->out.mtu,
243 0 : &out_produced,
244 0 : in,
245 0 : sz-ctx->in.frag_pos,
246 0 : &in_consumed );
247 0 : if( FD_UNLIKELY( ZSTD_isError( frame_res ) ) ) {
248 0 : FD_LOG_WARNING(( "error while decompressing %s snapshot (%u-%s)",
249 0 : ctx->full ? "full" : "incremental",
250 0 : ZSTD_getErrorCode( frame_res ), ZSTD_getErrorName( frame_res ) ));
251 0 : ctx->state = FD_SNAPSHOT_STATE_ERROR;
252 0 : fd_stem_publish( stem, 0UL, FD_SNAPSHOT_MSG_CTRL_ERROR, 0UL, 0UL, 0UL, 0UL, 0UL );
253 0 : return 0;
254 0 : }
255 :
256 0 : if( FD_LIKELY( out_produced ) ) {
257 0 : fd_stem_publish( stem, 0UL, FD_SNAPSHOT_MSG_DATA, ctx->out.chunk, out_produced, 0UL, 0UL, 0UL );
258 0 : ctx->out.chunk = fd_dcache_compact_next( ctx->out.chunk, out_produced, ctx->out.chunk0, ctx->out.wmark );
259 0 : }
260 :
261 0 : ctx->in.frag_pos += in_consumed;
262 0 : FD_TEST( ctx->in.frag_pos<=sz );
263 :
264 0 : if( FD_LIKELY( ctx->full ) ) {
265 0 : ctx->metrics.full.compressed_bytes_read += in_consumed;
266 0 : ctx->metrics.full.decompressed_bytes_written += out_produced;
267 0 : } else {
268 0 : ctx->metrics.incremental.compressed_bytes_read += in_consumed;
269 0 : ctx->metrics.incremental.decompressed_bytes_written += out_produced;
270 0 : }
271 :
272 0 : ctx->dirty = frame_res!=0UL;
273 :
274 0 : int maybe_more_output = out_produced==ctx->out.mtu || ctx->in.frag_pos<sz;
275 0 : if( FD_LIKELY( !maybe_more_output ) ) ctx->in.frag_pos = 0UL;
276 0 : return maybe_more_output;
277 0 : }
278 :
279 : static inline int
280 : returnable_frag( fd_snapdc_tile_t * ctx,
281 : ulong in_idx FD_PARAM_UNUSED,
282 : ulong seq FD_PARAM_UNUSED,
283 : ulong sig,
284 : ulong chunk,
285 : ulong sz,
286 : ulong ctl FD_PARAM_UNUSED,
287 : ulong tsorig FD_PARAM_UNUSED,
288 : ulong tspub FD_PARAM_UNUSED,
289 0 : fd_stem_context_t * stem ) {
290 0 : FD_TEST( ctx->state!=FD_SNAPSHOT_STATE_SHUTDOWN );
291 :
292 0 : if( FD_LIKELY( sig==FD_SNAPSHOT_MSG_DATA ) ) return handle_data_frag( ctx, stem, chunk, sz );
293 0 : else handle_control_frag( ctx, stem, sig, chunk, sz );
294 :
295 0 : return 0;
296 0 : }
297 :
298 : static ulong
299 : populate_allowed_fds( fd_topo_t const * topo FD_PARAM_UNUSED,
300 : fd_topo_tile_t const * tile FD_PARAM_UNUSED,
301 : ulong out_fds_cnt,
302 0 : int * out_fds ) {
303 0 : if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
304 :
305 0 : ulong out_cnt = 0;
306 0 : out_fds[ out_cnt++ ] = 2UL; /* stderr */
307 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) ) {
308 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
309 0 : }
310 :
311 0 : return out_cnt;
312 0 : }
313 :
314 : static ulong
315 : populate_allowed_seccomp( fd_topo_t const * topo FD_PARAM_UNUSED,
316 : fd_topo_tile_t const * tile FD_PARAM_UNUSED,
317 : ulong out_cnt,
318 0 : struct sock_filter * out ) {
319 0 : populate_sock_filter_policy_fd_snapdc_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
320 0 : return sock_filter_policy_fd_snapdc_tile_instr_cnt;
321 0 : }
322 :
323 : static void
324 : unprivileged_init( fd_topo_t * topo,
325 0 : fd_topo_tile_t * tile ) {
326 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
327 :
328 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
329 0 : fd_snapdc_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapdc_tile_t), sizeof(fd_snapdc_tile_t) );
330 0 : void * _zstd = FD_SCRATCH_ALLOC_APPEND( l, 32UL, ZSTD_estimateDStreamSize( ZSTD_WINDOW_SZ ) );
331 :
332 0 : ctx->state = FD_SNAPSHOT_STATE_IDLE;
333 :
334 0 : ctx->zstd = ZSTD_initStaticDStream( _zstd, ZSTD_estimateDStreamSize( ZSTD_WINDOW_SZ ) );
335 0 : FD_TEST( ctx->zstd );
336 0 : FD_TEST( ctx->zstd==_zstd );
337 :
338 0 : ctx->dirty = 0;
339 0 : ctx->in.frag_pos = 0UL;
340 0 : fd_memset( &ctx->metrics, 0, sizeof(ctx->metrics) );
341 :
342 0 : if( FD_UNLIKELY( tile->in_cnt !=1UL ) ) FD_LOG_ERR(( "tile `" NAME "` has %lu ins, expected 1", tile->in_cnt ));
343 0 : if( FD_UNLIKELY( tile->out_cnt!=1UL ) ) FD_LOG_ERR(( "tile `" NAME "` has %lu outs, expected 1", tile->out_cnt ));
344 :
345 0 : fd_topo_link_t * snapin_link = &topo->links[ tile->out_link_id[ 0UL ] ];
346 0 : FD_TEST( 0==strcmp( snapin_link->name, "snapdc_in" ) );
347 0 : ctx->out.mem = topo->workspaces[ topo->objs[ snapin_link->dcache_obj_id ].wksp_id ].wksp;
348 0 : ctx->out.chunk0 = fd_dcache_compact_chunk0( ctx->out.mem, snapin_link->dcache );
349 0 : ctx->out.wmark = fd_dcache_compact_wmark ( ctx->out.mem, snapin_link->dcache, snapin_link->mtu );
350 0 : ctx->out.chunk = ctx->out.chunk0;
351 0 : ctx->out.mtu = snapin_link->mtu;
352 :
353 0 : fd_topo_link_t const * in_link = &topo->links[ tile->in_link_id[ 0UL ] ];
354 0 : fd_topo_wksp_t const * in_wksp = &topo->workspaces[ topo->objs[ in_link->dcache_obj_id ].wksp_id ];
355 0 : ctx->in.mem = in_wksp->wksp;
356 0 : ctx->in.chunk0 = fd_dcache_compact_chunk0( ctx->in.mem, in_link->dcache );
357 0 : ctx->in.wmark = fd_dcache_compact_wmark( ctx->in.mem, in_link->dcache, in_link->mtu );
358 0 : ctx->in.mtu = in_link->mtu;
359 :
360 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
361 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
362 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu",
363 0 : scratch_top - (ulong)scratch - scratch_footprint( tile ),
364 0 : scratch_top,
365 0 : (ulong)scratch + scratch_footprint( tile ) ));
366 0 : }
367 :
368 : /* handle_data_frag can publish one data frag plus an error frag */
369 0 : #define STEM_BURST 2UL
370 :
371 0 : #define STEM_LAZY 1000L
372 :
373 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_snapdc_tile_t
374 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_snapdc_tile_t)
375 :
376 : #define STEM_CALLBACK_SHOULD_SHUTDOWN should_shutdown
377 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
378 0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
379 :
380 : #include "../../disco/stem/fd_stem.c"
381 :
382 : fd_topo_run_tile_t fd_tile_snapdc = {
383 : .name = NAME,
384 : .populate_allowed_fds = populate_allowed_fds,
385 : .populate_allowed_seccomp = populate_allowed_seccomp,
386 : .scratch_align = scratch_align,
387 : .scratch_footprint = scratch_footprint,
388 : .unprivileged_init = unprivileged_init,
389 : .run = stem_run,
390 : };
391 :
392 : #undef NAME
|