LCOV - code coverage report
Current view: top level - disco/net/xdp - fd_xdp_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 891 0.0 %
Date: 2026-03-19 18:19:27 Functions: 0 60 0.0 %

          Line data    Source code
       1             : /* The xdp tile translates between AF_XDP and fd_tango
       2             :    traffic.  It is responsible for setting up the XDP and
       3             :    XSK socket configuration. */
       4             : 
       5             : #include "../fd_net_tile.h"
       6             : 
       7             : #include <errno.h>
       8             : #include <fcntl.h>
       9             : #include <net/if.h>
      10             : #include <netinet/in.h>
      11             : #include <sys/socket.h> /* MSG_DONTWAIT needed before importing the net seccomp filter */
      12             : #include <linux/if_xdp.h>
      13             : 
      14             : #include "../fd_net_common.h"
      15             : #include "../../metrics/fd_metrics.h"
      16             : #include "../../netlink/fd_netlink_tile.h" /* neigh4_solicit */
      17             : #include "../../topo/fd_topo.h"
      18             : 
      19             : #include "../../../waltz/ip/fd_fib4.h"
      20             : #include "../../../waltz/neigh/fd_neigh4_map.h"
      21             : #include "../../../waltz/mib/fd_netdev_tbl.h"
      22             : #include "../../../waltz/mib/fd_dbl_buf.h"
      23             : #include "../../../waltz/xdp/fd_xdp_redirect_user.h" /* fd_xsk_activate */
      24             : #include "../../../waltz/xdp/fd_xsk.h"
      25             : #include "../../../util/log/fd_dtrace.h"
      26             : #include "../../../util/net/fd_eth.h"
      27             : #include "../../../util/net/fd_ip4.h"
      28             : #include "../../../util/net/fd_gre.h"
      29             : #include "../../../util/pod/fd_pod_format.h"
      30             : 
      31             : #include <unistd.h>
      32             : #include <linux/if.h> /* struct ifreq */
      33             : #include <sys/ioctl.h>
      34             : #include <linux/if_arp.h>
      35             : 
      36             : #include "generated/fd_xdp_tile_seccomp.h"
      37             : 
      38             : /* MAX_NET_INS controls the max number of TX links that a net tile can
      39             :    serve. */
      40             : 
      41             : #define MAX_NET_INS (32UL)
      42             : 
      43             : /* FD_XDP_STATS_INTERVAL_NS controls the XDP stats refresh interval.
      44             :    This should be lower than the interval at which the metrics tile
      45             :    collects metrics. */
      46             : 
      47           0 : #define FD_XDP_STATS_INTERVAL_NS (11e6) /* 11ms */
      48             : 
      49             : /* XSK_IDX_{MAIN,LO} are the hardcoded XSK indices in ctx->xsk[ ... ].
      50             :    Only net tile 0 has XSK_IDX_LO, all net tiles have XSK_IDX_MAIN. */
      51             : 
      52           0 : #define XSK_IDX_MAIN 0
      53           0 : #define XSK_IDX_LO   1
      54             : 
      55             : /* fd_net_in_ctx_t contains consumer information for an incoming tango
      56             :    link.  It is used as part of the TX path. */
      57             : 
      58             : typedef struct {
      59             :   fd_wksp_t * mem;
      60             :   ulong       chunk0;
      61             :   ulong       wmark;
      62             : } fd_net_in_ctx_t;
      63             : 
      64             : /* fd_net_out_ctx_t contains publisher information for a link to a
      65             :    downstream app tile.  It is used as part of the RX path. */
      66             : 
      67             : typedef struct {
      68             :   fd_frag_meta_t * mcache;
      69             :   ulong *          sync;
      70             :   ulong            depth;
      71             :   ulong            seq;
      72             : } fd_net_out_ctx_t;
      73             : 
      74             : /* fd_net_flusher_t controls the pacing of XDP sendto calls for flushing
      75             :    TX batches.  In the 'wakeup' XDP mode, no TX occurs unless the net
      76             :    tile wakes up the kernel periodically using the sendto() syscall.
      77             :    If sendto() is called too frequently, time is wasted on context
      78             :    switches.  If sendto() is called not often enough, packets are
      79             :    delayed or dropped.  sendto() calls make almost no guarantees how
      80             :    much packets are sent out, nor do they indicate when the kernel
      81             :    finishes a wakeup call (asynchronously dispatched).  The net tile
      82             :    thus uses a myraid of flush triggers that were tested for best
      83             :    performance. */
      84             : 
      85             : struct fd_net_flusher {
      86             : 
      87             :   /* Packets that were enqueued after the last sendto() wakeup are
      88             :      considered "pending".  If there are more than pending_wmark packets
      89             :      pending, a wakeup is dispatched.  Thus, this dispatch trigger is
      90             :      proportional to packet rate, but does not trigger if I/O is seldom. */
      91             :   ulong pending_cnt;
      92             :   ulong pending_wmark;
      93             : 
      94             :   /* Sometimes, packets are not flushed out even after a sendto()
      95             :      wakeup.  This can result in the tail of a burst getting delayed or
      96             :      overrun.  If more than tail_flush_backoff ticks pass since the last
      97             :      sendto() wakeup and there are still unacknowledged packets in the
      98             :      TX ring, issues another wakeup. */
      99             :   long next_tail_flush_ticks;
     100             :   long tail_flush_backoff;
     101             : 
     102             : };
     103             : 
     104             : typedef struct fd_net_flusher fd_net_flusher_t;
     105             : 
     106             : FD_PROTOTYPES_BEGIN
     107             : 
     108             : /* fd_net_flusher_inc marks a new packet as enqueued. */
     109             : 
     110             : static inline void
     111             : fd_net_flusher_inc( fd_net_flusher_t * flusher,
     112           0 :                     long               now ) {
     113           0 :   flusher->pending_cnt++;
     114           0 :   long next_flush = now + flusher->tail_flush_backoff;
     115           0 :   flusher->next_tail_flush_ticks = fd_long_min( flusher->next_tail_flush_ticks, next_flush );
     116           0 : }
     117             : 
     118             : /* fd_net_flusher_check returns 1 if a sendto() wakeup should be issued
     119             :    immediately.  now is a recent fd_tickcount() value.
     120             :    If tx_ring_empty==0 then the kernel is caught up with the net tile
     121             :    on the XDP TX ring.  (Otherwise, the kernel is behind the net tile) */
     122             : 
     123             : static inline int
     124             : fd_net_flusher_check( fd_net_flusher_t * flusher,
     125             :                       long               now,
     126           0 :                       int                tx_ring_empty ) {
     127           0 :   int flush_level   = flusher->pending_cnt >= flusher->pending_wmark;
     128           0 :   int flush_timeout = now >= flusher->next_tail_flush_ticks;
     129           0 :   int flush         = flush_level || flush_timeout;
     130           0 :   if( !flush ) return 0;
     131           0 :   if( FD_UNLIKELY( tx_ring_empty ) ) {
     132             :     /* Flush requested but caught up */
     133           0 :     flusher->pending_cnt           = 0UL;
     134           0 :     flusher->next_tail_flush_ticks = LONG_MAX;
     135           0 :     return 0;
     136           0 :   }
     137           0 :   return 1;
     138           0 : }
     139             : 
     140             : /* fd_net_flusher_wakeup signals a sendto() wakeup was done.  now is a
     141             :    recent fd_tickcount() value. */
     142             : 
     143             : static inline void
     144             : fd_net_flusher_wakeup( fd_net_flusher_t * flusher,
     145           0 :                        long               now ) {
     146           0 :   flusher->pending_cnt           = 0UL;
     147           0 :   flusher->next_tail_flush_ticks = now + flusher->tail_flush_backoff;
     148           0 : }
     149             : 
     150             : FD_PROTOTYPES_END
     151             : 
     152             : /* fd_net_free_ring is a FIFO queue that stores pointers to free XDP TX
     153             :    frames. */
     154             : 
     155             : struct fd_net_free_ring {
     156             :   ulong   prod;
     157             :   ulong   cons;
     158             :   ulong   depth;
     159             :   ulong * queue;
     160             : };
     161             : typedef struct fd_net_free_ring fd_net_free_ring_t;
     162             : 
     163             : typedef struct {
     164             :   /* An "XSK" is an AF_XDP socket */
     165             :   uint     xsk_cnt;
     166             :   fd_xsk_t xsk[ 2 ];
     167             :   int      prog_link_fds[ 2 ];
     168             :   uint     if_virt;
     169             : 
     170             :   /* UMEM frame region within dcache */
     171             :   void *   umem;    /* Start of UMEM */
     172             :   ulong    umem_sz; /* Size  of UMEM */
     173             : 
     174             :   /* UMEM chunk region within workspace */
     175             :   uint     umem_chunk0; /* Lowest allowed chunk number */
     176             :   uint     umem_wmark;  /* Highest allowed chunk number */
     177             : 
     178             :   /* All net tiles are subscribed to the same TX links.  (These are
     179             :      incoming links from app tiles asking the net tile to send out packets)
     180             :      The net tiles "take turns" doing TX jobs based on the L3+L4 dst hash.
     181             :      net_tile_id is the index of the current interface, net_tile_cnt is the
     182             :      total amount of interfaces. */
     183             :   uint net_tile_id;
     184             :   uint net_tile_cnt;
     185             : 
     186             :   /* Details pertaining to an inflight send op */
     187             :   struct {
     188             :     uint   xsk_idx;
     189             :     void * frame;
     190             :     uchar  mac_addrs[12];     /* First 12 bytes of Ethernet header */
     191             :     uint   src_ip;            /* src_ip in net order */
     192             : 
     193             :     uint   use_gre;           /* The tx packet will be GRE-encapsulated */
     194             :     uint   gre_outer_src_ip;  /* For GRE: Outer iphdr's src_ip in net order */
     195             :     uint   gre_outer_dst_ip;  /* For GRE: Outer iphdr's dst_ip in net order */
     196             :   } tx_op;
     197             : 
     198             :   /* Round-robin cycle serivce operations */
     199             :   uint rr_idx;
     200             : 
     201             :   /* Ring tracking free packet buffers */
     202             :   fd_net_free_ring_t free_tx;
     203             : 
     204             :   uchar  src_mac_addr[6];
     205             :   uint   default_address;
     206             : 
     207             :   uint   bind_address;
     208             :   ushort shred_listen_port;
     209             :   ushort quic_transaction_listen_port;
     210             :   ushort legacy_transaction_listen_port;
     211             :   ushort gossip_listen_port;
     212             :   ushort repair_intake_listen_port;
     213             :   ushort repair_serve_listen_port;
     214             :   ushort txsend_src_port;
     215             : 
     216             :   ulong in_cnt;
     217             :   fd_net_in_ctx_t in[ MAX_NET_INS ];
     218             : 
     219             :   fd_net_out_ctx_t quic_out[1];
     220             :   fd_net_out_ctx_t shred_out[1];
     221             :   fd_net_out_ctx_t gossvf_out[1];
     222             :   fd_net_out_ctx_t repair_out[1];
     223             :   fd_net_out_ctx_t txsend_out[1];
     224             : 
     225             :   /* XDP stats refresh timer */
     226             :   long xdp_stats_interval_ticks;
     227             :   long next_xdp_stats_refresh;
     228             : 
     229             :   /* TX flush timers */
     230             :   fd_net_flusher_t tx_flusher[2]; /* one per XSK */
     231             : 
     232             :   /* Route and neighbor tables */
     233             :   fd_fib4_t fib_local[1];
     234             :   fd_fib4_t fib_main[1];
     235             :   fd_neigh4_hmap_t  neigh4[1];
     236             :   fd_netlink_neigh4_solicit_link_t neigh4_solicit[1];
     237             : 
     238             :   /* Netdev table */
     239             :   fd_dbl_buf_t *       netdev_dbl_buf;    /* remote copy of device table */
     240             :   uchar *              netdev_buf;        /* local copy of device table */
     241             :   ulong                netdev_buf_sz;
     242             :   fd_netdev_tbl_join_t netdev_tbl;        /* join to local copy of device table */
     243             :   int                  has_gre_interface; /* enable GRE support? */
     244             : 
     245             :   struct {
     246             :     ulong rx_pkt_cnt;
     247             :     ulong rx_bytes_total;
     248             :     ulong rx_src_addr_invalid_cnt;
     249             :     ulong rx_undersz_cnt;
     250             :     ulong rx_fill_blocked_cnt;
     251             :     ulong rx_backp_cnt;
     252             :     long  rx_busy_cnt;
     253             :     long  rx_idle_cnt;
     254             : 
     255             :     ulong tx_submit_cnt;
     256             :     ulong tx_complete_cnt;
     257             :     ulong tx_bytes_total;
     258             :     ulong tx_route_fail_cnt;
     259             :     ulong tx_no_xdp_cnt;
     260             :     ulong tx_neigh_fail_cnt;
     261             :     ulong tx_full_fail_cnt;
     262             :     long  tx_busy_cnt;
     263             :     long  tx_idle_cnt;
     264             : 
     265             :     ulong xsk_tx_wakeup_cnt;
     266             :     ulong xsk_rx_wakeup_cnt;
     267             : 
     268             :     ulong rx_gre_cnt;
     269             :     ulong rx_gre_ignored_cnt;
     270             :     ulong rx_gre_inv_pkt_cnt;
     271             :     ulong tx_gre_cnt;
     272             :     ulong tx_gre_route_fail_cnt;
     273             :   } metrics;
     274             : } fd_net_ctx_t;
     275             : 
     276             : FD_FN_CONST static inline ulong
     277           0 : scratch_align( void ) {
     278           0 :   return 4096UL;
     279           0 : }
     280             : 
     281             : FD_FN_PURE static inline ulong
     282           0 : scratch_footprint( fd_topo_tile_t const * tile ) {
     283           0 :   ulong l = FD_LAYOUT_INIT;
     284           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_net_ctx_t), sizeof(fd_net_ctx_t)                      );
     285           0 :   l = FD_LAYOUT_APPEND( l, alignof(ulong),        tile->xdp.free_ring_depth * sizeof(ulong) );
     286           0 :   l = FD_LAYOUT_APPEND( l, fd_netdev_tbl_align(), fd_netdev_tbl_footprint( NETDEV_MAX, BOND_MASTER_MAX ) );
     287           0 :   return FD_LAYOUT_FINI( l, scratch_align() );
     288           0 : }
     289             : 
     290             : static void
     291           0 : metrics_write( fd_net_ctx_t * ctx ) {
     292           0 :   FD_MCNT_SET(   NET, RX_PKT_CNT,          ctx->metrics.rx_pkt_cnt          );
     293           0 :   FD_MCNT_SET(   NET, RX_BYTES_TOTAL,      ctx->metrics.rx_bytes_total      );
     294           0 :   FD_MCNT_SET(   NET, RX_UNDERSZ_CNT,      ctx->metrics.rx_undersz_cnt      );
     295           0 :   FD_MCNT_SET(   NET, RX_FILL_BLOCKED_CNT, ctx->metrics.rx_fill_blocked_cnt );
     296           0 :   FD_MCNT_SET(   NET, RX_BACKPRESSURE_CNT, ctx->metrics.rx_backp_cnt        );
     297           0 :   FD_MGAUGE_SET( NET, RX_BUSY_CNT, (ulong)fd_long_max( ctx->metrics.rx_busy_cnt, 0L ) );
     298           0 :   FD_MGAUGE_SET( NET, RX_IDLE_CNT, (ulong)fd_long_max( ctx->metrics.rx_idle_cnt, 0L ) );
     299           0 :   FD_MGAUGE_SET( NET, TX_BUSY_CNT, (ulong)fd_long_max( ctx->metrics.tx_busy_cnt, 0L ) );
     300           0 :   FD_MGAUGE_SET( NET, TX_IDLE_CNT, (ulong)fd_long_max( ctx->metrics.tx_idle_cnt, 0L ) );
     301             : 
     302           0 :   FD_MCNT_SET( NET, TX_SUBMIT_CNT,        ctx->metrics.tx_submit_cnt     );
     303           0 :   FD_MCNT_SET( NET, TX_COMPLETE_CNT,      ctx->metrics.tx_complete_cnt   );
     304           0 :   FD_MCNT_SET( NET, TX_BYTES_TOTAL,       ctx->metrics.tx_bytes_total    );
     305           0 :   FD_MCNT_SET( NET, TX_ROUTE_FAIL_CNT,    ctx->metrics.tx_route_fail_cnt );
     306           0 :   FD_MCNT_SET( NET, TX_NEIGHBOR_FAIL_CNT, ctx->metrics.tx_neigh_fail_cnt );
     307           0 :   FD_MCNT_SET( NET, TX_FULL_FAIL_CNT,     ctx->metrics.tx_full_fail_cnt  );
     308             : 
     309           0 :   FD_MCNT_SET( NET, XSK_TX_WAKEUP_CNT,    ctx->metrics.xsk_tx_wakeup_cnt    );
     310           0 :   FD_MCNT_SET( NET, XSK_RX_WAKEUP_CNT,    ctx->metrics.xsk_rx_wakeup_cnt    );
     311             : 
     312           0 :   FD_MCNT_SET( NET, RX_GRE_CNT,            ctx->metrics.rx_gre_cnt            );
     313           0 :   FD_MCNT_SET( NET, RX_GRE_INVALID_CNT,    ctx->metrics.rx_gre_inv_pkt_cnt    );
     314           0 :   FD_MCNT_SET( NET, RX_GRE_IGNORED_CNT,    ctx->metrics.rx_gre_ignored_cnt    );
     315           0 :   FD_MCNT_SET( NET, TX_GRE_CNT,            ctx->metrics.tx_gre_cnt            );
     316           0 :   FD_MCNT_SET( NET, TX_GRE_ROUTE_FAIL_CNT, ctx->metrics.tx_gre_route_fail_cnt );
     317           0 :   FD_MCNT_SET( NET, RX_SRC_ADDR_INVALID_CNT, ctx->metrics.rx_src_addr_invalid_cnt );
     318           0 : }
     319             : 
     320             : struct xdp_statistics_v0 {
     321             :   __u64 rx_dropped; /* Dropped for other reasons */
     322             :   __u64 rx_invalid_descs; /* Dropped due to invalid descriptor */
     323             :   __u64 tx_invalid_descs; /* Dropped due to invalid descriptor */
     324             : };
     325             : 
     326             : struct xdp_statistics_v1 {
     327             :   __u64 rx_dropped; /* Dropped for other reasons */
     328             :   __u64 rx_invalid_descs; /* Dropped due to invalid descriptor */
     329             :   __u64 tx_invalid_descs; /* Dropped due to invalid descriptor */
     330             :   __u64 rx_ring_full; /* Dropped due to rx ring being full */
     331             :   __u64 rx_fill_ring_empty_descs; /* Failed to retrieve item from fill ring */
     332             :   __u64 tx_ring_empty_descs; /* Failed to retrieve item from tx ring */
     333             : };
     334             : 
     335             : static void
     336           0 : poll_xdp_statistics( fd_net_ctx_t * ctx ) {
     337           0 :   struct xdp_statistics_v1 stats = {0};
     338           0 :   ulong xsk_cnt = ctx->xsk_cnt;
     339           0 :   for( ulong j=0UL; j<xsk_cnt; j++ ) {
     340           0 :     struct xdp_statistics_v1 sub_stats = {0};
     341           0 :     uint optlen = (uint)sizeof(struct xdp_statistics_v1);
     342           0 :     if( FD_UNLIKELY( -1==getsockopt( ctx->xsk[ j ].xsk_fd, SOL_XDP, XDP_STATISTICS, &sub_stats, &optlen ) ) )
     343           0 :       FD_LOG_ERR(( "getsockopt(SOL_XDP, XDP_STATISTICS) failed: %s", strerror( errno ) ));
     344           0 :     if( FD_UNLIKELY( optlen!=sizeof(struct xdp_statistics_v0) &&
     345           0 :                      optlen!=sizeof(struct xdp_statistics_v1) ) ) {
     346           0 :       FD_LOG_ERR(( "getsockopt(SOL_XDP, XDP_STATISTICS) returned unexpected size %u", optlen ));
     347           0 :     }
     348           0 :     stats.rx_dropped               += sub_stats.rx_dropped;
     349           0 :     stats.rx_invalid_descs         += sub_stats.rx_invalid_descs;
     350           0 :     stats.tx_invalid_descs         += sub_stats.tx_invalid_descs;
     351           0 :     stats.rx_ring_full             += sub_stats.rx_ring_full;
     352           0 :     stats.rx_fill_ring_empty_descs += sub_stats.rx_fill_ring_empty_descs;
     353           0 :     stats.tx_ring_empty_descs      += sub_stats.tx_ring_empty_descs;
     354           0 :   }
     355             : 
     356           0 :   FD_MCNT_SET( NET, XDP_RX_DROPPED_OTHER,         stats.rx_dropped               );
     357           0 :   FD_MCNT_SET( NET, XDP_RX_INVALID_DESCS,         stats.rx_invalid_descs         );
     358           0 :   FD_MCNT_SET( NET, XDP_TX_INVALID_DESCS,         stats.tx_invalid_descs         );
     359           0 :   FD_MCNT_SET( NET, XDP_RX_RING_FULL,             stats.rx_ring_full             );
     360           0 :   FD_MCNT_SET( NET, XDP_RX_FILL_RING_EMPTY_DESCS, stats.rx_fill_ring_empty_descs );
     361           0 :   FD_MCNT_SET( NET, XDP_TX_RING_EMPTY_DESCS,      stats.tx_ring_empty_descs      );
     362           0 : }
     363             : 
     364             : /* net_is_fatal_xdp_error returns 1 if the given errno returned by an
     365             :    XDP API indicates a non-recoverable error code.  The net tile should
     366             :    crash if it sees such an error so the problem does not go undetected.
     367             :    Otherwise, returns 0. */
     368             : 
     369             : static int
     370           0 : net_is_fatal_xdp_error( int err ) {
     371           0 :   return err==ESOCKTNOSUPPORT || err==EOPNOTSUPP || err==EINVAL ||
     372           0 :          err==EPERM;
     373           0 : }
     374             : 
     375             : /* Load the netdev table to ctx->netdev_buf. Create a join in ctx->netdev_tbl_handle  */
     376             : 
     377             : static void
     378           0 : net_load_netdev_tbl( fd_net_ctx_t * ctx ) {
     379             :   /* Copy netdev table from netlink tile.  This could fail briefly
     380             :      during startup if the netlink tile is late to start up. */
     381           0 :   if( FD_UNLIKELY( !fd_dbl_buf_read( ctx->netdev_dbl_buf, ctx->netdev_buf_sz, ctx->netdev_buf, NULL ) ) ) return;
     382             : 
     383             :   /* Join local copy */
     384           0 :   if( FD_UNLIKELY( !fd_netdev_tbl_join( &ctx->netdev_tbl, ctx->netdev_buf ) ) ) FD_LOG_ERR(("netdev table join failed"));
     385           0 : }
     386             : 
     387             : /* Iterates the netdev table and returns 1 if a GRE interface exists, 0 otherwise.
     388             :    Only called in privileged_init and during_housekeeping */
     389             : 
     390             : static int
     391           0 : net_check_gre_interface_exists( fd_net_ctx_t * ctx ) {
     392           0 :   fd_netdev_t * dev_tbl = ctx->netdev_tbl.dev_tbl;
     393           0 :   ushort        dev_cnt = ctx->netdev_tbl.hdr->dev_cnt;
     394             : 
     395           0 :   for( ushort if_idx = 0; if_idx<dev_cnt; if_idx++ ) {
     396           0 :     if( dev_tbl[if_idx].dev_type==ARPHRD_IPGRE ) return 1;
     397           0 :   }
     398           0 :   return 0;
     399           0 : }
     400             : 
     401             : 
     402             : /* net_tx_ready returns 1 if we can submit a job to this TX ring, and 0 otherwise.
     403             :    Reasons for block include:
     404             :    - No TX buffer is available (free ring empty)
     405             :    - TX ring is full
     406             : 
     407             :    tx_ring: pointer to the XDP TX ring
     408             :    free_ring: pointer to the free TX ring */
     409             : 
     410             : static int
     411             : net_tx_ready( fd_xdp_ring_t *      tx_ring,
     412           0 :               fd_net_free_ring_t * free_ring ) {
     413           0 :   if( FD_UNLIKELY( free_ring->prod == free_ring->cons ) ) return 0; /* drop - no free buffers */
     414           0 :   if( FD_UNLIKELY( fd_xdp_ring_full( tx_ring ) ) )        return 0; /* drop - tx ring full */
     415           0 :   return 1;
     416           0 : }
     417             : 
     418             : /* net_rx_wakeup triggers xsk_recvmsg to run in the kernel.  Needs to be
     419             :    called periodically in order to receive packets. */
     420             : 
     421             : static void
     422             : net_rx_wakeup( fd_net_ctx_t * ctx,
     423             :                fd_xsk_t *     xsk,
     424           0 :                int *          charge_busy ) {
     425           0 :   FD_VOLATILE( *xsk->ring_rx.cons ) = xsk->ring_rx.cached_cons; /* write-back local copies to fseqs */
     426           0 :   FD_VOLATILE( *xsk->ring_fr.prod ) = xsk->ring_fr.cached_prod;
     427           0 :   if( !fd_xsk_rx_need_wakeup( xsk ) ) return;
     428           0 :   *charge_busy = 1;
     429           0 :   struct msghdr _ignored[ 1 ] = { 0 };
     430           0 :   if( FD_UNLIKELY( -1==recvmsg( xsk->xsk_fd, _ignored, MSG_DONTWAIT ) ) ) {
     431           0 :     if( FD_UNLIKELY( net_is_fatal_xdp_error( errno ) ) ) {
     432           0 :       FD_LOG_ERR(( "xsk recvmsg failed xsk_fd=%d (%i-%s)", xsk->xsk_fd, errno, fd_io_strerror( errno ) ));
     433           0 :     }
     434           0 :     if( FD_UNLIKELY( errno!=EAGAIN ) ) {
     435           0 :       long ts = fd_log_wallclock();
     436           0 :       if( ts > xsk->log_suppress_until_ns ) {
     437           0 :         FD_LOG_WARNING(( "xsk recvmsg failed xsk_fd=%d (%i-%s)", xsk->xsk_fd, errno, fd_io_strerror( errno ) ));
     438           0 :         xsk->log_suppress_until_ns = ts + (long)1e9;
     439           0 :       }
     440           0 :     }
     441           0 :   }
     442           0 :   ctx->metrics.xsk_rx_wakeup_cnt++;
     443           0 : }
     444             : 
     445             : /* net_tx_wakeup triggers xsk_sendmsg to run in the kernel.  Needs to be
     446             :    called periodically in order to transmit packets. Should only be called
     447             :    if there are unconsumed packets in Tx ring. */
     448             : 
     449             : static void
     450             : net_tx_wakeup( fd_net_ctx_t * ctx,
     451             :                fd_xsk_t *     xsk,
     452           0 :                int *          charge_busy ) {
     453           0 :   FD_VOLATILE( *xsk->ring_tx.prod ) = xsk->ring_tx.cached_prod; /* write-back local copies to fseqs */
     454           0 :   FD_VOLATILE( *xsk->ring_cr.cons ) = xsk->ring_cr.cached_cons;
     455           0 :   if( !fd_xsk_tx_need_wakeup( xsk ) ) return;
     456           0 :   *charge_busy = 1;
     457           0 :   if( FD_UNLIKELY( -1==sendto( xsk->xsk_fd, NULL, 0, MSG_DONTWAIT, NULL, 0 ) ) ) {
     458           0 :     if( FD_UNLIKELY( net_is_fatal_xdp_error( errno ) ) ) {
     459           0 :       FD_LOG_ERR(( "xsk sendto failed xsk_fd=%d (%i-%s)", xsk->xsk_fd, errno, fd_io_strerror( errno ) ));
     460           0 :     }
     461           0 :     if( FD_UNLIKELY( errno!=EAGAIN ) ) {
     462           0 :       long ts = fd_log_wallclock();
     463           0 :       if( ts > xsk->log_suppress_until_ns ) {
     464           0 :         FD_LOG_WARNING(( "xsk sendto failed xsk_fd=%d (%i-%s)", xsk->xsk_fd, errno, fd_io_strerror( errno ) ));
     465           0 :         xsk->log_suppress_until_ns = ts + (long)1e9;
     466           0 :       }
     467           0 :     }
     468           0 :   }
     469           0 :   ctx->metrics.xsk_tx_wakeup_cnt++;
     470           0 : }
     471             : 
     472             : /* net_tx_periodic_wakeup does a timer based xsk_sendmsg wakeup. */
     473             : 
     474             : static inline int
     475             : net_tx_periodic_wakeup( fd_net_ctx_t * ctx,
     476             :                         uint           xsk_idx,
     477             :                         long           now,
     478           0 :                         int *          charge_busy ) {
     479           0 :   fd_xdp_ring_t * tx_ring        = &ctx->xsk[ xsk_idx ].ring_tx;
     480           0 :   int             tx_ring_empty  = fd_xdp_ring_empty( tx_ring, FD_XDP_RING_ROLE_PROD );
     481           0 :   if( fd_net_flusher_check( ctx->tx_flusher+xsk_idx, now, tx_ring_empty ) ) {
     482           0 :     net_tx_wakeup( ctx, &ctx->xsk[ xsk_idx ], charge_busy );
     483           0 :     fd_net_flusher_wakeup( ctx->tx_flusher+xsk_idx, now );
     484           0 :   }
     485           0 :   return 0;
     486           0 : }
     487             : 
     488             : static void
     489           0 : during_housekeeping( fd_net_ctx_t * ctx ) {
     490           0 :   long now = fd_tickcount();
     491           0 :   net_load_netdev_tbl( ctx );
     492           0 :   ctx->has_gre_interface = net_check_gre_interface_exists( ctx );
     493             : 
     494           0 :   ctx->metrics.rx_busy_cnt = 0UL;
     495           0 :   ctx->metrics.rx_idle_cnt = 0UL;
     496           0 :   ctx->metrics.tx_busy_cnt = 0UL;
     497           0 :   ctx->metrics.tx_idle_cnt = fd_seq_diff( ctx->free_tx.prod, ctx->free_tx.cons );
     498           0 :   for( uint j=0U; j<ctx->xsk_cnt; j++ ) {
     499           0 :     fd_xsk_t * xsk = &ctx->xsk[ j ];
     500           0 :     FD_COMPILER_MFENCE();
     501             :     /* Write back local copies to fseqs that we own */
     502           0 :     FD_VOLATILE( *xsk->ring_fr.prod ) = xsk->ring_fr.cached_prod;
     503           0 :     FD_VOLATILE( *xsk->ring_rx.cons ) = xsk->ring_rx.cached_cons;
     504           0 :     FD_VOLATILE( *xsk->ring_tx.prod ) = xsk->ring_tx.cached_prod;
     505           0 :     FD_VOLATILE( *xsk->ring_cr.cons ) = xsk->ring_cr.cached_cons;
     506             : 
     507             :     /* Refresh kernel-owned seq numbers for accurate stats */
     508           0 :     xsk->ring_fr.cached_cons = FD_VOLATILE_CONST( *xsk->ring_fr.cons );
     509           0 :     xsk->ring_rx.cached_prod = FD_VOLATILE_CONST( *xsk->ring_rx.prod );
     510           0 :     xsk->ring_tx.cached_cons = FD_VOLATILE_CONST( *xsk->ring_tx.cons );
     511           0 :     xsk->ring_cr.cached_prod = FD_VOLATILE_CONST( *xsk->ring_cr.prod );
     512             : 
     513           0 :     FD_COMPILER_MFENCE();
     514           0 :     ctx->metrics.rx_busy_cnt += (long)(int)( xsk->ring_rx.cached_prod - xsk->ring_rx.cached_cons );
     515           0 :     ctx->metrics.rx_idle_cnt += (long)(int)( xsk->ring_fr.cached_prod - xsk->ring_fr.cached_cons );
     516           0 :     ctx->metrics.tx_busy_cnt += (long)(int)( xsk->ring_tx.cached_prod - xsk->ring_tx.cached_cons );
     517           0 :     ctx->metrics.tx_busy_cnt += (long)(int)( xsk->ring_cr.cached_prod - xsk->ring_cr.cached_cons );
     518           0 :   }
     519             : 
     520           0 :   if( now > ctx->next_xdp_stats_refresh ) {
     521           0 :     ctx->next_xdp_stats_refresh = now + ctx->xdp_stats_interval_ticks;
     522           0 :     poll_xdp_statistics( ctx );
     523           0 :   }
     524           0 : }
     525             : 
     526             : 
     527             : /* net_tx_route resolves the xsk index, src ip address, src MAC address, and
     528             :    dst MAC address.  Returns 1 on success, 0 on failure.
     529             :    On success, tx_op->{xsk_idx,src_ip,mac_addrs} is set, and if the dst_ip
     530             :    belongs to a GRE interface, is_gre_inf will set to 1 and
     531             :    tx_op->{gre_outer_src_ip, gre_outer_dst_ip} will be loaded from the netdev
     532             :    table. is_gre_inf is set to 0 if dst_ip doesn't belong to a GRE interface. */
     533             : 
     534             : static int
     535             : net_tx_route( fd_net_ctx_t * ctx,
     536             :               uint           dst_ip,
     537           0 :               uint *         is_gre_inf ) {
     538             : 
     539             :   /* Route lookup */
     540             : 
     541           0 :   fd_fib4_hop_t hop[2] = {0};
     542           0 :   hop[0] = fd_fib4_lookup( ctx->fib_local, dst_ip, 0UL );
     543           0 :   hop[1] = fd_fib4_lookup( ctx->fib_main,  dst_ip, 0UL );
     544           0 :   fd_fib4_hop_t const * next_hop = fd_fib4_hop_or( hop+0, hop+1 );
     545             : 
     546           0 :   uint rtype   = next_hop->rtype;
     547           0 :   uint if_idx  = next_hop->if_idx;
     548           0 :   uint ip4_src = next_hop->ip4_src;
     549             : 
     550           0 :   if( FD_UNLIKELY( rtype==FD_FIB4_RTYPE_LOCAL ) ) {
     551           0 :     rtype  = FD_FIB4_RTYPE_UNICAST;
     552           0 :     if_idx = 1;
     553           0 :   }
     554             : 
     555           0 :   if( FD_UNLIKELY( rtype!=FD_FIB4_RTYPE_UNICAST ) ) {
     556           0 :     ctx->metrics.tx_route_fail_cnt++;
     557           0 :     return 0;
     558           0 :   }
     559             : 
     560           0 :   fd_netdev_t * netdev = fd_netdev_tbl_query( &ctx->netdev_tbl, if_idx );
     561           0 :   if( !netdev ) {
     562           0 :     ctx->metrics.tx_route_fail_cnt++;
     563           0 :     return 0;
     564           0 :   }
     565             : 
     566           0 :   ip4_src = fd_uint_if( !!ctx->bind_address, ctx->bind_address, ip4_src );
     567           0 :   ctx->tx_op.src_ip  = ip4_src;
     568           0 :   ctx->tx_op.xsk_idx = UINT_MAX;
     569             : 
     570           0 :   FD_TEST( is_gre_inf );
     571           0 :   *is_gre_inf = 0;
     572           0 :   if( netdev->dev_type==ARPHRD_LOOPBACK ) {
     573             :     /* Set Ethernet src and dst address to 00:00:00:00:00:00 */
     574           0 :     memset( ctx->tx_op.mac_addrs, 0, 12UL );
     575           0 :     ctx->tx_op.xsk_idx = XSK_IDX_LO;
     576             :     /* Set preferred src address to 127.0.0.1 if no bind address is set */
     577           0 :     if( !ctx->tx_op.src_ip ) ctx->tx_op.src_ip = FD_IP4_ADDR( 127,0,0,1 );
     578           0 :     return 1;
     579           0 :   } else if( netdev->dev_type==ARPHRD_IPGRE ) {
     580             :     /* skip MAC addrs lookup for GRE inner dst ip */
     581           0 :     if( netdev->gre_src_ip ) ctx->tx_op.gre_outer_src_ip = netdev->gre_src_ip;
     582           0 :     ctx->tx_op.gre_outer_dst_ip = netdev->gre_dst_ip;
     583           0 :     *is_gre_inf = 1;
     584           0 :     return 1;
     585           0 :   }
     586             : 
     587           0 :   if( FD_UNLIKELY( netdev->dev_type!=ARPHRD_ETHER ) ) return 0; // drop
     588             : 
     589           0 :   if( FD_UNLIKELY( if_idx!=ctx->if_virt ) ) {
     590           0 :     ctx->metrics.tx_no_xdp_cnt++;
     591           0 :     return 0;
     592           0 :   }
     593           0 :   ctx->tx_op.xsk_idx = XSK_IDX_MAIN;
     594             : 
     595             :   /* Neighbor resolve */
     596           0 :   uint neigh_ip = next_hop->ip4_gw;
     597           0 :   if( !neigh_ip ) neigh_ip = dst_ip;
     598             : 
     599           0 :   fd_neigh4_entry_t neigh[1];
     600           0 :   int neigh_res = fd_neigh4_hmap_query_entry( ctx->neigh4, neigh_ip, neigh );
     601           0 :   if( FD_UNLIKELY( neigh_res!=FD_MAP_SUCCESS ) ) {
     602             :     /* Neighbor not found */
     603           0 :     fd_netlink_neigh4_solicit( ctx->neigh4_solicit, neigh_ip, if_idx, fd_frag_meta_ts_comp( fd_tickcount() ) );
     604           0 :     ctx->metrics.tx_neigh_fail_cnt++;
     605           0 :     return 0;
     606           0 :   }
     607           0 :   if( FD_UNLIKELY( neigh->state != FD_NEIGH4_STATE_ACTIVE ) ) {
     608           0 :     ctx->metrics.tx_neigh_fail_cnt++;
     609           0 :     return 0;
     610           0 :   }
     611           0 :   ip4_src = fd_uint_if( !ip4_src, ctx->default_address, ip4_src );
     612           0 :   ctx->tx_op.src_ip = ip4_src;
     613           0 :   memcpy( ctx->tx_op.mac_addrs+0, neigh->mac_addr, 6 );
     614           0 :   memcpy( ctx->tx_op.mac_addrs+6, netdev->mac_addr,  6 );
     615             : 
     616           0 :   return 1;
     617           0 : }
     618             : 
     619             : /* before_frag is called when a new metadata descriptor for a TX job is
     620             :    found.  This callback determines whether this net tile is responsible
     621             :    for the TX job.  If so, it prepares the TX op for the during_frag and
     622             :    after_frag callbacks. */
     623             : 
     624             : static inline int
     625             : before_frag( fd_net_ctx_t * ctx,
     626             :              ulong          in_idx,
     627             :              ulong          seq,
     628           0 :              ulong          sig ) {
     629           0 :   (void)in_idx; (void)seq;
     630             : 
     631             :   /* Find interface index of next packet */
     632           0 :   ulong proto = fd_disco_netmux_sig_proto( sig );
     633           0 :   if( FD_UNLIKELY( proto!=DST_PROTO_OUTGOING ) ) return 1;
     634             : 
     635             :   /* Load balance TX */
     636           0 :   uint net_tile_cnt = ctx->net_tile_cnt;
     637           0 :   uint hash         = (uint)fd_disco_netmux_sig_hash( sig );
     638           0 :   uint target_idx   = hash % net_tile_cnt;
     639           0 :   uint net_tile_id  = ctx->net_tile_id;
     640           0 :   uint dst_ip       = fd_disco_netmux_sig_ip( sig );
     641             : 
     642             :   /* Skip if another net tile is responsible for this packet.
     643             :      Fast path for net tiles other than net_tile 0. */
     644             : 
     645           0 :   if( net_tile_id!=0 && net_tile_id!=target_idx ) return 1; /* ignore */
     646             : 
     647             : 
     648           0 :   ctx->tx_op.use_gre          = 0;
     649           0 :   ctx->tx_op.gre_outer_dst_ip = 0;
     650           0 :   ctx->tx_op.gre_outer_src_ip = 0;
     651           0 :   uint is_gre_inf             = 0;
     652             : 
     653           0 :   if( FD_UNLIKELY( !net_tx_route( ctx, dst_ip, &is_gre_inf ) ) ) {
     654           0 :     return 1; /* metrics incremented by net_tx_route */
     655           0 :   }
     656             : 
     657           0 :   uint xsk_idx     = ctx->tx_op.xsk_idx;
     658             : 
     659           0 :   if( is_gre_inf ) {
     660           0 :     uint inner_src_ip = ctx->tx_op.src_ip;
     661           0 :     if( FD_UNLIKELY( !inner_src_ip ) ) {
     662           0 :       ctx->metrics.tx_gre_route_fail_cnt++;
     663           0 :       return 1;
     664           0 :     }
     665             :     /* Find the MAC addrs for the eth hdr, and src ip for outer ip4 hdr if not found in netdev tbl */
     666           0 :     ctx->tx_op.src_ip  = 0;
     667           0 :     is_gre_inf         = 0;
     668           0 :     if( FD_UNLIKELY( !net_tx_route( ctx, ctx->tx_op.gre_outer_dst_ip, &is_gre_inf ) ) ) {
     669           0 :       ctx->metrics.tx_gre_route_fail_cnt++;
     670           0 :       return 1;
     671           0 :     }
     672           0 :     if( is_gre_inf ) {
     673             :       /* Only one layer of tunnelling supported */
     674           0 :       ctx->metrics.tx_gre_route_fail_cnt++;
     675           0 :       return 1;
     676           0 :     }
     677           0 :     if( !ctx->tx_op.gre_outer_src_ip ) {
     678           0 :       ctx->tx_op.gre_outer_src_ip = ctx->tx_op.src_ip;
     679           0 :     }
     680           0 :     ctx->tx_op.use_gre = 1; /* indicate to during_frag to use GRE header */
     681           0 :     ctx->tx_op.src_ip  = inner_src_ip;
     682           0 :     xsk_idx = XSK_IDX_MAIN;
     683           0 :   }
     684             : 
     685           0 :   if( FD_UNLIKELY( xsk_idx>=ctx->xsk_cnt ) ) {
     686             :     /* Packet does not route to an XDP interface */
     687           0 :     ctx->metrics.tx_no_xdp_cnt++;
     688           0 :     return 1;
     689           0 :   }
     690             : 
     691           0 :   if( xsk_idx==XSK_IDX_LO ) target_idx = 0; /* loopback always targets tile 0 */
     692             : 
     693             :   /* Skip if another net tile is responsible for this packet */
     694             : 
     695           0 :   if( net_tile_id!=target_idx ) return 1; /* ignore */
     696             : 
     697             :   /* Skip if TX is blocked */
     698             : 
     699           0 :   fd_xsk_t *           xsk  = &ctx->xsk[ xsk_idx ];
     700           0 :   fd_net_free_ring_t * free = &ctx->free_tx;
     701           0 :   if( FD_UNLIKELY( !net_tx_ready( &xsk->ring_tx, free ) ) ) {
     702           0 :     ctx->metrics.tx_full_fail_cnt++;
     703           0 :     return 1;
     704           0 :   }
     705             : 
     706             :   /* Allocate buffer for receive */
     707           0 :   ulong                alloc_seq = free->cons;
     708           0 :   void *               frame     = (void *)free->queue[ alloc_seq % free->depth ];
     709           0 :   free->cons = fd_seq_inc( alloc_seq, 1UL );
     710             : 
     711           0 :   ctx->tx_op.frame = frame;
     712             : 
     713           0 :   return 0; /* continue */
     714           0 : }
     715             : 
     716             : /* during_frag is called when before_frag has committed to transmit an
     717             :    outgoing packet. */
     718             : 
     719             : static inline void
     720             : during_frag( fd_net_ctx_t * ctx,
     721             :              ulong          in_idx,
     722             :              ulong          seq FD_PARAM_UNUSED,
     723             :              ulong          sig FD_PARAM_UNUSED,
     724             :              ulong          chunk,
     725             :              ulong          sz,
     726           0 :              ulong          ctl FD_PARAM_UNUSED ) {
     727           0 :   if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>FD_NET_MTU ) )
     728           0 :     FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
     729             : 
     730           0 :   if( FD_UNLIKELY( sz<( sizeof(fd_eth_hdr_t)+sizeof(fd_ip4_hdr_t) ) ) )
     731           0 :     FD_LOG_ERR(( "packet too small %lu (in_idx=%lu)", sz, in_idx ));
     732             : 
     733           0 :   if( FD_UNLIKELY( sz>FD_ETH_PAYLOAD_MAX ) )
     734           0 :     FD_LOG_ERR(( "packet too big %lu (in_idx=%lu)", sz, in_idx ));
     735             : 
     736           0 :   void * frame = ctx->tx_op.frame;
     737           0 :   if( FD_UNLIKELY( (ulong)frame < (ulong)ctx->umem ) )
     738           0 :     FD_LOG_ERR(( "frame %p out of bounds (below %p)", frame, (void *)ctx->umem ));
     739           0 :   ulong umem_off = (ulong)frame - (ulong)ctx->umem;
     740           0 :   if( FD_UNLIKELY( (ulong)umem_off > (ulong)ctx->umem_sz ) )
     741           0 :     FD_LOG_ERR(( "frame %p out of bounds (beyond %p)", frame, (void *)ctx->umem_sz ));
     742             : 
     743             :   /* Speculatively copy frame into XDP buffer */
     744           0 :   uchar const * src = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
     745             : 
     746           0 :   if( ctx->tx_op.use_gre ) {
     747             :     /* Discard the ethernet hdr from src. Copy the rest to where the inner ip4_hdr is.
     748             :        Safe from overflow: FD_ETH_PAYLOAD_MAX + header overhead < frame size (2048UL) */
     749           0 :     ulong overhead = sizeof(fd_eth_hdr_t) + sizeof(fd_ip4_hdr_t) + sizeof(fd_gre_hdr_t);
     750           0 :     fd_memcpy( (void *)( (ulong)ctx->tx_op.frame + overhead ), src + sizeof(fd_eth_hdr_t), sz - sizeof(fd_eth_hdr_t) );
     751           0 :   } else {
     752           0 :     fd_memcpy( ctx->tx_op.frame, src, sz );
     753           0 :   }
     754           0 : }
     755             : 
     756             : /* after_frag is called when the during_frag memcpy was _not_ overrun. */
     757             : 
     758             : static void
     759             : after_frag( fd_net_ctx_t *      ctx,
     760             :             ulong               in_idx,
     761             :             ulong               seq,
     762             :             ulong               sig,
     763             :             ulong               sz,
     764             :             ulong               tsorig,
     765             :             ulong               tspub,
     766           0 :             fd_stem_context_t * stem ) {
     767           0 :   (void)in_idx; (void)seq; (void)sig; (void)tsorig; (void)tspub; (void)stem;
     768             : 
     769             :   /* Current send operation */
     770             : 
     771           0 :   uchar *    frame   = ctx->tx_op.frame;
     772           0 :   uint       xsk_idx = ctx->tx_op.xsk_idx;
     773             : 
     774             :   /* Select Ethernet addresses */
     775           0 :   memcpy( frame, ctx->tx_op.mac_addrs, 12 );
     776             : 
     777           0 :   uchar * iphdr = frame + sizeof(fd_eth_hdr_t);
     778             : 
     779           0 :   if( ctx->tx_op.use_gre ) {
     780             : 
     781             :     /* For GRE packets, the ethertype will always be FD_ETH_HDR_TYPE_IP. outer source ip can't be 0 */
     782           0 :     if( FD_UNLIKELY( ctx->tx_op.gre_outer_src_ip==0 ) ) {
     783           0 :       ctx->metrics.tx_gre_route_fail_cnt++;
     784           0 :       return;
     785           0 :     }
     786             : 
     787             :     /* Write the last two bytes for eth_hdr */
     788           0 :     FD_STORE( ushort, frame+12, fd_ushort_bswap( FD_ETH_HDR_TYPE_IP ) );
     789             : 
     790           0 :     uchar * outer_iphdr       = frame + sizeof(fd_eth_hdr_t);
     791           0 :     uchar * gre_hdr           = outer_iphdr + sizeof(fd_ip4_hdr_t);
     792           0 :     uchar * inner_iphdr       = gre_hdr + sizeof(fd_gre_hdr_t);
     793             : 
     794             :     /* outer hdr + gre hdr + inner net_tot_len */
     795           0 :     ushort  outer_net_tot_len = (ushort)( sizeof(fd_ip4_hdr_t) + sizeof(fd_gre_hdr_t) + fd_ushort_bswap( ( (fd_ip4_hdr_t *)inner_iphdr )->net_tot_len ) );
     796             : 
     797             :     /* Construct outer ip header */
     798           0 :     fd_ip4_hdr_t ip4_outer = (fd_ip4_hdr_t) {
     799           0 :       .verihl       = FD_IP4_VERIHL( 4,5 ),
     800           0 :       .tos          = 0,
     801           0 :       .net_tot_len  = fd_ushort_bswap( outer_net_tot_len ),
     802           0 :       .net_id       = 0,
     803           0 :       .net_frag_off = fd_ushort_bswap( FD_IP4_HDR_FRAG_OFF_DF ),
     804           0 :       .ttl          = 64,
     805           0 :       .protocol     = FD_IP4_HDR_PROTOCOL_GRE,
     806           0 :       .check        = 0,
     807           0 :       .saddr        = ctx->tx_op.gre_outer_src_ip,
     808           0 :       .daddr        = ctx->tx_op.gre_outer_dst_ip,
     809           0 :     };
     810           0 :     ip4_outer.check = fd_ip4_hdr_check_fast( &ip4_outer );
     811           0 :     FD_STORE( fd_ip4_hdr_t, outer_iphdr, ip4_outer );
     812             : 
     813             :     /* Construct gre header */
     814           0 :     fd_gre_hdr_t gre_hdr_ = {
     815           0 :       .flags_version = FD_GRE_HDR_FLG_VER_BASIC,
     816           0 :       .protocol      = fd_ushort_bswap( FD_ETH_HDR_TYPE_IP )
     817           0 :     };
     818           0 :     FD_STORE( fd_gre_hdr_t, gre_hdr, gre_hdr_ );
     819             : 
     820           0 :     iphdr   = inner_iphdr;
     821           0 :     sz      = sizeof(fd_eth_hdr_t) + outer_net_tot_len;
     822           0 :     xsk_idx = 0;
     823           0 :   }
     824             : 
     825             :   /* Construct (inner) ip header */
     826           0 :   uint   ihl       = FD_IP4_GET_LEN( *(fd_ip4_hdr_t *)iphdr );
     827           0 :   uint   ver       = FD_IP4_GET_VERSION( *(fd_ip4_hdr_t *)iphdr );
     828           0 :   uint   ip4_saddr = FD_LOAD( uint, iphdr+12 );
     829           0 :   ushort ethertype = FD_LOAD( ushort, frame+12 );
     830             : 
     831           0 :   if( FD_UNLIKELY( ethertype!=fd_ushort_bswap( FD_ETH_HDR_TYPE_IP ) ) ) {
     832           0 :     FD_LOG_CRIT(( "in link %lu attempted to send packet with invalid ethertype %04x",
     833           0 :                   in_idx, fd_ushort_bswap( ethertype ) ));
     834           0 :   }
     835             : 
     836           0 :   if( ver!=0x4 ) {
     837           0 :     ctx->metrics.tx_route_fail_cnt++; // Not an IPv4 packet. drop
     838           0 :     return;
     839           0 :   }
     840             : 
     841           0 :   if( ip4_saddr==0 ) {
     842           0 :     if( FD_UNLIKELY( ctx->tx_op.src_ip==0 ||
     843           0 :                      ihl<sizeof(fd_ip4_hdr_t) ||
     844           0 :                      (sizeof(fd_eth_hdr_t)+ihl)>sz ) ) {
     845             :       /* Outgoing IPv4 packet with unknown src IP or invalid IHL */
     846             :       /* FIXME should select first IPv4 address of device table here */
     847           0 :       ctx->metrics.tx_route_fail_cnt++;
     848           0 :       return;
     849           0 :     }
     850             :     /* Recompute checksum after changing header */
     851           0 :     FD_STORE( uint,   iphdr+12, ctx->tx_op.src_ip );
     852           0 :     FD_STORE( ushort, iphdr+10, 0 );
     853           0 :     FD_STORE( ushort, iphdr+10, fd_ip4_hdr_check( iphdr ) );
     854           0 :   }
     855             : 
     856             :   /* Submit packet TX job
     857             : 
     858             :      Invariant for ring_tx: prod-cons<length
     859             :      (This invariant breaks if any other packet is sent over this ring
     860             :      between before_frag and this point, e.g. send_arp_probe.) */
     861             : 
     862           0 :   fd_xsk_t      * xsk     = &ctx->xsk[ xsk_idx ];
     863           0 :   fd_xdp_ring_t * tx_ring = &xsk->ring_tx;
     864           0 :   uint            tx_seq  = tx_ring->cached_prod;
     865           0 :   uint            tx_mask = tx_ring->depth - 1U;
     866           0 :   xsk->ring_tx.packet_ring[ tx_seq&tx_mask ] = (struct xdp_desc) {
     867           0 :     .addr    = (ulong)frame - (ulong)ctx->umem,
     868           0 :     .len     = (uint)sz,
     869           0 :     .options = 0
     870           0 :   };
     871             : 
     872             :   /* Frame is now owned by kernel. Clear tx_op. */
     873           0 :   ctx->tx_op.frame = NULL;
     874             : 
     875             :   /* Register newly enqueued packet */
     876           0 :   tx_ring->cached_prod = tx_seq+1U;
     877           0 :   ctx->metrics.tx_submit_cnt++;
     878           0 :   ctx->metrics.tx_bytes_total += sz;
     879           0 :   if( ctx->tx_op.use_gre ) ctx->metrics.tx_gre_cnt++;
     880           0 :   fd_net_flusher_inc( ctx->tx_flusher+xsk_idx, fd_tickcount() );
     881           0 : }
     882             : 
     883             : /* net_rx_packet is called when a new Ethernet frame is available.
     884             :    Attempts to copy out the frame to a downstream tile. */
     885             : 
     886             : static void
     887             : net_rx_packet( fd_net_ctx_t * ctx,
     888             :                ulong          umem_off,
     889             :                ulong          sz,
     890           0 :                uint *         freed_chunk ) {
     891             : 
     892           0 :   if( FD_UNLIKELY( sz<sizeof(fd_eth_hdr_t)+sizeof(fd_ip4_hdr_t)+sizeof(fd_udp_hdr_t) ) ) {
     893           0 :     FD_DTRACE_PROBE( net_tile_err_rx_undersz );
     894           0 :     ctx->metrics.rx_undersz_cnt++;
     895           0 :     return;
     896           0 :   }
     897             : 
     898           0 :   uchar        * packet     = (uchar *)ctx->umem + umem_off;
     899           0 :   uchar const  * packet_end = packet + sz;
     900           0 :   fd_ip4_hdr_t * iphdr      = (fd_ip4_hdr_t *)(packet + sizeof(fd_eth_hdr_t));
     901             : 
     902           0 :   if( FD_UNLIKELY( ((fd_eth_hdr_t *)packet)->net_type!=fd_ushort_bswap( FD_ETH_HDR_TYPE_IP ) ) ) return;
     903             : 
     904           0 :   int is_packet_gre = 0;
     905             :   /* Discard the GRE overhead (outer iphdr and gre hdr) */
     906           0 :   if( iphdr->protocol == FD_IP4_HDR_PROTOCOL_GRE ) {
     907           0 :     if( FD_UNLIKELY( ctx->has_gre_interface==0 ) ) {
     908           0 :       ctx->metrics.rx_gre_ignored_cnt++; // drop. No gre interface in netdev table
     909           0 :       return;
     910           0 :     }
     911           0 :     ulong gre_ipver = FD_IP4_GET_VERSION( *iphdr );
     912           0 :     ulong gre_iplen = FD_IP4_GET_LEN( *iphdr );
     913           0 :     if( FD_UNLIKELY( gre_ipver!=0x4 || gre_iplen<20 ) ) {
     914           0 :       FD_DTRACE_PROBE( net_tile_err_rx_noip );
     915           0 :       ctx->metrics.rx_gre_inv_pkt_cnt++; /* drop IPv6 packets */
     916           0 :       return;
     917           0 :     }
     918             : 
     919           0 :     ulong overhead = gre_iplen + sizeof(fd_gre_hdr_t);
     920           0 :     if( FD_UNLIKELY( (uchar *)iphdr+overhead+sizeof(fd_ip4_hdr_t)>packet_end ) ) {
     921           0 :       FD_DTRACE_PROBE( net_tile_err_rx_undersz );
     922           0 :       ctx->metrics.rx_undersz_cnt++;  // inner ip4 header invalid
     923           0 :       return;
     924           0 :     }
     925             : 
     926             :     /* The new iphdr is where the inner iphdr was. Copy over the eth_hdr */
     927           0 :     iphdr              = (fd_ip4_hdr_t *)((uchar *)iphdr + overhead);
     928           0 :     uchar * new_packet = (uchar *)iphdr - sizeof(fd_eth_hdr_t);
     929           0 :     fd_memcpy( new_packet, packet, sizeof(fd_eth_hdr_t) );
     930           0 :     sz                 -= overhead;
     931           0 :     packet             = new_packet;
     932           0 :     umem_off           = (ulong)( packet - (uchar *)ctx->umem );
     933           0 :     is_packet_gre      = 1;
     934           0 :   }
     935             : 
     936             :   /* Translate packet to UMEM frame index */
     937           0 :   ulong chunk       = ctx->umem_chunk0 + (umem_off>>FD_CHUNK_LG_SZ);
     938           0 :   ulong ctl         = umem_off & 0x3fUL;
     939             : 
     940             :   /* Filter for UDP/IPv4 packets. */
     941           0 :   ulong ipver = FD_IP4_GET_VERSION( *iphdr );
     942           0 :   ulong iplen = FD_IP4_GET_LEN    ( *iphdr );
     943           0 :   if( FD_UNLIKELY( ipver!=0x4 || iplen<20 ||
     944           0 :                    iphdr->protocol!=FD_IP4_HDR_PROTOCOL_UDP ) ) {
     945           0 :     FD_DTRACE_PROBE( net_tile_err_rx_noip );
     946           0 :     ctx->metrics.rx_undersz_cnt++; /* drop IPv6 packets */
     947           0 :     return;
     948           0 :   }
     949             : 
     950           0 :   uchar const * udp = (uchar *)iphdr + iplen;
     951           0 :   if( FD_UNLIKELY( udp+sizeof(fd_udp_hdr_t) > packet_end ) ) {
     952           0 :     FD_DTRACE_PROBE( net_tile_err_rx_undersz );
     953           0 :     ctx->metrics.rx_undersz_cnt++;
     954           0 :     return;
     955           0 :   }
     956             : 
     957           0 :   fd_udp_hdr_t const * udp_hdr = (fd_udp_hdr_t const *)udp;
     958           0 :   ulong        const   udp_sz  = fd_ushort_bswap( udp_hdr->net_len );
     959           0 :   if( FD_UNLIKELY( (udp_sz<sizeof(fd_udp_hdr_t)) | (udp+udp_sz>packet_end) ) ) {
     960           0 :     FD_DTRACE_PROBE( net_tile_err_rx_undersz );
     961           0 :     ctx->metrics.rx_undersz_cnt++;
     962           0 :     return;
     963           0 :   }
     964             : 
     965             :   /* Extract IP dest addr and UDP src/dest port */
     966           0 :   uint   ip_srcaddr   =  iphdr->saddr;
     967           0 :   ushort udp_srcport  =  fd_ushort_bswap( udp_hdr->net_sport );
     968           0 :   ushort udp_dstport  =  fd_ushort_bswap( udp_hdr->net_dport );
     969             : 
     970           0 :   if( FD_UNLIKELY( fd_ip4_addr_is_mcast( ip_srcaddr ) ) ) {
     971           0 :     ctx->metrics.rx_src_addr_invalid_cnt++;
     972           0 :     return;
     973           0 :   }
     974             : 
     975           0 :   FD_DTRACE_PROBE_4( net_tile_pkt_rx, ip_srcaddr, udp_srcport, udp_dstport, sz );
     976             : 
     977             :   /* Route packet to downstream tile */
     978           0 :   ushort proto;
     979           0 :   fd_net_out_ctx_t * out;
     980           0 :   if(      FD_UNLIKELY( udp_dstport==ctx->shred_listen_port ) ) {
     981           0 :     proto = DST_PROTO_SHRED;
     982           0 :     out = ctx->shred_out;
     983           0 :   } else if( FD_UNLIKELY( udp_dstport==ctx->quic_transaction_listen_port ) ) {
     984           0 :     proto = DST_PROTO_TPU_QUIC;
     985           0 :     out = ctx->quic_out;
     986           0 :   } else if( FD_UNLIKELY( udp_dstport==ctx->legacy_transaction_listen_port ) ) {
     987           0 :     proto = DST_PROTO_TPU_UDP;
     988           0 :     out = ctx->quic_out;
     989           0 :   } else if( FD_UNLIKELY( udp_dstport==ctx->gossip_listen_port ) ) {
     990           0 :     proto = DST_PROTO_GOSSIP;
     991           0 :     out = ctx->gossvf_out;
     992           0 :   } else if( FD_UNLIKELY( udp_dstport==ctx->repair_intake_listen_port ) ) {
     993           0 :     proto = DST_PROTO_REPAIR;
     994           0 :     if( FD_UNLIKELY( sz == REPAIR_PING_SZ ) ) out = ctx->repair_out; /* ping-pong */
     995           0 :     else                                      out = ctx->shred_out;
     996           0 :   } else if( FD_UNLIKELY( udp_dstport==ctx->repair_serve_listen_port ) ) {
     997           0 :     proto = DST_PROTO_REPAIR;
     998           0 :     out = ctx->repair_out;
     999           0 :   } else if( FD_UNLIKELY( udp_dstport==ctx->txsend_src_port ) ) {
    1000           0 :     proto = DST_PROTO_SEND;
    1001           0 :     out = ctx->txsend_out;
    1002           0 :   } else {
    1003             : 
    1004           0 :     FD_LOG_ERR(( "Firedancer received a UDP packet on port %hu which was not expected. "
    1005           0 :                   "Only the following ports should be configured to forward packets: "
    1006           0 :                   "%hu, %hu, %hu, %hu, %hu, %hu (excluding any 0 ports, which can be ignored)."
    1007           0 :                   "Please report this error to Firedancer maintainers.",
    1008           0 :                   udp_dstport,
    1009           0 :                   ctx->shred_listen_port,
    1010           0 :                   ctx->quic_transaction_listen_port,
    1011           0 :                   ctx->legacy_transaction_listen_port,
    1012           0 :                   ctx->gossip_listen_port,
    1013           0 :                   ctx->repair_intake_listen_port,
    1014           0 :                   ctx->repair_serve_listen_port ));
    1015           0 :   }
    1016             : 
    1017             :   /* tile can decide how to partition based on src ip addr and src port */
    1018           0 :   ulong sig              = fd_disco_netmux_sig( ip_srcaddr, udp_srcport, ip_srcaddr, proto, 14UL+8UL+iplen );
    1019             : 
    1020             :   /* Peek the mline for an old frame */
    1021           0 :   fd_frag_meta_t * mline = out->mcache + fd_mcache_line_idx( out->seq, out->depth );
    1022           0 :   *freed_chunk           = mline->chunk;
    1023             : 
    1024             :   /* Overwrite the mline with the new frame */
    1025           0 :   ulong tspub            = (ulong)fd_frag_meta_ts_comp( fd_tickcount() );
    1026           0 :   fd_mcache_publish( out->mcache, out->depth, out->seq, sig, chunk, sz, ctl, 0, tspub );
    1027             : 
    1028             :   /* Wind up for the next iteration */
    1029           0 :   out->seq               = fd_seq_inc( out->seq, 1UL );
    1030             : 
    1031           0 :   if( is_packet_gre ) ctx->metrics.rx_gre_cnt++;
    1032           0 :   ctx->metrics.rx_pkt_cnt++;
    1033           0 :   ctx->metrics.rx_bytes_total += sz;
    1034           0 : }
    1035             : 
    1036             : /* net_comp_event is called when an XDP TX frame is free again. */
    1037             : 
    1038             : static void
    1039             : net_comp_event( fd_net_ctx_t * ctx,
    1040             :                 fd_xsk_t *     xsk,
    1041           0 :                 uint           comp_seq ) {
    1042             : 
    1043             :   /* Locate the incoming frame */
    1044             : 
    1045           0 :   fd_xdp_ring_t * comp_ring  = &xsk->ring_cr;
    1046           0 :   uint            comp_mask  = comp_ring->depth - 1U;
    1047           0 :   ulong           frame      = FD_VOLATILE_CONST( comp_ring->frame_ring[ comp_seq&comp_mask ] );
    1048           0 :   ulong const     frame_mask = FD_NET_MTU - 1UL;
    1049           0 :   FD_STATIC_ASSERT( FD_ULONG_IS_POW2( FD_NET_MTU ), "FD_NET_MTU must be a power of two" );
    1050           0 :   if( FD_UNLIKELY( frame+FD_NET_MTU > ctx->umem_sz ) ) {
    1051           0 :     FD_LOG_ERR(( "Bounds check failed: frame=0x%lx umem_sz=0x%lx",
    1052           0 :                  frame, (ulong)ctx->umem_sz ));
    1053           0 :   }
    1054             : 
    1055             :   /* Check if we have space to return the freed frame */
    1056             : 
    1057           0 :   fd_net_free_ring_t * free      = &ctx->free_tx;
    1058           0 :   ulong                free_prod = free->prod;
    1059           0 :   ulong                free_mask = free->depth - 1UL;
    1060           0 :   ulong                free_cons = free->cons;
    1061           0 :   long                 free_cnt = fd_seq_diff( free_prod, free_cons );
    1062           0 :   FD_TEST( free_prod >= free_cons );
    1063           0 :   if( FD_UNLIKELY( free_cnt>=(long)free->depth ) ) return; /* blocked */
    1064             : 
    1065           0 :   free->queue[ free_prod&free_mask ] = (ulong)ctx->umem + (frame & (~frame_mask));
    1066           0 :   free->prod = fd_seq_inc( free_prod, 1UL );
    1067             : 
    1068             :   /* Wind up for next iteration */
    1069             : 
    1070           0 :   comp_ring->cached_cons = comp_seq+1U;
    1071           0 :   ctx->metrics.tx_complete_cnt++;
    1072           0 : }
    1073             : 
    1074             : /* net_rx_event is called when a new XDP RX frame is available.  Calls
    1075             :    net_rx_packet, then returns the packet back to the kernel via the fill
    1076             :    ring.  */
    1077             : 
    1078             : static void
    1079             : net_rx_event( fd_net_ctx_t * ctx,
    1080             :               fd_xsk_t *     xsk,
    1081           0 :               uint           rx_seq ) {
    1082             :   /* Locate the incoming frame */
    1083             : 
    1084           0 :   fd_xdp_ring_t * rx_ring = &xsk->ring_rx;
    1085           0 :   uint            rx_mask = rx_ring->depth - 1U;
    1086           0 :   struct xdp_desc frame   = FD_VOLATILE_CONST( rx_ring->packet_ring[ rx_seq&rx_mask ] );
    1087             : 
    1088           0 :   if( FD_UNLIKELY( frame.len>FD_NET_MTU ) )
    1089           0 :     FD_LOG_ERR(( "received a UDP packet with a too large payload (%u)", frame.len ));
    1090             : 
    1091             :   /* Check if we have space in the fill ring to free the frame */
    1092             : 
    1093           0 :   fd_xdp_ring_t * fill_ring  = &xsk->ring_fr;
    1094           0 :   if( FD_UNLIKELY( fd_xdp_ring_full( fill_ring ) ) ) {
    1095           0 :     ctx->metrics.rx_fill_blocked_cnt++;
    1096           0 :     return; /* blocked */
    1097           0 :   }
    1098             : 
    1099             :   /* Pass it to the receive handler */
    1100             : 
    1101           0 :   uint freed_chunk = (uint)( ctx->umem_chunk0 + (frame.addr>>FD_CHUNK_LG_SZ) );
    1102           0 :   net_rx_packet( ctx, frame.addr, frame.len, &freed_chunk );
    1103           0 :   FD_COMPILER_MFENCE();
    1104           0 :   rx_ring->cached_cons = rx_seq+1U;
    1105             : 
    1106             :   /* Every RX operation returns one frame to the FILL ring.  If the
    1107             :      packet was forwarded to a downstream ring, the newly shadowed frame
    1108             :      is returned.  Otherwise, the frame just received is returned. */
    1109             : 
    1110           0 :   if( FD_UNLIKELY( ( freed_chunk < ctx->umem_chunk0 ) |
    1111           0 :                     ( freed_chunk > ctx->umem_wmark ) ) ) {
    1112           0 :     FD_LOG_CRIT(( "mcache corruption detected: chunk=%u chunk0=%u wmark=%u",
    1113           0 :                   freed_chunk, ctx->umem_chunk0, ctx->umem_wmark ));
    1114           0 :   }
    1115             : 
    1116           0 :   FD_STATIC_ASSERT( FD_ULONG_IS_POW2( FD_NET_MTU ), "FD_NET_MTU must be a power of two" );
    1117           0 :   uint  fill_prod  = fill_ring->cached_prod;
    1118           0 :   uint  fill_mask  = (fill_ring->depth)-1U;
    1119           0 :   ulong frame_mask = FD_NET_MTU - 1UL;
    1120           0 :   ulong freed_off  = (freed_chunk - ctx->umem_chunk0)<<FD_CHUNK_LG_SZ;
    1121           0 :   fill_ring->frame_ring[ fill_prod&fill_mask ] = freed_off & (~frame_mask);
    1122           0 :   fill_ring->cached_prod = fill_prod+1U;
    1123           0 : }
    1124             : 
    1125             : /* before_credit is called every loop iteration. */
    1126             : 
    1127             : static void
    1128             : before_credit( fd_net_ctx_t *      ctx,
    1129             :                fd_stem_context_t * stem,
    1130           0 :                int *               charge_busy ) {
    1131           0 :   (void)stem;
    1132             :   /* A previous send attempt was overrun.  A corrupt copy of the packet was
    1133             :      placed into an XDP frame, but the frame was not yet submitted to the
    1134             :      TX ring.  Return the tx buffer to the free list. */
    1135             : 
    1136           0 :   if( ctx->tx_op.frame ) {
    1137           0 :     *charge_busy = 1;
    1138           0 :     fd_net_free_ring_t * free      = &ctx->free_tx;
    1139           0 :     ulong                alloc_seq = free->prod;
    1140           0 :     free->queue[ alloc_seq % free->depth ] = (ulong)ctx->tx_op.frame;
    1141           0 :     free->prod = fd_seq_inc( alloc_seq, 1UL );
    1142           0 :     ctx->tx_op.frame = NULL;
    1143           0 :   }
    1144             : 
    1145             :   /* Check if new packets are available or if TX frames are free again
    1146             :      (Round-robin through sockets) */
    1147             : 
    1148           0 :   uint       rr_idx = ctx->rr_idx;
    1149           0 :   fd_xsk_t * rr_xsk = &ctx->xsk[ rr_idx ];
    1150             : 
    1151           0 :   net_tx_periodic_wakeup( ctx, rr_idx, fd_tickcount(), charge_busy );
    1152             : 
    1153             :   /* Fire RX event if we have RX desc avail */
    1154           0 :   if( !fd_xdp_ring_empty( &rr_xsk->ring_rx, FD_XDP_RING_ROLE_CONS ) ) {
    1155           0 :     *charge_busy = 1;
    1156           0 :     net_rx_event( ctx, rr_xsk, rr_xsk->ring_rx.cached_cons );
    1157           0 :   } else {
    1158           0 :     net_rx_wakeup( ctx, rr_xsk, charge_busy );
    1159           0 :     ctx->rr_idx++;
    1160           0 :     ctx->rr_idx = fd_uint_if( ctx->rr_idx>=ctx->xsk_cnt, 0, ctx->rr_idx );
    1161           0 :   }
    1162             : 
    1163             :   /* Fire comp event if we have comp desc avail */
    1164           0 :   if( !fd_xdp_ring_empty( &rr_xsk->ring_cr, FD_XDP_RING_ROLE_CONS ) ) {
    1165           0 :     *charge_busy = 1;
    1166           0 :     net_comp_event( ctx, rr_xsk, rr_xsk->ring_cr.cached_cons );
    1167           0 :   }
    1168           0 : }
    1169             : 
    1170             : /* net_xsk_bootstrap assigns UMEM frames to the FILL ring. */
    1171             : 
    1172             : static ulong
    1173             : net_xsk_bootstrap( fd_net_ctx_t * ctx,
    1174             :                    uint           xsk_idx,
    1175           0 :                    ulong          frame_off ) {
    1176           0 :   fd_xsk_t * xsk = &ctx->xsk[ xsk_idx ];
    1177             : 
    1178           0 :   ulong const frame_sz  = FD_NET_MTU;
    1179           0 :   ulong const fr_depth  = ctx->xsk[ xsk_idx ].ring_fr.depth/2UL;
    1180             : 
    1181           0 :   fd_xdp_ring_t * fill      = &xsk->ring_fr;
    1182           0 :   uint            fill_prod = fill->cached_prod;
    1183           0 :   for( ulong j=0UL; j<fr_depth; j++ ) {
    1184           0 :     fill->frame_ring[ j ] = frame_off;
    1185           0 :     frame_off += frame_sz;
    1186           0 :   }
    1187           0 :   FD_VOLATILE( *fill->prod ) = fill->cached_prod = fill_prod + (uint)fr_depth;
    1188             : 
    1189           0 :   return frame_off;
    1190           0 : }
    1191             : 
    1192             : /* FIXME source MAC address from netlnk tile instead */
    1193             : 
    1194             : static void
    1195             : interface_addrs( const char * interface,
    1196             :                  uchar *      mac,
    1197           0 :                  uint *       ip4_addr ) {
    1198           0 :   int fd = socket( AF_INET, SOCK_DGRAM, 0 );
    1199           0 :   struct ifreq ifr;
    1200           0 :   ifr.ifr_addr.sa_family = AF_INET;
    1201             : 
    1202           0 :   strncpy( ifr.ifr_name, interface, IFNAMSIZ );
    1203           0 :   if( FD_UNLIKELY( ioctl( fd, SIOCGIFHWADDR, &ifr ) ) )
    1204           0 :     FD_LOG_ERR(( "could not get MAC address of interface `%s`: (%i-%s)", interface, errno, fd_io_strerror( errno ) ));
    1205           0 :   fd_memcpy( mac, ifr.ifr_hwaddr.sa_data, 6 );
    1206             : 
    1207           0 :   if( FD_UNLIKELY( ioctl( fd, SIOCGIFADDR, &ifr ) ) )
    1208           0 :     FD_LOG_ERR(( "could not get IP address of interface `%s`: (%i-%s)", interface, errno, fd_io_strerror( errno ) ));
    1209           0 :   *ip4_addr = ((struct sockaddr_in *)fd_type_pun( &ifr.ifr_addr ))->sin_addr.s_addr;
    1210             : 
    1211           0 :   if( FD_UNLIKELY( close(fd) ) )
    1212           0 :     FD_LOG_ERR(( "could not close socket (%i-%s)", errno, fd_io_strerror( errno ) ));
    1213           0 : }
    1214             : 
    1215             : /* privileged_init does the following initialization steps:
    1216             : 
    1217             :    - Create an AF_XDP socket
    1218             :    - Map XDP metadata rings
    1219             :    - Register UMEM data region with socket
    1220             :    - Insert AF_XDP socket into xsk_map
    1221             : 
    1222             :    Net tile 0 also runs fd_xdp_install and repeats the above step for
    1223             :    the loopback device.  (Unless the main interface is already loopback)
    1224             : 
    1225             :    Kernel object references:
    1226             : 
    1227             :      BPF_LINK file descriptor
    1228             :       |
    1229             :       +-> XDP program installation on NIC
    1230             :       |    |
    1231             :       |    +-> XDP program <-- BPF_PROG file descriptor (prog_fd)
    1232             :       |
    1233             :       +-> XSKMAP object <-- BPF_MAP file descriptor (xsk_map) */
    1234             : 
    1235             : FD_FN_UNUSED static void
    1236             : privileged_init( fd_topo_t *      topo,
    1237           0 :                  fd_topo_tile_t * tile ) {
    1238           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
    1239             : 
    1240           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
    1241           0 :   fd_net_ctx_t * ctx     = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_net_ctx_t), sizeof(fd_net_ctx_t) );
    1242           0 :   ulong *        free_tx = FD_SCRATCH_ALLOC_APPEND( l, alignof(ulong), tile->xdp.free_ring_depth * sizeof(ulong) );;
    1243             : 
    1244           0 :   fd_memset( ctx, 0, sizeof(fd_net_ctx_t) );
    1245             : 
    1246           0 :   interface_addrs( tile->xdp.if_virt, ctx->src_mac_addr, &ctx->default_address );
    1247           0 :   ctx->if_virt = if_nametoindex( tile->xdp.if_virt ); FD_TEST( ctx->if_virt );
    1248             : 
    1249             :   /* Load up dcache containing UMEM */
    1250             : 
    1251           0 :   void * const dcache_mem          = fd_topo_obj_laddr( topo, tile->net.umem_dcache_obj_id );
    1252           0 :   void * const umem                = fd_dcache_join( dcache_mem );
    1253           0 :   ulong  const umem_dcache_data_sz = fd_dcache_data_sz( umem );
    1254           0 :   ulong  const umem_frame_sz       = 2048UL;
    1255           0 :   ulong  const umem_sz             = fd_ulong_align_dn( umem_dcache_data_sz, umem_frame_sz );
    1256             : 
    1257             :   /* Derive chunk bounds */
    1258             : 
    1259           0 :   void * const umem_base   = fd_wksp_containing( dcache_mem );
    1260           0 :   ulong  const umem_chunk0 = ( (ulong)umem - (ulong)umem_base )>>FD_CHUNK_LG_SZ;
    1261           0 :   ulong  const umem_wmark  = umem_chunk0 + ( ( umem_sz-umem_frame_sz )>>FD_CHUNK_LG_SZ );
    1262             : 
    1263           0 :   if( FD_UNLIKELY( umem_chunk0>UINT_MAX || umem_wmark>UINT_MAX || umem_chunk0>umem_wmark ) ) {
    1264           0 :     FD_LOG_ERR(( "Calculated invalid UMEM bounds [%lu,%lu]", umem_chunk0, umem_wmark ));
    1265           0 :   }
    1266             : 
    1267           0 :   if( FD_UNLIKELY( !umem_base ) ) FD_LOG_ERR(( "UMEM dcache is not in a workspace" ));
    1268             : 
    1269           0 :   ctx->umem        = umem;
    1270           0 :   ctx->umem_sz     = umem_sz;
    1271           0 :   ctx->umem_chunk0 = (uint)umem_chunk0;
    1272           0 :   ctx->umem_wmark  = (uint)umem_wmark;
    1273             : 
    1274           0 :   ctx->free_tx.queue = free_tx;
    1275           0 :   ctx->free_tx.depth = tile->xdp.xdp_tx_queue_size;
    1276             : 
    1277             :   /* Create and install XSKs */
    1278             : 
    1279           0 :   uint if_phys_if_idx = if_nametoindex( tile->xdp.if_phys );
    1280           0 :   if( FD_UNLIKELY( !if_phys_if_idx ) ) FD_LOG_ERR(( "if_nametoindex(%s) failed", tile->xdp.if_phys ));
    1281             : 
    1282           0 :   fd_xsk_params_t params0 = {
    1283           0 :     .if_idx      = if_phys_if_idx,
    1284           0 :     .if_queue_id = tile->xdp.if_queue,
    1285             : 
    1286             :     /* Some kernels produce EOPNOTSUP errors on sendto calls when
    1287             :        starting up without either XDP_ZEROCOPY or XDP_COPY
    1288             :        (e.g. 5.14.0-503.23.1.el9_5 with i40e) */
    1289           0 :     .bind_flags  = tile->xdp.zero_copy ? XDP_ZEROCOPY : XDP_COPY,
    1290             : 
    1291           0 :     .fr_depth  = tile->xdp.xdp_rx_queue_size*2,
    1292           0 :     .rx_depth  = tile->xdp.xdp_rx_queue_size,
    1293           0 :     .cr_depth  = tile->xdp.xdp_tx_queue_size,
    1294           0 :     .tx_depth  = tile->xdp.xdp_tx_queue_size,
    1295             : 
    1296           0 :     .umem_addr = umem,
    1297           0 :     .frame_sz  = umem_frame_sz,
    1298           0 :     .umem_sz   = umem_sz,
    1299             : 
    1300           0 :     .core_dump = tile->xdp.xsk_core_dump,
    1301           0 :   };
    1302             : 
    1303             :   /* Re-derive XDP file descriptors */
    1304             : 
    1305           0 :   fd_xdp_fds_t xdp_fds[ FD_TOPO_XDP_FDS_MAX ];
    1306           0 :   uint         xdp_fds_cnt = FD_TOPO_XDP_FDS_MAX;
    1307           0 :   fd_topo_install_xdp( topo, xdp_fds, &xdp_fds_cnt, 0U, /* dry_run */ 1 );
    1308             : 
    1309           0 :   int xsk_map_fd = -1;
    1310           0 :   for( uint i=0U; i<xdp_fds_cnt; i++ ) {
    1311           0 :     if( xdp_fds[ i ].if_idx==if_phys_if_idx ) {
    1312           0 :       xsk_map_fd              = xdp_fds[ i ].xsk_map_fd;
    1313           0 :       ctx->prog_link_fds[ 0 ] = xdp_fds[ i ].prog_link_fd;
    1314           0 :       xdp_fds[ i ].prog_link_fd = -1; /* mark as used */
    1315           0 :       break;
    1316           0 :     }
    1317           0 :   }
    1318           0 :   FD_TEST( xsk_map_fd>=0 );
    1319             : 
    1320             :   /* Init XSK */
    1321           0 :   if( FD_UNLIKELY( !fd_xsk_init( &ctx->xsk[ 0 ], &params0 ) ) )       FD_LOG_ERR(( "failed to bind xsk for net tile %lu", tile->kind_id ));
    1322           0 :   if( FD_UNLIKELY( !fd_xsk_activate( &ctx->xsk[ 0 ], xsk_map_fd ) ) ) FD_LOG_ERR(( "failed to activate xsk for net tile %lu", tile->kind_id ));
    1323           0 :   ctx->xsk_cnt = 1;
    1324             : 
    1325             :   /* Networking tile at index 0 also binds to loopback (only queue 0 available on lo) */
    1326             : 
    1327           0 :   if( FD_UNLIKELY( strcmp( tile->xdp.if_virt, "lo" ) && !tile->kind_id ) ) {
    1328           0 :     ctx->xsk_cnt = 2;
    1329             : 
    1330           0 :     uint lo_idx = if_nametoindex( "lo" );
    1331           0 :     if( FD_UNLIKELY( !lo_idx ) ) FD_LOG_ERR(( "if_nametoindex(lo) failed" ));
    1332             : 
    1333           0 :     int lo_xsk_map_fd = -1;
    1334           0 :     for( uint i=0U; i<xdp_fds_cnt; i++ ) {
    1335           0 :       if( xdp_fds[ i ].if_idx==lo_idx ) {
    1336           0 :         lo_xsk_map_fd           = xdp_fds[ i ].xsk_map_fd;
    1337           0 :         ctx->prog_link_fds[ 1 ] = xdp_fds[ i ].prog_link_fd;
    1338           0 :         xdp_fds[ i ].prog_link_fd = -1; /* mark as used */
    1339           0 :         break;
    1340           0 :       }
    1341           0 :     }
    1342           0 :     FD_TEST( lo_xsk_map_fd>=0 );
    1343             : 
    1344             :     /* init xsk 1 */
    1345           0 :     fd_xsk_params_t params1 = params0;
    1346           0 :     params1.if_idx      = lo_idx; /* probably always 1 */
    1347           0 :     params1.if_queue_id = 0;
    1348           0 :     params1.bind_flags  = 0;
    1349           0 :     if( FD_UNLIKELY( !fd_xsk_init( &ctx->xsk[ 1 ], &params1 ) ) )          FD_LOG_ERR(( "failed to bind lo_xsk" ));
    1350           0 :     if( FD_UNLIKELY( !fd_xsk_activate( &ctx->xsk[ 1 ], lo_xsk_map_fd ) ) ) FD_LOG_ERR(( "failed to activate lo_xsk" ));
    1351           0 :   }
    1352             : 
    1353             :   /* Close unused XDP fds */
    1354             : 
    1355           0 :   if( FD_UNLIKELY( fd_sandbox_gettid()==fd_sandbox_getpid() ) ) {
    1356             :     /* Kind of gross.. in single threaded mode we don't want to close the xsk_map_fd
    1357             :        since it's shared with other net tiles.  Just check for that by seeing if we
    1358             :        are the only thread in the process. */
    1359           0 :     for( uint i=0U; i<xdp_fds_cnt; i++ ) {
    1360           0 :       if( -1==close( xdp_fds[ i ].xsk_map_fd ) ) {
    1361           0 :         FD_LOG_ERR(( "close(%d) failed (%d-%s)", xsk_map_fd, errno, fd_io_strerror( errno ) ));
    1362           0 :       }
    1363           0 :       if( xdp_fds[ i ].prog_link_fd>0 &&
    1364           0 :           -1==close( xdp_fds[ i ].prog_link_fd ) ) {
    1365           0 :         FD_LOG_ERR(( "close(%d) failed (%d-%s)", xsk_map_fd, errno, fd_io_strerror( errno ) ));
    1366           0 :       }
    1367           0 :     }
    1368           0 :   }
    1369             : 
    1370           0 :   double tick_per_ns = fd_tempo_tick_per_ns( NULL );
    1371           0 :   ctx->xdp_stats_interval_ticks = (long)( FD_XDP_STATS_INTERVAL_NS * tick_per_ns );
    1372             : 
    1373           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
    1374           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
    1375           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
    1376           0 : }
    1377             : 
    1378             : /* init_device_table joins the net tile to the netlink tile's device
    1379             :    table.  The device table is very frequently read, and rarely updated.
    1380             :    Therefore, the net tile keeps a local copy of the device table in
    1381             :    scratch memory.  This table is periodically copied over from the
    1382             :    netlink tile via a double buffer (netdev_dbl_buf).
    1383             : 
    1384             :    On startup, the netlink tile might not have produced its initial
    1385             :    device table.  Therefore, initialize the local copy to an empty
    1386             :    table. */
    1387             : 
    1388             : static void
    1389             : init_device_table( fd_net_ctx_t * ctx,
    1390           0 :                    void *         netdev_dbl_buf ) {
    1391             : 
    1392             :   /* Join remote double buffer containing device table updates */
    1393           0 :   ctx->netdev_dbl_buf = fd_dbl_buf_join( netdev_dbl_buf );
    1394           0 :   if( FD_UNLIKELY( !ctx->netdev_dbl_buf ) ) FD_LOG_ERR(( "fd_dbl_buf_join failed" ));
    1395           0 :   ctx->netdev_buf_sz  = fd_netdev_tbl_footprint( NETDEV_MAX, BOND_MASTER_MAX );
    1396             : 
    1397             :   /* Create temporary empty device table during startup */
    1398           0 :   FD_TEST( fd_netdev_tbl_join( &ctx->netdev_tbl, fd_netdev_tbl_new( ctx->netdev_buf, 1, 1 ) ) );
    1399           0 : }
    1400             : 
    1401             : FD_FN_UNUSED static void
    1402             : unprivileged_init( fd_topo_t *      topo,
    1403           0 :                    fd_topo_tile_t * tile ) {
    1404           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
    1405             : 
    1406           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
    1407           0 :   fd_net_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_net_ctx_t), sizeof(fd_net_ctx_t) );
    1408           0 :   FD_TEST( ctx->xsk_cnt!=0 );
    1409           0 :   FD_TEST( ctx->free_tx.queue!=NULL );
    1410           0 :   (void)FD_SCRATCH_ALLOC_APPEND( l, alignof(ulong), tile->xdp.free_ring_depth * sizeof(ulong) );
    1411           0 :   ctx->netdev_buf              = FD_SCRATCH_ALLOC_APPEND( l, fd_netdev_tbl_align(), ctx->netdev_buf_sz );
    1412             : 
    1413           0 :   ctx->net_tile_id  = (uint)tile->kind_id;
    1414           0 :   ctx->net_tile_cnt = (uint)fd_topo_tile_name_cnt( topo, tile->name );
    1415             : 
    1416           0 :   ctx->bind_address                   = tile->net.bind_address;
    1417           0 :   ctx->shred_listen_port              = tile->net.shred_listen_port;
    1418           0 :   ctx->quic_transaction_listen_port   = tile->net.quic_transaction_listen_port;
    1419           0 :   ctx->legacy_transaction_listen_port = tile->net.legacy_transaction_listen_port;
    1420           0 :   ctx->gossip_listen_port             = tile->net.gossip_listen_port;
    1421           0 :   ctx->repair_intake_listen_port      = tile->net.repair_intake_listen_port;
    1422           0 :   ctx->repair_serve_listen_port       = tile->net.repair_serve_listen_port;
    1423           0 :   ctx->txsend_src_port                = tile->net.txsend_src_port;
    1424             : 
    1425             :   /* Put a bound on chunks we read from the input, to make sure they
    1426             :      are within in the data region of the workspace. */
    1427             : 
    1428           0 :   if( FD_UNLIKELY( !tile->in_cnt ) ) FD_LOG_ERR(( "net tile in link cnt is zero" ));
    1429           0 :   if( FD_UNLIKELY( tile->in_cnt>MAX_NET_INS ) ) FD_LOG_ERR(( "net tile in link cnt %lu exceeds MAX_NET_INS %lu", tile->in_cnt, MAX_NET_INS ));
    1430           0 :   FD_TEST( tile->in_cnt<=32 );
    1431           0 :   for( ulong i=0UL; i<tile->in_cnt; i++ ) {
    1432           0 :     fd_topo_link_t * link = &topo->links[ tile->in_link_id[ i ] ];
    1433           0 :     if( FD_UNLIKELY( link->mtu!=FD_NET_MTU ) ) FD_LOG_ERR(( "net tile in link %s does not have a normal MTU", link->name ));
    1434             : 
    1435           0 :     ctx->in[ i ].mem    = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
    1436           0 :     ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
    1437           0 :     ctx->in[ i ].wmark  = fd_dcache_compact_wmark( ctx->in[ i ].mem, link->dcache, link->mtu );
    1438           0 :   }
    1439             : 
    1440           0 :   for( ulong i = 0; i < tile->out_cnt; i++ ) {
    1441           0 :     fd_topo_link_t * out_link = &topo->links[ tile->out_link_id[ i  ] ];
    1442           0 :     if( strcmp( out_link->name, "net_quic" ) == 0 ) {
    1443           0 :       fd_topo_link_t * quic_out = out_link;
    1444           0 :       ctx->quic_out->mcache = quic_out->mcache;
    1445           0 :       ctx->quic_out->sync   = fd_mcache_seq_laddr( ctx->quic_out->mcache );
    1446           0 :       ctx->quic_out->depth  = fd_mcache_depth( ctx->quic_out->mcache );
    1447           0 :       ctx->quic_out->seq    = fd_mcache_seq_query( ctx->quic_out->sync );
    1448           0 :     } else if( strcmp( out_link->name, "net_shred" ) == 0 ) {
    1449           0 :       fd_topo_link_t * shred_out = out_link;
    1450           0 :       ctx->shred_out->mcache = shred_out->mcache;
    1451           0 :       ctx->shred_out->sync   = fd_mcache_seq_laddr( ctx->shred_out->mcache );
    1452           0 :       ctx->shred_out->depth  = fd_mcache_depth( ctx->shred_out->mcache );
    1453           0 :       ctx->shred_out->seq    = fd_mcache_seq_query( ctx->shred_out->sync );
    1454           0 :     } else if( strcmp( out_link->name, "net_gossvf" ) == 0 ) {
    1455           0 :       fd_topo_link_t * gossip_out = out_link;
    1456           0 :       ctx->gossvf_out->mcache = gossip_out->mcache;
    1457           0 :       ctx->gossvf_out->sync   = fd_mcache_seq_laddr( ctx->gossvf_out->mcache );
    1458           0 :       ctx->gossvf_out->depth  = fd_mcache_depth( ctx->gossvf_out->mcache );
    1459           0 :       ctx->gossvf_out->seq    = fd_mcache_seq_query( ctx->gossvf_out->sync );
    1460           0 :     } else if( strcmp( out_link->name, "net_repair" ) == 0 ) {
    1461           0 :       fd_topo_link_t * repair_out = out_link;
    1462           0 :       ctx->repair_out->mcache = repair_out->mcache;
    1463           0 :       ctx->repair_out->sync   = fd_mcache_seq_laddr( ctx->repair_out->mcache );
    1464           0 :       ctx->repair_out->depth  = fd_mcache_depth( ctx->repair_out->mcache );
    1465           0 :       ctx->repair_out->seq    = fd_mcache_seq_query( ctx->repair_out->sync );
    1466           0 :     } else if( strcmp( out_link->name, "net_netlnk" ) == 0 ) {
    1467           0 :       fd_topo_link_t * netlink_out = out_link;
    1468           0 :       ctx->neigh4_solicit->mcache = netlink_out->mcache;
    1469           0 :       ctx->neigh4_solicit->depth  = fd_mcache_depth( ctx->neigh4_solicit->mcache );
    1470           0 :       ctx->neigh4_solicit->seq    = fd_mcache_seq_query( fd_mcache_seq_laddr( ctx->neigh4_solicit->mcache ) );
    1471           0 :     } else if( strcmp( out_link->name, "net_txsend" ) == 0 ) {
    1472           0 :       fd_topo_link_t * txsend_out = out_link;
    1473           0 :       ctx->txsend_out->mcache = txsend_out->mcache;
    1474           0 :       ctx->txsend_out->sync   = fd_mcache_seq_laddr( ctx->txsend_out->mcache );
    1475           0 :       ctx->txsend_out->depth  = fd_mcache_depth( ctx->txsend_out->mcache );
    1476           0 :       ctx->txsend_out->seq    = fd_mcache_seq_query( ctx->txsend_out->sync );
    1477           0 :     } else {
    1478           0 :       FD_LOG_ERR(( "unrecognized out link `%s`", out_link->name ));
    1479           0 :     }
    1480           0 :   }
    1481             : 
    1482             :   /* Check if any of the tiles we set a listen port for do not have an outlink. */
    1483           0 :   if( FD_UNLIKELY( ctx->shred_listen_port!=0 && ctx->shred_out->mcache==NULL ) ) {
    1484           0 :     FD_LOG_ERR(( "shred listen port set but no out link was found" ));
    1485           0 :   } else if( FD_UNLIKELY( ctx->quic_transaction_listen_port!=0 && ctx->quic_out->mcache==NULL ) ) {
    1486           0 :     FD_LOG_ERR(( "quic transaction listen port set but no out link was found" ));
    1487           0 :   } else if( FD_UNLIKELY( ctx->legacy_transaction_listen_port!=0 && ctx->quic_out->mcache==NULL ) ) {
    1488           0 :     FD_LOG_ERR(( "legacy transaction listen port set but no out link was found" ));
    1489           0 :   } else if( FD_UNLIKELY( ctx->gossip_listen_port!=0 && ctx->gossvf_out->mcache==NULL ) ) {
    1490           0 :     FD_LOG_ERR(( "gossip listen port set but no out link was found" ));
    1491           0 :   } else if( FD_UNLIKELY( ctx->repair_intake_listen_port!=0 && ctx->repair_out->mcache==NULL ) ) {
    1492           0 :     FD_LOG_ERR(( "repair intake port set but no out link was found" ));
    1493           0 :   } else if( FD_UNLIKELY( ctx->repair_serve_listen_port!=0 && ctx->repair_out->mcache==NULL ) ) {
    1494           0 :     FD_LOG_ERR(( "repair serve listen port set but no out link was found" ));
    1495           0 :   } else if( FD_UNLIKELY( ctx->neigh4_solicit->mcache==NULL ) ) {
    1496           0 :     FD_LOG_ERR(( "netlink request link not found" ));
    1497           0 :   } else if( FD_UNLIKELY( ctx->txsend_src_port!=0 && ctx->txsend_out->mcache==NULL ) ) {
    1498           0 :     FD_LOG_ERR(( "txsend listen port set but no out link was found" ));
    1499           0 :   }
    1500             : 
    1501           0 :   for( uint j=0U; j<2U; j++ ) {
    1502           0 :     ctx->tx_flusher[ j ].pending_wmark         = (ulong)( (double)tile->xdp.xdp_tx_queue_size * 0.7 );
    1503           0 :     ctx->tx_flusher[ j ].tail_flush_backoff    = (long)( (double)tile->xdp.tx_flush_timeout_ns * fd_tempo_tick_per_ns( NULL ) );
    1504           0 :     ctx->tx_flusher[ j ].next_tail_flush_ticks = LONG_MAX;
    1505           0 :   }
    1506             : 
    1507             :   /* Join netbase objects */
    1508           0 :   FD_TEST( fd_fib4_join( ctx->fib_local, fd_topo_obj_laddr( topo, tile->xdp.fib4_local_obj_id ) ) );
    1509           0 :   FD_TEST( fd_fib4_join( ctx->fib_main, fd_topo_obj_laddr( topo, tile->xdp.fib4_main_obj_id  ) ) );
    1510             : 
    1511           0 :   ulong neigh4_obj_id = tile->xdp.neigh4_obj_id;
    1512           0 :   ulong ele_max   = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "obj.%lu.ele_max",   neigh4_obj_id );
    1513           0 :   ulong probe_max = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "obj.%lu.probe_max", neigh4_obj_id );
    1514           0 :   ulong seed      = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "obj.%lu.seed",      neigh4_obj_id );
    1515           0 :   if( FD_UNLIKELY( (ele_max==ULONG_MAX) | (probe_max==ULONG_MAX) | (seed==ULONG_MAX) ) )
    1516           0 :     FD_LOG_ERR(( "neigh4 hmap properties not set" ));
    1517           0 :   if( FD_UNLIKELY( !fd_neigh4_hmap_join(
    1518           0 :       ctx->neigh4,
    1519           0 :       fd_topo_obj_laddr( topo, neigh4_obj_id ),
    1520           0 :       ele_max,
    1521           0 :       probe_max,
    1522           0 :       seed ) ) ) {
    1523           0 :     FD_LOG_ERR(( "fd_neigh4_hmap_join failed" ));
    1524           0 :   }
    1525             : 
    1526           0 :   init_device_table( ctx, fd_topo_obj_laddr( topo, tile->xdp.netdev_dbl_buf_obj_id ) );
    1527             : 
    1528             :   /* Initialize TX free ring */
    1529             : 
    1530           0 :   ulong const frame_sz  = 2048UL;
    1531           0 :   ulong       frame_off = 0UL;
    1532           0 :   ulong const tx_depth  = ctx->free_tx.depth;
    1533           0 :   for( ulong j=0; j<tx_depth; j++ ) {
    1534           0 :     ctx->free_tx.queue[ j ] = (ulong)ctx->umem + frame_off;
    1535           0 :     frame_off += frame_sz;
    1536           0 :   }
    1537           0 :   ctx->free_tx.prod = tx_depth;
    1538             : 
    1539             :   /* Initialize RX mcache chunks */
    1540             : 
    1541           0 :   for( ulong i=0UL; i<(tile->out_cnt); i++ ) {
    1542           0 :     fd_topo_link_t * out_link = &topo->links[ tile->out_link_id[ i  ] ];
    1543           0 :     fd_frag_meta_t * mcache   = out_link->mcache;
    1544           0 :     for( ulong j=0UL; j<fd_mcache_depth( mcache ); j++ ) {
    1545           0 :       mcache[ j ].chunk = (uint)( ctx->umem_chunk0 + (frame_off>>FD_CHUNK_LG_SZ) );
    1546           0 :       frame_off += frame_sz;
    1547           0 :     }
    1548           0 :   }
    1549             : 
    1550             :   /* Initialize FILL ring */
    1551             : 
    1552           0 :   int _charge_busy = 0;
    1553           0 :   for( uint j=0U; j<ctx->xsk_cnt; j++ ) {
    1554           0 :     frame_off = net_xsk_bootstrap( ctx, j, frame_off );
    1555           0 :     net_rx_wakeup( ctx, &ctx->xsk[ j ], &_charge_busy );
    1556           0 :     net_tx_wakeup( ctx, &ctx->xsk[ j ], &_charge_busy );
    1557           0 :   }
    1558             : 
    1559           0 :   if( FD_UNLIKELY( frame_off > ctx->umem_sz ) ) {
    1560           0 :     FD_LOG_ERR(( "UMEM is too small" ));
    1561           0 :   }
    1562           0 : }
    1563             : 
    1564             : FD_FN_UNUSED static ulong
    1565             : populate_allowed_seccomp( fd_topo_t const *      topo,
    1566             :                           fd_topo_tile_t const * tile,
    1567             :                           ulong                  out_cnt,
    1568           0 :                           struct sock_filter *   out ) {
    1569           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
    1570           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
    1571           0 :   fd_net_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_net_ctx_t ), sizeof( fd_net_ctx_t ) );
    1572             : 
    1573             :   /* A bit of a hack, if there is no loopback XSK for this tile, we still need to pass
    1574             :      two "allow" FD arguments to the net policy, so we just make them both the same. */
    1575           0 :   int allow_fd2 = ctx->xsk_cnt>1UL ? ctx->xsk[ 1 ].xsk_fd : ctx->xsk[ 0 ].xsk_fd;
    1576           0 :   FD_TEST( ctx->xsk[ 0 ].xsk_fd >= 0 && allow_fd2 >= 0 );
    1577             : 
    1578           0 :   populate_sock_filter_policy_fd_xdp_tile( out_cnt, out, (uint)fd_log_private_logfile_fd(), (uint)ctx->xsk[ 0 ].xsk_fd, (uint)allow_fd2 );
    1579           0 :   return sock_filter_policy_fd_xdp_tile_instr_cnt;
    1580           0 : }
    1581             : 
    1582             : FD_FN_UNUSED static ulong
    1583             : populate_allowed_fds( fd_topo_t const *      topo,
    1584             :                       fd_topo_tile_t const * tile,
    1585             :                       ulong                  out_fds_cnt,
    1586           0 :                       int *                  out_fds ) {
    1587           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
    1588           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
    1589           0 :   fd_net_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_net_ctx_t ), sizeof( fd_net_ctx_t ) );
    1590             : 
    1591           0 :   if( FD_UNLIKELY( out_fds_cnt<6UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
    1592             : 
    1593           0 :   ulong out_cnt = 0UL;
    1594             : 
    1595           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
    1596           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
    1597           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
    1598             : 
    1599           0 :                                       out_fds[ out_cnt++ ] = ctx->xsk[ 0 ].xsk_fd;
    1600           0 :                                       out_fds[ out_cnt++ ] = ctx->prog_link_fds[ 0 ];
    1601           0 :   if( FD_LIKELY( ctx->xsk_cnt>1UL ) ) out_fds[ out_cnt++ ] = ctx->xsk[ 1 ].xsk_fd;
    1602           0 :   if( FD_LIKELY( ctx->xsk_cnt>1UL ) ) out_fds[ out_cnt++ ] = ctx->prog_link_fds[ 1 ];
    1603           0 :   return out_cnt;
    1604           0 : }
    1605             : 
    1606           0 : #define STEM_BURST (1UL)
    1607           0 : #define STEM_LAZY ((ulong)30e3) /* 30 us */
    1608             : 
    1609           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_net_ctx_t
    1610           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_net_ctx_t)
    1611             : 
    1612           0 : #define STEM_CALLBACK_METRICS_WRITE       metrics_write
    1613           0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
    1614           0 : #define STEM_CALLBACK_BEFORE_CREDIT       before_credit
    1615           0 : #define STEM_CALLBACK_BEFORE_FRAG         before_frag
    1616           0 : #define STEM_CALLBACK_DURING_FRAG         during_frag
    1617           0 : #define STEM_CALLBACK_AFTER_FRAG          after_frag
    1618             : 
    1619             : #include "../../stem/fd_stem.c"
    1620             : 
    1621             : #ifndef FD_TILE_TEST
    1622             : fd_topo_run_tile_t fd_tile_net = {
    1623             :   .name                     = "net",
    1624             :   .populate_allowed_seccomp = populate_allowed_seccomp,
    1625             :   .populate_allowed_fds     = populate_allowed_fds,
    1626             :   .scratch_align            = scratch_align,
    1627             :   .scratch_footprint        = scratch_footprint,
    1628             :   .privileged_init          = privileged_init,
    1629             :   .unprivileged_init        = unprivileged_init,
    1630             :   .run                      = stem_run,
    1631             : };
    1632             : #endif

Generated by: LCOV version 1.14