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

Generated by: LCOV version 1.14