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