|
| 1 | +#include <stdlib.h> |
| 2 | +#define _GNU_SOURCE |
| 3 | +#include "../../disco/tiles.h" |
| 4 | +#include "generated/fd_arxiv_tile_seccomp.h" |
| 5 | +#include "fd_shred_arxiv.h" |
| 6 | +#include "../../disco/topo/fd_pod_format.h" |
| 7 | + |
| 8 | +#define REPLAY_IN_IDX 0UL |
| 9 | + |
| 10 | +struct fd_arxiv_tile_ctx { |
| 11 | + fd_shred_arxiver_t * arxiver; |
| 12 | + int arxiver_fd; /* file descriptor for the archive file */ |
| 13 | + fd_blockstore_t blockstore_ljoin; |
| 14 | + fd_blockstore_t * blockstore; |
| 15 | + |
| 16 | + void * replay_in_mem; |
| 17 | + ulong replay_in_chunk0; |
| 18 | + ulong replay_in_wmark; |
| 19 | +}; |
| 20 | +typedef struct fd_arxiv_tile_ctx fd_arxiv_tile_ctx_t; |
| 21 | + |
| 22 | +FD_FN_CONST static inline ulong |
| 23 | +scratch_align( void ) { |
| 24 | + return 128UL; |
| 25 | +} |
| 26 | + |
| 27 | +FD_FN_PURE static inline ulong |
| 28 | +scratch_footprint( fd_topo_tile_t const * tile FD_PARAM_UNUSED ) { |
| 29 | + /* clang-format off */ |
| 30 | + ulong l = FD_LAYOUT_INIT; |
| 31 | + l = FD_LAYOUT_APPEND( l, alignof(fd_arxiv_tile_ctx_t), sizeof(fd_arxiv_tile_ctx_t) ); |
| 32 | + l = FD_LAYOUT_APPEND( l, fd_shred_arxiv_align(), fd_shred_arxiv_footprint( FD_SHRED_ARXIV_MIN_SIZE ) ); |
| 33 | + return FD_LAYOUT_FINI( l, scratch_align() ); |
| 34 | + /* clang-format on */ |
| 35 | +} |
| 36 | + |
| 37 | +static void |
| 38 | +during_frag( fd_arxiv_tile_ctx_t * ctx, |
| 39 | + ulong in_idx, |
| 40 | + ulong seq FD_PARAM_UNUSED, |
| 41 | + ulong sig FD_PARAM_UNUSED, |
| 42 | + ulong chunk, |
| 43 | + ulong sz, |
| 44 | + ulong ctl FD_PARAM_UNUSED ) { |
| 45 | + |
| 46 | + if( in_idx == REPLAY_IN_IDX ) { |
| 47 | + if( FD_UNLIKELY( chunk<ctx->replay_in_chunk0 || chunk>ctx->replay_in_wmark || sz>USHORT_MAX ) ) { |
| 48 | + FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->replay_in_chunk0, ctx->replay_in_wmark )); |
| 49 | + } |
| 50 | + ulong slot = fd_disco_replay_arxiv_sig_slot( sig ); |
| 51 | + uint start_idx = fd_disco_replay_arxiv_sig_start_idx( sig ); |
| 52 | + uint end_idx = fd_disco_replay_arxiv_sig_end_idx( sig ); |
| 53 | + |
| 54 | + fd_shreds_checkpt( ctx->arxiver, ctx->blockstore, slot, start_idx, end_idx ); |
| 55 | + } |
| 56 | + |
| 57 | + (void)chunk; |
| 58 | + (void)sz; |
| 59 | +} |
| 60 | + |
| 61 | +static void |
| 62 | +after_frag( fd_arxiv_tile_ctx_t * ctx, |
| 63 | + ulong in_idx FD_PARAM_UNUSED, |
| 64 | + ulong seq FD_PARAM_UNUSED, |
| 65 | + ulong sig FD_PARAM_UNUSED, |
| 66 | + ulong sz FD_PARAM_UNUSED, |
| 67 | + ulong tsorig FD_PARAM_UNUSED, |
| 68 | + ulong tspub FD_PARAM_UNUSED, |
| 69 | + fd_stem_context_t * stem FD_PARAM_UNUSED ) { |
| 70 | + (void)ctx; |
| 71 | + /* Let's test for fun that the shred was written to file properly */ |
| 72 | + if( in_idx == REPLAY_IN_IDX ) { |
| 73 | + FD_LOG_WARNING(( "replay in idx %lu", in_idx )); |
| 74 | + ulong slot = fd_disco_replay_arxiv_sig_slot( sig ); |
| 75 | + uint end_idx = fd_disco_replay_arxiv_sig_end_idx( sig ); |
| 76 | + |
| 77 | + fd_shred_idx_t * idx = fd_shred_idx_query( ctx->arxiver->shred_idx, slot << 32 | end_idx, NULL ); |
| 78 | + uchar shred_buf[FD_SHRED_MIN_SZ]; |
| 79 | + int err = fd_shred_restore( ctx->arxiver, idx, shred_buf, FD_SHRED_MIN_SZ ); |
| 80 | + FD_TEST( err == 0 ); |
| 81 | + const fd_shred_t * shred = fd_shred_parse( shred_buf, FD_SHRED_MIN_SZ ); |
| 82 | + FD_TEST( shred->slot == slot ); |
| 83 | + FD_TEST( shred->idx == end_idx ); |
| 84 | + } |
| 85 | + |
| 86 | +} |
| 87 | + |
| 88 | +static void |
| 89 | +privileged_init( fd_topo_t * topo FD_PARAM_UNUSED, |
| 90 | + fd_topo_tile_t * tile FD_PARAM_UNUSED ) { |
| 91 | + void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id ); |
| 92 | + |
| 93 | + FD_SCRATCH_ALLOC_INIT( l, scratch ); |
| 94 | + fd_arxiv_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_arxiv_tile_ctx_t), sizeof(fd_arxiv_tile_ctx_t) ); |
| 95 | + FD_SCRATCH_ALLOC_FINI( l, scratch_align() ); |
| 96 | + memset( ctx, 0, sizeof(fd_arxiv_tile_ctx_t) ); |
| 97 | + |
| 98 | + ctx->arxiver_fd = open( tile->arxiv.blockstore_file, O_RDWR|O_CREAT, 0666 ); |
| 99 | + if( FD_UNLIKELY( ctx->arxiver_fd==-1 ) ) { |
| 100 | + FD_LOG_ERR(( "failed to open arxiver fd" )); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | +} |
| 105 | + |
| 106 | +static void |
| 107 | +unprivileged_init( fd_topo_t * topo, |
| 108 | + fd_topo_tile_t * tile ) { |
| 109 | + void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id ); |
| 110 | + |
| 111 | + FD_SCRATCH_ALLOC_INIT( l, scratch ); |
| 112 | + fd_arxiv_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_arxiv_tile_ctx_t), sizeof(fd_arxiv_tile_ctx_t) ); |
| 113 | + void * arxiver = FD_SCRATCH_ALLOC_APPEND( l, fd_shred_arxiv_align(), fd_shred_arxiv_footprint( FD_SHRED_ARXIV_MIN_SIZE ) ); |
| 114 | + ulong scratch_alloc_mem = FD_SCRATCH_ALLOC_FINI( l, scratch_align() ); |
| 115 | + if( FD_UNLIKELY( scratch_alloc_mem - (ulong)scratch - scratch_footprint( tile ) ) ) { |
| 116 | + FD_LOG_ERR(( "scratch footprint mismatch" )); |
| 117 | + } |
| 118 | + |
| 119 | + ulong blockstore_obj_id = fd_pod_queryf_ulong( topo->props, ULONG_MAX,"blockstore" ); |
| 120 | + FD_TEST( blockstore_obj_id!=ULONG_MAX ); |
| 121 | + ctx->blockstore = fd_blockstore_join( &ctx->blockstore_ljoin, fd_topo_obj_laddr( topo, blockstore_obj_id ) ); |
| 122 | + |
| 123 | + FD_TEST( ctx->blockstore->shmem->magic == FD_BLOCKSTORE_MAGIC ); |
| 124 | + |
| 125 | + ctx->arxiver = fd_shred_arxiv_join( fd_shred_arxiv_new( arxiver, FD_SHRED_ARXIV_MIN_SIZE ) ); |
| 126 | + ctx->arxiver->fd = ctx->arxiver_fd; |
| 127 | + |
| 128 | + /**********************************************************************/ |
| 129 | + /* links */ |
| 130 | + /**********************************************************************/ |
| 131 | + |
| 132 | + /* Setup replay tile input */ |
| 133 | + fd_topo_link_t * replay_in_link = &topo->links[ tile->in_link_id[ REPLAY_IN_IDX ] ]; |
| 134 | + ctx->replay_in_mem = topo->workspaces[ topo->objs[ replay_in_link->dcache_obj_id ].wksp_id ].wksp; |
| 135 | + ctx->replay_in_chunk0 = fd_dcache_compact_chunk0( ctx->replay_in_mem, replay_in_link->dcache ); |
| 136 | + ctx->replay_in_wmark = fd_dcache_compact_wmark( ctx->replay_in_mem, replay_in_link->dcache, replay_in_link->mtu ); |
| 137 | + |
| 138 | + |
| 139 | +} |
| 140 | + |
| 141 | +static void |
| 142 | +after_credit( fd_arxiv_tile_ctx_t * ctx, |
| 143 | + fd_stem_context_t * stem FD_PARAM_UNUSED, |
| 144 | + int * opt_poll_in FD_PARAM_UNUSED, |
| 145 | + int * charge_busy FD_PARAM_UNUSED ) { |
| 146 | + (void)ctx; |
| 147 | +} |
| 148 | + |
| 149 | +static ulong |
| 150 | +populate_allowed_seccomp( fd_topo_t const * topo, |
| 151 | + fd_topo_tile_t const * tile, |
| 152 | + ulong out_cnt, |
| 153 | + struct sock_filter * out ) { |
| 154 | + |
| 155 | + void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id ); |
| 156 | + |
| 157 | + FD_SCRATCH_ALLOC_INIT( l, scratch ); |
| 158 | + fd_arxiv_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_arxiv_tile_ctx_t), sizeof(fd_arxiv_tile_ctx_t) ); |
| 159 | + FD_SCRATCH_ALLOC_FINI( l, sizeof(fd_arxiv_tile_ctx_t) ); |
| 160 | + |
| 161 | + populate_sock_filter_policy_fd_arxiv_tile( out_cnt, out, (uint)fd_log_private_logfile_fd(), (uint)ctx->arxiver_fd ); |
| 162 | + return sock_filter_policy_fd_arxiv_tile_instr_cnt; |
| 163 | +} |
| 164 | + |
| 165 | +static ulong |
| 166 | +populate_allowed_fds( fd_topo_t const * topo, |
| 167 | + fd_topo_tile_t const * tile, |
| 168 | + ulong out_fds_cnt, |
| 169 | + int * out_fds ) { |
| 170 | + void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id ); |
| 171 | + |
| 172 | + FD_SCRATCH_ALLOC_INIT( l, scratch ); |
| 173 | + fd_arxiv_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_arxiv_tile_ctx_t), sizeof(fd_arxiv_tile_ctx_t) ); |
| 174 | + FD_SCRATCH_ALLOC_FINI( l, sizeof(fd_arxiv_tile_ctx_t) ); |
| 175 | + |
| 176 | + if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt )); |
| 177 | + |
| 178 | + ulong out_cnt = 0UL; |
| 179 | + out_fds[ out_cnt++ ] = 2; /* stderr */ |
| 180 | + if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) ) |
| 181 | + out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */ |
| 182 | + out_fds[ out_cnt++ ] = ctx->arxiver_fd; /* shred store fd */ |
| 183 | + return out_cnt; |
| 184 | +} |
| 185 | + |
| 186 | + |
| 187 | +#define STEM_BURST (1UL) |
| 188 | + |
| 189 | +#define STEM_CALLBACK_CONTEXT_TYPE fd_arxiv_tile_ctx_t |
| 190 | +#define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_arxiv_tile_ctx_t) |
| 191 | + |
| 192 | +#define STEM_CALLBACK_DURING_FRAG during_frag |
| 193 | +#define STEM_CALLBACK_AFTER_FRAG after_frag |
| 194 | +#define STEM_CALLBACK_AFTER_CREDIT after_credit |
| 195 | + |
| 196 | +#include "../../disco/stem/fd_stem.c" |
| 197 | + |
| 198 | +fd_topo_run_tile_t fd_tile_arxiv = { |
| 199 | + .name = "arxiv", |
| 200 | + .loose_footprint = 0UL, |
| 201 | + .populate_allowed_seccomp = populate_allowed_seccomp, |
| 202 | + .populate_allowed_fds = populate_allowed_fds, |
| 203 | + .scratch_align = scratch_align, |
| 204 | + .scratch_footprint = scratch_footprint, |
| 205 | + .privileged_init = privileged_init, |
| 206 | + .unprivileged_init = unprivileged_init, |
| 207 | + .run = stem_run, |
| 208 | +}; |
0 commit comments