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@xxxxxxxxxx> --- 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);