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);
15 extern int bpf_xdp_metadata_rx_timestamp(const struct xdp_md *ctx,
16 __u64 *timestamp) __ksym;
17 extern int bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx,
21 int rx(struct xdp_md *ctx)
23 void *data, *data_meta, *data_end;
24 struct ipv6hdr *ip6h = NULL;
25 struct ethhdr *eth = NULL;
26 struct udphdr *udp = NULL;
27 struct iphdr *iph = NULL;
28 struct xdp_meta *meta;
31 data = (void *)(long)ctx->data;
32 data_end = (void *)(long)ctx->data_end;
34 if (eth + 1 < data_end) {
35 if (eth->h_proto == bpf_htons(ETH_P_IP)) {
36 iph = (void *)(eth + 1);
37 if (iph + 1 < data_end && iph->protocol == IPPROTO_UDP)
38 udp = (void *)(iph + 1);
40 if (eth->h_proto == bpf_htons(ETH_P_IPV6)) {
41 ip6h = (void *)(eth + 1);
42 if (ip6h + 1 < data_end && ip6h->nexthdr == IPPROTO_UDP)
43 udp = (void *)(ip6h + 1);
45 if (udp && udp + 1 > data_end)
52 if (udp->dest != bpf_htons(9091))
55 bpf_printk("forwarding UDP:9091 to AF_XDP");
57 ret = bpf_xdp_adjust_meta(ctx, -(int)sizeof(struct xdp_meta));
59 bpf_printk("bpf_xdp_adjust_meta returned %d", ret);
63 data = (void *)(long)ctx->data;
64 data_meta = (void *)(long)ctx->data_meta;
67 if (meta + 1 > data) {
68 bpf_printk("bpf_xdp_adjust_meta doesn't appear to work");
72 if (!bpf_xdp_metadata_rx_timestamp(ctx, &meta->rx_timestamp))
73 bpf_printk("populated rx_timestamp with %llu", meta->rx_timestamp);
75 meta->rx_timestamp = 0; /* Used by AF_XDP as not avail signal */
77 if (!bpf_xdp_metadata_rx_hash(ctx, &meta->rx_hash))
78 bpf_printk("populated rx_hash with %u", meta->rx_hash);
80 meta->rx_hash = 0; /* Used by AF_XDP as not avail signal */
82 return bpf_redirect_map(&xsk, ctx->rx_queue_index, XDP_PASS);
85 char _license[] SEC("license") = "GPL";