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