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