Line data Source code
1 : /* The xdp tile translates between AF_XDP and fd_tango
2 : traffic. It is responsible for setting up the XDP and
3 : XSK socket configuration. */
4 :
5 : #include "../fd_net_tile.h"
6 : #include "../fd_net_tile_private.h"
7 :
8 : #include <errno.h>
9 : #include <fcntl.h>
10 : #include <net/if.h>
11 : #include <netinet/in.h>
12 : #include <sys/socket.h> /* MSG_DONTWAIT needed before importing the net seccomp filter */
13 : #include <linux/if_xdp.h>
14 : #include <sys/epoll.h>
15 :
16 : #include "../../../discof/repair/fd_repair.h"
17 : #include "../../metrics/fd_metrics.h"
18 : #include "../../netlink/fd_netlink_tile.h" /* neigh4_solicit */
19 : #include "../../waker/fd_waker.h"
20 : #include "../../topo/fd_topo.h"
21 :
22 : #include "../../../waltz/ip/fd_fib4.h"
23 : #include "../../../waltz/ip/fd_iproute.h"
24 : #include "../../../waltz/neigh/fd_neigh4_map.h"
25 : #include "../../../waltz/mib/fd_netdev_tbl.h"
26 : #include "../../../waltz/xdp/fd_xdp_redirect_user.h" /* fd_xsk_activate */
27 : #include "../../../waltz/xdp/fd_xsk.h"
28 : #include "../../../util/log/fd_dtrace.h"
29 : #include "../../../util/net/fd_eth.h"
30 : #include "../../../util/net/fd_ip4.h"
31 : #include "../../../util/net/fd_gre.h"
32 : #include "../../../util/pod/fd_pod_format.h"
33 :
34 : #include <unistd.h>
35 : #include <linux/if.h> /* struct ifreq */
36 : #include <sys/ioctl.h>
37 : #include <linux/if_arp.h>
38 : #include <linux/rtnetlink.h>
39 : #include <linux/futex.h>
40 :
41 : #include "generated/fd_xdp_tile_seccomp.h"
42 :
43 :
44 : /* FD_XDP_STATS_INTERVAL_NS controls the XDP stats refresh interval.
45 : This should be lower than the interval at which the metrics tile
46 : collects metrics. */
47 :
48 0 : #define FD_XDP_STATS_INTERVAL_NS (11e6) /* 11ms */
49 :
50 : /* XSK_IDX_{MAIN,LO} are the hardcoded XSK indices in ctx->xsk[ ... ].
51 : Only net tile 0 has XSK_IDX_LO, all net tiles have XSK_IDX_MAIN. */
52 :
53 0 : #define XSK_IDX_MAIN 0
54 0 : #define XSK_IDX_LO 1
55 :
56 : /* XSK 'busy_poll_usecs' value (max amount of time
57 : spent spinning in a NAPI poll before returning back to
58 : userspace if the processing budget hasn't already ran out).
59 :
60 : 64us chosen based on napibusy configuration tested and
61 : shown in https://lwn.net/Articles/997491/ Linux patch
62 : cover letter. Chosen over fullbusy since for Firedancer
63 : the values of fullbusy are unnecessarily high and could
64 : cause some extra latency to regular non-Firedancer traffic.*/
65 0 : #define PREFBUSY_TIME_BUDGET_MICROS (64L)
66 :
67 : /* PREFBUSY_RX_BUDGET is the NAPI RX processing budget (max num RX
68 : packets that the NIC driver can move from the hw rings into the
69 : XSK rings per poll).
70 :
71 : Default RX budget used by NIC drivers is 64, therefore
72 : it is safest to use 64 in prefbusy polling. Also reduces
73 : TX starvation risks as the TX budget set by the NIC driver
74 : is also generally 64. */
75 0 : #define PREFBUSY_RX_BUDGET (64L)
76 :
77 : /* Min time between each prefbusy poll. Necessary to avoid a no RX
78 : scenario livelocking TX with overly frequent sendto calls, given
79 : prefbusy polls whenever the RX queue is empty.
80 :
81 : Value chosen based on experimentation on ixgbe, mlx5 and i40e as well
82 : as on varying CPUs and clock speeds. Too low -> lower max TX
83 : throughput when RX is very low. Too high -> lower max RX and TX throughput. */
84 0 : #define PREFBUSY_MIN_INTERVAL_NS (5e3) /* 5us */
85 :
86 : /* Max time since last prefbusy poll before a prefbusy poll is
87 : forced (has been read that polling can sometimes resolve a stall).
88 :
89 : Exact value again not particularly important as this is just extra
90 : protection against stalls which have not been observed in testing but
91 : are still good to protect against since there is no cost to doing so.
92 :
93 : Value of 150us chosen since it is easily large enough to not interfere
94 : with standard prefbusy runtime unless there is a serious problem. */
95 0 : #define PREFBUSY_STALL_TIMEOUT_NS (150e3) /* 150us */
96 :
97 : /* fd_net_flusher_t controls the pacing of XDP sendto calls for flushing
98 : TX batches. In the 'wakeup' XDP mode, no TX occurs unless the net
99 : tile wakes up the kernel periodically using the sendto() syscall.
100 : If sendto() is called too frequently, time is wasted on context
101 : switches. If sendto() is called not often enough, packets are
102 : delayed or dropped. sendto() calls make almost no guarantees how
103 : much packets are sent out, nor do they indicate when the kernel
104 : finishes a wakeup call (asynchronously dispatched). The net tile
105 : thus uses a myriad of flush triggers that were tested for best
106 : performance. */
107 :
108 : struct fd_net_flusher {
109 :
110 : /* Packets that were enqueued after the last sendto() wakeup are
111 : considered "pending". If there are more than pending_wmark packets
112 : pending, a wakeup is dispatched. Thus, this dispatch trigger is
113 : proportional to packet rate, but does not trigger if I/O is seldom. */
114 : ulong pending_cnt;
115 : ulong pending_wmark;
116 :
117 : /* Sometimes, packets are not flushed out even after a sendto()
118 : wakeup. This can result in the tail of a burst getting delayed or
119 : overrun. If more than tail_flush_backoff ticks pass since the last
120 : sendto() wakeup and there are still unacknowledged packets in the
121 : TX ring, issues another wakeup. Only used by "softirq" poll mode. */
122 : long next_tail_flush_ticks;
123 : long tail_flush_backoff;
124 :
125 : /* When the most recent prefbusy poll was. */
126 : long prefbusy_last_poll_ticks;
127 : /* Min time between each prefbusy poll. */
128 : long prefbusy_min_interval_ticks;
129 : /* Max time since last prefbusy poll before a prefbusy poll is
130 : forced (has been read that polling can sometimes resolve a stall). */
131 : long prefbusy_stall_timeout_ticks;
132 : };
133 :
134 : typedef struct fd_net_flusher fd_net_flusher_t;
135 :
136 : FD_PROTOTYPES_BEGIN
137 :
138 : /* fd_net_flusher_inc marks a new packet as enqueued. */
139 :
140 : static inline void
141 : fd_net_flusher_inc( fd_net_flusher_t * flusher,
142 0 : long now ) {
143 0 : flusher->pending_cnt++;
144 0 : long next_flush = now + flusher->tail_flush_backoff;
145 0 : flusher->next_tail_flush_ticks = fd_long_min( flusher->next_tail_flush_ticks, next_flush );
146 0 : }
147 :
148 : /* fd_net_flusher_check returns 1 if a sendto() wakeup should be issued
149 : immediately. now is a recent fd_tickcount() value.
150 : If tx_ring_empty==0 then the kernel is caught up with the net tile
151 : on the XDP TX ring. (Otherwise, the kernel is behind the net tile) */
152 :
153 : static inline int
154 : fd_net_flusher_check( fd_net_flusher_t * flusher,
155 : long now,
156 0 : int tx_ring_empty ) {
157 0 : int flush_level = flusher->pending_cnt >= flusher->pending_wmark;
158 0 : int flush_timeout = now >= flusher->next_tail_flush_ticks;
159 0 : int flush = flush_level || flush_timeout;
160 0 : if( !flush ) return 0;
161 0 : if( FD_UNLIKELY( tx_ring_empty ) ) {
162 : /* Flush requested but caught up */
163 0 : flusher->pending_cnt = 0UL;
164 0 : flusher->next_tail_flush_ticks = LONG_MAX;
165 0 : return 0;
166 0 : }
167 0 : return 1;
168 0 : }
169 :
170 : /* fd_net_flusher_wakeup signals a sendto() wakeup was done. now is a
171 : recent fd_tickcount() value. */
172 :
173 : static inline void
174 : fd_net_flusher_wakeup( fd_net_flusher_t * flusher,
175 0 : long now ) {
176 0 : flusher->pending_cnt = 0UL;
177 0 : flusher->next_tail_flush_ticks = now + flusher->tail_flush_backoff;
178 0 : }
179 :
180 : FD_PROTOTYPES_END
181 :
182 : /* fd_net_free_ring is a FIFO queue that stores pointers to free XDP TX
183 : frames. */
184 :
185 : struct fd_net_free_ring {
186 : ulong prod;
187 : ulong cons;
188 : ulong depth;
189 : ulong * queue;
190 : };
191 : typedef struct fd_net_free_ring fd_net_free_ring_t;
192 :
193 : typedef struct {
194 : fd_net_tile_t net;
195 :
196 : /* An "XSK" is an AF_XDP socket */
197 : uint xsk_cnt;
198 : fd_xsk_t xsk[ 2 ];
199 : int prog_link_fds[ 2 ];
200 : uint if_virt;
201 :
202 : /* UMEM frame region within dcache */
203 : void * umem; /* Start of UMEM */
204 : ulong umem_sz; /* Size of UMEM */
205 :
206 : /* Details pertaining to an inflight send op */
207 : struct {
208 : uint xsk_idx;
209 : void * frame;
210 : } tx_op;
211 :
212 : /* Round-robin cycle service operations */
213 : uint rr_idx;
214 :
215 : /* Ring tracking free packet buffers */
216 : fd_net_free_ring_t free_tx;
217 :
218 : uchar src_mac_addr[6];
219 : uint default_address;
220 :
221 : /* XDP stats refresh timer */
222 : long xdp_stats_interval_ticks;
223 : long next_xdp_stats_refresh;
224 :
225 : /* TX flush timers */
226 : fd_net_flusher_t tx_flusher[2]; /* one per XSK */
227 :
228 : /* Route and neighbor tables */
229 : fd_fib4_t fib_local[1];
230 : fd_fib4_t fib_main[1];
231 : fd_neigh4_hmap_t neigh4[1];
232 :
233 : ulong netlnk_out_idx;
234 : uint solicit_ip;
235 : uint solicit_if_idx;
236 :
237 : ulong waker_client_idx;
238 : ulong * waker_fseq;
239 :
240 : /* Netdev table */
241 : fd_netdev_tbl_join_t netdev_tbl; /* local copy in scratch (hot path) */
242 : fd_netdev_tbl_join_t netdev_shared; /* shared table in netbase (seqlock protected) */
243 : ulong netdev_seq; /* netdev_shared seqlock value of the netdev_tbl copy */
244 :
245 : struct {
246 : ulong rx_fill_blocked_cnt;
247 : long rx_busy_cnt;
248 : long rx_idle_cnt;
249 :
250 : ulong tx_submit_cnt;
251 : ulong tx_neigh_fail_cnt;
252 : ulong tx_full_fail_cnt;
253 : long tx_busy_cnt;
254 : long tx_idle_cnt;
255 :
256 : ulong xsk_tx_wakeup_cnt;
257 : ulong xsk_rx_wakeup_cnt;
258 : } metrics;
259 : } fd_net_ctx_t;
260 :
261 : fd_fib4_t *
262 : fd_xdp_tile_fib4_join( fd_fib4_t * out,
263 : fd_topo_t const * topo,
264 : fd_topo_tile_t const * tile,
265 0 : int main_table ) {
266 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
267 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
268 0 : (void)FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_net_ctx_t), sizeof(fd_net_ctx_t) );
269 0 : (void)FD_SCRATCH_ALLOC_APPEND( l, alignof(ulong), tile->xdp.free_ring_depth*sizeof(ulong) );
270 0 : (void)FD_SCRATCH_ALLOC_APPEND( l, fd_netdev_tbl_align(), fd_netdev_tbl_footprint( NETDEV_MAX, BOND_MASTER_MAX ) );
271 0 : void * local_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_fib4_align(), fd_fib4_footprint( tile->xdp.route_max, tile->xdp.route_peer_max ) );
272 0 : void * main_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_fib4_align(), fd_fib4_footprint( tile->xdp.route_max, tile->xdp.route_peer_max ) );
273 0 : return fd_fib4_join( out, main_table ? main_mem : local_mem );
274 0 : }
275 :
276 : FD_FN_CONST static inline ulong
277 0 : scratch_align( void ) {
278 0 : return 4096UL;
279 0 : }
280 :
281 : FD_FN_PURE static inline ulong
282 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
283 0 : ulong l = FD_LAYOUT_INIT;
284 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_net_ctx_t), sizeof(fd_net_ctx_t) );
285 0 : l = FD_LAYOUT_APPEND( l, alignof(ulong), tile->xdp.free_ring_depth * sizeof(ulong) );
286 0 : l = FD_LAYOUT_APPEND( l, fd_netdev_tbl_align(), fd_netdev_tbl_footprint( NETDEV_MAX, BOND_MASTER_MAX ) );
287 0 : for( ulong i=0UL; i<2UL; i++ ) l = FD_LAYOUT_APPEND( l, fd_fib4_align(), fd_fib4_footprint( tile->xdp.route_max, tile->xdp.route_peer_max ) );
288 0 : return FD_LAYOUT_FINI( l, scratch_align() );
289 0 : }
290 :
291 : static void
292 0 : metrics_write( fd_net_ctx_t * ctx ) {
293 0 : FD_MCNT_SET( NET, PKT_RX, ctx->net.metrics.rx_pkt_cnt );
294 0 : FD_MCNT_SET( NET, PKT_RX_BYTES, ctx->net.metrics.rx_bytes_total );
295 0 : FD_MCNT_ENUM_COPY( NET, PKT_RX_DROPPED, ctx->net.metrics.rx_drop_cnt );
296 0 : FD_MCNT_SET( NET, GRE_PKT_RX, ctx->net.metrics.rx_gre_cnt );
297 0 : FD_MCNT_SET( NET, RX_FILL_RING_STALLED, ctx->metrics.rx_fill_blocked_cnt );
298 0 : FD_MGAUGE_SET( NET, RX_BUFFER_USED, (ulong)fd_long_max( ctx->metrics.rx_busy_cnt, 0L ) );
299 0 : FD_MGAUGE_SET( NET, RX_BUFFER_FREE, (ulong)fd_long_max( ctx->metrics.rx_idle_cnt, 0L ) );
300 0 : FD_MGAUGE_SET( NET, TX_BUFFER_USED, (ulong)fd_long_max( ctx->metrics.tx_busy_cnt, 0L ) );
301 0 : FD_MGAUGE_SET( NET, TX_BUFFER_FREE, (ulong)fd_long_max( ctx->metrics.tx_idle_cnt, 0L ) );
302 :
303 0 : FD_MCNT_SET( NET, PKT_TX_SUBMITTED, ctx->metrics.tx_submit_cnt );
304 0 : FD_MCNT_SET( NET, PKT_TX_COMPLETED, ctx->net.metrics.tx_pkt_cnt );
305 0 : FD_MCNT_SET( NET, PKT_TX_SUBMITTED_BYTES, ctx->net.metrics.tx_bytes_total );
306 0 : ctx->net.metrics.tx_drop_cnt[ FD_METRICS_ENUM_PKT_TX_DROP_REASON_V_NO_NEIGHBOR_IDX ] = ctx->metrics.tx_neigh_fail_cnt;
307 0 : ctx->net.metrics.tx_drop_cnt[ FD_METRICS_ENUM_PKT_TX_DROP_REASON_V_RING_FULL_IDX ] = ctx->metrics.tx_full_fail_cnt;
308 0 : FD_MCNT_ENUM_COPY( NET, PKT_TX_DROPPED, ctx->net.metrics.tx_drop_cnt );
309 :
310 0 : FD_MCNT_SET( NET, XSK_SYSCALL_TX, ctx->metrics.xsk_tx_wakeup_cnt );
311 0 : FD_MCNT_SET( NET, XSK_SYSCALL_RX, ctx->metrics.xsk_rx_wakeup_cnt );
312 :
313 0 : FD_MCNT_SET( NET, GRE_PKT_TX_SUBMITTED, ctx->net.metrics.tx_gre_cnt );
314 : /* fd_fib4_cnt includes the synthetic throw route at index zero. */
315 0 : FD_MGAUGE_SET( NET, ROUTE_COUNT_LOCAL, fd_ulong_sat_sub( fd_fib4_cnt( ctx->fib_local ), 1UL ) );
316 0 : FD_MGAUGE_SET( NET, ROUTE_COUNT_MAIN, fd_ulong_sat_sub( fd_fib4_cnt( ctx->fib_main ), 1UL ) );
317 0 : }
318 :
319 : struct xdp_statistics_v0 {
320 : __u64 rx_dropped; /* Dropped for other reasons */
321 : __u64 rx_invalid_descs; /* Dropped due to invalid descriptor */
322 : __u64 tx_invalid_descs; /* Dropped due to invalid descriptor */
323 : };
324 :
325 : struct xdp_statistics_v1 {
326 : __u64 rx_dropped; /* Dropped for other reasons */
327 : __u64 rx_invalid_descs; /* Dropped due to invalid descriptor */
328 : __u64 tx_invalid_descs; /* Dropped due to invalid descriptor */
329 : __u64 rx_ring_full; /* Dropped due to rx ring being full */
330 : __u64 rx_fill_ring_empty_descs; /* Failed to retrieve item from fill ring */
331 : __u64 tx_ring_empty_descs; /* Failed to retrieve item from tx ring */
332 : };
333 :
334 : static void
335 0 : poll_xdp_statistics( fd_net_ctx_t * ctx ) {
336 0 : struct xdp_statistics_v1 stats = {0};
337 0 : ulong xsk_cnt = ctx->xsk_cnt;
338 0 : for( ulong j=0UL; j<xsk_cnt; j++ ) {
339 0 : struct xdp_statistics_v1 sub_stats = {0};
340 0 : uint optlen = (uint)sizeof(struct xdp_statistics_v1);
341 0 : if( FD_UNLIKELY( -1==getsockopt( ctx->xsk[ j ].xsk_fd, SOL_XDP, XDP_STATISTICS, &sub_stats, &optlen ) ) )
342 0 : FD_LOG_ERR(( "getsockopt(SOL_XDP, XDP_STATISTICS) failed: %s", strerror( errno ) ));
343 0 : if( FD_UNLIKELY( optlen!=sizeof(struct xdp_statistics_v0) &&
344 0 : optlen!=sizeof(struct xdp_statistics_v1) ) ) {
345 0 : FD_LOG_ERR(( "getsockopt(SOL_XDP, XDP_STATISTICS) returned unexpected size %u", optlen ));
346 0 : }
347 0 : stats.rx_dropped += sub_stats.rx_dropped;
348 0 : stats.rx_invalid_descs += sub_stats.rx_invalid_descs;
349 0 : stats.tx_invalid_descs += sub_stats.tx_invalid_descs;
350 0 : stats.rx_ring_full += sub_stats.rx_ring_full;
351 0 : stats.rx_fill_ring_empty_descs += sub_stats.rx_fill_ring_empty_descs;
352 0 : stats.tx_ring_empty_descs += sub_stats.tx_ring_empty_descs;
353 0 : }
354 :
355 0 : FD_MCNT_SET( NET, XDP_RX_OTHER_DROPPED, stats.rx_dropped );
356 0 : FD_MCNT_SET( NET, XDP_RX_INVALID_DESCRIPTOR_DROPPED, stats.rx_invalid_descs );
357 0 : FD_MCNT_SET( NET, XDP_TX_INVALID_DESCRIPTOR_DROPPED, stats.tx_invalid_descs );
358 0 : FD_MCNT_SET( NET, XDP_RX_RING_FULL_DROPPED, stats.rx_ring_full );
359 0 : FD_MCNT_SET( NET, XDP_RX_FILL_RING_EMPTY, stats.rx_fill_ring_empty_descs );
360 0 : FD_MCNT_SET( NET, XDP_TX_RING_EMPTY, stats.tx_ring_empty_descs );
361 0 : }
362 :
363 : /* net_is_fatal_xdp_error returns 1 if the given errno returned by an
364 : XDP API indicates a non-recoverable error code. The net tile should
365 : crash if it sees such an error so the problem does not go undetected.
366 : Otherwise, returns 0. */
367 :
368 : static int
369 0 : net_is_fatal_xdp_error( int err ) {
370 0 : return err==ESOCKTNOSUPPORT || err==EOPNOTSUPP || err==EINVAL ||
371 0 : err==EPERM;
372 0 : }
373 :
374 : /* net_tx_ready returns 1 if we can submit a job to this TX ring, and 0 otherwise.
375 : Reasons for block include:
376 : - No TX buffer is available (free ring empty)
377 : - TX ring is full
378 :
379 : tx_ring: pointer to the XDP TX ring
380 : free_ring: pointer to the free TX ring */
381 :
382 : static int
383 : net_tx_ready( fd_xdp_ring_t * tx_ring,
384 0 : fd_net_free_ring_t * free_ring ) {
385 0 : if( FD_UNLIKELY( free_ring->prod == free_ring->cons ) ) return 0; /* drop - no free buffers */
386 0 : if( FD_UNLIKELY( fd_xdp_ring_full( tx_ring ) ) ) return 0; /* drop - tx ring full */
387 0 : return 1;
388 0 : }
389 :
390 : /* net_rx_wakeup triggers xsk_recvmsg to run in the kernel. Needs to be
391 : called periodically in order to receive packets. */
392 :
393 : static void
394 : net_rx_wakeup( fd_net_ctx_t * ctx,
395 : fd_xsk_t * xsk,
396 0 : int * charge_busy ) {
397 : /* write-back local copies to fseqs */
398 0 : __atomic_store_n( xsk->ring_rx.cons, xsk->ring_rx.cached_cons, __ATOMIC_RELEASE );
399 0 : __atomic_store_n( xsk->ring_fr.prod, xsk->ring_fr.cached_prod, __ATOMIC_RELEASE );
400 0 : if( !fd_xsk_rx_need_wakeup( xsk ) ) return;
401 0 : *charge_busy = 1;
402 0 : struct msghdr _ignored[ 1 ] = { 0 };
403 0 : if( FD_UNLIKELY( -1==recvmsg( xsk->xsk_fd, _ignored, MSG_DONTWAIT ) ) ) {
404 0 : if( FD_UNLIKELY( net_is_fatal_xdp_error( errno ) ) ) {
405 0 : FD_LOG_ERR(( "xsk recvmsg failed xsk_fd=%d (%i-%s)", xsk->xsk_fd, errno, fd_io_strerror( errno ) ));
406 0 : }
407 0 : if( FD_UNLIKELY( errno!=EAGAIN ) ) {
408 0 : long ts = fd_log_wallclock();
409 0 : if( ts > xsk->log_suppress_until_ns ) {
410 0 : FD_LOG_WARNING(( "xsk recvmsg failed xsk_fd=%d (%i-%s)", xsk->xsk_fd, errno, fd_io_strerror( errno ) ));
411 0 : xsk->log_suppress_until_ns = ts + (long)1e9;
412 0 : }
413 0 : }
414 0 : }
415 0 : ctx->metrics.xsk_rx_wakeup_cnt++;
416 0 : }
417 :
418 : /* net_tx_wakeup triggers xsk_sendmsg to run in the kernel. Needs to be
419 : called periodically in order to transmit packets. Should only be called
420 : if there are unconsumed packets in Tx ring. */
421 :
422 : static void
423 : net_tx_wakeup( fd_net_ctx_t * ctx,
424 : fd_xsk_t * xsk,
425 0 : int * charge_busy ) {
426 : /* write-back local copies to fseqs */
427 0 : __atomic_store_n( xsk->ring_tx.prod, xsk->ring_tx.cached_prod, __ATOMIC_RELEASE );
428 0 : __atomic_store_n( xsk->ring_cr.cons, xsk->ring_cr.cached_cons, __ATOMIC_RELEASE );
429 0 : if( !fd_xsk_tx_need_wakeup( xsk ) ) return;
430 0 : *charge_busy = 1;
431 0 : if( FD_UNLIKELY( -1==sendto( xsk->xsk_fd, NULL, 0, MSG_DONTWAIT, NULL, 0 ) ) ) {
432 0 : if( FD_UNLIKELY( net_is_fatal_xdp_error( errno ) ) ) {
433 0 : FD_LOG_ERR(( "xsk sendto failed xsk_fd=%d (%i-%s)", xsk->xsk_fd, errno, fd_io_strerror( errno ) ));
434 0 : }
435 0 : if( FD_UNLIKELY( errno!=EAGAIN ) ) {
436 0 : long ts = fd_log_wallclock();
437 0 : if( ts > xsk->log_suppress_until_ns ) {
438 0 : FD_LOG_WARNING(( "xsk sendto failed xsk_fd=%d (%i-%s)", xsk->xsk_fd, errno, fd_io_strerror( errno ) ));
439 0 : xsk->log_suppress_until_ns = ts + (long)1e9;
440 0 : }
441 0 : }
442 0 : }
443 0 : ctx->metrics.xsk_tx_wakeup_cnt++;
444 0 : }
445 :
446 : /* net_tx_periodic_wakeup does a timer based xsk_sendmsg wakeup. */
447 :
448 : static inline int
449 : net_tx_periodic_wakeup( fd_net_ctx_t * ctx,
450 : uint xsk_idx,
451 : long now,
452 0 : int * charge_busy ) {
453 0 : fd_xdp_ring_t * tx_ring = &ctx->xsk[ xsk_idx ].ring_tx;
454 0 : int tx_ring_empty = fd_xdp_ring_empty( tx_ring, FD_XDP_RING_ROLE_PROD );
455 0 : if( fd_net_flusher_check( ctx->tx_flusher+xsk_idx, now, tx_ring_empty ) ) {
456 0 : net_tx_wakeup( ctx, &ctx->xsk[ xsk_idx ], charge_busy );
457 0 : fd_net_flusher_wakeup( ctx->tx_flusher+xsk_idx, now );
458 0 : }
459 0 : return 0;
460 0 : }
461 :
462 : static void
463 0 : during_housekeeping( fd_net_ctx_t * ctx ) {
464 0 : long now = fd_tickcount();
465 0 : if( FD_UNLIKELY( fd_netdev_tbl_refresh( &ctx->netdev_tbl, &ctx->netdev_shared, &ctx->netdev_seq ) ) ) {
466 0 : fd_net_gre_tunnels_refresh( &ctx->net, &ctx->netdev_tbl );
467 0 : }
468 :
469 0 : ctx->metrics.rx_busy_cnt = 0UL;
470 0 : ctx->metrics.rx_idle_cnt = 0UL;
471 0 : ctx->metrics.tx_idle_cnt = fd_seq_diff( ctx->free_tx.prod, ctx->free_tx.cons );
472 0 : ctx->metrics.tx_busy_cnt = (long)ctx->free_tx.depth - ctx->metrics.tx_idle_cnt;
473 0 : for( uint j=0U; j<ctx->xsk_cnt; j++ ) {
474 0 : fd_xsk_t * xsk = &ctx->xsk[ j ];
475 0 : FD_COMPILER_MFENCE();
476 : /* Write back local copies to fseqs that we own */
477 0 : __atomic_store_n( xsk->ring_fr.prod, xsk->ring_fr.cached_prod, __ATOMIC_RELEASE );
478 0 : __atomic_store_n( xsk->ring_rx.cons, xsk->ring_rx.cached_cons, __ATOMIC_RELEASE );
479 0 : __atomic_store_n( xsk->ring_tx.prod, xsk->ring_tx.cached_prod, __ATOMIC_RELEASE );
480 0 : __atomic_store_n( xsk->ring_cr.cons, xsk->ring_cr.cached_cons, __ATOMIC_RELEASE );
481 :
482 : /* Refresh kernel-owned seq numbers for accurate stats */
483 0 : xsk->ring_fr.cached_cons = __atomic_load_n( xsk->ring_fr.cons, __ATOMIC_ACQUIRE );
484 0 : xsk->ring_rx.cached_prod = __atomic_load_n( xsk->ring_rx.prod, __ATOMIC_ACQUIRE );
485 0 : xsk->ring_tx.cached_cons = __atomic_load_n( xsk->ring_tx.cons, __ATOMIC_ACQUIRE );
486 0 : xsk->ring_cr.cached_prod = __atomic_load_n( xsk->ring_cr.prod, __ATOMIC_ACQUIRE );
487 :
488 0 : FD_COMPILER_MFENCE();
489 0 : ctx->metrics.rx_busy_cnt += (long)(int)( xsk->ring_rx.cached_prod - xsk->ring_rx.cached_cons );
490 0 : ctx->metrics.rx_idle_cnt += (long)(int)( xsk->ring_fr.cached_prod - xsk->ring_fr.cached_cons );
491 0 : }
492 :
493 0 : if( now > ctx->next_xdp_stats_refresh ) {
494 0 : ctx->next_xdp_stats_refresh = now + ctx->xdp_stats_interval_ticks;
495 0 : poll_xdp_statistics( ctx );
496 0 : }
497 0 : }
498 :
499 :
500 : /* net_tx_route resolves the src IP address and for non-GRE routes the XSK
501 : index and MAC addresses. Returns 1 on success, 0 on failure.
502 : On success, tx_op.xsk_idx and net.tx_route.src_ip are set, and if the dst_ip
503 : belongs to a GRE interface, is_gre_inf will set to 1 and net.tx_route.gre_outer_src_ip
504 : and net.tx_route.gre_outer_dst_ip will be loaded from the netdev
505 : table. is_gre_inf is set to 0 if dst_ip doesn't belong to a GRE interface. */
506 :
507 : static int
508 : net_tx_route( fd_net_ctx_t * ctx,
509 : uint dst_ip,
510 0 : uint * is_gre_inf ) {
511 0 : fd_net_tx_route_t * route = &ctx->net.tx_route;
512 :
513 : /* Route lookup */
514 :
515 0 : fd_fib4_hop_t hop[2] = {0};
516 0 : hop[0] = fd_fib4_lookup( ctx->fib_local, dst_ip, 0UL );
517 0 : hop[1] = fd_fib4_lookup( ctx->fib_main, dst_ip, 0UL );
518 0 : fd_fib4_hop_t const * next_hop = fd_fib4_hop_or( hop+0, hop+1 );
519 :
520 0 : uint rtype = next_hop->rtype;
521 0 : uint if_idx = next_hop->if_idx;
522 0 : uint ip4_src = next_hop->ip4_src;
523 :
524 0 : if( FD_UNLIKELY( rtype==FD_FIB4_RTYPE_LOCAL ) ) {
525 0 : rtype = FD_FIB4_RTYPE_UNICAST;
526 0 : if_idx = 1;
527 0 : }
528 :
529 0 : if( FD_UNLIKELY( rtype!=FD_FIB4_RTYPE_UNICAST ) ) {
530 0 : uint reason = fd_uint_if( rtype==FD_FIB4_RTYPE_THROW,
531 0 : FD_METRICS_ENUM_PKT_TX_DROP_REASON_V_NO_ROUTE_IDX,
532 0 : FD_METRICS_ENUM_PKT_TX_DROP_REASON_V_UNSUPPORTED_ROUTE_IDX );
533 0 : ctx->net.metrics.tx_drop_cnt[ reason ]++;
534 0 : return 0;
535 0 : }
536 :
537 0 : fd_netdev_t * netdev = fd_netdev_tbl_query( &ctx->netdev_tbl, if_idx );
538 0 : if( !netdev ) {
539 0 : ctx->net.metrics.tx_drop_cnt[ FD_METRICS_ENUM_PKT_TX_DROP_REASON_V_INTERFACE_MISSING_IDX ]++;
540 0 : return 0;
541 0 : }
542 :
543 0 : ip4_src = fd_uint_if( !!ctx->net.bind_address, ctx->net.bind_address, ip4_src );
544 0 : route->src_ip = ip4_src;
545 0 : route->mtu = netdev->mtu;
546 0 : ctx->tx_op.xsk_idx = UINT_MAX;
547 :
548 0 : FD_TEST( is_gre_inf );
549 0 : *is_gre_inf = 0;
550 0 : if( netdev->dev_type==ARPHRD_LOOPBACK ) {
551 : /* Set Ethernet src and dst address to 00:00:00:00:00:00 */
552 0 : memset( route->mac_addrs, 0, 12UL );
553 0 : ctx->tx_op.xsk_idx = if_idx==ctx->if_virt ? XSK_IDX_MAIN : XSK_IDX_LO;
554 : /* Set preferred src address to 127.0.0.1 if no bind address is set */
555 0 : if( !route->src_ip ) route->src_ip = FD_IP4_ADDR( 127,0,0,1 );
556 0 : return 1;
557 0 : } else if( netdev->dev_type==ARPHRD_IPGRE ) {
558 : /* skip MAC addrs lookup for GRE inner dst ip */
559 0 : if( netdev->gre_src_ip ) route->gre_outer_src_ip = netdev->gre_src_ip;
560 0 : route->gre_outer_dst_ip = netdev->gre_dst_ip;
561 0 : *is_gre_inf = 1;
562 0 : return 1;
563 0 : }
564 :
565 0 : if( FD_UNLIKELY( netdev->dev_type!=ARPHRD_ETHER ) ) {
566 0 : ctx->net.metrics.tx_drop_cnt[ FD_METRICS_ENUM_PKT_TX_DROP_REASON_V_UNSUPPORTED_INTERFACE_IDX ]++;
567 0 : return 0;
568 0 : }
569 :
570 0 : if( FD_UNLIKELY( if_idx!=ctx->if_virt ) ) {
571 0 : ctx->net.metrics.tx_drop_cnt[ FD_METRICS_ENUM_PKT_TX_DROP_REASON_V_UNSUPPORTED_INTERFACE_IDX ]++;
572 0 : return 0;
573 0 : }
574 0 : ctx->tx_op.xsk_idx = XSK_IDX_MAIN;
575 :
576 : /* Neighbor resolve */
577 0 : uint neigh_ip = next_hop->ip4_gw;
578 0 : if( !neigh_ip ) neigh_ip = dst_ip;
579 :
580 0 : fd_neigh4_entry_t neigh[1];
581 0 : int neigh_res = fd_neigh4_hmap_query_entry( ctx->neigh4, neigh_ip, neigh );
582 0 : if( FD_UNLIKELY( neigh_res!=FD_MAP_SUCCESS ) ) {
583 : /* Neighbor not found */
584 0 : ctx->solicit_ip = neigh_ip;
585 0 : ctx->solicit_if_idx = if_idx;
586 0 : ctx->metrics.tx_neigh_fail_cnt++;
587 0 : return 0;
588 0 : }
589 0 : if( FD_UNLIKELY( neigh->state != FD_NEIGH4_STATE_ACTIVE ) ) {
590 0 : ctx->metrics.tx_neigh_fail_cnt++;
591 0 : return 0;
592 0 : }
593 0 : ip4_src = fd_uint_if( !ip4_src, ctx->default_address, ip4_src );
594 0 : route->src_ip = ip4_src;
595 0 : memcpy( route->mac_addrs+0, neigh->mac_addr, 6 );
596 0 : memcpy( route->mac_addrs+6, netdev->mac_addr, 6 );
597 :
598 0 : return 1;
599 0 : }
600 :
601 : /* before_frag is called when a new metadata descriptor for a TX job is
602 : found. This callback determines whether this net tile is responsible
603 : for the TX job. If so, it prepares the TX op for the during_frag and
604 : after_frag callbacks. */
605 :
606 : static inline int
607 : before_frag( fd_net_ctx_t * ctx,
608 : ulong in_idx,
609 : ulong seq,
610 0 : ulong sig ) {
611 0 : (void)seq;
612 :
613 0 : if( FD_UNLIKELY( ctx->net.in_kind[ in_idx ]==FD_NET_IN_KIND_IPROUTE ) ) return 0;
614 :
615 : /* Find interface index of next packet */
616 0 : ulong proto = fd_disco_netmux_sig_proto( sig );
617 0 : if( FD_UNLIKELY( proto!=DST_PROTO_OUTGOING ) ) return 1;
618 :
619 : /* Load balance TX */
620 0 : ulong tile_cnt = ctx->net.tile_cnt;
621 0 : ulong hash = fd_disco_netmux_sig_hash( sig );
622 0 : ulong target_idx = hash % tile_cnt;
623 0 : ulong kind_id = ctx->net.kind_id;
624 0 : uint dst_ip = fd_disco_netmux_sig_ip( sig );
625 :
626 : /* Skip if another net tile is responsible for this packet.
627 : Fast path for net tiles other than net_tile 0. */
628 :
629 0 : if( kind_id!=0UL && kind_id!=target_idx ) return 1; /* ignore */
630 :
631 :
632 0 : fd_net_tx_route_t * route = &ctx->net.tx_route;
633 0 : *route = (fd_net_tx_route_t){0};
634 0 : uint is_gre_inf = 0;
635 :
636 0 : if( FD_UNLIKELY( !net_tx_route( ctx, dst_ip, &is_gre_inf ) ) ) {
637 0 : return 1; /* metrics incremented by net_tx_route */
638 0 : }
639 :
640 0 : uint xsk_idx = ctx->tx_op.xsk_idx;
641 :
642 0 : if( is_gre_inf ) {
643 0 : uint inner_src_ip = route->src_ip;
644 0 : if( FD_UNLIKELY( !inner_src_ip ) ) {
645 0 : ctx->net.metrics.tx_drop_cnt[ FD_METRICS_ENUM_PKT_TX_DROP_REASON_V_GRE_NO_ROUTE_IDX ]++;
646 0 : return 1;
647 0 : }
648 : /* Find the MAC addrs for the eth hdr, and src ip for outer ip4 hdr if not found in netdev tbl */
649 0 : route->src_ip = 0;
650 0 : is_gre_inf = 0;
651 : /* net_tx_route counts its own drop reason */
652 0 : if( FD_UNLIKELY( !net_tx_route( ctx, route->gre_outer_dst_ip, &is_gre_inf ) ) ) return 1;
653 0 : if( is_gre_inf ) {
654 : /* Only one layer of tunnelling supported */
655 0 : ctx->net.metrics.tx_drop_cnt[ FD_METRICS_ENUM_PKT_TX_DROP_REASON_V_GRE_NO_ROUTE_IDX ]++;
656 0 : return 1;
657 0 : }
658 0 : if( !route->gre_outer_src_ip ) {
659 0 : route->gre_outer_src_ip = route->src_ip;
660 0 : }
661 0 : route->use_gre = 1; /* indicate to during_frag to use GRE header */
662 0 : route->src_ip = inner_src_ip;
663 0 : xsk_idx = XSK_IDX_MAIN;
664 0 : }
665 :
666 0 : if( xsk_idx==XSK_IDX_LO ) target_idx = 0UL; /* loopback always targets tile 0 */
667 :
668 : /* Skip if another net tile is responsible for this packet */
669 :
670 0 : if( kind_id!=target_idx ) return 1; /* ignore */
671 :
672 0 : if( FD_UNLIKELY( xsk_idx>=ctx->xsk_cnt ) ) {
673 : /* Packet does not route to an XDP interface */
674 0 : ctx->net.metrics.tx_drop_cnt[ FD_METRICS_ENUM_PKT_TX_DROP_REASON_V_UNSUPPORTED_INTERFACE_IDX ]++;
675 0 : return 1;
676 0 : }
677 :
678 : /* Skip if TX is blocked */
679 :
680 0 : fd_xsk_t * xsk = &ctx->xsk[ xsk_idx ];
681 0 : fd_net_free_ring_t * free = &ctx->free_tx;
682 0 : if( FD_UNLIKELY( !net_tx_ready( &xsk->ring_tx, free ) ) ) {
683 0 : ctx->metrics.tx_full_fail_cnt++;
684 0 : return 1;
685 0 : }
686 :
687 : /* Allocate buffer for receive */
688 0 : ulong alloc_seq = free->cons;
689 0 : void * frame = (void *)free->queue[ alloc_seq % free->depth ];
690 0 : free->cons = fd_seq_inc( alloc_seq, 1UL );
691 :
692 0 : ctx->tx_op.frame = frame;
693 :
694 0 : return 0; /* continue */
695 0 : }
696 :
697 : /* during_frag is called when before_frag has committed to transmit an
698 : outgoing packet. */
699 :
700 : static inline void
701 : during_frag( fd_net_ctx_t * ctx,
702 : ulong in_idx,
703 : ulong seq FD_PARAM_UNUSED,
704 : ulong sig FD_PARAM_UNUSED,
705 : ulong chunk,
706 : ulong sz,
707 0 : ulong ctl FD_PARAM_UNUSED ) {
708 0 : fd_net_in_frag_validate( &ctx->net, in_idx, chunk, sz );
709 :
710 0 : if( FD_UNLIKELY( fd_net_iproute_msg_stage( &ctx->net, in_idx, chunk, sz ) ) ) return;
711 :
712 0 : void * frame = ctx->tx_op.frame;
713 0 : if( FD_UNLIKELY( (ulong)frame < (ulong)ctx->umem ) )
714 0 : FD_LOG_ERR(( "frame %p out of bounds (below %p)", frame, (void *)ctx->umem ));
715 0 : ulong umem_off = (ulong)frame - (ulong)ctx->umem;
716 0 : if( FD_UNLIKELY( (ulong)umem_off > (ulong)ctx->umem_sz ) )
717 0 : FD_LOG_ERR(( "frame %p out of bounds (beyond %p)", frame, (void *)ctx->umem_sz ));
718 :
719 : /* Speculatively copy frame into buffer from in link */
720 0 : fd_net_tx_pkt_cpy( &ctx->net, ctx->tx_op.frame, chunk, sz, in_idx );
721 0 : }
722 :
723 : /* after_frag is called when the during_frag memcpy was _not_ overrun. */
724 :
725 : static void
726 : after_frag( fd_net_ctx_t * ctx,
727 : ulong in_idx,
728 : ulong seq,
729 : ulong sig,
730 : ulong sz,
731 : ulong tsorig,
732 : ulong tspub,
733 0 : fd_stem_context_t * stem ) {
734 0 : (void)seq; (void)sig; (void)tsorig; (void)tspub; (void)stem;
735 :
736 0 : if( FD_UNLIKELY( ctx->net.in_kind[ in_idx ]==FD_NET_IN_KIND_IPROUTE ) ) {
737 0 : fd_iproute_msg_t const * msg = &ctx->net.iproute_msg;
738 0 : if( msg->op==FD_IPROUTE_OP_FLUSH ) {
739 0 : fd_fib4_clear( ctx->fib_local );
740 0 : fd_fib4_clear( ctx->fib_main );
741 0 : return;
742 0 : }
743 0 : fd_fib4_t * fib;
744 0 : if( msg->table_id==RT_TABLE_LOCAL ) fib = ctx->fib_local;
745 0 : else if( msg->table_id==RT_TABLE_MAIN ) fib = ctx->fib_main;
746 0 : else return;
747 0 : if( msg->op==FD_IPROUTE_OP_UPSERT && FD_UNLIKELY( !fd_fib4_insert( fib, msg->dst_addr, msg->prefix, msg->prio, &msg->hop ) ) ) {
748 0 : FD_LOG_WARNING(( "route update dropped: route table full (increase [net.max_routes] or [net.max_peer_routes])" ));
749 0 : if( FD_UNLIKELY( ctx->net.kind_id==0UL ) ) {
750 0 : fd_stem_publish( stem, ctx->netlnk_out_idx, FD_NETLINK_ROUTE4_SYNC_SIG, 0UL, 0UL, 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
751 0 : }
752 0 : }
753 0 : else if( msg->op==FD_IPROUTE_OP_DELETE ) fd_fib4_remove( fib, msg->dst_addr, msg->prefix, msg->prio );
754 0 : return;
755 0 : }
756 :
757 : /* Current send operation */
758 :
759 0 : uchar * frame = ctx->tx_op.frame;
760 0 : uint xsk_idx = ctx->tx_op.xsk_idx;
761 :
762 0 : if( FD_UNLIKELY( !fd_net_tx_pkt_prep( &ctx->net, frame, &sz, in_idx ) ) ) return;
763 0 : if( ctx->net.tx_route.use_gre ) xsk_idx = XSK_IDX_MAIN;
764 :
765 : /* Submit packet TX job
766 :
767 : Invariant for ring_tx: prod-cons<length
768 : (This invariant breaks if any other packet is sent over this ring
769 : between before_frag and this point, e.g. send_arp_probe.) */
770 :
771 0 : fd_xsk_t * xsk = &ctx->xsk[ xsk_idx ];
772 0 : fd_xdp_ring_t * tx_ring = &xsk->ring_tx;
773 0 : uint tx_seq = tx_ring->cached_prod;
774 0 : uint tx_mask = tx_ring->depth - 1U;
775 0 : xsk->ring_tx.packet_ring[ tx_seq&tx_mask ] = (struct xdp_desc) {
776 0 : .addr = (ulong)frame - (ulong)ctx->umem,
777 0 : .len = (uint)sz,
778 0 : .options = 0
779 0 : };
780 :
781 : /* Frame is now owned by kernel. Clear tx_op. */
782 0 : ctx->tx_op.frame = NULL;
783 :
784 : /* Register newly enqueued packet */
785 0 : tx_ring->cached_prod = tx_seq+1U;
786 0 : ctx->metrics.tx_submit_cnt++;
787 0 : ctx->net.metrics.tx_bytes_total += sz;
788 0 : if( ctx->net.tx_route.use_gre ) ctx->net.metrics.tx_gre_cnt++;
789 0 : fd_net_flusher_inc( ctx->tx_flusher+xsk_idx, fd_tickcount() );
790 0 : }
791 :
792 : /* net_comp_event is called when an XDP TX frame is free again. */
793 :
794 : static void
795 : net_comp_event( fd_net_ctx_t * ctx,
796 : fd_xsk_t * xsk,
797 0 : uint comp_seq ) {
798 :
799 : /* Locate the incoming frame */
800 :
801 0 : fd_xdp_ring_t * comp_ring = &xsk->ring_cr;
802 0 : uint comp_mask = comp_ring->depth - 1U;
803 0 : ulong frame = FD_VOLATILE_CONST( comp_ring->frame_ring[ comp_seq&comp_mask ] );
804 0 : ulong const frame_mask = FD_NET_MTU - 1UL;
805 0 : FD_STATIC_ASSERT( FD_ULONG_IS_POW2( FD_NET_MTU ), "FD_NET_MTU must be a power of two" );
806 0 : if( FD_UNLIKELY( frame+FD_NET_MTU > ctx->umem_sz ) ) {
807 0 : FD_LOG_ERR(( "Bounds check failed: frame=0x%lx umem_sz=0x%lx",
808 0 : frame, (ulong)ctx->umem_sz ));
809 0 : }
810 :
811 : /* Check if we have space to return the freed frame */
812 :
813 0 : fd_net_free_ring_t * free = &ctx->free_tx;
814 0 : ulong free_prod = free->prod;
815 0 : ulong free_mask = free->depth - 1UL;
816 0 : ulong free_cons = free->cons;
817 0 : long free_cnt = fd_seq_diff( free_prod, free_cons );
818 0 : FD_TEST( free_prod >= free_cons );
819 0 : if( FD_UNLIKELY( free_cnt>=(long)free->depth ) ) return; /* blocked */
820 :
821 0 : free->queue[ free_prod&free_mask ] = (ulong)ctx->umem + (frame & (~frame_mask));
822 0 : free->prod = fd_seq_inc( free_prod, 1UL );
823 :
824 : /* Wind up for next iteration */
825 :
826 0 : comp_ring->cached_cons = comp_seq+1U;
827 0 : ctx->net.metrics.tx_pkt_cnt++;
828 0 : }
829 :
830 : static int
831 0 : prevent_park( fd_net_ctx_t * ctx ) {
832 0 : for( uint i=0U; i<ctx->xsk_cnt; i++ ) {
833 0 : fd_xsk_t * xsk = &ctx->xsk[ i ];
834 0 : if( FD_UNLIKELY( xsk->prefbusy_poll_enabled ) ) return 1;
835 0 : if( FD_UNLIKELY( !fd_xdp_ring_empty( &xsk->ring_rx, FD_XDP_RING_ROLE_CONS ) ) ) return 1;
836 0 : if( FD_UNLIKELY( !fd_xdp_ring_empty( &xsk->ring_cr, FD_XDP_RING_ROLE_CONS ) ) ) return 1;
837 0 : if( FD_UNLIKELY( ctx->tx_flusher[ i ].pending_cnt ) ) return 1;
838 0 : if( FD_UNLIKELY( !fd_xdp_ring_empty( &xsk->ring_tx, FD_XDP_RING_ROLE_PROD ) ) ) return 1; /* the flusher paces the kicks */
839 0 : }
840 0 : return 0;
841 0 : }
842 :
843 : /* net_rx_event is called when a new XDP RX frame is available. Calls
844 : fd_net_rx_pkt, then returns the packet back to the kernel via the fill
845 : ring. */
846 :
847 : static void
848 : net_rx_event( fd_net_ctx_t * ctx,
849 : fd_stem_context_t * stem,
850 : fd_xsk_t * xsk,
851 0 : uint rx_seq ) {
852 : /* Locate the incoming frame */
853 :
854 0 : fd_xdp_ring_t * rx_ring = &xsk->ring_rx;
855 0 : uint rx_mask = rx_ring->depth - 1U;
856 0 : struct xdp_desc frame = FD_VOLATILE_CONST( rx_ring->packet_ring[ rx_seq&rx_mask ] );
857 :
858 0 : if( FD_UNLIKELY( frame.len>FD_NET_MTU ) )
859 0 : FD_LOG_ERR(( "received a UDP packet with a too large payload (%u)", frame.len ));
860 :
861 : /* Check if we have space in the fill ring to free the frame */
862 :
863 0 : fd_xdp_ring_t * fill_ring = &xsk->ring_fr;
864 0 : if( FD_UNLIKELY( fd_xdp_ring_full( fill_ring ) ) ) {
865 0 : ctx->metrics.rx_fill_blocked_cnt++;
866 0 : return; /* blocked */
867 0 : }
868 :
869 : /* Pass it to the receive handler */
870 :
871 0 : ulong const chunk = ctx->net.pkt_buf_chunk0 + (frame.addr >> FD_CHUNK_LG_SZ);
872 0 : ulong const tspub = (ulong)fd_frag_meta_ts_comp( fd_tickcount() );
873 0 : ulong freed_chunk;
874 0 : fd_net_rx_pkt( &ctx->net, stem, chunk, frame.addr & (FD_CHUNK_SZ-1UL), frame.len, tspub, &freed_chunk );
875 :
876 0 : FD_COMPILER_MFENCE();
877 0 : rx_ring->cached_cons = rx_seq+1U;
878 :
879 : /* Every RX operation returns one frame to the FILL ring. If the
880 : packet was forwarded to a downstream ring, the newly shadowed frame
881 : is returned. Otherwise, the frame just received is returned. */
882 :
883 0 : FD_STATIC_ASSERT( FD_ULONG_IS_POW2( FD_NET_MTU ), "FD_NET_MTU must be a power of two" );
884 : /* Packets can start partway into a UMEM frame, so align down to
885 : the frame start. */
886 0 : ulong const frame_sz_chunks = FD_NET_MTU>>FD_CHUNK_LG_SZ;
887 0 : freed_chunk = ctx->net.pkt_buf_chunk0 + fd_ulong_align_dn( freed_chunk-ctx->net.pkt_buf_chunk0, frame_sz_chunks );
888 0 : if( FD_UNLIKELY( freed_chunk<ctx->net.pkt_buf_chunk0 || freed_chunk>ctx->net.pkt_buf_wmark ) ) {
889 0 : FD_LOG_CRIT(( "mcache corruption detected: buffer chunk %lu out of bounds [%lu,%lu]",
890 0 : freed_chunk, ctx->net.pkt_buf_chunk0, ctx->net.pkt_buf_wmark ));
891 0 : }
892 0 : ulong const freed_off = (freed_chunk-ctx->net.pkt_buf_chunk0)<<FD_CHUNK_LG_SZ;
893 :
894 0 : uint fill_prod = fill_ring->cached_prod;
895 0 : uint fill_mask = (fill_ring->depth)-1U;
896 0 : fill_ring->frame_ring[ fill_prod&fill_mask ] = freed_off;
897 0 : fill_ring->cached_prod = fill_prod+1U;
898 0 : }
899 :
900 : static void
901 : before_credit_softirq( fd_net_ctx_t * ctx,
902 : fd_stem_context_t * stem,
903 : int * charge_busy,
904 : uint rr_idx,
905 0 : fd_xsk_t * rr_xsk ) {
906 :
907 0 : net_tx_periodic_wakeup( ctx, rr_idx, fd_tickcount(), charge_busy );
908 :
909 : /* Fire RX event if we have RX desc avail */
910 0 : if( !fd_xdp_ring_empty( &rr_xsk->ring_rx, FD_XDP_RING_ROLE_CONS ) ) {
911 0 : *charge_busy = 1;
912 0 : net_rx_event( ctx, stem, rr_xsk, rr_xsk->ring_rx.cached_cons );
913 0 : } else {
914 0 : net_rx_wakeup( ctx, rr_xsk, charge_busy );
915 :
916 : /* Iterate onto the next NAPI queue. */
917 0 : ctx->rr_idx++;
918 0 : ctx->rr_idx = fd_uint_if( ctx->rr_idx>=ctx->xsk_cnt, 0, ctx->rr_idx );
919 0 : }
920 0 : }
921 :
922 : static int
923 : net_prefbusy_poll_ready( fd_xsk_t * rr_xsk,
924 : fd_net_flusher_t * flusher,
925 0 : long now ) {
926 :
927 0 : if( FD_LIKELY( now < ( flusher->prefbusy_last_poll_ticks + flusher->prefbusy_min_interval_ticks ) ) ) return 0;
928 0 : if( FD_UNLIKELY( now > ( flusher->prefbusy_last_poll_ticks + flusher->prefbusy_stall_timeout_ticks ) ) ) return 1;
929 :
930 0 : int rx_empty = fd_xdp_ring_empty( &rr_xsk->ring_rx, FD_XDP_RING_ROLE_CONS );
931 :
932 0 : return rx_empty;
933 0 : }
934 :
935 : static void
936 : net_prefbusy_poll_flush( fd_net_flusher_t * flusher,
937 0 : long now ) {
938 0 : flusher->prefbusy_last_poll_ticks = now;
939 0 : }
940 :
941 : static void
942 : before_credit_prefbusy( fd_net_ctx_t * ctx,
943 : fd_stem_context_t * stem,
944 : int * charge_busy,
945 : uint rr_idx,
946 0 : fd_xsk_t * rr_xsk ) {
947 :
948 0 : fd_net_flusher_t * flusher = ctx->tx_flusher+rr_idx;
949 0 : if( FD_UNLIKELY( net_prefbusy_poll_ready( rr_xsk, flusher, fd_tickcount() ) ) ) {
950 : /* NAPI needs to be polled to process new TX from
951 : Firedancer's net tile and process new RX from the NIC. */
952 :
953 : /* write-back local copies to fseqs */
954 0 : __atomic_store_n( rr_xsk->ring_tx.prod, rr_xsk->ring_tx.cached_prod, __ATOMIC_RELEASE );
955 0 : __atomic_store_n( rr_xsk->ring_cr.cons, rr_xsk->ring_cr.cached_cons, __ATOMIC_RELEASE );
956 0 : __atomic_store_n( rr_xsk->ring_rx.cons, rr_xsk->ring_rx.cached_cons, __ATOMIC_RELEASE );
957 0 : __atomic_store_n( rr_xsk->ring_fr.prod, rr_xsk->ring_fr.cached_prod, __ATOMIC_RELEASE );
958 :
959 0 : if( FD_UNLIKELY( -1==sendto( rr_xsk->xsk_fd, NULL, 0, MSG_DONTWAIT, NULL, 0 ) ) ) {
960 0 : if( FD_UNLIKELY( net_is_fatal_xdp_error( errno ) ) ) {
961 0 : FD_LOG_ERR(( "xsk sendto failed xsk_fd=%d (%i-%s)", rr_xsk->xsk_fd, errno, fd_io_strerror( errno ) ));
962 0 : }
963 0 : if( FD_UNLIKELY( errno!=EAGAIN ) ) {
964 0 : long ts = fd_log_wallclock();
965 0 : if( ts > rr_xsk->log_suppress_until_ns ) {
966 0 : FD_LOG_WARNING(( "xsk sendto failed xsk_fd=%d (%i-%s)", rr_xsk->xsk_fd, errno, fd_io_strerror( errno ) ));
967 0 : rr_xsk->log_suppress_until_ns = ts + (long)1e9;
968 0 : }
969 0 : }
970 0 : }
971 : /* Since xsk sendmsg in prefbusy mode drives both rx and tx, both are incremented */
972 0 : ctx->metrics.xsk_tx_wakeup_cnt++;
973 0 : ctx->metrics.xsk_rx_wakeup_cnt++;
974 :
975 0 : net_prefbusy_poll_flush( flusher, fd_tickcount() );
976 0 : }
977 :
978 : /* Process new RX from xsk ring if there is any. */
979 0 : if( !fd_xdp_ring_empty( &rr_xsk->ring_rx, FD_XDP_RING_ROLE_CONS ) ) {
980 0 : *charge_busy = 1;
981 0 : net_rx_event( ctx, stem, rr_xsk, rr_xsk->ring_rx.cached_cons );
982 0 : }
983 : /* Iterate onto the next NAPI queue. */
984 0 : ctx->rr_idx++;
985 0 : ctx->rr_idx = fd_uint_if( ctx->rr_idx>=ctx->xsk_cnt, 0, ctx->rr_idx );
986 0 : }
987 :
988 : /* before_credit is called every loop iteration. */
989 :
990 : static void
991 : before_credit( fd_net_ctx_t * ctx,
992 : fd_stem_context_t * stem,
993 0 : int * charge_busy ) {
994 0 : if( FD_UNLIKELY( ctx->solicit_ip ) ) {
995 0 : fd_stem_publish( stem, ctx->netlnk_out_idx, fd_netlink_neigh4_solicit_sig( ctx->solicit_ip, ctx->solicit_if_idx ), 0UL, 0UL, 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
996 0 : ctx->solicit_ip = 0U;
997 0 : }
998 :
999 : /* A previous send attempt was overrun. A corrupt copy of the packet was
1000 : placed into an XDP frame, but the frame was not yet submitted to the
1001 : TX ring. Return the tx buffer to the free list. */
1002 :
1003 0 : if( ctx->tx_op.frame ) {
1004 0 : *charge_busy = 1;
1005 0 : fd_net_free_ring_t * free = &ctx->free_tx;
1006 0 : ulong alloc_seq = free->prod;
1007 0 : free->queue[ alloc_seq % free->depth ] = (ulong)ctx->tx_op.frame;
1008 0 : free->prod = fd_seq_inc( alloc_seq, 1UL );
1009 0 : ctx->tx_op.frame = NULL;
1010 0 : }
1011 :
1012 : /* Check if new packets are available or if TX frames are free again
1013 : (Round-robin through sockets) */
1014 :
1015 0 : uint rr_idx = ctx->rr_idx;
1016 0 : fd_xsk_t * rr_xsk = &ctx->xsk[ rr_idx ];
1017 :
1018 0 : if( FD_LIKELY( !rr_xsk->prefbusy_poll_enabled ) ) {
1019 : /* Default poll mode which relies on irqs and wakeups */
1020 0 : before_credit_softirq( ctx, stem, charge_busy, rr_idx, rr_xsk );
1021 0 : } else {
1022 0 : before_credit_prefbusy( ctx, stem, charge_busy, rr_idx, rr_xsk );
1023 0 : }
1024 :
1025 : /* Fire comp event if we have comp desc avail */
1026 0 : if( !fd_xdp_ring_empty( &rr_xsk->ring_cr, FD_XDP_RING_ROLE_CONS ) ) {
1027 0 : *charge_busy = 1;
1028 0 : net_comp_event( ctx, rr_xsk, rr_xsk->ring_cr.cached_cons );
1029 0 : }
1030 :
1031 0 : if( FD_UNLIKELY( ctx->waker_fseq && fd_fseq_query( ctx->waker_fseq )==1UL ) ) {
1032 0 : for( uint i=0U; i<ctx->xsk_cnt; i++ ) {
1033 0 : if( FD_UNLIKELY( !fd_xdp_ring_empty( &ctx->xsk[ i ].ring_rx, FD_XDP_RING_ROLE_CONS ) ) ) return;
1034 0 : if( FD_UNLIKELY( !fd_xdp_ring_empty( &ctx->xsk[ i ].ring_cr, FD_XDP_RING_ROLE_CONS ) ) ) return;
1035 0 : }
1036 0 : fd_fseq_update( ctx->waker_fseq, 0UL );
1037 0 : fd_waker_client_rearm( ctx->waker_client_idx );
1038 0 : }
1039 0 : }
1040 :
1041 : /* net_xsk_bootstrap assigns UMEM frames to the FILL ring. */
1042 :
1043 : static ulong
1044 : net_xsk_bootstrap( fd_net_ctx_t * ctx,
1045 : uint xsk_idx,
1046 0 : ulong frame_off ) {
1047 0 : fd_xsk_t * xsk = &ctx->xsk[ xsk_idx ];
1048 :
1049 0 : ulong const frame_sz = FD_NET_MTU;
1050 0 : ulong const fr_depth = ctx->xsk[ xsk_idx ].ring_fr.depth/2UL;
1051 :
1052 0 : fd_xdp_ring_t * fill = &xsk->ring_fr;
1053 0 : uint fill_prod = fill->cached_prod;
1054 0 : for( ulong j=0UL; j<fr_depth; j++ ) {
1055 0 : fill->frame_ring[ j ] = frame_off;
1056 0 : frame_off += frame_sz;
1057 0 : }
1058 0 : fill->cached_prod = fill_prod + (uint)fr_depth;
1059 0 : __atomic_store_n( fill->prod, fill->cached_prod, __ATOMIC_RELEASE );
1060 :
1061 0 : return frame_off;
1062 0 : }
1063 :
1064 : /* FIXME source MAC address from netlnk tile instead */
1065 :
1066 : static void
1067 : interface_addrs( const char * interface,
1068 : uchar * mac,
1069 0 : uint * ip4_addr ) {
1070 0 : int fd = socket( AF_INET, SOCK_DGRAM, 0 );
1071 0 : struct ifreq ifr;
1072 0 : ifr.ifr_addr.sa_family = AF_INET;
1073 :
1074 0 : strncpy( ifr.ifr_name, interface, IFNAMSIZ );
1075 0 : if( FD_UNLIKELY( ioctl( fd, SIOCGIFHWADDR, &ifr ) ) )
1076 0 : FD_LOG_ERR(( "could not get MAC address of interface `%s`: (%i-%s)", interface, errno, fd_io_strerror( errno ) ));
1077 0 : fd_memcpy( mac, ifr.ifr_hwaddr.sa_data, 6 );
1078 :
1079 0 : if( FD_UNLIKELY( ioctl( fd, SIOCGIFADDR, &ifr ) ) )
1080 0 : FD_LOG_ERR(( "could not get IP address of interface `%s`: (%i-%s)", interface, errno, fd_io_strerror( errno ) ));
1081 0 : *ip4_addr = ((struct sockaddr_in *)fd_type_pun( &ifr.ifr_addr ))->sin_addr.s_addr;
1082 :
1083 0 : if( FD_UNLIKELY( close(fd) ) )
1084 0 : FD_LOG_ERR(( "could not close socket (%i-%s)", errno, fd_io_strerror( errno ) ));
1085 0 : }
1086 :
1087 : /* privileged_init does the following initialization steps:
1088 :
1089 : - Create an AF_XDP socket
1090 : - Map XDP metadata rings
1091 : - Register UMEM data region with socket
1092 : - Insert AF_XDP socket into xsk_map
1093 :
1094 : Net tile 0 also runs fd_xdp_install and repeats the above step for
1095 : the loopback device. (Unless the main interface is already loopback)
1096 :
1097 : Kernel object references:
1098 :
1099 : BPF_LINK file descriptor
1100 : |
1101 : +-> XDP program installation on NIC
1102 : | |
1103 : | +-> XDP program <-- BPF_PROG file descriptor (prog_fd)
1104 : |
1105 : +-> XSKMAP object <-- BPF_MAP file descriptor (xsk_map) */
1106 :
1107 : FD_FN_UNUSED static void
1108 : privileged_init( fd_topo_t const * topo,
1109 0 : fd_topo_tile_t const * tile ) {
1110 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
1111 :
1112 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
1113 0 : fd_net_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_net_ctx_t), sizeof(fd_net_ctx_t) );
1114 0 : ulong * free_tx = FD_SCRATCH_ALLOC_APPEND( l, alignof(ulong), tile->xdp.free_ring_depth * sizeof(ulong) );;
1115 :
1116 0 : fd_memset( ctx, 0, sizeof(fd_net_ctx_t) );
1117 :
1118 0 : interface_addrs( tile->xdp.if_virt, ctx->src_mac_addr, &ctx->default_address );
1119 0 : ctx->if_virt = if_nametoindex( tile->xdp.if_virt ); FD_TEST( ctx->if_virt );
1120 :
1121 : /* Load up dcache containing UMEM */
1122 :
1123 0 : void * const dcache_mem = fd_topo_obj_laddr( topo, tile->net.umem_dcache_obj_id );
1124 0 : void * const umem = fd_dcache_join( dcache_mem );
1125 0 : ulong const umem_dcache_data_sz = fd_dcache_data_sz( umem );
1126 0 : ulong const umem_frame_sz = 2048UL;
1127 0 : ulong const umem_sz = fd_ulong_align_dn( umem_dcache_data_sz, umem_frame_sz );
1128 :
1129 : /* Derive chunk bounds */
1130 :
1131 0 : void * const umem_base = fd_wksp_containing( dcache_mem );
1132 0 : ulong const umem_chunk0 = ( (ulong)umem - (ulong)umem_base )>>FD_CHUNK_LG_SZ;
1133 0 : ulong const umem_wmark = umem_chunk0 + ( ( umem_sz-umem_frame_sz )>>FD_CHUNK_LG_SZ );
1134 :
1135 0 : if( FD_UNLIKELY( umem_chunk0>UINT_MAX || umem_wmark>UINT_MAX || umem_chunk0>umem_wmark ) ) {
1136 0 : FD_LOG_ERR(( "Calculated invalid UMEM bounds [%lu,%lu]", umem_chunk0, umem_wmark ));
1137 0 : }
1138 :
1139 0 : if( FD_UNLIKELY( !umem_base ) ) FD_LOG_ERR(( "UMEM dcache is not in a workspace" ));
1140 :
1141 0 : ctx->umem = umem;
1142 0 : ctx->umem_sz = umem_sz;
1143 0 : ctx->net.pkt_buf_wksp_base = umem_base;
1144 0 : ctx->net.pkt_buf_chunk0 = umem_chunk0;
1145 0 : ctx->net.pkt_buf_wmark = umem_wmark;
1146 :
1147 0 : ctx->free_tx.queue = free_tx;
1148 0 : ctx->free_tx.depth = tile->xdp.free_ring_depth;
1149 :
1150 : /* Create and install XSKs */
1151 :
1152 0 : uint if_phys_if_idx = if_nametoindex( tile->xdp.if_phys );
1153 0 : if( FD_UNLIKELY( !if_phys_if_idx ) ) FD_LOG_ERR(( "if_nametoindex(%s) failed", tile->xdp.if_phys ));
1154 :
1155 0 : fd_xsk_params_t params0 = {
1156 0 : .if_idx = if_phys_if_idx,
1157 0 : .if_queue_id = tile->xdp.if_queue,
1158 :
1159 : /* Some kernels produce EOPNOTSUP errors on sendto calls when
1160 : starting up without either XDP_ZEROCOPY or XDP_COPY
1161 : (e.g. 5.14.0-503.23.1.el9_5 with i40e) */
1162 0 : .bind_flags = tile->xdp.zero_copy ? XDP_ZEROCOPY : XDP_COPY,
1163 :
1164 0 : .prefbusy_time_budget_micros = PREFBUSY_TIME_BUDGET_MICROS,
1165 :
1166 0 : .prefbusy_rx_budget = PREFBUSY_RX_BUDGET,
1167 :
1168 0 : .fr_depth = tile->xdp.xdp_rx_queue_size*2,
1169 0 : .rx_depth = tile->xdp.xdp_rx_queue_size,
1170 0 : .cr_depth = tile->xdp.xdp_tx_queue_size,
1171 0 : .tx_depth = tile->xdp.xdp_tx_queue_size,
1172 :
1173 0 : .umem_addr = umem,
1174 0 : .frame_sz = umem_frame_sz,
1175 0 : .umem_sz = umem_sz,
1176 :
1177 0 : .core_dump = tile->xdp.xsk_core_dump,
1178 0 : };
1179 :
1180 0 : fd_cstr_ncpy( params0.poll_mode, tile->xdp.poll_mode, sizeof(params0.poll_mode) );
1181 :
1182 : /* Re-derive XDP file descriptors */
1183 :
1184 0 : fd_xdp_fds_t xdp_fds[ FD_TOPO_XDP_FDS_MAX ];
1185 0 : uint xdp_fds_cnt = FD_TOPO_XDP_FDS_MAX;
1186 0 : fd_topo_install_xdp( topo, xdp_fds, &xdp_fds_cnt, 0U, /* dry_run */ 1 );
1187 :
1188 0 : int xsk_map_fd = -1;
1189 0 : for( uint i=0U; i<xdp_fds_cnt; i++ ) {
1190 0 : if( xdp_fds[ i ].if_idx==if_phys_if_idx ) {
1191 0 : xsk_map_fd = xdp_fds[ i ].xsk_map_fd;
1192 0 : ctx->prog_link_fds[ 0 ] = xdp_fds[ i ].prog_link_fd;
1193 0 : xdp_fds[ i ].prog_link_fd = -1; /* mark as used */
1194 0 : break;
1195 0 : }
1196 0 : }
1197 0 : FD_TEST( xsk_map_fd>=0 );
1198 :
1199 : /* Init XSK */
1200 0 : if( FD_UNLIKELY( !fd_xsk_init( &ctx->xsk[ 0 ], ¶ms0 ) ) ) FD_LOG_ERR(( "failed to bind xsk for net tile %lu", tile->kind_id ));
1201 0 : if( FD_UNLIKELY( !fd_xsk_activate( &ctx->xsk[ 0 ], xsk_map_fd ) ) ) FD_LOG_ERR(( "failed to activate xsk for net tile %lu", tile->kind_id ));
1202 0 : ctx->xsk_cnt = 1;
1203 :
1204 : /* Networking tile at index 0 also binds to loopback (only queue 0 available on lo) */
1205 :
1206 0 : if( FD_UNLIKELY( strcmp( tile->xdp.if_virt, "lo" ) && !tile->kind_id ) ) {
1207 0 : ctx->xsk_cnt = 2;
1208 :
1209 0 : uint lo_idx = if_nametoindex( "lo" );
1210 0 : if( FD_UNLIKELY( !lo_idx ) ) FD_LOG_ERR(( "if_nametoindex(lo) failed" ));
1211 :
1212 0 : int lo_xsk_map_fd = -1;
1213 0 : for( uint i=0U; i<xdp_fds_cnt; i++ ) {
1214 0 : if( xdp_fds[ i ].if_idx==lo_idx ) {
1215 0 : lo_xsk_map_fd = xdp_fds[ i ].xsk_map_fd;
1216 0 : ctx->prog_link_fds[ 1 ] = xdp_fds[ i ].prog_link_fd;
1217 0 : xdp_fds[ i ].prog_link_fd = -1; /* mark as used */
1218 0 : break;
1219 0 : }
1220 0 : }
1221 0 : FD_TEST( lo_xsk_map_fd>=0 );
1222 :
1223 : /* init xsk 1 */
1224 0 : fd_xsk_params_t params1 = params0;
1225 0 : params1.if_idx = lo_idx; /* probably always 1 */
1226 0 : params1.if_queue_id = 0;
1227 0 : params1.bind_flags = 0;
1228 0 : if( FD_UNLIKELY( !fd_xsk_init( &ctx->xsk[ 1 ], ¶ms1 ) ) ) FD_LOG_ERR(( "failed to bind lo_xsk" ));
1229 0 : if( FD_UNLIKELY( !fd_xsk_activate( &ctx->xsk[ 1 ], lo_xsk_map_fd ) ) ) FD_LOG_ERR(( "failed to activate lo_xsk" ));
1230 0 : }
1231 :
1232 : /* Close unused XDP fds */
1233 :
1234 0 : if( FD_UNLIKELY( fd_sandbox_gettid()==fd_sandbox_getpid() ) ) {
1235 : /* Kind of gross.. in single threaded mode we don't want to close the xsk_map_fd
1236 : since it's shared with other net tiles. Just check for that by seeing if we
1237 : are the only thread in the process. */
1238 0 : for( uint i=0U; i<xdp_fds_cnt; i++ ) {
1239 0 : if( -1==close( xdp_fds[ i ].xsk_map_fd ) ) {
1240 0 : FD_LOG_ERR(( "close(%d) failed (%d-%s)", xsk_map_fd, errno, fd_io_strerror( errno ) ));
1241 0 : }
1242 0 : if( xdp_fds[ i ].prog_link_fd>0 &&
1243 0 : -1==close( xdp_fds[ i ].prog_link_fd ) ) {
1244 0 : FD_LOG_ERR(( "close(%d) failed (%d-%s)", xsk_map_fd, errno, fd_io_strerror( errno ) ));
1245 0 : }
1246 0 : }
1247 0 : }
1248 :
1249 0 : ctx->waker_client_idx = tile->waker_client_idx;
1250 0 : if( FD_UNLIKELY( topo->sleep_obj_id!=ULONG_MAX ) ) {
1251 0 : FD_TEST( ctx->waker_client_idx!=ULONG_MAX );
1252 0 : for( uint i=0U; i<ctx->xsk_cnt; i++ ) {
1253 0 : struct epoll_event ev = { .events = EPOLLIN, .data.fd = ctx->xsk[ i ].xsk_fd };
1254 0 : if( FD_UNLIKELY( -1==epoll_ctl( FD_WAKER_INNER_FD( ctx->waker_client_idx ), EPOLL_CTL_ADD, ctx->xsk[ i ].xsk_fd, &ev ) ) )
1255 0 : FD_LOG_ERR(( "epoll_ctl(ADD,xsk_fd) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1256 0 : }
1257 0 : }
1258 :
1259 0 : double tick_per_ns = fd_tempo_tick_per_ns( NULL );
1260 0 : ctx->xdp_stats_interval_ticks = (long)( FD_XDP_STATS_INTERVAL_NS * tick_per_ns );
1261 :
1262 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
1263 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
1264 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
1265 0 : }
1266 :
1267 : static void
1268 : init_device_table( fd_net_ctx_t * ctx,
1269 : void * netdev_tbl_shm,
1270 0 : void * netdev_tbl_local ) {
1271 0 : FD_TEST( fd_netdev_tbl_join( &ctx->netdev_shared, netdev_tbl_shm ) );
1272 0 : FD_TEST( fd_netdev_tbl_new( netdev_tbl_local, NETDEV_MAX, BOND_MASTER_MAX ) );
1273 0 : FD_TEST( fd_netdev_tbl_join( &ctx->netdev_tbl, netdev_tbl_local ) );
1274 0 : ctx->netdev_seq = fd_netdev_tbl_copy( &ctx->netdev_tbl, &ctx->netdev_shared );
1275 0 : fd_net_gre_tunnels_refresh( &ctx->net, &ctx->netdev_tbl );
1276 0 : }
1277 :
1278 : FD_FN_UNUSED static void
1279 : unprivileged_init( fd_topo_t const * topo,
1280 0 : fd_topo_tile_t const * tile ) {
1281 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
1282 :
1283 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
1284 0 : fd_net_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_net_ctx_t), sizeof(fd_net_ctx_t) );
1285 0 : FD_TEST( ctx->xsk_cnt!=0 );
1286 0 : FD_TEST( ctx->free_tx.queue!=NULL );
1287 0 : (void)FD_SCRATCH_ALLOC_APPEND( l, alignof(ulong), tile->xdp.free_ring_depth * sizeof(ulong) );
1288 0 : void * netdev_tbl_local = FD_SCRATCH_ALLOC_APPEND( l, fd_netdev_tbl_align(), fd_netdev_tbl_footprint( NETDEV_MAX, BOND_MASTER_MAX ) );
1289 0 : void * fib_local_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_fib4_align(), fd_fib4_footprint( tile->xdp.route_max, tile->xdp.route_peer_max ) );
1290 0 : void * fib_main_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_fib4_align(), fd_fib4_footprint( tile->xdp.route_max, tile->xdp.route_peer_max ) );
1291 0 : FD_TEST( fd_fib4_join( ctx->fib_local, fd_fib4_new( fib_local_mem, tile->xdp.route_max, tile->xdp.route_peer_max, tile->xdp.route_peer_seed ) ) );
1292 0 : FD_TEST( fd_fib4_join( ctx->fib_main, fd_fib4_new( fib_main_mem, tile->xdp.route_max, tile->xdp.route_peer_max, tile->xdp.route_peer_seed ) ) );
1293 :
1294 0 : ctx->net.kind_id = tile->kind_id;
1295 0 : ctx->net.tile_cnt = fd_topo_tile_name_cnt( topo, tile->name );
1296 :
1297 0 : ctx->net.bind_address = tile->net.bind_address;
1298 :
1299 : /* Put a bound on chunks we read from the input, to make sure they
1300 : are within in the data region of the workspace. */
1301 :
1302 0 : if( FD_UNLIKELY( !tile->in_cnt ) ) FD_LOG_ERR(( "net tile in link cnt is zero" ));
1303 0 : if( FD_UNLIKELY( tile->in_cnt>FD_NET_IN_MAX ) ) FD_LOG_ERR(( "net tile in link cnt %lu exceeds FD_NET_IN_MAX %lu", tile->in_cnt, FD_NET_IN_MAX ));
1304 0 : FD_TEST( tile->in_cnt<=32 );
1305 0 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
1306 0 : fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
1307 0 : if( !strcmp( link->name, "iproute_out" ) ) ctx->net.in_kind[i] = FD_NET_IN_KIND_IPROUTE;
1308 0 : else {
1309 0 : ctx->net.in_kind[i] = FD_NET_IN_KIND_TX;
1310 0 : if( FD_UNLIKELY( link->mtu!=FD_NET_MTU ) ) FD_LOG_ERR(( "net tile in link %s does not have a normal MTU", link->name ));
1311 0 : }
1312 :
1313 0 : ctx->net.in_dcache_ctx[ i ].wksp_base = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
1314 0 : ctx->net.in_dcache_ctx[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->net.in_dcache_ctx[ i ].wksp_base, link->dcache );
1315 0 : ctx->net.in_dcache_ctx[ i ].wmark = fd_dcache_compact_wmark( ctx->net.in_dcache_ctx[ i ].wksp_base, link->dcache, link->mtu );
1316 0 : }
1317 :
1318 0 : fd_net_rx_dst_ports_init( &ctx->net, topo, tile );
1319 :
1320 0 : ctx->netlnk_out_idx = fd_topo_find_tile_out_link( topo, tile, "net_netlnk", tile->kind_id );
1321 0 : if( FD_UNLIKELY( ctx->netlnk_out_idx==ULONG_MAX ) ) FD_LOG_ERR(( "netlink request link not found" ));
1322 0 : ctx->solicit_ip = 0U;
1323 :
1324 0 : ctx->waker_fseq = NULL;
1325 0 : if( FD_UNLIKELY( tile->is_waker_client ) ) {
1326 0 : ctx->waker_fseq = fd_fseq_join( fd_topo_obj_laddr( topo, tile->waker_fseq_obj_id ) );
1327 0 : FD_TEST( ctx->waker_fseq );
1328 0 : }
1329 :
1330 0 : for( uint j=0U; j<2U; j++ ) {
1331 0 : ctx->tx_flusher[ j ].pending_wmark = (ulong)( (double)tile->xdp.xdp_tx_queue_size * 0.7 );
1332 0 : ctx->tx_flusher[ j ].tail_flush_backoff = (long)( (double)tile->xdp.tx_flush_timeout_ns * fd_tempo_tick_per_ns( NULL ) );
1333 0 : ctx->tx_flusher[ j ].next_tail_flush_ticks = LONG_MAX;
1334 :
1335 0 : ctx->tx_flusher[ j ].prefbusy_last_poll_ticks = 0L;
1336 0 : ctx->tx_flusher[ j ].prefbusy_min_interval_ticks = (long)( PREFBUSY_MIN_INTERVAL_NS * fd_tempo_tick_per_ns( NULL ) );
1337 0 : ctx->tx_flusher[ j ].prefbusy_stall_timeout_ticks = (long)( PREFBUSY_STALL_TIMEOUT_NS * fd_tempo_tick_per_ns( NULL ) );
1338 0 : }
1339 :
1340 0 : ulong neigh4_obj_id = tile->xdp.neigh4_obj_id;
1341 0 : ulong ele_max = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "obj.%lu.ele_max", neigh4_obj_id );
1342 0 : ulong probe_max = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "obj.%lu.probe_max", neigh4_obj_id );
1343 0 : ulong seed = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "obj.%lu.seed", neigh4_obj_id );
1344 0 : if( FD_UNLIKELY( (ele_max==ULONG_MAX) | (probe_max==ULONG_MAX) | (seed==ULONG_MAX) ) )
1345 0 : FD_LOG_ERR(( "neigh4 hmap properties not set" ));
1346 0 : if( FD_UNLIKELY( !fd_neigh4_hmap_join(
1347 0 : ctx->neigh4,
1348 0 : fd_topo_obj_laddr( topo, neigh4_obj_id ),
1349 0 : ele_max,
1350 0 : probe_max,
1351 0 : seed ) ) ) {
1352 0 : FD_LOG_ERR(( "fd_neigh4_hmap_join failed" ));
1353 0 : }
1354 :
1355 0 : init_device_table( ctx, fd_topo_obj_laddr( topo, tile->xdp.netdev_tbl_obj_id ), netdev_tbl_local );
1356 :
1357 : /* Initialize TX free ring */
1358 :
1359 0 : ulong const frame_sz = 2048UL;
1360 0 : ulong frame_off = 0UL;
1361 0 : ulong const tx_depth = ctx->free_tx.depth;
1362 0 : for( ulong j=0; j<tx_depth; j++ ) {
1363 0 : ctx->free_tx.queue[ j ] = (ulong)ctx->umem + frame_off;
1364 0 : frame_off += frame_sz;
1365 0 : }
1366 0 : ctx->free_tx.prod = tx_depth;
1367 :
1368 : /* Initialize RX mcache chunks */
1369 :
1370 0 : for( ulong i=0UL; i<(tile->out_cnt); i++ ) {
1371 0 : fd_topo_link_t const * out_link = &topo->links[ tile->out_link_id[ i ] ];
1372 0 : fd_frag_meta_t * mcache = out_link->mcache;
1373 0 : for( ulong j=0UL; j<fd_mcache_depth( mcache ); j++ ) {
1374 0 : mcache[ j ].chunk = (uint)( ctx->net.pkt_buf_chunk0 + (frame_off>>FD_CHUNK_LG_SZ) );
1375 0 : frame_off += frame_sz;
1376 0 : }
1377 0 : }
1378 :
1379 : /* Initialize FILL ring */
1380 :
1381 0 : int _charge_busy = 0;
1382 0 : for( uint j=0U; j<ctx->xsk_cnt; j++ ) {
1383 0 : frame_off = net_xsk_bootstrap( ctx, j, frame_off );
1384 0 : net_rx_wakeup( ctx, &ctx->xsk[ j ], &_charge_busy );
1385 0 : net_tx_wakeup( ctx, &ctx->xsk[ j ], &_charge_busy );
1386 0 : }
1387 :
1388 0 : if( FD_UNLIKELY( frame_off > ctx->umem_sz ) ) {
1389 0 : FD_LOG_ERR(( "UMEM is too small" ));
1390 0 : }
1391 0 : }
1392 :
1393 : FD_FN_UNUSED static ulong
1394 : populate_allowed_seccomp( fd_topo_t const * topo,
1395 : fd_topo_tile_t const * tile,
1396 : ulong out_cnt,
1397 0 : struct sock_filter * out ) {
1398 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
1399 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
1400 0 : fd_net_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_net_ctx_t ), sizeof( fd_net_ctx_t ) );
1401 :
1402 : /* A bit of a hack, if there is no loopback XSK for this tile, we still need to pass
1403 : two "allow" FD arguments to the net policy, so we just make them both the same. */
1404 0 : int allow_fd2 = ctx->xsk_cnt>1UL ? ctx->xsk[ 1 ].xsk_fd : ctx->xsk[ 0 ].xsk_fd;
1405 0 : FD_TEST( ctx->xsk[ 0 ].xsk_fd >= 0 && allow_fd2 >= 0 );
1406 :
1407 : /* not a waker client in performance mode: no fd matches, epoll_ctl denied */
1408 0 : uint epoll_inner_fd = tile->is_waker_client ? (uint)FD_WAKER_INNER_FD( tile->waker_client_idx ) : (uint)-1;
1409 0 : uint epoll_outer_fd = tile->is_waker_client ? (uint)FD_WAKER_OUTER_FD : (uint)-1;
1410 0 : populate_sock_filter_policy_fd_xdp_tile( out_cnt, out, (uint)fd_log_private_logfile_fd(), (uint)ctx->xsk[ 0 ].xsk_fd, (uint)allow_fd2, epoll_inner_fd, epoll_outer_fd );
1411 0 : return sock_filter_policy_fd_xdp_tile_instr_cnt;
1412 0 : }
1413 :
1414 : FD_FN_UNUSED static ulong
1415 : populate_allowed_fds( fd_topo_t const * topo,
1416 : fd_topo_tile_t const * tile,
1417 : ulong out_fds_cnt,
1418 0 : int * out_fds ) {
1419 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
1420 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
1421 0 : fd_net_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_net_ctx_t ), sizeof( fd_net_ctx_t ) );
1422 :
1423 0 : if( FD_UNLIKELY( out_fds_cnt<8UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
1424 :
1425 0 : ulong out_cnt = 0UL;
1426 :
1427 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
1428 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
1429 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
1430 :
1431 0 : out_fds[ out_cnt++ ] = ctx->xsk[ 0 ].xsk_fd;
1432 0 : out_fds[ out_cnt++ ] = ctx->prog_link_fds[ 0 ];
1433 0 : if( FD_LIKELY( ctx->xsk_cnt>1UL ) ) out_fds[ out_cnt++ ] = ctx->xsk[ 1 ].xsk_fd;
1434 0 : if( FD_LIKELY( ctx->xsk_cnt>1UL ) ) out_fds[ out_cnt++ ] = ctx->prog_link_fds[ 1 ];
1435 0 : if( FD_UNLIKELY( tile->is_waker_client ) ) {
1436 0 : out_fds[ out_cnt++ ] = FD_WAKER_OUTER_FD; /* waker outer epoll fd (rearm) */
1437 0 : out_fds[ out_cnt++ ] = FD_WAKER_INNER_FD( tile->waker_client_idx ); /* waker inner epoll fd */
1438 0 : }
1439 0 : return out_cnt;
1440 0 : }
1441 :
1442 0 : #define STEM_BURST (1UL)
1443 0 : #define STEM_LAZY ((ulong)30e3) /* 30 us */
1444 :
1445 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_net_ctx_t
1446 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_net_ctx_t)
1447 :
1448 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
1449 0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
1450 : #define STEM_CALLBACK_PREVENT_PARK prevent_park
1451 0 : #define STEM_CALLBACK_BEFORE_CREDIT before_credit
1452 0 : #define STEM_CALLBACK_BEFORE_FRAG before_frag
1453 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
1454 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
1455 :
1456 : #include "../../stem/fd_stem.c"
1457 :
1458 : #ifndef FD_TILE_TEST
1459 : fd_topo_run_tile_t fd_tile_net = {
1460 : .name = "net",
1461 : .populate_allowed_seccomp = populate_allowed_seccomp,
1462 : .populate_allowed_fds = populate_allowed_fds,
1463 : .scratch_align = scratch_align,
1464 : .scratch_footprint = scratch_footprint,
1465 : .privileged_init = privileged_init,
1466 : .unprivileged_init = unprivileged_init,
1467 : .run = stem_run,
1468 : };
1469 : #endif
|