1 // SPDX-License-Identifier: GPL-2.0
4 #include "xdp_metadata.h"
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_endian.h>
9 __uint(type, BPF_MAP_TYPE_XSKMAP);
10 __uint(max_entries, 256);
19 extern int bpf_xdp_metadata_rx_timestamp(const struct xdp_md *ctx,
20 __u64 *timestamp) __ksym;
21 extern int bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx, __u32 *hash,
22 enum xdp_rss_hash_type *rss_type) __ksym;
25 int rx(struct xdp_md *ctx)
27 void *data, *data_meta, *data_end;
28 struct ipv6hdr *ip6h = NULL;
29 struct ethhdr *eth = NULL;
30 struct udphdr *udp = NULL;
31 struct iphdr *iph = NULL;
32 struct xdp_meta *meta;
35 data = (void *)(long)ctx->data;
36 data_end = (void *)(long)ctx->data_end;
38 if (eth + 1 < data_end) {
39 if (eth->h_proto == bpf_htons(ETH_P_IP)) {
40 iph = (void *)(eth + 1);
41 if (iph + 1 < data_end && iph->protocol == IPPROTO_UDP)
42 udp = (void *)(iph + 1);
44 if (eth->h_proto == bpf_htons(ETH_P_IPV6)) {
45 ip6h = (void *)(eth + 1);
46 if (ip6h + 1 < data_end && ip6h->nexthdr == IPPROTO_UDP)
47 udp = (void *)(ip6h + 1);
49 if (udp && udp + 1 > data_end)
54 __sync_add_and_fetch(&pkts_skip, 1);
58 /* Forwarding UDP:9091 to AF_XDP */
59 if (udp->dest != bpf_htons(9091)) {
60 __sync_add_and_fetch(&pkts_skip, 1);
64 err = bpf_xdp_adjust_meta(ctx, -(int)sizeof(struct xdp_meta));
66 __sync_add_and_fetch(&pkts_fail, 1);
70 data = (void *)(long)ctx->data;
71 data_meta = (void *)(long)ctx->data_meta;
74 if (meta + 1 > data) {
75 __sync_add_and_fetch(&pkts_fail, 1);
79 err = bpf_xdp_metadata_rx_timestamp(ctx, &meta->rx_timestamp);
81 meta->xdp_timestamp = bpf_ktime_get_tai_ns();
83 meta->rx_timestamp = 0; /* Used by AF_XDP as not avail signal */
85 err = bpf_xdp_metadata_rx_hash(ctx, &meta->rx_hash, &meta->rx_hash_type);
87 meta->rx_hash_err = err; /* Used by AF_XDP as no hash signal */
89 __sync_add_and_fetch(&pkts_redir, 1);
90 return bpf_redirect_map(&xsk, ctx->rx_queue_index, XDP_PASS);
93 char _license[] SEC("license") = "GPL";