LCOV - code coverage report
Current view: top level - disco/netlink - fd_netlink_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 423 0.0 %
Date: 2026-11-11 12:40:43 Functions: 0 17 0.0 %

          Line data    Source code
       1             : #include "fd_netlink_tile_private.h"
       2             : #include "../topo/fd_topo.h"
       3             : #include "../topo/fd_topob.h"
       4             : #include "../metrics/fd_metrics.h"
       5             : #include "../waker/fd_waker.h"
       6             : #include "../../waltz/ip/fd_fib4_netlink.h"
       7             : #include "../../waltz/mib/fd_netdev_netlink.h"
       8             : #include "../../waltz/neigh/fd_neigh4_netlink.h"
       9             : #include "../../util/pod/fd_pod_format.h"
      10             : #include "../../util/log/fd_dtrace.h"
      11             : #include "fd_netlink_tile.h"
      12             : 
      13             : #include <errno.h>
      14             : #include <net/if.h>
      15             : #include <netinet/in.h> /* MSG_DONTWAIT */
      16             : #include <sys/epoll.h>
      17             : #include <sys/socket.h> /* SOL_{...} */
      18             : #include <sys/random.h> /* getrandom */
      19             : #include <sys/time.h> /* struct timeval */
      20             : #include <time.h> /* CLOCK_REALTIME for seccomp filter */
      21             : #include <linux/rtnetlink.h> /* RTM_{...} */
      22             : 
      23             : #define FD_SOCKADDR_IN_SZ sizeof(struct sockaddr_in)
      24             : #include <linux/futex.h>
      25             : #include "generated/netlink_seccomp.h"
      26             : 
      27             : void
      28             : fd_netlink_topo_create( fd_topo_tile_t * netlink_tile,
      29             :                         fd_topo_t *      topo,
      30             :                         ulong            netlnk_max_routes,
      31             :                         ulong            netlnk_max_peer_routes,
      32             :                         ulong            netlnk_max_neighbors,
      33           0 :                         char const *     bind_interface ) {
      34           0 :   fd_topo_obj_t * netdev_tbl_obj = fd_topob_obj( topo, "netdev_tbl",  "netbase" );
      35           0 :   fd_topo_obj_t * neigh4_obj     = fd_topob_obj( topo, "neigh4_hmap", "netbase" );
      36             : 
      37           0 :   fd_topob_tile_uses( topo, netlink_tile, netdev_tbl_obj, FD_SHMEM_JOIN_MODE_READ_WRITE );
      38           0 :   fd_topob_tile_uses( topo, netlink_tile, neigh4_obj,     FD_SHMEM_JOIN_MODE_READ_WRITE );
      39             : 
      40             :   /* Configure netdev table */
      41           0 :   FD_TEST( fd_pod_insertf_ulong( topo->props, NETDEV_MAX,      "obj.%lu.dev_max",  netdev_tbl_obj->id ) );
      42           0 :   FD_TEST( fd_pod_insertf_ulong( topo->props, BOND_MASTER_MAX, "obj.%lu.bond_max", netdev_tbl_obj->id ) );
      43             : 
      44           0 :   netlink_tile->netlink.route_max      = netlnk_max_routes;
      45           0 :   netlink_tile->netlink.route_peer_max = netlnk_max_peer_routes;
      46             : 
      47             :   /* Configure neighbor hashmap */
      48           0 :   FD_TEST( fd_pod_insertf_ulong( topo->props, netlnk_max_neighbors, "obj.%lu.ele_max", neigh4_obj->id ) );
      49           0 :   FD_TEST( fd_pod_insertf_ulong( topo->props, 16UL, "obj.%lu.probe_max", neigh4_obj->id ) );
      50           0 :   ulong neigh4_seed;
      51           0 :   FD_TEST( 8UL==getrandom( &neigh4_seed, sizeof(ulong), 0 ) );
      52           0 :   FD_TEST( fd_pod_insertf_ulong( topo->props, neigh4_seed, "obj.%lu.seed", neigh4_obj->id ) );
      53             : 
      54           0 :   netlink_tile->netlink.netdev_tbl_obj_id = netdev_tbl_obj->id;
      55           0 :   memcpy( netlink_tile->netlink.neigh_if, bind_interface, sizeof(netlink_tile->netlink.neigh_if) );
      56           0 :   netlink_tile->netlink.neigh4_obj_id     = neigh4_obj->id;
      57           0 : }
      58             : 
      59             : void
      60             : fd_netlink_topo_join( fd_topo_t *      topo,
      61             :                       fd_topo_tile_t * netlink_tile,
      62           0 :                       fd_topo_tile_t * join_tile ) {
      63           0 :   fd_topob_tile_uses( topo, join_tile, &topo->objs[ netlink_tile->netlink.netdev_tbl_obj_id ], FD_SHMEM_JOIN_MODE_READ_ONLY );
      64           0 :   fd_topob_tile_uses( topo, join_tile, &topo->objs[ netlink_tile->netlink.neigh4_obj_id     ], FD_SHMEM_JOIN_MODE_READ_ONLY );
      65           0 : }
      66             : 
      67             : /* Begin tile methods */
      68             : 
      69             : FD_FN_CONST static inline ulong
      70           0 : scratch_align( void ) {
      71           0 :   return alignof(fd_netlink_tile_ctx_t);
      72           0 : }
      73             : 
      74             : FD_FN_PURE static inline ulong
      75           0 : scratch_footprint( fd_topo_tile_t const * tile ) {
      76           0 :   (void)tile;
      77           0 :   ulong l = FD_LAYOUT_INIT;
      78           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_netlink_tile_ctx_t), sizeof(fd_netlink_tile_ctx_t) );
      79           0 :   return FD_LAYOUT_FINI( l, scratch_align() );
      80           0 : }
      81             : 
      82             : static ulong
      83             : populate_allowed_seccomp( fd_topo_t const *      topo,
      84             :                           fd_topo_tile_t const * tile,
      85             :                           ulong                  out_cnt,
      86           0 :                           struct sock_filter *   out ) {
      87           0 :   fd_netlink_tile_ctx_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
      88           0 :   FD_TEST( ctx->magic==FD_NETLINK_TILE_CTX_MAGIC );
      89           0 :   uint epoll_inner_fd = (uint)FD_WAKER_INNER_FD( tile->waker_client_idx );
      90           0 :   uint epoll_outer_fd = (uint)FD_WAKER_OUTER_FD;
      91             : 
      92           0 :   populate_sock_filter_policy_netlink( out_cnt, out, (uint)fd_log_private_logfile_fd(), (uint)ctx->nl_monitor->fd, (uint)ctx->nl_req->fd, (uint)ctx->prober->sock_fd, epoll_inner_fd, epoll_outer_fd );
      93           0 :   return sock_filter_policy_netlink_instr_cnt;
      94           0 : }
      95             : 
      96             : static ulong
      97             : populate_allowed_fds( fd_topo_t const *      topo,
      98             :                       fd_topo_tile_t const * tile,
      99             :                       ulong                  out_fds_cnt,
     100           0 :                       int *                  out_fds ) {
     101           0 :   fd_netlink_tile_ctx_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     102           0 :   FD_TEST( ctx->magic==FD_NETLINK_TILE_CTX_MAGIC );
     103             : 
     104           0 :   if( FD_UNLIKELY( out_fds_cnt<7UL ) ) FD_LOG_ERR(( "out_fds_cnt too low (%lu)", out_fds_cnt ));
     105             : 
     106           0 :   ulong out_cnt = 0UL;
     107           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
     108           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     109           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     110           0 :   out_fds[ out_cnt++ ] = ctx->nl_monitor->fd;
     111           0 :   out_fds[ out_cnt++ ] = ctx->nl_req->fd;
     112           0 :   out_fds[ out_cnt++ ] = ctx->prober->sock_fd;
     113           0 :   out_fds[ out_cnt++ ] = FD_WAKER_OUTER_FD; /* waker outer epoll fd (rearm) */
     114           0 :   out_fds[ out_cnt++ ] = FD_WAKER_INNER_FD( tile->waker_client_idx ); /* waker inner epoll fd */
     115           0 :   return out_cnt;
     116           0 : }
     117             : 
     118             : static void
     119             : privileged_init( fd_topo_t const *      topo,
     120           0 :                  fd_topo_tile_t const * tile ) {
     121           0 :   if( FD_UNLIKELY( tile->kind_id!=0 ) ) {
     122           0 :     FD_LOG_ERR(( "Topology contains more than one netlink tile" ));
     123           0 :   }
     124             : 
     125           0 :   uint const neigh_if_idx = if_nametoindex( tile->netlink.neigh_if );
     126           0 :   if( FD_UNLIKELY( !neigh_if_idx ) ) FD_LOG_ERR(( "if_nametoindex(%.16s) failed (%i-%s)", tile->netlink.neigh_if, errno, fd_io_strerror( errno ) ));
     127             : 
     128           0 :   fd_netlink_tile_ctx_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     129           0 :   fd_memset( ctx, 0, sizeof(fd_netlink_tile_ctx_t) );
     130           0 :   ctx->magic = FD_NETLINK_TILE_CTX_MAGIC;
     131           0 :   ctx->neigh4_ifidx = neigh_if_idx;
     132             : 
     133           0 :   if( FD_UNLIKELY( !fd_netlink_init( ctx->nl_monitor, 1000U ) ) ) {
     134           0 :     FD_LOG_ERR(( "Failed to connect to rtnetlink" ));
     135           0 :   }
     136           0 :   if( FD_UNLIKELY( !fd_netlink_init( ctx->nl_req, 9000000U ) ) ) {
     137           0 :     FD_LOG_ERR(( "Failed to connect to rtnetlink" ));
     138           0 :   }
     139             : 
     140           0 :   union {
     141           0 :     struct sockaddr    sa;
     142           0 :     struct sockaddr_nl sanl;
     143           0 :   } sa;
     144           0 :   sa.sanl = (struct sockaddr_nl) {
     145           0 :     .nl_family = AF_NETLINK,
     146           0 :     .nl_groups = RTMGRP_LINK | RTMGRP_NEIGH | RTMGRP_IPV4_ROUTE
     147           0 :   };
     148           0 :   if( FD_UNLIKELY( 0!=bind( ctx->nl_monitor->fd, &sa.sa, sizeof(struct sockaddr_nl) ) ) ) {
     149           0 :     FD_LOG_ERR(( "bind(sock,RT_NETLINK,RTMGRP_{LINK,NEIGH,IPV4_ROUTE}) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     150           0 :   }
     151             : 
     152           0 :   float const max_probes_per_second =   3.f;
     153           0 :   ulong const max_probe_burst       = 128UL;
     154           0 :   float const probe_delay_seconds   =  15.f;
     155           0 :   fd_neigh4_prober_init( ctx->prober, max_probes_per_second, max_probe_burst, probe_delay_seconds );
     156             : 
     157           0 :   ctx->waker_client_idx = tile->waker_client_idx;
     158           0 :   FD_TEST( ctx->waker_client_idx!=ULONG_MAX );
     159             : 
     160           0 :   struct epoll_event ev = { .events = EPOLLIN, .data.fd = ctx->nl_monitor->fd };
     161           0 :   if( FD_UNLIKELY( -1==epoll_ctl( FD_WAKER_INNER_FD( ctx->waker_client_idx ), EPOLL_CTL_ADD, ctx->nl_monitor->fd, &ev ) ) )
     162           0 :     FD_LOG_ERR(( "epoll_ctl(ADD,nl_monitor) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     163           0 : }
     164             : 
     165             : static void
     166             : unprivileged_init( fd_topo_t const *      topo,
     167           0 :                    fd_topo_tile_t const * tile ) {
     168           0 :   FD_SCRATCH_ALLOC_INIT( l, fd_topo_obj_laddr( topo, tile->tile_obj_id ) );
     169           0 :   fd_netlink_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_netlink_tile_ctx_t), sizeof(fd_netlink_tile_ctx_t) );
     170           0 :   FD_TEST( ctx->magic==FD_NETLINK_TILE_CTX_MAGIC );
     171             : 
     172           0 :   FD_TEST( ctx->waker_client_idx!=ULONG_MAX );
     173           0 :   ctx->waker_fseq = fd_fseq_join( fd_topo_obj_laddr( topo, tile->waker_fseq_obj_id ) );
     174           0 :   FD_TEST( ctx->waker_fseq );
     175             : 
     176           0 :   FD_TEST( tile->netlink.netdev_tbl_obj_id );
     177           0 :   FD_TEST( tile->netlink.neigh4_obj_id     );
     178           0 :   ctx->route_max      = tile->netlink.route_max;
     179           0 :   ctx->route_peer_max = tile->netlink.route_peer_max;
     180             : 
     181           0 :   FD_TEST( fd_netdev_tbl_join( ctx->netdev_tbl, fd_topo_obj_laddr( topo, tile->netlink.netdev_tbl_obj_id ) ) );
     182             : 
     183           0 :   ulong neigh4_obj_id = tile->netlink.neigh4_obj_id;
     184           0 :   ulong neigh_ele_max   = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "obj.%lu.ele_max",   neigh4_obj_id );
     185           0 :   ulong neigh_probe_max = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "obj.%lu.probe_max", neigh4_obj_id );
     186           0 :   ulong neigh_seed      = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "obj.%lu.seed",      neigh4_obj_id );
     187           0 :   FD_TEST( neigh_ele_max!=ULONG_MAX && neigh_probe_max!=ULONG_MAX && neigh_seed!=ULONG_MAX );
     188           0 :   FD_TEST( fd_neigh4_hmap_join( ctx->neigh4, fd_topo_obj_laddr( topo, neigh4_obj_id ), neigh_ele_max, neigh_probe_max, neigh_seed ) );
     189             : 
     190           0 :   FD_TEST( tile->out_cnt==1UL );
     191           0 :   fd_topo_link_t const * out = &topo->links[ tile->out_link_id[0] ];
     192           0 :   ctx->out_mem    = topo->workspaces[ topo->objs[ out->dcache_obj_id ].wksp_id ].wksp;
     193           0 :   ctx->out_chunk0 = fd_dcache_compact_chunk0( ctx->out_mem, out->dcache );
     194           0 :   ctx->out_wmark  = fd_dcache_compact_wmark( ctx->out_mem, out->dcache, out->mtu );
     195           0 :   ctx->out_chunk  = ctx->out_chunk0;
     196             : 
     197             : 
     198           0 :   for( ulong i=0UL; i<tile->in_cnt; i++ ) {
     199           0 :     fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
     200           0 :     if( FD_UNLIKELY( link->mtu!=0UL ) ) FD_LOG_ERR(( "netlink solicit links must have an MTU of zero" ));
     201           0 :   }
     202             : 
     203           0 :   ctx->action |= FD_NET_TILE_ACTION_LINK_UPDATE;
     204           0 :   ctx->action |= FD_NET_TILE_ACTION_ROUTE4_UPDATE;
     205           0 :   ctx->action |= FD_NET_TILE_ACTION_NEIGH_UPDATE;
     206             : 
     207           0 :   ctx->update_backoff = (long)( fd_tempo_tick_per_ns( NULL ) * 500e6 ); /* 500ms */
     208           0 : }
     209             : 
     210             : /* Begin stem methods */
     211             : 
     212             : static inline void
     213           0 : metrics_write( fd_netlink_tile_ctx_t * ctx ) {
     214           0 :   ctx->metrics.neigh_probe[ FD_METRICS_ENUM_NEIGHBOR_PROBE_RESULT_V_HOST_RATE_LIMITED_IDX   ] = ctx->prober->local_rate_limited_cnt;
     215           0 :   ctx->metrics.neigh_probe[ FD_METRICS_ENUM_NEIGHBOR_PROBE_RESULT_V_GLOBAL_RATE_LIMITED_IDX ] = ctx->prober->global_rate_limited_cnt;
     216           0 :   FD_MCNT_SET(       NETLNK, RX_BUFFER_OVERFLOWED,     fd_netlink_enobufs_cnt         );
     217           0 :   FD_MCNT_SET(       NETLNK, LINK_TABLE_SYNCED,        ctx->metrics.link_full_syncs   );
     218           0 :   FD_MCNT_SET(       NETLNK, ROUTE_TABLE_SYNCED,       ctx->metrics.route_full_syncs  );
     219           0 :   FD_MCNT_ENUM_COPY( NETLNK, UPDATE_PROCESSED,         ctx->metrics.update_cnt        );
     220           0 :   FD_MGAUGE_SET(     NETLNK, INTERFACE_COUNT,          ctx->netdev_tbl->hdr->dev_cnt  );
     221           0 :   FD_MCNT_ENUM_COPY( NETLNK, NEIGHBOR_PROBE_REQUESTED, ctx->metrics.neigh_probe       );
     222           0 : }
     223             : 
     224             : static void
     225             : iproute_publish( fd_netlink_tile_ctx_t * ctx,
     226             :                  fd_stem_context_t *     stem,
     227           0 :                  fd_iproute_msg_t const * msg ) {
     228           0 :   fd_memcpy( fd_chunk_to_laddr( ctx->out_mem, ctx->out_chunk ), msg, sizeof(*msg) );
     229           0 :   fd_stem_publish( stem, 0UL, 0UL, ctx->out_chunk, sizeof(*msg), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
     230           0 :   ctx->out_chunk = fd_dcache_compact_next( ctx->out_chunk, sizeof(*msg), ctx->out_chunk0, ctx->out_wmark );
     231           0 : }
     232             : 
     233             : /* iproute_dump_begin starts a multipart RTM_GETROUTE request.  The
     234             :    response iterator remains in ctx so subsequent calls can consume it
     235             :    one netlink message at a time. */
     236             : 
     237             : static int
     238             : iproute_dump_begin( fd_netlink_tile_ctx_t * ctx,
     239           0 :                     uint                    table_id ) {
     240           0 :   uint seq = ctx->nl_req->seq++;
     241           0 :   struct {
     242           0 :     struct nlmsghdr nlh;
     243           0 :     struct rtmsg    rtm;
     244           0 :     struct rtattr   rta;
     245           0 :     uint            table_id;
     246           0 :   } request = {
     247           0 :     .nlh = {
     248           0 :       .nlmsg_type  = RTM_GETROUTE,
     249           0 :       .nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP,
     250           0 :       .nlmsg_len   = sizeof(request),
     251           0 :       .nlmsg_seq   = seq,
     252           0 :     },
     253           0 :     .rtm = { .rtm_family = AF_INET },
     254           0 :     .rta = {
     255           0 :       .rta_type = RTA_TABLE,
     256           0 :       .rta_len  = RTA_LENGTH( sizeof(uint) ),
     257           0 :     },
     258           0 :     .table_id = table_id,
     259           0 :   };
     260           0 :   long send_res = sendto( ctx->nl_req->fd, &request, sizeof(request), 0, NULL, 0 );
     261           0 :   if( FD_UNLIKELY( send_res<0L ) ) {
     262           0 :     FD_LOG_WARNING(( "netlink send(%d,RTM_GETROUTE,NLM_F_REQUEST|NLM_F_DUMP) failed (%d-%s)", ctx->nl_req->fd, errno, fd_io_strerror( errno ) ));
     263           0 :     return FD_FIB_NETLINK_ERR_IO;
     264           0 :   }
     265           0 :   if( FD_UNLIKELY( send_res!=(long)sizeof(request) ) ) {
     266           0 :     FD_LOG_WARNING(( "netlink send(%d,RTM_GETROUTE,NLM_F_REQUEST|NLM_F_DUMP) failed (short write)", ctx->nl_req->fd ));
     267           0 :     return FD_FIB_NETLINK_ERR_IO;
     268           0 :   }
     269           0 :   fd_netlink_iter_init( ctx->dump_iter, ctx->nl_req, ctx->dump_buf, sizeof(ctx->dump_buf) );
     270           0 :   ctx->dump_active  = 1;
     271           0 :   ctx->dump_advance = 0;
     272           0 :   ctx->dump_intr    = 0;
     273           0 :   return FD_FIB_NETLINK_SUCCESS;
     274           0 : }
     275             : 
     276             : #define FD_NETLINK_DUMP_NEXT_DONE     (0)
     277           0 : #define FD_NETLINK_DUMP_NEXT_ROUTE    (1)
     278           0 : #define FD_NETLINK_DUMP_NEXT_PROGRESS (2)
     279             : 
     280             : /* iproute_dump_next consumes at most one netlink message. */
     281             : 
     282             : static int
     283             : iproute_dump_next( fd_netlink_tile_ctx_t * ctx,
     284           0 :                    fd_iproute_msg_t *      route ) {
     285           0 :   if( ctx->dump_advance ) {
     286           0 :     fd_netlink_iter_next( ctx->dump_iter, ctx->nl_req );
     287           0 :     ctx->dump_advance = 0;
     288           0 :   }
     289           0 :   if( fd_netlink_iter_done( ctx->dump_iter ) ) {
     290           0 :     if( ctx->dump_iter->err==0 ) {
     291           0 :       struct nlmsghdr const * done = fd_netlink_iter_msg( ctx->dump_iter );
     292           0 :       if( FD_UNLIKELY( done->nlmsg_flags & NLM_F_DUMP_INTR ) ) ctx->dump_intr = 1;
     293           0 :     }
     294           0 :     int err = FD_FIB_NETLINK_SUCCESS;
     295           0 :     if( FD_UNLIKELY( ctx->dump_iter->err>0 ) ) err = FD_FIB_NETLINK_ERR_IO;
     296           0 :     else if( FD_UNLIKELY( ctx->dump_intr ) )   err = FD_FIB_NETLINK_ERR_INTR;
     297           0 :     else if( FD_UNLIKELY( ctx->dump_overflow ) ) err = FD_FIB_NETLINK_ERR_SPACE;
     298           0 :     ctx->dump_active = 0;
     299           0 :     return -err;
     300           0 :   }
     301             : 
     302           0 :   struct nlmsghdr const * nlh = fd_netlink_iter_msg( ctx->dump_iter );
     303           0 :   ctx->dump_advance = 1;
     304           0 :   if( FD_UNLIKELY( nlh->nlmsg_flags & NLM_F_DUMP_INTR ) ) ctx->dump_intr = 1;
     305           0 :   if( FD_UNLIKELY( nlh->nlmsg_type==NLMSG_ERROR ) ) {
     306           0 :     struct nlmsgerr const * err = NLMSG_DATA( nlh );
     307           0 :     int nl_err = -err->error;
     308           0 :     FD_LOG_WARNING(( "netlink RTM_GETROUTE,NLM_F_REQUEST|NLM_F_DUMP failed (%d-%s)", nl_err, fd_io_strerror( nl_err ) ));
     309           0 :     ctx->dump_active = 0;
     310           0 :     return -FD_FIB_NETLINK_ERR_IO;
     311           0 :   }
     312           0 :   if( FD_UNLIKELY( nlh->nlmsg_type!=RTM_NEWROUTE ) ) {
     313           0 :     FD_LOG_DEBUG(( "unexpected nlmsg_type %u", nlh->nlmsg_type ));
     314           0 :     return FD_NETLINK_DUMP_NEXT_PROGRESS;
     315           0 :   }
     316           0 :   if( FD_UNLIKELY( ctx->dump_overflow ) ) return FD_NETLINK_DUMP_NEXT_PROGRESS;
     317           0 :   if( !fd_fib4_netlink_translate( nlh, ctx->dump_table_id, route ) ) return FD_NETLINK_DUMP_NEXT_PROGRESS;
     318             : 
     319           0 :   ulong table_idx = ctx->dump_table_id==RT_TABLE_LOCAL ? 0UL : 1UL;
     320           0 :   if( route->prefix==32U && route->dst_addr ) {
     321           0 :     if( FD_UNLIKELY( ctx->dump_peer_cnt[ table_idx ]>=ctx->route_peer_max ) ) ctx->dump_overflow = 1;
     322           0 :     else ctx->dump_peer_cnt[ table_idx ]++;
     323           0 :   } else {
     324             :     /* fd_fib4 reserves one non-peer slot for its dummy throw route. */
     325           0 :     if( FD_UNLIKELY( ctx->dump_route_cnt[ table_idx ]+1UL>=ctx->route_max ) ) ctx->dump_overflow = 1;
     326           0 :     else ctx->dump_route_cnt[ table_idx ]++;
     327           0 :   }
     328           0 :   return ctx->dump_overflow ? FD_NETLINK_DUMP_NEXT_PROGRESS : FD_NETLINK_DUMP_NEXT_ROUTE;
     329           0 : }
     330             : 
     331             : /* netlink_monitor_read processes at most one netlink message.  Returns
     332             :    2 if it published a route, 1 if it processed a non-route message, and
     333             :    0 if no message was available. */
     334             : 
     335             : static int
     336             : netlink_monitor_read( fd_netlink_tile_ctx_t * ctx,
     337             :                       fd_stem_context_t *     stem,
     338           0 :                       int                     flags ) {
     339             : 
     340           0 :   if( ctx->monitor_buf_off>=ctx->monitor_buf_sz ) {
     341           0 :     long msg_sz = recvfrom( ctx->nl_monitor->fd, ctx->monitor_buf, sizeof(ctx->monitor_buf), flags, NULL, NULL );
     342           0 :     if( msg_sz<=0L ) {
     343           0 :       if( FD_LIKELY( errno==EAGAIN || errno==EINTR ) ) return 0;
     344           0 :       if( errno==ENOBUFS ) {
     345           0 :         fd_netlink_enobufs_cnt++;
     346           0 :         ctx->action |= FD_NET_TILE_ACTION_ROUTE4_UPDATE;
     347           0 :         return 0;
     348           0 :       }
     349           0 :       FD_LOG_ERR(( "recvfrom(nl_monitor) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     350           0 :     }
     351           0 :     ctx->monitor_buf_sz  = msg_sz;
     352           0 :     ctx->monitor_buf_off = 0L;
     353           0 :   }
     354             : 
     355           0 :   long rem = ctx->monitor_buf_sz-ctx->monitor_buf_off;
     356           0 :   struct nlmsghdr * nlh = fd_type_pun( ctx->monitor_buf+ctx->monitor_buf_off );
     357           0 :   if( FD_UNLIKELY( !NLMSG_OK( nlh, rem ) ) ) {
     358           0 :     FD_LOG_WARNING(( "malformed netlink monitor message" ));
     359           0 :     ctx->monitor_buf_off = ctx->monitor_buf_sz;
     360           0 :     return 1;
     361           0 :   }
     362           0 :   ctx->monitor_buf_off += (long)NLMSG_ALIGN( nlh->nlmsg_len );
     363           0 :   FD_DTRACE_PROBE_4( netlink_update, nlh->nlmsg_seq, nlh->nlmsg_type, nlh->nlmsg_len, nlh->nlmsg_flags );
     364           0 :   switch( nlh->nlmsg_type ) {
     365           0 :     case RTM_NEWLINK:
     366           0 :     case RTM_DELLINK:
     367           0 :       ctx->action |= FD_NET_TILE_ACTION_LINK_UPDATE;
     368           0 :       ctx->metrics.update_cnt[ FD_METRICS_ENUM_NETLINK_MESSAGE_TYPE_V_LINK_IDX ]++;
     369           0 :       break;
     370           0 :     case RTM_NEWROUTE:
     371           0 :     case RTM_DELROUTE: {
     372           0 :       fd_iproute_msg_t route;
     373           0 :       if( fd_fib4_netlink_translate( nlh, RT_TABLE_LOCAL, &route ) ||
     374           0 :           fd_fib4_netlink_translate( nlh, RT_TABLE_MAIN,  &route ) ) {
     375           0 :         iproute_publish( ctx, stem, &route );
     376           0 :         ctx->metrics.update_cnt[ FD_METRICS_ENUM_NETLINK_MESSAGE_TYPE_V_IPV4_ROUTE_IDX ]++;
     377           0 :         return 2;
     378           0 :       }
     379           0 :       ctx->metrics.update_cnt[ FD_METRICS_ENUM_NETLINK_MESSAGE_TYPE_V_IPV4_ROUTE_IDX ]++;
     380           0 :       break;
     381           0 :     }
     382           0 :     case RTM_NEWNEIGH:
     383           0 :     case RTM_DELNEIGH: {
     384           0 :       fd_neigh4_netlink_ingest_message( ctx->neigh4, nlh, ctx->neigh4_ifidx );
     385           0 :       ctx->metrics.update_cnt[ FD_METRICS_ENUM_NETLINK_MESSAGE_TYPE_V_NEIGHBOR_IDX ]++;
     386           0 :       break;
     387           0 :     }
     388           0 :     default:
     389           0 :       FD_LOG_INFO(( "Received unexpected netlink message type %u", nlh->nlmsg_type ));
     390           0 :       break;
     391           0 :   }
     392           0 :   return 1;
     393           0 : }
     394             : 
     395             : static void
     396           0 : during_housekeeping( fd_netlink_tile_ctx_t * ctx ) {
     397           0 :   long now = fd_tickcount();
     398           0 :   if( !ctx->dump_table_id && (ctx->action & FD_NET_TILE_ACTION_LINK_UPDATE) ) {
     399           0 :     if( now < ctx->link_update_ts ) return;
     400           0 :     ctx->action &= ~FD_NET_TILE_ACTION_LINK_UPDATE;
     401           0 :     fd_seqlock_write_lock( &ctx->netdev_tbl->hdr->seqlock );
     402           0 :     fd_netdev_netlink_load_table( ctx->netdev_tbl, ctx->nl_req );
     403           0 :     fd_seqlock_write_unlock( &ctx->netdev_tbl->hdr->seqlock );
     404           0 :     ctx->link_update_ts = now+ctx->update_backoff;
     405           0 :     ctx->metrics.link_full_syncs++;
     406           0 :   }
     407           0 :   if( !ctx->dump_table_id && (ctx->action & FD_NET_TILE_ACTION_NEIGH_UPDATE) ) {
     408           0 :     ctx->action &= ~FD_NET_TILE_ACTION_NEIGH_UPDATE;
     409           0 :     fd_neigh4_netlink_request_dump( ctx->nl_req, ctx->neigh4_ifidx );
     410           0 :     uchar buf[ 4096 ];
     411           0 :     fd_netlink_iter_t iter[1];
     412           0 :     for( fd_netlink_iter_init( iter, ctx->nl_req, buf, sizeof(buf) );
     413           0 :         !fd_netlink_iter_done( iter );
     414           0 :         fd_netlink_iter_next( iter, ctx->nl_req ) ) {
     415           0 :       fd_neigh4_netlink_ingest_message( ctx->neigh4, fd_netlink_iter_msg( iter ), ctx->neigh4_ifidx );
     416           0 :     }
     417           0 :   }
     418           0 : }
     419             : 
     420             : /* after_credit is called once per loop iteration when Stem has at least
     421             :    one credit available. */
     422             : 
     423             : static void
     424             : after_credit( fd_netlink_tile_ctx_t * ctx,
     425             :               fd_stem_context_t *     stem,
     426             :               int *                   opt_poll_in,
     427           0 :               int *                   charge_busy ) {
     428             : 
     429           0 :   long now = fd_tickcount();
     430             : 
     431           0 :   if( !ctx->dump_table_id && (ctx->action & FD_NET_TILE_ACTION_ROUTE4_UPDATE) && now>=ctx->route4_update_ts ) {
     432           0 :     ctx->action &= ~FD_NET_TILE_ACTION_ROUTE4_UPDATE;
     433           0 :     fd_iproute_msg_t flush = { .op=FD_IPROUTE_OP_FLUSH };
     434           0 :     iproute_publish( ctx, stem, &flush );
     435           0 :     fd_memset( ctx->dump_route_cnt, 0, sizeof(ctx->dump_route_cnt) );
     436           0 :     fd_memset( ctx->dump_peer_cnt,  0, sizeof(ctx->dump_peer_cnt)  );
     437           0 :     ctx->dump_overflow = 0;
     438           0 :     ctx->dump_table_id = RT_TABLE_LOCAL;
     439           0 :     *charge_busy = 1;
     440           0 :     *opt_poll_in = 0;
     441           0 :     return;
     442           0 :   }
     443             : 
     444           0 :   if( ctx->dump_table_id ) {
     445           0 :     if( !ctx->dump_active ) {
     446           0 :       int err = iproute_dump_begin( ctx, ctx->dump_table_id );
     447           0 :       if( FD_UNLIKELY( err ) ) {
     448           0 :         ctx->dump_table_id = 0U;
     449           0 :         ctx->action |= FD_NET_TILE_ACTION_ROUTE4_UPDATE;
     450           0 :         ctx->route4_update_ts = now+ctx->update_backoff;
     451           0 :         *charge_busy = 1;
     452           0 :         return;
     453           0 :       }
     454           0 :     }
     455             : 
     456           0 :     fd_iproute_msg_t route;
     457           0 :     int dump_res = iproute_dump_next( ctx, &route );
     458           0 :     if( dump_res==FD_NETLINK_DUMP_NEXT_ROUTE ) {
     459           0 :       iproute_publish( ctx, stem, &route );
     460           0 :       *charge_busy = 1;
     461           0 :       *opt_poll_in = 0;
     462           0 :       return;
     463           0 :     }
     464           0 :     if( dump_res==FD_NETLINK_DUMP_NEXT_PROGRESS ) {
     465           0 :       *charge_busy = 1;
     466           0 :       return;
     467           0 :     }
     468           0 :     if( FD_UNLIKELY( dump_res<0 ) ) {
     469           0 :       if( dump_res==-FD_FIB_NETLINK_ERR_SPACE ) FD_LOG_WARNING(( "routing table exceeds configured netlink route capacity" ));
     470           0 :       ctx->dump_table_id = 0U;
     471           0 :       ctx->action |= FD_NET_TILE_ACTION_ROUTE4_UPDATE;
     472           0 :       ctx->route4_update_ts = now+ctx->update_backoff;
     473           0 :       *charge_busy = 1;
     474           0 :       return;
     475           0 :     }
     476             : 
     477           0 :     if( ctx->dump_table_id==RT_TABLE_LOCAL ) {
     478           0 :       ctx->dump_table_id = RT_TABLE_MAIN;
     479           0 :     } else {
     480           0 :       ctx->dump_table_id = 0U;
     481           0 :       ctx->route4_update_ts = now+ctx->update_backoff;
     482           0 :       ctx->metrics.route_full_syncs++;
     483           0 :     }
     484           0 :     *charge_busy = 1;
     485           0 :     return;
     486           0 :   }
     487             : 
     488           0 :   int published = 0;
     489           0 :   if( FD_UNLIKELY( fd_fseq_query( ctx->waker_fseq )==1UL ) ) {
     490           0 :     fd_fseq_update( ctx->waker_fseq, 0UL );
     491           0 :     for(;;) {
     492           0 :       int read_res = netlink_monitor_read( ctx, stem, MSG_DONTWAIT );
     493           0 :       if( !read_res ) break;
     494           0 :       *charge_busy = 1;
     495           0 :       if( read_res==2 ) {
     496           0 :         published = 1;
     497           0 :         break;
     498           0 :       }
     499           0 :     }
     500           0 :     fd_waker_client_rearm( ctx->waker_client_idx );
     501           0 :     ctx->idle_cnt = -1L;
     502           0 :   }
     503             : 
     504           0 :   ctx->idle_cnt++;
     505           0 :   if( FD_UNLIKELY( !published && ctx->idle_cnt>=128L && !stem->sleep ) )
     506           0 :     fd_log_sleep( (long)1e6 );
     507             : 
     508           0 :   if( FD_UNLIKELY( published ) ) *opt_poll_in = 0;
     509           0 : }
     510             : 
     511             : /* after_poll_overrun is called when fd_stem.c was overrun while
     512             :    checking for new fragments: producers are hot, stay hot. */
     513             : 
     514             : static void
     515           0 : after_poll_overrun( fd_netlink_tile_ctx_t * ctx ) {
     516           0 :   ctx->idle_cnt = -1L;
     517           0 : }
     518             : 
     519             : /* after_frag handles a neighbor solicit request */
     520             : 
     521             : static void
     522             : after_frag( fd_netlink_tile_ctx_t * ctx,
     523             :             ulong                   in_idx,
     524             :             ulong                   seq,
     525             :             ulong                   sig,
     526             :             ulong                   sz,
     527             :             ulong                   tsorig,
     528             :             ulong                   tspub,
     529           0 :             fd_stem_context_t *     stem ) {
     530           0 :   (void)in_idx; (void)seq; (void)tsorig; (void)tspub; (void)stem;
     531             : 
     532           0 :   ctx->idle_cnt = -1L;
     533           0 :   long now = fd_tickcount();
     534             : 
     535             :   /* Parse request (fully contained in sig field) */
     536             : 
     537           0 :   if( FD_UNLIKELY( sz!=0UL ) ) {
     538           0 :     FD_LOG_WARNING(( "unexpected sz %lu", sz ));
     539           0 :   }
     540           0 :   if( FD_UNLIKELY( sig==FD_NETLINK_ROUTE4_SYNC_SIG ) ) {
     541           0 :     ctx->action |= FD_NET_TILE_ACTION_ROUTE4_UPDATE;
     542           0 :     return;
     543           0 :   }
     544           0 :   uint if_idx   = (uint)(sig>>32);
     545           0 :   uint ip4_addr = (uint)sig;
     546           0 :   if( FD_UNLIKELY( if_idx!=ctx->neigh4_ifidx ) ) {
     547           0 :     FD_LOG_ERR(( "received neighbor solicit request for invalid interface index %u", if_idx ));
     548           0 :     return;
     549           0 :   }
     550             : 
     551             :   /* Drop if the kernel is already working on the request */
     552           0 :   if( fd_neigh4_hmap_query( ctx->neigh4, &ip4_addr ) ) {
     553           0 :     ctx->metrics.neigh_probe[ FD_METRICS_ENUM_NEIGHBOR_PROBE_RESULT_V_IN_FLIGHT_IDX ]++;
     554           0 :     return;
     555           0 :   }
     556             : 
     557             :   /* Insert placeholder (take above branch next time) */
     558             : 
     559           0 :   fd_neigh4_entry_t * ele = fd_neigh4_hmap_insert( ctx->neigh4, &ip4_addr );
     560           0 :   if( FD_UNLIKELY( !ele ) ) {
     561           0 :     ctx->metrics.neigh_probe[ FD_METRICS_ENUM_NEIGHBOR_PROBE_RESULT_V_TABLE_FULL_IDX ]++;
     562           0 :     return;
     563           0 :   }
     564             :   /* Atomically write the entry, initializing MAC and probe suppression timestamp to 0 */
     565           0 :   fd_neigh4_entry_t to_insert = (fd_neigh4_entry_t) {
     566           0 :     .ip4_addr = ip4_addr,
     567           0 :     .state    = FD_NEIGH4_STATE_INCOMPLETE,
     568           0 :   };
     569           0 :   fd_neigh4_entry_atomic_st( ele, &to_insert );
     570             : 
     571             :   /* Trigger neighbor solicit via netlink */
     572             : 
     573           0 :   int probe_res = fd_neigh4_probe_rate_limited( ctx->prober, ele, ip4_addr, now );
     574           0 :   if( probe_res==0 ) {
     575           0 :     ctx->metrics.neigh_probe[ FD_METRICS_ENUM_NEIGHBOR_PROBE_RESULT_V_SENT_IDX ]++;
     576           0 :   } else {
     577           0 :     fd_neigh4_hmap_remove( ctx->neigh4, ele );
     578           0 :     if( probe_res>0 ) ctx->metrics.neigh_probe[ FD_METRICS_ENUM_NEIGHBOR_PROBE_RESULT_V_SEND_FAILED_IDX ]++;
     579           0 :   }
     580             : 
     581           0 : }
     582             : 
     583           0 : #define STEM_BURST (1UL)
     584           0 : #define STEM_LAZY ((ulong)13e6) /* 13ms */
     585             : 
     586           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_netlink_tile_ctx_t
     587           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_netlink_tile_ctx_t)
     588             : 
     589           0 : #define STEM_CALLBACK_METRICS_WRITE       metrics_write
     590           0 : #define STEM_CALLBACK_AFTER_POLL_OVERRUN  after_poll_overrun
     591           0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
     592           0 : #define STEM_CALLBACK_AFTER_CREDIT        after_credit
     593           0 : #define STEM_CALLBACK_AFTER_FRAG          after_frag
     594             : 
     595             : #include "../stem/fd_stem.c"
     596             : 
     597             : /* End stem methods */
     598             : 
     599             : fd_topo_run_tile_t fd_tile_netlnk = {
     600             :   .name                     = "netlnk",
     601             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     602             :   .populate_allowed_fds     = populate_allowed_fds,
     603             :   .scratch_align            = scratch_align,
     604             :   .scratch_footprint        = scratch_footprint,
     605             :   .privileged_init          = privileged_init,
     606             :   .unprivileged_init        = unprivileged_init,
     607             :   .run                      = stem_run
     608             : };

Generated by: LCOV version 1.14