Skip to content

Commit b3812a1

Browse files
DanielTimLeekernel-patches-bot
authored andcommitted
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: Daniel T. Lee <[email protected]>
1 parent a4702c6 commit b3812a1

File tree

3 files changed

+36
-39
lines changed

3 files changed

+36
-39
lines changed

samples/bpf/xdp_monitor_kern.c

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66
#include <uapi/linux/bpf.h>
77
#include <bpf/bpf_helpers.h>
88

9-
struct bpf_map_def SEC("maps") redirect_err_cnt = {
10-
.type = BPF_MAP_TYPE_PERCPU_ARRAY,
11-
.key_size = sizeof(u32),
12-
.value_size = sizeof(u64),
13-
.max_entries = 2,
9+
struct {
10+
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
11+
__type(key, u32);
12+
__type(value, u64);
13+
__uint(max_entries, 2);
1414
/* TODO: have entries for all possible errno's */
15-
};
15+
} redirect_err_cnt SEC(".maps");
1616

1717
#define XDP_UNKNOWN XDP_REDIRECT + 1
18-
struct bpf_map_def SEC("maps") exception_cnt = {
19-
.type = BPF_MAP_TYPE_PERCPU_ARRAY,
20-
.key_size = sizeof(u32),
21-
.value_size = sizeof(u64),
22-
.max_entries = XDP_UNKNOWN + 1,
23-
};
18+
struct {
19+
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
20+
__type(key, u32);
21+
__type(value, u64);
22+
__uint(max_entries, XDP_UNKNOWN + 1);
23+
} exception_cnt SEC(".maps");
2424

2525
/* Tracepoint format: /sys/kernel/debug/tracing/events/xdp/xdp_redirect/format
2626
* Code in: kernel/include/trace/events/xdp.h
@@ -129,19 +129,19 @@ struct datarec {
129129
};
130130
#define MAX_CPUS 64
131131

132-
struct bpf_map_def SEC("maps") cpumap_enqueue_cnt = {
133-
.type = BPF_MAP_TYPE_PERCPU_ARRAY,
134-
.key_size = sizeof(u32),
135-
.value_size = sizeof(struct datarec),
136-
.max_entries = MAX_CPUS,
137-
};
132+
struct {
133+
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
134+
__type(key, u32);
135+
__type(value, struct datarec);
136+
__uint(max_entries, MAX_CPUS);
137+
} cpumap_enqueue_cnt SEC(".maps");
138138

139-
struct bpf_map_def SEC("maps") cpumap_kthread_cnt = {
140-
.type = BPF_MAP_TYPE_PERCPU_ARRAY,
141-
.key_size = sizeof(u32),
142-
.value_size = sizeof(struct datarec),
143-
.max_entries = 1,
144-
};
139+
struct {
140+
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
141+
__type(key, u32);
142+
__type(value, struct datarec);
143+
__uint(max_entries, 1);
144+
} cpumap_kthread_cnt SEC(".maps");
145145

146146
/* Tracepoint: /sys/kernel/debug/tracing/events/xdp/xdp_cpumap_enqueue/format
147147
* Code in: kernel/include/trace/events/xdp.h
@@ -210,12 +210,12 @@ int trace_xdp_cpumap_kthread(struct cpumap_kthread_ctx *ctx)
210210
return 0;
211211
}
212212

213-
struct bpf_map_def SEC("maps") devmap_xmit_cnt = {
214-
.type = BPF_MAP_TYPE_PERCPU_ARRAY,
215-
.key_size = sizeof(u32),
216-
.value_size = sizeof(struct datarec),
217-
.max_entries = 1,
218-
};
213+
struct {
214+
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
215+
__type(key, u32);
216+
__type(value, struct datarec);
217+
__uint(max_entries, 1);
218+
} devmap_xmit_cnt SEC(".maps");
219219

220220
/* Tracepoint: /sys/kernel/debug/tracing/events/xdp/xdp_devmap_xmit/format
221221
* Code in: kernel/include/trace/events/xdp.h

samples/bpf/xdp_sample_pkts_kern.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
#include <bpf/bpf_helpers.h>
66

77
#define SAMPLE_SIZE 64ul
8-
#define MAX_CPUS 128
9-
10-
struct bpf_map_def SEC("maps") my_map = {
11-
.type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
12-
.key_size = sizeof(int),
13-
.value_size = sizeof(u32),
14-
.max_entries = MAX_CPUS,
15-
};
8+
9+
struct {
10+
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
11+
__uint(key_size, sizeof(int));
12+
__uint(value_size, sizeof(u32));
13+
} my_map SEC(".maps");
1614

1715
SEC("xdp_sample")
1816
int xdp_sample_prog(struct xdp_md *ctx)

samples/bpf/xdp_sample_pkts_user.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
#include "perf-sys.h"
2020

21-
#define MAX_CPUS 128
2221
static int if_idx;
2322
static char *if_name;
2423
static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;

0 commit comments

Comments
 (0)