LCOV - code coverage report
Current view: top level - app/shared/commands/configure - fd_ethtool_ioctl.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 387 0.0 %
Date: 2025-10-14 04:31:44 Functions: 0 16 0.0 %

          Line data    Source code
       1             : #include <errno.h>
       2             : #include <unistd.h>
       3             : #include <linux/ethtool.h>
       4             : #include <linux/sockios.h>
       5             : #include <net/if.h>
       6             : #include <sys/ioctl.h>
       7             : #include <sys/socket.h>
       8             : 
       9             : #include "fd_ethtool_ioctl.h"
      10             : #include "../../../../util/fd_util.h"
      11             : 
      12             : #define MAX_RXFH_KEY_SIZE   (1024)
      13           0 : #define MAX_FEATURES        (2048)
      14             : #define MAX_NTUPLE_RULES    (8192)
      15             : 
      16             : #define ETHTOOL_CMD_SIZE( base_t, data_t, data_len ) ( sizeof(base_t) + (sizeof(data_t)*(data_len)) )
      17             : 
      18             : static int
      19             : run_ioctl( fd_ethtool_ioctl_t * ioc,
      20             :            char const *         cmd,
      21             :            void *               data,
      22             :            int                  log,
      23           0 :            int                  log_notsupp ) {
      24           0 :   ioc->ifr.ifr_data = data;
      25           0 :   if( FD_UNLIKELY( ioctl( ioc->fd, SIOCETHTOOL, &ioc->ifr ) ) ) {
      26           0 :     if( (!!log) & ((errno!=EOPNOTSUPP) | (!!log_notsupp)) )
      27           0 :       FD_LOG_WARNING(( "error configuring network device (%s), ioctl(SIOCETHTOOL,%s) failed (%i-%s)",
      28           0 :                        ioc->ifr.ifr_name, cmd, errno, fd_io_strerror( errno ) ));
      29           0 :     return errno;
      30           0 :   }
      31           0 :   return 0;
      32           0 : }
      33             : 
      34             : #define TRY_RUN_IOCTL( ioc, cmd, data ) \
      35           0 :   do { int __ret__ = run_ioctl( (ioc), (cmd), (data), 1, 1 ); \
      36           0 :        if( FD_UNLIKELY( __ret__ != 0 ) ) { return __ret__; } } while(0)
      37             : 
      38             : fd_ethtool_ioctl_t *
      39             : fd_ethtool_ioctl_init( fd_ethtool_ioctl_t * ioc,
      40           0 :                        char const * device ) {
      41           0 :   if( FD_UNLIKELY( strlen( device ) >= IF_NAMESIZE ) ) {
      42           0 :     FD_LOG_WARNING(( "device name `%s` is too long", device ));
      43           0 :     return NULL;
      44           0 :   }
      45           0 :   if( FD_UNLIKELY( strlen( device ) == 0 ) ) {
      46           0 :     FD_LOG_WARNING(( "device name `%s` is empty", device ));
      47           0 :     return NULL;
      48           0 :   }
      49             : 
      50           0 :   ioc->fd = socket( AF_INET, SOCK_DGRAM, 0 );
      51           0 :   if( FD_UNLIKELY( ioc->fd < 0 ) ) {
      52           0 :     FD_LOG_WARNING(( "error configuring network device (%s), socket(AF_INET,SOCK_DGRAM,0) failed (%i-%s)",
      53           0 :                      device, errno, fd_io_strerror( errno ) ));
      54           0 :     return NULL;
      55           0 :   }
      56             : 
      57           0 :   fd_memset( &ioc->ifr, 0, sizeof(struct ifreq) );
      58           0 :   fd_cstr_fini( fd_cstr_append_cstr( fd_cstr_init( ioc->ifr.ifr_name ), device ) );
      59             : 
      60           0 :   return ioc;
      61           0 : }
      62             : 
      63             : void
      64           0 : fd_ethtool_ioctl_fini( fd_ethtool_ioctl_t * ioc ) {
      65           0 :   if( FD_UNLIKELY( close( ioc->fd ) ) ) {
      66           0 :     FD_LOG_WARNING(( "error configuring network device (%s), close() socket failed (%i-%s)",
      67           0 :                      ioc->ifr.ifr_name, errno, fd_io_strerror( errno ) ));
      68           0 :   }
      69           0 :   ioc->fd = -1;
      70           0 :   fd_memset( &ioc->ifr, 0, sizeof(struct ifreq) );
      71           0 : }
      72             : 
      73             : int
      74             : fd_ethtool_ioctl_channels_set_num( fd_ethtool_ioctl_t * ioc,
      75           0 :                                    uint                 num ) {
      76           0 :   struct ethtool_channels ech = { .cmd = ETHTOOL_GCHANNELS };
      77           0 :   int ret = run_ioctl( ioc, "ETHTOOL_GCHANNELS", &ech, 1, 0 );
      78           0 :   if( FD_UNLIKELY( ret!=0 ) ) {
      79           0 :     if( (ret==EOPNOTSUPP) & ((num==0) | (num==1))) return 0;
      80           0 :     return ret;
      81           0 :   }
      82             : 
      83           0 :   ech.cmd = ETHTOOL_SCHANNELS;
      84           0 :   if( num == 0 ) {
      85           0 :     uint max_queue_count = ech.max_combined ? ech.max_combined : ech.max_rx;
      86           0 :     num = fd_uint_min( max_queue_count, (uint)fd_shmem_cpu_cnt() );
      87           0 :   }
      88           0 :   if( ech.max_combined ) {
      89           0 :     ech.combined_count = num;
      90           0 :     ech.rx_count       = 0;
      91           0 :     ech.tx_count       = 0;
      92           0 :     FD_LOG_NOTICE(( "RUN: `ethtool --set-channels %s combined %u`", ioc->ifr.ifr_name, num ));
      93           0 :   } else {
      94           0 :     ech.combined_count = 0;
      95           0 :     ech.rx_count       = num;
      96           0 :     ech.tx_count       = num;
      97           0 :     FD_LOG_NOTICE(( "RUN: `ethtool --set-channels %s rx %u tx %u`", ioc->ifr.ifr_name, num, num ));
      98           0 :   }
      99           0 :   TRY_RUN_IOCTL( ioc, "ETHTOOL_SCHANNELS", &ech );
     100           0 :   return 0;
     101           0 : }
     102             : 
     103             : int
     104             : fd_ethtool_ioctl_channels_get_num( fd_ethtool_ioctl_t * ioc,
     105           0 :                                    fd_ethtool_ioctl_channels_t * channels ) {
     106           0 :   struct ethtool_channels ech = { .cmd = ETHTOOL_GCHANNELS };
     107           0 :   int ret = run_ioctl( ioc, "ETHTOOL_GCHANNELS", &ech, 1, 0 );
     108           0 :   if( FD_UNLIKELY( ret!=0 ) ) {
     109           0 :     if( FD_LIKELY( ret==EOPNOTSUPP ) ) {
     110             :       /* network device doesn't support getting number of channels, so
     111             :          it must always be 1 */
     112           0 :       channels->supported = 0;
     113           0 :       channels->current = 1;
     114           0 :       channels->max = 1;
     115           0 :       return 0;
     116           0 :     }
     117           0 :     return ret;
     118           0 :   }
     119           0 :   channels->supported = 1;
     120             : 
     121           0 :   if( FD_LIKELY( ech.combined_count ) ) {
     122           0 :     channels->current = ech.combined_count;
     123           0 :     channels->max = fd_uint_min( ech.max_combined, (uint)fd_shmem_cpu_cnt() );
     124           0 :     return 0;
     125           0 :   }
     126           0 :   if( ech.rx_count || ech.tx_count ) {
     127           0 :     if( FD_UNLIKELY( ech.rx_count != ech.tx_count ) )
     128           0 :       FD_LOG_WARNING(( "device `%s` has unbalanced channel count: (got %u rx, %u tx)",
     129           0 :                        ioc->ifr.ifr_name, ech.rx_count, ech.tx_count ));
     130           0 :     channels->current = ech.rx_count;
     131           0 :     channels->max = fd_uint_min( ech.max_rx, (uint)fd_shmem_cpu_cnt() );
     132           0 :     return 0;
     133           0 :   }
     134           0 :   return EINVAL;
     135           0 : }
     136             : 
     137             : int
     138           0 : fd_ethtool_ioctl_rxfh_set_default( fd_ethtool_ioctl_t * ioc ) {
     139           0 :   FD_LOG_NOTICE(( "RUN: `ethtool --set-rxfh-indir %s default`", ioc->ifr.ifr_name ));
     140           0 :   struct ethtool_rxfh_indir rxfh = {
     141           0 :     .cmd = ETHTOOL_SRXFHINDIR,
     142           0 :     .size = 0, /* default indirection table */
     143           0 :   };
     144           0 :   int ret = run_ioctl( ioc, "ETHTOOL_SRXFHINDIR", &rxfh, 1, 0 );
     145           0 :   if( FD_UNLIKELY( ret==EOPNOTSUPP ) ) return 0;
     146           0 :   return ret;
     147           0 : }
     148             : 
     149             : int
     150             : fd_ethtool_ioctl_rxfh_set_suffix( fd_ethtool_ioctl_t * ioc,
     151           0 :                                   uint                 start_idx ) {
     152             :   /* Get current channel count */
     153           0 :   struct ethtool_channels ech = { .cmd = ETHTOOL_GCHANNELS };
     154           0 :   TRY_RUN_IOCTL( ioc, "ETHTOOL_GCHANNELS", &ech );
     155           0 :   uint const num_channels = ech.combined_count ? ech.combined_count : ech.rx_count;
     156           0 :   if( FD_UNLIKELY( start_idx >= num_channels ))
     157           0 :     return EINVAL;
     158             : 
     159           0 :   union {
     160           0 :     struct ethtool_rxfh_indir m;
     161           0 :     uchar _[ ETHTOOL_CMD_SIZE( struct ethtool_rxfh_indir, uint, FD_ETHTOOL_MAX_RXFH_TABLE_CNT ) ];
     162           0 :   } rxfh = { 0 };
     163             : 
     164             :   /* Get count of rx indirection table */
     165           0 :   rxfh.m.cmd = ETHTOOL_GRXFHINDIR;
     166           0 :   rxfh.m.size = 0;
     167           0 :   TRY_RUN_IOCTL( ioc, "ETHTOOL_GRXFHINDIR", &rxfh );
     168           0 :   uint const table_ele_cnt = rxfh.m.size;
     169           0 :   if( FD_UNLIKELY( (table_ele_cnt == 0) | (table_ele_cnt > FD_ETHTOOL_MAX_RXFH_TABLE_CNT) ) )
     170           0 :     return EINVAL;
     171             : 
     172             :   /* Set table to round robin over all channels from [start_idx, num_channels) */
     173           0 :   FD_LOG_NOTICE(( "RUN: `ethtool --set-rxfh-indir %s start %u equal %u`",
     174           0 :                   ioc->ifr.ifr_name, start_idx, num_channels - start_idx ));
     175           0 :   rxfh.m.cmd = ETHTOOL_SRXFHINDIR;
     176           0 :   rxfh.m.size = table_ele_cnt;
     177           0 :   uint i = start_idx;
     178           0 :   for(uint j=0u; j<table_ele_cnt; j++) {
     179           0 :     rxfh.m.ring_index[ j ] = i++;
     180           0 :     if( i >= num_channels )
     181           0 :       i = start_idx;
     182           0 :   }
     183           0 :   TRY_RUN_IOCTL( ioc, "ETHTOOL_SRXFHINDIR", &rxfh );
     184             : 
     185           0 :   return 0;
     186           0 : }
     187             : 
     188             : int
     189             : fd_ethtool_ioctl_rxfh_get_table( fd_ethtool_ioctl_t * ioc,
     190             :                                  uint *               table,
     191           0 :                                  uint *               table_ele_cnt ) {
     192             :   /* Note: A simpler implementation of this would use ETHTOOL_GRXFHINDIR
     193             :      as we are only concerned with the indirection table and do not need
     194             :      the other information. However, it appears that the ice driver has
     195             :      a bugged implementation of this command. */
     196             : 
     197           0 :   union {
     198           0 :     struct ethtool_rxfh m;
     199           0 :     uchar _[ ETHTOOL_CMD_SIZE( struct ethtool_rxfh, uint, FD_ETHTOOL_MAX_RXFH_TABLE_CNT ) + MAX_RXFH_KEY_SIZE ];
     200           0 :   } rxfh = { 0 };
     201             : 
     202             :   /* First get the count of the indirection table and hash key */
     203           0 :   rxfh.m.cmd = ETHTOOL_GRSSH;
     204           0 :   int ret = run_ioctl( ioc, "ETHTOOL_GRSSH", &rxfh, 1, 0 );
     205           0 :   if( FD_UNLIKELY( ret!=0 ) ) {
     206           0 :     if( FD_LIKELY( ret==EOPNOTSUPP ) ) {
     207           0 :       *table_ele_cnt = 0;
     208           0 :       return 0;
     209           0 :     }
     210           0 :     return ret;
     211           0 :   }
     212           0 :   if( FD_UNLIKELY( (rxfh.m.indir_size > FD_ETHTOOL_MAX_RXFH_TABLE_CNT) |
     213           0 :                    (rxfh.m.key_size   > MAX_RXFH_KEY_SIZE) ) )
     214           0 :     return EINVAL;
     215           0 :   *table_ele_cnt = rxfh.m.indir_size;
     216             : 
     217           0 :   if( 0!=*table_ele_cnt ) {
     218             :     /* Now get the table contents itself. We also get the key bytes. */
     219           0 :     TRY_RUN_IOCTL( ioc, "ETHTOOL_GRSSH", &rxfh );
     220           0 :     fd_memcpy( table, rxfh.m.rss_config, *table_ele_cnt * sizeof(uint) );
     221           0 :   }
     222           0 :   return 0;
     223           0 : }
     224             : 
     225             : static int
     226             : get_feature_idx( fd_ethtool_ioctl_t * ioc,
     227             :                  char const *         name,
     228             :                  uint *               feature_idx,
     229           0 :                  uint *               feature_cnt ) {
     230             :   /* Check size of features string set is not too large (prevent overflow) */
     231           0 :   union {
     232           0 :     struct ethtool_sset_info m;
     233           0 :     uchar _[ ETHTOOL_CMD_SIZE( struct ethtool_sset_info, uint, 1 ) ];
     234           0 :   } esi = { .m = {
     235           0 :     .cmd = ETHTOOL_GSSET_INFO,
     236           0 :     .sset_mask = fd_ulong_mask_bit( ETH_SS_FEATURES )
     237           0 :   } };
     238           0 :   TRY_RUN_IOCTL( ioc, "ETHTOOL_GSSET_INFO", &esi );
     239           0 :   if( FD_UNLIKELY( (esi.m.data[0] == 0) | (esi.m.data[0] > MAX_FEATURES) ) )
     240           0 :     return EINVAL;
     241           0 :   *feature_cnt = esi.m.data[0];
     242             : 
     243             :   /* Get strings from features string set */
     244           0 :   union {
     245           0 :     struct ethtool_gstrings m;
     246           0 :     uchar _[ sizeof(struct ethtool_gstrings) + (MAX_FEATURES * ETH_GSTRING_LEN) ];
     247           0 :   } egs = { 0 };
     248           0 :   egs.m.cmd = ETHTOOL_GSTRINGS;
     249           0 :   egs.m.string_set = ETH_SS_FEATURES;
     250           0 :   TRY_RUN_IOCTL( ioc, "ETHTOOL_GSTRINGS", &egs );
     251             : 
     252             :   /* Find index of matching string from the set */
     253           0 :   for( uint i=0U; i<egs.m.len; i++) {
     254           0 :     uchar const * gstring = egs.m.data + (i * ETH_GSTRING_LEN);
     255           0 :     if( 0==strncmp( (char const *)gstring, name, ETH_GSTRING_LEN ) ) {
     256           0 :       *feature_idx = i;
     257           0 :       return 0;
     258           0 :     }
     259           0 :   }
     260           0 :   return -1;
     261           0 : }
     262             : 
     263             : int
     264             : fd_ethtool_ioctl_feature_set( fd_ethtool_ioctl_t * ioc,
     265             :                               char const *         name,
     266           0 :                               int                  enabled ) {
     267           0 :   uint feature_idx;
     268           0 :   uint feature_cnt;
     269           0 :   int ret = get_feature_idx( ioc, name, &feature_idx, &feature_cnt );
     270           0 :   if( FD_UNLIKELY( ret!=0 ) ) {
     271           0 :     if( (ret==-1) & (!enabled) ) return 0;
     272           0 :     return EINVAL;
     273           0 :   }
     274           0 :   uint feature_block = feature_idx / 32U;
     275           0 :   uint feature_offset = feature_idx % 32U;
     276             : 
     277           0 :   union {
     278           0 :     struct ethtool_gfeatures m;
     279           0 :     uchar _[ ETHTOOL_CMD_SIZE( struct ethtool_gfeatures, struct ethtool_get_features_block, MAX_FEATURES / 32U ) ];
     280           0 :   } egf = { 0 };
     281           0 :   egf.m.cmd = ETHTOOL_GFEATURES;
     282           0 :   egf.m.size = MAX_FEATURES / 32U;
     283           0 :   TRY_RUN_IOCTL( ioc, "ETHTOOL_GFEATURES", &egf );
     284           0 :   if( enabled == !!(egf.m.features[ feature_block ].active & fd_uint_mask_bit( (int)feature_offset )) )
     285           0 :     return 0;
     286             : 
     287           0 :   FD_LOG_NOTICE(( "RUN: `ethtool --features %s %s %s`",
     288           0 :                   ioc->ifr.ifr_name, name, enabled ? "on" : "off" ));
     289           0 :   union {
     290           0 :     struct ethtool_sfeatures m;
     291           0 :     uchar _[ ETHTOOL_CMD_SIZE( struct ethtool_sfeatures, struct ethtool_set_features_block, MAX_FEATURES / 32U ) ];
     292           0 :   } esf = { 0 };
     293           0 :   esf.m.cmd = ETHTOOL_SFEATURES;
     294           0 :   esf.m.size = fd_uint_align_up( feature_cnt, 32U ) / 32U;
     295           0 :   esf.m.features[ feature_block ].valid = fd_uint_mask_bit( (int)feature_offset );
     296           0 :   esf.m.features[ feature_block ].requested = enabled ? fd_uint_mask_bit( (int)feature_offset ) : 0;
     297             : 
     298             :   /* Note: ETHTOOL_SFEATURES has special behavior where it returns a
     299             :      positive nonzero number with flags set for specific things.
     300             :      ETHTOOL_F_UNSUPPORTED is set if the feature is not able to be
     301             :      changed, i.e. it is forever fixed on or fixed off. */
     302           0 :   ioc->ifr.ifr_data = &esf;
     303           0 :   ret = ioctl( ioc->fd, SIOCETHTOOL, &ioc->ifr );
     304           0 :   if( FD_UNLIKELY( ret < 0 ) ) {
     305           0 :     FD_LOG_WARNING(( "error configuring network device (%s), ioctl(SIOCETHTOOL,ETHTOOL_SFEATURES) failed (%i-%s)",
     306           0 :                      ioc->ifr.ifr_name, errno, fd_io_strerror( errno ) ));
     307           0 :     return errno;
     308           0 :   }
     309           0 :   if( FD_UNLIKELY( ret==ETHTOOL_F_UNSUPPORTED ) ) {
     310           0 :     FD_LOG_WARNING(( "error configuring network device (%s), unable to change fixed feature (%s)",
     311           0 :                      ioc->ifr.ifr_name, name ));
     312           0 :     return EINVAL;
     313           0 :   }
     314           0 :   if( FD_UNLIKELY( ret!=0 ) ) {
     315           0 :     FD_LOG_WARNING(( "error configuring network device (%s), ioctl(SIOCETHTOOL,ETHTOOL_SFEATURES) failed (%d)",
     316           0 :                      ioc->ifr.ifr_name, ret ));
     317           0 :     return EINVAL;
     318           0 :   }
     319           0 :   return 0;
     320           0 : }
     321             : 
     322             : int
     323             : fd_ethtool_ioctl_feature_test( fd_ethtool_ioctl_t * ioc,
     324             :                                char const *         name,
     325           0 :                                int *                enabled ) {
     326           0 :   uint feature_idx;
     327           0 :   uint feature_cnt;
     328           0 :   int ret = get_feature_idx( ioc, name, &feature_idx, &feature_cnt );
     329           0 :   if( FD_UNLIKELY( ret!=0 ) ) {
     330           0 :     if( ret==-1 ) {
     331           0 :       *enabled = 0;
     332           0 :       return 0;
     333           0 :     }
     334           0 :     return EINVAL;
     335           0 :   }
     336           0 :   uint feature_block = feature_idx / 32U;
     337           0 :   uint feature_offset = feature_idx % 32U;
     338             : 
     339           0 :   union {
     340           0 :     struct ethtool_gfeatures m;
     341           0 :     uchar _[ ETHTOOL_CMD_SIZE( struct ethtool_gfeatures, struct ethtool_get_features_block, MAX_FEATURES / 32U ) ];
     342           0 :   } egf = { 0 };
     343           0 :   egf.m.cmd = ETHTOOL_GFEATURES;
     344           0 :   egf.m.size = MAX_FEATURES / 32U;
     345           0 :   TRY_RUN_IOCTL( ioc, "ETHTOOL_GFEATURES", &egf );
     346             : 
     347           0 :   *enabled = !!(egf.m.features[ feature_block ].active & fd_uint_mask_bit( (int)feature_offset ));
     348           0 :   return 0;
     349           0 : }
     350             : 
     351             : int
     352             : fd_ethtool_ioctl_feature_gro_set( fd_ethtool_ioctl_t * ioc,
     353           0 :                                   int                  enabled ) {
     354           0 :   FD_LOG_NOTICE(( "RUN: `ethtool --offload %s generic-receive-offload %s`",
     355           0 :                   ioc->ifr.ifr_name, enabled ? "on" : "off" ));
     356           0 :   struct ethtool_value gro = {
     357           0 :     .cmd = ETHTOOL_SGRO,
     358           0 :     .data = !!enabled
     359           0 :   };
     360           0 :   int ret = run_ioctl( ioc, "ETHTOOL_SGRO", &gro, 1, 0 );
     361           0 :   if( FD_UNLIKELY( (ret==EOPNOTSUPP) & (!enabled) ) ) return 0;
     362           0 :   return ret;
     363           0 : }
     364             : 
     365             : int
     366             : fd_ethtool_ioctl_feature_gro_test( fd_ethtool_ioctl_t * ioc,
     367           0 :                                    int *                enabled ) {
     368           0 :   struct ethtool_value gro = { .cmd = ETHTOOL_GGRO };
     369           0 :   int ret = run_ioctl( ioc, "ETHTOOL_GGRO", &gro, 1, 0 );
     370           0 :   if( FD_UNLIKELY( ret!=0 ) ) {
     371           0 :     if( FD_LIKELY( ret==EOPNOTSUPP ) ) {
     372           0 :       *enabled = 0;
     373           0 :       return 0;
     374           0 :     }
     375           0 :     return ret;
     376           0 :   }
     377           0 :   *enabled = !!gro.data;
     378           0 :   return 0;
     379           0 : }
     380             : 
     381             : int
     382           0 : fd_ethtool_ioctl_ntuple_clear( fd_ethtool_ioctl_t * ioc ) {
     383           0 :   union {
     384           0 :     struct ethtool_rxnfc m;
     385           0 :     uchar _[ ETHTOOL_CMD_SIZE( struct ethtool_rxnfc, uint, MAX_NTUPLE_RULES ) ];
     386           0 :   } efc = { 0 };
     387             : 
     388             :   /* Get count of currently defined rules, return if none exist */
     389           0 :   efc.m.cmd = ETHTOOL_GRXCLSRLCNT;
     390           0 :   int ret = run_ioctl( ioc, "ETHTOOL_GRXCLSRLCNT", &efc, 1, 0 );
     391           0 :   if( FD_UNLIKELY( ret!=0 ) ) {
     392           0 :     if( ret==EOPNOTSUPP ) return 0;
     393           0 :     return ret;
     394           0 :   }
     395           0 :   uint const rule_cnt = efc.m.rule_cnt;
     396           0 :   if( FD_UNLIKELY( rule_cnt > MAX_NTUPLE_RULES ) )
     397           0 :     return EINVAL;
     398           0 :   if( rule_cnt == 0 )
     399           0 :     return 0;
     400             : 
     401             :   /* Get location indices of all rules */
     402           0 :   efc.m.cmd = ETHTOOL_GRXCLSRLALL;
     403           0 :   TRY_RUN_IOCTL( ioc, "ETHTOOL_GRXCLSRLALL", &efc );
     404             : 
     405             :   /* Delete all rules */
     406           0 :   for( uint i=0u; i<rule_cnt; i++) {
     407           0 :     FD_LOG_NOTICE(( "RUN: `ethtool --config-ntuple %s delete %u`", ioc->ifr.ifr_name, efc.m.rule_locs[ i ] ));
     408           0 :     struct ethtool_rxnfc del = {
     409           0 :       .cmd = ETHTOOL_SRXCLSRLDEL,
     410           0 :       .fs = { .location = efc.m.rule_locs[ i ] }
     411           0 :     };
     412           0 :     TRY_RUN_IOCTL( ioc, "ETHTOOL_SRXCLSRLDEL", &del );
     413           0 :   }
     414             : 
     415           0 :   return 0;
     416           0 : }
     417             : 
     418             : int
     419             : fd_ethtool_ioctl_ntuple_set_udp_dport( fd_ethtool_ioctl_t * ioc,
     420             :                                        uint                 rule_idx,
     421             :                                        ushort               dport,
     422           0 :                                        uint                 queue_idx ) {
     423             :   /* Note: Some drivers do not support RX_CLS_LOC_ANY (e.g. mlx5), and
     424             :      some drivers only support it (e.g. bnxt). So first we try with
     425             :      the explicit rule index and then again with the any location if
     426             :      the former failed. */
     427           0 :   FD_LOG_NOTICE(( "RUN: `ethtool --config-ntuple %s flow-type udp4 dst-port %hu queue %u`",
     428           0 :                   ioc->ifr.ifr_name, dport, queue_idx ));
     429           0 :   struct ethtool_rxnfc efc = {
     430           0 :     .cmd = ETHTOOL_SRXCLSRLINS,
     431           0 :     .fs = {
     432           0 :       .flow_type = UDP_V4_FLOW,
     433           0 :       .h_u = { .udp_ip4_spec = { .pdst = fd_ushort_bswap( dport ) } },
     434           0 :       .m_u = { .udp_ip4_spec = { .pdst = 0xFFFF } },
     435           0 :       .ring_cookie = queue_idx,
     436           0 :       .location = rule_idx
     437           0 :     }
     438           0 :   };
     439           0 :   if( FD_LIKELY( 0==run_ioctl( ioc, "ETHTOOL_SRXCLSRLINS", &efc, 0, 0 ) ) )
     440           0 :     return 0;
     441           0 :   efc.fs.location = RX_CLS_LOC_ANY;
     442           0 :   TRY_RUN_IOCTL( ioc, "ETHTOOL_SRXCLSRLINS", &efc );
     443           0 :   return 0;
     444           0 : }
     445             : 
     446             : int
     447             : fd_ethtool_ioctl_ntuple_validate_udp_dport( fd_ethtool_ioctl_t * ioc,
     448             :                                             ushort *             dports,
     449             :                                             uint                 num_dports,
     450             :                                             uint                 queue_idx,
     451           0 :                                             int *                valid ) {
     452           0 :   union {
     453           0 :     struct ethtool_rxnfc m;
     454           0 :     uchar _[ ETHTOOL_CMD_SIZE( struct ethtool_rxnfc, uint, MAX_NTUPLE_RULES ) ];
     455           0 :   } efc = { 0 };
     456             : 
     457             :   /* Get count of currently defined rules */
     458           0 :   efc.m.cmd = ETHTOOL_GRXCLSRLCNT;
     459           0 :   int ret = run_ioctl( ioc, "ETHTOOL_GRXCLSRLCNT", &efc, 1, 0 );
     460           0 :   if( FD_UNLIKELY( ret!=0 ) ) {
     461           0 :     if( FD_LIKELY( ret==EOPNOTSUPP ) ) {
     462           0 :       *valid = (num_dports == 0);
     463           0 :       return 0;
     464           0 :     }
     465           0 :     return ret;
     466           0 :   }
     467           0 :   uint const rule_cnt = efc.m.rule_cnt;
     468           0 :   if( FD_UNLIKELY( rule_cnt > MAX_NTUPLE_RULES ) )
     469           0 :     return EINVAL;
     470           0 :   if( rule_cnt == 0 ) {
     471           0 :     *valid = (num_dports == 0);
     472           0 :     return 0;
     473           0 :   }
     474           0 :   if( num_dports == 0 ) {
     475           0 :     *valid = 0;
     476           0 :     return 0;
     477           0 :   }
     478             : 
     479             :   /* Get location indices of all rules */
     480           0 :   efc.m.cmd = ETHTOOL_GRXCLSRLALL;
     481           0 :   efc.m.rule_cnt = rule_cnt;
     482           0 :   TRY_RUN_IOCTL( ioc, "ETHTOOL_GRXCLSRLALL", &efc );
     483             : 
     484             :   /* Loop over all rules, returning early if any are invalid */
     485           0 :   static const union ethtool_flow_union EXPECTED_MASK = { .udp_ip4_spec = { .pdst = 0xFFFF } };
     486           0 :   static const struct ethtool_flow_ext EXPECTED_EXT_MASK = { 0 };
     487           0 :   for( uint i=0u; i<efc.m.rule_cnt; i++) {
     488           0 :     struct ethtool_rxnfc get = {
     489           0 :       .cmd = ETHTOOL_GRXCLSRULE,
     490           0 :       .fs = { .location = efc.m.rule_locs[ i ] }
     491           0 :     };
     492           0 :     TRY_RUN_IOCTL( ioc, "ETHTOOL_GRXCLSRULE", &get );
     493           0 :     if( FD_UNLIKELY( ((get.fs.flow_type != UDP_V4_FLOW) | (get.fs.ring_cookie != queue_idx)) ||
     494           0 :                      0!=memcmp( &get.fs.m_u, &EXPECTED_MASK, sizeof(EXPECTED_MASK) ) ||
     495           0 :                      0!=memcmp( &get.fs.m_ext, &EXPECTED_EXT_MASK, sizeof(EXPECTED_EXT_MASK)) ) ) {
     496           0 :       *valid = 0;
     497           0 :       return 0;
     498           0 :     }
     499             :     /* This is a valid udp rule, find the expected port(s) it matches or return error */
     500           0 :     int found = 0;
     501           0 :     for( uint j=0u; j<num_dports; j++) {
     502           0 :       if( dports[ j ] == fd_ushort_bswap( get.fs.h_u.udp_ip4_spec.pdst ) ) {
     503           0 :         dports[ j ] = 0u;
     504           0 :         found = 1;
     505           0 :       }
     506           0 :     }
     507           0 :     if( !found ) {
     508           0 :       *valid = 0;
     509           0 :       return 0;
     510           0 :     }
     511           0 :   }
     512             : 
     513             :   /* All rules are valid and matched expected ports. Lastly, check that
     514             :      no expected ports were missing */
     515           0 :   *valid = 1;
     516           0 :   for( uint i=0u; i<num_dports; i++)
     517           0 :     if( dports[ i ] != 0 )
     518           0 :       *valid = 0;
     519           0 :   return 0;
     520           0 : }

Generated by: LCOV version 1.14