Linux 6.9-rc1
[linux-2.6-microblaze.git] / samples / bpf / xdp_redirect.bpf.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2016 John Fastabend <john.r.fastabend@intel.com>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  */
13 #include "vmlinux.h"
14 #include "xdp_sample.bpf.h"
15 #include "xdp_sample_shared.h"
16
17 const volatile int ifindex_out;
18
19 SEC("xdp")
20 int xdp_redirect_prog(struct xdp_md *ctx)
21 {
22         void *data_end = (void *)(long)ctx->data_end;
23         void *data = (void *)(long)ctx->data;
24         u32 key = bpf_get_smp_processor_id();
25         struct ethhdr *eth = data;
26         struct datarec *rec;
27         u64 nh_off;
28
29         nh_off = sizeof(*eth);
30         if (data + nh_off > data_end)
31                 return XDP_DROP;
32
33         rec = bpf_map_lookup_elem(&rx_cnt, &key);
34         if (!rec)
35                 return XDP_PASS;
36         NO_TEAR_INC(rec->processed);
37
38         swap_src_dst_mac(data);
39         return bpf_redirect(ifindex_out, 0);
40 }
41
42 /* Redirect require an XDP bpf_prog loaded on the TX device */
43 SEC("xdp")
44 int xdp_redirect_dummy_prog(struct xdp_md *ctx)
45 {
46         return XDP_PASS;
47 }
48
49 char _license[] SEC("license") = "GPL";