From: Jesper Dangaard Brouer <brouer@redhat.com>
To: David Ahern <dsahern@gmail.com>,
bpf@vger.kernel.org, netdev@vger.kernel.org
Cc: Jesper Dangaard Brouer <brouer@redhat.com>,
Daniel Borkmann <borkmann@iogearbox.net>,
Alexei Starovoitov <alexei.starovoitov@gmail.com>,
Andrii Nakryiko <andrii.nakryiko@gmail.com>
Subject: [PATCH bpf-next RFC 3/3] samples/bpf: change xdp_fwd to use new BTF config interface
Date: Fri, 29 May 2020 17:59:50 +0200 [thread overview]
Message-ID: <159076799073.1387573.15478442988219832285.stgit@firesoul> (raw)
In-Reply-To: <159076794319.1387573.8722376887638960093.stgit@firesoul>
This enable BTF for samples/bpf/xdp_fwd program, and demonstrates
how the BPF-developer can defined their own version of bpf_devmap_val.
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
---
samples/bpf/xdp_fwd.h | 24 ++++++++++++++++++++++++
samples/bpf/xdp_fwd_kern.c | 5 +++--
samples/bpf/xdp_fwd_user.c | 9 ++++++++-
3 files changed, 35 insertions(+), 3 deletions(-)
create mode 100644 samples/bpf/xdp_fwd.h
diff --git a/samples/bpf/xdp_fwd.h b/samples/bpf/xdp_fwd.h
new file mode 100644
index 000000000000..8abb2a417117
--- /dev/null
+++ b/samples/bpf/xdp_fwd.h
@@ -0,0 +1,24 @@
+// SPDX-License-Identifier: GPL-2.0
+#ifndef _SAMPLES_BPF_XDP_FWD_H
+#define _SAMPLES_BPF_XDP_FWD_H
+
+#define ENABLE_BPF_PROG 1
+
+/* Notice XDP prog can redefine this struct, which through BTF affect
+ * what kernel-side config options are available.
+ */
+struct bpf_devmap_val {
+ __u32 ifindex; /* device index - mandatory */
+#ifdef ENABLE_BPF_PROG
+ union {
+ int fd; /* prog fd on map write */
+ __u32 id; /* prog id on map read */
+ } bpf_prog;
+#endif
+ struct {
+ unsigned char data2[2];
+ __u16 vlan_hdr;
+ } storage;
+};
+
+#endif /* _SAMPLES_BPF_XDP_FWD_H */
diff --git a/samples/bpf/xdp_fwd_kern.c b/samples/bpf/xdp_fwd_kern.c
index 54c099cbd639..bb1ee44dcd60 100644
--- a/samples/bpf/xdp_fwd_kern.c
+++ b/samples/bpf/xdp_fwd_kern.c
@@ -20,13 +20,14 @@
#include <linux/ipv6.h>
#include <bpf/bpf_helpers.h>
+#include "xdp_fwd.h"
#define IPV6_FLOWINFO_MASK cpu_to_be32(0x0FFFFFFF)
struct {
__uint(type, BPF_MAP_TYPE_DEVMAP);
- __uint(key_size, sizeof(int));
- __uint(value_size, sizeof(int));
+ __type(key, u32);
+ __type(value, struct bpf_devmap_val);
__uint(max_entries, 64);
} xdp_tx_ports SEC(".maps");
diff --git a/samples/bpf/xdp_fwd_user.c b/samples/bpf/xdp_fwd_user.c
index 74a4583d0d86..4ddf70bcedde 100644
--- a/samples/bpf/xdp_fwd_user.c
+++ b/samples/bpf/xdp_fwd_user.c
@@ -26,13 +26,20 @@
#include <bpf/libbpf.h>
#include <bpf/bpf.h>
+#include "xdp_fwd.h"
static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
static int do_attach(int idx, int prog_fd, int map_fd, const char *name)
{
+ struct bpf_devmap_val val = { 0 };
int err;
+ val.ifindex = idx;
+#ifdef ENABLE_BPF_PROG
+ val.bpf_prog.fd = - 1;
+#endif
+
err = bpf_set_link_xdp_fd(idx, prog_fd, xdp_flags);
if (err < 0) {
printf("ERROR: failed to attach program to %s\n", name);
@@ -40,7 +47,7 @@ static int do_attach(int idx, int prog_fd, int map_fd, const char *name)
}
/* Adding ifindex as a possible egress TX port */
- err = bpf_map_update_elem(map_fd, &idx, &idx, 0);
+ err = bpf_map_update_elem(map_fd, &idx, &val, 0);
if (err)
printf("ERROR: failed using device %s as TX-port\n", name);
prev parent reply other threads:[~2020-05-29 15:59 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-05-29 15:59 [PATCH bpf-next RFC 0/3] bpf: dynamic map-value config layout via BTF Jesper Dangaard Brouer
2020-05-29 15:59 ` [PATCH bpf-next RFC 1/3] bpf: move struct bpf_devmap_val out of UAPI Jesper Dangaard Brouer
2020-05-29 16:06 ` David Ahern
2020-05-29 16:28 ` Jesper Dangaard Brouer
2020-05-29 15:59 ` [PATCH bpf-next RFC 2/3] bpf: devmap dynamic map-value storage area based on BTF Jesper Dangaard Brouer
2020-05-29 16:39 ` Toke Høiland-Jørgensen
2020-06-02 8:59 ` Jesper Dangaard Brouer
2020-06-02 9:23 ` Toke Høiland-Jørgensen
2020-06-02 10:01 ` Jesper Dangaard Brouer
2020-05-30 7:19 ` Andrii Nakryiko
2020-05-30 14:36 ` Jesper Dangaard Brouer
2020-06-01 21:30 ` Alexei Starovoitov
2020-06-02 7:00 ` Jesper Dangaard Brouer
2020-06-02 18:27 ` Alexei Starovoitov
2020-06-03 9:11 ` Jesper Dangaard Brouer
2020-06-03 16:20 ` Alexei Starovoitov
2020-05-29 15:59 ` Jesper Dangaard Brouer [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=159076799073.1387573.15478442988219832285.stgit@firesoul \
--to=brouer@redhat.com \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii.nakryiko@gmail.com \
--cc=borkmann@iogearbox.net \
--cc=bpf@vger.kernel.org \
--cc=dsahern@gmail.com \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).