aboutsummaryrefslogtreecommitdiff
path: root/samples/bpf/xdp_redirect.bpf.c
diff options
context:
space:
mode:
authorGravatar Kumar Kartikeya Dwivedi <memxor@gmail.com> 2021-08-21 05:50:03 +0530
committerGravatar Alexei Starovoitov <ast@kernel.org> 2021-08-24 14:48:42 -0700
commit66fc4ca85d910bdeecf019c3999bc2df7c80b726 (patch)
tree876f438ed6c8027ff9dee479f886e3d89ebeecec /samples/bpf/xdp_redirect.bpf.c
parentsamples: bpf: Convert xdp_monitor to XDP samples helper (diff)
downloadlinux-66fc4ca85d910bdeecf019c3999bc2df7c80b726.tar.gz
linux-66fc4ca85d910bdeecf019c3999bc2df7c80b726.tar.bz2
linux-66fc4ca85d910bdeecf019c3999bc2df7c80b726.zip
samples: bpf: Convert xdp_redirect_kern.o to XDP samples helper
We moved swap_src_dst_mac to xdp_sample.bpf.h to be shared with other potential users, so drop it while moving code to the new file. Also, consistently use SEC("xdp") naming instead. Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20210821002010.845777-16-memxor@gmail.com
Diffstat (limited to 'samples/bpf/xdp_redirect.bpf.c')
-rw-r--r--samples/bpf/xdp_redirect.bpf.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/samples/bpf/xdp_redirect.bpf.c b/samples/bpf/xdp_redirect.bpf.c
new file mode 100644
index 000000000000..7c02bacfe96b
--- /dev/null
+++ b/samples/bpf/xdp_redirect.bpf.c
@@ -0,0 +1,49 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2016 John Fastabend <john.r.fastabend@intel.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ */
+#include "vmlinux.h"
+#include "xdp_sample.bpf.h"
+#include "xdp_sample_shared.h"
+
+const volatile int ifindex_out;
+
+SEC("xdp")
+int xdp_redirect_prog(struct xdp_md *ctx)
+{
+ void *data_end = (void *)(long)ctx->data_end;
+ void *data = (void *)(long)ctx->data;
+ u32 key = bpf_get_smp_processor_id();
+ struct ethhdr *eth = data;
+ struct datarec *rec;
+ u64 nh_off;
+
+ nh_off = sizeof(*eth);
+ if (data + nh_off > data_end)
+ return XDP_DROP;
+
+ rec = bpf_map_lookup_elem(&rx_cnt, &key);
+ if (!rec)
+ return XDP_PASS;
+ NO_TEAR_INC(rec->processed);
+
+ swap_src_dst_mac(data);
+ return bpf_redirect(ifindex_out, 0);
+}
+
+/* Redirect require an XDP bpf_prog loaded on the TX device */
+SEC("xdp")
+int xdp_redirect_dummy_prog(struct xdp_md *ctx)
+{
+ return XDP_PASS;
+}
+
+char _license[] SEC("license") = "GPL";