LCOV - code coverage report
Current view: top level - disco/topo - fd_topo_run.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 196 0.0 %
Date: 2025-01-08 12:08:44 Functions: 0 8 0.0 %

          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_NOTICE(( "booting tile %s pid:%lu tid:%lu", thread_name, fd_log_group_id(), tid ));
      33           0 : }
      34             : 
      35             : static void
      36             : check_wait_debugger( ulong          pid,
      37             :                      volatile int * wait,
      38           0 :                      volatile int * debugger ) {
      39           0 :   if( FD_UNLIKELY( debugger ) ) {
      40           0 :     FD_LOG_WARNING(( "waiting for debugger to attach to tile pid:%lu", pid ));
      41           0 :     if( FD_UNLIKELY( -1==kill( getpid(), SIGSTOP ) ) )
      42           0 :       FD_LOG_ERR(( "kill(SIGSTOP) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
      43           0 :     *FD_VOLATILE( debugger ) = 1;
      44           0 :   }
      45             : 
      46           0 :   if( FD_UNLIKELY( wait ) ) {
      47           0 :     while( FD_LIKELY( !*FD_VOLATILE( wait ) ) ) FD_SPIN_PAUSE();
      48           0 :   }
      49           0 : }
      50             : 
      51             : void
      52             : fd_topo_run_tile( fd_topo_t *          topo,
      53             :                   fd_topo_tile_t *     tile,
      54             :                   int                  sandbox,
      55             :                   int                  keep_controlling_terminal,
      56             :                   uint                 uid,
      57             :                   uint                 gid,
      58             :                   int                  allow_fd,
      59             :                   volatile int *       wait,
      60             :                   volatile int *       debugger,
      61           0 :                   fd_topo_run_tile_t * tile_run ) {
      62           0 :   char thread_name[ 20 ];
      63           0 :   FD_TEST( fd_cstr_printf_check( thread_name, sizeof( thread_name ), NULL, "%s:%lu", tile->name, tile->kind_id ) );
      64           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 ) ));
      65             : 
      66           0 :   ulong pid = fd_sandbox_getpid(); /* Need to read /proc again.. we got a new PID from clone */
      67           0 :   ulong tid = fd_sandbox_gettid(); /* Need to read /proc again.. we got a new TID from clone */
      68             : 
      69           0 :   check_wait_debugger( pid, wait, debugger );
      70           0 :   initialize_logging( tile->name, tile->kind_id, pid, tid );
      71             : 
      72             :   /* preload shared memory before sandboxing, so it is already mapped */
      73           0 :   fd_topo_join_tile_workspaces( topo, tile );
      74             : 
      75           0 :   if( FD_UNLIKELY( tile_run->privileged_init ) )
      76           0 :     tile_run->privileged_init( topo, tile );
      77             : 
      78           0 :   ulong allow_fds_offset = 0UL;
      79           0 :   int allow_fds[ 256 ] = { 0 };
      80           0 :   if( FD_LIKELY( -1!=allow_fd ) ) {
      81           0 :     allow_fds_offset = 1UL;
      82           0 :     allow_fds[ 0 ] = allow_fd;
      83           0 :   }
      84           0 :   ulong allow_fds_cnt = 0UL;
      85           0 :   if( FD_LIKELY( tile_run->populate_allowed_fds ) ) {
      86           0 :     allow_fds_cnt = tile_run->populate_allowed_fds( topo,
      87           0 :                                                     tile,
      88           0 :                                                     (sizeof(allow_fds)/sizeof(allow_fds[ 0 ]))-allow_fds_offset,
      89           0 :                                                     allow_fds+allow_fds_offset );
      90           0 :   }
      91             : 
      92             : 
      93           0 :   struct sock_filter seccomp_filter[ 128UL ];
      94           0 :   ulong seccomp_filter_cnt = 0UL;
      95           0 :   if( FD_LIKELY( tile_run->populate_allowed_seccomp ) ) {
      96           0 :     seccomp_filter_cnt = tile_run->populate_allowed_seccomp( topo,
      97           0 :                                                              tile,
      98           0 :                                                              sizeof(seccomp_filter)/sizeof(seccomp_filter[ 0 ]),
      99           0 :                                                              seccomp_filter );
     100           0 :   }
     101             : 
     102           0 :   if( FD_LIKELY( sandbox ) ) {
     103           0 :     fd_sandbox_enter( uid,
     104           0 :                       gid,
     105           0 :                       tile_run->keep_host_networking,
     106           0 :                       keep_controlling_terminal,
     107           0 :                       tile_run->rlimit_file_cnt,
     108           0 :                       allow_fds_cnt+allow_fds_offset,
     109           0 :                       allow_fds,
     110           0 :                       seccomp_filter_cnt,
     111           0 :                       seccomp_filter );
     112           0 :   } else {
     113           0 :     fd_sandbox_switch_uid_gid( uid, gid );
     114           0 :   }
     115             : 
     116             :   /* Now we are sandboxed, join all the tango IPC objects in the workspaces */
     117           0 :   fd_topo_fill_tile( topo, tile );
     118             : 
     119           0 :   FD_TEST( tile->metrics );
     120           0 :   fd_metrics_register( tile->metrics );
     121             : 
     122           0 :   FD_MGAUGE_SET( TILE, PID, pid );
     123           0 :   FD_MGAUGE_SET( TILE, TID, tid );
     124             : 
     125           0 :   if( FD_UNLIKELY( tile_run->unprivileged_init ) )
     126           0 :     tile_run->unprivileged_init( topo, tile );
     127             : 
     128           0 :   tile_run->run( topo, tile );
     129           0 :   FD_LOG_ERR(( "tile run loop returned" ));
     130           0 : }
     131             : 
     132             : typedef struct {
     133             :   fd_topo_t *        topo;
     134             :   fd_topo_tile_t *   tile;
     135             :   fd_topo_run_tile_t tile_run;
     136             :   uint               uid;
     137             :   uint               gid;
     138             :   int *              done_futex;
     139             :   volatile int       copied;
     140             : } fd_topo_run_thread_args_t;
     141             : 
     142             : static void *
     143           0 : run_tile_thread_main( void * _args ) {
     144           0 :   fd_topo_run_thread_args_t args = *(fd_topo_run_thread_args_t *)_args;
     145           0 :   FD_COMPILER_MFENCE();
     146           0 :   ((fd_topo_run_thread_args_t *)_args)->copied = 1;
     147             : 
     148           0 :   fd_topo_run_tile( args.topo, args.tile, 0, 1, args.uid, args.gid, -1, NULL, NULL, &args.tile_run );
     149           0 :   if( FD_UNLIKELY( args.done_futex ) ) {
     150           0 :     for(;;) {
     151           0 :       if( FD_LIKELY( INT_MAX==FD_ATOMIC_CAS( args.done_futex, INT_MAX, (int)args.tile->id ) ) ) break;
     152           0 :       FD_SPIN_PAUSE();
     153           0 :     }
     154           0 :     if( FD_UNLIKELY( -1==syscall( SYS_futex, args.done_futex, FUTEX_WAKE, INT_MAX, NULL, NULL, 0 ) ) )
     155           0 :       FD_LOG_ERR(( "futex(FUTEX_WAKE) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     156           0 :   } else {
     157           0 :     FD_LOG_ERR(( "fd_topo_run_tile() returned" ));
     158           0 :   }
     159           0 :   return NULL;
     160           0 : }
     161             : 
     162             : void *
     163             : fd_topo_tile_stack_join( char const * app_name,
     164             :                          char const * tile_name,
     165           0 :                          ulong        tile_kind_id ) {
     166           0 :   char name[ PATH_MAX ];
     167           0 :   FD_TEST( fd_cstr_printf_check( name, PATH_MAX, NULL, "%s_stack_%s%lu", app_name, tile_name, tile_kind_id ) );
     168             : 
     169           0 :   uchar * stack = fd_shmem_join( name, FD_SHMEM_JOIN_MODE_READ_WRITE, NULL, NULL, NULL );
     170           0 :   if( FD_UNLIKELY( !stack ) ) FD_LOG_ERR(( "fd_shmem_join failed" ));
     171             : 
     172             :   /* Make space for guard lo and guard hi */
     173           0 :   if( FD_UNLIKELY( fd_shmem_release( stack, FD_SHMEM_HUGE_PAGE_SZ, 1UL ) ) )
     174           0 :     FD_LOG_ERR(( "fd_shmem_release (%d-%s)", errno, fd_io_strerror( errno ) ));
     175           0 :   stack += FD_SHMEM_HUGE_PAGE_SZ;
     176           0 :   if( FD_UNLIKELY( fd_shmem_release( stack + FD_TILE_PRIVATE_STACK_SZ, FD_SHMEM_HUGE_PAGE_SZ, 1UL ) ) )
     177           0 :     FD_LOG_ERR(( "fd_shmem_release (%d-%s)", errno, fd_io_strerror( errno ) ));
     178             : 
     179             :   /* Create the guard regions in the extra space */
     180           0 :   void * guard_lo = (void *)(stack - FD_SHMEM_NORMAL_PAGE_SZ );
     181           0 :   if( FD_UNLIKELY( mmap( guard_lo, FD_SHMEM_NORMAL_PAGE_SZ, PROT_NONE,
     182           0 :                          MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, (off_t)0 )!=guard_lo ) )
     183           0 :     FD_LOG_ERR(( "mmap failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     184             : 
     185           0 :   void * guard_hi = (void *)(stack + FD_TILE_PRIVATE_STACK_SZ);
     186           0 :   if( FD_UNLIKELY( mmap( guard_hi, FD_SHMEM_NORMAL_PAGE_SZ, PROT_NONE,
     187           0 :                          MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, (off_t)0 )!=guard_hi ) )
     188           0 :     FD_LOG_ERR(( "mmap failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     189             : 
     190           0 :   return stack;
     191           0 : }
     192             : 
     193             : fd_xdp_fds_t
     194           0 : fd_topo_install_xdp( fd_topo_t * topo ) {
     195           0 :   ulong net0_tile_idx = fd_topo_find_tile( topo, "net", 0UL );
     196           0 :   FD_TEST( net0_tile_idx!=ULONG_MAX );
     197           0 :   fd_topo_tile_t const * net0_tile = &topo->tiles[ net0_tile_idx ];
     198             : 
     199           0 :   ushort udp_port_candidates[] = {
     200           0 :     (ushort)net0_tile->net.legacy_transaction_listen_port,
     201           0 :     (ushort)net0_tile->net.quic_transaction_listen_port,
     202           0 :     (ushort)net0_tile->net.shred_listen_port,
     203           0 :     (ushort)net0_tile->net.gossip_listen_port,
     204           0 :     (ushort)net0_tile->net.repair_intake_listen_port,
     205           0 :     (ushort)net0_tile->net.repair_serve_listen_port,
     206           0 :   };
     207             : 
     208           0 :   uint if_idx = if_nametoindex( net0_tile->net.interface );
     209           0 :   if( FD_UNLIKELY( !if_idx ) ) FD_LOG_ERR(( "if_nametoindex(%s) failed", net0_tile->net.interface ));
     210             : 
     211           0 :   fd_xdp_fds_t xdp_fds = fd_xdp_install( if_idx,
     212           0 :                                          net0_tile->net.src_ip_addr,
     213           0 :                                          sizeof(udp_port_candidates)/sizeof(udp_port_candidates[0]),
     214           0 :                                          udp_port_candidates,
     215           0 :                                          net0_tile->net.xdp_mode );
     216           0 :   if( FD_UNLIKELY( -1==dup2( xdp_fds.xsk_map_fd, 123462 ) ) ) FD_LOG_ERR(( "dup2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     217           0 :   if( FD_UNLIKELY( -1==close( xdp_fds.xsk_map_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     218           0 :   if( FD_UNLIKELY( -1==dup2( xdp_fds.prog_link_fd, 123463 ) ) ) FD_LOG_ERR(( "dup2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     219           0 :   if( FD_UNLIKELY( -1==close( xdp_fds.prog_link_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     220             : 
     221           0 :   xdp_fds.xsk_map_fd = 123462;
     222           0 :   xdp_fds.prog_link_fd = 123463;
     223             : 
     224           0 :   return xdp_fds;
     225           0 : }
     226             : 
     227             : static inline void
     228             : run_tile_thread( fd_topo_t *         topo,
     229             :                  fd_topo_tile_t *    tile,
     230             :                  fd_topo_run_tile_t  tile_run,
     231             :                  uint                uid,
     232             :                  uint                gid,
     233             :                  int *               done_futex,
     234             :                  fd_cpuset_t const * floating_cpu_set,
     235           0 :                  int                 floating_priority ) {
     236             :   /* tpool will assign a thread later */
     237           0 :   if( FD_UNLIKELY( tile_run.for_tpool ) ) return;
     238           0 :   void * stack = fd_topo_tile_stack_join( topo->app_name, tile->name, tile->kind_id );
     239             : 
     240           0 :   pthread_attr_t attr[ 1 ];
     241           0 :   if( FD_UNLIKELY( pthread_attr_init( attr ) ) ) FD_LOG_ERR(( "pthread_attr_init() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     242           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 ) ));
     243             : 
     244           0 :   FD_CPUSET_DECL( cpu_set );
     245           0 :   if( FD_LIKELY( tile->cpu_idx<65535UL ) ) {
     246             :     /* set the thread affinity before we clone the new process to ensure
     247             :        kernel first touch happens on the desired thread. */
     248           0 :     fd_cpuset_insert( cpu_set, tile->cpu_idx );
     249           0 :     if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, -19 ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     250           0 :   } else {
     251           0 :     fd_memcpy( cpu_set, floating_cpu_set, fd_cpuset_footprint() );
     252           0 :     if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, floating_priority ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     253           0 :   }
     254             : 
     255           0 :   if( FD_UNLIKELY( fd_cpuset_setaffinity( 0, cpu_set ) ) ) {
     256           0 :     if( FD_LIKELY( errno==EINVAL ) ) {
     257           0 :       FD_LOG_ERR(( "Unable to set the thread affinity for tile %s:%lu on cpu %lu. It is likely that the affinity "
     258           0 :                    "you have specified for this tile in [layout.affinity] of your configuration file contains a "
     259           0 :                    "CPU (%lu) which does not exist on this machine.",
     260           0 :                    tile->name, tile->kind_id, tile->cpu_idx, tile->cpu_idx ));
     261           0 :     } else {
     262           0 :       FD_LOG_ERR(( "sched_setaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     263           0 :     }
     264           0 :   }
     265             : 
     266           0 :   fd_topo_run_thread_args_t args = {
     267           0 :     .topo       = topo,
     268           0 :     .tile       = tile,
     269           0 :     .tile_run   = tile_run,
     270           0 :     .uid        = uid,
     271           0 :     .gid        = gid,
     272           0 :     .done_futex = done_futex,
     273           0 :     .copied     = 0,
     274           0 :   };
     275             : 
     276           0 :   pthread_t pthread;
     277           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 ) ));
     278             : 
     279           0 :   while( !FD_VOLATILE( args.copied ) ) FD_SPIN_PAUSE();
     280           0 : }
     281             : 
     282             : void
     283             : fd_topo_run_single_process( fd_topo_t * topo,
     284             :                             int         agave,
     285             :                             uint        uid,
     286             :                             uint        gid,
     287             :                             fd_topo_run_tile_t (* tile_run )( fd_topo_tile_t * tile ),
     288           0 :                             int *       done_futex ) {
     289             :   /* Save the current affinity, it will be restored after creating any child tiles */
     290           0 :   FD_CPUSET_DECL( floating_cpu_set );
     291           0 :   if( FD_UNLIKELY( fd_cpuset_getaffinity( 0, floating_cpu_set ) ) )
     292           0 :     FD_LOG_ERR(( "sched_getaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     293             : 
     294           0 :   errno = 0;
     295           0 :   int save_priority = getpriority( PRIO_PROCESS, 0 );
     296           0 :   if( FD_UNLIKELY( -1==save_priority && errno ) ) FD_LOG_ERR(( "getpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     297             : 
     298           0 :   for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
     299           0 :     fd_topo_tile_t * tile = &topo->tiles[ i ];
     300           0 :     if( !agave && tile->is_agave ) continue;
     301           0 :     if( agave==1 && !tile->is_agave ) continue;
     302             : 
     303           0 :     fd_topo_run_tile_t run_tile = tile_run( tile );
     304           0 :     run_tile_thread( topo, tile, run_tile, uid, gid, done_futex, floating_cpu_set, save_priority );
     305           0 :   }
     306             : 
     307           0 :   fd_sandbox_switch_uid_gid( uid, gid );
     308             : 
     309           0 :   if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, save_priority ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     310           0 :   if( FD_UNLIKELY( fd_cpuset_setaffinity( 0, floating_cpu_set ) ) )
     311           0 :     FD_LOG_ERR(( "sched_setaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     312           0 : }

Generated by: LCOV version 1.14