Line data Source code
1 : #ifndef HEADER_fd_src_disco_net_fd_net_tile_private_h
2 : #define HEADER_fd_src_disco_net_fd_net_tile_private_h
3 :
4 : /* fd_net_tile_private.h defines private APIs used by network tiles. */
5 :
6 : #include <net/if.h>
7 : #include <linux/if_arp.h>
8 :
9 : #include "../metrics/fd_metrics.h"
10 : #include "../stem/fd_stem.h"
11 : #include "../topo/fd_topo.h"
12 : #include "../../waltz/mib/fd_netdev_tbl.h"
13 : #include "../../discof/repair/fd_repair.h"
14 :
15 : #include "../../waltz/ip/fd_iproute.h"
16 : #include "../../util/net/fd_eth.h"
17 : #include "../../util/net/fd_gre.h"
18 : #include "../../util/net/fd_ip4.h"
19 : #include "../../util/net/fd_udp.h"
20 :
21 0 : #define FD_NET_IN_KIND_TX (0U)
22 0 : #define FD_NET_IN_KIND_IPROUTE (1U)
23 :
24 : /* FD_NET_IN_MAX is the max number of input links per net tile */
25 : #define FD_NET_IN_MAX (32UL)
26 :
27 : /* FD_NET_GRE_MAX is the max number of GRE tunnels the net tile will
28 : monitor. If a packet comes in with a source IP that doesn't match
29 : the endpoint of one of the first FD_NET_GRE_MAX tunnels (in the order
30 : the OS enumerates them), it will be dropped. This is limited for
31 : performance reasons. */
32 0 : #define FD_NET_GRE_MAX (4UL)
33 :
34 : /* FD_NET_RX_PORT_MAX is the max number of UDP ports which the network
35 : stack can receive on. */
36 : #define FD_NET_RX_PORT_MAX (64UL)
37 :
38 : /* fd_net_tx_route_t stores the resolved TX route. IP addresses are in
39 : network byte order. */
40 : struct fd_net_tx_route {
41 : uchar mac_addrs[12];
42 : ushort mtu;
43 : uint src_ip;
44 : uint if_idx;
45 : uint use_loopback;
46 : uint use_gre;
47 : uint gre_outer_src_ip;
48 : uint gre_outer_dst_ip;
49 : };
50 : typedef struct fd_net_tx_route fd_net_tx_route_t;
51 :
52 : /* fd_net_in_dcache_ctx_t stores info needed to validate and locate a frag
53 : in an input link's dcache. */
54 : typedef struct {
55 : void * wksp_base;
56 : ulong chunk0; /* Lowest allowed starting chunk */
57 : ulong wmark; /* Highest allowed starting chunk */
58 : } fd_net_in_dcache_ctx_t;
59 :
60 : /* fd_net_tile_t stores provider agnostic private tile state */
61 : struct fd_net_tile {
62 : ulong kind_id;
63 : ulong tile_cnt;
64 :
65 : fd_net_in_dcache_ctx_t in_dcache_ctx[ FD_NET_IN_MAX ];
66 : uchar in_kind[ FD_NET_IN_MAX ];
67 :
68 : /* iproute_msg stages route updates between during_frag and after_frag */
69 : fd_iproute_msg_t iproute_msg;
70 : fd_net_tx_route_t tx_route;
71 :
72 : void * pkt_buf_wksp_base;
73 : ulong pkt_buf_chunk0;
74 : ulong pkt_buf_wmark;
75 : uint bind_address;
76 :
77 : ulong dst_port_cnt;
78 : ushort dst_ports[ FD_NET_RX_PORT_MAX ];
79 : ulong dst_protos[ FD_NET_RX_PORT_MAX ];
80 : ulong dst_out_idx[ FD_NET_RX_PORT_MAX ];
81 : ulong repair_out_idx;
82 : ulong rx_out_cnt;
83 : uint gre_tunnel_ip[ FD_NET_GRE_MAX ];
84 :
85 : struct {
86 : ulong rx_pkt_cnt;
87 : ulong rx_bytes_total;
88 : ulong rx_gre_cnt;
89 : ulong rx_drop_cnt[ FD_METRICS_ENUM_PKT_RX_DROP_REASON_CNT ];
90 :
91 : ulong tx_pkt_cnt;
92 : ulong tx_bytes_total;
93 : ulong tx_drop_cnt[ FD_METRICS_ENUM_PKT_TX_DROP_REASON_CNT ];
94 : ulong tx_gre_cnt;
95 : } metrics;
96 : };
97 : typedef struct fd_net_tile fd_net_tile_t;
98 :
99 : FD_PROTOTYPES_BEGIN
100 :
101 : /* fd_net_gre_tunnels_refresh fills ctx->gre_tunnel_ip with the IP
102 : addresses of (up to) the first FD_NET_GRE_MAX remote GRE tunnel
103 : endpoints. Any remaining entries are set to zero. */
104 : static inline void
105 : fd_net_gre_tunnels_refresh( fd_net_tile_t * ctx,
106 0 : fd_netdev_tbl_join_t const * netdev_tbl ) {
107 0 : fd_netdev_t const * dev_tbl = netdev_tbl->dev_tbl;
108 0 : ushort dev_cnt = netdev_tbl->hdr->dev_cnt;
109 :
110 0 : fd_memset( ctx->gre_tunnel_ip, 0, sizeof(ctx->gre_tunnel_ip) );
111 :
112 0 : ulong gre_tunnel_cnt = 0UL;
113 0 : for( ushort if_idx=0U; if_idx<dev_cnt && gre_tunnel_cnt<FD_NET_GRE_MAX; if_idx++ ) {
114 0 : fd_netdev_t const * netdev = dev_tbl+if_idx;
115 0 : if( netdev->dev_type==ARPHRD_IPGRE && netdev->gre_dst_ip ) {
116 0 : ctx->gre_tunnel_ip[ gre_tunnel_cnt++ ] = netdev->gre_dst_ip;
117 0 : }
118 0 : }
119 0 : }
120 :
121 : /* fd_net_in_frag_validate checks input dcache bounds and TX packet length. */
122 : static inline void
123 : fd_net_in_frag_validate( fd_net_tile_t const * ctx,
124 : ulong in_idx,
125 : ulong chunk,
126 0 : ulong frame_sz ) {
127 0 : fd_net_in_dcache_ctx_t const * input_ctx = &ctx->in_dcache_ctx[ in_idx ];
128 0 : if( FD_UNLIKELY( chunk<input_ctx->chunk0 ||
129 0 : chunk>input_ctx->wmark ||
130 0 : frame_sz>FD_NET_MTU ) ) {
131 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]",
132 0 : chunk, frame_sz, input_ctx->chunk0, input_ctx->wmark ));
133 0 : }
134 :
135 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==FD_NET_IN_KIND_IPROUTE ) ) return;
136 0 : if( FD_UNLIKELY( frame_sz<sizeof(fd_eth_hdr_t)+sizeof(fd_ip4_hdr_t) ) ) {
137 0 : FD_LOG_ERR(( "packet too small %lu (in_idx=%lu)", frame_sz, in_idx ));
138 0 : }
139 0 : if( FD_UNLIKELY( frame_sz>FD_ETH_PAYLOAD_MAX ) ) {
140 0 : FD_LOG_ERR(( "packet too big %lu (in_idx=%lu)", frame_sz, in_idx ));
141 0 : }
142 0 : }
143 :
144 : /* fd_net_iproute_msg_stage returns 1 if it staged an IPROUTE message, 0 otherwise. */
145 : static inline int
146 : fd_net_iproute_msg_stage( fd_net_tile_t * ctx,
147 : ulong in_idx,
148 : ulong chunk,
149 0 : ulong frame_sz ) {
150 0 : if( ctx->in_kind[ in_idx ]!=FD_NET_IN_KIND_IPROUTE ) return 0;
151 0 : if( FD_UNLIKELY( frame_sz!=sizeof(fd_iproute_msg_t) ) ) {
152 0 : FD_LOG_ERR(( "invalid iproute message size %lu", frame_sz ));
153 0 : }
154 0 : fd_memcpy( &ctx->iproute_msg, fd_chunk_to_laddr_const( ctx->in_dcache_ctx[ in_idx ].wksp_base, chunk ), sizeof(fd_iproute_msg_t) );
155 0 : return 1;
156 0 : }
157 :
158 : /* fd_net_tx_pkt_cpy copies a validated TX frag from an input dcache into
159 : the passed dst buffer, using chunk to locate the src frag. */
160 : static inline void
161 : fd_net_tx_pkt_cpy( fd_net_tile_t const * ctx,
162 : uchar * dst,
163 : ulong chunk,
164 : ulong frame_sz,
165 0 : ulong in_idx ) {
166 0 : uchar const * src = fd_chunk_to_laddr_const( ctx->in_dcache_ctx[ in_idx ].wksp_base, chunk );
167 0 : if( FD_UNLIKELY( ctx->tx_route.use_gre ) ) {
168 0 : ulong const inner_ip_off = sizeof(fd_eth_hdr_t)+sizeof(fd_ip4_hdr_t)+sizeof(fd_gre_hdr_t);
169 0 : fd_memcpy( dst, src, sizeof(fd_eth_hdr_t) );
170 0 : fd_memcpy( dst+inner_ip_off, src+sizeof(fd_eth_hdr_t), frame_sz-sizeof(fd_eth_hdr_t) );
171 0 : } else {
172 0 : fd_memcpy( dst, src, frame_sz );
173 0 : }
174 0 : }
175 :
176 : /* fd_net_tx_ip4_fill fills a missing source address with src_ip and
177 : updates the checksum. An existing source address is left unchanged. */
178 : static inline void
179 : fd_net_tx_ip4_fill( fd_ip4_hdr_t * ip4_hdr,
180 0 : uint src_ip ) {
181 0 : if( ip4_hdr->saddr==0U ) {
182 0 : ip4_hdr->saddr = src_ip;
183 0 : ip4_hdr->check = 0U;
184 0 : ip4_hdr->check = fd_ip4_hdr_check( ip4_hdr );
185 0 : }
186 0 : }
187 :
188 : /* fd_net_tx_pkt_prep updates *frame_sz for GRE encapsulation.
189 : Returns 1 on success or 0 after counting a dropped packet. */
190 : static inline int
191 : fd_net_tx_pkt_prep( fd_net_tile_t * ctx,
192 : uchar * frame,
193 : ulong * frame_sz,
194 0 : ulong in_idx ) {
195 0 : fd_net_tx_route_t const * route = &ctx->tx_route;
196 0 : fd_eth_hdr_t * eth_hdr = (fd_eth_hdr_t *)frame;
197 0 : if( FD_UNLIKELY( eth_hdr->net_type!=fd_ushort_bswap( FD_ETH_HDR_TYPE_IP ) ) ) {
198 0 : FD_LOG_CRIT(( "in link %lu attempted to send packet with invalid ethertype %04x",
199 0 : in_idx, fd_ushort_bswap( eth_hdr->net_type ) ));
200 0 : }
201 :
202 0 : ulong const ip4_off = sizeof(fd_eth_hdr_t) + (route->use_gre ? sizeof(fd_ip4_hdr_t)+sizeof(fd_gre_hdr_t) : 0UL);
203 0 : fd_ip4_hdr_t * ip4_hdr = (fd_ip4_hdr_t *)(frame+ip4_off);
204 0 : ulong const ip4_sz = *frame_sz-sizeof(fd_eth_hdr_t);
205 0 : ulong const ip4_hdr_sz = FD_IP4_GET_LEN( *ip4_hdr );
206 0 : if( FD_UNLIKELY( FD_IP4_GET_VERSION( *ip4_hdr )!=4U ||
207 0 : ip4_hdr_sz<sizeof(fd_ip4_hdr_t) ||
208 0 : ip4_hdr_sz>ip4_sz ) ) {
209 0 : ctx->metrics.tx_drop_cnt[ FD_METRICS_ENUM_PKT_TX_DROP_REASON_V_INVALID_IDX ]++;
210 0 : return 0;
211 0 : }
212 0 : if( FD_UNLIKELY( !ip4_hdr->saddr && !route->src_ip ) ) {
213 0 : ctx->metrics.tx_drop_cnt[ FD_METRICS_ENUM_PKT_TX_DROP_REASON_V_SOURCE_IP_MISSING_IDX ]++;
214 0 : return 0;
215 0 : }
216 :
217 0 : memcpy( eth_hdr->dst, route->mac_addrs, sizeof(eth_hdr->dst)+sizeof(eth_hdr->src) );
218 0 : fd_net_tx_ip4_fill( ip4_hdr, route->src_ip );
219 0 : if( FD_LIKELY( !route->use_gre ) ) return 1;
220 :
221 0 : if( FD_UNLIKELY( fd_ushort_bswap( ip4_hdr->net_tot_len )!=ip4_sz ) ) {
222 0 : ctx->metrics.tx_drop_cnt[ FD_METRICS_ENUM_PKT_TX_DROP_REASON_V_INVALID_IDX ]++;
223 0 : return 0;
224 0 : }
225 0 : if( FD_UNLIKELY( !route->gre_outer_src_ip || !route->gre_outer_dst_ip ) ) {
226 0 : ctx->metrics.tx_drop_cnt[ FD_METRICS_ENUM_PKT_TX_DROP_REASON_V_GRE_NO_ROUTE_IDX ]++;
227 0 : return 0;
228 0 : }
229 :
230 0 : ulong const outer_ip_sz = sizeof(fd_ip4_hdr_t)+sizeof(fd_gre_hdr_t)+ip4_sz;
231 0 : if( FD_UNLIKELY( sizeof(fd_eth_hdr_t)+outer_ip_sz>FD_NET_MTU || (route->mtu && outer_ip_sz>route->mtu) ) ) {
232 0 : ctx->metrics.tx_drop_cnt[ FD_METRICS_ENUM_PKT_TX_DROP_REASON_V_INVALID_IDX ]++;
233 0 : return 0;
234 0 : }
235 :
236 0 : fd_ip4_hdr_t outer_ip4 = {
237 0 : .verihl = FD_IP4_VERIHL( 4,5 ),
238 0 : .net_tot_len = fd_ushort_bswap( (ushort)outer_ip_sz ),
239 0 : .net_frag_off = fd_ushort_bswap( FD_IP4_HDR_FRAG_OFF_DF ),
240 0 : .ttl = 64U,
241 0 : .protocol = FD_IP4_HDR_PROTOCOL_GRE,
242 0 : .daddr = route->gre_outer_dst_ip
243 0 : };
244 0 : fd_net_tx_ip4_fill( &outer_ip4, route->gre_outer_src_ip );
245 0 : FD_STORE( fd_ip4_hdr_t, frame+sizeof(fd_eth_hdr_t), outer_ip4 );
246 :
247 0 : fd_gre_hdr_t const gre_hdr = {
248 0 : .flags_version = FD_GRE_HDR_FLG_VER_BASIC,
249 0 : .protocol = fd_ushort_bswap( FD_ETH_HDR_TYPE_IP )
250 0 : };
251 0 : FD_STORE( fd_gre_hdr_t, frame+sizeof(fd_eth_hdr_t)+sizeof(fd_ip4_hdr_t), gre_hdr );
252 0 : *frame_sz = sizeof(fd_eth_hdr_t)+outer_ip_sz;
253 0 : return 1;
254 0 : }
255 :
256 : /* fd_net_rx_dst_port_add maps an IPv4 UDP destination port to the
257 : output link with the requested name and tile kind. */
258 : static inline void
259 : fd_net_rx_dst_port_add( fd_net_tile_t * ctx,
260 : fd_topo_t const * topo,
261 : fd_topo_tile_t const * tile,
262 : ulong dst_proto,
263 : char const * out_link,
264 : ushort dst_port,
265 0 : int required ) {
266 0 : if( FD_UNLIKELY( !dst_port ) ) return;
267 0 : ulong out_idx = fd_topo_find_tile_out_link( topo, tile, out_link, tile->kind_id );
268 :
269 0 : if( FD_UNLIKELY( out_idx==ULONG_MAX ) ) {
270 0 : if( FD_UNLIKELY( required ) ) {
271 0 : FD_LOG_ERR(( "net output link `%s` is missing for UDP port %hu", out_link, dst_port ));
272 0 : }
273 0 : return;
274 0 : }
275 :
276 0 : if( FD_UNLIKELY( ctx->dst_port_cnt>=FD_NET_RX_PORT_MAX ) ) {
277 0 : FD_LOG_ERR(( "net tile RX port count exceeds max of %lu", FD_NET_RX_PORT_MAX ));
278 0 : }
279 :
280 0 : ctx->dst_protos [ ctx->dst_port_cnt ] = dst_proto;
281 0 : ctx->dst_ports [ ctx->dst_port_cnt ] = dst_port;
282 0 : ctx->dst_out_idx[ ctx->dst_port_cnt ] = out_idx;
283 0 : ctx->dst_port_cnt++;
284 0 : }
285 :
286 : static inline void
287 : fd_net_rx_dst_ports_init( fd_net_tile_t * ctx,
288 : fd_topo_t const * topo,
289 0 : fd_topo_tile_t const * tile ) {
290 0 : ctx->rx_out_cnt = tile->out_cnt;
291 0 : ctx->repair_out_idx = ULONG_MAX;
292 :
293 0 : fd_net_rx_dst_port_add( ctx, topo, tile, DST_PROTO_TPU_UDP, "net_quic", tile->net.legacy_transaction_listen_port, 1 );
294 0 : fd_net_rx_dst_port_add( ctx, topo, tile, DST_PROTO_TPU_QUIC, "net_quic", tile->net.quic_transaction_listen_port, 1 );
295 0 : fd_net_rx_dst_port_add( ctx, topo, tile, DST_PROTO_SHRED, "net_shred", tile->net.shred_listen_port, 1 );
296 0 : fd_net_rx_dst_port_add( ctx, topo, tile, DST_PROTO_GOSSIP, "net_gossvf", tile->net.gossip_listen_port, 1 );
297 0 : fd_net_rx_dst_port_add( ctx, topo, tile, DST_PROTO_REPAIR, "net_shred", tile->net.repair_client_listen_port, 1 );
298 0 : fd_net_rx_dst_port_add( ctx, topo, tile, DST_PROTO_RSERVE, "net_rserve", tile->net.repair_serve_listen_port, 0 );
299 0 : fd_net_rx_dst_port_add( ctx, topo, tile, DST_PROTO_VOTOR, "net_votor", tile->net.votor_quic_client_listen_port, 1 );
300 0 : fd_net_rx_dst_port_add( ctx, topo, tile, DST_PROTO_VOTOR, "net_votor", tile->net.votor_quic_server_listen_port, 1 );
301 :
302 0 : if( tile->net.repair_client_listen_port ) {
303 0 : ulong out_idx = fd_topo_find_tile_out_link( topo, tile, "net_repair", tile->kind_id );
304 0 : if( FD_UNLIKELY( out_idx==ULONG_MAX ) ) {
305 0 : FD_LOG_ERR(( "net output link `net_repair` is missing for repair pings" ));
306 0 : }
307 0 : ctx->repair_out_idx = out_idx;
308 0 : }
309 0 : }
310 :
311 : static inline int
312 : fd_net_rx_dst_port_lookup( fd_net_tile_t const * ctx,
313 : ushort net_dport,
314 : ulong payload_sz,
315 : ulong * out_idx,
316 0 : ulong * dst_proto ) {
317 0 : ulong rule_idx = ULONG_MAX;
318 0 : for( ulong i=0UL; i<ctx->dst_port_cnt; i++ ) {
319 0 : if( ctx->dst_ports[ i ]==net_dport ) {
320 0 : rule_idx = i;
321 0 : break;
322 0 : }
323 0 : }
324 0 : if( FD_UNLIKELY( rule_idx==ULONG_MAX ) ) return 0;
325 :
326 0 : *out_idx = ctx->dst_out_idx[ rule_idx ];
327 0 : *dst_proto = ctx->dst_protos [ rule_idx ];
328 0 : if( FD_UNLIKELY( *dst_proto==DST_PROTO_REPAIR && payload_sz<=AG_REPAIR_RESPONSE_MAX_SZ ) ) {
329 0 : if( FD_UNLIKELY( ctx->repair_out_idx==ULONG_MAX ) ) return 0;
330 0 : *out_idx = ctx->repair_out_idx;
331 0 : }
332 0 : return *out_idx<ctx->rx_out_cnt;
333 0 : }
334 :
335 : /* fd_net_rx_pkt validates and publishes an RX packet. It returns whether
336 : publication succeeded and sets freed_chunk to a reusable buffer. */
337 : static inline int
338 : fd_net_rx_pkt( fd_net_tile_t * ctx,
339 : fd_stem_context_t * stem,
340 : ulong chunk, /* offset high bits */
341 : ulong ctl, /* offset low bits */
342 : ulong byte_len,
343 : ulong tspub,
344 0 : ulong * freed_chunk ) {
345 0 : *freed_chunk = chunk;
346 :
347 0 : ulong const min_udp_frame_sz = sizeof(fd_eth_hdr_t)+sizeof(fd_ip4_hdr_t)+sizeof(fd_udp_hdr_t);
348 0 : if( FD_UNLIKELY( byte_len<min_udp_frame_sz || byte_len>FD_NET_MTU ) ) {
349 0 : ctx->metrics.rx_drop_cnt[ FD_METRICS_ENUM_PKT_RX_DROP_REASON_V_MALFORMED_IDX ]++;
350 0 : return 0;
351 0 : }
352 :
353 0 : uchar * frame = (uchar *)fd_chunk_to_laddr( ctx->pkt_buf_wksp_base, chunk ) + ctl;
354 0 : fd_eth_hdr_t * eth_hdr = (fd_eth_hdr_t *)frame;
355 0 : if( FD_UNLIKELY( fd_ushort_bswap( eth_hdr->net_type )!=FD_ETH_HDR_TYPE_IP ) ) {
356 0 : ctx->metrics.rx_drop_cnt[ FD_METRICS_ENUM_PKT_RX_DROP_REASON_V_MALFORMED_IDX ]++;
357 0 : return 0;
358 0 : }
359 :
360 0 : fd_ip4_hdr_t * ip4_hdr = (fd_ip4_hdr_t *)(eth_hdr+1);
361 0 : ulong ip4_hdr_sz = FD_IP4_GET_LEN( *ip4_hdr );
362 0 : ulong ip4_total_sz = fd_ushort_bswap( ip4_hdr->net_tot_len );
363 0 : if( FD_UNLIKELY( FD_IP4_GET_VERSION( *ip4_hdr )!=4 || ip4_hdr_sz<sizeof(fd_ip4_hdr_t) ) ) {
364 0 : ctx->metrics.rx_drop_cnt[ FD_METRICS_ENUM_PKT_RX_DROP_REASON_V_MALFORMED_IDX ]++;
365 0 : return 0;
366 0 : }
367 0 : if( FD_UNLIKELY( ip4_total_sz<ip4_hdr_sz ||
368 0 : sizeof(fd_eth_hdr_t)+ip4_total_sz>byte_len ) ) {
369 0 : ctx->metrics.rx_drop_cnt[ FD_METRICS_ENUM_PKT_RX_DROP_REASON_V_MALFORMED_IDX ]++;
370 0 : return 0;
371 0 : }
372 :
373 0 : int is_gre = ip4_hdr->protocol==FD_IP4_HDR_PROTOCOL_GRE;
374 0 : if( FD_UNLIKELY( is_gre ) ) {
375 0 : if( FD_UNLIKELY( !ctx->gre_tunnel_ip[0] ) ) {
376 0 : ctx->metrics.rx_drop_cnt[ FD_METRICS_ENUM_PKT_RX_DROP_REASON_V_GRE_UNCONFIGURED_IDX ]++;
377 0 : return 0;
378 0 : }
379 :
380 0 : int tunnel_found = 0;
381 0 : for( ulong i=0UL; i<FD_NET_GRE_MAX; i++ ) tunnel_found |= ip4_hdr->saddr==ctx->gre_tunnel_ip[ i ];
382 0 : ulong const overhead = ip4_hdr_sz+sizeof(fd_gre_hdr_t);
383 0 : if( FD_UNLIKELY( !tunnel_found || !ip4_hdr->saddr ||
384 0 : overhead+sizeof(fd_ip4_hdr_t)+sizeof(fd_udp_hdr_t)>ip4_total_sz ) ) {
385 0 : ctx->metrics.rx_drop_cnt[ FD_METRICS_ENUM_PKT_RX_DROP_REASON_V_GRE_INVALID_IDX ]++;
386 0 : return 0;
387 0 : }
388 :
389 0 : fd_gre_hdr_t const * gre_hdr = (fd_gre_hdr_t const *)((uchar *)ip4_hdr+ip4_hdr_sz);
390 0 : if( FD_UNLIKELY( gre_hdr->flags_version!=FD_GRE_HDR_FLG_VER_BASIC ||
391 0 : gre_hdr->protocol!=fd_ushort_bswap( FD_ETH_HDR_TYPE_IP ) ) ) {
392 0 : ctx->metrics.rx_drop_cnt[ FD_METRICS_ENUM_PKT_RX_DROP_REASON_V_GRE_INVALID_IDX ]++;
393 0 : return 0;
394 0 : }
395 :
396 0 : frame += overhead;
397 0 : fd_memcpy( frame, eth_hdr, sizeof(fd_eth_hdr_t) );
398 :
399 0 : byte_len -= overhead;
400 0 : ctl += overhead;
401 0 : eth_hdr = (fd_eth_hdr_t *)frame;
402 0 : ip4_hdr = (fd_ip4_hdr_t *)(eth_hdr+1);
403 0 : ip4_hdr_sz = FD_IP4_GET_LEN( *ip4_hdr );
404 0 : ip4_total_sz = fd_ushort_bswap( ip4_hdr->net_tot_len );
405 0 : }
406 :
407 0 : if( FD_UNLIKELY( FD_IP4_GET_VERSION( *ip4_hdr )!=4 ||
408 0 : ip4_hdr->protocol!=FD_IP4_HDR_PROTOCOL_UDP ||
409 0 : ip4_hdr_sz<sizeof(fd_ip4_hdr_t) || ip4_total_sz<ip4_hdr_sz ||
410 0 : sizeof(fd_eth_hdr_t)+ip4_total_sz>byte_len ) ) {
411 0 : ctx->metrics.rx_drop_cnt[ FD_METRICS_ENUM_PKT_RX_DROP_REASON_V_MALFORMED_IDX ]++;
412 0 : return 0;
413 0 : }
414 0 : if( FD_UNLIKELY( ctx->bind_address && ip4_hdr->daddr!=ctx->bind_address ) ) {
415 0 : ctx->metrics.rx_drop_cnt[ FD_METRICS_ENUM_PKT_RX_DROP_REASON_V_DST_IP_MISMATCH_IDX ]++;
416 0 : return 0;
417 0 : }
418 :
419 0 : ulong const udp_off = sizeof(fd_eth_hdr_t)+ip4_hdr_sz;
420 0 : ulong const dgram_off = udp_off+sizeof(fd_udp_hdr_t);
421 0 : if( FD_UNLIKELY( dgram_off>byte_len ) ) {
422 0 : ctx->metrics.rx_drop_cnt[ FD_METRICS_ENUM_PKT_RX_DROP_REASON_V_MALFORMED_IDX ]++;
423 0 : return 0;
424 0 : }
425 :
426 0 : fd_udp_hdr_t const * udp_hdr = (fd_udp_hdr_t const *)((uchar const *)eth_hdr+udp_off);
427 0 : ulong const udp_sz = fd_ushort_bswap( udp_hdr->net_len );
428 0 : if( FD_UNLIKELY( udp_sz<sizeof(fd_udp_hdr_t) || udp_sz>ip4_total_sz-ip4_hdr_sz ||
429 0 : fd_ip4_addr_is_mcast( ip4_hdr->saddr ) ) ) {
430 0 : ctx->metrics.rx_drop_cnt[ FD_METRICS_ENUM_PKT_RX_DROP_REASON_V_MALFORMED_IDX ]++;
431 0 : return 0;
432 0 : }
433 :
434 0 : ushort const dst_port = fd_ushort_bswap( udp_hdr->net_dport );
435 0 : ulong out_idx;
436 0 : ulong dst_proto;
437 0 : if( FD_UNLIKELY( !fd_net_rx_dst_port_lookup( ctx, dst_port, udp_sz-sizeof(fd_udp_hdr_t), &out_idx, &dst_proto ) ) ) {
438 0 : ctx->metrics.rx_drop_cnt[ FD_METRICS_ENUM_PKT_RX_DROP_REASON_V_DST_PORT_UNBOUND_IDX ]++;
439 0 : return 0;
440 0 : }
441 :
442 0 : fd_frag_meta_t * mcache = stem->mcaches[ out_idx ];
443 0 : ulong const depth = stem->depths [ out_idx ];
444 0 : ulong const seq = stem->seqs[ out_idx ];
445 :
446 0 : *freed_chunk = mcache[ fd_mcache_line_idx( seq, depth ) ].chunk;
447 :
448 0 : ushort const src_port = fd_ushort_bswap( udp_hdr->net_sport );
449 0 : ulong const sig = fd_disco_netmux_sig( ip4_hdr->saddr, src_port, ip4_hdr->saddr, dst_proto, dgram_off );
450 0 : ulong const tsorig = 0UL;
451 :
452 0 : fd_stem_publish( stem, out_idx, sig, chunk, byte_len, ctl, tsorig, tspub );
453 :
454 0 : ctx->metrics.rx_gre_cnt += (ulong)is_gre;
455 0 : ctx->metrics.rx_pkt_cnt++;
456 0 : ctx->metrics.rx_bytes_total += byte_len;
457 :
458 0 : return 1;
459 0 : }
460 :
461 : FD_PROTOTYPES_END
462 :
463 : #endif /* HEADER_fd_src_disco_net_fd_net_tile_private_h */
|