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