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