Line data Source code
1 : #define _GNU_SOURCE /* dup3 */
2 : #include "fd_sock_tile_private.h"
3 : #include "../fd_net_tile.h"
4 : #include "../../fd_disco_base.h"
5 : #include "../../topo/fd_topo.h"
6 : #include "../../../util/net/fd_eth.h"
7 : #include "../../../util/net/fd_ip4.h"
8 : #include "../../../util/net/fd_udp.h"
9 :
10 : #include <stdalign.h> /* alignof */
11 : #include <errno.h>
12 : #include <fcntl.h> /* fcntl */
13 : #include <unistd.h> /* dup3, close */
14 : #include <netinet/in.h> /* sockaddr_in */
15 : #include <net/if.h> /* if_nametoindex */
16 : #include <linux/filter.h> /* SO_ATTACH_FILTER */
17 : #include <sys/socket.h> /* socket */
18 : #include "../../metrics/fd_metrics.h"
19 :
20 : #include "generated/fd_sock_tile_seccomp.h"
21 :
22 : /* recv/sendmmsg packet count in batch and tango burst depth
23 : FIXME make configurable in the future?
24 : FIXME keep in sync with fd_net_tile_topo.c */
25 0 : #define STEM_BURST (64UL)
26 :
27 : /* Place RX socket file descriptors in contiguous integer range. */
28 0 : #define RX_SOCK_FD_MIN (128)
29 :
30 : /* Controls max ancillary data size.
31 : Must be aligned by alignof(struct cmsghdr) */
32 0 : #define FD_SOCK_CMSG_MAX (64UL)
33 :
34 : /* Musl's CMSG_NXTHDR macro compares size_t against ptrdiff_t,
35 : triggering -Wsign-compare. */
36 : #pragma GCC diagnostic push
37 : #pragma GCC diagnostic ignored "-Wsign-compare"
38 : #pragma GCC diagnostic ignored "-Wsign-conversion"
39 : static inline struct cmsghdr *
40 : fd_cmsg_nxthdr( struct msghdr * mhdr,
41 0 : struct cmsghdr * cmsg ) {
42 0 : return CMSG_NXTHDR( mhdr, cmsg );
43 0 : }
44 : #pragma GCC diagnostic pop
45 :
46 : static ulong
47 : populate_allowed_seccomp( fd_topo_t const * topo,
48 : fd_topo_tile_t const * tile,
49 : ulong out_cnt,
50 0 : struct sock_filter * out ) {
51 0 : FD_SCRATCH_ALLOC_INIT( l, fd_topo_obj_laddr( topo, tile->tile_obj_id ) );
52 0 : fd_sock_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_sock_tile_t), sizeof(fd_sock_tile_t) );
53 :
54 0 : populate_sock_filter_policy_fd_sock_tile( out_cnt, out, (uint)fd_log_private_logfile_fd(), (uint)ctx->tx_sock, RX_SOCK_FD_MIN, RX_SOCK_FD_MIN+(uint)ctx->sock_cnt );
55 0 : return sock_filter_policy_fd_sock_tile_instr_cnt;
56 0 : }
57 :
58 : static ulong
59 : populate_allowed_fds( fd_topo_t const * topo,
60 : fd_topo_tile_t const * tile,
61 : ulong out_fds_cnt,
62 0 : int * out_fds ) {
63 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
64 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
65 0 : fd_sock_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_sock_tile_t), sizeof(fd_sock_tile_t) );
66 :
67 0 : ulong sock_cnt = ctx->sock_cnt;
68 0 : if( FD_UNLIKELY( out_fds_cnt<sock_cnt+3UL ) ) {
69 0 : FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
70 0 : }
71 :
72 0 : ulong out_cnt = 0UL;
73 :
74 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
75 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) ) {
76 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
77 0 : }
78 0 : if( -1!=ctx->tx_sock ) out_fds[ out_cnt++ ] = ctx->tx_sock;
79 0 : for( ulong j=0UL; j<sock_cnt; j++ ) {
80 0 : out_fds[ out_cnt++ ] = ctx->pollfd[ j ].fd;
81 0 : }
82 0 : return out_cnt;
83 0 : }
84 :
85 : FD_FN_CONST static inline ulong
86 0 : tx_scratch_footprint( void ) {
87 0 : return STEM_BURST * fd_ulong_align_up( FD_NET_MTU, FD_CHUNK_ALIGN );
88 0 : }
89 :
90 : FD_FN_CONST static inline ulong
91 0 : scratch_align( void ) {
92 0 : return 4096UL;
93 0 : }
94 :
95 : FD_FN_PURE static inline ulong
96 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
97 0 : ulong l = FD_LAYOUT_INIT;
98 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_sock_tile_t), sizeof(fd_sock_tile_t) );
99 0 : l = FD_LAYOUT_APPEND( l, alignof(struct iovec), STEM_BURST*sizeof(struct iovec) );
100 0 : l = FD_LAYOUT_APPEND( l, alignof(struct cmsghdr), STEM_BURST*FD_SOCK_CMSG_MAX );
101 0 : l = FD_LAYOUT_APPEND( l, alignof(struct sockaddr_in), STEM_BURST*sizeof(struct sockaddr_in) );
102 0 : l = FD_LAYOUT_APPEND( l, alignof(struct mmsghdr), STEM_BURST*sizeof(struct mmsghdr) );
103 0 : l = FD_LAYOUT_APPEND( l, alignof(ulong), STEM_BURST*sizeof(ulong) );
104 0 : if( !tile->sock.only_recv_lo ) l = FD_LAYOUT_APPEND( l, FD_CHUNK_ALIGN, tx_scratch_footprint() );
105 0 : return FD_LAYOUT_FINI( l, scratch_align() );
106 0 : }
107 :
108 : /* create_udp_socket creates and configures a new UDP socket for the
109 : sock tile at the given file descriptor ID. */
110 :
111 : static void
112 : create_udp_socket( int sock_fd,
113 : uint bind_addr,
114 : ushort listen_port,
115 : int so_rcvbuf,
116 0 : uint lo_ifindex ) {
117 :
118 0 : if( fcntl( sock_fd, F_GETFD, 0 )!=-1 ) {
119 0 : FD_LOG_ERR(( "file descriptor %d already exists", sock_fd ));
120 0 : } else if( errno!=EBADF ) {
121 0 : FD_LOG_ERR(( "fcntl(F_GETFD) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
122 0 : }
123 :
124 0 : int orig_fd = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
125 0 : if( FD_UNLIKELY( orig_fd<0 ) ) {
126 0 : FD_LOG_ERR(( "socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
127 0 : }
128 :
129 0 : int reuseport = 1;
130 0 : if( FD_UNLIKELY( setsockopt( orig_fd, SOL_SOCKET, SO_REUSEPORT, &reuseport, sizeof(int) )<0 ) ) {
131 0 : FD_LOG_ERR(( "setsockopt(SOL_SOCKET,SO_REUSEPORT,1) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
132 0 : }
133 :
134 0 : int ip_pktinfo = 1;
135 0 : if( FD_UNLIKELY( setsockopt( orig_fd, IPPROTO_IP, IP_PKTINFO, &ip_pktinfo, sizeof(int) )<0 ) ) {
136 0 : FD_LOG_ERR(( "setsockopt(IPPROTO_IP,IP_PKTINFO,1) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
137 0 : }
138 :
139 0 : if( FD_UNLIKELY( 0!=setsockopt( orig_fd, SOL_SOCKET, SO_RCVBUF, &so_rcvbuf, sizeof(int) ) ) ) {
140 0 : FD_LOG_ERR(( "setsockopt(SOL_SOCKET,SO_RCVBUF,%i) failed (%i-%s)", so_rcvbuf, errno, fd_io_strerror( errno ) ));
141 0 : }
142 :
143 0 : struct sockaddr_in saddr = {
144 0 : .sin_family = AF_INET,
145 0 : .sin_addr.s_addr = bind_addr,
146 0 : .sin_port = fd_ushort_bswap( listen_port ),
147 0 : };
148 0 : if( lo_ifindex ) {
149 : /* SKF_AD_IFINDEX identifies the receive device, including local
150 : traffic addressed to a non loopback interface's IP address. */
151 0 : struct sock_filter filter[] = {
152 0 : BPF_STMT( BPF_LD|BPF_W|BPF_ABS, (uint)(SKF_AD_OFF+SKF_AD_IFINDEX) ),
153 0 : BPF_JUMP( BPF_JMP|BPF_JEQ|BPF_K, lo_ifindex, 0, 1 ),
154 0 : BPF_STMT( BPF_RET|BPF_K, UINT_MAX ),
155 0 : BPF_STMT( BPF_RET|BPF_K, 0 )
156 0 : };
157 0 : struct sock_fprog prog = { .len = 4, .filter = filter };
158 0 : if( FD_UNLIKELY( 0!=setsockopt( orig_fd, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog) ) ) ) {
159 0 : FD_LOG_ERR(( "setsockopt(SO_ATTACH_FILTER) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
160 0 : }
161 0 : }
162 0 : if( FD_UNLIKELY( 0!=bind( orig_fd, fd_type_pun_const( &saddr ), sizeof(struct sockaddr_in) ) ) ) {
163 0 : FD_LOG_ERR(( "bind(0.0.0.0:%i) failed (%i-%s)", listen_port, errno, fd_io_strerror( errno ) ));
164 0 : }
165 :
166 0 : if( orig_fd==sock_fd ) {
167 0 : if( FD_UNLIKELY( fcntl( sock_fd, F_SETFD, FD_CLOEXEC ) ) ) {
168 0 : FD_LOG_ERR(( "fcntl(F_SETFD) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
169 0 : }
170 0 : return;
171 0 : }
172 0 : int dup_res = dup3( orig_fd, sock_fd, O_CLOEXEC );
173 0 : if( FD_UNLIKELY( dup_res!=sock_fd ) ) {
174 0 : FD_LOG_ERR(( "dup3 returned %i (%i-%s)", dup_res, errno, fd_io_strerror( errno ) ));
175 0 : }
176 :
177 0 : if( FD_UNLIKELY( 0!=close( orig_fd ) ) ) {
178 0 : FD_LOG_ERR(( "close(%d) failed (%i-%s)", orig_fd, errno, fd_io_strerror( errno ) ));
179 0 : }
180 0 : }
181 :
182 : static void
183 : privileged_init( fd_topo_t const * topo,
184 0 : fd_topo_tile_t const * tile ) {
185 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
186 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
187 0 : fd_sock_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_sock_tile_t), sizeof(fd_sock_tile_t) );
188 0 : struct iovec * batch_iov = FD_SCRATCH_ALLOC_APPEND( l, alignof(struct iovec), STEM_BURST*sizeof(struct iovec) );
189 0 : void * batch_cmsg = FD_SCRATCH_ALLOC_APPEND( l, alignof(struct cmsghdr), STEM_BURST*FD_SOCK_CMSG_MAX );
190 0 : struct sockaddr_in * batch_sa = FD_SCRATCH_ALLOC_APPEND( l, alignof(struct sockaddr_in), STEM_BURST*sizeof(struct sockaddr_in) );
191 0 : struct mmsghdr * batch_msg = FD_SCRATCH_ALLOC_APPEND( l, alignof(struct mmsghdr), STEM_BURST*sizeof(struct mmsghdr) );
192 0 : ulong * batch_fsz = FD_SCRATCH_ALLOC_APPEND( l, alignof(ulong), STEM_BURST*sizeof(ulong) );
193 0 : ulong tx_sz = tile->sock.only_recv_lo ? 0UL : tx_scratch_footprint();
194 0 : uchar * tx_scratch = FD_SCRATCH_ALLOC_APPEND( l, FD_CHUNK_ALIGN, tx_sz );
195 0 : FD_DCHECK_CRIT( scratch==ctx, "invalid layout" );
196 :
197 0 : fd_memset( ctx, 0, sizeof(fd_sock_tile_t) );
198 0 : fd_memset( batch_iov, 0, STEM_BURST*sizeof(struct iovec) );
199 0 : fd_memset( batch_sa, 0, STEM_BURST*sizeof(struct sockaddr_in) );
200 0 : fd_memset( batch_msg, 0, STEM_BURST*sizeof(struct mmsghdr) );
201 :
202 0 : ctx->batch_cnt = 0UL;
203 0 : ctx->batch_iov = batch_iov;
204 0 : ctx->batch_cmsg = batch_cmsg;
205 0 : ctx->batch_sa = batch_sa;
206 0 : ctx->batch_msg = batch_msg;
207 0 : ctx->batch_frame_sz = batch_fsz;
208 0 : ctx->tx_scratch0 = tx_scratch;
209 0 : ctx->tx_scratch1 = tx_scratch + tx_sz;
210 0 : ctx->tx_ptr = tx_scratch;
211 0 : ctx->tx_sock = -1;
212 0 : ctx->bind_address = tile->sock.net.bind_address;
213 0 : ctx->tick_per_ns = fd_tempo_tick_per_ns( NULL );
214 0 : if( tile->sock.only_recv_lo ) {
215 0 : ctx->lo_ifindex = if_nametoindex( "lo" );
216 0 : if( FD_UNLIKELY( !ctx->lo_ifindex ) ) {
217 0 : FD_LOG_ERR(( "if_nametoindex(lo) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
218 0 : }
219 0 : }
220 :
221 : /* Create receive sockets. Incrementally assign them to file
222 : descriptors starting at sock_fd_min. */
223 :
224 0 : int sock_fd_min = RX_SOCK_FD_MIN;
225 0 : fd_net_rx_proto_t protos[ FD_NET_RX_PROTO_CNT ];
226 0 : fd_net_rx_protos_list( protos, &tile->net );
227 0 : for( ulong proto_idx=0UL; proto_idx<FD_NET_RX_PROTO_CNT; proto_idx++ ) {
228 0 : if( !protos[ proto_idx ].listen_port ) continue;
229 0 : uint sock_idx = ctx->sock_cnt;
230 0 : if( sock_idx>=FD_SOCK_TILE_MAX_SOCKETS ) FD_LOG_ERR(( "too many sockets" ));
231 0 : ushort listen_port = protos[ proto_idx ].listen_port;
232 :
233 0 : char const * target_link = protos[ proto_idx ].out_link;
234 0 : ctx->link_rx_map[ sock_idx ] = 0xFF;
235 0 : for( ulong j=0UL; j<(tile->out_cnt); j++ ) {
236 0 : if( 0==strcmp( topo->links[ tile->out_link_id[ j ] ].name, target_link ) ) {
237 0 : ctx->proto_id [ sock_idx ] = (uchar)protos[ proto_idx ].proto_id;
238 0 : ctx->link_rx_map [ sock_idx ] = (uchar)j;
239 0 : ctx->rx_listen_ports[ sock_idx ] = (ushort)listen_port;
240 0 : break;
241 0 : }
242 0 : }
243 0 : if( ctx->link_rx_map[ sock_idx ]==0xFF ) {
244 : /* listen port number has no associated links,
245 : i.e. the repair server is disabled, then no net_rserve link. */
246 0 : continue;
247 0 : }
248 :
249 0 : int sock_fd = sock_fd_min + (int)sock_idx;
250 0 : create_udp_socket( sock_fd, tile->sock.net.bind_address, listen_port, tile->sock.so_rcvbuf, ctx->lo_ifindex );
251 0 : ctx->pollfd[ sock_idx ].fd = sock_fd;
252 0 : ctx->pollfd[ sock_idx ].events = POLLIN;
253 0 : ctx->sock_cnt++;
254 0 : }
255 :
256 0 : if( tile->sock.only_recv_lo ) return;
257 :
258 : /* Create transmit socket */
259 :
260 0 : int tx_sock = socket( AF_INET, SOCK_RAW|SOCK_CLOEXEC, FD_IP4_HDR_PROTOCOL_UDP );
261 0 : if( FD_UNLIKELY( tx_sock<0 ) ) {
262 0 : FD_LOG_ERR(( "socket(AF_INET,SOCK_RAW|SOCK_CLOEXEC,17) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
263 0 : }
264 :
265 0 : if( FD_UNLIKELY( 0!=setsockopt( tx_sock, SOL_SOCKET, SO_SNDBUF, &tile->sock.so_sndbuf, sizeof(int) ) ) ) {
266 0 : FD_LOG_ERR(( "setsockopt(SOL_SOCKET,SO_SNDBUF,%i) failed (%i-%s)", tile->sock.so_sndbuf, errno, fd_io_strerror( errno ) ));
267 0 : }
268 :
269 0 : uchar mcast_ttl = 64;
270 0 : if( FD_UNLIKELY( 0!=setsockopt( tx_sock, IPPROTO_IP, IP_MULTICAST_TTL, &mcast_ttl, sizeof(mcast_ttl) ) ) ) {
271 0 : FD_LOG_ERR(( "setsockopt(IPPROTO_IP,IP_MULTICAST_TTL,%u) failed (%i-%s)", (uint)mcast_ttl, errno, fd_io_strerror( errno ) ));
272 0 : }
273 :
274 0 : ctx->tx_sock = tx_sock;
275 0 : ctx->bind_address = tile->sock.net.bind_address;
276 0 : }
277 :
278 : static void
279 : unprivileged_init( fd_topo_t const * topo,
280 0 : fd_topo_tile_t const * tile ) {
281 0 : fd_sock_tile_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
282 :
283 0 : if( FD_UNLIKELY( tile->out_cnt > MAX_NET_OUTS ) ) {
284 0 : FD_LOG_ERR(( "sock tile has %lu out links which exceeds the max (%lu)", tile->out_cnt, MAX_NET_OUTS ));
285 0 : }
286 :
287 0 : ctx->link_rx_cnt = tile->out_cnt;
288 0 : for( ulong i=0UL; i<(tile->out_cnt); i++ ) {
289 0 : if( 0!=strncmp( topo->links[ tile->out_link_id[ i ] ].name, "net_", 4 ) ) {
290 0 : FD_LOG_ERR(( "out link %lu is not a net RX link", i ));
291 0 : }
292 0 : fd_topo_link_t const * link = &topo->links[ tile->out_link_id[ i ] ];
293 0 : ctx->link_rx[ i ].base = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
294 0 : ctx->link_rx[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->link_rx[ i ].base, link->dcache );
295 0 : ctx->link_rx[ i ].wmark = fd_dcache_compact_wmark( ctx->link_rx[ i ].base, link->dcache, link->mtu );
296 0 : ctx->link_rx[ i ].chunk = ctx->link_rx[ i ].chunk0;
297 0 : if( FD_UNLIKELY( link->burst < STEM_BURST ) ) {
298 0 : FD_LOG_ERR(( "link %lu dcache burst is too low (%lu<%lu)",
299 0 : tile->out_link_id[ i ], link->burst, STEM_BURST ));
300 0 : }
301 0 : }
302 :
303 0 : FD_CHECK_ERR( tile->in_cnt<=MAX_NET_INS, "too many input links" );
304 :
305 : /* The loopback-only sock tile has no TX links and is not counted */
306 0 : ctx->tx_tile_idx = 0UL;
307 0 : ctx->tx_tile_cnt = 0UL;
308 0 : for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
309 0 : fd_topo_tile_t const * t = &topo->tiles[ i ];
310 0 : if( strcmp( t->name, "sock" ) || t->sock.only_recv_lo ) continue;
311 0 : if( t->id==tile->id ) ctx->tx_tile_idx = ctx->tx_tile_cnt;
312 0 : ctx->tx_tile_cnt++;
313 0 : }
314 0 : ctx->tx_tile_cnt = fd_ulong_max( ctx->tx_tile_cnt, 1UL );
315 :
316 0 : for( ulong i=0UL; i<(tile->in_cnt); i++ ) {
317 0 : if( !strstr( topo->links[ tile->in_link_id[ i ] ].name, "_net" ) ) {
318 0 : FD_LOG_ERR(( "in link %lu is not a net TX link", i ));
319 0 : }
320 0 : fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
321 0 : ctx->link_tx[ i ].base = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
322 0 : ctx->link_tx[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->link_tx[ i ].base, link->dcache );
323 0 : ctx->link_tx[ i ].wmark = fd_dcache_compact_wmark( ctx->link_tx[ i ].base, link->dcache, link->mtu );
324 0 : }
325 :
326 0 : }
327 :
328 : /* RX PATH (socket->tango) ********************************************/
329 :
330 : /* FIXME Pace RX polling and interleave it with TX jobs to reduce TX
331 : tail latency */
332 :
333 : /* poll_rx_socket does one recvmmsg batch receive on the given socket
334 : index. Returns the number of packets returned by recvmmsg. */
335 :
336 : static ulong
337 : poll_rx_socket( fd_sock_tile_t * ctx,
338 : fd_stem_context_t * stem,
339 : uint sock_idx,
340 : int sock_fd,
341 0 : ushort proto_id ) {
342 0 : ulong hdr_sz = sizeof(fd_eth_hdr_t) + sizeof(fd_ip4_hdr_t) + sizeof(fd_udp_hdr_t);
343 0 : ulong payload_max = FD_NET_MTU-hdr_sz;
344 0 : uchar rx_link = ctx->link_rx_map[ sock_idx ];
345 0 : ushort listen_port = ctx->rx_listen_ports[ sock_idx ];
346 :
347 0 : fd_sock_link_rx_t * link = ctx->link_rx + rx_link;
348 0 : void * const base = link->base;
349 0 : ulong const chunk0 = link->chunk0;
350 0 : ulong const wmark = link->wmark;
351 0 : ulong chunk_next = link->chunk;
352 0 : uchar * cmsg_next = ctx->batch_cmsg;
353 :
354 0 : for( ulong j=0UL; j<STEM_BURST; j++ ) {
355 0 : ctx->batch_iov[ j ].iov_base = (uchar *)fd_chunk_to_laddr( base, chunk_next ) + hdr_sz;
356 0 : ctx->batch_iov[ j ].iov_len = payload_max;
357 0 : ctx->batch_msg[ j ].msg_hdr = (struct msghdr) {
358 0 : .msg_iov = ctx->batch_iov+j,
359 0 : .msg_iovlen = 1,
360 0 : .msg_name = ctx->batch_sa+j,
361 0 : .msg_namelen = sizeof(struct sockaddr_in),
362 0 : .msg_control = cmsg_next,
363 0 : .msg_controllen = FD_SOCK_CMSG_MAX,
364 0 : };
365 0 : cmsg_next += FD_SOCK_CMSG_MAX;
366 : /* Speculatively prepare all chunk indexes for a receive.
367 : At function exit, chunks into which a packet was received are
368 : committed, all others are freed. */
369 0 : chunk_next = fd_dcache_compact_next( chunk_next, FD_NET_MTU, chunk0, wmark );
370 0 : }
371 :
372 0 : int msg_cnt = recvmmsg( sock_fd, ctx->batch_msg, STEM_BURST, MSG_DONTWAIT, NULL );
373 0 : if( FD_UNLIKELY( msg_cnt<0 ) ) {
374 0 : if( FD_UNLIKELY( errno==EINTR ) ) return 0UL;
375 0 : if( FD_LIKELY( errno==EAGAIN ) ) return 0UL;
376 : /* unreachable if socket is in a valid state */
377 0 : FD_LOG_ERR(( "recvmmsg failed (%i-%s)", errno, fd_io_strerror( errno ) ));
378 0 : }
379 0 : long ts = fd_tickcount();
380 0 : ctx->metrics.sys_recvmmsg_cnt++;
381 :
382 0 : if( FD_UNLIKELY( msg_cnt==0 ) ) return 0UL;
383 :
384 : /* Only frags published to rx_link may consume rx_link dcache space.
385 : Packets that are dropped or redirected leave a hole, so subsequent
386 : packets are compacted down to chunk_pub. */
387 0 : ulong chunk_pub = link->chunk;
388 :
389 0 : for( ulong j=0; j<(ulong)msg_cnt; j++ ) {
390 0 : uchar * payload = ctx->batch_iov[ j ].iov_base;
391 0 : ulong payload_sz = ctx->batch_msg[ j ].msg_len;
392 0 : struct sockaddr_in * sa = ctx->batch_msg[ j ].msg_hdr.msg_name;
393 0 : ulong frame_sz = payload_sz + hdr_sz;
394 0 : if( FD_UNLIKELY( ctx->batch_msg[ j ].msg_hdr.msg_flags & (MSG_TRUNC|MSG_CTRUNC) ) ) continue;
395 0 : ctx->metrics.rx_bytes_total += frame_sz;
396 0 : if( FD_UNLIKELY( sa->sin_family!=AF_INET ) ) {
397 : /* unreachable */
398 0 : FD_LOG_ERR(( "Received packet with unexpected sin_family %i", sa->sin_family ));
399 0 : }
400 :
401 0 : long daddr = -1;
402 0 : struct cmsghdr * cmsg = CMSG_FIRSTHDR( &ctx->batch_msg[ j ].msg_hdr );
403 0 : if( FD_LIKELY( cmsg ) ) {
404 0 : do {
405 0 : if( FD_LIKELY( (cmsg->cmsg_level==IPPROTO_IP) &
406 0 : (cmsg->cmsg_type ==IP_PKTINFO) ) ) {
407 0 : struct in_pktinfo const * pi = (struct in_pktinfo const *)CMSG_DATA( cmsg );
408 0 : daddr = pi->ipi_addr.s_addr;
409 0 : }
410 0 : cmsg = fd_cmsg_nxthdr( &ctx->batch_msg[ j ].msg_hdr, cmsg );
411 0 : } while( FD_UNLIKELY( cmsg ) ); /* optimize for 1 cmsg */
412 0 : }
413 0 : if( FD_UNLIKELY( daddr<0L ) ) {
414 : /* unreachable because IP_PKTINFO was set */
415 0 : FD_LOG_ERR(( "Missing IP_PKTINFO on incoming packet" ));
416 0 : }
417 :
418 0 : fd_eth_hdr_t * eth_hdr = (fd_eth_hdr_t *)( payload-42UL );
419 0 : fd_ip4_hdr_t * ip_hdr = (fd_ip4_hdr_t *)( payload-28UL );
420 0 : fd_udp_hdr_t * udp_hdr = (fd_udp_hdr_t *)( payload- 8UL );
421 0 : memset( eth_hdr->dst, 0, 6 );
422 0 : memset( eth_hdr->src, 0, 6 );
423 0 : eth_hdr->net_type = fd_ushort_bswap( FD_ETH_HDR_TYPE_IP );
424 0 : *ip_hdr = (fd_ip4_hdr_t) {
425 0 : .verihl = FD_IP4_VERIHL( 4, 5 ),
426 0 : .net_tot_len = fd_ushort_bswap( (ushort)( payload_sz+28UL ) ),
427 0 : .ttl = 1,
428 0 : .protocol = FD_IP4_HDR_PROTOCOL_UDP,
429 0 : };
430 0 : uint daddr_ = (uint)(ulong)daddr;
431 0 : memcpy( ip_hdr->saddr_c, &sa->sin_addr.s_addr, 4 );
432 0 : memcpy( ip_hdr->daddr_c, &daddr_, 4 );
433 0 : *udp_hdr = (fd_udp_hdr_t) {
434 0 : .net_sport = sa->sin_port,
435 0 : .net_dport = (ushort)fd_ushort_bswap( (ushort)listen_port ),
436 0 : .net_len = (ushort)fd_ushort_bswap( (ushort)( payload_sz+8UL ) ),
437 0 : .check = 0
438 0 : };
439 :
440 0 : ctx->metrics.rx_pkt_cnt++;
441 0 : ulong chunk = fd_laddr_to_chunk( base, eth_hdr );
442 0 : ulong sig = fd_disco_netmux_sig( sa->sin_addr.s_addr, fd_ushort_bswap( sa->sin_port ), sa->sin_addr.s_addr, proto_id, hdr_sz );
443 0 : ulong tspub = fd_frag_meta_ts_comp( ts );
444 :
445 0 : if( FD_UNLIKELY( chunk!=chunk_pub ) ) {
446 0 : memcpy( fd_chunk_to_laddr( base, chunk_pub ), eth_hdr, frame_sz );
447 0 : }
448 0 : fd_stem_publish( stem, rx_link, sig, chunk_pub, frame_sz, 0UL, 0UL, tspub );
449 0 : chunk_pub = fd_dcache_compact_next( chunk_pub, FD_NET_MTU, chunk0, wmark );
450 0 : }
451 :
452 : /* Rewind the chunk index to the first free index. */
453 0 : link->chunk = chunk_pub;
454 0 : return (ulong)msg_cnt;
455 0 : }
456 :
457 : static ulong
458 : poll_rx( fd_sock_tile_t * ctx,
459 0 : fd_stem_context_t * stem ) {
460 0 : ulong pkt_cnt = 0UL;
461 0 : if( FD_UNLIKELY( ctx->batch_cnt ) ) {
462 0 : FD_LOG_ERR(( "Batch is not clean" ));
463 0 : }
464 0 : ctx->tx_idle_cnt = 0; /* restart TX polling */
465 0 : long wait_ns = ctx->lo_ifindex ? (long)((double)fd_long_max( stem->then-fd_tickcount(), 0L )/ctx->tick_per_ns) : 0L;
466 0 : struct timespec ts = { .tv_sec = wait_ns/1000000000L, .tv_nsec = wait_ns%1000000000L };
467 0 : if( FD_UNLIKELY( ppoll( ctx->pollfd, ctx->sock_cnt, &ts, NULL )<0 ) ) {
468 0 : if( FD_UNLIKELY( errno==EINTR ) ) return 0UL;
469 0 : FD_LOG_ERR(( "ppoll failed (%i-%s)", errno, fd_io_strerror( errno ) ));
470 0 : }
471 0 : for( uint j=0U; j<ctx->sock_cnt; j++ ) {
472 0 : if( ctx->pollfd[ j ].revents & (POLLIN|POLLERR) ) {
473 0 : pkt_cnt += poll_rx_socket(
474 0 : ctx,
475 0 : stem,
476 0 : j,
477 0 : ctx->pollfd[ j ].fd,
478 0 : ctx->proto_id[ j ]
479 0 : );
480 0 : }
481 0 : ctx->pollfd[ j ].revents = 0;
482 0 : }
483 0 : return pkt_cnt;
484 0 : }
485 :
486 : /* TX PATH (tango->socket) ********************************************/
487 :
488 : static void
489 0 : flush_tx_batch( fd_sock_tile_t * ctx ) {
490 0 : ulong batch_cnt = ctx->batch_cnt;
491 0 : for( int j = 0; j < (int)batch_cnt; /* incremented in loop */ ) {
492 0 : int remain = (int)batch_cnt - j;
493 0 : int send_cnt = sendmmsg( ctx->tx_sock, ctx->batch_msg + j, (uint)remain, MSG_DONTWAIT );
494 0 : if( send_cnt>=0 ) {
495 0 : ctx->metrics.sys_sendmmsg_cnt[ FD_METRICS_ENUM_SENDMMSG_RESULT_V_SUCCESS_IDX ]++;
496 0 : }
497 0 : if( FD_UNLIKELY( send_cnt < remain ) ) {
498 0 : ctx->metrics.tx_drop_cnt++;
499 0 : if( FD_UNLIKELY( send_cnt < 0 ) ) {
500 0 : switch( errno ) {
501 0 : case EAGAIN:
502 0 : case ENOBUFS:
503 0 : ctx->metrics.sys_sendmmsg_cnt[ FD_METRICS_ENUM_SENDMMSG_RESULT_V_BUFFER_FULL_IDX ]++;
504 0 : break;
505 0 : case EPERM:
506 0 : ctx->metrics.sys_sendmmsg_cnt[ FD_METRICS_ENUM_SENDMMSG_RESULT_V_PERMISSION_IDX ]++;
507 0 : break;
508 0 : case ENETUNREACH:
509 0 : case EHOSTUNREACH:
510 0 : ctx->metrics.sys_sendmmsg_cnt[ FD_METRICS_ENUM_SENDMMSG_RESULT_V_UNREACHABLE_IDX ]++;
511 0 : break;
512 0 : case ENONET:
513 0 : case ENETDOWN:
514 0 : case EHOSTDOWN:
515 0 : ctx->metrics.sys_sendmmsg_cnt[ FD_METRICS_ENUM_SENDMMSG_RESULT_V_DOWN_IDX ]++;
516 0 : break;
517 0 : default:
518 0 : ctx->metrics.sys_sendmmsg_cnt[ FD_METRICS_ENUM_SENDMMSG_RESULT_V_OTHER_IDX ]++;
519 : /* log with NOTICE, since flushing has a significant negative performance impact */
520 0 : FD_LOG_NOTICE(( "sendmmsg failed (%i-%s)", errno, fd_io_strerror( errno ) ));
521 0 : }
522 :
523 : /* first message failed, so skip failing message and continue */
524 0 : j++;
525 0 : } else {
526 : /* send_cnt succeeded, so skip those and also the failing message */
527 0 : j += send_cnt + 1;
528 :
529 : /* add the successful count */
530 0 : ctx->metrics.tx_pkt_cnt += (ulong)send_cnt;
531 0 : for( int k=j-send_cnt-1; k<j-1; k++ ) ctx->metrics.tx_bytes_total += ctx->batch_frame_sz[ k ];
532 0 : }
533 :
534 0 : continue;
535 0 : }
536 :
537 : /* send_cnt == batch_cnt, so we sent everything */
538 0 : ctx->metrics.tx_pkt_cnt += (ulong)send_cnt;
539 0 : for( int k=j; k<j+send_cnt; k++ ) ctx->metrics.tx_bytes_total += ctx->batch_frame_sz[ k ];
540 0 : break;
541 0 : }
542 :
543 0 : ctx->tx_ptr = ctx->tx_scratch0;
544 0 : ctx->batch_cnt = 0;
545 0 : }
546 :
547 : /* before_frag is called when a new frag has been detected. Every sock
548 : tile reads every TX link, so each sends only its share of packets. */
549 :
550 : static inline int
551 : before_frag( fd_sock_tile_t * ctx,
552 : ulong in_idx FD_PARAM_UNUSED,
553 : ulong seq FD_PARAM_UNUSED,
554 0 : ulong sig ) {
555 0 : ulong proto_id = fd_disco_netmux_sig_proto( sig );
556 0 : if( FD_UNLIKELY( proto_id!=DST_PROTO_OUTGOING ) ) return 1;
557 0 : if( fd_disco_netmux_sig_hash( sig )%ctx->tx_tile_cnt!=ctx->tx_tile_idx ) return 1;
558 0 : return 0; /* continue */
559 0 : }
560 :
561 : /* during_frag is called when a new frag passed early filtering.
562 : Speculatively copies data into a sendmmsg buffer. (If all tiles
563 : respect backpressure could eliminate this copy) */
564 :
565 : static inline void
566 : during_frag( fd_sock_tile_t * ctx,
567 : ulong in_idx,
568 : ulong seq FD_PARAM_UNUSED,
569 : ulong sig,
570 : ulong chunk,
571 : ulong sz,
572 0 : ulong ctl FD_PARAM_UNUSED ) {
573 0 : if( FD_UNLIKELY( chunk<ctx->link_tx[ in_idx ].chunk0 || chunk>ctx->link_tx[ in_idx ].wmark || sz>FD_NET_MTU ) ) {
574 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->link_tx[ in_idx ].chunk0, ctx->link_tx[ in_idx ].wmark ));
575 0 : }
576 :
577 0 : ctx->parsed.invalid = 0;
578 :
579 0 : ulong const hdr_min = sizeof(fd_eth_hdr_t)+sizeof(fd_ip4_hdr_t)+sizeof(fd_udp_hdr_t);
580 0 : if( FD_UNLIKELY( sz<hdr_min ) ) {
581 : /* FIXME support ICMP messages in the future?
582 : Defer the error to after_frag, where we know we weren't
583 : overrun. */
584 0 : ctx->parsed.invalid = 1;
585 0 : return;
586 0 : }
587 :
588 0 : uchar const * frame = fd_chunk_to_laddr_const( ctx->link_tx[ in_idx ].base, chunk );
589 0 : ulong hdr_sz = fd_disco_netmux_sig_hdr_sz( sig );
590 0 : uchar const * payload = frame+hdr_sz;
591 0 : if( FD_UNLIKELY( hdr_sz>sz || hdr_sz<hdr_min ) ) {
592 0 : FD_LOG_ERR(( "packet from in_idx=%lu corrupt: hdr_sz=%lu total_sz=%lu",
593 0 : in_idx, hdr_sz, sz ));
594 0 : }
595 0 : ulong payload_sz = sz-hdr_sz;
596 :
597 0 : fd_ip4_hdr_t const * ip_hdr = (fd_ip4_hdr_t const *)( frame +sizeof(fd_eth_hdr_t) );
598 0 : fd_udp_hdr_t const * udp_hdr = (fd_udp_hdr_t const *)( payload-sizeof(fd_udp_hdr_t) );
599 0 : ctx->parsed.ip_version = FD_IP4_GET_VERSION( *ip_hdr );
600 0 : ctx->parsed.ip_protocol = ip_hdr->protocol;
601 0 : if( FD_UNLIKELY( ( ctx->parsed.ip_version !=4 ) |
602 0 : ( ctx->parsed.ip_protocol!=FD_IP4_HDR_PROTOCOL_UDP ) ) ) {
603 : /* sock tile only supports IPv4 UDP for now. Defer the error to
604 : after_frag, where we know we weren't overrun. */
605 0 : ctx->parsed.invalid = 1;
606 0 : return;
607 0 : }
608 :
609 0 : ulong msg_sz = sizeof(fd_udp_hdr_t) + payload_sz;
610 :
611 0 : ulong batch_idx = ctx->batch_cnt;
612 0 : FD_DCHECK_CRIT( batch_idx<STEM_BURST, "flow control error" );
613 0 : struct mmsghdr * msg = ctx->batch_msg + batch_idx;
614 0 : struct sockaddr_in * sa = ctx->batch_sa + batch_idx;
615 0 : struct iovec * iov = ctx->batch_iov + batch_idx;
616 0 : struct cmsghdr * cmsg = (void *)( (ulong)ctx->batch_cmsg + batch_idx*FD_SOCK_CMSG_MAX );
617 0 : uchar * buf = ctx->tx_ptr;
618 :
619 0 : *iov = (struct iovec) {
620 0 : .iov_base = buf,
621 0 : .iov_len = msg_sz,
622 0 : };
623 0 : sa->sin_family = AF_INET;
624 0 : sa->sin_addr.s_addr = FD_LOAD( uint, ip_hdr->daddr_c );
625 0 : sa->sin_port = 0; /* ignored */
626 :
627 0 : cmsg->cmsg_level = IPPROTO_IP;
628 0 : cmsg->cmsg_type = IP_PKTINFO;
629 0 : cmsg->cmsg_len = CMSG_LEN( sizeof(struct in_pktinfo) );
630 0 : struct in_pktinfo * pi = (struct in_pktinfo *)CMSG_DATA( cmsg );
631 0 : pi->ipi_ifindex = 0;
632 0 : pi->ipi_addr.s_addr = 0;
633 0 : pi->ipi_spec_dst.s_addr = fd_uint_if( !!ip_hdr->saddr, ip_hdr->saddr, ctx->bind_address );
634 :
635 0 : *msg = (struct mmsghdr) {
636 0 : .msg_hdr = {
637 0 : .msg_name = sa,
638 0 : .msg_namelen = sizeof(struct sockaddr_in),
639 0 : .msg_iov = iov,
640 0 : .msg_iovlen = 1,
641 0 : .msg_control = cmsg,
642 0 : .msg_controllen = CMSG_LEN( sizeof(struct in_pktinfo) )
643 0 : }
644 0 : };
645 :
646 0 : memcpy( buf, udp_hdr, sizeof(fd_udp_hdr_t) );
647 0 : fd_memcpy( buf+sizeof(fd_udp_hdr_t), payload, payload_sz );
648 0 : ctx->batch_frame_sz[ batch_idx ] = sz;
649 0 : }
650 :
651 : /* after_frag is called when a frag was copied into a sendmmsg buffer. */
652 :
653 : static void
654 : after_frag( fd_sock_tile_t * ctx,
655 : ulong in_idx,
656 : ulong seq FD_PARAM_UNUSED,
657 : ulong sig FD_PARAM_UNUSED,
658 : ulong sz,
659 : ulong tsorig FD_PARAM_UNUSED,
660 : ulong tspub FD_PARAM_UNUSED,
661 0 : fd_stem_context_t * stem FD_PARAM_UNUSED ) {
662 : /* Commit the packet added in during_frag. during_frag defers fatal
663 : errors to here (where we know we weren't overrun) by setting the
664 : invalid flag. */
665 :
666 0 : if( FD_UNLIKELY( ctx->parsed.invalid ) ) {
667 0 : ulong const hdr_min = sizeof(fd_eth_hdr_t)+sizeof(fd_ip4_hdr_t)+sizeof(fd_udp_hdr_t);
668 0 : if( FD_UNLIKELY( sz<hdr_min ) ) {
669 : /* FIXME support ICMP messages in the future? */
670 0 : FD_LOG_ERR(( "packet too small %lu (in_idx=%lu)", sz, in_idx ));
671 0 : }
672 0 : if( FD_UNLIKELY( ( ctx->parsed.ip_version !=4 ) |
673 0 : ( ctx->parsed.ip_protocol!=FD_IP4_HDR_PROTOCOL_UDP ) ) ) {
674 0 : FD_LOG_ERR(( "packet from in_idx=%lu: sock tile only supports IPv4 UDP for now", in_idx ));
675 0 : }
676 0 : }
677 :
678 0 : ctx->tx_idle_cnt = 0;
679 0 : ctx->batch_cnt++;
680 : /* Technically leaves a gap. sz is always larger than the payload
681 : written to tx_ptr because Ethernet & IPv4 headers were stripped. */
682 0 : ctx->tx_ptr += fd_ulong_align_up( sz, FD_CHUNK_ALIGN );
683 :
684 0 : if( ctx->batch_cnt >= STEM_BURST ) {
685 0 : flush_tx_batch( ctx );
686 0 : }
687 0 : }
688 :
689 : /* End TX path ********************************************************/
690 :
691 : static void
692 0 : metrics_write( fd_sock_tile_t * ctx ) {
693 0 : FD_MCNT_SET( SOCK, SYSCALL_RX, ctx->metrics.sys_recvmmsg_cnt );
694 0 : FD_MCNT_ENUM_COPY( SOCK, SYSCALL_TX, ctx->metrics.sys_sendmmsg_cnt );
695 0 : FD_MCNT_SET( SOCK, PKT_RX, ctx->metrics.rx_pkt_cnt );
696 0 : FD_MCNT_SET( SOCK, PKT_TX, ctx->metrics.tx_pkt_cnt );
697 0 : FD_MCNT_SET( SOCK, PKT_TX_FAILED, ctx->metrics.tx_drop_cnt );
698 0 : FD_MCNT_SET( SOCK, PKT_TX_BYTES, ctx->metrics.tx_bytes_total );
699 0 : FD_MCNT_SET( SOCK, PKT_RX_BYTES, ctx->metrics.rx_bytes_total );
700 0 : }
701 :
702 : /* after_credit is called every stem iteration when there are enough
703 : flow control credits to publish a burst of fragments. */
704 :
705 : static inline void
706 : after_credit( fd_sock_tile_t * ctx,
707 : fd_stem_context_t * stem,
708 : int * poll_in FD_PARAM_UNUSED,
709 0 : int * charge_busy ) {
710 0 : if( ctx->lo_ifindex ) {
711 : /* Housekeeping cannot publish progress while the tile sleeps. */
712 0 : FD_COMPILER_MFENCE();
713 0 : metrics_write( ctx );
714 0 : FD_MGAUGE_SET( TILE, HEARTBEAT_TIME, (ulong)fd_log_wallclock() );
715 0 : for( ulong i=0UL; i<ctx->link_rx_cnt; i++ ) {
716 0 : fd_mcache_seq_update( fd_mcache_seq_laddr( stem->mcaches[ i ] ), stem->seqs[ i ] );
717 0 : }
718 0 : FD_COMPILER_MFENCE();
719 0 : *charge_busy |= poll_rx( ctx, stem )!=0UL;
720 0 : return;
721 0 : }
722 0 : if( ctx->tx_idle_cnt > 512 ) {
723 0 : if( ctx->batch_cnt ) {
724 0 : flush_tx_batch( ctx );
725 0 : }
726 0 : ulong pkt_cnt = poll_rx( ctx, stem );
727 0 : *charge_busy = pkt_cnt!=0;
728 0 : }
729 0 : ctx->tx_idle_cnt++;
730 0 : }
731 :
732 : static ulong
733 : rlimit_file_cnt( fd_topo_t const * topo,
734 0 : fd_topo_tile_t const * tile ) {
735 0 : fd_sock_tile_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
736 0 : return RX_SOCK_FD_MIN + ctx->sock_cnt;
737 0 : }
738 :
739 : #define STEM_NEVER_PARK 1
740 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_sock_tile_t
741 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_sock_tile_t)
742 :
743 0 : #define STEM_LAZY ((long)10e6) /* 10ms */
744 :
745 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
746 0 : #define STEM_CALLBACK_AFTER_CREDIT after_credit
747 0 : #define STEM_CALLBACK_BEFORE_FRAG before_frag
748 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
749 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
750 :
751 : #include "../../stem/fd_stem.c"
752 :
753 : fd_topo_run_tile_t fd_tile_sock = {
754 : .name = "sock",
755 : .rlimit_file_cnt_fn = rlimit_file_cnt,
756 : .populate_allowed_seccomp = populate_allowed_seccomp,
757 : .populate_allowed_fds = populate_allowed_fds,
758 : .scratch_align = scratch_align,
759 : .scratch_footprint = scratch_footprint,
760 : .privileged_init = privileged_init,
761 : .unprivileged_init = unprivileged_init,
762 : .run = stem_run,
763 : };
|