LCOV - code coverage report
Current view: top level - disco/net/sock - fd_sock_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 561 0.0 %
Date: 2026-10-22 18:10:43 Functions: 0 18 0.0 %

          Line data    Source code
       1             : #define _GNU_SOURCE /* dup3 */
       2             : #include "fd_sock_tile_private.h"
       3             : #include "../../fd_disco_base.h"
       4             : #include "../../../discof/repair/fd_repair.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 udp_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( udp_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)", udp_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->repair_shred_sock_idx = UINT_MAX;
     212           0 :   ctx->tx_sock = -1;
     213           0 :   ctx->bind_address = tile->sock.net.bind_address;
     214           0 :   ctx->tick_per_ns  = fd_tempo_tick_per_ns( NULL );
     215           0 :   if( tile->sock.only_recv_lo ) {
     216           0 :     ctx->lo_ifindex = if_nametoindex( "lo" );
     217           0 :     if( FD_UNLIKELY( !ctx->lo_ifindex ) ) {
     218           0 :       FD_LOG_ERR(( "if_nametoindex(lo) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     219           0 :     }
     220           0 :   }
     221             : 
     222             :   /* Create receive sockets.  Incrementally assign them to file
     223             :      descriptors starting at sock_fd_min. */
     224             : 
     225           0 :   int sock_fd_min = RX_SOCK_FD_MIN;
     226           0 :   ushort udp_port_candidates[] = {
     227           0 :     (ushort)tile->sock.net.legacy_transaction_listen_port,
     228           0 :     (ushort)tile->sock.net.quic_transaction_listen_port,
     229           0 :     (ushort)tile->sock.net.shred_listen_port,
     230           0 :     (ushort)tile->sock.net.gossip_listen_port,
     231           0 :     (ushort)tile->sock.net.repair_client_listen_port,
     232           0 :     (ushort)tile->sock.net.repair_serve_listen_port,
     233           0 :     (ushort)tile->sock.net.votor_quic_client_listen_port,
     234           0 :     (ushort)tile->sock.net.votor_quic_server_listen_port
     235           0 :   };
     236           0 :   static char const * udp_port_links[] = {
     237           0 :     "net_quic",   /* legacy_transaction_listen_port */
     238           0 :     "net_quic",   /* quic_transaction_listen_port */
     239           0 :     "net_shred",  /* shred_listen_port (turbine) */
     240           0 :     "net_gossvf", /* gossip_listen_port */
     241           0 :     "net_shred",  /* shred_listen_port (repair) */
     242           0 :     "net_rserve", /* repair_serve_listen_port */
     243           0 :     "net_votor",  /* votor_quic_client_listen_port */
     244           0 :     "net_votor"   /* votor_quic_server_listen_port */
     245           0 :   };
     246           0 :   static uchar const udp_port_protos[] = {
     247           0 :     DST_PROTO_TPU_UDP,  /* legacy_transaction_listen_port */
     248           0 :     DST_PROTO_TPU_QUIC, /* quic_transaction_listen_port */
     249           0 :     DST_PROTO_SHRED,    /* shred_listen_port (turbine) */
     250           0 :     DST_PROTO_GOSSIP,   /* gossip_listen_port */
     251           0 :     DST_PROTO_REPAIR,   /* shred_listen_port (repair) */
     252           0 :     DST_PROTO_RSERVE,   /* repair_serve_listen_port */
     253           0 :     DST_PROTO_VOTOR,    /* votor_quic_client_listen_port */
     254             :     DST_PROTO_VOTOR     /* votor_quic_server_listen_port */
     255           0 :   };
     256           0 :   for( uint candidate_idx=0U; candidate_idx<sizeof(udp_port_candidates)/sizeof(udp_port_candidates[0]); candidate_idx++ ) {
     257           0 :     if( !udp_port_candidates[ candidate_idx ] ) continue;
     258           0 :     uint sock_idx = ctx->sock_cnt;
     259           0 :     if( sock_idx>=FD_SOCK_TILE_MAX_SOCKETS ) FD_LOG_ERR(( "too many sockets" ));
     260           0 :     ushort port = (ushort)udp_port_candidates[ candidate_idx ];
     261             : 
     262           0 :     char const * target_link = udp_port_links[ candidate_idx ];
     263           0 :     ctx->link_rx_map[ sock_idx ] = 0xFF;
     264           0 :     for( ulong j=0UL; j<(tile->out_cnt); j++ ) {
     265           0 :       if( 0==strcmp( topo->links[ tile->out_link_id[ j ] ].name, target_link ) ) {
     266           0 :         ctx->proto_id    [ sock_idx ] = (uchar)udp_port_protos[ candidate_idx ];
     267           0 :         ctx->link_rx_map [ sock_idx ] = (uchar)j;
     268           0 :         ctx->rx_sock_port[ sock_idx ] = (ushort)port;
     269           0 :         break;
     270           0 :       }
     271           0 :     }
     272           0 :     if( ctx->link_rx_map[ sock_idx ]==0xFF ) {
     273             :       /* listen port number has no associated links,
     274             :          i.e. the repair server is disabled, then no net_rserve link. */
     275           0 :       continue;
     276           0 :     }
     277             : 
     278             :     /* Record the socket index of the repair intake socket, so repair
     279             :        ping packets can be routed to the repair tile at runtime. */
     280           0 :     if( tile->sock.net.repair_client_listen_port &&
     281           0 :         udp_port_candidates[ candidate_idx ]==tile->sock.net.repair_client_listen_port )
     282           0 :       ctx->repair_shred_sock_idx = sock_idx;
     283             : 
     284           0 :     int sock_fd = sock_fd_min + (int)sock_idx;
     285           0 :     create_udp_socket( sock_fd, tile->sock.net.bind_address, port, tile->sock.so_rcvbuf, ctx->lo_ifindex );
     286           0 :     ctx->pollfd[ sock_idx ].fd     = sock_fd;
     287           0 :     ctx->pollfd[ sock_idx ].events = POLLIN;
     288           0 :     ctx->sock_cnt++;
     289           0 :   }
     290             : 
     291           0 :   if( tile->sock.only_recv_lo ) return;
     292             : 
     293             :   /* Create transmit socket */
     294             : 
     295           0 :   int tx_sock = socket( AF_INET, SOCK_RAW|SOCK_CLOEXEC, FD_IP4_HDR_PROTOCOL_UDP );
     296           0 :   if( FD_UNLIKELY( tx_sock<0 ) ) {
     297           0 :     FD_LOG_ERR(( "socket(AF_INET,SOCK_RAW|SOCK_CLOEXEC,17) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     298           0 :   }
     299             : 
     300           0 :   if( FD_UNLIKELY( 0!=setsockopt( tx_sock, SOL_SOCKET, SO_SNDBUF, &tile->sock.so_sndbuf, sizeof(int) ) ) ) {
     301           0 :     FD_LOG_ERR(( "setsockopt(SOL_SOCKET,SO_SNDBUF,%i) failed (%i-%s)", tile->sock.so_sndbuf, errno, fd_io_strerror( errno ) ));
     302           0 :   }
     303             : 
     304           0 :   uchar mcast_ttl = 64;
     305           0 :   if( FD_UNLIKELY( 0!=setsockopt( tx_sock, IPPROTO_IP, IP_MULTICAST_TTL, &mcast_ttl, sizeof(mcast_ttl) ) ) ) {
     306           0 :     FD_LOG_ERR(( "setsockopt(IPPROTO_IP,IP_MULTICAST_TTL,%u) failed (%i-%s)", (uint)mcast_ttl, errno, fd_io_strerror( errno ) ));
     307           0 :   }
     308             : 
     309           0 :   ctx->tx_sock      = tx_sock;
     310           0 :   ctx->bind_address = tile->sock.net.bind_address;
     311           0 : }
     312             : 
     313             : static void
     314             : unprivileged_init( fd_topo_t const *      topo,
     315           0 :                    fd_topo_tile_t const * tile ) {
     316           0 :   fd_sock_tile_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     317             : 
     318           0 :   if( FD_UNLIKELY( tile->out_cnt > MAX_NET_OUTS ) ) {
     319           0 :     FD_LOG_ERR(( "sock tile has %lu out links which exceeds the max (%lu)", tile->out_cnt, MAX_NET_OUTS ));
     320           0 :   }
     321             : 
     322           0 :   ctx->link_rx_cnt = tile->out_cnt;
     323           0 :   ctx->repair_rx = 0xFF;
     324           0 :   for( ulong i=0UL; i<(tile->out_cnt); i++ ) {
     325           0 :     if( 0!=strncmp( topo->links[ tile->out_link_id[ i ] ].name, "net_", 4 ) ) {
     326           0 :       FD_LOG_ERR(( "out link %lu is not a net RX link", i ));
     327           0 :     }
     328           0 :     if( 0==strcmp( topo->links[ tile->out_link_id[ i ] ].name, "net_repair" ) ) {
     329           0 :       if( FD_UNLIKELY( ctx->repair_rx!=0xFF ) ) FD_LOG_ERR(( "multiple net_repair out links" ));
     330           0 :       ctx->repair_rx = (uchar)i;
     331           0 :     }
     332           0 :     fd_topo_link_t const * link = &topo->links[ tile->out_link_id[ i ] ];
     333           0 :     ctx->link_rx[ i ].base   = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
     334           0 :     ctx->link_rx[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->link_rx[ i ].base, link->dcache );
     335           0 :     ctx->link_rx[ i ].wmark  = fd_dcache_compact_wmark(  ctx->link_rx[ i ].base, link->dcache, link->mtu );
     336           0 :     ctx->link_rx[ i ].chunk  = ctx->link_rx[ i ].chunk0;
     337           0 :     if( FD_UNLIKELY( link->burst < STEM_BURST ) ) {
     338           0 :       FD_LOG_ERR(( "link %lu dcache burst is too low (%lu<%lu)",
     339           0 :                    tile->out_link_id[ i ], link->burst, STEM_BURST ));
     340           0 :     }
     341           0 :   }
     342             : 
     343           0 :   if( FD_UNLIKELY( ctx->repair_shred_sock_idx!=UINT_MAX && ctx->repair_rx==0xFF ) ) {
     344           0 :     FD_LOG_ERR(( "repair intake socket configured but no net_repair out link was found" ));
     345           0 :   }
     346             : 
     347           0 :   FD_CHECK_ERR( tile->in_cnt<=MAX_NET_INS, "too many input links" );
     348             : 
     349             :   /* The loopback-only sock tile has no TX links and is not counted */
     350           0 :   ctx->tx_tile_idx = 0UL;
     351           0 :   ctx->tx_tile_cnt = 0UL;
     352           0 :   for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
     353           0 :     fd_topo_tile_t const * t = &topo->tiles[ i ];
     354           0 :     if( strcmp( t->name, "sock" ) || t->sock.only_recv_lo ) continue;
     355           0 :     if( t->id==tile->id ) ctx->tx_tile_idx = ctx->tx_tile_cnt;
     356           0 :     ctx->tx_tile_cnt++;
     357           0 :   }
     358           0 :   ctx->tx_tile_cnt = fd_ulong_max( ctx->tx_tile_cnt, 1UL );
     359             : 
     360           0 :   for( ulong i=0UL; i<(tile->in_cnt); i++ ) {
     361           0 :     if( !strstr( topo->links[ tile->in_link_id[ i ] ].name, "_net" ) ) {
     362           0 :       FD_LOG_ERR(( "in link %lu is not a net TX link", i ));
     363           0 :     }
     364           0 :     fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
     365           0 :     ctx->link_tx[ i ].base   = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
     366           0 :     ctx->link_tx[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->link_tx[ i ].base, link->dcache );
     367           0 :     ctx->link_tx[ i ].wmark  = fd_dcache_compact_wmark(  ctx->link_tx[ i ].base, link->dcache, link->mtu );
     368           0 :   }
     369             : 
     370           0 : }
     371             : 
     372             : /* RX PATH (socket->tango) ********************************************/
     373             : 
     374             : /* FIXME Pace RX polling and interleave it with TX jobs to reduce TX
     375             :          tail latency */
     376             : 
     377             : /* poll_rx_socket does one recvmmsg batch receive on the given socket
     378             :    index.  Returns the number of packets returned by recvmmsg. */
     379             : 
     380             : static ulong
     381             : poll_rx_socket( fd_sock_tile_t *    ctx,
     382             :                 fd_stem_context_t * stem,
     383             :                 uint                sock_idx,
     384             :                 int                 sock_fd,
     385           0 :                 ushort              proto ) {
     386           0 :   ulong  hdr_sz      = sizeof(fd_eth_hdr_t) + sizeof(fd_ip4_hdr_t) + sizeof(fd_udp_hdr_t);
     387           0 :   ulong  payload_max = FD_NET_MTU-hdr_sz;
     388           0 :   uchar  rx_link     = ctx->link_rx_map[ sock_idx ];
     389           0 :   ushort dport       = ctx->rx_sock_port[ sock_idx ];
     390             : 
     391           0 :   fd_sock_link_rx_t * link = ctx->link_rx + rx_link;
     392           0 :   void * const base       = link->base;
     393           0 :   ulong  const chunk0     = link->chunk0;
     394           0 :   ulong  const wmark      = link->wmark;
     395           0 :   ulong        chunk_next = link->chunk;
     396           0 :   uchar *      cmsg_next  = ctx->batch_cmsg;
     397             : 
     398           0 :   for( ulong j=0UL; j<STEM_BURST; j++ ) {
     399           0 :     ctx->batch_iov[ j ].iov_base = (uchar *)fd_chunk_to_laddr( base, chunk_next ) + hdr_sz;
     400           0 :     ctx->batch_iov[ j ].iov_len  = payload_max;
     401           0 :     ctx->batch_msg[ j ].msg_hdr  = (struct msghdr) {
     402           0 :       .msg_iov        = ctx->batch_iov+j,
     403           0 :       .msg_iovlen     = 1,
     404           0 :       .msg_name       = ctx->batch_sa+j,
     405           0 :       .msg_namelen    = sizeof(struct sockaddr_in),
     406           0 :       .msg_control    = cmsg_next,
     407           0 :       .msg_controllen = FD_SOCK_CMSG_MAX,
     408           0 :     };
     409           0 :     cmsg_next += FD_SOCK_CMSG_MAX;
     410             :     /* Speculatively prepare all chunk indexes for a receive.
     411             :        At function exit, chunks into which a packet was received are
     412             :        committed, all others are freed. */
     413           0 :     chunk_next = fd_dcache_compact_next( chunk_next, FD_NET_MTU, chunk0, wmark );
     414           0 :   }
     415             : 
     416           0 :   int msg_cnt = recvmmsg( sock_fd, ctx->batch_msg, STEM_BURST, MSG_DONTWAIT, NULL );
     417           0 :   if( FD_UNLIKELY( msg_cnt<0 ) ) {
     418           0 :     if( FD_UNLIKELY( errno==EINTR ) ) return 0UL;
     419           0 :     if( FD_LIKELY( errno==EAGAIN ) ) return 0UL;
     420             :     /* unreachable if socket is in a valid state */
     421           0 :     FD_LOG_ERR(( "recvmmsg failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     422           0 :   }
     423           0 :   long ts = fd_tickcount();
     424           0 :   ctx->metrics.sys_recvmmsg_cnt++;
     425             : 
     426           0 :   if( FD_UNLIKELY( msg_cnt==0 ) ) return 0UL;
     427             : 
     428             :   /* Track the chunk index of the last frag populated, so we can derive
     429             :      the chunk indexes for the next poll_rx_socket call.
     430             :      Guaranteed to be set since msg_cnt>0. */
     431           0 :   ulong last_chunk;
     432             : 
     433           0 :   for( ulong j=0; j<(ulong)msg_cnt; j++ ) {
     434           0 :     uchar * payload         = ctx->batch_iov[ j ].iov_base;
     435           0 :     ulong   payload_sz      = ctx->batch_msg[ j ].msg_len;
     436           0 :     struct sockaddr_in * sa = ctx->batch_msg[ j ].msg_hdr.msg_name;
     437           0 :     ulong frame_sz          = payload_sz + hdr_sz;
     438           0 :     last_chunk = fd_laddr_to_chunk( base, payload-hdr_sz );
     439           0 :     if( FD_UNLIKELY( ctx->lo_ifindex &&
     440           0 :                      (ctx->batch_msg[ j ].msg_hdr.msg_flags & (MSG_TRUNC|MSG_CTRUNC)) ) ) continue;
     441           0 :     ctx->metrics.rx_bytes_total += frame_sz;
     442           0 :     if( FD_UNLIKELY( sa->sin_family!=AF_INET ) ) {
     443             :       /* unreachable */
     444           0 :       FD_LOG_ERR(( "Received packet with unexpected sin_family %i", sa->sin_family ));
     445           0 :     }
     446             : 
     447           0 :     long daddr = -1;
     448           0 :     struct cmsghdr * cmsg = CMSG_FIRSTHDR( &ctx->batch_msg[ j ].msg_hdr );
     449           0 :     if( FD_LIKELY( cmsg ) ) {
     450           0 :       do {
     451           0 :         if( FD_LIKELY( (cmsg->cmsg_level==IPPROTO_IP) &
     452           0 :                        (cmsg->cmsg_type ==IP_PKTINFO) ) ) {
     453           0 :           struct in_pktinfo const * pi = (struct in_pktinfo const *)CMSG_DATA( cmsg );
     454           0 :           daddr = pi->ipi_addr.s_addr;
     455           0 :         }
     456           0 :         cmsg = fd_cmsg_nxthdr( &ctx->batch_msg[ j ].msg_hdr, cmsg );
     457           0 :       } while( FD_UNLIKELY( cmsg ) ); /* optimize for 1 cmsg */
     458           0 :     }
     459           0 :     if( FD_UNLIKELY( daddr<0L ) ) {
     460             :       /* unreachable because IP_PKTINFO was set */
     461           0 :       FD_LOG_ERR(( "Missing IP_PKTINFO on incoming packet" ));
     462           0 :     }
     463             : 
     464           0 :     fd_eth_hdr_t * eth_hdr    = (fd_eth_hdr_t *)( payload-42UL );
     465           0 :     fd_ip4_hdr_t * ip_hdr     = (fd_ip4_hdr_t *)( payload-28UL );
     466           0 :     fd_udp_hdr_t * udp_hdr    = (fd_udp_hdr_t *)( payload- 8UL );
     467           0 :     memset( eth_hdr->dst, 0, 6 );
     468           0 :     memset( eth_hdr->src, 0, 6 );
     469           0 :     eth_hdr->net_type = fd_ushort_bswap( FD_ETH_HDR_TYPE_IP );
     470           0 :     *ip_hdr = (fd_ip4_hdr_t) {
     471           0 :       .verihl      = FD_IP4_VERIHL( 4, 5 ),
     472           0 :       .net_tot_len = fd_ushort_bswap( (ushort)( payload_sz+28UL ) ),
     473           0 :       .ttl         = 1,
     474           0 :       .protocol    = FD_IP4_HDR_PROTOCOL_UDP,
     475           0 :     };
     476           0 :     uint daddr_ = (uint)(ulong)daddr;
     477           0 :     memcpy( ip_hdr->saddr_c, &sa->sin_addr.s_addr, 4 );
     478           0 :     memcpy( ip_hdr->daddr_c, &daddr_,              4 );
     479           0 :     *udp_hdr = (fd_udp_hdr_t) {
     480           0 :       .net_sport = sa->sin_port,
     481           0 :       .net_dport = (ushort)fd_ushort_bswap( (ushort)dport ),
     482           0 :       .net_len   = (ushort)fd_ushort_bswap( (ushort)( payload_sz+8UL ) ),
     483           0 :       .check     = 0
     484           0 :     };
     485             : 
     486           0 :     ctx->metrics.rx_pkt_cnt++;
     487           0 :     ulong chunk = fd_laddr_to_chunk( base, eth_hdr );
     488           0 :     ulong sig   = fd_disco_netmux_sig( sa->sin_addr.s_addr, fd_ushort_bswap( sa->sin_port ), sa->sin_addr.s_addr, proto, hdr_sz );
     489           0 :     ulong tspub = fd_frag_meta_ts_comp( ts );
     490             : 
     491             :     /* When a message arrives on the repair intake port, it is sent to
     492             :        the shred tile, unless it is a ping message or an alpenglow
     493             :        repair response (identified by the frame size), then it is sent
     494             :        to the repair tile.  The repair tile does not own any sockets, so
     495             :        we look up the net_repair link directly. */
     496           0 :     if( FD_UNLIKELY( sock_idx==ctx->repair_shred_sock_idx && payload_sz<=AG_REPAIR_RESPONSE_MAX_SZ ) ) {
     497           0 :       fd_sock_link_rx_t * repair_link = ctx->link_rx + ctx->repair_rx;
     498           0 :       uchar * repair_buf = fd_chunk_to_laddr( repair_link->base, repair_link->chunk );
     499           0 :       memcpy( repair_buf, eth_hdr, frame_sz );
     500           0 :       fd_stem_publish( stem, ctx->repair_rx, sig, repair_link->chunk, frame_sz, 0UL, 0UL, tspub );
     501           0 :       repair_link->chunk = fd_dcache_compact_next( repair_link->chunk, FD_NET_MTU, repair_link->chunk0, repair_link->wmark );
     502           0 :     } else {
     503           0 :       fd_stem_publish( stem, rx_link, sig, chunk, frame_sz, 0UL, 0UL, tspub );
     504           0 :     }
     505             : 
     506           0 :     last_chunk = chunk;
     507           0 :   }
     508             : 
     509             :   /* Rewind the chunk index to the first free index. */
     510           0 :   link->chunk = fd_dcache_compact_next( last_chunk, FD_NET_MTU, chunk0, wmark );
     511           0 :   return (ulong)msg_cnt;
     512           0 : }
     513             : 
     514             : static ulong
     515             : poll_rx( fd_sock_tile_t *    ctx,
     516           0 :          fd_stem_context_t * stem ) {
     517           0 :   ulong pkt_cnt = 0UL;
     518           0 :   if( FD_UNLIKELY( ctx->batch_cnt ) ) {
     519           0 :     FD_LOG_ERR(( "Batch is not clean" ));
     520           0 :   }
     521           0 :   ctx->tx_idle_cnt = 0; /* restart TX polling */
     522           0 :   long wait_ns = ctx->lo_ifindex ? (long)((double)fd_long_max( stem->then-fd_tickcount(), 0L )/ctx->tick_per_ns) : 0L;
     523           0 :   struct timespec ts = { .tv_sec = wait_ns/1000000000L, .tv_nsec = wait_ns%1000000000L };
     524           0 :   if( FD_UNLIKELY( ppoll( ctx->pollfd, ctx->sock_cnt, &ts, NULL )<0 ) ) {
     525           0 :     if( FD_UNLIKELY( errno==EINTR ) ) return 0UL;
     526           0 :     FD_LOG_ERR(( "ppoll failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     527           0 :   }
     528           0 :   for( uint j=0U; j<ctx->sock_cnt; j++ ) {
     529           0 :     if( ctx->pollfd[ j ].revents & (POLLIN|POLLERR) ) {
     530           0 :       pkt_cnt += poll_rx_socket(
     531           0 :         ctx,
     532           0 :         stem,
     533           0 :         j,
     534           0 :         ctx->pollfd[ j ].fd,
     535           0 :         ctx->proto_id[ j ]
     536           0 :       );
     537           0 :     }
     538           0 :     ctx->pollfd[ j ].revents = 0;
     539           0 :   }
     540           0 :   return pkt_cnt;
     541           0 : }
     542             : 
     543             : /* TX PATH (tango->socket) ********************************************/
     544             : 
     545             : static void
     546           0 : flush_tx_batch( fd_sock_tile_t * ctx ) {
     547           0 :   ulong batch_cnt = ctx->batch_cnt;
     548           0 :   for( int j = 0; j < (int)batch_cnt; /* incremented in loop */ ) {
     549           0 :     int remain   = (int)batch_cnt - j;
     550           0 :     int send_cnt = sendmmsg( ctx->tx_sock, ctx->batch_msg + j, (uint)remain, MSG_DONTWAIT );
     551           0 :     if( send_cnt>=0 ) {
     552           0 :       ctx->metrics.sys_sendmmsg_cnt[ FD_METRICS_ENUM_SENDMMSG_RESULT_V_SUCCESS_IDX ]++;
     553           0 :     }
     554           0 :     if( FD_UNLIKELY( send_cnt < remain ) ) {
     555           0 :       ctx->metrics.tx_drop_cnt++;
     556           0 :       if( FD_UNLIKELY( send_cnt < 0 ) ) {
     557           0 :         switch( errno ) {
     558           0 :         case EAGAIN:
     559           0 :         case ENOBUFS:
     560           0 :           ctx->metrics.sys_sendmmsg_cnt[ FD_METRICS_ENUM_SENDMMSG_RESULT_V_BUFFER_FULL_IDX ]++;
     561           0 :           break;
     562           0 :         case EPERM:
     563           0 :           ctx->metrics.sys_sendmmsg_cnt[ FD_METRICS_ENUM_SENDMMSG_RESULT_V_PERMISSION_IDX ]++;
     564           0 :           break;
     565           0 :         case ENETUNREACH:
     566           0 :         case EHOSTUNREACH:
     567           0 :           ctx->metrics.sys_sendmmsg_cnt[ FD_METRICS_ENUM_SENDMMSG_RESULT_V_UNREACHABLE_IDX ]++;
     568           0 :           break;
     569           0 :         case ENONET:
     570           0 :         case ENETDOWN:
     571           0 :         case EHOSTDOWN:
     572           0 :           ctx->metrics.sys_sendmmsg_cnt[ FD_METRICS_ENUM_SENDMMSG_RESULT_V_DOWN_IDX ]++;
     573           0 :           break;
     574           0 :         default:
     575           0 :           ctx->metrics.sys_sendmmsg_cnt[ FD_METRICS_ENUM_SENDMMSG_RESULT_V_OTHER_IDX ]++;
     576             :           /* log with NOTICE, since flushing has a significant negative performance impact */
     577           0 :           FD_LOG_NOTICE(( "sendmmsg failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     578           0 :         }
     579             : 
     580             :         /* first message failed, so skip failing message and continue */
     581           0 :         j++;
     582           0 :       } else {
     583             :         /* send_cnt succeeded, so skip those and also the failing message */
     584           0 :         j += send_cnt + 1;
     585             : 
     586             :         /* add the successful count */
     587           0 :         ctx->metrics.tx_pkt_cnt += (ulong)send_cnt;
     588           0 :         for( int k=j-send_cnt-1; k<j-1; k++ ) ctx->metrics.tx_bytes_total += ctx->batch_frame_sz[ k ];
     589           0 :       }
     590             : 
     591           0 :       continue;
     592           0 :     }
     593             : 
     594             :     /* send_cnt == batch_cnt, so we sent everything */
     595           0 :     ctx->metrics.tx_pkt_cnt += (ulong)send_cnt;
     596           0 :     for( int k=j; k<j+send_cnt; k++ ) ctx->metrics.tx_bytes_total += ctx->batch_frame_sz[ k ];
     597           0 :     break;
     598           0 :   }
     599             : 
     600           0 :   ctx->tx_ptr = ctx->tx_scratch0;
     601           0 :   ctx->batch_cnt = 0;
     602           0 : }
     603             : 
     604             : /* before_frag is called when a new frag has been detected.  Every sock
     605             :    tile reads every TX link, so each sends only its share of packets. */
     606             : 
     607             : static inline int
     608             : before_frag( fd_sock_tile_t * ctx,
     609             :              ulong            in_idx FD_PARAM_UNUSED,
     610             :              ulong            seq    FD_PARAM_UNUSED,
     611           0 :              ulong            sig ) {
     612           0 :   ulong proto = fd_disco_netmux_sig_proto( sig );
     613           0 :   if( FD_UNLIKELY( proto!=DST_PROTO_OUTGOING ) ) return 1;
     614           0 :   if( fd_disco_netmux_sig_hash( sig )%ctx->tx_tile_cnt!=ctx->tx_tile_idx ) return 1;
     615           0 :   return 0; /* continue */
     616           0 : }
     617             : 
     618             : /* during_frag is called when a new frag passed early filtering.
     619             :    Speculatively copies data into a sendmmsg buffer.  (If all tiles
     620             :    respect backpressure could eliminate this copy) */
     621             : 
     622             : static inline void
     623             : during_frag( fd_sock_tile_t * ctx,
     624             :              ulong            in_idx,
     625             :              ulong            seq FD_PARAM_UNUSED,
     626             :              ulong            sig,
     627             :              ulong            chunk,
     628             :              ulong            sz,
     629           0 :              ulong            ctl FD_PARAM_UNUSED ) {
     630           0 :   if( FD_UNLIKELY( chunk<ctx->link_tx[ in_idx ].chunk0 || chunk>ctx->link_tx[ in_idx ].wmark || sz>FD_NET_MTU ) ) {
     631           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 ));
     632           0 :   }
     633             : 
     634           0 :   ctx->parsed.invalid = 0;
     635             : 
     636           0 :   ulong const hdr_min = sizeof(fd_eth_hdr_t)+sizeof(fd_ip4_hdr_t)+sizeof(fd_udp_hdr_t);
     637           0 :   if( FD_UNLIKELY( sz<hdr_min ) ) {
     638             :     /* FIXME support ICMP messages in the future?
     639             :        Defer the error to after_frag, where we know we weren't
     640             :        overrun. */
     641           0 :     ctx->parsed.invalid = 1;
     642           0 :     return;
     643           0 :   }
     644             : 
     645           0 :   uchar const * frame   = fd_chunk_to_laddr_const( ctx->link_tx[ in_idx ].base, chunk );
     646           0 :   ulong         hdr_sz  = fd_disco_netmux_sig_hdr_sz( sig );
     647           0 :   uchar const * payload = frame+hdr_sz;
     648           0 :   if( FD_UNLIKELY( hdr_sz>sz || hdr_sz<hdr_min ) ) {
     649           0 :     FD_LOG_ERR(( "packet from in_idx=%lu corrupt: hdr_sz=%lu total_sz=%lu",
     650           0 :                  in_idx, hdr_sz, sz ));
     651           0 :   }
     652           0 :   ulong payload_sz = sz-hdr_sz;
     653             : 
     654           0 :   fd_ip4_hdr_t const * ip_hdr  = (fd_ip4_hdr_t const *)( frame  +sizeof(fd_eth_hdr_t) );
     655           0 :   fd_udp_hdr_t const * udp_hdr = (fd_udp_hdr_t const *)( payload-sizeof(fd_udp_hdr_t) );
     656           0 :   ctx->parsed.ip_version  = FD_IP4_GET_VERSION( *ip_hdr );
     657           0 :   ctx->parsed.ip_protocol = ip_hdr->protocol;
     658           0 :   if( FD_UNLIKELY( ( ctx->parsed.ip_version !=4                       ) |
     659           0 :                    ( ctx->parsed.ip_protocol!=FD_IP4_HDR_PROTOCOL_UDP ) ) ) {
     660             :     /* sock tile only supports IPv4 UDP for now.  Defer the error to
     661             :        after_frag, where we know we weren't overrun. */
     662           0 :     ctx->parsed.invalid = 1;
     663           0 :     return;
     664           0 :   }
     665             : 
     666           0 :   ulong msg_sz = sizeof(fd_udp_hdr_t) + payload_sz;
     667             : 
     668           0 :   ulong batch_idx = ctx->batch_cnt;
     669           0 :   FD_DCHECK_CRIT( batch_idx<STEM_BURST, "flow control error" );
     670           0 :   struct mmsghdr *     msg  = ctx->batch_msg + batch_idx;
     671           0 :   struct sockaddr_in * sa   = ctx->batch_sa  + batch_idx;
     672           0 :   struct iovec   *     iov  = ctx->batch_iov + batch_idx;
     673           0 :   struct cmsghdr *     cmsg = (void *)( (ulong)ctx->batch_cmsg + batch_idx*FD_SOCK_CMSG_MAX );
     674           0 :   uchar *              buf  = ctx->tx_ptr;
     675             : 
     676           0 :   *iov = (struct iovec) {
     677           0 :     .iov_base = buf,
     678           0 :     .iov_len  = msg_sz,
     679           0 :   };
     680           0 :   sa->sin_family      = AF_INET;
     681           0 :   sa->sin_addr.s_addr = FD_LOAD( uint, ip_hdr->daddr_c );
     682           0 :   sa->sin_port        = 0; /* ignored */
     683             : 
     684           0 :   cmsg->cmsg_level = IPPROTO_IP;
     685           0 :   cmsg->cmsg_type  = IP_PKTINFO;
     686           0 :   cmsg->cmsg_len   = CMSG_LEN( sizeof(struct in_pktinfo) );
     687           0 :   struct in_pktinfo * pi = (struct in_pktinfo *)CMSG_DATA( cmsg );
     688           0 :   pi->ipi_ifindex         = 0;
     689           0 :   pi->ipi_addr.s_addr     = 0;
     690           0 :   pi->ipi_spec_dst.s_addr = fd_uint_if( !!ip_hdr->saddr, ip_hdr->saddr, ctx->bind_address );
     691             : 
     692           0 :   *msg = (struct mmsghdr) {
     693           0 :     .msg_hdr = {
     694           0 :       .msg_name       = sa,
     695           0 :       .msg_namelen    = sizeof(struct sockaddr_in),
     696           0 :       .msg_iov        = iov,
     697           0 :       .msg_iovlen     = 1,
     698           0 :       .msg_control    = cmsg,
     699           0 :       .msg_controllen = CMSG_LEN( sizeof(struct in_pktinfo) )
     700           0 :     }
     701           0 :   };
     702             : 
     703           0 :   memcpy( buf, udp_hdr, sizeof(fd_udp_hdr_t) );
     704           0 :   fd_memcpy( buf+sizeof(fd_udp_hdr_t), payload, payload_sz );
     705           0 :   ctx->batch_frame_sz[ batch_idx ] = sz;
     706           0 : }
     707             : 
     708             : /* after_frag is called when a frag was copied into a sendmmsg buffer. */
     709             : 
     710             : static void
     711             : after_frag( fd_sock_tile_t *    ctx,
     712             :             ulong               in_idx,
     713             :             ulong               seq    FD_PARAM_UNUSED,
     714             :             ulong               sig    FD_PARAM_UNUSED,
     715             :             ulong               sz,
     716             :             ulong               tsorig FD_PARAM_UNUSED,
     717             :             ulong               tspub  FD_PARAM_UNUSED,
     718           0 :             fd_stem_context_t * stem   FD_PARAM_UNUSED ) {
     719             :   /* Commit the packet added in during_frag.  during_frag defers fatal
     720             :      errors to here (where we know we weren't overrun) by setting the
     721             :      invalid flag. */
     722             : 
     723           0 :   if( FD_UNLIKELY( ctx->parsed.invalid ) ) {
     724           0 :     ulong const hdr_min = sizeof(fd_eth_hdr_t)+sizeof(fd_ip4_hdr_t)+sizeof(fd_udp_hdr_t);
     725           0 :     if( FD_UNLIKELY( sz<hdr_min ) ) {
     726             :       /* FIXME support ICMP messages in the future? */
     727           0 :       FD_LOG_ERR(( "packet too small %lu (in_idx=%lu)", sz, in_idx ));
     728           0 :     }
     729           0 :     if( FD_UNLIKELY( ( ctx->parsed.ip_version !=4                       ) |
     730           0 :                      ( ctx->parsed.ip_protocol!=FD_IP4_HDR_PROTOCOL_UDP ) ) ) {
     731           0 :       FD_LOG_ERR(( "packet from in_idx=%lu: sock tile only supports IPv4 UDP for now", in_idx ));
     732           0 :     }
     733           0 :   }
     734             : 
     735           0 :   ctx->tx_idle_cnt = 0;
     736           0 :   ctx->batch_cnt++;
     737             :   /* Technically leaves a gap.  sz is always larger than the payload
     738             :      written to tx_ptr because Ethernet & IPv4 headers were stripped. */
     739           0 :   ctx->tx_ptr += fd_ulong_align_up( sz, FD_CHUNK_ALIGN );
     740             : 
     741           0 :   if( ctx->batch_cnt >= STEM_BURST ) {
     742           0 :     flush_tx_batch( ctx );
     743           0 :   }
     744           0 : }
     745             : 
     746             : /* End TX path ********************************************************/
     747             : 
     748             : static void
     749           0 : metrics_write( fd_sock_tile_t * ctx ) {
     750           0 :   FD_MCNT_SET( SOCK, SYSCALL_RX,              ctx->metrics.sys_recvmmsg_cnt     );
     751           0 :   FD_MCNT_ENUM_COPY( SOCK, SYSCALL_TX,        ctx->metrics.sys_sendmmsg_cnt     );
     752           0 :   FD_MCNT_SET( SOCK, PKT_RX,                  ctx->metrics.rx_pkt_cnt           );
     753           0 :   FD_MCNT_SET( SOCK, PKT_TX,                  ctx->metrics.tx_pkt_cnt           );
     754           0 :   FD_MCNT_SET( SOCK, PKT_TX_FAILED,           ctx->metrics.tx_drop_cnt          );
     755           0 :   FD_MCNT_SET( SOCK, PKT_TX_BYTES,            ctx->metrics.tx_bytes_total       );
     756           0 :   FD_MCNT_SET( SOCK, PKT_RX_BYTES,            ctx->metrics.rx_bytes_total       );
     757           0 : }
     758             : 
     759             : /* after_credit is called every stem iteration when there are enough
     760             :    flow control credits to publish a burst of fragments. */
     761             : 
     762             : static inline void
     763             : after_credit( fd_sock_tile_t *    ctx,
     764             :               fd_stem_context_t * stem,
     765             :               int *               poll_in FD_PARAM_UNUSED,
     766           0 :               int *               charge_busy ) {
     767           0 :   if( ctx->lo_ifindex ) {
     768             :     /* Housekeeping cannot publish progress while the tile sleeps. */
     769           0 :     FD_COMPILER_MFENCE();
     770           0 :     metrics_write( ctx );
     771           0 :     FD_MGAUGE_SET( TILE, HEARTBEAT_TIME, (ulong)fd_log_wallclock() );
     772           0 :     for( ulong i=0UL; i<ctx->link_rx_cnt; i++ ) {
     773           0 :       fd_mcache_seq_update( fd_mcache_seq_laddr( stem->mcaches[ i ] ), stem->seqs[ i ] );
     774           0 :     }
     775           0 :     FD_COMPILER_MFENCE();
     776           0 :     *charge_busy |= poll_rx( ctx, stem )!=0UL;
     777           0 :     return;
     778           0 :   }
     779           0 :   if( ctx->tx_idle_cnt > 512 ) {
     780           0 :     if( ctx->batch_cnt ) {
     781           0 :       flush_tx_batch( ctx );
     782           0 :     }
     783           0 :     ulong pkt_cnt = poll_rx( ctx, stem );
     784           0 :     *charge_busy = pkt_cnt!=0;
     785           0 :   }
     786           0 :   ctx->tx_idle_cnt++;
     787           0 : }
     788             : 
     789             : static ulong
     790             : rlimit_file_cnt( fd_topo_t const *      topo,
     791           0 :                  fd_topo_tile_t const * tile ) {
     792           0 :   fd_sock_tile_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     793           0 :   return RX_SOCK_FD_MIN + ctx->sock_cnt;
     794           0 : }
     795             : 
     796             : #define STEM_NEVER_PARK             1
     797           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_sock_tile_t
     798           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_sock_tile_t)
     799             : 
     800           0 : #define STEM_LAZY ((long)10e6) /* 10ms */
     801             : 
     802           0 : #define STEM_CALLBACK_METRICS_WRITE       metrics_write
     803           0 : #define STEM_CALLBACK_AFTER_CREDIT        after_credit
     804           0 : #define STEM_CALLBACK_BEFORE_FRAG         before_frag
     805           0 : #define STEM_CALLBACK_DURING_FRAG         during_frag
     806           0 : #define STEM_CALLBACK_AFTER_FRAG          after_frag
     807             : 
     808             : #include "../../stem/fd_stem.c"
     809             : 
     810             : fd_topo_run_tile_t fd_tile_sock = {
     811             :   .name                     = "sock",
     812             :   .rlimit_file_cnt_fn       = rlimit_file_cnt,
     813             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     814             :   .populate_allowed_fds     = populate_allowed_fds,
     815             :   .scratch_align            = scratch_align,
     816             :   .scratch_footprint        = scratch_footprint,
     817             :   .privileged_init          = privileged_init,
     818             :   .unprivileged_init        = unprivileged_init,
     819             :   .run                      = stem_run,
     820             : };

Generated by: LCOV version 1.14