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_dbl_buf_obj = fd_topob_obj( topo, "dbl_buf", "netbase" );
31 0 : fd_topo_obj_t * fib4_main_obj = fd_topob_obj( topo, "fib4", "netbase" );
32 0 : fd_topo_obj_t * fib4_local_obj = fd_topob_obj( topo, "fib4", "netbase" );
33 0 : fd_topo_obj_t * neigh4_obj = fd_topob_obj( topo, "neigh4_hmap", "netbase" );
34 :
35 0 : fd_topob_tile_uses( topo, netlink_tile, netdev_dbl_buf_obj, FD_SHMEM_JOIN_MODE_READ_WRITE );
36 0 : fd_topob_tile_uses( topo, netlink_tile, fib4_main_obj, FD_SHMEM_JOIN_MODE_READ_WRITE );
37 0 : fd_topob_tile_uses( topo, netlink_tile, fib4_local_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 double buffer of netdev table */
41 0 : ulong const netdev_dbl_buf_mtu = fd_netdev_tbl_footprint( NETDEV_MAX, BOND_MASTER_MAX );
42 0 : FD_TEST( fd_pod_insertf_ulong( topo->props, netdev_dbl_buf_mtu, "obj.%lu.mtu", netdev_dbl_buf_obj->id ) );
43 :
44 : /* Configure route table */
45 0 : FD_TEST( fd_pod_insertf_ulong( topo->props, netlnk_max_routes, "obj.%lu.route_max", fib4_main_obj->id ) );
46 0 : FD_TEST( fd_pod_insertf_ulong( topo->props, netlnk_max_routes, "obj.%lu.route_max", fib4_local_obj->id ) );
47 0 : FD_TEST( fd_pod_insertf_ulong( topo->props, netlnk_max_peer_routes, "obj.%lu.route_peer_max", fib4_main_obj->id ) );
48 0 : FD_TEST( fd_pod_insertf_ulong( topo->props, netlnk_max_peer_routes, "obj.%lu.route_peer_max", fib4_local_obj->id ) );
49 0 : ulong fib4_seed;
50 0 : FD_TEST( 8UL==getrandom( &fib4_seed, sizeof(ulong), 0 ) );
51 0 : FD_TEST( fd_pod_insertf_ulong( topo->props, fib4_seed, "obj.%lu.route_peer_seed", fib4_local_obj->id ) );
52 0 : FD_TEST( fd_pod_insertf_ulong( topo->props, fib4_seed, "obj.%lu.route_peer_seed", fib4_main_obj->id ) );
53 :
54 : /* Configure neighbor hashmap */
55 0 : FD_TEST( fd_pod_insertf_ulong( topo->props, netlnk_max_neighbors, "obj.%lu.ele_max", neigh4_obj->id ) );
56 0 : FD_TEST( fd_pod_insertf_ulong( topo->props, 16UL, "obj.%lu.probe_max", neigh4_obj->id ) );
57 0 : ulong neigh4_seed;
58 0 : FD_TEST( 8UL==getrandom( &neigh4_seed, sizeof(ulong), 0 ) );
59 0 : FD_TEST( fd_pod_insertf_ulong( topo->props, neigh4_seed, "obj.%lu.seed", neigh4_obj->id ) );
60 :
61 0 : netlink_tile->netlink.netdev_dbl_buf_obj_id = netdev_dbl_buf_obj->id;
62 0 : netlink_tile->netlink.fib4_main_obj_id = fib4_main_obj->id;
63 0 : netlink_tile->netlink.fib4_local_obj_id = fib4_local_obj->id;
64 0 : memcpy( netlink_tile->netlink.neigh_if, bind_interface, sizeof(netlink_tile->netlink.neigh_if) );
65 0 : netlink_tile->netlink.neigh4_obj_id = neigh4_obj->id;
66 0 : }
67 :
68 : void
69 : fd_netlink_topo_join( fd_topo_t * topo,
70 : fd_topo_tile_t * netlink_tile,
71 0 : fd_topo_tile_t * join_tile ) {
72 0 : fd_topob_tile_uses( topo, join_tile, &topo->objs[ netlink_tile->netlink.neigh4_obj_id ], FD_SHMEM_JOIN_MODE_READ_ONLY );
73 0 : fd_topob_tile_uses( topo, join_tile, &topo->objs[ netlink_tile->netlink.fib4_main_obj_id ], FD_SHMEM_JOIN_MODE_READ_ONLY );
74 0 : fd_topob_tile_uses( topo, join_tile, &topo->objs[ netlink_tile->netlink.fib4_local_obj_id ], FD_SHMEM_JOIN_MODE_READ_ONLY );
75 0 : }
76 :
77 : /* Begin tile methods */
78 :
79 : FD_FN_CONST static inline ulong
80 0 : scratch_align( void ) {
81 0 : return fd_ulong_max( alignof(fd_netlink_tile_ctx_t), FD_NETDEV_TBL_ALIGN );
82 0 : }
83 :
84 : FD_FN_PURE static inline ulong
85 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
86 0 : (void)tile;
87 0 : ulong l = FD_LAYOUT_INIT;
88 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_netlink_tile_ctx_t), sizeof(fd_netlink_tile_ctx_t) );
89 0 : l = FD_LAYOUT_APPEND( l, fd_netdev_tbl_align(), fd_netdev_tbl_footprint( NETDEV_MAX, BOND_MASTER_MAX ) );
90 0 : return FD_LAYOUT_FINI( l, scratch_align() );
91 0 : }
92 :
93 : static ulong
94 : populate_allowed_seccomp( fd_topo_t const * topo,
95 : fd_topo_tile_t const * tile,
96 : ulong out_cnt,
97 0 : struct sock_filter * out ) {
98 0 : fd_netlink_tile_ctx_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
99 0 : FD_TEST( ctx->magic==FD_NETLINK_TILE_CTX_MAGIC );
100 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 );
101 0 : return sock_filter_policy_netlink_instr_cnt;
102 0 : }
103 :
104 : static ulong
105 : populate_allowed_fds( fd_topo_t const * topo,
106 : fd_topo_tile_t const * tile,
107 : ulong out_fds_cnt,
108 0 : int * out_fds ) {
109 0 : fd_netlink_tile_ctx_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
110 0 : FD_TEST( ctx->magic==FD_NETLINK_TILE_CTX_MAGIC );
111 :
112 0 : if( FD_UNLIKELY( out_fds_cnt<5UL ) ) FD_LOG_ERR(( "out_fds_cnt too low (%lu)", out_fds_cnt ));
113 :
114 0 : ulong out_cnt = 0UL;
115 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
116 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
117 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
118 0 : out_fds[ out_cnt++ ] = ctx->nl_monitor->fd;
119 0 : out_fds[ out_cnt++ ] = ctx->nl_req->fd;
120 0 : out_fds[ out_cnt++ ] = ctx->prober->sock_fd;
121 0 : return out_cnt;
122 0 : }
123 :
124 : static void
125 : privileged_init( fd_topo_t * topo,
126 0 : fd_topo_tile_t * tile ) {
127 0 : if( FD_UNLIKELY( tile->kind_id!=0 ) ) {
128 0 : FD_LOG_ERR(( "Topology contains more than one netlink tile" ));
129 0 : }
130 :
131 0 : uint const neigh_if_idx = if_nametoindex( tile->netlink.neigh_if );
132 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 ) ));
133 :
134 0 : fd_netlink_tile_ctx_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
135 0 : fd_memset( ctx, 0, sizeof(fd_netlink_tile_ctx_t) );
136 0 : ctx->magic = FD_NETLINK_TILE_CTX_MAGIC;
137 0 : ctx->neigh4_ifidx = neigh_if_idx;
138 :
139 0 : if( FD_UNLIKELY( !fd_netlink_init( ctx->nl_monitor, 1000U ) ) ) {
140 0 : FD_LOG_ERR(( "Failed to connect to rtnetlink" ));
141 0 : }
142 0 : if( FD_UNLIKELY( !fd_netlink_init( ctx->nl_req, 9000000U ) ) ) {
143 0 : FD_LOG_ERR(( "Failed to connect to rtnetlink" ));
144 0 : }
145 :
146 0 : union {
147 0 : struct sockaddr sa;
148 0 : struct sockaddr_nl sanl;
149 0 : } sa;
150 0 : sa.sanl = (struct sockaddr_nl) {
151 0 : .nl_family = AF_NETLINK,
152 0 : .nl_groups = RTMGRP_LINK | RTMGRP_NEIGH | RTMGRP_IPV4_ROUTE
153 0 : };
154 0 : if( FD_UNLIKELY( 0!=bind( ctx->nl_monitor->fd, &sa.sa, sizeof(struct sockaddr_nl) ) ) ) {
155 0 : FD_LOG_ERR(( "bind(sock,RT_NETLINK,RTMGRP_{LINK,NEIGH,IPV4_ROUTE}) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
156 0 : }
157 :
158 0 : float const max_probes_per_second = 3.f;
159 0 : ulong const max_probe_burst = 128UL;
160 0 : float const probe_delay_seconds = 15.f;
161 0 : fd_neigh4_prober_init( ctx->prober, max_probes_per_second, max_probe_burst, probe_delay_seconds );
162 :
163 : /* Set duration of blocking reads in before_credit */
164 0 : struct timeval tv = { .tv_usec = 2000 }; /* 2ms */
165 0 : if( FD_UNLIKELY( 0!=setsockopt( ctx->nl_monitor->fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval) ) ) ) {
166 0 : FD_LOG_ERR(( "setsockopt(sock,SOL_SOCKET,SO_RCVTIMEO) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
167 0 : }
168 0 : }
169 :
170 : static void
171 : unprivileged_init( fd_topo_t * topo,
172 0 : fd_topo_tile_t * tile ) {
173 0 : FD_SCRATCH_ALLOC_INIT( l, fd_topo_obj_laddr( topo, tile->tile_obj_id ) );
174 0 : fd_netlink_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_netlink_tile_ctx_t), sizeof(fd_netlink_tile_ctx_t) );
175 0 : FD_TEST( ctx->magic==FD_NETLINK_TILE_CTX_MAGIC );
176 0 : ctx->netdev_sz = fd_netdev_tbl_footprint( NETDEV_MAX, BOND_MASTER_MAX );
177 0 : ctx->netdev_local = FD_SCRATCH_ALLOC_APPEND( l, fd_netdev_tbl_align(), ctx->netdev_sz );
178 :
179 0 : FD_TEST( tile->netlink.netdev_dbl_buf_obj_id );
180 0 : FD_TEST( tile->netlink.neigh4_obj_id );
181 0 : FD_TEST( tile->netlink.fib4_local_obj_id );
182 0 : FD_TEST( tile->netlink.fib4_main_obj_id );
183 :
184 0 : FD_TEST( fd_netdev_tbl_new( ctx->netdev_local, NETDEV_MAX, BOND_MASTER_MAX ) );
185 0 : FD_TEST( fd_netdev_tbl_join( ctx->netdev_tbl, ctx->netdev_local ) );
186 :
187 0 : FD_TEST( ctx->netdev_buf = fd_dbl_buf_join( fd_topo_obj_laddr( topo, tile->netlink.netdev_dbl_buf_obj_id ) ) );
188 :
189 0 : ulong neigh4_obj_id = tile->netlink.neigh4_obj_id;
190 0 : ulong neigh_ele_max = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "obj.%lu.ele_max", neigh4_obj_id );
191 0 : ulong neigh_probe_max = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "obj.%lu.probe_max", neigh4_obj_id );
192 0 : ulong neigh_seed = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "obj.%lu.seed", neigh4_obj_id );
193 0 : FD_TEST( neigh_ele_max!=ULONG_MAX && neigh_probe_max!=ULONG_MAX && neigh_seed!=ULONG_MAX );
194 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 ) );
195 :
196 0 : FD_TEST( fd_fib4_join( ctx->fib4_local, fd_topo_obj_laddr( topo, tile->netlink.fib4_local_obj_id ) ) );
197 0 : FD_TEST( fd_fib4_join( ctx->fib4_main, fd_topo_obj_laddr( topo, tile->netlink.fib4_main_obj_id ) ) );
198 :
199 0 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
200 0 : fd_topo_link_t * link = &topo->links[ tile->in_link_id[ i ] ];
201 0 : if( FD_UNLIKELY( link->mtu!=0UL ) ) FD_LOG_ERR(( "netlink solicit links must have an MTU of zero" ));
202 0 : }
203 :
204 0 : ctx->action |= FD_NET_TILE_ACTION_LINK_UPDATE;
205 0 : ctx->action |= FD_NET_TILE_ACTION_ROUTE4_UPDATE;
206 0 : ctx->action |= FD_NET_TILE_ACTION_NEIGH_UPDATE;
207 :
208 0 : ctx->update_backoff = (long)( fd_tempo_tick_per_ns( NULL ) * 10e6 ); /* 10ms */
209 0 : }
210 :
211 : /* Begin stem methods
212 :
213 : Note: Using stem here might seem odd since fd_netlink_tile does not
214 : send or receive any messages. Use of stem here is justified because of
215 : the initialization, generic metrics, and event loop functionality it
216 : provides. */
217 :
218 : static inline void
219 0 : metrics_write( fd_netlink_tile_ctx_t * ctx ) {
220 0 : FD_MCNT_SET( NETLNK, DROP_EVENTS, fd_netlink_enobufs_cnt );
221 0 : FD_MCNT_SET( NETLNK, LINK_FULL_SYNCS, ctx->metrics.link_full_syncs );
222 0 : FD_MCNT_SET( NETLNK, ROUTE_FULL_SYNCS, ctx->metrics.route_full_syncs );
223 0 : FD_MCNT_ENUM_COPY( NETLNK, UPDATES, ctx->metrics.update_cnt );
224 0 : FD_MGAUGE_SET( NETLNK, INTERFACE_COUNT, ctx->netdev_tbl->hdr->dev_cnt );
225 0 : FD_MGAUGE_SET( NETLNK, ROUTE_COUNT_LOCAL, fd_fib4_cnt( ctx->fib4_local ) );
226 0 : FD_MGAUGE_SET( NETLNK, ROUTE_COUNT_MAIN, fd_fib4_cnt( ctx->fib4_main ) );
227 0 : FD_MCNT_SET( NETLNK, NEIGH_PROBE_SENT, ctx->metrics.neigh_solicits_sent );
228 0 : FD_MCNT_SET( NETLNK, NEIGH_PROBE_FAILS, ctx->metrics.neigh_solicits_fails );
229 0 : FD_MCNT_SET( NETLNK, NEIGH_PROBE_RATE_LIMIT_HOST, ctx->prober->local_rate_limited_cnt );
230 0 : FD_MCNT_SET( NETLNK, NEIGH_PROBE_RATE_LIMIT_GLOBAL, ctx->prober->global_rate_limited_cnt );
231 0 : }
232 :
233 : /* netlink_monitor_read calls recvfrom to process a link, route, or
234 : neighbor update. Returns 1 if a message was read, 0 otherwise. */
235 :
236 : static int
237 : netlink_monitor_read( fd_netlink_tile_ctx_t * ctx,
238 0 : int flags ) {
239 :
240 0 : uchar msg[ 16384 ];
241 0 : long msg_sz = recvfrom( ctx->nl_monitor->fd, msg, sizeof(msg), flags, NULL, NULL );
242 0 : if( msg_sz<=0L ) {
243 0 : if( FD_LIKELY( errno==EAGAIN || errno==EINTR ) ) return 0;
244 0 : if( errno==ENOBUFS ) {
245 0 : fd_netlink_enobufs_cnt++;
246 0 : return 0;
247 0 : }
248 0 : FD_LOG_ERR(( "recvfrom(nl_monitor) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
249 0 : }
250 :
251 0 : struct nlmsghdr * nlh = fd_type_pun( msg );
252 0 : FD_DTRACE_PROBE_4( netlink_update, nlh->nlmsg_seq, nlh->nlmsg_type, nlh->nlmsg_len, nlh->nlmsg_flags );
253 0 : switch( nlh->nlmsg_type ) {
254 0 : case RTM_NEWLINK:
255 0 : case RTM_DELLINK:
256 0 : ctx->action |= FD_NET_TILE_ACTION_LINK_UPDATE;
257 0 : ctx->metrics.update_cnt[ FD_METRICS_ENUM_NETLINK_MSG_V_LINK_IDX ]++;
258 0 : break;
259 0 : case RTM_NEWROUTE:
260 0 : case RTM_DELROUTE:
261 0 : ctx->action |= FD_NET_TILE_ACTION_ROUTE4_UPDATE;
262 0 : ctx->metrics.update_cnt[ FD_METRICS_ENUM_NETLINK_MSG_V_IPV4_ROUTE_IDX ]++;
263 0 : break;
264 0 : case RTM_NEWNEIGH:
265 0 : case RTM_DELNEIGH: {
266 0 : fd_neigh4_netlink_ingest_message( ctx->neigh4, nlh, ctx->neigh4_ifidx );
267 0 : ctx->metrics.update_cnt[ FD_METRICS_ENUM_NETLINK_MSG_V_NEIGH_IDX ]++;
268 0 : break;
269 0 : }
270 0 : default:
271 0 : FD_LOG_INFO(( "Received unexpected netlink message type %u", nlh->nlmsg_type ));
272 0 : break;
273 0 : }
274 :
275 0 : return 1;
276 0 : }
277 :
278 : static void
279 0 : during_housekeeping( fd_netlink_tile_ctx_t * ctx ) {
280 0 : long now = fd_tickcount();
281 0 : if( ctx->action & FD_NET_TILE_ACTION_LINK_UPDATE ) {
282 0 : if( now < ctx->link_update_ts ) return;
283 0 : ctx->action &= ~FD_NET_TILE_ACTION_LINK_UPDATE;
284 0 : fd_netdev_netlink_load_table( ctx->netdev_tbl, ctx->nl_req );
285 0 : fd_dbl_buf_insert( ctx->netdev_buf, ctx->netdev_local, ctx->netdev_sz );
286 0 : ctx->link_update_ts = now+ctx->update_backoff;
287 0 : ctx->metrics.link_full_syncs++;
288 0 : }
289 0 : if( ctx->action & FD_NET_TILE_ACTION_ROUTE4_UPDATE ) {
290 0 : if( now < ctx->route4_update_ts ) return;
291 0 : ctx->action &= ~FD_NET_TILE_ACTION_ROUTE4_UPDATE;
292 0 : fd_fib4_netlink_load_table( ctx->fib4_local, ctx->nl_req, RT_TABLE_LOCAL );
293 0 : fd_fib4_netlink_load_table( ctx->fib4_main, ctx->nl_req, RT_TABLE_MAIN );
294 0 : ctx->route4_update_ts = now+ctx->update_backoff;
295 0 : ctx->metrics.route_full_syncs++;
296 0 : }
297 0 : if( ctx->action & FD_NET_TILE_ACTION_NEIGH_UPDATE ) {
298 0 : ctx->action &= ~FD_NET_TILE_ACTION_NEIGH_UPDATE;
299 0 : fd_neigh4_netlink_request_dump( ctx->nl_req, ctx->neigh4_ifidx );
300 0 : uchar buf[ 4096 ];
301 0 : fd_netlink_iter_t iter[1];
302 0 : for( fd_netlink_iter_init( iter, ctx->nl_req, buf, sizeof(buf) );
303 0 : !fd_netlink_iter_done( iter );
304 0 : fd_netlink_iter_next( iter, ctx->nl_req ) ) {
305 0 : fd_neigh4_netlink_ingest_message( ctx->neigh4, fd_netlink_iter_msg( iter ), ctx->neigh4_ifidx );
306 0 : }
307 0 : }
308 0 : }
309 :
310 : /* before_credit is called once per loop iteration */
311 :
312 : static void
313 : before_credit( fd_netlink_tile_ctx_t * ctx,
314 : fd_stem_context_t * stem FD_PARAM_UNUSED,
315 0 : int * charge_busy ) {
316 :
317 0 : for(;;) {
318 : /* Clear socket buffer */
319 0 : if( !netlink_monitor_read( ctx, MSG_DONTWAIT ) ) break;
320 0 : *charge_busy = 1;
321 0 : }
322 :
323 0 : ctx->idle_cnt++;
324 0 : if( FD_UNLIKELY( ctx->idle_cnt >= 128L ) ) {
325 : /* Blocking read (yield to scheduler) */
326 0 : *charge_busy = 0;
327 0 : netlink_monitor_read( ctx, 0 );
328 0 : }
329 :
330 0 : }
331 :
332 : /* after_poll_overrun is called when fd_stem.c was overrun while
333 : checking for new fragments. This typically happens when
334 : before_credit takes too long (e.g. we were in a blocking netlink
335 : read) */
336 :
337 : static void
338 0 : after_poll_overrun( fd_netlink_tile_ctx_t * ctx ) {
339 0 : ctx->idle_cnt = -1L;
340 0 : }
341 :
342 : /* after_frag handles a neighbor solicit request */
343 :
344 : static void
345 : after_frag( fd_netlink_tile_ctx_t * ctx,
346 : ulong in_idx,
347 : ulong seq,
348 : ulong sig,
349 : ulong sz,
350 : ulong tsorig,
351 : ulong tspub,
352 0 : fd_stem_context_t * stem ) {
353 0 : (void)in_idx; (void)seq; (void)tsorig; (void)tspub; (void)stem;
354 :
355 0 : long now = fd_tickcount();
356 0 : ctx->idle_cnt = -1L;
357 :
358 : /* Parse request (fully contained in sig field) */
359 :
360 0 : if( FD_UNLIKELY( sz!=0UL ) ) {
361 0 : FD_LOG_WARNING(( "unexpected sz %lu", sz ));
362 0 : }
363 0 : if( FD_UNLIKELY( sig>>48 ) ) {
364 0 : FD_LOG_WARNING(( "unexpected high bits in sig %016lx", sig ));
365 0 : }
366 0 : ushort if_idx = (ushort)(sig>>32);
367 0 : uint ip4_addr = (uint)sig;
368 0 : if( FD_UNLIKELY( if_idx!=ctx->neigh4_ifidx ) ) {
369 0 : ctx->metrics.neigh_solicits_fails++;
370 0 : FD_LOG_ERR(( "received neighbor solicit request for invalid interface index %u", if_idx ));
371 0 : return;
372 0 : }
373 :
374 : /* Drop if the kernel is already working on the request */
375 0 : if( fd_neigh4_hmap_query( ctx->neigh4, &ip4_addr ) ) {
376 0 : ctx->metrics.neigh_solicits_fails++;
377 0 : return;
378 0 : }
379 :
380 : /* Insert placeholder (take above branch next time) */
381 :
382 0 : fd_neigh4_entry_t * ele = fd_neigh4_hmap_insert( ctx->neigh4, &ip4_addr );
383 0 : if( FD_UNLIKELY( !ele ) ) {
384 0 : ctx->metrics.neigh_solicits_fails++;
385 0 : return;
386 0 : }
387 : /* Atomically write the entry, initializing MAC and probe suppression timestamp to 0 */
388 0 : fd_neigh4_entry_t to_insert = (fd_neigh4_entry_t) {
389 0 : .ip4_addr = ip4_addr,
390 0 : .state = FD_NEIGH4_STATE_INCOMPLETE,
391 0 : };
392 0 : fd_neigh4_entry_atomic_st( ele, &to_insert );
393 :
394 : /* Trigger neighbor solicit via netlink */
395 :
396 0 : int probe_res = fd_neigh4_probe_rate_limited( ctx->prober, ele, ip4_addr, now );
397 0 : if( probe_res==0 ) {
398 0 : ctx->metrics.neigh_solicits_sent++;
399 0 : } else if( probe_res>0 ) {
400 0 : ctx->metrics.neigh_solicits_fails++;
401 0 : }
402 :
403 0 : }
404 :
405 0 : #define STEM_BURST (1UL)
406 0 : #define STEM_LAZY ((ulong)13e6) /* 13ms */
407 :
408 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_netlink_tile_ctx_t
409 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_netlink_tile_ctx_t)
410 :
411 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
412 0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
413 0 : #define STEM_CALLBACK_BEFORE_CREDIT before_credit
414 0 : #define STEM_CALLBACK_AFTER_POLL_OVERRUN after_poll_overrun
415 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
416 :
417 : #include "../stem/fd_stem.c"
418 :
419 : /* End stem methods */
420 :
421 : fd_topo_run_tile_t fd_tile_netlnk = {
422 : .name = "netlnk",
423 : .populate_allowed_seccomp = populate_allowed_seccomp,
424 : .populate_allowed_fds = populate_allowed_fds,
425 : .scratch_align = scratch_align,
426 : .scratch_footprint = scratch_footprint,
427 : .privileged_init = privileged_init,
428 : .unprivileged_init = unprivileged_init,
429 : .run = stem_run
430 : };
|