mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-09-18 00:24:39 +00:00
571fa247ab
Generate vmlinux.h only from the in-tree vmlinux, and remove enum
declarations that would cause a build failure in case of version
mismatches.
There are now two options when building the samples:
1. Compile the kernel to use in-tree vmlinux for vmlinux.h
2. Override VMLINUX_BTF for samples using something like this:
make VMLINUX_BTF=/sys/kernel/btf/vmlinux -C samples/bpf
This change was tested with relative builds, e.g. cases like:
* make O=build -C samples/bpf
* make KBUILD_OUTPUT=build -C samples/bpf
* make -C samples/bpf
* cd samples/bpf && make
When a suitable VMLINUX_BTF is not found, the following message is
printed:
/home/kkd/src/linux/samples/bpf/Makefile:333: *** Cannot find a vmlinux
for VMLINUX_BTF at any of " ./vmlinux", build the kernel or set
VMLINUX_BTF variable. Stop.
Fixes: 384b6b3bbf
(samples: bpf: Add vmlinux.h generation support)
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Toke Høiland-Jørgensen <toke@redhat.com>
Link: https://lore.kernel.org/bpf/20210928054608.1799021-1-memxor@gmail.com
77 lines
1.7 KiB
C
77 lines
1.7 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
#define KBUILD_MODNAME "foo"
|
|
|
|
#include "vmlinux.h"
|
|
#include "xdp_sample.bpf.h"
|
|
#include "xdp_sample_shared.h"
|
|
|
|
struct {
|
|
__uint(type, BPF_MAP_TYPE_DEVMAP_HASH);
|
|
__uint(key_size, sizeof(int));
|
|
__uint(value_size, sizeof(int));
|
|
__uint(max_entries, 32);
|
|
} forward_map_general SEC(".maps");
|
|
|
|
struct {
|
|
__uint(type, BPF_MAP_TYPE_DEVMAP_HASH);
|
|
__uint(key_size, sizeof(int));
|
|
__uint(value_size, sizeof(struct bpf_devmap_val));
|
|
__uint(max_entries, 32);
|
|
} forward_map_native SEC(".maps");
|
|
|
|
/* map to store egress interfaces mac addresses */
|
|
struct {
|
|
__uint(type, BPF_MAP_TYPE_HASH);
|
|
__type(key, u32);
|
|
__type(value, __be64);
|
|
__uint(max_entries, 32);
|
|
} mac_map SEC(".maps");
|
|
|
|
static int xdp_redirect_map(struct xdp_md *ctx, void *forward_map)
|
|
{
|
|
u32 key = bpf_get_smp_processor_id();
|
|
struct datarec *rec;
|
|
|
|
rec = bpf_map_lookup_elem(&rx_cnt, &key);
|
|
if (!rec)
|
|
return XDP_PASS;
|
|
NO_TEAR_INC(rec->processed);
|
|
|
|
return bpf_redirect_map(forward_map, 0,
|
|
BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);
|
|
}
|
|
|
|
SEC("xdp")
|
|
int xdp_redirect_map_general(struct xdp_md *ctx)
|
|
{
|
|
return xdp_redirect_map(ctx, &forward_map_general);
|
|
}
|
|
|
|
SEC("xdp")
|
|
int xdp_redirect_map_native(struct xdp_md *ctx)
|
|
{
|
|
return xdp_redirect_map(ctx, &forward_map_native);
|
|
}
|
|
|
|
SEC("xdp_devmap/egress")
|
|
int xdp_devmap_prog(struct xdp_md *ctx)
|
|
{
|
|
void *data_end = (void *)(long)ctx->data_end;
|
|
void *data = (void *)(long)ctx->data;
|
|
u32 key = ctx->egress_ifindex;
|
|
struct ethhdr *eth = data;
|
|
__be64 *mac;
|
|
u64 nh_off;
|
|
|
|
nh_off = sizeof(*eth);
|
|
if (data + nh_off > data_end)
|
|
return XDP_DROP;
|
|
|
|
mac = bpf_map_lookup_elem(&mac_map, &key);
|
|
if (mac)
|
|
__builtin_memcpy(eth->h_source, mac, ETH_ALEN);
|
|
|
|
return XDP_PASS;
|
|
}
|
|
|
|
char _license[] SEC("license") = "GPL";
|