Skip to content

Commit 59929cd

Browse files
DanielTimLeeborkmann
authored andcommitted
samples, bpf: Refactor kprobe, tail call kern progs map definition
Because the previous two commit replaced the bpf_load implementation of the user program with libbpf, the corresponding kernel program's MAP definition can be replaced with new BTF-defined map syntax. This commit only updates the samples which uses libbpf API for loading bpf program not with bpf_load. Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Yonghong Song <yhs@fb.com> Link: https://lore.kernel.org/bpf/20200516040608.1377876-6-danieltimlee@gmail.com
1 parent 14846dd commit 59929cd

8 files changed

Lines changed: 93 additions & 91 deletions

File tree

‎samples/bpf/sampleip_kern.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313

1414
#define MAX_IPS 8192
1515

16-
struct bpf_map_def SEC("maps") ip_map = {
17-
.type = BPF_MAP_TYPE_HASH,
18-
.key_size = sizeof(u64),
19-
.value_size = sizeof(u32),
20-
.max_entries = MAX_IPS,
21-
};
16+
struct {
17+
__uint(type, BPF_MAP_TYPE_HASH);
18+
__type(key, u64);
19+
__type(value, u32);
20+
__uint(max_entries, MAX_IPS);
21+
} ip_map SEC(".maps");
2222

2323
SEC("perf_event")
2424
int do_sample(struct bpf_perf_event_data *ctx)

‎samples/bpf/sockex3_kern.c‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919

2020
#define PROG(F) SEC("socket/"__stringify(F)) int bpf_func_##F
2121

22-
struct bpf_map_def SEC("maps") jmp_table = {
23-
.type = BPF_MAP_TYPE_PROG_ARRAY,
24-
.key_size = sizeof(u32),
25-
.value_size = sizeof(u32),
26-
.max_entries = 8,
27-
};
22+
struct {
23+
__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
24+
__uint(key_size, sizeof(u32));
25+
__uint(value_size, sizeof(u32));
26+
__uint(max_entries, 8);
27+
} jmp_table SEC(".maps");
2828

2929
#define PARSE_VLAN 1
3030
#define PARSE_MPLS 2
@@ -92,12 +92,12 @@ struct globals {
9292
struct flow_key_record flow;
9393
};
9494

95-
struct bpf_map_def SEC("maps") percpu_map = {
96-
.type = BPF_MAP_TYPE_ARRAY,
97-
.key_size = sizeof(__u32),
98-
.value_size = sizeof(struct globals),
99-
.max_entries = 32,
100-
};
95+
struct {
96+
__uint(type, BPF_MAP_TYPE_ARRAY);
97+
__type(key, __u32);
98+
__type(value, struct globals);
99+
__uint(max_entries, 32);
100+
} percpu_map SEC(".maps");
101101

102102
/* user poor man's per_cpu until native support is ready */
103103
static struct globals *this_cpu_globals(void)
@@ -113,12 +113,12 @@ struct pair {
113113
__u64 bytes;
114114
};
115115

116-
struct bpf_map_def SEC("maps") hash_map = {
117-
.type = BPF_MAP_TYPE_HASH,
118-
.key_size = sizeof(struct flow_key_record),
119-
.value_size = sizeof(struct pair),
120-
.max_entries = 1024,
121-
};
116+
struct {
117+
__uint(type, BPF_MAP_TYPE_HASH);
118+
__type(key, struct flow_key_record);
119+
__type(value, struct pair);
120+
__uint(max_entries, 1024);
121+
} hash_map SEC(".maps");
122122

