LCOV - code coverage report
Current view: top level - app/shared/commands/configure - sysctl.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 65 0.0 %
Date: 2025-03-20 12:08:36 Functions: 0 5 0.0 %

          Line data    Source code
       1             : #include "configure.h"
       2             : 
       3           0 : #define NAME "sysctl"
       4             : 
       5             : #include "../../../shared/fd_file_util.h"
       6             : 
       7             : #include <errno.h>
       8             : #include <stdio.h>
       9             : #include <linux/capability.h>
      10             : 
      11             : static void
      12             : init_perm( fd_cap_chk_t *   chk,
      13           0 :            config_t const * config FD_PARAM_UNUSED ) {
      14           0 :   fd_cap_chk_cap( chk, NAME, CAP_SYS_ADMIN, "set kernel parameters in `/proc/sys`" );
      15           0 : }
      16             : 
      17           0 : #define ENFORCE_MINIMUM 0
      18           0 : #define WARN_MINIMUM    1
      19           0 : #define WARN_EXACT      2
      20             : 
      21             : typedef struct {
      22             :   char const * path;
      23             :   ulong        value;
      24             :   int          mode;
      25             : } sysctl_param_t;
      26             : 
      27             : static const sysctl_param_t params[] = {
      28             :   {
      29             :     "/proc/sys/vm/max_map_count", /* int */
      30             :     1000000,
      31             :     ENFORCE_MINIMUM,
      32             :   },
      33             :   {
      34             :     "/proc/sys/fs/file-max", /* ulong */
      35             :     CONFIGURE_NR_OPEN_FILES,
      36             :     ENFORCE_MINIMUM,
      37             :   },
      38             :   {
      39             :     "/proc/sys/fs/nr_open", /* uint */
      40             :     CONFIGURE_NR_OPEN_FILES,
      41             :     ENFORCE_MINIMUM,
      42             :   },
      43             :   {
      44             :     "/proc/sys/kernel/numa_balancing", /* int? */
      45             :     0,
      46             :     WARN_EXACT,
      47             :   },
      48             :   {0}
      49             : };
      50             : 
      51             : static const sysctl_param_t xdp_params[] = {
      52             :   {
      53             :     "/proc/sys/net/ipv4/conf/lo/rp_filter",
      54             :     2,
      55             :     ENFORCE_MINIMUM,
      56             :   },
      57             :   {
      58             :     "/proc/sys/net/ipv4/conf/lo/accept_local",
      59             :     1,
      60             :     ENFORCE_MINIMUM,
      61             :   },
      62             :   {
      63             :     "/proc/sys/net/core/bpf_jit_enable",
      64             :     1,
      65             :     WARN_MINIMUM,
      66             :   },
      67             :   {0}
      68             : };
      69             : 
      70             : /* Some of these sysctl limits are needed for the Agave client, not
      71             :    Firedancer.  We set them on their behalf to make configuration easier
      72             :    for users. */
      73             : 
      74             : static void
      75           0 : init_param_list( sysctl_param_t const * list ) {
      76           0 :   for( sysctl_param_t const * p=list; p->path; p++ ) {
      77           0 :     ulong param;
      78           0 :     if( FD_UNLIKELY( -1==fd_file_util_read_ulong( p->path, &param ) ) )
      79           0 :       FD_LOG_ERR(( "could not read kernel parameter `%s`, system might not support configuring sysctl (%i-%s)", p->path, errno, fd_io_strerror( errno ) ));
      80           0 :     switch( p->mode ) {
      81           0 :       case ENFORCE_MINIMUM:
      82           0 :         if( FD_UNLIKELY( param<(p->value) ) ) {
      83           0 :           FD_LOG_NOTICE(( "RUN: `echo \"%lu\" > %s`", p->value, p->path ) );
      84           0 :           if( FD_UNLIKELY( -1==fd_file_util_write_ulong( p->path, p->value ) ) )
      85           0 :             FD_LOG_ERR(( "could not set kernel parameter `%s` to %lu (%i-%s)", p->path, p->value, errno, fd_io_strerror( errno ) ));
      86           0 :         }
      87           0 :         break;
      88           0 :       default:
      89           0 :         break;
      90           0 :     }
      91           0 :   }
      92           0 : }
      93             : 
      94             : static void
      95           0 : init( config_t const * config ) {
      96           0 :   init_param_list( params );
      97           0 :   if( 0==strcmp( config->development.net.provider, "xdp" ) ) {
      98           0 :     init_param_list( xdp_params );
      99           0 :   }
     100           0 : }
     101             : 
     102             : static configure_result_t
     103           0 : check_param_list( sysctl_param_t const * list ) {
     104           0 :   static int has_warned = 0;
     105             : 
     106           0 :   for( sysctl_param_t const * p=list; p->path; p++ ) {
     107           0 :     ulong param;
     108           0 :     if( FD_UNLIKELY( -1==fd_file_util_read_ulong( p->path, &param ) ) )
     109           0 :       FD_LOG_ERR(( "could not read kernel parameter `%s`, system might not support configuring sysctl (%i-%s)", p->path, errno, fd_io_strerror( errno ) ));
     110           0 :     switch( p->mode ) {
     111           0 :       case ENFORCE_MINIMUM:
     112           0 :         if( FD_UNLIKELY( param<(p->value) ) )
     113           0 :           NOT_CONFIGURED( "kernel parameter `%s` is too low (got %lu but expected at least %lu)", p->path, param, p->value );
     114           0 :         break;
     115           0 :       case WARN_MINIMUM:
     116           0 :         if( FD_UNLIKELY( !has_warned && param<(p->value) ) )
     117           0 :           FD_LOG_WARNING(( "kernel parameter `%s` is too low (got %lu but expected at least %lu). Proceeding but performance may be reduced.", p->path, param, p->value ));
     118           0 :         break;
     119           0 :       case WARN_EXACT:
     120           0 :         if( FD_UNLIKELY( !has_warned && param!=(p->value) ) )
     121           0 :           FD_LOG_WARNING(( "kernel parameter `%s` is set to %lu, not the expected value of %lu. Proceeding but performance may be reduced.", p->path, param, p->value ));
     122           0 :         break;
     123           0 :     }
     124           0 :   }
     125             : 
     126           0 :   has_warned = 1;
     127             : 
     128           0 :   CONFIGURE_OK();
     129           0 : }
     130             : 
     131             : static configure_result_t
     132           0 : check( config_t const * config ) {
     133           0 :   configure_result_t r;
     134             : 
     135           0 :   r = check_param_list( params );
     136           0 :   if( r.result!=CONFIGURE_OK ) return r;
     137             : 
     138           0 :   if( 0==strcmp( config->development.net.provider, "xdp" ) ) {
     139           0 :     check_param_list( xdp_params );
     140           0 :     if( r.result!=CONFIGURE_OK ) return r;
     141           0 :   }
     142             : 
     143           0 :   CONFIGURE_OK();
     144           0 : }
     145             : 
     146             : configure_stage_t fd_cfg_stage_sysctl = {
     147             :   .name            = NAME,
     148             :   .always_recreate = 0,
     149             :   .enabled         = NULL,
     150             :   .init_perm       = init_perm,
     151             :   .fini_perm       = NULL,
     152             :   .init            = init,
     153             :   .fini            = NULL,
     154             :   .check           = check,
     155             : };
     156             : 
     157             : #undef NAME

Generated by: LCOV version 1.14