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

Generated by: LCOV version 1.14