123123
static void update_stats(struct __sk_buff *skb, struct globals *g)
124124
{

‎samples/bpf/trace_event_kern.c‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ struct key_t {
1818
u32 userstack;
1919
};
2020

21-
struct bpf_map_def SEC("maps") counts = {
22-
.type = BPF_MAP_TYPE_HASH,
23-
.key_size = sizeof(struct key_t),
24-
.value_size = sizeof(u64),
25-
.max_entries = 10000,
26-
};
21+
struct {
22+
__uint(type, BPF_MAP_TYPE_HASH);
23+
__type(key, struct key_t);
24+
__type(value, u64);
25+
__uint(max_entries, 10000);
26+
} counts SEC(".maps");
2727

28-
struct bpf_map_def SEC("maps") stackmap = {
29-
.type = BPF_MAP_TYPE_STACK_TRACE,
30-
.key_size = sizeof(u32),
31-
.value_size = PERF_MAX_STACK_DEPTH * sizeof(u64),
32-
.max_entries = 10000,
33-
};
28+
struct {
29+
__uint(type, BPF_MAP_TYPE_STACK_TRACE);
30+
__uint(key_size, sizeof(u32));
31+
__uint(value_size, PERF_MAX_STACK_DEPTH * sizeof(u64));
32+
__uint(max_entries, 10000);
33+
} stackmap SEC(".maps");
3434

3535
#define KERN_STACKID_FLAGS (0 | BPF_F_FAST_STACK_CMP)
3636
#define USER_STACKID_FLAGS (0 | BPF_F_FAST_STACK_CMP | BPF_F_USER_STACK)

‎samples/bpf/tracex2_kern.c‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
#include <bpf/bpf_tracing.h>
1313
#include "trace_common.h"
1414

15-
struct bpf_map_def SEC("maps") my_map = {
16-
.type = BPF_MAP_TYPE_HASH,
17-
.key_size = sizeof(long),
18-
.value_size = sizeof(long),
19-
.max_entries = 1024,
20-
};
15+
struct {
16+
__uint(type, BPF_MAP_TYPE_HASH);
17+
__type(key, long);
18+
__type(value, long);
19+
__uint(max_entries, 1024);
20+
} my_map SEC(".maps");
2121

2222
/* kprobe is NOT a stable ABI. If kernel internals change this bpf+kprobe
2323
* example will no longer be meaningful
@@ -71,12 +71,12 @@ struct hist_key {
7171
u64 index;
7272
};
7373

74-
struct bpf_map_def SEC("maps") my_hist_map = {
75-
.type = BPF_MAP_TYPE_PERCPU_HASH,
76-
.key_size = sizeof(struct hist_key),
77-
.value_size = sizeof(long),
78-
.max_entries = 1024,
79-
};
74+
struct {
75+
__uint(type, BPF_MAP_TYPE_PERCPU_HASH);
76+
__uint(key_size, sizeof(struct hist_key));
77+
__uint(value_size, sizeof(long));
78+
__uint(max_entries, 1024);
79+
} my_hist_map SEC(".maps");
8080

8181
SEC("kprobe/" SYSCALL(sys_write))
8282
int bpf_prog3(struct pt_regs *ctx)

‎samples/bpf/tracex3_kern.c‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
#include <bpf/bpf_helpers.h>
1212
#include <bpf/bpf_tracing.h>
1313

14-
struct bpf_map_def SEC("maps") my_map = {
15-
.type = BPF_MAP_TYPE_HASH,
16-
.key_size = sizeof(long),
17-
.value_size = sizeof(u64),
18-
.max_entries = 4096,
19-
};
14+
struct {
15+
__uint(type, BPF_MAP_TYPE_HASH);
16+
__type(key, long);
17+
__type(value, u64);
18+
__uint(max_entries, 4096);
19+
} my_map SEC(".maps");
2020

2121
/* kprobe is NOT a stable ABI. If kernel internals change this bpf+kprobe
2222
* example will no longer be meaningful
@@ -42,12 +42,12 @@ static unsigned int log2l(unsigned long long n)
4242

4343
#define SLOTS 100
4444

45-
struct bpf_map_def SEC("maps") lat_map = {
46-
.type = BPF_MAP_TYPE_PERCPU_ARRAY,
47-
.key_size = sizeof(u32),
48-
.value_size = sizeof(u64),
49-
.max_entries = SLOTS,
50-
};
45+
struct {
46+
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
47+
__uint(key_size, sizeof(u32));
48+
__uint(value_size, sizeof(u64));
49+
__uint(max_entries, SLOTS);
50+
} lat_map SEC(".maps");
5151

5252
SEC("kprobe/blk_account_io_completion")
5353
int bpf_prog2(struct pt_regs *ctx)

‎samples/bpf/tracex4_kern.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ struct pair {
1515
u64 ip;
1616
};
1717

18-
struct bpf_map_def SEC("maps") my_map = {
19-
.type = BPF_MAP_TYPE_HASH,
20-
.key_size = sizeof(long),
21-
.value_size = sizeof(struct pair),
22-
.max_entries = 1000000,
23-
};
18+
struct {
19+
__uint(type, BPF_MAP_TYPE_HASH);
20+
__type(key, long);
21+
__type(value, struct pair);
22+
__uint(max_entries, 1000000);
23+
} my_map SEC(".maps");
2424

2525
/* kprobe is NOT a stable ABI. If kernel internals change this bpf+kprobe
2626
* example will no longer be meaningful

‎samples/bpf/tracex5_kern.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515

1616
#define PROG(F) SEC("kprobe/"__stringify(F)) int bpf_func_##F
1717

18-
struct bpf_map_def SEC("maps") progs = {
19-
.type = BPF_MAP_TYPE_PROG_ARRAY,
20-
.key_size = sizeof(u32),
21-
.value_size = sizeof(u32),
18+
struct {
19+
__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
20+
__uint(key_size, sizeof(u32));
21+
__uint(value_size, sizeof(u32));
2222
#ifdef __mips__
23-
.max_entries = 6000, /* MIPS n64 syscalls start at 5000 */
23+
__uint(max_entries, 6000); /* MIPS n64 syscalls start at 5000 */
2424
#else
25-
.max_entries = 1024,
25+
__uint(max_entries, 1024);
2626
#endif
27-
};
27+
} progs SEC(".maps");
2828

