Line data Source code
1 : #include "../tiles.h"
2 :
3 : #include "fd_archiver.h"
4 : #include <unistd.h>
5 : #include <linux/unistd.h>
6 : #include <sys/socket.h>
7 : #include <linux/if_xdp.h>
8 : #include "generated/archiver_feeder_seccomp.h"
9 :
10 : /* The archiver feeder tiles forward fragments that we want to capture from a subset of
11 : the input links to the single archiver writer tile.
12 :
13 : There can (should) be many archiver feeder tiles, and a single archiver writer tile.
14 :
15 : This can be used to set up a variety of flexible topologies for capturing, such as round-robin
16 : or 1-1. */
17 :
18 : #define FD_ARCHIVER_FEEDER_MAX_INPUT_LINKS (32UL)
19 :
20 : typedef struct {
21 : fd_wksp_t * mem;
22 : ulong chunk0;
23 : ulong wmark;
24 : } fd_archiver_feeder_in_ctx_t;
25 :
26 : struct fd_archiver_feeder_tile_ctx {
27 : fd_wksp_t * out_mem;
28 : ulong out_chunk0;
29 : ulong out_wmark;
30 : ulong out_chunk;
31 :
32 : ulong count;
33 :
34 : ulong round_robin_idx;
35 : ulong round_robin_cnt;
36 :
37 : /* Input links */
38 : fd_archiver_feeder_in_ctx_t in[ FD_ARCHIVER_FEEDER_MAX_INPUT_LINKS ];
39 : };
40 : typedef struct fd_archiver_feeder_tile_ctx fd_archiver_feeder_tile_ctx_t;
41 :
42 : FD_FN_CONST static inline ulong
43 0 : scratch_align( void ) {
44 0 : return 4096UL;
45 0 : }
46 :
47 : static ulong
48 : populate_allowed_seccomp( fd_topo_t const * topo,
49 : fd_topo_tile_t const * tile,
50 : ulong out_cnt,
51 0 : struct sock_filter * out ) {
52 0 : (void)topo;
53 0 : (void)tile;
54 :
55 0 : populate_sock_filter_policy_archiver_feeder( out_cnt, out, (uint)fd_log_private_logfile_fd() );
56 0 : return sock_filter_policy_archiver_feeder_instr_cnt;
57 0 : }
58 :
59 : static ulong
60 : populate_allowed_fds( fd_topo_t const * topo,
61 : fd_topo_tile_t const * tile,
62 : ulong out_fds_cnt,
63 0 : int * out_fds ) {
64 0 : (void)topo;
65 0 : (void)tile;
66 0 : (void)out_fds_cnt;
67 :
68 0 : ulong out_cnt = 0UL;
69 :
70 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
71 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
72 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
73 :
74 0 : return out_cnt;
75 0 : }
76 :
77 : FD_FN_PURE static inline ulong
78 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
79 0 : (void)tile;
80 0 : ulong l = FD_LAYOUT_INIT;
81 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_archiver_feeder_tile_ctx_t), sizeof(fd_archiver_feeder_tile_ctx_t) );
82 0 : return FD_LAYOUT_FINI( l, scratch_align() );
83 0 : }
84 :
85 : static void
86 : unprivileged_init( fd_topo_t * topo,
87 0 : fd_topo_tile_t * tile ) {
88 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
89 :
90 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
91 0 : fd_archiver_feeder_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_archiver_feeder_tile_ctx_t), sizeof(fd_archiver_feeder_tile_ctx_t) );
92 0 : memset( ctx, 0, sizeof(fd_archiver_feeder_tile_ctx_t) );
93 :
94 0 : ctx->round_robin_cnt = fd_topo_tile_name_cnt( topo, tile->name );
95 0 : ctx->round_robin_idx = tile->kind_id;
96 :
97 0 : for( ulong i=0; i<tile->in_cnt; i++ ) {
98 0 : fd_topo_link_t * link = &topo->links[ tile->in_link_id[ i ] ];
99 0 : fd_topo_wksp_t * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
100 :
101 0 : ctx->in[ i ].mem = link_wksp->wksp;
102 0 : ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
103 0 : ctx->in[ i ].wmark = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
104 0 : }
105 :
106 0 : ctx->out_mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ 0 ] ].dcache_obj_id ].wksp_id ].wksp;
107 0 : ctx->out_chunk0 = fd_dcache_compact_chunk0( ctx->out_mem, topo->links[ tile->out_link_id[ 0 ] ].dcache );
108 0 : ctx->out_wmark = fd_dcache_compact_wmark ( ctx->out_mem, topo->links[ tile->out_link_id[ 0 ] ].dcache, topo->links[ tile->out_link_id[ 0 ] ].mtu );
109 0 : ctx->out_chunk = ctx->out_chunk0;
110 :
111 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
112 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) ) {
113 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
114 0 : }
115 0 : }
116 :
117 : // TODO: if you wanted, you could round-robin using before_frag
118 :
119 : static inline void
120 : during_frag( fd_archiver_feeder_tile_ctx_t * ctx,
121 : ulong in_idx,
122 : ulong seq,
123 : ulong sig,
124 : ulong chunk,
125 : ulong sz,
126 0 : ulong ctl FD_PARAM_UNUSED ) {
127 : /* TODO: filter by signature in before_credit */
128 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark ) ) {
129 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
130 0 : }
131 :
132 0 : uchar * src = (uchar *)fd_chunk_to_laddr( ctx->in[in_idx].mem, chunk );
133 0 : uchar * dst = (uchar *)fd_chunk_to_laddr( ctx->out_mem, ctx->out_chunk );
134 :
135 0 : if( FD_LIKELY( sz ) ) {
136 : /* Write the header to the dst */
137 0 : fd_archiver_frag_header_t * header = fd_type_pun( dst );
138 0 : header->magic = FD_ARCHIVER_HEADER_MAGIC;
139 0 : header->version = FD_ARCHIVER_HEADER_VERSION;
140 0 : header->tile_id = FD_ARCHIVER_SIG_TILE_ID(sig);
141 : /* header->ns_since_prev_fragment is set in the single writer tile, so that we have a total order */
142 0 : header->sz = sz;
143 0 : header->sig = FD_ARCHIVER_SIG_CLEAR(sig);
144 0 : header->seq = seq;
145 :
146 : /* Write the frag to the dst */
147 0 : fd_memcpy( dst + FD_ARCHIVER_FRAG_HEADER_FOOTPRINT, src, sz );
148 0 : }
149 0 : }
150 :
151 : static inline void
152 : after_frag( fd_archiver_feeder_tile_ctx_t * ctx,
153 : ulong in_idx FD_PARAM_UNUSED,
154 : ulong seq FD_PARAM_UNUSED,
155 : ulong sig FD_PARAM_UNUSED,
156 : ulong sz,
157 : ulong tsorig,
158 : ulong tspub FD_PARAM_UNUSED,
159 0 : fd_stem_context_t * stem ) {
160 : /* Publish the message to the queue */
161 0 : ulong full_sz = sz + FD_ARCHIVER_FRAG_HEADER_FOOTPRINT;
162 0 : fd_stem_publish( stem, 0UL, 0UL, ctx->out_chunk, full_sz, 0UL, tsorig, 0UL);
163 0 : ctx->out_chunk = fd_dcache_compact_next( ctx->out_chunk, full_sz, ctx->out_chunk0, ctx->out_wmark );
164 0 : }
165 :
166 0 : #define STEM_BURST (1UL)
167 :
168 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_archiver_feeder_tile_ctx_t
169 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_archiver_feeder_tile_ctx_t)
170 :
171 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
172 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
173 :
174 : #include "../stem/fd_stem.c"
175 :
176 : fd_topo_run_tile_t fd_tile_archiver_feeder = {
177 : .name = "arch_f",
178 : .populate_allowed_seccomp = populate_allowed_seccomp,
179 : .populate_allowed_fds = populate_allowed_fds,
180 : .scratch_align = scratch_align,
181 : .scratch_footprint = scratch_footprint,
182 : .unprivileged_init = unprivileged_init,
183 : .run = stem_run,
184 : };
|