Commit 321f6324 authored by Daniel T. Lee's avatar Daniel T. Lee Committed by Alexei Starovoitov

samples: bpf: Refactor XDP kern program maps with BTF-defined map

Most of the samples were converted to use the new BTF-defined MAP as
they moved to libbpf, but some of the samples were missing.

Instead of using the previous BPF MAP definition, this commit refactors
xdp_monitor and xdp_sample_pkts_kern MAP definition with the new
BTF-defined MAP format.

Also, this commit removes the max_entries attribute at PERF_EVENT_ARRAY
map type. The libbpf's bpf_object__create_map() will automatically
set max_entries to the maximum configured number of CPUs on the host.
Signed-off-by: default avatarDaniel T. Lee <danieltimlee@gmail.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20201010181734.1109-4-danieltimlee@gmail.com
parent 151936bf
...@@ -6,21 +6,21 @@ ...@@ -6,21 +6,21 @@
#include <uapi/linux/bpf.h> #include <uapi/linux/bpf.h>
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
struct bpf_map_def SEC("maps") redirect_err_cnt = { struct {
.type = BPF_MAP_TYPE_PERCPU_ARRAY, __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
.key_size = sizeof(u32), __type(key, u32);
.value_size = sizeof(u64), __type(value, u64);
.max_entries = 2, __uint(max_entries, 2);
/* TODO: have entries for all possible errno's */ /* TODO: have entries for all possible errno's */
}; } redirect_err_cnt SEC(".maps");
#define XDP_UNKNOWN XDP_REDIRECT + 1 #define XDP_UNKNOWN XDP_REDIRECT + 1
struct bpf_map_def SEC("maps") exception_cnt = { struct {
.type = BPF_MAP_TYPE_PERCPU_ARRAY, __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
.key_size = sizeof(u32), __type(key, u32);
.value_size = sizeof(u64), __type(value, u64);
.max_entries = XDP_UNKNOWN + 1, __uint(max_entries, XDP_UNKNOWN + 1);
}; } exception_cnt SEC(".maps");
/* Tracepoint format: /sys/kernel/debug/tracing/events/xdp/xdp_redirect/format /* Tracepoint format: /sys/kernel/debug/tracing/events/xdp/xdp_redirect/format
* Code in: kernel/include/trace/events/xdp.h * Code in: kernel/include/trace/events/xdp.h
...@@ -129,19 +129,19 @@ struct datarec { ...@@ -129,19 +129,19 @@ struct datarec {
}; };
#define MAX_CPUS 64 #define MAX_CPUS 64
struct bpf_map_def SEC("maps") cpumap_enqueue_cnt = { struct {
.type = BPF_MAP_TYPE_PERCPU_ARRAY, __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
.key_size = sizeof(u32), __type(key, u32);
.value_size = sizeof(struct datarec), __type(value, struct datarec);
.max_entries = MAX_CPUS, __uint(max_entries, MAX_CPUS);
}; } cpumap_enqueue_cnt SEC(".maps");
struct bpf_map_def SEC("maps") cpumap_kthread_cnt = { struct {
.type = BPF_MAP_TYPE_PERCPU_ARRAY, __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
.key_size = sizeof(u32), __type(key, u32);
.value_size = sizeof(struct datarec), __type(value, struct datarec);
.max_entries = 1, __uint(max_entries, 1);
}; } cpumap_kthread_cnt SEC(".maps");
/* Tracepoint: /sys/kernel/debug/tracing/events/xdp/xdp_cpumap_enqueue/format /* Tracepoint: /sys/kernel/debug/tracing/events/xdp/xdp_cpumap_enqueue/format
* Code in: kernel/include/trace/events/xdp.h * Code in: kernel/include/trace/events/xdp.h
...@@ -210,12 +210,12 @@ int trace_xdp_cpumap_kthread(struct cpumap_kthread_ctx *ctx) ...@@ -210,12 +210,12 @@ int trace_xdp_cpumap_kthread(struct cpumap_kthread_ctx *ctx)
return 0; return 0;
} }
struct bpf_map_def SEC("maps") devmap_xmit_cnt = { struct {
.type = BPF_MAP_TYPE_PERCPU_ARRAY, __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
.key_size = sizeof(u32), __type(key, u32);
.value_size = sizeof(struct datarec), __type(value, struct datarec);
.max_entries = 1, __uint(max_entries, 1);
}; } devmap_xmit_cnt SEC(".maps");
/* Tracepoint: /sys/kernel/debug/tracing/events/xdp/xdp_devmap_xmit/format /* Tracepoint: /sys/kernel/debug/tracing/events/xdp/xdp_devmap_xmit/format
* Code in: kernel/include/trace/events/xdp.h * Code in: kernel/include/trace/events/xdp.h
......
...@@ -5,14 +5,12 @@ ...@@ -5,14 +5,12 @@
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
#define SAMPLE_SIZE 64ul #define SAMPLE_SIZE 64ul
#define MAX_CPUS 128
struct {
struct bpf_map_def SEC("maps") my_map = { __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
.type = BPF_MAP_TYPE_PERF_EVENT_ARRAY, __uint(key_size, sizeof(int));
.key_size = sizeof(int), __uint(value_size, sizeof(u32));
.value_size = sizeof(u32), } my_map SEC(".maps");
.max_entries = MAX_CPUS,
};
SEC("xdp_sample") SEC("xdp_sample")
int xdp_sample_prog(struct xdp_md *ctx) int xdp_sample_prog(struct xdp_md *ctx)
......
...@@ -18,7 +18,6 @@ ...@@ -18,7 +18,6 @@
#include "perf-sys.h" #include "perf-sys.h"
#define MAX_CPUS 128
static int if_idx; static int if_idx;
static char *if_name; static char *if_name;
static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST; static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment