summaryrefslogtreecommitdiff
path: root/samples/bpf/xdpsock_kern.c
diff options
context:
space:
mode:
authorMagnus Karlsson <magnus.karlsson@intel.com>2019-11-07 20:47:37 +0300
committerAlexei Starovoitov <ast@kernel.org>2019-11-11 06:30:45 +0300
commit2e5d72c15f0dc713c203464c5c76eb4ec285f598 (patch)
treef094baeb232a60c362d051de71b827a7d2309bdb /samples/bpf/xdpsock_kern.c
parentcbf07409d0c2afad7bb54be039490bffad8bc737 (diff)
downloadlinux-2e5d72c15f0dc713c203464c5c76eb4ec285f598.tar.xz
samples/bpf: Add XDP_SHARED_UMEM support to xdpsock
Add support for the XDP_SHARED_UMEM mode to the xdpsock sample application. As libbpf does not have a built in XDP program for this mode, we use an explicitly loaded XDP program. This also serves as an example on how to write your own XDP program that can route to an AF_XDP socket. Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Tested-by: William Tu <u9012063@gmail.com> Acked-by: Jonathan Lemon <jonathan.lemon@gmail.com> Link: https://lore.kernel.org/bpf/1573148860-30254-3-git-send-email-magnus.karlsson@intel.com
Diffstat (limited to 'samples/bpf/xdpsock_kern.c')
-rw-r--r--samples/bpf/xdpsock_kern.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/samples/bpf/xdpsock_kern.c b/samples/bpf/xdpsock_kern.c
new file mode 100644
index 000000000000..a06177c262cd
--- /dev/null
+++ b/samples/bpf/xdpsock_kern.c
@@ -0,0 +1,24 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/bpf.h>
+#include "bpf_helpers.h"
+#include "xdpsock.h"
+
+/* This XDP program is only needed for the XDP_SHARED_UMEM mode.
+ * If you do not use this mode, libbpf can supply an XDP program for you.
+ */
+
+struct {
+ __uint(type, BPF_MAP_TYPE_XSKMAP);
+ __uint(max_entries, MAX_SOCKS);
+ __uint(key_size, sizeof(int));
+ __uint(value_size, sizeof(int));
+} xsks_map SEC(".maps");
+
+static unsigned int rr;
+
+SEC("xdp_sock") int xdp_sock_prog(struct xdp_md *ctx)
+{
+ rr = (rr + 1) & (MAX_SOCKS - 1);
+
+ return bpf_redirect_map(&xsks_map, rr, XDP_DROP);
+}