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