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 :
7 : #include <errno.h>
8 : #include <fcntl.h>
9 : #include <net/if.h>
10 : #include <netinet/in.h>
11 : #include <sys/socket.h> /* MSG_DONTWAIT needed before importing the net seccomp filter */
12 : #include <linux/if_xdp.h>
13 :
14 : #include "../fd_net_common.h"
15 : #include "../../metrics/fd_metrics.h"
16 : #include "../../netlink/fd_netlink_tile.h" /* neigh4_solicit */
17 : #include "../../topo/fd_topo.h"
18 :
19 : #include "../../../waltz/ip/fd_fib4.h"
20 : #include "../../../waltz/ip/fd_iproute.h"
21 : #include "../../../waltz/neigh/fd_neigh4_map.h"
22 : #include "../../../waltz/mib/fd_netdev_tbl.h"
23 : #include "../../../waltz/xdp/fd_xdp_redirect_user.h" /* fd_xsk_activate */
24 : #include "../../../waltz/xdp/fd_xsk.h"
25 : #include "../../../util/log/fd_dtrace.h"
26 : #include "../../../util/net/fd_eth.h"
27 : #include "../../../util/net/fd_ip4.h"
28 : #include "../../../util/net/fd_gre.h"
29 : #include "../../../util/pod/fd_pod_format.h"
30 :
31 : #include <unistd.h>
32 : #include <linux/if.h> /* struct ifreq */
33 : #include <sys/ioctl.h>
34 : #include <linux/if_arp.h>
35 : #include <linux/rtnetlink.h>
36 :
37 : #include "generated/fd_xdp_tile_seccomp.h"
38 :
39 : /* MAX_NET_INS controls the max number of TX links that a net tile can
40 : serve. */
41 :
42 : #define MAX_NET_INS (32UL)
43 :
44 0 : #define IN_KIND_NET (0U)
45 3 : #define IN_KIND_IPROUTE (1U)
46 :
47 : /* FD_XDP_STATS_INTERVAL_NS controls the XDP stats refresh interval.
48 : This should be lower than the interval at which the metrics tile
49 : collects metrics. */
50 :
51 0 : #define FD_XDP_STATS_INTERVAL_NS (11e6) /* 11ms */
52 :
53 : /* XSK_IDX_{MAIN,LO} are the hardcoded XSK indices in ctx->xsk[ ... ].
54 : Only net tile 0 has XSK_IDX_LO, all net tiles have XSK_IDX_MAIN. */
55 :
56 30 : #define XSK_IDX_MAIN 0
57 18 : #define XSK_IDX_LO 1
58 :
59 : /* XSK 'busy_poll_usecs' value (max amount of time
60 : spent spinning in a NAPI poll before returning back to
61 : userspace if the processing budget hasn't already ran out).
62 :
63 : 64us chosen based on napibusy configuration tested and
64 : shown in https://lwn.net/Articles/997491/ Linux patch
65 : cover letter. Chosen over fullbusy since for Firedancer
66 : the values of fullbusy are unnecessarily high and could
67 : cause some extra latency to regular non-Firedancer traffic.*/
68 0 : #define PREFBUSY_TIME_BUDGET_MICROS (64L)
69 :
70 : /* PREFBUSY_RX_BUDGET is the NAPI RX processing budget (max num RX
71 : packets that the NIC driver can move from the hw rings into the
72 : XSK rings per poll).
73 :
74 : Default RX budget used by NIC drivers is 64, therefore
75 : it is safest to use 64 in prefbusy polling. Also reduces
76 : TX starvation risks as the TX budget set by the NIC driver
77 : is also generally 64. */
78 0 : #define PREFBUSY_RX_BUDGET (64L)
79 :
80 : /* Min time between each prefbusy poll. Necessary to avoid a no RX
81 : scenario livelocking TX with overly frequent sendto calls, given
82 : prefbusy polls whenever the RX queue is empty.
83 :
84 : Value chosen based on experimentation on ixgbe, mlx5 and i40e as well
85 : as on varying CPUs and clock speeds. Too low -> lower max TX
86 : throughput when RX is very low. Too high -> lower max RX and TX throughput. */
87 0 : #define PREFBUSY_MIN_INTERVAL_NS (5e3) /* 5us */
88 :
89 : /* Max time since last prefbusy poll before a prefbusy poll is
90 : forced (has been read that polling can sometimes resolve a stall).
91 :
92 : Exact value again not particularly important as this is just extra
93 : protection against stalls which have not been observed in testing but
94 : are still good to protect against since there is no cost to doing so.
95 :
96 : Value of 150us chosen since it is easily large enough to not interfere
97 : with standard prefbusy runtime unless there is a serious problem. */
98 0 : #define PREFBUSY_STALL_TIMEOUT_NS (150e3) /* 150us */
99 :
100 : /* MAX_GRE_CNT is the maximum number of GRE tunnels the XDP tile will
101 : monitor. If a packet comes in with a source IP that doesn't match
102 : the endpoint of one of the first MAX_GRE_CNT tunnels (in the order
103 : the OS enumerates them), it will be dropped. This is limited for
104 : performance reasons. */
105 111 : #define MAX_GRE_CNT 4UL
106 :
107 : /* fd_net_in_ctx_t contains consumer information for an incoming tango
108 : link. It is used as part of the TX path. */
109 :
110 : typedef struct {
111 : fd_wksp_t * mem;
112 : ulong chunk0;
113 : ulong wmark;
114 : } fd_net_in_ctx_t;
115 :
116 : /* fd_net_out_ctx_t contains publisher information for a link to a
117 : downstream app tile. It is used as part of the RX path. */
118 :
119 : typedef struct {
120 : fd_frag_meta_t * mcache;
121 : ulong * sync;
122 : ulong depth;
123 : ulong seq;
124 : } fd_net_out_ctx_t;
125 :
126 : /* fd_net_flusher_t controls the pacing of XDP sendto calls for flushing
127 : TX batches. In the 'wakeup' XDP mode, no TX occurs unless the net
128 : tile wakes up the kernel periodically using the sendto() syscall.
129 : If sendto() is called too frequently, time is wasted on context
130 : switches. If sendto() is called not often enough, packets are
131 : delayed or dropped. sendto() calls make almost no guarantees how
132 : much packets are sent out, nor do they indicate when the kernel
133 : finishes a wakeup call (asynchronously dispatched). The net tile
134 : thus uses a myriad of flush triggers that were tested for best
135 : performance. */
136 :
137 : struct fd_net_flusher {
138 :
139 : /* Packets that were enqueued after the last sendto() wakeup are
140 : considered "pending". If there are more than pending_wmark packets
141 : pending, a wakeup is dispatched. Thus, this dispatch trigger is
142 : proportional to packet rate, but does not trigger if I/O is seldom. */
143 : ulong pending_cnt;
144 : ulong pending_wmark;
145 :
146 : /* Sometimes, packets are not flushed out even after a sendto()
147 : wakeup. This can result in the tail of a burst getting delayed or
148 : overrun. If more than tail_flush_backoff ticks pass since the last
149 : sendto() wakeup and there are still unacknowledged packets in the
150 : TX ring, issues another wakeup. Only used by "softirq" poll mode. */
151 : long next_tail_flush_ticks;
152 : long tail_flush_backoff;
153 :
154 : /* When the most recent prefbusy poll was. */
155 : long prefbusy_last_poll_ticks;
156 : /* Min time between each prefbusy poll. */
157 : long prefbusy_min_interval_ticks;
158 : /* Max time since last prefbusy poll before a prefbusy poll is
159 : forced (has been read that polling can sometimes resolve a stall). */
160 : long prefbusy_stall_timeout_ticks;
161 : };
162 :
163 : typedef struct fd_net_flusher fd_net_flusher_t;
164 :
165 : FD_PROTOTYPES_BEGIN
166 :
167 : /* fd_net_flusher_inc marks a new packet as enqueued. */
168 :
169 : static inline void
170 : fd_net_flusher_inc( fd_net_flusher_t * flusher,
171 18 : long now ) {
172 18 : flusher->pending_cnt++;
173 18 : long next_flush = now + flusher->tail_flush_backoff;
174 18 : flusher->next_tail_flush_ticks = fd_long_min( flusher->next_tail_flush_ticks, next_flush );
175 18 : }
176 :
177 : /* fd_net_flusher_check returns 1 if a sendto() wakeup should be issued
178 : immediately. now is a recent fd_tickcount() value.
179 : If tx_ring_empty==0 then the kernel is caught up with the net tile
180 : on the XDP TX ring. (Otherwise, the kernel is behind the net tile) */
181 :
182 : static inline int
183 : fd_net_flusher_check( fd_net_flusher_t * flusher,
184 : long now,
185 27 : int tx_ring_empty ) {
186 27 : int flush_level = flusher->pending_cnt >= flusher->pending_wmark;
187 27 : int flush_timeout = now >= flusher->next_tail_flush_ticks;
188 27 : int flush = flush_level || flush_timeout;
189 27 : if( !flush ) return 0;
190 27 : if( FD_UNLIKELY( tx_ring_empty ) ) {
191 : /* Flush requested but caught up */
192 3 : flusher->pending_cnt = 0UL;
193 3 : flusher->next_tail_flush_ticks = LONG_MAX;
194 3 : return 0;
195 3 : }
196 24 : return 1;
197 27 : }
198 :
199 : /* fd_net_flusher_wakeup signals a sendto() wakeup was done. now is a
200 : recent fd_tickcount() value. */
201 :
202 : static inline void
203 : fd_net_flusher_wakeup( fd_net_flusher_t * flusher,
204 24 : long now ) {
205 24 : flusher->pending_cnt = 0UL;
206 24 : flusher->next_tail_flush_ticks = now + flusher->tail_flush_backoff;
207 24 : }
208 :
209 : FD_PROTOTYPES_END
210 :
211 : /* fd_net_free_ring is a FIFO queue that stores pointers to free XDP TX
212 : frames. */
213 :
214 : struct fd_net_free_ring {
215 : ulong prod;
216 : ulong cons;
217 : ulong depth;
218 : ulong * queue;
219 : };
220 : typedef struct fd_net_free_ring fd_net_free_ring_t;
221 :
222 : typedef struct {
223 : /* An "XSK" is an AF_XDP socket */
224 : uint xsk_cnt;
225 : fd_xsk_t xsk[ 2 ];
226 : int prog_link_fds[ 2 ];
227 : uint if_virt;
228 :
229 : /* UMEM frame region within dcache */
230 : void * umem; /* Start of UMEM */
231 : ulong umem_sz; /* Size of UMEM */
232 :
233 : /* UMEM chunk region within workspace */
234 : uint umem_chunk0; /* Lowest allowed chunk number */
235 : uint umem_wmark; /* Highest allowed chunk number */
236 :
237 : /* All net tiles are subscribed to the same TX links. (These are
238 : incoming links from app tiles asking the net tile to send out packets)
239 : The net tiles "take turns" doing TX jobs based on the L3+L4 dst hash.
240 : net_tile_id is the index of the current interface, net_tile_cnt is the
241 : total amount of interfaces. */
242 : uint net_tile_id;
243 : uint net_tile_cnt;
244 :
245 : /* Details pertaining to an inflight send op */
246 : struct {
247 : uint xsk_idx;
248 : void * frame;
249 : uchar mac_addrs[12]; /* First 12 bytes of Ethernet header */
250 : uint src_ip; /* src_ip in net order */
251 :
252 : uint use_gre; /* The tx packet will be GRE-encapsulated */
253 : uint gre_outer_src_ip; /* For GRE: Outer iphdr's src_ip in net order */
254 : uint gre_outer_dst_ip; /* For GRE: Outer iphdr's dst_ip in net order */
255 : } tx_op;
256 :
257 : /* Round-robin cycle service operations */
258 : uint rr_idx;
259 :
260 : /* Ring tracking free packet buffers */
261 : fd_net_free_ring_t free_tx;
262 :
263 : uchar src_mac_addr[6];
264 : uint default_address;
265 :
266 : uint bind_address;
267 : ushort shred_listen_port;
268 : ushort quic_transaction_listen_port;
269 : ushort legacy_transaction_listen_port;
270 : ushort gossip_listen_port;
271 : ushort repair_client_listen_port;
272 : ushort repair_serve_listen_port;
273 : ushort txsend_src_port;
274 :
275 : ulong in_cnt;
276 : fd_net_in_ctx_t in[ MAX_NET_INS ];
277 : uchar in_kind[ MAX_NET_INS ];
278 : fd_iproute_msg_t iproute_msg;
279 :
280 : fd_net_out_ctx_t quic_out[1];
281 : fd_net_out_ctx_t shred_out[1];
282 : fd_net_out_ctx_t gossvf_out[1];
283 : fd_net_out_ctx_t repair_out[1];
284 : fd_net_out_ctx_t txsend_out[1];
285 :
286 : fd_net_out_ctx_t rserve_out[1];
287 : int rserve_enabled;
288 :
289 : /* XDP stats refresh timer */
290 : long xdp_stats_interval_ticks;
291 : long next_xdp_stats_refresh;
292 :
293 : /* TX flush timers */
294 : fd_net_flusher_t tx_flusher[2]; /* one per XSK */
295 :
296 : /* Route and neighbor tables */
297 : fd_fib4_t fib_local[1];
298 : fd_fib4_t fib_main[1];
299 : fd_neigh4_hmap_t neigh4[1];
300 : fd_netlink_neigh4_solicit_link_t neigh4_solicit[1];
301 :
302 : /* Netdev table */
303 : fd_netdev_tbl_join_t netdev_tbl; /* local copy in scratch (hot path) */
304 : fd_netdev_tbl_join_t netdev_shared; /* shared table in netbase (seqlock protected) */
305 : uint gre_tunnel_ip[MAX_GRE_CNT]; /* 0 means unused */
306 :
307 : struct {
308 : ulong rx_pkt_cnt;
309 : ulong rx_bytes_total;
310 : ulong rx_src_addr_invalid_cnt;
311 : ulong rx_undersz_cnt;
312 : ulong rx_fill_blocked_cnt;
313 : ulong rx_backp_cnt;
314 : long rx_busy_cnt;
315 : long rx_idle_cnt;
316 :
317 : ulong tx_submit_cnt;
318 : ulong tx_complete_cnt;
319 : ulong tx_bytes_total;
320 : ulong tx_route_fail_cnt[ FD_METRICS_COUNTER_NET_PKT_TX_ROUTE_FAIL_CNT ];
321 : ulong tx_invalid_cnt;
322 : ulong tx_no_xdp_cnt;
323 : ulong tx_neigh_fail_cnt;
324 : ulong tx_full_fail_cnt;
325 : long tx_busy_cnt;
326 : long tx_idle_cnt;
327 :
328 : ulong xsk_tx_wakeup_cnt;
329 : ulong xsk_rx_wakeup_cnt;
330 :
331 : ulong rx_gre_cnt;
332 : ulong rx_gre_ignored_cnt;
333 : ulong rx_gre_inv_pkt_cnt;
334 : ulong tx_gre_cnt;
335 : ulong tx_gre_route_fail_cnt;
336 : } metrics;
337 : } fd_net_ctx_t;
338 :
339 : fd_fib4_t *
340 : fd_net_tile_fib4_join( fd_fib4_t * out,
341 : fd_topo_t const * topo,
342 : fd_topo_tile_t const * tile,
343 0 : int main_table ) {
344 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
345 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
346 0 : (void)FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_net_ctx_t), sizeof(fd_net_ctx_t) );
347 0 : (void)FD_SCRATCH_ALLOC_APPEND( l, alignof(ulong), tile->xdp.free_ring_depth*sizeof(ulong) );
348 0 : (void)FD_SCRATCH_ALLOC_APPEND( l, fd_netdev_tbl_align(), fd_netdev_tbl_footprint( NETDEV_MAX, BOND_MASTER_MAX ) );
349 0 : void * local_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_fib4_align(), fd_fib4_footprint( tile->xdp.route_max, tile->xdp.route_peer_max ) );
350 0 : void * main_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_fib4_align(), fd_fib4_footprint( tile->xdp.route_max, tile->xdp.route_peer_max ) );
351 0 : return fd_fib4_join( out, main_table ? main_mem : local_mem );
352 0 : }
353 :
354 : FD_FN_CONST static inline ulong
355 9 : scratch_align( void ) {
356 9 : return 4096UL;
357 9 : }
358 :
359 : FD_FN_PURE static inline ulong
360 3 : scratch_footprint( fd_topo_tile_t const * tile ) {
361 3 : ulong l = FD_LAYOUT_INIT;
362 3 : l = FD_LAYOUT_APPEND( l, alignof(fd_net_ctx_t), sizeof(fd_net_ctx_t) );
363 3 : l = FD_LAYOUT_APPEND( l, alignof(ulong), tile->xdp.free_ring_depth * sizeof(ulong) );
364 3 : l = FD_LAYOUT_APPEND( l, fd_netdev_tbl_align(), fd_netdev_tbl_footprint( NETDEV_MAX, BOND_MASTER_MAX ) );
365 9 : 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 ) );
366 3 : return FD_LAYOUT_FINI( l, scratch_align() );
367 3 : }
368 :
369 : static void
370 0 : metrics_write( fd_net_ctx_t * ctx ) {
371 0 : FD_MCNT_SET( NET, PKT_RX, ctx->metrics.rx_pkt_cnt );
372 0 : FD_MCNT_SET( NET, PKT_RX_BYTES, ctx->metrics.rx_bytes_total );
373 0 : FD_MCNT_SET( NET, PKT_RX_UNDERSIZE, ctx->metrics.rx_undersz_cnt );
374 0 : FD_MCNT_SET( NET, PKT_RX_FILL_RING_FULL, ctx->metrics.rx_fill_blocked_cnt );
375 0 : FD_MCNT_SET( NET, PKT_RX_BACKPRESSURE, ctx->metrics.rx_backp_cnt );
376 0 : FD_MGAUGE_SET( NET, RX_BUFFER_BUSY, (ulong)fd_long_max( ctx->metrics.rx_busy_cnt, 0L ) );
377 0 : FD_MGAUGE_SET( NET, RX_BUFFER_IDLE, (ulong)fd_long_max( ctx->metrics.rx_idle_cnt, 0L ) );
378 0 : FD_MGAUGE_SET( NET, TX_BUFFER_BUSY, (ulong)fd_long_max( ctx->metrics.tx_busy_cnt, 0L ) );
379 0 : FD_MGAUGE_SET( NET, TX_BUFFER_IDLE, (ulong)fd_long_max( ctx->metrics.tx_idle_cnt, 0L ) );
380 :
381 0 : FD_MCNT_SET( NET, PKT_TX_SUBMITTED, ctx->metrics.tx_submit_cnt );
382 0 : FD_MCNT_SET( NET, PKT_TX_COMPLETED, ctx->metrics.tx_complete_cnt );
383 0 : FD_MCNT_SET( NET, PKT_TX_BYTES, ctx->metrics.tx_bytes_total );
384 0 : FD_MCNT_ENUM_COPY( NET, PKT_TX_ROUTE_FAIL, ctx->metrics.tx_route_fail_cnt );
385 0 : FD_MCNT_SET( NET, PKT_TX_INVALID, ctx->metrics.tx_invalid_cnt );
386 0 : FD_MCNT_SET( NET, PKT_TX_NO_NEIGHBOR, ctx->metrics.tx_neigh_fail_cnt );
387 0 : FD_MCNT_SET( NET, PKT_TX_RING_FULL, ctx->metrics.tx_full_fail_cnt );
388 :
389 0 : FD_MCNT_SET( NET, XSK_SYSCALL_TX, ctx->metrics.xsk_tx_wakeup_cnt );
390 0 : FD_MCNT_SET( NET, XSK_SYSCALL_RX, ctx->metrics.xsk_rx_wakeup_cnt );
391 :
392 0 : FD_MCNT_SET( NET, GRE_PKT_RX, ctx->metrics.rx_gre_cnt );
393 0 : FD_MCNT_SET( NET, GRE_PKT_RX_INVALID, ctx->metrics.rx_gre_inv_pkt_cnt );
394 0 : FD_MCNT_SET( NET, GRE_PKT_RX_IGNORED, ctx->metrics.rx_gre_ignored_cnt );
395 0 : FD_MCNT_SET( NET, GRE_PKT_TX_SUBMITTED, ctx->metrics.tx_gre_cnt );
396 0 : FD_MCNT_SET( NET, GRE_PKT_TX_NO_ROUTE, ctx->metrics.tx_gre_route_fail_cnt );
397 0 : FD_MCNT_SET( NET, PKT_RX_SRC_INVALID, ctx->metrics.rx_src_addr_invalid_cnt );
398 : /* fd_fib4_cnt includes the synthetic throw route at index zero. */
399 0 : FD_MGAUGE_SET( NET, ROUTE_COUNT_LOCAL, fd_ulong_sat_sub( fd_fib4_cnt( ctx->fib_local ), 1UL ) );
400 0 : FD_MGAUGE_SET( NET, ROUTE_COUNT_MAIN, fd_ulong_sat_sub( fd_fib4_cnt( ctx->fib_main ), 1UL ) );
401 0 : }
402 :
403 : struct xdp_statistics_v0 {
404 : __u64 rx_dropped; /* Dropped for other reasons */
405 : __u64 rx_invalid_descs; /* Dropped due to invalid descriptor */
406 : __u64 tx_invalid_descs; /* Dropped due to invalid descriptor */
407 : };
408 :
409 : struct xdp_statistics_v1 {
410 : __u64 rx_dropped; /* Dropped for other reasons */
411 : __u64 rx_invalid_descs; /* Dropped due to invalid descriptor */
412 : __u64 tx_invalid_descs; /* Dropped due to invalid descriptor */
413 : __u64 rx_ring_full; /* Dropped due to rx ring being full */
414 : __u64 rx_fill_ring_empty_descs; /* Failed to retrieve item from fill ring */
415 : __u64 tx_ring_empty_descs; /* Failed to retrieve item from tx ring */
416 : };
417 :
418 : static void
419 0 : poll_xdp_statistics( fd_net_ctx_t * ctx ) {
420 0 : struct xdp_statistics_v1 stats = {0};
421 0 : ulong xsk_cnt = ctx->xsk_cnt;
422 0 : for( ulong j=0UL; j<xsk_cnt; j++ ) {
423 0 : struct xdp_statistics_v1 sub_stats = {0};
424 0 : uint optlen = (uint)sizeof(struct xdp_statistics_v1);
425 0 : if( FD_UNLIKELY( -1==getsockopt( ctx->xsk[ j ].xsk_fd, SOL_XDP, XDP_STATISTICS, &sub_stats, &optlen ) ) )
426 0 : FD_LOG_ERR(( "getsockopt(SOL_XDP, XDP_STATISTICS) failed: %s", strerror( errno ) ));
427 0 : if( FD_UNLIKELY( optlen!=sizeof(struct xdp_statistics_v0) &&
428 0 : optlen!=sizeof(struct xdp_statistics_v1) ) ) {
429 0 : FD_LOG_ERR(( "getsockopt(SOL_XDP, XDP_STATISTICS) returned unexpected size %u", optlen ));
430 0 : }
431 0 : stats.rx_dropped += sub_stats.rx_dropped;
432 0 : stats.rx_invalid_descs += sub_stats.rx_invalid_descs;
433 0 : stats.tx_invalid_descs += sub_stats.tx_invalid_descs;
434 0 : stats.rx_ring_full += sub_stats.rx_ring_full;
435 0 : stats.rx_fill_ring_empty_descs += sub_stats.rx_fill_ring_empty_descs;
436 0 : stats.tx_ring_empty_descs += sub_stats.tx_ring_empty_descs;
437 0 : }
438 :
439 0 : FD_MCNT_SET( NET, XDP_RX_OTHER_DROPPED, stats.rx_dropped );
440 0 : FD_MCNT_SET( NET, XDP_RX_INVALID_DESCRIPTOR, stats.rx_invalid_descs );
441 0 : FD_MCNT_SET( NET, XDP_TX_INVALID_DESCRIPTOR, stats.tx_invalid_descs );
442 0 : FD_MCNT_SET( NET, XDP_RX_RING_FULL, stats.rx_ring_full );
443 0 : FD_MCNT_SET( NET, XDP_RX_FILL_RING_EMPTY, stats.rx_fill_ring_empty_descs );
444 0 : FD_MCNT_SET( NET, XDP_TX_RING_EMPTY, stats.tx_ring_empty_descs );
445 0 : }
446 :
447 : /* net_is_fatal_xdp_error returns 1 if the given errno returned by an
448 : XDP API indicates a non-recoverable error code. The net tile should
449 : crash if it sees such an error so the problem does not go undetected.
450 : Otherwise, returns 0. */
451 :
452 : static int
453 0 : net_is_fatal_xdp_error( int err ) {
454 0 : return err==ESOCKTNOSUPPORT || err==EOPNOTSUPP || err==EINVAL ||
455 0 : err==EPERM;
456 0 : }
457 :
458 : /* net_gre_tunnel_ip fills ctx->gre_tunnel_ip. The first gre_tunnel_cnt
459 : entries will be populated with the IP address of the GRE tunnel peer
460 : for the first gre_tunnel_cnt untagged GRE tunnels, and the rest of
461 : the entries will be set to 0, where gre_tunnel_cnt = min(MAX_GRE_CNT,
462 : the number of untagged GRE tunnels). Returns gre_tunnel_cnt. */
463 :
464 : static ulong
465 6 : net_gre_tunnel_ip( fd_net_ctx_t * ctx ) {
466 6 : fd_netdev_t * dev_tbl = ctx->netdev_tbl.dev_tbl;
467 6 : ushort dev_cnt = ctx->netdev_tbl.hdr->dev_cnt;
468 :
469 6 : ulong gre_tunnel_cnt = 0UL;
470 6 : memset( ctx->gre_tunnel_ip, '\0', MAX_GRE_CNT*sizeof(uint) );
471 21 : for( ushort if_idx = 0; (if_idx<dev_cnt) & (gre_tunnel_cnt<MAX_GRE_CNT); if_idx++ ) {
472 15 : fd_netdev_t const * dev = dev_tbl+if_idx;
473 15 : if( dev->dev_type==ARPHRD_IPGRE && dev->gre_dst_ip ) ctx->gre_tunnel_ip[ gre_tunnel_cnt++ ] = dev->gre_dst_ip;
474 15 : }
475 6 : return gre_tunnel_cnt;
476 6 : }
477 :
478 :
479 : /* net_tx_ready returns 1 if we can submit a job to this TX ring, and 0 otherwise.
480 : Reasons for block include:
481 : - No TX buffer is available (free ring empty)
482 : - TX ring is full
483 :
484 : tx_ring: pointer to the XDP TX ring
485 : free_ring: pointer to the free TX ring */
486 :
487 : static int
488 : net_tx_ready( fd_xdp_ring_t * tx_ring,
489 42 : fd_net_free_ring_t * free_ring ) {
490 42 : if( FD_UNLIKELY( free_ring->prod == free_ring->cons ) ) return 0; /* drop - no free buffers */
491 36 : if( FD_UNLIKELY( fd_xdp_ring_full( tx_ring ) ) ) return 0; /* drop - tx ring full */
492 33 : return 1;
493 36 : }
494 :
495 : /* net_rx_wakeup triggers xsk_recvmsg to run in the kernel. Needs to be
496 : called periodically in order to receive packets. */
497 :
498 : static void
499 : net_rx_wakeup( fd_net_ctx_t * ctx,
500 : fd_xsk_t * xsk,
501 0 : int * charge_busy ) {
502 0 : FD_VOLATILE( *xsk->ring_rx.cons ) = xsk->ring_rx.cached_cons; /* write-back local copies to fseqs */
503 0 : FD_VOLATILE( *xsk->ring_fr.prod ) = xsk->ring_fr.cached_prod;
504 0 : if( !fd_xsk_rx_need_wakeup( xsk ) ) return;
505 0 : *charge_busy = 1;
506 0 : struct msghdr _ignored[ 1 ] = { 0 };
507 0 : if( FD_UNLIKELY( -1==recvmsg( xsk->xsk_fd, _ignored, MSG_DONTWAIT ) ) ) {
508 0 : if( FD_UNLIKELY( net_is_fatal_xdp_error( errno ) ) ) {
509 0 : FD_LOG_ERR(( "xsk recvmsg failed xsk_fd=%d (%i-%s)", xsk->xsk_fd, errno, fd_io_strerror( errno ) ));
510 0 : }
511 0 : if( FD_UNLIKELY( errno!=EAGAIN ) ) {
512 0 : long ts = fd_log_wallclock();
513 0 : if( ts > xsk->log_suppress_until_ns ) {
514 0 : FD_LOG_WARNING(( "xsk recvmsg failed xsk_fd=%d (%i-%s)", xsk->xsk_fd, errno, fd_io_strerror( errno ) ));
515 0 : xsk->log_suppress_until_ns = ts + (long)1e9;
516 0 : }
517 0 : }
518 0 : }
519 0 : ctx->metrics.xsk_rx_wakeup_cnt++;
520 0 : }
521 :
522 : /* net_tx_wakeup triggers xsk_sendmsg to run in the kernel. Needs to be
523 : called periodically in order to transmit packets. Should only be called
524 : if there are unconsumed packets in Tx ring. */
525 :
526 : static void
527 : net_tx_wakeup( fd_net_ctx_t * ctx,
528 : fd_xsk_t * xsk,
529 24 : int * charge_busy ) {
530 24 : FD_VOLATILE( *xsk->ring_tx.prod ) = xsk->ring_tx.cached_prod; /* write-back local copies to fseqs */
531 24 : FD_VOLATILE( *xsk->ring_cr.cons ) = xsk->ring_cr.cached_cons;
532 24 : if( !fd_xsk_tx_need_wakeup( xsk ) ) return;
533 0 : *charge_busy = 1;
534 0 : if( FD_UNLIKELY( -1==sendto( xsk->xsk_fd, NULL, 0, MSG_DONTWAIT, NULL, 0 ) ) ) {
535 0 : if( FD_UNLIKELY( net_is_fatal_xdp_error( errno ) ) ) {
536 0 : FD_LOG_ERR(( "xsk sendto failed xsk_fd=%d (%i-%s)", xsk->xsk_fd, errno, fd_io_strerror( errno ) ));
537 0 : }
538 0 : if( FD_UNLIKELY( errno!=EAGAIN ) ) {
539 0 : long ts = fd_log_wallclock();
540 0 : if( ts > xsk->log_suppress_until_ns ) {
541 0 : FD_LOG_WARNING(( "xsk sendto failed xsk_fd=%d (%i-%s)", xsk->xsk_fd, errno, fd_io_strerror( errno ) ));
542 0 : xsk->log_suppress_until_ns = ts + (long)1e9;
543 0 : }
544 0 : }
545 0 : }
546 0 : ctx->metrics.xsk_tx_wakeup_cnt++;
547 0 : }
548 :
549 : /* net_tx_periodic_wakeup does a timer based xsk_sendmsg wakeup. */
550 :
551 : static inline int
552 : net_tx_periodic_wakeup( fd_net_ctx_t * ctx,
553 : uint xsk_idx,
554 : long now,
555 27 : int * charge_busy ) {
556 27 : fd_xdp_ring_t * tx_ring = &ctx->xsk[ xsk_idx ].ring_tx;
557 27 : int tx_ring_empty = fd_xdp_ring_empty( tx_ring, FD_XDP_RING_ROLE_PROD );
558 27 : if( fd_net_flusher_check( ctx->tx_flusher+xsk_idx, now, tx_ring_empty ) ) {
559 24 : net_tx_wakeup( ctx, &ctx->xsk[ xsk_idx ], charge_busy );
560 24 : fd_net_flusher_wakeup( ctx->tx_flusher+xsk_idx, now );
561 24 : }
562 27 : return 0;
563 27 : }
564 :
565 : static void
566 0 : during_housekeeping( fd_net_ctx_t * ctx ) {
567 0 : long now = fd_tickcount();
568 0 : if( FD_LIKELY( !fd_seqlock_locked_hint( &ctx->netdev_shared.hdr->seqlock ) ) ) {
569 0 : fd_netdev_tbl_copy( &ctx->netdev_tbl, &ctx->netdev_shared );
570 0 : }
571 0 : net_gre_tunnel_ip( ctx );
572 :
573 0 : ctx->metrics.rx_busy_cnt = 0UL;
574 0 : ctx->metrics.rx_idle_cnt = 0UL;
575 0 : ctx->metrics.tx_busy_cnt = 0UL;
576 0 : ctx->metrics.tx_idle_cnt = fd_seq_diff( ctx->free_tx.prod, ctx->free_tx.cons );
577 0 : for( uint j=0U; j<ctx->xsk_cnt; j++ ) {
578 0 : fd_xsk_t * xsk = &ctx->xsk[ j ];
579 0 : FD_COMPILER_MFENCE();
580 : /* Write back local copies to fseqs that we own */
581 0 : FD_VOLATILE( *xsk->ring_fr.prod ) = xsk->ring_fr.cached_prod;
582 0 : FD_VOLATILE( *xsk->ring_rx.cons ) = xsk->ring_rx.cached_cons;
583 0 : FD_VOLATILE( *xsk->ring_tx.prod ) = xsk->ring_tx.cached_prod;
584 0 : FD_VOLATILE( *xsk->ring_cr.cons ) = xsk->ring_cr.cached_cons;
585 :
586 : /* Refresh kernel-owned seq numbers for accurate stats */
587 0 : xsk->ring_fr.cached_cons = FD_VOLATILE_CONST( *xsk->ring_fr.cons );
588 0 : xsk->ring_rx.cached_prod = FD_VOLATILE_CONST( *xsk->ring_rx.prod );
589 0 : xsk->ring_tx.cached_cons = FD_VOLATILE_CONST( *xsk->ring_tx.cons );
590 0 : xsk->ring_cr.cached_prod = FD_VOLATILE_CONST( *xsk->ring_cr.prod );
591 :
592 0 : FD_COMPILER_MFENCE();
593 0 : ctx->metrics.rx_busy_cnt += (long)(int)( xsk->ring_rx.cached_prod - xsk->ring_rx.cached_cons );
594 0 : ctx->metrics.rx_idle_cnt += (long)(int)( xsk->ring_fr.cached_prod - xsk->ring_fr.cached_cons );
595 0 : ctx->metrics.tx_busy_cnt += (long)(int)( xsk->ring_tx.cached_prod - xsk->ring_tx.cached_cons );
596 0 : ctx->metrics.tx_busy_cnt += (long)(int)( xsk->ring_cr.cached_prod - xsk->ring_cr.cached_cons );
597 0 : }
598 :
599 0 : if( now > ctx->next_xdp_stats_refresh ) {
600 0 : ctx->next_xdp_stats_refresh = now + ctx->xdp_stats_interval_ticks;
601 0 : poll_xdp_statistics( ctx );
602 0 : }
603 0 : }
604 :
605 :
606 : /* net_tx_route resolves the xsk index, src ip address, src MAC address, and
607 : dst MAC address. Returns 1 on success, 0 on failure.
608 : On success, tx_op->{xsk_idx,src_ip,mac_addrs} is set, and if the dst_ip
609 : belongs to a GRE interface, is_gre_inf will set to 1 and
610 : tx_op->{gre_outer_src_ip, gre_outer_dst_ip} will be loaded from the netdev
611 : table. is_gre_inf is set to 0 if dst_ip doesn't belong to a GRE interface. */
612 :
613 : static int
614 : net_tx_route( fd_net_ctx_t * ctx,
615 : uint dst_ip,
616 33 : uint * is_gre_inf ) {
617 :
618 : /* Route lookup */
619 :
620 33 : fd_fib4_hop_t hop[2] = {0};
621 33 : hop[0] = fd_fib4_lookup( ctx->fib_local, dst_ip, 0UL );
622 33 : hop[1] = fd_fib4_lookup( ctx->fib_main, dst_ip, 0UL );
623 33 : fd_fib4_hop_t const * next_hop = fd_fib4_hop_or( hop+0, hop+1 );
624 :
625 33 : uint rtype = next_hop->rtype;
626 33 : uint if_idx = next_hop->if_idx;
627 33 : uint ip4_src = next_hop->ip4_src;
628 :
629 33 : if( FD_UNLIKELY( rtype==FD_FIB4_RTYPE_LOCAL ) ) {
630 0 : rtype = FD_FIB4_RTYPE_UNICAST;
631 0 : if_idx = 1;
632 0 : }
633 :
634 33 : if( FD_UNLIKELY( rtype!=FD_FIB4_RTYPE_UNICAST ) ) {
635 0 : uint reason = fd_uint_if( rtype==FD_FIB4_RTYPE_THROW,
636 0 : FD_METRICS_ENUM_ROUTE_FAIL_V_NO_ROUTE_IDX,
637 0 : FD_METRICS_ENUM_ROUTE_FAIL_V_ROUTE_TYPE_IDX );
638 0 : ctx->metrics.tx_route_fail_cnt[ reason ]++;
639 0 : return 0;
640 0 : }
641 :
642 33 : fd_netdev_t * netdev = fd_netdev_tbl_query( &ctx->netdev_tbl, if_idx );
643 33 : if( !netdev ) {
644 3 : ctx->metrics.tx_route_fail_cnt[ FD_METRICS_ENUM_ROUTE_FAIL_V_INTERFACE_IDX ]++;
645 3 : return 0;
646 3 : }
647 :
648 30 : ip4_src = fd_uint_if( !!ctx->bind_address, ctx->bind_address, ip4_src );
649 30 : ctx->tx_op.src_ip = ip4_src;
650 30 : ctx->tx_op.xsk_idx = UINT_MAX;
651 :
652 30 : FD_TEST( is_gre_inf );
653 30 : *is_gre_inf = 0;
654 30 : if( netdev->dev_type==ARPHRD_LOOPBACK ) {
655 : /* Set Ethernet src and dst address to 00:00:00:00:00:00 */
656 0 : memset( ctx->tx_op.mac_addrs, 0, 12UL );
657 0 : ctx->tx_op.xsk_idx = XSK_IDX_LO;
658 : /* Set preferred src address to 127.0.0.1 if no bind address is set */
659 0 : if( !ctx->tx_op.src_ip ) ctx->tx_op.src_ip = FD_IP4_ADDR( 127,0,0,1 );
660 0 : return 1;
661 30 : } else if( netdev->dev_type==ARPHRD_IPGRE ) {
662 : /* skip MAC addrs lookup for GRE inner dst ip */
663 12 : if( netdev->gre_src_ip ) ctx->tx_op.gre_outer_src_ip = netdev->gre_src_ip;
664 12 : ctx->tx_op.gre_outer_dst_ip = netdev->gre_dst_ip;
665 12 : *is_gre_inf = 1;
666 12 : return 1;
667 12 : }
668 :
669 18 : if( FD_UNLIKELY( netdev->dev_type!=ARPHRD_ETHER ) ) return 0; // drop
670 :
671 18 : if( FD_UNLIKELY( if_idx!=ctx->if_virt ) ) {
672 0 : ctx->metrics.tx_no_xdp_cnt++;
673 0 : return 0;
674 0 : }
675 18 : ctx->tx_op.xsk_idx = XSK_IDX_MAIN;
676 :
677 : /* Neighbor resolve */
678 18 : uint neigh_ip = next_hop->ip4_gw;
679 18 : if( !neigh_ip ) neigh_ip = dst_ip;
680 :
681 18 : fd_neigh4_entry_t neigh[1];
682 18 : int neigh_res = fd_neigh4_hmap_query_entry( ctx->neigh4, neigh_ip, neigh );
683 18 : if( FD_UNLIKELY( neigh_res!=FD_MAP_SUCCESS ) ) {
684 : /* Neighbor not found */
685 0 : fd_netlink_neigh4_solicit( ctx->neigh4_solicit, neigh_ip, if_idx, fd_frag_meta_ts_comp( fd_tickcount() ) );
686 0 : ctx->metrics.tx_neigh_fail_cnt++;
687 0 : return 0;
688 0 : }
689 18 : if( FD_UNLIKELY( neigh->state != FD_NEIGH4_STATE_ACTIVE ) ) {
690 0 : ctx->metrics.tx_neigh_fail_cnt++;
691 0 : return 0;
692 0 : }
693 18 : ip4_src = fd_uint_if( !ip4_src, ctx->default_address, ip4_src );
694 18 : ctx->tx_op.src_ip = ip4_src;
695 18 : memcpy( ctx->tx_op.mac_addrs+0, neigh->mac_addr, 6 );
696 18 : memcpy( ctx->tx_op.mac_addrs+6, netdev->mac_addr, 6 );
697 :
698 18 : return 1;
699 18 : }
700 :
701 : /* before_frag is called when a new metadata descriptor for a TX job is
702 : found. This callback determines whether this net tile is responsible
703 : for the TX job. If so, it prepares the TX op for the during_frag and
704 : after_frag callbacks. */
705 :
706 : static inline int
707 : before_frag( fd_net_ctx_t * ctx,
708 : ulong in_idx,
709 : ulong seq,
710 18 : ulong sig ) {
711 18 : (void)seq;
712 :
713 18 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_IPROUTE ) ) return 0;
714 :
715 : /* Find interface index of next packet */
716 18 : ulong proto = fd_disco_netmux_sig_proto( sig );
717 18 : if( FD_UNLIKELY( proto!=DST_PROTO_OUTGOING ) ) return 1;
718 :
719 : /* Load balance TX */
720 18 : uint net_tile_cnt = ctx->net_tile_cnt;
721 18 : uint hash = (uint)fd_disco_netmux_sig_hash( sig );
722 18 : uint target_idx = hash % net_tile_cnt;
723 18 : uint net_tile_id = ctx->net_tile_id;
724 18 : uint dst_ip = fd_disco_netmux_sig_ip( sig );
725 :
726 : /* Skip if another net tile is responsible for this packet.
727 : Fast path for net tiles other than net_tile 0. */
728 :
729 18 : if( net_tile_id!=0 && net_tile_id!=target_idx ) return 1; /* ignore */
730 :
731 :
732 18 : ctx->tx_op.use_gre = 0;
733 18 : ctx->tx_op.gre_outer_dst_ip = 0;
734 18 : ctx->tx_op.gre_outer_src_ip = 0;
735 18 : uint is_gre_inf = 0;
736 :
737 18 : if( FD_UNLIKELY( !net_tx_route( ctx, dst_ip, &is_gre_inf ) ) ) {
738 0 : return 1; /* metrics incremented by net_tx_route */
739 0 : }
740 :
741 18 : uint xsk_idx = ctx->tx_op.xsk_idx;
742 :
743 18 : if( is_gre_inf ) {
744 12 : uint inner_src_ip = ctx->tx_op.src_ip;
745 12 : if( FD_UNLIKELY( !inner_src_ip ) ) {
746 0 : ctx->metrics.tx_gre_route_fail_cnt++;
747 0 : return 1;
748 0 : }
749 : /* Find the MAC addrs for the eth hdr, and src ip for outer ip4 hdr if not found in netdev tbl */
750 12 : ctx->tx_op.src_ip = 0;
751 12 : is_gre_inf = 0;
752 12 : if( FD_UNLIKELY( !net_tx_route( ctx, ctx->tx_op.gre_outer_dst_ip, &is_gre_inf ) ) ) {
753 0 : ctx->metrics.tx_gre_route_fail_cnt++;
754 0 : return 1;
755 0 : }
756 12 : if( is_gre_inf ) {
757 : /* Only one layer of tunnelling supported */
758 0 : ctx->metrics.tx_gre_route_fail_cnt++;
759 0 : return 1;
760 0 : }
761 12 : if( !ctx->tx_op.gre_outer_src_ip ) {
762 6 : ctx->tx_op.gre_outer_src_ip = ctx->tx_op.src_ip;
763 6 : }
764 12 : ctx->tx_op.use_gre = 1; /* indicate to during_frag to use GRE header */
765 12 : ctx->tx_op.src_ip = inner_src_ip;
766 12 : xsk_idx = XSK_IDX_MAIN;
767 12 : }
768 :
769 18 : if( FD_UNLIKELY( xsk_idx>=ctx->xsk_cnt ) ) {
770 : /* Packet does not route to an XDP interface */
771 0 : ctx->metrics.tx_no_xdp_cnt++;
772 0 : return 1;
773 0 : }
774 :
775 18 : if( xsk_idx==XSK_IDX_LO ) target_idx = 0; /* loopback always targets tile 0 */
776 :
777 : /* Skip if another net tile is responsible for this packet */
778 :
779 18 : if( net_tile_id!=target_idx ) return 1; /* ignore */
780 :
781 : /* Skip if TX is blocked */
782 :
783 18 : fd_xsk_t * xsk = &ctx->xsk[ xsk_idx ];
784 18 : fd_net_free_ring_t * free = &ctx->free_tx;
785 18 : if( FD_UNLIKELY( !net_tx_ready( &xsk->ring_tx, free ) ) ) {
786 0 : ctx->metrics.tx_full_fail_cnt++;
787 0 : return 1;
788 0 : }
789 :
790 : /* Allocate buffer for receive */
791 18 : ulong alloc_seq = free->cons;
792 18 : void * frame = (void *)free->queue[ alloc_seq % free->depth ];
793 18 : free->cons = fd_seq_inc( alloc_seq, 1UL );
794 :
795 18 : ctx->tx_op.frame = frame;
796 :
797 18 : return 0; /* continue */
798 18 : }
799 :
800 : /* during_frag is called when before_frag has committed to transmit an
801 : outgoing packet. */
802 :
803 : static inline void
804 : during_frag( fd_net_ctx_t * ctx,
805 : ulong in_idx,
806 : ulong seq FD_PARAM_UNUSED,
807 : ulong sig FD_PARAM_UNUSED,
808 : ulong chunk,
809 : ulong sz,
810 18 : ulong ctl FD_PARAM_UNUSED ) {
811 18 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>FD_NET_MTU ) )
812 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
813 :
814 18 : if( FD_UNLIKELY( ctx->in_kind[in_idx]==IN_KIND_IPROUTE ) ) {
815 0 : if( FD_UNLIKELY( sz!=sizeof(fd_iproute_msg_t) ) ) FD_LOG_ERR(( "invalid iproute message size %lu", sz ));
816 0 : fd_memcpy( &ctx->iproute_msg, fd_chunk_to_laddr_const( ctx->in[in_idx].mem, chunk ), sizeof(fd_iproute_msg_t) );
817 0 : return;
818 0 : }
819 :
820 18 : if( FD_UNLIKELY( sz<( sizeof(fd_eth_hdr_t)+sizeof(fd_ip4_hdr_t) ) ) )
821 0 : FD_LOG_ERR(( "packet too small %lu (in_idx=%lu)", sz, in_idx ));
822 :
823 18 : if( FD_UNLIKELY( sz>FD_ETH_PAYLOAD_MAX ) )
824 0 : FD_LOG_ERR(( "packet too big %lu (in_idx=%lu)", sz, in_idx ));
825 :
826 18 : void * frame = ctx->tx_op.frame;
827 18 : if( FD_UNLIKELY( (ulong)frame < (ulong)ctx->umem ) )
828 0 : FD_LOG_ERR(( "frame %p out of bounds (below %p)", frame, (void *)ctx->umem ));
829 18 : ulong umem_off = (ulong)frame - (ulong)ctx->umem;
830 18 : if( FD_UNLIKELY( (ulong)umem_off > (ulong)ctx->umem_sz ) )
831 0 : FD_LOG_ERR(( "frame %p out of bounds (beyond %p)", frame, (void *)ctx->umem_sz ));
832 :
833 : /* Speculatively copy frame into XDP buffer */
834 18 : uchar const * src = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
835 :
836 18 : if( ctx->tx_op.use_gre ) {
837 : /* Discard the ethernet hdr from src. Copy the rest to where the inner ip4_hdr is.
838 : Safe from overflow: FD_ETH_PAYLOAD_MAX + header overhead < frame size (2048UL) */
839 12 : ulong overhead = sizeof(fd_eth_hdr_t) + sizeof(fd_ip4_hdr_t) + sizeof(fd_gre_hdr_t);
840 12 : fd_memcpy( (void *)( (ulong)ctx->tx_op.frame + overhead ), src + sizeof(fd_eth_hdr_t), sz - sizeof(fd_eth_hdr_t) );
841 12 : } else {
842 6 : fd_memcpy( ctx->tx_op.frame, src, sz );
843 6 : }
844 18 : }
845 :
846 : /* after_frag is called when the during_frag memcpy was _not_ overrun. */
847 :
848 : static void
849 : after_frag( fd_net_ctx_t * ctx,
850 : ulong in_idx,
851 : ulong seq,
852 : ulong sig,
853 : ulong sz,
854 : ulong tsorig,
855 : ulong tspub,
856 90 : fd_stem_context_t * stem ) {
857 90 : (void)seq; (void)sig; (void)tsorig; (void)tspub; (void)stem;
858 :
859 90 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_IPROUTE ) ) {
860 72 : fd_iproute_msg_t const * msg = &ctx->iproute_msg;
861 72 : if( msg->op==FD_IPROUTE_OP_FLUSH ) {
862 6 : fd_fib4_clear( ctx->fib_local );
863 6 : fd_fib4_clear( ctx->fib_main );
864 6 : return;
865 6 : }
866 66 : fd_fib4_t * fib;
867 66 : if( msg->table_id==RT_TABLE_LOCAL ) fib = ctx->fib_local;
868 66 : else if( msg->table_id==RT_TABLE_MAIN ) fib = ctx->fib_main;
869 0 : else return;
870 66 : if( msg->op==FD_IPROUTE_OP_UPSERT && FD_UNLIKELY( !fd_fib4_insert( fib, msg->dst_addr, msg->prefix, msg->prio, &msg->hop ) ) ) {
871 6 : FD_LOG_WARNING(( "route update dropped: route table full (increase [net.max_routes] or [net.max_peer_routes])" ));
872 6 : if( FD_UNLIKELY( ctx->net_tile_id==0U ) ) {
873 3 : fd_netlink_route4_sync( ctx->neigh4_solicit, fd_frag_meta_ts_comp( fd_tickcount() ) );
874 3 : }
875 6 : }
876 60 : else if( msg->op==FD_IPROUTE_OP_DELETE ) fd_fib4_remove( fib, msg->dst_addr, msg->prefix, msg->prio );
877 66 : return;
878 66 : }
879 :
880 : /* Current send operation */
881 :
882 18 : uchar * frame = ctx->tx_op.frame;
883 18 : uint xsk_idx = ctx->tx_op.xsk_idx;
884 :
885 : /* Select Ethernet addresses */
886 18 : memcpy( frame, ctx->tx_op.mac_addrs, 12 );
887 :
888 18 : uchar * iphdr = frame + sizeof(fd_eth_hdr_t);
889 :
890 18 : if( ctx->tx_op.use_gre ) {
891 :
892 : /* For GRE packets, the ethertype will always be FD_ETH_HDR_TYPE_IP. outer source ip can't be 0 */
893 12 : if( FD_UNLIKELY( ctx->tx_op.gre_outer_src_ip==0 ) ) {
894 0 : ctx->metrics.tx_gre_route_fail_cnt++;
895 0 : return;
896 0 : }
897 :
898 : /* Write the last two bytes for eth_hdr */
899 12 : FD_STORE( ushort, frame+12, fd_ushort_bswap( FD_ETH_HDR_TYPE_IP ) );
900 :
901 12 : uchar * outer_iphdr = frame + sizeof(fd_eth_hdr_t);
902 12 : uchar * gre_hdr = outer_iphdr + sizeof(fd_ip4_hdr_t);
903 12 : uchar * inner_iphdr = gre_hdr + sizeof(fd_gre_hdr_t);
904 :
905 : /* outer hdr + gre hdr + inner net_tot_len */
906 12 : ushort outer_net_tot_len = (ushort)( sizeof(fd_ip4_hdr_t) + sizeof(fd_gre_hdr_t) + fd_ushort_bswap( ( (fd_ip4_hdr_t *)inner_iphdr )->net_tot_len ) );
907 :
908 : /* Construct outer ip header */
909 12 : fd_ip4_hdr_t ip4_outer = (fd_ip4_hdr_t) {
910 12 : .verihl = FD_IP4_VERIHL( 4,5 ),
911 12 : .tos = 0,
912 12 : .net_tot_len = fd_ushort_bswap( outer_net_tot_len ),
913 12 : .net_id = 0,
914 12 : .net_frag_off = fd_ushort_bswap( FD_IP4_HDR_FRAG_OFF_DF ),
915 12 : .ttl = 64,
916 12 : .protocol = FD_IP4_HDR_PROTOCOL_GRE,
917 12 : .check = 0,
918 12 : .saddr = ctx->tx_op.gre_outer_src_ip,
919 12 : .daddr = ctx->tx_op.gre_outer_dst_ip,
920 12 : };
921 12 : ip4_outer.check = fd_ip4_hdr_check_fast( &ip4_outer );
922 12 : FD_STORE( fd_ip4_hdr_t, outer_iphdr, ip4_outer );
923 :
924 : /* Construct gre header */
925 12 : fd_gre_hdr_t gre_hdr_ = {
926 12 : .flags_version = FD_GRE_HDR_FLG_VER_BASIC,
927 12 : .protocol = fd_ushort_bswap( FD_ETH_HDR_TYPE_IP )
928 12 : };
929 12 : FD_STORE( fd_gre_hdr_t, gre_hdr, gre_hdr_ );
930 :
931 12 : iphdr = inner_iphdr;
932 12 : sz = sizeof(fd_eth_hdr_t) + outer_net_tot_len;
933 12 : xsk_idx = 0;
934 12 : }
935 :
936 : /* Construct (inner) ip header */
937 18 : uint ihl = FD_IP4_GET_LEN( *(fd_ip4_hdr_t *)iphdr );
938 18 : uint ver = FD_IP4_GET_VERSION( *(fd_ip4_hdr_t *)iphdr );
939 18 : uint ip4_saddr = FD_LOAD( uint, iphdr+12 );
940 18 : ushort ethertype = FD_LOAD( ushort, frame+12 );
941 :
942 18 : if( FD_UNLIKELY( ethertype!=fd_ushort_bswap( FD_ETH_HDR_TYPE_IP ) ) ) {
943 0 : FD_LOG_CRIT(( "in link %lu attempted to send packet with invalid ethertype %04x",
944 0 : in_idx, fd_ushort_bswap( ethertype ) ));
945 0 : }
946 :
947 18 : if( FD_UNLIKELY( ver!=0x4 ) ) {
948 0 : FD_LOG_WARNING(( "in_idx %lu: invalid IP version (%u)", in_idx, ver ));
949 0 : ctx->metrics.tx_invalid_cnt++;
950 0 : return;
951 0 : }
952 :
953 18 : if( FD_UNLIKELY( ihl<sizeof(fd_ip4_hdr_t) ||
954 18 : (sizeof(fd_eth_hdr_t)+ihl)>sz ) ) {
955 0 : FD_LOG_WARNING(( "in_idx %lu: invalid IHL (%u)", in_idx, ihl ));
956 0 : ctx->metrics.tx_invalid_cnt++;
957 0 : return;
958 0 : }
959 :
960 18 : if( ip4_saddr==0 ) {
961 18 : if( FD_UNLIKELY( ctx->tx_op.src_ip==0 ) ) {
962 : /* Outgoing IPv4 packet with unknown src IP or invalid IHL */
963 0 : ctx->metrics.tx_route_fail_cnt[ FD_METRICS_ENUM_ROUTE_FAIL_V_SOURCE_IP_IDX ]++;
964 0 : return;
965 0 : }
966 : /* Recompute checksum after changing header */
967 18 : FD_STORE( uint, iphdr+12, ctx->tx_op.src_ip );
968 18 : FD_STORE( ushort, iphdr+10, 0 );
969 18 : FD_STORE( ushort, iphdr+10, fd_ip4_hdr_check( iphdr ) );
970 18 : }
971 :
972 : /* Submit packet TX job
973 :
974 : Invariant for ring_tx: prod-cons<length
975 : (This invariant breaks if any other packet is sent over this ring
976 : between before_frag and this point, e.g. send_arp_probe.) */
977 :
978 18 : fd_xsk_t * xsk = &ctx->xsk[ xsk_idx ];
979 18 : fd_xdp_ring_t * tx_ring = &xsk->ring_tx;
980 18 : uint tx_seq = tx_ring->cached_prod;
981 18 : uint tx_mask = tx_ring->depth - 1U;
982 18 : xsk->ring_tx.packet_ring[ tx_seq&tx_mask ] = (struct xdp_desc) {
983 18 : .addr = (ulong)frame - (ulong)ctx->umem,
984 18 : .len = (uint)sz,
985 18 : .options = 0
986 18 : };
987 :
988 : /* Frame is now owned by kernel. Clear tx_op. */
989 18 : ctx->tx_op.frame = NULL;
990 :
991 : /* Register newly enqueued packet */
992 18 : tx_ring->cached_prod = tx_seq+1U;
993 18 : ctx->metrics.tx_submit_cnt++;
994 18 : ctx->metrics.tx_bytes_total += sz;
995 18 : if( ctx->tx_op.use_gre ) ctx->metrics.tx_gre_cnt++;
996 18 : fd_net_flusher_inc( ctx->tx_flusher+xsk_idx, fd_tickcount() );
997 18 : }
998 :
999 : /* net_rx_packet is called when a new Ethernet frame is available.
1000 : Attempts to copy out the frame to a downstream tile. */
1001 :
1002 : static void
1003 : net_rx_packet( fd_net_ctx_t * ctx,
1004 : ulong umem_off,
1005 : ulong sz,
1006 27 : uint * freed_chunk ) {
1007 :
1008 27 : if( FD_UNLIKELY( sz<sizeof(fd_eth_hdr_t)+sizeof(fd_ip4_hdr_t)+sizeof(fd_udp_hdr_t) ) ) {
1009 0 : FD_DTRACE_PROBE( net_tile_err_rx_undersz );
1010 0 : ctx->metrics.rx_undersz_cnt++;
1011 0 : return;
1012 0 : }
1013 :
1014 27 : uchar * packet = (uchar *)ctx->umem + umem_off;
1015 27 : uchar const * packet_end = packet + sz;
1016 27 : fd_ip4_hdr_t * iphdr = (fd_ip4_hdr_t *)(packet + sizeof(fd_eth_hdr_t));
1017 :
1018 27 : if( FD_UNLIKELY( ((fd_eth_hdr_t *)packet)->net_type!=fd_ushort_bswap( FD_ETH_HDR_TYPE_IP ) ) ) return;
1019 :
1020 27 : int is_packet_gre = 0;
1021 : /* Discard the GRE overhead (outer iphdr and gre hdr) */
1022 27 : if( iphdr->protocol == FD_IP4_HDR_PROTOCOL_GRE ) {
1023 15 : if( FD_UNLIKELY( !ctx->gre_tunnel_ip[0] ) ) { /* if the first entry is 0, they all are */
1024 0 : ctx->metrics.rx_gre_ignored_cnt++;
1025 0 : return;
1026 0 : }
1027 15 : ulong gre_ipver = FD_IP4_GET_VERSION( *iphdr );
1028 15 : ulong gre_iplen = FD_IP4_GET_LEN( *iphdr );
1029 15 : if( FD_UNLIKELY( gre_ipver!=0x4 || gre_iplen<20 ) ) {
1030 0 : FD_DTRACE_PROBE( net_tile_err_rx_noip );
1031 0 : ctx->metrics.rx_gre_inv_pkt_cnt++; /* drop IPv6 packets */
1032 0 : return;
1033 0 : }
1034 :
1035 15 : int found = 0;
1036 75 : for( ulong i=0UL; i<MAX_GRE_CNT; i++ ) found |= (iphdr->saddr==ctx->gre_tunnel_ip[i]);
1037 15 : if( FD_UNLIKELY( (!found) | (iphdr->saddr==0U) ) ) {
1038 3 : ctx->metrics.rx_src_addr_invalid_cnt++;
1039 3 : return;
1040 3 : }
1041 :
1042 12 : ulong overhead = gre_iplen + sizeof(fd_gre_hdr_t);
1043 12 : if( FD_UNLIKELY( (uchar *)iphdr+overhead+sizeof(fd_ip4_hdr_t)>packet_end ) ) {
1044 0 : FD_DTRACE_PROBE( net_tile_err_rx_undersz );
1045 0 : ctx->metrics.rx_undersz_cnt++; /* inner ip4 header invalid */
1046 0 : return;
1047 0 : }
1048 :
1049 : /* The new iphdr is where the inner iphdr was. Copy over the eth_hdr */
1050 12 : iphdr = (fd_ip4_hdr_t *)((uchar *)iphdr + overhead);
1051 12 : uchar * new_packet = (uchar *)iphdr - sizeof(fd_eth_hdr_t);
1052 12 : fd_memcpy( new_packet, packet, sizeof(fd_eth_hdr_t) );
1053 12 : sz -= overhead;
1054 12 : packet = new_packet;
1055 12 : umem_off = (ulong)( packet - (uchar *)ctx->umem );
1056 12 : is_packet_gre = 1;
1057 12 : }
1058 :
1059 : /* Translate packet to UMEM frame index */
1060 24 : ulong chunk = ctx->umem_chunk0 + (umem_off>>FD_CHUNK_LG_SZ);
1061 24 : ulong ctl = umem_off & 0x3fUL;
1062 :
1063 : /* Filter for UDP/IPv4 packets. */
1064 24 : ulong ipver = FD_IP4_GET_VERSION( *iphdr );
1065 24 : ulong iplen = FD_IP4_GET_LEN ( *iphdr );
1066 24 : if( FD_UNLIKELY( ipver!=0x4 || iplen<20 ||
1067 24 : iphdr->protocol!=FD_IP4_HDR_PROTOCOL_UDP ) ) {
1068 0 : FD_DTRACE_PROBE( net_tile_err_rx_noip );
1069 0 : ctx->metrics.rx_undersz_cnt++; /* drop IPv6 packets */
1070 0 : return;
1071 0 : }
1072 :
1073 24 : uchar const * udp = (uchar *)iphdr + iplen;
1074 24 : if( FD_UNLIKELY( udp+sizeof(fd_udp_hdr_t) > packet_end ) ) {
1075 0 : FD_DTRACE_PROBE( net_tile_err_rx_undersz );
1076 0 : ctx->metrics.rx_undersz_cnt++;
1077 0 : return;
1078 0 : }
1079 :
1080 24 : fd_udp_hdr_t const * udp_hdr = (fd_udp_hdr_t const *)udp;
1081 24 : ulong const udp_sz = fd_ushort_bswap( udp_hdr->net_len );
1082 24 : if( FD_UNLIKELY( (udp_sz<sizeof(fd_udp_hdr_t)) | (udp+udp_sz>packet_end) ) ) {
1083 6 : FD_DTRACE_PROBE( net_tile_err_rx_undersz );
1084 6 : ctx->metrics.rx_undersz_cnt++;
1085 6 : return;
1086 6 : }
1087 :
1088 : /* Extract IP dest addr and UDP src/dest port */
1089 18 : uint ip_srcaddr = iphdr->saddr;
1090 18 : ushort udp_srcport = fd_ushort_bswap( udp_hdr->net_sport );
1091 18 : ushort udp_dstport = fd_ushort_bswap( udp_hdr->net_dport );
1092 :
1093 18 : if( FD_UNLIKELY( fd_ip4_addr_is_mcast( ip_srcaddr ) ) ) {
1094 0 : ctx->metrics.rx_src_addr_invalid_cnt++;
1095 0 : return;
1096 0 : }
1097 :
1098 18 : FD_DTRACE_PROBE_4( net_tile_pkt_rx, ip_srcaddr, udp_srcport, udp_dstport, sz );
1099 :
1100 : /* Route packet to downstream tile */
1101 18 : ushort proto;
1102 18 : fd_net_out_ctx_t * out;
1103 18 : if( FD_UNLIKELY( udp_dstport==ctx->shred_listen_port ) ) {
1104 18 : proto = DST_PROTO_SHRED;
1105 18 : out = ctx->shred_out;
1106 18 : } else if( FD_UNLIKELY( udp_dstport==ctx->quic_transaction_listen_port ) ) {
1107 0 : proto = DST_PROTO_TPU_QUIC;
1108 0 : out = ctx->quic_out;
1109 0 : } else if( FD_UNLIKELY( udp_dstport==ctx->legacy_transaction_listen_port ) ) {
1110 0 : proto = DST_PROTO_TPU_UDP;
1111 0 : out = ctx->quic_out;
1112 0 : } else if( FD_UNLIKELY( udp_dstport==ctx->gossip_listen_port ) ) {
1113 0 : proto = DST_PROTO_GOSSIP;
1114 0 : out = ctx->gossvf_out;
1115 0 : } else if( FD_UNLIKELY( udp_dstport==ctx->repair_client_listen_port ) ) {
1116 0 : proto = DST_PROTO_REPAIR;
1117 0 : if( FD_UNLIKELY( sz == REPAIR_PING_SZ ) ) out = ctx->repair_out; /* ping-pong */
1118 0 : else out = ctx->shred_out;
1119 0 : } else if( FD_UNLIKELY( udp_dstport==ctx->repair_serve_listen_port ) ) {
1120 0 : if( FD_UNLIKELY( !ctx->rserve_enabled ) ) return;
1121 0 : proto = DST_PROTO_RSERVE;
1122 0 : out = ctx->rserve_out;
1123 0 : } else if( FD_UNLIKELY( udp_dstport==ctx->txsend_src_port ) ) {
1124 0 : proto = DST_PROTO_SEND;
1125 0 : out = ctx->txsend_out;
1126 0 : } else {
1127 0 : FD_LOG_ERR(( "Firedancer received a UDP packet on port %hu which was not expected. "
1128 0 : "Only the following ports should be configured to forward packets: "
1129 0 : "%hu, %hu, %hu, %hu, %hu, %hu (excluding any 0 ports, which can be ignored)."
1130 0 : "Please report this error to Firedancer maintainers.",
1131 0 : udp_dstport,
1132 0 : ctx->shred_listen_port,
1133 0 : ctx->quic_transaction_listen_port,
1134 0 : ctx->legacy_transaction_listen_port,
1135 0 : ctx->gossip_listen_port,
1136 0 : ctx->repair_client_listen_port,
1137 0 : ctx->repair_serve_listen_port ));
1138 0 : }
1139 :
1140 : /* tile can decide how to partition based on src ip addr and src port */
1141 18 : ulong sig = fd_disco_netmux_sig( ip_srcaddr, udp_srcport, ip_srcaddr, proto, 14UL+8UL+iplen );
1142 :
1143 : /* Peek the mline for an old frame */
1144 18 : fd_frag_meta_t * mline = out->mcache + fd_mcache_line_idx( out->seq, out->depth );
1145 18 : *freed_chunk = mline->chunk;
1146 :
1147 : /* Overwrite the mline with the new frame */
1148 18 : ulong tspub = (ulong)fd_frag_meta_ts_comp( fd_tickcount() );
1149 18 : # if FD_HAS_AVX
1150 18 : fd_mcache_publish_avx( out->mcache, out->depth, out->seq, sig, chunk, sz, ctl, 0, tspub );
1151 : # else
1152 : fd_mcache_publish( out->mcache, out->depth, out->seq, sig, chunk, sz, ctl, 0, tspub );
1153 : # endif
1154 :
1155 : /* Wind up for the next iteration */
1156 18 : out->seq = fd_seq_inc( out->seq, 1UL );
1157 :
1158 18 : if( is_packet_gre ) ctx->metrics.rx_gre_cnt++;
1159 18 : ctx->metrics.rx_pkt_cnt++;
1160 18 : ctx->metrics.rx_bytes_total += sz;
1161 18 : }
1162 :
1163 : /* net_comp_event is called when an XDP TX frame is free again. */
1164 :
1165 : static void
1166 : net_comp_event( fd_net_ctx_t * ctx,
1167 : fd_xsk_t * xsk,
1168 0 : uint comp_seq ) {
1169 :
1170 : /* Locate the incoming frame */
1171 :
1172 0 : fd_xdp_ring_t * comp_ring = &xsk->ring_cr;
1173 0 : uint comp_mask = comp_ring->depth - 1U;
1174 0 : ulong frame = FD_VOLATILE_CONST( comp_ring->frame_ring[ comp_seq&comp_mask ] );
1175 0 : ulong const frame_mask = FD_NET_MTU - 1UL;
1176 0 : FD_STATIC_ASSERT( FD_ULONG_IS_POW2( FD_NET_MTU ), "FD_NET_MTU must be a power of two" );
1177 0 : if( FD_UNLIKELY( frame+FD_NET_MTU > ctx->umem_sz ) ) {
1178 0 : FD_LOG_ERR(( "Bounds check failed: frame=0x%lx umem_sz=0x%lx",
1179 0 : frame, (ulong)ctx->umem_sz ));
1180 0 : }
1181 :
1182 : /* Check if we have space to return the freed frame */
1183 :
1184 0 : fd_net_free_ring_t * free = &ctx->free_tx;
1185 0 : ulong free_prod = free->prod;
1186 0 : ulong free_mask = free->depth - 1UL;
1187 0 : ulong free_cons = free->cons;
1188 0 : long free_cnt = fd_seq_diff( free_prod, free_cons );
1189 0 : FD_TEST( free_prod >= free_cons );
1190 0 : if( FD_UNLIKELY( free_cnt>=(long)free->depth ) ) return; /* blocked */
1191 :
1192 0 : free->queue[ free_prod&free_mask ] = (ulong)ctx->umem + (frame & (~frame_mask));
1193 0 : free->prod = fd_seq_inc( free_prod, 1UL );
1194 :
1195 : /* Wind up for next iteration */
1196 :
1197 0 : comp_ring->cached_cons = comp_seq+1U;
1198 0 : ctx->metrics.tx_complete_cnt++;
1199 0 : }
1200 :
1201 : /* net_rx_event is called when a new XDP RX frame is available. Calls
1202 : net_rx_packet, then returns the packet back to the kernel via the fill
1203 : ring. */
1204 :
1205 : static void
1206 : net_rx_event( fd_net_ctx_t * ctx,
1207 : fd_xsk_t * xsk,
1208 27 : uint rx_seq ) {
1209 : /* Locate the incoming frame */
1210 :
1211 27 : fd_xdp_ring_t * rx_ring = &xsk->ring_rx;
1212 27 : uint rx_mask = rx_ring->depth - 1U;
1213 27 : struct xdp_desc frame = FD_VOLATILE_CONST( rx_ring->packet_ring[ rx_seq&rx_mask ] );
1214 :
1215 27 : if( FD_UNLIKELY( frame.len>FD_NET_MTU ) )
1216 0 : FD_LOG_ERR(( "received a UDP packet with a too large payload (%u)", frame.len ));
1217 :
1218 : /* Check if we have space in the fill ring to free the frame */
1219 :
1220 27 : fd_xdp_ring_t * fill_ring = &xsk->ring_fr;
1221 27 : if( FD_UNLIKELY( fd_xdp_ring_full( fill_ring ) ) ) {
1222 0 : ctx->metrics.rx_fill_blocked_cnt++;
1223 0 : return; /* blocked */
1224 0 : }
1225 :
1226 : /* Pass it to the receive handler */
1227 :
1228 27 : uint freed_chunk = (uint)( ctx->umem_chunk0 + (frame.addr>>FD_CHUNK_LG_SZ) );
1229 27 : net_rx_packet( ctx, frame.addr, frame.len, &freed_chunk );
1230 27 : FD_COMPILER_MFENCE();
1231 27 : rx_ring->cached_cons = rx_seq+1U;
1232 :
1233 : /* Every RX operation returns one frame to the FILL ring. If the
1234 : packet was forwarded to a downstream ring, the newly shadowed frame
1235 : is returned. Otherwise, the frame just received is returned. */
1236 :
1237 27 : if( FD_UNLIKELY( ( freed_chunk < ctx->umem_chunk0 ) |
1238 27 : ( freed_chunk > ctx->umem_wmark ) ) ) {
1239 0 : FD_LOG_CRIT(( "mcache corruption detected: chunk=%u chunk0=%u wmark=%u",
1240 0 : freed_chunk, ctx->umem_chunk0, ctx->umem_wmark ));
1241 0 : }
1242 :
1243 27 : FD_STATIC_ASSERT( FD_ULONG_IS_POW2( FD_NET_MTU ), "FD_NET_MTU must be a power of two" );
1244 27 : uint fill_prod = fill_ring->cached_prod;
1245 27 : uint fill_mask = (fill_ring->depth)-1U;
1246 27 : ulong frame_mask = FD_NET_MTU - 1UL;
1247 27 : ulong freed_off = (freed_chunk - ctx->umem_chunk0)<<FD_CHUNK_LG_SZ;
1248 27 : fill_ring->frame_ring[ fill_prod&fill_mask ] = freed_off & (~frame_mask);
1249 27 : fill_ring->cached_prod = fill_prod+1U;
1250 27 : }
1251 :
1252 : static void
1253 : before_credit_softirq( fd_net_ctx_t * ctx,
1254 : int * charge_busy,
1255 : uint rr_idx,
1256 27 : fd_xsk_t * rr_xsk ) {
1257 :
1258 27 : net_tx_periodic_wakeup( ctx, rr_idx, fd_tickcount(), charge_busy );
1259 :
1260 : /* Fire RX event if we have RX desc avail */
1261 27 : if( !fd_xdp_ring_empty( &rr_xsk->ring_rx, FD_XDP_RING_ROLE_CONS ) ) {
1262 27 : *charge_busy = 1;
1263 27 : net_rx_event( ctx, rr_xsk, rr_xsk->ring_rx.cached_cons );
1264 27 : } else {
1265 0 : net_rx_wakeup( ctx, rr_xsk, charge_busy );
1266 :
1267 : /* Iterate onto the next NAPI queue. */
1268 0 : ctx->rr_idx++;
1269 0 : ctx->rr_idx = fd_uint_if( ctx->rr_idx>=ctx->xsk_cnt, 0, ctx->rr_idx );
1270 0 : }
1271 27 : }
1272 :
1273 : static int
1274 : net_prefbusy_poll_ready( fd_xsk_t * rr_xsk,
1275 : fd_net_flusher_t * flusher,
1276 0 : long now ) {
1277 :
1278 0 : if( FD_LIKELY( now < ( flusher->prefbusy_last_poll_ticks + flusher->prefbusy_min_interval_ticks ) ) ) return 0;
1279 0 : if( FD_UNLIKELY( now > ( flusher->prefbusy_last_poll_ticks + flusher->prefbusy_stall_timeout_ticks ) ) ) return 1;
1280 :
1281 0 : int rx_empty = fd_xdp_ring_empty( &rr_xsk->ring_rx, FD_XDP_RING_ROLE_CONS );
1282 :
1283 0 : return rx_empty;
1284 0 : }
1285 :
1286 : static void
1287 : net_prefbusy_poll_flush( fd_net_flusher_t * flusher,
1288 0 : long now ) {
1289 0 : flusher->prefbusy_last_poll_ticks = now;
1290 0 : }
1291 :
1292 : static void
1293 : before_credit_prefbusy( fd_net_ctx_t * ctx,
1294 : int * charge_busy,
1295 : uint rr_idx,
1296 0 : fd_xsk_t * rr_xsk ) {
1297 :
1298 0 : fd_net_flusher_t * flusher = ctx->tx_flusher+rr_idx;
1299 0 : if( FD_UNLIKELY( net_prefbusy_poll_ready( rr_xsk, flusher, fd_tickcount() ) ) ) {
1300 : /* NAPI needs to be polled to process new TX from
1301 : Firedancer's net tile and process new RX from the NIC. */
1302 :
1303 0 : FD_VOLATILE( *rr_xsk->ring_tx.prod ) = rr_xsk->ring_tx.cached_prod; /* write-back local copies to fseqs */
1304 0 : FD_VOLATILE( *rr_xsk->ring_cr.cons ) = rr_xsk->ring_cr.cached_cons;
1305 0 : FD_VOLATILE( *rr_xsk->ring_rx.cons ) = rr_xsk->ring_rx.cached_cons;
1306 0 : FD_VOLATILE( *rr_xsk->ring_fr.prod ) = rr_xsk->ring_fr.cached_prod;
1307 :
1308 0 : if( FD_UNLIKELY( -1==sendto( rr_xsk->xsk_fd, NULL, 0, MSG_DONTWAIT, NULL, 0 ) ) ) {
1309 0 : if( FD_UNLIKELY( net_is_fatal_xdp_error( errno ) ) ) {
1310 0 : FD_LOG_ERR(( "xsk sendto failed xsk_fd=%d (%i-%s)", rr_xsk->xsk_fd, errno, fd_io_strerror( errno ) ));
1311 0 : }
1312 0 : if( FD_UNLIKELY( errno!=EAGAIN ) ) {
1313 0 : long ts = fd_log_wallclock();
1314 0 : if( ts > rr_xsk->log_suppress_until_ns ) {
1315 0 : FD_LOG_WARNING(( "xsk sendto failed xsk_fd=%d (%i-%s)", rr_xsk->xsk_fd, errno, fd_io_strerror( errno ) ));
1316 0 : rr_xsk->log_suppress_until_ns = ts + (long)1e9;
1317 0 : }
1318 0 : }
1319 0 : }
1320 : /* Since xsk sendmsg in prefbusy mode drives both rx and tx, both are incremented */
1321 0 : ctx->metrics.xsk_tx_wakeup_cnt++;
1322 0 : ctx->metrics.xsk_rx_wakeup_cnt++;
1323 :
1324 0 : net_prefbusy_poll_flush( flusher, fd_tickcount() );
1325 0 : }
1326 :
1327 : /* Process new RX from xsk ring if there is any. */
1328 0 : if( !fd_xdp_ring_empty( &rr_xsk->ring_rx, FD_XDP_RING_ROLE_CONS ) ) {
1329 0 : *charge_busy = 1;
1330 0 : net_rx_event( ctx, rr_xsk, rr_xsk->ring_rx.cached_cons );
1331 0 : }
1332 : /* Iterate onto the next NAPI queue. */
1333 0 : ctx->rr_idx++;
1334 0 : ctx->rr_idx = fd_uint_if( ctx->rr_idx>=ctx->xsk_cnt, 0, ctx->rr_idx );
1335 0 : }
1336 :
1337 : /* before_credit is called every loop iteration. */
1338 :
1339 : static void
1340 : before_credit( fd_net_ctx_t * ctx,
1341 : fd_stem_context_t * stem,
1342 27 : int * charge_busy ) {
1343 27 : (void)stem;
1344 : /* A previous send attempt was overrun. A corrupt copy of the packet was
1345 : placed into an XDP frame, but the frame was not yet submitted to the
1346 : TX ring. Return the tx buffer to the free list. */
1347 :
1348 27 : if( ctx->tx_op.frame ) {
1349 0 : *charge_busy = 1;
1350 0 : fd_net_free_ring_t * free = &ctx->free_tx;
1351 0 : ulong alloc_seq = free->prod;
1352 0 : free->queue[ alloc_seq % free->depth ] = (ulong)ctx->tx_op.frame;
1353 0 : free->prod = fd_seq_inc( alloc_seq, 1UL );
1354 0 : ctx->tx_op.frame = NULL;
1355 0 : }
1356 :
1357 : /* Check if new packets are available or if TX frames are free again
1358 : (Round-robin through sockets) */
1359 :
1360 27 : uint rr_idx = ctx->rr_idx;
1361 27 : fd_xsk_t * rr_xsk = &ctx->xsk[ rr_idx ];
1362 :
1363 27 : if( FD_LIKELY( !rr_xsk->prefbusy_poll_enabled ) ) {
1364 : /* Default poll mode which relies on irqs and wakeups */
1365 27 : before_credit_softirq( ctx, charge_busy, rr_idx, rr_xsk );
1366 27 : } else {
1367 0 : before_credit_prefbusy( ctx, charge_busy, rr_idx, rr_xsk );
1368 0 : }
1369 :
1370 : /* Fire comp event if we have comp desc avail */
1371 27 : if( !fd_xdp_ring_empty( &rr_xsk->ring_cr, FD_XDP_RING_ROLE_CONS ) ) {
1372 0 : *charge_busy = 1;
1373 0 : net_comp_event( ctx, rr_xsk, rr_xsk->ring_cr.cached_cons );
1374 0 : }
1375 27 : }
1376 :
1377 : /* net_xsk_bootstrap assigns UMEM frames to the FILL ring. */
1378 :
1379 : static ulong
1380 : net_xsk_bootstrap( fd_net_ctx_t * ctx,
1381 : uint xsk_idx,
1382 0 : ulong frame_off ) {
1383 0 : fd_xsk_t * xsk = &ctx->xsk[ xsk_idx ];
1384 :
1385 0 : ulong const frame_sz = FD_NET_MTU;
1386 0 : ulong const fr_depth = ctx->xsk[ xsk_idx ].ring_fr.depth/2UL;
1387 :
1388 0 : fd_xdp_ring_t * fill = &xsk->ring_fr;
1389 0 : uint fill_prod = fill->cached_prod;
1390 0 : for( ulong j=0UL; j<fr_depth; j++ ) {
1391 0 : fill->frame_ring[ j ] = frame_off;
1392 0 : frame_off += frame_sz;
1393 0 : }
1394 0 : FD_VOLATILE( *fill->prod ) = fill->cached_prod = fill_prod + (uint)fr_depth;
1395 :
1396 0 : return frame_off;
1397 0 : }
1398 :
1399 : /* FIXME source MAC address from netlnk tile instead */
1400 :
1401 : static void
1402 : interface_addrs( const char * interface,
1403 : uchar * mac,
1404 0 : uint * ip4_addr ) {
1405 0 : int fd = socket( AF_INET, SOCK_DGRAM, 0 );
1406 0 : struct ifreq ifr;
1407 0 : ifr.ifr_addr.sa_family = AF_INET;
1408 :
1409 0 : strncpy( ifr.ifr_name, interface, IFNAMSIZ );
1410 0 : if( FD_UNLIKELY( ioctl( fd, SIOCGIFHWADDR, &ifr ) ) )
1411 0 : FD_LOG_ERR(( "could not get MAC address of interface `%s`: (%i-%s)", interface, errno, fd_io_strerror( errno ) ));
1412 0 : fd_memcpy( mac, ifr.ifr_hwaddr.sa_data, 6 );
1413 :
1414 0 : if( FD_UNLIKELY( ioctl( fd, SIOCGIFADDR, &ifr ) ) )
1415 0 : FD_LOG_ERR(( "could not get IP address of interface `%s`: (%i-%s)", interface, errno, fd_io_strerror( errno ) ));
1416 0 : *ip4_addr = ((struct sockaddr_in *)fd_type_pun( &ifr.ifr_addr ))->sin_addr.s_addr;
1417 :
1418 0 : if( FD_UNLIKELY( close(fd) ) )
1419 0 : FD_LOG_ERR(( "could not close socket (%i-%s)", errno, fd_io_strerror( errno ) ));
1420 0 : }
1421 :
1422 : /* privileged_init does the following initialization steps:
1423 :
1424 : - Create an AF_XDP socket
1425 : - Map XDP metadata rings
1426 : - Register UMEM data region with socket
1427 : - Insert AF_XDP socket into xsk_map
1428 :
1429 : Net tile 0 also runs fd_xdp_install and repeats the above step for
1430 : the loopback device. (Unless the main interface is already loopback)
1431 :
1432 : Kernel object references:
1433 :
1434 : BPF_LINK file descriptor
1435 : |
1436 : +-> XDP program installation on NIC
1437 : | |
1438 : | +-> XDP program <-- BPF_PROG file descriptor (prog_fd)
1439 : |
1440 : +-> XSKMAP object <-- BPF_MAP file descriptor (xsk_map) */
1441 :
1442 : FD_FN_UNUSED static void
1443 : privileged_init( fd_topo_t const * topo,
1444 0 : fd_topo_tile_t const * tile ) {
1445 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
1446 :
1447 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
1448 0 : fd_net_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_net_ctx_t), sizeof(fd_net_ctx_t) );
1449 0 : ulong * free_tx = FD_SCRATCH_ALLOC_APPEND( l, alignof(ulong), tile->xdp.free_ring_depth * sizeof(ulong) );;
1450 :
1451 0 : fd_memset( ctx, 0, sizeof(fd_net_ctx_t) );
1452 :
1453 0 : interface_addrs( tile->xdp.if_virt, ctx->src_mac_addr, &ctx->default_address );
1454 0 : ctx->if_virt = if_nametoindex( tile->xdp.if_virt ); FD_TEST( ctx->if_virt );
1455 :
1456 : /* Load up dcache containing UMEM */
1457 :
1458 0 : void * const dcache_mem = fd_topo_obj_laddr( topo, tile->net.umem_dcache_obj_id );
1459 0 : void * const umem = fd_dcache_join( dcache_mem );
1460 0 : ulong const umem_dcache_data_sz = fd_dcache_data_sz( umem );
1461 0 : ulong const umem_frame_sz = 2048UL;
1462 0 : ulong const umem_sz = fd_ulong_align_dn( umem_dcache_data_sz, umem_frame_sz );
1463 :
1464 : /* Derive chunk bounds */
1465 :
1466 0 : void * const umem_base = fd_wksp_containing( dcache_mem );
1467 0 : ulong const umem_chunk0 = ( (ulong)umem - (ulong)umem_base )>>FD_CHUNK_LG_SZ;
1468 0 : ulong const umem_wmark = umem_chunk0 + ( ( umem_sz-umem_frame_sz )>>FD_CHUNK_LG_SZ );
1469 :
1470 0 : if( FD_UNLIKELY( umem_chunk0>UINT_MAX || umem_wmark>UINT_MAX || umem_chunk0>umem_wmark ) ) {
1471 0 : FD_LOG_ERR(( "Calculated invalid UMEM bounds [%lu,%lu]", umem_chunk0, umem_wmark ));
1472 0 : }
1473 :
1474 0 : if( FD_UNLIKELY( !umem_base ) ) FD_LOG_ERR(( "UMEM dcache is not in a workspace" ));
1475 :
1476 0 : ctx->umem = umem;
1477 0 : ctx->umem_sz = umem_sz;
1478 0 : ctx->umem_chunk0 = (uint)umem_chunk0;
1479 0 : ctx->umem_wmark = (uint)umem_wmark;
1480 :
1481 0 : ctx->free_tx.queue = free_tx;
1482 0 : ctx->free_tx.depth = tile->xdp.xdp_tx_queue_size;
1483 :
1484 : /* Create and install XSKs */
1485 :
1486 0 : uint if_phys_if_idx = if_nametoindex( tile->xdp.if_phys );
1487 0 : if( FD_UNLIKELY( !if_phys_if_idx ) ) FD_LOG_ERR(( "if_nametoindex(%s) failed", tile->xdp.if_phys ));
1488 :
1489 0 : fd_xsk_params_t params0 = {
1490 0 : .if_idx = if_phys_if_idx,
1491 0 : .if_queue_id = tile->xdp.if_queue,
1492 :
1493 : /* Some kernels produce EOPNOTSUP errors on sendto calls when
1494 : starting up without either XDP_ZEROCOPY or XDP_COPY
1495 : (e.g. 5.14.0-503.23.1.el9_5 with i40e) */
1496 0 : .bind_flags = tile->xdp.zero_copy ? XDP_ZEROCOPY : XDP_COPY,
1497 :
1498 0 : .prefbusy_time_budget_micros = PREFBUSY_TIME_BUDGET_MICROS,
1499 :
1500 0 : .prefbusy_rx_budget = PREFBUSY_RX_BUDGET,
1501 :
1502 0 : .fr_depth = tile->xdp.xdp_rx_queue_size*2,
1503 0 : .rx_depth = tile->xdp.xdp_rx_queue_size,
1504 0 : .cr_depth = tile->xdp.xdp_tx_queue_size,
1505 0 : .tx_depth = tile->xdp.xdp_tx_queue_size,
1506 :
1507 0 : .umem_addr = umem,
1508 0 : .frame_sz = umem_frame_sz,
1509 0 : .umem_sz = umem_sz,
1510 :
1511 0 : .core_dump = tile->xdp.xsk_core_dump,
1512 0 : };
1513 :
1514 0 : fd_cstr_ncpy( params0.poll_mode, tile->xdp.poll_mode, sizeof(params0.poll_mode) );
1515 :
1516 : /* Re-derive XDP file descriptors */
1517 :
1518 0 : fd_xdp_fds_t xdp_fds[ FD_TOPO_XDP_FDS_MAX ];
1519 0 : uint xdp_fds_cnt = FD_TOPO_XDP_FDS_MAX;
1520 0 : fd_topo_install_xdp( topo, xdp_fds, &xdp_fds_cnt, 0U, /* dry_run */ 1 );
1521 :
1522 0 : int xsk_map_fd = -1;
1523 0 : for( uint i=0U; i<xdp_fds_cnt; i++ ) {
1524 0 : if( xdp_fds[ i ].if_idx==if_phys_if_idx ) {
1525 0 : xsk_map_fd = xdp_fds[ i ].xsk_map_fd;
1526 0 : ctx->prog_link_fds[ 0 ] = xdp_fds[ i ].prog_link_fd;
1527 0 : xdp_fds[ i ].prog_link_fd = -1; /* mark as used */
1528 0 : break;
1529 0 : }
1530 0 : }
1531 0 : FD_TEST( xsk_map_fd>=0 );
1532 :
1533 : /* Init XSK */
1534 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 ));
1535 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 ));
1536 0 : ctx->xsk_cnt = 1;
1537 :
1538 : /* Networking tile at index 0 also binds to loopback (only queue 0 available on lo) */
1539 :
1540 0 : if( FD_UNLIKELY( strcmp( tile->xdp.if_virt, "lo" ) && !tile->kind_id ) ) {
1541 0 : ctx->xsk_cnt = 2;
1542 :
1543 0 : uint lo_idx = if_nametoindex( "lo" );
1544 0 : if( FD_UNLIKELY( !lo_idx ) ) FD_LOG_ERR(( "if_nametoindex(lo) failed" ));
1545 :
1546 0 : int lo_xsk_map_fd = -1;
1547 0 : for( uint i=0U; i<xdp_fds_cnt; i++ ) {
1548 0 : if( xdp_fds[ i ].if_idx==lo_idx ) {
1549 0 : lo_xsk_map_fd = xdp_fds[ i ].xsk_map_fd;
1550 0 : ctx->prog_link_fds[ 1 ] = xdp_fds[ i ].prog_link_fd;
1551 0 : xdp_fds[ i ].prog_link_fd = -1; /* mark as used */
1552 0 : break;
1553 0 : }
1554 0 : }
1555 0 : FD_TEST( lo_xsk_map_fd>=0 );
1556 :
1557 : /* init xsk 1 */
1558 0 : fd_xsk_params_t params1 = params0;
1559 0 : params1.if_idx = lo_idx; /* probably always 1 */
1560 0 : params1.if_queue_id = 0;
1561 0 : params1.bind_flags = 0;
1562 0 : if( FD_UNLIKELY( !fd_xsk_init( &ctx->xsk[ 1 ], ¶ms1 ) ) ) FD_LOG_ERR(( "failed to bind lo_xsk" ));
1563 0 : if( FD_UNLIKELY( !fd_xsk_activate( &ctx->xsk[ 1 ], lo_xsk_map_fd ) ) ) FD_LOG_ERR(( "failed to activate lo_xsk" ));
1564 0 : }
1565 :
1566 : /* Close unused XDP fds */
1567 :
1568 0 : if( FD_UNLIKELY( fd_sandbox_gettid()==fd_sandbox_getpid() ) ) {
1569 : /* Kind of gross.. in single threaded mode we don't want to close the xsk_map_fd
1570 : since it's shared with other net tiles. Just check for that by seeing if we
1571 : are the only thread in the process. */
1572 0 : for( uint i=0U; i<xdp_fds_cnt; i++ ) {
1573 0 : if( -1==close( xdp_fds[ i ].xsk_map_fd ) ) {
1574 0 : FD_LOG_ERR(( "close(%d) failed (%d-%s)", xsk_map_fd, errno, fd_io_strerror( errno ) ));
1575 0 : }
1576 0 : if( xdp_fds[ i ].prog_link_fd>0 &&
1577 0 : -1==close( xdp_fds[ i ].prog_link_fd ) ) {
1578 0 : FD_LOG_ERR(( "close(%d) failed (%d-%s)", xsk_map_fd, errno, fd_io_strerror( errno ) ));
1579 0 : }
1580 0 : }
1581 0 : }
1582 :
1583 0 : double tick_per_ns = fd_tempo_tick_per_ns( NULL );
1584 0 : ctx->xdp_stats_interval_ticks = (long)( FD_XDP_STATS_INTERVAL_NS * tick_per_ns );
1585 :
1586 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
1587 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
1588 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
1589 0 : }
1590 :
1591 : static void
1592 : init_device_table( fd_net_ctx_t * ctx,
1593 : void * netdev_tbl_shm,
1594 3 : void * netdev_tbl_local ) {
1595 3 : FD_TEST( fd_netdev_tbl_join( &ctx->netdev_shared, netdev_tbl_shm ) );
1596 3 : FD_TEST( fd_netdev_tbl_new( netdev_tbl_local, NETDEV_MAX, BOND_MASTER_MAX ) );
1597 3 : FD_TEST( fd_netdev_tbl_join( &ctx->netdev_tbl, netdev_tbl_local ) );
1598 3 : }
1599 :
1600 : FD_FN_UNUSED static void
1601 : unprivileged_init( fd_topo_t const * topo,
1602 0 : fd_topo_tile_t const * tile ) {
1603 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
1604 :
1605 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
1606 0 : fd_net_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_net_ctx_t), sizeof(fd_net_ctx_t) );
1607 0 : FD_TEST( ctx->xsk_cnt!=0 );
1608 0 : FD_TEST( ctx->free_tx.queue!=NULL );
1609 0 : (void)FD_SCRATCH_ALLOC_APPEND( l, alignof(ulong), tile->xdp.free_ring_depth * sizeof(ulong) );
1610 0 : void * netdev_tbl_local = FD_SCRATCH_ALLOC_APPEND( l, fd_netdev_tbl_align(), fd_netdev_tbl_footprint( NETDEV_MAX, BOND_MASTER_MAX ) );
1611 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 ) );
1612 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 ) );
1613 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 ) ) );
1614 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 ) ) );
1615 :
1616 0 : ctx->net_tile_id = (uint)tile->kind_id;
1617 0 : ctx->net_tile_cnt = (uint)fd_topo_tile_name_cnt( topo, tile->name );
1618 :
1619 0 : ctx->bind_address = tile->net.bind_address;
1620 0 : ctx->shred_listen_port = tile->net.shred_listen_port;
1621 0 : ctx->quic_transaction_listen_port = tile->net.quic_transaction_listen_port;
1622 0 : ctx->legacy_transaction_listen_port = tile->net.legacy_transaction_listen_port;
1623 0 : ctx->gossip_listen_port = tile->net.gossip_listen_port;
1624 0 : ctx->repair_client_listen_port = tile->net.repair_client_listen_port;
1625 0 : ctx->repair_serve_listen_port = tile->net.repair_serve_listen_port;
1626 0 : ctx->txsend_src_port = tile->net.txsend_src_port;
1627 :
1628 : /* Put a bound on chunks we read from the input, to make sure they
1629 : are within in the data region of the workspace. */
1630 :
1631 0 : if( FD_UNLIKELY( !tile->in_cnt ) ) FD_LOG_ERR(( "net tile in link cnt is zero" ));
1632 0 : if( FD_UNLIKELY( tile->in_cnt>MAX_NET_INS ) ) FD_LOG_ERR(( "net tile in link cnt %lu exceeds MAX_NET_INS %lu", tile->in_cnt, MAX_NET_INS ));
1633 0 : FD_TEST( tile->in_cnt<=32 );
1634 0 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
1635 0 : fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
1636 0 : if( !strcmp( link->name, "iproute_out" ) ) ctx->in_kind[i] = IN_KIND_IPROUTE;
1637 0 : else {
1638 0 : ctx->in_kind[i] = IN_KIND_NET;
1639 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 ));
1640 0 : }
1641 :
1642 0 : ctx->in[ i ].mem = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
1643 0 : ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
1644 0 : ctx->in[ i ].wmark = fd_dcache_compact_wmark( ctx->in[ i ].mem, link->dcache, link->mtu );
1645 0 : }
1646 :
1647 0 : ctx->rserve_enabled = 0;
1648 0 : for( ulong i = 0; i < tile->out_cnt; i++ ) {
1649 0 : fd_topo_link_t const * out_link = &topo->links[ tile->out_link_id[ i ] ];
1650 0 : if( strcmp( out_link->name, "net_quic" ) == 0 ) {
1651 0 : fd_topo_link_t const * quic_out = out_link;
1652 0 : ctx->quic_out->mcache = quic_out->mcache;
1653 0 : ctx->quic_out->sync = fd_mcache_seq_laddr( ctx->quic_out->mcache );
1654 0 : ctx->quic_out->depth = fd_mcache_depth( ctx->quic_out->mcache );
1655 0 : ctx->quic_out->seq = fd_mcache_seq_query( ctx->quic_out->sync );
1656 0 : } else if( strcmp( out_link->name, "net_shred" ) == 0 ) {
1657 0 : fd_topo_link_t const * shred_out = out_link;
1658 0 : ctx->shred_out->mcache = shred_out->mcache;
1659 0 : ctx->shred_out->sync = fd_mcache_seq_laddr( ctx->shred_out->mcache );
1660 0 : ctx->shred_out->depth = fd_mcache_depth( ctx->shred_out->mcache );
1661 0 : ctx->shred_out->seq = fd_mcache_seq_query( ctx->shred_out->sync );
1662 0 : } else if( strcmp( out_link->name, "net_gossvf" ) == 0 ) {
1663 0 : fd_topo_link_t const * gossip_out = out_link;
1664 0 : ctx->gossvf_out->mcache = gossip_out->mcache;
1665 0 : ctx->gossvf_out->sync = fd_mcache_seq_laddr( ctx->gossvf_out->mcache );
1666 0 : ctx->gossvf_out->depth = fd_mcache_depth( ctx->gossvf_out->mcache );
1667 0 : ctx->gossvf_out->seq = fd_mcache_seq_query( ctx->gossvf_out->sync );
1668 0 : } else if( strcmp( out_link->name, "net_repair" ) == 0 ) {
1669 0 : fd_topo_link_t const * repair_out = out_link;
1670 0 : ctx->repair_out->mcache = repair_out->mcache;
1671 0 : ctx->repair_out->sync = fd_mcache_seq_laddr( ctx->repair_out->mcache );
1672 0 : ctx->repair_out->depth = fd_mcache_depth( ctx->repair_out->mcache );
1673 0 : ctx->repair_out->seq = fd_mcache_seq_query( ctx->repair_out->sync );
1674 0 : } else if( strcmp( out_link->name, "net_netlnk" ) == 0 ) {
1675 0 : fd_topo_link_t const * netlink_out = out_link;
1676 0 : ctx->neigh4_solicit->mcache = netlink_out->mcache;
1677 0 : ctx->neigh4_solicit->depth = fd_mcache_depth( ctx->neigh4_solicit->mcache );
1678 0 : ctx->neigh4_solicit->seq = fd_mcache_seq_query( fd_mcache_seq_laddr( ctx->neigh4_solicit->mcache ) );
1679 0 : } else if( strcmp( out_link->name, "net_txsend" ) == 0 ) {
1680 0 : fd_topo_link_t const * txsend_out = out_link;
1681 0 : ctx->txsend_out->mcache = txsend_out->mcache;
1682 0 : ctx->txsend_out->sync = fd_mcache_seq_laddr( ctx->txsend_out->mcache );
1683 0 : ctx->txsend_out->depth = fd_mcache_depth( ctx->txsend_out->mcache );
1684 0 : ctx->txsend_out->seq = fd_mcache_seq_query( ctx->txsend_out->sync );
1685 0 : } else if( strcmp( out_link->name, "net_rserve" ) == 0 ) {
1686 0 : fd_topo_link_t const * rserve_out = out_link;
1687 0 : ctx->rserve_out->mcache = rserve_out->mcache;
1688 0 : ctx->rserve_out->sync = fd_mcache_seq_laddr( ctx->rserve_out->mcache );
1689 0 : ctx->rserve_out->depth = fd_mcache_depth( ctx->rserve_out->mcache );
1690 0 : ctx->rserve_out->seq = fd_mcache_seq_query( ctx->rserve_out->sync );
1691 0 : ctx->rserve_enabled = 1;
1692 0 : } else {
1693 0 : FD_LOG_ERR(( "unrecognized out link `%s`", out_link->name ));
1694 0 : }
1695 0 : }
1696 :
1697 : /* Check if any of the tiles we set a listen port for do not have an outlink. */
1698 0 : if( FD_UNLIKELY( ctx->shred_listen_port!=0 && ctx->shred_out->mcache==NULL ) ) {
1699 0 : FD_LOG_ERR(( "shred listen port set but no out link was found" ));
1700 0 : } else if( FD_UNLIKELY( ctx->quic_transaction_listen_port!=0 && ctx->quic_out->mcache==NULL ) ) {
1701 0 : FD_LOG_ERR(( "quic transaction listen port set but no out link was found" ));
1702 0 : } else if( FD_UNLIKELY( ctx->legacy_transaction_listen_port!=0 && ctx->quic_out->mcache==NULL ) ) {
1703 0 : FD_LOG_ERR(( "legacy transaction listen port set but no out link was found" ));
1704 0 : } else if( FD_UNLIKELY( ctx->gossip_listen_port!=0 && ctx->gossvf_out->mcache==NULL ) ) {
1705 0 : FD_LOG_ERR(( "gossip listen port set but no out link was found" ));
1706 0 : } else if( FD_UNLIKELY( ctx->repair_client_listen_port!=0 && ctx->repair_out->mcache==NULL ) ) {
1707 0 : FD_LOG_ERR(( "repair intake port set but no out link was found" ));
1708 0 : } else if( FD_UNLIKELY( ctx->repair_serve_listen_port!=0 && ctx->repair_out->mcache==NULL ) ) {
1709 0 : FD_LOG_ERR(( "repair serve listen port set but no out link was found" ));
1710 0 : } else if( FD_UNLIKELY( ctx->neigh4_solicit->mcache==NULL ) ) {
1711 0 : FD_LOG_ERR(( "netlink request link not found" ));
1712 0 : } else if( FD_UNLIKELY( ctx->txsend_src_port!=0 && ctx->txsend_out->mcache==NULL ) ) {
1713 0 : FD_LOG_ERR(( "txsend listen port set but no out link was found" ));
1714 0 : }
1715 :
1716 0 : for( uint j=0U; j<2U; j++ ) {
1717 0 : ctx->tx_flusher[ j ].pending_wmark = (ulong)( (double)tile->xdp.xdp_tx_queue_size * 0.7 );
1718 0 : ctx->tx_flusher[ j ].tail_flush_backoff = (long)( (double)tile->xdp.tx_flush_timeout_ns * fd_tempo_tick_per_ns( NULL ) );
1719 0 : ctx->tx_flusher[ j ].next_tail_flush_ticks = LONG_MAX;
1720 :
1721 0 : ctx->tx_flusher[ j ].prefbusy_last_poll_ticks = 0L;
1722 0 : ctx->tx_flusher[ j ].prefbusy_min_interval_ticks = (long)( PREFBUSY_MIN_INTERVAL_NS * fd_tempo_tick_per_ns( NULL ) );
1723 0 : ctx->tx_flusher[ j ].prefbusy_stall_timeout_ticks = (long)( PREFBUSY_STALL_TIMEOUT_NS * fd_tempo_tick_per_ns( NULL ) );
1724 0 : }
1725 :
1726 0 : ulong neigh4_obj_id = tile->xdp.neigh4_obj_id;
1727 0 : ulong ele_max = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "obj.%lu.ele_max", neigh4_obj_id );
1728 0 : ulong probe_max = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "obj.%lu.probe_max", neigh4_obj_id );
1729 0 : ulong seed = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "obj.%lu.seed", neigh4_obj_id );
1730 0 : if( FD_UNLIKELY( (ele_max==ULONG_MAX) | (probe_max==ULONG_MAX) | (seed==ULONG_MAX) ) )
1731 0 : FD_LOG_ERR(( "neigh4 hmap properties not set" ));
1732 0 : if( FD_UNLIKELY( !fd_neigh4_hmap_join(
1733 0 : ctx->neigh4,
1734 0 : fd_topo_obj_laddr( topo, neigh4_obj_id ),
1735 0 : ele_max,
1736 0 : probe_max,
1737 0 : seed ) ) ) {
1738 0 : FD_LOG_ERR(( "fd_neigh4_hmap_join failed" ));
1739 0 : }
1740 :
1741 0 : init_device_table( ctx, fd_topo_obj_laddr( topo, tile->xdp.netdev_tbl_obj_id ), netdev_tbl_local );
1742 :
1743 : /* Initialize TX free ring */
1744 :
1745 0 : ulong const frame_sz = 2048UL;
1746 0 : ulong frame_off = 0UL;
1747 0 : ulong const tx_depth = ctx->free_tx.depth;
1748 0 : for( ulong j=0; j<tx_depth; j++ ) {
1749 0 : ctx->free_tx.queue[ j ] = (ulong)ctx->umem + frame_off;
1750 0 : frame_off += frame_sz;
1751 0 : }
1752 0 : ctx->free_tx.prod = tx_depth;
1753 :
1754 : /* Initialize RX mcache chunks */
1755 :
1756 0 : for( ulong i=0UL; i<(tile->out_cnt); i++ ) {
1757 0 : fd_topo_link_t const * out_link = &topo->links[ tile->out_link_id[ i ] ];
1758 0 : fd_frag_meta_t * mcache = out_link->mcache;
1759 0 : for( ulong j=0UL; j<fd_mcache_depth( mcache ); j++ ) {
1760 0 : mcache[ j ].chunk = (uint)( ctx->umem_chunk0 + (frame_off>>FD_CHUNK_LG_SZ) );
1761 0 : frame_off += frame_sz;
1762 0 : }
1763 0 : }
1764 :
1765 : /* Initialize FILL ring */
1766 :
1767 0 : int _charge_busy = 0;
1768 0 : for( uint j=0U; j<ctx->xsk_cnt; j++ ) {
1769 0 : frame_off = net_xsk_bootstrap( ctx, j, frame_off );
1770 0 : net_rx_wakeup( ctx, &ctx->xsk[ j ], &_charge_busy );
1771 0 : net_tx_wakeup( ctx, &ctx->xsk[ j ], &_charge_busy );
1772 0 : }
1773 :
1774 0 : if( FD_UNLIKELY( frame_off > ctx->umem_sz ) ) {
1775 0 : FD_LOG_ERR(( "UMEM is too small" ));
1776 0 : }
1777 0 : }
1778 :
1779 : FD_FN_UNUSED static ulong
1780 : populate_allowed_seccomp( fd_topo_t const * topo,
1781 : fd_topo_tile_t const * tile,
1782 : ulong out_cnt,
1783 0 : struct sock_filter * out ) {
1784 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
1785 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
1786 0 : fd_net_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_net_ctx_t ), sizeof( fd_net_ctx_t ) );
1787 :
1788 : /* A bit of a hack, if there is no loopback XSK for this tile, we still need to pass
1789 : two "allow" FD arguments to the net policy, so we just make them both the same. */
1790 0 : int allow_fd2 = ctx->xsk_cnt>1UL ? ctx->xsk[ 1 ].xsk_fd : ctx->xsk[ 0 ].xsk_fd;
1791 0 : FD_TEST( ctx->xsk[ 0 ].xsk_fd >= 0 && allow_fd2 >= 0 );
1792 :
1793 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 );
1794 0 : return sock_filter_policy_fd_xdp_tile_instr_cnt;
1795 0 : }
1796 :
1797 : FD_FN_UNUSED static ulong
1798 : populate_allowed_fds( fd_topo_t const * topo,
1799 : fd_topo_tile_t const * tile,
1800 : ulong out_fds_cnt,
1801 0 : int * out_fds ) {
1802 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
1803 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
1804 0 : fd_net_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_net_ctx_t ), sizeof( fd_net_ctx_t ) );
1805 :
1806 0 : if( FD_UNLIKELY( out_fds_cnt<6UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
1807 :
1808 0 : ulong out_cnt = 0UL;
1809 :
1810 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
1811 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
1812 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
1813 :
1814 0 : out_fds[ out_cnt++ ] = ctx->xsk[ 0 ].xsk_fd;
1815 0 : out_fds[ out_cnt++ ] = ctx->prog_link_fds[ 0 ];
1816 0 : if( FD_LIKELY( ctx->xsk_cnt>1UL ) ) out_fds[ out_cnt++ ] = ctx->xsk[ 1 ].xsk_fd;
1817 0 : if( FD_LIKELY( ctx->xsk_cnt>1UL ) ) out_fds[ out_cnt++ ] = ctx->prog_link_fds[ 1 ];
1818 0 : return out_cnt;
1819 0 : }
1820 :
1821 0 : #define STEM_BURST (1UL)
1822 0 : #define STEM_LAZY ((ulong)30e3) /* 30 us */
1823 :
1824 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_net_ctx_t
1825 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_net_ctx_t)
1826 :
1827 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
1828 0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
1829 0 : #define STEM_CALLBACK_BEFORE_CREDIT before_credit
1830 0 : #define STEM_CALLBACK_BEFORE_FRAG before_frag
1831 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
1832 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
1833 :
1834 : #include "../../stem/fd_stem.c"
1835 :
1836 : #ifndef FD_TILE_TEST
1837 : fd_topo_run_tile_t fd_tile_net = {
1838 : .name = "net",
1839 : .populate_allowed_seccomp = populate_allowed_seccomp,
1840 : .populate_allowed_fds = populate_allowed_fds,
1841 : .scratch_align = scratch_align,
1842 : .scratch_footprint = scratch_footprint,
1843 : .privileged_init = privileged_init,
1844 : .unprivileged_init = unprivileged_init,
1845 : .run = stem_run,
1846 : };
1847 : #endif
|