Line data Source code
1 : #define _GNU_SOURCE
2 : #include "fd_topo.h"
3 :
4 : #include "../metrics/fd_metrics.h"
5 : #include "../../waltz/xdp/fd_xdp1.h"
6 : #include "../../util/tile/fd_tile_private.h"
7 :
8 : #include <unistd.h>
9 : #include <signal.h>
10 : #include <errno.h>
11 : #include <pthread.h>
12 : #include <sys/syscall.h>
13 : #include <linux/futex.h>
14 : #include <sys/resource.h>
15 : #include <sys/prctl.h>
16 : #include <sys/stat.h>
17 : #include <sys/mman.h>
18 : #include <net/if.h>
19 :
20 : static void
21 : initialize_logging( char const * tile_name,
22 : ulong tile_kind_id,
23 : ulong pid,
24 0 : ulong tid ) {
25 0 : fd_log_cpu_set( NULL );
26 0 : fd_log_private_tid_set( pid );
27 0 : char thread_name[ 20 ];
28 0 : FD_TEST( fd_cstr_printf_check( thread_name, sizeof( thread_name ), NULL, "%s:%lu", tile_name, tile_kind_id ) );
29 0 : fd_log_thread_set( thread_name );
30 0 : fd_log_private_stack_discover( FD_TILE_PRIVATE_STACK_SZ,
31 0 : &fd_tile_private_stack0, &fd_tile_private_stack1 );
32 0 : FD_LOG_INFO(( "booting tile %s pid:%lu tid:%lu", thread_name, fd_log_group_id(), tid ));
33 :
34 : /* FD_LOG_* calls fd_log_wallclock_cstr, which calls localtime_r. In
35 : glibc, this ends up calling a function called tzset_internal. The
36 : first time tzset_internal is called by a process, it may (and
37 : almost always does) call __tzfile_read, which invokes the openat
38 : syscall, and possibly several others on the time zone file
39 : (typically /etc/localtime) or on a file in the time zone directory.
40 : This kind of behavior is tricky to sandbox, so the easiest thing to
41 : do is initialize it prior to the sandbox and hope whatever libc is
42 : used behaves like glibc. This only matters when both the logfile
43 : and stderr filters are strict enough so that the immediately prior
44 : FD_LOG_INFO call is a no-op, since otherwise that call would have
45 : taken care of it. */
46 0 : char wallclock[FD_LOG_WALLCLOCK_CSTR_BUF_SZ];
47 0 : fd_log_wallclock_cstr( 0L, wallclock );
48 0 : }
49 :
50 : static void
51 : check_wait_debugger( ulong pid,
52 : volatile int * wait,
53 0 : volatile int * debugger ) {
54 0 : if( FD_UNLIKELY( debugger ) ) {
55 0 : FD_LOG_WARNING(( "waiting for debugger to attach to tile pid:%lu", pid ));
56 0 : if( FD_UNLIKELY( -1==kill( getpid(), SIGSTOP ) ) )
57 0 : FD_LOG_ERR(( "kill(SIGSTOP) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
58 0 : *FD_VOLATILE( debugger ) = 1;
59 0 : }
60 :
61 0 : if( FD_UNLIKELY( wait ) ) {
62 0 : while( FD_LIKELY( !*FD_VOLATILE( wait ) ) ) FD_SPIN_PAUSE();
63 0 : }
64 0 : }
65 :
66 : void
67 : fd_topo_run_tile( fd_topo_t * topo,
68 : fd_topo_tile_t * tile,
69 : int sandbox,
70 : int keep_controlling_terminal,
71 : int dumpable,
72 : uint uid,
73 : uint gid,
74 : int allow_fd,
75 : volatile int * wait,
76 : volatile int * debugger,
77 0 : fd_topo_run_tile_t * tile_run ) {
78 0 : char thread_name[ 20 ];
79 0 : FD_TEST( fd_cstr_printf_check( thread_name, sizeof( thread_name ), NULL, "%s:%lu", tile->name, tile->kind_id ) );
80 0 : if( FD_UNLIKELY( prctl( PR_SET_NAME, thread_name, 0, 0, 0 ) ) ) FD_LOG_ERR(( "prctl(PR_SET_NAME) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
81 :
82 0 : ulong pid = fd_sandbox_getpid(); /* Need to read /proc again.. we got a new PID from clone */
83 0 : ulong tid = fd_sandbox_gettid(); /* Need to read /proc again.. we got a new TID from clone */
84 :
85 0 : check_wait_debugger( pid, wait, debugger );
86 0 : initialize_logging( tile->name, tile->kind_id, pid, tid );
87 :
88 : /* preload shared memory before sandboxing, so it is already mapped */
89 0 : fd_topo_join_tile_workspaces( topo, tile );
90 :
91 0 : if( FD_UNLIKELY( tile_run->privileged_init ) )
92 0 : tile_run->privileged_init( topo, tile );
93 :
94 0 : ulong allow_fds_offset = 0UL;
95 0 : int allow_fds[ 256 ] = { 0 };
96 0 : if( FD_LIKELY( -1!=allow_fd ) ) {
97 0 : allow_fds_offset = 1UL;
98 0 : allow_fds[ 0 ] = allow_fd;
99 0 : }
100 0 : ulong allow_fds_cnt = 0UL;
101 0 : if( FD_LIKELY( tile_run->populate_allowed_fds ) ) {
102 0 : allow_fds_cnt = tile_run->populate_allowed_fds( topo,
103 0 : tile,
104 0 : (sizeof(allow_fds)/sizeof(allow_fds[ 0 ]))-allow_fds_offset,
105 0 : allow_fds+allow_fds_offset );
106 0 : }
107 :
108 :
109 0 : struct sock_filter seccomp_filter[ 128UL ];
110 0 : ulong seccomp_filter_cnt = 0UL;
111 0 : if( FD_LIKELY( tile_run->populate_allowed_seccomp ) ) {
112 0 : seccomp_filter_cnt = tile_run->populate_allowed_seccomp( topo,
113 0 : tile,
114 0 : sizeof(seccomp_filter)/sizeof(seccomp_filter[ 0 ]),
115 0 : seccomp_filter );
116 0 : }
117 :
118 0 : ulong rlimit_file_cnt = tile_run->rlimit_file_cnt;
119 0 : if( tile_run->rlimit_file_cnt_fn ) {
120 0 : rlimit_file_cnt = tile_run->rlimit_file_cnt_fn( topo, tile );
121 0 : }
122 :
123 0 : if( FD_LIKELY( sandbox ) ) {
124 0 : fd_sandbox_enter( uid,
125 0 : gid,
126 0 : tile_run->keep_host_networking,
127 0 : tile_run->allow_connect,
128 0 : keep_controlling_terminal,
129 0 : dumpable,
130 0 : rlimit_file_cnt,
131 0 : tile_run->rlimit_address_space,
132 0 : tile_run->rlimit_data,
133 0 : allow_fds_cnt+allow_fds_offset,
134 0 : allow_fds,
135 0 : seccomp_filter_cnt,
136 0 : seccomp_filter );
137 0 : } else {
138 0 : fd_sandbox_switch_uid_gid( uid, gid );
139 0 : }
140 :
141 : /* Now we are sandboxed, join all the tango IPC objects in the workspaces */
142 0 : fd_topo_fill_tile( topo, tile );
143 :
144 0 : FD_TEST( tile->metrics );
145 0 : fd_metrics_register( tile->metrics );
146 :
147 0 : FD_MGAUGE_SET( TILE, PID, pid );
148 0 : FD_MGAUGE_SET( TILE, TID, tid );
149 :
150 0 : if( FD_UNLIKELY( tile_run->unprivileged_init ) )
151 0 : tile_run->unprivileged_init( topo, tile );
152 :
153 0 : tile_run->run( topo, tile );
154 0 : if( FD_UNLIKELY( !tile->allow_shutdown ) ) FD_LOG_ERR(( "tile %s:%lu run loop returned", tile->name, tile->kind_id ));
155 :
156 0 : FD_MGAUGE_SET( TILE, STATUS, 2UL );
157 0 : }
158 :
159 : typedef struct {
160 : fd_topo_t * topo;
161 : fd_topo_tile_t * tile;
162 : fd_topo_run_tile_t tile_run;
163 : uint uid;
164 : uint gid;
165 : volatile int copied;
166 : void * stack_lo;
167 : void * stack_hi;
168 : } fd_topo_run_thread_args_t;
169 :
170 : static void *
171 0 : run_tile_thread_main( void * _args ) {
172 0 : fd_topo_run_thread_args_t args = *(fd_topo_run_thread_args_t *)_args;
173 0 : FD_COMPILER_MFENCE();
174 0 : ((fd_topo_run_thread_args_t *)_args)->copied = 1;
175 0 : FD_COMPILER_MFENCE();
176 :
177 : /* Prevent fork() from smashing the stack */
178 0 : if( FD_UNLIKELY( madvise( args.stack_lo, (ulong)args.stack_hi - (ulong)args.stack_lo, MADV_DONTFORK ) ) ) {
179 0 : FD_LOG_ERR(( "madvise(stack,MADV_DONTFORK) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
180 0 : }
181 :
182 0 : fd_topo_run_tile( args.topo, args.tile, 0, 1, 1, args.uid, args.gid, -1, NULL, NULL, &args.tile_run );
183 0 : FD_TEST( args.tile->allow_shutdown );
184 0 : return NULL;
185 0 : }
186 :
187 : /* fd_topo_tile_stack_join_anon is a variant of fd_topo_tile_stack_join
188 : that acquires private anonymous memory instead of shared pages.
189 :
190 : This is required for fork() to work, as the parent and child process
191 : would otherwise share a stack and corrupt each other. While fork()
192 : is banned in tile user code, some dynamic analysis tools (like MSan)
193 : unfortunately rely on it. */
194 :
195 : FD_FN_UNUSED static void *
196 0 : fd_topo_tile_stack_join_anon( void ) {
197 0 :
198 0 : ulong sz = 2*FD_TILE_PRIVATE_STACK_SZ;
199 0 : int prot = PROT_READ|PROT_WRITE;
200 0 : int flags = MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK;
201 0 :
202 0 : uchar * stack = MAP_FAILED;
203 0 : #if !FD_HAS_ASAN && !FD_HAS_MSAN
204 0 : stack = mmap( NULL, sz, prot, flags|MAP_HUGETLB, -1, 0 );
205 0 : #endif
206 0 :
207 0 : if( stack==MAP_FAILED ) {
208 0 : stack = mmap( NULL, sz, prot, flags, -1, 0 );
209 0 : if( FD_UNLIKELY( stack==MAP_FAILED ) ) {
210 0 : FD_LOG_ERR(( "mmap() for stack failed (%i-%s)", errno, fd_io_strerror( errno ) ));
211 0 : }
212 0 : }
213 0 :
214 0 : /* Create the guard regions in the extra space */
215 0 : void * guard_lo = (void *)( stack - FD_SHMEM_NORMAL_PAGE_SZ );
216 0 : if( FD_UNLIKELY( mmap( guard_lo, FD_SHMEM_NORMAL_PAGE_SZ, PROT_NONE,
217 0 : MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, (off_t)0 )!=guard_lo ) )
218 0 : FD_LOG_ERR(( "mmap(%p) failed (%i-%s)", guard_lo, errno, fd_io_strerror( errno ) ));
219 0 :
220 0 : void * guard_hi = (void *)( stack + FD_TILE_PRIVATE_STACK_SZ );
221 0 : if( FD_UNLIKELY( mmap( guard_hi, FD_SHMEM_NORMAL_PAGE_SZ, PROT_NONE,
222 0 : MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, (off_t)0 )!=guard_hi ) )
223 0 : FD_LOG_ERR(( "mmap(%p) failed (%i-%s)", guard_hi, errno, fd_io_strerror( errno ) ));
224 0 :
225 0 : return stack;
226 0 : }
227 :
228 : void *
229 : fd_topo_tile_stack_join( char const * app_name,
230 : char const * tile_name,
231 0 : ulong tile_kind_id ) {
232 : #if FD_HAS_MSAN
233 : return fd_topo_tile_stack_join_anon();
234 : #endif
235 :
236 0 : char name[ PATH_MAX ];
237 0 : FD_TEST( fd_cstr_printf_check( name, PATH_MAX, NULL, "%s_stack_%s%lu", app_name, tile_name, tile_kind_id ) );
238 :
239 0 : uchar * stack = fd_shmem_join( name, FD_SHMEM_JOIN_MODE_READ_WRITE, NULL, NULL, NULL, 1 );
240 0 : if( FD_UNLIKELY( !stack ) ) FD_LOG_ERR(( "fd_shmem_join failed" ));
241 :
242 : /* Make space for guard lo and guard hi */
243 0 : if( FD_UNLIKELY( fd_shmem_release( stack, FD_SHMEM_HUGE_PAGE_SZ, 1UL ) ) )
244 0 : FD_LOG_ERR(( "fd_shmem_release (%d-%s)", errno, fd_io_strerror( errno ) ));
245 0 : stack += FD_SHMEM_HUGE_PAGE_SZ;
246 0 : if( FD_UNLIKELY( fd_shmem_release( stack + FD_TILE_PRIVATE_STACK_SZ, FD_SHMEM_HUGE_PAGE_SZ, 1UL ) ) )
247 0 : FD_LOG_ERR(( "fd_shmem_release (%d-%s)", errno, fd_io_strerror( errno ) ));
248 :
249 : /* Create the guard regions in the extra space */
250 0 : void * guard_lo = (void *)(stack - FD_SHMEM_NORMAL_PAGE_SZ );
251 0 : if( FD_UNLIKELY( mmap( guard_lo, FD_SHMEM_NORMAL_PAGE_SZ, PROT_NONE,
252 0 : MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, (off_t)0 )!=guard_lo ) )
253 0 : FD_LOG_ERR(( "mmap failed (%i-%s)", errno, fd_io_strerror( errno ) ));
254 :
255 0 : void * guard_hi = (void *)(stack + FD_TILE_PRIVATE_STACK_SZ);
256 0 : if( FD_UNLIKELY( mmap( guard_hi, FD_SHMEM_NORMAL_PAGE_SZ, PROT_NONE,
257 0 : MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, (off_t)0 )!=guard_hi ) )
258 0 : FD_LOG_ERR(( "mmap failed (%i-%s)", errno, fd_io_strerror( errno ) ));
259 :
260 0 : return stack;
261 0 : }
262 :
263 : fd_xdp_fds_t
264 : fd_topo_install_xdp( fd_topo_t const * topo,
265 0 : uint bind_addr ) {
266 0 : ulong net0_tile_idx = fd_topo_find_tile( topo, "net", 0UL );
267 0 : FD_TEST( net0_tile_idx!=ULONG_MAX );
268 0 : fd_topo_tile_t const * net0_tile = &topo->tiles[ net0_tile_idx ];
269 :
270 0 : ushort udp_port_candidates[] = {
271 0 : (ushort)net0_tile->xdp.net.legacy_transaction_listen_port,
272 0 : (ushort)net0_tile->xdp.net.quic_transaction_listen_port,
273 0 : (ushort)net0_tile->xdp.net.shred_listen_port,
274 0 : (ushort)net0_tile->xdp.net.gossip_listen_port,
275 0 : (ushort)net0_tile->xdp.net.repair_intake_listen_port,
276 0 : (ushort)net0_tile->xdp.net.repair_serve_listen_port,
277 0 : (ushort)net0_tile->xdp.net.send_src_port,
278 0 : };
279 :
280 0 : uint if_idx = if_nametoindex( net0_tile->xdp.interface );
281 0 : if( FD_UNLIKELY( !if_idx ) ) FD_LOG_ERR(( "if_nametoindex(%s) failed", net0_tile->xdp.interface ));
282 :
283 0 : fd_xdp_fds_t xdp_fds = fd_xdp_install( if_idx,
284 0 : bind_addr,
285 0 : sizeof(udp_port_candidates)/sizeof(udp_port_candidates[0]),
286 0 : udp_port_candidates,
287 0 : net0_tile->xdp.xdp_mode );
288 0 : if( FD_UNLIKELY( -1==dup2( xdp_fds.xsk_map_fd, 123462 ) ) ) FD_LOG_ERR(( "dup2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
289 0 : if( FD_UNLIKELY( -1==close( xdp_fds.xsk_map_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
290 0 : if( FD_UNLIKELY( -1==dup2( xdp_fds.prog_link_fd, 123463 ) ) ) FD_LOG_ERR(( "dup2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
291 0 : if( FD_UNLIKELY( -1==close( xdp_fds.prog_link_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
292 :
293 0 : xdp_fds.xsk_map_fd = 123462;
294 0 : xdp_fds.prog_link_fd = 123463;
295 :
296 0 : return xdp_fds;
297 0 : }
298 :
299 : static inline void
300 : run_tile_thread( fd_topo_t * topo,
301 : fd_topo_tile_t * tile,
302 : fd_topo_run_tile_t tile_run,
303 : uint uid,
304 : uint gid,
305 : fd_cpuset_t const * floating_cpu_set,
306 : int floating_priority,
307 0 : fd_topo_run_thread_args_t * args ) {
308 : /* tpool will assign a thread later */
309 0 : if( FD_UNLIKELY( tile_run.for_tpool ) ) return;
310 0 : void * stack = fd_topo_tile_stack_join( topo->app_name, tile->name, tile->kind_id );
311 :
312 0 : pthread_attr_t attr[ 1 ];
313 0 : if( FD_UNLIKELY( pthread_attr_init( attr ) ) ) FD_LOG_ERR(( "pthread_attr_init() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
314 0 : if( FD_UNLIKELY( pthread_attr_setstack( attr, stack, FD_TILE_PRIVATE_STACK_SZ ) ) ) FD_LOG_ERR(( "pthread_attr_setstacksize() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
315 :
316 0 : FD_CPUSET_DECL( cpu_set );
317 0 : if( FD_LIKELY( tile->cpu_idx<65535UL ) ) {
318 : /* set the thread affinity before we clone the new process to ensure
319 : kernel first touch happens on the desired thread. */
320 0 : fd_cpuset_insert( cpu_set, tile->cpu_idx );
321 0 : if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, -19 ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
322 0 : } else {
323 0 : fd_memcpy( cpu_set, floating_cpu_set, fd_cpuset_footprint() );
324 0 : if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, floating_priority ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
325 0 : }
326 :
327 0 : if( FD_UNLIKELY( fd_cpuset_setaffinity( 0, cpu_set ) ) ) {
328 0 : if( FD_LIKELY( errno==EINVAL ) ) {
329 0 : FD_LOG_ERR(( "Unable to set the thread affinity for tile %s:%lu on cpu %lu. It is likely that the affinity "
330 0 : "you have specified for this tile in [layout.affinity] of your configuration file contains a "
331 0 : "CPU (%lu) which does not exist on this machine.",
332 0 : tile->name, tile->kind_id, tile->cpu_idx, tile->cpu_idx ));
333 0 : } else {
334 0 : FD_LOG_ERR(( "sched_setaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));
335 0 : }
336 0 : }
337 :
338 0 : *args = (fd_topo_run_thread_args_t) {
339 0 : .topo = topo,
340 0 : .tile = tile,
341 0 : .tile_run = tile_run,
342 0 : .uid = uid,
343 0 : .gid = gid,
344 0 : .copied = 0,
345 0 : .stack_lo = stack,
346 0 : .stack_hi = (uchar *)stack + FD_TILE_PRIVATE_STACK_SZ
347 0 : };
348 :
349 0 : pthread_t pthread;
350 0 : if( FD_UNLIKELY( pthread_create( &pthread, attr, run_tile_thread_main, args ) ) ) FD_LOG_ERR(( "pthread_create() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
351 0 : }
352 :
353 : void
354 : fd_topo_run_single_process( fd_topo_t * topo,
355 : int agave,
356 : uint uid,
357 : uint gid,
358 0 : fd_topo_run_tile_t (* tile_run )( fd_topo_tile_t const * tile ) ) {
359 0 : FD_LOG_NOTICE(( "running single threaded topology with %lu tiles and %lu GiB memory",
360 0 : topo->tile_cnt, fd_topo_mlock( topo ) / (1UL << 30) ));
361 :
362 : /* Save the current affinity, it will be restored after creating any child tiles */
363 0 : FD_CPUSET_DECL( floating_cpu_set );
364 0 : if( FD_UNLIKELY( fd_cpuset_getaffinity( 0, floating_cpu_set ) ) )
365 0 : FD_LOG_ERR(( "sched_getaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));
366 :
367 0 : errno = 0;
368 0 : int save_priority = getpriority( PRIO_PROCESS, 0 );
369 0 : if( FD_UNLIKELY( -1==save_priority && errno ) ) FD_LOG_ERR(( "getpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
370 :
371 0 : fd_topo_run_thread_args_t args[ FD_TOPO_MAX_TILES ];
372 :
373 0 : for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
374 0 : fd_topo_tile_t * tile = &topo->tiles[ i ];
375 0 : if( !agave && tile->is_agave ) continue;
376 0 : if( agave==1 && !tile->is_agave ) continue;
377 :
378 0 : fd_topo_run_tile_t run_tile = tile_run( tile );
379 0 : run_tile_thread( topo, tile, run_tile, uid, gid, floating_cpu_set, save_priority, &args[ i ] );
380 0 : }
381 :
382 0 : for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
383 0 : fd_topo_tile_t * tile = &topo->tiles[ i ];
384 0 : if( !agave && tile->is_agave ) continue;
385 0 : if( agave==1 && !tile->is_agave ) continue;
386 :
387 0 : while( !FD_VOLATILE( args[ i ].copied ) ) FD_SPIN_PAUSE();
388 0 : }
389 :
390 0 : fd_sandbox_switch_uid_gid( uid, gid );
391 :
392 0 : if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, save_priority ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
393 0 : if( FD_UNLIKELY( fd_cpuset_setaffinity( 0, floating_cpu_set ) ) )
394 0 : FD_LOG_ERR(( "sched_setaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));
395 0 : }
|