LCOV - code coverage report
Current view: top level - app/fdctl/configure - ethtool-gro.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 105 0.0 %
Date: 2025-01-08 12:08:44 Functions: 0 8 0.0 %

          Line data    Source code
       1             : #include "configure.h"
       2             : 
       3             : #include <stdio.h>
       4             : #include <unistd.h>
       5             : #include <sys/ioctl.h>
       6             : #include <sys/stat.h>
       7             : #include <linux/if.h>
       8             : #include <linux/ethtool.h>
       9             : #include <linux/sockios.h>
      10             : 
      11           0 : #define NAME "ethtool-gro"
      12             : 
      13             : static int
      14           0 : enabled( config_t * const config ) {
      15             :   /* if we're running in a network namespace, we configure ethtool on
      16             :       the virtual device as part of netns setup, not here */
      17           0 :   return !config->development.netns.enabled;
      18           0 : }
      19             : 
      20             : static void
      21             : init_perm( fd_caps_ctx_t *  caps,
      22           0 :            config_t * const config ) {
      23           0 :   (void)config;
      24           0 :   fd_caps_check_root( caps, NAME, "disable network device generic-receive-offload (gro) with `ethtool --set-offload generic-receive-offload off`" );
      25           0 : }
      26             : 
      27             : static int
      28           0 : device_is_bonded( const char * device ) {
      29           0 :   char path[ PATH_MAX ];
      30           0 :   FD_TEST( fd_cstr_printf_check( path, PATH_MAX, NULL, "/sys/class/net/%s/bonding", device ) );
      31           0 :   struct stat st;
      32           0 :   int err = stat( path, &st );
      33           0 :   if( FD_UNLIKELY( err && errno != ENOENT ) )
      34           0 :     FD_LOG_ERR(( "error checking if device `%s` is bonded, stat(%s) failed (%i-%s)",
      35           0 :                  device, path, errno, fd_io_strerror( errno ) ));
      36           0 :   return !err;
      37           0 : }
      38             : 
      39             : static void
      40             : device_read_slaves( const char * device,
      41           0 :                     char         output[ 4096 ] ) {
      42           0 :   char path[ PATH_MAX ];
      43           0 :   FD_TEST( fd_cstr_printf_check( path, PATH_MAX, NULL, "/sys/class/net/%s/bonding/slaves", device ) );
      44             : 
      45           0 :   FILE * fp = fopen( path, "r" );
      46           0 :   if( FD_UNLIKELY( !fp ) )
      47           0 :     FD_LOG_ERR(( "error configuring network device, fopen(%s) failed (%i-%s)", path, errno, fd_io_strerror( errno ) ));
      48           0 :   if( FD_UNLIKELY( !fgets( output, 4096, fp ) ) )
      49           0 :     FD_LOG_ERR(( "error configuring network device, fgets(%s) failed (%i-%s)", path, errno, fd_io_strerror( errno ) ));
      50           0 :   if( FD_UNLIKELY( feof( fp ) ) ) FD_LOG_ERR(( "error configuring network device, fgets(%s) failed (EOF)", path ));
      51           0 :   if( FD_UNLIKELY( ferror( fp ) ) ) FD_LOG_ERR(( "error configuring network device, fgets(%s) failed (error)", path ));
      52           0 :   if( FD_UNLIKELY( strlen( output ) == 4095 ) ) FD_LOG_ERR(( "line too long in `%s`", path ));
      53           0 :   if( FD_UNLIKELY( strlen( output ) == 0 ) ) FD_LOG_ERR(( "line empty in `%s`", path ));
      54           0 :   if( FD_UNLIKELY( fclose( fp ) ) )
      55           0 :     FD_LOG_ERR(( "error configuring network device, fclose(%s) failed (%i-%s)", path, errno, fd_io_strerror( errno ) ));
      56           0 :   output[ strlen( output ) - 1 ] = '\0';
      57           0 : }
      58             : 
      59             : static void
      60           0 : init_device( const char * device ) {
      61           0 :   if( FD_UNLIKELY( strlen( device ) >= IF_NAMESIZE ) ) FD_LOG_ERR(( "device name `%s` is too long", device ));
      62           0 :   if( FD_UNLIKELY( strlen( device ) == 0 ) ) FD_LOG_ERR(( "device name `%s` is empty", device ));
      63             : 
      64           0 :   int sock = socket( AF_INET, SOCK_DGRAM, 0 );
      65           0 :   if( FD_UNLIKELY( sock < 0 ) )
      66           0 :     FD_LOG_ERR(( "error configuring network device, socket(AF_INET,SOCK_DGRAM,0) failed (%i-%s)",
      67           0 :                  errno, fd_io_strerror( errno ) ));
      68             : 
      69           0 :   struct ifreq ifr = {0};
      70           0 :   strncpy( ifr.ifr_name, device, IF_NAMESIZE-1 );
      71             : 
      72             :   /* turn off generic-receive-offload, which is entirely incompatible with
      73             :    * AF_XDP and QUIC
      74             :    * It results in multiple UDP payloads being merged into a single UDP packet,
      75             :    * with IP and UDP headers rewritten, combining the lengths and updating the
      76             :    * checksums. QUIC short packets cannot be processed reliably in this case. */
      77             : 
      78             :   /* command for generic-receive-offload = off */
      79           0 :   struct ethtool_value gro = { .cmd = ETHTOOL_SGRO, .data = 0 };
      80             : 
      81             :   /* attach command to ifr */
      82           0 :   ifr.ifr_data = (void *)&gro;
      83             : 
      84             :   /* log command */
      85           0 :   FD_LOG_NOTICE(( "RUN: `ethtool --offload %s generic-receive-offload off`",
      86           0 :                   device ));
      87             : 
      88             :   /* execute command */
      89           0 :   if( FD_UNLIKELY( ioctl( sock, SIOCETHTOOL, &ifr ) ) ) {
      90           0 :     FD_LOG_ERR(( "configuring network device, ioctl(SIOCETHTOOL,ETHTOOL_SGRO) failed (%i-%s)",
      91           0 :                  errno, fd_io_strerror( errno ) ));
      92           0 :   }
      93             : 
      94           0 :   if( FD_UNLIKELY( close( sock ) ) )
      95           0 :     FD_LOG_ERR(( "error configuring network device, close() socket failed (%i-%s)", errno, fd_io_strerror( errno ) ));
      96           0 : }
      97             : 
      98             : static void
      99           0 : init( config_t * const config ) {
     100             :   /* we need one channel for both TX and RX on the NIC for each QUIC
     101             :      tile, but the interface probably defaults to one channel total */
     102           0 :   if( FD_UNLIKELY( device_is_bonded( config->tiles.net.interface ) ) ) {
     103             :     /* if using a bonded device, we need to disable gro on the
     104             :        underlying devices.
     105             : 
     106             :        we don't need to disable gro on the bonded device, as the packets are
     107             :        redirected by XDP before any of the kernel bonding logic */
     108           0 :     char line[ 4096 ];
     109           0 :     device_read_slaves( config->tiles.net.interface, line );
     110           0 :     char * saveptr;
     111           0 :     for( char * token=strtok_r( line , " \t", &saveptr ); token!=NULL; token=strtok_r( NULL, " \t", &saveptr ) ) {
     112           0 :       init_device( token );
     113           0 :     }
     114           0 :   } else {
     115           0 :     init_device( config->tiles.net.interface );
     116           0 :   }
     117           0 : }
     118             : 
     119             : static configure_result_t
     120           0 : check_device( const char * device ) {
     121           0 :   if( FD_UNLIKELY( strlen( device ) >= IF_NAMESIZE ) ) FD_LOG_ERR(( "device name `%s` is too long", device ));
     122           0 :   if( FD_UNLIKELY( strlen( device ) == 0 ) ) FD_LOG_ERR(( "device name `%s` is empty", device ));
     123             : 
     124           0 :   int sock = socket( AF_INET, SOCK_DGRAM, 0 );
     125           0 :   if( FD_UNLIKELY( sock < 0 ) )
     126           0 :     FD_LOG_ERR(( "error configuring network device, socket(AF_INET,SOCK_DGRAM,0) failed (%i-%s)",
     127           0 :                  errno, fd_io_strerror( errno ) ));
     128             : 
     129           0 :   struct ifreq ifr = {0};
     130           0 :   strncpy( ifr.ifr_name, device, IF_NAMESIZE );
     131           0 :   ifr.ifr_name[ IF_NAMESIZE - 1 ] = '\0'; // silence linter, not needed for correctness
     132             : 
     133             :   /* check generic-receive-offload, which is entirely incompatible with
     134             :    * AF_XDP and QUIC */
     135             : 
     136             :   /* command for getting generic-receive-offload */
     137           0 :   struct ethtool_value gro = { .cmd = ETHTOOL_GGRO, .data = 0 };
     138             : 
     139             :   /* attach command to ifr */
     140           0 :   ifr.ifr_data = (void *)&gro;
     141             : 
     142             :   /* execute command */
     143           0 :   if( FD_UNLIKELY( ioctl( sock, SIOCETHTOOL, &ifr ) ) ) {
     144           0 :     if( FD_LIKELY( errno != EOPNOTSUPP ) ) {
     145           0 :       FD_LOG_ERR(( "configuring network device, ioctl(SIOCETHTOOL,ETHTOOL_GGRO) failed (%i-%s)",
     146           0 :                    errno, fd_io_strerror( errno ) ));
     147           0 :     }
     148           0 :   }
     149             : 
     150           0 :   if( FD_UNLIKELY( close( sock ) ) )
     151           0 :     FD_LOG_ERR(( "error configuring network device, close() socket failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     152             : 
     153             :   /* if generic-receive-offload enabled, set NOT_CONFIGURED */
     154           0 :   if( FD_UNLIKELY( gro.data ) ) {
     155           0 :     NOT_CONFIGURED( "device `%s` has generic-receive-offload enabled. Should be disabled",
     156           0 :                     device );
     157           0 :   }
     158             : 
     159           0 :   CONFIGURE_OK();
     160           0 : }
     161             : 
     162             : static configure_result_t
     163           0 : check( config_t * const config ) {
     164           0 :   if( FD_UNLIKELY( device_is_bonded( config->tiles.net.interface ) ) ) {
     165           0 :     char line[ 4096 ];
     166           0 :     device_read_slaves( config->tiles.net.interface, line );
     167           0 :     char * saveptr;
     168           0 :     for( char * token=strtok_r( line, " \t", &saveptr ); token!=NULL; token=strtok_r( NULL, " \t", &saveptr ) ) {
     169           0 :       CHECK( check_device( token ) );
     170           0 :     }
     171           0 :   } else {
     172           0 :     CHECK( check_device( config->tiles.net.interface ) );
     173           0 :   }
     174             : 
     175           0 :   CONFIGURE_OK();
     176           0 : }
     177             : 
     178             : configure_stage_t fd_cfg_stage_ethtool_gro = {
     179             :   .name            = NAME,
     180             :   .always_recreate = 0,
     181             :   .enabled         = enabled,
     182             :   .init_perm       = init_perm,
     183             :   .fini_perm       = NULL,
     184             :   .init            = init,
     185             :   .fini            = NULL,
     186             :   .check           = check,
     187             : };
     188             : 
     189             : #undef NAME

Generated by: LCOV version 1.14