2929
SEC("kprobe/__seccomp_filter")
3030
int bpf_prog1(struct pt_regs *ctx)

‎samples/bpf/tracex6_kern.c‎

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,26 @@
33
#include <uapi/linux/bpf.h>
44
#include <bpf/bpf_helpers.h>
55

6-
struct bpf_map_def SEC("maps") counters = {
7-
.type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
8-
.key_size = sizeof(int),
9-
.value_size = sizeof(u32),
10-
.max_entries = 64,
11-
};
12-
struct bpf_map_def SEC("maps") values = {
13-
.type = BPF_MAP_TYPE_HASH,
14-
.key_size = sizeof(int),
15-
.value_size = sizeof(u64),
16-
.max_entries = 64,
17-
};
18-
struct bpf_map_def SEC("maps") values2 = {
19-
.type = BPF_MAP_TYPE_HASH,
20-
.key_size = sizeof(int),
21-
.value_size = sizeof(struct bpf_perf_event_value),
22-
.max_entries = 64,
23-
};
6+
struct {
7+
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
8+
__uint(key_size, sizeof(int));
9+
__uint(value_size, sizeof(u32));
10+
__uint(max_entries, 64);
11+
} counters SEC(".maps");
12+
13+
struct {
14+
__uint(type, BPF_MAP_TYPE_HASH);
15+
__type(key, int);
16+
__type(value, u64);
17+
__uint(max_entries, 64);
18+
} values SEC(".maps");
19+
20+
struct {
21+
__uint(type, BPF_MAP_TYPE_HASH);
22+
__type(key, int);
23+
__type(value, struct bpf_perf_event_value);
24+
__uint(max_entries, 64);
25+
} values2 SEC(".maps");
2426

2527
SEC("kprobe/htab_map_get_next_key")
2628
int bpf_prog1(struct pt_regs *ctx)

0 commit comments

Comments
 (0)