1 // SPDX-License-Identifier: GPL-2.0
5 #include <linux/netdev.h>
6 #include <bpf/bpf_helpers.h>
7 #include <bpf/bpf_endian.h>
8 #include <bpf/bpf_tracing.h>
9 #include <linux/if_ether.h>
11 #include <linux/ipv6.h>
13 #include <linux/in6.h>
14 #include <linux/udp.h>
15 #include <asm-generic/errno-base.h>
17 #include "xdp_features.h"
19 #define ipv6_addr_equal(a, b) ((a).s6_addr32[0] == (b).s6_addr32[0] && \
20 (a).s6_addr32[1] == (b).s6_addr32[1] && \
21 (a).s6_addr32[2] == (b).s6_addr32[2] && \
22 (a).s6_addr32[3] == (b).s6_addr32[3])
27 struct xdp_cpumap_stats {
28 unsigned int redirect;
34 __uint(type, BPF_MAP_TYPE_ARRAY);
37 __uint(max_entries, 1);
41 __uint(type, BPF_MAP_TYPE_ARRAY);
44 __uint(max_entries, 1);
45 } dut_stats SEC(".maps");
48 __uint(type, BPF_MAP_TYPE_CPUMAP);
49 __uint(key_size, sizeof(__u32));
50 __uint(value_size, sizeof(struct bpf_cpumap_val));
51 __uint(max_entries, 1);
52 } cpu_map SEC(".maps");
55 __uint(type, BPF_MAP_TYPE_DEVMAP);
56 __uint(key_size, sizeof(__u32));
57 __uint(value_size, sizeof(struct bpf_devmap_val));
58 __uint(max_entries, 1);
59 } dev_map SEC(".maps");
61 const volatile struct in6_addr tester_addr;
62 const volatile struct in6_addr dut_addr;
64 static __always_inline int
65 xdp_process_echo_packet(struct xdp_md *xdp, bool dut)
67 void *data_end = (void *)(long)xdp->data_end;
68 void *data = (void *)(long)xdp->data;
69 struct ethhdr *eh = data;
74 if (eh + 1 > (struct ethhdr *)data_end)
77 if (eh->h_proto == bpf_htons(ETH_P_IP)) {
78 struct iphdr *ih = (struct iphdr *)(eh + 1);
79 __be32 saddr = dut ? tester_addr.s6_addr32[3]
80 : dut_addr.s6_addr32[3];
81 __be32 daddr = dut ? dut_addr.s6_addr32[3]
82 : tester_addr.s6_addr32[3];
84 ih = (struct iphdr *)(eh + 1);
85 if (ih + 1 > (struct iphdr *)data_end)
88 if (saddr != ih->saddr)
91 if (daddr != ih->daddr)
94 if (ih->protocol != IPPROTO_UDP)
97 uh = (struct udphdr *)(ih + 1);
98 } else if (eh->h_proto == bpf_htons(ETH_P_IPV6)) {
99 struct in6_addr saddr = dut ? tester_addr : dut_addr;
100 struct in6_addr daddr = dut ? dut_addr : tester_addr;
101 struct ipv6hdr *ih6 = (struct ipv6hdr *)(eh + 1);
103 if (ih6 + 1 > (struct ipv6hdr *)data_end)
106 if (!ipv6_addr_equal(saddr, ih6->saddr))
109 if (!ipv6_addr_equal(daddr, ih6->daddr))
112 if (ih6->nexthdr != IPPROTO_UDP)
115 uh = (struct udphdr *)(ih6 + 1);
120 if (uh + 1 > (struct udphdr *)data_end)
123 port = dut ? uh->dest : uh->source;
124 if (port != bpf_htons(DUT_ECHO_PORT))
127 tlv = (struct tlv_hdr *)(uh + 1);
128 if (tlv + 1 > data_end)
131 return bpf_htons(tlv->type) == CMD_ECHO ? 0 : -EINVAL;
134 static __always_inline int
135 xdp_update_stats(struct xdp_md *xdp, bool tx, bool dut)
139 if (xdp_process_echo_packet(xdp, tx))
143 val = bpf_map_lookup_elem(&dut_stats, &key);
145 val = bpf_map_lookup_elem(&stats, &key);
148 __sync_add_and_fetch(val, 1);
156 int xdp_tester_check_tx(struct xdp_md *xdp)
158 xdp_update_stats(xdp, true, false);
164 int xdp_tester_check_rx(struct xdp_md *xdp)
166 xdp_update_stats(xdp, false, false);
174 int xdp_do_pass(struct xdp_md *xdp)
176 xdp_update_stats(xdp, true, true);
182 int xdp_do_drop(struct xdp_md *xdp)
184 if (xdp_update_stats(xdp, true, true))
191 int xdp_do_aborted(struct xdp_md *xdp)
193 if (xdp_process_echo_packet(xdp, true))
200 int xdp_do_tx(struct xdp_md *xdp)
202 void *data = (void *)(long)xdp->data;
203 struct ethhdr *eh = data;
204 __u8 tmp_mac[ETH_ALEN];
206 if (xdp_update_stats(xdp, true, true))
209 __builtin_memcpy(tmp_mac, eh->h_source, ETH_ALEN);
210 __builtin_memcpy(eh->h_source, eh->h_dest, ETH_ALEN);
211 __builtin_memcpy(eh->h_dest, tmp_mac, ETH_ALEN);
217 int xdp_do_redirect(struct xdp_md *xdp)
219 if (xdp_process_echo_packet(xdp, true))
222 return bpf_redirect_map(&cpu_map, 0, 0);
225 SEC("tp_btf/xdp_exception")
226 int BPF_PROG(xdp_exception, const struct net_device *dev,
227 const struct bpf_prog *xdp, __u32 act)
231 val = bpf_map_lookup_elem(&dut_stats, &key);
233 __sync_add_and_fetch(val, 1);
238 SEC("tp_btf/xdp_cpumap_kthread")
239 int BPF_PROG(tp_xdp_cpumap_kthread, int map_id, unsigned int processed,
240 unsigned int drops, int sched, struct xdp_cpumap_stats *xdp_stats)
244 val = bpf_map_lookup_elem(&dut_stats, &key);
246 __sync_add_and_fetch(val, 1);
252 int xdp_do_redirect_cpumap(struct xdp_md *xdp)
254 void *data = (void *)(long)xdp->data;
255 struct ethhdr *eh = data;
256 __u8 tmp_mac[ETH_ALEN];
258 if (xdp_process_echo_packet(xdp, true))
261 __builtin_memcpy(tmp_mac, eh->h_source, ETH_ALEN);
262 __builtin_memcpy(eh->h_source, eh->h_dest, ETH_ALEN);
263 __builtin_memcpy(eh->h_dest, tmp_mac, ETH_ALEN);
265 return bpf_redirect_map(&dev_map, 0, 0);
268 char _license[] SEC("license") = "GPL";